1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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
131 -- main 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
134 -- in the main program, or by the use of a -Tnnn parameter for the
135 -- binder (if both are present, the binder value overrides). The
136 -- value is in milliseconds. A value of zero indicates that time
137 -- slicing should be suppressed. If no pragma is present, and no
138 -- -T switch was used, the value is -1.
140 -- WC_Encoding shows the wide character encoding method used for
141 -- the main program. This is one of the encoding letters defined
142 -- in System.WCh_Con.WC_Encoding_Letters.
144 -- Locking_Policy is a space if no locking policy was specified
145 -- for the partition. If a locking policy was specified, the value
146 -- is the upper case first character of the locking policy name,
147 -- for example, 'C' for Ceiling_Locking.
149 -- Queuing_Policy is a space if no queuing policy was specified
150 -- for the partition. If a queuing policy was specified, the value
151 -- is the upper case first character of the queuing policy name
152 -- for example, 'F' for FIFO_Queuing.
154 -- Task_Dispatching_Policy is a space if no task dispatching policy
155 -- was specified for the partition. If a task dispatching policy
156 -- was specified, the value is the upper case first character of
157 -- the policy name, e.g. 'F' for FIFO_Within_Priorities.
159 -- Priority_Specific_Dispatching is the address of a string used to
160 -- store the task dispatching policy specified for the different priorities
161 -- in the partition. The length of this string is determined by the last
162 -- priority for which such a pragma applies (the string will be a null
163 -- string if no specific dispatching policies were used). If pragma were
164 -- present, the entries apply to the priorities in sequence from the first
165 -- priority. The value stored is the upper case first character of the
166 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
167 -- for those priority ranges not specified.
169 -- Num_Specific_Dispatching is the length of the
170 -- Priority_Specific_Dispatching string. It will be set to zero if no
171 -- Priority_Specific_Dispatching pragmas are present.
173 -- Restrictions is the address of a null-terminated string specifying the
174 -- restrictions information for the partition. The format is identical to
175 -- that of the parameter string found on R lines in ali files (see Lib.Writ
176 -- spec in lib-writ.ads for full details). The difference is that in this
177 -- context the values are the cumulative ones for the entire partition.
179 -- Interrupt_States is the address of a string used to specify the
180 -- cumulative results of Interrupt_State pragmas used in the partition.
181 -- The length of this string is determined by the last interrupt for which
182 -- such a pragma is given (the string will be a null string if no pragmas
183 -- were used). If pragma were present the entries apply to the interrupts
184 -- in sequence from the first interrupt, and are set to one of four
185 -- possible settings: 'n' for not specified, 'u' for user, 'r' for
186 -- run time, 's' for system, see description of Interrupt_State pragma
187 -- for further details.
189 -- Num_Interrupt_States is the length of the Interrupt_States string.
190 -- It will be set to zero if no Interrupt_State pragmas are present.
192 -- Unreserve_All_Interrupts is set to one if at least one unit in the
193 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
195 -- Exception_Tracebacks is set to one if the -E parameter was present
196 -- in the bind and to zero otherwise. Note that on some targets exception
197 -- tracebacks are provided by default, so a value of zero for this
198 -- parameter does not necessarily mean no trace backs are available.
200 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
201 -- this partition, and to zero if longjmp/setjmp exceptions are used.
204 -- Detect_Blocking indicates whether pragma Detect_Blocking is
205 -- active or not. A value of zero indicates that the pragma is not
206 -- present, while a value of 1 signals its presence in the
209 -- Default_Stack_Size is the default stack size used when creating an
210 -- Ada task with no explicit Storize_Size clause.
212 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
213 -- disabled. A value of zero indicates that leap seconds are turned "off",
214 -- while a value of one signifies "on" status.
216 -----------------------
217 -- Local Subprograms --
218 -----------------------
220 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
221 -- Convenient shorthand used throughout
223 procedure Check_System_Restrictions_Used;
224 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
225 -- System.Restrictions is present in the partition, otherwise False).
227 procedure Gen_Adainit_Ada;
228 -- Generates the Adainit procedure (Ada code case)
230 procedure Gen_Adainit_C;
231 -- Generates the Adainit procedure (C code case)
233 procedure Gen_Adafinal_Ada;
234 -- Generate the Adafinal procedure (Ada code case)
236 procedure Gen_Adafinal_C;
237 -- Generate the Adafinal procedure (C code case)
239 procedure Gen_Elab_Calls_Ada;
240 -- Generate sequence of elaboration calls (Ada code case)
242 procedure Gen_Elab_Calls_C;
243 -- Generate sequence of elaboration calls (C code case)
245 procedure Gen_Elab_Order_Ada;
246 -- Generate comments showing elaboration order chosen (Ada case)
248 procedure Gen_Elab_Order_C;
249 -- Generate comments showing elaboration order chosen (C case)
251 procedure Gen_Elab_Defs_C;
252 -- Generate sequence of definitions for elaboration routines (C code case)
254 procedure Gen_Main_Ada;
255 -- Generate procedure main (Ada code case)
257 procedure Gen_Main_C;
258 -- Generate main() procedure (C code case)
260 procedure Gen_Object_Files_Options;
261 -- Output comments containing a list of the full names of the object
262 -- files to be linked and the list of linker options supplied by
263 -- Linker_Options pragmas in the source. (C and Ada code case)
265 procedure Gen_Output_File_Ada (Filename : String);
266 -- Generate output file (Ada code case)
268 procedure Gen_Output_File_C (Filename : String);
269 -- Generate output file (C code case)
271 procedure Gen_Restrictions_Ada;
272 -- Generate initialization of restrictions variable (Ada code case)
274 procedure Gen_Restrictions_C;
275 -- Generate initialization of restrictions variable (C code case)
277 procedure Gen_Versions_Ada;
278 -- Output series of definitions for unit versions (Ada code case)
280 procedure Gen_Versions_C;
281 -- Output series of definitions for unit versions (C code case)
283 function Get_Ada_Main_Name return String;
284 -- This function is used in the Ada main output case to compute a usable
285 -- name for the generated main program. The normal main program name is
286 -- Ada_Main, but this won't work if the user has a unit with this name.
287 -- This function tries Ada_Main first, and if there is such a clash, then
288 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
290 function Get_Main_Unit_Name (S : String) return String;
291 -- Return the main unit name corresponding to S by replacing '.' with '_'
293 function Get_Main_Name return String;
294 -- This function is used in the Ada main output case to compute the
295 -- correct external main program. It is "main" by default, unless the
296 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
297 -- is the name of the Ada main name without the "_ada". This default
298 -- can be overridden explicitly using the -Mname binder switch.
300 function Get_WC_Encoding return Character;
301 -- Return wide character encoding method to set as WC_Encoding in output.
302 -- If -W has been used, returns the specified encoding, otherwise returns
303 -- the encoding method used for the main program source. If there is no
304 -- main program source (-z switch used), returns brackets ('b').
306 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
307 -- Compare linker options, when sorting, first according to
308 -- Is_Internal_File (internal files come later) and then by
309 -- elaboration order position (latest to earliest).
311 procedure Move_Linker_Option (From : Natural; To : Natural);
312 -- Move routine for sorting linker options
314 procedure Resolve_Binder_Options;
315 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
316 -- since it tests for a package named "dec" which might cause a conflict
317 -- on non-VMS systems.
319 procedure Set_Char (C : Character);
320 -- Set given character in Statement_Buffer at the Last + 1 position
321 -- and increment Last by one to reflect the stored character.
323 procedure Set_Int (N : Int);
324 -- Set given value in decimal in Statement_Buffer with no spaces
325 -- starting at the Last + 1 position, and updating Last past the value.
326 -- A minus sign is output for a negative value.
328 procedure Set_Boolean (B : Boolean);
329 -- Set given boolean value in Statement_Buffer at the Last + 1 position
330 -- and update Last past the value.
332 procedure Set_IS_Pragma_Table;
333 -- Initializes contents of IS_Pragma_Settings table from ALI table
335 procedure Set_Main_Program_Name;
336 -- Given the main program name in Name_Buffer (length in Name_Len)
337 -- generate the name of the routine to be used in the call. The name
338 -- is generated starting at Last + 1, and Last is updated past it.
340 procedure Set_Name_Buffer;
341 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
343 procedure Set_PSD_Pragma_Table;
344 -- Initializes contents of PSD_Pragma_Settings table from ALI table
346 procedure Set_String (S : String);
347 -- Sets characters of given string in Statement_Buffer, starting at the
348 -- Last + 1 position, and updating last past the string value.
350 procedure Set_Unit_Name;
351 -- Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
352 -- starting at the Last + 1 position, and updating last past the value.
353 -- changing periods to double underscores, and updating Last appropriately.
355 procedure Set_Unit_Number (U : Unit_Id);
356 -- Sets unit number (first unit is 1, leading zeroes output to line
357 -- up all output unit numbers nicely as required by the value, and
358 -- by the total number of units.
360 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
361 -- For C code case, write C & Common, for Ada case write Ada & Common
362 -- to current binder output file using Write_Binder_Info.
364 procedure Write_Statement_Buffer;
365 -- Write out contents of statement buffer up to Last, and reset Last to 0
367 procedure Write_Statement_Buffer (S : String);
368 -- First writes its argument (using Set_String (S)), then writes out the
369 -- contents of statement buffer up to Last, and reset Last to 0
371 ------------------------------------
372 -- Check_System_Restrictions_Used --
373 ------------------------------------
375 procedure Check_System_Restrictions_Used is
377 for J in Units.First .. Units.Last loop
378 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
379 System_Restrictions_Used := True;
384 System_Restrictions_Used := False;
385 end Check_System_Restrictions_Used;
387 ----------------------
388 -- Gen_Adafinal_Ada --
389 ----------------------
391 procedure Gen_Adafinal_Ada is
394 WBI (" procedure " & Ada_Final_Name.all & " is");
397 -- If compiling for the JVM, we directly call Adafinal because
398 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
400 if VM_Target /= No_VM then
401 WBI (" System.Standard_Library.Adafinal;");
403 -- If there is no finalization, there is nothing to do
405 elsif Cumulative_Restrictions.Set (No_Finalization) then
408 WBI (" Do_Finalize;");
411 WBI (" end " & Ada_Final_Name.all & ";");
412 end Gen_Adafinal_Ada;
418 procedure Gen_Adafinal_C is
420 WBI ("void " & Ada_Final_Name.all & " (void) {");
421 WBI (" system__standard_library__adafinal ();");
426 ---------------------
427 -- Gen_Adainit_Ada --
428 ---------------------
430 procedure Gen_Adainit_Ada is
431 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
434 WBI (" procedure " & Ada_Init_Name.all & " is");
436 -- Generate externals for elaboration entities
438 for E in Elab_Order.First .. Elab_Order.Last loop
440 Unum : constant Unit_Id := Elab_Order.Table (E);
441 U : Unit_Record renames Units.Table (Unum);
444 -- Check for Elab_Entity to be set for this unit
448 -- Don't generate reference for stand alone library
450 and then not U.SAL_Interface
452 -- Don't generate reference for predefined file in No_Run_Time
453 -- mode, since we don't include the object files in this case
457 and then Is_Predefined_File_Name (U.Sfile))
461 Set_Unit_Number (Unum);
464 when No_VM | JVM_Target =>
465 Set_String (" : Boolean; pragma Import (Ada, ");
467 Set_String (" : Boolean; pragma Import (CIL, ");
471 Set_Unit_Number (Unum);
473 Get_Name_String (U.Uname);
475 -- In the case of JGNAT we need to emit an Import name
476 -- that includes the class name (using '$' separators
477 -- in the case of a child unit name).
479 if VM_Target /= No_VM then
480 for J in 1 .. Name_Len - 2 loop
481 if VM_Target = CLI_Target
482 or else Name_Buffer (J) /= '.'
484 Set_Char (Name_Buffer (J));
490 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
493 Set_String ("_pkg.");
496 -- If the unit name is very long, then split the
497 -- Import link name across lines using "&" (occurs
498 -- in some C2 tests).
500 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
502 Write_Statement_Buffer;
508 Set_String ("_E"");");
509 Write_Statement_Buffer;
514 Write_Statement_Buffer;
516 -- If the standard library is suppressed, then the only global variable
517 -- that might be needed (by the Ravenscar profile) is the priority of
520 if Suppress_Standard_Library_On_Target then
521 if Main_Priority /= No_Main_Priority then
522 WBI (" Main_Priority : Integer;");
523 WBI (" pragma Import (C, Main_Priority," &
524 " ""__gl_main_priority"");");
530 if Main_Priority /= No_Main_Priority then
531 Set_String (" Main_Priority := ");
532 Set_Int (Main_Priority);
534 Write_Statement_Buffer;
540 -- Normal case (standard library not suppressed). Set all global values
541 -- used by the run time.
544 WBI (" Main_Priority : Integer;");
545 WBI (" pragma Import (C, Main_Priority, " &
546 """__gl_main_priority"");");
547 WBI (" Time_Slice_Value : Integer;");
548 WBI (" pragma Import (C, Time_Slice_Value, " &
549 """__gl_time_slice_val"");");
550 WBI (" WC_Encoding : Character;");
551 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
552 WBI (" Locking_Policy : Character;");
553 WBI (" pragma Import (C, Locking_Policy, " &
554 """__gl_locking_policy"");");
555 WBI (" Queuing_Policy : Character;");
556 WBI (" pragma Import (C, Queuing_Policy, " &
557 """__gl_queuing_policy"");");
558 WBI (" Task_Dispatching_Policy : Character;");
559 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
560 """__gl_task_dispatching_policy"");");
561 WBI (" Priority_Specific_Dispatching : System.Address;");
562 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
563 """__gl_priority_specific_dispatching"");");
564 WBI (" Num_Specific_Dispatching : Integer;");
565 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
566 """__gl_num_specific_dispatching"");");
568 WBI (" Interrupt_States : System.Address;");
569 WBI (" pragma Import (C, Interrupt_States, " &
570 """__gl_interrupt_states"");");
571 WBI (" Num_Interrupt_States : Integer;");
572 WBI (" pragma Import (C, Num_Interrupt_States, " &
573 """__gl_num_interrupt_states"");");
574 WBI (" Unreserve_All_Interrupts : Integer;");
575 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
576 """__gl_unreserve_all_interrupts"");");
578 if Exception_Tracebacks then
579 WBI (" Exception_Tracebacks : Integer;");
580 WBI (" pragma Import (C, Exception_Tracebacks, " &
581 """__gl_exception_tracebacks"");");
584 WBI (" Zero_Cost_Exceptions : Integer;");
585 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
586 """__gl_zero_cost_exceptions"");");
587 WBI (" Detect_Blocking : Integer;");
588 WBI (" pragma Import (C, Detect_Blocking, " &
589 """__gl_detect_blocking"");");
590 WBI (" Default_Stack_Size : Integer;");
591 WBI (" pragma Import (C, Default_Stack_Size, " &
592 """__gl_default_stack_size"");");
593 WBI (" Leap_Seconds_Support : Integer;");
594 WBI (" pragma Import (C, Leap_Seconds_Support, " &
595 """__gl_leap_seconds_support"");");
597 -- Import entry point for elaboration time signal handler
598 -- installation, and indication of if it's been called previously.
601 WBI (" procedure Install_Handler;");
602 WBI (" pragma Import (C, Install_Handler, " &
603 """__gnat_install_handler"");");
605 WBI (" Handler_Installed : Integer;");
606 WBI (" pragma Import (C, Handler_Installed, " &
607 """__gnat_handler_installed"");");
609 -- Initialize stack limit variable of the environment task if the
610 -- stack check method is stack limit and stack check is enabled.
612 if Stack_Check_Limits_On_Target
613 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
616 WBI (" procedure Initialize_Stack_Limit;");
617 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
618 """__gnat_initialize_stack_limit"");");
621 if VM_Target = CLI_Target
622 and then Bind_Main_Program
623 and then not No_Main_Subprogram
627 if ALIs.Table (ALIs.First).Main_Program = Func then
628 WBI (" Result : Integer;");
630 WBI (" function Ada_Main_Program return Integer;");
633 WBI (" procedure Ada_Main_Program;");
636 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
637 Name_Len := Name_Len - 2;
638 WBI (" pragma Import (CIL, Ada_Main_Program, """
639 & Name_Buffer (1 .. Name_Len) & "."
640 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
645 Set_String (" Main_Priority := ");
646 Set_Int (Main_Priority);
648 Write_Statement_Buffer;
650 Set_String (" Time_Slice_Value := ");
652 if Task_Dispatching_Policy_Specified = 'F'
653 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
657 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
661 Write_Statement_Buffer;
663 Set_String (" WC_Encoding := '");
664 Set_Char (Get_WC_Encoding);
667 Write_Statement_Buffer;
669 Set_String (" Locking_Policy := '");
670 Set_Char (Locking_Policy_Specified);
672 Write_Statement_Buffer;
674 Set_String (" Queuing_Policy := '");
675 Set_Char (Queuing_Policy_Specified);
677 Write_Statement_Buffer;
679 Set_String (" Task_Dispatching_Policy := '");
680 Set_Char (Task_Dispatching_Policy_Specified);
682 Write_Statement_Buffer;
684 Gen_Restrictions_Ada;
686 WBI (" Priority_Specific_Dispatching :=");
687 WBI (" Local_Priority_Specific_Dispatching'Address;");
689 Set_String (" Num_Specific_Dispatching := ");
690 Set_Int (PSD_Pragma_Settings.Last + 1);
692 Write_Statement_Buffer;
694 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
696 Set_String (" Num_Interrupt_States := ");
697 Set_Int (IS_Pragma_Settings.Last + 1);
699 Write_Statement_Buffer;
701 Set_String (" Unreserve_All_Interrupts := ");
703 if Unreserve_All_Interrupts_Specified then
710 Write_Statement_Buffer;
712 if Exception_Tracebacks then
713 WBI (" Exception_Tracebacks := 1;");
716 Set_String (" Zero_Cost_Exceptions := ");
718 if Zero_Cost_Exceptions_Specified then
725 Write_Statement_Buffer;
727 Set_String (" Detect_Blocking := ");
729 if Detect_Blocking then
736 Write_Statement_Buffer;
738 Set_String (" Default_Stack_Size := ");
739 Set_Int (Default_Stack_Size);
741 Write_Statement_Buffer;
743 Set_String (" Leap_Seconds_Support := ");
745 if Leap_Seconds_Support then
752 Write_Statement_Buffer;
754 -- Generate call to Install_Handler
757 WBI (" if Handler_Installed = 0 then");
758 WBI (" Install_Handler;");
762 -- Generate call to set Initialize_Scalar values if active
764 if Initialize_Scalars_Used then
766 Set_String (" System.Scalar_Values.Initialize ('");
767 Set_Char (Initialize_Scalars_Mode1);
769 Set_Char (Initialize_Scalars_Mode2);
771 Write_Statement_Buffer;
774 -- Generate assignment of default secondary stack size if set
776 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
778 Set_String (" System.Secondary_Stack.");
779 Set_String ("Default_Secondary_Stack_Size := ");
780 Set_Int (Opt.Default_Sec_Stack_Size);
782 Write_Statement_Buffer;
785 -- Initialize stack limit variable of the environment task if the
786 -- stack check method is stack limit and stack check is enabled.
788 if Stack_Check_Limits_On_Target
789 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
792 WBI (" Initialize_Stack_Limit;");
795 -- Generate elaboration calls
800 if VM_Target = CLI_Target
801 and then Bind_Main_Program
802 and then not No_Main_Subprogram
804 if ALIs.Table (ALIs.First).Main_Program = Func then
805 WBI (" Result := Ada_Main_Program;");
807 WBI (" Ada_Main_Program;");
811 WBI (" end " & Ada_Init_Name.all & ";");
818 procedure Gen_Adainit_C is
819 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
822 WBI ("void " & Ada_Init_Name.all & " (void)");
825 -- Generate externals for elaboration entities
827 for E in Elab_Order.First .. Elab_Order.Last loop
829 Unum : constant Unit_Id := Elab_Order.Table (E);
830 U : Unit_Record renames Units.Table (Unum);
833 -- Check for Elab entity to be set for this unit
837 -- Don't generate reference for stand alone library
839 and then not U.SAL_Interface
841 -- Don't generate reference for predefined file in No_Run_Time
842 -- mode, since we don't include the object files in this case
846 and then Is_Predefined_File_Name (U.Sfile))
848 Set_String (" extern char ");
849 Get_Name_String (U.Uname);
852 Write_Statement_Buffer;
857 Write_Statement_Buffer;
859 -- Standard library suppressed
861 if Suppress_Standard_Library_On_Target then
863 -- Case of High_Integrity_Mode mode. Set __gl_main_priority if needed
864 -- for the Ravenscar profile.
866 if Main_Priority /= No_Main_Priority then
867 WBI (" extern int __gl_main_priority;");
868 Set_String (" __gl_main_priority = ");
869 Set_Int (Main_Priority);
871 Write_Statement_Buffer;
874 -- Normal case (standard library not suppressed)
877 -- Generate definition for interrupt states string
879 Set_String (" static const char *local_interrupt_states = """);
881 for J in 0 .. IS_Pragma_Settings.Last loop
882 Set_Char (IS_Pragma_Settings.Table (J));
886 Write_Statement_Buffer;
888 -- Generate definition for priority specific dispatching string
891 (" static const char *local_priority_specific_dispatching = """);
893 for J in 0 .. PSD_Pragma_Settings.Last loop
894 Set_Char (PSD_Pragma_Settings.Table (J));
898 Write_Statement_Buffer;
900 -- Generate declaration for secondary stack default if needed
902 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
903 WBI (" extern int system__secondary_stack__" &
904 "default_secondary_stack_size;");
909 -- Code for normal case (standard library not suppressed)
911 -- We call the routine from inside adainit() because this works for
912 -- both programs with and without binder generated "main" functions.
914 WBI (" extern int __gl_main_priority;");
915 Set_String (" __gl_main_priority = ");
916 Set_Int (Main_Priority);
918 Write_Statement_Buffer;
920 WBI (" extern int __gl_time_slice_val;");
921 Set_String (" __gl_time_slice_val = ");
923 if Task_Dispatching_Policy = 'F'
924 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
928 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
932 Write_Statement_Buffer;
934 WBI (" extern char __gl_wc_encoding;");
935 Set_String (" __gl_wc_encoding = '");
936 Set_Char (Get_WC_Encoding);
939 Write_Statement_Buffer;
941 WBI (" extern char __gl_locking_policy;");
942 Set_String (" __gl_locking_policy = '");
943 Set_Char (Locking_Policy_Specified);
945 Write_Statement_Buffer;
947 WBI (" extern char __gl_queuing_policy;");
948 Set_String (" __gl_queuing_policy = '");
949 Set_Char (Queuing_Policy_Specified);
951 Write_Statement_Buffer;
953 WBI (" extern char __gl_task_dispatching_policy;");
954 Set_String (" __gl_task_dispatching_policy = '");
955 Set_Char (Task_Dispatching_Policy_Specified);
957 Write_Statement_Buffer;
961 WBI (" extern const void *__gl_interrupt_states;");
962 WBI (" __gl_interrupt_states = local_interrupt_states;");
964 WBI (" extern int __gl_num_interrupt_states;");
965 Set_String (" __gl_num_interrupt_states = ");
966 Set_Int (IS_Pragma_Settings.Last + 1);
968 Write_Statement_Buffer;
970 WBI (" extern const void *__gl_priority_specific_dispatching;");
971 WBI (" __gl_priority_specific_dispatching =" &
972 " local_priority_specific_dispatching;");
974 WBI (" extern int __gl_num_specific_dispatching;");
975 Set_String (" __gl_num_specific_dispatching = ");
976 Set_Int (PSD_Pragma_Settings.Last + 1);
978 Write_Statement_Buffer;
980 WBI (" extern int __gl_unreserve_all_interrupts;");
981 Set_String (" __gl_unreserve_all_interrupts = ");
982 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
984 Write_Statement_Buffer;
986 if Exception_Tracebacks then
987 WBI (" extern int __gl_exception_tracebacks;");
988 WBI (" __gl_exception_tracebacks = 1;");
991 WBI (" extern int __gl_zero_cost_exceptions;");
992 Set_String (" __gl_zero_cost_exceptions = ");
993 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
995 Write_Statement_Buffer;
997 WBI (" extern int __gl_detect_blocking;");
998 Set_String (" __gl_detect_blocking = ");
1000 if Detect_Blocking then
1007 Write_Statement_Buffer;
1009 WBI (" extern int __gl_default_stack_size;");
1010 Set_String (" __gl_default_stack_size = ");
1011 Set_Int (Default_Stack_Size);
1013 Write_Statement_Buffer;
1015 WBI (" extern int __gl_leap_seconds_support;");
1016 Set_String (" __gl_leap_seconds_support = ");
1018 if Leap_Seconds_Support then
1025 Write_Statement_Buffer;
1029 -- Install elaboration time signal handler
1031 WBI (" if (__gnat_handler_installed == 0)");
1033 WBI (" __gnat_install_handler ();");
1037 -- Initialize stack limit for the environment task if the stack
1038 -- check method is stack limit and stack check is enabled.
1040 if Stack_Check_Limits_On_Target
1041 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1044 WBI (" __gnat_initialize_stack_limit ();");
1047 -- Generate call to set Initialize_Scalar values if needed
1049 if Initialize_Scalars_Used then
1051 Set_String (" system__scalar_values__initialize('");
1052 Set_Char (Initialize_Scalars_Mode1);
1053 Set_String ("', '");
1054 Set_Char (Initialize_Scalars_Mode2);
1056 Write_Statement_Buffer;
1059 -- Generate assignment of default secondary stack size if set
1061 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1063 Set_String (" system__secondary_stack__");
1064 Set_String ("default_secondary_stack_size = ");
1065 Set_Int (Opt.Default_Sec_Stack_Size);
1067 Write_Statement_Buffer;
1070 -- Generate elaboration calls
1077 ------------------------
1078 -- Gen_Elab_Calls_Ada --
1079 ------------------------
1081 procedure Gen_Elab_Calls_Ada is
1083 for E in Elab_Order.First .. Elab_Order.Last loop
1085 Unum : constant Unit_Id := Elab_Order.Table (E);
1086 U : Unit_Record renames Units.Table (Unum);
1088 Unum_Spec : Unit_Id;
1089 -- This is the unit number of the spec that corresponds to
1090 -- this entry. It is the same as Unum except when the body
1091 -- and spec are different and we are currently processing
1092 -- the body, in which case it is the spec (Unum + 1).
1095 if U.Utype = Is_Body then
1096 Unum_Spec := Unum + 1;
1101 -- Nothing to do if predefined unit in no run time mode
1103 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1106 -- Case of no elaboration code
1108 elsif U.No_Elab then
1110 -- The only case in which we have to do something is if
1111 -- this is a body, with a separate spec, where the separate
1112 -- spec has an elaboration entity defined.
1114 -- In that case, this is where we set the elaboration entity
1115 -- to True, we do not need to test if this has already been
1116 -- done, since it is quicker to set the flag than to test it.
1118 if not U.SAL_Interface and then U.Utype = Is_Body
1119 and then Units.Table (Unum_Spec).Set_Elab_Entity
1122 Set_Unit_Number (Unum_Spec);
1123 Set_String (" := True;");
1124 Write_Statement_Buffer;
1127 -- Here if elaboration code is present. If binding a library
1128 -- or if there is a non-Ada main subprogram then we generate:
1130 -- if not uname_E then
1131 -- uname'elab_[spec|body];
1135 -- Otherwise, elaboration routines are called unconditionally:
1137 -- uname'elab_[spec|body];
1140 -- The uname_E assignment is skipped if this is a separate spec,
1141 -- since the assignment will be done when we process the body.
1143 elsif not U.SAL_Interface then
1144 if Force_Checking_Of_Elaboration_Flags or
1145 Interface_Library_Unit or
1146 (not Bind_Main_Program)
1148 Set_String (" if not E");
1149 Set_Unit_Number (Unum_Spec);
1150 Set_String (" then");
1151 Write_Statement_Buffer;
1156 Get_Decoded_Name_String_With_Brackets (U.Uname);
1158 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1159 if Name_Buffer (Name_Len) = 's' then
1160 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1163 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1167 Name_Len := Name_Len + 12;
1170 if Name_Buffer (Name_Len) = 's' then
1171 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1174 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1178 Name_Len := Name_Len + 8;
1181 Set_Casing (U.Icasing);
1184 Write_Statement_Buffer;
1186 if U.Utype /= Is_Spec then
1187 if Force_Checking_Of_Elaboration_Flags or
1188 Interface_Library_Unit or
1189 (not Bind_Main_Program)
1195 Set_Unit_Number (Unum_Spec);
1196 Set_String (" := True;");
1197 Write_Statement_Buffer;
1200 if Force_Checking_Of_Elaboration_Flags or
1201 Interface_Library_Unit or
1202 (not Bind_Main_Program)
1209 end Gen_Elab_Calls_Ada;
1211 ----------------------
1212 -- Gen_Elab_Calls_C --
1213 ----------------------
1215 procedure Gen_Elab_Calls_C is
1217 for E in Elab_Order.First .. Elab_Order.Last loop
1219 Unum : constant Unit_Id := Elab_Order.Table (E);
1220 U : Unit_Record renames Units.Table (Unum);
1222 Unum_Spec : Unit_Id;
1223 -- This is the unit number of the spec that corresponds to
1224 -- this entry. It is the same as Unum except when the body
1225 -- and spec are different and we are currently processing
1226 -- the body, in which case it is the spec (Unum + 1).
1229 if U.Utype = Is_Body then
1230 Unum_Spec := Unum + 1;
1235 -- Nothing to do if predefined unit in no run time mode
1237 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1240 -- Case of no elaboration code
1242 elsif U.No_Elab then
1244 -- The only case in which we have to do something is if
1245 -- this is a body, with a separate spec, where the separate
1246 -- spec has an elaboration entity defined.
1248 -- In that case, this is where we set the elaboration entity
1249 -- to True, we do not need to test if this has already been
1250 -- done, since it is quicker to set the flag than to test it.
1252 if not U.SAL_Interface and then U.Utype = Is_Body
1253 and then Units.Table (Unum_Spec).Set_Elab_Entity
1256 Get_Name_String (U.Uname);
1258 Set_String ("_E = 1;");
1259 Write_Statement_Buffer;
1262 -- Here if elaboration code is present. If binding a library
1263 -- or if there is a non-Ada main subprogram then we generate:
1265 -- if (uname_E == 0) {
1266 -- uname__elab[s|b] ();
1270 -- The uname_E assignment is skipped if this is a separate spec,
1271 -- since the assignment will be done when we process the body.
1273 elsif not U.SAL_Interface then
1274 Get_Name_String (U.Uname);
1276 if Force_Checking_Of_Elaboration_Flags or
1277 Interface_Library_Unit or
1278 (not Bind_Main_Program)
1280 Set_String (" if (");
1282 Set_String ("_E == 0) {");
1283 Write_Statement_Buffer;
1289 Set_String ("___elab");
1290 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1291 Set_String (" ();");
1292 Write_Statement_Buffer;
1294 if U.Utype /= Is_Spec then
1295 if Force_Checking_Of_Elaboration_Flags or
1296 Interface_Library_Unit or
1297 (not Bind_Main_Program)
1304 Set_String ("_E++;");
1305 Write_Statement_Buffer;
1308 if Force_Checking_Of_Elaboration_Flags or
1309 Interface_Library_Unit or
1310 (not Bind_Main_Program)
1318 end Gen_Elab_Calls_C;
1320 ----------------------
1321 -- Gen_Elab_Defs_C --
1322 ----------------------
1324 procedure Gen_Elab_Defs_C is
1326 for E in Elab_Order.First .. Elab_Order.Last loop
1328 -- Generate declaration of elaboration procedure if elaboration
1329 -- needed. Note that passive units are always excluded.
1331 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1332 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1333 Set_String ("extern void ");
1335 Set_String ("___elab");
1336 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1337 Set_String (" (void);");
1338 Write_Statement_Buffer;
1344 end Gen_Elab_Defs_C;
1346 ------------------------
1347 -- Gen_Elab_Order_Ada --
1348 ------------------------
1350 procedure Gen_Elab_Order_Ada is
1353 WBI (" -- BEGIN ELABORATION ORDER");
1355 for J in Elab_Order.First .. Elab_Order.Last loop
1356 Set_String (" -- ");
1357 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1359 Write_Statement_Buffer;
1362 WBI (" -- END ELABORATION ORDER");
1363 end Gen_Elab_Order_Ada;
1365 ----------------------
1366 -- Gen_Elab_Order_C --
1367 ----------------------
1369 procedure Gen_Elab_Order_C is
1372 WBI ("/* BEGIN ELABORATION ORDER");
1374 for J in Elab_Order.First .. Elab_Order.Last loop
1375 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1377 Write_Statement_Buffer;
1380 WBI (" END ELABORATION ORDER */");
1381 end Gen_Elab_Order_C;
1387 procedure Gen_Main_Ada is
1391 if Exit_Status_Supported_On_Target then
1392 Set_String (" function ");
1394 Set_String (" procedure ");
1397 Set_String (Get_Main_Name);
1399 if Command_Line_Args_On_Target then
1400 Write_Statement_Buffer;
1401 WBI (" (argc : Integer;");
1402 WBI (" argv : System.Address;");
1403 WBI (" envp : System.Address)");
1405 if Exit_Status_Supported_On_Target then
1406 WBI (" return Integer");
1412 if Exit_Status_Supported_On_Target then
1413 Set_String (" return Integer is");
1418 Write_Statement_Buffer;
1421 if Opt.Default_Exit_Status /= 0
1422 and then Bind_Main_Program
1423 and then not Configurable_Run_Time_Mode
1425 WBI (" procedure Set_Exit_Status (Status : Integer);");
1426 WBI (" pragma Import (C, Set_Exit_Status, " &
1427 """__gnat_set_exit_status"");");
1431 -- Initialize and Finalize
1433 if not Cumulative_Restrictions.Set (No_Finalization) then
1434 WBI (" procedure initialize (Addr : System.Address);");
1435 WBI (" pragma Import (C, initialize, ""__gnat_initialize"");");
1437 WBI (" procedure finalize;");
1438 WBI (" pragma Import (C, finalize, ""__gnat_finalize"");");
1441 -- If we want to analyze the stack, we have to import corresponding
1444 if Dynamic_Stack_Measurement then
1446 WBI (" procedure Output_Results;");
1447 WBI (" pragma Import (C, Output_Results, " &
1448 """__gnat_stack_usage_output_results"");");
1452 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1453 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1454 """__gnat_stack_usage_initialize"");");
1457 -- Deal with declarations for main program case
1459 if not No_Main_Subprogram then
1461 -- To call the main program, we declare it using a pragma Import
1462 -- Ada with the right link name.
1464 -- It might seem more obvious to "with" the main program, and call
1465 -- it in the normal Ada manner. We do not do this for three reasons:
1467 -- 1. It is more efficient not to recompile the main program
1468 -- 2. We are not entitled to assume the source is accessible
1469 -- 3. We don't know what options to use to compile it
1471 -- It is really reason 3 that is most critical (indeed we used
1472 -- to generate the "with", but several regression tests failed).
1476 if ALIs.Table (ALIs.First).Main_Program = Func then
1477 WBI (" Result : Integer;");
1479 WBI (" function Ada_Main_Program return Integer;");
1482 WBI (" procedure Ada_Main_Program;");
1485 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1486 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1487 Set_Main_Program_Name;
1488 Set_String (""");");
1490 Write_Statement_Buffer;
1493 if Bind_Main_Program
1494 and then not Suppress_Standard_Library_On_Target
1496 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1501 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1502 -- not referenced elsewhere in the generated program, but is needed
1503 -- by the debugger (that's why it is generated in the first place).
1504 -- The reference stops Ada_Main_Program_Name from being optimized
1505 -- away by smart linkers, such as the AiX linker.
1507 -- Because this variable is unused, we make this variable "aliased"
1508 -- with a pragma Volatile in order to tell the compiler to preserve
1509 -- this variable at any level of optimization.
1511 if Bind_Main_Program then
1513 (" Ensure_Reference : aliased System.Address := " &
1514 "Ada_Main_Program_Name'Address;");
1515 WBI (" pragma Volatile (Ensure_Reference);");
1521 -- Acquire command line arguments if present on target
1523 if Command_Line_Args_On_Target then
1524 WBI (" gnat_argc := argc;");
1525 WBI (" gnat_argv := argv;");
1526 WBI (" gnat_envp := envp;");
1529 -- If configurable run time and no command line args, then nothing
1530 -- needs to be done since the gnat_argc/argv/envp variables are
1531 -- suppressed in this case.
1533 elsif Configurable_Run_Time_On_Target then
1536 -- Otherwise set dummy values (to be filled in by some other unit?)
1539 WBI (" gnat_argc := 0;");
1540 WBI (" gnat_argv := System.Null_Address;");
1541 WBI (" gnat_envp := System.Null_Address;");
1544 if Opt.Default_Exit_Status /= 0
1545 and then Bind_Main_Program
1546 and then not Configurable_Run_Time_Mode
1548 Set_String (" Set_Exit_Status (");
1549 Set_Int (Opt.Default_Exit_Status);
1551 Write_Statement_Buffer;
1554 if Dynamic_Stack_Measurement then
1555 Set_String (" Initialize_Stack_Analysis (");
1556 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1558 Write_Statement_Buffer;
1561 if not Cumulative_Restrictions.Set (No_Finalization) then
1562 if not No_Main_Subprogram
1563 and then Bind_Main_Program
1564 and then not Suppress_Standard_Library_On_Target
1566 WBI (" Initialize (SEH'Address);");
1568 WBI (" Initialize (System.Null_Address);");
1572 WBI (" " & Ada_Init_Name.all & ";");
1574 if not No_Main_Subprogram then
1575 WBI (" Break_Start;");
1577 if ALIs.Table (ALIs.First).Main_Program = Proc then
1578 WBI (" Ada_Main_Program;");
1580 WBI (" Result := Ada_Main_Program;");
1584 -- Adafinal call is skipped if no finalization
1586 if not Cumulative_Restrictions.Set (No_Finalization) then
1588 -- If compiling for the JVM, we directly call Adafinal because
1589 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1591 if VM_Target = No_VM then
1592 WBI (" Do_Finalize;");
1594 WBI (" System.Standard_Library.Adafinal;");
1598 -- Prints the result of static stack analysis
1600 if Dynamic_Stack_Measurement then
1601 WBI (" Output_Results;");
1604 -- Finalize is only called if we have a run time
1606 if not Cumulative_Restrictions.Set (No_Finalization) then
1612 if Exit_Status_Supported_On_Target then
1613 if No_Main_Subprogram
1614 or else ALIs.Table (ALIs.First).Main_Program = Proc
1616 WBI (" return (gnat_exit_status);");
1618 WBI (" return (Result);");
1629 procedure Gen_Main_C is
1631 if Exit_Status_Supported_On_Target then
1632 WBI ("#include <stdlib.h>");
1633 Set_String ("int ");
1635 Set_String ("void ");
1638 Set_String (Get_Main_Name);
1640 -- Generate command line args in prototype if present on target
1642 if Command_Line_Args_On_Target then
1643 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
1645 -- Case of no command line arguments on target
1648 Write_Statement_Buffer (" (void)");
1653 -- Generate a reference to __gnat_ada_main_program_name. This symbol
1654 -- is not referenced elsewhere in the generated program, but is
1655 -- needed by the debugger (that's why it is generated in the first
1656 -- place). The reference stops Ada_Main_Program_Name from being
1657 -- optimized away by smart linkers, such as the AiX linker.
1659 -- Because this variable is unused, we declare this variable as
1660 -- volatile in order to tell the compiler to preserve it at any
1661 -- level of optimization.
1663 if Bind_Main_Program then
1664 WBI (" char * volatile ensure_reference " &
1665 "__attribute__ ((__unused__)) = " &
1666 "__gnat_ada_main_program_name;");
1669 if not Suppress_Standard_Library_On_Target
1670 and then not No_Main_Subprogram
1672 WBI (" int SEH [2];");
1677 -- If main program is a function, generate result variable
1679 if ALIs.Table (ALIs.First).Main_Program = Func then
1680 WBI (" int result;");
1683 -- Set command line argument values from parameters if command line
1684 -- arguments are present on target
1686 if Command_Line_Args_On_Target then
1687 WBI (" gnat_argc = argc;");
1688 WBI (" gnat_argv = argv;");
1689 WBI (" gnat_envp = envp;");
1692 -- If configurable run-time, then nothing to do, since in this case
1693 -- the gnat_argc/argv/envp variables are entirely suppressed.
1695 elsif Configurable_Run_Time_On_Target then
1698 -- if no command line arguments on target, set dummy values
1701 WBI (" gnat_argc = 0;");
1702 WBI (" gnat_argv = 0;");
1703 WBI (" gnat_envp = 0;");
1706 if Opt.Default_Exit_Status /= 0
1707 and then Bind_Main_Program
1708 and then not Configurable_Run_Time_Mode
1710 Set_String (" __gnat_set_exit_status (");
1711 Set_Int (Opt.Default_Exit_Status);
1713 Write_Statement_Buffer;
1716 -- Initializes dynamic stack measurement if needed
1718 if Dynamic_Stack_Measurement then
1719 Set_String (" __gnat_stack_usage_initialize (");
1720 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1722 Write_Statement_Buffer;
1725 -- The __gnat_initialize routine is used only if we have a run-time
1727 if not Suppress_Standard_Library_On_Target then
1728 if not No_Main_Subprogram and then Bind_Main_Program then
1729 WBI (" __gnat_initialize ((void *)SEH);");
1731 WBI (" __gnat_initialize ((void *)0);");
1735 WBI (" " & Ada_Init_Name.all & " ();");
1737 if not No_Main_Subprogram then
1738 WBI (" __gnat_break_start ();");
1741 -- Output main program name
1743 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1745 -- Main program is procedure case
1747 if ALIs.Table (ALIs.First).Main_Program = Proc then
1749 Set_Main_Program_Name;
1750 Set_String (" ();");
1751 Write_Statement_Buffer;
1753 -- Main program is function case
1755 else -- ALIs.Table (ALIs_First).Main_Program = Func
1756 Set_String (" result = ");
1757 Set_Main_Program_Name;
1758 Set_String (" ();");
1759 Write_Statement_Buffer;
1764 -- Call adafinal if finalization active
1766 if not Cumulative_Restrictions.Set (No_Finalization) then
1768 WBI (" system__standard_library__adafinal ();");
1771 -- Outputs the dynamic stack measurement if needed
1773 if Dynamic_Stack_Measurement then
1774 WBI (" __gnat_stack_usage_output_results ();");
1777 -- The finalize routine is used only if we have a run-time
1779 if not Suppress_Standard_Library_On_Target then
1780 WBI (" __gnat_finalize ();");
1783 -- Case of main program is a function, so the value it returns
1784 -- is the exit status in this case.
1786 if ALIs.Table (ALIs.First).Main_Program = Func then
1787 if Exit_Status_Supported_On_Target then
1789 -- VMS must use Posix exit routine in order to get the effect
1790 -- of a Unix compatible setting of the program exit status.
1791 -- For all other systems, we use the standard exit routine.
1793 if OpenVMS_On_Target then
1794 WBI (" decc$__posix_exit (result);");
1796 WBI (" exit (result);");
1800 -- Case of main program is a procedure, in which case the exit
1801 -- status is whatever was set by a Set_Exit call most recently
1804 if Exit_Status_Supported_On_Target then
1806 -- VMS must use Posix exit routine in order to get the effect
1807 -- of a Unix compatible setting of the program exit status.
1808 -- For all other systems, we use the standard exit routine.
1810 if OpenVMS_On_Target then
1811 WBI (" decc$__posix_exit (gnat_exit_status);");
1813 WBI (" exit (gnat_exit_status);");
1821 ------------------------------
1822 -- Gen_Object_Files_Options --
1823 ------------------------------
1825 procedure Gen_Object_Files_Options is
1827 -- This keeps track of the position in the sorted set of entries
1828 -- in the Linker_Options table of where the first entry from an
1829 -- internal file appears.
1831 Linker_Option_List_Started : Boolean := False;
1832 -- Set to True when "LINKER OPTION LIST" is displayed
1834 procedure Write_Linker_Option;
1835 -- Write binder info linker option
1837 -------------------------
1838 -- Write_Linker_Option --
1839 -------------------------
1841 procedure Write_Linker_Option is
1846 -- Loop through string, breaking at null's
1849 while Start < Name_Len loop
1851 -- Find null ending this section
1854 while Name_Buffer (Stop) /= ASCII.NUL
1855 and then Stop <= Name_Len loop
1859 -- Process section if non-null
1861 if Stop > Start then
1862 if Output_Linker_Option_List then
1863 if not Zero_Formatting then
1864 if not Linker_Option_List_Started then
1865 Linker_Option_List_Started := True;
1867 Write_Str (" LINKER OPTION LIST");
1875 Write_Str (Name_Buffer (Start .. Stop - 1));
1879 (" -- ", "", Name_Buffer (Start .. Stop - 1));
1884 end Write_Linker_Option;
1886 -- Start of processing for Gen_Object_Files_Options
1890 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
1892 for E in Elab_Order.First .. Elab_Order.Last loop
1894 -- If not spec that has an associated body, then generate a
1895 -- comment giving the name of the corresponding object file.
1897 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
1898 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1902 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1904 -- If the presence of an object file is necessary or if it
1905 -- exists, then use it.
1907 if not Hostparm.Exclude_Missing_Objects
1909 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1911 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1913 if Output_Object_List then
1914 Write_Str (Name_Buffer (1 .. Name_Len));
1918 -- Don't link with the shared library on VMS if an internal
1919 -- filename object is seen. Multiply defined symbols will
1922 if OpenVMS_On_Target
1923 and then Is_Internal_File_Name
1925 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
1927 -- Special case for g-trasym.obj, which is not included
1930 Get_Name_String (ALIs.Table
1931 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
1933 if Name_Buffer (1 .. 8) /= "g-trasym" then
1934 Opt.Shared_Libgnat := False;
1941 -- Add a "-Ldir" for each directory in the object path
1943 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1945 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1948 Add_Str_To_Name_Buffer ("-L");
1949 Add_Str_To_Name_Buffer (Dir.all);
1950 Write_Linker_Option;
1954 -- Sort linker options
1956 -- This sort accomplishes two important purposes:
1958 -- a) All application files are sorted to the front, and all
1959 -- GNAT internal files are sorted to the end. This results
1960 -- in a well defined dividing line between the two sets of
1961 -- files, for the purpose of inserting certain standard
1962 -- library references into the linker arguments list.
1964 -- b) Given two different units, we sort the linker options so
1965 -- that those from a unit earlier in the elaboration order
1966 -- comes later in the list. This is a heuristic designed
1967 -- to create a more friendly order of linker options when
1968 -- the operations appear in separate units. The idea is that
1969 -- if unit A must be elaborated before unit B, then it is
1970 -- more likely that B references libraries included by A,
1971 -- than vice versa, so we want the libraries included by
1972 -- A to come after the libraries included by B.
1974 -- These two criteria are implemented by function Lt_Linker_Option.
1975 -- Note that a special case of b) is that specs are elaborated before
1976 -- bodies, so linker options from specs come after linker options
1977 -- for bodies, and again, the assumption is that libraries used by
1978 -- the body are more likely to reference libraries used by the spec,
1982 (Linker_Options.Last,
1983 Move_Linker_Option'Access,
1984 Lt_Linker_Option'Access);
1986 -- Write user linker options, i.e. the set of linker options that
1987 -- come from all files other than GNAT internal files, Lgnat is
1988 -- left set to point to the first entry from a GNAT internal file,
1989 -- or past the end of the entriers if there are no internal files.
1991 Lgnat := Linker_Options.Last + 1;
1993 for J in 1 .. Linker_Options.Last loop
1994 if not Linker_Options.Table (J).Internal_File then
1995 Get_Name_String (Linker_Options.Table (J).Name);
1996 Write_Linker_Option;
2003 -- Now we insert standard linker options that must appear after the
2004 -- entries from user files, and before the entries from GNAT run-time
2005 -- files. The reason for this decision is that libraries referenced
2006 -- by internal routines may reference these standard library entries.
2008 -- Note that we do not insert anything when pragma No_Run_Time has been
2009 -- specified or when the standard libraries are not to be used,
2010 -- otherwise on some platforms, such as VMS, we may get duplicate
2011 -- symbols when linking.
2013 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2016 if Opt.Shared_Libgnat then
2017 Add_Str_To_Name_Buffer ("-shared");
2019 Add_Str_To_Name_Buffer ("-static");
2022 -- Write directly to avoid -K output (why???)
2024 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2026 if With_DECGNAT then
2029 if Opt.Shared_Libgnat then
2030 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2032 Add_Str_To_Name_Buffer ("-ldecgnat");
2035 Write_Linker_Option;
2041 if Opt.Shared_Libgnat then
2042 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2044 Add_Str_To_Name_Buffer ("-lgnarl");
2047 Write_Linker_Option;
2052 if Opt.Shared_Libgnat then
2053 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2055 Add_Str_To_Name_Buffer ("-lgnat");
2058 Write_Linker_Option;
2061 -- Write linker options from all internal files
2063 for J in Lgnat .. Linker_Options.Last loop
2064 Get_Name_String (Linker_Options.Table (J).Name);
2065 Write_Linker_Option;
2068 if Output_Linker_Option_List and then not Zero_Formatting then
2072 if Ada_Bind_File then
2073 WBI ("-- END Object file/option list ");
2075 WBI (" END Object file/option list */");
2077 end Gen_Object_Files_Options;
2079 ---------------------
2080 -- Gen_Output_File --
2081 ---------------------
2083 procedure Gen_Output_File (Filename : String) is
2085 -- Acquire settings for Interrupt_State pragmas
2087 Set_IS_Pragma_Table;
2089 -- Acquire settings for Priority_Specific_Dispatching pragma
2091 Set_PSD_Pragma_Table;
2093 -- Override Ada_Bind_File and Bind_Main_Program for VMs since
2094 -- JGNAT only supports Ada code, and the main program is already
2095 -- generated by the compiler.
2097 if VM_Target /= No_VM then
2098 Ada_Bind_File := True;
2100 if VM_Target = JVM_Target then
2101 Bind_Main_Program := False;
2105 -- Override time slice value if -T switch is set
2107 if Time_Slice_Set then
2108 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2111 -- Count number of elaboration calls
2113 for E in Elab_Order.First .. Elab_Order.Last loop
2114 if Units.Table (Elab_Order.Table (E)).No_Elab then
2117 Num_Elab_Calls := Num_Elab_Calls + 1;
2121 -- Generate output file in appropriate language
2123 Check_System_Restrictions_Used;
2125 if Ada_Bind_File then
2126 Gen_Output_File_Ada (Filename);
2128 Gen_Output_File_C (Filename);
2130 end Gen_Output_File;
2132 -------------------------
2133 -- Gen_Output_File_Ada --
2134 -------------------------
2136 procedure Gen_Output_File_Ada (Filename : String) is
2139 -- Name of generated bind file (spec)
2142 -- Name of generated bind file (body)
2144 Ada_Main : constant String := Get_Ada_Main_Name;
2145 -- Name to be used for generated Ada main program. See the body of
2146 -- function Get_Ada_Main_Name for details on the form of the name.
2149 -- Create spec first
2151 Create_Binder_Output (Filename, 's', Bfiles);
2153 -- We always compile the binder file in Ada 95 mode so that we properly
2154 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2155 -- of the Ada 2005 constructs are needed by the binder file.
2157 WBI ("pragma Ada_95;");
2159 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2160 -- then we need to make sure that the binder program is compiled with
2161 -- the same restriction, so that no exception tables are generated.
2163 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2164 WBI ("pragma Restrictions (No_Exception_Handlers);");
2167 -- Same processing for Restrictions (No_Exception_Propagation)
2169 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2170 WBI ("pragma Restrictions (No_Exception_Propagation);");
2173 -- Same processing for pragma No_Run_Time
2175 if No_Run_Time_Mode then
2176 WBI ("pragma No_Run_Time;");
2179 -- Generate with of System so we can reference System.Address
2181 WBI ("with System;");
2183 -- Generate with of System.Initialize_Scalars if active
2185 if Initialize_Scalars_Used then
2186 WBI ("with System.Scalar_Values;");
2189 -- Generate with of System.Secondary_Stack if active
2191 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2192 WBI ("with System.Secondary_Stack;");
2195 Resolve_Binder_Options;
2197 if VM_Target /= No_VM then
2198 if not Suppress_Standard_Library_On_Target then
2200 -- Usually, adafinal is called using a pragma Import C. Since
2201 -- Import C doesn't have the same semantics for JGNAT, we use
2204 WBI ("with System.Standard_Library;");
2208 WBI ("package " & Ada_Main & " is");
2209 WBI (" pragma Warnings (Off);");
2211 -- Main program case
2213 if Bind_Main_Program then
2214 if VM_Target = No_VM then
2216 -- Generate argc/argv stuff unless suppressed
2218 if Command_Line_Args_On_Target
2219 or not Configurable_Run_Time_On_Target
2222 WBI (" gnat_argc : Integer;");
2223 WBI (" gnat_argv : System.Address;");
2224 WBI (" gnat_envp : System.Address;");
2226 -- If the standard library is not suppressed, these variables
2227 -- are in the runtime data area for easy access from the
2230 if not Suppress_Standard_Library_On_Target then
2232 WBI (" pragma Import (C, gnat_argc);");
2233 WBI (" pragma Import (C, gnat_argv);");
2234 WBI (" pragma Import (C, gnat_envp);");
2238 -- Define exit status. Again in normal mode, this is in the
2239 -- run-time library, and is initialized there, but in the
2240 -- configurable runtime case, the variable is declared and
2241 -- initialized in this file.
2245 if Configurable_Run_Time_Mode then
2246 if Exit_Status_Supported_On_Target then
2247 WBI (" gnat_exit_status : Integer := 0;");
2251 WBI (" gnat_exit_status : Integer;");
2252 WBI (" pragma Import (C, gnat_exit_status);");
2255 -- Generate the GNAT_Version and Ada_Main_Program_Name info only
2256 -- for the main program. Otherwise, it can lead under some
2257 -- circumstances to a symbol duplication during the link (for
2258 -- instance when a C program uses 2 Ada libraries)
2262 WBI (" GNAT_Version : constant String :=");
2263 WBI (" ""GNAT Version: " &
2264 Gnat_Version_String & """;");
2265 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2268 Set_String (" Ada_Main_Program_Name : constant String := """);
2269 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2271 if VM_Target = No_VM then
2272 Set_Main_Program_Name;
2273 Set_String (""" & Ascii.NUL;");
2275 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2278 Write_Statement_Buffer;
2281 (" pragma Export (C, Ada_Main_Program_Name, " &
2282 """__gnat_ada_main_program_name"");");
2286 WBI (" procedure " & Ada_Final_Name.all & ";");
2287 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2288 Ada_Final_Name.all & """);");
2290 WBI (" procedure " & Ada_Init_Name.all & ";");
2291 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2292 Ada_Init_Name.all & """);");
2294 -- If -a has been specified use pragma Linker_Constructor for the init
2295 -- procedure. No need to use a similar pragma for the final procedure as
2296 -- global finalization will occur when the executable finishes execution
2297 -- and for plugins (shared stand-alone libraries that can be
2298 -- "unloaded"), finalization should not occur automatically, otherwise
2299 -- the main executable may not continue to work properly.
2301 if Use_Pragma_Linker_Constructor then
2302 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2305 if Bind_Main_Program and then VM_Target = No_VM then
2307 -- If we have the standard library, then Break_Start is defined
2308 -- there, but when the standard library is suppressed, Break_Start
2312 WBI (" procedure Break_Start;");
2314 if Suppress_Standard_Library_On_Target then
2315 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2317 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2322 if Exit_Status_Supported_On_Target then
2323 Set_String (" function ");
2325 Set_String (" procedure ");
2328 Set_String (Get_Main_Name);
2330 -- Generate argument list if present
2332 if Command_Line_Args_On_Target then
2333 Write_Statement_Buffer;
2334 WBI (" (argc : Integer;");
2335 WBI (" argv : System.Address;");
2337 (" envp : System.Address)");
2339 if Exit_Status_Supported_On_Target then
2340 Write_Statement_Buffer;
2341 WBI (" return Integer;");
2343 Write_Statement_Buffer (";");
2347 if Exit_Status_Supported_On_Target then
2348 Write_Statement_Buffer (" return Integer;");
2350 Write_Statement_Buffer (";");
2354 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2355 Get_Main_Name & """);");
2364 WBI ("end " & Ada_Main & ";");
2365 Close_Binder_Output;
2367 -- Prepare to write body
2369 Create_Binder_Output (Filename, 'b', Bfileb);
2371 -- We always compile the binder file in Ada 95 mode so that we properly
2372 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2373 -- of the Ada 2005 constructs are needed by the binder file.
2375 WBI ("pragma Ada_95;");
2377 -- Output Source_File_Name pragmas which look like
2379 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2380 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2382 -- where sss/bbb are the spec/body file names respectively
2384 Get_Name_String (Bfiles);
2385 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2387 WBI ("pragma Source_File_Name (" &
2389 ", Spec_File_Name => """ &
2390 Name_Buffer (1 .. Name_Len + 3));
2392 Get_Name_String (Bfileb);
2393 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2395 WBI ("pragma Source_File_Name (" &
2397 ", Body_File_Name => """ &
2398 Name_Buffer (1 .. Name_Len + 3));
2400 -- Generate with of System.Restrictions to initialize
2401 -- Run_Time_Restrictions.
2403 if System_Restrictions_Used
2404 and not Suppress_Standard_Library_On_Target
2407 WBI ("with System.Restrictions;");
2411 WBI ("package body " & Ada_Main & " is");
2412 WBI (" pragma Warnings (Off);");
2414 -- Import the finalization procedure only if finalization active
2416 if not Cumulative_Restrictions.Set (No_Finalization) then
2418 -- In the Java case, pragma Import C cannot be used, so the
2419 -- standard Ada constructs will be used instead.
2421 if VM_Target = No_VM then
2423 WBI (" procedure Do_Finalize;");
2425 (" pragma Import (C, Do_Finalize, " &
2426 """system__standard_library__adafinal"");");
2431 if not Suppress_Standard_Library_On_Target then
2433 -- Generate Priority_Specific_Dispatching pragma string
2436 (" Local_Priority_Specific_Dispatching : constant String := """);
2438 for J in 0 .. PSD_Pragma_Settings.Last loop
2439 Set_Char (PSD_Pragma_Settings.Table (J));
2443 Write_Statement_Buffer;
2445 -- Generate Interrupt_State pragma string
2447 Set_String (" Local_Interrupt_States : constant String := """);
2449 for J in 0 .. IS_Pragma_Settings.Last loop
2450 Set_Char (IS_Pragma_Settings.Table (J));
2454 Write_Statement_Buffer;
2462 if Bind_Main_Program and then VM_Target = No_VM then
2464 -- When suppressing the standard library then generate dummy body
2467 if Suppress_Standard_Library_On_Target then
2469 WBI (" procedure Break_Start is");
2478 -- Output object file list and the Ada body is complete
2480 Gen_Object_Files_Options;
2483 WBI ("end " & Ada_Main & ";");
2485 Close_Binder_Output;
2486 end Gen_Output_File_Ada;
2488 -----------------------
2489 -- Gen_Output_File_C --
2490 -----------------------
2492 procedure Gen_Output_File_C (Filename : String) is
2494 pragma Warnings (Off, Bfile);
2495 -- Name of generated bind file (not referenced)
2498 Create_Binder_Output (Filename, 'c', Bfile);
2500 Resolve_Binder_Options;
2502 WBI ("extern void " & Ada_Final_Name.all & " (void);");
2504 -- If -a has been specified use __attribute__((constructor)) for the
2505 -- init procedure. No need to use a similar featute for the final
2506 -- procedure as global finalization will occur when the executable
2507 -- finishes execution and for plugins (shared stand-alone libraries that
2508 -- can be "unloaded"), finalization should not occur automatically,
2509 -- otherwise the main executable may not continue to work properly.
2511 if Use_Pragma_Linker_Constructor then
2512 WBI ("extern void " & Ada_Init_Name.all &
2513 " (void) __attribute__((constructor));");
2515 WBI ("extern void " & Ada_Init_Name.all & " (void);");
2518 WBI ("extern void system__standard_library__adafinal (void);");
2520 if not No_Main_Subprogram then
2521 Set_String ("extern ");
2523 if Exit_Status_Supported_On_Target then
2526 Set_String ("void");
2529 Set_String (" main ");
2531 if Command_Line_Args_On_Target then
2532 Write_Statement_Buffer ("(int, char **, char **);");
2534 Write_Statement_Buffer ("(void);");
2537 if OpenVMS_On_Target then
2538 WBI ("extern void decc$__posix_exit (int);");
2540 WBI ("extern void exit (int);");
2543 WBI ("extern void __gnat_break_start (void);");
2544 Set_String ("extern ");
2546 if ALIs.Table (ALIs.First).Main_Program = Proc then
2547 Set_String ("void ");
2549 Set_String ("int ");
2552 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2553 Set_Main_Program_Name;
2554 Set_String (" (void);");
2555 Write_Statement_Buffer;
2558 if not Suppress_Standard_Library_On_Target then
2559 WBI ("extern void __gnat_initialize (void *);");
2560 WBI ("extern void __gnat_finalize (void);");
2561 WBI ("extern void __gnat_install_handler (void);");
2564 if Dynamic_Stack_Measurement then
2566 WBI ("extern void __gnat_stack_usage_output_results (void);");
2567 WBI ("extern void __gnat_stack_usage_initialize (int size);");
2570 -- Initialize stack limit for the environment task if the stack
2571 -- check method is stack limit and stack check is enabled.
2573 if Stack_Check_Limits_On_Target
2574 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
2577 WBI ("extern void __gnat_initialize_stack_limit (void);");
2584 -- Imported variable used to track elaboration/finalization phase.
2585 -- Used only when we have a runtime.
2587 if not Suppress_Standard_Library_On_Target then
2588 WBI ("extern int __gnat_handler_installed;");
2592 -- Write argv/argc exit status stuff if main program case
2594 if Bind_Main_Program then
2596 -- First deal with argc/argv/envp. In the normal case they
2597 -- are in the run-time library.
2599 if not Configurable_Run_Time_On_Target then
2600 WBI ("extern int gnat_argc;");
2601 WBI ("extern char **gnat_argv;");
2602 WBI ("extern char **gnat_envp;");
2604 -- If configurable run time and no command line args, then the
2605 -- generation of these variables is entirely suppressed.
2607 elsif not Command_Line_Args_On_Target then
2610 -- Otherwise, in the configurable run-time case they are right in
2614 WBI ("int gnat_argc;");
2615 WBI ("char **gnat_argv;");
2616 WBI ("char **gnat_envp;");
2619 -- Similarly deal with exit status
2620 -- are in the run-time library.
2622 if not Configurable_Run_Time_On_Target then
2623 WBI ("extern int gnat_exit_status;");
2625 -- If configurable run time and no exit status on target, then
2626 -- the generation of this variables is entirely suppressed.
2628 elsif not Exit_Status_Supported_On_Target then
2631 -- Otherwise, in the configurable run-time case this variable is
2632 -- right in the binder file, and initialized to zero there.
2635 WBI ("int gnat_exit_status = 0;");
2641 -- When suppressing the standard library, the __gnat_break_start
2642 -- routine (for the debugger to get initial control) is defined in
2645 if Suppress_Standard_Library_On_Target then
2647 WBI ("void __gnat_break_start (void) {}");
2650 -- Generate the __gnat_version and __gnat_ada_main_program_name info
2651 -- only for the main program. Otherwise, it can lead under some
2652 -- circumstances to a symbol duplication during the link (for instance
2653 -- when a C program uses 2 Ada libraries)
2655 if Bind_Main_Program then
2657 WBI ("char __gnat_version[] = ""GNAT Version: " &
2658 Gnat_Version_String & """;");
2660 Set_String ("char __gnat_ada_main_program_name[] = """);
2661 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2662 Set_Main_Program_Name;
2664 Write_Statement_Buffer;
2667 -- Generate the adafinal routine. In no runtime mode, this is
2668 -- not needed, since there is no finalization to do.
2670 if not Cumulative_Restrictions.Set (No_Finalization) then
2676 -- Main is only present for Ada main case
2678 if Bind_Main_Program then
2682 -- Generate versions, elaboration order, list of object files
2686 Gen_Object_Files_Options;
2688 -- C binder output is complete
2690 Close_Binder_Output;
2691 end Gen_Output_File_C;
2693 --------------------------
2694 -- Gen_Restrictions_Ada --
2695 --------------------------
2697 procedure Gen_Restrictions_Ada is
2701 if Suppress_Standard_Library_On_Target
2702 or not System_Restrictions_Used
2707 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2713 for J in Cumulative_Restrictions.Set'First ..
2714 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2716 Set_Boolean (Cumulative_Restrictions.Set (J));
2721 Write_Statement_Buffer;
2728 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last));
2730 Write_Statement_Buffer;
2731 Set_String (" Value => (");
2733 for J in Cumulative_Restrictions.Value'First ..
2734 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2736 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2740 Set_Int (Int (Cumulative_Restrictions.Value
2741 (Cumulative_Restrictions.Value'Last)));
2743 Write_Statement_Buffer;
2744 WBI (" Violated =>");
2748 for J in Cumulative_Restrictions.Violated'First ..
2749 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2751 Set_Boolean (Cumulative_Restrictions.Violated (J));
2756 Write_Statement_Buffer;
2762 Set_Boolean (Cumulative_Restrictions.Violated
2763 (Cumulative_Restrictions.Violated'Last));
2765 Write_Statement_Buffer;
2766 Set_String (" Count => (");
2768 for J in Cumulative_Restrictions.Count'First ..
2769 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2771 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2775 Set_Int (Int (Cumulative_Restrictions.Count
2776 (Cumulative_Restrictions.Count'Last)));
2778 Write_Statement_Buffer;
2779 Set_String (" Unknown => (");
2781 for J in Cumulative_Restrictions.Unknown'First ..
2782 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2784 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2789 (Cumulative_Restrictions.Unknown
2790 (Cumulative_Restrictions.Unknown'Last));
2792 Write_Statement_Buffer;
2793 end Gen_Restrictions_Ada;
2795 ------------------------
2796 -- Gen_Restrictions_C --
2797 ------------------------
2799 procedure Gen_Restrictions_C is
2801 if Suppress_Standard_Library_On_Target
2802 or not System_Restrictions_Used
2807 WBI (" typedef struct {");
2808 Set_String (" char set [");
2809 Set_Int (Cumulative_Restrictions.Set'Length);
2811 Write_Statement_Buffer;
2813 Set_String (" int value [");
2814 Set_Int (Cumulative_Restrictions.Value'Length);
2816 Write_Statement_Buffer;
2818 Set_String (" char violated [");
2819 Set_Int (Cumulative_Restrictions.Violated'Length);
2821 Write_Statement_Buffer;
2823 Set_String (" int count [");
2824 Set_Int (Cumulative_Restrictions.Count'Length);
2826 Write_Statement_Buffer;
2828 Set_String (" char unknown [");
2829 Set_Int (Cumulative_Restrictions.Unknown'Length);
2831 Write_Statement_Buffer;
2832 WBI (" } restrictions;");
2833 WBI (" extern restrictions " &
2834 "system__restrictions__run_time_restrictions;");
2835 WBI (" restrictions r = {");
2838 for J in Cumulative_Restrictions.Set'First ..
2839 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2841 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
2845 Set_Int (Boolean'Pos
2846 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last)));
2848 Write_Statement_Buffer;
2851 for J in Cumulative_Restrictions.Value'First ..
2852 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2854 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2858 Set_Int (Int (Cumulative_Restrictions.Value
2859 (Cumulative_Restrictions.Value'Last)));
2861 Write_Statement_Buffer;
2864 for J in Cumulative_Restrictions.Violated'First ..
2865 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2867 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
2871 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated
2872 (Cumulative_Restrictions.Violated'Last)));
2874 Write_Statement_Buffer;
2877 for J in Cumulative_Restrictions.Count'First ..
2878 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2880 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2884 Set_Int (Int (Cumulative_Restrictions.Count
2885 (Cumulative_Restrictions.Count'Last)));
2887 Write_Statement_Buffer;
2890 for J in Cumulative_Restrictions.Unknown'First ..
2891 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2893 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
2897 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown
2898 (Cumulative_Restrictions.Unknown'Last)));
2900 Write_Statement_Buffer;
2901 WBI (" system__restrictions__run_time_restrictions = r;");
2902 end Gen_Restrictions_C;
2904 ----------------------
2905 -- Gen_Versions_Ada --
2906 ----------------------
2908 -- This routine generates two sets of lines. The first set has the form:
2910 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2912 -- The second set has the form
2914 -- pragma Export (C, unnnnn, unam);
2916 -- for each unit, where unam is the unit name suffixed by either B or
2917 -- S for body or spec, with dots replaced by double underscores, and
2918 -- hhhhhhhh is the version number, and nnnnn is a 5-digits serial number.
2920 procedure Gen_Versions_Ada is
2921 Ubuf : String (1 .. 6) := "u00000";
2923 procedure Increment_Ubuf;
2924 -- Little procedure to increment the serial number
2926 procedure Increment_Ubuf is
2928 for J in reverse Ubuf'Range loop
2929 Ubuf (J) := Character'Succ (Ubuf (J));
2930 exit when Ubuf (J) <= '9';
2935 -- Start of processing for Gen_Versions_Ada
2938 if Bind_For_Library then
2940 -- When building libraries, the version number of each unit can
2941 -- not be computed, since the binder does not know the full list
2942 -- of units. Therefore, the 'Version and 'Body_Version
2943 -- attributes cannot supported in this case.
2950 WBI (" type Version_32 is mod 2 ** 32;");
2951 for U in Units.First .. Units.Last loop
2953 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2954 Units.Table (U).Version & "#;");
2960 for U in Units.First .. Units.Last loop
2962 Set_String (" pragma Export (C, ");
2964 Set_String (", """);
2966 Get_Name_String (Units.Table (U).Uname);
2968 for K in 1 .. Name_Len loop
2969 if Name_Buffer (K) = '.' then
2973 elsif Name_Buffer (K) = '%' then
2977 Set_Char (Name_Buffer (K));
2981 if Name_Buffer (Name_Len) = 's' then
2987 Set_String (""");");
2988 Write_Statement_Buffer;
2991 end Gen_Versions_Ada;
2993 --------------------
2994 -- Gen_Versions_C --
2995 --------------------
2997 -- This routine generates a line of the form:
2999 -- unsigned unam = 0xhhhhhhhh;
3001 -- for each unit, where unam is the unit name suffixed by either B or
3002 -- S for body or spec, with dots replaced by double underscores.
3004 procedure Gen_Versions_C is
3006 if Bind_For_Library then
3008 -- When building libraries, the version number of each unit can
3009 -- not be computed, since the binder does not know the full list
3010 -- of units. Therefore, the 'Version and 'Body_Version
3011 -- attributes cannot supported.
3016 for U in Units.First .. Units.Last loop
3017 Set_String ("unsigned ");
3019 Get_Name_String (Units.Table (U).Uname);
3021 for K in 1 .. Name_Len loop
3022 if Name_Buffer (K) = '.' then
3025 elsif Name_Buffer (K) = '%' then
3029 Set_Char (Name_Buffer (K));
3033 if Name_Buffer (Name_Len) = 's' then
3039 Set_String (" = 0x");
3040 Set_String (Units.Table (U).Version);
3042 Write_Statement_Buffer;
3047 ------------------------
3048 -- Get_Main_Unit_Name --
3049 ------------------------
3051 function Get_Main_Unit_Name (S : String) return String is
3052 Result : String := S;
3055 for J in S'Range loop
3056 if Result (J) = '.' then
3062 end Get_Main_Unit_Name;
3064 -----------------------
3065 -- Get_Ada_Main_Name --
3066 -----------------------
3068 function Get_Ada_Main_Name return String is
3069 Suffix : constant String := "_00";
3070 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3071 Opt.Ada_Main_Name.all & Suffix;
3075 -- The main program generated by JGNAT expects a package called
3076 -- ada_<main procedure>.
3078 if VM_Target /= No_VM then
3079 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3080 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3083 -- This loop tries the following possibilities in order
3089 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3090 -- it is set to 'ada_main'.
3092 for J in 0 .. 99 loop
3094 Nlen := Name'Length - Suffix'Length;
3096 Nlen := Name'Length;
3097 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3098 Name (Name'Last - 1) :=
3099 Character'Val (J / 10 + Character'Pos ('0'));
3102 for K in ALIs.First .. ALIs.Last loop
3103 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3105 -- Get unit name, removing %b or %e at end
3107 Get_Name_String (Units.Table (L).Uname);
3108 Name_Len := Name_Len - 2;
3110 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3116 return Name (1 .. Nlen);
3122 -- If we fall through, just use a peculiar unlikely name
3124 return ("Qwertyuiop");
3125 end Get_Ada_Main_Name;
3131 function Get_Main_Name return String is
3133 -- Explicit name given with -M switch
3135 if Bind_Alternate_Main_Name then
3136 return Alternate_Main_Name.all;
3138 -- Case of main program name to be used directly
3140 elsif Use_Ada_Main_Program_Name_On_Target then
3142 -- Get main program name
3144 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3146 -- If this is a child name, return only the name of the child,
3147 -- since we can't have dots in a nested program name. Note that
3148 -- we do not include the %b at the end of the unit name.
3150 for J in reverse 1 .. Name_Len - 2 loop
3151 if J = 1 or else Name_Buffer (J - 1) = '.' then
3152 return Name_Buffer (J .. Name_Len - 2);
3156 raise Program_Error; -- impossible exit
3158 -- Case where "main" is to be used as default
3165 ---------------------
3166 -- Get_WC_Encoding --
3167 ---------------------
3169 function Get_WC_Encoding return Character is
3171 -- If encoding method specified by -W switch, then return it
3173 if Wide_Character_Encoding_Method_Specified then
3174 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3176 -- If no main program, and not specified, set brackets, we really have
3177 -- no better choice. If some other encoding is required when there is
3178 -- no main, it must be set explicitly using -Wx.
3180 -- Note: if the ALI file always passed the wide character encoding
3181 -- of every file, then we could use the encoding of the initial
3182 -- specified file, but this information is passed only for potential
3183 -- main programs. We could fix this sometime, but it is a very minor
3184 -- point (wide character default encoding for [Wide_[Wide_]Text_IO
3185 -- when there is no main program).
3187 elsif No_Main_Subprogram then
3190 -- Otherwise if there is a main program, take encoding from it
3193 return ALIs.Table (ALIs.First).WC_Encoding;
3195 end Get_WC_Encoding;
3197 ----------------------
3198 -- Lt_Linker_Option --
3199 ----------------------
3201 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3203 -- Sort internal files last
3205 if Linker_Options.Table (Op1).Internal_File
3207 Linker_Options.Table (Op2).Internal_File
3209 -- Note: following test uses False < True
3211 return Linker_Options.Table (Op1).Internal_File
3213 Linker_Options.Table (Op2).Internal_File;
3215 -- If both internal or both non-internal, sort according to the
3216 -- elaboration position. A unit that is elaborated later should
3217 -- come earlier in the linker options list.
3220 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3222 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3225 end Lt_Linker_Option;
3227 ------------------------
3228 -- Move_Linker_Option --
3229 ------------------------
3231 procedure Move_Linker_Option (From : Natural; To : Natural) is
3233 Linker_Options.Table (To) := Linker_Options.Table (From);
3234 end Move_Linker_Option;
3236 ----------------------------
3237 -- Resolve_Binder_Options --
3238 ----------------------------
3240 procedure Resolve_Binder_Options is
3242 for E in Elab_Order.First .. Elab_Order.Last loop
3243 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3245 -- The procedure of looking for specific packages and setting
3246 -- flags is somewhat dubious, but there isn't a good alternative
3247 -- at the current time ???
3249 if Name_Buffer (1 .. 19) = "system.os_interface" then
3253 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3254 With_DECGNAT := True;
3257 end Resolve_Binder_Options;
3263 procedure Set_Boolean (B : Boolean) is
3264 True_Str : constant String := "True";
3265 False_Str : constant String := "False";
3268 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3269 Last := Last + True_Str'Length;
3271 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3272 Last := Last + False_Str'Length;
3280 procedure Set_Char (C : Character) is
3283 Statement_Buffer (Last) := C;
3290 procedure Set_Int (N : Int) is
3302 Statement_Buffer (Last) :=
3303 Character'Val (N mod 10 + Character'Pos ('0'));
3307 -------------------------
3308 -- Set_IS_Pragma_Table --
3309 -------------------------
3311 procedure Set_IS_Pragma_Table is
3313 for F in ALIs.First .. ALIs.Last loop
3314 for K in ALIs.Table (F).First_Interrupt_State ..
3315 ALIs.Table (F).Last_Interrupt_State
3318 Inum : constant Int :=
3319 Interrupt_States.Table (K).Interrupt_Id;
3320 Stat : constant Character :=
3321 Interrupt_States.Table (K).Interrupt_State;
3324 while IS_Pragma_Settings.Last < Inum loop
3325 IS_Pragma_Settings.Append ('n');
3328 IS_Pragma_Settings.Table (Inum) := Stat;
3332 end Set_IS_Pragma_Table;
3334 ---------------------------
3335 -- Set_Main_Program_Name --
3336 ---------------------------
3338 procedure Set_Main_Program_Name is
3340 -- Note that name has %b on the end which we ignore
3342 -- First we output the initial _ada_ since we know that the main
3343 -- program is a library level subprogram.
3345 Set_String ("_ada_");
3347 -- Copy name, changing dots to double underscores
3349 for J in 1 .. Name_Len - 2 loop
3350 if Name_Buffer (J) = '.' then
3353 Set_Char (Name_Buffer (J));
3356 end Set_Main_Program_Name;
3358 ---------------------
3359 -- Set_Name_Buffer --
3360 ---------------------
3362 procedure Set_Name_Buffer is
3364 for J in 1 .. Name_Len loop
3365 Set_Char (Name_Buffer (J));
3367 end Set_Name_Buffer;
3369 -------------------------
3370 -- Set_PSD_Pragma_Table --
3371 -------------------------
3373 procedure Set_PSD_Pragma_Table is
3375 for F in ALIs.First .. ALIs.Last loop
3376 for K in ALIs.Table (F).First_Specific_Dispatching ..
3377 ALIs.Table (F).Last_Specific_Dispatching
3380 DTK : Specific_Dispatching_Record
3381 renames Specific_Dispatching.Table (K);
3384 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3385 PSD_Pragma_Settings.Append ('F');
3388 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3389 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3394 end Set_PSD_Pragma_Table;
3400 procedure Set_String (S : String) is
3402 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3403 Last := Last + S'Length;
3410 procedure Set_Unit_Name is
3412 for J in 1 .. Name_Len - 2 loop
3413 if Name_Buffer (J) /= '.' then
3414 Set_Char (Name_Buffer (J));
3421 ---------------------
3422 -- Set_Unit_Number --
3423 ---------------------
3425 procedure Set_Unit_Number (U : Unit_Id) is
3426 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3427 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3430 if Num_Units >= 10 and then Unum < 10 then
3434 if Num_Units >= 100 and then Unum < 100 then
3439 end Set_Unit_Number;
3441 ----------------------
3442 -- Write_Info_Ada_C --
3443 ----------------------
3445 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3447 if Ada_Bind_File then
3449 S : String (1 .. Ada'Length + Common'Length);
3451 S (1 .. Ada'Length) := Ada;
3452 S (Ada'Length + 1 .. S'Length) := Common;
3458 S : String (1 .. C'Length + Common'Length);
3460 S (1 .. C'Length) := C;
3461 S (C'Length + 1 .. S'Length) := Common;
3465 end Write_Info_Ada_C;
3467 ----------------------------
3468 -- Write_Statement_Buffer --
3469 ----------------------------
3471 procedure Write_Statement_Buffer is
3473 WBI (Statement_Buffer (1 .. Last));
3475 end Write_Statement_Buffer;
3477 procedure Write_Statement_Buffer (S : String) is
3480 Write_Statement_Buffer;
3481 end Write_Statement_Buffer;