6d1642d488b737f6250b2e6dc5bd197ddaac0d13
[gcc.git] / gcc / ada / bindgen.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- B I N D G E N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with ALI; use ALI;
27 with Binde; use Binde;
28 with Casing; use Casing;
29 with Fname; use Fname;
30 with Gnatvsn; use Gnatvsn;
31 with Hostparm;
32 with Namet; use Namet;
33 with Opt; use Opt;
34 with Osint; use Osint;
35 with Osint.B; use Osint.B;
36 with Output; use Output;
37 with Rident; use Rident;
38 with Table; use Table;
39 with Targparm; use Targparm;
40 with Types; use Types;
41
42 with System.OS_Lib; use System.OS_Lib;
43 with System.WCh_Con; use System.WCh_Con;
44
45 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
46
47 package body Bindgen is
48
49 Statement_Buffer : String (1 .. 1000);
50 -- Buffer used for constructing output statements
51
52 Last : Natural := 0;
53 -- Last location in Statement_Buffer currently set
54
55 With_DECGNAT : Boolean := False;
56 -- Flag which indicates whether the program uses the DECGNAT library
57 -- (presence of the unit DEC).
58
59 With_GNARL : Boolean := False;
60 -- Flag which indicates whether the program uses the GNARL library
61 -- (presence of the unit System.OS_Interface)
62
63 Num_Elab_Calls : Nat := 0;
64 -- Number of generated calls to elaboration routines
65
66 System_Restrictions_Used : Boolean;
67 -- Flag indicating whether the unit System.Restrictions is in the closure
68 -- of the partition. This is set by Check_System_Restrictions_Used, and
69 -- is used to determine whether or not to initialize the restrictions
70 -- information in the body of the binder generated file (we do not want
71 -- to do this unconditionally, since it drags in the System.Restrictions
72 -- unit unconditionally, which is unpleasand, especially for ZFP etc.)
73
74 ----------------------------------
75 -- Interface_State Pragma Table --
76 ----------------------------------
77
78 -- This table assembles the interface state pragma information from
79 -- all the units in the partition. Note that Bcheck has already checked
80 -- that the information is consistent across units. The entries
81 -- in this table are n/u/r/s for not set/user/runtime/system.
82
83 package IS_Pragma_Settings is new Table.Table (
84 Table_Component_Type => Character,
85 Table_Index_Type => Int,
86 Table_Low_Bound => 0,
87 Table_Initial => 100,
88 Table_Increment => 200,
89 Table_Name => "IS_Pragma_Settings");
90
91 -- This table assembles the Priority_Specific_Dispatching pragma
92 -- information from all the units in the partition. Note that Bcheck has
93 -- already checked that the information is consistent across units.
94 -- The entries in this table are the upper case first character of the
95 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
96
97 package PSD_Pragma_Settings is new Table.Table (
98 Table_Component_Type => Character,
99 Table_Index_Type => Int,
100 Table_Low_Bound => 0,
101 Table_Initial => 100,
102 Table_Increment => 200,
103 Table_Name => "PSD_Pragma_Settings");
104
105 ----------------------
106 -- Run-Time Globals --
107 ----------------------
108
109 -- This section documents the global variables that set from the
110 -- generated binder file.
111
112 -- Main_Priority : Integer;
113 -- Time_Slice_Value : Integer;
114 -- Heap_Size : Natural;
115 -- WC_Encoding : Character;
116 -- Locking_Policy : Character;
117 -- Queuing_Policy : Character;
118 -- Task_Dispatching_Policy : Character;
119 -- Priority_Specific_Dispatching : System.Address;
120 -- Num_Specific_Dispatching : Integer;
121 -- Restrictions : System.Address;
122 -- Interrupt_States : System.Address;
123 -- Num_Interrupt_States : Integer;
124 -- Unreserve_All_Interrupts : Integer;
125 -- Exception_Tracebacks : Integer;
126 -- Zero_Cost_Exceptions : Integer;
127 -- Detect_Blocking : Integer;
128 -- Default_Stack_Size : Integer;
129 -- Leap_Seconds_Support : Integer;
130
131 -- Main_Priority is the priority value set by pragma Priority in the main
132 -- program. If no such pragma is present, the value is -1.
133
134 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
135 -- main program, or by the use of a -Tnnn parameter for the binder (if both
136 -- are present, the binder value overrides). The value is in milliseconds.
137 -- A value of zero indicates that time slicing should be suppressed. If no
138 -- pragma is present, and no -T switch was used, the value is -1.
139
140 -- Heap_Size is the heap to use for memory allocations set by use of a
141 -- -Hnn parameter for the binder or by the GNAT$NO_MALLOC_64 logical.
142 -- Valid values are 32 and 64. This switch is only available on VMS.
143
144 -- WC_Encoding shows the wide character encoding method used for the main
145 -- program. This is one of the encoding letters defined in
146 -- System.WCh_Con.WC_Encoding_Letters.
147
148 -- Locking_Policy is a space if no locking policy was specified for the
149 -- partition. If a locking policy was specified, the value is the upper
150 -- case first character of the locking policy name, for example, 'C' for
151 -- Ceiling_Locking.
152
153 -- Queuing_Policy is a space if no queuing policy was specified for the
154 -- partition. If a queuing policy was specified, the value is the upper
155 -- case first character of the queuing policy name for example, 'F' for
156 -- FIFO_Queuing.
157
158 -- Task_Dispatching_Policy is a space if no task dispatching policy was
159 -- specified for the partition. If a task dispatching policy was specified,
160 -- the value is the upper case first character of the policy name, e.g. 'F'
161 -- for FIFO_Within_Priorities.
162
163 -- Priority_Specific_Dispatching is the address of a string used to store
164 -- the task dispatching policy specified for the different priorities in
165 -- the partition. The length of this string is determined by the last
166 -- priority for which such a pragma applies (the string will be a null
167 -- string if no specific dispatching policies were used). If pragma were
168 -- present, the entries apply to the priorities in sequence from the first
169 -- priority. The value stored is the upper case first character of the
170 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
171 -- for those priority ranges not specified.
172
173 -- Num_Specific_Dispatching is the length of the
174 -- Priority_Specific_Dispatching string. It will be set to zero if no
175 -- Priority_Specific_Dispatching pragmas are present.
176
177 -- Restrictions is the address of a null-terminated string specifying the
178 -- restrictions information for the partition. The format is identical to
179 -- that of the parameter string found on R lines in ali files (see Lib.Writ
180 -- spec in lib-writ.ads for full details). The difference is that in this
181 -- context the values are the cumulative ones for the entire partition.
182
183 -- Interrupt_States is the address of a string used to specify the
184 -- cumulative results of Interrupt_State pragmas used in the partition.
185 -- The length of this string is determined by the last interrupt for which
186 -- such a pragma is given (the string will be a null string if no pragmas
187 -- were used). If pragma were present the entries apply to the interrupts
188 -- in sequence from the first interrupt, and are set to one of four
189 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
190 -- time, 's' for system, see description of Interrupt_State pragma for
191 -- further details.
192
193 -- Num_Interrupt_States is the length of the Interrupt_States string. It
194 -- will be set to zero if no Interrupt_State pragmas are present.
195
196 -- Unreserve_All_Interrupts is set to one if at least one unit in the
197 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
198
199 -- Exception_Tracebacks is set to one if the -E parameter was present
200 -- in the bind and to zero otherwise. Note that on some targets exception
201 -- tracebacks are provided by default, so a value of zero for this
202 -- parameter does not necessarily mean no trace backs are available.
203
204 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
205 -- this partition, and to zero if longjmp/setjmp exceptions are used.
206
207 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
208 -- not. A value of zero indicates that the pragma is not present, while a
209 -- value of 1 signals its presence in the partition.
210
211 -- Default_Stack_Size is the default stack size used when creating an Ada
212 -- task with no explicit Storage_Size clause.
213
214 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
215 -- disabled. A value of zero indicates that leap seconds are turned "off",
216 -- while a value of one signifies "on" status.
217
218 -----------------------
219 -- Local Subprograms --
220 -----------------------
221
222 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
223 -- Convenient shorthand used throughout
224
225 procedure Check_System_Restrictions_Used;
226 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
227 -- System.Restrictions is present in the partition, otherwise False).
228
229 procedure Gen_Adainit_Ada;
230 -- Generates the Adainit procedure (Ada code case)
231
232 procedure Gen_Adainit_C;
233 -- Generates the Adainit procedure (C code case)
234
235 procedure Gen_Adafinal_Ada;
236 -- Generate the Adafinal procedure (Ada code case)
237
238 procedure Gen_Adafinal_C;
239 -- Generate the Adafinal procedure (C code case)
240
241 procedure Gen_Elab_Calls_Ada;
242 -- Generate sequence of elaboration calls (Ada code case)
243
244 procedure Gen_Elab_Calls_C;
245 -- Generate sequence of elaboration calls (C code case)
246
247 procedure Gen_Elab_Order_Ada;
248 -- Generate comments showing elaboration order chosen (Ada case)
249
250 procedure Gen_Elab_Order_C;
251 -- Generate comments showing elaboration order chosen (C case)
252
253 procedure Gen_Elab_Defs_C;
254 -- Generate sequence of definitions for elaboration routines (C code case)
255
256 procedure Gen_Main_Ada;
257 -- Generate procedure main (Ada code case)
258
259 procedure Gen_Main_C;
260 -- Generate main() procedure (C code case)
261
262 procedure Gen_Object_Files_Options;
263 -- Output comments containing a list of the full names of the object
264 -- files to be linked and the list of linker options supplied by
265 -- Linker_Options pragmas in the source. (C and Ada code case)
266
267 procedure Gen_Output_File_Ada (Filename : String);
268 -- Generate output file (Ada code case)
269
270 procedure Gen_Output_File_C (Filename : String);
271 -- Generate output file (C code case)
272
273 procedure Gen_Restrictions_Ada;
274 -- Generate initialization of restrictions variable (Ada code case)
275
276 procedure Gen_Restrictions_C;
277 -- Generate initialization of restrictions variable (C code case)
278
279 procedure Gen_Versions_Ada;
280 -- Output series of definitions for unit versions (Ada code case)
281
282 procedure Gen_Versions_C;
283 -- Output series of definitions for unit versions (C code case)
284
285 function Get_Ada_Main_Name return String;
286 -- This function is used in the Ada main output case to compute a usable
287 -- name for the generated main program. The normal main program name is
288 -- Ada_Main, but this won't work if the user has a unit with this name.
289 -- This function tries Ada_Main first, and if there is such a clash, then
290 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
291
292 function Get_Main_Unit_Name (S : String) return String;
293 -- Return the main unit name corresponding to S by replacing '.' with '_'
294
295 function Get_Main_Name return String;
296 -- This function is used in the Ada main output case to compute the
297 -- correct external main program. It is "main" by default, unless the
298 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
299 -- is the name of the Ada main name without the "_ada". This default
300 -- can be overridden explicitly using the -Mname binder switch.
301
302 function Get_WC_Encoding return Character;
303 -- Return wide character encoding method to set as WC_Encoding in output.
304 -- If -W has been used, returns the specified encoding, otherwise returns
305 -- the encoding method used for the main program source. If there is no
306 -- main program source (-z switch used), returns brackets ('b').
307
308 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
309 -- Compare linker options, when sorting, first according to
310 -- Is_Internal_File (internal files come later) and then by
311 -- elaboration order position (latest to earliest).
312
313 procedure Move_Linker_Option (From : Natural; To : Natural);
314 -- Move routine for sorting linker options
315
316 procedure Resolve_Binder_Options;
317 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
318 -- since it tests for a package named "dec" which might cause a conflict
319 -- on non-VMS systems.
320
321 procedure Set_Char (C : Character);
322 -- Set given character in Statement_Buffer at the Last + 1 position
323 -- and increment Last by one to reflect the stored character.
324
325 procedure Set_Int (N : Int);
326 -- Set given value in decimal in Statement_Buffer with no spaces
327 -- starting at the Last + 1 position, and updating Last past the value.
328 -- A minus sign is output for a negative value.
329
330 procedure Set_Boolean (B : Boolean);
331 -- Set given boolean value in Statement_Buffer at the Last + 1 position
332 -- and update Last past the value.
333
334 procedure Set_IS_Pragma_Table;
335 -- Initializes contents of IS_Pragma_Settings table from ALI table
336
337 procedure Set_Main_Program_Name;
338 -- Given the main program name in Name_Buffer (length in Name_Len)
339 -- generate the name of the routine to be used in the call. The name
340 -- is generated starting at Last + 1, and Last is updated past it.
341
342 procedure Set_Name_Buffer;
343 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
344
345 procedure Set_PSD_Pragma_Table;
346 -- Initializes contents of PSD_Pragma_Settings table from ALI table
347
348 procedure Set_String (S : String);
349 -- Sets characters of given string in Statement_Buffer, starting at the
350 -- Last + 1 position, and updating last past the string value.
351
352 procedure Set_String_Replace (S : String);
353 -- Replaces the last S'Length characters in the Statement_Buffer with
354 -- the characters of S. The caller must ensure that these characters do
355 -- in fact exist in the Statement_Buffer.
356
357 procedure Set_Unit_Name;
358 -- Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
359 -- starting at the Last + 1 position, and updating last past the value.
360 -- changing periods to double underscores, and updating Last appropriately.
361
362 procedure Set_Unit_Number (U : Unit_Id);
363 -- Sets unit number (first unit is 1, leading zeroes output to line
364 -- up all output unit numbers nicely as required by the value, and
365 -- by the total number of units.
366
367 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
368 -- For C code case, write C & Common, for Ada case write Ada & Common
369 -- to current binder output file using Write_Binder_Info.
370
371 procedure Write_Statement_Buffer;
372 -- Write out contents of statement buffer up to Last, and reset Last to 0
373
374 procedure Write_Statement_Buffer (S : String);
375 -- First writes its argument (using Set_String (S)), then writes out the
376 -- contents of statement buffer up to Last, and reset Last to 0
377
378 ------------------------------------
379 -- Check_System_Restrictions_Used --
380 ------------------------------------
381
382 procedure Check_System_Restrictions_Used is
383 begin
384 for J in Units.First .. Units.Last loop
385 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
386 System_Restrictions_Used := True;
387 return;
388 end if;
389 end loop;
390
391 System_Restrictions_Used := False;
392 end Check_System_Restrictions_Used;
393
394 ----------------------
395 -- Gen_Adafinal_Ada --
396 ----------------------
397
398 procedure Gen_Adafinal_Ada is
399 begin
400 WBI ("");
401 WBI (" procedure " & Ada_Final_Name.all & " is");
402 WBI (" begin");
403
404 -- If compiling for the JVM, we directly call Adafinal because
405 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
406
407 if VM_Target /= No_VM then
408 WBI (" System.Standard_Library.Adafinal;");
409
410 -- If there is no finalization, there is nothing to do
411
412 elsif Cumulative_Restrictions.Set (No_Finalization) then
413 WBI (" null;");
414 else
415 WBI (" Do_Finalize;");
416 end if;
417
418 WBI (" end " & Ada_Final_Name.all & ";");
419 end Gen_Adafinal_Ada;
420
421 --------------------
422 -- Gen_Adafinal_C --
423 --------------------
424
425 procedure Gen_Adafinal_C is
426 begin
427 WBI ("void " & Ada_Final_Name.all & " (void) {");
428 WBI (" system__standard_library__adafinal ();");
429 WBI ("}");
430 WBI ("");
431 end Gen_Adafinal_C;
432
433 ---------------------
434 -- Gen_Adainit_Ada --
435 ---------------------
436
437 procedure Gen_Adainit_Ada is
438 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
439
440 begin
441 WBI (" procedure " & Ada_Init_Name.all & " is");
442
443 -- Generate externals for elaboration entities
444
445 for E in Elab_Order.First .. Elab_Order.Last loop
446 declare
447 Unum : constant Unit_Id := Elab_Order.Table (E);
448 U : Unit_Record renames Units.Table (Unum);
449
450 begin
451 -- Check for Elab_Entity to be set for this unit
452
453 if U.Set_Elab_Entity
454
455 -- Don't generate reference for stand alone library
456
457 and then not U.SAL_Interface
458
459 -- Don't generate reference for predefined file in No_Run_Time
460 -- mode, since we don't include the object files in this case
461
462 and then not
463 (No_Run_Time_Mode
464 and then Is_Predefined_File_Name (U.Sfile))
465 then
466 Set_String (" ");
467 Set_String ("E");
468 Set_Unit_Number (Unum);
469
470 case VM_Target is
471 when No_VM | JVM_Target =>
472 Set_String (" : Boolean; pragma Import (Ada, ");
473 when CLI_Target =>
474 Set_String (" : Boolean; pragma Import (CIL, ");
475 end case;
476
477 Set_String ("E");
478 Set_Unit_Number (Unum);
479 Set_String (", """);
480 Get_Name_String (U.Uname);
481
482 -- In the case of JGNAT we need to emit an Import name
483 -- that includes the class name (using '$' separators
484 -- in the case of a child unit name).
485
486 if VM_Target /= No_VM then
487 for J in 1 .. Name_Len - 2 loop
488 if VM_Target = CLI_Target
489 or else Name_Buffer (J) /= '.'
490 then
491 Set_Char (Name_Buffer (J));
492 else
493 Set_String ("$");
494 end if;
495 end loop;
496
497 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
498 Set_String (".");
499 else
500 Set_String ("_pkg.");
501 end if;
502
503 -- If the unit name is very long, then split the
504 -- Import link name across lines using "&" (occurs
505 -- in some C2 tests).
506
507 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
508 Set_String (""" &");
509 Write_Statement_Buffer;
510 Set_String (" """);
511 end if;
512 end if;
513
514 Set_Unit_Name;
515 Set_String ("_E"");");
516 Write_Statement_Buffer;
517 end if;
518 end;
519 end loop;
520
521 Write_Statement_Buffer;
522
523 -- If the standard library is suppressed, then the only global variable
524 -- that might be needed (by the Ravenscar profile) is the priority of
525 -- the environment.
526
527 if Suppress_Standard_Library_On_Target then
528 if Main_Priority /= No_Main_Priority then
529 WBI (" Main_Priority : Integer;");
530 WBI (" pragma Import (C, Main_Priority," &
531 " ""__gl_main_priority"");");
532 WBI ("");
533 end if;
534
535 WBI (" begin");
536
537 if Main_Priority /= No_Main_Priority then
538 Set_String (" Main_Priority := ");
539 Set_Int (Main_Priority);
540 Set_Char (';');
541 Write_Statement_Buffer;
542
543 else
544 WBI (" null;");
545 end if;
546
547 -- Normal case (standard library not suppressed). Set all global values
548 -- used by the run time.
549
550 else
551 WBI (" Main_Priority : Integer;");
552 WBI (" pragma Import (C, Main_Priority, " &
553 """__gl_main_priority"");");
554 WBI (" Time_Slice_Value : Integer;");
555 WBI (" pragma Import (C, Time_Slice_Value, " &
556 """__gl_time_slice_val"");");
557 WBI (" WC_Encoding : Character;");
558 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
559 WBI (" Locking_Policy : Character;");
560 WBI (" pragma Import (C, Locking_Policy, " &
561 """__gl_locking_policy"");");
562 WBI (" Queuing_Policy : Character;");
563 WBI (" pragma Import (C, Queuing_Policy, " &
564 """__gl_queuing_policy"");");
565 WBI (" Task_Dispatching_Policy : Character;");
566 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
567 """__gl_task_dispatching_policy"");");
568 WBI (" Priority_Specific_Dispatching : System.Address;");
569 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
570 """__gl_priority_specific_dispatching"");");
571 WBI (" Num_Specific_Dispatching : Integer;");
572 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
573 """__gl_num_specific_dispatching"");");
574
575 WBI (" Interrupt_States : System.Address;");
576 WBI (" pragma Import (C, Interrupt_States, " &
577 """__gl_interrupt_states"");");
578 WBI (" Num_Interrupt_States : Integer;");
579 WBI (" pragma Import (C, Num_Interrupt_States, " &
580 """__gl_num_interrupt_states"");");
581 WBI (" Unreserve_All_Interrupts : Integer;");
582 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
583 """__gl_unreserve_all_interrupts"");");
584
585 if Exception_Tracebacks then
586 WBI (" Exception_Tracebacks : Integer;");
587 WBI (" pragma Import (C, Exception_Tracebacks, " &
588 """__gl_exception_tracebacks"");");
589 end if;
590
591 WBI (" Zero_Cost_Exceptions : Integer;");
592 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
593 """__gl_zero_cost_exceptions"");");
594 WBI (" Detect_Blocking : Integer;");
595 WBI (" pragma Import (C, Detect_Blocking, " &
596 """__gl_detect_blocking"");");
597 WBI (" Default_Stack_Size : Integer;");
598 WBI (" pragma Import (C, Default_Stack_Size, " &
599 """__gl_default_stack_size"");");
600 WBI (" Leap_Seconds_Support : Integer;");
601 WBI (" pragma Import (C, Leap_Seconds_Support, " &
602 """__gl_leap_seconds_support"");");
603
604 -- Import entry point for elaboration time signal handler
605 -- installation, and indication of if it's been called previously.
606
607 WBI ("");
608 WBI (" procedure Install_Handler;");
609 WBI (" pragma Import (C, Install_Handler, " &
610 """__gnat_install_handler"");");
611 WBI ("");
612 WBI (" Handler_Installed : Integer;");
613 WBI (" pragma Import (C, Handler_Installed, " &
614 """__gnat_handler_installed"");");
615
616 -- Import entry point for environment feature enable/disable
617 -- routine, and indication that it's been called previously.
618
619 if OpenVMS_On_Target then
620 WBI ("");
621 WBI (" procedure Set_Features;");
622 WBI (" pragma Import (C, Set_Features, " &
623 """__gnat_set_features"");");
624 WBI ("");
625 WBI (" Features_Set : Integer;");
626 WBI (" pragma Import (C, Features_Set, " &
627 """__gnat_features_set"");");
628
629 if Opt.Heap_Size /= 0 then
630 WBI ("");
631 WBI (" Heap_Size : Integer;");
632 WBI (" pragma Import (C, Heap_Size, " &
633 """__gl_heap_size"");");
634
635 Write_Statement_Buffer;
636 end if;
637 end if;
638
639 -- Initialize stack limit variable of the environment task if the
640 -- stack check method is stack limit and stack check is enabled.
641
642 if Stack_Check_Limits_On_Target
643 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
644 then
645 WBI ("");
646 WBI (" procedure Initialize_Stack_Limit;");
647 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
648 """__gnat_initialize_stack_limit"");");
649 end if;
650
651 -- Special processing when main program is CIL function/procedure
652
653 if VM_Target = CLI_Target
654 and then Bind_Main_Program
655 and then not No_Main_Subprogram
656 then
657 WBI ("");
658
659 -- Function case, use Set_Exit_Status to report the returned
660 -- status code, since that is the only mechanism available.
661
662 if ALIs.Table (ALIs.First).Main_Program = Func then
663 WBI (" Result : Integer;");
664 WBI (" procedure Set_Exit_Status (Code : Integer);");
665 WBI (" pragma Import (C, Set_Exit_Status, " &
666 """__gnat_set_exit_status"");");
667 WBI ("");
668 WBI (" function Ada_Main_Program return Integer;");
669
670 -- Procedure case
671
672 else
673 WBI (" procedure Ada_Main_Program;");
674 end if;
675
676 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
677 Name_Len := Name_Len - 2;
678 WBI (" pragma Import (CIL, Ada_Main_Program, """
679 & Name_Buffer (1 .. Name_Len) & "."
680 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
681 end if;
682
683 WBI (" begin");
684
685 Set_String (" Main_Priority := ");
686 Set_Int (Main_Priority);
687 Set_Char (';');
688 Write_Statement_Buffer;
689
690 Set_String (" Time_Slice_Value := ");
691
692 if Task_Dispatching_Policy_Specified = 'F'
693 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
694 then
695 Set_Int (0);
696 else
697 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
698 end if;
699
700 Set_Char (';');
701 Write_Statement_Buffer;
702
703 Set_String (" WC_Encoding := '");
704 Set_Char (Get_WC_Encoding);
705
706 Set_String ("';");
707 Write_Statement_Buffer;
708
709 Set_String (" Locking_Policy := '");
710 Set_Char (Locking_Policy_Specified);
711 Set_String ("';");
712 Write_Statement_Buffer;
713
714 Set_String (" Queuing_Policy := '");
715 Set_Char (Queuing_Policy_Specified);
716 Set_String ("';");
717 Write_Statement_Buffer;
718
719 Set_String (" Task_Dispatching_Policy := '");
720 Set_Char (Task_Dispatching_Policy_Specified);
721 Set_String ("';");
722 Write_Statement_Buffer;
723
724 Gen_Restrictions_Ada;
725
726 WBI (" Priority_Specific_Dispatching :=");
727 WBI (" Local_Priority_Specific_Dispatching'Address;");
728
729 Set_String (" Num_Specific_Dispatching := ");
730 Set_Int (PSD_Pragma_Settings.Last + 1);
731 Set_Char (';');
732 Write_Statement_Buffer;
733
734 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
735
736 Set_String (" Num_Interrupt_States := ");
737 Set_Int (IS_Pragma_Settings.Last + 1);
738 Set_Char (';');
739 Write_Statement_Buffer;
740
741 Set_String (" Unreserve_All_Interrupts := ");
742
743 if Unreserve_All_Interrupts_Specified then
744 Set_String ("1");
745 else
746 Set_String ("0");
747 end if;
748
749 Set_Char (';');
750 Write_Statement_Buffer;
751
752 if Exception_Tracebacks then
753 WBI (" Exception_Tracebacks := 1;");
754 end if;
755
756 Set_String (" Zero_Cost_Exceptions := ");
757
758 if Zero_Cost_Exceptions_Specified then
759 Set_String ("1");
760 else
761 Set_String ("0");
762 end if;
763
764 Set_String (";");
765 Write_Statement_Buffer;
766
767 Set_String (" Detect_Blocking := ");
768
769 if Detect_Blocking then
770 Set_Int (1);
771 else
772 Set_Int (0);
773 end if;
774
775 Set_String (";");
776 Write_Statement_Buffer;
777
778 Set_String (" Default_Stack_Size := ");
779 Set_Int (Default_Stack_Size);
780 Set_String (";");
781 Write_Statement_Buffer;
782
783 Set_String (" Leap_Seconds_Support := ");
784
785 if Leap_Seconds_Support then
786 Set_Int (1);
787 else
788 Set_Int (0);
789 end if;
790
791 Set_String (";");
792 Write_Statement_Buffer;
793
794 -- Generate call to Install_Handler
795 -- In .NET, when binding with -z, we don't install the signal
796 -- handler to let the caller handle the last exception handler.
797
798 if VM_Target /= CLI_Target
799 or else Bind_Main_Program
800 then
801 WBI ("");
802 WBI (" if Handler_Installed = 0 then");
803 WBI (" Install_Handler;");
804 WBI (" end if;");
805 end if;
806
807 -- Generate call to Set_Features
808
809 if OpenVMS_On_Target then
810 WBI ("");
811 WBI (" if Features_Set = 0 then");
812 WBI (" Set_Features;");
813 WBI (" end if;");
814
815 -- Features_Set may twiddle the heap size according to a logical
816 -- name, but the binder switch must override.
817
818 if Opt.Heap_Size /= 0 then
819 Set_String (" Heap_Size := ");
820 Set_Int (Opt.Heap_Size);
821 Set_Char (';');
822 Write_Statement_Buffer;
823 end if;
824 end if;
825 end if;
826
827 -- Generate call to set Initialize_Scalar values if active
828
829 if Initialize_Scalars_Used then
830 WBI ("");
831 Set_String (" System.Scalar_Values.Initialize ('");
832 Set_Char (Initialize_Scalars_Mode1);
833 Set_String ("', '");
834 Set_Char (Initialize_Scalars_Mode2);
835 Set_String ("');");
836 Write_Statement_Buffer;
837 end if;
838
839 -- Generate assignment of default secondary stack size if set
840
841 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
842 WBI ("");
843 Set_String (" System.Secondary_Stack.");
844 Set_String ("Default_Secondary_Stack_Size := ");
845 Set_Int (Opt.Default_Sec_Stack_Size);
846 Set_Char (';');
847 Write_Statement_Buffer;
848 end if;
849
850 -- Initialize stack limit variable of the environment task if the
851 -- stack check method is stack limit and stack check is enabled.
852
853 if Stack_Check_Limits_On_Target
854 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
855 then
856 WBI ("");
857 WBI (" Initialize_Stack_Limit;");
858 end if;
859
860 -- Generate elaboration calls
861
862 WBI ("");
863 Gen_Elab_Calls_Ada;
864
865 -- Case of main program is CIL function or procedure
866
867 if VM_Target = CLI_Target
868 and then Bind_Main_Program
869 and then not No_Main_Subprogram
870 then
871 -- For function case, use Set_Exit_Status to set result
872
873 if ALIs.Table (ALIs.First).Main_Program = Func then
874 WBI (" Result := Ada_Main_Program;");
875 WBI (" Set_Exit_Status (Result);");
876
877 -- Procedure case
878
879 else
880 WBI (" Ada_Main_Program;");
881 end if;
882 end if;
883
884 WBI (" end " & Ada_Init_Name.all & ";");
885 end Gen_Adainit_Ada;
886
887 -------------------
888 -- Gen_Adainit_C --
889 --------------------
890
891 procedure Gen_Adainit_C is
892 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
893
894 begin
895 WBI ("void " & Ada_Init_Name.all & " (void)");
896 WBI ("{");
897
898 -- Generate externals for elaboration entities
899
900 for E in Elab_Order.First .. Elab_Order.Last loop
901 declare
902 Unum : constant Unit_Id := Elab_Order.Table (E);
903 U : Unit_Record renames Units.Table (Unum);
904
905 begin
906 -- Check for Elab entity to be set for this unit
907
908 if U.Set_Elab_Entity
909
910 -- Don't generate reference for stand alone library
911
912 and then not U.SAL_Interface
913
914 -- Don't generate reference for predefined file in No_Run_Time
915 -- mode, since we don't include the object files in this case
916
917 and then not
918 (No_Run_Time_Mode
919 and then Is_Predefined_File_Name (U.Sfile))
920 then
921 Set_String (" extern char ");
922 Get_Name_String (U.Uname);
923 Set_Unit_Name;
924 Set_String ("_E;");
925 Write_Statement_Buffer;
926 end if;
927 end;
928 end loop;
929
930 Write_Statement_Buffer;
931
932 -- Standard library suppressed
933
934 if Suppress_Standard_Library_On_Target then
935
936 -- Case of High_Integrity_Mode mode. Set __gl_main_priority if needed
937 -- for the Ravenscar profile.
938
939 if Main_Priority /= No_Main_Priority then
940 WBI (" extern int __gl_main_priority;");
941 Set_String (" __gl_main_priority = ");
942 Set_Int (Main_Priority);
943 Set_Char (';');
944 Write_Statement_Buffer;
945 end if;
946
947 -- Normal case (standard library not suppressed)
948
949 else
950 -- Generate definition for interrupt states string
951
952 Set_String (" static const char *local_interrupt_states = """);
953
954 for J in 0 .. IS_Pragma_Settings.Last loop
955 Set_Char (IS_Pragma_Settings.Table (J));
956 end loop;
957
958 Set_String (""";");
959 Write_Statement_Buffer;
960
961 -- Generate definition for priority specific dispatching string
962
963 Set_String
964 (" static const char *local_priority_specific_dispatching = """);
965
966 for J in 0 .. PSD_Pragma_Settings.Last loop
967 Set_Char (PSD_Pragma_Settings.Table (J));
968 end loop;
969
970 Set_String (""";");
971 Write_Statement_Buffer;
972
973 -- Generate declaration for secondary stack default if needed
974
975 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
976 WBI (" extern int system__secondary_stack__" &
977 "default_secondary_stack_size;");
978 end if;
979
980 WBI ("");
981
982 -- Code for normal case (standard library not suppressed)
983
984 -- We call the routine from inside adainit() because this works for
985 -- both programs with and without binder generated "main" functions.
986
987 WBI (" extern int __gl_main_priority;");
988 Set_String (" __gl_main_priority = ");
989 Set_Int (Main_Priority);
990 Set_Char (';');
991 Write_Statement_Buffer;
992
993 WBI (" extern int __gl_time_slice_val;");
994 Set_String (" __gl_time_slice_val = ");
995
996 if Task_Dispatching_Policy = 'F'
997 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
998 then
999 Set_Int (0);
1000 else
1001 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
1002 end if;
1003
1004 Set_Char (';');
1005 Write_Statement_Buffer;
1006
1007 WBI (" extern char __gl_wc_encoding;");
1008 Set_String (" __gl_wc_encoding = '");
1009 Set_Char (Get_WC_Encoding);
1010
1011 Set_String ("';");
1012 Write_Statement_Buffer;
1013
1014 WBI (" extern char __gl_locking_policy;");
1015 Set_String (" __gl_locking_policy = '");
1016 Set_Char (Locking_Policy_Specified);
1017 Set_String ("';");
1018 Write_Statement_Buffer;
1019
1020 WBI (" extern char __gl_queuing_policy;");
1021 Set_String (" __gl_queuing_policy = '");
1022 Set_Char (Queuing_Policy_Specified);
1023 Set_String ("';");
1024 Write_Statement_Buffer;
1025
1026 WBI (" extern char __gl_task_dispatching_policy;");
1027 Set_String (" __gl_task_dispatching_policy = '");
1028 Set_Char (Task_Dispatching_Policy_Specified);
1029 Set_String ("';");
1030 Write_Statement_Buffer;
1031
1032 Gen_Restrictions_C;
1033
1034 WBI (" extern const void *__gl_interrupt_states;");
1035 WBI (" __gl_interrupt_states = local_interrupt_states;");
1036
1037 WBI (" extern int __gl_num_interrupt_states;");
1038 Set_String (" __gl_num_interrupt_states = ");
1039 Set_Int (IS_Pragma_Settings.Last + 1);
1040 Set_String (";");
1041 Write_Statement_Buffer;
1042
1043 WBI (" extern const void *__gl_priority_specific_dispatching;");
1044 WBI (" __gl_priority_specific_dispatching =" &
1045 " local_priority_specific_dispatching;");
1046
1047 WBI (" extern int __gl_num_specific_dispatching;");
1048 Set_String (" __gl_num_specific_dispatching = ");
1049 Set_Int (PSD_Pragma_Settings.Last + 1);
1050 Set_String (";");
1051 Write_Statement_Buffer;
1052
1053 WBI (" extern int __gl_unreserve_all_interrupts;");
1054 Set_String (" __gl_unreserve_all_interrupts = ");
1055 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
1056 Set_String (";");
1057 Write_Statement_Buffer;
1058
1059 if Exception_Tracebacks then
1060 WBI (" extern int __gl_exception_tracebacks;");
1061 WBI (" __gl_exception_tracebacks = 1;");
1062 end if;
1063
1064 WBI (" extern int __gl_zero_cost_exceptions;");
1065 Set_String (" __gl_zero_cost_exceptions = ");
1066 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
1067 Set_String (";");
1068 Write_Statement_Buffer;
1069
1070 WBI (" extern int __gl_detect_blocking;");
1071 Set_String (" __gl_detect_blocking = ");
1072
1073 if Detect_Blocking then
1074 Set_Int (1);
1075 else
1076 Set_Int (0);
1077 end if;
1078
1079 Set_String (";");
1080 Write_Statement_Buffer;
1081
1082 WBI (" extern int __gl_default_stack_size;");
1083 Set_String (" __gl_default_stack_size = ");
1084 Set_Int (Default_Stack_Size);
1085 Set_String (";");
1086 Write_Statement_Buffer;
1087
1088 WBI (" extern int __gl_leap_seconds_support;");
1089 Set_String (" __gl_leap_seconds_support = ");
1090
1091 if Leap_Seconds_Support then
1092 Set_Int (1);
1093 else
1094 Set_Int (0);
1095 end if;
1096
1097 Set_String (";");
1098 Write_Statement_Buffer;
1099
1100 WBI ("");
1101
1102 -- Install elaboration time signal handler
1103
1104 WBI (" if (__gnat_handler_installed == 0)");
1105 WBI (" {");
1106 WBI (" __gnat_install_handler ();");
1107 WBI (" }");
1108
1109 -- Call feature enable/disable routine
1110
1111 if OpenVMS_On_Target then
1112 WBI (" if (__gnat_features_set == 0)");
1113 WBI (" {");
1114 WBI (" __gnat_set_features ();");
1115 WBI (" }");
1116 end if;
1117 end if;
1118
1119 -- Initialize stack limit for the environment task if the stack
1120 -- check method is stack limit and stack check is enabled.
1121
1122 if Stack_Check_Limits_On_Target
1123 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1124 then
1125 WBI ("");
1126 WBI (" __gnat_initialize_stack_limit ();");
1127 end if;
1128
1129 -- Generate call to set Initialize_Scalar values if needed
1130
1131 if Initialize_Scalars_Used then
1132 WBI ("");
1133 Set_String (" system__scalar_values__initialize('");
1134 Set_Char (Initialize_Scalars_Mode1);
1135 Set_String ("', '");
1136 Set_Char (Initialize_Scalars_Mode2);
1137 Set_String ("');");
1138 Write_Statement_Buffer;
1139 end if;
1140
1141 -- Generate assignment of default secondary stack size if set
1142
1143 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1144 WBI ("");
1145 Set_String (" system__secondary_stack__");
1146 Set_String ("default_secondary_stack_size = ");
1147 Set_Int (Opt.Default_Sec_Stack_Size);
1148 Set_Char (';');
1149 Write_Statement_Buffer;
1150 end if;
1151
1152 -- Generate elaboration calls
1153
1154 WBI ("");
1155 Gen_Elab_Calls_C;
1156 WBI ("}");
1157 end Gen_Adainit_C;
1158
1159 ------------------------
1160 -- Gen_Elab_Calls_Ada --
1161 ------------------------
1162
1163 procedure Gen_Elab_Calls_Ada is
1164 begin
1165 for E in Elab_Order.First .. Elab_Order.Last loop
1166 declare
1167 Unum : constant Unit_Id := Elab_Order.Table (E);
1168 U : Unit_Record renames Units.Table (Unum);
1169
1170 Unum_Spec : Unit_Id;
1171 -- This is the unit number of the spec that corresponds to
1172 -- this entry. It is the same as Unum except when the body
1173 -- and spec are different and we are currently processing
1174 -- the body, in which case it is the spec (Unum + 1).
1175
1176 begin
1177 if U.Utype = Is_Body then
1178 Unum_Spec := Unum + 1;
1179 else
1180 Unum_Spec := Unum;
1181 end if;
1182
1183 -- Nothing to do if predefined unit in no run time mode
1184
1185 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1186 null;
1187
1188 -- Case of no elaboration code
1189
1190 elsif U.No_Elab then
1191
1192 -- The only case in which we have to do something is if
1193 -- this is a body, with a separate spec, where the separate
1194 -- spec has an elaboration entity defined.
1195
1196 -- In that case, this is where we set the elaboration entity
1197 -- to True, we do not need to test if this has already been
1198 -- done, since it is quicker to set the flag than to test it.
1199
1200 if not U.SAL_Interface and then U.Utype = Is_Body
1201 and then Units.Table (Unum_Spec).Set_Elab_Entity
1202 then
1203 Set_String (" E");
1204 Set_Unit_Number (Unum_Spec);
1205 Set_String (" := True;");
1206 Write_Statement_Buffer;
1207 end if;
1208
1209 -- Here if elaboration code is present. If binding a library
1210 -- or if there is a non-Ada main subprogram then we generate:
1211
1212 -- if not uname_E then
1213 -- uname'elab_[spec|body];
1214 -- uname_E := True;
1215 -- end if;
1216
1217 -- Otherwise, elaboration routines are called unconditionally:
1218
1219 -- uname'elab_[spec|body];
1220 -- uname_E := True;
1221
1222 -- The uname_E assignment is skipped if this is a separate spec,
1223 -- since the assignment will be done when we process the body.
1224
1225 elsif not U.SAL_Interface then
1226 if Force_Checking_Of_Elaboration_Flags or
1227 Interface_Library_Unit or
1228 (not Bind_Main_Program)
1229 then
1230 Set_String (" if not E");
1231 Set_Unit_Number (Unum_Spec);
1232 Set_String (" then");
1233 Write_Statement_Buffer;
1234 Set_String (" ");
1235 end if;
1236
1237 Set_String (" ");
1238 Get_Decoded_Name_String_With_Brackets (U.Uname);
1239
1240 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1241 if Name_Buffer (Name_Len) = 's' then
1242 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1243 "_pkg'elab_spec";
1244 else
1245 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1246 "_pkg'elab_body";
1247 end if;
1248
1249 Name_Len := Name_Len + 12;
1250
1251 else
1252 if Name_Buffer (Name_Len) = 's' then
1253 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1254 "'elab_spec";
1255 else
1256 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1257 "'elab_body";
1258 end if;
1259
1260 Name_Len := Name_Len + 8;
1261 end if;
1262
1263 Set_Casing (U.Icasing);
1264 Set_Name_Buffer;
1265 Set_Char (';');
1266 Write_Statement_Buffer;
1267
1268 if U.Utype /= Is_Spec then
1269 if Force_Checking_Of_Elaboration_Flags or
1270 Interface_Library_Unit or
1271 (not Bind_Main_Program)
1272 then
1273 Set_String (" ");
1274 end if;
1275
1276 Set_String (" E");
1277 Set_Unit_Number (Unum_Spec);
1278 Set_String (" := True;");
1279 Write_Statement_Buffer;
1280 end if;
1281
1282 if Force_Checking_Of_Elaboration_Flags or
1283 Interface_Library_Unit or
1284 (not Bind_Main_Program)
1285 then
1286 WBI (" end if;");
1287 end if;
1288 end if;
1289 end;
1290 end loop;
1291 end Gen_Elab_Calls_Ada;
1292
1293 ----------------------
1294 -- Gen_Elab_Calls_C --
1295 ----------------------
1296
1297 procedure Gen_Elab_Calls_C is
1298 begin
1299 for E in Elab_Order.First .. Elab_Order.Last loop
1300 declare
1301 Unum : constant Unit_Id := Elab_Order.Table (E);
1302 U : Unit_Record renames Units.Table (Unum);
1303
1304 Unum_Spec : Unit_Id;
1305 -- This is the unit number of the spec that corresponds to
1306 -- this entry. It is the same as Unum except when the body
1307 -- and spec are different and we are currently processing
1308 -- the body, in which case it is the spec (Unum + 1).
1309
1310 begin
1311 if U.Utype = Is_Body then
1312 Unum_Spec := Unum + 1;
1313 else
1314 Unum_Spec := Unum;
1315 end if;
1316
1317 -- Nothing to do if predefined unit in no run time mode
1318
1319 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1320 null;
1321
1322 -- Case of no elaboration code
1323
1324 elsif U.No_Elab then
1325
1326 -- The only case in which we have to do something is if
1327 -- this is a body, with a separate spec, where the separate
1328 -- spec has an elaboration entity defined.
1329
1330 -- In that case, this is where we set the elaboration entity
1331 -- to True, we do not need to test if this has already been
1332 -- done, since it is quicker to set the flag than to test it.
1333
1334 if not U.SAL_Interface and then U.Utype = Is_Body
1335 and then Units.Table (Unum_Spec).Set_Elab_Entity
1336 then
1337 Set_String (" ");
1338 Get_Name_String (U.Uname);
1339 Set_Unit_Name;
1340 Set_String ("_E = 1;");
1341 Write_Statement_Buffer;
1342 end if;
1343
1344 -- Here if elaboration code is present. If binding a library
1345 -- or if there is a non-Ada main subprogram then we generate:
1346
1347 -- if (uname_E == 0) {
1348 -- uname__elab[s|b] ();
1349 -- uname_E++;
1350 -- }
1351
1352 -- The uname_E assignment is skipped if this is a separate spec,
1353 -- since the assignment will be done when we process the body.
1354
1355 elsif not U.SAL_Interface then
1356 Get_Name_String (U.Uname);
1357
1358 if Force_Checking_Of_Elaboration_Flags or
1359 Interface_Library_Unit or
1360 (not Bind_Main_Program)
1361 then
1362 Set_String (" if (");
1363 Set_Unit_Name;
1364 Set_String ("_E == 0) {");
1365 Write_Statement_Buffer;
1366 Set_String (" ");
1367 end if;
1368
1369 Set_String (" ");
1370 Set_Unit_Name;
1371 Set_String ("___elab");
1372 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1373 Set_String (" ();");
1374 Write_Statement_Buffer;
1375
1376 if U.Utype /= Is_Spec then
1377 if Force_Checking_Of_Elaboration_Flags or
1378 Interface_Library_Unit or
1379 (not Bind_Main_Program)
1380 then
1381 Set_String (" ");
1382 end if;
1383
1384 Set_String (" ");
1385 Set_Unit_Name;
1386 Set_String ("_E++;");
1387 Write_Statement_Buffer;
1388 end if;
1389
1390 if Force_Checking_Of_Elaboration_Flags or
1391 Interface_Library_Unit or
1392 (not Bind_Main_Program)
1393 then
1394 WBI (" }");
1395 end if;
1396 end if;
1397 end;
1398 end loop;
1399
1400 end Gen_Elab_Calls_C;
1401
1402 ----------------------
1403 -- Gen_Elab_Defs_C --
1404 ----------------------
1405
1406 procedure Gen_Elab_Defs_C is
1407 begin
1408 for E in Elab_Order.First .. Elab_Order.Last loop
1409
1410 -- Generate declaration of elaboration procedure if elaboration
1411 -- needed. Note that passive units are always excluded.
1412
1413 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1414 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1415 Set_String ("extern void ");
1416 Set_Unit_Name;
1417 Set_String ("___elab");
1418 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1419 Set_String (" (void);");
1420 Write_Statement_Buffer;
1421 end if;
1422
1423 end loop;
1424
1425 WBI ("");
1426 end Gen_Elab_Defs_C;
1427
1428 ------------------------
1429 -- Gen_Elab_Order_Ada --
1430 ------------------------
1431
1432 procedure Gen_Elab_Order_Ada is
1433 begin
1434 WBI ("");
1435 WBI (" -- BEGIN ELABORATION ORDER");
1436
1437 for J in Elab_Order.First .. Elab_Order.Last loop
1438 Set_String (" -- ");
1439 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1440 Set_Name_Buffer;
1441 Write_Statement_Buffer;
1442 end loop;
1443
1444 WBI (" -- END ELABORATION ORDER");
1445 end Gen_Elab_Order_Ada;
1446
1447 ----------------------
1448 -- Gen_Elab_Order_C --
1449 ----------------------
1450
1451 procedure Gen_Elab_Order_C is
1452 begin
1453 WBI ("");
1454 WBI ("/* BEGIN ELABORATION ORDER");
1455
1456 for J in Elab_Order.First .. Elab_Order.Last loop
1457 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1458 Set_Name_Buffer;
1459 Write_Statement_Buffer;
1460 end loop;
1461
1462 WBI (" END ELABORATION ORDER */");
1463 end Gen_Elab_Order_C;
1464
1465 ------------------
1466 -- Gen_Main_Ada --
1467 ------------------
1468
1469 procedure Gen_Main_Ada is
1470 begin
1471 WBI ("");
1472
1473 if Exit_Status_Supported_On_Target then
1474 Set_String (" function ");
1475 else
1476 Set_String (" procedure ");
1477 end if;
1478
1479 Set_String (Get_Main_Name);
1480
1481 if Command_Line_Args_On_Target then
1482 Write_Statement_Buffer;
1483 WBI (" (argc : Integer;");
1484 WBI (" argv : System.Address;");
1485 WBI (" envp : System.Address)");
1486
1487 if Exit_Status_Supported_On_Target then
1488 WBI (" return Integer");
1489 end if;
1490
1491 WBI (" is");
1492
1493 else
1494 if Exit_Status_Supported_On_Target then
1495 Set_String (" return Integer is");
1496 else
1497 Set_String (" is");
1498 end if;
1499
1500 Write_Statement_Buffer;
1501 end if;
1502
1503 if Opt.Default_Exit_Status /= 0
1504 and then Bind_Main_Program
1505 and then not Configurable_Run_Time_Mode
1506 then
1507 WBI (" procedure Set_Exit_Status (Status : Integer);");
1508 WBI (" pragma Import (C, Set_Exit_Status, " &
1509 """__gnat_set_exit_status"");");
1510 WBI ("");
1511 end if;
1512
1513 -- Initialize and Finalize
1514
1515 if not Cumulative_Restrictions.Set (No_Finalization) then
1516 WBI (" procedure initialize (Addr : System.Address);");
1517 WBI (" pragma Import (C, initialize, ""__gnat_initialize"");");
1518 WBI ("");
1519 WBI (" procedure finalize;");
1520 WBI (" pragma Import (C, finalize, ""__gnat_finalize"");");
1521 end if;
1522
1523 -- If we want to analyze the stack, we have to import corresponding
1524 -- symbols
1525
1526 if Dynamic_Stack_Measurement then
1527 WBI ("");
1528 WBI (" procedure Output_Results;");
1529 WBI (" pragma Import (C, Output_Results, " &
1530 """__gnat_stack_usage_output_results"");");
1531
1532 WBI ("");
1533 WBI (" " &
1534 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1535 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1536 """__gnat_stack_usage_initialize"");");
1537 end if;
1538
1539 -- Deal with declarations for main program case
1540
1541 if not No_Main_Subprogram then
1542
1543 -- To call the main program, we declare it using a pragma Import
1544 -- Ada with the right link name.
1545
1546 -- It might seem more obvious to "with" the main program, and call
1547 -- it in the normal Ada manner. We do not do this for three reasons:
1548
1549 -- 1. It is more efficient not to recompile the main program
1550 -- 2. We are not entitled to assume the source is accessible
1551 -- 3. We don't know what options to use to compile it
1552
1553 -- It is really reason 3 that is most critical (indeed we used
1554 -- to generate the "with", but several regression tests failed).
1555
1556 WBI ("");
1557
1558 if ALIs.Table (ALIs.First).Main_Program = Func then
1559 WBI (" Result : Integer;");
1560 WBI ("");
1561 WBI (" function Ada_Main_Program return Integer;");
1562
1563 else
1564 WBI (" procedure Ada_Main_Program;");
1565 end if;
1566
1567 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1568 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1569 Set_Main_Program_Name;
1570 Set_String (""");");
1571
1572 Write_Statement_Buffer;
1573 WBI ("");
1574
1575 if Bind_Main_Program
1576 and then not Suppress_Standard_Library_On_Target
1577 then
1578 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1579 WBI ("");
1580 end if;
1581 end if;
1582
1583 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1584 -- not referenced elsewhere in the generated program, but is needed
1585 -- by the debugger (that's why it is generated in the first place).
1586 -- The reference stops Ada_Main_Program_Name from being optimized
1587 -- away by smart linkers, such as the AiX linker.
1588
1589 -- Because this variable is unused, we make this variable "aliased"
1590 -- with a pragma Volatile in order to tell the compiler to preserve
1591 -- this variable at any level of optimization.
1592
1593 if Bind_Main_Program then
1594 WBI
1595 (" Ensure_Reference : aliased System.Address := " &
1596 "Ada_Main_Program_Name'Address;");
1597 WBI (" pragma Volatile (Ensure_Reference);");
1598 WBI ("");
1599 end if;
1600
1601 WBI (" begin");
1602
1603 -- Acquire command line arguments if present on target
1604
1605 if Command_Line_Args_On_Target then
1606 WBI (" gnat_argc := argc;");
1607 WBI (" gnat_argv := argv;");
1608 WBI (" gnat_envp := envp;");
1609 WBI ("");
1610
1611 -- If configurable run time and no command line args, then nothing
1612 -- needs to be done since the gnat_argc/argv/envp variables are
1613 -- suppressed in this case.
1614
1615 elsif Configurable_Run_Time_On_Target then
1616 null;
1617
1618 -- Otherwise set dummy values (to be filled in by some other unit?)
1619
1620 else
1621 WBI (" gnat_argc := 0;");
1622 WBI (" gnat_argv := System.Null_Address;");
1623 WBI (" gnat_envp := System.Null_Address;");
1624 end if;
1625
1626 if Opt.Default_Exit_Status /= 0
1627 and then Bind_Main_Program
1628 and then not Configurable_Run_Time_Mode
1629 then
1630 Set_String (" Set_Exit_Status (");
1631 Set_Int (Opt.Default_Exit_Status);
1632 Set_String (");");
1633 Write_Statement_Buffer;
1634 end if;
1635
1636 if Dynamic_Stack_Measurement then
1637 Set_String (" Initialize_Stack_Analysis (");
1638 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1639 Set_String (");");
1640 Write_Statement_Buffer;
1641 end if;
1642
1643 if not Cumulative_Restrictions.Set (No_Finalization) then
1644 if not No_Main_Subprogram
1645 and then Bind_Main_Program
1646 and then not Suppress_Standard_Library_On_Target
1647 then
1648 WBI (" Initialize (SEH'Address);");
1649 else
1650 WBI (" Initialize (System.Null_Address);");
1651 end if;
1652 end if;
1653
1654 WBI (" " & Ada_Init_Name.all & ";");
1655
1656 if not No_Main_Subprogram then
1657 WBI (" Break_Start;");
1658
1659 if ALIs.Table (ALIs.First).Main_Program = Proc then
1660 WBI (" Ada_Main_Program;");
1661 else
1662 WBI (" Result := Ada_Main_Program;");
1663 end if;
1664 end if;
1665
1666 -- Adafinal call is skipped if no finalization
1667
1668 if not Cumulative_Restrictions.Set (No_Finalization) then
1669
1670 -- If compiling for the JVM, we directly call Adafinal because
1671 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1672
1673 if VM_Target = No_VM then
1674 WBI (" Do_Finalize;");
1675 else
1676 WBI (" System.Standard_Library.Adafinal;");
1677 end if;
1678 end if;
1679
1680 -- Prints the result of static stack analysis
1681
1682 if Dynamic_Stack_Measurement then
1683 WBI (" Output_Results;");
1684 end if;
1685
1686 -- Finalize is only called if we have a run time
1687
1688 if not Cumulative_Restrictions.Set (No_Finalization) then
1689 WBI (" Finalize;");
1690 end if;
1691
1692 -- Return result
1693
1694 if Exit_Status_Supported_On_Target then
1695 if No_Main_Subprogram
1696 or else ALIs.Table (ALIs.First).Main_Program = Proc
1697 then
1698 WBI (" return (gnat_exit_status);");
1699 else
1700 WBI (" return (Result);");
1701 end if;
1702 end if;
1703
1704 WBI (" end;");
1705 end Gen_Main_Ada;
1706
1707 ----------------
1708 -- Gen_Main_C --
1709 ----------------
1710
1711 procedure Gen_Main_C is
1712 begin
1713 if Exit_Status_Supported_On_Target then
1714 WBI ("#include <stdlib.h>");
1715 Set_String ("int ");
1716 else
1717 Set_String ("void ");
1718 end if;
1719
1720 Set_String (Get_Main_Name);
1721
1722 -- Generate command line args in prototype if present on target
1723
1724 if Command_Line_Args_On_Target then
1725 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
1726
1727 -- Case of no command line arguments on target
1728
1729 else
1730 Write_Statement_Buffer (" (void)");
1731 end if;
1732
1733 WBI ("{");
1734
1735 -- Generate a reference to __gnat_ada_main_program_name. This symbol
1736 -- is not referenced elsewhere in the generated program, but is
1737 -- needed by the debugger (that's why it is generated in the first
1738 -- place). The reference stops Ada_Main_Program_Name from being
1739 -- optimized away by smart linkers, such as the AiX linker.
1740
1741 -- Because this variable is unused, we declare this variable as
1742 -- volatile in order to tell the compiler to preserve it at any
1743 -- level of optimization.
1744
1745 if Bind_Main_Program then
1746 WBI (" char * volatile ensure_reference " &
1747 "__attribute__ ((__unused__)) = " &
1748 "__gnat_ada_main_program_name;");
1749 WBI ("");
1750
1751 if not Suppress_Standard_Library_On_Target
1752 and then not No_Main_Subprogram
1753 then
1754 WBI (" int SEH [2];");
1755 WBI ("");
1756 end if;
1757 end if;
1758
1759 -- If main program is a function, generate result variable
1760
1761 if ALIs.Table (ALIs.First).Main_Program = Func then
1762 WBI (" int result;");
1763 end if;
1764
1765 -- Set command line argument values from parameters if command line
1766 -- arguments are present on target
1767
1768 if Command_Line_Args_On_Target then
1769 WBI (" gnat_argc = argc;");
1770 WBI (" gnat_argv = argv;");
1771 WBI (" gnat_envp = envp;");
1772 WBI (" ");
1773
1774 -- If configurable run-time, then nothing to do, since in this case
1775 -- the gnat_argc/argv/envp variables are entirely suppressed.
1776
1777 elsif Configurable_Run_Time_On_Target then
1778 null;
1779
1780 -- if no command line arguments on target, set dummy values
1781
1782 else
1783 WBI (" gnat_argc = 0;");
1784 WBI (" gnat_argv = 0;");
1785 WBI (" gnat_envp = 0;");
1786 end if;
1787
1788 if Opt.Default_Exit_Status /= 0
1789 and then Bind_Main_Program
1790 and then not Configurable_Run_Time_Mode
1791 then
1792 Set_String (" __gnat_set_exit_status (");
1793 Set_Int (Opt.Default_Exit_Status);
1794 Set_String (");");
1795 Write_Statement_Buffer;
1796 end if;
1797
1798 -- Initializes dynamic stack measurement if needed
1799
1800 if Dynamic_Stack_Measurement then
1801 Set_String (" __gnat_stack_usage_initialize (");
1802 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1803 Set_String (");");
1804 Write_Statement_Buffer;
1805 end if;
1806
1807 -- The __gnat_initialize routine is used only if we have a run-time
1808
1809 if not Suppress_Standard_Library_On_Target then
1810 if not No_Main_Subprogram and then Bind_Main_Program then
1811 WBI (" __gnat_initialize ((void *)SEH);");
1812 else
1813 WBI (" __gnat_initialize ((void *)0);");
1814 end if;
1815 end if;
1816
1817 WBI (" " & Ada_Init_Name.all & " ();");
1818
1819 if not No_Main_Subprogram then
1820 WBI (" __gnat_break_start ();");
1821 WBI (" ");
1822
1823 -- Output main program name
1824
1825 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1826
1827 -- Main program is procedure case
1828
1829 if ALIs.Table (ALIs.First).Main_Program = Proc then
1830 Set_String (" ");
1831 Set_Main_Program_Name;
1832 Set_String (" ();");
1833 Write_Statement_Buffer;
1834
1835 -- Main program is function case
1836
1837 else -- ALIs.Table (ALIs_First).Main_Program = Func
1838 Set_String (" result = ");
1839 Set_Main_Program_Name;
1840 Set_String (" ();");
1841 Write_Statement_Buffer;
1842 end if;
1843
1844 end if;
1845
1846 -- Call adafinal if finalization active
1847
1848 if not Cumulative_Restrictions.Set (No_Finalization) then
1849 WBI (" ");
1850 WBI (" system__standard_library__adafinal ();");
1851 end if;
1852
1853 -- Outputs the dynamic stack measurement if needed
1854
1855 if Dynamic_Stack_Measurement then
1856 WBI (" __gnat_stack_usage_output_results ();");
1857 end if;
1858
1859 -- The finalize routine is used only if we have a run-time
1860
1861 if not Suppress_Standard_Library_On_Target then
1862 WBI (" __gnat_finalize ();");
1863 end if;
1864
1865 -- Case of main program is a function, so the value it returns
1866 -- is the exit status in this case.
1867
1868 if ALIs.Table (ALIs.First).Main_Program = Func then
1869 if Exit_Status_Supported_On_Target then
1870
1871 -- VMS must use Posix exit routine in order to get the effect
1872 -- of a Unix compatible setting of the program exit status.
1873 -- For all other systems, we use the standard exit routine.
1874
1875 if OpenVMS_On_Target then
1876 WBI (" decc$__posix_exit (result);");
1877 else
1878 WBI (" exit (result);");
1879 end if;
1880 end if;
1881
1882 -- Case of main program is a procedure, in which case the exit
1883 -- status is whatever was set by a Set_Exit call most recently
1884
1885 else
1886 if Exit_Status_Supported_On_Target then
1887
1888 -- VMS must use Posix exit routine in order to get the effect
1889 -- of a Unix compatible setting of the program exit status.
1890 -- For all other systems, we use the standard exit routine.
1891
1892 if OpenVMS_On_Target then
1893 WBI (" decc$__posix_exit (gnat_exit_status);");
1894 else
1895 WBI (" exit (gnat_exit_status);");
1896 end if;
1897 end if;
1898 end if;
1899
1900 WBI ("}");
1901 end Gen_Main_C;
1902
1903 ------------------------------
1904 -- Gen_Object_Files_Options --
1905 ------------------------------
1906
1907 procedure Gen_Object_Files_Options is
1908 Lgnat : Natural;
1909 -- This keeps track of the position in the sorted set of entries
1910 -- in the Linker_Options table of where the first entry from an
1911 -- internal file appears.
1912
1913 Linker_Option_List_Started : Boolean := False;
1914 -- Set to True when "LINKER OPTION LIST" is displayed
1915
1916 procedure Write_Linker_Option;
1917 -- Write binder info linker option
1918
1919 -------------------------
1920 -- Write_Linker_Option --
1921 -------------------------
1922
1923 procedure Write_Linker_Option is
1924 Start : Natural;
1925 Stop : Natural;
1926
1927 begin
1928 -- Loop through string, breaking at null's
1929
1930 Start := 1;
1931 while Start < Name_Len loop
1932
1933 -- Find null ending this section
1934
1935 Stop := Start + 1;
1936 while Name_Buffer (Stop) /= ASCII.NUL
1937 and then Stop <= Name_Len loop
1938 Stop := Stop + 1;
1939 end loop;
1940
1941 -- Process section if non-null
1942
1943 if Stop > Start then
1944 if Output_Linker_Option_List then
1945 if not Zero_Formatting then
1946 if not Linker_Option_List_Started then
1947 Linker_Option_List_Started := True;
1948 Write_Eol;
1949 Write_Str (" LINKER OPTION LIST");
1950 Write_Eol;
1951 Write_Eol;
1952 end if;
1953
1954 Write_Str (" ");
1955 end if;
1956
1957 Write_Str (Name_Buffer (Start .. Stop - 1));
1958 Write_Eol;
1959 end if;
1960 Write_Info_Ada_C
1961 (" -- ", "", Name_Buffer (Start .. Stop - 1));
1962 end if;
1963
1964 Start := Stop + 1;
1965 end loop;
1966 end Write_Linker_Option;
1967
1968 -- Start of processing for Gen_Object_Files_Options
1969
1970 begin
1971 WBI ("");
1972 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
1973
1974 if Object_List_Filename /= null then
1975 Set_List_File (Object_List_Filename.all);
1976 end if;
1977
1978 for E in Elab_Order.First .. Elab_Order.Last loop
1979
1980 -- If not spec that has an associated body, then generate a comment
1981 -- giving the name of the corresponding object file.
1982
1983 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
1984 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1985 then
1986 Get_Name_String
1987 (ALIs.Table
1988 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1989
1990 -- If the presence of an object file is necessary or if it exists,
1991 -- then use it.
1992
1993 if not Hostparm.Exclude_Missing_Objects
1994 or else
1995 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1996 then
1997 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1998
1999 if Output_Object_List then
2000 Write_Str (Name_Buffer (1 .. Name_Len));
2001 Write_Eol;
2002 end if;
2003
2004 -- Don't link with the shared library on VMS if an internal
2005 -- filename object is seen. Multiply defined symbols will
2006 -- result.
2007
2008 if OpenVMS_On_Target
2009 and then Is_Internal_File_Name
2010 (ALIs.Table
2011 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
2012 then
2013 -- Special case for g-trasym.obj (not included in libgnat)
2014
2015 Get_Name_String (ALIs.Table
2016 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
2017
2018 if Name_Buffer (1 .. 8) /= "g-trasym" then
2019 Opt.Shared_Libgnat := False;
2020 end if;
2021 end if;
2022 end if;
2023 end if;
2024 end loop;
2025
2026 if Object_List_Filename /= null then
2027 Close_List_File;
2028 end if;
2029
2030 -- Add a "-Ldir" for each directory in the object path
2031
2032 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2033 declare
2034 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2035 begin
2036 Name_Len := 0;
2037 Add_Str_To_Name_Buffer ("-L");
2038 Add_Str_To_Name_Buffer (Dir.all);
2039 Write_Linker_Option;
2040 end;
2041 end loop;
2042
2043 -- Sort linker options
2044
2045 -- This sort accomplishes two important purposes:
2046
2047 -- a) All application files are sorted to the front, and all GNAT
2048 -- internal files are sorted to the end. This results in a well
2049 -- defined dividing line between the two sets of files, for the
2050 -- purpose of inserting certain standard library references into
2051 -- the linker arguments list.
2052
2053 -- b) Given two different units, we sort the linker options so that
2054 -- those from a unit earlier in the elaboration order comes later
2055 -- in the list. This is a heuristic designed to create a more
2056 -- friendly order of linker options when the operations appear in
2057 -- separate units. The idea is that if unit A must be elaborated
2058 -- before unit B, then it is more likely that B references
2059 -- libraries included by A, than vice versa, so we want libraries
2060 -- included by A to come after libraries included by B.
2061
2062 -- These two criteria are implemented by function Lt_Linker_Option. Note
2063 -- that a special case of b) is that specs are elaborated before bodies,
2064 -- so linker options from specs come after linker options for bodies,
2065 -- and again, the assumption is that libraries used by the body are more
2066 -- likely to reference libraries used by the spec, than vice versa.
2067
2068 Sort
2069 (Linker_Options.Last,
2070 Move_Linker_Option'Access,
2071 Lt_Linker_Option'Access);
2072
2073 -- Write user linker options, i.e. the set of linker options that come
2074 -- from all files other than GNAT internal files, Lgnat is left set to
2075 -- point to the first entry from a GNAT internal file, or past the end
2076 -- of the entriers if there are no internal files.
2077
2078 Lgnat := Linker_Options.Last + 1;
2079
2080 for J in 1 .. Linker_Options.Last loop
2081 if not Linker_Options.Table (J).Internal_File then
2082 Get_Name_String (Linker_Options.Table (J).Name);
2083 Write_Linker_Option;
2084 else
2085 Lgnat := J;
2086 exit;
2087 end if;
2088 end loop;
2089
2090 -- Now we insert standard linker options that must appear after the
2091 -- entries from user files, and before the entries from GNAT run-time
2092 -- files. The reason for this decision is that libraries referenced
2093 -- by internal routines may reference these standard library entries.
2094
2095 -- Note that we do not insert anything when pragma No_Run_Time has been
2096 -- specified or when the standard libraries are not to be used,
2097 -- otherwise on some platforms, such as VMS, we may get duplicate
2098 -- symbols when linking.
2099
2100 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2101 Name_Len := 0;
2102
2103 if Opt.Shared_Libgnat then
2104 Add_Str_To_Name_Buffer ("-shared");
2105 else
2106 Add_Str_To_Name_Buffer ("-static");
2107 end if;
2108
2109 -- Write directly to avoid -K output (why???)
2110
2111 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2112
2113 if With_DECGNAT then
2114 Name_Len := 0;
2115
2116 if Opt.Shared_Libgnat then
2117 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2118 else
2119 Add_Str_To_Name_Buffer ("-ldecgnat");
2120 end if;
2121
2122 Write_Linker_Option;
2123 end if;
2124
2125 if With_GNARL then
2126 Name_Len := 0;
2127
2128 if Opt.Shared_Libgnat then
2129 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2130 else
2131 Add_Str_To_Name_Buffer ("-lgnarl");
2132 end if;
2133
2134 Write_Linker_Option;
2135 end if;
2136
2137 Name_Len := 0;
2138
2139 if Opt.Shared_Libgnat then
2140 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2141 else
2142 Add_Str_To_Name_Buffer ("-lgnat");
2143 end if;
2144
2145 Write_Linker_Option;
2146 end if;
2147
2148 -- Write linker options from all internal files
2149
2150 for J in Lgnat .. Linker_Options.Last loop
2151 Get_Name_String (Linker_Options.Table (J).Name);
2152 Write_Linker_Option;
2153 end loop;
2154
2155 if Output_Linker_Option_List and then not Zero_Formatting then
2156 Write_Eol;
2157 end if;
2158
2159 if Ada_Bind_File then
2160 WBI ("-- END Object file/option list ");
2161 else
2162 WBI (" END Object file/option list */");
2163 end if;
2164 end Gen_Object_Files_Options;
2165
2166 ---------------------
2167 -- Gen_Output_File --
2168 ---------------------
2169
2170 procedure Gen_Output_File (Filename : String) is
2171 begin
2172 -- Acquire settings for Interrupt_State pragmas
2173
2174 Set_IS_Pragma_Table;
2175
2176 -- Acquire settings for Priority_Specific_Dispatching pragma
2177
2178 Set_PSD_Pragma_Table;
2179
2180 -- Override Ada_Bind_File and Bind_Main_Program for VMs since JGNAT only
2181 -- supports Ada code, and the main program is already generated by the
2182 -- compiler.
2183
2184 if VM_Target /= No_VM then
2185 Ada_Bind_File := True;
2186
2187 if VM_Target = JVM_Target then
2188 Bind_Main_Program := False;
2189 end if;
2190 end if;
2191
2192 -- Override time slice value if -T switch is set
2193
2194 if Time_Slice_Set then
2195 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2196 end if;
2197
2198 -- Count number of elaboration calls
2199
2200 for E in Elab_Order.First .. Elab_Order.Last loop
2201 if Units.Table (Elab_Order.Table (E)).No_Elab then
2202 null;
2203 else
2204 Num_Elab_Calls := Num_Elab_Calls + 1;
2205 end if;
2206 end loop;
2207
2208 -- Generate output file in appropriate language
2209
2210 Check_System_Restrictions_Used;
2211
2212 if Ada_Bind_File then
2213 Gen_Output_File_Ada (Filename);
2214 else
2215 Gen_Output_File_C (Filename);
2216 end if;
2217 end Gen_Output_File;
2218
2219 -------------------------
2220 -- Gen_Output_File_Ada --
2221 -------------------------
2222
2223 procedure Gen_Output_File_Ada (Filename : String) is
2224
2225 Bfiles : Name_Id;
2226 -- Name of generated bind file (spec)
2227
2228 Bfileb : Name_Id;
2229 -- Name of generated bind file (body)
2230
2231 Ada_Main : constant String := Get_Ada_Main_Name;
2232 -- Name to be used for generated Ada main program. See the body of
2233 -- function Get_Ada_Main_Name for details on the form of the name.
2234
2235 begin
2236 -- Create spec first
2237
2238 Create_Binder_Output (Filename, 's', Bfiles);
2239
2240 -- We always compile the binder file in Ada 95 mode so that we properly
2241 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2242 -- of the Ada 2005 constructs are needed by the binder file.
2243
2244 WBI ("pragma Ada_95;");
2245
2246 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2247 -- then we need to make sure that the binder program is compiled with
2248 -- the same restriction, so that no exception tables are generated.
2249
2250 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2251 WBI ("pragma Restrictions (No_Exception_Handlers);");
2252 end if;
2253
2254 -- Same processing for Restrictions (No_Exception_Propagation)
2255
2256 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2257 WBI ("pragma Restrictions (No_Exception_Propagation);");
2258 end if;
2259
2260 -- Same processing for pragma No_Run_Time
2261
2262 if No_Run_Time_Mode then
2263 WBI ("pragma No_Run_Time;");
2264 end if;
2265
2266 -- Generate with of System so we can reference System.Address
2267
2268 WBI ("with System;");
2269
2270 -- Generate with of System.Initialize_Scalars if active
2271
2272 if Initialize_Scalars_Used then
2273 WBI ("with System.Scalar_Values;");
2274 end if;
2275
2276 -- Generate with of System.Secondary_Stack if active
2277
2278 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2279 WBI ("with System.Secondary_Stack;");
2280 end if;
2281
2282 Resolve_Binder_Options;
2283
2284 if VM_Target /= No_VM then
2285 if not Suppress_Standard_Library_On_Target then
2286
2287 -- Usually, adafinal is called using a pragma Import C. Since
2288 -- Import C doesn't have the same semantics for JGNAT, we use
2289 -- standard Ada.
2290
2291 WBI ("with System.Standard_Library;");
2292 end if;
2293 end if;
2294
2295 WBI ("package " & Ada_Main & " is");
2296 WBI (" pragma Warnings (Off);");
2297
2298 -- Main program case
2299
2300 if Bind_Main_Program then
2301 if VM_Target = No_VM then
2302
2303 -- Generate argc/argv stuff unless suppressed
2304
2305 if Command_Line_Args_On_Target
2306 or not Configurable_Run_Time_On_Target
2307 then
2308 WBI ("");
2309 WBI (" gnat_argc : Integer;");
2310 WBI (" gnat_argv : System.Address;");
2311 WBI (" gnat_envp : System.Address;");
2312
2313 -- If the standard library is not suppressed, these variables
2314 -- are in the run-time data area for easy run time access.
2315
2316 if not Suppress_Standard_Library_On_Target then
2317 WBI ("");
2318 WBI (" pragma Import (C, gnat_argc);");
2319 WBI (" pragma Import (C, gnat_argv);");
2320 WBI (" pragma Import (C, gnat_envp);");
2321 end if;
2322 end if;
2323
2324 -- Define exit status. Again in normal mode, this is in the
2325 -- run-time library, and is initialized there, but in the
2326 -- configurable runtime case, the variable is declared and
2327 -- initialized in this file.
2328
2329 WBI ("");
2330
2331 if Configurable_Run_Time_Mode then
2332 if Exit_Status_Supported_On_Target then
2333 WBI (" gnat_exit_status : Integer := 0;");
2334 end if;
2335
2336 else
2337 WBI (" gnat_exit_status : Integer;");
2338 WBI (" pragma Import (C, gnat_exit_status);");
2339 end if;
2340 end if;
2341
2342 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2343 -- the main program. Otherwise, it can lead under some circumstances
2344 -- to a symbol duplication during the link (for instance when a C
2345 -- program uses two Ada libraries). Also zero terminate the string
2346 -- so that its end can be found reliably at run time.
2347
2348 WBI ("");
2349 WBI (" GNAT_Version : constant String :=");
2350 WBI (" """ & Ver_Prefix &
2351 Gnat_Version_String &
2352 """ & ASCII.NUL;");
2353 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2354
2355 WBI ("");
2356 Set_String (" Ada_Main_Program_Name : constant String := """);
2357 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2358
2359 if VM_Target = No_VM then
2360 Set_Main_Program_Name;
2361 Set_String (""" & ASCII.NUL;");
2362 else
2363 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2364 end if;
2365
2366 Write_Statement_Buffer;
2367
2368 WBI
2369 (" pragma Export (C, Ada_Main_Program_Name, " &
2370 """__gnat_ada_main_program_name"");");
2371 end if;
2372
2373 if not Cumulative_Restrictions.Set (No_Finalization) then
2374 WBI ("");
2375 WBI (" procedure " & Ada_Final_Name.all & ";");
2376 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2377 Ada_Final_Name.all & """);");
2378 end if;
2379
2380 WBI ("");
2381 WBI (" procedure " & Ada_Init_Name.all & ";");
2382 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2383 Ada_Init_Name.all & """);");
2384
2385 -- If -a has been specified use pragma Linker_Constructor for the init
2386 -- procedure. No need to use a similar pragma for the final procedure as
2387 -- global finalization will occur when the executable finishes execution
2388 -- and for plugins (shared stand-alone libraries that can be
2389 -- "unloaded"), finalization should not occur automatically, otherwise
2390 -- the main executable may not continue to work properly.
2391
2392 if Use_Pragma_Linker_Constructor then
2393 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2394 end if;
2395
2396 if Bind_Main_Program and then VM_Target = No_VM then
2397
2398 -- If we have the standard library, then Break_Start is defined
2399 -- there, but when the standard library is suppressed, Break_Start
2400 -- is defined here.
2401
2402 WBI ("");
2403 WBI (" procedure Break_Start;");
2404
2405 if Suppress_Standard_Library_On_Target then
2406 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2407 else
2408 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2409 end if;
2410
2411 WBI ("");
2412
2413 if Exit_Status_Supported_On_Target then
2414 Set_String (" function ");
2415 else
2416 Set_String (" procedure ");
2417 end if;
2418
2419 Set_String (Get_Main_Name);
2420
2421 -- Generate argument list if present
2422
2423 if Command_Line_Args_On_Target then
2424 Write_Statement_Buffer;
2425 WBI (" (argc : Integer;");
2426 WBI (" argv : System.Address;");
2427 Set_String
2428 (" envp : System.Address)");
2429
2430 if Exit_Status_Supported_On_Target then
2431 Write_Statement_Buffer;
2432 WBI (" return Integer;");
2433 else
2434 Write_Statement_Buffer (";");
2435 end if;
2436
2437 else
2438 if Exit_Status_Supported_On_Target then
2439 Write_Statement_Buffer (" return Integer;");
2440 else
2441 Write_Statement_Buffer (";");
2442 end if;
2443 end if;
2444
2445 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2446 Get_Main_Name & """);");
2447 end if;
2448
2449 Gen_Versions_Ada;
2450 Gen_Elab_Order_Ada;
2451
2452 -- Spec is complete
2453
2454 WBI ("");
2455 WBI ("end " & Ada_Main & ";");
2456 Close_Binder_Output;
2457
2458 -- Prepare to write body
2459
2460 Create_Binder_Output (Filename, 'b', Bfileb);
2461
2462 -- We always compile the binder file in Ada 95 mode so that we properly
2463 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2464 -- of the Ada 2005 constructs are needed by the binder file.
2465
2466 WBI ("pragma Ada_95;");
2467
2468 -- Output Source_File_Name pragmas which look like
2469
2470 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2471 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2472
2473 -- where sss/bbb are the spec/body file names respectively
2474
2475 Get_Name_String (Bfiles);
2476 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2477
2478 WBI ("pragma Source_File_Name (" &
2479 Ada_Main &
2480 ", Spec_File_Name => """ &
2481 Name_Buffer (1 .. Name_Len + 3));
2482
2483 Get_Name_String (Bfileb);
2484 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2485
2486 WBI ("pragma Source_File_Name (" &
2487 Ada_Main &
2488 ", Body_File_Name => """ &
2489 Name_Buffer (1 .. Name_Len + 3));
2490
2491 -- Generate with of System.Restrictions to initialize
2492 -- Run_Time_Restrictions.
2493
2494 if System_Restrictions_Used
2495 and not Suppress_Standard_Library_On_Target
2496 then
2497 WBI ("");
2498 WBI ("with System.Restrictions;");
2499 end if;
2500
2501 WBI ("");
2502 WBI ("package body " & Ada_Main & " is");
2503 WBI (" pragma Warnings (Off);");
2504
2505 -- Import the finalization procedure only if finalization active
2506
2507 if not Cumulative_Restrictions.Set (No_Finalization) then
2508
2509 -- In the Java case, pragma Import C cannot be used, so the standard
2510 -- Ada constructs will be used instead.
2511
2512 if VM_Target = No_VM then
2513 WBI ("");
2514 WBI (" procedure Do_Finalize;");
2515 WBI
2516 (" pragma Import (C, Do_Finalize, " &
2517 """system__standard_library__adafinal"");");
2518 WBI ("");
2519 end if;
2520 end if;
2521
2522 if not Suppress_Standard_Library_On_Target then
2523
2524 -- Generate Priority_Specific_Dispatching pragma string
2525
2526 Set_String
2527 (" Local_Priority_Specific_Dispatching : constant String := """);
2528
2529 for J in 0 .. PSD_Pragma_Settings.Last loop
2530 Set_Char (PSD_Pragma_Settings.Table (J));
2531 end loop;
2532
2533 Set_String (""";");
2534 Write_Statement_Buffer;
2535
2536 -- Generate Interrupt_State pragma string
2537
2538 Set_String (" Local_Interrupt_States : constant String := """);
2539
2540 for J in 0 .. IS_Pragma_Settings.Last loop
2541 Set_Char (IS_Pragma_Settings.Table (J));
2542 end loop;
2543
2544 Set_String (""";");
2545 Write_Statement_Buffer;
2546 WBI ("");
2547 end if;
2548
2549 Gen_Adainit_Ada;
2550
2551 -- Generate the adafinal routine unless there is no finalization to do
2552
2553 if not Cumulative_Restrictions.Set (No_Finalization) then
2554 Gen_Adafinal_Ada;
2555 end if;
2556
2557 if Bind_Main_Program and then VM_Target = No_VM then
2558
2559 -- When suppressing the standard library then generate dummy body
2560 -- for Break_Start
2561
2562 if Suppress_Standard_Library_On_Target then
2563 WBI ("");
2564 WBI (" procedure Break_Start is");
2565 WBI (" begin");
2566 WBI (" null;");
2567 WBI (" end;");
2568 end if;
2569
2570 Gen_Main_Ada;
2571 end if;
2572
2573 -- Output object file list and the Ada body is complete
2574
2575 Gen_Object_Files_Options;
2576
2577 WBI ("");
2578 WBI ("end " & Ada_Main & ";");
2579
2580 Close_Binder_Output;
2581 end Gen_Output_File_Ada;
2582
2583 -----------------------
2584 -- Gen_Output_File_C --
2585 -----------------------
2586
2587 procedure Gen_Output_File_C (Filename : String) is
2588 Bfile : Name_Id;
2589 pragma Warnings (Off, Bfile);
2590 -- Name of generated bind file (not referenced)
2591
2592 begin
2593 Create_Binder_Output (Filename, 'c', Bfile);
2594
2595 Resolve_Binder_Options;
2596
2597 WBI ("extern void " & Ada_Final_Name.all & " (void);");
2598
2599 -- If -a has been specified use __attribute__((constructor)) for the
2600 -- init procedure. No need to use a similar featute for the final
2601 -- procedure as global finalization will occur when the executable
2602 -- finishes execution and for plugins (shared stand-alone libraries that
2603 -- can be "unloaded"), finalization should not occur automatically,
2604 -- otherwise the main executable may not continue to work properly.
2605
2606 if Use_Pragma_Linker_Constructor then
2607 WBI ("extern void " & Ada_Init_Name.all &
2608 " (void) __attribute__((constructor));");
2609 else
2610 WBI ("extern void " & Ada_Init_Name.all & " (void);");
2611 end if;
2612
2613 WBI ("extern void system__standard_library__adafinal (void);");
2614
2615 if not No_Main_Subprogram then
2616 Set_String ("extern ");
2617
2618 if Exit_Status_Supported_On_Target then
2619 Set_String ("int");
2620 else
2621 Set_String ("void");
2622 end if;
2623
2624 Set_String (" main ");
2625
2626 if Command_Line_Args_On_Target then
2627 Write_Statement_Buffer ("(int, char **, char **);");
2628 else
2629 Write_Statement_Buffer ("(void);");
2630 end if;
2631
2632 if OpenVMS_On_Target then
2633 WBI ("extern void decc$__posix_exit (int);");
2634 else
2635 WBI ("extern void exit (int);");
2636 end if;
2637
2638 WBI ("extern void __gnat_break_start (void);");
2639 Set_String ("extern ");
2640
2641 if ALIs.Table (ALIs.First).Main_Program = Proc then
2642 Set_String ("void ");
2643 else
2644 Set_String ("int ");
2645 end if;
2646
2647 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2648 Set_Main_Program_Name;
2649 Set_String (" (void);");
2650 Write_Statement_Buffer;
2651 end if;
2652
2653 if not Suppress_Standard_Library_On_Target then
2654 WBI ("extern void __gnat_initialize (void *);");
2655 WBI ("extern void __gnat_finalize (void);");
2656 WBI ("extern void __gnat_install_handler (void);");
2657 end if;
2658
2659 if Dynamic_Stack_Measurement then
2660 WBI ("");
2661 WBI ("extern void __gnat_stack_usage_output_results (void);");
2662 WBI ("extern void __gnat_stack_usage_initialize (int size);");
2663 end if;
2664
2665 -- Initialize stack limit for the environment task if the stack check
2666 -- method is stack limit and stack check is enabled.
2667
2668 if Stack_Check_Limits_On_Target
2669 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
2670 then
2671 WBI ("");
2672 WBI ("extern void __gnat_initialize_stack_limit (void);");
2673 end if;
2674
2675 WBI ("");
2676
2677 Gen_Elab_Defs_C;
2678
2679 -- Imported variables used only when we have a runtime
2680
2681 if not Suppress_Standard_Library_On_Target then
2682
2683 -- Track elaboration/finalization phase
2684
2685 WBI ("extern int __gnat_handler_installed;");
2686 WBI ("");
2687
2688 -- Track feature enable/disable on VMS
2689
2690 if OpenVMS_On_Target then
2691 WBI ("extern int __gnat_features_set;");
2692 WBI ("");
2693 end if;
2694 end if;
2695
2696 -- Write argv/argc exit status stuff if main program case
2697
2698 if Bind_Main_Program then
2699
2700 -- First deal with argc/argv/envp. In the normal case they are in the
2701 -- run-time library.
2702
2703 if not Configurable_Run_Time_On_Target then
2704 WBI ("extern int gnat_argc;");
2705 WBI ("extern char **gnat_argv;");
2706 WBI ("extern char **gnat_envp;");
2707
2708 -- If configurable run time and no command line args, then the
2709 -- generation of these variables is entirely suppressed.
2710
2711 elsif not Command_Line_Args_On_Target then
2712 null;
2713
2714 -- Otherwise, in the configurable run-time case they are right in the
2715 -- binder file.
2716
2717 else
2718 WBI ("int gnat_argc;");
2719 WBI ("char **gnat_argv;");
2720 WBI ("char **gnat_envp;");
2721 end if;
2722
2723 -- Similarly deal with exit status
2724
2725 if not Configurable_Run_Time_On_Target then
2726 WBI ("extern int gnat_exit_status;");
2727
2728 -- If configurable run time and no exit status on target, then the
2729 -- generation of this variables is entirely suppressed.
2730
2731 elsif not Exit_Status_Supported_On_Target then
2732 null;
2733
2734 -- Otherwise, in the configurable run-time case this variable is
2735 -- right in the binder file, and initialized to zero there.
2736
2737 else
2738 WBI ("int gnat_exit_status = 0;");
2739 end if;
2740
2741 WBI ("");
2742 end if;
2743
2744 -- When suppressing the standard library, the __gnat_break_start routine
2745 -- (for the debugger to get initial control) is defined in this file.
2746
2747 if Suppress_Standard_Library_On_Target then
2748 WBI ("");
2749 WBI ("void __gnat_break_start (void) {}");
2750 end if;
2751
2752 -- Generate the __gnat_version and __gnat_ada_main_program_name info
2753 -- only for the main program. Otherwise, it can lead under some
2754 -- circumstances to a symbol duplication during the link (for instance
2755 -- when a C program uses 2 Ada libraries)
2756
2757 if Bind_Main_Program then
2758 WBI ("");
2759 WBI ("char __gnat_version[] = """ & Ver_Prefix &
2760 Gnat_Version_String & """;");
2761
2762 Set_String ("char __gnat_ada_main_program_name[] = """);
2763 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2764 Set_Main_Program_Name;
2765 Set_String (""";");
2766 Write_Statement_Buffer;
2767 end if;
2768
2769 -- Generate the adafinal routine. In no runtime mode, this is not
2770 -- needed, since there is no finalization to do.
2771
2772 if not Cumulative_Restrictions.Set (No_Finalization) then
2773 Gen_Adafinal_C;
2774 end if;
2775
2776 Gen_Adainit_C;
2777
2778 -- Main is only present for Ada main case
2779
2780 if Bind_Main_Program then
2781 Gen_Main_C;
2782 end if;
2783
2784 -- Generate versions, elaboration order, list of object files
2785
2786 Gen_Versions_C;
2787 Gen_Elab_Order_C;
2788 Gen_Object_Files_Options;
2789
2790 -- C binder output is complete
2791
2792 Close_Binder_Output;
2793 end Gen_Output_File_C;
2794
2795 --------------------------
2796 -- Gen_Restrictions_Ada --
2797 --------------------------
2798
2799 procedure Gen_Restrictions_Ada is
2800 Count : Integer;
2801
2802 begin
2803 if Suppress_Standard_Library_On_Target
2804 or not System_Restrictions_Used
2805 then
2806 return;
2807 end if;
2808
2809 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2810 WBI (" (Set =>");
2811 Set_String (" (");
2812
2813 Count := 0;
2814
2815 for J in Cumulative_Restrictions.Set'Range loop
2816 Set_Boolean (Cumulative_Restrictions.Set (J));
2817 Set_String (", ");
2818 Count := Count + 1;
2819
2820 if Count = 8 then
2821 Write_Statement_Buffer;
2822 Set_String (" ");
2823 Count := 0;
2824 end if;
2825 end loop;
2826
2827 Set_String_Replace ("),");
2828 Write_Statement_Buffer;
2829 Set_String (" Value => (");
2830
2831 for J in Cumulative_Restrictions.Value'Range loop
2832 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2833 Set_String (", ");
2834 end loop;
2835
2836 Set_String_Replace ("),");
2837 Write_Statement_Buffer;
2838 WBI (" Violated =>");
2839 Set_String (" (");
2840 Count := 0;
2841
2842 for J in Cumulative_Restrictions.Violated'Range loop
2843 Set_Boolean (Cumulative_Restrictions.Violated (J));
2844 Set_String (", ");
2845 Count := Count + 1;
2846
2847 if Count = 8 then
2848 Write_Statement_Buffer;
2849 Set_String (" ");
2850 Count := 0;
2851 end if;
2852 end loop;
2853
2854 Set_String_Replace ("),");
2855 Write_Statement_Buffer;
2856 Set_String (" Count => (");
2857
2858 for J in Cumulative_Restrictions.Count'Range loop
2859 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2860 Set_String (", ");
2861 end loop;
2862
2863 Set_String_Replace ("),");
2864 Write_Statement_Buffer;
2865 Set_String (" Unknown => (");
2866
2867 for J in Cumulative_Restrictions.Unknown'Range loop
2868 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2869 Set_String (", ");
2870 end loop;
2871
2872 Set_String_Replace ("))");
2873 Set_String (";");
2874 Write_Statement_Buffer;
2875 end Gen_Restrictions_Ada;
2876
2877 ------------------------
2878 -- Gen_Restrictions_C --
2879 ------------------------
2880
2881 procedure Gen_Restrictions_C is
2882 begin
2883 if Suppress_Standard_Library_On_Target
2884 or not System_Restrictions_Used
2885 then
2886 return;
2887 end if;
2888
2889 WBI (" typedef struct {");
2890 Set_String (" char set [");
2891 Set_Int (Cumulative_Restrictions.Set'Length);
2892 Set_String ("];");
2893 Write_Statement_Buffer;
2894
2895 Set_String (" int value [");
2896 Set_Int (Cumulative_Restrictions.Value'Length);
2897 Set_String ("];");
2898 Write_Statement_Buffer;
2899
2900 Set_String (" char violated [");
2901 Set_Int (Cumulative_Restrictions.Violated'Length);
2902 Set_String ("];");
2903 Write_Statement_Buffer;
2904
2905 Set_String (" int count [");
2906 Set_Int (Cumulative_Restrictions.Count'Length);
2907 Set_String ("];");
2908 Write_Statement_Buffer;
2909
2910 Set_String (" char unknown [");
2911 Set_Int (Cumulative_Restrictions.Unknown'Length);
2912 Set_String ("];");
2913 Write_Statement_Buffer;
2914 WBI (" } restrictions;");
2915 WBI (" extern restrictions " &
2916 "system__restrictions__run_time_restrictions;");
2917 WBI (" restrictions r = {");
2918 Set_String (" {");
2919
2920 for J in Cumulative_Restrictions.Set'Range loop
2921 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
2922 Set_String (", ");
2923 end loop;
2924
2925 Set_String_Replace ("},");
2926 Write_Statement_Buffer;
2927 Set_String (" {");
2928
2929 for J in Cumulative_Restrictions.Value'Range loop
2930 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2931 Set_String (", ");
2932 end loop;
2933
2934 Set_String_Replace ("},");
2935 Write_Statement_Buffer;
2936 Set_String (" {");
2937
2938 for J in Cumulative_Restrictions.Violated'Range loop
2939 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
2940 Set_String (", ");
2941 end loop;
2942
2943 Set_String_Replace ("},");
2944 Write_Statement_Buffer;
2945 Set_String (" {");
2946
2947 for J in Cumulative_Restrictions.Count'Range loop
2948 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2949 Set_String (", ");
2950 end loop;
2951
2952 Set_String_Replace ("},");
2953 Write_Statement_Buffer;
2954 Set_String (" {");
2955
2956 for J in Cumulative_Restrictions.Unknown'Range loop
2957 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
2958 Set_String (", ");
2959 end loop;
2960
2961 Set_String_Replace ("}}");
2962 Set_String (";");
2963 Write_Statement_Buffer;
2964 WBI (" system__restrictions__run_time_restrictions = r;");
2965 end Gen_Restrictions_C;
2966
2967 ----------------------
2968 -- Gen_Versions_Ada --
2969 ----------------------
2970
2971 -- This routine generates lines such as:
2972
2973 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2974 -- pragma Export (C, unnnnn, unam);
2975
2976 -- for each unit, where unam is the unit name suffixed by either B or S for
2977 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2978 -- the version number, and nnnnn is a 5-digits serial number.
2979
2980 procedure Gen_Versions_Ada is
2981 Ubuf : String (1 .. 6) := "u00000";
2982
2983 procedure Increment_Ubuf;
2984 -- Little procedure to increment the serial number
2985
2986 procedure Increment_Ubuf is
2987 begin
2988 for J in reverse Ubuf'Range loop
2989 Ubuf (J) := Character'Succ (Ubuf (J));
2990 exit when Ubuf (J) <= '9';
2991 Ubuf (J) := '0';
2992 end loop;
2993 end Increment_Ubuf;
2994
2995 -- Start of processing for Gen_Versions_Ada
2996
2997 begin
2998 WBI ("");
2999
3000 WBI (" type Version_32 is mod 2 ** 32;");
3001 for U in Units.First .. Units.Last loop
3002 if not Units.Table (U).SAL_Interface and then
3003 ((not Bind_For_Library) or else Units.Table (U).Directly_Scanned)
3004 then
3005 Increment_Ubuf;
3006 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
3007 Units.Table (U).Version & "#;");
3008 Set_String (" pragma Export (C, ");
3009 Set_String (Ubuf);
3010 Set_String (", """);
3011
3012 Get_Name_String (Units.Table (U).Uname);
3013
3014 for K in 1 .. Name_Len loop
3015 if Name_Buffer (K) = '.' then
3016 Set_Char ('_');
3017 Set_Char ('_');
3018
3019 elsif Name_Buffer (K) = '%' then
3020 exit;
3021
3022 else
3023 Set_Char (Name_Buffer (K));
3024 end if;
3025 end loop;
3026
3027 if Name_Buffer (Name_Len) = 's' then
3028 Set_Char ('S');
3029 else
3030 Set_Char ('B');
3031 end if;
3032
3033 Set_String (""");");
3034 Write_Statement_Buffer;
3035 end if;
3036 end loop;
3037
3038 end Gen_Versions_Ada;
3039
3040 --------------------
3041 -- Gen_Versions_C --
3042 --------------------
3043
3044 -- This routine generates a line of the form:
3045
3046 -- unsigned unam = 0xhhhhhhhh;
3047
3048 -- for each unit, where unam is the unit name suffixed by either B or S for
3049 -- body or spec, with dots replaced by double underscores.
3050
3051 procedure Gen_Versions_C is
3052 begin
3053 for U in Units.First .. Units.Last loop
3054 if not Units.Table (U).SAL_Interface and then
3055 ((not Bind_For_Library) or else Units.Table (U).Directly_Scanned)
3056 then
3057 Set_String ("unsigned ");
3058
3059 Get_Name_String (Units.Table (U).Uname);
3060
3061 for K in 1 .. Name_Len loop
3062 if Name_Buffer (K) = '.' then
3063 Set_String ("__");
3064
3065 elsif Name_Buffer (K) = '%' then
3066 exit;
3067
3068 else
3069 Set_Char (Name_Buffer (K));
3070 end if;
3071 end loop;
3072
3073 if Name_Buffer (Name_Len) = 's' then
3074 Set_Char ('S');
3075 else
3076 Set_Char ('B');
3077 end if;
3078
3079 Set_String (" = 0x");
3080 Set_String (Units.Table (U).Version);
3081 Set_Char (';');
3082 Write_Statement_Buffer;
3083 end if;
3084 end loop;
3085
3086 end Gen_Versions_C;
3087
3088 ------------------------
3089 -- Get_Main_Unit_Name --
3090 ------------------------
3091
3092 function Get_Main_Unit_Name (S : String) return String is
3093 Result : String := S;
3094
3095 begin
3096 for J in S'Range loop
3097 if Result (J) = '.' then
3098 Result (J) := '_';
3099 end if;
3100 end loop;
3101
3102 return Result;
3103 end Get_Main_Unit_Name;
3104
3105 -----------------------
3106 -- Get_Ada_Main_Name --
3107 -----------------------
3108
3109 function Get_Ada_Main_Name return String is
3110 Suffix : constant String := "_00";
3111 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3112 Opt.Ada_Main_Name.all & Suffix;
3113 Nlen : Natural;
3114
3115 begin
3116 -- The main program generated by JGNAT expects a package called
3117 -- ada_<main procedure>.
3118
3119 if VM_Target /= No_VM then
3120 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3121 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3122 end if;
3123
3124 -- This loop tries the following possibilities in order
3125 -- <Ada_Main>
3126 -- <Ada_Main>_01
3127 -- <Ada_Main>_02
3128 -- ..
3129 -- <Ada_Main>_99
3130 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3131 -- it is set to 'ada_main'.
3132
3133 for J in 0 .. 99 loop
3134 if J = 0 then
3135 Nlen := Name'Length - Suffix'Length;
3136 else
3137 Nlen := Name'Length;
3138 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3139 Name (Name'Last - 1) :=
3140 Character'Val (J / 10 + Character'Pos ('0'));
3141 end if;
3142
3143 for K in ALIs.First .. ALIs.Last loop
3144 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3145
3146 -- Get unit name, removing %b or %e at end
3147
3148 Get_Name_String (Units.Table (L).Uname);
3149 Name_Len := Name_Len - 2;
3150
3151 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3152 goto Continue;
3153 end if;
3154 end loop;
3155 end loop;
3156
3157 return Name (1 .. Nlen);
3158
3159 <<Continue>>
3160 null;
3161 end loop;
3162
3163 -- If we fall through, just use a peculiar unlikely name
3164
3165 return ("Qwertyuiop");
3166 end Get_Ada_Main_Name;
3167
3168 -------------------
3169 -- Get_Main_Name --
3170 -------------------
3171
3172 function Get_Main_Name return String is
3173 begin
3174 -- Explicit name given with -M switch
3175
3176 if Bind_Alternate_Main_Name then
3177 return Alternate_Main_Name.all;
3178
3179 -- Case of main program name to be used directly
3180
3181 elsif Use_Ada_Main_Program_Name_On_Target then
3182
3183 -- Get main program name
3184
3185 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3186
3187 -- If this is a child name, return only the name of the child, since
3188 -- we can't have dots in a nested program name. Note that we do not
3189 -- include the %b at the end of the unit name.
3190
3191 for J in reverse 1 .. Name_Len - 2 loop
3192 if J = 1 or else Name_Buffer (J - 1) = '.' then
3193 return Name_Buffer (J .. Name_Len - 2);
3194 end if;
3195 end loop;
3196
3197 raise Program_Error; -- impossible exit
3198
3199 -- Case where "main" is to be used as default
3200
3201 else
3202 return "main";
3203 end if;
3204 end Get_Main_Name;
3205
3206 ---------------------
3207 -- Get_WC_Encoding --
3208 ---------------------
3209
3210 function Get_WC_Encoding return Character is
3211 begin
3212 -- If encoding method specified by -W switch, then return it
3213
3214 if Wide_Character_Encoding_Method_Specified then
3215 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3216
3217 -- If no main program, and not specified, set brackets, we really have
3218 -- no better choice. If some other encoding is required when there is
3219 -- no main, it must be set explicitly using -Wx.
3220
3221 -- Note: if the ALI file always passed the wide character encoding of
3222 -- every file, then we could use the encoding of the initial specified
3223 -- file, but this information is passed only for potential main
3224 -- programs. We could fix this sometime, but it is a very minor point
3225 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
3226 -- is no main program).
3227
3228 elsif No_Main_Subprogram then
3229 return 'b';
3230
3231 -- Otherwise if there is a main program, take encoding from it
3232
3233 else
3234 return ALIs.Table (ALIs.First).WC_Encoding;
3235 end if;
3236 end Get_WC_Encoding;
3237
3238 ----------------------
3239 -- Lt_Linker_Option --
3240 ----------------------
3241
3242 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3243 begin
3244 -- Sort internal files last
3245
3246 if Linker_Options.Table (Op1).Internal_File
3247 /=
3248 Linker_Options.Table (Op2).Internal_File
3249 then
3250 -- Note: following test uses False < True
3251
3252 return Linker_Options.Table (Op1).Internal_File
3253 <
3254 Linker_Options.Table (Op2).Internal_File;
3255
3256 -- If both internal or both non-internal, sort according to the
3257 -- elaboration position. A unit that is elaborated later should come
3258 -- earlier in the linker options list.
3259
3260 else
3261 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3262 >
3263 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3264
3265 end if;
3266 end Lt_Linker_Option;
3267
3268 ------------------------
3269 -- Move_Linker_Option --
3270 ------------------------
3271
3272 procedure Move_Linker_Option (From : Natural; To : Natural) is
3273 begin
3274 Linker_Options.Table (To) := Linker_Options.Table (From);
3275 end Move_Linker_Option;
3276
3277 ----------------------------
3278 -- Resolve_Binder_Options --
3279 ----------------------------
3280
3281 procedure Resolve_Binder_Options is
3282 begin
3283 for E in Elab_Order.First .. Elab_Order.Last loop
3284 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3285
3286 -- This is not a perfect approach, but is the current protocol
3287 -- between the run-time and the binder to indicate that tasking is
3288 -- used: system.os_interface should always be used by any tasking
3289 -- application.
3290
3291 if Name_Buffer (1 .. 19) = "system.os_interface" then
3292 With_GNARL := True;
3293 end if;
3294
3295 -- Ditto for declib and the "dec" package
3296
3297 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3298 With_DECGNAT := True;
3299 end if;
3300 end loop;
3301 end Resolve_Binder_Options;
3302
3303 -----------------
3304 -- Set_Boolean --
3305 -----------------
3306
3307 procedure Set_Boolean (B : Boolean) is
3308 True_Str : constant String := "True";
3309 False_Str : constant String := "False";
3310 begin
3311 if B then
3312 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3313 Last := Last + True_Str'Length;
3314 else
3315 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3316 Last := Last + False_Str'Length;
3317 end if;
3318 end Set_Boolean;
3319
3320 --------------
3321 -- Set_Char --
3322 --------------
3323
3324 procedure Set_Char (C : Character) is
3325 begin
3326 Last := Last + 1;
3327 Statement_Buffer (Last) := C;
3328 end Set_Char;
3329
3330 -------------
3331 -- Set_Int --
3332 -------------
3333
3334 procedure Set_Int (N : Int) is
3335 begin
3336 if N < 0 then
3337 Set_String ("-");
3338 Set_Int (-N);
3339
3340 else
3341 if N > 9 then
3342 Set_Int (N / 10);
3343 end if;
3344
3345 Last := Last + 1;
3346 Statement_Buffer (Last) :=
3347 Character'Val (N mod 10 + Character'Pos ('0'));
3348 end if;
3349 end Set_Int;
3350
3351 -------------------------
3352 -- Set_IS_Pragma_Table --
3353 -------------------------
3354
3355 procedure Set_IS_Pragma_Table is
3356 begin
3357 for F in ALIs.First .. ALIs.Last loop
3358 for K in ALIs.Table (F).First_Interrupt_State ..
3359 ALIs.Table (F).Last_Interrupt_State
3360 loop
3361 declare
3362 Inum : constant Int :=
3363 Interrupt_States.Table (K).Interrupt_Id;
3364 Stat : constant Character :=
3365 Interrupt_States.Table (K).Interrupt_State;
3366
3367 begin
3368 while IS_Pragma_Settings.Last < Inum loop
3369 IS_Pragma_Settings.Append ('n');
3370 end loop;
3371
3372 IS_Pragma_Settings.Table (Inum) := Stat;
3373 end;
3374 end loop;
3375 end loop;
3376 end Set_IS_Pragma_Table;
3377
3378 ---------------------------
3379 -- Set_Main_Program_Name --
3380 ---------------------------
3381
3382 procedure Set_Main_Program_Name is
3383 begin
3384 -- Note that name has %b on the end which we ignore
3385
3386 -- First we output the initial _ada_ since we know that the main
3387 -- program is a library level subprogram.
3388
3389 Set_String ("_ada_");
3390
3391 -- Copy name, changing dots to double underscores
3392
3393 for J in 1 .. Name_Len - 2 loop
3394 if Name_Buffer (J) = '.' then
3395 Set_String ("__");
3396 else
3397 Set_Char (Name_Buffer (J));
3398 end if;
3399 end loop;
3400 end Set_Main_Program_Name;
3401
3402 ---------------------
3403 -- Set_Name_Buffer --
3404 ---------------------
3405
3406 procedure Set_Name_Buffer is
3407 begin
3408 for J in 1 .. Name_Len loop
3409 Set_Char (Name_Buffer (J));
3410 end loop;
3411 end Set_Name_Buffer;
3412
3413 -------------------------
3414 -- Set_PSD_Pragma_Table --
3415 -------------------------
3416
3417 procedure Set_PSD_Pragma_Table is
3418 begin
3419 for F in ALIs.First .. ALIs.Last loop
3420 for K in ALIs.Table (F).First_Specific_Dispatching ..
3421 ALIs.Table (F).Last_Specific_Dispatching
3422 loop
3423 declare
3424 DTK : Specific_Dispatching_Record
3425 renames Specific_Dispatching.Table (K);
3426
3427 begin
3428 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3429 PSD_Pragma_Settings.Append ('F');
3430 end loop;
3431
3432 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3433 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3434 end loop;
3435 end;
3436 end loop;
3437 end loop;
3438 end Set_PSD_Pragma_Table;
3439
3440 ----------------
3441 -- Set_String --
3442 ----------------
3443
3444 procedure Set_String (S : String) is
3445 begin
3446 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3447 Last := Last + S'Length;
3448 end Set_String;
3449
3450 ------------------------
3451 -- Set_String_Replace --
3452 ------------------------
3453
3454 procedure Set_String_Replace (S : String) is
3455 begin
3456 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3457 end Set_String_Replace;
3458
3459 -------------------
3460 -- Set_Unit_Name --
3461 -------------------
3462
3463 procedure Set_Unit_Name is
3464 begin
3465 for J in 1 .. Name_Len - 2 loop
3466 if Name_Buffer (J) /= '.' then
3467 Set_Char (Name_Buffer (J));
3468 else
3469 Set_String ("__");
3470 end if;
3471 end loop;
3472 end Set_Unit_Name;
3473
3474 ---------------------
3475 -- Set_Unit_Number --
3476 ---------------------
3477
3478 procedure Set_Unit_Number (U : Unit_Id) is
3479 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3480 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3481
3482 begin
3483 if Num_Units >= 10 and then Unum < 10 then
3484 Set_Char ('0');
3485 end if;
3486
3487 if Num_Units >= 100 and then Unum < 100 then
3488 Set_Char ('0');
3489 end if;
3490
3491 Set_Int (Unum);
3492 end Set_Unit_Number;
3493
3494 ----------------------
3495 -- Write_Info_Ada_C --
3496 ----------------------
3497
3498 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3499 begin
3500 if Ada_Bind_File then
3501 declare
3502 S : String (1 .. Ada'Length + Common'Length);
3503 begin
3504 S (1 .. Ada'Length) := Ada;
3505 S (Ada'Length + 1 .. S'Length) := Common;
3506 WBI (S);
3507 end;
3508
3509 else
3510 declare
3511 S : String (1 .. C'Length + Common'Length);
3512 begin
3513 S (1 .. C'Length) := C;
3514 S (C'Length + 1 .. S'Length) := Common;
3515 WBI (S);
3516 end;
3517 end if;
3518 end Write_Info_Ada_C;
3519
3520 ----------------------------
3521 -- Write_Statement_Buffer --
3522 ----------------------------
3523
3524 procedure Write_Statement_Buffer is
3525 begin
3526 WBI (Statement_Buffer (1 .. Last));
3527 Last := 0;
3528 end Write_Statement_Buffer;
3529
3530 procedure Write_Statement_Buffer (S : String) is
3531 begin
3532 Set_String (S);
3533 Write_Statement_Buffer;
3534 end Write_Statement_Buffer;
3535
3536 end Bindgen;