1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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 Casing; use Casing;
28 with Fname; use Fname;
29 with Gnatvsn; use Gnatvsn;
31 with Namet; use Namet;
33 with Osint; use Osint;
34 with Osint.B; use Osint.B;
35 with Output; use Output;
36 with Rident; use Rident;
37 with Stringt; use Stringt;
39 with Targparm; use Targparm;
40 with Types; use Types;
43 with System.WCh_Con; use System.WCh_Con;
45 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
48 package body Bindgen is
49 Statement_Buffer : String (1 .. 1000);
50 -- Buffer used for constructing output statements
52 Stm_Last : Natural := 0;
53 -- Stm_Last location in Statement_Buffer currently set
55 With_GNARL : Boolean := False;
56 -- Flag which indicates whether the program uses the GNARL library
57 -- (presence of the unit System.OS_Interface)
59 Num_Elab_Calls : Nat := 0;
60 -- Number of generated calls to elaboration routines
62 Num_Primary_Stacks : Int := 0;
63 -- Number of default-sized primary stacks the binder needs to allocate for
64 -- task objects declared in the program.
66 Num_Sec_Stacks : Int := 0;
67 -- Number of default-sized primary stacks the binder needs to allocate for
68 -- task objects declared in the program.
70 System_Restrictions_Used : Boolean := False;
71 -- Flag indicating whether the unit System.Restrictions is in the closure
72 -- of the partition. This is set by Resolve_Binder_Options, and is used
73 -- to determine whether or not to initialize the restrictions information
74 -- in the body of the binder generated file (we do not want to do this
75 -- unconditionally, since it drags in the System.Restrictions unit
76 -- unconditionally, which is unpleasand, especially for ZFP etc.)
78 Dispatching_Domains_Used : Boolean := False;
79 -- Flag indicating whether multiprocessor dispatching domains are used in
80 -- the closure of the partition. This is set by Resolve_Binder_Options, and
81 -- is used to call the routine to disallow the creation of new dispatching
82 -- domains just before calling the main procedure from the environment
85 System_Secondary_Stack_Used : Boolean := False;
86 -- Flag indicating whether the unit System.Secondary_Stack is in the
87 -- closure of the partition. This is set by Resolve_Binder_Options, and
88 -- is used to initialize the package in cases where the run-time brings
89 -- in package but the secondary stack is not used.
91 System_Tasking_Restricted_Stages_Used : Boolean := False;
92 -- Flag indicating whether the unit System.Tasking.Restricted.Stages is in
93 -- the closure of the partition. This is set by Resolve_Binder_Options,
94 -- and it used to call a routine to active all the tasks at the end of
95 -- the elaboration when partition elaboration policy is sequential.
97 System_Interrupts_Used : Boolean := False;
98 -- Flag indicating whether the unit System.Interrups is in the closure of
99 -- the partition. This is set by Resolve_Binder_Options, and it used to
100 -- attach interrupt handlers at the end of the elaboration when partition
101 -- elaboration policy is sequential.
103 System_BB_CPU_Primitives_Multiprocessors_Used : Boolean := False;
104 -- Flag indicating whether unit System.BB.CPU_Primitives.Multiprocessors
105 -- is in the closure of the partition. This is set by procedure
106 -- Resolve_Binder_Options, and it is used to call a procedure that starts
109 System_Version_Control_Used : Boolean := False;
110 -- Flag indicating whether unit System.Version_Control is in the closure.
111 -- This unit is implicitly withed by the compiler when Version or
112 -- Body_Version attributes are used. If the package is not in the closure,
113 -- the version definitions can be removed.
115 Lib_Final_Built : Boolean := False;
116 -- Flag indicating whether the finalize_library rountine has been built
118 Bind_Env_String_Built : Boolean := False;
119 -- Flag indicating whether a bind environment string has been built
121 CodePeer_Wrapper_Name : constant String := "call_main_subprogram";
122 -- For CodePeer, introduce a wrapper subprogram which calls the
123 -- user-defined main subprogram.
125 ----------------------------------
126 -- Interface_State Pragma Table --
127 ----------------------------------
129 -- This table assembles the interface state pragma information from
130 -- all the units in the partition. Note that Bcheck has already checked
131 -- that the information is consistent across units. The entries
132 -- in this table are n/u/r/s for not set/user/runtime/system.
134 package IS_Pragma_Settings is new Table.Table
135 (Table_Component_Type => Character,
136 Table_Index_Type => Int,
137 Table_Low_Bound => 0,
138 Table_Initial => 100,
139 Table_Increment => 200,
140 Table_Name => "IS_Pragma_Settings");
142 -- This table assembles the Priority_Specific_Dispatching pragma
143 -- information from all the units in the partition. Note that Bcheck has
144 -- already checked that the information is consistent across units.
145 -- The entries in this table are the upper case first character of the
146 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
148 package PSD_Pragma_Settings is new Table.Table
149 (Table_Component_Type => Character,
150 Table_Index_Type => Int,
151 Table_Low_Bound => 0,
152 Table_Initial => 100,
153 Table_Increment => 200,
154 Table_Name => "PSD_Pragma_Settings");
156 ----------------------------
157 -- Bind_Environment Table --
158 ----------------------------
160 subtype Header_Num is Int range 0 .. 36;
162 function Hash (Nam : Name_Id) return Header_Num;
164 package Bind_Environment is new GNAT.HTable.Simple_HTable
165 (Header_Num => Header_Num,
167 No_Element => No_Name,
172 ----------------------
173 -- Run-Time Globals --
174 ----------------------
176 -- This section documents the global variables that are set from the
177 -- generated binder file.
179 -- Main_Priority : Integer;
180 -- Time_Slice_Value : Integer;
181 -- Heap_Size : Natural;
182 -- WC_Encoding : Character;
183 -- Locking_Policy : Character;
184 -- Queuing_Policy : Character;
185 -- Task_Dispatching_Policy : Character;
186 -- Priority_Specific_Dispatching : System.Address;
187 -- Num_Specific_Dispatching : Integer;
188 -- Restrictions : System.Address;
189 -- Interrupt_States : System.Address;
190 -- Num_Interrupt_States : Integer;
191 -- Unreserve_All_Interrupts : Integer;
192 -- Exception_Tracebacks : Integer;
193 -- Exception_Tracebacks_Symbolic : Integer;
194 -- Detect_Blocking : Integer;
195 -- Default_Stack_Size : Integer;
196 -- Default_Secondary_Stack_Size : System.Parameters.Size_Type;
197 -- Leap_Seconds_Support : Integer;
198 -- Main_CPU : Integer;
199 -- Default_Sized_SS_Pool : System.Address;
200 -- Binder_Sec_Stacks_Count : Natural;
202 -- Main_Priority is the priority value set by pragma Priority in the main
203 -- program. If no such pragma is present, the value is -1.
205 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
206 -- main program, or by the use of a -Tnnn parameter for the binder (if both
207 -- are present, the binder value overrides). The value is in milliseconds.
208 -- A value of zero indicates that time slicing should be suppressed. If no
209 -- pragma is present, and no -T switch was used, the value is -1.
211 -- WC_Encoding shows the wide character encoding method used for the main
212 -- program. This is one of the encoding letters defined in
213 -- System.WCh_Con.WC_Encoding_Letters.
215 -- Locking_Policy is a space if no locking policy was specified for the
216 -- partition. If a locking policy was specified, the value is the upper
217 -- case first character of the locking policy name, for example, 'C' for
220 -- Queuing_Policy is a space if no queuing policy was specified for the
221 -- partition. If a queuing policy was specified, the value is the upper
222 -- case first character of the queuing policy name for example, 'F' for
225 -- Task_Dispatching_Policy is a space if no task dispatching policy was
226 -- specified for the partition. If a task dispatching policy was specified,
227 -- the value is the upper case first character of the policy name, e.g. 'F'
228 -- for FIFO_Within_Priorities.
230 -- Priority_Specific_Dispatching is the address of a string used to store
231 -- the task dispatching policy specified for the different priorities in
232 -- the partition. The length of this string is determined by the last
233 -- priority for which such a pragma applies (the string will be a null
234 -- string if no specific dispatching policies were used). If pragma were
235 -- present, the entries apply to the priorities in sequence from the first
236 -- priority. The value stored is the upper case first character of the
237 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
238 -- for those priority ranges not specified.
240 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
241 -- string. It will be set to zero if no Priority_Specific_Dispatching
242 -- pragmas are present.
244 -- Restrictions is the address of a null-terminated string specifying the
245 -- restrictions information for the partition. The format is identical to
246 -- that of the parameter string found on R lines in ali files (see Lib.Writ
247 -- spec in lib-writ.ads for full details). The difference is that in this
248 -- context the values are the cumulative ones for the entire partition.
250 -- Interrupt_States is the address of a string used to specify the
251 -- cumulative results of Interrupt_State pragmas used in the partition.
252 -- The length of this string is determined by the last interrupt for which
253 -- such a pragma is given (the string will be a null string if no pragmas
254 -- were used). If pragma were present the entries apply to the interrupts
255 -- in sequence from the first interrupt, and are set to one of four
256 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
257 -- time, 's' for system, see description of Interrupt_State pragma for
260 -- Num_Interrupt_States is the length of the Interrupt_States string. It
261 -- will be set to zero if no Interrupt_State pragmas are present.
263 -- Unreserve_All_Interrupts is set to one if at least one unit in the
264 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
266 -- Exception_Tracebacks is set to one if the -Ea or -E parameter was
267 -- present in the bind and to zero otherwise. Note that on some targets
268 -- exception tracebacks are provided by default, so a value of zero for
269 -- this parameter does not necessarily mean no trace backs are available.
271 -- Exception_Tracebacks_Symbolic is set to one if the -Es parameter was
272 -- present in the bind and to zero otherwise.
274 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
275 -- not. A value of zero indicates that the pragma is not present, while a
276 -- value of 1 signals its presence in the partition.
278 -- Default_Stack_Size is the default stack size used when creating an Ada
279 -- task with no explicit Storage_Size clause.
281 -- Default_Secondary_Stack_Size is the default secondary stack size used
282 -- when creating an Ada task with no explicit Secondary_Stack_Size clause.
284 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
285 -- disabled. A value of zero indicates that leap seconds are turned "off",
286 -- while a value of one signifies "on" status.
288 -- Main_CPU is the processor set by pragma CPU in the main program. If no
289 -- such pragma is present, the value is -1.
291 -- Default_Sized_SS_Pool is set to the address of the default-sized
292 -- secondary stacks array generated by the binder. This pool of stacks is
293 -- generated when either the restriction No_Implicit_Heap_Allocations
294 -- or No_Implicit_Task_Allocations is active.
296 -- Binder_Sec_Stacks_Count is the number of generated secondary stacks in
297 -- the Default_Sized_SS_Pool.
299 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
300 -- Convenient shorthand used throughout
302 -----------------------
303 -- Local Subprograms --
304 -----------------------
306 procedure Gen_Adainit (Elab_Order : Unit_Id_Array);
307 -- Generates the Adainit procedure
309 procedure Gen_Adafinal;
310 -- Generate the Adafinal procedure
312 procedure Gen_Bind_Env_String;
313 -- Generate the bind environment buffer
315 procedure Gen_CodePeer_Wrapper;
316 -- For CodePeer, generate wrapper which calls user-defined main subprogram
318 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array);
319 -- Generate sequence of elaboration calls
321 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array);
322 -- Generate sequence of external declarations for elaboration
324 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array);
325 -- Generate comments showing elaboration order chosen
327 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array);
328 -- Generate a sequence of finalization calls to elaborated packages
331 -- Generate procedure main
333 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array);
334 -- Output comments containing a list of the full names of the object
335 -- files to be linked and the list of linker options supplied by
336 -- Linker_Options pragmas in the source.
338 procedure Gen_Output_File_Ada
340 Elab_Order : Unit_Id_Array);
341 -- Generate Ada output file
343 procedure Gen_Restrictions;
344 -- Generate initialization of restrictions variable
346 procedure Gen_Versions;
347 -- Output series of definitions for unit versions
349 function Get_Ada_Main_Name return String;
350 -- This function is used for the Ada main output to compute a usable name
351 -- for the generated main program. The normal main program name is
352 -- Ada_Main, but this won't work if the user has a unit with this name.
353 -- This function tries Ada_Main first, and if there is such a clash, then
354 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
356 function Get_Main_Unit_Name (S : String) return String;
357 -- Return the main unit name corresponding to S by replacing '.' with '_'
359 function Get_Main_Name return String;
360 -- This function is used in the main output case to compute the correct
361 -- external main program. It is "main" by default, unless the flag
362 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
363 -- of the Ada main name without the "_ada". This default can be overridden
364 -- explicitly using the -Mname binder switch.
366 function Get_WC_Encoding return Character;
367 -- Return wide character encoding method to set as WC_Encoding in output.
368 -- If -W has been used, returns the specified encoding, otherwise returns
369 -- the encoding method used for the main program source. If there is no
370 -- main program source (-z switch used), returns brackets ('b').
372 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean;
373 -- Determine whether the current unit has at least one library-level
376 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean;
377 -- Compare linker options, when sorting, first according to
378 -- Is_Internal_File (internal files come later) and then by
379 -- elaboration order position (latest to earliest).
381 procedure Move_Linker_Option (From : Natural; To : Natural);
382 -- Move routine for sorting linker options
384 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array);
385 -- Set the value of With_GNARL
387 procedure Set_Char (C : Character);
388 -- Set given character in Statement_Buffer at the Stm_Last + 1 position
389 -- and increment Stm_Last by one to reflect the stored character.
391 procedure Set_Int (N : Int);
392 -- Set given value in decimal in Statement_Buffer with no spaces starting
393 -- at the Stm_Last + 1 position, and updating Stm_Last past the value. A
394 -- minus sign is output for a negative value.
396 procedure Set_Boolean (B : Boolean);
397 -- Set given boolean value in Statement_Buffer at the Stm_Last + 1 position
398 -- and update Stm_Last past the value.
400 procedure Set_IS_Pragma_Table;
401 -- Initializes contents of IS_Pragma_Settings table from ALI table
403 procedure Set_Main_Program_Name;
404 -- Given the main program name in Name_Buffer (length in Name_Len) generate
405 -- the name of the routine to be used in the call. The name is generated
406 -- starting at Stm_Last + 1, and Stm_Last is updated past it.
408 procedure Set_Name_Buffer;
409 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
411 procedure Set_PSD_Pragma_Table;
412 -- Initializes contents of PSD_Pragma_Settings table from ALI table
414 procedure Set_String (S : String);
415 -- Sets characters of given string in Statement_Buffer, starting at the
416 -- Stm_Last + 1 position, and updating last past the string value.
418 procedure Set_String_Replace (S : String);
419 -- Replaces the last S'Length characters in the Statement_Buffer with the
420 -- characters of S. The caller must ensure that these characters do in fact
421 -- exist in the Statement_Buffer.
423 procedure Set_Unit_Name;
424 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
425 -- starting at the Stm_Last + 1 position and update Stm_Last past the
426 -- value. Each dot (.) will be qualified into double underscores (__).
428 procedure Set_Unit_Number (U : Unit_Id);
429 -- Sets unit number (first unit is 1, leading zeroes output to line up all
430 -- output unit numbers nicely as required by the value, and by the total
433 procedure Write_Statement_Buffer;
434 -- Write out contents of statement buffer up to Stm_Last, and reset
437 procedure Write_Statement_Buffer (S : String);
438 -- First writes its argument (using Set_String (S)), then writes out the
439 -- contents of statement buffer up to Stm_Last, and resets Stm_Last to 0.
441 procedure Write_Bind_Line (S : String);
442 -- Write S (an LF-terminated string) to the binder file (for use with
443 -- Set_Special_Output).
449 procedure Gen_Adafinal is
451 WBI (" procedure " & Ada_Final_Name.all & " is");
453 -- Call s_stalib_adafinal to await termination of tasks and so on. We
454 -- want to do this if there is a main program, either in Ada or in some
455 -- other language. (Note that Bind_Main_Program is True for Ada mains,
456 -- but False for mains in other languages.) We do not want to do this if
457 -- we're binding a library.
459 if not Bind_For_Library and not CodePeer_Mode then
460 WBI (" procedure s_stalib_adafinal;");
461 Set_String (" pragma Import (C, s_stalib_adafinal, ");
462 Set_String ("""system__standard_library__adafinal"");");
463 Write_Statement_Buffer;
467 WBI (" procedure Runtime_Finalize;");
468 WBI (" pragma Import (C, Runtime_Finalize, " &
469 """__gnat_runtime_finalize"");");
473 if not CodePeer_Mode then
474 WBI (" if not Is_Elaborated then");
477 WBI (" Is_Elaborated := False;");
480 WBI (" Runtime_Finalize;");
482 -- By default (real targets), finalization is done differently depending
483 -- on whether this is the main program or a library.
485 if not CodePeer_Mode then
486 if not Bind_For_Library then
487 WBI (" s_stalib_adafinal;");
488 elsif Lib_Final_Built then
489 WBI (" finalize_library;");
494 -- Pragma Import C cannot be used on virtual targets, therefore call the
495 -- runtime finalization routine directly in CodePeer mode, where
496 -- imported functions are ignored.
499 WBI (" System.Standard_Library.Adafinal;");
502 WBI (" end " & Ada_Final_Name.all & ";");
510 procedure Gen_Adainit (Elab_Order : Unit_Id_Array) is
511 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
512 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
515 -- Declare the access-to-subprogram type used for initialization of
516 -- of __gnat_finalize_library_objects. This is declared at library
517 -- level for compatibility with the type used in System.Soft_Links.
518 -- The import of the soft link which performs library-level object
519 -- finalization does not work for CodePeer, so regular Ada is used in
520 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
521 -- tasks are non-terminating, so we do not want finalization.
523 if not Suppress_Standard_Library_On_Target
524 and then not CodePeer_Mode
525 and then not Configurable_Run_Time_On_Target
527 WBI (" type No_Param_Proc is access procedure;");
531 WBI (" procedure " & Ada_Init_Name.all & " is");
533 -- In CodePeer mode, simplify adainit procedure by only calling
534 -- elaboration procedures.
536 if CodePeer_Mode then
539 -- If the standard library is suppressed, then the only global variables
540 -- that might be needed (by the Ravenscar profile) are the priority and
541 -- the processor for the environment task.
543 elsif Suppress_Standard_Library_On_Target then
544 if Main_Priority /= No_Main_Priority then
545 WBI (" Main_Priority : Integer;");
546 WBI (" pragma Import (C, Main_Priority," &
547 " ""__gl_main_priority"");");
551 if Main_CPU /= No_Main_CPU then
552 WBI (" Main_CPU : Integer;");
553 WBI (" pragma Import (C, Main_CPU," &
554 " ""__gl_main_cpu"");");
558 if System_Interrupts_Used
559 and then Partition_Elaboration_Policy_Specified = 'S'
561 WBI (" procedure Install_Restricted_Handlers_Sequential;");
562 WBI (" pragma Import (C," &
563 "Install_Restricted_Handlers_Sequential," &
564 " ""__gnat_attach_all_handlers"");");
568 if System_Tasking_Restricted_Stages_Used
569 and then Partition_Elaboration_Policy_Specified = 'S'
571 WBI (" Partition_Elaboration_Policy : Character;");
572 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
573 " ""__gnat_partition_elaboration_policy"");");
575 WBI (" procedure Activate_All_Tasks_Sequential;");
576 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
577 " ""__gnat_activate_all_tasks"");");
581 if System_BB_CPU_Primitives_Multiprocessors_Used then
582 WBI (" procedure Start_Slave_CPUs;");
583 WBI (" pragma Import (C, Start_Slave_CPUs," &
584 " ""__gnat_start_slave_cpus"");");
588 -- A restricted run-time may attempt to initialize the main task's
589 -- secondary stack even if the stack is not used. Consequently,
590 -- the binder needs to initialize Binder_Sec_Stacks_Count anytime
591 -- System.Secondary_Stack is in the enclosure of the partition.
593 if System_Secondary_Stack_Used then
594 WBI (" Binder_Sec_Stacks_Count : Natural;");
595 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
596 """__gnat_binder_ss_count"");");
600 if Sec_Stack_Used then
601 WBI (" Default_Secondary_Stack_Size : " &
602 "System.Parameters.Size_Type;");
603 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
604 """__gnat_default_ss_size"");");
606 WBI (" Default_Sized_SS_Pool : System.Address;");
607 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
608 """__gnat_default_ss_pool"");");
615 if Main_Priority /= No_Main_Priority then
616 Set_String (" Main_Priority := ");
617 Set_Int (Main_Priority);
619 Write_Statement_Buffer;
622 if Main_CPU /= No_Main_CPU then
623 Set_String (" Main_CPU := ");
626 Write_Statement_Buffer;
629 if System_Tasking_Restricted_Stages_Used
630 and then Partition_Elaboration_Policy_Specified = 'S'
632 Set_String (" Partition_Elaboration_Policy := '");
633 Set_Char (Partition_Elaboration_Policy_Specified);
635 Write_Statement_Buffer;
638 if Main_Priority = No_Main_Priority
639 and then Main_CPU = No_Main_CPU
640 and then not System_Tasking_Restricted_Stages_Used
645 -- Generate default-sized secondary stack pool and set secondary
648 if Sec_Stack_Used then
650 -- Elaborate the body of the binder to initialize the default-
651 -- sized secondary stack pool.
654 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
656 -- Generate the default-sized secondary stack pool and set the
657 -- related secondary stack globals.
659 Set_String (" Default_Secondary_Stack_Size := ");
661 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
662 Set_Int (Opt.Default_Sec_Stack_Size);
664 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
668 Write_Statement_Buffer;
670 Set_String (" Binder_Sec_Stacks_Count := ");
671 Set_Int (Num_Sec_Stacks);
673 Write_Statement_Buffer;
675 WBI (" Default_Sized_SS_Pool := " &
676 "Sec_Default_Sized_Stacks'Address;");
679 -- When a restricted run-time initializes the main task's secondary
680 -- stack but the program does not use it, no secondary stack is
681 -- generated. Binder_Sec_Stacks_Count is set to zero so the run-time
682 -- is aware that the lack of pre-allocated secondary stack is
685 elsif System_Secondary_Stack_Used then
686 WBI (" Binder_Sec_Stacks_Count := 0;");
689 -- Normal case (standard library not suppressed). Set all global values
690 -- used by the run time.
693 WBI (" Main_Priority : Integer;");
694 WBI (" pragma Import (C, Main_Priority, " &
695 """__gl_main_priority"");");
696 WBI (" Time_Slice_Value : Integer;");
697 WBI (" pragma Import (C, Time_Slice_Value, " &
698 """__gl_time_slice_val"");");
699 WBI (" WC_Encoding : Character;");
700 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
701 WBI (" Locking_Policy : Character;");
702 WBI (" pragma Import (C, Locking_Policy, " &
703 """__gl_locking_policy"");");
704 WBI (" Queuing_Policy : Character;");
705 WBI (" pragma Import (C, Queuing_Policy, " &
706 """__gl_queuing_policy"");");
707 WBI (" Task_Dispatching_Policy : Character;");
708 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
709 """__gl_task_dispatching_policy"");");
710 WBI (" Priority_Specific_Dispatching : System.Address;");
711 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
712 """__gl_priority_specific_dispatching"");");
713 WBI (" Num_Specific_Dispatching : Integer;");
714 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
715 """__gl_num_specific_dispatching"");");
716 WBI (" Main_CPU : Integer;");
717 WBI (" pragma Import (C, Main_CPU, " &
718 """__gl_main_cpu"");");
720 WBI (" Interrupt_States : System.Address;");
721 WBI (" pragma Import (C, Interrupt_States, " &
722 """__gl_interrupt_states"");");
723 WBI (" Num_Interrupt_States : Integer;");
724 WBI (" pragma Import (C, Num_Interrupt_States, " &
725 """__gl_num_interrupt_states"");");
726 WBI (" Unreserve_All_Interrupts : Integer;");
727 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
728 """__gl_unreserve_all_interrupts"");");
730 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
731 WBI (" Exception_Tracebacks : Integer;");
732 WBI (" pragma Import (C, Exception_Tracebacks, " &
733 """__gl_exception_tracebacks"");");
735 if Exception_Tracebacks_Symbolic then
736 WBI (" Exception_Tracebacks_Symbolic : Integer;");
737 WBI (" pragma Import (C, Exception_Tracebacks_Symbolic, " &
738 """__gl_exception_tracebacks_symbolic"");");
742 WBI (" Detect_Blocking : Integer;");
743 WBI (" pragma Import (C, Detect_Blocking, " &
744 """__gl_detect_blocking"");");
745 WBI (" Default_Stack_Size : Integer;");
746 WBI (" pragma Import (C, Default_Stack_Size, " &
747 """__gl_default_stack_size"");");
749 if Sec_Stack_Used then
750 WBI (" Default_Secondary_Stack_Size : " &
751 "System.Parameters.Size_Type;");
752 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
753 """__gnat_default_ss_size"");");
756 WBI (" Leap_Seconds_Support : Integer;");
757 WBI (" pragma Import (C, Leap_Seconds_Support, " &
758 """__gl_leap_seconds_support"");");
759 WBI (" Bind_Env_Addr : System.Address;");
760 WBI (" pragma Import (C, Bind_Env_Addr, " &
761 """__gl_bind_env_addr"");");
763 -- Import entry point for elaboration time signal handler
764 -- installation, and indication of if it's been called previously.
767 WBI (" procedure Runtime_Initialize " &
768 "(Install_Handler : Integer);");
769 WBI (" pragma Import (C, Runtime_Initialize, " &
770 """__gnat_runtime_initialize"");");
772 -- Import handlers attach procedure for sequential elaboration policy
774 if System_Interrupts_Used
775 and then Partition_Elaboration_Policy_Specified = 'S'
777 WBI (" procedure Install_Restricted_Handlers_Sequential;");
778 WBI (" pragma Import (C," &
779 "Install_Restricted_Handlers_Sequential," &
780 " ""__gnat_attach_all_handlers"");");
784 -- Import task activation procedure for sequential elaboration
787 if System_Tasking_Restricted_Stages_Used
788 and then Partition_Elaboration_Policy_Specified = 'S'
790 WBI (" Partition_Elaboration_Policy : Character;");
791 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
792 " ""__gnat_partition_elaboration_policy"");");
794 WBI (" procedure Activate_All_Tasks_Sequential;");
795 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
796 " ""__gnat_activate_all_tasks"");");
799 -- Import procedure to start slave cpus for bareboard runtime
801 if System_BB_CPU_Primitives_Multiprocessors_Used then
802 WBI (" procedure Start_Slave_CPUs;");
803 WBI (" pragma Import (C, Start_Slave_CPUs," &
804 " ""__gnat_start_slave_cpus"");");
807 -- For restricted run-time libraries (ZFP and Ravenscar)
808 -- tasks are non-terminating, so we do not want finalization.
810 if not Configurable_Run_Time_On_Target then
812 WBI (" Finalize_Library_Objects : No_Param_Proc;");
813 WBI (" pragma Import (C, Finalize_Library_Objects, " &
814 """__gnat_finalize_library_objects"");");
817 -- Initialize stack limit variable of the environment task if the
818 -- stack check method is stack limit and stack check is enabled.
820 if Stack_Check_Limits_On_Target
821 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
824 WBI (" procedure Initialize_Stack_Limit;");
825 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
826 """__gnat_initialize_stack_limit"");");
829 -- When dispatching domains are used then we need to signal it
830 -- before calling the main procedure.
832 if Dispatching_Domains_Used then
833 WBI (" procedure Freeze_Dispatching_Domains;");
834 WBI (" pragma Import");
835 WBI (" (Ada, Freeze_Dispatching_Domains, "
836 & """__gnat_freeze_dispatching_domains"");");
839 -- Secondary stack global variables
841 WBI (" Binder_Sec_Stacks_Count : Natural;");
842 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
843 """__gnat_binder_ss_count"");");
845 WBI (" Default_Sized_SS_Pool : System.Address;");
846 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
847 """__gnat_default_ss_pool"");");
851 -- Start of processing for Adainit
854 WBI (" if Is_Elaborated then");
857 WBI (" Is_Elaborated := True;");
859 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
860 -- restriction No_Standard_Allocators_After_Elaboration is active.
862 if Cumulative_Restrictions.Set
863 (No_Standard_Allocators_After_Elaboration)
865 WBI (" System.Elaboration_Allocators."
866 & "Mark_Start_Of_Elaboration;");
869 -- Generate assignments to initialize globals
871 Set_String (" Main_Priority := ");
872 Set_Int (Main_Priority);
874 Write_Statement_Buffer;
876 Set_String (" Time_Slice_Value := ");
878 if Task_Dispatching_Policy_Specified = 'F'
879 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
883 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
887 Write_Statement_Buffer;
889 Set_String (" WC_Encoding := '");
890 Set_Char (Get_WC_Encoding);
893 Write_Statement_Buffer;
895 Set_String (" Locking_Policy := '");
896 Set_Char (Locking_Policy_Specified);
898 Write_Statement_Buffer;
900 Set_String (" Queuing_Policy := '");
901 Set_Char (Queuing_Policy_Specified);
903 Write_Statement_Buffer;
905 Set_String (" Task_Dispatching_Policy := '");
906 Set_Char (Task_Dispatching_Policy_Specified);
908 Write_Statement_Buffer;
910 if System_Tasking_Restricted_Stages_Used
911 and then Partition_Elaboration_Policy_Specified = 'S'
913 Set_String (" Partition_Elaboration_Policy := '");
914 Set_Char (Partition_Elaboration_Policy_Specified);
916 Write_Statement_Buffer;
921 WBI (" Priority_Specific_Dispatching :=");
922 WBI (" Local_Priority_Specific_Dispatching'Address;");
924 Set_String (" Num_Specific_Dispatching := ");
925 Set_Int (PSD_Pragma_Settings.Last + 1);
927 Write_Statement_Buffer;
929 Set_String (" Main_CPU := ");
932 Write_Statement_Buffer;
934 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
936 Set_String (" Num_Interrupt_States := ");
937 Set_Int (IS_Pragma_Settings.Last + 1);
939 Write_Statement_Buffer;
941 Set_String (" Unreserve_All_Interrupts := ");
943 if Unreserve_All_Interrupts_Specified then
950 Write_Statement_Buffer;
952 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
953 WBI (" Exception_Tracebacks := 1;");
955 if Exception_Tracebacks_Symbolic then
956 WBI (" Exception_Tracebacks_Symbolic := 1;");
960 Set_String (" Detect_Blocking := ");
962 if Detect_Blocking then
969 Write_Statement_Buffer;
971 Set_String (" Default_Stack_Size := ");
972 Set_Int (Default_Stack_Size);
974 Write_Statement_Buffer;
976 Set_String (" Leap_Seconds_Support := ");
978 if Leap_Seconds_Support then
985 Write_Statement_Buffer;
987 if Bind_Env_String_Built then
988 WBI (" Bind_Env_Addr := Bind_Env'Address;");
993 -- Generate default-sized secondary stack pool and set secondary
996 if Sec_Stack_Used then
998 -- Elaborate the body of the binder to initialize the default-
999 -- sized secondary stack pool.
1001 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
1003 -- Generate the default-sized secondary stack pool and set the
1004 -- related secondary stack globals.
1006 Set_String (" Default_Secondary_Stack_Size := ");
1008 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
1009 Set_Int (Opt.Default_Sec_Stack_Size);
1011 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
1015 Write_Statement_Buffer;
1017 Set_String (" Binder_Sec_Stacks_Count := ");
1018 Set_Int (Num_Sec_Stacks);
1020 Write_Statement_Buffer;
1022 Set_String (" Default_Sized_SS_Pool := ");
1024 if Num_Sec_Stacks > 0 then
1025 Set_String ("Sec_Default_Sized_Stacks'Address;");
1027 Set_String ("System.Null_Address;");
1030 Write_Statement_Buffer;
1034 -- Generate call to Runtime_Initialize
1036 WBI (" Runtime_Initialize (1);");
1039 -- Generate call to set Initialize_Scalar values if active
1041 if Initialize_Scalars_Used then
1043 Set_String (" System.Scalar_Values.Initialize ('");
1044 Set_Char (Initialize_Scalars_Mode1);
1045 Set_String ("', '");
1046 Set_Char (Initialize_Scalars_Mode2);
1048 Write_Statement_Buffer;
1051 -- Initialize stack limit variable of the environment task if the stack
1052 -- check method is stack limit and stack check is enabled.
1054 if Stack_Check_Limits_On_Target
1055 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1058 WBI (" Initialize_Stack_Limit;");
1061 -- On CodePeer, the finalization of library objects is not relevant
1063 if CodePeer_Mode then
1066 -- If this is the main program case, attach finalize_library to the soft
1067 -- link. Do it only when not using a restricted run time, in which case
1068 -- tasks are non-terminating, so we do not want library-level
1071 elsif not Bind_For_Library
1072 and then not Configurable_Run_Time_On_Target
1073 and then not Suppress_Standard_Library_On_Target
1077 if Lib_Final_Built then
1078 Set_String (" Finalize_Library_Objects := ");
1079 Set_String ("finalize_library'access;");
1081 Set_String (" Finalize_Library_Objects := null;");
1084 Write_Statement_Buffer;
1087 -- Generate elaboration calls
1089 if not CodePeer_Mode then
1093 Gen_Elab_Calls (Elab_Order);
1095 if not CodePeer_Mode then
1097 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
1098 -- restriction No_Standard_Allocators_After_Elaboration is active.
1100 if Cumulative_Restrictions.Set
1101 (No_Standard_Allocators_After_Elaboration)
1104 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
1107 -- From this point, no new dispatching domain can be created
1109 if Dispatching_Domains_Used then
1110 WBI (" Freeze_Dispatching_Domains;");
1113 -- Sequential partition elaboration policy
1115 if Partition_Elaboration_Policy_Specified = 'S' then
1116 if System_Interrupts_Used then
1117 WBI (" Install_Restricted_Handlers_Sequential;");
1120 if System_Tasking_Restricted_Stages_Used then
1121 WBI (" Activate_All_Tasks_Sequential;");
1125 if System_BB_CPU_Primitives_Multiprocessors_Used then
1126 WBI (" Start_Slave_CPUs;");
1130 WBI (" end " & Ada_Init_Name.all & ";");
1134 -------------------------
1135 -- Gen_Bind_Env_String --
1136 -------------------------
1138 procedure Gen_Bind_Env_String is
1139 procedure Write_Name_With_Len (Nam : Name_Id);
1140 -- Write Nam as a string literal, prefixed with one
1141 -- character encoding Nam's length.
1143 -------------------------
1144 -- Write_Name_With_Len --
1145 -------------------------
1147 procedure Write_Name_With_Len (Nam : Name_Id) is
1149 Get_Name_String (Nam);
1152 Store_String_Char (Character'Val (Name_Len));
1153 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1155 Write_String_Table_Entry (End_String);
1156 end Write_Name_With_Len;
1161 KN : Name_Id := No_Name;
1162 VN : Name_Id := No_Name;
1164 -- Start of processing for Gen_Bind_Env_String
1167 Bind_Environment.Get_First (KN, VN);
1169 if VN = No_Name then
1173 Set_Special_Output (Write_Bind_Line'Access);
1175 WBI (" Bind_Env : aliased constant String :=");
1177 while VN /= No_Name loop
1178 Write_Str (" " & Amp & ' ');
1179 Write_Name_With_Len (KN);
1181 Write_Name_With_Len (VN);
1184 Bind_Environment.Get_Next (KN, VN);
1187 WBI (" & ASCII.NUL;");
1189 Cancel_Special_Output;
1191 Bind_Env_String_Built := True;
1192 end Gen_Bind_Env_String;
1194 --------------------------
1195 -- Gen_CodePeer_Wrapper --
1196 --------------------------
1198 procedure Gen_CodePeer_Wrapper is
1199 Callee_Name : constant String := "Ada_Main_Program";
1202 if ALIs.Table (ALIs.First).Main_Program = Proc then
1203 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
1205 WBI (" " & Callee_Name & ";");
1208 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1210 WBI (" return " & Callee_Name & ";");
1213 WBI (" end " & CodePeer_Wrapper_Name & ";");
1215 end Gen_CodePeer_Wrapper;
1217 --------------------
1218 -- Gen_Elab_Calls --
1219 --------------------
1221 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array) is
1222 Check_Elab_Flag : Boolean;
1225 -- Loop through elaboration order entries
1227 for E in Elab_Order'Range loop
1229 Unum : constant Unit_Id := Elab_Order (E);
1230 U : Unit_Record renames Units.Table (Unum);
1232 Unum_Spec : Unit_Id;
1233 -- This is the unit number of the spec that corresponds to
1234 -- this entry. It is the same as Unum except when the body
1235 -- and spec are different and we are currently processing
1236 -- the body, in which case it is the spec (Unum + 1).
1239 if U.Utype = Is_Body then
1240 Unum_Spec := Unum + 1;
1245 -- Nothing to do if predefined unit in no run time mode
1247 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1250 -- Likewise if this is an interface to a stand alone library
1252 elsif U.SAL_Interface then
1255 -- Case of no elaboration code
1259 -- In CodePeer mode, we special case subprogram bodies which
1260 -- are handled in the 'else' part below, and lead to a call
1261 -- to <subp>'Elab_Subp_Body.
1263 and then (not CodePeer_Mode
1267 or else U.Utype = Is_Spec
1268 or else U.Utype = Is_Spec_Only
1269 or else U.Unit_Kind /= 's')
1271 -- In the case of a body with a separate spec, where the
1272 -- separate spec has an elaboration entity defined, this is
1273 -- where we increment the elaboration entity if one exists.
1275 -- Likewise for lone specs with an elaboration entity defined
1276 -- despite No_Elaboration_Code, e.g. when requested to preserve
1279 if (U.Utype = Is_Body or else U.Utype = Is_Spec_Only)
1280 and then Units.Table (Unum_Spec).Set_Elab_Entity
1281 and then not CodePeer_Mode
1284 Set_Unit_Number (Unum_Spec);
1285 Set_String (" := E");
1286 Set_Unit_Number (Unum_Spec);
1287 Set_String (" + 1;");
1288 Write_Statement_Buffer;
1291 -- Here if elaboration code is present. If binding a library
1292 -- or if there is a non-Ada main subprogram then we generate:
1294 -- if uname_E = 0 then
1295 -- uname'elab_[spec|body];
1297 -- uname_E := uname_E + 1;
1299 -- Otherwise, elaboration routines are called unconditionally:
1301 -- uname'elab_[spec|body];
1302 -- uname_E := uname_E + 1;
1304 -- The uname_E increment is skipped if this is a separate spec,
1305 -- since it will be done when we process the body.
1307 -- In CodePeer mode, we do not generate any reference to xxx_E
1308 -- variables, only calls to 'Elab* subprograms.
1311 -- Check incompatibilities with No_Multiple_Elaboration
1313 if not CodePeer_Mode
1314 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1316 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1318 if Force_Checking_Of_Elaboration_Flags then
1320 ("-F (force elaboration checks) switch not allowed "
1321 & "with restriction No_Multiple_Elaboration active");
1323 -- Interfacing of libraries not allowed
1325 elsif Interface_Library_Unit then
1327 ("binding of interfaced libraries not allowed "
1328 & "with restriction No_Multiple_Elaboration active");
1330 -- Non-Ada main program not allowed
1332 elsif not Bind_Main_Program then
1334 ("non-Ada main program not allowed "
1335 & "with restriction No_Multiple_Elaboration active");
1339 -- OK, see if we need to test elaboration flag
1342 Units.Table (Unum_Spec).Set_Elab_Entity
1343 and then not CodePeer_Mode
1344 and then (Force_Checking_Of_Elaboration_Flags
1345 or Interface_Library_Unit
1346 or not Bind_Main_Program);
1348 if Check_Elab_Flag then
1349 Set_String (" if E");
1350 Set_Unit_Number (Unum_Spec);
1351 Set_String (" = 0 then");
1352 Write_Statement_Buffer;
1357 Get_Decoded_Name_String_With_Brackets (U.Uname);
1359 if Name_Buffer (Name_Len) = 's' then
1360 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1362 Name_Len := Name_Len + 8;
1364 -- Special case in CodePeer mode for subprogram bodies
1365 -- which correspond to CodePeer 'Elab_Subp_Body special
1368 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1369 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1371 Name_Len := Name_Len + 13;
1374 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1376 Name_Len := Name_Len + 8;
1379 Set_Casing (U.Icasing);
1382 Write_Statement_Buffer;
1384 if Check_Elab_Flag then
1388 if U.Utype /= Is_Spec
1389 and then not CodePeer_Mode
1390 and then Units.Table (Unum_Spec).Set_Elab_Entity
1393 Set_Unit_Number (Unum_Spec);
1394 Set_String (" := E");
1395 Set_Unit_Number (Unum_Spec);
1396 Set_String (" + 1;");
1397 Write_Statement_Buffer;
1404 ------------------------
1405 -- Gen_Elab_Externals --
1406 ------------------------
1408 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array) is
1410 if CodePeer_Mode then
1414 for E in Elab_Order'Range loop
1416 Unum : constant Unit_Id := Elab_Order (E);
1417 U : Unit_Record renames Units.Table (Unum);
1420 -- Check for Elab_Entity to be set for this unit
1422 if U.Set_Elab_Entity
1424 -- Don't generate reference for stand alone library
1426 and then not U.SAL_Interface
1428 -- Don't generate reference for predefined file in No_Run_Time
1429 -- mode, since we don't include the object files in this case
1433 and then Is_Predefined_File_Name (U.Sfile))
1435 Get_Name_String (U.Sfile);
1438 Set_Unit_Number (Unum);
1439 Set_String (" : Short_Integer; pragma Import (Ada, E");
1440 Set_Unit_Number (Unum);
1441 Set_String (", """);
1442 Get_Name_String (U.Uname);
1444 Set_String ("_E"");");
1445 Write_Statement_Buffer;
1451 end Gen_Elab_Externals;
1453 --------------------
1454 -- Gen_Elab_Order --
1455 --------------------
1457 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array) is
1460 WBI (" -- BEGIN ELABORATION ORDER");
1462 for J in Elab_Order'Range loop
1463 Set_String (" -- ");
1464 Get_Name_String (Units.Table (Elab_Order (J)).Uname);
1466 Write_Statement_Buffer;
1469 WBI (" -- END ELABORATION ORDER");
1472 --------------------------
1473 -- Gen_Finalize_Library --
1474 --------------------------
1476 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array) is
1477 procedure Gen_Header;
1478 -- Generate the header of the finalization routine
1484 procedure Gen_Header is
1486 WBI (" procedure finalize_library is");
1494 Uspec : Unit_Record;
1497 -- Start of processing for Gen_Finalize_Library
1500 if CodePeer_Mode then
1504 for E in reverse Elab_Order'Range loop
1505 Unum := Elab_Order (E);
1506 U := Units.Table (Unum);
1508 -- Dealing with package bodies is a little complicated. In such
1509 -- cases we must retrieve the package spec since it contains the
1510 -- spec of the body finalizer.
1512 if U.Utype = Is_Body then
1514 Uspec := Units.Table (Unum);
1519 Get_Name_String (Uspec.Uname);
1521 -- We are only interested in non-generic packages
1523 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1526 -- That aren't an interface to a stand alone library
1528 elsif U.SAL_Interface then
1531 -- Case of no finalization
1533 elsif not U.Has_Finalizer then
1535 -- The only case in which we have to do something is if this
1536 -- is a body, with a separate spec, where the separate spec
1537 -- has a finalizer. In that case, this is where we decrement
1538 -- the elaboration entity.
1540 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1541 if not Lib_Final_Built then
1543 Lib_Final_Built := True;
1547 Set_Unit_Number (Unum);
1548 Set_String (" := E");
1549 Set_Unit_Number (Unum);
1550 Set_String (" - 1;");
1551 Write_Statement_Buffer;
1555 if not Lib_Final_Built then
1557 Lib_Final_Built := True;
1562 -- procedure F<Count>;
1564 Set_String (" declare");
1565 Write_Statement_Buffer;
1567 Set_String (" procedure F");
1570 Write_Statement_Buffer;
1573 -- pragma Import (Ada, F<Count>,
1574 -- "xx__yy__finalize_[body|spec]");
1576 Set_String (" pragma Import (Ada, F");
1578 Set_String (", """);
1580 -- Perform name construction
1583 Set_String ("__finalize_");
1585 -- Package spec processing
1587 if U.Utype = Is_Spec
1588 or else U.Utype = Is_Spec_Only
1590 Set_String ("spec");
1592 -- Package body processing
1595 Set_String ("body");
1598 Set_String (""");");
1599 Write_Statement_Buffer;
1601 -- If binding a library or if there is a non-Ada main subprogram
1602 -- then we generate:
1605 -- uname_E := uname_E - 1;
1606 -- if uname_E = 0 then
1611 -- Otherwise, finalization routines are called unconditionally:
1614 -- uname_E := uname_E - 1;
1618 -- The uname_E decrement is skipped if this is a separate spec,
1619 -- since it will be done when we process the body.
1623 if U.Utype /= Is_Spec then
1625 Set_Unit_Number (Unum);
1626 Set_String (" := E");
1627 Set_Unit_Number (Unum);
1628 Set_String (" - 1;");
1629 Write_Statement_Buffer;
1632 if Interface_Library_Unit or not Bind_Main_Program then
1633 Set_String (" if E");
1634 Set_Unit_Number (Unum);
1635 Set_String (" = 0 then");
1636 Write_Statement_Buffer;
1643 Write_Statement_Buffer;
1645 if Interface_Library_Unit or not Bind_Main_Program then
1655 if Lib_Final_Built then
1657 -- It is possible that the finalization of a library-level object
1658 -- raised an exception. In that case import the actual exception
1659 -- and the routine necessary to raise it.
1662 WBI (" procedure Reraise_Library_Exception_If_Any;");
1664 Set_String (" pragma Import (Ada, ");
1665 Set_String ("Reraise_Library_Exception_If_Any, ");
1666 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1667 Write_Statement_Buffer;
1670 WBI (" Reraise_Library_Exception_If_Any;");
1672 WBI (" end finalize_library;");
1675 end Gen_Finalize_Library;
1681 procedure Gen_Main is
1683 if not No_Main_Subprogram then
1685 -- To call the main program, we declare it using a pragma Import
1686 -- Ada with the right link name.
1688 -- It might seem more obvious to "with" the main program, and call
1689 -- it in the normal Ada manner. We do not do this for three
1692 -- 1. It is more efficient not to recompile the main program
1693 -- 2. We are not entitled to assume the source is accessible
1694 -- 3. We don't know what options to use to compile it
1696 -- It is really reason 3 that is most critical (indeed we used
1697 -- to generate the "with", but several regression tests failed).
1699 if ALIs.Table (ALIs.First).Main_Program = Func then
1700 WBI (" function Ada_Main_Program return Integer;");
1702 WBI (" procedure Ada_Main_Program;");
1705 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1706 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1707 Set_Main_Program_Name;
1708 Set_String (""");");
1710 Write_Statement_Buffer;
1713 -- For CodePeer, declare a wrapper for the user-defined main program
1715 if CodePeer_Mode then
1716 Gen_CodePeer_Wrapper;
1720 if Exit_Status_Supported_On_Target then
1721 Set_String (" function ");
1723 Set_String (" procedure ");
1726 Set_String (Get_Main_Name);
1728 if Command_Line_Args_On_Target then
1729 Write_Statement_Buffer;
1730 WBI (" (argc : Integer;");
1731 WBI (" argv : System.Address;");
1732 WBI (" envp : System.Address)");
1734 if Exit_Status_Supported_On_Target then
1735 WBI (" return Integer");
1741 if Exit_Status_Supported_On_Target then
1742 Set_String (" return Integer is");
1747 Write_Statement_Buffer;
1750 if Opt.Default_Exit_Status /= 0
1751 and then Bind_Main_Program
1752 and then not Configurable_Run_Time_Mode
1754 WBI (" procedure Set_Exit_Status (Status : Integer);");
1755 WBI (" pragma Import (C, Set_Exit_Status, " &
1756 """__gnat_set_exit_status"");");
1760 -- Initialize and Finalize
1762 if not CodePeer_Mode
1763 and then not Cumulative_Restrictions.Set (No_Finalization)
1765 WBI (" procedure Initialize (Addr : System.Address);");
1766 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1768 WBI (" procedure Finalize;");
1769 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1772 -- If we want to analyze the stack, we must import corresponding symbols
1774 if Dynamic_Stack_Measurement then
1776 WBI (" procedure Output_Results;");
1777 WBI (" pragma Import (C, Output_Results, " &
1778 """__gnat_stack_usage_output_results"");");
1782 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1783 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1784 """__gnat_stack_usage_initialize"");");
1787 -- Deal with declarations for main program case
1789 if not No_Main_Subprogram then
1790 if ALIs.Table (ALIs.First).Main_Program = Func then
1791 WBI (" Result : Integer;");
1795 if Bind_Main_Program
1796 and not Suppress_Standard_Library_On_Target
1797 and not CodePeer_Mode
1799 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1804 -- Generate a reference to Ada_Main_Program_Name. This symbol is not
1805 -- referenced elsewhere in the generated program, but is needed by
1806 -- the debugger (that's why it is generated in the first place). The
1807 -- reference stops Ada_Main_Program_Name from being optimized away by
1810 -- Because this variable is unused, we make this variable "aliased"
1811 -- with a pragma Volatile in order to tell the compiler to preserve
1812 -- this variable at any level of optimization.
1814 -- CodePeer and CCG do not need this extra code on the other hand.
1816 if Bind_Main_Program
1817 and then not CodePeer_Mode
1818 and then not Generate_C_Code
1820 WBI (" Ensure_Reference : aliased System.Address := " &
1821 "Ada_Main_Program_Name'Address;");
1822 WBI (" pragma Volatile (Ensure_Reference);");
1828 -- Acquire command line arguments if present on target
1830 if CodePeer_Mode then
1833 elsif Command_Line_Args_On_Target then
1834 WBI (" gnat_argc := argc;");
1835 WBI (" gnat_argv := argv;");
1836 WBI (" gnat_envp := envp;");
1839 -- If configurable run time and no command line args, then nothing needs
1840 -- to be done since the gnat_argc/argv/envp variables are suppressed in
1843 elsif Configurable_Run_Time_On_Target then
1846 -- Otherwise set dummy values (to be filled in by some other unit?)
1849 WBI (" gnat_argc := 0;");
1850 WBI (" gnat_argv := System.Null_Address;");
1851 WBI (" gnat_envp := System.Null_Address;");
1854 if Opt.Default_Exit_Status /= 0
1855 and then Bind_Main_Program
1856 and then not Configurable_Run_Time_Mode
1858 Set_String (" Set_Exit_Status (");
1859 Set_Int (Opt.Default_Exit_Status);
1861 Write_Statement_Buffer;
1864 if Dynamic_Stack_Measurement then
1865 Set_String (" Initialize_Stack_Analysis (");
1866 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1868 Write_Statement_Buffer;
1871 if not Cumulative_Restrictions.Set (No_Finalization)
1872 and then not CodePeer_Mode
1874 if not No_Main_Subprogram
1875 and then Bind_Main_Program
1876 and then not Suppress_Standard_Library_On_Target
1878 WBI (" Initialize (SEH'Address);");
1880 WBI (" Initialize (System.Null_Address);");
1884 WBI (" " & Ada_Init_Name.all & ";");
1886 if not No_Main_Subprogram then
1887 if CodePeer_Mode then
1888 if ALIs.Table (ALIs.First).Main_Program = Proc then
1889 WBI (" " & CodePeer_Wrapper_Name & ";");
1891 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1894 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1895 WBI (" Ada_Main_Program;");
1898 WBI (" Result := Ada_Main_Program;");
1902 -- Adafinal call is skipped if no finalization
1904 if not Cumulative_Restrictions.Set (No_Finalization) then
1908 -- Prints the result of static stack analysis
1910 if Dynamic_Stack_Measurement then
1911 WBI (" Output_Results;");
1914 -- Finalize is only called if we have a run time
1916 if not Cumulative_Restrictions.Set (No_Finalization)
1917 and then not CodePeer_Mode
1924 if Exit_Status_Supported_On_Target then
1925 if No_Main_Subprogram
1926 or else ALIs.Table (ALIs.First).Main_Program = Proc
1928 WBI (" return (gnat_exit_status);");
1930 WBI (" return (Result);");
1938 ------------------------------
1939 -- Gen_Object_Files_Options --
1940 ------------------------------
1942 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array) is
1944 -- This keeps track of the position in the sorted set of entries in the
1945 -- Linker_Options table of where the first entry from an internal file
1948 Linker_Option_List_Started : Boolean := False;
1949 -- Set to True when "LINKER OPTION LIST" is displayed
1951 procedure Write_Linker_Option;
1952 -- Write binder info linker option
1954 -------------------------
1955 -- Write_Linker_Option --
1956 -------------------------
1958 procedure Write_Linker_Option is
1963 -- Loop through string, breaking at null's
1966 while Start < Name_Len loop
1968 -- Find null ending this section
1971 while Name_Buffer (Stop) /= ASCII.NUL
1972 and then Stop <= Name_Len loop
1976 -- Process section if non-null
1978 if Stop > Start then
1979 if Output_Linker_Option_List then
1980 if not Zero_Formatting then
1981 if not Linker_Option_List_Started then
1982 Linker_Option_List_Started := True;
1984 Write_Str (" LINKER OPTION LIST");
1992 Write_Str (Name_Buffer (Start .. Stop - 1));
1995 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
2000 end Write_Linker_Option;
2002 -- Start of processing for Gen_Object_Files_Options
2005 WBI ("-- BEGIN Object file/option list");
2007 if Object_List_Filename /= null then
2008 Set_List_File (Object_List_Filename.all);
2011 for E in Elab_Order'Range loop
2013 -- If not spec that has an associated body, then generate a comment
2014 -- giving the name of the corresponding object file.
2016 if not Units.Table (Elab_Order (E)).SAL_Interface
2017 and then Units.Table (Elab_Order (E)).Utype /= Is_Spec
2021 (Units.Table (Elab_Order (E)).My_ALI).Ofile_Full_Name);
2023 -- If the presence of an object file is necessary or if it exists,
2026 if not Hostparm.Exclude_Missing_Objects
2028 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
2030 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2032 if Output_Object_List then
2033 Write_Str (Name_Buffer (1 .. Name_Len));
2040 if Object_List_Filename /= null then
2044 -- Add a "-Ldir" for each directory in the object path
2046 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2048 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2052 Add_Str_To_Name_Buffer ("-L");
2053 Add_Str_To_Name_Buffer (Dir.all);
2054 Write_Linker_Option;
2058 if not (Opt.No_Run_Time_Mode or Opt.No_Stdlib) then
2061 if Opt.Shared_Libgnat then
2062 Add_Str_To_Name_Buffer ("-shared");
2064 Add_Str_To_Name_Buffer ("-static");
2067 -- Write directly to avoid inclusion in -K output as -static and
2068 -- -shared are not usually specified linker options.
2070 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2073 -- Sort linker options
2075 -- This sort accomplishes two important purposes:
2077 -- a) All application files are sorted to the front, and all GNAT
2078 -- internal files are sorted to the end. This results in a well
2079 -- defined dividing line between the two sets of files, for the
2080 -- purpose of inserting certain standard library references into
2081 -- the linker arguments list.
2083 -- b) Given two different units, we sort the linker options so that
2084 -- those from a unit earlier in the elaboration order comes later
2085 -- in the list. This is a heuristic designed to create a more
2086 -- friendly order of linker options when the operations appear in
2087 -- separate units. The idea is that if unit A must be elaborated
2088 -- before unit B, then it is more likely that B references
2089 -- libraries included by A, than vice versa, so we want libraries
2090 -- included by A to come after libraries included by B.
2092 -- These two criteria are implemented by function Lt_Linker_Option. Note
2093 -- that a special case of b) is that specs are elaborated before bodies,
2094 -- so linker options from specs come after linker options for bodies,
2095 -- and again, the assumption is that libraries used by the body are more
2096 -- likely to reference libraries used by the spec, than vice versa.
2099 (Linker_Options.Last,
2100 Move_Linker_Option'Access,
2101 Lt_Linker_Option'Access);
2103 -- Write user linker options, i.e. the set of linker options that come
2104 -- from all files other than GNAT internal files, Lgnat is left set to
2105 -- point to the first entry from a GNAT internal file, or past the end
2106 -- of the entries if there are no internal files.
2108 Lgnat := Linker_Options.Last + 1;
2110 for J in 1 .. Linker_Options.Last loop
2111 if not Linker_Options.Table (J).Internal_File then
2112 Get_Name_String (Linker_Options.Table (J).Name);
2113 Write_Linker_Option;
2120 -- Now we insert standard linker options that must appear after the
2121 -- entries from user files, and before the entries from GNAT run-time
2122 -- files. The reason for this decision is that libraries referenced
2123 -- by internal routines may reference these standard library entries.
2125 -- Note that we do not insert anything when pragma No_Run_Time has
2126 -- been specified or when the standard libraries are not to be used,
2127 -- otherwise on some platforms, we may get duplicate symbols when
2128 -- linking (not clear if this is still the case, but it is harmless).
2130 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2134 if Opt.Shared_Libgnat then
2135 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2137 Add_Str_To_Name_Buffer ("-lgnarl");
2140 Write_Linker_Option;
2145 if Opt.Shared_Libgnat then
2146 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2148 Add_Str_To_Name_Buffer ("-lgnat");
2151 Write_Linker_Option;
2154 -- Write linker options from all internal files
2156 for J in Lgnat .. Linker_Options.Last loop
2157 Get_Name_String (Linker_Options.Table (J).Name);
2158 Write_Linker_Option;
2161 if Output_Linker_Option_List and then not Zero_Formatting then
2165 WBI ("-- END Object file/option list ");
2166 end Gen_Object_Files_Options;
2168 ---------------------
2169 -- Gen_Output_File --
2170 ---------------------
2172 procedure Gen_Output_File
2174 Elab_Order : Unit_Id_Array)
2177 -- Acquire settings for Interrupt_State pragmas
2179 Set_IS_Pragma_Table;
2181 -- Acquire settings for Priority_Specific_Dispatching pragma
2183 Set_PSD_Pragma_Table;
2185 -- Override time slice value if -T switch is set
2187 if Time_Slice_Set then
2188 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2191 -- Count number of elaboration calls
2193 for E in Elab_Order'Range loop
2194 if Units.Table (Elab_Order (E)).No_Elab then
2197 Num_Elab_Calls := Num_Elab_Calls + 1;
2201 -- Count the number of statically allocated stacks to be generated by
2202 -- the binder. If the user has specified the number of default-sized
2203 -- secondary stacks, use that number. Otherwise start the count at one
2204 -- as the binder is responsible for creating a secondary stack for the
2207 if Opt.Quantity_Of_Default_Size_Sec_Stacks /= -1 then
2208 Num_Sec_Stacks := Quantity_Of_Default_Size_Sec_Stacks;
2209 elsif Sec_Stack_Used then
2210 Num_Sec_Stacks := 1;
2213 for J in Units.First .. Units.Last loop
2214 Num_Primary_Stacks :=
2215 Num_Primary_Stacks + Units.Table (J).Primary_Stack_Count;
2218 Num_Sec_Stacks + Units.Table (J).Sec_Stack_Count;
2221 -- Generate output file in appropriate language
2223 Gen_Output_File_Ada (Filename, Elab_Order);
2224 end Gen_Output_File;
2226 -------------------------
2227 -- Gen_Output_File_Ada --
2228 -------------------------
2230 procedure Gen_Output_File_Ada
2231 (Filename : String; Elab_Order : Unit_Id_Array)
2233 Ada_Main : constant String := Get_Ada_Main_Name;
2234 -- Name to be used for generated Ada main program. See the body of
2235 -- function Get_Ada_Main_Name for details on the form of the name.
2237 Needs_Library_Finalization : constant Boolean :=
2238 not Configurable_Run_Time_On_Target
2239 and then Has_Finalizer (Elab_Order);
2240 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2241 -- non-terminating, so we do not want finalization.
2244 -- Name of generated bind file (spec)
2247 -- Name of generated bind file (body)
2250 -- Create spec first
2252 Create_Binder_Output (Filename, 's', Bfiles);
2254 -- We always compile the binder file in Ada 95 mode so that we properly
2255 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2256 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2258 WBI ("pragma Warnings (Off);");
2259 WBI ("pragma Ada_95;");
2261 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2262 -- then we need to make sure that the binder program is compiled with
2263 -- the same restriction, so that no exception tables are generated.
2265 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2266 WBI ("pragma Restrictions (No_Exception_Handlers);");
2269 -- Same processing for Restrictions (No_Exception_Propagation)
2271 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2272 WBI ("pragma Restrictions (No_Exception_Propagation);");
2275 -- Same processing for pragma No_Run_Time
2277 if No_Run_Time_Mode then
2278 WBI ("pragma No_Run_Time;");
2281 -- Generate with of System so we can reference System.Address
2283 WBI ("with System;");
2285 -- Generate with of System.Initialize_Scalars if active
2287 if Initialize_Scalars_Used then
2288 WBI ("with System.Scalar_Values;");
2291 -- Generate withs of System.Secondary_Stack and System.Parameters to
2292 -- allow the generation of the default-sized secondary stack pool.
2294 if Sec_Stack_Used then
2295 WBI ("with System.Parameters;");
2296 WBI ("with System.Secondary_Stack;");
2299 Resolve_Binder_Options (Elab_Order);
2301 -- Generate standard with's
2303 if not Suppress_Standard_Library_On_Target then
2304 if CodePeer_Mode then
2305 WBI ("with System.Standard_Library;");
2309 WBI ("package " & Ada_Main & " is");
2311 -- Main program case
2313 if Bind_Main_Program then
2314 -- Generate argc/argv stuff unless suppressed
2316 if Command_Line_Args_On_Target
2317 or not Configurable_Run_Time_On_Target
2320 WBI (" gnat_argc : Integer;");
2321 WBI (" gnat_argv : System.Address;");
2322 WBI (" gnat_envp : System.Address;");
2324 -- If the standard library is not suppressed, these variables
2325 -- are in the run-time data area for easy run time access.
2327 if not Suppress_Standard_Library_On_Target then
2329 WBI (" pragma Import (C, gnat_argc);");
2330 WBI (" pragma Import (C, gnat_argv);");
2331 WBI (" pragma Import (C, gnat_envp);");
2335 -- Define exit status. Again in normal mode, this is in the run-time
2336 -- library, and is initialized there, but in the configurable
2337 -- run-time case, the variable is declared and initialized in this
2342 if Configurable_Run_Time_Mode then
2343 if Exit_Status_Supported_On_Target then
2344 WBI (" gnat_exit_status : Integer := 0;");
2348 WBI (" gnat_exit_status : Integer;");
2349 WBI (" pragma Import (C, gnat_exit_status);");
2352 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2353 -- the main program. Otherwise, it can lead under some circumstances
2354 -- to a symbol duplication during the link (for instance when a C
2355 -- program uses two Ada libraries). Also zero terminate the string
2356 -- so that its end can be found reliably at run time.
2359 WBI (" GNAT_Version : constant String :=");
2360 WBI (" """ & Ver_Prefix &
2361 Gnat_Version_String &
2363 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2366 Set_String (" Ada_Main_Program_Name : constant String := """);
2367 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2369 Set_Main_Program_Name;
2370 Set_String (""" & ASCII.NUL;");
2372 Write_Statement_Buffer;
2375 (" pragma Export (C, Ada_Main_Program_Name, " &
2376 """__gnat_ada_main_program_name"");");
2380 WBI (" procedure " & Ada_Init_Name.all & ";");
2381 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2382 Ada_Init_Name.all & """);");
2384 -- If -a has been specified use pragma Linker_Constructor for the init
2385 -- procedure and pragma Linker_Destructor for the final procedure.
2387 if Use_Pragma_Linker_Constructor then
2388 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2391 if not Cumulative_Restrictions.Set (No_Finalization) then
2393 WBI (" procedure " & Ada_Final_Name.all & ";");
2394 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2395 Ada_Final_Name.all & """);");
2397 if Use_Pragma_Linker_Constructor then
2398 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2402 if Bind_Main_Program then
2406 if Exit_Status_Supported_On_Target then
2407 Set_String (" function ");
2409 Set_String (" procedure ");
2412 Set_String (Get_Main_Name);
2414 -- Generate argument list if present
2416 if Command_Line_Args_On_Target then
2417 Write_Statement_Buffer;
2418 WBI (" (argc : Integer;");
2419 WBI (" argv : System.Address;");
2421 (" envp : System.Address)");
2423 if Exit_Status_Supported_On_Target then
2424 Write_Statement_Buffer;
2425 WBI (" return Integer;");
2427 Write_Statement_Buffer (";");
2431 if Exit_Status_Supported_On_Target then
2432 Write_Statement_Buffer (" return Integer;");
2434 Write_Statement_Buffer (";");
2438 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2439 Get_Main_Name & """);");
2442 -- Generate version numbers for units, only if needed. Be very safe on
2445 if not Configurable_Run_Time_On_Target
2446 or else System_Version_Control_Used
2447 or else not Bind_Main_Program
2452 Gen_Elab_Order (Elab_Order);
2457 WBI ("end " & Ada_Main & ";");
2458 Close_Binder_Output;
2460 -- Prepare to write body
2462 Create_Binder_Output (Filename, 'b', Bfileb);
2464 -- We always compile the binder file in Ada 95 mode so that we properly
2465 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2466 -- of the Ada 2005/2012 constructs are needed by the binder file.
2468 WBI ("pragma Warnings (Off);");
2469 WBI ("pragma Ada_95;");
2471 -- Output Source_File_Name pragmas which look like
2473 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2474 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2476 -- where sss/bbb are the spec/body file names respectively
2478 Get_Name_String (Bfiles);
2479 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2481 WBI ("pragma Source_File_Name (" &
2483 ", Spec_File_Name => """ &
2484 Name_Buffer (1 .. Name_Len + 3));
2486 Get_Name_String (Bfileb);
2487 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2489 WBI ("pragma Source_File_Name (" &
2491 ", Body_File_Name => """ &
2492 Name_Buffer (1 .. Name_Len + 3));
2494 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2495 -- versions of the compiler which have overflow checks on by default.
2496 -- We do not want overflow checking enabled for the increments of the
2497 -- elaboration variables (since this can cause an unwanted reference to
2498 -- the last chance exception handler for limited run-times).
2500 WBI ("pragma Suppress (Overflow_Check);");
2502 -- Generate with of System.Restrictions to initialize
2503 -- Run_Time_Restrictions.
2505 if System_Restrictions_Used
2506 and not Suppress_Standard_Library_On_Target
2509 WBI ("with System.Restrictions;");
2512 -- Generate with of Ada.Exceptions if needs library finalization
2514 if Needs_Library_Finalization then
2515 WBI ("with Ada.Exceptions;");
2518 -- Generate with of System.Elaboration_Allocators if the restriction
2519 -- No_Standard_Allocators_After_Elaboration was present.
2521 if Cumulative_Restrictions.Set
2522 (No_Standard_Allocators_After_Elaboration)
2524 WBI ("with System.Elaboration_Allocators;");
2527 -- Generate start of package body
2530 WBI ("package body " & Ada_Main & " is");
2533 -- Generate externals for elaboration entities
2535 Gen_Elab_Externals (Elab_Order);
2537 -- Generate default-sized secondary stacks pool. At least one stack is
2538 -- created and assigned to the environment task if secondary stacks are
2539 -- used by the program.
2541 if Sec_Stack_Used then
2542 Set_String (" Sec_Default_Sized_Stacks");
2543 Set_String (" : array (1 .. ");
2544 Set_Int (Num_Sec_Stacks);
2545 Set_String (") of aliased System.Secondary_Stack.SS_Stack (");
2547 if Opt.Default_Sec_Stack_Size /= No_Stack_Size then
2548 Set_Int (Opt.Default_Sec_Stack_Size);
2550 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
2554 Write_Statement_Buffer;
2558 -- Generate reference
2560 if not CodePeer_Mode then
2561 if not Suppress_Standard_Library_On_Target then
2563 -- Generate Priority_Specific_Dispatching pragma string
2566 (" Local_Priority_Specific_Dispatching : " &
2567 "constant String := """);
2569 for J in 0 .. PSD_Pragma_Settings.Last loop
2570 Set_Char (PSD_Pragma_Settings.Table (J));
2574 Write_Statement_Buffer;
2576 -- Generate Interrupt_State pragma string
2578 Set_String (" Local_Interrupt_States : constant String := """);
2580 for J in 0 .. IS_Pragma_Settings.Last loop
2581 Set_Char (IS_Pragma_Settings.Table (J));
2585 Write_Statement_Buffer;
2589 if not Suppress_Standard_Library_On_Target then
2591 -- The B.1(39) implementation advice says that the adainit and
2592 -- adafinal routines should be idempotent. Generate a flag to
2593 -- ensure that. This is not needed if we are suppressing the
2594 -- standard library since it would never be referenced.
2596 WBI (" Is_Elaborated : Boolean := False;");
2598 -- Generate bind environment string
2600 Gen_Bind_Env_String;
2606 -- Generate the adafinal routine unless there is no finalization to do
2608 if not Cumulative_Restrictions.Set (No_Finalization) then
2609 if Needs_Library_Finalization then
2610 Gen_Finalize_Library (Elab_Order);
2616 Gen_Adainit (Elab_Order);
2618 if Bind_Main_Program then
2622 -- Output object file list and the Ada body is complete
2624 Gen_Object_Files_Options (Elab_Order);
2627 WBI ("end " & Ada_Main & ";");
2629 Close_Binder_Output;
2630 end Gen_Output_File_Ada;
2632 ----------------------
2633 -- Gen_Restrictions --
2634 ----------------------
2636 procedure Gen_Restrictions is
2640 if Suppress_Standard_Library_On_Target
2641 or not System_Restrictions_Used
2646 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2652 for J in Cumulative_Restrictions.Set'Range loop
2653 Set_Boolean (Cumulative_Restrictions.Set (J));
2657 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2658 Write_Statement_Buffer;
2664 Set_String_Replace ("),");
2665 Write_Statement_Buffer;
2666 Set_String (" Value => (");
2668 for J in Cumulative_Restrictions.Value'Range loop
2669 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2673 Set_String_Replace ("),");
2674 Write_Statement_Buffer;
2675 WBI (" Violated =>");
2679 for J in Cumulative_Restrictions.Violated'Range loop
2680 Set_Boolean (Cumulative_Restrictions.Violated (J));
2684 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2685 Write_Statement_Buffer;
2691 Set_String_Replace ("),");
2692 Write_Statement_Buffer;
2693 Set_String (" Count => (");
2695 for J in Cumulative_Restrictions.Count'Range loop
2696 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2700 Set_String_Replace ("),");
2701 Write_Statement_Buffer;
2702 Set_String (" Unknown => (");
2704 for J in Cumulative_Restrictions.Unknown'Range loop
2705 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2709 Set_String_Replace ("))");
2711 Write_Statement_Buffer;
2712 end Gen_Restrictions;
2718 -- This routine generates lines such as:
2720 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2721 -- pragma Export (C, unnnnn, unam);
2723 -- for each unit, where unam is the unit name suffixed by either B or S for
2724 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2725 -- the version number, and nnnnn is a 5-digits serial number.
2727 procedure Gen_Versions is
2728 Ubuf : String (1 .. 6) := "u00000";
2730 procedure Increment_Ubuf;
2731 -- Little procedure to increment the serial number
2733 --------------------
2734 -- Increment_Ubuf --
2735 --------------------
2737 procedure Increment_Ubuf is
2739 for J in reverse Ubuf'Range loop
2740 Ubuf (J) := Character'Succ (Ubuf (J));
2741 exit when Ubuf (J) <= '9';
2746 -- Start of processing for Gen_Versions
2751 WBI (" type Version_32 is mod 2 ** 32;");
2752 for U in Units.First .. Units.Last loop
2753 if not Units.Table (U).SAL_Interface
2754 and then (not Bind_For_Library
2755 or else Units.Table (U).Directly_Scanned)
2758 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2759 Units.Table (U).Version & "#;");
2760 Set_String (" pragma Export (C, ");
2762 Set_String (", """);
2764 Get_Name_String (Units.Table (U).Uname);
2766 for K in 1 .. Name_Len loop
2767 if Name_Buffer (K) = '.' then
2771 elsif Name_Buffer (K) = '%' then
2775 Set_Char (Name_Buffer (K));
2779 if Name_Buffer (Name_Len) = 's' then
2785 Set_String (""");");
2786 Write_Statement_Buffer;
2791 ------------------------
2792 -- Get_Main_Unit_Name --
2793 ------------------------
2795 function Get_Main_Unit_Name (S : String) return String is
2796 Result : String := S;
2799 for J in S'Range loop
2800 if Result (J) = '.' then
2806 end Get_Main_Unit_Name;
2808 -----------------------
2809 -- Get_Ada_Main_Name --
2810 -----------------------
2812 function Get_Ada_Main_Name return String is
2813 Suffix : constant String := "_00";
2814 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2815 Opt.Ada_Main_Name.all & Suffix;
2819 -- For CodePeer, we want reproducible names (independent of other mains
2820 -- that may or may not be present) that don't collide when analyzing
2821 -- multiple mains and which are easily recognizable as "ada_main" names.
2823 if CodePeer_Mode then
2824 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2828 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2831 -- This loop tries the following possibilities in order
2837 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2838 -- it is set to 'ada_main'.
2840 for J in 0 .. 99 loop
2842 Nlen := Name'Length - Suffix'Length;
2844 Nlen := Name'Length;
2845 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2846 Name (Name'Last - 1) :=
2847 Character'Val (J / 10 + Character'Pos ('0'));
2850 for K in ALIs.First .. ALIs.Last loop
2851 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2853 -- Get unit name, removing %b or %e at end
2855 Get_Name_String (Units.Table (L).Uname);
2856 Name_Len := Name_Len - 2;
2858 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2864 return Name (1 .. Nlen);
2870 -- If we fall through, just use a peculiar unlikely name
2872 return ("Qwertyuiop");
2873 end Get_Ada_Main_Name;
2879 function Get_Main_Name return String is
2881 -- Explicit name given with -M switch
2883 if Bind_Alternate_Main_Name then
2884 return Alternate_Main_Name.all;
2886 -- Case of main program name to be used directly
2888 elsif Use_Ada_Main_Program_Name_On_Target then
2890 -- Get main program name
2892 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2894 -- If this is a child name, return only the name of the child, since
2895 -- we can't have dots in a nested program name. Note that we do not
2896 -- include the %b at the end of the unit name.
2898 for J in reverse 1 .. Name_Len - 2 loop
2899 if J = 1 or else Name_Buffer (J - 1) = '.' then
2900 return Name_Buffer (J .. Name_Len - 2);
2904 raise Program_Error; -- impossible exit
2906 -- Case where "main" is to be used as default
2913 ---------------------
2914 -- Get_WC_Encoding --
2915 ---------------------
2917 function Get_WC_Encoding return Character is
2919 -- If encoding method specified by -W switch, then return it
2921 if Wide_Character_Encoding_Method_Specified then
2922 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2924 -- If no main program, and not specified, set brackets, we really have
2925 -- no better choice. If some other encoding is required when there is
2926 -- no main, it must be set explicitly using -Wx.
2928 -- Note: if the ALI file always passed the wide character encoding of
2929 -- every file, then we could use the encoding of the initial specified
2930 -- file, but this information is passed only for potential main
2931 -- programs. We could fix this sometime, but it is a very minor point
2932 -- (wide character default encoding for [Wide_[Wide_]]Text_IO when there
2933 -- is no main program).
2935 elsif No_Main_Subprogram then
2938 -- Otherwise if there is a main program, take encoding from it
2941 return ALIs.Table (ALIs.First).WC_Encoding;
2943 end Get_WC_Encoding;
2949 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean is
2954 for E in reverse Elab_Order'Range loop
2955 Unum := Elab_Order (E);
2956 U := Units.Table (Unum);
2958 -- We are only interested in non-generic packages
2960 if U.Unit_Kind = 'p'
2961 and then U.Has_Finalizer
2962 and then not U.Is_Generic
2963 and then not U.No_Elab
2976 function Hash (Nam : Name_Id) return Header_Num is
2978 return Int (Nam - Names_Low_Bound) rem Header_Num'Last;
2981 ----------------------
2982 -- Lt_Linker_Option --
2983 ----------------------
2985 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean is
2987 -- Sort internal files last
2989 if Linker_Options.Table (Op1).Internal_File
2991 Linker_Options.Table (Op2).Internal_File
2993 -- Note: following test uses False < True
2995 return Linker_Options.Table (Op1).Internal_File
2997 Linker_Options.Table (Op2).Internal_File;
2999 -- If both internal or both non-internal, sort according to the
3000 -- elaboration position. A unit that is elaborated later should come
3001 -- earlier in the linker options list.
3004 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3006 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3008 end Lt_Linker_Option;
3010 ------------------------
3011 -- Move_Linker_Option --
3012 ------------------------
3014 procedure Move_Linker_Option (From : Natural; To : Natural) is
3016 Linker_Options.Table (To) := Linker_Options.Table (From);
3017 end Move_Linker_Option;
3019 ----------------------------
3020 -- Resolve_Binder_Options --
3021 ----------------------------
3023 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array) is
3024 procedure Check_Package (Var : in out Boolean; Name : String);
3025 -- Set Var to true iff the current identifier in Namet is Name. Do
3026 -- nothing if it doesn't match. This procedure is just a helper to
3027 -- avoid explicitly dealing with length.
3033 procedure Check_Package (Var : in out Boolean; Name : String) is
3035 if Name_Len = Name'Length
3036 and then Name_Buffer (1 .. Name_Len) = Name
3042 -- Start of processing for Resolve_Binder_Options
3045 for E in Elab_Order'Range loop
3046 Get_Name_String (Units.Table (Elab_Order (E)).Uname);
3048 -- This is not a perfect approach, but is the current protocol
3049 -- between the run-time and the binder to indicate that tasking is
3050 -- used: System.OS_Interface should always be used by any tasking
3053 Check_Package (With_GNARL, "system.os_interface%s");
3055 -- Ditto for the use of restricted tasking
3058 (System_Tasking_Restricted_Stages_Used,
3059 "system.tasking.restricted.stages%s");
3061 -- Ditto for the use of interrupts
3063 Check_Package (System_Interrupts_Used, "system.interrupts%s");
3065 -- Ditto for the use of dispatching domains
3068 (Dispatching_Domains_Used,
3069 "system.multiprocessors.dispatching_domains%s");
3071 -- Ditto for the use of restrictions
3073 Check_Package (System_Restrictions_Used, "system.restrictions%s");
3075 -- Ditto for the use of System.Secondary_Stack
3078 (System_Secondary_Stack_Used, "system.secondary_stack%s");
3080 -- Ditto for use of an SMP bareboard runtime
3082 Check_Package (System_BB_CPU_Primitives_Multiprocessors_Used,
3083 "system.bb.cpu_primitives.multiprocessors%s");
3085 -- Ditto for System.Version_Control, which is used for Version and
3086 -- Body_Version attributes.
3088 Check_Package (System_Version_Control_Used,
3089 "system.version_control%s");
3091 end Resolve_Binder_Options;
3097 procedure Set_Bind_Env (Key, Value : String) is
3099 -- The lengths of Key and Value are stored as single bytes
3101 if Key'Length > 255 then
3102 Osint.Fail ("bind environment key """ & Key & """ too long");
3105 if Value'Length > 255 then
3106 Osint.Fail ("bind environment value """ & Value & """ too long");
3109 Bind_Environment.Set (Name_Find (Key), Name_Find (Value));
3116 procedure Set_Boolean (B : Boolean) is
3117 False_Str : constant String := "False";
3118 True_Str : constant String := "True";
3122 Statement_Buffer (Stm_Last + 1 .. Stm_Last + True_Str'Length) :=
3124 Stm_Last := Stm_Last + True_Str'Length;
3126 Statement_Buffer (Stm_Last + 1 .. Stm_Last + False_Str'Length) :=
3128 Stm_Last := Stm_Last + False_Str'Length;
3136 procedure Set_Char (C : Character) is
3138 Stm_Last := Stm_Last + 1;
3139 Statement_Buffer (Stm_Last) := C;
3146 procedure Set_Int (N : Int) is
3157 Stm_Last := Stm_Last + 1;
3158 Statement_Buffer (Stm_Last) :=
3159 Character'Val (N mod 10 + Character'Pos ('0'));
3163 -------------------------
3164 -- Set_IS_Pragma_Table --
3165 -------------------------
3167 procedure Set_IS_Pragma_Table is
3169 for F in ALIs.First .. ALIs.Last loop
3170 for K in ALIs.Table (F).First_Interrupt_State ..
3171 ALIs.Table (F).Last_Interrupt_State
3174 Inum : constant Int :=
3175 Interrupt_States.Table (K).Interrupt_Id;
3176 Stat : constant Character :=
3177 Interrupt_States.Table (K).Interrupt_State;
3180 while IS_Pragma_Settings.Last < Inum loop
3181 IS_Pragma_Settings.Append ('n');
3184 IS_Pragma_Settings.Table (Inum) := Stat;
3188 end Set_IS_Pragma_Table;
3190 ---------------------------
3191 -- Set_Main_Program_Name --
3192 ---------------------------
3194 procedure Set_Main_Program_Name is
3196 -- Note that name has %b on the end which we ignore
3198 -- First we output the initial _ada_ since we know that the main program
3199 -- is a library level subprogram.
3201 Set_String ("_ada_");
3203 -- Copy name, changing dots to double underscores
3205 for J in 1 .. Name_Len - 2 loop
3206 if Name_Buffer (J) = '.' then
3209 Set_Char (Name_Buffer (J));
3212 end Set_Main_Program_Name;
3214 ---------------------
3215 -- Set_Name_Buffer --
3216 ---------------------
3218 procedure Set_Name_Buffer is
3220 for J in 1 .. Name_Len loop
3221 Set_Char (Name_Buffer (J));
3223 end Set_Name_Buffer;
3225 -------------------------
3226 -- Set_PSD_Pragma_Table --
3227 -------------------------
3229 procedure Set_PSD_Pragma_Table is
3231 for F in ALIs.First .. ALIs.Last loop
3232 for K in ALIs.Table (F).First_Specific_Dispatching ..
3233 ALIs.Table (F).Last_Specific_Dispatching
3236 DTK : Specific_Dispatching_Record
3237 renames Specific_Dispatching.Table (K);
3240 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3241 PSD_Pragma_Settings.Append ('F');
3244 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3245 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3250 end Set_PSD_Pragma_Table;
3256 procedure Set_String (S : String) is
3258 Statement_Buffer (Stm_Last + 1 .. Stm_Last + S'Length) := S;
3259 Stm_Last := Stm_Last + S'Length;
3262 ------------------------
3263 -- Set_String_Replace --
3264 ------------------------
3266 procedure Set_String_Replace (S : String) is
3268 Statement_Buffer (Stm_Last - S'Length + 1 .. Stm_Last) := S;
3269 end Set_String_Replace;
3275 procedure Set_Unit_Name is
3277 for J in 1 .. Name_Len - 2 loop
3278 if Name_Buffer (J) = '.' then
3281 Set_Char (Name_Buffer (J));
3286 ---------------------
3287 -- Set_Unit_Number --
3288 ---------------------
3290 procedure Set_Unit_Number (U : Unit_Id) is
3291 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3292 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3295 if Num_Units >= 10 and then Unum < 10 then
3299 if Num_Units >= 100 and then Unum < 100 then
3304 end Set_Unit_Number;
3306 ---------------------
3307 -- Write_Bind_Line --
3308 ---------------------
3310 procedure Write_Bind_Line (S : String) is
3312 -- Need to strip trailing LF from S
3314 WBI (S (S'First .. S'Last - 1));
3315 end Write_Bind_Line;
3317 ----------------------------
3318 -- Write_Statement_Buffer --
3319 ----------------------------
3321 procedure Write_Statement_Buffer is
3323 WBI (Statement_Buffer (1 .. Stm_Last));
3325 end Write_Statement_Buffer;
3327 procedure Write_Statement_Buffer (S : String) is
3330 Write_Statement_Buffer;
3331 end Write_Statement_Buffer;