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;
75 -- Flag indicating whether the finalize_library rountine has been built
77 ----------------------------------
78 -- Interface_State Pragma Table --
79 ----------------------------------
81 -- This table assembles the interface state pragma information from
82 -- all the units in the partition. Note that Bcheck has already checked
83 -- that the information is consistent across units. The entries
84 -- in this table are n/u/r/s for not set/user/runtime/system.
86 package IS_Pragma_Settings is new Table.Table (
87 Table_Component_Type => Character,
88 Table_Index_Type => Int,
91 Table_Increment => 200,
92 Table_Name => "IS_Pragma_Settings");
94 -- This table assembles the Priority_Specific_Dispatching pragma
95 -- information from all the units in the partition. Note that Bcheck has
96 -- already checked that the information is consistent across units.
97 -- The entries in this table are the upper case first character of the
98 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
100 package PSD_Pragma_Settings is new Table.Table (
101 Table_Component_Type => Character,
102 Table_Index_Type => Int,
103 Table_Low_Bound => 0,
104 Table_Initial => 100,
105 Table_Increment => 200,
106 Table_Name => "PSD_Pragma_Settings");
108 ----------------------
109 -- Run-Time Globals --
110 ----------------------
112 -- This section documents the global variables that set from the
113 -- generated binder file.
115 -- Main_Priority : Integer;
116 -- Time_Slice_Value : Integer;
117 -- Heap_Size : Natural;
118 -- WC_Encoding : Character;
119 -- Locking_Policy : Character;
120 -- Queuing_Policy : Character;
121 -- Task_Dispatching_Policy : Character;
122 -- Priority_Specific_Dispatching : System.Address;
123 -- Num_Specific_Dispatching : Integer;
124 -- Restrictions : System.Address;
125 -- Interrupt_States : System.Address;
126 -- Num_Interrupt_States : Integer;
127 -- Unreserve_All_Interrupts : Integer;
128 -- Exception_Tracebacks : Integer;
129 -- Zero_Cost_Exceptions : Integer;
130 -- Detect_Blocking : Integer;
131 -- Default_Stack_Size : Integer;
132 -- Leap_Seconds_Support : Integer;
133 -- Main_CPU : Integer;
135 -- Main_Priority is the priority value set by pragma Priority in the main
136 -- program. If no such pragma is present, the value is -1.
138 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
139 -- main program, or by the use of a -Tnnn parameter for the binder (if both
140 -- are present, the binder value overrides). The value is in milliseconds.
141 -- A value of zero indicates that time slicing should be suppressed. If no
142 -- pragma is present, and no -T switch was used, the value is -1.
144 -- Heap_Size is the heap to use for memory allocations set by use of a
145 -- -Hnn parameter for the binder or by the GNAT$NO_MALLOC_64 logical.
146 -- Valid values are 32 and 64. This switch is only effective on VMS.
148 -- WC_Encoding shows the wide character encoding method used for the main
149 -- program. This is one of the encoding letters defined in
150 -- System.WCh_Con.WC_Encoding_Letters.
152 -- Locking_Policy is a space if no locking policy was specified for the
153 -- partition. If a locking policy was specified, the value is the upper
154 -- case first character of the locking policy name, for example, 'C' for
157 -- Queuing_Policy is a space if no queuing policy was specified for the
158 -- partition. If a queuing policy was specified, the value is the upper
159 -- case first character of the queuing policy name for example, 'F' for
162 -- Task_Dispatching_Policy is a space if no task dispatching policy was
163 -- specified for the partition. If a task dispatching policy was specified,
164 -- the value is the upper case first character of the policy name, e.g. 'F'
165 -- for FIFO_Within_Priorities.
167 -- Priority_Specific_Dispatching is the address of a string used to store
168 -- the task dispatching policy specified for the different priorities in
169 -- the partition. The length of this string is determined by the last
170 -- priority for which such a pragma applies (the string will be a null
171 -- string if no specific dispatching policies were used). If pragma were
172 -- present, the entries apply to the priorities in sequence from the first
173 -- priority. The value stored is the upper case first character of the
174 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
175 -- for those priority ranges not specified.
177 -- Num_Specific_Dispatching is the length of the
178 -- Priority_Specific_Dispatching string. It will be set to zero if no
179 -- Priority_Specific_Dispatching pragmas are present.
181 -- Restrictions is the address of a null-terminated string specifying the
182 -- restrictions information for the partition. The format is identical to
183 -- that of the parameter string found on R lines in ali files (see Lib.Writ
184 -- spec in lib-writ.ads for full details). The difference is that in this
185 -- context the values are the cumulative ones for the entire partition.
187 -- Interrupt_States is the address of a string used to specify the
188 -- cumulative results of Interrupt_State pragmas used in the partition.
189 -- The length of this string is determined by the last interrupt for which
190 -- such a pragma is given (the string will be a null string if no pragmas
191 -- were used). If pragma were present the entries apply to the interrupts
192 -- in sequence from the first interrupt, and are set to one of four
193 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
194 -- time, 's' for system, see description of Interrupt_State pragma for
197 -- Num_Interrupt_States is the length of the Interrupt_States string. It
198 -- will be set to zero if no Interrupt_State pragmas are present.
200 -- Unreserve_All_Interrupts is set to one if at least one unit in the
201 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
203 -- Exception_Tracebacks is set to one if the -E parameter was present
204 -- in the bind and to zero otherwise. Note that on some targets exception
205 -- tracebacks are provided by default, so a value of zero for this
206 -- parameter does not necessarily mean no trace backs are available.
208 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
209 -- this partition, and to zero if longjmp/setjmp exceptions are used.
211 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
212 -- not. A value of zero indicates that the pragma is not present, while a
213 -- value of 1 signals its presence in the partition.
215 -- Default_Stack_Size is the default stack size used when creating an Ada
216 -- task with no explicit Storage_Size clause.
218 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
219 -- disabled. A value of zero indicates that leap seconds are turned "off",
220 -- while a value of one signifies "on" status.
222 -- Main_CPU is the processor set by pragma CPU in the main program. If no
223 -- such pragma is present, the value is -1.
225 -----------------------
226 -- Local Subprograms --
227 -----------------------
229 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
230 -- Convenient shorthand used throughout
232 procedure Check_System_Restrictions_Used;
233 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
234 -- System.Restrictions is present in the partition, otherwise False).
236 procedure Gen_Adainit_Ada;
237 -- Generates the Adainit procedure (Ada code case)
239 procedure Gen_Adainit_C;
240 -- Generates the Adainit procedure (C code case)
242 procedure Gen_Adafinal_Ada;
243 -- Generate the Adafinal procedure (Ada code case)
245 procedure Gen_Adafinal_C;
246 -- Generate the Adafinal procedure (C code case)
248 procedure Gen_Elab_Externals_Ada;
249 -- Generate sequence of external declarations for elaboration (Ada)
251 procedure Gen_Elab_Externals_C;
252 -- Generate sequence of external declarations for elaboration (C)
254 procedure Gen_Elab_Calls_Ada;
255 -- Generate sequence of elaboration calls (Ada code case)
257 procedure Gen_Elab_Calls_C;
258 -- Generate sequence of elaboration calls (C code case)
260 procedure Gen_Elab_Order_Ada;
261 -- Generate comments showing elaboration order chosen (Ada code case)
263 procedure Gen_Elab_Order_C;
264 -- Generate comments showing elaboration order chosen (C code case)
266 procedure Gen_Elab_Defs_C;
267 -- Generate sequence of definitions for elaboration routines (C code case)
269 procedure Gen_Finalize_Library_Ada;
270 -- Generate a sequence of finalization calls to elaborated packages (Ada)
272 procedure Gen_Finalize_Library_C;
273 -- Generate a sequence of finalization calls to elaborated packages (C)
275 procedure Gen_Finalize_Library_Defs_C;
276 -- Generate a sequence of defininitions for package finalizers (C case)
278 procedure Gen_Main_Ada;
279 -- Generate procedure main (Ada code case)
281 procedure Gen_Main_C;
282 -- Generate main() procedure (C code case)
284 procedure Gen_Object_Files_Options;
285 -- Output comments containing a list of the full names of the object
286 -- files to be linked and the list of linker options supplied by
287 -- Linker_Options pragmas in the source. (C and Ada code case)
289 procedure Gen_Output_File_Ada (Filename : String);
290 -- Generate output file (Ada code case)
292 procedure Gen_Output_File_C (Filename : String);
293 -- Generate output file (C code case)
295 procedure Gen_Restrictions_Ada;
296 -- Generate initialization of restrictions variable (Ada code case)
298 procedure Gen_Restrictions_C;
299 -- Generate initialization of restrictions variable (C code case)
301 procedure Gen_Versions_Ada;
302 -- Output series of definitions for unit versions (Ada code case)
304 procedure Gen_Versions_C;
305 -- Output series of definitions for unit versions (C code case)
307 function Get_Ada_Main_Name return String;
308 -- This function is used in the Ada main output case to compute a usable
309 -- name for the generated main program. The normal main program name is
310 -- Ada_Main, but this won't work if the user has a unit with this name.
311 -- This function tries Ada_Main first, and if there is such a clash, then
312 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
314 function Get_Main_Unit_Name (S : String) return String;
315 -- Return the main unit name corresponding to S by replacing '.' with '_'
317 function Get_Main_Name return String;
318 -- This function is used in the Ada main output case to compute the
319 -- correct external main program. It is "main" by default, unless the
320 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
321 -- is the name of the Ada main name without the "_ada". This default
322 -- can be overridden explicitly using the -Mname binder switch.
324 function Get_WC_Encoding return Character;
325 -- Return wide character encoding method to set as WC_Encoding in output.
326 -- If -W has been used, returns the specified encoding, otherwise returns
327 -- the encoding method used for the main program source. If there is no
328 -- main program source (-z switch used), returns brackets ('b').
330 function Has_Finalizer return Boolean;
331 -- Determine whether the current unit has at least one library-level
334 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
335 -- Compare linker options, when sorting, first according to
336 -- Is_Internal_File (internal files come later) and then by
337 -- elaboration order position (latest to earliest).
339 procedure Move_Linker_Option (From : Natural; To : Natural);
340 -- Move routine for sorting linker options
342 procedure Resolve_Binder_Options;
343 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
344 -- since it tests for a package named "dec" which might cause a conflict
345 -- on non-VMS systems.
347 procedure Set_Char (C : Character);
348 -- Set given character in Statement_Buffer at the Last + 1 position
349 -- and increment Last by one to reflect the stored character.
351 procedure Set_Int (N : Int);
352 -- Set given value in decimal in Statement_Buffer with no spaces
353 -- starting at the Last + 1 position, and updating Last past the value.
354 -- A minus sign is output for a negative value.
356 procedure Set_Boolean (B : Boolean);
357 -- Set given boolean value in Statement_Buffer at the Last + 1 position
358 -- and update Last past the value.
360 procedure Set_IS_Pragma_Table;
361 -- Initializes contents of IS_Pragma_Settings table from ALI table
363 procedure Set_Main_Program_Name;
364 -- Given the main program name in Name_Buffer (length in Name_Len)
365 -- generate the name of the routine to be used in the call. The name
366 -- is generated starting at Last + 1, and Last is updated past it.
368 procedure Set_Name_Buffer;
369 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
371 procedure Set_PSD_Pragma_Table;
372 -- Initializes contents of PSD_Pragma_Settings table from ALI table
374 procedure Set_String (S : String);
375 -- Sets characters of given string in Statement_Buffer, starting at the
376 -- Last + 1 position, and updating last past the string value.
378 procedure Set_String_Replace (S : String);
379 -- Replaces the last S'Length characters in the Statement_Buffer with
380 -- the characters of S. The caller must ensure that these characters do
381 -- in fact exist in the Statement_Buffer.
383 type Qualification_Mode is (Dollar_Sign, Dot, Double_Underscores);
385 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores);
386 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
387 -- starting at the Last + 1 position and update Last past the value.
388 -- Depending on parameter Mode, a dot (.) can be qualified into double
389 -- underscores (__), a dollar sign ($) or left as is.
391 procedure Set_Unit_Number (U : Unit_Id);
392 -- Sets unit number (first unit is 1, leading zeroes output to line
393 -- up all output unit numbers nicely as required by the value, and
394 -- by the total number of units.
396 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
397 -- For C code case, write C & Common, for Ada case write Ada & Common
398 -- to current binder output file using Write_Binder_Info.
400 procedure Write_Statement_Buffer;
401 -- Write out contents of statement buffer up to Last, and reset Last to 0
403 procedure Write_Statement_Buffer (S : String);
404 -- First writes its argument (using Set_String (S)), then writes out the
405 -- contents of statement buffer up to Last, and reset Last to 0
407 ------------------------------------
408 -- Check_System_Restrictions_Used --
409 ------------------------------------
411 procedure Check_System_Restrictions_Used is
413 for J in Units.First .. Units.Last loop
414 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
415 System_Restrictions_Used := True;
420 System_Restrictions_Used := False;
421 end Check_System_Restrictions_Used;
423 ----------------------
424 -- Gen_Adafinal_Ada --
425 ----------------------
427 procedure Gen_Adafinal_Ada is
429 WBI (" procedure " & Ada_Final_Name.all & " is");
431 if Bind_Main_Program and then VM_Target = No_VM then
432 WBI (" procedure s_stalib_adafinal;");
433 Set_String (" pragma Import (C, s_stalib_adafinal, ");
434 Set_String ("""system__standard_library__adafinal"");");
435 Write_Statement_Buffer;
439 WBI (" if not Is_Elaborated then");
442 WBI (" Is_Elaborated := False;");
444 if not Bind_Main_Program then
445 if Lib_Final_Built then
446 WBI (" finalize_library;");
453 elsif VM_Target = No_VM then
454 WBI (" s_stalib_adafinal;");
456 -- Pragma Import C cannot be used on virtual machine targets, therefore
457 -- call the runtime finalization routine directly.
460 WBI (" System.Standard_Library.Adafinal;");
463 WBI (" end " & Ada_Final_Name.all & ";");
465 end Gen_Adafinal_Ada;
471 procedure Gen_Adafinal_C is
473 WBI ("void " & Ada_Final_Name.all & " (void) {");
475 WBI (" if (!is_elaborated)");
477 WBI (" is_elaborated = 0;");
479 if not Bind_Main_Program then
480 if Lib_Final_Built then
481 WBI (" finalize_library ();");
487 WBI (" system__standard_library__adafinal ();");
493 ---------------------
494 -- Gen_Adainit_Ada --
495 ---------------------
497 procedure Gen_Adainit_Ada is
498 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
499 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
502 WBI (" procedure " & Ada_Init_Name.all & " is");
504 -- If the standard library is suppressed, then the only global variables
505 -- that might be needed (by the Ravenscar profile) are the priority and
506 -- the processor for the environment task.
508 if Suppress_Standard_Library_On_Target then
509 if Main_Priority /= No_Main_Priority then
510 WBI (" Main_Priority : Integer;");
511 WBI (" pragma Import (C, Main_Priority," &
512 " ""__gl_main_priority"");");
516 if Main_CPU /= No_Main_CPU then
517 WBI (" Main_CPU : Integer;");
518 WBI (" pragma Import (C, Main_CPU," &
519 " ""__gl_main_cpu"");");
525 if Main_Priority /= No_Main_Priority then
526 Set_String (" Main_Priority := ");
527 Set_Int (Main_Priority);
529 Write_Statement_Buffer;
532 if Main_CPU /= No_Main_CPU then
533 Set_String (" Main_CPU := ");
536 Write_Statement_Buffer;
539 if Main_Priority = No_Main_Priority
540 and then Main_CPU = No_Main_CPU
545 -- Normal case (standard library not suppressed). Set all global values
546 -- used by the run time.
549 WBI (" Main_Priority : Integer;");
550 WBI (" pragma Import (C, Main_Priority, " &
551 """__gl_main_priority"");");
552 WBI (" Time_Slice_Value : Integer;");
553 WBI (" pragma Import (C, Time_Slice_Value, " &
554 """__gl_time_slice_val"");");
555 WBI (" WC_Encoding : Character;");
556 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
557 WBI (" Locking_Policy : Character;");
558 WBI (" pragma Import (C, Locking_Policy, " &
559 """__gl_locking_policy"");");
560 WBI (" Queuing_Policy : Character;");
561 WBI (" pragma Import (C, Queuing_Policy, " &
562 """__gl_queuing_policy"");");
563 WBI (" Task_Dispatching_Policy : Character;");
564 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
565 """__gl_task_dispatching_policy"");");
566 WBI (" Priority_Specific_Dispatching : System.Address;");
567 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
568 """__gl_priority_specific_dispatching"");");
569 WBI (" Num_Specific_Dispatching : Integer;");
570 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
571 """__gl_num_specific_dispatching"");");
572 WBI (" Main_CPU : Integer;");
573 WBI (" pragma Import (C, Main_CPU, " &
574 """__gl_main_cpu"");");
576 WBI (" Interrupt_States : System.Address;");
577 WBI (" pragma Import (C, Interrupt_States, " &
578 """__gl_interrupt_states"");");
579 WBI (" Num_Interrupt_States : Integer;");
580 WBI (" pragma Import (C, Num_Interrupt_States, " &
581 """__gl_num_interrupt_states"");");
582 WBI (" Unreserve_All_Interrupts : Integer;");
583 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
584 """__gl_unreserve_all_interrupts"");");
586 if Exception_Tracebacks then
587 WBI (" Exception_Tracebacks : Integer;");
588 WBI (" pragma Import (C, Exception_Tracebacks, " &
589 """__gl_exception_tracebacks"");");
592 WBI (" Zero_Cost_Exceptions : Integer;");
593 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
594 """__gl_zero_cost_exceptions"");");
595 WBI (" Detect_Blocking : Integer;");
596 WBI (" pragma Import (C, Detect_Blocking, " &
597 """__gl_detect_blocking"");");
598 WBI (" Default_Stack_Size : Integer;");
599 WBI (" pragma Import (C, Default_Stack_Size, " &
600 """__gl_default_stack_size"");");
601 WBI (" Leap_Seconds_Support : Integer;");
602 WBI (" pragma Import (C, Leap_Seconds_Support, " &
603 """__gl_leap_seconds_support"");");
605 -- Import entry point for elaboration time signal handler
606 -- installation, and indication of if it's been called previously.
609 WBI (" procedure Install_Handler;");
610 WBI (" pragma Import (C, Install_Handler, " &
611 """__gnat_install_handler"");");
613 WBI (" Handler_Installed : Integer;");
614 WBI (" pragma Import (C, Handler_Installed, " &
615 """__gnat_handler_installed"");");
617 -- The import of the soft link which performs library-level object
618 -- finalization is not needed for VM targets; regular Ada is used in
619 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
620 -- tasks are non-terminating, so we do not want finalization.
622 if VM_Target = No_VM and then not Configurable_Run_Time_On_Target then
624 WBI (" type No_Param_Proc is access procedure;");
625 WBI (" Finalize_Library_Objects : No_Param_Proc;");
626 WBI (" pragma Import (C, Finalize_Library_Objects, " &
627 """__gnat_finalize_library_objects"");");
630 -- Import entry point for environment feature enable/disable
631 -- routine, and indication that it's been called previously.
633 if OpenVMS_On_Target then
635 WBI (" procedure Set_Features;");
636 WBI (" pragma Import (C, Set_Features, " &
637 """__gnat_set_features"");");
639 WBI (" Features_Set : Integer;");
640 WBI (" pragma Import (C, Features_Set, " &
641 """__gnat_features_set"");");
643 if Opt.Heap_Size /= 0 then
645 WBI (" Heap_Size : Integer;");
646 WBI (" pragma Import (C, Heap_Size, " &
647 """__gl_heap_size"");");
649 Write_Statement_Buffer;
653 -- Initialize stack limit variable of the environment task if the
654 -- stack check method is stack limit and stack check is enabled.
656 if Stack_Check_Limits_On_Target
657 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
660 WBI (" procedure Initialize_Stack_Limit;");
661 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
662 """__gnat_initialize_stack_limit"");");
665 -- Special processing when main program is CIL function/procedure
667 if VM_Target = CLI_Target
668 and then Bind_Main_Program
669 and then not No_Main_Subprogram
673 -- Function case, use Set_Exit_Status to report the returned
674 -- status code, since that is the only mechanism available.
676 if ALIs.Table (ALIs.First).Main_Program = Func then
677 WBI (" Result : Integer;");
678 WBI (" procedure Set_Exit_Status (Code : Integer);");
679 WBI (" pragma Import (C, Set_Exit_Status, " &
680 """__gnat_set_exit_status"");");
682 WBI (" function Ada_Main_Program return Integer;");
687 WBI (" procedure Ada_Main_Program;");
690 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
691 Name_Len := Name_Len - 2;
692 WBI (" pragma Import (CIL, Ada_Main_Program, """
693 & Name_Buffer (1 .. Name_Len) & "."
694 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
699 WBI (" if Is_Elaborated then");
702 WBI (" Is_Elaborated := True;");
704 Set_String (" Main_Priority := ");
705 Set_Int (Main_Priority);
707 Write_Statement_Buffer;
709 Set_String (" Time_Slice_Value := ");
711 if Task_Dispatching_Policy_Specified = 'F'
712 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
716 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
720 Write_Statement_Buffer;
722 Set_String (" WC_Encoding := '");
723 Set_Char (Get_WC_Encoding);
726 Write_Statement_Buffer;
728 Set_String (" Locking_Policy := '");
729 Set_Char (Locking_Policy_Specified);
731 Write_Statement_Buffer;
733 Set_String (" Queuing_Policy := '");
734 Set_Char (Queuing_Policy_Specified);
736 Write_Statement_Buffer;
738 Set_String (" Task_Dispatching_Policy := '");
739 Set_Char (Task_Dispatching_Policy_Specified);
741 Write_Statement_Buffer;
743 Gen_Restrictions_Ada;
745 WBI (" Priority_Specific_Dispatching :=");
746 WBI (" Local_Priority_Specific_Dispatching'Address;");
748 Set_String (" Num_Specific_Dispatching := ");
749 Set_Int (PSD_Pragma_Settings.Last + 1);
751 Write_Statement_Buffer;
753 Set_String (" Main_CPU := ");
756 Write_Statement_Buffer;
758 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
760 Set_String (" Num_Interrupt_States := ");
761 Set_Int (IS_Pragma_Settings.Last + 1);
763 Write_Statement_Buffer;
765 Set_String (" Unreserve_All_Interrupts := ");
767 if Unreserve_All_Interrupts_Specified then
774 Write_Statement_Buffer;
776 if Exception_Tracebacks then
777 WBI (" Exception_Tracebacks := 1;");
780 Set_String (" Zero_Cost_Exceptions := ");
782 if Zero_Cost_Exceptions_Specified then
789 Write_Statement_Buffer;
791 Set_String (" Detect_Blocking := ");
793 if Detect_Blocking then
800 Write_Statement_Buffer;
802 Set_String (" Default_Stack_Size := ");
803 Set_Int (Default_Stack_Size);
805 Write_Statement_Buffer;
807 Set_String (" Leap_Seconds_Support := ");
809 if Leap_Seconds_Support then
816 Write_Statement_Buffer;
818 -- Generate call to Install_Handler
820 -- In .NET, when binding with -z, we don't install the signal handler
821 -- to let the caller handle the last exception handler.
823 if VM_Target /= CLI_Target
824 or else Bind_Main_Program
827 WBI (" if Handler_Installed = 0 then");
828 WBI (" Install_Handler;");
832 -- Generate call to Set_Features
834 if OpenVMS_On_Target then
836 WBI (" if Features_Set = 0 then");
837 WBI (" Set_Features;");
840 -- Features_Set may twiddle the heap size according to a logical
841 -- name, but the binder switch must override.
843 if Opt.Heap_Size /= 0 then
844 Set_String (" Heap_Size := ");
845 Set_Int (Opt.Heap_Size);
847 Write_Statement_Buffer;
852 -- Generate call to set Initialize_Scalar values if active
854 if Initialize_Scalars_Used then
856 Set_String (" System.Scalar_Values.Initialize ('");
857 Set_Char (Initialize_Scalars_Mode1);
859 Set_Char (Initialize_Scalars_Mode2);
861 Write_Statement_Buffer;
864 -- Generate assignment of default secondary stack size if set
866 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
868 Set_String (" System.Secondary_Stack.");
869 Set_String ("Default_Secondary_Stack_Size := ");
870 Set_Int (Opt.Default_Sec_Stack_Size);
872 Write_Statement_Buffer;
875 -- Initialize stack limit variable of the environment task if the
876 -- stack check method is stack limit and stack check is enabled.
878 if Stack_Check_Limits_On_Target
879 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
882 WBI (" Initialize_Stack_Limit;");
885 -- In the main program case, attach finalize_library to the soft link.
886 -- Do it only when not using a restricted run time, in which case tasks
887 -- are non-terminating, so we do not want library-level finalization.
890 and then not Configurable_Run_Time_On_Target
891 and then not Suppress_Standard_Library_On_Target
895 if VM_Target = No_VM then
896 if Lib_Final_Built then
897 Set_String (" Finalize_Library_Objects := ");
898 Set_String ("finalize_library'access;");
900 Set_String (" Finalize_Library_Objects := null;");
903 -- On VM targets use regular Ada to set the soft link
906 if Lib_Final_Built then
908 (" System.Soft_Links.Finalize_Library_Objects");
909 Set_String (" := finalize_library'access;");
912 (" System.Soft_Links.Finalize_Library_Objects");
913 Set_String (" := null;");
917 Write_Statement_Buffer;
920 -- Generate elaboration calls
925 -- Case of main program is CIL function or procedure
927 if VM_Target = CLI_Target
928 and then Bind_Main_Program
929 and then not No_Main_Subprogram
931 -- For function case, use Set_Exit_Status to set result
933 if ALIs.Table (ALIs.First).Main_Program = Func then
934 WBI (" Result := Ada_Main_Program;");
935 WBI (" Set_Exit_Status (Result);");
940 WBI (" Ada_Main_Program;");
944 WBI (" end " & Ada_Init_Name.all & ";");
952 procedure Gen_Adainit_C is
953 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
954 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
957 WBI ("void " & Ada_Init_Name.all & " (void)");
960 WBI (" if (is_elaborated)");
962 WBI (" is_elaborated = 1;");
964 -- Standard library suppressed
966 if Suppress_Standard_Library_On_Target then
968 -- Case of High_Integrity_Mode mode. Set __gl_main_priority and
969 -- __gl_main_cpu if needed for the Ravenscar profile.
971 if Main_Priority /= No_Main_Priority then
972 WBI (" extern int __gl_main_priority;");
973 Set_String (" __gl_main_priority = ");
974 Set_Int (Main_Priority);
976 Write_Statement_Buffer;
979 if Main_CPU /= No_Main_CPU then
980 WBI (" extern int __gl_main_cpu;");
981 Set_String (" __gl_main_cpu = ");
984 Write_Statement_Buffer;
987 -- Normal case (standard library not suppressed)
990 -- Generate definition for interrupt states string
992 Set_String (" static const char *local_interrupt_states = """);
994 for J in 0 .. IS_Pragma_Settings.Last loop
995 Set_Char (IS_Pragma_Settings.Table (J));
999 Write_Statement_Buffer;
1001 -- Generate definition for priority specific dispatching string
1004 (" static const char *local_priority_specific_dispatching = """);
1006 for J in 0 .. PSD_Pragma_Settings.Last loop
1007 Set_Char (PSD_Pragma_Settings.Table (J));
1011 Write_Statement_Buffer;
1013 -- Generate declaration for secondary stack default if needed
1015 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1016 WBI (" extern int system__secondary_stack__" &
1017 "default_secondary_stack_size;");
1022 -- Code for normal case (standard library not suppressed)
1024 -- We call the routine from inside adainit() because this works for
1025 -- both programs with and without binder generated "main" functions.
1027 WBI (" extern int __gl_main_priority;");
1028 Set_String (" __gl_main_priority = ");
1029 Set_Int (Main_Priority);
1031 Write_Statement_Buffer;
1033 WBI (" extern int __gl_time_slice_val;");
1034 Set_String (" __gl_time_slice_val = ");
1036 if Task_Dispatching_Policy = 'F'
1037 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
1041 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
1045 Write_Statement_Buffer;
1047 WBI (" extern char __gl_wc_encoding;");
1048 Set_String (" __gl_wc_encoding = '");
1049 Set_Char (Get_WC_Encoding);
1052 Write_Statement_Buffer;
1054 WBI (" extern char __gl_locking_policy;");
1055 Set_String (" __gl_locking_policy = '");
1056 Set_Char (Locking_Policy_Specified);
1058 Write_Statement_Buffer;
1060 WBI (" extern char __gl_queuing_policy;");
1061 Set_String (" __gl_queuing_policy = '");
1062 Set_Char (Queuing_Policy_Specified);
1064 Write_Statement_Buffer;
1066 WBI (" extern char __gl_task_dispatching_policy;");
1067 Set_String (" __gl_task_dispatching_policy = '");
1068 Set_Char (Task_Dispatching_Policy_Specified);
1070 Write_Statement_Buffer;
1072 WBI (" extern int __gl_main_cpu;");
1073 Set_String (" __gl_main_cpu = ");
1076 Write_Statement_Buffer;
1080 WBI (" extern const void *__gl_interrupt_states;");
1081 WBI (" __gl_interrupt_states = local_interrupt_states;");
1083 WBI (" extern int __gl_num_interrupt_states;");
1084 Set_String (" __gl_num_interrupt_states = ");
1085 Set_Int (IS_Pragma_Settings.Last + 1);
1087 Write_Statement_Buffer;
1089 WBI (" extern const void *__gl_priority_specific_dispatching;");
1090 WBI (" __gl_priority_specific_dispatching =" &
1091 " local_priority_specific_dispatching;");
1093 WBI (" extern int __gl_num_specific_dispatching;");
1094 Set_String (" __gl_num_specific_dispatching = ");
1095 Set_Int (PSD_Pragma_Settings.Last + 1);
1097 Write_Statement_Buffer;
1099 WBI (" extern int __gl_unreserve_all_interrupts;");
1100 Set_String (" __gl_unreserve_all_interrupts = ");
1101 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
1103 Write_Statement_Buffer;
1105 if Exception_Tracebacks then
1106 WBI (" extern int __gl_exception_tracebacks;");
1107 WBI (" __gl_exception_tracebacks = 1;");
1110 WBI (" extern int __gl_zero_cost_exceptions;");
1111 Set_String (" __gl_zero_cost_exceptions = ");
1112 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
1114 Write_Statement_Buffer;
1116 WBI (" extern int __gl_detect_blocking;");
1117 Set_String (" __gl_detect_blocking = ");
1119 if Detect_Blocking then
1126 Write_Statement_Buffer;
1128 WBI (" extern int __gl_default_stack_size;");
1129 Set_String (" __gl_default_stack_size = ");
1130 Set_Int (Default_Stack_Size);
1132 Write_Statement_Buffer;
1134 WBI (" extern int __gl_leap_seconds_support;");
1135 Set_String (" __gl_leap_seconds_support = ");
1137 if Leap_Seconds_Support then
1144 Write_Statement_Buffer;
1146 -- Import entry point for elaboration time signal handler
1147 -- installation, and indication of if it's been called previously.
1149 WBI (" extern int __gnat_handler_installed;");
1152 -- Install elaboration time signal handler
1154 WBI (" if (__gnat_handler_installed == 0)");
1155 WBI (" __gnat_install_handler ();");
1157 -- Import entry point for environment feature enable/disable
1158 -- routine, and indication that it's been called previously.
1160 if OpenVMS_On_Target then
1161 WBI (" extern int __gnat_features_set;");
1164 WBI (" if (__gnat_features_set == 0)");
1165 WBI (" __gnat_set_features ();");
1169 -- Initialize stack limit for the environment task if the stack
1170 -- check method is stack limit and stack check is enabled.
1172 if Stack_Check_Limits_On_Target
1173 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1176 WBI (" __gnat_initialize_stack_limit ();");
1179 -- Generate call to set Initialize_Scalar values if needed
1181 if Initialize_Scalars_Used then
1183 Set_String (" system__scalar_values__initialize('");
1184 Set_Char (Initialize_Scalars_Mode1);
1185 Set_String ("', '");
1186 Set_Char (Initialize_Scalars_Mode2);
1188 Write_Statement_Buffer;
1191 -- Generate assignment of default secondary stack size if set
1193 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1195 Set_String (" system__secondary_stack__");
1196 Set_String ("default_secondary_stack_size = ");
1197 Set_Int (Opt.Default_Sec_Stack_Size);
1199 Write_Statement_Buffer;
1202 -- In the main program case, attach finalize_library to the soft link.
1203 -- Do it only when not using a restricted run time, in which case tasks
1204 -- are non-terminating, so we do not want library-level finalization.
1206 if Bind_Main_Program
1207 and then not Configurable_Run_Time_On_Target
1208 and then not Suppress_Standard_Library_On_Target
1211 WBI (" extern void (*__gnat_finalize_library_objects)(void);");
1213 if Lib_Final_Built then
1214 Set_String (" __gnat_finalize_library_objects = ");
1215 Set_String ("&finalize_library;");
1217 Set_String (" __gnat_finalize_library_objects = 0;");
1220 Write_Statement_Buffer;
1223 -- Generate elaboration calls
1231 ----------------------------
1232 -- Gen_Elab_Externals_Ada --
1233 ----------------------------
1235 procedure Gen_Elab_Externals_Ada is
1237 for E in Elab_Order.First .. Elab_Order.Last loop
1239 Unum : constant Unit_Id := Elab_Order.Table (E);
1240 U : Unit_Record renames Units.Table (Unum);
1243 -- Check for Elab_Entity to be set for this unit
1245 if U.Set_Elab_Entity
1247 -- Don't generate reference for stand alone library
1249 and then not U.SAL_Interface
1251 -- Don't generate reference for predefined file in No_Run_Time
1252 -- mode, since we don't include the object files in this case
1256 and then Is_Predefined_File_Name (U.Sfile))
1260 Set_Unit_Number (Unum);
1263 when No_VM | JVM_Target =>
1264 Set_String (" : Short_Integer; pragma Import (Ada, ");
1266 Set_String (" : Short_Integer; pragma Import (CIL, ");
1270 Set_Unit_Number (Unum);
1271 Set_String (", """);
1272 Get_Name_String (U.Uname);
1274 -- In the case of JGNAT we need to emit an Import name that
1275 -- includes the class name (using '$' separators in the case
1276 -- of a child unit name).
1278 if VM_Target /= No_VM then
1279 for J in 1 .. Name_Len - 2 loop
1280 if VM_Target = CLI_Target
1281 or else Name_Buffer (J) /= '.'
1283 Set_Char (Name_Buffer (J));
1289 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
1292 Set_String ("_pkg.");
1295 -- If the unit name is very long, then split the
1296 -- Import link name across lines using "&" (occurs
1297 -- in some C2 tests).
1299 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
1300 Set_String (""" &");
1301 Write_Statement_Buffer;
1307 Set_String ("_E"");");
1308 Write_Statement_Buffer;
1314 end Gen_Elab_Externals_Ada;
1316 --------------------------
1317 -- Gen_Elab_Externals_C --
1318 --------------------------
1320 procedure Gen_Elab_Externals_C is
1322 for E in Elab_Order.First .. Elab_Order.Last loop
1324 Unum : constant Unit_Id := Elab_Order.Table (E);
1325 U : Unit_Record renames Units.Table (Unum);
1328 -- Check for Elab entity to be set for this unit
1330 if U.Set_Elab_Entity
1332 -- Don't generate reference for stand alone library
1334 and then not U.SAL_Interface
1336 -- Don't generate reference for predefined file in No_Run_Time
1337 -- mode, since we don't include the object files in this case
1341 and then Is_Predefined_File_Name (U.Sfile))
1343 Set_String ("extern short int ");
1344 Get_Name_String (U.Uname);
1347 Write_Statement_Buffer;
1353 end Gen_Elab_Externals_C;
1355 ------------------------
1356 -- Gen_Elab_Calls_Ada --
1357 ------------------------
1359 procedure Gen_Elab_Calls_Ada is
1361 for E in Elab_Order.First .. Elab_Order.Last loop
1363 Unum : constant Unit_Id := Elab_Order.Table (E);
1364 U : Unit_Record renames Units.Table (Unum);
1366 Unum_Spec : Unit_Id;
1367 -- This is the unit number of the spec that corresponds to
1368 -- this entry. It is the same as Unum except when the body
1369 -- and spec are different and we are currently processing
1370 -- the body, in which case it is the spec (Unum + 1).
1373 if U.Utype = Is_Body then
1374 Unum_Spec := Unum + 1;
1379 -- Nothing to do if predefined unit in no run time mode
1381 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1384 -- Likewise if this is an interface to a stand alone library
1386 elsif U.SAL_Interface then
1389 -- Case of no elaboration code
1391 elsif U.No_Elab then
1393 -- The only case in which we have to do something is if this
1394 -- is a body, with a separate spec, where the separate spec
1395 -- has an elaboration entity defined. In that case, this is
1396 -- where we increment the elaboration entity.
1398 if U.Utype = Is_Body
1399 and then Units.Table (Unum_Spec).Set_Elab_Entity
1402 Set_Unit_Number (Unum_Spec);
1403 Set_String (" := E");
1404 Set_Unit_Number (Unum_Spec);
1405 Set_String (" + 1;");
1406 Write_Statement_Buffer;
1409 -- Here if elaboration code is present. If binding a library
1410 -- or if there is a non-Ada main subprogram then we generate:
1412 -- if uname_E = 0 then
1413 -- uname'elab_[spec|body];
1415 -- uname_E := uname_E + 1;
1417 -- Otherwise, elaboration routines are called unconditionally:
1419 -- uname'elab_[spec|body];
1420 -- uname_E := uname_E + 1;
1422 -- The uname_E increment is skipped if this is a separate spec,
1423 -- since it will be done when we process the body.
1426 if Force_Checking_Of_Elaboration_Flags
1427 or Interface_Library_Unit
1428 or not Bind_Main_Program
1430 Set_String (" if E");
1431 Set_Unit_Number (Unum_Spec);
1432 Set_String (" = 0 then");
1433 Write_Statement_Buffer;
1438 Get_Decoded_Name_String_With_Brackets (U.Uname);
1440 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1441 if Name_Buffer (Name_Len) = 's' then
1442 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1445 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1449 Name_Len := Name_Len + 12;
1452 if Name_Buffer (Name_Len) = 's' then
1453 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1456 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1460 Name_Len := Name_Len + 8;
1463 Set_Casing (U.Icasing);
1466 Write_Statement_Buffer;
1468 if Force_Checking_Of_Elaboration_Flags
1469 or Interface_Library_Unit
1470 or not Bind_Main_Program
1475 if U.Utype /= Is_Spec then
1477 Set_Unit_Number (Unum_Spec);
1478 Set_String (" := E");
1479 Set_Unit_Number (Unum_Spec);
1480 Set_String (" + 1;");
1481 Write_Statement_Buffer;
1486 end Gen_Elab_Calls_Ada;
1488 ----------------------
1489 -- Gen_Elab_Calls_C --
1490 ----------------------
1492 procedure Gen_Elab_Calls_C is
1494 for E in Elab_Order.First .. Elab_Order.Last loop
1496 Unum : constant Unit_Id := Elab_Order.Table (E);
1497 U : Unit_Record renames Units.Table (Unum);
1499 Unum_Spec : Unit_Id;
1500 -- This is the unit number of the spec that corresponds to
1501 -- this entry. It is the same as Unum except when the body
1502 -- and spec are different and we are currently processing
1503 -- the body, in which case it is the spec (Unum + 1).
1506 if U.Utype = Is_Body then
1507 Unum_Spec := Unum + 1;
1512 -- Nothing to do if predefined unit in no run time mode
1514 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1517 -- Likewise if this is an interface to a stand alone library
1519 elsif U.SAL_Interface then
1522 -- Case of no elaboration code
1524 elsif U.No_Elab then
1526 -- The only case in which we have to do something is if this
1527 -- is a body, with a separate spec, where the separate spec
1528 -- has an elaboration entity defined. In that case, this is
1529 -- where we increment the elaboration entity.
1531 if U.Utype = Is_Body
1532 and then Units.Table (Unum_Spec).Set_Elab_Entity
1534 Get_Name_String (U.Uname);
1538 Set_String ("_E++;");
1539 Write_Statement_Buffer;
1542 -- Here if elaboration code is present. If binding a library
1543 -- or if there is a non-Ada main subprogram then we generate:
1545 -- if (uname_E == 0)
1546 -- uname__elab[s|b] ();
1549 -- Otherwise, elaboration routines are called unconditionally:
1551 -- uname__elab[s|b] ();
1554 -- The uname_E increment is skipped if this is a separate spec,
1555 -- since it will be done when we process the body.
1558 Get_Name_String (U.Uname);
1560 if Force_Checking_Of_Elaboration_Flags
1561 or Interface_Library_Unit
1562 or not Bind_Main_Program
1564 Set_String (" if (");
1566 Set_String ("_E == 0)");
1567 Write_Statement_Buffer;
1573 Set_String ("___elab");
1574 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1575 Set_String (" ();");
1576 Write_Statement_Buffer;
1578 if U.Utype /= Is_Spec then
1581 Set_String ("_E++;");
1582 Write_Statement_Buffer;
1587 end Gen_Elab_Calls_C;
1589 ----------------------
1590 -- Gen_Elab_Defs_C --
1591 ----------------------
1593 procedure Gen_Elab_Defs_C is
1595 WBI ("/* BEGIN ELABORATION DEFINITIONS */");
1597 for E in Elab_Order.First .. Elab_Order.Last loop
1599 -- Generate declaration of elaboration procedure if elaboration
1600 -- needed. Note that passive units are always excluded.
1602 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1603 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1604 Set_String ("extern void ");
1606 Set_String ("___elab");
1607 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1608 Set_String (" (void);");
1609 Write_Statement_Buffer;
1613 WBI ("/* END ELABORATION DEFINITIONS */");
1615 end Gen_Elab_Defs_C;
1617 ------------------------
1618 -- Gen_Elab_Order_Ada --
1619 ------------------------
1621 procedure Gen_Elab_Order_Ada is
1623 WBI (" -- BEGIN ELABORATION ORDER");
1625 for J in Elab_Order.First .. Elab_Order.Last loop
1626 Set_String (" -- ");
1627 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1629 Write_Statement_Buffer;
1632 WBI (" -- END ELABORATION ORDER");
1634 end Gen_Elab_Order_Ada;
1636 ----------------------
1637 -- Gen_Elab_Order_C --
1638 ----------------------
1640 procedure Gen_Elab_Order_C is
1642 WBI ("/* BEGIN ELABORATION ORDER");
1644 for J in Elab_Order.First .. Elab_Order.Last loop
1645 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1647 Write_Statement_Buffer;
1650 WBI (" END ELABORATION ORDER */");
1652 end Gen_Elab_Order_C;
1654 ------------------------------
1655 -- Gen_Finalize_Library_Ada --
1656 ------------------------------
1658 procedure Gen_Finalize_Library_Ada is
1661 Uspec : Unit_Record;
1665 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1666 Unum := Elab_Order.Table (E);
1667 U := Units.Table (Unum);
1669 -- We are only interested in non-generic packages
1671 if U.Unit_Kind = 'p'
1672 and then U.Has_Finalizer
1673 and then not U.Is_Generic
1674 and then not U.SAL_Interface
1675 and then not U.No_Elab
1677 if not Lib_Final_Built then
1678 Lib_Final_Built := True;
1680 WBI (" procedure finalize_library is");
1682 -- The following flag is used to check for library-level
1683 -- exceptions raised during finalization. The symbol comes
1684 -- from System.Soft_Links. VM targets use regular Ada to
1685 -- reference the entity.
1687 if VM_Target = No_VM then
1688 WBI (" LE_Set : Boolean;");
1690 Set_String (" pragma Import (Ada, LE_Set, ");
1691 Set_String ("""__gnat_library_exception_set"");");
1692 Write_Statement_Buffer;
1700 -- procedure F<Count>;
1702 Set_String (" declare");
1703 Write_Statement_Buffer;
1705 Set_String (" procedure F");
1708 Write_Statement_Buffer;
1711 -- pragma Import (CIL, F<Count>,
1712 -- "xx.yy_pkg.xx__yy__finalize_[body|spec]");
1713 -- -- for .NET targets
1715 -- pragma Import (Java, F<Count>,
1716 -- "xx$yy.xx__yy__finalize_[body|spec]");
1717 -- -- for JVM targets
1719 -- pragma Import (Ada, F<Count>,
1720 -- "xx__yy__finalize_[body|spec]");
1721 -- -- for default targets
1723 if VM_Target = CLI_Target then
1724 Set_String (" pragma Import (CIL, F");
1725 elsif VM_Target = JVM_Target then
1726 Set_String (" pragma Import (Java, F");
1728 Set_String (" pragma Import (Ada, F");
1732 Set_String (", """);
1734 -- Dealing with package bodies is a little complicated. In such
1735 -- cases we must retrieve the package spec since it contains the
1736 -- spec of the body finalizer.
1738 if U.Utype = Is_Body then
1740 Uspec := Units.Table (Unum);
1745 Get_Name_String (Uspec.Uname);
1747 -- Perform name construction
1749 -- .NET xx.yy_pkg.xx__yy__finalize
1751 if VM_Target = CLI_Target then
1752 Set_Unit_Name (Mode => Dot);
1753 Set_String ("_pkg.");
1755 -- JVM xx$yy.xx__yy__finalize
1757 elsif VM_Target = JVM_Target then
1758 Set_Unit_Name (Mode => Dollar_Sign);
1762 -- Default xx__yy__finalize
1765 Set_String ("__finalize_");
1767 -- Package spec processing
1769 if U.Utype = Is_Spec
1770 or else U.Utype = Is_Spec_Only
1772 Set_String ("spec");
1774 -- Package body processing
1777 Set_String ("body");
1780 Set_String (""");");
1781 Write_Statement_Buffer;
1783 -- If binding a library or if there is a non-Ada main subprogram
1784 -- then we generate:
1787 -- uname_E := uname_E - 1;
1788 -- if uname_E = 0 then
1793 -- Otherwise, finalization routines are called unconditionally:
1796 -- uname_E := uname_E - 1;
1802 Set_Unit_Number (Unum);
1803 Set_String (" := E");
1804 Set_Unit_Number (Unum);
1805 Set_String (" - 1;");
1806 Write_Statement_Buffer;
1808 if Interface_Library_Unit or not Bind_Main_Program then
1809 Set_String (" if E");
1810 Set_Unit_Number (Unum);
1811 Set_String (" = 0 then");
1812 Write_Statement_Buffer;
1819 Write_Statement_Buffer;
1821 if Interface_Library_Unit or not Bind_Main_Program then
1831 if Lib_Final_Built then
1833 -- It is possible that the finalization of a library-level object
1834 -- raised an exception. In that case import the actual exception
1835 -- and the routine necessary to raise it.
1837 if VM_Target = No_VM then
1838 WBI (" if LE_Set then");
1840 WBI (" LE : Ada.Exceptions.Exception_Occurrence;");
1842 Set_String (" pragma Import (Ada, LE, ");
1843 Set_String ("""__gnat_library_exception"");");
1844 Write_Statement_Buffer;
1846 Set_String (" procedure Raise_From_Controlled_");
1847 Set_String ("Operation ");
1848 Set_String ("(X : Ada.Exceptions.Exception_Occurrence; ");
1849 Set_String (" From_Abort : Boolean);");
1850 Write_Statement_Buffer;
1852 Set_String (" pragma Import (Ada, Raise_From_");
1853 Set_String ("Controlled_Operation, ");
1854 Set_String ("""__gnat_raise_from_controlled_operation"");");
1855 Write_Statement_Buffer;
1858 WBI (" Raise_From_Controlled_Operation (LE, False);");
1861 -- VM-specific code, use regular Ada to produce the desired behavior
1864 WBI (" if System.Soft_Links.Library_Exception_Set then");
1866 Set_String (" Ada.Exceptions.Reraise_Occurrence (");
1867 Set_String ("System.Soft_Links.Library_Exception);");
1868 Write_Statement_Buffer;
1872 WBI (" end finalize_library;");
1875 end Gen_Finalize_Library_Ada;
1877 ----------------------------
1878 -- Gen_Finalize_Library_C --
1879 ----------------------------
1881 procedure Gen_Finalize_Library_C is
1883 Uspec : Unit_Record;
1887 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1888 Unum := Elab_Order.Table (E);
1889 U := Units.Table (Unum);
1891 -- We are only interested in non-generic packages
1893 if U.Unit_Kind = 'p'
1894 and then U.Has_Finalizer
1895 and then not U.Is_Generic
1896 and then not U.SAL_Interface
1897 and then not U.No_Elab
1899 if not Lib_Final_Built then
1900 Lib_Final_Built := True;
1902 WBI ("static void finalize_library(void) {");
1905 -- Dealing with package bodies is a little complicated. In such
1906 -- cases we must retrieve the package spec since it contains the
1907 -- spec of the body finalizer.
1909 if U.Utype = Is_Body then
1911 Uspec := Units.Table (Unum);
1916 Get_Name_String (Uspec.Uname);
1918 -- If binding a library or if there is a non-Ada main subprogram
1919 -- then we generate:
1922 -- if (uname_E == 0)
1923 -- uname__finalize_[spec|body] ();
1925 -- Otherwise, finalization routines are called unconditionally:
1928 -- uname__finalize_[spec|body] ();
1932 Set_String ("_E--;");
1933 Write_Statement_Buffer;
1935 if Interface_Library_Unit or not Bind_Main_Program then
1936 Set_String (" if (");
1938 Set_String ("_E == 0)");
1939 Write_Statement_Buffer;
1944 Get_Name_String (Uspec.Uname);
1946 Set_String ("__finalize_");
1948 -- Package spec processing
1950 if U.Utype = Is_Spec
1951 or else U.Utype = Is_Spec_Only
1953 Set_String ("spec");
1955 -- Package body processing
1958 Set_String ("body");
1961 Set_String (" ();");
1963 Write_Statement_Buffer;
1967 if Lib_Final_Built then
1971 end Gen_Finalize_Library_C;
1973 ---------------------------------
1974 -- Gen_Finalize_Library_Defs_C --
1975 ---------------------------------
1977 procedure Gen_Finalize_Library_Defs_C is
1979 Uspec : Unit_Record;
1983 WBI ("/* BEGIN FINALIZE DEFINITIONS */");
1985 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1986 Unum := Elab_Order.Table (E);
1987 U := Units.Table (Unum);
1989 -- We are only interested in non-generic packages
1991 if U.Unit_Kind = 'p'
1992 and then U.Has_Finalizer
1993 and then not U.Is_Generic
1994 and then not U.No_Elab
1996 -- Dealing with package bodies is a little complicated. In such
1997 -- cases we must retrieve the package spec since it contains the
1998 -- spec of the body finalizer.
2000 if U.Utype = Is_Body then
2002 Uspec := Units.Table (Unum);
2007 Set_String ("extern void ");
2008 Get_Name_String (Uspec.Uname);
2010 Set_String ("__finalize_");
2012 if U.Utype = Is_Spec
2013 or else U.Utype = Is_Spec_Only
2015 Set_String ("spec");
2017 Set_String ("body");
2020 Set_String (" (void);");
2021 Write_Statement_Buffer;
2025 WBI ("/* END FINALIZE DEFINITIONS */");
2027 end Gen_Finalize_Library_Defs_C;
2033 procedure Gen_Main_Ada is
2035 if Exit_Status_Supported_On_Target then
2036 Set_String (" function ");
2038 Set_String (" procedure ");
2041 Set_String (Get_Main_Name);
2043 if Command_Line_Args_On_Target then
2044 Write_Statement_Buffer;
2045 WBI (" (argc : Integer;");
2046 WBI (" argv : System.Address;");
2047 WBI (" envp : System.Address)");
2049 if Exit_Status_Supported_On_Target then
2050 WBI (" return Integer");
2056 if Exit_Status_Supported_On_Target then
2057 Set_String (" return Integer is");
2062 Write_Statement_Buffer;
2065 if Opt.Default_Exit_Status /= 0
2066 and then Bind_Main_Program
2067 and then not Configurable_Run_Time_Mode
2069 WBI (" procedure Set_Exit_Status (Status : Integer);");
2070 WBI (" pragma Import (C, Set_Exit_Status, " &
2071 """__gnat_set_exit_status"");");
2075 -- Initialize and Finalize
2077 if not Cumulative_Restrictions.Set (No_Finalization) then
2078 WBI (" procedure Initialize (Addr : System.Address);");
2079 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
2081 WBI (" procedure Finalize;");
2082 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
2085 -- If we want to analyze the stack, we have to import corresponding
2088 if Dynamic_Stack_Measurement then
2090 WBI (" procedure Output_Results;");
2091 WBI (" pragma Import (C, Output_Results, " &
2092 """__gnat_stack_usage_output_results"");");
2096 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
2097 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
2098 """__gnat_stack_usage_initialize"");");
2101 -- Deal with declarations for main program case
2103 if not No_Main_Subprogram then
2105 -- To call the main program, we declare it using a pragma Import
2106 -- Ada with the right link name.
2108 -- It might seem more obvious to "with" the main program, and call
2109 -- it in the normal Ada manner. We do not do this for three reasons:
2111 -- 1. It is more efficient not to recompile the main program
2112 -- 2. We are not entitled to assume the source is accessible
2113 -- 3. We don't know what options to use to compile it
2115 -- It is really reason 3 that is most critical (indeed we used
2116 -- to generate the "with", but several regression tests failed).
2120 if ALIs.Table (ALIs.First).Main_Program = Func then
2121 WBI (" Result : Integer;");
2123 WBI (" function Ada_Main_Program return Integer;");
2126 WBI (" procedure Ada_Main_Program;");
2129 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
2130 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2131 Set_Main_Program_Name;
2132 Set_String (""");");
2134 Write_Statement_Buffer;
2137 if Bind_Main_Program
2138 and then not Suppress_Standard_Library_On_Target
2140 WBI (" SEH : aliased array (1 .. 2) of Integer;");
2145 -- Generate a reference to Ada_Main_Program_Name. This symbol is
2146 -- not referenced elsewhere in the generated program, but is needed
2147 -- by the debugger (that's why it is generated in the first place).
2148 -- The reference stops Ada_Main_Program_Name from being optimized
2149 -- away by smart linkers, such as the AiX linker.
2151 -- Because this variable is unused, we make this variable "aliased"
2152 -- with a pragma Volatile in order to tell the compiler to preserve
2153 -- this variable at any level of optimization.
2155 if Bind_Main_Program then
2157 (" Ensure_Reference : aliased System.Address := " &
2158 "Ada_Main_Program_Name'Address;");
2159 WBI (" pragma Volatile (Ensure_Reference);");
2165 -- Acquire command line arguments if present on target
2167 if Command_Line_Args_On_Target then
2168 WBI (" gnat_argc := argc;");
2169 WBI (" gnat_argv := argv;");
2170 WBI (" gnat_envp := envp;");
2173 -- If configurable run time and no command line args, then nothing
2174 -- needs to be done since the gnat_argc/argv/envp variables are
2175 -- suppressed in this case.
2177 elsif Configurable_Run_Time_On_Target then
2180 -- Otherwise set dummy values (to be filled in by some other unit?)
2183 WBI (" gnat_argc := 0;");
2184 WBI (" gnat_argv := System.Null_Address;");
2185 WBI (" gnat_envp := System.Null_Address;");
2188 if Opt.Default_Exit_Status /= 0
2189 and then Bind_Main_Program
2190 and then not Configurable_Run_Time_Mode
2192 Set_String (" Set_Exit_Status (");
2193 Set_Int (Opt.Default_Exit_Status);
2195 Write_Statement_Buffer;
2198 if Dynamic_Stack_Measurement then
2199 Set_String (" Initialize_Stack_Analysis (");
2200 Set_Int (Dynamic_Stack_Measurement_Array_Size);
2202 Write_Statement_Buffer;
2205 if not Cumulative_Restrictions.Set (No_Finalization) then
2206 if not No_Main_Subprogram
2207 and then Bind_Main_Program
2208 and then not Suppress_Standard_Library_On_Target
2210 WBI (" Initialize (SEH'Address);");
2212 WBI (" Initialize (System.Null_Address);");
2216 WBI (" " & Ada_Init_Name.all & ";");
2218 if not No_Main_Subprogram then
2219 WBI (" Break_Start;");
2221 if CodePeer_Mode then
2222 -- Bypass Ada_Main_Program; its Import pragma confuses CodePeer.
2223 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2225 Callee_Name : String renames Name_Buffer (1 .. Name_Len - 2);
2226 -- strip trailing "%b"
2228 if ALIs.Table (ALIs.First).Main_Program = Proc then
2229 WBI (" " & Callee_Name & ";");
2231 WBI (" Result := " & Callee_Name & ";");
2234 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
2235 WBI (" Ada_Main_Program;");
2237 WBI (" Result := Ada_Main_Program;");
2241 -- Adafinal call is skipped if no finalization
2243 if not Cumulative_Restrictions.Set (No_Finalization) then
2247 -- Prints the result of static stack analysis
2249 if Dynamic_Stack_Measurement then
2250 WBI (" Output_Results;");
2253 -- Finalize is only called if we have a run time
2255 if not Cumulative_Restrictions.Set (No_Finalization) then
2261 if Exit_Status_Supported_On_Target then
2262 if No_Main_Subprogram
2263 or else ALIs.Table (ALIs.First).Main_Program = Proc
2265 WBI (" return (gnat_exit_status);");
2267 WBI (" return (Result);");
2279 procedure Gen_Main_C is
2281 if Exit_Status_Supported_On_Target then
2282 WBI ("#include <stdlib.h>");
2284 Set_String ("int ");
2286 Set_String ("void ");
2289 Set_String (Get_Main_Name);
2291 -- Generate command line args in prototype if present on target
2293 if Command_Line_Args_On_Target then
2294 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
2296 -- Case of no command line arguments on target
2299 Write_Statement_Buffer (" (void)");
2304 -- Generate a reference to __gnat_ada_main_program_name. This symbol
2305 -- is not referenced elsewhere in the generated program, but is
2306 -- needed by the debugger (that's why it is generated in the first
2307 -- place). The reference stops Ada_Main_Program_Name from being
2308 -- optimized away by smart linkers, such as the AiX linker.
2310 -- Because this variable is unused, we declare this variable as
2311 -- volatile in order to tell the compiler to preserve it at any
2312 -- level of optimization.
2314 if Bind_Main_Program then
2315 WBI (" char * volatile ensure_reference " &
2316 "__attribute__ ((__unused__)) = " &
2317 "__gnat_ada_main_program_name;");
2320 if not Suppress_Standard_Library_On_Target
2321 and then not No_Main_Subprogram
2323 WBI (" int SEH [2];");
2328 -- If main program is a function, generate result variable
2330 if ALIs.Table (ALIs.First).Main_Program = Func then
2331 WBI (" int result;");
2334 -- Set command line argument values from parameters if command line
2335 -- arguments are present on target
2337 if Command_Line_Args_On_Target then
2338 WBI (" gnat_argc = argc;");
2339 WBI (" gnat_argv = argv;");
2340 WBI (" gnat_envp = envp;");
2343 -- If configurable run-time, then nothing to do, since in this case
2344 -- the gnat_argc/argv/envp variables are entirely suppressed.
2346 elsif Configurable_Run_Time_On_Target then
2349 -- if no command line arguments on target, set dummy values
2352 WBI (" gnat_argc = 0;");
2353 WBI (" gnat_argv = 0;");
2354 WBI (" gnat_envp = 0;");
2357 if Opt.Default_Exit_Status /= 0
2358 and then Bind_Main_Program
2359 and then not Configurable_Run_Time_Mode
2361 Set_String (" __gnat_set_exit_status (");
2362 Set_Int (Opt.Default_Exit_Status);
2364 Write_Statement_Buffer;
2367 -- Initializes dynamic stack measurement if needed
2369 if Dynamic_Stack_Measurement then
2370 Set_String (" __gnat_stack_usage_initialize (");
2371 Set_Int (Dynamic_Stack_Measurement_Array_Size);
2373 Write_Statement_Buffer;
2376 -- The __gnat_initialize routine is used only if we have a run-time
2378 if not Suppress_Standard_Library_On_Target then
2379 if not No_Main_Subprogram and then Bind_Main_Program then
2380 WBI (" __gnat_initialize ((void *)SEH);");
2382 WBI (" __gnat_initialize ((void *)0);");
2386 WBI (" " & Ada_Init_Name.all & " ();");
2388 if not No_Main_Subprogram then
2389 WBI (" __gnat_break_start ();");
2391 -- Output main program name
2393 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2395 -- Main program is procedure case
2397 if ALIs.Table (ALIs.First).Main_Program = Proc then
2399 Set_Main_Program_Name;
2400 Set_String (" ();");
2401 Write_Statement_Buffer;
2403 -- Main program is function case
2405 else -- ALIs.Table (ALIs_First).Main_Program = Func
2406 Set_String (" result = ");
2407 Set_Main_Program_Name;
2408 Set_String (" ();");
2409 Write_Statement_Buffer;
2414 -- Call adafinal if finalization active
2416 if not Cumulative_Restrictions.Set (No_Finalization) then
2417 WBI (" " & Ada_Final_Name.all & " ();");
2420 -- Outputs the dynamic stack measurement if needed
2422 if Dynamic_Stack_Measurement then
2423 WBI (" __gnat_stack_usage_output_results ();");
2426 -- The finalize routine is used only if we have a run-time
2428 if not Suppress_Standard_Library_On_Target then
2429 WBI (" __gnat_finalize ();");
2432 -- Case of main program is a function, so the value it returns
2433 -- is the exit status in this case.
2435 if ALIs.Table (ALIs.First).Main_Program = Func then
2436 if Exit_Status_Supported_On_Target then
2438 -- VMS must use Posix exit routine in order to get the effect
2439 -- of a Unix compatible setting of the program exit status.
2440 -- For all other systems, we use the standard exit routine.
2442 if OpenVMS_On_Target then
2443 WBI (" decc$__posix_exit (result);");
2445 WBI (" exit (result);");
2449 -- Case of main program is a procedure, in which case the exit
2450 -- status is whatever was set by a Set_Exit call most recently
2453 if Exit_Status_Supported_On_Target then
2455 -- VMS must use Posix exit routine in order to get the effect
2456 -- of a Unix compatible setting of the program exit status.
2457 -- For all other systems, we use the standard exit routine.
2459 if OpenVMS_On_Target then
2460 WBI (" decc$__posix_exit (gnat_exit_status);");
2462 WBI (" exit (gnat_exit_status);");
2471 ------------------------------
2472 -- Gen_Object_Files_Options --
2473 ------------------------------
2475 procedure Gen_Object_Files_Options is
2477 -- This keeps track of the position in the sorted set of entries
2478 -- in the Linker_Options table of where the first entry from an
2479 -- internal file appears.
2481 Linker_Option_List_Started : Boolean := False;
2482 -- Set to True when "LINKER OPTION LIST" is displayed
2484 procedure Write_Linker_Option;
2485 -- Write binder info linker option
2487 -------------------------
2488 -- Write_Linker_Option --
2489 -------------------------
2491 procedure Write_Linker_Option is
2496 -- Loop through string, breaking at null's
2499 while Start < Name_Len loop
2501 -- Find null ending this section
2504 while Name_Buffer (Stop) /= ASCII.NUL
2505 and then Stop <= Name_Len loop
2509 -- Process section if non-null
2511 if Stop > Start then
2512 if Output_Linker_Option_List then
2513 if not Zero_Formatting then
2514 if not Linker_Option_List_Started then
2515 Linker_Option_List_Started := True;
2517 Write_Str (" LINKER OPTION LIST");
2525 Write_Str (Name_Buffer (Start .. Stop - 1));
2529 (" -- ", "", Name_Buffer (Start .. Stop - 1));
2534 end Write_Linker_Option;
2536 -- Start of processing for Gen_Object_Files_Options
2539 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
2541 if Object_List_Filename /= null then
2542 Set_List_File (Object_List_Filename.all);
2545 for E in Elab_Order.First .. Elab_Order.Last loop
2547 -- If not spec that has an associated body, then generate a comment
2548 -- giving the name of the corresponding object file.
2550 if not Units.Table (Elab_Order.Table (E)).SAL_Interface
2551 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
2555 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
2557 -- If the presence of an object file is necessary or if it exists,
2560 if not Hostparm.Exclude_Missing_Objects
2562 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
2564 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2566 if Output_Object_List then
2567 Write_Str (Name_Buffer (1 .. Name_Len));
2571 -- Don't link with the shared library on VMS if an internal
2572 -- filename object is seen. Multiply defined symbols will
2575 if OpenVMS_On_Target
2576 and then Is_Internal_File_Name
2578 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
2580 -- Special case for g-trasym.obj (not included in libgnat)
2582 Get_Name_String (ALIs.Table
2583 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
2585 if Name_Buffer (1 .. 8) /= "g-trasym" then
2586 Opt.Shared_Libgnat := False;
2593 if Object_List_Filename /= null then
2597 -- Add a "-Ldir" for each directory in the object path
2598 if VM_Target /= CLI_Target then
2599 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2601 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2604 Add_Str_To_Name_Buffer ("-L");
2605 Add_Str_To_Name_Buffer (Dir.all);
2606 Write_Linker_Option;
2611 -- Sort linker options
2613 -- This sort accomplishes two important purposes:
2615 -- a) All application files are sorted to the front, and all GNAT
2616 -- internal files are sorted to the end. This results in a well
2617 -- defined dividing line between the two sets of files, for the
2618 -- purpose of inserting certain standard library references into
2619 -- the linker arguments list.
2621 -- b) Given two different units, we sort the linker options so that
2622 -- those from a unit earlier in the elaboration order comes later
2623 -- in the list. This is a heuristic designed to create a more
2624 -- friendly order of linker options when the operations appear in
2625 -- separate units. The idea is that if unit A must be elaborated
2626 -- before unit B, then it is more likely that B references
2627 -- libraries included by A, than vice versa, so we want libraries
2628 -- included by A to come after libraries included by B.
2630 -- These two criteria are implemented by function Lt_Linker_Option. Note
2631 -- that a special case of b) is that specs are elaborated before bodies,
2632 -- so linker options from specs come after linker options for bodies,
2633 -- and again, the assumption is that libraries used by the body are more
2634 -- likely to reference libraries used by the spec, than vice versa.
2637 (Linker_Options.Last,
2638 Move_Linker_Option'Access,
2639 Lt_Linker_Option'Access);
2641 -- Write user linker options, i.e. the set of linker options that come
2642 -- from all files other than GNAT internal files, Lgnat is left set to
2643 -- point to the first entry from a GNAT internal file, or past the end
2644 -- of the entries if there are no internal files.
2646 Lgnat := Linker_Options.Last + 1;
2648 for J in 1 .. Linker_Options.Last loop
2649 if not Linker_Options.Table (J).Internal_File then
2650 Get_Name_String (Linker_Options.Table (J).Name);
2651 Write_Linker_Option;
2658 -- Now we insert standard linker options that must appear after the
2659 -- entries from user files, and before the entries from GNAT run-time
2660 -- files. The reason for this decision is that libraries referenced
2661 -- by internal routines may reference these standard library entries.
2663 -- Note that we do not insert anything when pragma No_Run_Time has been
2664 -- specified or when the standard libraries are not to be used,
2665 -- otherwise on some platforms, such as VMS, we may get duplicate
2666 -- symbols when linking.
2668 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2671 if Opt.Shared_Libgnat then
2672 Add_Str_To_Name_Buffer ("-shared");
2674 Add_Str_To_Name_Buffer ("-static");
2677 -- Write directly to avoid -K output (why???)
2679 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2681 if With_DECGNAT then
2684 if Opt.Shared_Libgnat then
2685 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2687 Add_Str_To_Name_Buffer ("-ldecgnat");
2690 Write_Linker_Option;
2696 if Opt.Shared_Libgnat then
2697 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2699 Add_Str_To_Name_Buffer ("-lgnarl");
2702 Write_Linker_Option;
2707 if Opt.Shared_Libgnat then
2708 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2710 Add_Str_To_Name_Buffer ("-lgnat");
2713 Write_Linker_Option;
2716 -- Write linker options from all internal files
2718 for J in Lgnat .. Linker_Options.Last loop
2719 Get_Name_String (Linker_Options.Table (J).Name);
2720 Write_Linker_Option;
2723 if Output_Linker_Option_List and then not Zero_Formatting then
2727 if Ada_Bind_File then
2728 WBI ("-- END Object file/option list ");
2730 WBI (" END Object file/option list */");
2732 end Gen_Object_Files_Options;
2734 ---------------------
2735 -- Gen_Output_File --
2736 ---------------------
2738 procedure Gen_Output_File (Filename : String) is
2740 -- Acquire settings for Interrupt_State pragmas
2742 Set_IS_Pragma_Table;
2744 -- Acquire settings for Priority_Specific_Dispatching pragma
2746 Set_PSD_Pragma_Table;
2748 -- Override Ada_Bind_File and Bind_Main_Program for VMs since JGNAT only
2749 -- supports Ada code, and the main program is already generated by the
2752 if VM_Target /= No_VM then
2753 Ada_Bind_File := True;
2755 if VM_Target = JVM_Target then
2756 Bind_Main_Program := False;
2760 -- Override time slice value if -T switch is set
2762 if Time_Slice_Set then
2763 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2766 -- Count number of elaboration calls
2768 for E in Elab_Order.First .. Elab_Order.Last loop
2769 if Units.Table (Elab_Order.Table (E)).No_Elab then
2772 Num_Elab_Calls := Num_Elab_Calls + 1;
2776 -- Generate output file in appropriate language
2778 Check_System_Restrictions_Used;
2780 if Ada_Bind_File then
2781 Gen_Output_File_Ada (Filename);
2783 Gen_Output_File_C (Filename);
2785 end Gen_Output_File;
2787 -------------------------
2788 -- Gen_Output_File_Ada --
2789 -------------------------
2791 procedure Gen_Output_File_Ada (Filename : String) is
2793 Ada_Main : constant String := Get_Ada_Main_Name;
2794 -- Name to be used for generated Ada main program. See the body of
2795 -- function Get_Ada_Main_Name for details on the form of the name.
2797 Needs_Library_Finalization : constant Boolean :=
2798 not Configurable_Run_Time_On_Target
2799 and then Has_Finalizer;
2800 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2801 -- non-terminating, so we do not want finalization.
2804 -- Name of generated bind file (spec)
2807 -- Name of generated bind file (body)
2810 -- Create spec first
2812 Create_Binder_Output (Filename, 's', Bfiles);
2814 -- We always compile the binder file in Ada 95 mode so that we properly
2815 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2816 -- of the Ada 2005 constructs are needed by the binder file.
2818 WBI ("pragma Ada_95;");
2820 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2821 -- then we need to make sure that the binder program is compiled with
2822 -- the same restriction, so that no exception tables are generated.
2824 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2825 WBI ("pragma Restrictions (No_Exception_Handlers);");
2828 -- Same processing for Restrictions (No_Exception_Propagation)
2830 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2831 WBI ("pragma Restrictions (No_Exception_Propagation);");
2834 -- Same processing for pragma No_Run_Time
2836 if No_Run_Time_Mode then
2837 WBI ("pragma No_Run_Time;");
2840 -- Generate with of System so we can reference System.Address
2842 WBI ("with System;");
2844 -- Generate with of System.Initialize_Scalars if active
2846 if Initialize_Scalars_Used then
2847 WBI ("with System.Scalar_Values;");
2850 -- Generate with of System.Secondary_Stack if active
2852 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2853 WBI ("with System.Secondary_Stack;");
2856 Resolve_Binder_Options;
2858 -- Usually, adafinal is called using a pragma Import C. Since Import C
2859 -- doesn't have the same semantics for JGNAT, we use standard Ada.
2861 if VM_Target /= No_VM
2862 and then not Suppress_Standard_Library_On_Target
2864 WBI ("with System.Soft_Links;");
2865 WBI ("with System.Standard_Library;");
2868 WBI ("package " & Ada_Main & " is");
2869 WBI (" pragma Warnings (Off);");
2871 -- Main program case
2873 if Bind_Main_Program then
2874 if VM_Target = No_VM then
2876 -- Generate argc/argv stuff unless suppressed
2878 if Command_Line_Args_On_Target
2879 or not Configurable_Run_Time_On_Target
2882 WBI (" gnat_argc : Integer;");
2883 WBI (" gnat_argv : System.Address;");
2884 WBI (" gnat_envp : System.Address;");
2886 -- If the standard library is not suppressed, these variables
2887 -- are in the run-time data area for easy run time access.
2889 if not Suppress_Standard_Library_On_Target then
2891 WBI (" pragma Import (C, gnat_argc);");
2892 WBI (" pragma Import (C, gnat_argv);");
2893 WBI (" pragma Import (C, gnat_envp);");
2897 -- Define exit status. Again in normal mode, this is in the
2898 -- run-time library, and is initialized there, but in the
2899 -- configurable runtime case, the variable is declared and
2900 -- initialized in this file.
2904 if Configurable_Run_Time_Mode then
2905 if Exit_Status_Supported_On_Target then
2906 WBI (" gnat_exit_status : Integer := 0;");
2910 WBI (" gnat_exit_status : Integer;");
2911 WBI (" pragma Import (C, gnat_exit_status);");
2915 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2916 -- the main program. Otherwise, it can lead under some circumstances
2917 -- to a symbol duplication during the link (for instance when a C
2918 -- program uses two Ada libraries). Also zero terminate the string
2919 -- so that its end can be found reliably at run time.
2922 WBI (" GNAT_Version : constant String :=");
2923 WBI (" """ & Ver_Prefix &
2924 Gnat_Version_String &
2926 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2929 Set_String (" Ada_Main_Program_Name : constant String := """);
2930 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2932 if VM_Target = No_VM then
2933 Set_Main_Program_Name;
2934 Set_String (""" & ASCII.NUL;");
2936 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2939 Write_Statement_Buffer;
2942 (" pragma Export (C, Ada_Main_Program_Name, " &
2943 """__gnat_ada_main_program_name"");");
2947 WBI (" procedure " & Ada_Init_Name.all & ";");
2948 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2949 Ada_Init_Name.all & """);");
2951 -- If -a has been specified use pragma Linker_Constructor for the init
2952 -- procedure and pragma Linker_Destructor for the final procedure.
2954 if Use_Pragma_Linker_Constructor then
2955 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2958 if not Cumulative_Restrictions.Set (No_Finalization) then
2960 WBI (" procedure " & Ada_Final_Name.all & ";");
2961 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2962 Ada_Final_Name.all & """);");
2964 if Use_Pragma_Linker_Constructor then
2965 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2969 if Bind_Main_Program and then VM_Target = No_VM then
2971 -- If we have the standard library, then Break_Start is defined
2972 -- there, but when the standard library is suppressed, Break_Start
2976 WBI (" procedure Break_Start;");
2978 if Suppress_Standard_Library_On_Target then
2979 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2981 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2986 if Exit_Status_Supported_On_Target then
2987 Set_String (" function ");
2989 Set_String (" procedure ");
2992 Set_String (Get_Main_Name);
2994 -- Generate argument list if present
2996 if Command_Line_Args_On_Target then
2997 Write_Statement_Buffer;
2998 WBI (" (argc : Integer;");
2999 WBI (" argv : System.Address;");
3001 (" envp : System.Address)");
3003 if Exit_Status_Supported_On_Target then
3004 Write_Statement_Buffer;
3005 WBI (" return Integer;");
3007 Write_Statement_Buffer (";");
3011 if Exit_Status_Supported_On_Target then
3012 Write_Statement_Buffer (" return Integer;");
3014 Write_Statement_Buffer (";");
3018 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
3019 Get_Main_Name & """);");
3028 WBI ("end " & Ada_Main & ";");
3029 Close_Binder_Output;
3031 -- Prepare to write body
3033 Create_Binder_Output (Filename, 'b', Bfileb);
3035 -- We always compile the binder file in Ada 95 mode so that we properly
3036 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
3037 -- of the Ada 2005 constructs are needed by the binder file.
3039 WBI ("pragma Ada_95;");
3041 -- Output Source_File_Name pragmas which look like
3043 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
3044 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
3046 -- where sss/bbb are the spec/body file names respectively
3048 Get_Name_String (Bfiles);
3049 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
3051 WBI ("pragma Source_File_Name (" &
3053 ", Spec_File_Name => """ &
3054 Name_Buffer (1 .. Name_Len + 3));
3056 Get_Name_String (Bfileb);
3057 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
3059 WBI ("pragma Source_File_Name (" &
3061 ", Body_File_Name => """ &
3062 Name_Buffer (1 .. Name_Len + 3));
3064 -- Generate with of System.Restrictions to initialize
3065 -- Run_Time_Restrictions.
3067 if System_Restrictions_Used
3068 and not Suppress_Standard_Library_On_Target
3071 WBI ("with System.Restrictions;");
3074 if Needs_Library_Finalization then
3075 WBI ("with Ada.Exceptions;");
3078 if CodePeer_Mode then
3079 -- For CodePeer, main program is not called via an Import pragma.
3080 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3081 WBI ("with " & Name_Buffer (1 .. Name_Len - 2) & ";");
3082 -- strip trailing "%b"
3086 WBI ("package body " & Ada_Main & " is");
3087 WBI (" pragma Warnings (Off);");
3090 -- Generate externals for elaboration entities
3091 Gen_Elab_Externals_Ada;
3093 if not Suppress_Standard_Library_On_Target then
3095 -- Generate Priority_Specific_Dispatching pragma string
3098 (" Local_Priority_Specific_Dispatching : constant String := """);
3100 for J in 0 .. PSD_Pragma_Settings.Last loop
3101 Set_Char (PSD_Pragma_Settings.Table (J));
3105 Write_Statement_Buffer;
3107 -- Generate Interrupt_State pragma string
3109 Set_String (" Local_Interrupt_States : constant String := """);
3111 for J in 0 .. IS_Pragma_Settings.Last loop
3112 Set_Char (IS_Pragma_Settings.Table (J));
3116 Write_Statement_Buffer;
3120 -- The B.1 (39) implementation advice says that the adainit/adafinal
3121 -- routines should be idempotent. Generate a flag to ensure that.
3123 WBI (" Is_Elaborated : Boolean := False;");
3126 -- Generate the adafinal routine unless there is no finalization to do
3128 if not Cumulative_Restrictions.Set (No_Finalization) then
3129 if Needs_Library_Finalization then
3130 Gen_Finalize_Library_Ada;
3138 if Bind_Main_Program and then VM_Target = No_VM then
3140 -- When suppressing the standard library then generate dummy body
3143 if Suppress_Standard_Library_On_Target then
3145 WBI (" procedure Break_Start is");
3154 -- Output object file list and the Ada body is complete
3156 Gen_Object_Files_Options;
3159 WBI ("end " & Ada_Main & ";");
3161 Close_Binder_Output;
3162 end Gen_Output_File_Ada;
3164 -----------------------
3165 -- Gen_Output_File_C --
3166 -----------------------
3168 procedure Gen_Output_File_C (Filename : String) is
3170 Needs_Library_Finalization : constant Boolean :=
3171 not Configurable_Run_Time_On_Target
3172 and then Has_Finalizer;
3173 -- ??? seems like we repeat this cantation often, should it be global?
3176 pragma Warnings (Off, Bfile);
3177 -- Name of generated bind file (not referenced)
3180 Create_Binder_Output (Filename, 'c', Bfile);
3182 Resolve_Binder_Options;
3184 -- If -a has been specified use __attribute__((constructor)) for the
3185 -- init procedure and __attribute__((destructor)) for the final one.
3187 if Use_Pragma_Linker_Constructor then
3188 WBI ("extern void " & Ada_Init_Name.all &
3189 " (void) __attribute__((constructor));");
3191 WBI ("extern void " & Ada_Init_Name.all & " (void);");
3194 if not Cumulative_Restrictions.Set (No_Finalization) then
3195 if Use_Pragma_Linker_Constructor then
3196 WBI ("extern void " & Ada_Final_Name.all &
3197 " (void) __attribute__((destructor));");
3199 WBI ("extern void " & Ada_Final_Name.all & " (void);");
3203 WBI ("extern void system__standard_library__adafinal (void);");
3205 if not No_Main_Subprogram then
3206 Set_String ("extern ");
3208 if Exit_Status_Supported_On_Target then
3211 Set_String ("void");
3214 Set_String (" main ");
3216 if Command_Line_Args_On_Target then
3217 Write_Statement_Buffer ("(int, char **, char **);");
3219 Write_Statement_Buffer ("(void);");
3222 if OpenVMS_On_Target then
3223 WBI ("extern void decc$__posix_exit (int);");
3225 WBI ("extern void exit (int);");
3228 WBI ("extern void __gnat_break_start (void);");
3229 Set_String ("extern ");
3231 if ALIs.Table (ALIs.First).Main_Program = Proc then
3232 Set_String ("void ");
3234 Set_String ("int ");
3237 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3238 Set_Main_Program_Name;
3239 Set_String (" (void);");
3240 Write_Statement_Buffer;
3243 if not Suppress_Standard_Library_On_Target then
3244 WBI ("extern void __gnat_initialize (void *);");
3245 WBI ("extern void __gnat_finalize (void);");
3246 WBI ("extern void __gnat_install_handler (void);");
3249 if Dynamic_Stack_Measurement then
3251 WBI ("extern void __gnat_stack_usage_output_results (void);");
3252 WBI ("extern void __gnat_stack_usage_initialize (int size);");
3255 -- Initialize stack limit for the environment task if the stack check
3256 -- method is stack limit and stack check is enabled.
3258 if Stack_Check_Limits_On_Target
3259 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
3262 WBI ("extern void __gnat_initialize_stack_limit (void);");
3267 -- Generate externals for elaboration entities
3268 Gen_Elab_Externals_C;
3272 if Needs_Library_Finalization then
3273 Gen_Finalize_Library_Defs_C;
3276 -- Write argv/argc exit status stuff if main program case
3278 if Bind_Main_Program then
3280 -- First deal with argc/argv/envp. In the normal case they are in the
3281 -- run-time library.
3283 if not Configurable_Run_Time_On_Target then
3284 WBI ("extern int gnat_argc;");
3285 WBI ("extern char **gnat_argv;");
3286 WBI ("extern char **gnat_envp;");
3288 -- If configurable run time and no command line args, then the
3289 -- generation of these variables is entirely suppressed.
3291 elsif not Command_Line_Args_On_Target then
3294 -- Otherwise, in the configurable run-time case they are right in the
3298 WBI ("int gnat_argc;");
3299 WBI ("char **gnat_argv;");
3300 WBI ("char **gnat_envp;");
3303 -- Similarly deal with exit status
3305 if not Configurable_Run_Time_On_Target then
3306 WBI ("extern int gnat_exit_status;");
3308 -- If configurable run time and no exit status on target, then the
3309 -- generation of this variables is entirely suppressed.
3311 elsif not Exit_Status_Supported_On_Target then
3314 -- Otherwise, in the configurable run-time case this variable is
3315 -- right in the binder file, and initialized to zero there.
3318 WBI ("int gnat_exit_status = 0;");
3324 -- When suppressing the standard library, the __gnat_break_start routine
3325 -- (for the debugger to get initial control) is defined in this file.
3327 if Suppress_Standard_Library_On_Target then
3328 WBI ("void __gnat_break_start (void) {}");
3332 -- Generate the __gnat_version and __gnat_ada_main_program_name info
3333 -- only for the main program. Otherwise, it can lead under some
3334 -- circumstances to a symbol duplication during the link (for instance
3335 -- when a C program uses 2 Ada libraries)
3337 if Bind_Main_Program then
3338 WBI ("char __gnat_version[] = """ & Ver_Prefix &
3339 Gnat_Version_String & """;");
3341 Set_String ("char __gnat_ada_main_program_name[] = """);
3342 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3343 Set_Main_Program_Name;
3345 Write_Statement_Buffer;
3349 -- The B.1 (39) implementation advice says that the adainit/adafinal
3350 -- routines should be idempotent. Generate a flag to ensure that.
3352 WBI ("static char is_elaborated = 0;");
3355 -- Generate the adafinal routine unless there is no finalization to do
3357 if not Cumulative_Restrictions.Set (No_Finalization) then
3358 if Needs_Library_Finalization then
3359 Gen_Finalize_Library_C;
3367 -- Main is only present for Ada main case
3369 if Bind_Main_Program then
3373 -- Generate versions, elaboration order, list of object files
3377 Gen_Object_Files_Options;
3379 -- C binder output is complete
3381 Close_Binder_Output;
3382 end Gen_Output_File_C;
3384 --------------------------
3385 -- Gen_Restrictions_Ada --
3386 --------------------------
3388 procedure Gen_Restrictions_Ada is
3392 if Suppress_Standard_Library_On_Target
3393 or not System_Restrictions_Used
3398 WBI (" System.Restrictions.Run_Time_Restrictions :=");
3404 for J in Cumulative_Restrictions.Set'Range loop
3405 Set_Boolean (Cumulative_Restrictions.Set (J));
3409 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
3410 Write_Statement_Buffer;
3416 Set_String_Replace ("),");
3417 Write_Statement_Buffer;
3418 Set_String (" Value => (");
3420 for J in Cumulative_Restrictions.Value'Range loop
3421 Set_Int (Int (Cumulative_Restrictions.Value (J)));
3425 Set_String_Replace ("),");
3426 Write_Statement_Buffer;
3427 WBI (" Violated =>");
3431 for J in Cumulative_Restrictions.Violated'Range loop
3432 Set_Boolean (Cumulative_Restrictions.Violated (J));
3436 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
3437 Write_Statement_Buffer;
3443 Set_String_Replace ("),");
3444 Write_Statement_Buffer;
3445 Set_String (" Count => (");
3447 for J in Cumulative_Restrictions.Count'Range loop
3448 Set_Int (Int (Cumulative_Restrictions.Count (J)));
3452 Set_String_Replace ("),");
3453 Write_Statement_Buffer;
3454 Set_String (" Unknown => (");
3456 for J in Cumulative_Restrictions.Unknown'Range loop
3457 Set_Boolean (Cumulative_Restrictions.Unknown (J));
3461 Set_String_Replace ("))");
3463 Write_Statement_Buffer;
3464 end Gen_Restrictions_Ada;
3466 ------------------------
3467 -- Gen_Restrictions_C --
3468 ------------------------
3470 procedure Gen_Restrictions_C is
3472 if Suppress_Standard_Library_On_Target
3473 or not System_Restrictions_Used
3478 WBI (" typedef struct {");
3479 Set_String (" char set [");
3480 Set_Int (Cumulative_Restrictions.Set'Length);
3482 Write_Statement_Buffer;
3484 Set_String (" int value [");
3485 Set_Int (Cumulative_Restrictions.Value'Length);
3487 Write_Statement_Buffer;
3489 Set_String (" char violated [");
3490 Set_Int (Cumulative_Restrictions.Violated'Length);
3492 Write_Statement_Buffer;
3494 Set_String (" int count [");
3495 Set_Int (Cumulative_Restrictions.Count'Length);
3497 Write_Statement_Buffer;
3499 Set_String (" char unknown [");
3500 Set_Int (Cumulative_Restrictions.Unknown'Length);
3502 Write_Statement_Buffer;
3503 WBI (" } restrictions;");
3504 WBI (" extern restrictions " &
3505 "system__restrictions__run_time_restrictions;");
3506 WBI (" restrictions r = {");
3509 for J in Cumulative_Restrictions.Set'Range loop
3510 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
3514 Set_String_Replace ("},");
3515 Write_Statement_Buffer;
3518 for J in Cumulative_Restrictions.Value'Range loop
3519 Set_Int (Int (Cumulative_Restrictions.Value (J)));
3523 Set_String_Replace ("},");
3524 Write_Statement_Buffer;
3527 for J in Cumulative_Restrictions.Violated'Range loop
3528 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
3532 Set_String_Replace ("},");
3533 Write_Statement_Buffer;
3536 for J in Cumulative_Restrictions.Count'Range loop
3537 Set_Int (Int (Cumulative_Restrictions.Count (J)));
3541 Set_String_Replace ("},");
3542 Write_Statement_Buffer;
3545 for J in Cumulative_Restrictions.Unknown'Range loop
3546 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
3550 Set_String_Replace ("}}");
3552 Write_Statement_Buffer;
3553 WBI (" system__restrictions__run_time_restrictions = r;");
3554 end Gen_Restrictions_C;
3556 ----------------------
3557 -- Gen_Versions_Ada --
3558 ----------------------
3560 -- This routine generates lines such as:
3562 -- unnnnn : constant Integer := 16#hhhhhhhh#;
3563 -- pragma Export (C, unnnnn, unam);
3565 -- for each unit, where unam is the unit name suffixed by either B or S for
3566 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
3567 -- the version number, and nnnnn is a 5-digits serial number.
3569 procedure Gen_Versions_Ada is
3570 Ubuf : String (1 .. 6) := "u00000";
3572 procedure Increment_Ubuf;
3573 -- Little procedure to increment the serial number
3575 --------------------
3576 -- Increment_Ubuf --
3577 --------------------
3579 procedure Increment_Ubuf is
3581 for J in reverse Ubuf'Range loop
3582 Ubuf (J) := Character'Succ (Ubuf (J));
3583 exit when Ubuf (J) <= '9';
3588 -- Start of processing for Gen_Versions_Ada
3593 WBI (" type Version_32 is mod 2 ** 32;");
3594 for U in Units.First .. Units.Last loop
3595 if not Units.Table (U).SAL_Interface
3597 (not Bind_For_Library or else Units.Table (U).Directly_Scanned)
3600 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
3601 Units.Table (U).Version & "#;");
3602 Set_String (" pragma Export (C, ");
3604 Set_String (", """);
3606 Get_Name_String (Units.Table (U).Uname);
3608 for K in 1 .. Name_Len loop
3609 if Name_Buffer (K) = '.' then
3613 elsif Name_Buffer (K) = '%' then
3617 Set_Char (Name_Buffer (K));
3621 if Name_Buffer (Name_Len) = 's' then
3627 Set_String (""");");
3628 Write_Statement_Buffer;
3631 end Gen_Versions_Ada;
3633 --------------------
3634 -- Gen_Versions_C --
3635 --------------------
3637 -- This routine generates a line of the form:
3639 -- unsigned unam = 0xhhhhhhhh;
3641 -- for each unit, where unam is the unit name suffixed by either B or S for
3642 -- body or spec, with dots replaced by double underscores.
3644 procedure Gen_Versions_C is
3646 for U in Units.First .. Units.Last loop
3647 if not Units.Table (U).SAL_Interface
3649 (not Bind_For_Library or else Units.Table (U).Directly_Scanned)
3651 Set_String ("unsigned ");
3653 Get_Name_String (Units.Table (U).Uname);
3655 for K in 1 .. Name_Len loop
3656 if Name_Buffer (K) = '.' then
3659 elsif Name_Buffer (K) = '%' then
3663 Set_Char (Name_Buffer (K));
3667 if Name_Buffer (Name_Len) = 's' then
3673 Set_String (" = 0x");
3674 Set_String (Units.Table (U).Version);
3676 Write_Statement_Buffer;
3681 ------------------------
3682 -- Get_Main_Unit_Name --
3683 ------------------------
3685 function Get_Main_Unit_Name (S : String) return String is
3686 Result : String := S;
3689 for J in S'Range loop
3690 if Result (J) = '.' then
3696 end Get_Main_Unit_Name;
3698 -----------------------
3699 -- Get_Ada_Main_Name --
3700 -----------------------
3702 function Get_Ada_Main_Name return String is
3703 Suffix : constant String := "_00";
3704 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3705 Opt.Ada_Main_Name.all & Suffix;
3709 -- The main program generated by JGNAT expects a package called
3710 -- ada_<main procedure>.
3712 if VM_Target /= No_VM then
3713 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3714 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3717 -- This loop tries the following possibilities in order
3723 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3724 -- it is set to 'ada_main'.
3726 for J in 0 .. 99 loop
3728 Nlen := Name'Length - Suffix'Length;
3730 Nlen := Name'Length;
3731 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3732 Name (Name'Last - 1) :=
3733 Character'Val (J / 10 + Character'Pos ('0'));
3736 for K in ALIs.First .. ALIs.Last loop
3737 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3739 -- Get unit name, removing %b or %e at end
3741 Get_Name_String (Units.Table (L).Uname);
3742 Name_Len := Name_Len - 2;
3744 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3750 return Name (1 .. Nlen);
3756 -- If we fall through, just use a peculiar unlikely name
3758 return ("Qwertyuiop");
3759 end Get_Ada_Main_Name;
3765 function Get_Main_Name return String is
3767 -- Explicit name given with -M switch
3769 if Bind_Alternate_Main_Name then
3770 return Alternate_Main_Name.all;
3772 -- Case of main program name to be used directly
3774 elsif Use_Ada_Main_Program_Name_On_Target then
3776 -- Get main program name
3778 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3780 -- If this is a child name, return only the name of the child, since
3781 -- we can't have dots in a nested program name. Note that we do not
3782 -- include the %b at the end of the unit name.
3784 for J in reverse 1 .. Name_Len - 2 loop
3785 if J = 1 or else Name_Buffer (J - 1) = '.' then
3786 return Name_Buffer (J .. Name_Len - 2);
3790 raise Program_Error; -- impossible exit
3792 -- Case where "main" is to be used as default
3799 ---------------------
3800 -- Get_WC_Encoding --
3801 ---------------------
3803 function Get_WC_Encoding return Character is
3805 -- If encoding method specified by -W switch, then return it
3807 if Wide_Character_Encoding_Method_Specified then
3808 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3810 -- If no main program, and not specified, set brackets, we really have
3811 -- no better choice. If some other encoding is required when there is
3812 -- no main, it must be set explicitly using -Wx.
3814 -- Note: if the ALI file always passed the wide character encoding of
3815 -- every file, then we could use the encoding of the initial specified
3816 -- file, but this information is passed only for potential main
3817 -- programs. We could fix this sometime, but it is a very minor point
3818 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
3819 -- is no main program).
3821 elsif No_Main_Subprogram then
3824 -- Otherwise if there is a main program, take encoding from it
3827 return ALIs.Table (ALIs.First).WC_Encoding;
3829 end Get_WC_Encoding;
3835 function Has_Finalizer return Boolean is
3840 for E in reverse Elab_Order.First .. Elab_Order.Last loop
3841 Unum := Elab_Order.Table (E);
3842 U := Units.Table (Unum);
3844 -- We are only interested in non-generic packages
3846 if U.Unit_Kind = 'p'
3847 and then U.Has_Finalizer
3848 and then not U.Is_Generic
3849 and then not U.No_Elab
3858 ----------------------
3859 -- Lt_Linker_Option --
3860 ----------------------
3862 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3864 -- Sort internal files last
3866 if Linker_Options.Table (Op1).Internal_File
3868 Linker_Options.Table (Op2).Internal_File
3870 -- Note: following test uses False < True
3872 return Linker_Options.Table (Op1).Internal_File
3874 Linker_Options.Table (Op2).Internal_File;
3876 -- If both internal or both non-internal, sort according to the
3877 -- elaboration position. A unit that is elaborated later should come
3878 -- earlier in the linker options list.
3881 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3883 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3886 end Lt_Linker_Option;
3888 ------------------------
3889 -- Move_Linker_Option --
3890 ------------------------
3892 procedure Move_Linker_Option (From : Natural; To : Natural) is
3894 Linker_Options.Table (To) := Linker_Options.Table (From);
3895 end Move_Linker_Option;
3897 ----------------------------
3898 -- Resolve_Binder_Options --
3899 ----------------------------
3901 procedure Resolve_Binder_Options is
3903 for E in Elab_Order.First .. Elab_Order.Last loop
3904 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3906 -- This is not a perfect approach, but is the current protocol
3907 -- between the run-time and the binder to indicate that tasking is
3908 -- used: system.os_interface should always be used by any tasking
3911 if Name_Buffer (1 .. 19) = "system.os_interface" then
3915 -- Ditto for declib and the "dec" package
3917 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3918 With_DECGNAT := True;
3921 end Resolve_Binder_Options;
3927 procedure Set_Boolean (B : Boolean) is
3928 True_Str : constant String := "True";
3929 False_Str : constant String := "False";
3932 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3933 Last := Last + True_Str'Length;
3935 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3936 Last := Last + False_Str'Length;
3944 procedure Set_Char (C : Character) is
3947 Statement_Buffer (Last) := C;
3954 procedure Set_Int (N : Int) is
3966 Statement_Buffer (Last) :=
3967 Character'Val (N mod 10 + Character'Pos ('0'));
3971 -------------------------
3972 -- Set_IS_Pragma_Table --
3973 -------------------------
3975 procedure Set_IS_Pragma_Table is
3977 for F in ALIs.First .. ALIs.Last loop
3978 for K in ALIs.Table (F).First_Interrupt_State ..
3979 ALIs.Table (F).Last_Interrupt_State
3982 Inum : constant Int :=
3983 Interrupt_States.Table (K).Interrupt_Id;
3984 Stat : constant Character :=
3985 Interrupt_States.Table (K).Interrupt_State;
3988 while IS_Pragma_Settings.Last < Inum loop
3989 IS_Pragma_Settings.Append ('n');
3992 IS_Pragma_Settings.Table (Inum) := Stat;
3996 end Set_IS_Pragma_Table;
3998 ---------------------------
3999 -- Set_Main_Program_Name --
4000 ---------------------------
4002 procedure Set_Main_Program_Name is
4004 -- Note that name has %b on the end which we ignore
4006 -- First we output the initial _ada_ since we know that the main
4007 -- program is a library level subprogram.
4009 Set_String ("_ada_");
4011 -- Copy name, changing dots to double underscores
4013 for J in 1 .. Name_Len - 2 loop
4014 if Name_Buffer (J) = '.' then
4017 Set_Char (Name_Buffer (J));
4020 end Set_Main_Program_Name;
4022 ---------------------
4023 -- Set_Name_Buffer --
4024 ---------------------
4026 procedure Set_Name_Buffer is
4028 for J in 1 .. Name_Len loop
4029 Set_Char (Name_Buffer (J));
4031 end Set_Name_Buffer;
4033 -------------------------
4034 -- Set_PSD_Pragma_Table --
4035 -------------------------
4037 procedure Set_PSD_Pragma_Table is
4039 for F in ALIs.First .. ALIs.Last loop
4040 for K in ALIs.Table (F).First_Specific_Dispatching ..
4041 ALIs.Table (F).Last_Specific_Dispatching
4044 DTK : Specific_Dispatching_Record
4045 renames Specific_Dispatching.Table (K);
4048 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
4049 PSD_Pragma_Settings.Append ('F');
4052 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
4053 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
4058 end Set_PSD_Pragma_Table;
4064 procedure Set_String (S : String) is
4066 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
4067 Last := Last + S'Length;
4070 ------------------------
4071 -- Set_String_Replace --
4072 ------------------------
4074 procedure Set_String_Replace (S : String) is
4076 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
4077 end Set_String_Replace;
4083 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores) is
4085 for J in 1 .. Name_Len - 2 loop
4086 if Name_Buffer (J) = '.' then
4087 if Mode = Double_Underscores then
4089 elsif Mode = Dot then
4095 Set_Char (Name_Buffer (J));
4100 ---------------------
4101 -- Set_Unit_Number --
4102 ---------------------
4104 procedure Set_Unit_Number (U : Unit_Id) is
4105 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
4106 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
4109 if Num_Units >= 10 and then Unum < 10 then
4113 if Num_Units >= 100 and then Unum < 100 then
4118 end Set_Unit_Number;
4120 ----------------------
4121 -- Write_Info_Ada_C --
4122 ----------------------
4124 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
4126 if Ada_Bind_File then
4128 S : String (1 .. Ada'Length + Common'Length);
4130 S (1 .. Ada'Length) := Ada;
4131 S (Ada'Length + 1 .. S'Length) := Common;
4137 S : String (1 .. C'Length + Common'Length);
4139 S (1 .. C'Length) := C;
4140 S (C'Length + 1 .. S'Length) := Common;
4144 end Write_Info_Ada_C;
4146 ----------------------------
4147 -- Write_Statement_Buffer --
4148 ----------------------------
4150 procedure Write_Statement_Buffer is
4152 WBI (Statement_Buffer (1 .. Last));
4154 end Write_Statement_Buffer;
4156 procedure Write_Statement_Buffer (S : String) is
4159 Write_Statement_Buffer;
4160 end Write_Statement_Buffer;