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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Binde; use Binde;
29 with Casing; use Casing;
30 with Fname; use Fname;
31 with Gnatvsn; use Gnatvsn;
33 with Namet; use Namet;
35 with Osint; use Osint;
36 with Osint.B; use Osint.B;
37 with Output; use Output;
38 with Rident; use Rident;
39 with Table; use Table;
40 with Targparm; use Targparm;
41 with Types; use Types;
43 with System.OS_Lib; use System.OS_Lib;
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;
129 -- Main_Priority is the priority value set by pragma Priority in the
130 -- main program. If no such pragma is present, the value is -1.
132 -- Time_Slice_Value is the time slice value set by pragma Time_Slice
133 -- in the main program, or by the use of a -Tnnn parameter for the
134 -- binder (if both are present, the binder value overrides). The
135 -- value is in milliseconds. A value of zero indicates that time
136 -- slicing should be suppressed. If no pragma is present, and no
137 -- -T switch was used, the value is -1.
139 -- WC_Encoding shows the wide character encoding method used for
140 -- the main program. This is one of the encoding letters defined
141 -- in System.WCh_Con.WC_Encoding_Letters.
143 -- Locking_Policy is a space if no locking policy was specified
144 -- for the partition. If a locking policy was specified, the value
145 -- is the upper case first character of the locking policy name,
146 -- for example, 'C' for Ceiling_Locking.
148 -- Queuing_Policy is a space if no queuing policy was specified
149 -- for the partition. If a queuing policy was specified, the value
150 -- is the upper case first character of the queuing policy name
151 -- for example, 'F' for FIFO_Queuing.
153 -- Task_Dispatching_Policy is a space if no task dispatching policy
154 -- was specified for the partition. If a task dispatching policy
155 -- was specified, the value is the upper case first character of
156 -- the policy name, e.g. 'F' for FIFO_Within_Priorities.
158 -- Priority_Specific_Dispatching is the address of a string used to
159 -- store the task dispatching policy specified for the different priorities
160 -- in 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
185 -- run time, 's' for system, see description of Interrupt_State pragma
186 -- for further details.
188 -- Num_Interrupt_States is the length of the Interrupt_States string.
189 -- It will be set to zero if no Interrupt_State pragmas are present.
191 -- Unreserve_All_Interrupts is set to one if at least one unit in the
192 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
194 -- Exception_Tracebacks is set to one if the -E parameter was present
195 -- in the bind and to zero otherwise. Note that on some targets exception
196 -- tracebacks are provided by default, so a value of zero for this
197 -- parameter does not necessarily mean no trace backs are available.
199 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
200 -- this partition, and to zero if longjmp/setjmp exceptions are used.
203 -- Detect_Blocking indicates whether pragma Detect_Blocking is
204 -- active or not. A value of zero indicates that the pragma is not
205 -- present, while a value of 1 signals its presence in the
208 -- Default_Stack_Size is the default stack size used when creating an
209 -- Ada task with no explicit Storize_Size clause.
211 -----------------------
212 -- Local Subprograms --
213 -----------------------
215 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
216 -- Convenient shorthand used throughout
218 procedure Check_System_Restrictions_Used;
219 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
220 -- System.Restrictions is present in the partition, otherwise False).
222 procedure Gen_Adainit_Ada;
223 -- Generates the Adainit procedure (Ada code case)
225 procedure Gen_Adainit_C;
226 -- Generates the Adainit procedure (C code case)
228 procedure Gen_Adafinal_Ada;
229 -- Generate the Adafinal procedure (Ada code case)
231 procedure Gen_Adafinal_C;
232 -- Generate the Adafinal procedure (C code case)
234 procedure Gen_Elab_Calls_Ada;
235 -- Generate sequence of elaboration calls (Ada code case)
237 procedure Gen_Elab_Calls_C;
238 -- Generate sequence of elaboration calls (C code case)
240 procedure Gen_Elab_Order_Ada;
241 -- Generate comments showing elaboration order chosen (Ada case)
243 procedure Gen_Elab_Order_C;
244 -- Generate comments showing elaboration order chosen (C case)
246 procedure Gen_Elab_Defs_C;
247 -- Generate sequence of definitions for elaboration routines (C code case)
249 procedure Gen_Main_Ada;
250 -- Generate procedure main (Ada code case)
252 procedure Gen_Main_C;
253 -- Generate main() procedure (C code case)
255 procedure Gen_Object_Files_Options;
256 -- Output comments containing a list of the full names of the object
257 -- files to be linked and the list of linker options supplied by
258 -- Linker_Options pragmas in the source. (C and Ada code case)
260 procedure Gen_Output_File_Ada (Filename : String);
261 -- Generate output file (Ada code case)
263 procedure Gen_Output_File_C (Filename : String);
264 -- Generate output file (C code case)
266 procedure Gen_Restrictions_Ada;
267 -- Generate initialization of restrictions variable (Ada code case)
269 procedure Gen_Restrictions_C;
270 -- Generate initialization of restrictions variable (C code case)
272 procedure Gen_Versions_Ada;
273 -- Output series of definitions for unit versions (Ada code case)
275 procedure Gen_Versions_C;
276 -- Output series of definitions for unit versions (C code case)
278 function Get_Ada_Main_Name return String;
279 -- This function is used in the Ada main output case to compute a usable
280 -- name for the generated main program. The normal main program name is
281 -- Ada_Main, but this won't work if the user has a unit with this name.
282 -- This function tries Ada_Main first, and if there is such a clash, then
283 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
285 function Get_Main_Name return String;
286 -- This function is used in the Ada main output case to compute the
287 -- correct external main program. It is "main" by default, unless the
288 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
289 -- is the name of the Ada main name without the "_ada". This default
290 -- can be overridden explicitly using the -Mname binder switch.
292 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
293 -- Compare linker options, when sorting, first according to
294 -- Is_Internal_File (internal files come later) and then by
295 -- elaboration order position (latest to earliest).
297 procedure Move_Linker_Option (From : Natural; To : Natural);
298 -- Move routine for sorting linker options
300 procedure Resolve_Binder_Options;
301 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
302 -- since it tests for a package named "dec" which might cause a conflict
303 -- on non-VMS systems.
305 procedure Set_Char (C : Character);
306 -- Set given character in Statement_Buffer at the Last + 1 position
307 -- and increment Last by one to reflect the stored character.
309 procedure Set_Int (N : Int);
310 -- Set given value in decimal in Statement_Buffer with no spaces
311 -- starting at the Last + 1 position, and updating Last past the value.
312 -- A minus sign is output for a negative value.
314 procedure Set_Boolean (B : Boolean);
315 -- Set given boolean value in Statement_Buffer at the Last + 1 position
316 -- and update Last past the value.
318 procedure Set_IS_Pragma_Table;
319 -- Initializes contents of IS_Pragma_Settings table from ALI table
321 procedure Set_Main_Program_Name;
322 -- Given the main program name in Name_Buffer (length in Name_Len)
323 -- generate the name of the routine to be used in the call. The name
324 -- is generated starting at Last + 1, and Last is updated past it.
326 procedure Set_Name_Buffer;
327 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
329 procedure Set_PSD_Pragma_Table;
330 -- Initializes contents of PSD_Pragma_Settings table from ALI table
332 procedure Set_String (S : String);
333 -- Sets characters of given string in Statement_Buffer, starting at the
334 -- Last + 1 position, and updating last past the string value.
336 procedure Set_Unit_Name;
337 -- Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
338 -- starting at the Last + 1 position, and updating last past the value.
339 -- changing periods to double underscores, and updating Last appropriately.
341 procedure Set_Unit_Number (U : Unit_Id);
342 -- Sets unit number (first unit is 1, leading zeroes output to line
343 -- up all output unit numbers nicely as required by the value, and
344 -- by the total number of units.
346 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
347 -- For C code case, write C & Common, for Ada case write Ada & Common
348 -- to current binder output file using Write_Binder_Info.
350 procedure Write_Statement_Buffer;
351 -- Write out contents of statement buffer up to Last, and reset Last to 0
353 procedure Write_Statement_Buffer (S : String);
354 -- First writes its argument (using Set_String (S)), then writes out the
355 -- contents of statement buffer up to Last, and reset Last to 0
357 ------------------------------------
358 -- Check_System_Restrictions_Used --
359 ------------------------------------
361 procedure Check_System_Restrictions_Used is
363 for J in Units.First .. Units.Last loop
364 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
365 System_Restrictions_Used := True;
370 System_Restrictions_Used := False;
371 end Check_System_Restrictions_Used;
373 ----------------------
374 -- Gen_Adafinal_Ada --
375 ----------------------
377 procedure Gen_Adafinal_Ada is
380 WBI (" procedure " & Ada_Final_Name.all & " is");
383 -- If compiling for the JVM, we directly call Adafinal because
384 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
386 if VM_Target /= No_VM then
387 WBI (" System.Standard_Library.Adafinal;");
389 -- If there is no finalization, there is nothing to do
391 elsif Cumulative_Restrictions.Set (No_Finalization) then
394 WBI (" Do_Finalize;");
397 WBI (" end " & Ada_Final_Name.all & ";");
398 end Gen_Adafinal_Ada;
404 procedure Gen_Adafinal_C is
406 WBI ("void " & Ada_Final_Name.all & " (void) {");
407 WBI (" system__standard_library__adafinal ();");
412 ---------------------
413 -- Gen_Adainit_Ada --
414 ---------------------
416 procedure Gen_Adainit_Ada is
417 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
420 WBI (" procedure " & Ada_Init_Name.all & " is");
422 -- Generate externals for elaboration entities
424 for E in Elab_Order.First .. Elab_Order.Last loop
426 Unum : constant Unit_Id := Elab_Order.Table (E);
427 U : Unit_Record renames Units.Table (Unum);
430 -- Check for Elab_Entity to be set for this unit
434 -- Don't generate reference for stand alone library
436 and then not U.SAL_Interface
438 -- Don't generate reference for predefined file in No_Run_Time
439 -- mode, since we don't include the object files in this case
443 and then Is_Predefined_File_Name (U.Sfile))
447 Set_Unit_Number (Unum);
450 when No_VM | JVM_Target =>
451 Set_String (" : Boolean; pragma Import (Ada, ");
453 Set_String (" : Boolean; pragma Import (CIL, ");
457 Set_Unit_Number (Unum);
459 Get_Name_String (U.Uname);
461 -- In the case of JGNAT we need to emit an Import name
462 -- that includes the class name (using '$' separators
463 -- in the case of a child unit name).
465 if VM_Target /= No_VM then
466 for J in 1 .. Name_Len - 2 loop
467 if VM_Target = CLI_Target
468 or else Name_Buffer (J) /= '.'
470 Set_Char (Name_Buffer (J));
476 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
479 Set_String ("_pkg.");
482 -- If the unit name is very long, then split the
483 -- Import link name across lines using "&" (occurs
484 -- in some C2 tests).
486 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
488 Write_Statement_Buffer;
494 Set_String ("_E"");");
495 Write_Statement_Buffer;
500 Write_Statement_Buffer;
502 -- If the standard library is suppressed, then the only global variable
503 -- that might be needed (by the Ravenscar profile) is the priority of
506 if Suppress_Standard_Library_On_Target then
507 if Main_Priority /= No_Main_Priority then
508 WBI (" Main_Priority : Integer;");
509 WBI (" pragma Import (C, Main_Priority," &
510 " ""__gl_main_priority"");");
516 if Main_Priority /= No_Main_Priority then
517 Set_String (" Main_Priority := ");
518 Set_Int (Main_Priority);
520 Write_Statement_Buffer;
526 -- Normal case (standard library not suppressed). Set all global values
527 -- used by the run time.
530 WBI (" Main_Priority : Integer;");
531 WBI (" pragma Import (C, Main_Priority, " &
532 """__gl_main_priority"");");
533 WBI (" Time_Slice_Value : Integer;");
534 WBI (" pragma Import (C, Time_Slice_Value, " &
535 """__gl_time_slice_val"");");
536 WBI (" WC_Encoding : Character;");
537 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
538 WBI (" Locking_Policy : Character;");
539 WBI (" pragma Import (C, Locking_Policy, " &
540 """__gl_locking_policy"");");
541 WBI (" Queuing_Policy : Character;");
542 WBI (" pragma Import (C, Queuing_Policy, " &
543 """__gl_queuing_policy"");");
544 WBI (" Task_Dispatching_Policy : Character;");
545 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
546 """__gl_task_dispatching_policy"");");
547 WBI (" Priority_Specific_Dispatching : System.Address;");
548 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
549 """__gl_priority_specific_dispatching"");");
550 WBI (" Num_Specific_Dispatching : Integer;");
551 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
552 """__gl_num_specific_dispatching"");");
554 WBI (" Interrupt_States : System.Address;");
555 WBI (" pragma Import (C, Interrupt_States, " &
556 """__gl_interrupt_states"");");
557 WBI (" Num_Interrupt_States : Integer;");
558 WBI (" pragma Import (C, Num_Interrupt_States, " &
559 """__gl_num_interrupt_states"");");
560 WBI (" Unreserve_All_Interrupts : Integer;");
561 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
562 """__gl_unreserve_all_interrupts"");");
564 if Exception_Tracebacks then
565 WBI (" Exception_Tracebacks : Integer;");
566 WBI (" pragma Import (C, Exception_Tracebacks, " &
567 """__gl_exception_tracebacks"");");
570 WBI (" Zero_Cost_Exceptions : Integer;");
571 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
572 """__gl_zero_cost_exceptions"");");
573 WBI (" Detect_Blocking : Integer;");
574 WBI (" pragma Import (C, Detect_Blocking, " &
575 """__gl_detect_blocking"");");
576 WBI (" Default_Stack_Size : Integer;");
577 WBI (" pragma Import (C, Default_Stack_Size, " &
578 """__gl_default_stack_size"");");
580 -- Import entry point for elaboration time signal handler
581 -- installation, and indication of if it's been called previously.
584 WBI (" procedure Install_Handler;");
585 WBI (" pragma Import (C, Install_Handler, " &
586 """__gnat_install_handler"");");
588 WBI (" Handler_Installed : Integer;");
589 WBI (" pragma Import (C, Handler_Installed, " &
590 """__gnat_handler_installed"");");
593 Set_String (" Main_Priority := ");
594 Set_Int (Main_Priority);
596 Write_Statement_Buffer;
598 Set_String (" Time_Slice_Value := ");
600 if Task_Dispatching_Policy_Specified = 'F'
601 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
605 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
609 Write_Statement_Buffer;
611 Set_String (" WC_Encoding := '");
612 Set_Char (ALIs.Table (ALIs.First).WC_Encoding);
614 Write_Statement_Buffer;
616 Set_String (" Locking_Policy := '");
617 Set_Char (Locking_Policy_Specified);
619 Write_Statement_Buffer;
621 Set_String (" Queuing_Policy := '");
622 Set_Char (Queuing_Policy_Specified);
624 Write_Statement_Buffer;
626 Set_String (" Task_Dispatching_Policy := '");
627 Set_Char (Task_Dispatching_Policy_Specified);
629 Write_Statement_Buffer;
631 Gen_Restrictions_Ada;
633 WBI (" Priority_Specific_Dispatching :=");
634 WBI (" Local_Priority_Specific_Dispatching'Address;");
636 Set_String (" Num_Specific_Dispatching := ");
637 Set_Int (PSD_Pragma_Settings.Last + 1);
639 Write_Statement_Buffer;
641 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
643 Set_String (" Num_Interrupt_States := ");
644 Set_Int (IS_Pragma_Settings.Last + 1);
646 Write_Statement_Buffer;
648 Set_String (" Unreserve_All_Interrupts := ");
650 if Unreserve_All_Interrupts_Specified then
657 Write_Statement_Buffer;
659 if Exception_Tracebacks then
660 WBI (" Exception_Tracebacks := 1;");
663 Set_String (" Zero_Cost_Exceptions := ");
665 if Zero_Cost_Exceptions_Specified then
672 Write_Statement_Buffer;
674 Set_String (" Detect_Blocking := ");
676 if Detect_Blocking then
683 Write_Statement_Buffer;
685 Set_String (" Default_Stack_Size := ");
686 Set_Int (Default_Stack_Size);
688 Write_Statement_Buffer;
690 -- Generate call to Install_Handler
693 WBI (" if Handler_Installed = 0 then");
694 WBI (" Install_Handler;");
698 -- Generate call to set Initialize_Scalar values if active
700 if Initialize_Scalars_Used then
702 Set_String (" System.Scalar_Values.Initialize ('");
703 Set_Char (Initialize_Scalars_Mode1);
705 Set_Char (Initialize_Scalars_Mode2);
707 Write_Statement_Buffer;
710 -- Generate assignment of default secondary stack size if set
712 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
714 Set_String (" System.Secondary_Stack.");
715 Set_String ("Default_Secondary_Stack_Size := ");
716 Set_Int (Opt.Default_Sec_Stack_Size);
718 Write_Statement_Buffer;
721 -- Generate elaboration calls
726 WBI (" end " & Ada_Init_Name.all & ";");
733 procedure Gen_Adainit_C is
734 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
737 WBI ("void " & Ada_Init_Name.all & " (void)");
740 -- Generate externals for elaboration entities
742 for E in Elab_Order.First .. Elab_Order.Last loop
744 Unum : constant Unit_Id := Elab_Order.Table (E);
745 U : Unit_Record renames Units.Table (Unum);
748 -- Check for Elab entity to be set for this unit
752 -- Don't generate reference for stand alone library
754 and then not U.SAL_Interface
756 -- Don't generate reference for predefined file in No_Run_Time
757 -- mode, since we don't include the object files in this case
761 and then Is_Predefined_File_Name (U.Sfile))
763 Set_String (" extern char ");
764 Get_Name_String (U.Uname);
767 Write_Statement_Buffer;
772 Write_Statement_Buffer;
774 -- Standard library suppressed
776 if Suppress_Standard_Library_On_Target then
778 -- Case of High_Integrity_Mode mode. Set __gl_main_priority if needed
779 -- for the Ravenscar profile.
781 if Main_Priority /= No_Main_Priority then
782 WBI (" extern int __gl_main_priority;");
783 Set_String (" __gl_main_priority = ");
784 Set_Int (Main_Priority);
786 Write_Statement_Buffer;
789 -- Normal case (standard library not suppressed)
792 -- Generate definition for interrupt states string
794 Set_String (" static const char *local_interrupt_states = """);
796 for J in 0 .. IS_Pragma_Settings.Last loop
797 Set_Char (IS_Pragma_Settings.Table (J));
801 Write_Statement_Buffer;
803 -- Generate definition for priority specific dispatching string
806 (" static const char *local_priority_specific_dispatching = """);
808 for J in 0 .. PSD_Pragma_Settings.Last loop
809 Set_Char (PSD_Pragma_Settings.Table (J));
813 Write_Statement_Buffer;
815 -- Generate declaration for secondary stack default if needed
817 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
818 WBI (" extern int system__secondary_stack__" &
819 "default_secondary_stack_size;");
824 -- Code for normal case (standard library not suppressed)
826 -- We call the routine from inside adainit() because this works for
827 -- both programs with and without binder generated "main" functions.
829 WBI (" extern int __gl_main_priority;");
830 Set_String (" __gl_main_priority = ");
831 Set_Int (Main_Priority);
833 Write_Statement_Buffer;
835 WBI (" extern int __gl_time_slice_val;");
836 Set_String (" __gl_time_slice_val = ");
838 if Task_Dispatching_Policy = 'F'
839 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
843 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
847 Write_Statement_Buffer;
849 WBI (" extern char __gl_wc_encoding;");
850 Set_String (" __gl_wc_encoding = '");
851 Set_Char (ALIs.Table (ALIs.First).WC_Encoding);
853 Write_Statement_Buffer;
855 WBI (" extern char __gl_locking_policy;");
856 Set_String (" __gl_locking_policy = '");
857 Set_Char (Locking_Policy_Specified);
859 Write_Statement_Buffer;
861 WBI (" extern char __gl_queuing_policy;");
862 Set_String (" __gl_queuing_policy = '");
863 Set_Char (Queuing_Policy_Specified);
865 Write_Statement_Buffer;
867 WBI (" extern char __gl_task_dispatching_policy;");
868 Set_String (" __gl_task_dispatching_policy = '");
869 Set_Char (Task_Dispatching_Policy_Specified);
871 Write_Statement_Buffer;
875 WBI (" extern const void *__gl_interrupt_states;");
876 WBI (" __gl_interrupt_states = local_interrupt_states;");
878 WBI (" extern int __gl_num_interrupt_states;");
879 Set_String (" __gl_num_interrupt_states = ");
880 Set_Int (IS_Pragma_Settings.Last + 1);
882 Write_Statement_Buffer;
884 WBI (" extern const void *__gl_priority_specific_dispatching;");
885 WBI (" __gl_priority_specific_dispatching =" &
886 " local_priority_specific_dispatching;");
888 WBI (" extern int __gl_num_specific_dispatching;");
889 Set_String (" __gl_num_specific_dispatching = ");
890 Set_Int (PSD_Pragma_Settings.Last + 1);
892 Write_Statement_Buffer;
894 WBI (" extern int __gl_unreserve_all_interrupts;");
895 Set_String (" __gl_unreserve_all_interrupts = ");
896 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
898 Write_Statement_Buffer;
900 if Exception_Tracebacks then
901 WBI (" extern int __gl_exception_tracebacks;");
902 WBI (" __gl_exception_tracebacks = 1;");
905 WBI (" extern int __gl_zero_cost_exceptions;");
906 Set_String (" __gl_zero_cost_exceptions = ");
907 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
909 Write_Statement_Buffer;
911 WBI (" extern int __gl_detect_blocking;");
912 Set_String (" __gl_detect_blocking = ");
914 if Detect_Blocking then
921 Write_Statement_Buffer;
923 WBI (" extern int __gl_default_stack_size;");
924 Set_String (" __gl_default_stack_size = ");
925 Set_Int (Default_Stack_Size);
927 Write_Statement_Buffer;
931 -- Install elaboration time signal handler
933 WBI (" if (__gnat_handler_installed == 0)");
935 WBI (" __gnat_install_handler ();");
939 -- Generate call to set Initialize_Scalar values if needed
941 if Initialize_Scalars_Used then
943 Set_String (" system__scalar_values__initialize('");
944 Set_Char (Initialize_Scalars_Mode1);
946 Set_Char (Initialize_Scalars_Mode2);
948 Write_Statement_Buffer;
951 -- Generate assignment of default secondary stack size if set
953 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
955 Set_String (" system__secondary_stack__");
956 Set_String ("default_secondary_stack_size = ");
957 Set_Int (Opt.Default_Sec_Stack_Size);
959 Write_Statement_Buffer;
962 -- Generate elaboration calls
969 ------------------------
970 -- Gen_Elab_Calls_Ada --
971 ------------------------
973 procedure Gen_Elab_Calls_Ada is
975 for E in Elab_Order.First .. Elab_Order.Last loop
977 Unum : constant Unit_Id := Elab_Order.Table (E);
978 U : Unit_Record renames Units.Table (Unum);
981 -- This is the unit number of the spec that corresponds to
982 -- this entry. It is the same as Unum except when the body
983 -- and spec are different and we are currently processing
984 -- the body, in which case it is the spec (Unum + 1).
987 if U.Utype = Is_Body then
988 Unum_Spec := Unum + 1;
993 -- Nothing to do if predefined unit in no run time mode
995 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
998 -- Case of no elaboration code
1000 elsif U.No_Elab then
1002 -- The only case in which we have to do something is if
1003 -- this is a body, with a separate spec, where the separate
1004 -- spec has an elaboration entity defined.
1006 -- In that case, this is where we set the elaboration entity
1007 -- to True, we do not need to test if this has already been
1008 -- done, since it is quicker to set the flag than to test it.
1010 if not U.SAL_Interface and then U.Utype = Is_Body
1011 and then Units.Table (Unum_Spec).Set_Elab_Entity
1014 Set_Unit_Number (Unum_Spec);
1015 Set_String (" := True;");
1016 Write_Statement_Buffer;
1019 -- Here if elaboration code is present. If binding a library
1020 -- or if there is a non-Ada main subprogram then we generate:
1022 -- if not uname_E then
1023 -- uname'elab_[spec|body];
1027 -- Otherwise, elaboration routines are called unconditionally:
1029 -- uname'elab_[spec|body];
1032 -- The uname_E assignment is skipped if this is a separate spec,
1033 -- since the assignment will be done when we process the body.
1035 elsif not U.SAL_Interface then
1036 if Force_Checking_Of_Elaboration_Flags or
1037 Interface_Library_Unit or
1038 (not Bind_Main_Program)
1040 Set_String (" if not E");
1041 Set_Unit_Number (Unum_Spec);
1042 Set_String (" then");
1043 Write_Statement_Buffer;
1048 Get_Decoded_Name_String_With_Brackets (U.Uname);
1050 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1051 if Name_Buffer (Name_Len) = 's' then
1052 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1055 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1059 Name_Len := Name_Len + 12;
1062 if Name_Buffer (Name_Len) = 's' then
1063 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1066 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1070 Name_Len := Name_Len + 8;
1073 Set_Casing (U.Icasing);
1076 Write_Statement_Buffer;
1078 if U.Utype /= Is_Spec then
1079 if Force_Checking_Of_Elaboration_Flags or
1080 Interface_Library_Unit or
1081 (not Bind_Main_Program)
1087 Set_Unit_Number (Unum_Spec);
1088 Set_String (" := True;");
1089 Write_Statement_Buffer;
1092 if Force_Checking_Of_Elaboration_Flags or
1093 Interface_Library_Unit or
1094 (not Bind_Main_Program)
1101 end Gen_Elab_Calls_Ada;
1103 ----------------------
1104 -- Gen_Elab_Calls_C --
1105 ----------------------
1107 procedure Gen_Elab_Calls_C is
1109 for E in Elab_Order.First .. Elab_Order.Last loop
1111 Unum : constant Unit_Id := Elab_Order.Table (E);
1112 U : Unit_Record renames Units.Table (Unum);
1114 Unum_Spec : Unit_Id;
1115 -- This is the unit number of the spec that corresponds to
1116 -- this entry. It is the same as Unum except when the body
1117 -- and spec are different and we are currently processing
1118 -- the body, in which case it is the spec (Unum + 1).
1121 if U.Utype = Is_Body then
1122 Unum_Spec := Unum + 1;
1127 -- Nothing to do if predefined unit in no run time mode
1129 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1132 -- Case of no elaboration code
1134 elsif U.No_Elab then
1136 -- The only case in which we have to do something is if
1137 -- this is a body, with a separate spec, where the separate
1138 -- spec has an elaboration entity defined.
1140 -- In that case, this is where we set the elaboration entity
1141 -- to True, we do not need to test if this has already been
1142 -- done, since it is quicker to set the flag than to test it.
1144 if not U.SAL_Interface and then U.Utype = Is_Body
1145 and then Units.Table (Unum_Spec).Set_Elab_Entity
1148 Get_Name_String (U.Uname);
1150 Set_String ("_E = 1;");
1151 Write_Statement_Buffer;
1154 -- Here if elaboration code is present. If binding a library
1155 -- or if there is a non-Ada main subprogram then we generate:
1157 -- if (uname_E == 0) {
1158 -- uname__elab[s|b] ();
1162 -- The uname_E assignment is skipped if this is a separate spec,
1163 -- since the assignment will be done when we process the body.
1165 elsif not U.SAL_Interface then
1166 Get_Name_String (U.Uname);
1168 if Force_Checking_Of_Elaboration_Flags or
1169 Interface_Library_Unit or
1170 (not Bind_Main_Program)
1172 Set_String (" if (");
1174 Set_String ("_E == 0) {");
1175 Write_Statement_Buffer;
1181 Set_String ("___elab");
1182 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1183 Set_String (" ();");
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)
1196 Set_String ("_E++;");
1197 Write_Statement_Buffer;
1200 if Force_Checking_Of_Elaboration_Flags or
1201 Interface_Library_Unit or
1202 (not Bind_Main_Program)
1210 end Gen_Elab_Calls_C;
1212 ----------------------
1213 -- Gen_Elab_Defs_C --
1214 ----------------------
1216 procedure Gen_Elab_Defs_C is
1218 for E in Elab_Order.First .. Elab_Order.Last loop
1220 -- Generate declaration of elaboration procedure if elaboration
1221 -- needed. Note that passive units are always excluded.
1223 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1224 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1225 Set_String ("extern void ");
1227 Set_String ("___elab");
1228 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1229 Set_String (" (void);");
1230 Write_Statement_Buffer;
1236 end Gen_Elab_Defs_C;
1238 ------------------------
1239 -- Gen_Elab_Order_Ada --
1240 ------------------------
1242 procedure Gen_Elab_Order_Ada is
1245 WBI (" -- BEGIN ELABORATION ORDER");
1247 for J in Elab_Order.First .. Elab_Order.Last loop
1248 Set_String (" -- ");
1249 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1251 Write_Statement_Buffer;
1254 WBI (" -- END ELABORATION ORDER");
1255 end Gen_Elab_Order_Ada;
1257 ----------------------
1258 -- Gen_Elab_Order_C --
1259 ----------------------
1261 procedure Gen_Elab_Order_C is
1264 WBI ("/* BEGIN ELABORATION ORDER");
1266 for J in Elab_Order.First .. Elab_Order.Last loop
1267 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1269 Write_Statement_Buffer;
1272 WBI (" END ELABORATION ORDER */");
1273 end Gen_Elab_Order_C;
1279 procedure Gen_Main_Ada is
1283 if Exit_Status_Supported_On_Target then
1284 Set_String (" function ");
1286 Set_String (" procedure ");
1289 Set_String (Get_Main_Name);
1291 if Command_Line_Args_On_Target then
1292 Write_Statement_Buffer;
1293 WBI (" (argc : Integer;");
1294 WBI (" argv : System.Address;");
1295 WBI (" envp : System.Address)");
1297 if Exit_Status_Supported_On_Target then
1298 WBI (" return Integer");
1304 if Exit_Status_Supported_On_Target then
1305 Set_String (" return Integer is");
1310 Write_Statement_Buffer;
1313 if Opt.Default_Exit_Status /= 0
1314 and then Bind_Main_Program
1315 and then not Configurable_Run_Time_Mode
1317 WBI (" procedure Set_Exit_Status (Status : Integer);");
1318 WBI (" pragma Import (C, Set_Exit_Status, " &
1319 """__gnat_set_exit_status"");");
1323 -- Initialize and Finalize
1325 if not Cumulative_Restrictions.Set (No_Finalization) then
1326 WBI (" procedure initialize (Addr : System.Address);");
1327 WBI (" pragma Import (C, initialize, ""__gnat_initialize"");");
1329 WBI (" procedure finalize;");
1330 WBI (" pragma Import (C, finalize, ""__gnat_finalize"");");
1333 -- If we want to analyze the stack, we have to import corresponding
1336 if Dynamic_Stack_Measurement then
1338 WBI (" procedure Output_Results;");
1339 WBI (" pragma Import (C, Output_Results, " &
1340 """__gnat_stack_usage_output_results"");");
1344 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1345 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1346 """__gnat_stack_usage_initialize"");");
1349 -- Deal with declarations for main program case
1351 if not No_Main_Subprogram then
1353 -- To call the main program, we declare it using a pragma Import
1354 -- Ada with the right link name.
1356 -- It might seem more obvious to "with" the main program, and call
1357 -- it in the normal Ada manner. We do not do this for three reasons:
1359 -- 1. It is more efficient not to recompile the main program
1360 -- 2. We are not entitled to assume the source is accessible
1361 -- 3. We don't know what options to use to compile it
1363 -- It is really reason 3 that is most critical (indeed we used
1364 -- to generate the "with", but several regression tests failed).
1368 if ALIs.Table (ALIs.First).Main_Program = Func then
1369 WBI (" Result : Integer;");
1371 WBI (" function Ada_Main_Program return Integer;");
1374 WBI (" procedure Ada_Main_Program;");
1377 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1378 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1379 Set_Main_Program_Name;
1380 Set_String (""");");
1382 Write_Statement_Buffer;
1385 if Bind_Main_Program
1386 and then not Suppress_Standard_Library_On_Target
1388 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1393 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1394 -- not referenced elsewhere in the generated program, but is needed
1395 -- by the debugger (that's why it is generated in the first place).
1396 -- The reference stops Ada_Main_Program_Name from being optimized
1397 -- away by smart linkers, such as the AiX linker.
1399 -- Because this variable is unused, we make this variable "aliased"
1400 -- with a pragma Volatile in order to tell the compiler to preserve
1401 -- this variable at any level of optimization.
1403 if Bind_Main_Program then
1405 (" Ensure_Reference : aliased System.Address := " &
1406 "Ada_Main_Program_Name'Address;");
1407 WBI (" pragma Volatile (Ensure_Reference);");
1413 -- Acquire command line arguments if present on target
1415 if Command_Line_Args_On_Target then
1416 WBI (" gnat_argc := argc;");
1417 WBI (" gnat_argv := argv;");
1418 WBI (" gnat_envp := envp;");
1421 -- If configurable run time and no command line args, then nothing
1422 -- needs to be done since the gnat_argc/argv/envp variables are
1423 -- suppressed in this case.
1425 elsif Configurable_Run_Time_On_Target then
1428 -- Otherwise set dummy values (to be filled in by some other unit?)
1431 WBI (" gnat_argc := 0;");
1432 WBI (" gnat_argv := System.Null_Address;");
1433 WBI (" gnat_envp := System.Null_Address;");
1436 if Opt.Default_Exit_Status /= 0
1437 and then Bind_Main_Program
1438 and then not Configurable_Run_Time_Mode
1440 Set_String (" Set_Exit_Status (");
1441 Set_Int (Opt.Default_Exit_Status);
1443 Write_Statement_Buffer;
1446 if Dynamic_Stack_Measurement then
1447 Set_String (" Initialize_Stack_Analysis (");
1448 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1450 Write_Statement_Buffer;
1453 if not Cumulative_Restrictions.Set (No_Finalization) then
1454 if not No_Main_Subprogram
1455 and then Bind_Main_Program
1456 and then not Suppress_Standard_Library_On_Target
1458 WBI (" Initialize (SEH'Address);");
1460 WBI (" Initialize (System.Null_Address);");
1464 WBI (" " & Ada_Init_Name.all & ";");
1466 if not No_Main_Subprogram then
1467 WBI (" Break_Start;");
1469 if ALIs.Table (ALIs.First).Main_Program = Proc then
1470 WBI (" Ada_Main_Program;");
1472 WBI (" Result := Ada_Main_Program;");
1476 -- Adafinal call is skipped if no finalization
1478 if not Cumulative_Restrictions.Set (No_Finalization) then
1480 -- If compiling for the JVM, we directly call Adafinal because
1481 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1483 if VM_Target = No_VM then
1484 WBI (" Do_Finalize;");
1486 WBI (" System.Standard_Library.Adafinal;");
1490 -- Prints the result of static stack analysis
1492 if Dynamic_Stack_Measurement then
1493 WBI (" Output_Results;");
1496 -- Finalize is only called if we have a run time
1498 if not Cumulative_Restrictions.Set (No_Finalization) then
1504 if Exit_Status_Supported_On_Target then
1505 if No_Main_Subprogram
1506 or else ALIs.Table (ALIs.First).Main_Program = Proc
1508 WBI (" return (gnat_exit_status);");
1510 WBI (" return (Result);");
1521 procedure Gen_Main_C is
1523 if Exit_Status_Supported_On_Target then
1524 WBI ("#include <stdlib.h>");
1525 Set_String ("int ");
1527 Set_String ("void ");
1530 Set_String (Get_Main_Name);
1532 -- Generate command line args in prototype if present on target
1534 if Command_Line_Args_On_Target then
1535 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
1537 -- Case of no command line arguments on target
1540 Write_Statement_Buffer (" (void)");
1545 -- Generate a reference to __gnat_ada_main_program_name. This symbol
1546 -- is not referenced elsewhere in the generated program, but is
1547 -- needed by the debugger (that's why it is generated in the first
1548 -- place). The reference stops Ada_Main_Program_Name from being
1549 -- optimized away by smart linkers, such as the AiX linker.
1551 -- Because this variable is unused, we declare this variable as
1552 -- volatile in order to tell the compiler to preserve it at any
1553 -- level of optimization.
1555 if Bind_Main_Program then
1556 WBI (" char * volatile ensure_reference " &
1557 "__attribute__ ((__unused__)) = " &
1558 "__gnat_ada_main_program_name;");
1561 if not Suppress_Standard_Library_On_Target
1562 and then not No_Main_Subprogram
1564 WBI (" int SEH [2];");
1569 -- If main program is a function, generate result variable
1571 if ALIs.Table (ALIs.First).Main_Program = Func then
1572 WBI (" int result;");
1575 -- Set command line argument values from parameters if command line
1576 -- arguments are present on target
1578 if Command_Line_Args_On_Target then
1579 WBI (" gnat_argc = argc;");
1580 WBI (" gnat_argv = argv;");
1581 WBI (" gnat_envp = envp;");
1584 -- If configurable run-time, then nothing to do, since in this case
1585 -- the gnat_argc/argv/envp variables are entirely suppressed.
1587 elsif Configurable_Run_Time_On_Target then
1590 -- if no command line arguments on target, set dummy values
1593 WBI (" gnat_argc = 0;");
1594 WBI (" gnat_argv = 0;");
1595 WBI (" gnat_envp = 0;");
1598 if Opt.Default_Exit_Status /= 0
1599 and then Bind_Main_Program
1600 and then not Configurable_Run_Time_Mode
1602 Set_String (" __gnat_set_exit_status (");
1603 Set_Int (Opt.Default_Exit_Status);
1605 Write_Statement_Buffer;
1608 -- Initializes dynamic stack measurement if needed
1610 if Dynamic_Stack_Measurement then
1611 Set_String (" __gnat_stack_usage_initialize (");
1612 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1614 Write_Statement_Buffer;
1617 -- The __gnat_initialize routine is used only if we have a run-time
1619 if not Suppress_Standard_Library_On_Target then
1620 if not No_Main_Subprogram and then Bind_Main_Program then
1621 WBI (" __gnat_initialize ((void *)SEH);");
1623 WBI (" __gnat_initialize ((void *)0);");
1627 WBI (" " & Ada_Init_Name.all & " ();");
1629 if not No_Main_Subprogram then
1630 WBI (" __gnat_break_start ();");
1633 -- Output main program name
1635 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1637 -- Main program is procedure case
1639 if ALIs.Table (ALIs.First).Main_Program = Proc then
1641 Set_Main_Program_Name;
1642 Set_String (" ();");
1643 Write_Statement_Buffer;
1645 -- Main program is function case
1647 else -- ALIs.Table (ALIs_First).Main_Program = Func
1648 Set_String (" result = ");
1649 Set_Main_Program_Name;
1650 Set_String (" ();");
1651 Write_Statement_Buffer;
1656 -- Call adafinal if finalization active
1658 if not Cumulative_Restrictions.Set (No_Finalization) then
1660 WBI (" system__standard_library__adafinal ();");
1663 -- Outputs the dynamic stack measurement if needed
1665 if Dynamic_Stack_Measurement then
1666 WBI (" __gnat_stack_usage_output_results ();");
1669 -- The finalize routine is used only if we have a run-time
1671 if not Suppress_Standard_Library_On_Target then
1672 WBI (" __gnat_finalize ();");
1675 -- Case of main program is a function, so the value it returns
1676 -- is the exit status in this case.
1678 if ALIs.Table (ALIs.First).Main_Program = Func then
1679 if Exit_Status_Supported_On_Target then
1681 -- VMS must use Posix exit routine in order to get the effect
1682 -- of a Unix compatible setting of the program exit status.
1683 -- For all other systems, we use the standard exit routine.
1685 if OpenVMS_On_Target then
1686 WBI (" decc$__posix_exit (result);");
1688 WBI (" exit (result);");
1692 -- Case of main program is a procedure, in which case the exit
1693 -- status is whatever was set by a Set_Exit call most recently
1696 if Exit_Status_Supported_On_Target then
1698 -- VMS must use Posix exit routine in order to get the effect
1699 -- of a Unix compatible setting of the program exit status.
1700 -- For all other systems, we use the standard exit routine.
1702 if OpenVMS_On_Target then
1703 WBI (" decc$__posix_exit (gnat_exit_status);");
1705 WBI (" exit (gnat_exit_status);");
1713 ------------------------------
1714 -- Gen_Object_Files_Options --
1715 ------------------------------
1717 procedure Gen_Object_Files_Options is
1719 -- This keeps track of the position in the sorted set of entries
1720 -- in the Linker_Options table of where the first entry from an
1721 -- internal file appears.
1723 Linker_Option_List_Started : Boolean := False;
1724 -- Set to True when "LINKER OPTION LIST" is displayed
1726 procedure Write_Linker_Option;
1727 -- Write binder info linker option
1729 -------------------------
1730 -- Write_Linker_Option --
1731 -------------------------
1733 procedure Write_Linker_Option is
1738 -- Loop through string, breaking at null's
1741 while Start < Name_Len loop
1743 -- Find null ending this section
1746 while Name_Buffer (Stop) /= ASCII.NUL
1747 and then Stop <= Name_Len loop
1751 -- Process section if non-null
1753 if Stop > Start then
1754 if Output_Linker_Option_List then
1755 if not Zero_Formatting then
1756 if not Linker_Option_List_Started then
1757 Linker_Option_List_Started := True;
1759 Write_Str (" LINKER OPTION LIST");
1767 Write_Str (Name_Buffer (Start .. Stop - 1));
1771 (" -- ", "", Name_Buffer (Start .. Stop - 1));
1776 end Write_Linker_Option;
1778 -- Start of processing for Gen_Object_Files_Options
1782 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
1784 for E in Elab_Order.First .. Elab_Order.Last loop
1786 -- If not spec that has an associated body, then generate a
1787 -- comment giving the name of the corresponding object file.
1789 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
1790 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1794 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1796 -- If the presence of an object file is necessary or if it
1797 -- exists, then use it.
1799 if not Hostparm.Exclude_Missing_Objects
1801 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1803 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1805 if Output_Object_List then
1806 Write_Str (Name_Buffer (1 .. Name_Len));
1810 -- Don't link with the shared library on VMS if an internal
1811 -- filename object is seen. Multiply defined symbols will
1814 if OpenVMS_On_Target
1815 and then Is_Internal_File_Name
1817 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
1819 -- Special case for g-trasym.obj, which is not included
1822 Get_Name_String (ALIs.Table
1823 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
1825 if Name_Buffer (1 .. 8) /= "g-trasym" then
1826 Opt.Shared_Libgnat := False;
1833 -- Add a "-Ldir" for each directory in the object path
1835 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1837 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1840 Add_Str_To_Name_Buffer ("-L");
1841 Add_Str_To_Name_Buffer (Dir.all);
1842 Write_Linker_Option;
1846 -- Sort linker options
1848 -- This sort accomplishes two important purposes:
1850 -- a) All application files are sorted to the front, and all
1851 -- GNAT internal files are sorted to the end. This results
1852 -- in a well defined dividing line between the two sets of
1853 -- files, for the purpose of inserting certain standard
1854 -- library references into the linker arguments list.
1856 -- b) Given two different units, we sort the linker options so
1857 -- that those from a unit earlier in the elaboration order
1858 -- comes later in the list. This is a heuristic designed
1859 -- to create a more friendly order of linker options when
1860 -- the operations appear in separate units. The idea is that
1861 -- if unit A must be elaborated before unit B, then it is
1862 -- more likely that B references libraries included by A,
1863 -- than vice versa, so we want the libraries included by
1864 -- A to come after the libraries included by B.
1866 -- These two criteria are implemented by function Lt_Linker_Option.
1867 -- Note that a special case of b) is that specs are elaborated before
1868 -- bodies, so linker options from specs come after linker options
1869 -- for bodies, and again, the assumption is that libraries used by
1870 -- the body are more likely to reference libraries used by the spec,
1874 (Linker_Options.Last,
1875 Move_Linker_Option'Access,
1876 Lt_Linker_Option'Access);
1878 -- Write user linker options, i.e. the set of linker options that
1879 -- come from all files other than GNAT internal files, Lgnat is
1880 -- left set to point to the first entry from a GNAT internal file,
1881 -- or past the end of the entriers if there are no internal files.
1883 Lgnat := Linker_Options.Last + 1;
1885 for J in 1 .. Linker_Options.Last loop
1886 if not Linker_Options.Table (J).Internal_File then
1887 Get_Name_String (Linker_Options.Table (J).Name);
1888 Write_Linker_Option;
1895 -- Now we insert standard linker options that must appear after the
1896 -- entries from user files, and before the entries from GNAT run-time
1897 -- files. The reason for this decision is that libraries referenced
1898 -- by internal routines may reference these standard library entries.
1900 -- Note that we do not insert anything when pragma No_Run_Time has been
1901 -- specified or when the standard libraries are not to be used,
1902 -- otherwise on some platforms, such as VMS, we may get duplicate
1903 -- symbols when linking.
1905 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
1908 if Opt.Shared_Libgnat then
1909 Add_Str_To_Name_Buffer ("-shared");
1911 Add_Str_To_Name_Buffer ("-static");
1914 -- Write directly to avoid -K output (why???)
1916 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1918 if With_DECGNAT then
1921 if Opt.Shared_Libgnat then
1922 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
1924 Add_Str_To_Name_Buffer ("-ldecgnat");
1927 Write_Linker_Option;
1933 if Opt.Shared_Libgnat then
1934 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
1936 Add_Str_To_Name_Buffer ("-lgnarl");
1939 Write_Linker_Option;
1944 if Opt.Shared_Libgnat then
1945 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
1947 Add_Str_To_Name_Buffer ("-lgnat");
1950 Write_Linker_Option;
1953 -- Write linker options from all internal files
1955 for J in Lgnat .. Linker_Options.Last loop
1956 Get_Name_String (Linker_Options.Table (J).Name);
1957 Write_Linker_Option;
1960 if Output_Linker_Option_List and then not Zero_Formatting then
1964 if Ada_Bind_File then
1965 WBI ("-- END Object file/option list ");
1967 WBI (" END Object file/option list */");
1969 end Gen_Object_Files_Options;
1971 ---------------------
1972 -- Gen_Output_File --
1973 ---------------------
1975 procedure Gen_Output_File (Filename : String) is
1977 -- Acquire settings for Interrupt_State pragmas
1979 Set_IS_Pragma_Table;
1981 -- Acquire settings for Priority_Specific_Dispatching pragma
1983 Set_PSD_Pragma_Table;
1985 -- Override Ada_Bind_File and Bind_Main_Program for VMs since
1986 -- JGNAT only supports Ada code, and the main program is already
1987 -- generated by the compiler.
1989 if VM_Target /= No_VM then
1990 Ada_Bind_File := True;
1991 Bind_Main_Program := False;
1994 -- Override time slice value if -T switch is set
1996 if Time_Slice_Set then
1997 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2000 -- Count number of elaboration calls
2002 for E in Elab_Order.First .. Elab_Order.Last loop
2003 if Units.Table (Elab_Order.Table (E)).No_Elab then
2006 Num_Elab_Calls := Num_Elab_Calls + 1;
2010 -- Generate output file in appropriate language
2012 Check_System_Restrictions_Used;
2014 if Ada_Bind_File then
2015 Gen_Output_File_Ada (Filename);
2017 Gen_Output_File_C (Filename);
2019 end Gen_Output_File;
2021 -------------------------
2022 -- Gen_Output_File_Ada --
2023 -------------------------
2025 procedure Gen_Output_File_Ada (Filename : String) is
2028 -- Name of generated bind file (spec)
2031 -- Name of generated bind file (body)
2033 Ada_Main : constant String := Get_Ada_Main_Name;
2034 -- Name to be used for generated Ada main program. See the body of
2035 -- function Get_Ada_Main_Name for details on the form of the name.
2038 -- Create spec first
2040 Create_Binder_Output (Filename, 's', Bfiles);
2042 -- We always compile the binder file in Ada 95 mode so that we properly
2043 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2044 -- of the Ada 2005 constructs are needed by the binder file.
2046 WBI ("pragma Ada_95;");
2048 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2049 -- then we need to make sure that the binder program is compiled with
2050 -- the same restriction, so that no exception tables are generated.
2052 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2053 WBI ("pragma Restrictions (No_Exception_Handlers);");
2056 -- Same processing for Restrictions (No_Exception_Propagation)
2058 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2059 WBI ("pragma Restrictions (No_Exception_Propagation);");
2062 -- Same processing for pragma No_Run_Time
2064 if No_Run_Time_Mode then
2065 WBI ("pragma No_Run_Time;");
2068 -- Generate with of System so we can reference System.Address
2070 WBI ("with System;");
2072 -- Generate with of System.Initialize_Scalars if active
2074 if Initialize_Scalars_Used then
2075 WBI ("with System.Scalar_Values;");
2078 -- Generate with of System.Secondary_Stack if active
2080 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2081 WBI ("with System.Secondary_Stack;");
2084 Resolve_Binder_Options;
2086 if not Suppress_Standard_Library_On_Target then
2087 -- Usually, adafinal is called using a pragma Import C. Since
2088 -- Import C doesn't have the same semantics for JGNAT, we use
2091 if VM_Target /= No_VM then
2092 WBI ("with System.Standard_Library;");
2096 WBI ("package " & Ada_Main & " is");
2097 WBI (" pragma Warnings (Off);");
2099 -- Main program case
2101 if Bind_Main_Program then
2103 -- Generate argc/argv stuff unless suppressed
2105 if Command_Line_Args_On_Target
2106 or not Configurable_Run_Time_On_Target
2109 WBI (" gnat_argc : Integer;");
2110 WBI (" gnat_argv : System.Address;");
2111 WBI (" gnat_envp : System.Address;");
2113 -- If the standard library is not suppressed, these variables are
2114 -- in the runtime data area for easy access from the runtime
2116 if not Suppress_Standard_Library_On_Target then
2118 WBI (" pragma Import (C, gnat_argc);");
2119 WBI (" pragma Import (C, gnat_argv);");
2120 WBI (" pragma Import (C, gnat_envp);");
2124 -- Define exit status. Again in normal mode, this is in the
2125 -- run-time library, and is initialized there, but in the
2126 -- configurable runtime case, the variable is declared and
2127 -- initialized in this file.
2131 if Configurable_Run_Time_Mode then
2132 if Exit_Status_Supported_On_Target then
2133 WBI (" gnat_exit_status : Integer := 0;");
2136 WBI (" gnat_exit_status : Integer;");
2137 WBI (" pragma Import (C, gnat_exit_status);");
2141 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2142 -- the main program. Otherwise, it can lead under some circumstances
2143 -- to a symbol duplication during the link (for instance when a
2144 -- C program uses 2 Ada libraries)
2146 if Bind_Main_Program then
2148 WBI (" GNAT_Version : constant String :=");
2149 WBI (" ""GNAT Version: " &
2150 Gnat_Version_String & """;");
2151 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2154 Set_String (" Ada_Main_Program_Name : constant String := """);
2155 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2156 Set_Main_Program_Name;
2157 Set_String (""" & Ascii.NUL;");
2158 Write_Statement_Buffer;
2161 (" pragma Export (C, Ada_Main_Program_Name, " &
2162 """__gnat_ada_main_program_name"");");
2166 WBI (" procedure " & Ada_Final_Name.all & ";");
2167 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2168 Ada_Final_Name.all & """);");
2170 WBI (" procedure " & Ada_Init_Name.all & ";");
2171 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2172 Ada_Init_Name.all & """);");
2174 -- If -a has been specified use pragma Linker_Constructor for the init
2175 -- procedure. No need to use a similar pragma for the final procedure as
2176 -- global finalization will occur when the executable finishes execution
2177 -- and for plugins (shared stand-alone libraries that can be
2178 -- "unloaded"), finalization should not occur automatically, otherwise
2179 -- the main executable may not continue to work properly.
2181 if Use_Pragma_Linker_Constructor then
2182 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2185 if Bind_Main_Program then
2187 -- If we have the standard library, then Break_Start is defined
2188 -- there, but when the standard library is suppressed, Break_Start
2192 WBI (" procedure Break_Start;");
2194 if Suppress_Standard_Library_On_Target then
2195 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2197 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2202 if Exit_Status_Supported_On_Target then
2203 Set_String (" function ");
2205 Set_String (" procedure ");
2208 Set_String (Get_Main_Name);
2210 -- Generate argument list if present
2212 if Command_Line_Args_On_Target then
2213 Write_Statement_Buffer;
2214 WBI (" (argc : Integer;");
2215 WBI (" argv : System.Address;");
2217 (" envp : System.Address)");
2219 if Exit_Status_Supported_On_Target then
2220 Write_Statement_Buffer;
2221 WBI (" return Integer;");
2223 Write_Statement_Buffer (";");
2227 if Exit_Status_Supported_On_Target then
2228 Write_Statement_Buffer (" return Integer;");
2230 Write_Statement_Buffer (";");
2234 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2235 Get_Main_Name & """);");
2244 WBI ("end " & Ada_Main & ";");
2245 Close_Binder_Output;
2247 -- Prepare to write body
2249 Create_Binder_Output (Filename, 'b', Bfileb);
2251 -- We always compile the binder file in Ada 95 mode so that we properly
2252 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2253 -- of the Ada 2005 constructs are needed by the binder file.
2255 WBI ("pragma Ada_95;");
2257 -- Output Source_File_Name pragmas which look like
2259 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2260 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2262 -- where sss/bbb are the spec/body file names respectively
2264 Get_Name_String (Bfiles);
2265 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2267 WBI ("pragma Source_File_Name (" &
2269 ", Spec_File_Name => """ &
2270 Name_Buffer (1 .. Name_Len + 3));
2272 Get_Name_String (Bfileb);
2273 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2275 WBI ("pragma Source_File_Name (" &
2277 ", Body_File_Name => """ &
2278 Name_Buffer (1 .. Name_Len + 3));
2280 -- Generate with of System.Restrictions to initialize
2281 -- Run_Time_Restrictions.
2283 if System_Restrictions_Used
2284 and not Suppress_Standard_Library_On_Target
2287 WBI ("with System.Restrictions;");
2291 WBI ("package body " & Ada_Main & " is");
2292 WBI (" pragma Warnings (Off);");
2294 -- Import the finalization procedure only if finalization active
2296 if not Cumulative_Restrictions.Set (No_Finalization) then
2298 -- In the Java case, pragma Import C cannot be used, so the
2299 -- standard Ada constructs will be used instead.
2301 if VM_Target = No_VM then
2303 WBI (" procedure Do_Finalize;");
2305 (" pragma Import (C, Do_Finalize, " &
2306 """system__standard_library__adafinal"");");
2311 if not Suppress_Standard_Library_On_Target then
2313 -- Generate Priority_Specific_Dispatching pragma string
2316 (" Local_Priority_Specific_Dispatching : constant String := """);
2318 for J in 0 .. PSD_Pragma_Settings.Last loop
2319 Set_Char (PSD_Pragma_Settings.Table (J));
2323 Write_Statement_Buffer;
2325 -- Generate Interrupt_State pragma string
2327 Set_String (" Local_Interrupt_States : constant String := """);
2329 for J in 0 .. IS_Pragma_Settings.Last loop
2330 Set_Char (IS_Pragma_Settings.Table (J));
2334 Write_Statement_Buffer;
2342 if Bind_Main_Program then
2344 -- When suppressing the standard library then generate dummy body
2347 if Suppress_Standard_Library_On_Target then
2349 WBI (" procedure Break_Start is");
2358 -- Output object file list and the Ada body is complete
2360 Gen_Object_Files_Options;
2363 WBI ("end " & Ada_Main & ";");
2365 Close_Binder_Output;
2366 end Gen_Output_File_Ada;
2368 -----------------------
2369 -- Gen_Output_File_C --
2370 -----------------------
2372 procedure Gen_Output_File_C (Filename : String) is
2375 -- Name of generated bind file
2378 Create_Binder_Output (Filename, 'c', Bfile);
2380 Resolve_Binder_Options;
2382 WBI ("extern void " & Ada_Final_Name.all & " (void);");
2384 -- If -a has been specified use __attribute__((constructor)) for the
2385 -- init procedure. No need to use a similar featute for the final
2386 -- procedure as global finalization will occur when the executable
2387 -- finishes execution and for plugins (shared stand-alone libraries that
2388 -- can be "unloaded"), finalization should not occur automatically,
2389 -- otherwise the main executable may not continue to work properly.
2391 if Use_Pragma_Linker_Constructor then
2392 WBI ("extern void " & Ada_Init_Name.all &
2393 " (void) __attribute__((constructor));");
2396 WBI ("extern void " & Ada_Init_Name.all & " (void);");
2399 WBI ("extern void system__standard_library__adafinal (void);");
2401 if not No_Main_Subprogram then
2402 Set_String ("extern ");
2404 if Exit_Status_Supported_On_Target then
2407 Set_String ("void");
2410 Set_String (" main ");
2412 if Command_Line_Args_On_Target then
2413 Write_Statement_Buffer ("(int, char **, char **);");
2415 Write_Statement_Buffer ("(void);");
2418 if OpenVMS_On_Target then
2419 WBI ("extern void decc$__posix_exit (int);");
2421 WBI ("extern void exit (int);");
2424 WBI ("extern void __gnat_break_start (void);");
2425 Set_String ("extern ");
2427 if ALIs.Table (ALIs.First).Main_Program = Proc then
2428 Set_String ("void ");
2430 Set_String ("int ");
2433 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2434 Set_Main_Program_Name;
2435 Set_String (" (void);");
2436 Write_Statement_Buffer;
2439 if not Suppress_Standard_Library_On_Target then
2440 WBI ("extern void __gnat_initialize (void *);");
2441 WBI ("extern void __gnat_finalize (void);");
2442 WBI ("extern void __gnat_install_handler (void);");
2445 if Dynamic_Stack_Measurement then
2447 WBI ("extern void __gnat_stack_usage_output_results (void);");
2448 WBI ("extern void __gnat_stack_usage_initialize (int size);");
2455 -- Imported variable used to track elaboration/finalization phase.
2456 -- Used only when we have a runtime.
2458 if not Suppress_Standard_Library_On_Target then
2459 WBI ("extern int __gnat_handler_installed;");
2463 -- Write argv/argc exit status stuff if main program case
2465 if Bind_Main_Program then
2467 -- First deal with argc/argv/envp. In the normal case they
2468 -- are in the run-time library.
2470 if not Configurable_Run_Time_On_Target then
2471 WBI ("extern int gnat_argc;");
2472 WBI ("extern char **gnat_argv;");
2473 WBI ("extern char **gnat_envp;");
2475 -- If configurable run time and no command line args, then the
2476 -- generation of these variables is entirely suppressed.
2478 elsif not Command_Line_Args_On_Target then
2481 -- Otherwise, in the configurable run-time case they are right in
2485 WBI ("int gnat_argc;");
2486 WBI ("char **gnat_argv;");
2487 WBI ("char **gnat_envp;");
2490 -- Similarly deal with exit status
2491 -- are in the run-time library.
2493 if not Configurable_Run_Time_On_Target then
2494 WBI ("extern int gnat_exit_status;");
2496 -- If configurable run time and no exit status on target, then
2497 -- the generation of this variables is entirely suppressed.
2499 elsif not Exit_Status_Supported_On_Target then
2502 -- Otherwise, in the configurable run-time case this variable is
2503 -- right in the binder file, and initialized to zero there.
2506 WBI ("int gnat_exit_status = 0;");
2512 -- When suppressing the standard library, the __gnat_break_start
2513 -- routine (for the debugger to get initial control) is defined in
2516 if Suppress_Standard_Library_On_Target then
2518 WBI ("void __gnat_break_start (void) {}");
2521 -- Generate the __gnat_version and __gnat_ada_main_program_name info
2522 -- only for the main program. Otherwise, it can lead under some
2523 -- circumstances to a symbol duplication during the link (for instance
2524 -- when a C program uses 2 Ada libraries)
2526 if Bind_Main_Program then
2528 WBI ("char __gnat_version[] = ""GNAT Version: " &
2529 Gnat_Version_String & """;");
2531 Set_String ("char __gnat_ada_main_program_name[] = """);
2532 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2533 Set_Main_Program_Name;
2535 Write_Statement_Buffer;
2538 -- Generate the adafinal routine. In no runtime mode, this is
2539 -- not needed, since there is no finalization to do.
2541 if not Cumulative_Restrictions.Set (No_Finalization) then
2547 -- Main is only present for Ada main case
2549 if Bind_Main_Program then
2553 -- Generate versions, elaboration order, list of object files
2557 Gen_Object_Files_Options;
2559 -- C binder output is complete
2561 Close_Binder_Output;
2562 end Gen_Output_File_C;
2564 --------------------------
2565 -- Gen_Restrictions_Ada --
2566 --------------------------
2568 procedure Gen_Restrictions_Ada is
2572 if Suppress_Standard_Library_On_Target
2573 or not System_Restrictions_Used
2578 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2584 for J in Cumulative_Restrictions.Set'First ..
2585 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2587 Set_Boolean (Cumulative_Restrictions.Set (J));
2592 Write_Statement_Buffer;
2599 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last));
2601 Write_Statement_Buffer;
2602 Set_String (" Value => (");
2604 for J in Cumulative_Restrictions.Value'First ..
2605 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2607 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2611 Set_Int (Int (Cumulative_Restrictions.Value
2612 (Cumulative_Restrictions.Value'Last)));
2614 Write_Statement_Buffer;
2615 WBI (" Violated =>");
2619 for J in Cumulative_Restrictions.Violated'First ..
2620 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2622 Set_Boolean (Cumulative_Restrictions.Violated (J));
2627 Write_Statement_Buffer;
2633 Set_Boolean (Cumulative_Restrictions.Violated
2634 (Cumulative_Restrictions.Violated'Last));
2636 Write_Statement_Buffer;
2637 Set_String (" Count => (");
2639 for J in Cumulative_Restrictions.Count'First ..
2640 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2642 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2646 Set_Int (Int (Cumulative_Restrictions.Count
2647 (Cumulative_Restrictions.Count'Last)));
2649 Write_Statement_Buffer;
2650 Set_String (" Unknown => (");
2652 for J in Cumulative_Restrictions.Unknown'First ..
2653 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2655 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2660 (Cumulative_Restrictions.Unknown
2661 (Cumulative_Restrictions.Unknown'Last));
2663 Write_Statement_Buffer;
2664 end Gen_Restrictions_Ada;
2666 ------------------------
2667 -- Gen_Restrictions_C --
2668 ------------------------
2670 procedure Gen_Restrictions_C is
2672 if Suppress_Standard_Library_On_Target
2673 or not System_Restrictions_Used
2678 WBI (" typedef struct {");
2679 Set_String (" char set [");
2680 Set_Int (Cumulative_Restrictions.Set'Length);
2682 Write_Statement_Buffer;
2684 Set_String (" int value [");
2685 Set_Int (Cumulative_Restrictions.Value'Length);
2687 Write_Statement_Buffer;
2689 Set_String (" char violated [");
2690 Set_Int (Cumulative_Restrictions.Violated'Length);
2692 Write_Statement_Buffer;
2694 Set_String (" int count [");
2695 Set_Int (Cumulative_Restrictions.Count'Length);
2697 Write_Statement_Buffer;
2699 Set_String (" char unknown [");
2700 Set_Int (Cumulative_Restrictions.Unknown'Length);
2702 Write_Statement_Buffer;
2703 WBI (" } restrictions;");
2704 WBI (" extern restrictions " &
2705 "system__restrictions__run_time_restrictions;");
2706 WBI (" restrictions r = {");
2709 for J in Cumulative_Restrictions.Set'First ..
2710 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2712 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
2716 Set_Int (Boolean'Pos
2717 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last)));
2719 Write_Statement_Buffer;
2722 for J in Cumulative_Restrictions.Value'First ..
2723 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2725 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2729 Set_Int (Int (Cumulative_Restrictions.Value
2730 (Cumulative_Restrictions.Value'Last)));
2732 Write_Statement_Buffer;
2735 for J in Cumulative_Restrictions.Violated'First ..
2736 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2738 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
2742 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated
2743 (Cumulative_Restrictions.Violated'Last)));
2745 Write_Statement_Buffer;
2748 for J in Cumulative_Restrictions.Count'First ..
2749 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2751 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2755 Set_Int (Int (Cumulative_Restrictions.Count
2756 (Cumulative_Restrictions.Count'Last)));
2758 Write_Statement_Buffer;
2761 for J in Cumulative_Restrictions.Unknown'First ..
2762 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2764 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
2768 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown
2769 (Cumulative_Restrictions.Unknown'Last)));
2771 Write_Statement_Buffer;
2772 WBI (" system__restrictions__run_time_restrictions = r;");
2773 end Gen_Restrictions_C;
2775 ----------------------
2776 -- Gen_Versions_Ada --
2777 ----------------------
2779 -- This routine generates two sets of lines. The first set has the form:
2781 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2783 -- The second set has the form
2785 -- pragma Export (C, unnnnn, unam);
2787 -- for each unit, where unam is the unit name suffixed by either B or
2788 -- S for body or spec, with dots replaced by double underscores, and
2789 -- hhhhhhhh is the version number, and nnnnn is a 5-digits serial number.
2791 procedure Gen_Versions_Ada is
2792 Ubuf : String (1 .. 6) := "u00000";
2794 procedure Increment_Ubuf;
2795 -- Little procedure to increment the serial number
2797 procedure Increment_Ubuf is
2799 for J in reverse Ubuf'Range loop
2800 Ubuf (J) := Character'Succ (Ubuf (J));
2801 exit when Ubuf (J) <= '9';
2806 -- Start of processing for Gen_Versions_Ada
2809 if Bind_For_Library then
2811 -- When building libraries, the version number of each unit can
2812 -- not be computed, since the binder does not know the full list
2813 -- of units. Therefore, the 'Version and 'Body_Version
2814 -- attributes cannot supported in this case.
2821 WBI (" type Version_32 is mod 2 ** 32;");
2822 for U in Units.First .. Units.Last loop
2824 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2825 Units.Table (U).Version & "#;");
2831 for U in Units.First .. Units.Last loop
2833 Set_String (" pragma Export (C, ");
2835 Set_String (", """);
2837 Get_Name_String (Units.Table (U).Uname);
2839 for K in 1 .. Name_Len loop
2840 if Name_Buffer (K) = '.' then
2844 elsif Name_Buffer (K) = '%' then
2848 Set_Char (Name_Buffer (K));
2852 if Name_Buffer (Name_Len) = 's' then
2858 Set_String (""");");
2859 Write_Statement_Buffer;
2862 end Gen_Versions_Ada;
2864 --------------------
2865 -- Gen_Versions_C --
2866 --------------------
2868 -- This routine generates a line of the form:
2870 -- unsigned unam = 0xhhhhhhhh;
2872 -- for each unit, where unam is the unit name suffixed by either B or
2873 -- S for body or spec, with dots replaced by double underscores.
2875 procedure Gen_Versions_C is
2877 if Bind_For_Library then
2879 -- When building libraries, the version number of each unit can
2880 -- not be computed, since the binder does not know the full list
2881 -- of units. Therefore, the 'Version and 'Body_Version
2882 -- attributes cannot supported.
2887 for U in Units.First .. Units.Last loop
2888 Set_String ("unsigned ");
2890 Get_Name_String (Units.Table (U).Uname);
2892 for K in 1 .. Name_Len loop
2893 if Name_Buffer (K) = '.' then
2896 elsif Name_Buffer (K) = '%' then
2900 Set_Char (Name_Buffer (K));
2904 if Name_Buffer (Name_Len) = 's' then
2910 Set_String (" = 0x");
2911 Set_String (Units.Table (U).Version);
2913 Write_Statement_Buffer;
2918 -----------------------
2919 -- Get_Ada_Main_Name --
2920 -----------------------
2922 function Get_Ada_Main_Name return String is
2923 Suffix : constant String := "_00";
2924 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2925 Opt.Ada_Main_Name.all & Suffix;
2929 -- The main program generated by JGNAT expects a package called
2930 -- ada_<main procedure>.
2932 if VM_Target /= No_VM then
2934 -- Get main program name
2936 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2940 return "ada_" & Name_Buffer (1 .. Name_Len - 2);
2943 -- This loop tries the following possibilities in order
2949 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2950 -- it is set to 'ada_main'.
2952 for J in 0 .. 99 loop
2954 Nlen := Name'Length - Suffix'Length;
2956 Nlen := Name'Length;
2957 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2958 Name (Name'Last - 1) :=
2959 Character'Val (J / 10 + Character'Pos ('0'));
2962 for K in ALIs.First .. ALIs.Last loop
2963 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2965 -- Get unit name, removing %b or %e at end
2967 Get_Name_String (Units.Table (L).Uname);
2968 Name_Len := Name_Len - 2;
2970 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2976 return Name (1 .. Nlen);
2982 -- If we fall through, just use a peculiar unlikely name
2984 return ("Qwertyuiop");
2985 end Get_Ada_Main_Name;
2991 function Get_Main_Name return String is
2993 -- Explicit name given with -M switch
2995 if Bind_Alternate_Main_Name then
2996 return Alternate_Main_Name.all;
2998 -- Case of main program name to be used directly
3000 elsif Use_Ada_Main_Program_Name_On_Target then
3002 -- Get main program name
3004 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3006 -- If this is a child name, return only the name of the child,
3007 -- since we can't have dots in a nested program name. Note that
3008 -- we do not include the %b at the end of the unit name.
3010 for J in reverse 1 .. Name_Len - 2 loop
3011 if J = 1 or else Name_Buffer (J - 1) = '.' then
3012 return Name_Buffer (J .. Name_Len - 2);
3016 raise Program_Error; -- impossible exit
3018 -- Case where "main" is to be used as default
3025 ----------------------
3026 -- Lt_Linker_Option --
3027 ----------------------
3029 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3031 -- Sort internal files last
3033 if Linker_Options.Table (Op1).Internal_File
3035 Linker_Options.Table (Op2).Internal_File
3037 -- Note: following test uses False < True
3039 return Linker_Options.Table (Op1).Internal_File
3041 Linker_Options.Table (Op2).Internal_File;
3043 -- If both internal or both non-internal, sort according to the
3044 -- elaboration position. A unit that is elaborated later should
3045 -- come earlier in the linker options list.
3048 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3050 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3053 end Lt_Linker_Option;
3055 ------------------------
3056 -- Move_Linker_Option --
3057 ------------------------
3059 procedure Move_Linker_Option (From : Natural; To : Natural) is
3061 Linker_Options.Table (To) := Linker_Options.Table (From);
3062 end Move_Linker_Option;
3064 ----------------------------
3065 -- Resolve_Binder_Options --
3066 ----------------------------
3068 procedure Resolve_Binder_Options is
3070 for E in Elab_Order.First .. Elab_Order.Last loop
3071 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3073 -- The procedure of looking for specific packages and setting
3074 -- flags is somewhat dubious, but there isn't a good alternative
3075 -- at the current time ???
3077 if Name_Buffer (1 .. 19) = "system.os_interface" then
3081 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3082 With_DECGNAT := True;
3085 end Resolve_Binder_Options;
3091 procedure Set_Boolean (B : Boolean) is
3092 True_Str : constant String := "True";
3093 False_Str : constant String := "False";
3096 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3097 Last := Last + True_Str'Length;
3099 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3100 Last := Last + False_Str'Length;
3108 procedure Set_Char (C : Character) is
3111 Statement_Buffer (Last) := C;
3118 procedure Set_Int (N : Int) is
3130 Statement_Buffer (Last) :=
3131 Character'Val (N mod 10 + Character'Pos ('0'));
3135 -------------------------
3136 -- Set_IS_Pragma_Table --
3137 -------------------------
3139 procedure Set_IS_Pragma_Table is
3141 for F in ALIs.First .. ALIs.Last loop
3142 for K in ALIs.Table (F).First_Interrupt_State ..
3143 ALIs.Table (F).Last_Interrupt_State
3146 Inum : constant Int :=
3147 Interrupt_States.Table (K).Interrupt_Id;
3148 Stat : constant Character :=
3149 Interrupt_States.Table (K).Interrupt_State;
3152 while IS_Pragma_Settings.Last < Inum loop
3153 IS_Pragma_Settings.Append ('n');
3156 IS_Pragma_Settings.Table (Inum) := Stat;
3160 end Set_IS_Pragma_Table;
3162 ---------------------------
3163 -- Set_Main_Program_Name --
3164 ---------------------------
3166 procedure Set_Main_Program_Name is
3168 -- Note that name has %b on the end which we ignore
3170 -- First we output the initial _ada_ since we know that the main
3171 -- program is a library level subprogram.
3173 Set_String ("_ada_");
3175 -- Copy name, changing dots to double underscores
3177 for J in 1 .. Name_Len - 2 loop
3178 if Name_Buffer (J) = '.' then
3181 Set_Char (Name_Buffer (J));
3184 end Set_Main_Program_Name;
3186 ---------------------
3187 -- Set_Name_Buffer --
3188 ---------------------
3190 procedure Set_Name_Buffer is
3192 for J in 1 .. Name_Len loop
3193 Set_Char (Name_Buffer (J));
3195 end Set_Name_Buffer;
3197 -------------------------
3198 -- Set_PSD_Pragma_Table --
3199 -------------------------
3201 procedure Set_PSD_Pragma_Table is
3203 for F in ALIs.First .. ALIs.Last loop
3204 for K in ALIs.Table (F).First_Specific_Dispatching ..
3205 ALIs.Table (F).Last_Specific_Dispatching
3208 DTK : Specific_Dispatching_Record
3209 renames Specific_Dispatching.Table (K);
3212 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3213 PSD_Pragma_Settings.Append ('F');
3216 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3217 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3222 end Set_PSD_Pragma_Table;
3228 procedure Set_String (S : String) is
3230 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3231 Last := Last + S'Length;
3238 procedure Set_Unit_Name is
3240 for J in 1 .. Name_Len - 2 loop
3241 if Name_Buffer (J) /= '.' then
3242 Set_Char (Name_Buffer (J));
3249 ---------------------
3250 -- Set_Unit_Number --
3251 ---------------------
3253 procedure Set_Unit_Number (U : Unit_Id) is
3254 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3255 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3258 if Num_Units >= 10 and then Unum < 10 then
3262 if Num_Units >= 100 and then Unum < 100 then
3267 end Set_Unit_Number;
3269 ----------------------
3270 -- Write_Info_Ada_C --
3271 ----------------------
3273 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3275 if Ada_Bind_File then
3277 S : String (1 .. Ada'Length + Common'Length);
3279 S (1 .. Ada'Length) := Ada;
3280 S (Ada'Length + 1 .. S'Length) := Common;
3286 S : String (1 .. C'Length + Common'Length);
3288 S (1 .. C'Length) := C;
3289 S (C'Length + 1 .. S'Length) := Common;
3293 end Write_Info_Ada_C;
3295 ----------------------------
3296 -- Write_Statement_Buffer --
3297 ----------------------------
3299 procedure Write_Statement_Buffer is
3301 WBI (Statement_Buffer (1 .. Last));
3303 end Write_Statement_Buffer;
3305 procedure Write_Statement_Buffer (S : String) is
3308 Write_Statement_Buffer;
3309 end Write_Statement_Buffer;