1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Binde; use Binde;
28 with Casing; use Casing;
29 with Fname; use Fname;
30 with Gnatvsn; use Gnatvsn;
32 with Namet; use Namet;
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;
42 with System.OS_Lib; use System.OS_Lib;
43 with System.WCh_Con; use System.WCh_Con;
45 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
47 package body Bindgen is
49 Statement_Buffer : String (1 .. 1000);
50 -- Buffer used for constructing output statements
53 -- Last location in Statement_Buffer currently set
55 With_DECGNAT : Boolean := False;
56 -- Flag which indicates whether the program uses the DECGNAT library
57 -- (presence of the unit DEC).
59 With_GNARL : Boolean := False;
60 -- Flag which indicates whether the program uses the GNARL library
61 -- (presence of the unit System.OS_Interface)
63 Num_Elab_Calls : Nat := 0;
64 -- Number of generated calls to elaboration routines
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.)
74 ----------------------------------
75 -- Interface_State Pragma Table --
76 ----------------------------------
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.
83 package IS_Pragma_Settings is new Table.Table (
84 Table_Component_Type => Character,
85 Table_Index_Type => Int,
88 Table_Increment => 200,
89 Table_Name => "IS_Pragma_Settings");
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.
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");
105 ----------------------
106 -- Run-Time Globals --
107 ----------------------
109 -- This section documents the global variables that set from the
110 -- generated binder file.
112 -- Main_Priority : Integer;
113 -- Time_Slice_Value : Integer;
114 -- WC_Encoding : Character;
115 -- Locking_Policy : Character;
116 -- Queuing_Policy : Character;
117 -- Task_Dispatching_Policy : Character;
118 -- Priority_Specific_Dispatching : System.Address;
119 -- Num_Specific_Dispatching : Integer;
120 -- Restrictions : System.Address;
121 -- Interrupt_States : System.Address;
122 -- Num_Interrupt_States : Integer;
123 -- Unreserve_All_Interrupts : Integer;
124 -- Exception_Tracebacks : Integer;
125 -- Zero_Cost_Exceptions : Integer;
126 -- Detect_Blocking : Integer;
127 -- Default_Stack_Size : Integer;
128 -- Leap_Seconds_Support : Integer;
130 -- Main_Priority is the priority value set by pragma Priority in the main
131 -- program. If no such pragma is present, the value is -1.
133 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
134 -- main program, or by the use of a -Tnnn parameter for the binder (if both
135 -- are present, the binder value overrides). The value is in milliseconds.
136 -- A value of zero indicates that time slicing should be suppressed. If no
137 -- pragma is present, and no -T switch was used, the value is -1.
139 -- WC_Encoding shows the wide character encoding method used for the main
140 -- program. This is one of the encoding letters defined in
141 -- System.WCh_Con.WC_Encoding_Letters.
143 -- Locking_Policy is a space if no locking policy was specified for the
144 -- partition. If a locking policy was specified, the value is the upper
145 -- case first character of the locking policy name, for example, 'C' for
148 -- Queuing_Policy is a space if no queuing policy was specified for the
149 -- partition. If a queuing policy was specified, the value is the upper
150 -- case first character of the queuing policy name for example, 'F' for
153 -- Task_Dispatching_Policy is a space if no task dispatching policy was
154 -- specified for the partition. If a task dispatching policy was specified,
155 -- the value is the upper case first character of the policy name, e.g. 'F'
156 -- for FIFO_Within_Priorities.
158 -- Priority_Specific_Dispatching is the address of a string used to store
159 -- the task dispatching policy specified for the different priorities in
160 -- the partition. The length of this string is determined by the last
161 -- priority for which such a pragma applies (the string will be a null
162 -- string if no specific dispatching policies were used). If pragma were
163 -- present, the entries apply to the priorities in sequence from the first
164 -- priority. The value stored is the upper case first character of the
165 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
166 -- for those priority ranges not specified.
168 -- Num_Specific_Dispatching is the length of the
169 -- Priority_Specific_Dispatching string. It will be set to zero if no
170 -- Priority_Specific_Dispatching pragmas are present.
172 -- Restrictions is the address of a null-terminated string specifying the
173 -- restrictions information for the partition. The format is identical to
174 -- that of the parameter string found on R lines in ali files (see Lib.Writ
175 -- spec in lib-writ.ads for full details). The difference is that in this
176 -- context the values are the cumulative ones for the entire partition.
178 -- Interrupt_States is the address of a string used to specify the
179 -- cumulative results of Interrupt_State pragmas used in the partition.
180 -- The length of this string is determined by the last interrupt for which
181 -- such a pragma is given (the string will be a null string if no pragmas
182 -- were used). If pragma were present the entries apply to the interrupts
183 -- in sequence from the first interrupt, and are set to one of four
184 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
185 -- time, 's' for system, see description of Interrupt_State pragma for
188 -- Num_Interrupt_States is the length of the Interrupt_States string. It
189 -- will be set to zero if no Interrupt_State pragmas are present.
191 -- Unreserve_All_Interrupts is set to one if at least one unit in the
192 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
194 -- Exception_Tracebacks is set to one if the -E parameter was present
195 -- in the bind and to zero otherwise. Note that on some targets exception
196 -- tracebacks are provided by default, so a value of zero for this
197 -- parameter does not necessarily mean no trace backs are available.
199 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
200 -- this partition, and to zero if longjmp/setjmp exceptions are used.
203 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
204 -- not. A value of zero indicates that the pragma is not present, while a
205 -- value of 1 signals its presence in the partition.
207 -- Default_Stack_Size is the default stack size used when creating an Ada
208 -- task with no explicit Storize_Size clause.
210 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
211 -- disabled. A value of zero indicates that leap seconds are turned "off",
212 -- while a value of one signifies "on" status.
214 -----------------------
215 -- Local Subprograms --
216 -----------------------
218 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
219 -- Convenient shorthand used throughout
221 procedure Check_System_Restrictions_Used;
222 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
223 -- System.Restrictions is present in the partition, otherwise False).
225 procedure Gen_Adainit_Ada;
226 -- Generates the Adainit procedure (Ada code case)
228 procedure Gen_Adainit_C;
229 -- Generates the Adainit procedure (C code case)
231 procedure Gen_Adafinal_Ada;
232 -- Generate the Adafinal procedure (Ada code case)
234 procedure Gen_Adafinal_C;
235 -- Generate the Adafinal procedure (C code case)
237 procedure Gen_Elab_Calls_Ada;
238 -- Generate sequence of elaboration calls (Ada code case)
240 procedure Gen_Elab_Calls_C;
241 -- Generate sequence of elaboration calls (C code case)
243 procedure Gen_Elab_Order_Ada;
244 -- Generate comments showing elaboration order chosen (Ada case)
246 procedure Gen_Elab_Order_C;
247 -- Generate comments showing elaboration order chosen (C case)
249 procedure Gen_Elab_Defs_C;
250 -- Generate sequence of definitions for elaboration routines (C code case)
252 procedure Gen_Main_Ada;
253 -- Generate procedure main (Ada code case)
255 procedure Gen_Main_C;
256 -- Generate main() procedure (C code case)
258 procedure Gen_Object_Files_Options;
259 -- Output comments containing a list of the full names of the object
260 -- files to be linked and the list of linker options supplied by
261 -- Linker_Options pragmas in the source. (C and Ada code case)
263 procedure Gen_Output_File_Ada (Filename : String);
264 -- Generate output file (Ada code case)
266 procedure Gen_Output_File_C (Filename : String);
267 -- Generate output file (C code case)
269 procedure Gen_Restrictions_Ada;
270 -- Generate initialization of restrictions variable (Ada code case)
272 procedure Gen_Restrictions_C;
273 -- Generate initialization of restrictions variable (C code case)
275 procedure Gen_Versions_Ada;
276 -- Output series of definitions for unit versions (Ada code case)
278 procedure Gen_Versions_C;
279 -- Output series of definitions for unit versions (C code case)
281 function Get_Ada_Main_Name return String;
282 -- This function is used in the Ada main output case to compute a usable
283 -- name for the generated main program. The normal main program name is
284 -- Ada_Main, but this won't work if the user has a unit with this name.
285 -- This function tries Ada_Main first, and if there is such a clash, then
286 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
288 function Get_Main_Unit_Name (S : String) return String;
289 -- Return the main unit name corresponding to S by replacing '.' with '_'
291 function Get_Main_Name return String;
292 -- This function is used in the Ada main output case to compute the
293 -- correct external main program. It is "main" by default, unless the
294 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
295 -- is the name of the Ada main name without the "_ada". This default
296 -- can be overridden explicitly using the -Mname binder switch.
298 function Get_WC_Encoding return Character;
299 -- Return wide character encoding method to set as WC_Encoding in output.
300 -- If -W has been used, returns the specified encoding, otherwise returns
301 -- the encoding method used for the main program source. If there is no
302 -- main program source (-z switch used), returns brackets ('b').
304 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
305 -- Compare linker options, when sorting, first according to
306 -- Is_Internal_File (internal files come later) and then by
307 -- elaboration order position (latest to earliest).
309 procedure Move_Linker_Option (From : Natural; To : Natural);
310 -- Move routine for sorting linker options
312 procedure Resolve_Binder_Options;
313 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
314 -- since it tests for a package named "dec" which might cause a conflict
315 -- on non-VMS systems.
317 procedure Set_Char (C : Character);
318 -- Set given character in Statement_Buffer at the Last + 1 position
319 -- and increment Last by one to reflect the stored character.
321 procedure Set_Int (N : Int);
322 -- Set given value in decimal in Statement_Buffer with no spaces
323 -- starting at the Last + 1 position, and updating Last past the value.
324 -- A minus sign is output for a negative value.
326 procedure Set_Boolean (B : Boolean);
327 -- Set given boolean value in Statement_Buffer at the Last + 1 position
328 -- and update Last past the value.
330 procedure Set_IS_Pragma_Table;
331 -- Initializes contents of IS_Pragma_Settings table from ALI table
333 procedure Set_Main_Program_Name;
334 -- Given the main program name in Name_Buffer (length in Name_Len)
335 -- generate the name of the routine to be used in the call. The name
336 -- is generated starting at Last + 1, and Last is updated past it.
338 procedure Set_Name_Buffer;
339 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
341 procedure Set_PSD_Pragma_Table;
342 -- Initializes contents of PSD_Pragma_Settings table from ALI table
344 procedure Set_String (S : String);
345 -- Sets characters of given string in Statement_Buffer, starting at the
346 -- Last + 1 position, and updating last past the string value.
348 procedure Set_Unit_Name;
349 -- Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
350 -- starting at the Last + 1 position, and updating last past the value.
351 -- changing periods to double underscores, and updating Last appropriately.
353 procedure Set_Unit_Number (U : Unit_Id);
354 -- Sets unit number (first unit is 1, leading zeroes output to line
355 -- up all output unit numbers nicely as required by the value, and
356 -- by the total number of units.
358 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
359 -- For C code case, write C & Common, for Ada case write Ada & Common
360 -- to current binder output file using Write_Binder_Info.
362 procedure Write_Statement_Buffer;
363 -- Write out contents of statement buffer up to Last, and reset Last to 0
365 procedure Write_Statement_Buffer (S : String);
366 -- First writes its argument (using Set_String (S)), then writes out the
367 -- contents of statement buffer up to Last, and reset Last to 0
369 ------------------------------------
370 -- Check_System_Restrictions_Used --
371 ------------------------------------
373 procedure Check_System_Restrictions_Used is
375 for J in Units.First .. Units.Last loop
376 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
377 System_Restrictions_Used := True;
382 System_Restrictions_Used := False;
383 end Check_System_Restrictions_Used;
385 ----------------------
386 -- Gen_Adafinal_Ada --
387 ----------------------
389 procedure Gen_Adafinal_Ada is
392 WBI (" procedure " & Ada_Final_Name.all & " is");
395 -- If compiling for the JVM, we directly call Adafinal because
396 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
398 if VM_Target /= No_VM then
399 WBI (" System.Standard_Library.Adafinal;");
401 -- If there is no finalization, there is nothing to do
403 elsif Cumulative_Restrictions.Set (No_Finalization) then
406 WBI (" Do_Finalize;");
409 WBI (" end " & Ada_Final_Name.all & ";");
410 end Gen_Adafinal_Ada;
416 procedure Gen_Adafinal_C is
418 WBI ("void " & Ada_Final_Name.all & " (void) {");
419 WBI (" system__standard_library__adafinal ();");
424 ---------------------
425 -- Gen_Adainit_Ada --
426 ---------------------
428 procedure Gen_Adainit_Ada is
429 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
432 WBI (" procedure " & Ada_Init_Name.all & " is");
434 -- Generate externals for elaboration entities
436 for E in Elab_Order.First .. Elab_Order.Last loop
438 Unum : constant Unit_Id := Elab_Order.Table (E);
439 U : Unit_Record renames Units.Table (Unum);
442 -- Check for Elab_Entity to be set for this unit
446 -- Don't generate reference for stand alone library
448 and then not U.SAL_Interface
450 -- Don't generate reference for predefined file in No_Run_Time
451 -- mode, since we don't include the object files in this case
455 and then Is_Predefined_File_Name (U.Sfile))
459 Set_Unit_Number (Unum);
462 when No_VM | JVM_Target =>
463 Set_String (" : Boolean; pragma Import (Ada, ");
465 Set_String (" : Boolean; pragma Import (CIL, ");
469 Set_Unit_Number (Unum);
471 Get_Name_String (U.Uname);
473 -- In the case of JGNAT we need to emit an Import name
474 -- that includes the class name (using '$' separators
475 -- in the case of a child unit name).
477 if VM_Target /= No_VM then
478 for J in 1 .. Name_Len - 2 loop
479 if VM_Target = CLI_Target
480 or else Name_Buffer (J) /= '.'
482 Set_Char (Name_Buffer (J));
488 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
491 Set_String ("_pkg.");
494 -- If the unit name is very long, then split the
495 -- Import link name across lines using "&" (occurs
496 -- in some C2 tests).
498 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
500 Write_Statement_Buffer;
506 Set_String ("_E"");");
507 Write_Statement_Buffer;
512 Write_Statement_Buffer;
514 -- If the standard library is suppressed, then the only global variable
515 -- that might be needed (by the Ravenscar profile) is the priority of
518 if Suppress_Standard_Library_On_Target then
519 if Main_Priority /= No_Main_Priority then
520 WBI (" Main_Priority : Integer;");
521 WBI (" pragma Import (C, Main_Priority," &
522 " ""__gl_main_priority"");");
528 if Main_Priority /= No_Main_Priority then
529 Set_String (" Main_Priority := ");
530 Set_Int (Main_Priority);
532 Write_Statement_Buffer;
538 -- Normal case (standard library not suppressed). Set all global values
539 -- used by the run time.
542 WBI (" Main_Priority : Integer;");
543 WBI (" pragma Import (C, Main_Priority, " &
544 """__gl_main_priority"");");
545 WBI (" Time_Slice_Value : Integer;");
546 WBI (" pragma Import (C, Time_Slice_Value, " &
547 """__gl_time_slice_val"");");
548 WBI (" WC_Encoding : Character;");
549 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
550 WBI (" Locking_Policy : Character;");
551 WBI (" pragma Import (C, Locking_Policy, " &
552 """__gl_locking_policy"");");
553 WBI (" Queuing_Policy : Character;");
554 WBI (" pragma Import (C, Queuing_Policy, " &
555 """__gl_queuing_policy"");");
556 WBI (" Task_Dispatching_Policy : Character;");
557 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
558 """__gl_task_dispatching_policy"");");
559 WBI (" Priority_Specific_Dispatching : System.Address;");
560 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
561 """__gl_priority_specific_dispatching"");");
562 WBI (" Num_Specific_Dispatching : Integer;");
563 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
564 """__gl_num_specific_dispatching"");");
566 WBI (" Interrupt_States : System.Address;");
567 WBI (" pragma Import (C, Interrupt_States, " &
568 """__gl_interrupt_states"");");
569 WBI (" Num_Interrupt_States : Integer;");
570 WBI (" pragma Import (C, Num_Interrupt_States, " &
571 """__gl_num_interrupt_states"");");
572 WBI (" Unreserve_All_Interrupts : Integer;");
573 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
574 """__gl_unreserve_all_interrupts"");");
576 if Exception_Tracebacks then
577 WBI (" Exception_Tracebacks : Integer;");
578 WBI (" pragma Import (C, Exception_Tracebacks, " &
579 """__gl_exception_tracebacks"");");
582 WBI (" Zero_Cost_Exceptions : Integer;");
583 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
584 """__gl_zero_cost_exceptions"");");
585 WBI (" Detect_Blocking : Integer;");
586 WBI (" pragma Import (C, Detect_Blocking, " &
587 """__gl_detect_blocking"");");
588 WBI (" Default_Stack_Size : Integer;");
589 WBI (" pragma Import (C, Default_Stack_Size, " &
590 """__gl_default_stack_size"");");
591 WBI (" Leap_Seconds_Support : Integer;");
592 WBI (" pragma Import (C, Leap_Seconds_Support, " &
593 """__gl_leap_seconds_support"");");
595 -- Import entry point for elaboration time signal handler
596 -- installation, and indication of if it's been called previously.
599 WBI (" procedure Install_Handler;");
600 WBI (" pragma Import (C, Install_Handler, " &
601 """__gnat_install_handler"");");
603 WBI (" Handler_Installed : Integer;");
604 WBI (" pragma Import (C, Handler_Installed, " &
605 """__gnat_handler_installed"");");
607 -- Initialize stack limit variable of the environment task if the
608 -- stack check method is stack limit and stack check is enabled.
610 if Stack_Check_Limits_On_Target
611 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
614 WBI (" procedure Initialize_Stack_Limit;");
615 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
616 """__gnat_initialize_stack_limit"");");
619 -- Special processing when main program is CIL function/procedure
621 if VM_Target = CLI_Target
622 and then Bind_Main_Program
623 and then not No_Main_Subprogram
627 -- Function case, use Set_Exit_Status to report the returned
628 -- status code, since that is the only mechanism available.
630 if ALIs.Table (ALIs.First).Main_Program = Func then
631 WBI (" Result : Integer;");
632 WBI (" procedure Set_Exit_Status (Code : Integer);");
633 WBI (" pragma Import (C, Set_Exit_Status, " &
634 """__gnat_set_exit_status"");");
636 WBI (" function Ada_Main_Program return Integer;");
641 WBI (" procedure Ada_Main_Program;");
644 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
645 Name_Len := Name_Len - 2;
646 WBI (" pragma Import (CIL, Ada_Main_Program, """
647 & Name_Buffer (1 .. Name_Len) & "."
648 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
653 Set_String (" Main_Priority := ");
654 Set_Int (Main_Priority);
656 Write_Statement_Buffer;
658 Set_String (" Time_Slice_Value := ");
660 if Task_Dispatching_Policy_Specified = 'F'
661 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
665 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
669 Write_Statement_Buffer;
671 Set_String (" WC_Encoding := '");
672 Set_Char (Get_WC_Encoding);
675 Write_Statement_Buffer;
677 Set_String (" Locking_Policy := '");
678 Set_Char (Locking_Policy_Specified);
680 Write_Statement_Buffer;
682 Set_String (" Queuing_Policy := '");
683 Set_Char (Queuing_Policy_Specified);
685 Write_Statement_Buffer;
687 Set_String (" Task_Dispatching_Policy := '");
688 Set_Char (Task_Dispatching_Policy_Specified);
690 Write_Statement_Buffer;
692 Gen_Restrictions_Ada;
694 WBI (" Priority_Specific_Dispatching :=");
695 WBI (" Local_Priority_Specific_Dispatching'Address;");
697 Set_String (" Num_Specific_Dispatching := ");
698 Set_Int (PSD_Pragma_Settings.Last + 1);
700 Write_Statement_Buffer;
702 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
704 Set_String (" Num_Interrupt_States := ");
705 Set_Int (IS_Pragma_Settings.Last + 1);
707 Write_Statement_Buffer;
709 Set_String (" Unreserve_All_Interrupts := ");
711 if Unreserve_All_Interrupts_Specified then
718 Write_Statement_Buffer;
720 if Exception_Tracebacks then
721 WBI (" Exception_Tracebacks := 1;");
724 Set_String (" Zero_Cost_Exceptions := ");
726 if Zero_Cost_Exceptions_Specified then
733 Write_Statement_Buffer;
735 Set_String (" Detect_Blocking := ");
737 if Detect_Blocking then
744 Write_Statement_Buffer;
746 Set_String (" Default_Stack_Size := ");
747 Set_Int (Default_Stack_Size);
749 Write_Statement_Buffer;
751 Set_String (" Leap_Seconds_Support := ");
753 if Leap_Seconds_Support then
760 Write_Statement_Buffer;
762 -- Generate call to Install_Handler
765 WBI (" if Handler_Installed = 0 then");
766 WBI (" Install_Handler;");
770 -- Generate call to set Initialize_Scalar values if active
772 if Initialize_Scalars_Used then
774 Set_String (" System.Scalar_Values.Initialize ('");
775 Set_Char (Initialize_Scalars_Mode1);
777 Set_Char (Initialize_Scalars_Mode2);
779 Write_Statement_Buffer;
782 -- Generate assignment of default secondary stack size if set
784 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
786 Set_String (" System.Secondary_Stack.");
787 Set_String ("Default_Secondary_Stack_Size := ");
788 Set_Int (Opt.Default_Sec_Stack_Size);
790 Write_Statement_Buffer;
793 -- Initialize stack limit variable of the environment task if the
794 -- stack check method is stack limit and stack check is enabled.
796 if Stack_Check_Limits_On_Target
797 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
800 WBI (" Initialize_Stack_Limit;");
803 -- Generate elaboration calls
808 -- Case of main program is CIL function or procedure
810 if VM_Target = CLI_Target
811 and then Bind_Main_Program
812 and then not No_Main_Subprogram
814 -- For function case, use Set_Exit_Status to set result
816 if ALIs.Table (ALIs.First).Main_Program = Func then
817 WBI (" Result := Ada_Main_Program;");
818 WBI (" Set_Exit_Status (Result);");
823 WBI (" Ada_Main_Program;");
827 WBI (" end " & Ada_Init_Name.all & ";");
834 procedure Gen_Adainit_C is
835 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
838 WBI ("void " & Ada_Init_Name.all & " (void)");
841 -- Generate externals for elaboration entities
843 for E in Elab_Order.First .. Elab_Order.Last loop
845 Unum : constant Unit_Id := Elab_Order.Table (E);
846 U : Unit_Record renames Units.Table (Unum);
849 -- Check for Elab entity to be set for this unit
853 -- Don't generate reference for stand alone library
855 and then not U.SAL_Interface
857 -- Don't generate reference for predefined file in No_Run_Time
858 -- mode, since we don't include the object files in this case
862 and then Is_Predefined_File_Name (U.Sfile))
864 Set_String (" extern char ");
865 Get_Name_String (U.Uname);
868 Write_Statement_Buffer;
873 Write_Statement_Buffer;
875 -- Standard library suppressed
877 if Suppress_Standard_Library_On_Target then
879 -- Case of High_Integrity_Mode mode. Set __gl_main_priority if needed
880 -- for the Ravenscar profile.
882 if Main_Priority /= No_Main_Priority then
883 WBI (" extern int __gl_main_priority;");
884 Set_String (" __gl_main_priority = ");
885 Set_Int (Main_Priority);
887 Write_Statement_Buffer;
890 -- Normal case (standard library not suppressed)
893 -- Generate definition for interrupt states string
895 Set_String (" static const char *local_interrupt_states = """);
897 for J in 0 .. IS_Pragma_Settings.Last loop
898 Set_Char (IS_Pragma_Settings.Table (J));
902 Write_Statement_Buffer;
904 -- Generate definition for priority specific dispatching string
907 (" static const char *local_priority_specific_dispatching = """);
909 for J in 0 .. PSD_Pragma_Settings.Last loop
910 Set_Char (PSD_Pragma_Settings.Table (J));
914 Write_Statement_Buffer;
916 -- Generate declaration for secondary stack default if needed
918 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
919 WBI (" extern int system__secondary_stack__" &
920 "default_secondary_stack_size;");
925 -- Code for normal case (standard library not suppressed)
927 -- We call the routine from inside adainit() because this works for
928 -- both programs with and without binder generated "main" functions.
930 WBI (" extern int __gl_main_priority;");
931 Set_String (" __gl_main_priority = ");
932 Set_Int (Main_Priority);
934 Write_Statement_Buffer;
936 WBI (" extern int __gl_time_slice_val;");
937 Set_String (" __gl_time_slice_val = ");
939 if Task_Dispatching_Policy = 'F'
940 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
944 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
948 Write_Statement_Buffer;
950 WBI (" extern char __gl_wc_encoding;");
951 Set_String (" __gl_wc_encoding = '");
952 Set_Char (Get_WC_Encoding);
955 Write_Statement_Buffer;
957 WBI (" extern char __gl_locking_policy;");
958 Set_String (" __gl_locking_policy = '");
959 Set_Char (Locking_Policy_Specified);
961 Write_Statement_Buffer;
963 WBI (" extern char __gl_queuing_policy;");
964 Set_String (" __gl_queuing_policy = '");
965 Set_Char (Queuing_Policy_Specified);
967 Write_Statement_Buffer;
969 WBI (" extern char __gl_task_dispatching_policy;");
970 Set_String (" __gl_task_dispatching_policy = '");
971 Set_Char (Task_Dispatching_Policy_Specified);
973 Write_Statement_Buffer;
977 WBI (" extern const void *__gl_interrupt_states;");
978 WBI (" __gl_interrupt_states = local_interrupt_states;");
980 WBI (" extern int __gl_num_interrupt_states;");
981 Set_String (" __gl_num_interrupt_states = ");
982 Set_Int (IS_Pragma_Settings.Last + 1);
984 Write_Statement_Buffer;
986 WBI (" extern const void *__gl_priority_specific_dispatching;");
987 WBI (" __gl_priority_specific_dispatching =" &
988 " local_priority_specific_dispatching;");
990 WBI (" extern int __gl_num_specific_dispatching;");
991 Set_String (" __gl_num_specific_dispatching = ");
992 Set_Int (PSD_Pragma_Settings.Last + 1);
994 Write_Statement_Buffer;
996 WBI (" extern int __gl_unreserve_all_interrupts;");
997 Set_String (" __gl_unreserve_all_interrupts = ");
998 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
1000 Write_Statement_Buffer;
1002 if Exception_Tracebacks then
1003 WBI (" extern int __gl_exception_tracebacks;");
1004 WBI (" __gl_exception_tracebacks = 1;");
1007 WBI (" extern int __gl_zero_cost_exceptions;");
1008 Set_String (" __gl_zero_cost_exceptions = ");
1009 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
1011 Write_Statement_Buffer;
1013 WBI (" extern int __gl_detect_blocking;");
1014 Set_String (" __gl_detect_blocking = ");
1016 if Detect_Blocking then
1023 Write_Statement_Buffer;
1025 WBI (" extern int __gl_default_stack_size;");
1026 Set_String (" __gl_default_stack_size = ");
1027 Set_Int (Default_Stack_Size);
1029 Write_Statement_Buffer;
1031 WBI (" extern int __gl_leap_seconds_support;");
1032 Set_String (" __gl_leap_seconds_support = ");
1034 if Leap_Seconds_Support then
1041 Write_Statement_Buffer;
1045 -- Install elaboration time signal handler
1047 WBI (" if (__gnat_handler_installed == 0)");
1049 WBI (" __gnat_install_handler ();");
1053 -- Initialize stack limit for the environment task if the stack
1054 -- check method is stack limit and stack check is enabled.
1056 if Stack_Check_Limits_On_Target
1057 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1060 WBI (" __gnat_initialize_stack_limit ();");
1063 -- Generate call to set Initialize_Scalar values if needed
1065 if Initialize_Scalars_Used then
1067 Set_String (" system__scalar_values__initialize('");
1068 Set_Char (Initialize_Scalars_Mode1);
1069 Set_String ("', '");
1070 Set_Char (Initialize_Scalars_Mode2);
1072 Write_Statement_Buffer;
1075 -- Generate assignment of default secondary stack size if set
1077 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1079 Set_String (" system__secondary_stack__");
1080 Set_String ("default_secondary_stack_size = ");
1081 Set_Int (Opt.Default_Sec_Stack_Size);
1083 Write_Statement_Buffer;
1086 -- Generate elaboration calls
1093 ------------------------
1094 -- Gen_Elab_Calls_Ada --
1095 ------------------------
1097 procedure Gen_Elab_Calls_Ada is
1099 for E in Elab_Order.First .. Elab_Order.Last loop
1101 Unum : constant Unit_Id := Elab_Order.Table (E);
1102 U : Unit_Record renames Units.Table (Unum);
1104 Unum_Spec : Unit_Id;
1105 -- This is the unit number of the spec that corresponds to
1106 -- this entry. It is the same as Unum except when the body
1107 -- and spec are different and we are currently processing
1108 -- the body, in which case it is the spec (Unum + 1).
1111 if U.Utype = Is_Body then
1112 Unum_Spec := Unum + 1;
1117 -- Nothing to do if predefined unit in no run time mode
1119 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1122 -- Case of no elaboration code
1124 elsif U.No_Elab then
1126 -- The only case in which we have to do something is if
1127 -- this is a body, with a separate spec, where the separate
1128 -- spec has an elaboration entity defined.
1130 -- In that case, this is where we set the elaboration entity
1131 -- to True, we do not need to test if this has already been
1132 -- done, since it is quicker to set the flag than to test it.
1134 if not U.SAL_Interface and then U.Utype = Is_Body
1135 and then Units.Table (Unum_Spec).Set_Elab_Entity
1138 Set_Unit_Number (Unum_Spec);
1139 Set_String (" := True;");
1140 Write_Statement_Buffer;
1143 -- Here if elaboration code is present. If binding a library
1144 -- or if there is a non-Ada main subprogram then we generate:
1146 -- if not uname_E then
1147 -- uname'elab_[spec|body];
1151 -- Otherwise, elaboration routines are called unconditionally:
1153 -- uname'elab_[spec|body];
1156 -- The uname_E assignment is skipped if this is a separate spec,
1157 -- since the assignment will be done when we process the body.
1159 elsif not U.SAL_Interface then
1160 if Force_Checking_Of_Elaboration_Flags or
1161 Interface_Library_Unit or
1162 (not Bind_Main_Program)
1164 Set_String (" if not E");
1165 Set_Unit_Number (Unum_Spec);
1166 Set_String (" then");
1167 Write_Statement_Buffer;
1172 Get_Decoded_Name_String_With_Brackets (U.Uname);
1174 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1175 if Name_Buffer (Name_Len) = 's' then
1176 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1179 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1183 Name_Len := Name_Len + 12;
1186 if Name_Buffer (Name_Len) = 's' then
1187 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1190 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1194 Name_Len := Name_Len + 8;
1197 Set_Casing (U.Icasing);
1200 Write_Statement_Buffer;
1202 if U.Utype /= Is_Spec then
1203 if Force_Checking_Of_Elaboration_Flags or
1204 Interface_Library_Unit or
1205 (not Bind_Main_Program)
1211 Set_Unit_Number (Unum_Spec);
1212 Set_String (" := True;");
1213 Write_Statement_Buffer;
1216 if Force_Checking_Of_Elaboration_Flags or
1217 Interface_Library_Unit or
1218 (not Bind_Main_Program)
1225 end Gen_Elab_Calls_Ada;
1227 ----------------------
1228 -- Gen_Elab_Calls_C --
1229 ----------------------
1231 procedure Gen_Elab_Calls_C is
1233 for E in Elab_Order.First .. Elab_Order.Last loop
1235 Unum : constant Unit_Id := Elab_Order.Table (E);
1236 U : Unit_Record renames Units.Table (Unum);
1238 Unum_Spec : Unit_Id;
1239 -- This is the unit number of the spec that corresponds to
1240 -- this entry. It is the same as Unum except when the body
1241 -- and spec are different and we are currently processing
1242 -- the body, in which case it is the spec (Unum + 1).
1245 if U.Utype = Is_Body then
1246 Unum_Spec := Unum + 1;
1251 -- Nothing to do if predefined unit in no run time mode
1253 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1256 -- Case of no elaboration code
1258 elsif U.No_Elab then
1260 -- The only case in which we have to do something is if
1261 -- this is a body, with a separate spec, where the separate
1262 -- spec has an elaboration entity defined.
1264 -- In that case, this is where we set the elaboration entity
1265 -- to True, we do not need to test if this has already been
1266 -- done, since it is quicker to set the flag than to test it.
1268 if not U.SAL_Interface and then U.Utype = Is_Body
1269 and then Units.Table (Unum_Spec).Set_Elab_Entity
1272 Get_Name_String (U.Uname);
1274 Set_String ("_E = 1;");
1275 Write_Statement_Buffer;
1278 -- Here if elaboration code is present. If binding a library
1279 -- or if there is a non-Ada main subprogram then we generate:
1281 -- if (uname_E == 0) {
1282 -- uname__elab[s|b] ();
1286 -- The uname_E assignment is skipped if this is a separate spec,
1287 -- since the assignment will be done when we process the body.
1289 elsif not U.SAL_Interface then
1290 Get_Name_String (U.Uname);
1292 if Force_Checking_Of_Elaboration_Flags or
1293 Interface_Library_Unit or
1294 (not Bind_Main_Program)
1296 Set_String (" if (");
1298 Set_String ("_E == 0) {");
1299 Write_Statement_Buffer;
1305 Set_String ("___elab");
1306 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1307 Set_String (" ();");
1308 Write_Statement_Buffer;
1310 if U.Utype /= Is_Spec then
1311 if Force_Checking_Of_Elaboration_Flags or
1312 Interface_Library_Unit or
1313 (not Bind_Main_Program)
1320 Set_String ("_E++;");
1321 Write_Statement_Buffer;
1324 if Force_Checking_Of_Elaboration_Flags or
1325 Interface_Library_Unit or
1326 (not Bind_Main_Program)
1334 end Gen_Elab_Calls_C;
1336 ----------------------
1337 -- Gen_Elab_Defs_C --
1338 ----------------------
1340 procedure Gen_Elab_Defs_C is
1342 for E in Elab_Order.First .. Elab_Order.Last loop
1344 -- Generate declaration of elaboration procedure if elaboration
1345 -- needed. Note that passive units are always excluded.
1347 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1348 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1349 Set_String ("extern void ");
1351 Set_String ("___elab");
1352 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1353 Set_String (" (void);");
1354 Write_Statement_Buffer;
1360 end Gen_Elab_Defs_C;
1362 ------------------------
1363 -- Gen_Elab_Order_Ada --
1364 ------------------------
1366 procedure Gen_Elab_Order_Ada is
1369 WBI (" -- BEGIN ELABORATION ORDER");
1371 for J in Elab_Order.First .. Elab_Order.Last loop
1372 Set_String (" -- ");
1373 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1375 Write_Statement_Buffer;
1378 WBI (" -- END ELABORATION ORDER");
1379 end Gen_Elab_Order_Ada;
1381 ----------------------
1382 -- Gen_Elab_Order_C --
1383 ----------------------
1385 procedure Gen_Elab_Order_C is
1388 WBI ("/* BEGIN ELABORATION ORDER");
1390 for J in Elab_Order.First .. Elab_Order.Last loop
1391 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1393 Write_Statement_Buffer;
1396 WBI (" END ELABORATION ORDER */");
1397 end Gen_Elab_Order_C;
1403 procedure Gen_Main_Ada is
1407 if Exit_Status_Supported_On_Target then
1408 Set_String (" function ");
1410 Set_String (" procedure ");
1413 Set_String (Get_Main_Name);
1415 if Command_Line_Args_On_Target then
1416 Write_Statement_Buffer;
1417 WBI (" (argc : Integer;");
1418 WBI (" argv : System.Address;");
1419 WBI (" envp : System.Address)");
1421 if Exit_Status_Supported_On_Target then
1422 WBI (" return Integer");
1428 if Exit_Status_Supported_On_Target then
1429 Set_String (" return Integer is");
1434 Write_Statement_Buffer;
1437 if Opt.Default_Exit_Status /= 0
1438 and then Bind_Main_Program
1439 and then not Configurable_Run_Time_Mode
1441 WBI (" procedure Set_Exit_Status (Status : Integer);");
1442 WBI (" pragma Import (C, Set_Exit_Status, " &
1443 """__gnat_set_exit_status"");");
1447 -- Initialize and Finalize
1449 if not Cumulative_Restrictions.Set (No_Finalization) then
1450 WBI (" procedure initialize (Addr : System.Address);");
1451 WBI (" pragma Import (C, initialize, ""__gnat_initialize"");");
1453 WBI (" procedure finalize;");
1454 WBI (" pragma Import (C, finalize, ""__gnat_finalize"");");
1457 -- If we want to analyze the stack, we have to import corresponding
1460 if Dynamic_Stack_Measurement then
1462 WBI (" procedure Output_Results;");
1463 WBI (" pragma Import (C, Output_Results, " &
1464 """__gnat_stack_usage_output_results"");");
1468 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1469 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1470 """__gnat_stack_usage_initialize"");");
1473 -- Deal with declarations for main program case
1475 if not No_Main_Subprogram then
1477 -- To call the main program, we declare it using a pragma Import
1478 -- Ada with the right link name.
1480 -- It might seem more obvious to "with" the main program, and call
1481 -- it in the normal Ada manner. We do not do this for three reasons:
1483 -- 1. It is more efficient not to recompile the main program
1484 -- 2. We are not entitled to assume the source is accessible
1485 -- 3. We don't know what options to use to compile it
1487 -- It is really reason 3 that is most critical (indeed we used
1488 -- to generate the "with", but several regression tests failed).
1492 if ALIs.Table (ALIs.First).Main_Program = Func then
1493 WBI (" Result : Integer;");
1495 WBI (" function Ada_Main_Program return Integer;");
1498 WBI (" procedure Ada_Main_Program;");
1501 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1502 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1503 Set_Main_Program_Name;
1504 Set_String (""");");
1506 Write_Statement_Buffer;
1509 if Bind_Main_Program
1510 and then not Suppress_Standard_Library_On_Target
1512 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1517 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1518 -- not referenced elsewhere in the generated program, but is needed
1519 -- by the debugger (that's why it is generated in the first place).
1520 -- The reference stops Ada_Main_Program_Name from being optimized
1521 -- away by smart linkers, such as the AiX linker.
1523 -- Because this variable is unused, we make this variable "aliased"
1524 -- with a pragma Volatile in order to tell the compiler to preserve
1525 -- this variable at any level of optimization.
1527 if Bind_Main_Program then
1529 (" Ensure_Reference : aliased System.Address := " &
1530 "Ada_Main_Program_Name'Address;");
1531 WBI (" pragma Volatile (Ensure_Reference);");
1537 -- Acquire command line arguments if present on target
1539 if Command_Line_Args_On_Target then
1540 WBI (" gnat_argc := argc;");
1541 WBI (" gnat_argv := argv;");
1542 WBI (" gnat_envp := envp;");
1545 -- If configurable run time and no command line args, then nothing
1546 -- needs to be done since the gnat_argc/argv/envp variables are
1547 -- suppressed in this case.
1549 elsif Configurable_Run_Time_On_Target then
1552 -- Otherwise set dummy values (to be filled in by some other unit?)
1555 WBI (" gnat_argc := 0;");
1556 WBI (" gnat_argv := System.Null_Address;");
1557 WBI (" gnat_envp := System.Null_Address;");
1560 if Opt.Default_Exit_Status /= 0
1561 and then Bind_Main_Program
1562 and then not Configurable_Run_Time_Mode
1564 Set_String (" Set_Exit_Status (");
1565 Set_Int (Opt.Default_Exit_Status);
1567 Write_Statement_Buffer;
1570 if Dynamic_Stack_Measurement then
1571 Set_String (" Initialize_Stack_Analysis (");
1572 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1574 Write_Statement_Buffer;
1577 if not Cumulative_Restrictions.Set (No_Finalization) then
1578 if not No_Main_Subprogram
1579 and then Bind_Main_Program
1580 and then not Suppress_Standard_Library_On_Target
1582 WBI (" Initialize (SEH'Address);");
1584 WBI (" Initialize (System.Null_Address);");
1588 WBI (" " & Ada_Init_Name.all & ";");
1590 if not No_Main_Subprogram then
1591 WBI (" Break_Start;");
1593 if ALIs.Table (ALIs.First).Main_Program = Proc then
1594 WBI (" Ada_Main_Program;");
1596 WBI (" Result := Ada_Main_Program;");
1600 -- Adafinal call is skipped if no finalization
1602 if not Cumulative_Restrictions.Set (No_Finalization) then
1604 -- If compiling for the JVM, we directly call Adafinal because
1605 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1607 if VM_Target = No_VM then
1608 WBI (" Do_Finalize;");
1610 WBI (" System.Standard_Library.Adafinal;");
1614 -- Prints the result of static stack analysis
1616 if Dynamic_Stack_Measurement then
1617 WBI (" Output_Results;");
1620 -- Finalize is only called if we have a run time
1622 if not Cumulative_Restrictions.Set (No_Finalization) then
1628 if Exit_Status_Supported_On_Target then
1629 if No_Main_Subprogram
1630 or else ALIs.Table (ALIs.First).Main_Program = Proc
1632 WBI (" return (gnat_exit_status);");
1634 WBI (" return (Result);");
1645 procedure Gen_Main_C is
1647 if Exit_Status_Supported_On_Target then
1648 WBI ("#include <stdlib.h>");
1649 Set_String ("int ");
1651 Set_String ("void ");
1654 Set_String (Get_Main_Name);
1656 -- Generate command line args in prototype if present on target
1658 if Command_Line_Args_On_Target then
1659 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
1661 -- Case of no command line arguments on target
1664 Write_Statement_Buffer (" (void)");
1669 -- Generate a reference to __gnat_ada_main_program_name. This symbol
1670 -- is not referenced elsewhere in the generated program, but is
1671 -- needed by the debugger (that's why it is generated in the first
1672 -- place). The reference stops Ada_Main_Program_Name from being
1673 -- optimized away by smart linkers, such as the AiX linker.
1675 -- Because this variable is unused, we declare this variable as
1676 -- volatile in order to tell the compiler to preserve it at any
1677 -- level of optimization.
1679 if Bind_Main_Program then
1680 WBI (" char * volatile ensure_reference " &
1681 "__attribute__ ((__unused__)) = " &
1682 "__gnat_ada_main_program_name;");
1685 if not Suppress_Standard_Library_On_Target
1686 and then not No_Main_Subprogram
1688 WBI (" int SEH [2];");
1693 -- If main program is a function, generate result variable
1695 if ALIs.Table (ALIs.First).Main_Program = Func then
1696 WBI (" int result;");
1699 -- Set command line argument values from parameters if command line
1700 -- arguments are present on target
1702 if Command_Line_Args_On_Target then
1703 WBI (" gnat_argc = argc;");
1704 WBI (" gnat_argv = argv;");
1705 WBI (" gnat_envp = envp;");
1708 -- If configurable run-time, then nothing to do, since in this case
1709 -- the gnat_argc/argv/envp variables are entirely suppressed.
1711 elsif Configurable_Run_Time_On_Target then
1714 -- if no command line arguments on target, set dummy values
1717 WBI (" gnat_argc = 0;");
1718 WBI (" gnat_argv = 0;");
1719 WBI (" gnat_envp = 0;");
1722 if Opt.Default_Exit_Status /= 0
1723 and then Bind_Main_Program
1724 and then not Configurable_Run_Time_Mode
1726 Set_String (" __gnat_set_exit_status (");
1727 Set_Int (Opt.Default_Exit_Status);
1729 Write_Statement_Buffer;
1732 -- Initializes dynamic stack measurement if needed
1734 if Dynamic_Stack_Measurement then
1735 Set_String (" __gnat_stack_usage_initialize (");
1736 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1738 Write_Statement_Buffer;
1741 -- The __gnat_initialize routine is used only if we have a run-time
1743 if not Suppress_Standard_Library_On_Target then
1744 if not No_Main_Subprogram and then Bind_Main_Program then
1745 WBI (" __gnat_initialize ((void *)SEH);");
1747 WBI (" __gnat_initialize ((void *)0);");
1751 WBI (" " & Ada_Init_Name.all & " ();");
1753 if not No_Main_Subprogram then
1754 WBI (" __gnat_break_start ();");
1757 -- Output main program name
1759 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1761 -- Main program is procedure case
1763 if ALIs.Table (ALIs.First).Main_Program = Proc then
1765 Set_Main_Program_Name;
1766 Set_String (" ();");
1767 Write_Statement_Buffer;
1769 -- Main program is function case
1771 else -- ALIs.Table (ALIs_First).Main_Program = Func
1772 Set_String (" result = ");
1773 Set_Main_Program_Name;
1774 Set_String (" ();");
1775 Write_Statement_Buffer;
1780 -- Call adafinal if finalization active
1782 if not Cumulative_Restrictions.Set (No_Finalization) then
1784 WBI (" system__standard_library__adafinal ();");
1787 -- Outputs the dynamic stack measurement if needed
1789 if Dynamic_Stack_Measurement then
1790 WBI (" __gnat_stack_usage_output_results ();");
1793 -- The finalize routine is used only if we have a run-time
1795 if not Suppress_Standard_Library_On_Target then
1796 WBI (" __gnat_finalize ();");
1799 -- Case of main program is a function, so the value it returns
1800 -- is the exit status in this case.
1802 if ALIs.Table (ALIs.First).Main_Program = Func then
1803 if Exit_Status_Supported_On_Target then
1805 -- VMS must use Posix exit routine in order to get the effect
1806 -- of a Unix compatible setting of the program exit status.
1807 -- For all other systems, we use the standard exit routine.
1809 if OpenVMS_On_Target then
1810 WBI (" decc$__posix_exit (result);");
1812 WBI (" exit (result);");
1816 -- Case of main program is a procedure, in which case the exit
1817 -- status is whatever was set by a Set_Exit call most recently
1820 if Exit_Status_Supported_On_Target then
1822 -- VMS must use Posix exit routine in order to get the effect
1823 -- of a Unix compatible setting of the program exit status.
1824 -- For all other systems, we use the standard exit routine.
1826 if OpenVMS_On_Target then
1827 WBI (" decc$__posix_exit (gnat_exit_status);");
1829 WBI (" exit (gnat_exit_status);");
1837 ------------------------------
1838 -- Gen_Object_Files_Options --
1839 ------------------------------
1841 procedure Gen_Object_Files_Options is
1843 -- This keeps track of the position in the sorted set of entries
1844 -- in the Linker_Options table of where the first entry from an
1845 -- internal file appears.
1847 Linker_Option_List_Started : Boolean := False;
1848 -- Set to True when "LINKER OPTION LIST" is displayed
1850 procedure Write_Linker_Option;
1851 -- Write binder info linker option
1853 -------------------------
1854 -- Write_Linker_Option --
1855 -------------------------
1857 procedure Write_Linker_Option is
1862 -- Loop through string, breaking at null's
1865 while Start < Name_Len loop
1867 -- Find null ending this section
1870 while Name_Buffer (Stop) /= ASCII.NUL
1871 and then Stop <= Name_Len loop
1875 -- Process section if non-null
1877 if Stop > Start then
1878 if Output_Linker_Option_List then
1879 if not Zero_Formatting then
1880 if not Linker_Option_List_Started then
1881 Linker_Option_List_Started := True;
1883 Write_Str (" LINKER OPTION LIST");
1891 Write_Str (Name_Buffer (Start .. Stop - 1));
1895 (" -- ", "", Name_Buffer (Start .. Stop - 1));
1900 end Write_Linker_Option;
1902 -- Start of processing for Gen_Object_Files_Options
1906 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
1908 for E in Elab_Order.First .. Elab_Order.Last loop
1910 -- If not spec that has an associated body, then generate a
1911 -- comment giving the name of the corresponding object file.
1913 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
1914 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1918 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1920 -- If the presence of an object file is necessary or if it
1921 -- exists, then use it.
1923 if not Hostparm.Exclude_Missing_Objects
1925 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1927 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1929 if Output_Object_List then
1930 Write_Str (Name_Buffer (1 .. Name_Len));
1934 -- Don't link with the shared library on VMS if an internal
1935 -- filename object is seen. Multiply defined symbols will
1938 if OpenVMS_On_Target
1939 and then Is_Internal_File_Name
1941 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
1943 -- Special case for g-trasym.obj, which is not included
1946 Get_Name_String (ALIs.Table
1947 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
1949 if Name_Buffer (1 .. 8) /= "g-trasym" then
1950 Opt.Shared_Libgnat := False;
1957 -- Add a "-Ldir" for each directory in the object path
1959 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1961 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1964 Add_Str_To_Name_Buffer ("-L");
1965 Add_Str_To_Name_Buffer (Dir.all);
1966 Write_Linker_Option;
1970 -- Sort linker options
1972 -- This sort accomplishes two important purposes:
1974 -- a) All application files are sorted to the front, and all
1975 -- GNAT internal files are sorted to the end. This results
1976 -- in a well defined dividing line between the two sets of
1977 -- files, for the purpose of inserting certain standard
1978 -- library references into the linker arguments list.
1980 -- b) Given two different units, we sort the linker options so
1981 -- that those from a unit earlier in the elaboration order
1982 -- comes later in the list. This is a heuristic designed
1983 -- to create a more friendly order of linker options when
1984 -- the operations appear in separate units. The idea is that
1985 -- if unit A must be elaborated before unit B, then it is
1986 -- more likely that B references libraries included by A,
1987 -- than vice versa, so we want the libraries included by
1988 -- A to come after the libraries included by B.
1990 -- These two criteria are implemented by function Lt_Linker_Option.
1991 -- Note that a special case of b) is that specs are elaborated before
1992 -- bodies, so linker options from specs come after linker options
1993 -- for bodies, and again, the assumption is that libraries used by
1994 -- the body are more likely to reference libraries used by the spec,
1998 (Linker_Options.Last,
1999 Move_Linker_Option'Access,
2000 Lt_Linker_Option'Access);
2002 -- Write user linker options, i.e. the set of linker options that
2003 -- come from all files other than GNAT internal files, Lgnat is
2004 -- left set to point to the first entry from a GNAT internal file,
2005 -- or past the end of the entriers if there are no internal files.
2007 Lgnat := Linker_Options.Last + 1;
2009 for J in 1 .. Linker_Options.Last loop
2010 if not Linker_Options.Table (J).Internal_File then
2011 Get_Name_String (Linker_Options.Table (J).Name);
2012 Write_Linker_Option;
2019 -- Now we insert standard linker options that must appear after the
2020 -- entries from user files, and before the entries from GNAT run-time
2021 -- files. The reason for this decision is that libraries referenced
2022 -- by internal routines may reference these standard library entries.
2024 -- Note that we do not insert anything when pragma No_Run_Time has been
2025 -- specified or when the standard libraries are not to be used,
2026 -- otherwise on some platforms, such as VMS, we may get duplicate
2027 -- symbols when linking.
2029 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2032 if Opt.Shared_Libgnat then
2033 Add_Str_To_Name_Buffer ("-shared");
2035 Add_Str_To_Name_Buffer ("-static");
2038 -- Write directly to avoid -K output (why???)
2040 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2042 if With_DECGNAT then
2045 if Opt.Shared_Libgnat then
2046 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2048 Add_Str_To_Name_Buffer ("-ldecgnat");
2051 Write_Linker_Option;
2057 if Opt.Shared_Libgnat then
2058 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2060 Add_Str_To_Name_Buffer ("-lgnarl");
2063 Write_Linker_Option;
2068 if Opt.Shared_Libgnat then
2069 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2071 Add_Str_To_Name_Buffer ("-lgnat");
2074 Write_Linker_Option;
2077 -- Write linker options from all internal files
2079 for J in Lgnat .. Linker_Options.Last loop
2080 Get_Name_String (Linker_Options.Table (J).Name);
2081 Write_Linker_Option;
2084 if Output_Linker_Option_List and then not Zero_Formatting then
2088 if Ada_Bind_File then
2089 WBI ("-- END Object file/option list ");
2091 WBI (" END Object file/option list */");
2093 end Gen_Object_Files_Options;
2095 ---------------------
2096 -- Gen_Output_File --
2097 ---------------------
2099 procedure Gen_Output_File (Filename : String) is
2101 -- Acquire settings for Interrupt_State pragmas
2103 Set_IS_Pragma_Table;
2105 -- Acquire settings for Priority_Specific_Dispatching pragma
2107 Set_PSD_Pragma_Table;
2109 -- Override Ada_Bind_File and Bind_Main_Program for VMs since
2110 -- JGNAT only supports Ada code, and the main program is already
2111 -- generated by the compiler.
2113 if VM_Target /= No_VM then
2114 Ada_Bind_File := True;
2116 if VM_Target = JVM_Target then
2117 Bind_Main_Program := False;
2121 -- Override time slice value if -T switch is set
2123 if Time_Slice_Set then
2124 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2127 -- Count number of elaboration calls
2129 for E in Elab_Order.First .. Elab_Order.Last loop
2130 if Units.Table (Elab_Order.Table (E)).No_Elab then
2133 Num_Elab_Calls := Num_Elab_Calls + 1;
2137 -- Generate output file in appropriate language
2139 Check_System_Restrictions_Used;
2141 if Ada_Bind_File then
2142 Gen_Output_File_Ada (Filename);
2144 Gen_Output_File_C (Filename);
2146 end Gen_Output_File;
2148 -------------------------
2149 -- Gen_Output_File_Ada --
2150 -------------------------
2152 procedure Gen_Output_File_Ada (Filename : String) is
2155 -- Name of generated bind file (spec)
2158 -- Name of generated bind file (body)
2160 Ada_Main : constant String := Get_Ada_Main_Name;
2161 -- Name to be used for generated Ada main program. See the body of
2162 -- function Get_Ada_Main_Name for details on the form of the name.
2165 -- Create spec first
2167 Create_Binder_Output (Filename, 's', Bfiles);
2169 -- We always compile the binder file in Ada 95 mode so that we properly
2170 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2171 -- of the Ada 2005 constructs are needed by the binder file.
2173 WBI ("pragma Ada_95;");
2175 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2176 -- then we need to make sure that the binder program is compiled with
2177 -- the same restriction, so that no exception tables are generated.
2179 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2180 WBI ("pragma Restrictions (No_Exception_Handlers);");
2183 -- Same processing for Restrictions (No_Exception_Propagation)
2185 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2186 WBI ("pragma Restrictions (No_Exception_Propagation);");
2189 -- Same processing for pragma No_Run_Time
2191 if No_Run_Time_Mode then
2192 WBI ("pragma No_Run_Time;");
2195 -- Generate with of System so we can reference System.Address
2197 WBI ("with System;");
2199 -- Generate with of System.Initialize_Scalars if active
2201 if Initialize_Scalars_Used then
2202 WBI ("with System.Scalar_Values;");
2205 -- Generate with of System.Secondary_Stack if active
2207 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2208 WBI ("with System.Secondary_Stack;");
2211 Resolve_Binder_Options;
2213 if VM_Target /= No_VM then
2214 if not Suppress_Standard_Library_On_Target then
2216 -- Usually, adafinal is called using a pragma Import C. Since
2217 -- Import C doesn't have the same semantics for JGNAT, we use
2220 WBI ("with System.Standard_Library;");
2224 WBI ("package " & Ada_Main & " is");
2225 WBI (" pragma Warnings (Off);");
2227 -- Main program case
2229 if Bind_Main_Program then
2230 if VM_Target = No_VM then
2232 -- Generate argc/argv stuff unless suppressed
2234 if Command_Line_Args_On_Target
2235 or not Configurable_Run_Time_On_Target
2238 WBI (" gnat_argc : Integer;");
2239 WBI (" gnat_argv : System.Address;");
2240 WBI (" gnat_envp : System.Address;");
2242 -- If the standard library is not suppressed, these variables
2243 -- are in the runtime data area for easy access from the
2246 if not Suppress_Standard_Library_On_Target then
2248 WBI (" pragma Import (C, gnat_argc);");
2249 WBI (" pragma Import (C, gnat_argv);");
2250 WBI (" pragma Import (C, gnat_envp);");
2254 -- Define exit status. Again in normal mode, this is in the
2255 -- run-time library, and is initialized there, but in the
2256 -- configurable runtime case, the variable is declared and
2257 -- initialized in this file.
2261 if Configurable_Run_Time_Mode then
2262 if Exit_Status_Supported_On_Target then
2263 WBI (" gnat_exit_status : Integer := 0;");
2267 WBI (" gnat_exit_status : Integer;");
2268 WBI (" pragma Import (C, gnat_exit_status);");
2272 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2273 -- the main program. Otherwise, it can lead under some circumstances
2274 -- to a symbol duplication during the link (for instance when a C
2275 -- program uses two Ada libraries). Also zero terminate the string
2276 -- so that its end can be found reliably at run time.
2279 WBI (" GNAT_Version : constant String :=");
2280 WBI (" ""GNAT Version: " &
2281 Gnat_Version_String &
2283 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2286 Set_String (" Ada_Main_Program_Name : constant String := """);
2287 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2289 if VM_Target = No_VM then
2290 Set_Main_Program_Name;
2291 Set_String (""" & ASCII.NUL;");
2293 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2296 Write_Statement_Buffer;
2299 (" pragma Export (C, Ada_Main_Program_Name, " &
2300 """__gnat_ada_main_program_name"");");
2304 WBI (" procedure " & Ada_Final_Name.all & ";");
2305 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2306 Ada_Final_Name.all & """);");
2308 WBI (" procedure " & Ada_Init_Name.all & ";");
2309 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2310 Ada_Init_Name.all & """);");
2312 -- If -a has been specified use pragma Linker_Constructor for the init
2313 -- procedure. No need to use a similar pragma for the final procedure as
2314 -- global finalization will occur when the executable finishes execution
2315 -- and for plugins (shared stand-alone libraries that can be
2316 -- "unloaded"), finalization should not occur automatically, otherwise
2317 -- the main executable may not continue to work properly.
2319 if Use_Pragma_Linker_Constructor then
2320 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2323 if Bind_Main_Program and then VM_Target = No_VM then
2325 -- If we have the standard library, then Break_Start is defined
2326 -- there, but when the standard library is suppressed, Break_Start
2330 WBI (" procedure Break_Start;");
2332 if Suppress_Standard_Library_On_Target then
2333 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2335 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2340 if Exit_Status_Supported_On_Target then
2341 Set_String (" function ");
2343 Set_String (" procedure ");
2346 Set_String (Get_Main_Name);
2348 -- Generate argument list if present
2350 if Command_Line_Args_On_Target then
2351 Write_Statement_Buffer;
2352 WBI (" (argc : Integer;");
2353 WBI (" argv : System.Address;");
2355 (" envp : System.Address)");
2357 if Exit_Status_Supported_On_Target then
2358 Write_Statement_Buffer;
2359 WBI (" return Integer;");
2361 Write_Statement_Buffer (";");
2365 if Exit_Status_Supported_On_Target then
2366 Write_Statement_Buffer (" return Integer;");
2368 Write_Statement_Buffer (";");
2372 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2373 Get_Main_Name & """);");
2382 WBI ("end " & Ada_Main & ";");
2383 Close_Binder_Output;
2385 -- Prepare to write body
2387 Create_Binder_Output (Filename, 'b', Bfileb);
2389 -- We always compile the binder file in Ada 95 mode so that we properly
2390 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2391 -- of the Ada 2005 constructs are needed by the binder file.
2393 WBI ("pragma Ada_95;");
2395 -- Output Source_File_Name pragmas which look like
2397 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2398 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2400 -- where sss/bbb are the spec/body file names respectively
2402 Get_Name_String (Bfiles);
2403 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2405 WBI ("pragma Source_File_Name (" &
2407 ", Spec_File_Name => """ &
2408 Name_Buffer (1 .. Name_Len + 3));
2410 Get_Name_String (Bfileb);
2411 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2413 WBI ("pragma Source_File_Name (" &
2415 ", Body_File_Name => """ &
2416 Name_Buffer (1 .. Name_Len + 3));
2418 -- Generate with of System.Restrictions to initialize
2419 -- Run_Time_Restrictions.
2421 if System_Restrictions_Used
2422 and not Suppress_Standard_Library_On_Target
2425 WBI ("with System.Restrictions;");
2429 WBI ("package body " & Ada_Main & " is");
2430 WBI (" pragma Warnings (Off);");
2432 -- Import the finalization procedure only if finalization active
2434 if not Cumulative_Restrictions.Set (No_Finalization) then
2436 -- In the Java case, pragma Import C cannot be used, so the
2437 -- standard Ada constructs will be used instead.
2439 if VM_Target = No_VM then
2441 WBI (" procedure Do_Finalize;");
2443 (" pragma Import (C, Do_Finalize, " &
2444 """system__standard_library__adafinal"");");
2449 if not Suppress_Standard_Library_On_Target then
2451 -- Generate Priority_Specific_Dispatching pragma string
2454 (" Local_Priority_Specific_Dispatching : constant String := """);
2456 for J in 0 .. PSD_Pragma_Settings.Last loop
2457 Set_Char (PSD_Pragma_Settings.Table (J));
2461 Write_Statement_Buffer;
2463 -- Generate Interrupt_State pragma string
2465 Set_String (" Local_Interrupt_States : constant String := """);
2467 for J in 0 .. IS_Pragma_Settings.Last loop
2468 Set_Char (IS_Pragma_Settings.Table (J));
2472 Write_Statement_Buffer;
2480 if Bind_Main_Program and then VM_Target = No_VM then
2482 -- When suppressing the standard library then generate dummy body
2485 if Suppress_Standard_Library_On_Target then
2487 WBI (" procedure Break_Start is");
2496 -- Output object file list and the Ada body is complete
2498 Gen_Object_Files_Options;
2501 WBI ("end " & Ada_Main & ";");
2503 Close_Binder_Output;
2504 end Gen_Output_File_Ada;
2506 -----------------------
2507 -- Gen_Output_File_C --
2508 -----------------------
2510 procedure Gen_Output_File_C (Filename : String) is
2512 pragma Warnings (Off, Bfile);
2513 -- Name of generated bind file (not referenced)
2516 Create_Binder_Output (Filename, 'c', Bfile);
2518 Resolve_Binder_Options;
2520 WBI ("extern void " & Ada_Final_Name.all & " (void);");
2522 -- If -a has been specified use __attribute__((constructor)) for the
2523 -- init procedure. No need to use a similar featute for the final
2524 -- procedure as global finalization will occur when the executable
2525 -- finishes execution and for plugins (shared stand-alone libraries that
2526 -- can be "unloaded"), finalization should not occur automatically,
2527 -- otherwise the main executable may not continue to work properly.
2529 if Use_Pragma_Linker_Constructor then
2530 WBI ("extern void " & Ada_Init_Name.all &
2531 " (void) __attribute__((constructor));");
2533 WBI ("extern void " & Ada_Init_Name.all & " (void);");
2536 WBI ("extern void system__standard_library__adafinal (void);");
2538 if not No_Main_Subprogram then
2539 Set_String ("extern ");
2541 if Exit_Status_Supported_On_Target then
2544 Set_String ("void");
2547 Set_String (" main ");
2549 if Command_Line_Args_On_Target then
2550 Write_Statement_Buffer ("(int, char **, char **);");
2552 Write_Statement_Buffer ("(void);");
2555 if OpenVMS_On_Target then
2556 WBI ("extern void decc$__posix_exit (int);");
2558 WBI ("extern void exit (int);");
2561 WBI ("extern void __gnat_break_start (void);");
2562 Set_String ("extern ");
2564 if ALIs.Table (ALIs.First).Main_Program = Proc then
2565 Set_String ("void ");
2567 Set_String ("int ");
2570 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2571 Set_Main_Program_Name;
2572 Set_String (" (void);");
2573 Write_Statement_Buffer;
2576 if not Suppress_Standard_Library_On_Target then
2577 WBI ("extern void __gnat_initialize (void *);");
2578 WBI ("extern void __gnat_finalize (void);");
2579 WBI ("extern void __gnat_install_handler (void);");
2582 if Dynamic_Stack_Measurement then
2584 WBI ("extern void __gnat_stack_usage_output_results (void);");
2585 WBI ("extern void __gnat_stack_usage_initialize (int size);");
2588 -- Initialize stack limit for the environment task if the stack
2589 -- check method is stack limit and stack check is enabled.
2591 if Stack_Check_Limits_On_Target
2592 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
2595 WBI ("extern void __gnat_initialize_stack_limit (void);");
2602 -- Imported variable used to track elaboration/finalization phase.
2603 -- Used only when we have a runtime.
2605 if not Suppress_Standard_Library_On_Target then
2606 WBI ("extern int __gnat_handler_installed;");
2610 -- Write argv/argc exit status stuff if main program case
2612 if Bind_Main_Program then
2614 -- First deal with argc/argv/envp. In the normal case they
2615 -- are in the run-time library.
2617 if not Configurable_Run_Time_On_Target then
2618 WBI ("extern int gnat_argc;");
2619 WBI ("extern char **gnat_argv;");
2620 WBI ("extern char **gnat_envp;");
2622 -- If configurable run time and no command line args, then the
2623 -- generation of these variables is entirely suppressed.
2625 elsif not Command_Line_Args_On_Target then
2628 -- Otherwise, in the configurable run-time case they are right in
2632 WBI ("int gnat_argc;");
2633 WBI ("char **gnat_argv;");
2634 WBI ("char **gnat_envp;");
2637 -- Similarly deal with exit status
2638 -- are in the run-time library.
2640 if not Configurable_Run_Time_On_Target then
2641 WBI ("extern int gnat_exit_status;");
2643 -- If configurable run time and no exit status on target, then
2644 -- the generation of this variables is entirely suppressed.
2646 elsif not Exit_Status_Supported_On_Target then
2649 -- Otherwise, in the configurable run-time case this variable is
2650 -- right in the binder file, and initialized to zero there.
2653 WBI ("int gnat_exit_status = 0;");
2659 -- When suppressing the standard library, the __gnat_break_start
2660 -- routine (for the debugger to get initial control) is defined in
2663 if Suppress_Standard_Library_On_Target then
2665 WBI ("void __gnat_break_start (void) {}");
2668 -- Generate the __gnat_version and __gnat_ada_main_program_name info
2669 -- only for the main program. Otherwise, it can lead under some
2670 -- circumstances to a symbol duplication during the link (for instance
2671 -- when a C program uses 2 Ada libraries)
2673 if Bind_Main_Program then
2675 WBI ("char __gnat_version[] = ""GNAT Version: " &
2676 Gnat_Version_String & """;");
2678 Set_String ("char __gnat_ada_main_program_name[] = """);
2679 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2680 Set_Main_Program_Name;
2682 Write_Statement_Buffer;
2685 -- Generate the adafinal routine. In no runtime mode, this is
2686 -- not needed, since there is no finalization to do.
2688 if not Cumulative_Restrictions.Set (No_Finalization) then
2694 -- Main is only present for Ada main case
2696 if Bind_Main_Program then
2700 -- Generate versions, elaboration order, list of object files
2704 Gen_Object_Files_Options;
2706 -- C binder output is complete
2708 Close_Binder_Output;
2709 end Gen_Output_File_C;
2711 --------------------------
2712 -- Gen_Restrictions_Ada --
2713 --------------------------
2715 procedure Gen_Restrictions_Ada is
2719 if Suppress_Standard_Library_On_Target
2720 or not System_Restrictions_Used
2725 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2731 for J in Cumulative_Restrictions.Set'First ..
2732 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2734 Set_Boolean (Cumulative_Restrictions.Set (J));
2739 Write_Statement_Buffer;
2746 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last));
2748 Write_Statement_Buffer;
2749 Set_String (" Value => (");
2751 for J in Cumulative_Restrictions.Value'First ..
2752 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2754 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2758 Set_Int (Int (Cumulative_Restrictions.Value
2759 (Cumulative_Restrictions.Value'Last)));
2761 Write_Statement_Buffer;
2762 WBI (" Violated =>");
2766 for J in Cumulative_Restrictions.Violated'First ..
2767 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2769 Set_Boolean (Cumulative_Restrictions.Violated (J));
2774 Write_Statement_Buffer;
2780 Set_Boolean (Cumulative_Restrictions.Violated
2781 (Cumulative_Restrictions.Violated'Last));
2783 Write_Statement_Buffer;
2784 Set_String (" Count => (");
2786 for J in Cumulative_Restrictions.Count'First ..
2787 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2789 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2793 Set_Int (Int (Cumulative_Restrictions.Count
2794 (Cumulative_Restrictions.Count'Last)));
2796 Write_Statement_Buffer;
2797 Set_String (" Unknown => (");
2799 for J in Cumulative_Restrictions.Unknown'First ..
2800 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2802 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2807 (Cumulative_Restrictions.Unknown
2808 (Cumulative_Restrictions.Unknown'Last));
2810 Write_Statement_Buffer;
2811 end Gen_Restrictions_Ada;
2813 ------------------------
2814 -- Gen_Restrictions_C --
2815 ------------------------
2817 procedure Gen_Restrictions_C is
2819 if Suppress_Standard_Library_On_Target
2820 or not System_Restrictions_Used
2825 WBI (" typedef struct {");
2826 Set_String (" char set [");
2827 Set_Int (Cumulative_Restrictions.Set'Length);
2829 Write_Statement_Buffer;
2831 Set_String (" int value [");
2832 Set_Int (Cumulative_Restrictions.Value'Length);
2834 Write_Statement_Buffer;
2836 Set_String (" char violated [");
2837 Set_Int (Cumulative_Restrictions.Violated'Length);
2839 Write_Statement_Buffer;
2841 Set_String (" int count [");
2842 Set_Int (Cumulative_Restrictions.Count'Length);
2844 Write_Statement_Buffer;
2846 Set_String (" char unknown [");
2847 Set_Int (Cumulative_Restrictions.Unknown'Length);
2849 Write_Statement_Buffer;
2850 WBI (" } restrictions;");
2851 WBI (" extern restrictions " &
2852 "system__restrictions__run_time_restrictions;");
2853 WBI (" restrictions r = {");
2856 for J in Cumulative_Restrictions.Set'First ..
2857 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2859 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
2863 Set_Int (Boolean'Pos
2864 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last)));
2866 Write_Statement_Buffer;
2869 for J in Cumulative_Restrictions.Value'First ..
2870 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2872 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2876 Set_Int (Int (Cumulative_Restrictions.Value
2877 (Cumulative_Restrictions.Value'Last)));
2879 Write_Statement_Buffer;
2882 for J in Cumulative_Restrictions.Violated'First ..
2883 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2885 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
2889 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated
2890 (Cumulative_Restrictions.Violated'Last)));
2892 Write_Statement_Buffer;
2895 for J in Cumulative_Restrictions.Count'First ..
2896 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2898 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2902 Set_Int (Int (Cumulative_Restrictions.Count
2903 (Cumulative_Restrictions.Count'Last)));
2905 Write_Statement_Buffer;
2908 for J in Cumulative_Restrictions.Unknown'First ..
2909 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2911 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
2915 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown
2916 (Cumulative_Restrictions.Unknown'Last)));
2918 Write_Statement_Buffer;
2919 WBI (" system__restrictions__run_time_restrictions = r;");
2920 end Gen_Restrictions_C;
2922 ----------------------
2923 -- Gen_Versions_Ada --
2924 ----------------------
2926 -- This routine generates two sets of lines. The first set has the form:
2928 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2930 -- The second set has the form
2932 -- pragma Export (C, unnnnn, unam);
2934 -- for each unit, where unam is the unit name suffixed by either B or
2935 -- S for body or spec, with dots replaced by double underscores, and
2936 -- hhhhhhhh is the version number, and nnnnn is a 5-digits serial number.
2938 procedure Gen_Versions_Ada is
2939 Ubuf : String (1 .. 6) := "u00000";
2941 procedure Increment_Ubuf;
2942 -- Little procedure to increment the serial number
2944 procedure Increment_Ubuf is
2946 for J in reverse Ubuf'Range loop
2947 Ubuf (J) := Character'Succ (Ubuf (J));
2948 exit when Ubuf (J) <= '9';
2953 -- Start of processing for Gen_Versions_Ada
2956 if Bind_For_Library then
2958 -- When building libraries, the version number of each unit can
2959 -- not be computed, since the binder does not know the full list
2960 -- of units. Therefore, the 'Version and 'Body_Version
2961 -- attributes cannot supported in this case.
2968 WBI (" type Version_32 is mod 2 ** 32;");
2969 for U in Units.First .. Units.Last loop
2971 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2972 Units.Table (U).Version & "#;");
2978 for U in Units.First .. Units.Last loop
2980 Set_String (" pragma Export (C, ");
2982 Set_String (", """);
2984 Get_Name_String (Units.Table (U).Uname);
2986 for K in 1 .. Name_Len loop
2987 if Name_Buffer (K) = '.' then
2991 elsif Name_Buffer (K) = '%' then
2995 Set_Char (Name_Buffer (K));
2999 if Name_Buffer (Name_Len) = 's' then
3005 Set_String (""");");
3006 Write_Statement_Buffer;
3009 end Gen_Versions_Ada;
3011 --------------------
3012 -- Gen_Versions_C --
3013 --------------------
3015 -- This routine generates a line of the form:
3017 -- unsigned unam = 0xhhhhhhhh;
3019 -- for each unit, where unam is the unit name suffixed by either B or
3020 -- S for body or spec, with dots replaced by double underscores.
3022 procedure Gen_Versions_C is
3024 if Bind_For_Library then
3026 -- When building libraries, the version number of each unit can
3027 -- not be computed, since the binder does not know the full list
3028 -- of units. Therefore, the 'Version and 'Body_Version
3029 -- attributes cannot supported.
3034 for U in Units.First .. Units.Last loop
3035 Set_String ("unsigned ");
3037 Get_Name_String (Units.Table (U).Uname);
3039 for K in 1 .. Name_Len loop
3040 if Name_Buffer (K) = '.' then
3043 elsif Name_Buffer (K) = '%' then
3047 Set_Char (Name_Buffer (K));
3051 if Name_Buffer (Name_Len) = 's' then
3057 Set_String (" = 0x");
3058 Set_String (Units.Table (U).Version);
3060 Write_Statement_Buffer;
3065 ------------------------
3066 -- Get_Main_Unit_Name --
3067 ------------------------
3069 function Get_Main_Unit_Name (S : String) return String is
3070 Result : String := S;
3073 for J in S'Range loop
3074 if Result (J) = '.' then
3080 end Get_Main_Unit_Name;
3082 -----------------------
3083 -- Get_Ada_Main_Name --
3084 -----------------------
3086 function Get_Ada_Main_Name return String is
3087 Suffix : constant String := "_00";
3088 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3089 Opt.Ada_Main_Name.all & Suffix;
3093 -- The main program generated by JGNAT expects a package called
3094 -- ada_<main procedure>.
3096 if VM_Target /= No_VM then
3097 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3098 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3101 -- This loop tries the following possibilities in order
3107 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3108 -- it is set to 'ada_main'.
3110 for J in 0 .. 99 loop
3112 Nlen := Name'Length - Suffix'Length;
3114 Nlen := Name'Length;
3115 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3116 Name (Name'Last - 1) :=
3117 Character'Val (J / 10 + Character'Pos ('0'));
3120 for K in ALIs.First .. ALIs.Last loop
3121 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3123 -- Get unit name, removing %b or %e at end
3125 Get_Name_String (Units.Table (L).Uname);
3126 Name_Len := Name_Len - 2;
3128 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3134 return Name (1 .. Nlen);
3140 -- If we fall through, just use a peculiar unlikely name
3142 return ("Qwertyuiop");
3143 end Get_Ada_Main_Name;
3149 function Get_Main_Name return String is
3151 -- Explicit name given with -M switch
3153 if Bind_Alternate_Main_Name then
3154 return Alternate_Main_Name.all;
3156 -- Case of main program name to be used directly
3158 elsif Use_Ada_Main_Program_Name_On_Target then
3160 -- Get main program name
3162 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3164 -- If this is a child name, return only the name of the child,
3165 -- since we can't have dots in a nested program name. Note that
3166 -- we do not include the %b at the end of the unit name.
3168 for J in reverse 1 .. Name_Len - 2 loop
3169 if J = 1 or else Name_Buffer (J - 1) = '.' then
3170 return Name_Buffer (J .. Name_Len - 2);
3174 raise Program_Error; -- impossible exit
3176 -- Case where "main" is to be used as default
3183 ---------------------
3184 -- Get_WC_Encoding --
3185 ---------------------
3187 function Get_WC_Encoding return Character is
3189 -- If encoding method specified by -W switch, then return it
3191 if Wide_Character_Encoding_Method_Specified then
3192 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3194 -- If no main program, and not specified, set brackets, we really have
3195 -- no better choice. If some other encoding is required when there is
3196 -- no main, it must be set explicitly using -Wx.
3198 -- Note: if the ALI file always passed the wide character encoding
3199 -- of every file, then we could use the encoding of the initial
3200 -- specified file, but this information is passed only for potential
3201 -- main programs. We could fix this sometime, but it is a very minor
3202 -- point (wide character default encoding for [Wide_[Wide_]Text_IO
3203 -- when there is no main program).
3205 elsif No_Main_Subprogram then
3208 -- Otherwise if there is a main program, take encoding from it
3211 return ALIs.Table (ALIs.First).WC_Encoding;
3213 end Get_WC_Encoding;
3215 ----------------------
3216 -- Lt_Linker_Option --
3217 ----------------------
3219 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3221 -- Sort internal files last
3223 if Linker_Options.Table (Op1).Internal_File
3225 Linker_Options.Table (Op2).Internal_File
3227 -- Note: following test uses False < True
3229 return Linker_Options.Table (Op1).Internal_File
3231 Linker_Options.Table (Op2).Internal_File;
3233 -- If both internal or both non-internal, sort according to the
3234 -- elaboration position. A unit that is elaborated later should
3235 -- come earlier in the linker options list.
3238 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3240 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3243 end Lt_Linker_Option;
3245 ------------------------
3246 -- Move_Linker_Option --
3247 ------------------------
3249 procedure Move_Linker_Option (From : Natural; To : Natural) is
3251 Linker_Options.Table (To) := Linker_Options.Table (From);
3252 end Move_Linker_Option;
3254 ----------------------------
3255 -- Resolve_Binder_Options --
3256 ----------------------------
3258 procedure Resolve_Binder_Options is
3260 for E in Elab_Order.First .. Elab_Order.Last loop
3261 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3263 -- This is not a perfect approach, but is the current protocol
3264 -- between the run-time and the binder to indicate that tasking
3265 -- is used: system.os_interface should always be used by any
3266 -- tasking application.
3268 if Name_Buffer (1 .. 19) = "system.os_interface" then
3272 -- Ditto for declib and the "dec" package
3274 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3275 With_DECGNAT := True;
3278 end Resolve_Binder_Options;
3284 procedure Set_Boolean (B : Boolean) is
3285 True_Str : constant String := "True";
3286 False_Str : constant String := "False";
3289 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3290 Last := Last + True_Str'Length;
3292 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3293 Last := Last + False_Str'Length;
3301 procedure Set_Char (C : Character) is
3304 Statement_Buffer (Last) := C;
3311 procedure Set_Int (N : Int) is
3323 Statement_Buffer (Last) :=
3324 Character'Val (N mod 10 + Character'Pos ('0'));
3328 -------------------------
3329 -- Set_IS_Pragma_Table --
3330 -------------------------
3332 procedure Set_IS_Pragma_Table is
3334 for F in ALIs.First .. ALIs.Last loop
3335 for K in ALIs.Table (F).First_Interrupt_State ..
3336 ALIs.Table (F).Last_Interrupt_State
3339 Inum : constant Int :=
3340 Interrupt_States.Table (K).Interrupt_Id;
3341 Stat : constant Character :=
3342 Interrupt_States.Table (K).Interrupt_State;
3345 while IS_Pragma_Settings.Last < Inum loop
3346 IS_Pragma_Settings.Append ('n');
3349 IS_Pragma_Settings.Table (Inum) := Stat;
3353 end Set_IS_Pragma_Table;
3355 ---------------------------
3356 -- Set_Main_Program_Name --
3357 ---------------------------
3359 procedure Set_Main_Program_Name is
3361 -- Note that name has %b on the end which we ignore
3363 -- First we output the initial _ada_ since we know that the main
3364 -- program is a library level subprogram.
3366 Set_String ("_ada_");
3368 -- Copy name, changing dots to double underscores
3370 for J in 1 .. Name_Len - 2 loop
3371 if Name_Buffer (J) = '.' then
3374 Set_Char (Name_Buffer (J));
3377 end Set_Main_Program_Name;
3379 ---------------------
3380 -- Set_Name_Buffer --
3381 ---------------------
3383 procedure Set_Name_Buffer is
3385 for J in 1 .. Name_Len loop
3386 Set_Char (Name_Buffer (J));
3388 end Set_Name_Buffer;
3390 -------------------------
3391 -- Set_PSD_Pragma_Table --
3392 -------------------------
3394 procedure Set_PSD_Pragma_Table is
3396 for F in ALIs.First .. ALIs.Last loop
3397 for K in ALIs.Table (F).First_Specific_Dispatching ..
3398 ALIs.Table (F).Last_Specific_Dispatching
3401 DTK : Specific_Dispatching_Record
3402 renames Specific_Dispatching.Table (K);
3405 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3406 PSD_Pragma_Settings.Append ('F');
3409 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3410 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3415 end Set_PSD_Pragma_Table;
3421 procedure Set_String (S : String) is
3423 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3424 Last := Last + S'Length;
3431 procedure Set_Unit_Name is
3433 for J in 1 .. Name_Len - 2 loop
3434 if Name_Buffer (J) /= '.' then
3435 Set_Char (Name_Buffer (J));
3442 ---------------------
3443 -- Set_Unit_Number --
3444 ---------------------
3446 procedure Set_Unit_Number (U : Unit_Id) is
3447 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3448 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3451 if Num_Units >= 10 and then Unum < 10 then
3455 if Num_Units >= 100 and then Unum < 100 then
3460 end Set_Unit_Number;
3462 ----------------------
3463 -- Write_Info_Ada_C --
3464 ----------------------
3466 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3468 if Ada_Bind_File then
3470 S : String (1 .. Ada'Length + Common'Length);
3472 S (1 .. Ada'Length) := Ada;
3473 S (Ada'Length + 1 .. S'Length) := Common;
3479 S : String (1 .. C'Length + Common'Length);
3481 S (1 .. C'Length) := C;
3482 S (C'Length + 1 .. S'Length) := Common;
3486 end Write_Info_Ada_C;
3488 ----------------------------
3489 -- Write_Statement_Buffer --
3490 ----------------------------
3492 procedure Write_Statement_Buffer is
3494 WBI (Statement_Buffer (1 .. Last));
3496 end Write_Statement_Buffer;
3498 procedure Write_Statement_Buffer (S : String) is
3501 Write_Statement_Buffer;
3502 end Write_Statement_Buffer;