ocarina-backends-c_tree-nutils.ads 18.9 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--       O C A R I N A . B A C K E N D S . C _ T R E E . N U T I L S        --
--                                                                          --
--                                 S p e c                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2016 ESA & ISAE.      --
10
--                                                                          --
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27
28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29
30
31
32
33
34
35
--                                                                          --
------------------------------------------------------------------------------

with Ocarina.Backends.C_Tree.Nodes; use Ocarina.Backends.C_Tree.Nodes;

package Ocarina.Backends.C_Tree.Nutils is

36
37
   Int0_Val : Value_Id;
   Int1_Val : Value_Id;
38

39
40
   Var_Suffix  : constant String := "_j";
   Initialized : Boolean         := False;
41

42
   Output_Unit_Withing : Boolean := False;
43
44
45
46
47
48
   --  Control flags

   type Browsing_Kind is (By_Source, By_Destination, Default);

   type Token_Type is
     (
yoogx's avatar
yoogx committed
49
50
51
   --   Token name      Token type
   --   Keywords
   Tok_Null,            -- NULL   **** First Keyword
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
      Tok_Break,           -- BREAK
      Tok_Case,            -- CASE
      Tok_Const,           -- CONST
      Tok_Define,          -- DEFINE
      Tok_Default,         -- DEFAULT
      Tok_Endif,           -- ENDIF
      Tok_Else,            -- ELSE
      Tok_Enum,            -- ENUM
      Tok_Extern,          -- EXTERN
      Tok_Struct,          -- STRUCT
      Tok_Union,           -- UNION
      Tok_Exit,            -- EXIT
      Tok_Goto,            -- GOTO
      Tok_If,              -- IF
      Tok_Ifdef,           -- IFDEF
      Tok_Ifndef,          -- IFNDEF
      Tok_Include,         -- INCLUDE
      Tok_Return,          -- RETURN
      Tok_Until,           -- UNTIL
      Tok_For,             -- FOR
      Tok_While,           -- WHILE
73
      Tok_Static,          -- STATIC
74
75
76
      Tok_Switch,          -- SWITCH
      Tok_Typedef,         -- TYPEDEF

77
   --  Graphic Characters
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
      Tok_Xor,             -- ^
      Tok_Sharp,           -- #
      Tok_Mod,             -- %
      Tok_Not,             -- !
      Tok_Left_Brace,      -- {
      Tok_Right_Brace,     -- }
      Tok_Or,              -- ||
      Tok_And,             -- &&
      Tok_Ampersand,       -- &
      Tok_Minus,           -- -
      Tok_Underscore,      -- _
      Tok_Plus,            -- +
      Tok_Asterisk,        -- *
      Tok_Slash,           -- /
      Tok_Quote,           -- "
      Tok_Dot,             -- .
      Tok_Apostrophe,      -- '
      Tok_Left_Paren,      -- (
      Tok_Right_Paren,     -- )
      Tok_Left_Hook,       -- [
      Tok_Right_Hook,      -- ]
      Tok_Comma,           -- ,
      Tok_Less,            -- <
      Tok_Equal,           -- =
      Tok_Equal_Equal,     -- ==
      Tok_Greater,         -- >
      Tok_Not_Equal,       -- /=
      Tok_Greater_Equal,   -- >=
      Tok_Less_Equal,      -- <=
      Tok_Colon,           -- :
      Tok_Greater_Greater, -- >>
      Tok_Less_Less,       -- <<
      Tok_Semicolon,       -- ;
      Tok_Arrow,           -- ->
      Tok_Vertical_Bar);   -- |

   Token_Image : array (Token_Type) of Name_Id;

116
   subtype Keyword_Type is Token_Type range Tok_Null .. Tok_Typedef;
117

