1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 System_Restrictions_Used : Boolean := False;
63 -- Flag indicating whether the unit System.Restrictions is in the closure
64 -- of the partition. This is set by Resolve_Binder_Options, and is used
65 -- to determine whether or not to initialize the restrictions information
66 -- in the body of the binder generated file (we do not want to do this
67 -- unconditionally, since it drags in the System.Restrictions unit
68 -- unconditionally, which is unpleasand, especially for ZFP etc.)
70 Dispatching_Domains_Used : Boolean := False;
71 -- Flag indicating whether multiprocessor dispatching domains are used in
72 -- the closure of the partition. This is set by Resolve_Binder_Options, and
73 -- is used to call the routine to disallow the creation of new dispatching
74 -- domains just before calling the main procedure from the environment
77 System_Tasking_Restricted_Stages_Used : Boolean := False;
78 -- Flag indicating whether the unit System.Tasking.Restricted.Stages is in
79 -- the closure of the partition. This is set by Resolve_Binder_Options,
80 -- and it used to call a routine to active all the tasks at the end of
81 -- the elaboration when partition elaboration policy is sequential.
83 System_Interrupts_Used : Boolean := False;
84 -- Flag indicating whether the unit System.Interrups is in the closure of
85 -- the partition. This is set by Resolve_Binder_Options, and it used to
86 -- attach interrupt handlers at the end of the elaboration when partition
87 -- elaboration policy is sequential.
89 System_BB_CPU_Primitives_Multiprocessors_Used : Boolean := False;
90 -- Flag indicating whether unit System.BB.CPU_Primitives.Multiprocessors
91 -- is in the closure of the partition. This is set by procedure
92 -- Resolve_Binder_Options, and it is used to call a procedure that starts
95 System_Version_Control_Used : Boolean := False;
96 -- Flag indicating whether unit System.Version_Control is in the closure.
97 -- This unit is implicitly withed by the compiler when Version or
98 -- Body_Version attributes are used. If the package is not in the closure,
99 -- the version definitions can be removed.
101 Lib_Final_Built : Boolean := False;
102 -- Flag indicating whether the finalize_library rountine has been built
104 Bind_Env_String_Built : Boolean := False;
105 -- Flag indicating whether a bind environment string has been built
107 CodePeer_Wrapper_Name : constant String := "call_main_subprogram";
108 -- For CodePeer, introduce a wrapper subprogram which calls the
109 -- user-defined main subprogram.
111 ----------------------------------
112 -- Interface_State Pragma Table --
113 ----------------------------------
115 -- This table assembles the interface state pragma information from
116 -- all the units in the partition. Note that Bcheck has already checked
117 -- that the information is consistent across units. The entries
118 -- in this table are n/u/r/s for not set/user/runtime/system.
120 package IS_Pragma_Settings is new Table.Table
121 (Table_Component_Type => Character,
122 Table_Index_Type => Int,
123 Table_Low_Bound => 0,
124 Table_Initial => 100,
125 Table_Increment => 200,
126 Table_Name => "IS_Pragma_Settings");
128 -- This table assembles the Priority_Specific_Dispatching pragma
129 -- information from all the units in the partition. Note that Bcheck has
130 -- already checked that the information is consistent across units.
131 -- The entries in this table are the upper case first character of the
132 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
134 package PSD_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 => "PSD_Pragma_Settings");
142 ----------------------------
143 -- Bind_Environment Table --
144 ----------------------------
146 subtype Header_Num is Int range 0 .. 36;
148 function Hash (Nam : Name_Id) return Header_Num;
150 package Bind_Environment is new GNAT.HTable.Simple_HTable
151 (Header_Num => Header_Num,
153 No_Element => No_Name,
158 ----------------------
159 -- Run-Time Globals --
160 ----------------------
162 -- This section documents the global variables that are set from the
163 -- generated binder file.
165 -- Main_Priority : Integer;
166 -- Time_Slice_Value : Integer;
167 -- Heap_Size : Natural;
168 -- WC_Encoding : Character;
169 -- Locking_Policy : Character;
170 -- Queuing_Policy : Character;
171 -- Task_Dispatching_Policy : Character;
172 -- Priority_Specific_Dispatching : System.Address;
173 -- Num_Specific_Dispatching : Integer;
174 -- Restrictions : System.Address;
175 -- Interrupt_States : System.Address;
176 -- Num_Interrupt_States : Integer;
177 -- Unreserve_All_Interrupts : Integer;
178 -- Exception_Tracebacks : Integer;
179 -- Exception_Tracebacks_Symbolic : Integer;
180 -- Detect_Blocking : Integer;
181 -- Default_Stack_Size : Integer;
182 -- Leap_Seconds_Support : Integer;
183 -- Main_CPU : Integer;
185 -- Main_Priority is the priority value set by pragma Priority in the main
186 -- program. If no such pragma is present, the value is -1.
188 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
189 -- main program, or by the use of a -Tnnn parameter for the binder (if both
190 -- are present, the binder value overrides). The value is in milliseconds.
191 -- A value of zero indicates that time slicing should be suppressed. If no
192 -- pragma is present, and no -T switch was used, the value is -1.
194 -- WC_Encoding shows the wide character encoding method used for the main
195 -- program. This is one of the encoding letters defined in
196 -- System.WCh_Con.WC_Encoding_Letters.
198 -- Locking_Policy is a space if no locking policy was specified for the
199 -- partition. If a locking policy was specified, the value is the upper
200 -- case first character of the locking policy name, for example, 'C' for
203 -- Queuing_Policy is a space if no queuing policy was specified for the
204 -- partition. If a queuing policy was specified, the value is the upper
205 -- case first character of the queuing policy name for example, 'F' for
208 -- Task_Dispatching_Policy is a space if no task dispatching policy was
209 -- specified for the partition. If a task dispatching policy was specified,
210 -- the value is the upper case first character of the policy name, e.g. 'F'
211 -- for FIFO_Within_Priorities.
213 -- Priority_Specific_Dispatching is the address of a string used to store
214 -- the task dispatching policy specified for the different priorities in
215 -- the partition. The length of this string is determined by the last
216 -- priority for which such a pragma applies (the string will be a null
217 -- string if no specific dispatching policies were used). If pragma were
218 -- present, the entries apply to the priorities in sequence from the first
219 -- priority. The value stored is the upper case first character of the
220 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
221 -- for those priority ranges not specified.
223 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
224 -- string. It will be set to zero if no Priority_Specific_Dispatching
225 -- pragmas are present.
227 -- Restrictions is the address of a null-terminated string specifying the
228 -- restrictions information for the partition. The format is identical to
229 -- that of the parameter string found on R lines in ali files (see Lib.Writ
230 -- spec in lib-writ.ads for full details). The difference is that in this
231 -- context the values are the cumulative ones for the entire partition.
233 -- Interrupt_States is the address of a string used to specify the
234 -- cumulative results of Interrupt_State pragmas used in the partition.
235 -- The length of this string is determined by the last interrupt for which
236 -- such a pragma is given (the string will be a null string if no pragmas
237 -- were used). If pragma were present the entries apply to the interrupts
238 -- in sequence from the first interrupt, and are set to one of four
239 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
240 -- time, 's' for system, see description of Interrupt_State pragma for
243 -- Num_Interrupt_States is the length of the Interrupt_States string. It
244 -- will be set to zero if no Interrupt_State pragmas are present.
246 -- Unreserve_All_Interrupts is set to one if at least one unit in the
247 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
249 -- Exception_Tracebacks is set to one if the -Ea or -E parameter was
250 -- present in the bind and to zero otherwise. Note that on some targets
251 -- exception tracebacks are provided by default, so a value of zero for
252 -- this parameter does not necessarily mean no trace backs are available.
254 -- Exception_Tracebacks_Symbolic is set to one if the -Es parameter was
255 -- present in the bind and to zero otherwise.
257 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
258 -- not. A value of zero indicates that the pragma is not present, while a
259 -- value of 1 signals its presence in the partition.
261 -- Default_Stack_Size is the default stack size used when creating an Ada
262 -- task with no explicit Storage_Size clause.
264 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
265 -- disabled. A value of zero indicates that leap seconds are turned "off",
266 -- while a value of one signifies "on" status.
268 -- Main_CPU is the processor set by pragma CPU in the main program. If no
269 -- such pragma is present, the value is -1.
271 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
272 -- Convenient shorthand used throughout
274 -----------------------
275 -- Local Subprograms --
276 -----------------------
278 procedure Gen_Adainit (Elab_Order : Unit_Id_Array);
279 -- Generates the Adainit procedure
281 procedure Gen_Adafinal;
282 -- Generate the Adafinal procedure
284 procedure Gen_Bind_Env_String;
285 -- Generate the bind environment buffer
287 procedure Gen_CodePeer_Wrapper;
288 -- For CodePeer, generate wrapper which calls user-defined main subprogram
290 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array);
291 -- Generate sequence of elaboration calls
293 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array);
294 -- Generate sequence of external declarations for elaboration
296 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array);
297 -- Generate comments showing elaboration order chosen
299 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array);
300 -- Generate a sequence of finalization calls to elaborated packages
303 -- Generate procedure main
305 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array);
306 -- Output comments containing a list of the full names of the object
307 -- files to be linked and the list of linker options supplied by
308 -- Linker_Options pragmas in the source.
310 procedure Gen_Output_File_Ada
312 Elab_Order : Unit_Id_Array);
313 -- Generate Ada output file
315 procedure Gen_Restrictions;
316 -- Generate initialization of restrictions variable
318 procedure Gen_Versions;
319 -- Output series of definitions for unit versions
321 function Get_Ada_Main_Name return String;
322 -- This function is used for the Ada main output to compute a usable name
323 -- for the generated main program. The normal main program name is
324 -- Ada_Main, but this won't work if the user has a unit with this name.
325 -- This function tries Ada_Main first, and if there is such a clash, then
326 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
328 function Get_Main_Unit_Name (S : String) return String;
329 -- Return the main unit name corresponding to S by replacing '.' with '_'
331 function Get_Main_Name return String;
332 -- This function is used in the main output case to compute the correct
333 -- external main program. It is "main" by default, unless the flag
334 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
335 -- of the Ada main name without the "_ada". This default can be overridden
336 -- explicitly using the -Mname binder switch.
338 function Get_WC_Encoding return Character;
339 -- Return wide character encoding method to set as WC_Encoding in output.
340 -- If -W has been used, returns the specified encoding, otherwise returns
341 -- the encoding method used for the main program source. If there is no
342 -- main program source (-z switch used), returns brackets ('b').
344 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean;
345 -- Determine whether the current unit has at least one library-level
348 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean;
349 -- Compare linker options, when sorting, first according to
350 -- Is_Internal_File (internal files come later) and then by
351 -- elaboration order position (latest to earliest).
353 procedure Move_Linker_Option (From : Natural; To : Natural);
354 -- Move routine for sorting linker options
356 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array);
357 -- Set the value of With_GNARL
359 procedure Set_Char (C : Character);
360 -- Set given character in Statement_Buffer at the Stm_Last + 1 position
361 -- and increment Stm_Last by one to reflect the stored character.
363 procedure Set_Int (N : Int);
364 -- Set given value in decimal in Statement_Buffer with no spaces starting
365 -- at the Stm_Last + 1 position, and updating Stm_Last past the value. A
366 -- minus sign is output for a negative value.
368 procedure Set_Boolean (B : Boolean);
369 -- Set given boolean value in Statement_Buffer at the Stm_Last + 1 position
370 -- and update Stm_Last past the value.
372 procedure Set_IS_Pragma_Table;
373 -- Initializes contents of IS_Pragma_Settings table from ALI table
375 procedure Set_Main_Program_Name;
376 -- Given the main program name in Name_Buffer (length in Name_Len) generate
377 -- the name of the routine to be used in the call. The name is generated
378 -- starting at Stm_Last + 1, and Stm_Last is updated past it.
380 procedure Set_Name_Buffer;
381 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
383 procedure Set_PSD_Pragma_Table;
384 -- Initializes contents of PSD_Pragma_Settings table from ALI table
386 procedure Set_String (S : String);
387 -- Sets characters of given string in Statement_Buffer, starting at the
388 -- Stm_Last + 1 position, and updating last past the string value.
390 procedure Set_String_Replace (S : String);
391 -- Replaces the last S'Length characters in the Statement_Buffer with the
392 -- characters of S. The caller must ensure that these characters do in fact
393 -- exist in the Statement_Buffer.
395 procedure Set_Unit_Name;
396 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
397 -- starting at the Stm_Last + 1 position and update Stm_Last past the
398 -- value. Each dot (.) will be qualified into double underscores (__).
400 procedure Set_Unit_Number (U : Unit_Id);
401 -- Sets unit number (first unit is 1, leading zeroes output to line up all
402 -- output unit numbers nicely as required by the value, and by the total
405 procedure Write_Statement_Buffer;
406 -- Write out contents of statement buffer up to Stm_Last, and reset
409 procedure Write_Statement_Buffer (S : String);
410 -- First writes its argument (using Set_String (S)), then writes out the
411 -- contents of statement buffer up to Stm_Last, and resets Stm_Last to 0.
413 procedure Write_Bind_Line (S : String);
414 -- Write S (an LF-terminated string) to the binder file (for use with
415 -- Set_Special_Output).
421 procedure Gen_Adafinal is
423 WBI (" procedure " & Ada_Final_Name.all & " is");
425 -- Call s_stalib_adafinal to await termination of tasks and so on. We
426 -- want to do this if there is a main program, either in Ada or in some
427 -- other language. (Note that Bind_Main_Program is True for Ada mains,
428 -- but False for mains in other languages.) We do not want to do this if
429 -- we're binding a library.
431 if not Bind_For_Library and not CodePeer_Mode 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 (" procedure Runtime_Finalize;");
440 WBI (" pragma Import (C, Runtime_Finalize, " &
441 """__gnat_runtime_finalize"");");
445 if not CodePeer_Mode then
446 WBI (" if not Is_Elaborated then");
449 WBI (" Is_Elaborated := False;");
452 WBI (" Runtime_Finalize;");
454 -- By default (real targets), finalization is done differently depending
455 -- on whether this is the main program or a library.
457 if not CodePeer_Mode then
458 if not Bind_For_Library then
459 WBI (" s_stalib_adafinal;");
460 elsif Lib_Final_Built then
461 WBI (" finalize_library;");
466 -- Pragma Import C cannot be used on virtual targets, therefore call the
467 -- runtime finalization routine directly in CodePeer mode, where
468 -- imported functions are ignored.
471 WBI (" System.Standard_Library.Adafinal;");
474 WBI (" end " & Ada_Final_Name.all & ";");
482 procedure Gen_Adainit (Elab_Order : Unit_Id_Array) is
483 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
484 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
487 -- Declare the access-to-subprogram type used for initialization of
488 -- of __gnat_finalize_library_objects. This is declared at library
489 -- level for compatibility with the type used in System.Soft_Links.
490 -- The import of the soft link which performs library-level object
491 -- finalization does not work for CodePeer, so regular Ada is used in
492 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
493 -- tasks are non-terminating, so we do not want finalization.
495 if not Suppress_Standard_Library_On_Target
496 and then not CodePeer_Mode
497 and then not Configurable_Run_Time_On_Target
499 WBI (" type No_Param_Proc is access procedure;");
503 WBI (" procedure " & Ada_Init_Name.all & " is");
505 -- In CodePeer mode, simplify adainit procedure by only calling
506 -- elaboration procedures.
508 if CodePeer_Mode then
511 -- If the standard library is suppressed, then the only global variables
512 -- that might be needed (by the Ravenscar profile) are the priority and
513 -- the processor for the environment task.
515 elsif Suppress_Standard_Library_On_Target then
516 if Main_Priority /= No_Main_Priority then
517 WBI (" Main_Priority : Integer;");
518 WBI (" pragma Import (C, Main_Priority," &
519 " ""__gl_main_priority"");");
523 if Main_CPU /= No_Main_CPU then
524 WBI (" Main_CPU : Integer;");
525 WBI (" pragma Import (C, Main_CPU," &
526 " ""__gl_main_cpu"");");
530 if System_Interrupts_Used
531 and then Partition_Elaboration_Policy_Specified = 'S'
533 WBI (" procedure Install_Restricted_Handlers_Sequential;");
534 WBI (" pragma Import (C," &
535 "Install_Restricted_Handlers_Sequential," &
536 " ""__gnat_attach_all_handlers"");");
540 if System_Tasking_Restricted_Stages_Used
541 and then Partition_Elaboration_Policy_Specified = 'S'
543 WBI (" Partition_Elaboration_Policy : Character;");
544 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
545 " ""__gnat_partition_elaboration_policy"");");
547 WBI (" procedure Activate_All_Tasks_Sequential;");
548 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
549 " ""__gnat_activate_all_tasks"");");
553 if System_BB_CPU_Primitives_Multiprocessors_Used then
554 WBI (" procedure Start_Slave_CPUs;");
555 WBI (" pragma Import (C, Start_Slave_CPUs," &
556 " ""__gnat_start_slave_cpus"");");
561 if Main_Priority /= No_Main_Priority then
562 Set_String (" Main_Priority := ");
563 Set_Int (Main_Priority);
565 Write_Statement_Buffer;
568 if Main_CPU /= No_Main_CPU then
569 Set_String (" Main_CPU := ");
572 Write_Statement_Buffer;
575 if System_Tasking_Restricted_Stages_Used
576 and then Partition_Elaboration_Policy_Specified = 'S'
578 Set_String (" Partition_Elaboration_Policy := '");
579 Set_Char (Partition_Elaboration_Policy_Specified);
581 Write_Statement_Buffer;
584 if Main_Priority = No_Main_Priority
585 and then Main_CPU = No_Main_CPU
586 and then not System_Tasking_Restricted_Stages_Used
591 -- Normal case (standard library not suppressed). Set all global values
592 -- used by the run time.
595 WBI (" Main_Priority : Integer;");
596 WBI (" pragma Import (C, Main_Priority, " &
597 """__gl_main_priority"");");
598 WBI (" Time_Slice_Value : Integer;");
599 WBI (" pragma Import (C, Time_Slice_Value, " &
600 """__gl_time_slice_val"");");
601 WBI (" WC_Encoding : Character;");
602 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
603 WBI (" Locking_Policy : Character;");
604 WBI (" pragma Import (C, Locking_Policy, " &
605 """__gl_locking_policy"");");
606 WBI (" Queuing_Policy : Character;");
607 WBI (" pragma Import (C, Queuing_Policy, " &
608 """__gl_queuing_policy"");");
609 WBI (" Task_Dispatching_Policy : Character;");
610 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
611 """__gl_task_dispatching_policy"");");
612 WBI (" Priority_Specific_Dispatching : System.Address;");
613 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
614 """__gl_priority_specific_dispatching"");");
615 WBI (" Num_Specific_Dispatching : Integer;");
616 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
617 """__gl_num_specific_dispatching"");");
618 WBI (" Main_CPU : Integer;");
619 WBI (" pragma Import (C, Main_CPU, " &
620 """__gl_main_cpu"");");
622 WBI (" Interrupt_States : System.Address;");
623 WBI (" pragma Import (C, Interrupt_States, " &
624 """__gl_interrupt_states"");");
625 WBI (" Num_Interrupt_States : Integer;");
626 WBI (" pragma Import (C, Num_Interrupt_States, " &
627 """__gl_num_interrupt_states"");");
628 WBI (" Unreserve_All_Interrupts : Integer;");
629 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
630 """__gl_unreserve_all_interrupts"");");
632 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
633 WBI (" Exception_Tracebacks : Integer;");
634 WBI (" pragma Import (C, Exception_Tracebacks, " &
635 """__gl_exception_tracebacks"");");
637 if Exception_Tracebacks_Symbolic then
638 WBI (" Exception_Tracebacks_Symbolic : Integer;");
639 WBI (" pragma Import (C, Exception_Tracebacks_Symbolic, " &
640 """__gl_exception_tracebacks_symbolic"");");
644 WBI (" Detect_Blocking : Integer;");
645 WBI (" pragma Import (C, Detect_Blocking, " &
646 """__gl_detect_blocking"");");
647 WBI (" Default_Stack_Size : Integer;");
648 WBI (" pragma Import (C, Default_Stack_Size, " &
649 """__gl_default_stack_size"");");
650 WBI (" Leap_Seconds_Support : Integer;");
651 WBI (" pragma Import (C, Leap_Seconds_Support, " &
652 """__gl_leap_seconds_support"");");
653 WBI (" Bind_Env_Addr : System.Address;");
654 WBI (" pragma Import (C, Bind_Env_Addr, " &
655 """__gl_bind_env_addr"");");
657 -- Import entry point for elaboration time signal handler
658 -- installation, and indication of if it's been called previously.
661 WBI (" procedure Runtime_Initialize " &
662 "(Install_Handler : Integer);");
663 WBI (" pragma Import (C, Runtime_Initialize, " &
664 """__gnat_runtime_initialize"");");
666 -- Import handlers attach procedure for sequential elaboration policy
668 if System_Interrupts_Used
669 and then Partition_Elaboration_Policy_Specified = 'S'
671 WBI (" procedure Install_Restricted_Handlers_Sequential;");
672 WBI (" pragma Import (C," &
673 "Install_Restricted_Handlers_Sequential," &
674 " ""__gnat_attach_all_handlers"");");
678 -- Import task activation procedure for sequential elaboration
681 if System_Tasking_Restricted_Stages_Used
682 and then Partition_Elaboration_Policy_Specified = 'S'
684 WBI (" Partition_Elaboration_Policy : Character;");
685 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
686 " ""__gnat_partition_elaboration_policy"");");
688 WBI (" procedure Activate_All_Tasks_Sequential;");
689 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
690 " ""__gnat_activate_all_tasks"");");
693 -- Import procedure to start slave cpus for bareboard runtime
695 if System_BB_CPU_Primitives_Multiprocessors_Used then
696 WBI (" procedure Start_Slave_CPUs;");
697 WBI (" pragma Import (C, Start_Slave_CPUs," &
698 " ""__gnat_start_slave_cpus"");");
701 -- For restricted run-time libraries (ZFP and Ravenscar)
702 -- tasks are non-terminating, so we do not want finalization.
704 if not Configurable_Run_Time_On_Target then
706 WBI (" Finalize_Library_Objects : No_Param_Proc;");
707 WBI (" pragma Import (C, Finalize_Library_Objects, " &
708 """__gnat_finalize_library_objects"");");
711 -- Initialize stack limit variable of the environment task if the
712 -- stack check method is stack limit and stack check is enabled.
714 if Stack_Check_Limits_On_Target
715 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
718 WBI (" procedure Initialize_Stack_Limit;");
719 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
720 """__gnat_initialize_stack_limit"");");
723 -- When dispatching domains are used then we need to signal it
724 -- before calling the main procedure.
726 if Dispatching_Domains_Used then
727 WBI (" procedure Freeze_Dispatching_Domains;");
728 WBI (" pragma Import");
729 WBI (" (Ada, Freeze_Dispatching_Domains, "
730 & """__gnat_freeze_dispatching_domains"");");
733 -- Start of processing for Adainit
736 WBI (" if Is_Elaborated then");
739 WBI (" Is_Elaborated := True;");
741 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
742 -- restriction No_Standard_Allocators_After_Elaboration is active.
744 if Cumulative_Restrictions.Set
745 (No_Standard_Allocators_After_Elaboration)
747 WBI (" System.Elaboration_Allocators."
748 & "Mark_Start_Of_Elaboration;");
751 -- Generate assignments to initialize globals
753 Set_String (" Main_Priority := ");
754 Set_Int (Main_Priority);
756 Write_Statement_Buffer;
758 Set_String (" Time_Slice_Value := ");
760 if Task_Dispatching_Policy_Specified = 'F'
761 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
765 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
769 Write_Statement_Buffer;
771 Set_String (" WC_Encoding := '");
772 Set_Char (Get_WC_Encoding);
775 Write_Statement_Buffer;
777 Set_String (" Locking_Policy := '");
778 Set_Char (Locking_Policy_Specified);
780 Write_Statement_Buffer;
782 Set_String (" Queuing_Policy := '");
783 Set_Char (Queuing_Policy_Specified);
785 Write_Statement_Buffer;
787 Set_String (" Task_Dispatching_Policy := '");
788 Set_Char (Task_Dispatching_Policy_Specified);
790 Write_Statement_Buffer;
792 if System_Tasking_Restricted_Stages_Used
793 and then Partition_Elaboration_Policy_Specified = 'S'
795 Set_String (" Partition_Elaboration_Policy := '");
796 Set_Char (Partition_Elaboration_Policy_Specified);
798 Write_Statement_Buffer;
803 WBI (" Priority_Specific_Dispatching :=");
804 WBI (" Local_Priority_Specific_Dispatching'Address;");
806 Set_String (" Num_Specific_Dispatching := ");
807 Set_Int (PSD_Pragma_Settings.Last + 1);
809 Write_Statement_Buffer;
811 Set_String (" Main_CPU := ");
814 Write_Statement_Buffer;
816 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
818 Set_String (" Num_Interrupt_States := ");
819 Set_Int (IS_Pragma_Settings.Last + 1);
821 Write_Statement_Buffer;
823 Set_String (" Unreserve_All_Interrupts := ");
825 if Unreserve_All_Interrupts_Specified then
832 Write_Statement_Buffer;
834 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
835 WBI (" Exception_Tracebacks := 1;");
837 if Exception_Tracebacks_Symbolic then
838 WBI (" Exception_Tracebacks_Symbolic := 1;");
842 Set_String (" Detect_Blocking := ");
844 if Detect_Blocking then
851 Write_Statement_Buffer;
853 Set_String (" Default_Stack_Size := ");
854 Set_Int (Default_Stack_Size);
856 Write_Statement_Buffer;
858 Set_String (" Leap_Seconds_Support := ");
860 if Leap_Seconds_Support then
867 Write_Statement_Buffer;
869 if Bind_Env_String_Built then
870 WBI (" Bind_Env_Addr := Bind_Env'Address;");
873 -- Generate call to Install_Handler
876 WBI (" Runtime_Initialize (1);");
879 -- Generate call to set Initialize_Scalar values if active
881 if Initialize_Scalars_Used then
883 Set_String (" System.Scalar_Values.Initialize ('");
884 Set_Char (Initialize_Scalars_Mode1);
886 Set_Char (Initialize_Scalars_Mode2);
888 Write_Statement_Buffer;
891 -- Generate assignment of default secondary stack size if set
893 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
895 Set_String (" System.Secondary_Stack.");
896 Set_String ("Default_Secondary_Stack_Size := ");
897 Set_Int (Opt.Default_Sec_Stack_Size);
899 Write_Statement_Buffer;
902 -- Initialize stack limit variable of the environment task if the stack
903 -- check method is stack limit and stack check is enabled.
905 if Stack_Check_Limits_On_Target
906 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
909 WBI (" Initialize_Stack_Limit;");
912 -- On CodePeer, the finalization of library objects is not relevant
914 if CodePeer_Mode then
917 -- If this is the main program case, attach finalize_library to the soft
918 -- link. Do it only when not using a restricted run time, in which case
919 -- tasks are non-terminating, so we do not want library-level
922 elsif not Bind_For_Library
923 and then not Configurable_Run_Time_On_Target
924 and then not Suppress_Standard_Library_On_Target
928 if Lib_Final_Built then
929 Set_String (" Finalize_Library_Objects := ");
930 Set_String ("finalize_library'access;");
932 Set_String (" Finalize_Library_Objects := null;");
935 Write_Statement_Buffer;
938 -- Generate elaboration calls
940 if not CodePeer_Mode then
944 Gen_Elab_Calls (Elab_Order);
946 if not CodePeer_Mode then
948 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
949 -- restriction No_Standard_Allocators_After_Elaboration is active.
951 if Cumulative_Restrictions.Set
952 (No_Standard_Allocators_After_Elaboration)
955 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
958 -- From this point, no new dispatching domain can be created
960 if Dispatching_Domains_Used then
961 WBI (" Freeze_Dispatching_Domains;");
964 -- Sequential partition elaboration policy
966 if Partition_Elaboration_Policy_Specified = 'S' then
967 if System_Interrupts_Used then
968 WBI (" Install_Restricted_Handlers_Sequential;");
971 if System_Tasking_Restricted_Stages_Used then
972 WBI (" Activate_All_Tasks_Sequential;");
976 if System_BB_CPU_Primitives_Multiprocessors_Used then
977 WBI (" Start_Slave_CPUs;");
981 WBI (" end " & Ada_Init_Name.all & ";");
985 -------------------------
986 -- Gen_Bind_Env_String --
987 -------------------------
989 procedure Gen_Bind_Env_String is
990 procedure Write_Name_With_Len (Nam : Name_Id);
991 -- Write Nam as a string literal, prefixed with one
992 -- character encoding Nam's length.
994 -------------------------
995 -- Write_Name_With_Len --
996 -------------------------
998 procedure Write_Name_With_Len (Nam : Name_Id) is
1000 Get_Name_String (Nam);
1003 Store_String_Char (Character'Val (Name_Len));
1004 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1006 Write_String_Table_Entry (End_String);
1007 end Write_Name_With_Len;
1012 KN : Name_Id := No_Name;
1013 VN : Name_Id := No_Name;
1015 -- Start of processing for Gen_Bind_Env_String
1018 Bind_Environment.Get_First (KN, VN);
1020 if VN = No_Name then
1024 Set_Special_Output (Write_Bind_Line'Access);
1026 WBI (" Bind_Env : aliased constant String :=");
1028 while VN /= No_Name loop
1029 Write_Str (" " & Amp & ' ');
1030 Write_Name_With_Len (KN);
1032 Write_Name_With_Len (VN);
1035 Bind_Environment.Get_Next (KN, VN);
1038 WBI (" & ASCII.NUL;");
1040 Set_Special_Output (null);
1042 Bind_Env_String_Built := True;
1043 end Gen_Bind_Env_String;
1045 --------------------------
1046 -- Gen_CodePeer_Wrapper --
1047 --------------------------
1049 procedure Gen_CodePeer_Wrapper is
1050 Callee_Name : constant String := "Ada_Main_Program";
1053 if ALIs.Table (ALIs.First).Main_Program = Proc then
1054 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
1056 WBI (" " & Callee_Name & ";");
1059 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1061 WBI (" return " & Callee_Name & ";");
1064 WBI (" end " & CodePeer_Wrapper_Name & ";");
1066 end Gen_CodePeer_Wrapper;
1068 --------------------
1069 -- Gen_Elab_Calls --
1070 --------------------
1072 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array) is
1073 Check_Elab_Flag : Boolean;
1076 -- Loop through elaboration order entries
1078 for E in Elab_Order'Range loop
1080 Unum : constant Unit_Id := Elab_Order (E);
1081 U : Unit_Record renames Units.Table (Unum);
1083 Unum_Spec : Unit_Id;
1084 -- This is the unit number of the spec that corresponds to
1085 -- this entry. It is the same as Unum except when the body
1086 -- and spec are different and we are currently processing
1087 -- the body, in which case it is the spec (Unum + 1).
1090 if U.Utype = Is_Body then
1091 Unum_Spec := Unum + 1;
1096 -- Nothing to do if predefined unit in no run time mode
1098 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1101 -- Likewise if this is an interface to a stand alone library
1103 elsif U.SAL_Interface then
1106 -- Case of no elaboration code
1110 -- In CodePeer mode, we special case subprogram bodies which
1111 -- are handled in the 'else' part below, and lead to a call
1112 -- to <subp>'Elab_Subp_Body.
1114 and then (not CodePeer_Mode
1118 or else U.Utype = Is_Spec
1119 or else U.Utype = Is_Spec_Only
1120 or else U.Unit_Kind /= 's')
1122 -- In the case of a body with a separate spec, where the
1123 -- separate spec has an elaboration entity defined, this is
1124 -- where we increment the elaboration entity if one exists.
1126 -- Likewise for lone specs with an elaboration entity defined
1127 -- despite No_Elaboration_Code, e.g. when requested to preserve
1130 if (U.Utype = Is_Body or else U.Utype = Is_Spec_Only)
1131 and then Units.Table (Unum_Spec).Set_Elab_Entity
1132 and then not CodePeer_Mode
1135 Set_Unit_Number (Unum_Spec);
1136 Set_String (" := E");
1137 Set_Unit_Number (Unum_Spec);
1138 Set_String (" + 1;");
1139 Write_Statement_Buffer;
1142 -- Here if elaboration code is present. If binding a library
1143 -- or if there is a non-Ada main subprogram then we generate:
1145 -- if uname_E = 0 then
1146 -- uname'elab_[spec|body];
1148 -- uname_E := uname_E + 1;
1150 -- Otherwise, elaboration routines are called unconditionally:
1152 -- uname'elab_[spec|body];
1153 -- uname_E := uname_E + 1;
1155 -- The uname_E increment is skipped if this is a separate spec,
1156 -- since it will be done when we process the body.
1158 -- In CodePeer mode, we do not generate any reference to xxx_E
1159 -- variables, only calls to 'Elab* subprograms.
1162 -- Check incompatibilities with No_Multiple_Elaboration
1164 if not CodePeer_Mode
1165 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1167 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1169 if Force_Checking_Of_Elaboration_Flags then
1171 ("-F (force elaboration checks) switch not allowed "
1172 & "with restriction No_Multiple_Elaboration active");
1174 -- Interfacing of libraries not allowed
1176 elsif Interface_Library_Unit then
1178 ("binding of interfaced libraries not allowed "
1179 & "with restriction No_Multiple_Elaboration active");
1181 -- Non-Ada main program not allowed
1183 elsif not Bind_Main_Program then
1185 ("non-Ada main program not allowed "
1186 & "with restriction No_Multiple_Elaboration active");
1190 -- OK, see if we need to test elaboration flag
1193 Units.Table (Unum_Spec).Set_Elab_Entity
1194 and then not CodePeer_Mode
1195 and then (Force_Checking_Of_Elaboration_Flags
1196 or Interface_Library_Unit
1197 or not Bind_Main_Program);
1199 if Check_Elab_Flag then
1200 Set_String (" if E");
1201 Set_Unit_Number (Unum_Spec);
1202 Set_String (" = 0 then");
1203 Write_Statement_Buffer;
1208 Get_Decoded_Name_String_With_Brackets (U.Uname);
1210 if Name_Buffer (Name_Len) = 's' then
1211 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1213 Name_Len := Name_Len + 8;
1215 -- Special case in CodePeer mode for subprogram bodies
1216 -- which correspond to CodePeer 'Elab_Subp_Body special
1219 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1220 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1222 Name_Len := Name_Len + 13;
1225 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1227 Name_Len := Name_Len + 8;
1230 Set_Casing (U.Icasing);
1233 Write_Statement_Buffer;
1235 if Check_Elab_Flag then
1239 if U.Utype /= Is_Spec
1240 and then not CodePeer_Mode
1241 and then Units.Table (Unum_Spec).Set_Elab_Entity
1244 Set_Unit_Number (Unum_Spec);
1245 Set_String (" := E");
1246 Set_Unit_Number (Unum_Spec);
1247 Set_String (" + 1;");
1248 Write_Statement_Buffer;
1255 ------------------------
1256 -- Gen_Elab_Externals --
1257 ------------------------
1259 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array) is
1261 if CodePeer_Mode then
1265 for E in Elab_Order'Range loop
1267 Unum : constant Unit_Id := Elab_Order (E);
1268 U : Unit_Record renames Units.Table (Unum);
1271 -- Check for Elab_Entity to be set for this unit
1273 if U.Set_Elab_Entity
1275 -- Don't generate reference for stand alone library
1277 and then not U.SAL_Interface
1279 -- Don't generate reference for predefined file in No_Run_Time
1280 -- mode, since we don't include the object files in this case
1284 and then Is_Predefined_File_Name (U.Sfile))
1286 Get_Name_String (U.Sfile);
1289 Set_Unit_Number (Unum);
1290 Set_String (" : Short_Integer; pragma Import (Ada, E");
1291 Set_Unit_Number (Unum);
1292 Set_String (", """);
1293 Get_Name_String (U.Uname);
1295 Set_String ("_E"");");
1296 Write_Statement_Buffer;
1302 end Gen_Elab_Externals;
1304 --------------------
1305 -- Gen_Elab_Order --
1306 --------------------
1308 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array) is
1311 WBI (" -- BEGIN ELABORATION ORDER");
1313 for J in Elab_Order'Range loop
1314 Set_String (" -- ");
1315 Get_Name_String (Units.Table (Elab_Order (J)).Uname);
1317 Write_Statement_Buffer;
1320 WBI (" -- END ELABORATION ORDER");
1323 --------------------------
1324 -- Gen_Finalize_Library --
1325 --------------------------
1327 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array) is
1328 procedure Gen_Header;
1329 -- Generate the header of the finalization routine
1335 procedure Gen_Header is
1337 WBI (" procedure finalize_library is");
1345 Uspec : Unit_Record;
1348 -- Start of processing for Gen_Finalize_Library
1351 if CodePeer_Mode then
1355 for E in reverse Elab_Order'Range loop
1356 Unum := Elab_Order (E);
1357 U := Units.Table (Unum);
1359 -- Dealing with package bodies is a little complicated. In such
1360 -- cases we must retrieve the package spec since it contains the
1361 -- spec of the body finalizer.
1363 if U.Utype = Is_Body then
1365 Uspec := Units.Table (Unum);
1370 Get_Name_String (Uspec.Uname);
1372 -- We are only interested in non-generic packages
1374 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1377 -- That aren't an interface to a stand alone library
1379 elsif U.SAL_Interface then
1382 -- Case of no finalization
1384 elsif not U.Has_Finalizer then
1386 -- The only case in which we have to do something is if this
1387 -- is a body, with a separate spec, where the separate spec
1388 -- has a finalizer. In that case, this is where we decrement
1389 -- the elaboration entity.
1391 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1392 if not Lib_Final_Built then
1394 Lib_Final_Built := True;
1398 Set_Unit_Number (Unum);
1399 Set_String (" := E");
1400 Set_Unit_Number (Unum);
1401 Set_String (" - 1;");
1402 Write_Statement_Buffer;
1406 if not Lib_Final_Built then
1408 Lib_Final_Built := True;
1413 -- procedure F<Count>;
1415 Set_String (" declare");
1416 Write_Statement_Buffer;
1418 Set_String (" procedure F");
1421 Write_Statement_Buffer;
1424 -- pragma Import (Ada, F<Count>,
1425 -- "xx__yy__finalize_[body|spec]");
1427 Set_String (" pragma Import (Ada, F");
1429 Set_String (", """);
1431 -- Perform name construction
1434 Set_String ("__finalize_");
1436 -- Package spec processing
1438 if U.Utype = Is_Spec
1439 or else U.Utype = Is_Spec_Only
1441 Set_String ("spec");
1443 -- Package body processing
1446 Set_String ("body");
1449 Set_String (""");");
1450 Write_Statement_Buffer;
1452 -- If binding a library or if there is a non-Ada main subprogram
1453 -- then we generate:
1456 -- uname_E := uname_E - 1;
1457 -- if uname_E = 0 then
1462 -- Otherwise, finalization routines are called unconditionally:
1465 -- uname_E := uname_E - 1;
1469 -- The uname_E decrement is skipped if this is a separate spec,
1470 -- since it will be done when we process the body.
1474 if U.Utype /= Is_Spec then
1476 Set_Unit_Number (Unum);
1477 Set_String (" := E");
1478 Set_Unit_Number (Unum);
1479 Set_String (" - 1;");
1480 Write_Statement_Buffer;
1483 if Interface_Library_Unit or not Bind_Main_Program then
1484 Set_String (" if E");
1485 Set_Unit_Number (Unum);
1486 Set_String (" = 0 then");
1487 Write_Statement_Buffer;
1494 Write_Statement_Buffer;
1496 if Interface_Library_Unit or not Bind_Main_Program then
1506 if Lib_Final_Built then
1508 -- It is possible that the finalization of a library-level object
1509 -- raised an exception. In that case import the actual exception
1510 -- and the routine necessary to raise it.
1513 WBI (" procedure Reraise_Library_Exception_If_Any;");
1515 Set_String (" pragma Import (Ada, ");
1516 Set_String ("Reraise_Library_Exception_If_Any, ");
1517 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1518 Write_Statement_Buffer;
1521 WBI (" Reraise_Library_Exception_If_Any;");
1523 WBI (" end finalize_library;");
1526 end Gen_Finalize_Library;
1532 procedure Gen_Main is
1534 if not No_Main_Subprogram then
1536 -- To call the main program, we declare it using a pragma Import
1537 -- Ada with the right link name.
1539 -- It might seem more obvious to "with" the main program, and call
1540 -- it in the normal Ada manner. We do not do this for three
1543 -- 1. It is more efficient not to recompile the main program
1544 -- 2. We are not entitled to assume the source is accessible
1545 -- 3. We don't know what options to use to compile it
1547 -- It is really reason 3 that is most critical (indeed we used
1548 -- to generate the "with", but several regression tests failed).
1550 if ALIs.Table (ALIs.First).Main_Program = Func then
1551 WBI (" function Ada_Main_Program return Integer;");
1553 WBI (" procedure Ada_Main_Program;");
1556 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1557 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1558 Set_Main_Program_Name;
1559 Set_String (""");");
1561 Write_Statement_Buffer;
1564 -- For CodePeer, declare a wrapper for the user-defined main program
1566 if CodePeer_Mode then
1567 Gen_CodePeer_Wrapper;
1571 if Exit_Status_Supported_On_Target then
1572 Set_String (" function ");
1574 Set_String (" procedure ");
1577 Set_String (Get_Main_Name);
1579 if Command_Line_Args_On_Target then
1580 Write_Statement_Buffer;
1581 WBI (" (argc : Integer;");
1582 WBI (" argv : System.Address;");
1583 WBI (" envp : System.Address)");
1585 if Exit_Status_Supported_On_Target then
1586 WBI (" return Integer");
1592 if Exit_Status_Supported_On_Target then
1593 Set_String (" return Integer is");
1598 Write_Statement_Buffer;
1601 if Opt.Default_Exit_Status /= 0
1602 and then Bind_Main_Program
1603 and then not Configurable_Run_Time_Mode
1605 WBI (" procedure Set_Exit_Status (Status : Integer);");
1606 WBI (" pragma Import (C, Set_Exit_Status, " &
1607 """__gnat_set_exit_status"");");
1611 -- Initialize and Finalize
1613 if not CodePeer_Mode
1614 and then not Cumulative_Restrictions.Set (No_Finalization)
1616 WBI (" procedure Initialize (Addr : System.Address);");
1617 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1619 WBI (" procedure Finalize;");
1620 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1623 -- If we want to analyze the stack, we must import corresponding symbols
1625 if Dynamic_Stack_Measurement then
1627 WBI (" procedure Output_Results;");
1628 WBI (" pragma Import (C, Output_Results, " &
1629 """__gnat_stack_usage_output_results"");");
1633 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1634 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1635 """__gnat_stack_usage_initialize"");");
1638 -- Deal with declarations for main program case
1640 if not No_Main_Subprogram then
1641 if ALIs.Table (ALIs.First).Main_Program = Func then
1642 WBI (" Result : Integer;");
1646 if Bind_Main_Program
1647 and not Suppress_Standard_Library_On_Target
1648 and not CodePeer_Mode
1650 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1655 -- Generate a reference to Ada_Main_Program_Name. This symbol is not
1656 -- referenced elsewhere in the generated program, but is needed by
1657 -- the debugger (that's why it is generated in the first place). The
1658 -- reference stops Ada_Main_Program_Name from being optimized away by
1659 -- smart linkers, such as the AiX linker.
1661 -- Because this variable is unused, we make this variable "aliased"
1662 -- with a pragma Volatile in order to tell the compiler to preserve
1663 -- this variable at any level of optimization.
1665 if Bind_Main_Program and not CodePeer_Mode then
1666 WBI (" Ensure_Reference : aliased System.Address := " &
1667 "Ada_Main_Program_Name'Address;");
1668 WBI (" pragma Volatile (Ensure_Reference);");
1674 -- Acquire command line arguments if present on target
1676 if CodePeer_Mode then
1679 elsif Command_Line_Args_On_Target then
1680 WBI (" gnat_argc := argc;");
1681 WBI (" gnat_argv := argv;");
1682 WBI (" gnat_envp := envp;");
1685 -- If configurable run time and no command line args, then nothing needs
1686 -- to be done since the gnat_argc/argv/envp variables are suppressed in
1689 elsif Configurable_Run_Time_On_Target then
1692 -- Otherwise set dummy values (to be filled in by some other unit?)
1695 WBI (" gnat_argc := 0;");
1696 WBI (" gnat_argv := System.Null_Address;");
1697 WBI (" gnat_envp := System.Null_Address;");
1700 if Opt.Default_Exit_Status /= 0
1701 and then Bind_Main_Program
1702 and then not Configurable_Run_Time_Mode
1704 Set_String (" Set_Exit_Status (");
1705 Set_Int (Opt.Default_Exit_Status);
1707 Write_Statement_Buffer;
1710 if Dynamic_Stack_Measurement then
1711 Set_String (" Initialize_Stack_Analysis (");
1712 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1714 Write_Statement_Buffer;
1717 if not Cumulative_Restrictions.Set (No_Finalization)
1718 and then not CodePeer_Mode
1720 if not No_Main_Subprogram
1721 and then Bind_Main_Program
1722 and then not Suppress_Standard_Library_On_Target
1724 WBI (" Initialize (SEH'Address);");
1726 WBI (" Initialize (System.Null_Address);");
1730 WBI (" " & Ada_Init_Name.all & ";");
1732 if not No_Main_Subprogram then
1733 if CodePeer_Mode then
1734 if ALIs.Table (ALIs.First).Main_Program = Proc then
1735 WBI (" " & CodePeer_Wrapper_Name & ";");
1737 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1740 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1741 WBI (" Ada_Main_Program;");
1744 WBI (" Result := Ada_Main_Program;");
1748 -- Adafinal call is skipped if no finalization
1750 if not Cumulative_Restrictions.Set (No_Finalization) then
1754 -- Prints the result of static stack analysis
1756 if Dynamic_Stack_Measurement then
1757 WBI (" Output_Results;");
1760 -- Finalize is only called if we have a run time
1762 if not Cumulative_Restrictions.Set (No_Finalization)
1763 and then not CodePeer_Mode
1770 if Exit_Status_Supported_On_Target then
1771 if No_Main_Subprogram
1772 or else ALIs.Table (ALIs.First).Main_Program = Proc
1774 WBI (" return (gnat_exit_status);");
1776 WBI (" return (Result);");
1784 ------------------------------
1785 -- Gen_Object_Files_Options --
1786 ------------------------------
1788 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array) is
1790 -- This keeps track of the position in the sorted set of entries in the
1791 -- Linker_Options table of where the first entry from an internal file
1794 Linker_Option_List_Started : Boolean := False;
1795 -- Set to True when "LINKER OPTION LIST" is displayed
1797 procedure Write_Linker_Option;
1798 -- Write binder info linker option
1800 -------------------------
1801 -- Write_Linker_Option --
1802 -------------------------
1804 procedure Write_Linker_Option is
1809 -- Loop through string, breaking at null's
1812 while Start < Name_Len loop
1814 -- Find null ending this section
1817 while Name_Buffer (Stop) /= ASCII.NUL
1818 and then Stop <= Name_Len loop
1822 -- Process section if non-null
1824 if Stop > Start then
1825 if Output_Linker_Option_List then
1826 if not Zero_Formatting then
1827 if not Linker_Option_List_Started then
1828 Linker_Option_List_Started := True;
1830 Write_Str (" LINKER OPTION LIST");
1838 Write_Str (Name_Buffer (Start .. Stop - 1));
1841 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
1846 end Write_Linker_Option;
1848 -- Start of processing for Gen_Object_Files_Options
1851 WBI ("-- BEGIN Object file/option list");
1853 if Object_List_Filename /= null then
1854 Set_List_File (Object_List_Filename.all);
1857 for E in Elab_Order'Range loop
1859 -- If not spec that has an associated body, then generate a comment
1860 -- giving the name of the corresponding object file.
1862 if not Units.Table (Elab_Order (E)).SAL_Interface
1863 and then Units.Table (Elab_Order (E)).Utype /= Is_Spec
1867 (Units.Table (Elab_Order (E)).My_ALI).Ofile_Full_Name);
1869 -- If the presence of an object file is necessary or if it exists,
1872 if not Hostparm.Exclude_Missing_Objects
1874 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1876 WBI (" -- " & Name_Buffer (1 .. Name_Len));
1878 if Output_Object_List then
1879 Write_Str (Name_Buffer (1 .. Name_Len));
1886 if Object_List_Filename /= null then
1890 -- Add a "-Ldir" for each directory in the object path
1892 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1894 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1898 Add_Str_To_Name_Buffer ("-L");
1899 Add_Str_To_Name_Buffer (Dir.all);
1900 Write_Linker_Option;
1904 if not (Opt.No_Run_Time_Mode or Opt.No_Stdlib) then
1907 if Opt.Shared_Libgnat then
1908 Add_Str_To_Name_Buffer ("-shared");
1910 Add_Str_To_Name_Buffer ("-static");
1913 -- Write directly to avoid inclusion in -K output as -static and
1914 -- -shared are not usually specified linker options.
1916 WBI (" -- " & Name_Buffer (1 .. Name_Len));
1919 -- Sort linker options
1921 -- This sort accomplishes two important purposes:
1923 -- a) All application files are sorted to the front, and all GNAT
1924 -- internal files are sorted to the end. This results in a well
1925 -- defined dividing line between the two sets of files, for the
1926 -- purpose of inserting certain standard library references into
1927 -- the linker arguments list.
1929 -- b) Given two different units, we sort the linker options so that
1930 -- those from a unit earlier in the elaboration order comes later
1931 -- in the list. This is a heuristic designed to create a more
1932 -- friendly order of linker options when the operations appear in
1933 -- separate units. The idea is that if unit A must be elaborated
1934 -- before unit B, then it is more likely that B references
1935 -- libraries included by A, than vice versa, so we want libraries
1936 -- included by A to come after libraries included by B.
1938 -- These two criteria are implemented by function Lt_Linker_Option. Note
1939 -- that a special case of b) is that specs are elaborated before bodies,
1940 -- so linker options from specs come after linker options for bodies,
1941 -- and again, the assumption is that libraries used by the body are more
1942 -- likely to reference libraries used by the spec, than vice versa.
1945 (Linker_Options.Last,
1946 Move_Linker_Option'Access,
1947 Lt_Linker_Option'Access);
1949 -- Write user linker options, i.e. the set of linker options that come
1950 -- from all files other than GNAT internal files, Lgnat is left set to
1951 -- point to the first entry from a GNAT internal file, or past the end
1952 -- of the entries if there are no internal files.
1954 Lgnat := Linker_Options.Last + 1;
1956 for J in 1 .. Linker_Options.Last loop
1957 if not Linker_Options.Table (J).Internal_File then
1958 Get_Name_String (Linker_Options.Table (J).Name);
1959 Write_Linker_Option;
1966 -- Now we insert standard linker options that must appear after the
1967 -- entries from user files, and before the entries from GNAT run-time
1968 -- files. The reason for this decision is that libraries referenced
1969 -- by internal routines may reference these standard library entries.
1971 -- Note that we do not insert anything when pragma No_Run_Time has
1972 -- been specified or when the standard libraries are not to be used,
1973 -- otherwise on some platforms, we may get duplicate symbols when
1974 -- linking (not clear if this is still the case, but it is harmless).
1976 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
1980 if Opt.Shared_Libgnat then
1981 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
1983 Add_Str_To_Name_Buffer ("-lgnarl");
1986 Write_Linker_Option;
1991 if Opt.Shared_Libgnat then
1992 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
1994 Add_Str_To_Name_Buffer ("-lgnat");
1997 Write_Linker_Option;
2000 -- Write linker options from all internal files
2002 for J in Lgnat .. Linker_Options.Last loop
2003 Get_Name_String (Linker_Options.Table (J).Name);
2004 Write_Linker_Option;
2007 if Output_Linker_Option_List and then not Zero_Formatting then
2011 WBI ("-- END Object file/option list ");
2012 end Gen_Object_Files_Options;
2014 ---------------------
2015 -- Gen_Output_File --
2016 ---------------------
2018 procedure Gen_Output_File
2020 Elab_Order : Unit_Id_Array)
2023 -- Acquire settings for Interrupt_State pragmas
2025 Set_IS_Pragma_Table;
2027 -- Acquire settings for Priority_Specific_Dispatching pragma
2029 Set_PSD_Pragma_Table;
2031 -- Override time slice value if -T switch is set
2033 if Time_Slice_Set then
2034 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2037 -- Count number of elaboration calls
2039 for E in Elab_Order'Range loop
2040 if Units.Table (Elab_Order (E)).No_Elab then
2043 Num_Elab_Calls := Num_Elab_Calls + 1;
2047 -- Generate output file in appropriate language
2049 Gen_Output_File_Ada (Filename, Elab_Order);
2050 end Gen_Output_File;
2052 -------------------------
2053 -- Gen_Output_File_Ada --
2054 -------------------------
2056 procedure Gen_Output_File_Ada
2057 (Filename : String; Elab_Order : Unit_Id_Array)
2059 Ada_Main : constant String := Get_Ada_Main_Name;
2060 -- Name to be used for generated Ada main program. See the body of
2061 -- function Get_Ada_Main_Name for details on the form of the name.
2063 Needs_Library_Finalization : constant Boolean :=
2064 not Configurable_Run_Time_On_Target
2065 and then Has_Finalizer (Elab_Order);
2066 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2067 -- non-terminating, so we do not want finalization.
2070 -- Name of generated bind file (spec)
2073 -- Name of generated bind file (body)
2076 -- Create spec first
2078 Create_Binder_Output (Filename, 's', Bfiles);
2080 -- We always compile the binder file in Ada 95 mode so that we properly
2081 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2082 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2084 WBI ("pragma Warnings (Off);");
2085 WBI ("pragma Ada_95;");
2087 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2088 -- then we need to make sure that the binder program is compiled with
2089 -- the same restriction, so that no exception tables are generated.
2091 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2092 WBI ("pragma Restrictions (No_Exception_Handlers);");
2095 -- Same processing for Restrictions (No_Exception_Propagation)
2097 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2098 WBI ("pragma Restrictions (No_Exception_Propagation);");
2101 -- Same processing for pragma No_Run_Time
2103 if No_Run_Time_Mode then
2104 WBI ("pragma No_Run_Time;");
2107 -- Generate with of System so we can reference System.Address
2109 WBI ("with System;");
2111 -- Generate with of System.Initialize_Scalars if active
2113 if Initialize_Scalars_Used then
2114 WBI ("with System.Scalar_Values;");
2117 -- Generate with of System.Secondary_Stack if active
2119 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2120 WBI ("with System.Secondary_Stack;");
2123 Resolve_Binder_Options (Elab_Order);
2125 -- Generate standard with's
2127 if not Suppress_Standard_Library_On_Target then
2128 if CodePeer_Mode then
2129 WBI ("with System.Standard_Library;");
2133 WBI ("package " & Ada_Main & " is");
2135 -- Main program case
2137 if Bind_Main_Program then
2138 -- Generate argc/argv stuff unless suppressed
2140 if Command_Line_Args_On_Target
2141 or not Configurable_Run_Time_On_Target
2144 WBI (" gnat_argc : Integer;");
2145 WBI (" gnat_argv : System.Address;");
2146 WBI (" gnat_envp : System.Address;");
2148 -- If the standard library is not suppressed, these variables
2149 -- are in the run-time data area for easy run time access.
2151 if not Suppress_Standard_Library_On_Target then
2153 WBI (" pragma Import (C, gnat_argc);");
2154 WBI (" pragma Import (C, gnat_argv);");
2155 WBI (" pragma Import (C, gnat_envp);");
2159 -- Define exit status. Again in normal mode, this is in the
2160 -- run-time library, and is initialized there, but in the
2161 -- configurable runtime case, the variable is declared and
2162 -- initialized in this file.
2166 if Configurable_Run_Time_Mode then
2167 if Exit_Status_Supported_On_Target then
2168 WBI (" gnat_exit_status : Integer := 0;");
2172 WBI (" gnat_exit_status : Integer;");
2173 WBI (" pragma Import (C, gnat_exit_status);");
2176 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2177 -- the main program. Otherwise, it can lead under some circumstances
2178 -- to a symbol duplication during the link (for instance when a C
2179 -- program uses two Ada libraries). Also zero terminate the string
2180 -- so that its end can be found reliably at run time.
2183 WBI (" GNAT_Version : constant String :=");
2184 WBI (" """ & Ver_Prefix &
2185 Gnat_Version_String &
2187 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2190 Set_String (" Ada_Main_Program_Name : constant String := """);
2191 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2193 Set_Main_Program_Name;
2194 Set_String (""" & ASCII.NUL;");
2196 Write_Statement_Buffer;
2199 (" pragma Export (C, Ada_Main_Program_Name, " &
2200 """__gnat_ada_main_program_name"");");
2204 WBI (" procedure " & Ada_Init_Name.all & ";");
2205 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2206 Ada_Init_Name.all & """);");
2208 -- If -a has been specified use pragma Linker_Constructor for the init
2209 -- procedure and pragma Linker_Destructor for the final procedure.
2211 if Use_Pragma_Linker_Constructor then
2212 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2215 if not Cumulative_Restrictions.Set (No_Finalization) then
2217 WBI (" procedure " & Ada_Final_Name.all & ";");
2218 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2219 Ada_Final_Name.all & """);");
2221 if Use_Pragma_Linker_Constructor then
2222 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2226 if Bind_Main_Program then
2230 if Exit_Status_Supported_On_Target then
2231 Set_String (" function ");
2233 Set_String (" procedure ");
2236 Set_String (Get_Main_Name);
2238 -- Generate argument list if present
2240 if Command_Line_Args_On_Target then
2241 Write_Statement_Buffer;
2242 WBI (" (argc : Integer;");
2243 WBI (" argv : System.Address;");
2245 (" envp : System.Address)");
2247 if Exit_Status_Supported_On_Target then
2248 Write_Statement_Buffer;
2249 WBI (" return Integer;");
2251 Write_Statement_Buffer (";");
2255 if Exit_Status_Supported_On_Target then
2256 Write_Statement_Buffer (" return Integer;");
2258 Write_Statement_Buffer (";");
2262 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2263 Get_Main_Name & """);");
2266 -- Generate version numbers for units, only if needed. Be very safe on
2269 if not Configurable_Run_Time_On_Target
2270 or else System_Version_Control_Used
2271 or else not Bind_Main_Program
2276 Gen_Elab_Order (Elab_Order);
2281 WBI ("end " & Ada_Main & ";");
2282 Close_Binder_Output;
2284 -- Prepare to write body
2286 Create_Binder_Output (Filename, 'b', Bfileb);
2288 -- We always compile the binder file in Ada 95 mode so that we properly
2289 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2290 -- of the Ada 2005/2012 constructs are needed by the binder file.
2292 WBI ("pragma Warnings (Off);");
2293 WBI ("pragma Ada_95;");
2295 -- Output Source_File_Name pragmas which look like
2297 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2298 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2300 -- where sss/bbb are the spec/body file names respectively
2302 Get_Name_String (Bfiles);
2303 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2305 WBI ("pragma Source_File_Name (" &
2307 ", Spec_File_Name => """ &
2308 Name_Buffer (1 .. Name_Len + 3));
2310 Get_Name_String (Bfileb);
2311 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2313 WBI ("pragma Source_File_Name (" &
2315 ", Body_File_Name => """ &
2316 Name_Buffer (1 .. Name_Len + 3));
2318 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2319 -- versions of the compiler which have overflow checks on by default.
2320 -- We do not want overflow checking enabled for the increments of the
2321 -- elaboration variables (since this can cause an unwanted reference to
2322 -- the last chance exception handler for limited run-times).
2324 WBI ("pragma Suppress (Overflow_Check);");
2326 -- Generate with of System.Restrictions to initialize
2327 -- Run_Time_Restrictions.
2329 if System_Restrictions_Used
2330 and not Suppress_Standard_Library_On_Target
2333 WBI ("with System.Restrictions;");
2336 -- Generate with of Ada.Exceptions if needs library finalization
2338 if Needs_Library_Finalization then
2339 WBI ("with Ada.Exceptions;");
2342 -- Generate with of System.Elaboration_Allocators if the restriction
2343 -- No_Standard_Allocators_After_Elaboration was present.
2345 if Cumulative_Restrictions.Set
2346 (No_Standard_Allocators_After_Elaboration)
2348 WBI ("with System.Elaboration_Allocators;");
2351 -- Generate start of package body
2354 WBI ("package body " & Ada_Main & " is");
2357 -- Generate externals for elaboration entities
2359 Gen_Elab_Externals (Elab_Order);
2361 if not CodePeer_Mode then
2362 if not Suppress_Standard_Library_On_Target then
2364 -- Generate Priority_Specific_Dispatching pragma string
2367 (" Local_Priority_Specific_Dispatching : " &
2368 "constant String := """);
2370 for J in 0 .. PSD_Pragma_Settings.Last loop
2371 Set_Char (PSD_Pragma_Settings.Table (J));
2375 Write_Statement_Buffer;
2377 -- Generate Interrupt_State pragma string
2379 Set_String (" Local_Interrupt_States : constant String := """);
2381 for J in 0 .. IS_Pragma_Settings.Last loop
2382 Set_Char (IS_Pragma_Settings.Table (J));
2386 Write_Statement_Buffer;
2390 if not Suppress_Standard_Library_On_Target then
2392 -- The B.1(39) implementation advice says that the adainit
2393 -- and adafinal routines should be idempotent. Generate a flag to
2394 -- ensure that. This is not needed if we are suppressing the
2395 -- standard library since it would never be referenced.
2397 WBI (" Is_Elaborated : Boolean := False;");
2399 -- Generate bind environment string
2401 Gen_Bind_Env_String;
2407 -- Generate the adafinal routine unless there is no finalization to do
2409 if not Cumulative_Restrictions.Set (No_Finalization) then
2410 if Needs_Library_Finalization then
2411 Gen_Finalize_Library (Elab_Order);
2417 Gen_Adainit (Elab_Order);
2419 if Bind_Main_Program then
2423 -- Output object file list and the Ada body is complete
2425 Gen_Object_Files_Options (Elab_Order);
2428 WBI ("end " & Ada_Main & ";");
2430 Close_Binder_Output;
2431 end Gen_Output_File_Ada;
2433 ----------------------
2434 -- Gen_Restrictions --
2435 ----------------------
2437 procedure Gen_Restrictions is
2441 if Suppress_Standard_Library_On_Target
2442 or not System_Restrictions_Used
2447 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2453 for J in Cumulative_Restrictions.Set'Range loop
2454 Set_Boolean (Cumulative_Restrictions.Set (J));
2458 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2459 Write_Statement_Buffer;
2465 Set_String_Replace ("),");
2466 Write_Statement_Buffer;
2467 Set_String (" Value => (");
2469 for J in Cumulative_Restrictions.Value'Range loop
2470 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2474 Set_String_Replace ("),");
2475 Write_Statement_Buffer;
2476 WBI (" Violated =>");
2480 for J in Cumulative_Restrictions.Violated'Range loop
2481 Set_Boolean (Cumulative_Restrictions.Violated (J));
2485 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2486 Write_Statement_Buffer;
2492 Set_String_Replace ("),");
2493 Write_Statement_Buffer;
2494 Set_String (" Count => (");
2496 for J in Cumulative_Restrictions.Count'Range loop
2497 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2501 Set_String_Replace ("),");
2502 Write_Statement_Buffer;
2503 Set_String (" Unknown => (");
2505 for J in Cumulative_Restrictions.Unknown'Range loop
2506 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2510 Set_String_Replace ("))");
2512 Write_Statement_Buffer;
2513 end Gen_Restrictions;
2519 -- This routine generates lines such as:
2521 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2522 -- pragma Export (C, unnnnn, unam);
2524 -- for each unit, where unam is the unit name suffixed by either B or S for
2525 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2526 -- the version number, and nnnnn is a 5-digits serial number.
2528 procedure Gen_Versions is
2529 Ubuf : String (1 .. 6) := "u00000";
2531 procedure Increment_Ubuf;
2532 -- Little procedure to increment the serial number
2534 --------------------
2535 -- Increment_Ubuf --
2536 --------------------
2538 procedure Increment_Ubuf is
2540 for J in reverse Ubuf'Range loop
2541 Ubuf (J) := Character'Succ (Ubuf (J));
2542 exit when Ubuf (J) <= '9';
2547 -- Start of processing for Gen_Versions
2552 WBI (" type Version_32 is mod 2 ** 32;");
2553 for U in Units.First .. Units.Last loop
2554 if not Units.Table (U).SAL_Interface
2555 and then (not Bind_For_Library
2556 or else Units.Table (U).Directly_Scanned)
2559 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2560 Units.Table (U).Version & "#;");
2561 Set_String (" pragma Export (C, ");
2563 Set_String (", """);
2565 Get_Name_String (Units.Table (U).Uname);
2567 for K in 1 .. Name_Len loop
2568 if Name_Buffer (K) = '.' then
2572 elsif Name_Buffer (K) = '%' then
2576 Set_Char (Name_Buffer (K));
2580 if Name_Buffer (Name_Len) = 's' then
2586 Set_String (""");");
2587 Write_Statement_Buffer;
2592 ------------------------
2593 -- Get_Main_Unit_Name --
2594 ------------------------
2596 function Get_Main_Unit_Name (S : String) return String is
2597 Result : String := S;
2600 for J in S'Range loop
2601 if Result (J) = '.' then
2607 end Get_Main_Unit_Name;
2609 -----------------------
2610 -- Get_Ada_Main_Name --
2611 -----------------------
2613 function Get_Ada_Main_Name return String is
2614 Suffix : constant String := "_00";
2615 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2616 Opt.Ada_Main_Name.all & Suffix;
2620 -- For CodePeer, we want reproducible names (independent of other mains
2621 -- that may or may not be present) that don't collide when analyzing
2622 -- multiple mains and which are easily recognizable as "ada_main" names.
2624 if CodePeer_Mode then
2625 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2629 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2632 -- This loop tries the following possibilities in order
2638 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2639 -- it is set to 'ada_main'.
2641 for J in 0 .. 99 loop
2643 Nlen := Name'Length - Suffix'Length;
2645 Nlen := Name'Length;
2646 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2647 Name (Name'Last - 1) :=
2648 Character'Val (J / 10 + Character'Pos ('0'));
2651 for K in ALIs.First .. ALIs.Last loop
2652 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2654 -- Get unit name, removing %b or %e at end
2656 Get_Name_String (Units.Table (L).Uname);
2657 Name_Len := Name_Len - 2;
2659 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2665 return Name (1 .. Nlen);
2671 -- If we fall through, just use a peculiar unlikely name
2673 return ("Qwertyuiop");
2674 end Get_Ada_Main_Name;
2680 function Get_Main_Name return String is
2682 -- Explicit name given with -M switch
2684 if Bind_Alternate_Main_Name then
2685 return Alternate_Main_Name.all;
2687 -- Case of main program name to be used directly
2689 elsif Use_Ada_Main_Program_Name_On_Target then
2691 -- Get main program name
2693 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2695 -- If this is a child name, return only the name of the child, since
2696 -- we can't have dots in a nested program name. Note that we do not
2697 -- include the %b at the end of the unit name.
2699 for J in reverse 1 .. Name_Len - 2 loop
2700 if J = 1 or else Name_Buffer (J - 1) = '.' then
2701 return Name_Buffer (J .. Name_Len - 2);
2705 raise Program_Error; -- impossible exit
2707 -- Case where "main" is to be used as default
2714 ---------------------
2715 -- Get_WC_Encoding --
2716 ---------------------
2718 function Get_WC_Encoding return Character is
2720 -- If encoding method specified by -W switch, then return it
2722 if Wide_Character_Encoding_Method_Specified then
2723 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2725 -- If no main program, and not specified, set brackets, we really have
2726 -- no better choice. If some other encoding is required when there is
2727 -- no main, it must be set explicitly using -Wx.
2729 -- Note: if the ALI file always passed the wide character encoding of
2730 -- every file, then we could use the encoding of the initial specified
2731 -- file, but this information is passed only for potential main
2732 -- programs. We could fix this sometime, but it is a very minor point
2733 -- (wide character default encoding for [Wide_[Wide_]]Text_IO when there
2734 -- is no main program).
2736 elsif No_Main_Subprogram then
2739 -- Otherwise if there is a main program, take encoding from it
2742 return ALIs.Table (ALIs.First).WC_Encoding;
2744 end Get_WC_Encoding;
2750 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean is
2755 for E in reverse Elab_Order'Range loop
2756 Unum := Elab_Order (E);
2757 U := Units.Table (Unum);
2759 -- We are only interested in non-generic packages
2761 if U.Unit_Kind = 'p'
2762 and then U.Has_Finalizer
2763 and then not U.Is_Generic
2764 and then not U.No_Elab
2777 function Hash (Nam : Name_Id) return Header_Num is
2779 return Int (Nam - Names_Low_Bound) rem Header_Num'Last;
2782 ----------------------
2783 -- Lt_Linker_Option --
2784 ----------------------
2786 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean is
2788 -- Sort internal files last
2790 if Linker_Options.Table (Op1).Internal_File
2792 Linker_Options.Table (Op2).Internal_File
2794 -- Note: following test uses False < True
2796 return Linker_Options.Table (Op1).Internal_File
2798 Linker_Options.Table (Op2).Internal_File;
2800 -- If both internal or both non-internal, sort according to the
2801 -- elaboration position. A unit that is elaborated later should come
2802 -- earlier in the linker options list.
2805 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2807 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2809 end Lt_Linker_Option;
2811 ------------------------
2812 -- Move_Linker_Option --
2813 ------------------------
2815 procedure Move_Linker_Option (From : Natural; To : Natural) is
2817 Linker_Options.Table (To) := Linker_Options.Table (From);
2818 end Move_Linker_Option;
2820 ----------------------------
2821 -- Resolve_Binder_Options --
2822 ----------------------------
2824 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array) is
2825 procedure Check_Package (Var : in out Boolean; Name : String);
2826 -- Set Var to true iff the current identifier in Namet is Name. Do
2827 -- nothing if it doesn't match. This procedure is just a helper to
2828 -- avoid explicitly dealing with length.
2834 procedure Check_Package (Var : in out Boolean; Name : String) is
2836 if Name_Len = Name'Length
2837 and then Name_Buffer (1 .. Name_Len) = Name
2843 -- Start of processing for Resolve_Binder_Options
2846 for E in Elab_Order'Range loop
2847 Get_Name_String (Units.Table (Elab_Order (E)).Uname);
2849 -- This is not a perfect approach, but is the current protocol
2850 -- between the run-time and the binder to indicate that tasking is
2851 -- used: System.OS_Interface should always be used by any tasking
2854 Check_Package (With_GNARL, "system.os_interface%s");
2856 -- Ditto for the use of restricted tasking
2859 (System_Tasking_Restricted_Stages_Used,
2860 "system.tasking.restricted.stages%s");
2862 -- Ditto for the use of interrupts
2864 Check_Package (System_Interrupts_Used, "system.interrupts%s");
2866 -- Ditto for the use of dispatching domains
2869 (Dispatching_Domains_Used,
2870 "system.multiprocessors.dispatching_domains%s");
2872 -- Ditto for the use of restrictions
2874 Check_Package (System_Restrictions_Used, "system.restrictions%s");
2876 -- Ditto for use of an SMP bareboard runtime
2878 Check_Package (System_BB_CPU_Primitives_Multiprocessors_Used,
2879 "system.bb.cpu_primitives.multiprocessors%s");
2881 -- Ditto for System.Version_Control, which is used for Version and
2882 -- Body_Version attributes.
2884 Check_Package (System_Version_Control_Used,
2885 "system.version_control%s");
2887 end Resolve_Binder_Options;
2893 procedure Set_Bind_Env (Key, Value : String) is
2895 -- The lengths of Key and Value are stored as single bytes
2897 if Key'Length > 255 then
2898 Osint.Fail ("bind environment key """ & Key & """ too long");
2901 if Value'Length > 255 then
2902 Osint.Fail ("bind environment value """ & Value & """ too long");
2905 Bind_Environment.Set (Name_Find (Key), Name_Find (Value));
2912 procedure Set_Boolean (B : Boolean) is
2913 False_Str : constant String := "False";
2914 True_Str : constant String := "True";
2918 Statement_Buffer (Stm_Last + 1 .. Stm_Last + True_Str'Length) :=
2920 Stm_Last := Stm_Last + True_Str'Length;
2922 Statement_Buffer (Stm_Last + 1 .. Stm_Last + False_Str'Length) :=
2924 Stm_Last := Stm_Last + False_Str'Length;
2932 procedure Set_Char (C : Character) is
2934 Stm_Last := Stm_Last + 1;
2935 Statement_Buffer (Stm_Last) := C;
2942 procedure Set_Int (N : Int) is
2953 Stm_Last := Stm_Last + 1;
2954 Statement_Buffer (Stm_Last) :=
2955 Character'Val (N mod 10 + Character'Pos ('0'));
2959 -------------------------
2960 -- Set_IS_Pragma_Table --
2961 -------------------------
2963 procedure Set_IS_Pragma_Table is
2965 for F in ALIs.First .. ALIs.Last loop
2966 for K in ALIs.Table (F).First_Interrupt_State ..
2967 ALIs.Table (F).Last_Interrupt_State
2970 Inum : constant Int :=
2971 Interrupt_States.Table (K).Interrupt_Id;
2972 Stat : constant Character :=
2973 Interrupt_States.Table (K).Interrupt_State;
2976 while IS_Pragma_Settings.Last < Inum loop
2977 IS_Pragma_Settings.Append ('n');
2980 IS_Pragma_Settings.Table (Inum) := Stat;
2984 end Set_IS_Pragma_Table;
2986 ---------------------------
2987 -- Set_Main_Program_Name --
2988 ---------------------------
2990 procedure Set_Main_Program_Name is
2992 -- Note that name has %b on the end which we ignore
2994 -- First we output the initial _ada_ since we know that the main program
2995 -- is a library level subprogram.
2997 Set_String ("_ada_");
2999 -- Copy name, changing dots to double underscores
3001 for J in 1 .. Name_Len - 2 loop
3002 if Name_Buffer (J) = '.' then
3005 Set_Char (Name_Buffer (J));
3008 end Set_Main_Program_Name;
3010 ---------------------
3011 -- Set_Name_Buffer --
3012 ---------------------
3014 procedure Set_Name_Buffer is
3016 for J in 1 .. Name_Len loop
3017 Set_Char (Name_Buffer (J));
3019 end Set_Name_Buffer;
3021 -------------------------
3022 -- Set_PSD_Pragma_Table --
3023 -------------------------
3025 procedure Set_PSD_Pragma_Table is
3027 for F in ALIs.First .. ALIs.Last loop
3028 for K in ALIs.Table (F).First_Specific_Dispatching ..
3029 ALIs.Table (F).Last_Specific_Dispatching
3032 DTK : Specific_Dispatching_Record
3033 renames Specific_Dispatching.Table (K);
3036 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3037 PSD_Pragma_Settings.Append ('F');
3040 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3041 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3046 end Set_PSD_Pragma_Table;
3052 procedure Set_String (S : String) is
3054 Statement_Buffer (Stm_Last + 1 .. Stm_Last + S'Length) := S;
3055 Stm_Last := Stm_Last + S'Length;
3058 ------------------------
3059 -- Set_String_Replace --
3060 ------------------------
3062 procedure Set_String_Replace (S : String) is
3064 Statement_Buffer (Stm_Last - S'Length + 1 .. Stm_Last) := S;
3065 end Set_String_Replace;
3071 procedure Set_Unit_Name is
3073 for J in 1 .. Name_Len - 2 loop
3074 if Name_Buffer (J) = '.' then
3077 Set_Char (Name_Buffer (J));
3082 ---------------------
3083 -- Set_Unit_Number --
3084 ---------------------
3086 procedure Set_Unit_Number (U : Unit_Id) is
3087 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3088 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3091 if Num_Units >= 10 and then Unum < 10 then
3095 if Num_Units >= 100 and then Unum < 100 then
3100 end Set_Unit_Number;
3102 ---------------------
3103 -- Write_Bind_Line --
3104 ---------------------
3106 procedure Write_Bind_Line (S : String) is
3108 -- Need to strip trailing LF from S
3110 WBI (S (S'First .. S'Last - 1));
3111 end Write_Bind_Line;
3113 ----------------------------
3114 -- Write_Statement_Buffer --
3115 ----------------------------
3117 procedure Write_Statement_Buffer is
3119 WBI (Statement_Buffer (1 .. Stm_Last));
3121 end Write_Statement_Buffer;
3123 procedure Write_Statement_Buffer (S : String) is
3126 Write_Statement_Buffer;
3127 end Write_Statement_Buffer;