1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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.
202 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
203 -- not. A value of zero indicates that the pragma is not present, while a
204 -- value of 1 signals its presence in the partition.
206 -- Default_Stack_Size is the default stack size used when creating an Ada
207 -- task with no explicit Storage_Size clause.
209 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
210 -- disabled. A value of zero indicates that leap seconds are turned "off",
211 -- while a value of one signifies "on" status.
213 -----------------------
214 -- Local Subprograms --
215 -----------------------
217 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
218 -- Convenient shorthand used throughout
220 procedure Check_System_Restrictions_Used;
221 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
222 -- System.Restrictions is present in the partition, otherwise False).
224 procedure Gen_Adainit_Ada;
225 -- Generates the Adainit procedure (Ada code case)
227 procedure Gen_Adainit_C;
228 -- Generates the Adainit procedure (C code case)
230 procedure Gen_Adafinal_Ada;
231 -- Generate the Adafinal procedure (Ada code case)
233 procedure Gen_Adafinal_C;
234 -- Generate the Adafinal procedure (C code case)
236 procedure Gen_Elab_Calls_Ada;
237 -- Generate sequence of elaboration calls (Ada code case)
239 procedure Gen_Elab_Calls_C;
240 -- Generate sequence of elaboration calls (C code case)
242 procedure Gen_Elab_Order_Ada;
243 -- Generate comments showing elaboration order chosen (Ada case)
245 procedure Gen_Elab_Order_C;
246 -- Generate comments showing elaboration order chosen (C case)
248 procedure Gen_Elab_Defs_C;
249 -- Generate sequence of definitions for elaboration routines (C code case)
251 procedure Gen_Main_Ada;
252 -- Generate procedure main (Ada code case)
254 procedure Gen_Main_C;
255 -- Generate main() procedure (C code case)
257 procedure Gen_Object_Files_Options;
258 -- Output comments containing a list of the full names of the object
259 -- files to be linked and the list of linker options supplied by
260 -- Linker_Options pragmas in the source. (C and Ada code case)
262 procedure Gen_Output_File_Ada (Filename : String);
263 -- Generate output file (Ada code case)
265 procedure Gen_Output_File_C (Filename : String);
266 -- Generate output file (C code case)
268 procedure Gen_Restrictions_Ada;
269 -- Generate initialization of restrictions variable (Ada code case)
271 procedure Gen_Restrictions_C;
272 -- Generate initialization of restrictions variable (C code case)
274 procedure Gen_Versions_Ada;
275 -- Output series of definitions for unit versions (Ada code case)
277 procedure Gen_Versions_C;
278 -- Output series of definitions for unit versions (C code case)
280 function Get_Ada_Main_Name return String;
281 -- This function is used in the Ada main output case to compute a usable
282 -- name for the generated main program. The normal main program name is
283 -- Ada_Main, but this won't work if the user has a unit with this name.
284 -- This function tries Ada_Main first, and if there is such a clash, then
285 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
287 function Get_Main_Unit_Name (S : String) return String;
288 -- Return the main unit name corresponding to S by replacing '.' with '_'
290 function Get_Main_Name return String;
291 -- This function is used in the Ada main output case to compute the
292 -- correct external main program. It is "main" by default, unless the
293 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
294 -- is the name of the Ada main name without the "_ada". This default
295 -- can be overridden explicitly using the -Mname binder switch.
297 function Get_WC_Encoding return Character;
298 -- Return wide character encoding method to set as WC_Encoding in output.
299 -- If -W has been used, returns the specified encoding, otherwise returns
300 -- the encoding method used for the main program source. If there is no
301 -- main program source (-z switch used), returns brackets ('b').
303 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
304 -- Compare linker options, when sorting, first according to
305 -- Is_Internal_File (internal files come later) and then by
306 -- elaboration order position (latest to earliest).
308 procedure Move_Linker_Option (From : Natural; To : Natural);
309 -- Move routine for sorting linker options
311 procedure Resolve_Binder_Options;
312 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
313 -- since it tests for a package named "dec" which might cause a conflict
314 -- on non-VMS systems.
316 procedure Set_Char (C : Character);
317 -- Set given character in Statement_Buffer at the Last + 1 position
318 -- and increment Last by one to reflect the stored character.
320 procedure Set_Int (N : Int);
321 -- Set given value in decimal in Statement_Buffer with no spaces
322 -- starting at the Last + 1 position, and updating Last past the value.
323 -- A minus sign is output for a negative value.
325 procedure Set_Boolean (B : Boolean);
326 -- Set given boolean value in Statement_Buffer at the Last + 1 position
327 -- and update Last past the value.
329 procedure Set_IS_Pragma_Table;
330 -- Initializes contents of IS_Pragma_Settings table from ALI table
332 procedure Set_Main_Program_Name;
333 -- Given the main program name in Name_Buffer (length in Name_Len)
334 -- generate the name of the routine to be used in the call. The name
335 -- is generated starting at Last + 1, and Last is updated past it.
337 procedure Set_Name_Buffer;
338 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
340 procedure Set_PSD_Pragma_Table;
341 -- Initializes contents of PSD_Pragma_Settings table from ALI table
343 procedure Set_String (S : String);
344 -- Sets characters of given string in Statement_Buffer, starting at the
345 -- Last + 1 position, and updating last past the string value.
347 procedure Set_Unit_Name;
348 -- Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
349 -- starting at the Last + 1 position, and updating last past the value.
350 -- changing periods to double underscores, and updating Last appropriately.
352 procedure Set_Unit_Number (U : Unit_Id);
353 -- Sets unit number (first unit is 1, leading zeroes output to line
354 -- up all output unit numbers nicely as required by the value, and
355 -- by the total number of units.
357 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
358 -- For C code case, write C & Common, for Ada case write Ada & Common
359 -- to current binder output file using Write_Binder_Info.
361 procedure Write_Statement_Buffer;
362 -- Write out contents of statement buffer up to Last, and reset Last to 0
364 procedure Write_Statement_Buffer (S : String);
365 -- First writes its argument (using Set_String (S)), then writes out the
366 -- contents of statement buffer up to Last, and reset Last to 0
368 ------------------------------------
369 -- Check_System_Restrictions_Used --
370 ------------------------------------
372 procedure Check_System_Restrictions_Used is
374 for J in Units.First .. Units.Last loop
375 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
376 System_Restrictions_Used := True;
381 System_Restrictions_Used := False;
382 end Check_System_Restrictions_Used;
384 ----------------------
385 -- Gen_Adafinal_Ada --
386 ----------------------
388 procedure Gen_Adafinal_Ada is
391 WBI (" procedure " & Ada_Final_Name.all & " is");
394 -- If compiling for the JVM, we directly call Adafinal because
395 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
397 if VM_Target /= No_VM then
398 WBI (" System.Standard_Library.Adafinal;");
400 -- If there is no finalization, there is nothing to do
402 elsif Cumulative_Restrictions.Set (No_Finalization) then
405 WBI (" Do_Finalize;");
408 WBI (" end " & Ada_Final_Name.all & ";");
409 end Gen_Adafinal_Ada;
415 procedure Gen_Adafinal_C is
417 WBI ("void " & Ada_Final_Name.all & " (void) {");
418 WBI (" system__standard_library__adafinal ();");
423 ---------------------
424 -- Gen_Adainit_Ada --
425 ---------------------
427 procedure Gen_Adainit_Ada is
428 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
431 WBI (" procedure " & Ada_Init_Name.all & " is");
433 -- Generate externals for elaboration entities
435 for E in Elab_Order.First .. Elab_Order.Last loop
437 Unum : constant Unit_Id := Elab_Order.Table (E);
438 U : Unit_Record renames Units.Table (Unum);
441 -- Check for Elab_Entity to be set for this unit
445 -- Don't generate reference for stand alone library
447 and then not U.SAL_Interface
449 -- Don't generate reference for predefined file in No_Run_Time
450 -- mode, since we don't include the object files in this case
454 and then Is_Predefined_File_Name (U.Sfile))
458 Set_Unit_Number (Unum);
461 when No_VM | JVM_Target =>
462 Set_String (" : Boolean; pragma Import (Ada, ");
464 Set_String (" : Boolean; pragma Import (CIL, ");
468 Set_Unit_Number (Unum);
470 Get_Name_String (U.Uname);
472 -- In the case of JGNAT we need to emit an Import name
473 -- that includes the class name (using '$' separators
474 -- in the case of a child unit name).
476 if VM_Target /= No_VM then
477 for J in 1 .. Name_Len - 2 loop
478 if VM_Target = CLI_Target
479 or else Name_Buffer (J) /= '.'
481 Set_Char (Name_Buffer (J));
487 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
490 Set_String ("_pkg.");
493 -- If the unit name is very long, then split the
494 -- Import link name across lines using "&" (occurs
495 -- in some C2 tests).
497 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
499 Write_Statement_Buffer;
505 Set_String ("_E"");");
506 Write_Statement_Buffer;
511 Write_Statement_Buffer;
513 -- If the standard library is suppressed, then the only global variable
514 -- that might be needed (by the Ravenscar profile) is the priority of
517 if Suppress_Standard_Library_On_Target then
518 if Main_Priority /= No_Main_Priority then
519 WBI (" Main_Priority : Integer;");
520 WBI (" pragma Import (C, Main_Priority," &
521 " ""__gl_main_priority"");");
527 if Main_Priority /= No_Main_Priority then
528 Set_String (" Main_Priority := ");
529 Set_Int (Main_Priority);
531 Write_Statement_Buffer;
537 -- Normal case (standard library not suppressed). Set all global values
538 -- used by the run time.
541 WBI (" Main_Priority : Integer;");
542 WBI (" pragma Import (C, Main_Priority, " &
543 """__gl_main_priority"");");
544 WBI (" Time_Slice_Value : Integer;");
545 WBI (" pragma Import (C, Time_Slice_Value, " &
546 """__gl_time_slice_val"");");
547 WBI (" WC_Encoding : Character;");
548 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
549 WBI (" Locking_Policy : Character;");
550 WBI (" pragma Import (C, Locking_Policy, " &
551 """__gl_locking_policy"");");
552 WBI (" Queuing_Policy : Character;");
553 WBI (" pragma Import (C, Queuing_Policy, " &
554 """__gl_queuing_policy"");");
555 WBI (" Task_Dispatching_Policy : Character;");
556 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
557 """__gl_task_dispatching_policy"");");
558 WBI (" Priority_Specific_Dispatching : System.Address;");
559 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
560 """__gl_priority_specific_dispatching"");");
561 WBI (" Num_Specific_Dispatching : Integer;");
562 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
563 """__gl_num_specific_dispatching"");");
565 WBI (" Interrupt_States : System.Address;");
566 WBI (" pragma Import (C, Interrupt_States, " &
567 """__gl_interrupt_states"");");
568 WBI (" Num_Interrupt_States : Integer;");
569 WBI (" pragma Import (C, Num_Interrupt_States, " &
570 """__gl_num_interrupt_states"");");
571 WBI (" Unreserve_All_Interrupts : Integer;");
572 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
573 """__gl_unreserve_all_interrupts"");");
575 if Exception_Tracebacks then
576 WBI (" Exception_Tracebacks : Integer;");
577 WBI (" pragma Import (C, Exception_Tracebacks, " &
578 """__gl_exception_tracebacks"");");
581 WBI (" Zero_Cost_Exceptions : Integer;");
582 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
583 """__gl_zero_cost_exceptions"");");
584 WBI (" Detect_Blocking : Integer;");
585 WBI (" pragma Import (C, Detect_Blocking, " &
586 """__gl_detect_blocking"");");
587 WBI (" Default_Stack_Size : Integer;");
588 WBI (" pragma Import (C, Default_Stack_Size, " &
589 """__gl_default_stack_size"");");
590 WBI (" Leap_Seconds_Support : Integer;");
591 WBI (" pragma Import (C, Leap_Seconds_Support, " &
592 """__gl_leap_seconds_support"");");
594 -- Import entry point for elaboration time signal handler
595 -- installation, and indication of if it's been called previously.
598 WBI (" procedure Install_Handler;");
599 WBI (" pragma Import (C, Install_Handler, " &
600 """__gnat_install_handler"");");
602 WBI (" Handler_Installed : Integer;");
603 WBI (" pragma Import (C, Handler_Installed, " &
604 """__gnat_handler_installed"");");
606 -- Import entry point for environment feature enable/disable
607 -- routine, and indication that it's been called previously.
609 if OpenVMS_On_Target then
611 WBI (" procedure Set_Features;");
612 WBI (" pragma Import (C, Set_Features, " &
613 """__gnat_set_features"");");
615 WBI (" Features_Set : Integer;");
616 WBI (" pragma Import (C, Features_Set, " &
617 """__gnat_features_set"");");
620 -- Initialize stack limit variable of the environment task if the
621 -- stack check method is stack limit and stack check is enabled.
623 if Stack_Check_Limits_On_Target
624 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
627 WBI (" procedure Initialize_Stack_Limit;");
628 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
629 """__gnat_initialize_stack_limit"");");
632 -- Special processing when main program is CIL function/procedure
634 if VM_Target = CLI_Target
635 and then Bind_Main_Program
636 and then not No_Main_Subprogram
640 -- Function case, use Set_Exit_Status to report the returned
641 -- status code, since that is the only mechanism available.
643 if ALIs.Table (ALIs.First).Main_Program = Func then
644 WBI (" Result : Integer;");
645 WBI (" procedure Set_Exit_Status (Code : Integer);");
646 WBI (" pragma Import (C, Set_Exit_Status, " &
647 """__gnat_set_exit_status"");");
649 WBI (" function Ada_Main_Program return Integer;");
654 WBI (" procedure Ada_Main_Program;");
657 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
658 Name_Len := Name_Len - 2;
659 WBI (" pragma Import (CIL, Ada_Main_Program, """
660 & Name_Buffer (1 .. Name_Len) & "."
661 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
666 Set_String (" Main_Priority := ");
667 Set_Int (Main_Priority);
669 Write_Statement_Buffer;
671 Set_String (" Time_Slice_Value := ");
673 if Task_Dispatching_Policy_Specified = 'F'
674 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
678 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
682 Write_Statement_Buffer;
684 Set_String (" WC_Encoding := '");
685 Set_Char (Get_WC_Encoding);
688 Write_Statement_Buffer;
690 Set_String (" Locking_Policy := '");
691 Set_Char (Locking_Policy_Specified);
693 Write_Statement_Buffer;
695 Set_String (" Queuing_Policy := '");
696 Set_Char (Queuing_Policy_Specified);
698 Write_Statement_Buffer;
700 Set_String (" Task_Dispatching_Policy := '");
701 Set_Char (Task_Dispatching_Policy_Specified);
703 Write_Statement_Buffer;
705 Gen_Restrictions_Ada;
707 WBI (" Priority_Specific_Dispatching :=");
708 WBI (" Local_Priority_Specific_Dispatching'Address;");
710 Set_String (" Num_Specific_Dispatching := ");
711 Set_Int (PSD_Pragma_Settings.Last + 1);
713 Write_Statement_Buffer;
715 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
717 Set_String (" Num_Interrupt_States := ");
718 Set_Int (IS_Pragma_Settings.Last + 1);
720 Write_Statement_Buffer;
722 Set_String (" Unreserve_All_Interrupts := ");
724 if Unreserve_All_Interrupts_Specified then
731 Write_Statement_Buffer;
733 if Exception_Tracebacks then
734 WBI (" Exception_Tracebacks := 1;");
737 Set_String (" Zero_Cost_Exceptions := ");
739 if Zero_Cost_Exceptions_Specified then
746 Write_Statement_Buffer;
748 Set_String (" Detect_Blocking := ");
750 if Detect_Blocking then
757 Write_Statement_Buffer;
759 Set_String (" Default_Stack_Size := ");
760 Set_Int (Default_Stack_Size);
762 Write_Statement_Buffer;
764 Set_String (" Leap_Seconds_Support := ");
766 if Leap_Seconds_Support then
773 Write_Statement_Buffer;
775 -- Generate call to Install_Handler
778 WBI (" if Handler_Installed = 0 then");
779 WBI (" Install_Handler;");
782 -- Generate call to Set_Features
784 if OpenVMS_On_Target then
786 WBI (" if Features_Set = 0 then");
787 WBI (" Set_Features;");
792 -- Generate call to set Initialize_Scalar values if active
794 if Initialize_Scalars_Used then
796 Set_String (" System.Scalar_Values.Initialize ('");
797 Set_Char (Initialize_Scalars_Mode1);
799 Set_Char (Initialize_Scalars_Mode2);
801 Write_Statement_Buffer;
804 -- Generate assignment of default secondary stack size if set
806 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
808 Set_String (" System.Secondary_Stack.");
809 Set_String ("Default_Secondary_Stack_Size := ");
810 Set_Int (Opt.Default_Sec_Stack_Size);
812 Write_Statement_Buffer;
815 -- Initialize stack limit variable of the environment task if the
816 -- stack check method is stack limit and stack check is enabled.
818 if Stack_Check_Limits_On_Target
819 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
822 WBI (" Initialize_Stack_Limit;");
825 -- Generate elaboration calls
830 -- Case of main program is CIL function or procedure
832 if VM_Target = CLI_Target
833 and then Bind_Main_Program
834 and then not No_Main_Subprogram
836 -- For function case, use Set_Exit_Status to set result
838 if ALIs.Table (ALIs.First).Main_Program = Func then
839 WBI (" Result := Ada_Main_Program;");
840 WBI (" Set_Exit_Status (Result);");
845 WBI (" Ada_Main_Program;");
849 WBI (" end " & Ada_Init_Name.all & ";");
856 procedure Gen_Adainit_C is
857 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
860 WBI ("void " & Ada_Init_Name.all & " (void)");
863 -- Generate externals for elaboration entities
865 for E in Elab_Order.First .. Elab_Order.Last loop
867 Unum : constant Unit_Id := Elab_Order.Table (E);
868 U : Unit_Record renames Units.Table (Unum);
871 -- Check for Elab entity to be set for this unit
875 -- Don't generate reference for stand alone library
877 and then not U.SAL_Interface
879 -- Don't generate reference for predefined file in No_Run_Time
880 -- mode, since we don't include the object files in this case
884 and then Is_Predefined_File_Name (U.Sfile))
886 Set_String (" extern char ");
887 Get_Name_String (U.Uname);
890 Write_Statement_Buffer;
895 Write_Statement_Buffer;
897 -- Standard library suppressed
899 if Suppress_Standard_Library_On_Target then
901 -- Case of High_Integrity_Mode mode. Set __gl_main_priority if needed
902 -- for the Ravenscar profile.
904 if Main_Priority /= No_Main_Priority then
905 WBI (" extern int __gl_main_priority;");
906 Set_String (" __gl_main_priority = ");
907 Set_Int (Main_Priority);
909 Write_Statement_Buffer;
912 -- Normal case (standard library not suppressed)
915 -- Generate definition for interrupt states string
917 Set_String (" static const char *local_interrupt_states = """);
919 for J in 0 .. IS_Pragma_Settings.Last loop
920 Set_Char (IS_Pragma_Settings.Table (J));
924 Write_Statement_Buffer;
926 -- Generate definition for priority specific dispatching string
929 (" static const char *local_priority_specific_dispatching = """);
931 for J in 0 .. PSD_Pragma_Settings.Last loop
932 Set_Char (PSD_Pragma_Settings.Table (J));
936 Write_Statement_Buffer;
938 -- Generate declaration for secondary stack default if needed
940 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
941 WBI (" extern int system__secondary_stack__" &
942 "default_secondary_stack_size;");
947 -- Code for normal case (standard library not suppressed)
949 -- We call the routine from inside adainit() because this works for
950 -- both programs with and without binder generated "main" functions.
952 WBI (" extern int __gl_main_priority;");
953 Set_String (" __gl_main_priority = ");
954 Set_Int (Main_Priority);
956 Write_Statement_Buffer;
958 WBI (" extern int __gl_time_slice_val;");
959 Set_String (" __gl_time_slice_val = ");
961 if Task_Dispatching_Policy = 'F'
962 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
966 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
970 Write_Statement_Buffer;
972 WBI (" extern char __gl_wc_encoding;");
973 Set_String (" __gl_wc_encoding = '");
974 Set_Char (Get_WC_Encoding);
977 Write_Statement_Buffer;
979 WBI (" extern char __gl_locking_policy;");
980 Set_String (" __gl_locking_policy = '");
981 Set_Char (Locking_Policy_Specified);
983 Write_Statement_Buffer;
985 WBI (" extern char __gl_queuing_policy;");
986 Set_String (" __gl_queuing_policy = '");
987 Set_Char (Queuing_Policy_Specified);
989 Write_Statement_Buffer;
991 WBI (" extern char __gl_task_dispatching_policy;");
992 Set_String (" __gl_task_dispatching_policy = '");
993 Set_Char (Task_Dispatching_Policy_Specified);
995 Write_Statement_Buffer;
999 WBI (" extern const void *__gl_interrupt_states;");
1000 WBI (" __gl_interrupt_states = local_interrupt_states;");
1002 WBI (" extern int __gl_num_interrupt_states;");
1003 Set_String (" __gl_num_interrupt_states = ");
1004 Set_Int (IS_Pragma_Settings.Last + 1);
1006 Write_Statement_Buffer;
1008 WBI (" extern const void *__gl_priority_specific_dispatching;");
1009 WBI (" __gl_priority_specific_dispatching =" &
1010 " local_priority_specific_dispatching;");
1012 WBI (" extern int __gl_num_specific_dispatching;");
1013 Set_String (" __gl_num_specific_dispatching = ");
1014 Set_Int (PSD_Pragma_Settings.Last + 1);
1016 Write_Statement_Buffer;
1018 WBI (" extern int __gl_unreserve_all_interrupts;");
1019 Set_String (" __gl_unreserve_all_interrupts = ");
1020 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
1022 Write_Statement_Buffer;
1024 if Exception_Tracebacks then
1025 WBI (" extern int __gl_exception_tracebacks;");
1026 WBI (" __gl_exception_tracebacks = 1;");
1029 WBI (" extern int __gl_zero_cost_exceptions;");
1030 Set_String (" __gl_zero_cost_exceptions = ");
1031 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
1033 Write_Statement_Buffer;
1035 WBI (" extern int __gl_detect_blocking;");
1036 Set_String (" __gl_detect_blocking = ");
1038 if Detect_Blocking then
1045 Write_Statement_Buffer;
1047 WBI (" extern int __gl_default_stack_size;");
1048 Set_String (" __gl_default_stack_size = ");
1049 Set_Int (Default_Stack_Size);
1051 Write_Statement_Buffer;
1053 WBI (" extern int __gl_leap_seconds_support;");
1054 Set_String (" __gl_leap_seconds_support = ");
1056 if Leap_Seconds_Support then
1063 Write_Statement_Buffer;
1067 -- Install elaboration time signal handler
1069 WBI (" if (__gnat_handler_installed == 0)");
1071 WBI (" __gnat_install_handler ();");
1074 -- Call feature enable/disable routine
1076 if OpenVMS_On_Target then
1077 WBI (" if (__gnat_features_set == 0)");
1079 WBI (" __gnat_set_features ();");
1084 -- Initialize stack limit for the environment task if the stack
1085 -- check method is stack limit and stack check is enabled.
1087 if Stack_Check_Limits_On_Target
1088 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1091 WBI (" __gnat_initialize_stack_limit ();");
1094 -- Generate call to set Initialize_Scalar values if needed
1096 if Initialize_Scalars_Used then
1098 Set_String (" system__scalar_values__initialize('");
1099 Set_Char (Initialize_Scalars_Mode1);
1100 Set_String ("', '");
1101 Set_Char (Initialize_Scalars_Mode2);
1103 Write_Statement_Buffer;
1106 -- Generate assignment of default secondary stack size if set
1108 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1110 Set_String (" system__secondary_stack__");
1111 Set_String ("default_secondary_stack_size = ");
1112 Set_Int (Opt.Default_Sec_Stack_Size);
1114 Write_Statement_Buffer;
1117 -- Generate elaboration calls
1124 ------------------------
1125 -- Gen_Elab_Calls_Ada --
1126 ------------------------
1128 procedure Gen_Elab_Calls_Ada is
1130 for E in Elab_Order.First .. Elab_Order.Last loop
1132 Unum : constant Unit_Id := Elab_Order.Table (E);
1133 U : Unit_Record renames Units.Table (Unum);
1135 Unum_Spec : Unit_Id;
1136 -- This is the unit number of the spec that corresponds to
1137 -- this entry. It is the same as Unum except when the body
1138 -- and spec are different and we are currently processing
1139 -- the body, in which case it is the spec (Unum + 1).
1142 if U.Utype = Is_Body then
1143 Unum_Spec := Unum + 1;
1148 -- Nothing to do if predefined unit in no run time mode
1150 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1153 -- Case of no elaboration code
1155 elsif U.No_Elab then
1157 -- The only case in which we have to do something is if
1158 -- this is a body, with a separate spec, where the separate
1159 -- spec has an elaboration entity defined.
1161 -- In that case, this is where we set the elaboration entity
1162 -- to True, we do not need to test if this has already been
1163 -- done, since it is quicker to set the flag than to test it.
1165 if not U.SAL_Interface and then U.Utype = Is_Body
1166 and then Units.Table (Unum_Spec).Set_Elab_Entity
1169 Set_Unit_Number (Unum_Spec);
1170 Set_String (" := True;");
1171 Write_Statement_Buffer;
1174 -- Here if elaboration code is present. If binding a library
1175 -- or if there is a non-Ada main subprogram then we generate:
1177 -- if not uname_E then
1178 -- uname'elab_[spec|body];
1182 -- Otherwise, elaboration routines are called unconditionally:
1184 -- uname'elab_[spec|body];
1187 -- The uname_E assignment is skipped if this is a separate spec,
1188 -- since the assignment will be done when we process the body.
1190 elsif not U.SAL_Interface then
1191 if Force_Checking_Of_Elaboration_Flags or
1192 Interface_Library_Unit or
1193 (not Bind_Main_Program)
1195 Set_String (" if not E");
1196 Set_Unit_Number (Unum_Spec);
1197 Set_String (" then");
1198 Write_Statement_Buffer;
1203 Get_Decoded_Name_String_With_Brackets (U.Uname);
1205 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1206 if Name_Buffer (Name_Len) = 's' then
1207 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1210 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1214 Name_Len := Name_Len + 12;
1217 if Name_Buffer (Name_Len) = 's' then
1218 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1221 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1225 Name_Len := Name_Len + 8;
1228 Set_Casing (U.Icasing);
1231 Write_Statement_Buffer;
1233 if U.Utype /= Is_Spec then
1234 if Force_Checking_Of_Elaboration_Flags or
1235 Interface_Library_Unit or
1236 (not Bind_Main_Program)
1242 Set_Unit_Number (Unum_Spec);
1243 Set_String (" := True;");
1244 Write_Statement_Buffer;
1247 if Force_Checking_Of_Elaboration_Flags or
1248 Interface_Library_Unit or
1249 (not Bind_Main_Program)
1256 end Gen_Elab_Calls_Ada;
1258 ----------------------
1259 -- Gen_Elab_Calls_C --
1260 ----------------------
1262 procedure Gen_Elab_Calls_C is
1264 for E in Elab_Order.First .. Elab_Order.Last loop
1266 Unum : constant Unit_Id := Elab_Order.Table (E);
1267 U : Unit_Record renames Units.Table (Unum);
1269 Unum_Spec : Unit_Id;
1270 -- This is the unit number of the spec that corresponds to
1271 -- this entry. It is the same as Unum except when the body
1272 -- and spec are different and we are currently processing
1273 -- the body, in which case it is the spec (Unum + 1).
1276 if U.Utype = Is_Body then
1277 Unum_Spec := Unum + 1;
1282 -- Nothing to do if predefined unit in no run time mode
1284 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1287 -- Case of no elaboration code
1289 elsif U.No_Elab then
1291 -- The only case in which we have to do something is if
1292 -- this is a body, with a separate spec, where the separate
1293 -- spec has an elaboration entity defined.
1295 -- In that case, this is where we set the elaboration entity
1296 -- to True, we do not need to test if this has already been
1297 -- done, since it is quicker to set the flag than to test it.
1299 if not U.SAL_Interface and then U.Utype = Is_Body
1300 and then Units.Table (Unum_Spec).Set_Elab_Entity
1303 Get_Name_String (U.Uname);
1305 Set_String ("_E = 1;");
1306 Write_Statement_Buffer;
1309 -- Here if elaboration code is present. If binding a library
1310 -- or if there is a non-Ada main subprogram then we generate:
1312 -- if (uname_E == 0) {
1313 -- uname__elab[s|b] ();
1317 -- The uname_E assignment is skipped if this is a separate spec,
1318 -- since the assignment will be done when we process the body.
1320 elsif not U.SAL_Interface then
1321 Get_Name_String (U.Uname);
1323 if Force_Checking_Of_Elaboration_Flags or
1324 Interface_Library_Unit or
1325 (not Bind_Main_Program)
1327 Set_String (" if (");
1329 Set_String ("_E == 0) {");
1330 Write_Statement_Buffer;
1336 Set_String ("___elab");
1337 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1338 Set_String (" ();");
1339 Write_Statement_Buffer;
1341 if U.Utype /= Is_Spec then
1342 if Force_Checking_Of_Elaboration_Flags or
1343 Interface_Library_Unit or
1344 (not Bind_Main_Program)
1351 Set_String ("_E++;");
1352 Write_Statement_Buffer;
1355 if Force_Checking_Of_Elaboration_Flags or
1356 Interface_Library_Unit or
1357 (not Bind_Main_Program)
1365 end Gen_Elab_Calls_C;
1367 ----------------------
1368 -- Gen_Elab_Defs_C --
1369 ----------------------
1371 procedure Gen_Elab_Defs_C is
1373 for E in Elab_Order.First .. Elab_Order.Last loop
1375 -- Generate declaration of elaboration procedure if elaboration
1376 -- needed. Note that passive units are always excluded.
1378 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1379 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1380 Set_String ("extern void ");
1382 Set_String ("___elab");
1383 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1384 Set_String (" (void);");
1385 Write_Statement_Buffer;
1391 end Gen_Elab_Defs_C;
1393 ------------------------
1394 -- Gen_Elab_Order_Ada --
1395 ------------------------
1397 procedure Gen_Elab_Order_Ada is
1400 WBI (" -- BEGIN ELABORATION ORDER");
1402 for J in Elab_Order.First .. Elab_Order.Last loop
1403 Set_String (" -- ");
1404 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1406 Write_Statement_Buffer;
1409 WBI (" -- END ELABORATION ORDER");
1410 end Gen_Elab_Order_Ada;
1412 ----------------------
1413 -- Gen_Elab_Order_C --
1414 ----------------------
1416 procedure Gen_Elab_Order_C is
1419 WBI ("/* BEGIN ELABORATION ORDER");
1421 for J in Elab_Order.First .. Elab_Order.Last loop
1422 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1424 Write_Statement_Buffer;
1427 WBI (" END ELABORATION ORDER */");
1428 end Gen_Elab_Order_C;
1434 procedure Gen_Main_Ada is
1438 if Exit_Status_Supported_On_Target then
1439 Set_String (" function ");
1441 Set_String (" procedure ");
1444 Set_String (Get_Main_Name);
1446 if Command_Line_Args_On_Target then
1447 Write_Statement_Buffer;
1448 WBI (" (argc : Integer;");
1449 WBI (" argv : System.Address;");
1450 WBI (" envp : System.Address)");
1452 if Exit_Status_Supported_On_Target then
1453 WBI (" return Integer");
1459 if Exit_Status_Supported_On_Target then
1460 Set_String (" return Integer is");
1465 Write_Statement_Buffer;
1468 if Opt.Default_Exit_Status /= 0
1469 and then Bind_Main_Program
1470 and then not Configurable_Run_Time_Mode
1472 WBI (" procedure Set_Exit_Status (Status : Integer);");
1473 WBI (" pragma Import (C, Set_Exit_Status, " &
1474 """__gnat_set_exit_status"");");
1478 -- Initialize and Finalize
1480 if not Cumulative_Restrictions.Set (No_Finalization) then
1481 WBI (" procedure initialize (Addr : System.Address);");
1482 WBI (" pragma Import (C, initialize, ""__gnat_initialize"");");
1484 WBI (" procedure finalize;");
1485 WBI (" pragma Import (C, finalize, ""__gnat_finalize"");");
1488 -- If we want to analyze the stack, we have to import corresponding
1491 if Dynamic_Stack_Measurement then
1493 WBI (" procedure Output_Results;");
1494 WBI (" pragma Import (C, Output_Results, " &
1495 """__gnat_stack_usage_output_results"");");
1499 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1500 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1501 """__gnat_stack_usage_initialize"");");
1504 -- Deal with declarations for main program case
1506 if not No_Main_Subprogram then
1508 -- To call the main program, we declare it using a pragma Import
1509 -- Ada with the right link name.
1511 -- It might seem more obvious to "with" the main program, and call
1512 -- it in the normal Ada manner. We do not do this for three reasons:
1514 -- 1. It is more efficient not to recompile the main program
1515 -- 2. We are not entitled to assume the source is accessible
1516 -- 3. We don't know what options to use to compile it
1518 -- It is really reason 3 that is most critical (indeed we used
1519 -- to generate the "with", but several regression tests failed).
1523 if ALIs.Table (ALIs.First).Main_Program = Func then
1524 WBI (" Result : Integer;");
1526 WBI (" function Ada_Main_Program return Integer;");
1529 WBI (" procedure Ada_Main_Program;");
1532 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1533 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1534 Set_Main_Program_Name;
1535 Set_String (""");");
1537 Write_Statement_Buffer;
1540 if Bind_Main_Program
1541 and then not Suppress_Standard_Library_On_Target
1543 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1548 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1549 -- not referenced elsewhere in the generated program, but is needed
1550 -- by the debugger (that's why it is generated in the first place).
1551 -- The reference stops Ada_Main_Program_Name from being optimized
1552 -- away by smart linkers, such as the AiX linker.
1554 -- Because this variable is unused, we make this variable "aliased"
1555 -- with a pragma Volatile in order to tell the compiler to preserve
1556 -- this variable at any level of optimization.
1558 if Bind_Main_Program then
1560 (" Ensure_Reference : aliased System.Address := " &
1561 "Ada_Main_Program_Name'Address;");
1562 WBI (" pragma Volatile (Ensure_Reference);");
1568 -- Acquire command line arguments if present on target
1570 if Command_Line_Args_On_Target then
1571 WBI (" gnat_argc := argc;");
1572 WBI (" gnat_argv := argv;");
1573 WBI (" gnat_envp := envp;");
1576 -- If configurable run time and no command line args, then nothing
1577 -- needs to be done since the gnat_argc/argv/envp variables are
1578 -- suppressed in this case.
1580 elsif Configurable_Run_Time_On_Target then
1583 -- Otherwise set dummy values (to be filled in by some other unit?)
1586 WBI (" gnat_argc := 0;");
1587 WBI (" gnat_argv := System.Null_Address;");
1588 WBI (" gnat_envp := System.Null_Address;");
1591 if Opt.Default_Exit_Status /= 0
1592 and then Bind_Main_Program
1593 and then not Configurable_Run_Time_Mode
1595 Set_String (" Set_Exit_Status (");
1596 Set_Int (Opt.Default_Exit_Status);
1598 Write_Statement_Buffer;
1601 if Dynamic_Stack_Measurement then
1602 Set_String (" Initialize_Stack_Analysis (");
1603 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1605 Write_Statement_Buffer;
1608 if not Cumulative_Restrictions.Set (No_Finalization) then
1609 if not No_Main_Subprogram
1610 and then Bind_Main_Program
1611 and then not Suppress_Standard_Library_On_Target
1613 WBI (" Initialize (SEH'Address);");
1615 WBI (" Initialize (System.Null_Address);");
1619 WBI (" " & Ada_Init_Name.all & ";");
1621 if not No_Main_Subprogram then
1622 WBI (" Break_Start;");
1624 if ALIs.Table (ALIs.First).Main_Program = Proc then
1625 WBI (" Ada_Main_Program;");
1627 WBI (" Result := Ada_Main_Program;");
1631 -- Adafinal call is skipped if no finalization
1633 if not Cumulative_Restrictions.Set (No_Finalization) then
1635 -- If compiling for the JVM, we directly call Adafinal because
1636 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1638 if VM_Target = No_VM then
1639 WBI (" Do_Finalize;");
1641 WBI (" System.Standard_Library.Adafinal;");
1645 -- Prints the result of static stack analysis
1647 if Dynamic_Stack_Measurement then
1648 WBI (" Output_Results;");
1651 -- Finalize is only called if we have a run time
1653 if not Cumulative_Restrictions.Set (No_Finalization) then
1659 if Exit_Status_Supported_On_Target then
1660 if No_Main_Subprogram
1661 or else ALIs.Table (ALIs.First).Main_Program = Proc
1663 WBI (" return (gnat_exit_status);");
1665 WBI (" return (Result);");
1676 procedure Gen_Main_C is
1678 if Exit_Status_Supported_On_Target then
1679 WBI ("#include <stdlib.h>");
1680 Set_String ("int ");
1682 Set_String ("void ");
1685 Set_String (Get_Main_Name);
1687 -- Generate command line args in prototype if present on target
1689 if Command_Line_Args_On_Target then
1690 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
1692 -- Case of no command line arguments on target
1695 Write_Statement_Buffer (" (void)");
1700 -- Generate a reference to __gnat_ada_main_program_name. This symbol
1701 -- is not referenced elsewhere in the generated program, but is
1702 -- needed by the debugger (that's why it is generated in the first
1703 -- place). The reference stops Ada_Main_Program_Name from being
1704 -- optimized away by smart linkers, such as the AiX linker.
1706 -- Because this variable is unused, we declare this variable as
1707 -- volatile in order to tell the compiler to preserve it at any
1708 -- level of optimization.
1710 if Bind_Main_Program then
1711 WBI (" char * volatile ensure_reference " &
1712 "__attribute__ ((__unused__)) = " &
1713 "__gnat_ada_main_program_name;");
1716 if not Suppress_Standard_Library_On_Target
1717 and then not No_Main_Subprogram
1719 WBI (" int SEH [2];");
1724 -- If main program is a function, generate result variable
1726 if ALIs.Table (ALIs.First).Main_Program = Func then
1727 WBI (" int result;");
1730 -- Set command line argument values from parameters if command line
1731 -- arguments are present on target
1733 if Command_Line_Args_On_Target then
1734 WBI (" gnat_argc = argc;");
1735 WBI (" gnat_argv = argv;");
1736 WBI (" gnat_envp = envp;");
1739 -- If configurable run-time, then nothing to do, since in this case
1740 -- the gnat_argc/argv/envp variables are entirely suppressed.
1742 elsif Configurable_Run_Time_On_Target then
1745 -- if no command line arguments on target, set dummy values
1748 WBI (" gnat_argc = 0;");
1749 WBI (" gnat_argv = 0;");
1750 WBI (" gnat_envp = 0;");
1753 if Opt.Default_Exit_Status /= 0
1754 and then Bind_Main_Program
1755 and then not Configurable_Run_Time_Mode
1757 Set_String (" __gnat_set_exit_status (");
1758 Set_Int (Opt.Default_Exit_Status);
1760 Write_Statement_Buffer;
1763 -- Initializes dynamic stack measurement if needed
1765 if Dynamic_Stack_Measurement then
1766 Set_String (" __gnat_stack_usage_initialize (");
1767 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1769 Write_Statement_Buffer;
1772 -- The __gnat_initialize routine is used only if we have a run-time
1774 if not Suppress_Standard_Library_On_Target then
1775 if not No_Main_Subprogram and then Bind_Main_Program then
1776 WBI (" __gnat_initialize ((void *)SEH);");
1778 WBI (" __gnat_initialize ((void *)0);");
1782 WBI (" " & Ada_Init_Name.all & " ();");
1784 if not No_Main_Subprogram then
1785 WBI (" __gnat_break_start ();");
1788 -- Output main program name
1790 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1792 -- Main program is procedure case
1794 if ALIs.Table (ALIs.First).Main_Program = Proc then
1796 Set_Main_Program_Name;
1797 Set_String (" ();");
1798 Write_Statement_Buffer;
1800 -- Main program is function case
1802 else -- ALIs.Table (ALIs_First).Main_Program = Func
1803 Set_String (" result = ");
1804 Set_Main_Program_Name;
1805 Set_String (" ();");
1806 Write_Statement_Buffer;
1811 -- Call adafinal if finalization active
1813 if not Cumulative_Restrictions.Set (No_Finalization) then
1815 WBI (" system__standard_library__adafinal ();");
1818 -- Outputs the dynamic stack measurement if needed
1820 if Dynamic_Stack_Measurement then
1821 WBI (" __gnat_stack_usage_output_results ();");
1824 -- The finalize routine is used only if we have a run-time
1826 if not Suppress_Standard_Library_On_Target then
1827 WBI (" __gnat_finalize ();");
1830 -- Case of main program is a function, so the value it returns
1831 -- is the exit status in this case.
1833 if ALIs.Table (ALIs.First).Main_Program = Func then
1834 if Exit_Status_Supported_On_Target then
1836 -- VMS must use Posix exit routine in order to get the effect
1837 -- of a Unix compatible setting of the program exit status.
1838 -- For all other systems, we use the standard exit routine.
1840 if OpenVMS_On_Target then
1841 WBI (" decc$__posix_exit (result);");
1843 WBI (" exit (result);");
1847 -- Case of main program is a procedure, in which case the exit
1848 -- status is whatever was set by a Set_Exit call most recently
1851 if Exit_Status_Supported_On_Target then
1853 -- VMS must use Posix exit routine in order to get the effect
1854 -- of a Unix compatible setting of the program exit status.
1855 -- For all other systems, we use the standard exit routine.
1857 if OpenVMS_On_Target then
1858 WBI (" decc$__posix_exit (gnat_exit_status);");
1860 WBI (" exit (gnat_exit_status);");
1868 ------------------------------
1869 -- Gen_Object_Files_Options --
1870 ------------------------------
1872 procedure Gen_Object_Files_Options is
1874 -- This keeps track of the position in the sorted set of entries
1875 -- in the Linker_Options table of where the first entry from an
1876 -- internal file appears.
1878 Linker_Option_List_Started : Boolean := False;
1879 -- Set to True when "LINKER OPTION LIST" is displayed
1881 procedure Write_Linker_Option;
1882 -- Write binder info linker option
1884 -------------------------
1885 -- Write_Linker_Option --
1886 -------------------------
1888 procedure Write_Linker_Option is
1893 -- Loop through string, breaking at null's
1896 while Start < Name_Len loop
1898 -- Find null ending this section
1901 while Name_Buffer (Stop) /= ASCII.NUL
1902 and then Stop <= Name_Len loop
1906 -- Process section if non-null
1908 if Stop > Start then
1909 if Output_Linker_Option_List then
1910 if not Zero_Formatting then
1911 if not Linker_Option_List_Started then
1912 Linker_Option_List_Started := True;
1914 Write_Str (" LINKER OPTION LIST");
1922 Write_Str (Name_Buffer (Start .. Stop - 1));
1926 (" -- ", "", Name_Buffer (Start .. Stop - 1));
1931 end Write_Linker_Option;
1933 -- Start of processing for Gen_Object_Files_Options
1937 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
1939 for E in Elab_Order.First .. Elab_Order.Last loop
1941 -- If not spec that has an associated body, then generate a
1942 -- comment giving the name of the corresponding object file.
1944 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
1945 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1949 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1951 -- If the presence of an object file is necessary or if it
1952 -- exists, then use it.
1954 if not Hostparm.Exclude_Missing_Objects
1956 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1958 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1960 if Output_Object_List then
1961 Write_Str (Name_Buffer (1 .. Name_Len));
1965 -- Don't link with the shared library on VMS if an internal
1966 -- filename object is seen. Multiply defined symbols will
1969 if OpenVMS_On_Target
1970 and then Is_Internal_File_Name
1972 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
1974 -- Special case for g-trasym.obj, which is not included
1977 Get_Name_String (ALIs.Table
1978 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
1980 if Name_Buffer (1 .. 8) /= "g-trasym" then
1981 Opt.Shared_Libgnat := False;
1988 -- Add a "-Ldir" for each directory in the object path
1990 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1992 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1995 Add_Str_To_Name_Buffer ("-L");
1996 Add_Str_To_Name_Buffer (Dir.all);
1997 Write_Linker_Option;
2001 -- Sort linker options
2003 -- This sort accomplishes two important purposes:
2005 -- a) All application files are sorted to the front, and all
2006 -- GNAT internal files are sorted to the end. This results
2007 -- in a well defined dividing line between the two sets of
2008 -- files, for the purpose of inserting certain standard
2009 -- library references into the linker arguments list.
2011 -- b) Given two different units, we sort the linker options so
2012 -- that those from a unit earlier in the elaboration order
2013 -- comes later in the list. This is a heuristic designed
2014 -- to create a more friendly order of linker options when
2015 -- the operations appear in separate units. The idea is that
2016 -- if unit A must be elaborated before unit B, then it is
2017 -- more likely that B references libraries included by A,
2018 -- than vice versa, so we want the libraries included by
2019 -- A to come after the libraries included by B.
2021 -- These two criteria are implemented by function Lt_Linker_Option.
2022 -- Note that a special case of b) is that specs are elaborated before
2023 -- bodies, so linker options from specs come after linker options
2024 -- for bodies, and again, the assumption is that libraries used by
2025 -- the body are more likely to reference libraries used by the spec,
2029 (Linker_Options.Last,
2030 Move_Linker_Option'Access,
2031 Lt_Linker_Option'Access);
2033 -- Write user linker options, i.e. the set of linker options that
2034 -- come from all files other than GNAT internal files, Lgnat is
2035 -- left set to point to the first entry from a GNAT internal file,
2036 -- or past the end of the entriers if there are no internal files.
2038 Lgnat := Linker_Options.Last + 1;
2040 for J in 1 .. Linker_Options.Last loop
2041 if not Linker_Options.Table (J).Internal_File then
2042 Get_Name_String (Linker_Options.Table (J).Name);
2043 Write_Linker_Option;
2050 -- Now we insert standard linker options that must appear after the
2051 -- entries from user files, and before the entries from GNAT run-time
2052 -- files. The reason for this decision is that libraries referenced
2053 -- by internal routines may reference these standard library entries.
2055 -- Note that we do not insert anything when pragma No_Run_Time has been
2056 -- specified or when the standard libraries are not to be used,
2057 -- otherwise on some platforms, such as VMS, we may get duplicate
2058 -- symbols when linking.
2060 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2063 if Opt.Shared_Libgnat then
2064 Add_Str_To_Name_Buffer ("-shared");
2066 Add_Str_To_Name_Buffer ("-static");
2069 -- Write directly to avoid -K output (why???)
2071 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2073 if With_DECGNAT then
2076 if Opt.Shared_Libgnat then
2077 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2079 Add_Str_To_Name_Buffer ("-ldecgnat");
2082 Write_Linker_Option;
2088 if Opt.Shared_Libgnat then
2089 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2091 Add_Str_To_Name_Buffer ("-lgnarl");
2094 Write_Linker_Option;
2099 if Opt.Shared_Libgnat then
2100 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2102 Add_Str_To_Name_Buffer ("-lgnat");
2105 Write_Linker_Option;
2108 -- Write linker options from all internal files
2110 for J in Lgnat .. Linker_Options.Last loop
2111 Get_Name_String (Linker_Options.Table (J).Name);
2112 Write_Linker_Option;
2115 if Output_Linker_Option_List and then not Zero_Formatting then
2119 if Ada_Bind_File then
2120 WBI ("-- END Object file/option list ");
2122 WBI (" END Object file/option list */");
2124 end Gen_Object_Files_Options;
2126 ---------------------
2127 -- Gen_Output_File --
2128 ---------------------
2130 procedure Gen_Output_File (Filename : String) is
2132 -- Acquire settings for Interrupt_State pragmas
2134 Set_IS_Pragma_Table;
2136 -- Acquire settings for Priority_Specific_Dispatching pragma
2138 Set_PSD_Pragma_Table;
2140 -- Override Ada_Bind_File and Bind_Main_Program for VMs since
2141 -- JGNAT only supports Ada code, and the main program is already
2142 -- generated by the compiler.
2144 if VM_Target /= No_VM then
2145 Ada_Bind_File := True;
2147 if VM_Target = JVM_Target then
2148 Bind_Main_Program := False;
2152 -- Override time slice value if -T switch is set
2154 if Time_Slice_Set then
2155 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2158 -- Count number of elaboration calls
2160 for E in Elab_Order.First .. Elab_Order.Last loop
2161 if Units.Table (Elab_Order.Table (E)).No_Elab then
2164 Num_Elab_Calls := Num_Elab_Calls + 1;
2168 -- Generate output file in appropriate language
2170 Check_System_Restrictions_Used;
2172 if Ada_Bind_File then
2173 Gen_Output_File_Ada (Filename);
2175 Gen_Output_File_C (Filename);
2177 end Gen_Output_File;
2179 -------------------------
2180 -- Gen_Output_File_Ada --
2181 -------------------------
2183 procedure Gen_Output_File_Ada (Filename : String) is
2186 -- Name of generated bind file (spec)
2189 -- Name of generated bind file (body)
2191 Ada_Main : constant String := Get_Ada_Main_Name;
2192 -- Name to be used for generated Ada main program. See the body of
2193 -- function Get_Ada_Main_Name for details on the form of the name.
2196 -- Create spec first
2198 Create_Binder_Output (Filename, 's', Bfiles);
2200 -- We always compile the binder file in Ada 95 mode so that we properly
2201 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2202 -- of the Ada 2005 constructs are needed by the binder file.
2204 WBI ("pragma Ada_95;");
2206 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2207 -- then we need to make sure that the binder program is compiled with
2208 -- the same restriction, so that no exception tables are generated.
2210 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2211 WBI ("pragma Restrictions (No_Exception_Handlers);");
2214 -- Same processing for Restrictions (No_Exception_Propagation)
2216 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2217 WBI ("pragma Restrictions (No_Exception_Propagation);");
2220 -- Same processing for pragma No_Run_Time
2222 if No_Run_Time_Mode then
2223 WBI ("pragma No_Run_Time;");
2226 -- Generate with of System so we can reference System.Address
2228 WBI ("with System;");
2230 -- Generate with of System.Initialize_Scalars if active
2232 if Initialize_Scalars_Used then
2233 WBI ("with System.Scalar_Values;");
2236 -- Generate with of System.Secondary_Stack if active
2238 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2239 WBI ("with System.Secondary_Stack;");
2242 Resolve_Binder_Options;
2244 if VM_Target /= No_VM then
2245 if not Suppress_Standard_Library_On_Target then
2247 -- Usually, adafinal is called using a pragma Import C. Since
2248 -- Import C doesn't have the same semantics for JGNAT, we use
2251 WBI ("with System.Standard_Library;");
2255 WBI ("package " & Ada_Main & " is");
2256 WBI (" pragma Warnings (Off);");
2258 -- Main program case
2260 if Bind_Main_Program then
2261 if VM_Target = No_VM then
2263 -- Generate argc/argv stuff unless suppressed
2265 if Command_Line_Args_On_Target
2266 or not Configurable_Run_Time_On_Target
2269 WBI (" gnat_argc : Integer;");
2270 WBI (" gnat_argv : System.Address;");
2271 WBI (" gnat_envp : System.Address;");
2273 -- If the standard library is not suppressed, these variables
2274 -- are in the runtime data area for easy access from the
2277 if not Suppress_Standard_Library_On_Target then
2279 WBI (" pragma Import (C, gnat_argc);");
2280 WBI (" pragma Import (C, gnat_argv);");
2281 WBI (" pragma Import (C, gnat_envp);");
2285 -- Define exit status. Again in normal mode, this is in the
2286 -- run-time library, and is initialized there, but in the
2287 -- configurable runtime case, the variable is declared and
2288 -- initialized in this file.
2292 if Configurable_Run_Time_Mode then
2293 if Exit_Status_Supported_On_Target then
2294 WBI (" gnat_exit_status : Integer := 0;");
2298 WBI (" gnat_exit_status : Integer;");
2299 WBI (" pragma Import (C, gnat_exit_status);");
2303 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2304 -- the main program. Otherwise, it can lead under some circumstances
2305 -- to a symbol duplication during the link (for instance when a C
2306 -- program uses two Ada libraries). Also zero terminate the string
2307 -- so that its end can be found reliably at run time.
2310 WBI (" GNAT_Version : constant String :=");
2311 WBI (" ""GNAT Version: " &
2312 Gnat_Version_String &
2314 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2317 Set_String (" Ada_Main_Program_Name : constant String := """);
2318 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2320 if VM_Target = No_VM then
2321 Set_Main_Program_Name;
2322 Set_String (""" & ASCII.NUL;");
2324 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2327 Write_Statement_Buffer;
2330 (" pragma Export (C, Ada_Main_Program_Name, " &
2331 """__gnat_ada_main_program_name"");");
2334 if not Cumulative_Restrictions.Set (No_Finalization) then
2336 WBI (" procedure " & Ada_Final_Name.all & ";");
2337 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2338 Ada_Final_Name.all & """);");
2342 WBI (" procedure " & Ada_Init_Name.all & ";");
2343 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2344 Ada_Init_Name.all & """);");
2346 -- If -a has been specified use pragma Linker_Constructor for the init
2347 -- procedure. No need to use a similar pragma for the final procedure as
2348 -- global finalization will occur when the executable finishes execution
2349 -- and for plugins (shared stand-alone libraries that can be
2350 -- "unloaded"), finalization should not occur automatically, otherwise
2351 -- the main executable may not continue to work properly.
2353 if Use_Pragma_Linker_Constructor then
2354 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2357 if Bind_Main_Program and then VM_Target = No_VM then
2359 -- If we have the standard library, then Break_Start is defined
2360 -- there, but when the standard library is suppressed, Break_Start
2364 WBI (" procedure Break_Start;");
2366 if Suppress_Standard_Library_On_Target then
2367 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2369 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2374 if Exit_Status_Supported_On_Target then
2375 Set_String (" function ");
2377 Set_String (" procedure ");
2380 Set_String (Get_Main_Name);
2382 -- Generate argument list if present
2384 if Command_Line_Args_On_Target then
2385 Write_Statement_Buffer;
2386 WBI (" (argc : Integer;");
2387 WBI (" argv : System.Address;");
2389 (" envp : System.Address)");
2391 if Exit_Status_Supported_On_Target then
2392 Write_Statement_Buffer;
2393 WBI (" return Integer;");
2395 Write_Statement_Buffer (";");
2399 if Exit_Status_Supported_On_Target then
2400 Write_Statement_Buffer (" return Integer;");
2402 Write_Statement_Buffer (";");
2406 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2407 Get_Main_Name & """);");
2416 WBI ("end " & Ada_Main & ";");
2417 Close_Binder_Output;
2419 -- Prepare to write body
2421 Create_Binder_Output (Filename, 'b', Bfileb);
2423 -- We always compile the binder file in Ada 95 mode so that we properly
2424 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2425 -- of the Ada 2005 constructs are needed by the binder file.
2427 WBI ("pragma Ada_95;");
2429 -- Output Source_File_Name pragmas which look like
2431 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2432 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2434 -- where sss/bbb are the spec/body file names respectively
2436 Get_Name_String (Bfiles);
2437 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2439 WBI ("pragma Source_File_Name (" &
2441 ", Spec_File_Name => """ &
2442 Name_Buffer (1 .. Name_Len + 3));
2444 Get_Name_String (Bfileb);
2445 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2447 WBI ("pragma Source_File_Name (" &
2449 ", Body_File_Name => """ &
2450 Name_Buffer (1 .. Name_Len + 3));
2452 -- Generate with of System.Restrictions to initialize
2453 -- Run_Time_Restrictions.
2455 if System_Restrictions_Used
2456 and not Suppress_Standard_Library_On_Target
2459 WBI ("with System.Restrictions;");
2463 WBI ("package body " & Ada_Main & " is");
2464 WBI (" pragma Warnings (Off);");
2466 -- Import the finalization procedure only if finalization active
2468 if not Cumulative_Restrictions.Set (No_Finalization) then
2470 -- In the Java case, pragma Import C cannot be used, so the
2471 -- standard Ada constructs will be used instead.
2473 if VM_Target = No_VM then
2475 WBI (" procedure Do_Finalize;");
2477 (" pragma Import (C, Do_Finalize, " &
2478 """system__standard_library__adafinal"");");
2483 if not Suppress_Standard_Library_On_Target then
2485 -- Generate Priority_Specific_Dispatching pragma string
2488 (" Local_Priority_Specific_Dispatching : constant String := """);
2490 for J in 0 .. PSD_Pragma_Settings.Last loop
2491 Set_Char (PSD_Pragma_Settings.Table (J));
2495 Write_Statement_Buffer;
2497 -- Generate Interrupt_State pragma string
2499 Set_String (" Local_Interrupt_States : constant String := """);
2501 for J in 0 .. IS_Pragma_Settings.Last loop
2502 Set_Char (IS_Pragma_Settings.Table (J));
2506 Write_Statement_Buffer;
2512 -- Generate the adafinal routine unless there is no finalization to do
2514 if not Cumulative_Restrictions.Set (No_Finalization) then
2518 if Bind_Main_Program and then VM_Target = No_VM then
2520 -- When suppressing the standard library then generate dummy body
2523 if Suppress_Standard_Library_On_Target then
2525 WBI (" procedure Break_Start is");
2534 -- Output object file list and the Ada body is complete
2536 Gen_Object_Files_Options;
2539 WBI ("end " & Ada_Main & ";");
2541 Close_Binder_Output;
2542 end Gen_Output_File_Ada;
2544 -----------------------
2545 -- Gen_Output_File_C --
2546 -----------------------
2548 procedure Gen_Output_File_C (Filename : String) is
2550 pragma Warnings (Off, Bfile);
2551 -- Name of generated bind file (not referenced)
2554 Create_Binder_Output (Filename, 'c', Bfile);
2556 Resolve_Binder_Options;
2558 WBI ("extern void " & Ada_Final_Name.all & " (void);");
2560 -- If -a has been specified use __attribute__((constructor)) for the
2561 -- init procedure. No need to use a similar featute for the final
2562 -- procedure as global finalization will occur when the executable
2563 -- finishes execution and for plugins (shared stand-alone libraries that
2564 -- can be "unloaded"), finalization should not occur automatically,
2565 -- otherwise the main executable may not continue to work properly.
2567 if Use_Pragma_Linker_Constructor then
2568 WBI ("extern void " & Ada_Init_Name.all &
2569 " (void) __attribute__((constructor));");
2571 WBI ("extern void " & Ada_Init_Name.all & " (void);");
2574 WBI ("extern void system__standard_library__adafinal (void);");
2576 if not No_Main_Subprogram then
2577 Set_String ("extern ");
2579 if Exit_Status_Supported_On_Target then
2582 Set_String ("void");
2585 Set_String (" main ");
2587 if Command_Line_Args_On_Target then
2588 Write_Statement_Buffer ("(int, char **, char **);");
2590 Write_Statement_Buffer ("(void);");
2593 if OpenVMS_On_Target then
2594 WBI ("extern void decc$__posix_exit (int);");
2596 WBI ("extern void exit (int);");
2599 WBI ("extern void __gnat_break_start (void);");
2600 Set_String ("extern ");
2602 if ALIs.Table (ALIs.First).Main_Program = Proc then
2603 Set_String ("void ");
2605 Set_String ("int ");
2608 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2609 Set_Main_Program_Name;
2610 Set_String (" (void);");
2611 Write_Statement_Buffer;
2614 if not Suppress_Standard_Library_On_Target then
2615 WBI ("extern void __gnat_initialize (void *);");
2616 WBI ("extern void __gnat_finalize (void);");
2617 WBI ("extern void __gnat_install_handler (void);");
2620 if Dynamic_Stack_Measurement then
2622 WBI ("extern void __gnat_stack_usage_output_results (void);");
2623 WBI ("extern void __gnat_stack_usage_initialize (int size);");
2626 -- Initialize stack limit for the environment task if the stack
2627 -- check method is stack limit and stack check is enabled.
2629 if Stack_Check_Limits_On_Target
2630 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
2633 WBI ("extern void __gnat_initialize_stack_limit (void);");
2640 -- Imported variables used only when we have a runtime
2642 if not Suppress_Standard_Library_On_Target then
2644 -- Track elaboration/finalization phase
2646 WBI ("extern int __gnat_handler_installed;");
2649 -- Track feature enable/disable on VMS
2651 if OpenVMS_On_Target then
2652 WBI ("extern int __gnat_features_set;");
2657 -- Write argv/argc exit status stuff if main program case
2659 if Bind_Main_Program then
2661 -- First deal with argc/argv/envp. In the normal case they
2662 -- are in the run-time library.
2664 if not Configurable_Run_Time_On_Target then
2665 WBI ("extern int gnat_argc;");
2666 WBI ("extern char **gnat_argv;");
2667 WBI ("extern char **gnat_envp;");
2669 -- If configurable run time and no command line args, then the
2670 -- generation of these variables is entirely suppressed.
2672 elsif not Command_Line_Args_On_Target then
2675 -- Otherwise, in the configurable run-time case they are right in
2679 WBI ("int gnat_argc;");
2680 WBI ("char **gnat_argv;");
2681 WBI ("char **gnat_envp;");
2684 -- Similarly deal with exit status
2686 if not Configurable_Run_Time_On_Target then
2687 WBI ("extern int gnat_exit_status;");
2689 -- If configurable run time and no exit status on target, then
2690 -- the generation of this variables is entirely suppressed.
2692 elsif not Exit_Status_Supported_On_Target then
2695 -- Otherwise, in the configurable run-time case this variable is
2696 -- right in the binder file, and initialized to zero there.
2699 WBI ("int gnat_exit_status = 0;");
2705 -- When suppressing the standard library, the __gnat_break_start
2706 -- routine (for the debugger to get initial control) is defined in
2709 if Suppress_Standard_Library_On_Target then
2711 WBI ("void __gnat_break_start (void) {}");
2714 -- Generate the __gnat_version and __gnat_ada_main_program_name info
2715 -- only for the main program. Otherwise, it can lead under some
2716 -- circumstances to a symbol duplication during the link (for instance
2717 -- when a C program uses 2 Ada libraries)
2719 if Bind_Main_Program then
2721 WBI ("char __gnat_version[] = ""GNAT Version: " &
2722 Gnat_Version_String & """;");
2724 Set_String ("char __gnat_ada_main_program_name[] = """);
2725 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2726 Set_Main_Program_Name;
2728 Write_Statement_Buffer;
2731 -- Generate the adafinal routine. In no runtime mode, this is
2732 -- not needed, since there is no finalization to do.
2734 if not Cumulative_Restrictions.Set (No_Finalization) then
2740 -- Main is only present for Ada main case
2742 if Bind_Main_Program then
2746 -- Generate versions, elaboration order, list of object files
2750 Gen_Object_Files_Options;
2752 -- C binder output is complete
2754 Close_Binder_Output;
2755 end Gen_Output_File_C;
2757 --------------------------
2758 -- Gen_Restrictions_Ada --
2759 --------------------------
2761 procedure Gen_Restrictions_Ada is
2765 if Suppress_Standard_Library_On_Target
2766 or not System_Restrictions_Used
2771 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2777 for J in Cumulative_Restrictions.Set'First ..
2778 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2780 Set_Boolean (Cumulative_Restrictions.Set (J));
2785 Write_Statement_Buffer;
2792 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last));
2794 Write_Statement_Buffer;
2795 Set_String (" Value => (");
2797 for J in Cumulative_Restrictions.Value'First ..
2798 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2800 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2804 Set_Int (Int (Cumulative_Restrictions.Value
2805 (Cumulative_Restrictions.Value'Last)));
2807 Write_Statement_Buffer;
2808 WBI (" Violated =>");
2812 for J in Cumulative_Restrictions.Violated'First ..
2813 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2815 Set_Boolean (Cumulative_Restrictions.Violated (J));
2820 Write_Statement_Buffer;
2826 Set_Boolean (Cumulative_Restrictions.Violated
2827 (Cumulative_Restrictions.Violated'Last));
2829 Write_Statement_Buffer;
2830 Set_String (" Count => (");
2832 for J in Cumulative_Restrictions.Count'First ..
2833 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2835 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2839 Set_Int (Int (Cumulative_Restrictions.Count
2840 (Cumulative_Restrictions.Count'Last)));
2842 Write_Statement_Buffer;
2843 Set_String (" Unknown => (");
2845 for J in Cumulative_Restrictions.Unknown'First ..
2846 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2848 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2853 (Cumulative_Restrictions.Unknown
2854 (Cumulative_Restrictions.Unknown'Last));
2856 Write_Statement_Buffer;
2857 end Gen_Restrictions_Ada;
2859 ------------------------
2860 -- Gen_Restrictions_C --
2861 ------------------------
2863 procedure Gen_Restrictions_C is
2865 if Suppress_Standard_Library_On_Target
2866 or not System_Restrictions_Used
2871 WBI (" typedef struct {");
2872 Set_String (" char set [");
2873 Set_Int (Cumulative_Restrictions.Set'Length);
2875 Write_Statement_Buffer;
2877 Set_String (" int value [");
2878 Set_Int (Cumulative_Restrictions.Value'Length);
2880 Write_Statement_Buffer;
2882 Set_String (" char violated [");
2883 Set_Int (Cumulative_Restrictions.Violated'Length);
2885 Write_Statement_Buffer;
2887 Set_String (" int count [");
2888 Set_Int (Cumulative_Restrictions.Count'Length);
2890 Write_Statement_Buffer;
2892 Set_String (" char unknown [");
2893 Set_Int (Cumulative_Restrictions.Unknown'Length);
2895 Write_Statement_Buffer;
2896 WBI (" } restrictions;");
2897 WBI (" extern restrictions " &
2898 "system__restrictions__run_time_restrictions;");
2899 WBI (" restrictions r = {");
2902 for J in Cumulative_Restrictions.Set'First ..
2903 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2905 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
2909 Set_Int (Boolean'Pos
2910 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last)));
2912 Write_Statement_Buffer;
2915 for J in Cumulative_Restrictions.Value'First ..
2916 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2918 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2922 Set_Int (Int (Cumulative_Restrictions.Value
2923 (Cumulative_Restrictions.Value'Last)));
2925 Write_Statement_Buffer;
2928 for J in Cumulative_Restrictions.Violated'First ..
2929 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2931 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
2935 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated
2936 (Cumulative_Restrictions.Violated'Last)));
2938 Write_Statement_Buffer;
2941 for J in Cumulative_Restrictions.Count'First ..
2942 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2944 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2948 Set_Int (Int (Cumulative_Restrictions.Count
2949 (Cumulative_Restrictions.Count'Last)));
2951 Write_Statement_Buffer;
2954 for J in Cumulative_Restrictions.Unknown'First ..
2955 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2957 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
2961 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown
2962 (Cumulative_Restrictions.Unknown'Last)));
2964 Write_Statement_Buffer;
2965 WBI (" system__restrictions__run_time_restrictions = r;");
2966 end Gen_Restrictions_C;
2968 ----------------------
2969 -- Gen_Versions_Ada --
2970 ----------------------
2972 -- This routine generates two sets of lines. The first set has the form:
2974 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2976 -- The second set has the form
2978 -- pragma Export (C, unnnnn, unam);
2980 -- for each unit, where unam is the unit name suffixed by either B or
2981 -- S for body or spec, with dots replaced by double underscores, and
2982 -- hhhhhhhh is the version number, and nnnnn is a 5-digits serial number.
2984 procedure Gen_Versions_Ada is
2985 Ubuf : String (1 .. 6) := "u00000";
2987 procedure Increment_Ubuf;
2988 -- Little procedure to increment the serial number
2990 procedure Increment_Ubuf is
2992 for J in reverse Ubuf'Range loop
2993 Ubuf (J) := Character'Succ (Ubuf (J));
2994 exit when Ubuf (J) <= '9';
2999 -- Start of processing for Gen_Versions_Ada
3002 if Bind_For_Library then
3004 -- When building libraries, the version number of each unit can
3005 -- not be computed, since the binder does not know the full list
3006 -- of units. Therefore, the 'Version and 'Body_Version
3007 -- attributes cannot supported in this case.
3014 WBI (" type Version_32 is mod 2 ** 32;");
3015 for U in Units.First .. Units.Last loop
3017 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
3018 Units.Table (U).Version & "#;");
3024 for U in Units.First .. Units.Last loop
3026 Set_String (" pragma Export (C, ");
3028 Set_String (", """);
3030 Get_Name_String (Units.Table (U).Uname);
3032 for K in 1 .. Name_Len loop
3033 if Name_Buffer (K) = '.' then
3037 elsif Name_Buffer (K) = '%' then
3041 Set_Char (Name_Buffer (K));
3045 if Name_Buffer (Name_Len) = 's' then
3051 Set_String (""");");
3052 Write_Statement_Buffer;
3055 end Gen_Versions_Ada;
3057 --------------------
3058 -- Gen_Versions_C --
3059 --------------------
3061 -- This routine generates a line of the form:
3063 -- unsigned unam = 0xhhhhhhhh;
3065 -- for each unit, where unam is the unit name suffixed by either B or
3066 -- S for body or spec, with dots replaced by double underscores.
3068 procedure Gen_Versions_C is
3070 if Bind_For_Library then
3072 -- When building libraries, the version number of each unit can
3073 -- not be computed, since the binder does not know the full list
3074 -- of units. Therefore, the 'Version and 'Body_Version
3075 -- attributes cannot supported.
3080 for U in Units.First .. Units.Last loop
3081 Set_String ("unsigned ");
3083 Get_Name_String (Units.Table (U).Uname);
3085 for K in 1 .. Name_Len loop
3086 if Name_Buffer (K) = '.' then
3089 elsif Name_Buffer (K) = '%' then
3093 Set_Char (Name_Buffer (K));
3097 if Name_Buffer (Name_Len) = 's' then
3103 Set_String (" = 0x");
3104 Set_String (Units.Table (U).Version);
3106 Write_Statement_Buffer;
3111 ------------------------
3112 -- Get_Main_Unit_Name --
3113 ------------------------
3115 function Get_Main_Unit_Name (S : String) return String is
3116 Result : String := S;
3119 for J in S'Range loop
3120 if Result (J) = '.' then
3126 end Get_Main_Unit_Name;
3128 -----------------------
3129 -- Get_Ada_Main_Name --
3130 -----------------------
3132 function Get_Ada_Main_Name return String is
3133 Suffix : constant String := "_00";
3134 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3135 Opt.Ada_Main_Name.all & Suffix;
3139 -- The main program generated by JGNAT expects a package called
3140 -- ada_<main procedure>.
3142 if VM_Target /= No_VM then
3143 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3144 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3147 -- This loop tries the following possibilities in order
3153 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3154 -- it is set to 'ada_main'.
3156 for J in 0 .. 99 loop
3158 Nlen := Name'Length - Suffix'Length;
3160 Nlen := Name'Length;
3161 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3162 Name (Name'Last - 1) :=
3163 Character'Val (J / 10 + Character'Pos ('0'));
3166 for K in ALIs.First .. ALIs.Last loop
3167 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3169 -- Get unit name, removing %b or %e at end
3171 Get_Name_String (Units.Table (L).Uname);
3172 Name_Len := Name_Len - 2;
3174 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3180 return Name (1 .. Nlen);
3186 -- If we fall through, just use a peculiar unlikely name
3188 return ("Qwertyuiop");
3189 end Get_Ada_Main_Name;
3195 function Get_Main_Name return String is
3197 -- Explicit name given with -M switch
3199 if Bind_Alternate_Main_Name then
3200 return Alternate_Main_Name.all;
3202 -- Case of main program name to be used directly
3204 elsif Use_Ada_Main_Program_Name_On_Target then
3206 -- Get main program name
3208 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3210 -- If this is a child name, return only the name of the child,
3211 -- since we can't have dots in a nested program name. Note that
3212 -- we do not include the %b at the end of the unit name.
3214 for J in reverse 1 .. Name_Len - 2 loop
3215 if J = 1 or else Name_Buffer (J - 1) = '.' then
3216 return Name_Buffer (J .. Name_Len - 2);
3220 raise Program_Error; -- impossible exit
3222 -- Case where "main" is to be used as default
3229 ---------------------
3230 -- Get_WC_Encoding --
3231 ---------------------
3233 function Get_WC_Encoding return Character is
3235 -- If encoding method specified by -W switch, then return it
3237 if Wide_Character_Encoding_Method_Specified then
3238 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3240 -- If no main program, and not specified, set brackets, we really have
3241 -- no better choice. If some other encoding is required when there is
3242 -- no main, it must be set explicitly using -Wx.
3244 -- Note: if the ALI file always passed the wide character encoding
3245 -- of every file, then we could use the encoding of the initial
3246 -- specified file, but this information is passed only for potential
3247 -- main programs. We could fix this sometime, but it is a very minor
3248 -- point (wide character default encoding for [Wide_[Wide_]Text_IO
3249 -- when there is no main program).
3251 elsif No_Main_Subprogram then
3254 -- Otherwise if there is a main program, take encoding from it
3257 return ALIs.Table (ALIs.First).WC_Encoding;
3259 end Get_WC_Encoding;
3261 ----------------------
3262 -- Lt_Linker_Option --
3263 ----------------------
3265 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3267 -- Sort internal files last
3269 if Linker_Options.Table (Op1).Internal_File
3271 Linker_Options.Table (Op2).Internal_File
3273 -- Note: following test uses False < True
3275 return Linker_Options.Table (Op1).Internal_File
3277 Linker_Options.Table (Op2).Internal_File;
3279 -- If both internal or both non-internal, sort according to the
3280 -- elaboration position. A unit that is elaborated later should
3281 -- come earlier in the linker options list.
3284 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3286 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3289 end Lt_Linker_Option;
3291 ------------------------
3292 -- Move_Linker_Option --
3293 ------------------------
3295 procedure Move_Linker_Option (From : Natural; To : Natural) is
3297 Linker_Options.Table (To) := Linker_Options.Table (From);
3298 end Move_Linker_Option;
3300 ----------------------------
3301 -- Resolve_Binder_Options --
3302 ----------------------------
3304 procedure Resolve_Binder_Options is
3306 for E in Elab_Order.First .. Elab_Order.Last loop
3307 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3309 -- This is not a perfect approach, but is the current protocol
3310 -- between the run-time and the binder to indicate that tasking
3311 -- is used: system.os_interface should always be used by any
3312 -- tasking application.
3314 if Name_Buffer (1 .. 19) = "system.os_interface" then
3318 -- Ditto for declib and the "dec" package
3320 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3321 With_DECGNAT := True;
3324 end Resolve_Binder_Options;
3330 procedure Set_Boolean (B : Boolean) is
3331 True_Str : constant String := "True";
3332 False_Str : constant String := "False";
3335 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3336 Last := Last + True_Str'Length;
3338 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3339 Last := Last + False_Str'Length;
3347 procedure Set_Char (C : Character) is
3350 Statement_Buffer (Last) := C;
3357 procedure Set_Int (N : Int) is
3369 Statement_Buffer (Last) :=
3370 Character'Val (N mod 10 + Character'Pos ('0'));
3374 -------------------------
3375 -- Set_IS_Pragma_Table --
3376 -------------------------
3378 procedure Set_IS_Pragma_Table is
3380 for F in ALIs.First .. ALIs.Last loop
3381 for K in ALIs.Table (F).First_Interrupt_State ..
3382 ALIs.Table (F).Last_Interrupt_State
3385 Inum : constant Int :=
3386 Interrupt_States.Table (K).Interrupt_Id;
3387 Stat : constant Character :=
3388 Interrupt_States.Table (K).Interrupt_State;
3391 while IS_Pragma_Settings.Last < Inum loop
3392 IS_Pragma_Settings.Append ('n');
3395 IS_Pragma_Settings.Table (Inum) := Stat;
3399 end Set_IS_Pragma_Table;
3401 ---------------------------
3402 -- Set_Main_Program_Name --
3403 ---------------------------
3405 procedure Set_Main_Program_Name is
3407 -- Note that name has %b on the end which we ignore
3409 -- First we output the initial _ada_ since we know that the main
3410 -- program is a library level subprogram.
3412 Set_String ("_ada_");
3414 -- Copy name, changing dots to double underscores
3416 for J in 1 .. Name_Len - 2 loop
3417 if Name_Buffer (J) = '.' then
3420 Set_Char (Name_Buffer (J));
3423 end Set_Main_Program_Name;
3425 ---------------------
3426 -- Set_Name_Buffer --
3427 ---------------------
3429 procedure Set_Name_Buffer is
3431 for J in 1 .. Name_Len loop
3432 Set_Char (Name_Buffer (J));
3434 end Set_Name_Buffer;
3436 -------------------------
3437 -- Set_PSD_Pragma_Table --
3438 -------------------------
3440 procedure Set_PSD_Pragma_Table is
3442 for F in ALIs.First .. ALIs.Last loop
3443 for K in ALIs.Table (F).First_Specific_Dispatching ..
3444 ALIs.Table (F).Last_Specific_Dispatching
3447 DTK : Specific_Dispatching_Record
3448 renames Specific_Dispatching.Table (K);
3451 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3452 PSD_Pragma_Settings.Append ('F');
3455 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3456 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3461 end Set_PSD_Pragma_Table;
3467 procedure Set_String (S : String) is
3469 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3470 Last := Last + S'Length;
3477 procedure Set_Unit_Name is
3479 for J in 1 .. Name_Len - 2 loop
3480 if Name_Buffer (J) /= '.' then
3481 Set_Char (Name_Buffer (J));
3488 ---------------------
3489 -- Set_Unit_Number --
3490 ---------------------
3492 procedure Set_Unit_Number (U : Unit_Id) is
3493 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3494 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3497 if Num_Units >= 10 and then Unum < 10 then
3501 if Num_Units >= 100 and then Unum < 100 then
3506 end Set_Unit_Number;
3508 ----------------------
3509 -- Write_Info_Ada_C --
3510 ----------------------
3512 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3514 if Ada_Bind_File then
3516 S : String (1 .. Ada'Length + Common'Length);
3518 S (1 .. Ada'Length) := Ada;
3519 S (Ada'Length + 1 .. S'Length) := Common;
3525 S : String (1 .. C'Length + Common'Length);
3527 S (1 .. C'Length) := C;
3528 S (C'Length + 1 .. S'Length) := Common;
3532 end Write_Info_Ada_C;
3534 ----------------------------
3535 -- Write_Statement_Buffer --
3536 ----------------------------
3538 procedure Write_Statement_Buffer is
3540 WBI (Statement_Buffer (1 .. Last));
3542 end Write_Statement_Buffer;
3544 procedure Write_Statement_Buffer (S : String) is
3547 Write_Statement_Buffer;
3548 end Write_Statement_Buffer;