1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 Lib_Final_Built : Boolean := False;
76 ----------------------------------
77 -- Interface_State Pragma Table --
78 ----------------------------------
80 -- This table assembles the interface state pragma information from
81 -- all the units in the partition. Note that Bcheck has already checked
82 -- that the information is consistent across units. The entries
83 -- in this table are n/u/r/s for not set/user/runtime/system.
85 package IS_Pragma_Settings is new Table.Table (
86 Table_Component_Type => Character,
87 Table_Index_Type => Int,
90 Table_Increment => 200,
91 Table_Name => "IS_Pragma_Settings");
93 -- This table assembles the Priority_Specific_Dispatching pragma
94 -- information from all the units in the partition. Note that Bcheck has
95 -- already checked that the information is consistent across units.
96 -- The entries in this table are the upper case first character of the
97 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
99 package PSD_Pragma_Settings is new Table.Table (
100 Table_Component_Type => Character,
101 Table_Index_Type => Int,
102 Table_Low_Bound => 0,
103 Table_Initial => 100,
104 Table_Increment => 200,
105 Table_Name => "PSD_Pragma_Settings");
107 ----------------------
108 -- Run-Time Globals --
109 ----------------------
111 -- This section documents the global variables that set from the
112 -- generated binder file.
114 -- Main_Priority : Integer;
115 -- Time_Slice_Value : Integer;
116 -- Heap_Size : Natural;
117 -- WC_Encoding : Character;
118 -- Locking_Policy : Character;
119 -- Queuing_Policy : Character;
120 -- Task_Dispatching_Policy : Character;
121 -- Priority_Specific_Dispatching : System.Address;
122 -- Num_Specific_Dispatching : Integer;
123 -- Restrictions : System.Address;
124 -- Interrupt_States : System.Address;
125 -- Num_Interrupt_States : Integer;
126 -- Unreserve_All_Interrupts : Integer;
127 -- Exception_Tracebacks : Integer;
128 -- Zero_Cost_Exceptions : Integer;
129 -- Detect_Blocking : Integer;
130 -- Default_Stack_Size : Integer;
131 -- Leap_Seconds_Support : Integer;
132 -- Main_CPU : Integer;
134 -- Main_Priority is the priority value set by pragma Priority in the main
135 -- program. If no such pragma is present, the value is -1.
137 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
138 -- main program, or by the use of a -Tnnn parameter for the binder (if both
139 -- are present, the binder value overrides). The value is in milliseconds.
140 -- A value of zero indicates that time slicing should be suppressed. If no
141 -- pragma is present, and no -T switch was used, the value is -1.
143 -- Heap_Size is the heap to use for memory allocations set by use of a
144 -- -Hnn parameter for the binder or by the GNAT$NO_MALLOC_64 logical.
145 -- Valid values are 32 and 64. This switch is only effective on VMS.
147 -- WC_Encoding shows the wide character encoding method used for the main
148 -- program. This is one of the encoding letters defined in
149 -- System.WCh_Con.WC_Encoding_Letters.
151 -- Locking_Policy is a space if no locking policy was specified for the
152 -- partition. If a locking policy was specified, the value is the upper
153 -- case first character of the locking policy name, for example, 'C' for
156 -- Queuing_Policy is a space if no queuing policy was specified for the
157 -- partition. If a queuing policy was specified, the value is the upper
158 -- case first character of the queuing policy name for example, 'F' for
161 -- Task_Dispatching_Policy is a space if no task dispatching policy was
162 -- specified for the partition. If a task dispatching policy was specified,
163 -- the value is the upper case first character of the policy name, e.g. 'F'
164 -- for FIFO_Within_Priorities.
166 -- Priority_Specific_Dispatching is the address of a string used to store
167 -- the task dispatching policy specified for the different priorities in
168 -- the partition. The length of this string is determined by the last
169 -- priority for which such a pragma applies (the string will be a null
170 -- string if no specific dispatching policies were used). If pragma were
171 -- present, the entries apply to the priorities in sequence from the first
172 -- priority. The value stored is the upper case first character of the
173 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
174 -- for those priority ranges not specified.
176 -- Num_Specific_Dispatching is the length of the
177 -- Priority_Specific_Dispatching string. It will be set to zero if no
178 -- Priority_Specific_Dispatching pragmas are present.
180 -- Restrictions is the address of a null-terminated string specifying the
181 -- restrictions information for the partition. The format is identical to
182 -- that of the parameter string found on R lines in ali files (see Lib.Writ
183 -- spec in lib-writ.ads for full details). The difference is that in this
184 -- context the values are the cumulative ones for the entire partition.
186 -- Interrupt_States is the address of a string used to specify the
187 -- cumulative results of Interrupt_State pragmas used in the partition.
188 -- The length of this string is determined by the last interrupt for which
189 -- such a pragma is given (the string will be a null string if no pragmas
190 -- were used). If pragma were present the entries apply to the interrupts
191 -- in sequence from the first interrupt, and are set to one of four
192 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
193 -- time, 's' for system, see description of Interrupt_State pragma for
196 -- Num_Interrupt_States is the length of the Interrupt_States string. It
197 -- will be set to zero if no Interrupt_State pragmas are present.
199 -- Unreserve_All_Interrupts is set to one if at least one unit in the
200 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
202 -- Exception_Tracebacks is set to one if the -E parameter was present
203 -- in the bind and to zero otherwise. Note that on some targets exception
204 -- tracebacks are provided by default, so a value of zero for this
205 -- parameter does not necessarily mean no trace backs are available.
207 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
208 -- this partition, and to zero if longjmp/setjmp exceptions are used.
210 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
211 -- not. A value of zero indicates that the pragma is not present, while a
212 -- value of 1 signals its presence in the partition.
214 -- Default_Stack_Size is the default stack size used when creating an Ada
215 -- task with no explicit Storage_Size clause.
217 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
218 -- disabled. A value of zero indicates that leap seconds are turned "off",
219 -- while a value of one signifies "on" status.
221 -- Main_CPU is the processor set by pragma CPU in the main program. If no
222 -- such pragma is present, the value is -1.
224 -----------------------
225 -- Local Subprograms --
226 -----------------------
228 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
229 -- Convenient shorthand used throughout
231 procedure Check_System_Restrictions_Used;
232 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
233 -- System.Restrictions is present in the partition, otherwise False).
235 procedure Gen_Adainit_Ada;
236 -- Generates the Adainit procedure (Ada code case)
238 procedure Gen_Adainit_C;
239 -- Generates the Adainit procedure (C code case)
241 procedure Gen_Adafinal_Ada;
242 -- Generate the Adafinal procedure (Ada code case)
244 procedure Gen_Adafinal_C;
245 -- Generate the Adafinal procedure (C code case)
247 procedure Gen_Elab_Calls_Ada;
248 -- Generate sequence of elaboration calls (Ada code case)
250 procedure Gen_Elab_Calls_C;
251 -- Generate sequence of elaboration calls (C code case)
253 procedure Gen_Elab_Order_Ada;
254 -- Generate comments showing elaboration order chosen (Ada code case)
256 procedure Gen_Elab_Order_C;
257 -- Generate comments showing elaboration order chosen (C code case)
259 procedure Gen_Elab_Defs_C;
260 -- Generate sequence of definitions for elaboration routines (C code case)
262 procedure Gen_Finalize_Library_Ada;
263 -- Generate a sequence of finalization calls to elaborated packages (Ada)
265 procedure Gen_Finalize_Library_C;
266 -- Generate a sequence of finalization calls to elaborated packages (C)
268 procedure Gen_Finalize_Library_Defs_C;
269 -- Generate a sequence of defininitions for package finalizers (C case)
271 procedure Gen_Main_Ada;
272 -- Generate procedure main (Ada code case)
274 procedure Gen_Main_C;
275 -- Generate main() procedure (C code case)
277 procedure Gen_Object_Files_Options;
278 -- Output comments containing a list of the full names of the object
279 -- files to be linked and the list of linker options supplied by
280 -- Linker_Options pragmas in the source. (C and Ada code case)
282 procedure Gen_Output_File_Ada (Filename : String);
283 -- Generate output file (Ada code case)
285 procedure Gen_Output_File_C (Filename : String);
286 -- Generate output file (C code case)
288 procedure Gen_Restrictions_Ada;
289 -- Generate initialization of restrictions variable (Ada code case)
291 procedure Gen_Restrictions_C;
292 -- Generate initialization of restrictions variable (C code case)
294 procedure Gen_Versions_Ada;
295 -- Output series of definitions for unit versions (Ada code case)
297 procedure Gen_Versions_C;
298 -- Output series of definitions for unit versions (C code case)
300 function Get_Ada_Main_Name return String;
301 -- This function is used in the Ada main output case to compute a usable
302 -- name for the generated main program. The normal main program name is
303 -- Ada_Main, but this won't work if the user has a unit with this name.
304 -- This function tries Ada_Main first, and if there is such a clash, then
305 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
307 function Get_Main_Unit_Name (S : String) return String;
308 -- Return the main unit name corresponding to S by replacing '.' with '_'
310 function Get_Main_Name return String;
311 -- This function is used in the Ada main output case to compute the
312 -- correct external main program. It is "main" by default, unless the
313 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
314 -- is the name of the Ada main name without the "_ada". This default
315 -- can be overridden explicitly using the -Mname binder switch.
317 function Get_WC_Encoding return Character;
318 -- Return wide character encoding method to set as WC_Encoding in output.
319 -- If -W has been used, returns the specified encoding, otherwise returns
320 -- the encoding method used for the main program source. If there is no
321 -- main program source (-z switch used), returns brackets ('b').
323 function Has_Finalizer return Boolean;
324 -- Determine whether the current unit has at least one library-level
327 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
328 -- Compare linker options, when sorting, first according to
329 -- Is_Internal_File (internal files come later) and then by
330 -- elaboration order position (latest to earliest).
332 procedure Move_Linker_Option (From : Natural; To : Natural);
333 -- Move routine for sorting linker options
335 procedure Resolve_Binder_Options;
336 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
337 -- since it tests for a package named "dec" which might cause a conflict
338 -- on non-VMS systems.
340 procedure Set_Char (C : Character);
341 -- Set given character in Statement_Buffer at the Last + 1 position
342 -- and increment Last by one to reflect the stored character.
344 procedure Set_Int (N : Int);
345 -- Set given value in decimal in Statement_Buffer with no spaces
346 -- starting at the Last + 1 position, and updating Last past the value.
347 -- A minus sign is output for a negative value.
349 procedure Set_Boolean (B : Boolean);
350 -- Set given boolean value in Statement_Buffer at the Last + 1 position
351 -- and update Last past the value.
353 procedure Set_IS_Pragma_Table;
354 -- Initializes contents of IS_Pragma_Settings table from ALI table
356 procedure Set_Main_Program_Name;
357 -- Given the main program name in Name_Buffer (length in Name_Len)
358 -- generate the name of the routine to be used in the call. The name
359 -- is generated starting at Last + 1, and Last is updated past it.
361 procedure Set_Name_Buffer;
362 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
364 procedure Set_PSD_Pragma_Table;
365 -- Initializes contents of PSD_Pragma_Settings table from ALI table
367 procedure Set_String (S : String);
368 -- Sets characters of given string in Statement_Buffer, starting at the
369 -- Last + 1 position, and updating last past the string value.
371 procedure Set_String_Replace (S : String);
372 -- Replaces the last S'Length characters in the Statement_Buffer with
373 -- the characters of S. The caller must ensure that these characters do
374 -- in fact exist in the Statement_Buffer.
376 type Qualification_Mode is (Dollar_Sign, Dot, Double_Underscores);
378 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores);
379 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
380 -- starting at the Last + 1 position and update Last past the value.
381 -- Depending on parameter Mode, a dot (.) can be qualified into double
382 -- underscores (__), a dollar sign ($) or left as is.
384 procedure Set_Unit_Number (U : Unit_Id);
385 -- Sets unit number (first unit is 1, leading zeroes output to line
386 -- up all output unit numbers nicely as required by the value, and
387 -- by the total number of units.
389 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
390 -- For C code case, write C & Common, for Ada case write Ada & Common
391 -- to current binder output file using Write_Binder_Info.
393 procedure Write_Statement_Buffer;
394 -- Write out contents of statement buffer up to Last, and reset Last to 0
396 procedure Write_Statement_Buffer (S : String);
397 -- First writes its argument (using Set_String (S)), then writes out the
398 -- contents of statement buffer up to Last, and reset Last to 0
400 ------------------------------------
401 -- Check_System_Restrictions_Used --
402 ------------------------------------
404 procedure Check_System_Restrictions_Used is
406 for J in Units.First .. Units.Last loop
407 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
408 System_Restrictions_Used := True;
413 System_Restrictions_Used := False;
414 end Check_System_Restrictions_Used;
416 ----------------------
417 -- Gen_Adafinal_Ada --
418 ----------------------
420 procedure Gen_Adafinal_Ada is
422 WBI (" procedure " & Ada_Final_Name.all & " is");
424 -- Do nothing if finalization is disabled
426 if Cumulative_Restrictions.Set (No_Finalization) then
432 elsif VM_Target = No_VM then
433 WBI (" procedure s_stalib_adafinal;");
434 WBI (" pragma Import (C, s_stalib_adafinal, " &
435 """system__standard_library__adafinal"");");
437 WBI (" s_stalib_adafinal;");
439 -- Pragma Import C cannot be used on virtual machine targets, therefore
440 -- call the runtime finalization routine directly.
444 WBI (" System.Standard_Library.Adafinal;");
447 WBI (" end " & Ada_Final_Name.all & ";");
449 end Gen_Adafinal_Ada;
455 procedure Gen_Adafinal_C is
457 WBI ("void " & Ada_Final_Name.all & " (void) {");
458 WBI (" system__standard_library__adafinal ();");
463 ---------------------
464 -- Gen_Adainit_Ada --
465 ---------------------
467 procedure Gen_Adainit_Ada is
468 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
469 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
472 WBI (" procedure " & Ada_Init_Name.all & " is");
474 -- Generate externals for elaboration entities
476 for E in Elab_Order.First .. Elab_Order.Last loop
478 Unum : constant Unit_Id := Elab_Order.Table (E);
479 U : Unit_Record renames Units.Table (Unum);
482 -- Check for Elab_Entity to be set for this unit
486 -- Don't generate reference for stand alone library
488 and then not U.SAL_Interface
490 -- Don't generate reference for predefined file in No_Run_Time
491 -- mode, since we don't include the object files in this case
495 and then Is_Predefined_File_Name (U.Sfile))
499 Set_Unit_Number (Unum);
502 when No_VM | JVM_Target =>
503 Set_String (" : Boolean; pragma Import (Ada, ");
505 Set_String (" : Boolean; pragma Import (CIL, ");
509 Set_Unit_Number (Unum);
511 Get_Name_String (U.Uname);
513 -- In the case of JGNAT we need to emit an Import name that
514 -- includes the class name (using '$' separators in the case
515 -- of a child unit name).
517 if VM_Target /= No_VM then
518 for J in 1 .. Name_Len - 2 loop
519 if VM_Target = CLI_Target
520 or else Name_Buffer (J) /= '.'
522 Set_Char (Name_Buffer (J));
528 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
531 Set_String ("_pkg.");
534 -- If the unit name is very long, then split the
535 -- Import link name across lines using "&" (occurs
536 -- in some C2 tests).
538 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
540 Write_Statement_Buffer;
546 Set_String ("_E"");");
547 Write_Statement_Buffer;
552 Write_Statement_Buffer;
554 -- If the standard library is suppressed, then the only global variables
555 -- that might be needed (by the Ravenscar profile) are the priority and
556 -- the processor for the environment task.
558 if Suppress_Standard_Library_On_Target then
559 if Main_Priority /= No_Main_Priority then
560 WBI (" Main_Priority : Integer;");
561 WBI (" pragma Import (C, Main_Priority," &
562 " ""__gl_main_priority"");");
566 if Main_CPU /= No_Main_CPU then
567 WBI (" Main_CPU : Integer;");
568 WBI (" pragma Import (C, Main_CPU," &
569 " ""__gl_main_cpu"");");
575 if Main_Priority /= No_Main_Priority then
576 Set_String (" Main_Priority := ");
577 Set_Int (Main_Priority);
579 Write_Statement_Buffer;
582 if Main_CPU /= No_Main_CPU then
583 Set_String (" Main_CPU := ");
586 Write_Statement_Buffer;
589 if Main_Priority = No_Main_Priority
590 and then Main_CPU = No_Main_CPU
595 -- Normal case (standard library not suppressed). Set all global values
596 -- used by the run time.
599 WBI (" Main_Priority : Integer;");
600 WBI (" pragma Import (C, Main_Priority, " &
601 """__gl_main_priority"");");
602 WBI (" Time_Slice_Value : Integer;");
603 WBI (" pragma Import (C, Time_Slice_Value, " &
604 """__gl_time_slice_val"");");
605 WBI (" WC_Encoding : Character;");
606 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
607 WBI (" Locking_Policy : Character;");
608 WBI (" pragma Import (C, Locking_Policy, " &
609 """__gl_locking_policy"");");
610 WBI (" Queuing_Policy : Character;");
611 WBI (" pragma Import (C, Queuing_Policy, " &
612 """__gl_queuing_policy"");");
613 WBI (" Task_Dispatching_Policy : Character;");
614 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
615 """__gl_task_dispatching_policy"");");
616 WBI (" Priority_Specific_Dispatching : System.Address;");
617 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
618 """__gl_priority_specific_dispatching"");");
619 WBI (" Num_Specific_Dispatching : Integer;");
620 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
621 """__gl_num_specific_dispatching"");");
622 WBI (" Main_CPU : Integer;");
623 WBI (" pragma Import (C, Main_CPU, " &
624 """__gl_main_cpu"");");
626 WBI (" Interrupt_States : System.Address;");
627 WBI (" pragma Import (C, Interrupt_States, " &
628 """__gl_interrupt_states"");");
629 WBI (" Num_Interrupt_States : Integer;");
630 WBI (" pragma Import (C, Num_Interrupt_States, " &
631 """__gl_num_interrupt_states"");");
632 WBI (" Unreserve_All_Interrupts : Integer;");
633 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
634 """__gl_unreserve_all_interrupts"");");
636 if Exception_Tracebacks then
637 WBI (" Exception_Tracebacks : Integer;");
638 WBI (" pragma Import (C, Exception_Tracebacks, " &
639 """__gl_exception_tracebacks"");");
642 WBI (" Zero_Cost_Exceptions : Integer;");
643 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
644 """__gl_zero_cost_exceptions"");");
645 WBI (" Detect_Blocking : Integer;");
646 WBI (" pragma Import (C, Detect_Blocking, " &
647 """__gl_detect_blocking"");");
648 WBI (" Default_Stack_Size : Integer;");
649 WBI (" pragma Import (C, Default_Stack_Size, " &
650 """__gl_default_stack_size"");");
651 WBI (" Leap_Seconds_Support : Integer;");
652 WBI (" pragma Import (C, Leap_Seconds_Support, " &
653 """__gl_leap_seconds_support"");");
655 -- Import entry point for elaboration time signal handler
656 -- installation, and indication of if it's been called previously.
659 WBI (" procedure Install_Handler;");
660 WBI (" pragma Import (C, Install_Handler, " &
661 """__gnat_install_handler"");");
663 WBI (" Handler_Installed : Integer;");
664 WBI (" pragma Import (C, Handler_Installed, " &
665 """__gnat_handler_installed"");");
667 -- The import of the soft link which performs library-level object
668 -- finalization is not needed for VM targets; regular Ada is used in
669 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
670 -- tasks are non-terminating, so we do not want finalization.
672 if VM_Target = No_VM and then not Configurable_Run_Time_On_Target then
674 WBI (" type No_Param_Proc is access procedure;");
675 WBI (" Finalize_Library_Objects : No_Param_Proc;");
676 WBI (" pragma Import (C, Finalize_Library_Objects, " &
677 """__gnat_finalize_library_objects"");");
680 -- Import entry point for environment feature enable/disable
681 -- routine, and indication that it's been called previously.
683 if OpenVMS_On_Target then
685 WBI (" procedure Set_Features;");
686 WBI (" pragma Import (C, Set_Features, " &
687 """__gnat_set_features"");");
689 WBI (" Features_Set : Integer;");
690 WBI (" pragma Import (C, Features_Set, " &
691 """__gnat_features_set"");");
693 if Opt.Heap_Size /= 0 then
695 WBI (" Heap_Size : Integer;");
696 WBI (" pragma Import (C, Heap_Size, " &
697 """__gl_heap_size"");");
699 Write_Statement_Buffer;
703 -- Initialize stack limit variable of the environment task if the
704 -- stack check method is stack limit and stack check is enabled.
706 if Stack_Check_Limits_On_Target
707 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
710 WBI (" procedure Initialize_Stack_Limit;");
711 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
712 """__gnat_initialize_stack_limit"");");
715 -- Special processing when main program is CIL function/procedure
717 if VM_Target = CLI_Target
718 and then Bind_Main_Program
719 and then not No_Main_Subprogram
723 -- Function case, use Set_Exit_Status to report the returned
724 -- status code, since that is the only mechanism available.
726 if ALIs.Table (ALIs.First).Main_Program = Func then
727 WBI (" Result : Integer;");
728 WBI (" procedure Set_Exit_Status (Code : Integer);");
729 WBI (" pragma Import (C, Set_Exit_Status, " &
730 """__gnat_set_exit_status"");");
732 WBI (" function Ada_Main_Program return Integer;");
737 WBI (" procedure Ada_Main_Program;");
740 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
741 Name_Len := Name_Len - 2;
742 WBI (" pragma Import (CIL, Ada_Main_Program, """
743 & Name_Buffer (1 .. Name_Len) & "."
744 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
749 Set_String (" Main_Priority := ");
750 Set_Int (Main_Priority);
752 Write_Statement_Buffer;
754 Set_String (" Time_Slice_Value := ");
756 if Task_Dispatching_Policy_Specified = 'F'
757 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
761 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
765 Write_Statement_Buffer;
767 Set_String (" WC_Encoding := '");
768 Set_Char (Get_WC_Encoding);
771 Write_Statement_Buffer;
773 Set_String (" Locking_Policy := '");
774 Set_Char (Locking_Policy_Specified);
776 Write_Statement_Buffer;
778 Set_String (" Queuing_Policy := '");
779 Set_Char (Queuing_Policy_Specified);
781 Write_Statement_Buffer;
783 Set_String (" Task_Dispatching_Policy := '");
784 Set_Char (Task_Dispatching_Policy_Specified);
786 Write_Statement_Buffer;
788 Gen_Restrictions_Ada;
790 WBI (" Priority_Specific_Dispatching :=");
791 WBI (" Local_Priority_Specific_Dispatching'Address;");
793 Set_String (" Num_Specific_Dispatching := ");
794 Set_Int (PSD_Pragma_Settings.Last + 1);
796 Write_Statement_Buffer;
798 Set_String (" Main_CPU := ");
801 Write_Statement_Buffer;
803 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
805 Set_String (" Num_Interrupt_States := ");
806 Set_Int (IS_Pragma_Settings.Last + 1);
808 Write_Statement_Buffer;
810 Set_String (" Unreserve_All_Interrupts := ");
812 if Unreserve_All_Interrupts_Specified then
819 Write_Statement_Buffer;
821 if Exception_Tracebacks then
822 WBI (" Exception_Tracebacks := 1;");
825 Set_String (" Zero_Cost_Exceptions := ");
827 if Zero_Cost_Exceptions_Specified then
834 Write_Statement_Buffer;
836 Set_String (" Detect_Blocking := ");
838 if Detect_Blocking then
845 Write_Statement_Buffer;
847 Set_String (" Default_Stack_Size := ");
848 Set_Int (Default_Stack_Size);
850 Write_Statement_Buffer;
852 Set_String (" Leap_Seconds_Support := ");
854 if Leap_Seconds_Support then
861 Write_Statement_Buffer;
863 -- Generate call to Install_Handler
865 -- In .NET, when binding with -z, we don't install the signal handler
866 -- to let the caller handle the last exception handler.
868 if VM_Target /= CLI_Target
869 or else Bind_Main_Program
872 WBI (" if Handler_Installed = 0 then");
873 WBI (" Install_Handler;");
877 -- Generate call to Set_Features
879 if OpenVMS_On_Target then
881 WBI (" if Features_Set = 0 then");
882 WBI (" Set_Features;");
885 -- Features_Set may twiddle the heap size according to a logical
886 -- name, but the binder switch must override.
888 if Opt.Heap_Size /= 0 then
889 Set_String (" Heap_Size := ");
890 Set_Int (Opt.Heap_Size);
892 Write_Statement_Buffer;
897 -- Generate call to set Initialize_Scalar values if active
899 if Initialize_Scalars_Used then
901 Set_String (" System.Scalar_Values.Initialize ('");
902 Set_Char (Initialize_Scalars_Mode1);
904 Set_Char (Initialize_Scalars_Mode2);
906 Write_Statement_Buffer;
909 -- Generate assignment of default secondary stack size if set
911 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
913 Set_String (" System.Secondary_Stack.");
914 Set_String ("Default_Secondary_Stack_Size := ");
915 Set_Int (Opt.Default_Sec_Stack_Size);
917 Write_Statement_Buffer;
920 -- Initialize stack limit variable of the environment task if the
921 -- stack check method is stack limit and stack check is enabled.
923 if Stack_Check_Limits_On_Target
924 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
927 WBI (" Initialize_Stack_Limit;");
930 -- Attach Finalize_Library to the right soft link. Do it only when not
931 -- using a restricted run time, in which case tasks are
932 -- non-terminating, so we do not want library-level finalization.
934 if not Configurable_Run_Time_On_Target then
935 if not Suppress_Standard_Library_On_Target then
938 if VM_Target = No_VM then
939 if Lib_Final_Built then
940 Set_String (" Finalize_Library_Objects := ");
941 Set_String ("Finalize_Library'access;");
943 Set_String (" Finalize_Library_Objects := null;");
946 -- On VM targets use regular Ada to set the soft link
949 if Lib_Final_Built then
951 (" System.Soft_Links.Finalize_Library_Objects");
952 Set_String (" := Finalize_Library'access;");
955 (" System.Soft_Links.Finalize_Library_Objects");
956 Set_String (" := null;");
960 Write_Statement_Buffer;
964 -- Generate elaboration calls
969 -- Case of main program is CIL function or procedure
971 if VM_Target = CLI_Target
972 and then Bind_Main_Program
973 and then not No_Main_Subprogram
975 -- For function case, use Set_Exit_Status to set result
977 if ALIs.Table (ALIs.First).Main_Program = Func then
978 WBI (" Result := Ada_Main_Program;");
979 WBI (" Set_Exit_Status (Result);");
984 WBI (" Ada_Main_Program;");
988 WBI (" end " & Ada_Init_Name.all & ";");
996 procedure Gen_Adainit_C is
997 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
998 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
1001 WBI ("void " & Ada_Init_Name.all & " (void)");
1004 -- Generate externals for elaboration entities
1006 for E in Elab_Order.First .. Elab_Order.Last loop
1008 Unum : constant Unit_Id := Elab_Order.Table (E);
1009 U : Unit_Record renames Units.Table (Unum);
1012 -- Check for Elab entity to be set for this unit
1014 if U.Set_Elab_Entity
1016 -- Don't generate reference for stand alone library
1018 and then not U.SAL_Interface
1020 -- Don't generate reference for predefined file in No_Run_Time
1021 -- mode, since we don't include the object files in this case
1025 and then Is_Predefined_File_Name (U.Sfile))
1027 Set_String (" extern char ");
1028 Get_Name_String (U.Uname);
1031 Write_Statement_Buffer;
1036 Write_Statement_Buffer;
1038 -- Standard library suppressed
1040 if Suppress_Standard_Library_On_Target then
1042 -- Case of High_Integrity_Mode mode. Set __gl_main_priority and
1043 -- __gl_main_cpu if needed for the Ravenscar profile.
1045 if Main_Priority /= No_Main_Priority then
1046 WBI (" extern int __gl_main_priority;");
1047 Set_String (" __gl_main_priority = ");
1048 Set_Int (Main_Priority);
1050 Write_Statement_Buffer;
1053 if Main_CPU /= No_Main_CPU then
1054 WBI (" extern int __gl_main_cpu;");
1055 Set_String (" __gl_main_cpu = ");
1058 Write_Statement_Buffer;
1061 -- Normal case (standard library not suppressed)
1064 -- Generate definition for interrupt states string
1066 Set_String (" static const char *local_interrupt_states = """);
1068 for J in 0 .. IS_Pragma_Settings.Last loop
1069 Set_Char (IS_Pragma_Settings.Table (J));
1073 Write_Statement_Buffer;
1075 -- Generate definition for priority specific dispatching string
1078 (" static const char *local_priority_specific_dispatching = """);
1080 for J in 0 .. PSD_Pragma_Settings.Last loop
1081 Set_Char (PSD_Pragma_Settings.Table (J));
1085 Write_Statement_Buffer;
1087 -- Generate declaration for secondary stack default if needed
1089 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1090 WBI (" extern int system__secondary_stack__" &
1091 "default_secondary_stack_size;");
1096 -- Code for normal case (standard library not suppressed)
1098 -- We call the routine from inside adainit() because this works for
1099 -- both programs with and without binder generated "main" functions.
1101 WBI (" extern int __gl_main_priority;");
1102 Set_String (" __gl_main_priority = ");
1103 Set_Int (Main_Priority);
1105 Write_Statement_Buffer;
1107 WBI (" extern int __gl_time_slice_val;");
1108 Set_String (" __gl_time_slice_val = ");
1110 if Task_Dispatching_Policy = 'F'
1111 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
1115 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
1119 Write_Statement_Buffer;
1121 WBI (" extern char __gl_wc_encoding;");
1122 Set_String (" __gl_wc_encoding = '");
1123 Set_Char (Get_WC_Encoding);
1126 Write_Statement_Buffer;
1128 WBI (" extern char __gl_locking_policy;");
1129 Set_String (" __gl_locking_policy = '");
1130 Set_Char (Locking_Policy_Specified);
1132 Write_Statement_Buffer;
1134 WBI (" extern char __gl_queuing_policy;");
1135 Set_String (" __gl_queuing_policy = '");
1136 Set_Char (Queuing_Policy_Specified);
1138 Write_Statement_Buffer;
1140 WBI (" extern char __gl_task_dispatching_policy;");
1141 Set_String (" __gl_task_dispatching_policy = '");
1142 Set_Char (Task_Dispatching_Policy_Specified);
1144 Write_Statement_Buffer;
1146 WBI (" extern int __gl_main_cpu;");
1147 Set_String (" __gl_main_cpu = ");
1150 Write_Statement_Buffer;
1154 WBI (" extern const void *__gl_interrupt_states;");
1155 WBI (" __gl_interrupt_states = local_interrupt_states;");
1157 WBI (" extern int __gl_num_interrupt_states;");
1158 Set_String (" __gl_num_interrupt_states = ");
1159 Set_Int (IS_Pragma_Settings.Last + 1);
1161 Write_Statement_Buffer;
1163 WBI (" extern const void *__gl_priority_specific_dispatching;");
1164 WBI (" __gl_priority_specific_dispatching =" &
1165 " local_priority_specific_dispatching;");
1167 WBI (" extern int __gl_num_specific_dispatching;");
1168 Set_String (" __gl_num_specific_dispatching = ");
1169 Set_Int (PSD_Pragma_Settings.Last + 1);
1171 Write_Statement_Buffer;
1173 WBI (" extern int __gl_unreserve_all_interrupts;");
1174 Set_String (" __gl_unreserve_all_interrupts = ");
1175 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
1177 Write_Statement_Buffer;
1179 if Exception_Tracebacks then
1180 WBI (" extern int __gl_exception_tracebacks;");
1181 WBI (" __gl_exception_tracebacks = 1;");
1184 WBI (" extern int __gl_zero_cost_exceptions;");
1185 Set_String (" __gl_zero_cost_exceptions = ");
1186 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
1188 Write_Statement_Buffer;
1190 WBI (" extern int __gl_detect_blocking;");
1191 Set_String (" __gl_detect_blocking = ");
1193 if Detect_Blocking then
1200 Write_Statement_Buffer;
1202 WBI (" extern int __gl_default_stack_size;");
1203 Set_String (" __gl_default_stack_size = ");
1204 Set_Int (Default_Stack_Size);
1206 Write_Statement_Buffer;
1208 WBI (" extern int __gl_leap_seconds_support;");
1209 Set_String (" __gl_leap_seconds_support = ");
1211 if Leap_Seconds_Support then
1218 Write_Statement_Buffer;
1222 -- Install elaboration time signal handler
1224 WBI (" if (__gnat_handler_installed == 0)");
1226 WBI (" __gnat_install_handler ();");
1229 -- Call feature enable/disable routine
1231 if OpenVMS_On_Target then
1232 WBI (" if (__gnat_features_set == 0)");
1234 WBI (" __gnat_set_features ();");
1239 -- Initialize stack limit for the environment task if the stack
1240 -- check method is stack limit and stack check is enabled.
1242 if Stack_Check_Limits_On_Target
1243 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1246 WBI (" __gnat_initialize_stack_limit ();");
1249 -- Generate call to set Initialize_Scalar values if needed
1251 if Initialize_Scalars_Used then
1253 Set_String (" system__scalar_values__initialize('");
1254 Set_Char (Initialize_Scalars_Mode1);
1255 Set_String ("', '");
1256 Set_Char (Initialize_Scalars_Mode2);
1258 Write_Statement_Buffer;
1261 -- Generate assignment of default secondary stack size if set
1263 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1265 Set_String (" system__secondary_stack__");
1266 Set_String ("default_secondary_stack_size = ");
1267 Set_Int (Opt.Default_Sec_Stack_Size);
1269 Write_Statement_Buffer;
1272 -- Generate elaboration calls
1280 ------------------------
1281 -- Gen_Elab_Calls_Ada --
1282 ------------------------
1284 procedure Gen_Elab_Calls_Ada is
1286 for E in Elab_Order.First .. Elab_Order.Last loop
1288 Unum : constant Unit_Id := Elab_Order.Table (E);
1289 U : Unit_Record renames Units.Table (Unum);
1291 Unum_Spec : Unit_Id;
1292 -- This is the unit number of the spec that corresponds to
1293 -- this entry. It is the same as Unum except when the body
1294 -- and spec are different and we are currently processing
1295 -- the body, in which case it is the spec (Unum + 1).
1298 if U.Utype = Is_Body then
1299 Unum_Spec := Unum + 1;
1304 -- Nothing to do if predefined unit in no run time mode
1306 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1309 -- Case of no elaboration code
1311 elsif U.No_Elab then
1313 -- The only case in which we have to do something is if
1314 -- this is a body, with a separate spec, where the separate
1315 -- spec has an elaboration entity defined.
1317 -- In that case, this is where we set the elaboration entity
1318 -- to True, we do not need to test if this has already been
1319 -- done, since it is quicker to set the flag than to test it.
1321 if not U.SAL_Interface and then U.Utype = Is_Body
1322 and then Units.Table (Unum_Spec).Set_Elab_Entity
1325 Set_Unit_Number (Unum_Spec);
1326 Set_String (" := True;");
1327 Write_Statement_Buffer;
1330 -- Here if elaboration code is present. If binding a library
1331 -- or if there is a non-Ada main subprogram then we generate:
1333 -- if not uname_E then
1334 -- uname'elab_[spec|body];
1338 -- Otherwise, elaboration routines are called unconditionally:
1340 -- uname'elab_[spec|body];
1343 -- The uname_E assignment is skipped if this is a separate spec,
1344 -- since the assignment will be done when we process the body.
1346 elsif not U.SAL_Interface then
1347 if Force_Checking_Of_Elaboration_Flags or
1348 Interface_Library_Unit or
1349 (not Bind_Main_Program)
1351 Set_String (" if not E");
1352 Set_Unit_Number (Unum_Spec);
1353 Set_String (" then");
1354 Write_Statement_Buffer;
1359 Get_Decoded_Name_String_With_Brackets (U.Uname);
1361 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1362 if Name_Buffer (Name_Len) = 's' then
1363 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1366 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1370 Name_Len := Name_Len + 12;
1373 if Name_Buffer (Name_Len) = 's' then
1374 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1377 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1381 Name_Len := Name_Len + 8;
1384 Set_Casing (U.Icasing);
1387 Write_Statement_Buffer;
1389 if U.Utype /= Is_Spec then
1390 if Force_Checking_Of_Elaboration_Flags or
1391 Interface_Library_Unit or
1392 (not Bind_Main_Program)
1398 Set_Unit_Number (Unum_Spec);
1399 Set_String (" := True;");
1400 Write_Statement_Buffer;
1403 if Force_Checking_Of_Elaboration_Flags or
1404 Interface_Library_Unit or
1405 (not Bind_Main_Program)
1412 end Gen_Elab_Calls_Ada;
1414 ----------------------
1415 -- Gen_Elab_Calls_C --
1416 ----------------------
1418 procedure Gen_Elab_Calls_C is
1420 for E in Elab_Order.First .. Elab_Order.Last loop
1422 Unum : constant Unit_Id := Elab_Order.Table (E);
1423 U : Unit_Record renames Units.Table (Unum);
1425 Unum_Spec : Unit_Id;
1426 -- This is the unit number of the spec that corresponds to
1427 -- this entry. It is the same as Unum except when the body
1428 -- and spec are different and we are currently processing
1429 -- the body, in which case it is the spec (Unum + 1).
1432 if U.Utype = Is_Body then
1433 Unum_Spec := Unum + 1;
1438 -- Nothing to do if predefined unit in no run time mode
1440 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1443 -- Case of no elaboration code
1445 elsif U.No_Elab then
1447 -- The only case in which we have to do something is if
1448 -- this is a body, with a separate spec, where the separate
1449 -- spec has an elaboration entity defined.
1451 -- In that case, this is where we set the elaboration entity
1452 -- to True, we do not need to test if this has already been
1453 -- done, since it is quicker to set the flag than to test it.
1455 if not U.SAL_Interface and then U.Utype = Is_Body
1456 and then Units.Table (Unum_Spec).Set_Elab_Entity
1459 Get_Name_String (U.Uname);
1461 Set_String ("_E = 1;");
1462 Write_Statement_Buffer;
1465 -- Here if elaboration code is present. If binding a library
1466 -- or if there is a non-Ada main subprogram then we generate:
1468 -- if (uname_E == 0) {
1469 -- uname__elab[s|b] ();
1473 -- The uname_E assignment is skipped if this is a separate spec,
1474 -- since the assignment will be done when we process the body.
1476 elsif not U.SAL_Interface then
1477 Get_Name_String (U.Uname);
1479 if Force_Checking_Of_Elaboration_Flags or
1480 Interface_Library_Unit or
1481 (not Bind_Main_Program)
1483 Set_String (" if (");
1485 Set_String ("_E == 0) {");
1486 Write_Statement_Buffer;
1492 Set_String ("___elab");
1493 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1494 Set_String (" ();");
1495 Write_Statement_Buffer;
1497 if U.Utype /= Is_Spec then
1498 if Force_Checking_Of_Elaboration_Flags or
1499 Interface_Library_Unit or
1500 (not Bind_Main_Program)
1507 Set_String ("_E++;");
1508 Write_Statement_Buffer;
1511 if Force_Checking_Of_Elaboration_Flags or
1512 Interface_Library_Unit or
1513 (not Bind_Main_Program)
1520 end Gen_Elab_Calls_C;
1522 ----------------------
1523 -- Gen_Elab_Defs_C --
1524 ----------------------
1526 procedure Gen_Elab_Defs_C is
1528 WBI ("/* BEGIN ELABORATION DEFINITIONS */");
1530 for E in Elab_Order.First .. Elab_Order.Last loop
1532 -- Generate declaration of elaboration procedure if elaboration
1533 -- needed. Note that passive units are always excluded.
1535 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1536 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1537 Set_String ("extern void ");
1539 Set_String ("___elab");
1540 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1541 Set_String (" (void);");
1542 Write_Statement_Buffer;
1546 end Gen_Elab_Defs_C;
1548 ------------------------
1549 -- Gen_Elab_Order_Ada --
1550 ------------------------
1552 procedure Gen_Elab_Order_Ada is
1554 WBI (" -- BEGIN ELABORATION ORDER");
1556 for J in Elab_Order.First .. Elab_Order.Last loop
1557 Set_String (" -- ");
1558 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1560 Write_Statement_Buffer;
1563 WBI (" -- END ELABORATION ORDER");
1565 end Gen_Elab_Order_Ada;
1567 ----------------------
1568 -- Gen_Elab_Order_C --
1569 ----------------------
1571 procedure Gen_Elab_Order_C is
1573 WBI ("/* BEGIN ELABORATION ORDER");
1575 for J in Elab_Order.First .. Elab_Order.Last loop
1576 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1578 Write_Statement_Buffer;
1581 WBI (" END ELABORATION ORDER */");
1583 end Gen_Elab_Order_C;
1585 ------------------------------
1586 -- Gen_Finalize_Library_Ada --
1587 ------------------------------
1589 procedure Gen_Finalize_Library_Ada is
1592 Uspec : Unit_Record;
1596 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1597 Unum := Elab_Order.Table (E);
1598 U := Units.Table (Unum);
1600 -- We are only interested in non-generic packages
1602 if U.Unit_Kind = 'p'
1603 and then U.Has_Finalizer
1604 and then not U.Is_Generic
1605 and then not U.No_Elab
1607 if not Lib_Final_Built then
1608 Lib_Final_Built := True;
1610 WBI (" procedure Finalize_Library is");
1612 -- The following flag is used to check for library-level
1613 -- exceptions raised during finalization. The symbol comes
1614 -- from System.Soft_Links. VM targets use regular Ada to
1615 -- reference the entity.
1617 if VM_Target = No_VM then
1618 WBI (" LE_Set : Boolean;");
1620 Set_String (" pragma Import (Ada, LE_Set, ");
1621 Set_String ("""__gnat_library_exception_set"");");
1622 Write_Statement_Buffer;
1630 -- procedure F<Count>;
1632 Set_String (" declare");
1633 Write_Statement_Buffer;
1635 Set_String (" procedure F");
1638 Write_Statement_Buffer;
1641 -- pragma Import (CIL, F<Count>, "xx.yy_pkg.Finalize[B/S]");
1642 -- -- for .NET targets
1644 -- pragma Import (Java, F<Count>, "xx$yy.Finalize[B/S]");
1645 -- -- for JVM targets
1647 -- pragma Import (Ada, F<Count>, "xx__yy__Finalize[B/S]");
1648 -- -- for default targets
1650 if VM_Target = CLI_Target then
1651 Set_String (" pragma Import (CIL, F");
1652 elsif VM_Target = JVM_Target then
1653 Set_String (" pragma Import (Java, F");
1655 Set_String (" pragma Import (Ada, F");
1659 Set_String (", """);
1661 -- Dealing with package bodies is a little complicated. In such
1662 -- cases we must retrieve the package spec since it contains the
1663 -- spec of the body finalizer.
1665 if U.Utype = Is_Body then
1667 Uspec := Units.Table (Unum);
1672 Get_Name_String (Uspec.Uname);
1674 -- Perform name construction
1676 -- .NET xx.yy_pkg.finalize
1678 if VM_Target = CLI_Target then
1679 Set_Unit_Name (Mode => Dot);
1680 Set_String ("_pkg.finalize");
1682 -- JVM xx$yy.finalize
1684 elsif VM_Target = JVM_Target then
1685 Set_Unit_Name (Mode => Dollar_Sign);
1686 Set_String (".finalize");
1688 -- Default xx__yy__finalize
1692 Set_String ("__finalize");
1695 -- Package spec processing
1697 if U.Utype = Is_Spec
1698 or else U.Utype = Is_Spec_Only
1702 -- Package body processing
1708 Set_String (""");");
1709 Write_Statement_Buffer;
1720 Write_Statement_Buffer;
1727 if Lib_Final_Built then
1729 -- It is possible that the finalization of a library-level object
1730 -- raised an exception. In that case import the actual exception
1731 -- and the routine necessary to raise it.
1733 if VM_Target = No_VM then
1734 WBI (" if LE_Set then");
1736 WBI (" LE : Ada.Exceptions.Exception_Occurrence;");
1738 Set_String (" pragma Import (Ada, LE, ");
1739 Set_String ("""__gnat_library_exception"");");
1740 Write_Statement_Buffer;
1742 Set_String (" procedure Raise_Controlled ");
1743 Set_String ("(E : Ada.Exceptions.Exception_Occurrence);");
1744 Write_Statement_Buffer;
1746 Set_String (" pragma Import (Ada, Raise_Controlled, ");
1747 Set_String ("""__gnat_raise_from_controlled_operation"");");
1748 Write_Statement_Buffer;
1751 WBI (" Raise_Controlled (LE);");
1754 -- VM-specific code, use regular Ada to produce the desired behavior
1757 WBI (" if System.Soft_Links.Library_Exception_Set then");
1759 Set_String (" Ada.Exceptions.Reraise_Occurrence (");
1760 Set_String ("System.Soft_Links.Library_Exception);");
1761 Write_Statement_Buffer;
1765 WBI (" end Finalize_Library;");
1768 end Gen_Finalize_Library_Ada;
1770 ----------------------------
1771 -- Gen_Finalize_Library_C --
1772 ----------------------------
1774 procedure Gen_Finalize_Library_C is
1776 Uspec : Unit_Record;
1780 WBI (" /* BEGIN FINALIZE */");
1782 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1783 Unum := Elab_Order.Table (E);
1784 U := Units.Table (Unum);
1786 -- We are only interested in non-generic packages
1788 if U.Unit_Kind = 'p'
1789 and then U.Has_Finalizer
1790 and then not U.Is_Generic
1791 and then not U.No_Elab
1795 -- Dealing with package bodies is a little complicated. In such
1796 -- cases we must retrieve the package spec since it contains the
1797 -- spec of the body finalizer.
1799 if U.Utype = Is_Body then
1801 Uspec := Units.Table (Unum);
1806 Get_Name_String (Uspec.Uname);
1808 Set_String ("__finalize");
1810 -- Package spec processing
1812 if U.Utype = Is_Spec
1813 or else U.Utype = Is_Spec_Only
1817 -- Package body processing
1823 Set_String (" ();");
1825 Write_Statement_Buffer;
1829 WBI (" /* END FINALIZE */");
1831 end Gen_Finalize_Library_C;
1833 ---------------------------------
1834 -- Gen_Finalize_Library_Defs_C --
1835 ---------------------------------
1837 procedure Gen_Finalize_Library_Defs_C is
1839 Uspec : Unit_Record;
1843 WBI ("/* BEGIN FINALIZE DEFINITIONS */");
1845 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1846 Unum := Elab_Order.Table (E);
1847 U := Units.Table (Unum);
1849 -- We are only interested in non-generic packages
1851 if U.Unit_Kind = 'p'
1852 and then U.Has_Finalizer
1853 and then not U.Is_Generic
1854 and then not U.No_Elab
1856 -- Dealing with package bodies is a little complicated. In such
1857 -- cases we must retrieve the package spec since it contains the
1858 -- spec of the body finalizer.
1860 if U.Utype = Is_Body then
1862 Uspec := Units.Table (Unum);
1867 Set_String ("extern void ");
1868 Get_Name_String (Uspec.Uname);
1870 Set_String ("__finalize");
1872 if U.Utype = Is_Spec
1873 or else U.Utype = Is_Spec_Only
1880 Set_String (" (void);");
1881 Write_Statement_Buffer;
1885 WBI ("/* END FINALIZE DEFINITIONS */");
1887 end Gen_Finalize_Library_Defs_C;
1893 procedure Gen_Main_Ada is
1895 if Exit_Status_Supported_On_Target then
1896 Set_String (" function ");
1898 Set_String (" procedure ");
1901 Set_String (Get_Main_Name);
1903 if Command_Line_Args_On_Target then
1904 Write_Statement_Buffer;
1905 WBI (" (argc : Integer;");
1906 WBI (" argv : System.Address;");
1907 WBI (" envp : System.Address)");
1909 if Exit_Status_Supported_On_Target then
1910 WBI (" return Integer");
1916 if Exit_Status_Supported_On_Target then
1917 Set_String (" return Integer is");
1922 Write_Statement_Buffer;
1925 if Opt.Default_Exit_Status /= 0
1926 and then Bind_Main_Program
1927 and then not Configurable_Run_Time_Mode
1929 WBI (" procedure Set_Exit_Status (Status : Integer);");
1930 WBI (" pragma Import (C, Set_Exit_Status, " &
1931 """__gnat_set_exit_status"");");
1935 -- Initialize and Finalize
1937 if not Cumulative_Restrictions.Set (No_Finalization) then
1938 WBI (" procedure Initialize (Addr : System.Address);");
1939 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1941 WBI (" procedure Finalize;");
1942 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1945 -- If we want to analyze the stack, we have to import corresponding
1948 if Dynamic_Stack_Measurement then
1950 WBI (" procedure Output_Results;");
1951 WBI (" pragma Import (C, Output_Results, " &
1952 """__gnat_stack_usage_output_results"");");
1956 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1957 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1958 """__gnat_stack_usage_initialize"");");
1961 -- Deal with declarations for main program case
1963 if not No_Main_Subprogram then
1965 -- To call the main program, we declare it using a pragma Import
1966 -- Ada with the right link name.
1968 -- It might seem more obvious to "with" the main program, and call
1969 -- it in the normal Ada manner. We do not do this for three reasons:
1971 -- 1. It is more efficient not to recompile the main program
1972 -- 2. We are not entitled to assume the source is accessible
1973 -- 3. We don't know what options to use to compile it
1975 -- It is really reason 3 that is most critical (indeed we used
1976 -- to generate the "with", but several regression tests failed).
1980 if ALIs.Table (ALIs.First).Main_Program = Func then
1981 WBI (" Result : Integer;");
1983 WBI (" function Ada_Main_Program return Integer;");
1986 WBI (" procedure Ada_Main_Program;");
1989 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1990 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1991 Set_Main_Program_Name;
1992 Set_String (""");");
1994 Write_Statement_Buffer;
1997 if Bind_Main_Program
1998 and then not Suppress_Standard_Library_On_Target
2000 WBI (" SEH : aliased array (1 .. 2) of Integer;");
2005 -- Generate a reference to Ada_Main_Program_Name. This symbol is
2006 -- not referenced elsewhere in the generated program, but is needed
2007 -- by the debugger (that's why it is generated in the first place).
2008 -- The reference stops Ada_Main_Program_Name from being optimized
2009 -- away by smart linkers, such as the AiX linker.
2011 -- Because this variable is unused, we make this variable "aliased"
2012 -- with a pragma Volatile in order to tell the compiler to preserve
2013 -- this variable at any level of optimization.
2015 if Bind_Main_Program then
2017 (" Ensure_Reference : aliased System.Address := " &
2018 "Ada_Main_Program_Name'Address;");
2019 WBI (" pragma Volatile (Ensure_Reference);");
2025 -- Acquire command line arguments if present on target
2027 if Command_Line_Args_On_Target then
2028 WBI (" gnat_argc := argc;");
2029 WBI (" gnat_argv := argv;");
2030 WBI (" gnat_envp := envp;");
2033 -- If configurable run time and no command line args, then nothing
2034 -- needs to be done since the gnat_argc/argv/envp variables are
2035 -- suppressed in this case.
2037 elsif Configurable_Run_Time_On_Target then
2040 -- Otherwise set dummy values (to be filled in by some other unit?)
2043 WBI (" gnat_argc := 0;");
2044 WBI (" gnat_argv := System.Null_Address;");
2045 WBI (" gnat_envp := System.Null_Address;");
2048 if Opt.Default_Exit_Status /= 0
2049 and then Bind_Main_Program
2050 and then not Configurable_Run_Time_Mode
2052 Set_String (" Set_Exit_Status (");
2053 Set_Int (Opt.Default_Exit_Status);
2055 Write_Statement_Buffer;
2058 if Dynamic_Stack_Measurement then
2059 Set_String (" Initialize_Stack_Analysis (");
2060 Set_Int (Dynamic_Stack_Measurement_Array_Size);
2062 Write_Statement_Buffer;
2065 if not Cumulative_Restrictions.Set (No_Finalization) then
2066 if not No_Main_Subprogram
2067 and then Bind_Main_Program
2068 and then not Suppress_Standard_Library_On_Target
2070 WBI (" Initialize (SEH'Address);");
2072 WBI (" Initialize (System.Null_Address);");
2076 WBI (" " & Ada_Init_Name.all & ";");
2078 if not No_Main_Subprogram then
2079 WBI (" Break_Start;");
2081 if ALIs.Table (ALIs.First).Main_Program = Proc then
2082 WBI (" Ada_Main_Program;");
2084 WBI (" Result := Ada_Main_Program;");
2088 -- Adafinal call is skipped if no finalization
2090 if not Cumulative_Restrictions.Set (No_Finalization) then
2094 -- Prints the result of static stack analysis
2096 if Dynamic_Stack_Measurement then
2097 WBI (" Output_Results;");
2100 -- Finalize is only called if we have a run time
2102 if not Cumulative_Restrictions.Set (No_Finalization) then
2108 if Exit_Status_Supported_On_Target then
2109 if No_Main_Subprogram
2110 or else ALIs.Table (ALIs.First).Main_Program = Proc
2112 WBI (" return (gnat_exit_status);");
2114 WBI (" return (Result);");
2126 procedure Gen_Main_C is
2127 Needs_Library_Finalization : constant Boolean :=
2128 not Configurable_Run_Time_On_Target and then Has_Finalizer;
2129 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2130 -- non-terminating, so we do not want library-level finalization.
2133 if Exit_Status_Supported_On_Target then
2134 WBI ("#include <stdlib.h>");
2135 Set_String ("int ");
2137 Set_String ("void ");
2140 Set_String (Get_Main_Name);
2142 -- Generate command line args in prototype if present on target
2144 if Command_Line_Args_On_Target then
2145 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
2147 -- Case of no command line arguments on target
2150 Write_Statement_Buffer (" (void)");
2155 -- Generate a reference to __gnat_ada_main_program_name. This symbol
2156 -- is not referenced elsewhere in the generated program, but is
2157 -- needed by the debugger (that's why it is generated in the first
2158 -- place). The reference stops Ada_Main_Program_Name from being
2159 -- optimized away by smart linkers, such as the AiX linker.
2161 -- Because this variable is unused, we declare this variable as
2162 -- volatile in order to tell the compiler to preserve it at any
2163 -- level of optimization.
2165 if Bind_Main_Program then
2166 WBI (" char * volatile ensure_reference " &
2167 "__attribute__ ((__unused__)) = " &
2168 "__gnat_ada_main_program_name;");
2171 if not Suppress_Standard_Library_On_Target
2172 and then not No_Main_Subprogram
2174 WBI (" int SEH [2];");
2179 -- If main program is a function, generate result variable
2181 if ALIs.Table (ALIs.First).Main_Program = Func then
2182 WBI (" int result;");
2185 -- Set command line argument values from parameters if command line
2186 -- arguments are present on target
2188 if Command_Line_Args_On_Target then
2189 WBI (" gnat_argc = argc;");
2190 WBI (" gnat_argv = argv;");
2191 WBI (" gnat_envp = envp;");
2194 -- If configurable run-time, then nothing to do, since in this case
2195 -- the gnat_argc/argv/envp variables are entirely suppressed.
2197 elsif Configurable_Run_Time_On_Target then
2200 -- if no command line arguments on target, set dummy values
2203 WBI (" gnat_argc = 0;");
2204 WBI (" gnat_argv = 0;");
2205 WBI (" gnat_envp = 0;");
2208 if Opt.Default_Exit_Status /= 0
2209 and then Bind_Main_Program
2210 and then not Configurable_Run_Time_Mode
2212 Set_String (" __gnat_set_exit_status (");
2213 Set_Int (Opt.Default_Exit_Status);
2215 Write_Statement_Buffer;
2218 -- Initializes dynamic stack measurement if needed
2220 if Dynamic_Stack_Measurement then
2221 Set_String (" __gnat_stack_usage_initialize (");
2222 Set_Int (Dynamic_Stack_Measurement_Array_Size);
2224 Write_Statement_Buffer;
2227 -- The __gnat_initialize routine is used only if we have a run-time
2229 if not Suppress_Standard_Library_On_Target then
2230 if not No_Main_Subprogram and then Bind_Main_Program then
2231 WBI (" __gnat_initialize ((void *)SEH);");
2233 WBI (" __gnat_initialize ((void *)0);");
2237 WBI (" " & Ada_Init_Name.all & " ();");
2239 if not No_Main_Subprogram then
2240 WBI (" __gnat_break_start ();");
2243 -- Output main program name
2245 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2247 -- Main program is procedure case
2249 if ALIs.Table (ALIs.First).Main_Program = Proc then
2251 Set_Main_Program_Name;
2252 Set_String (" ();");
2253 Write_Statement_Buffer;
2255 -- Main program is function case
2257 else -- ALIs.Table (ALIs_First).Main_Program = Func
2258 Set_String (" result = ");
2259 Set_Main_Program_Name;
2260 Set_String (" ();");
2261 Write_Statement_Buffer;
2266 -- Call adafinal if finalization active
2268 if not Cumulative_Restrictions.Set (No_Finalization)
2269 and then Needs_Library_Finalization
2271 Gen_Finalize_Library_C;
2274 -- Outputs the dynamic stack measurement if needed
2276 if Dynamic_Stack_Measurement then
2277 WBI (" __gnat_stack_usage_output_results ();");
2280 -- The finalize routine is used only if we have a run-time
2282 if not Suppress_Standard_Library_On_Target then
2283 WBI (" __gnat_finalize ();");
2286 -- Case of main program is a function, so the value it returns
2287 -- is the exit status in this case.
2289 if ALIs.Table (ALIs.First).Main_Program = Func then
2290 if Exit_Status_Supported_On_Target then
2292 -- VMS must use Posix exit routine in order to get the effect
2293 -- of a Unix compatible setting of the program exit status.
2294 -- For all other systems, we use the standard exit routine.
2296 if OpenVMS_On_Target then
2297 WBI (" decc$__posix_exit (result);");
2299 WBI (" exit (result);");
2303 -- Case of main program is a procedure, in which case the exit
2304 -- status is whatever was set by a Set_Exit call most recently
2307 if Exit_Status_Supported_On_Target then
2309 -- VMS must use Posix exit routine in order to get the effect
2310 -- of a Unix compatible setting of the program exit status.
2311 -- For all other systems, we use the standard exit routine.
2313 if OpenVMS_On_Target then
2314 WBI (" decc$__posix_exit (gnat_exit_status);");
2316 WBI (" exit (gnat_exit_status);");
2325 ------------------------------
2326 -- Gen_Object_Files_Options --
2327 ------------------------------
2329 procedure Gen_Object_Files_Options is
2331 -- This keeps track of the position in the sorted set of entries
2332 -- in the Linker_Options table of where the first entry from an
2333 -- internal file appears.
2335 Linker_Option_List_Started : Boolean := False;
2336 -- Set to True when "LINKER OPTION LIST" is displayed
2338 procedure Write_Linker_Option;
2339 -- Write binder info linker option
2341 -------------------------
2342 -- Write_Linker_Option --
2343 -------------------------
2345 procedure Write_Linker_Option is
2350 -- Loop through string, breaking at null's
2353 while Start < Name_Len loop
2355 -- Find null ending this section
2358 while Name_Buffer (Stop) /= ASCII.NUL
2359 and then Stop <= Name_Len loop
2363 -- Process section if non-null
2365 if Stop > Start then
2366 if Output_Linker_Option_List then
2367 if not Zero_Formatting then
2368 if not Linker_Option_List_Started then
2369 Linker_Option_List_Started := True;
2371 Write_Str (" LINKER OPTION LIST");
2379 Write_Str (Name_Buffer (Start .. Stop - 1));
2383 (" -- ", "", Name_Buffer (Start .. Stop - 1));
2388 end Write_Linker_Option;
2390 -- Start of processing for Gen_Object_Files_Options
2393 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
2395 if Object_List_Filename /= null then
2396 Set_List_File (Object_List_Filename.all);
2399 for E in Elab_Order.First .. Elab_Order.Last loop
2401 -- If not spec that has an associated body, then generate a comment
2402 -- giving the name of the corresponding object file.
2404 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
2405 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
2409 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
2411 -- If the presence of an object file is necessary or if it exists,
2414 if not Hostparm.Exclude_Missing_Objects
2416 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
2418 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2420 if Output_Object_List then
2421 Write_Str (Name_Buffer (1 .. Name_Len));
2425 -- Don't link with the shared library on VMS if an internal
2426 -- filename object is seen. Multiply defined symbols will
2429 if OpenVMS_On_Target
2430 and then Is_Internal_File_Name
2432 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
2434 -- Special case for g-trasym.obj (not included in libgnat)
2436 Get_Name_String (ALIs.Table
2437 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
2439 if Name_Buffer (1 .. 8) /= "g-trasym" then
2440 Opt.Shared_Libgnat := False;
2447 if Object_List_Filename /= null then
2451 -- Add a "-Ldir" for each directory in the object path
2452 if VM_Target /= CLI_Target then
2453 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2455 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2458 Add_Str_To_Name_Buffer ("-L");
2459 Add_Str_To_Name_Buffer (Dir.all);
2460 Write_Linker_Option;
2465 -- Sort linker options
2467 -- This sort accomplishes two important purposes:
2469 -- a) All application files are sorted to the front, and all GNAT
2470 -- internal files are sorted to the end. This results in a well
2471 -- defined dividing line between the two sets of files, for the
2472 -- purpose of inserting certain standard library references into
2473 -- the linker arguments list.
2475 -- b) Given two different units, we sort the linker options so that
2476 -- those from a unit earlier in the elaboration order comes later
2477 -- in the list. This is a heuristic designed to create a more
2478 -- friendly order of linker options when the operations appear in
2479 -- separate units. The idea is that if unit A must be elaborated
2480 -- before unit B, then it is more likely that B references
2481 -- libraries included by A, than vice versa, so we want libraries
2482 -- included by A to come after libraries included by B.
2484 -- These two criteria are implemented by function Lt_Linker_Option. Note
2485 -- that a special case of b) is that specs are elaborated before bodies,
2486 -- so linker options from specs come after linker options for bodies,
2487 -- and again, the assumption is that libraries used by the body are more
2488 -- likely to reference libraries used by the spec, than vice versa.
2491 (Linker_Options.Last,
2492 Move_Linker_Option'Access,
2493 Lt_Linker_Option'Access);
2495 -- Write user linker options, i.e. the set of linker options that come
2496 -- from all files other than GNAT internal files, Lgnat is left set to
2497 -- point to the first entry from a GNAT internal file, or past the end
2498 -- of the entries if there are no internal files.
2500 Lgnat := Linker_Options.Last + 1;
2502 for J in 1 .. Linker_Options.Last loop
2503 if not Linker_Options.Table (J).Internal_File then
2504 Get_Name_String (Linker_Options.Table (J).Name);
2505 Write_Linker_Option;
2512 -- Now we insert standard linker options that must appear after the
2513 -- entries from user files, and before the entries from GNAT run-time
2514 -- files. The reason for this decision is that libraries referenced
2515 -- by internal routines may reference these standard library entries.
2517 -- Note that we do not insert anything when pragma No_Run_Time has been
2518 -- specified or when the standard libraries are not to be used,
2519 -- otherwise on some platforms, such as VMS, we may get duplicate
2520 -- symbols when linking.
2522 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2525 if Opt.Shared_Libgnat then
2526 Add_Str_To_Name_Buffer ("-shared");
2528 Add_Str_To_Name_Buffer ("-static");
2531 -- Write directly to avoid -K output (why???)
2533 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2535 if With_DECGNAT then
2538 if Opt.Shared_Libgnat then
2539 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2541 Add_Str_To_Name_Buffer ("-ldecgnat");
2544 Write_Linker_Option;
2550 if Opt.Shared_Libgnat then
2551 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2553 Add_Str_To_Name_Buffer ("-lgnarl");
2556 Write_Linker_Option;
2561 if Opt.Shared_Libgnat then
2562 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2564 Add_Str_To_Name_Buffer ("-lgnat");
2567 Write_Linker_Option;
2570 -- Write linker options from all internal files
2572 for J in Lgnat .. Linker_Options.Last loop
2573 Get_Name_String (Linker_Options.Table (J).Name);
2574 Write_Linker_Option;
2577 if Output_Linker_Option_List and then not Zero_Formatting then
2581 if Ada_Bind_File then
2582 WBI ("-- END Object file/option list ");
2584 WBI (" END Object file/option list */");
2586 end Gen_Object_Files_Options;
2588 ---------------------
2589 -- Gen_Output_File --
2590 ---------------------
2592 procedure Gen_Output_File (Filename : String) is
2594 -- Acquire settings for Interrupt_State pragmas
2596 Set_IS_Pragma_Table;
2598 -- Acquire settings for Priority_Specific_Dispatching pragma
2600 Set_PSD_Pragma_Table;
2602 -- Override Ada_Bind_File and Bind_Main_Program for VMs since JGNAT only
2603 -- supports Ada code, and the main program is already generated by the
2606 if VM_Target /= No_VM then
2607 Ada_Bind_File := True;
2609 if VM_Target = JVM_Target then
2610 Bind_Main_Program := False;
2614 -- Override time slice value if -T switch is set
2616 if Time_Slice_Set then
2617 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2620 -- Count number of elaboration calls
2622 for E in Elab_Order.First .. Elab_Order.Last loop
2623 if Units.Table (Elab_Order.Table (E)).No_Elab then
2626 Num_Elab_Calls := Num_Elab_Calls + 1;
2630 -- Generate output file in appropriate language
2632 Check_System_Restrictions_Used;
2634 if Ada_Bind_File then
2635 Gen_Output_File_Ada (Filename);
2637 Gen_Output_File_C (Filename);
2639 end Gen_Output_File;
2641 -------------------------
2642 -- Gen_Output_File_Ada --
2643 -------------------------
2645 procedure Gen_Output_File_Ada (Filename : String) is
2647 Ada_Main : constant String := Get_Ada_Main_Name;
2648 -- Name to be used for generated Ada main program. See the body of
2649 -- function Get_Ada_Main_Name for details on the form of the name.
2651 Needs_Library_Finalization : constant Boolean :=
2652 not Configurable_Run_Time_On_Target and then Has_Finalizer;
2653 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2654 -- non-terminating, so we do not want finalization.
2657 -- Name of generated bind file (spec)
2660 -- Name of generated bind file (body)
2663 -- Create spec first
2665 Create_Binder_Output (Filename, 's', Bfiles);
2667 -- We always compile the binder file in Ada 95 mode so that we properly
2668 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2669 -- of the Ada 2005 constructs are needed by the binder file.
2671 WBI ("pragma Ada_95;");
2673 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2674 -- then we need to make sure that the binder program is compiled with
2675 -- the same restriction, so that no exception tables are generated.
2677 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2678 WBI ("pragma Restrictions (No_Exception_Handlers);");
2681 -- Same processing for Restrictions (No_Exception_Propagation)
2683 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2684 WBI ("pragma Restrictions (No_Exception_Propagation);");
2687 -- Same processing for pragma No_Run_Time
2689 if No_Run_Time_Mode then
2690 WBI ("pragma No_Run_Time;");
2693 -- Generate with of System so we can reference System.Address
2695 WBI ("with System;");
2697 -- Generate with of System.Initialize_Scalars if active
2699 if Initialize_Scalars_Used then
2700 WBI ("with System.Scalar_Values;");
2703 -- Generate with of System.Secondary_Stack if active
2705 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2706 WBI ("with System.Secondary_Stack;");
2709 Resolve_Binder_Options;
2711 -- Usually, adafinal is called using a pragma Import C. Since Import C
2712 -- doesn't have the same semantics for JGNAT, we use standard Ada.
2714 if VM_Target /= No_VM
2715 and then not Suppress_Standard_Library_On_Target
2717 WBI ("with System.Soft_Links;");
2718 WBI ("with System.Standard_Library;");
2721 WBI ("package " & Ada_Main & " is");
2722 WBI (" pragma Warnings (Off);");
2724 -- Main program case
2726 if Bind_Main_Program then
2727 if VM_Target = No_VM then
2729 -- Generate argc/argv stuff unless suppressed
2731 if Command_Line_Args_On_Target
2732 or not Configurable_Run_Time_On_Target
2735 WBI (" gnat_argc : Integer;");
2736 WBI (" gnat_argv : System.Address;");
2737 WBI (" gnat_envp : System.Address;");
2739 -- If the standard library is not suppressed, these variables
2740 -- are in the run-time data area for easy run time access.
2742 if not Suppress_Standard_Library_On_Target then
2744 WBI (" pragma Import (C, gnat_argc);");
2745 WBI (" pragma Import (C, gnat_argv);");
2746 WBI (" pragma Import (C, gnat_envp);");
2750 -- Define exit status. Again in normal mode, this is in the
2751 -- run-time library, and is initialized there, but in the
2752 -- configurable runtime case, the variable is declared and
2753 -- initialized in this file.
2757 if Configurable_Run_Time_Mode then
2758 if Exit_Status_Supported_On_Target then
2759 WBI (" gnat_exit_status : Integer := 0;");
2763 WBI (" gnat_exit_status : Integer;");
2764 WBI (" pragma Import (C, gnat_exit_status);");
2768 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2769 -- the main program. Otherwise, it can lead under some circumstances
2770 -- to a symbol duplication during the link (for instance when a C
2771 -- program uses two Ada libraries). Also zero terminate the string
2772 -- so that its end can be found reliably at run time.
2775 WBI (" GNAT_Version : constant String :=");
2776 WBI (" """ & Ver_Prefix &
2777 Gnat_Version_String &
2779 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2782 Set_String (" Ada_Main_Program_Name : constant String := """);
2783 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2785 if VM_Target = No_VM then
2786 Set_Main_Program_Name;
2787 Set_String (""" & ASCII.NUL;");
2789 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2792 Write_Statement_Buffer;
2795 (" pragma Export (C, Ada_Main_Program_Name, " &
2796 """__gnat_ada_main_program_name"");");
2799 if not Cumulative_Restrictions.Set (No_Finalization) then
2801 WBI (" procedure " & Ada_Final_Name.all & ";");
2802 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2803 Ada_Final_Name.all & """);");
2807 WBI (" procedure " & Ada_Init_Name.all & ";");
2808 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2809 Ada_Init_Name.all & """);");
2811 -- If -a has been specified use pragma Linker_Constructor for the init
2812 -- procedure. No need to use a similar pragma for the final procedure as
2813 -- global finalization will occur when the executable finishes execution
2814 -- and for plugins (shared stand-alone libraries that can be
2815 -- "unloaded"), finalization should not occur automatically, otherwise
2816 -- the main executable may not continue to work properly.
2818 if Use_Pragma_Linker_Constructor then
2819 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2822 if Bind_Main_Program and then VM_Target = No_VM then
2824 -- If we have the standard library, then Break_Start is defined
2825 -- there, but when the standard library is suppressed, Break_Start
2829 WBI (" procedure Break_Start;");
2831 if Suppress_Standard_Library_On_Target then
2832 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2834 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2839 if Exit_Status_Supported_On_Target then
2840 Set_String (" function ");
2842 Set_String (" procedure ");
2845 Set_String (Get_Main_Name);
2847 -- Generate argument list if present
2849 if Command_Line_Args_On_Target then
2850 Write_Statement_Buffer;
2851 WBI (" (argc : Integer;");
2852 WBI (" argv : System.Address;");
2854 (" envp : System.Address)");
2856 if Exit_Status_Supported_On_Target then
2857 Write_Statement_Buffer;
2858 WBI (" return Integer;");
2860 Write_Statement_Buffer (";");
2864 if Exit_Status_Supported_On_Target then
2865 Write_Statement_Buffer (" return Integer;");
2867 Write_Statement_Buffer (";");
2871 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2872 Get_Main_Name & """);");
2881 WBI ("end " & Ada_Main & ";");
2882 Close_Binder_Output;
2884 -- Prepare to write body
2886 Create_Binder_Output (Filename, 'b', Bfileb);
2888 -- We always compile the binder file in Ada 95 mode so that we properly
2889 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2890 -- of the Ada 2005 constructs are needed by the binder file.
2892 WBI ("pragma Ada_95;");
2894 -- Output Source_File_Name pragmas which look like
2896 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2897 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2899 -- where sss/bbb are the spec/body file names respectively
2901 Get_Name_String (Bfiles);
2902 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2904 WBI ("pragma Source_File_Name (" &
2906 ", Spec_File_Name => """ &
2907 Name_Buffer (1 .. Name_Len + 3));
2909 Get_Name_String (Bfileb);
2910 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2912 WBI ("pragma Source_File_Name (" &
2914 ", Body_File_Name => """ &
2915 Name_Buffer (1 .. Name_Len + 3));
2917 -- Generate with of System.Restrictions to initialize
2918 -- Run_Time_Restrictions.
2920 if System_Restrictions_Used
2921 and not Suppress_Standard_Library_On_Target
2924 WBI ("with System.Restrictions;");
2927 if Needs_Library_Finalization then
2928 WBI ("with Ada.Exceptions;");
2932 WBI ("package body " & Ada_Main & " is");
2933 WBI (" pragma Warnings (Off);");
2935 if not Suppress_Standard_Library_On_Target then
2937 -- Generate Priority_Specific_Dispatching pragma string
2940 (" Local_Priority_Specific_Dispatching : constant String := """);
2942 for J in 0 .. PSD_Pragma_Settings.Last loop
2943 Set_Char (PSD_Pragma_Settings.Table (J));
2947 Write_Statement_Buffer;
2949 -- Generate Interrupt_State pragma string
2951 Set_String (" Local_Interrupt_States : constant String := """);
2953 for J in 0 .. IS_Pragma_Settings.Last loop
2954 Set_Char (IS_Pragma_Settings.Table (J));
2958 Write_Statement_Buffer;
2962 -- Generate the adafinal routine unless there is no finalization to do
2964 if not Cumulative_Restrictions.Set (No_Finalization) then
2967 if Needs_Library_Finalization then
2968 Gen_Finalize_Library_Ada;
2974 if Bind_Main_Program and then VM_Target = No_VM then
2976 -- When suppressing the standard library then generate dummy body
2979 if Suppress_Standard_Library_On_Target then
2981 WBI (" procedure Break_Start is");
2990 -- Output object file list and the Ada body is complete
2992 Gen_Object_Files_Options;
2995 WBI ("end " & Ada_Main & ";");
2997 Close_Binder_Output;
2998 end Gen_Output_File_Ada;
3000 -----------------------
3001 -- Gen_Output_File_C --
3002 -----------------------
3004 procedure Gen_Output_File_C (Filename : String) is
3006 Needs_Library_Finalization : constant Boolean :=
3007 not Configurable_Run_Time_On_Target and then Has_Finalizer;
3010 pragma Warnings (Off, Bfile);
3011 -- Name of generated bind file (not referenced)
3014 Create_Binder_Output (Filename, 'c', Bfile);
3016 Resolve_Binder_Options;
3018 WBI ("extern void " & Ada_Final_Name.all & " (void);");
3020 -- If -a has been specified use __attribute__((constructor)) for the
3021 -- init procedure. No need to use a similar featute for the final
3022 -- procedure as global finalization will occur when the executable
3023 -- finishes execution and for plugins (shared stand-alone libraries that
3024 -- can be "unloaded"), finalization should not occur automatically,
3025 -- otherwise the main executable may not continue to work properly.
3027 if Use_Pragma_Linker_Constructor then
3028 WBI ("extern void " & Ada_Init_Name.all &
3029 " (void) __attribute__((constructor));");
3031 WBI ("extern void " & Ada_Init_Name.all & " (void);");
3034 WBI ("extern void system__standard_library__adafinal (void);");
3036 if not No_Main_Subprogram then
3037 Set_String ("extern ");
3039 if Exit_Status_Supported_On_Target then
3042 Set_String ("void");
3045 Set_String (" main ");
3047 if Command_Line_Args_On_Target then
3048 Write_Statement_Buffer ("(int, char **, char **);");
3050 Write_Statement_Buffer ("(void);");
3053 if OpenVMS_On_Target then
3054 WBI ("extern void decc$__posix_exit (int);");
3056 WBI ("extern void exit (int);");
3059 WBI ("extern void __gnat_break_start (void);");
3060 Set_String ("extern ");
3062 if ALIs.Table (ALIs.First).Main_Program = Proc then
3063 Set_String ("void ");
3065 Set_String ("int ");
3068 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3069 Set_Main_Program_Name;
3070 Set_String (" (void);");
3071 Write_Statement_Buffer;
3074 if not Suppress_Standard_Library_On_Target then
3075 WBI ("extern void __gnat_initialize (void *);");
3076 WBI ("extern void __gnat_finalize (void);");
3077 WBI ("extern void __gnat_install_handler (void);");
3080 if Dynamic_Stack_Measurement then
3082 WBI ("extern void __gnat_stack_usage_output_results (void);");
3083 WBI ("extern void __gnat_stack_usage_initialize (int size);");
3086 -- Initialize stack limit for the environment task if the stack check
3087 -- method is stack limit and stack check is enabled.
3089 if Stack_Check_Limits_On_Target
3090 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
3093 WBI ("extern void __gnat_initialize_stack_limit (void);");
3100 if Needs_Library_Finalization then
3101 Gen_Finalize_Library_Defs_C;
3104 -- Imported variables used only when we have a runtime
3106 if not Suppress_Standard_Library_On_Target then
3108 -- Track elaboration/finalization phase
3110 WBI ("extern int __gnat_handler_installed;");
3113 -- Track feature enable/disable on VMS
3115 if OpenVMS_On_Target then
3116 WBI ("extern int __gnat_features_set;");
3121 -- Write argv/argc exit status stuff if main program case
3123 if Bind_Main_Program then
3125 -- First deal with argc/argv/envp. In the normal case they are in the
3126 -- run-time library.
3128 if not Configurable_Run_Time_On_Target then
3129 WBI ("extern int gnat_argc;");
3130 WBI ("extern char **gnat_argv;");
3131 WBI ("extern char **gnat_envp;");
3133 -- If configurable run time and no command line args, then the
3134 -- generation of these variables is entirely suppressed.
3136 elsif not Command_Line_Args_On_Target then
3139 -- Otherwise, in the configurable run-time case they are right in the
3143 WBI ("int gnat_argc;");
3144 WBI ("char **gnat_argv;");
3145 WBI ("char **gnat_envp;");
3148 -- Similarly deal with exit status
3150 if not Configurable_Run_Time_On_Target then
3151 WBI ("extern int gnat_exit_status;");
3153 -- If configurable run time and no exit status on target, then the
3154 -- generation of this variables is entirely suppressed.
3156 elsif not Exit_Status_Supported_On_Target then
3159 -- Otherwise, in the configurable run-time case this variable is
3160 -- right in the binder file, and initialized to zero there.
3163 WBI ("int gnat_exit_status = 0;");
3169 -- When suppressing the standard library, the __gnat_break_start routine
3170 -- (for the debugger to get initial control) is defined in this file.
3172 if Suppress_Standard_Library_On_Target then
3174 WBI ("void __gnat_break_start (void) {}");
3177 -- Generate the __gnat_version and __gnat_ada_main_program_name info
3178 -- only for the main program. Otherwise, it can lead under some
3179 -- circumstances to a symbol duplication during the link (for instance
3180 -- when a C program uses 2 Ada libraries)
3182 if Bind_Main_Program then
3184 WBI ("char __gnat_version[] = """ & Ver_Prefix &
3185 Gnat_Version_String & """;");
3187 Set_String ("char __gnat_ada_main_program_name[] = """);
3188 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3189 Set_Main_Program_Name;
3191 Write_Statement_Buffer;
3194 -- Generate the adafinal routine. In no runtime mode, this is not
3195 -- needed, since there is no finalization to do.
3197 if not Cumulative_Restrictions.Set (No_Finalization) then
3203 -- Main is only present for Ada main case
3205 if Bind_Main_Program then
3209 -- Generate versions, elaboration order, list of object files
3213 Gen_Object_Files_Options;
3215 -- C binder output is complete
3217 Close_Binder_Output;
3218 end Gen_Output_File_C;
3220 --------------------------
3221 -- Gen_Restrictions_Ada --
3222 --------------------------
3224 procedure Gen_Restrictions_Ada is
3228 if Suppress_Standard_Library_On_Target
3229 or not System_Restrictions_Used
3234 WBI (" System.Restrictions.Run_Time_Restrictions :=");
3240 for J in Cumulative_Restrictions.Set'Range loop
3241 Set_Boolean (Cumulative_Restrictions.Set (J));
3245 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
3246 Write_Statement_Buffer;
3252 Set_String_Replace ("),");
3253 Write_Statement_Buffer;
3254 Set_String (" Value => (");
3256 for J in Cumulative_Restrictions.Value'Range loop
3257 Set_Int (Int (Cumulative_Restrictions.Value (J)));
3261 Set_String_Replace ("),");
3262 Write_Statement_Buffer;
3263 WBI (" Violated =>");
3267 for J in Cumulative_Restrictions.Violated'Range loop
3268 Set_Boolean (Cumulative_Restrictions.Violated (J));
3272 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
3273 Write_Statement_Buffer;
3279 Set_String_Replace ("),");
3280 Write_Statement_Buffer;
3281 Set_String (" Count => (");
3283 for J in Cumulative_Restrictions.Count'Range loop
3284 Set_Int (Int (Cumulative_Restrictions.Count (J)));
3288 Set_String_Replace ("),");
3289 Write_Statement_Buffer;
3290 Set_String (" Unknown => (");
3292 for J in Cumulative_Restrictions.Unknown'Range loop
3293 Set_Boolean (Cumulative_Restrictions.Unknown (J));
3297 Set_String_Replace ("))");
3299 Write_Statement_Buffer;
3300 end Gen_Restrictions_Ada;
3302 ------------------------
3303 -- Gen_Restrictions_C --
3304 ------------------------
3306 procedure Gen_Restrictions_C is
3308 if Suppress_Standard_Library_On_Target
3309 or not System_Restrictions_Used
3314 WBI (" typedef struct {");
3315 Set_String (" char set [");
3316 Set_Int (Cumulative_Restrictions.Set'Length);
3318 Write_Statement_Buffer;
3320 Set_String (" int value [");
3321 Set_Int (Cumulative_Restrictions.Value'Length);
3323 Write_Statement_Buffer;
3325 Set_String (" char violated [");
3326 Set_Int (Cumulative_Restrictions.Violated'Length);
3328 Write_Statement_Buffer;
3330 Set_String (" int count [");
3331 Set_Int (Cumulative_Restrictions.Count'Length);
3333 Write_Statement_Buffer;
3335 Set_String (" char unknown [");
3336 Set_Int (Cumulative_Restrictions.Unknown'Length);
3338 Write_Statement_Buffer;
3339 WBI (" } restrictions;");
3340 WBI (" extern restrictions " &
3341 "system__restrictions__run_time_restrictions;");
3342 WBI (" restrictions r = {");
3345 for J in Cumulative_Restrictions.Set'Range loop
3346 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
3350 Set_String_Replace ("},");
3351 Write_Statement_Buffer;
3354 for J in Cumulative_Restrictions.Value'Range loop
3355 Set_Int (Int (Cumulative_Restrictions.Value (J)));
3359 Set_String_Replace ("},");
3360 Write_Statement_Buffer;
3363 for J in Cumulative_Restrictions.Violated'Range loop
3364 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
3368 Set_String_Replace ("},");
3369 Write_Statement_Buffer;
3372 for J in Cumulative_Restrictions.Count'Range loop
3373 Set_Int (Int (Cumulative_Restrictions.Count (J)));
3377 Set_String_Replace ("},");
3378 Write_Statement_Buffer;
3381 for J in Cumulative_Restrictions.Unknown'Range loop
3382 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
3386 Set_String_Replace ("}}");
3388 Write_Statement_Buffer;
3389 WBI (" system__restrictions__run_time_restrictions = r;");
3390 end Gen_Restrictions_C;
3392 ----------------------
3393 -- Gen_Versions_Ada --
3394 ----------------------
3396 -- This routine generates lines such as:
3398 -- unnnnn : constant Integer := 16#hhhhhhhh#;
3399 -- pragma Export (C, unnnnn, unam);
3401 -- for each unit, where unam is the unit name suffixed by either B or S for
3402 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
3403 -- the version number, and nnnnn is a 5-digits serial number.
3405 procedure Gen_Versions_Ada is
3406 Ubuf : String (1 .. 6) := "u00000";
3408 procedure Increment_Ubuf;
3409 -- Little procedure to increment the serial number
3411 --------------------
3412 -- Increment_Ubuf --
3413 --------------------
3415 procedure Increment_Ubuf is
3417 for J in reverse Ubuf'Range loop
3418 Ubuf (J) := Character'Succ (Ubuf (J));
3419 exit when Ubuf (J) <= '9';
3424 -- Start of processing for Gen_Versions_Ada
3429 WBI (" type Version_32 is mod 2 ** 32;");
3430 for U in Units.First .. Units.Last loop
3431 if not Units.Table (U).SAL_Interface and then
3432 ((not Bind_For_Library) or else Units.Table (U).Directly_Scanned)
3435 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
3436 Units.Table (U).Version & "#;");
3437 Set_String (" pragma Export (C, ");
3439 Set_String (", """);
3441 Get_Name_String (Units.Table (U).Uname);
3443 for K in 1 .. Name_Len loop
3444 if Name_Buffer (K) = '.' then
3448 elsif Name_Buffer (K) = '%' then
3452 Set_Char (Name_Buffer (K));
3456 if Name_Buffer (Name_Len) = 's' then
3462 Set_String (""");");
3463 Write_Statement_Buffer;
3466 end Gen_Versions_Ada;
3468 --------------------
3469 -- Gen_Versions_C --
3470 --------------------
3472 -- This routine generates a line of the form:
3474 -- unsigned unam = 0xhhhhhhhh;
3476 -- for each unit, where unam is the unit name suffixed by either B or S for
3477 -- body or spec, with dots replaced by double underscores.
3479 procedure Gen_Versions_C is
3481 for U in Units.First .. Units.Last loop
3482 if not Units.Table (U).SAL_Interface and then
3483 ((not Bind_For_Library) or else Units.Table (U).Directly_Scanned)
3485 Set_String ("unsigned ");
3487 Get_Name_String (Units.Table (U).Uname);
3489 for K in 1 .. Name_Len loop
3490 if Name_Buffer (K) = '.' then
3493 elsif Name_Buffer (K) = '%' then
3497 Set_Char (Name_Buffer (K));
3501 if Name_Buffer (Name_Len) = 's' then
3507 Set_String (" = 0x");
3508 Set_String (Units.Table (U).Version);
3510 Write_Statement_Buffer;
3515 ------------------------
3516 -- Get_Main_Unit_Name --
3517 ------------------------
3519 function Get_Main_Unit_Name (S : String) return String is
3520 Result : String := S;
3523 for J in S'Range loop
3524 if Result (J) = '.' then
3530 end Get_Main_Unit_Name;
3532 -----------------------
3533 -- Get_Ada_Main_Name --
3534 -----------------------
3536 function Get_Ada_Main_Name return String is
3537 Suffix : constant String := "_00";
3538 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3539 Opt.Ada_Main_Name.all & Suffix;
3543 -- The main program generated by JGNAT expects a package called
3544 -- ada_<main procedure>.
3546 if VM_Target /= No_VM then
3547 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3548 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3551 -- This loop tries the following possibilities in order
3557 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3558 -- it is set to 'ada_main'.
3560 for J in 0 .. 99 loop
3562 Nlen := Name'Length - Suffix'Length;
3564 Nlen := Name'Length;
3565 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3566 Name (Name'Last - 1) :=
3567 Character'Val (J / 10 + Character'Pos ('0'));
3570 for K in ALIs.First .. ALIs.Last loop
3571 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3573 -- Get unit name, removing %b or %e at end
3575 Get_Name_String (Units.Table (L).Uname);
3576 Name_Len := Name_Len - 2;
3578 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3584 return Name (1 .. Nlen);
3590 -- If we fall through, just use a peculiar unlikely name
3592 return ("Qwertyuiop");
3593 end Get_Ada_Main_Name;
3599 function Get_Main_Name return String is
3601 -- Explicit name given with -M switch
3603 if Bind_Alternate_Main_Name then
3604 return Alternate_Main_Name.all;
3606 -- Case of main program name to be used directly
3608 elsif Use_Ada_Main_Program_Name_On_Target then
3610 -- Get main program name
3612 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3614 -- If this is a child name, return only the name of the child, since
3615 -- we can't have dots in a nested program name. Note that we do not
3616 -- include the %b at the end of the unit name.
3618 for J in reverse 1 .. Name_Len - 2 loop
3619 if J = 1 or else Name_Buffer (J - 1) = '.' then
3620 return Name_Buffer (J .. Name_Len - 2);
3624 raise Program_Error; -- impossible exit
3626 -- Case where "main" is to be used as default
3633 ---------------------
3634 -- Get_WC_Encoding --
3635 ---------------------
3637 function Get_WC_Encoding return Character is
3639 -- If encoding method specified by -W switch, then return it
3641 if Wide_Character_Encoding_Method_Specified then
3642 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3644 -- If no main program, and not specified, set brackets, we really have
3645 -- no better choice. If some other encoding is required when there is
3646 -- no main, it must be set explicitly using -Wx.
3648 -- Note: if the ALI file always passed the wide character encoding of
3649 -- every file, then we could use the encoding of the initial specified
3650 -- file, but this information is passed only for potential main
3651 -- programs. We could fix this sometime, but it is a very minor point
3652 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
3653 -- is no main program).
3655 elsif No_Main_Subprogram then
3658 -- Otherwise if there is a main program, take encoding from it
3661 return ALIs.Table (ALIs.First).WC_Encoding;
3663 end Get_WC_Encoding;
3669 function Has_Finalizer return Boolean is
3674 for E in reverse Elab_Order.First .. Elab_Order.Last loop
3675 Unum := Elab_Order.Table (E);
3676 U := Units.Table (Unum);
3678 -- We are only interested in non-generic packages
3680 if U.Unit_Kind = 'p'
3681 and then U.Has_Finalizer
3682 and then not U.Is_Generic
3683 and then not U.No_Elab
3692 ----------------------
3693 -- Lt_Linker_Option --
3694 ----------------------
3696 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3698 -- Sort internal files last
3700 if Linker_Options.Table (Op1).Internal_File
3702 Linker_Options.Table (Op2).Internal_File
3704 -- Note: following test uses False < True
3706 return Linker_Options.Table (Op1).Internal_File
3708 Linker_Options.Table (Op2).Internal_File;
3710 -- If both internal or both non-internal, sort according to the
3711 -- elaboration position. A unit that is elaborated later should come
3712 -- earlier in the linker options list.
3715 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3717 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3720 end Lt_Linker_Option;
3722 ------------------------
3723 -- Move_Linker_Option --
3724 ------------------------
3726 procedure Move_Linker_Option (From : Natural; To : Natural) is
3728 Linker_Options.Table (To) := Linker_Options.Table (From);
3729 end Move_Linker_Option;
3731 ----------------------------
3732 -- Resolve_Binder_Options --
3733 ----------------------------
3735 procedure Resolve_Binder_Options is
3737 for E in Elab_Order.First .. Elab_Order.Last loop
3738 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3740 -- This is not a perfect approach, but is the current protocol
3741 -- between the run-time and the binder to indicate that tasking is
3742 -- used: system.os_interface should always be used by any tasking
3745 if Name_Buffer (1 .. 19) = "system.os_interface" then
3749 -- Ditto for declib and the "dec" package
3751 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3752 With_DECGNAT := True;
3755 end Resolve_Binder_Options;
3761 procedure Set_Boolean (B : Boolean) is
3762 True_Str : constant String := "True";
3763 False_Str : constant String := "False";
3766 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3767 Last := Last + True_Str'Length;
3769 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3770 Last := Last + False_Str'Length;
3778 procedure Set_Char (C : Character) is
3781 Statement_Buffer (Last) := C;
3788 procedure Set_Int (N : Int) is
3800 Statement_Buffer (Last) :=
3801 Character'Val (N mod 10 + Character'Pos ('0'));
3805 -------------------------
3806 -- Set_IS_Pragma_Table --
3807 -------------------------
3809 procedure Set_IS_Pragma_Table is
3811 for F in ALIs.First .. ALIs.Last loop
3812 for K in ALIs.Table (F).First_Interrupt_State ..
3813 ALIs.Table (F).Last_Interrupt_State
3816 Inum : constant Int :=
3817 Interrupt_States.Table (K).Interrupt_Id;
3818 Stat : constant Character :=
3819 Interrupt_States.Table (K).Interrupt_State;
3822 while IS_Pragma_Settings.Last < Inum loop
3823 IS_Pragma_Settings.Append ('n');
3826 IS_Pragma_Settings.Table (Inum) := Stat;
3830 end Set_IS_Pragma_Table;
3832 ---------------------------
3833 -- Set_Main_Program_Name --
3834 ---------------------------
3836 procedure Set_Main_Program_Name is
3838 -- Note that name has %b on the end which we ignore
3840 -- First we output the initial _ada_ since we know that the main
3841 -- program is a library level subprogram.
3843 Set_String ("_ada_");
3845 -- Copy name, changing dots to double underscores
3847 for J in 1 .. Name_Len - 2 loop
3848 if Name_Buffer (J) = '.' then
3851 Set_Char (Name_Buffer (J));
3854 end Set_Main_Program_Name;
3856 ---------------------
3857 -- Set_Name_Buffer --
3858 ---------------------
3860 procedure Set_Name_Buffer is
3862 for J in 1 .. Name_Len loop
3863 Set_Char (Name_Buffer (J));
3865 end Set_Name_Buffer;
3867 -------------------------
3868 -- Set_PSD_Pragma_Table --
3869 -------------------------
3871 procedure Set_PSD_Pragma_Table is
3873 for F in ALIs.First .. ALIs.Last loop
3874 for K in ALIs.Table (F).First_Specific_Dispatching ..
3875 ALIs.Table (F).Last_Specific_Dispatching
3878 DTK : Specific_Dispatching_Record
3879 renames Specific_Dispatching.Table (K);
3882 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3883 PSD_Pragma_Settings.Append ('F');
3886 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3887 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3892 end Set_PSD_Pragma_Table;
3898 procedure Set_String (S : String) is
3900 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3901 Last := Last + S'Length;
3904 ------------------------
3905 -- Set_String_Replace --
3906 ------------------------
3908 procedure Set_String_Replace (S : String) is
3910 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3911 end Set_String_Replace;
3917 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores) is
3919 for J in 1 .. Name_Len - 2 loop
3920 if Name_Buffer (J) = '.' then
3921 if Mode = Double_Underscores then
3923 elsif Mode = Dot then
3929 Set_Char (Name_Buffer (J));
3934 ---------------------
3935 -- Set_Unit_Number --
3936 ---------------------
3938 procedure Set_Unit_Number (U : Unit_Id) is
3939 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3940 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3943 if Num_Units >= 10 and then Unum < 10 then
3947 if Num_Units >= 100 and then Unum < 100 then
3952 end Set_Unit_Number;
3954 ----------------------
3955 -- Write_Info_Ada_C --
3956 ----------------------
3958 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3960 if Ada_Bind_File then
3962 S : String (1 .. Ada'Length + Common'Length);
3964 S (1 .. Ada'Length) := Ada;
3965 S (Ada'Length + 1 .. S'Length) := Common;
3971 S : String (1 .. C'Length + Common'Length);
3973 S (1 .. C'Length) := C;
3974 S (C'Length + 1 .. S'Length) := Common;
3978 end Write_Info_Ada_C;
3980 ----------------------------
3981 -- Write_Statement_Buffer --
3982 ----------------------------
3984 procedure Write_Statement_Buffer is
3986 WBI (Statement_Buffer (1 .. Last));
3988 end Write_Statement_Buffer;
3990 procedure Write_Statement_Buffer (S : String) is
3993 Write_Statement_Buffer;
3994 end Write_Statement_Buffer;