118
   type Operator_Type is
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
     (Op_Not,             -- not
      Op_And,             -- and
      Op_In,              -- in
      Op_And_Then,        -- and then
      Op_Or,              -- or
      Op_Or_Else,         -- or else
      Op_And_Symbol,      -- &
      Op_Double_Asterisk, -- **
      Op_Minus,           -- -
      Op_Plus,            -- +
      Op_Asterisk,        -- *
      Op_Slash,           -- /
      Op_Less,            -- <
      Op_Equal,           -- =
      Op_Equal_Equal,     -- ==
      Op_Greater,         -- >
      Op_Not_Equal,       -- !=
      Op_Greater_Equal,   -- >=
      Op_Less_Equal,      -- <=
      Op_Greater_Greater, -- >>
      Op_Less_Less,       -- <<
      Op_Semicolon,       -- ;
      Op_Arrow,           -- ->
      Op_Vertical_Bar,    -- |
      Op_None);           -- No operation

   Operator_Image : array
146
147
   (Operator_Type'Pos (Op_And) ..
        Operator_Type'Pos (Op_Vertical_Bar)) of Name_Id;
148

149
150
   subtype Keyword_Operator is
     Operator_Type range Operator_Type'First .. Op_Or_Else;
151
152
153
154
155
156
157
158

   type Parameter_Id is
     (P_From,
      P_To,
      P_Unused,
      P_Message,
      P_Msg,
      P_Request,
159
      P_Buffer,
160
      P_Status,
161
      P_Entity,
162
      P_Task,
163
      P_Partition,
164
      P_Pkt,
165
      P_Port,
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
      P_Error,
      P_Offset,
      P_Self,
      P_Value);

   PN : array (Parameter_Id) of Name_Id;

   type Member_Id is
     (M_Operation,
      M_Protected_Id,
      M_Failed_Process_Id,
      M_Error_Code,
      M_Port,
      M_Entry,
      M_Entry_Point,
181
182
      M_Kind,
      M_Msg,
183
184
185
186
187
188
189
190
191
192
193
194
195
196
      M_Name,
      M_Base_Priority,
      M_Priority,
      M_Failed_Thread,
      M_Error_Kind,
      M_Time_Capacity,
      M_Stack_Size,
      M_Deadline,
      M_Period,
      M_Flags,
      M_Vars);

   MN : array (Member_Id) of Name_Id;

197
   type Constant_Id is (C_Null);
198
199
200
201
202
203
204

   CONST : array (Constant_Id) of Name_Id;

   type Variable_Id is
     (V_Request,
      V_Ret,
      V_Tattr,
205
      V_Lua_Context,
206
207
208
209
210
211
      V_Next_Period,
      V_Port_Global_To_Entity,
      V_Port_Global_To_Local_Port,
      V_Server_Entity_Table,
      V_Invalid_Server,
      V_Got_Data,
jdelange's avatar
jdelange committed
212
      V_Dev_Id,
213
      V_Entity,
214
      V_Period,
215
      V_Offset,
216
      V_Port,
217
      V_Pkt,
218
219
220
221
222
223
224
225
226
227
      V_Thread,
      V_Error,
      V_Error_Status,
      V_Out,
      V_In,
      V_Message);

   VN : array (Variable_Id) of Name_Id;

   type Function_Id is
228
     (F_Process_Request, F_Register_Source, F_Init_Lane, F_Sizeof, F_Create);
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

   FN : array (Function_Id) of Name_Id;

   type Component_Id is
     (C_Address,
      C_Dispatcher,
      C_From,
      C_Los,
      C_Name,
      C_Pid,
      C_Proc_Id,
      C_Switch,
      C_Conf_Table,
      C_Priority,
      C_Operation);

   CN : array (Component_Id) of Name_Id;

   type Attribute_Id is
     (A_Access,
      A_Class,
      A_First,
      A_Pos,
      A_Range,
      A_Val,
      A_Identity,
      A_Adress,
      A_Last);

   AN : array (Attribute_Id) of Name_Id;

   type Type_Id is
     (T_Char,
      T_Float,
      T_Int,
      T_Uint8_T,
      T_Uint32_T,
      T_Int8_T,
      T_Int16_T,
      T_Int32_T,
      T_Int64_T,
      T_Void,
      T_Unsigned);

   TN : array (Type_Id) of Name_Id;

   function Add_Prefix_To_Name
     (Prefix : String;
277
      Name   : Name_Id) return Name_Id;
278
279
280

   function Add_Suffix_To_Name
     (Suffix : String;
281
      Name   : Name_Id) return Name_Id;
282
283
284

   function Remove_Suffix_From_Name
     (Suffix : String;
285
      Name   : Name_Id) return Name_Id;
286
287
288
289
290
291
292
293
294
295
   --  This function returns a new name without the suffix. If the
   --  suffix does not exist, the returned name is equal to the given
   --  name.

   procedure Append_Node_To_List (E : Node_Id; L : List_Id);
   procedure Insert_After_Node (E : Node_Id; N : Node_Id);
   procedure Insert_Before_Node (E : Node_Id; N : Node_Id; L : List_Id);

   procedure Push_Entity (E : Node_Id);
   procedure Pop_Entity;
296
297
   function Current_Entity return Node_Id;
   function Current_File return Node_Id;
298

299
   function Copy_Node (N : Node_Id) return Node_Id;
300
301
302

   function New_Node
     (Kind : Node_Kind;
303
      From : Node_Id := No_Node) return Node_Id;
304
305
306

   function New_List
     (Kind : Node_Kind;
307
      From : Node_Id := No_Node) return List_Id;
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327

   function Image (T : Token_Type) return String;
   function Image (O : Operator_Type) return String;

   procedure Initialize;
   procedure Reset;

   procedure New_Token (T : Token_Type; I : String := "");

   function Length (L : List_Id) return Natural;

   procedure Remove_Node_From_List (E : Node_Id; L : List_Id);
   --  Remove node N to list L.

   function Is_Empty (L : List_Id) return Boolean;
   pragma Inline (Is_Empty);
   --  Return True when L is empty

   function Make_C_Comment
     (N                 : Name_Id;
328
      Has_Header_Spaces : Boolean := True) return Node_Id;
329
330
331
332
333
334
335
   --  This function does only the fllowing thing: it creates a node
   --  whose name is the full text of the comment. It does not split
   --  the comment into many lines. This is done in the code
   --  generation phase

   function Make_Assignment_Statement
     (Variable_Identifier : Node_Id;
336
      Expression          : Node_Id) return Node_Id;
337
338

   function Make_Defining_Identifier
339
340
     (Name           : Name_Id;
      C_Conversion   : Boolean := True;
341
      Ada_Conversion : Boolean := False;
342
      Pointer        : Boolean := False) return Node_Id;
343
344
345
346

   function Make_Expression
     (Left_Expr  : Node_Id;
      Operator   : Operator_Type := Op_None;
347
      Right_Expr : Node_Id       := No_Node) return Node_Id;
348
349
350
351
352
353

   function Make_For_Statement
     (Defining_Identifier : Node_Id;
      Pre_Cond            : Node_Id;
      Condition           : Node_Id;
      Post_Cond           : Node_Id;
354
      Statements          : List_Id) return Node_Id;
355
356
357

   function Make_Variable_Declaration
     (Defining_Identifier : Node_Id;
358
359
      Used_Type           : Node_Id;
      Is_Static           : Boolean := False) return Node_Id;
360
361
362

   function Make_Member_Declaration
     (Defining_Identifier : Node_Id;
363
      Used_Type           : Node_Id) return Node_Id;
364

365
   function Make_Enum_Aggregate (Members : List_Id) return Node_Id;
366
367
368

   function Make_Struct_Aggregate
     (Defining_Identifier : Node_Id := No_Node;
369
      Members             : List_Id) return Node_Id;
370
371
372

   function Make_Union_Aggregate
     (Defining_Identifier : Node_Id := No_Node;
373
      Members             : List_Id) return Node_Id;
374
375

   function Make_While_Statement
376
377
     (Condition  : Node_Id;
      Statements : List_Id) return Node_Id;
378
379
380

   function Make_Full_Type_Declaration
     (Defining_Identifier : Node_Id;
381
      Type_Definition     : Node_Id) return Node_Id;
382
383
384
385
   --  No_Node as Type_Definition made type declaration without actual
   --  definition (eg. "type X;").

   function Make_If_Statement
386
387
388
     (Condition       : Node_Id;
      Statements      : List_Id;
      Else_Statements : List_Id := No_List) return Node_Id;
389
390
391
392

   function Make_List_Id
     (N1 : Node_Id;
      N2 : Node_Id := No_Node;
393
      N3 : Node_Id := No_Node) return List_Id;
394
395
396

   function Make_Parameter_Specification
     (Defining_Identifier : Node_Id;
397
      Parameter_Type      : Node_Id := No_Node) return Node_Id;
398
399

   function Make_Return_Statement
400
     (Expression : Node_Id := No_Node) return Node_Id;
401
402

   function Make_Call_Profile
403
404
     (Defining_Identifier : Node_Id;
      Parameters          : List_Id := No_List) return Node_Id;
405
406
407
408

   function Make_Function_Implementation
     (Specification : Node_Id;
      Declarations  : List_Id;
409
      Statements    : List_Id) return Node_Id;
410
411

   function Make_Function_Specification
412
413
414
     (Defining_Identifier : Node_Id;
      Parameters          : List_Id := No_List;
      Return_Type         : Node_Id := No_Node) return Node_Id;
415
416
417

   function Make_Type_Attribute
     (Designator : Node_Id;
418
      Attribute  : Attribute_Id) return Node_Id;
419
420
421

   function Make_Type_Conversion
     (Subtype_Mark : Node_Id;
422
      Expression   : Node_Id) return Node_Id;
423

424
   procedure Make_Comment_Header (Header : List_Id);
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
   --  This procedure generates a comment header for the generated
   --  packages.

   function Next_N_Node (N : Node_Id; Num : Natural) return Node_Id;
   --  This function executes Next_Node Num times

   function Message_Comment (M : Name_Id) return Node_Id;
   function Message_Comment (M : String) return Node_Id;
   --  Return a comment message. Used by all the tree
   --  converters

   procedure Set_Activity_Source (N : Node_Id := No_Node);

   procedure Set_Activity_Header (N : Node_Id := No_Node);

   procedure Set_Main_Source (N : Node_Id := No_Node);

   procedure Set_Main_Header (N : Node_Id := No_Node);

   procedure Set_Request_Source (N : Node_Id := No_Node);

   procedure Set_Request_Header (N : Node_Id := No_Node);

448
449
450
451
   function To_C_Name
     (N             : Name_Id;
      Ada_Style     : Boolean := False;
      Keyword_Check : Boolean := True) return Name_Id;
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
   --  Convert N to a valid Ada identifier (no clashing with keywords,
   --  no consecutive '_', no heading '_'...).
   --  If Ada_Style is true, '.' is replaced by "__"

   function Conventional_Base_Name (N : Name_Id) return Name_Id;
   --  Return a lower case name of N

   function Make_Source_File (Identifier : Node_Id) return Node_Id;

   function Make_Header_File (Identifier : Node_Id) return Node_Id;

   procedure Set_Deployment_Header (N : Node_Id := No_Node);

   function Make_Literal (Value : Value_Id) return Node_Id;

467
468
469
   function Make_Define_Statement
     (Defining_Identifier : Node_Id;
      Value               : Node_Id) return Node_Id;
470
471
472

   function Make_Pointer_Type (Used_Type : Node_Id) return Node_Id;

473
   procedure Add_Include (E : Node_Id; Preserve_Case : Boolean := False);
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

   procedure Set_Types_Header (N : Node_Id := No_Node);

   procedure Set_Types_Source (N : Node_Id := No_Node);

   procedure Set_Naming_Source (N : Node_Id := No_Node);

   procedure Set_Subprograms_Source (N : Node_Id := No_Node);

   procedure Set_Subprograms_Header (N : Node_Id := No_Node);

   procedure Set_Marshallers_Source (N : Node_Id := No_Node);

   procedure Set_Marshallers_Header (N : Node_Id := No_Node);

489
   function Make_Variable_Address (Expression : Node_Id) return Node_Id;
490
491
492
493
494
495
496

   function Make_Member_Designator
     (Defining_Identifier : Node_Id;
      Aggregate_Name      : Node_Id;
      Is_Pointer          : Boolean := False) return Node_Id;

   function Make_Switch_Alternative
497
     (Labels     : List_Id;
498
499
500
      Statements : List_Id) return Node_Id;

   function Make_Switch_Statement
501
502
     (Expression   : Node_Id;
      Alternatives : List_Id) return Node_Id;
503
504

   function Make_Macro_Call
505
506
     (Defining_Identifier : Node_Id;
      Parameters          : List_Id := No_List) return Node_Id;
507
508

   procedure POK_Add_Return_Assertion
509
     (Statements      : List_Id;
510
511
512
      Exception_Error : Node_Id := No_Node);

   procedure Handle_Call_Sequence
jdelange's avatar
jdelange committed
513
514
515
516
517
518
519
520
521
     (Caller            : Node_Id;
      Call_Seq          : Node_Id;
      Declarations      : List_Id;
      Statements        : List_Id;
      Containing_Device : Node_Id := No_Node);
   --  The Containing_Device argument is used when the call
   --  sequence is generated for a thread that is within
   --  a device. In that case, the first parameter is ALWAYS
   --  the device id of the containing device.
522
523
524
525
526
527
528

   procedure Set_Deployment_Source (N : Node_Id := No_Node);

   function Make_Array_Declaration
     (Defining_Identifier : Node_Id;
      Array_Size          : Node_Id) return Node_Id;

529
   function Make_Array_Values (Values : List_Id := No_List) return Node_Id;
530

531
   function Make_Extern_Entity_Declaration (Entity : Node_Id) return Node_Id;
532
533
534
535
536
537
538
539

   function Make_Constant_Type (Used_Type : Node_Id) return Node_Id;

   procedure Set_Naming_Header (N : Node_Id := No_Node);

   function Get_C_Default_Value (D : Node_Id) return Node_Id;

   function POK_Make_Function_Call_With_Assert
540
541
     (Function_Name : Node_Id;
      Parameters    : List_Id) return Node_Id;
542

543
544
545
   function Make_Include_Clause
     (Header_Name : Node_Id;
      Local       : Boolean := False) return Node_Id;
546
547

   function Make_Ifdef_Clause
548
549
     (Clause          : Node_Id;
      Negation        : Boolean := False;
550
551
552
553
554
      Then_Statements : List_Id;
      Else_Statements : List_Id) return Node_Id;

   procedure Add_Define_Deployment (E : Node_Id);

555
556
557
   function Make_Array_Value
     (Array_Name : Node_Id;
      Array_Item : Node_Id) return Node_Id;
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

   function Get_Data_Size (Data : Node_Id) return Node_Id;
   --  Returns a node that represent an expression with the size
   --  (in bytes) of a data.

   procedure Add_Return_Variable_In_Parameters (Parameters : List_Id);
   --  Add the name of the return variable in the list.
   --  The primary purpose of this function is to add
   --  the return variable at the end of a function call
   --  for the ARINC653 API.

   procedure POK_Declare_Return_Variable (Declarations : List_Id);
   --  Declare the return variable used for assertions
   --  in function declarations or other list.

   function Get_Inter_Partition_Port_Size (Port : Node_Id) return Node_Id;
   --  Get_Inter_Partition_Port_Size returns the port size of an inter
   --  partition communication. It takes the whole data flow of a connection
   --  and look for virtual bus binding in this flow to see if we need
   --  larger data.

579
   function Get_Inter_Partition_Port_Type (Port : Node_Id) return Node_Id;
580
581
582
583
   --  Get_Inter_Partition_Port_Type returns a Node_Id that contains
   --  all information to generate a C data type linked with
   --  virtual bus layers.

584
   function Make_Doxygen_C_Comment
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
     (Desc              : String;
      Brief             : String  := "";
      Element_Name      : String  := "";
      Is_Struct         : Boolean := False;
      Is_Union          : Boolean := False;
      Is_Enum           : Boolean := False;
      Is_Function       : Boolean := False;
      Is_Variable       : Boolean := False;
      Is_Define         : Boolean := False;
      Is_Typedef        : Boolean := False;
      Is_File           : Boolean := False;
      Is_Namespace      : Boolean := False;
      Is_Package        : Boolean := False;
      Is_Interface      : Boolean := False;
      Has_Header_Spaces : Boolean := True) return Node_Id;
600
601
602

   --  The Make_Doxygen_C_Comment.

603
end Ocarina.Backends.C_Tree.Nutils;