1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Binde; use Binde;
28 with Casing; use Casing;
29 with Fname; use Fname;
30 with Gnatvsn; use Gnatvsn;
32 with Namet; use Namet;
34 with Osint; use Osint;
35 with Osint.B; use Osint.B;
36 with Output; use Output;
37 with Rident; use Rident;
38 with Stringt; use Stringt;
39 with Table; use Table;
40 with Targparm; use Targparm;
41 with Types; use Types;
43 with System.OS_Lib; use System.OS_Lib;
44 with System.WCh_Con; use System.WCh_Con;
46 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
49 package body Bindgen is
51 Statement_Buffer : String (1 .. 1000);
52 -- Buffer used for constructing output statements
55 -- Last location in Statement_Buffer currently set
57 With_GNARL : Boolean := False;
58 -- Flag which indicates whether the program uses the GNARL library
59 -- (presence of the unit System.OS_Interface)
61 Num_Elab_Calls : Nat := 0;
62 -- Number of generated calls to elaboration routines
64 System_Restrictions_Used : Boolean := False;
65 -- Flag indicating whether the unit System.Restrictions is in the closure
66 -- of the partition. This is set by Resolve_Binder_Options, and is used
67 -- to determine whether or not to initialize the restrictions information
68 -- in the body of the binder generated file (we do not want to do this
69 -- unconditionally, since it drags in the System.Restrictions unit
70 -- unconditionally, which is unpleasand, especially for ZFP etc.)
72 Dispatching_Domains_Used : Boolean := False;
73 -- Flag indicating whether multiprocessor dispatching domains are used in
74 -- the closure of the partition. This is set by Resolve_Binder_Options, and
75 -- is used to call the routine to disallow the creation of new dispatching
76 -- domains just before calling the main procedure from the environment
79 System_Tasking_Restricted_Stages_Used : Boolean := False;
80 -- Flag indicating whether the unit System.Tasking.Restricted.Stages is in
81 -- the closure of the partition. This is set by Resolve_Binder_Options,
82 -- and it used to call a routine to active all the tasks at the end of
83 -- the elaboration when partition elaboration policy is sequential.
85 System_Interrupts_Used : Boolean := False;
86 -- Flag indicating whether the unit System.Interrups is in the closure of
87 -- the partition. This is set by Resolve_Binder_Options, and it used to
88 -- attach interrupt handlers at the end of the elaboration when partition
89 -- elaboration policy is sequential.
91 Lib_Final_Built : Boolean := False;
92 -- Flag indicating whether the finalize_library rountine has been built
94 Bind_Env_String_Built : Boolean := False;
95 -- Flag indicating whether a bind environment string has been built
97 CodePeer_Wrapper_Name : constant String := "call_main_subprogram";
98 -- For CodePeer, introduce a wrapper subprogram which calls the
99 -- user-defined main subprogram.
101 ----------------------------------
102 -- Interface_State Pragma Table --
103 ----------------------------------
105 -- This table assembles the interface state pragma information from
106 -- all the units in the partition. Note that Bcheck has already checked
107 -- that the information is consistent across units. The entries
108 -- in this table are n/u/r/s for not set/user/runtime/system.
110 package IS_Pragma_Settings is new Table.Table (
111 Table_Component_Type => Character,
112 Table_Index_Type => Int,
113 Table_Low_Bound => 0,
114 Table_Initial => 100,
115 Table_Increment => 200,
116 Table_Name => "IS_Pragma_Settings");
118 -- This table assembles the Priority_Specific_Dispatching pragma
119 -- information from all the units in the partition. Note that Bcheck has
120 -- already checked that the information is consistent across units.
121 -- The entries in this table are the upper case first character of the
122 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
124 package PSD_Pragma_Settings is new Table.Table (
125 Table_Component_Type => Character,
126 Table_Index_Type => Int,
127 Table_Low_Bound => 0,
128 Table_Initial => 100,
129 Table_Increment => 200,
130 Table_Name => "PSD_Pragma_Settings");
132 ----------------------------
133 -- Bind_Environment Table --
134 ----------------------------
136 subtype Header_Num is Int range 0 .. 36;
138 function Hash (Nam : Name_Id) return Header_Num;
140 package Bind_Environment is new GNAT.HTable.Simple_HTable
141 (Header_Num => Header_Num,
143 No_Element => No_Name,
148 ----------------------
149 -- Run-Time Globals --
150 ----------------------
152 -- This section documents the global variables that are set from the
153 -- generated binder file.
155 -- Main_Priority : Integer;
156 -- Time_Slice_Value : Integer;
157 -- Heap_Size : Natural;
158 -- WC_Encoding : Character;
159 -- Locking_Policy : Character;
160 -- Queuing_Policy : Character;
161 -- Task_Dispatching_Policy : Character;
162 -- Priority_Specific_Dispatching : System.Address;
163 -- Num_Specific_Dispatching : Integer;
164 -- Restrictions : System.Address;
165 -- Interrupt_States : System.Address;
166 -- Num_Interrupt_States : Integer;
167 -- Unreserve_All_Interrupts : Integer;
168 -- Exception_Tracebacks : Integer;
169 -- Detect_Blocking : Integer;
170 -- Default_Stack_Size : Integer;
171 -- Leap_Seconds_Support : Integer;
172 -- Main_CPU : Integer;
174 -- Main_Priority is the priority value set by pragma Priority in the main
175 -- program. If no such pragma is present, the value is -1.
177 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
178 -- main program, or by the use of a -Tnnn parameter for the binder (if both
179 -- are present, the binder value overrides). The value is in milliseconds.
180 -- A value of zero indicates that time slicing should be suppressed. If no
181 -- pragma is present, and no -T switch was used, the value is -1.
183 -- WC_Encoding shows the wide character encoding method used for the main
184 -- program. This is one of the encoding letters defined in
185 -- System.WCh_Con.WC_Encoding_Letters.
187 -- Locking_Policy is a space if no locking policy was specified for the
188 -- partition. If a locking policy was specified, the value is the upper
189 -- case first character of the locking policy name, for example, 'C' for
192 -- Queuing_Policy is a space if no queuing policy was specified for the
193 -- partition. If a queuing policy was specified, the value is the upper
194 -- case first character of the queuing policy name for example, 'F' for
197 -- Task_Dispatching_Policy is a space if no task dispatching policy was
198 -- specified for the partition. If a task dispatching policy was specified,
199 -- the value is the upper case first character of the policy name, e.g. 'F'
200 -- for FIFO_Within_Priorities.
202 -- Priority_Specific_Dispatching is the address of a string used to store
203 -- the task dispatching policy specified for the different priorities in
204 -- the partition. The length of this string is determined by the last
205 -- priority for which such a pragma applies (the string will be a null
206 -- string if no specific dispatching policies were used). If pragma were
207 -- present, the entries apply to the priorities in sequence from the first
208 -- priority. The value stored is the upper case first character of the
209 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
210 -- for those priority ranges not specified.
212 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
213 -- string. It will be set to zero if no Priority_Specific_Dispatching
214 -- pragmas are present.
216 -- Restrictions is the address of a null-terminated string specifying the
217 -- restrictions information for the partition. The format is identical to
218 -- that of the parameter string found on R lines in ali files (see Lib.Writ
219 -- spec in lib-writ.ads for full details). The difference is that in this
220 -- context the values are the cumulative ones for the entire partition.
222 -- Interrupt_States is the address of a string used to specify the
223 -- cumulative results of Interrupt_State pragmas used in the partition.
224 -- The length of this string is determined by the last interrupt for which
225 -- such a pragma is given (the string will be a null string if no pragmas
226 -- were used). If pragma were present the entries apply to the interrupts
227 -- in sequence from the first interrupt, and are set to one of four
228 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
229 -- time, 's' for system, see description of Interrupt_State pragma for
232 -- Num_Interrupt_States is the length of the Interrupt_States string. It
233 -- will be set to zero if no Interrupt_State pragmas are present.
235 -- Unreserve_All_Interrupts is set to one if at least one unit in the
236 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
238 -- Exception_Tracebacks is set to one if the -E parameter was present
239 -- in the bind and to zero otherwise. Note that on some targets exception
240 -- tracebacks are provided by default, so a value of zero for this
241 -- parameter does not necessarily mean no trace backs are available.
243 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
244 -- not. A value of zero indicates that the pragma is not present, while a
245 -- value of 1 signals its presence in the partition.
247 -- Default_Stack_Size is the default stack size used when creating an Ada
248 -- task with no explicit Storage_Size clause.
250 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
251 -- disabled. A value of zero indicates that leap seconds are turned "off",
252 -- while a value of one signifies "on" status.
254 -- Main_CPU is the processor set by pragma CPU in the main program. If no
255 -- such pragma is present, the value is -1.
257 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
258 -- Convenient shorthand used throughout
260 -----------------------
261 -- Local Subprograms --
262 -----------------------
264 procedure Gen_Adainit;
265 -- Generates the Adainit procedure
267 procedure Gen_Adafinal;
268 -- Generate the Adafinal procedure
270 procedure Gen_Bind_Env_String;
271 -- Generate the bind environment buffer
273 procedure Gen_CodePeer_Wrapper;
274 -- For CodePeer, generate wrapper which calls user-defined main subprogram
276 procedure Gen_Elab_Calls;
277 -- Generate sequence of elaboration calls
279 procedure Gen_Elab_Externals;
280 -- Generate sequence of external declarations for elaboration
282 procedure Gen_Elab_Order;
283 -- Generate comments showing elaboration order chosen
285 procedure Gen_Finalize_Library;
286 -- Generate a sequence of finalization calls to elaborated packages
289 -- Generate procedure main
291 procedure Gen_Object_Files_Options;
292 -- Output comments containing a list of the full names of the object
293 -- files to be linked and the list of linker options supplied by
294 -- Linker_Options pragmas in the source.
296 procedure Gen_Output_File_Ada (Filename : String);
297 -- Generate Ada output file
299 procedure Gen_Restrictions;
300 -- Generate initialization of restrictions variable
302 procedure Gen_Versions;
303 -- Output series of definitions for unit versions
305 function Get_Ada_Main_Name return String;
306 -- This function is used for the Ada main output to compute a usable name
307 -- for the generated main program. The normal main program name is
308 -- Ada_Main, but this won't work if the user has a unit with this name.
309 -- This function tries Ada_Main first, and if there is such a clash, then
310 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
312 function Get_Main_Unit_Name (S : String) return String;
313 -- Return the main unit name corresponding to S by replacing '.' with '_'
315 function Get_Main_Name return String;
316 -- This function is used in the main output case to compute the correct
317 -- external main program. It is "main" by default, unless the flag
318 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
319 -- of the Ada main name without the "_ada". This default can be overridden
320 -- explicitly using the -Mname binder switch.
322 function Get_WC_Encoding return Character;
323 -- Return wide character encoding method to set as WC_Encoding in output.
324 -- If -W has been used, returns the specified encoding, otherwise returns
325 -- the encoding method used for the main program source. If there is no
326 -- main program source (-z switch used), returns brackets ('b').
328 function Has_Finalizer return Boolean;
329 -- Determine whether the current unit has at least one library-level
332 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
333 -- Compare linker options, when sorting, first according to
334 -- Is_Internal_File (internal files come later) and then by
335 -- elaboration order position (latest to earliest).
337 procedure Move_Linker_Option (From : Natural; To : Natural);
338 -- Move routine for sorting linker options
340 procedure Resolve_Binder_Options;
341 -- Set the value of With_GNARL
343 procedure Set_Char (C : Character);
344 -- Set given character in Statement_Buffer at the Last + 1 position
345 -- and increment Last by one to reflect the stored character.
347 procedure Set_Int (N : Int);
348 -- Set given value in decimal in Statement_Buffer with no spaces starting
349 -- at the Last + 1 position, and updating Last past the value. A minus sign
350 -- is output for a negative value.
352 procedure Set_Boolean (B : Boolean);
353 -- Set given boolean value in Statement_Buffer at the Last + 1 position
354 -- and update Last past the value.
356 procedure Set_IS_Pragma_Table;
357 -- Initializes contents of IS_Pragma_Settings table from ALI table
359 procedure Set_Main_Program_Name;
360 -- Given the main program name in Name_Buffer (length in Name_Len) generate
361 -- the name of the routine to be used in the call. The name is generated
362 -- starting at Last + 1, and Last is updated past it.
364 procedure Set_Name_Buffer;
365 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
367 procedure Set_PSD_Pragma_Table;
368 -- Initializes contents of PSD_Pragma_Settings table from ALI table
370 procedure Set_String (S : String);
371 -- Sets characters of given string in Statement_Buffer, starting at the
372 -- Last + 1 position, and updating last past the string value.
374 procedure Set_String_Replace (S : String);
375 -- Replaces the last S'Length characters in the Statement_Buffer with the
376 -- characters of S. The caller must ensure that these characters do in fact
377 -- exist in the Statement_Buffer.
379 procedure Set_Unit_Name;
380 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
381 -- starting at the Last + 1 position and update Last past the value.
382 -- Each dot (.) will be qualified into double underscores (__).
384 procedure Set_Unit_Number (U : Unit_Id);
385 -- Sets unit number (first unit is 1, leading zeroes output to line up all
386 -- output unit numbers nicely as required by the value, and by the total
389 procedure Write_Statement_Buffer;
390 -- Write out contents of statement buffer up to Last, and reset Last to 0
392 procedure Write_Statement_Buffer (S : String);
393 -- First writes its argument (using Set_String (S)), then writes out the
394 -- contents of statement buffer up to Last, and reset Last to 0
396 procedure Write_Bind_Line (S : String);
397 -- Write S (an LF-terminated string) to the binder file (for use with
398 -- Set_Special_Output).
404 procedure Gen_Adafinal is
406 WBI (" procedure " & Ada_Final_Name.all & " is");
408 if Bind_Main_Program and not CodePeer_Mode then
409 WBI (" procedure s_stalib_adafinal;");
410 Set_String (" pragma Import (C, s_stalib_adafinal, ");
411 Set_String ("""system__standard_library__adafinal"");");
412 Write_Statement_Buffer;
416 WBI (" procedure Runtime_Finalize;");
417 WBI (" pragma Import (C, Runtime_Finalize, " &
418 """__gnat_runtime_finalize"");");
422 if not CodePeer_Mode then
423 WBI (" if not Is_Elaborated then");
426 WBI (" Is_Elaborated := False;");
429 WBI (" Runtime_Finalize;");
431 -- By default (real targets), finalization is done differently depending
432 -- on whether this is the main program or a library.
434 if not CodePeer_Mode then
435 if Bind_Main_Program then
436 WBI (" s_stalib_adafinal;");
437 elsif Lib_Final_Built then
438 WBI (" finalize_library;");
443 -- Pragma Import C cannot be used on virtual targets, therefore call the
444 -- runtime finalization routine directly in CodePeer mode, where
445 -- imported functions are ignored.
448 WBI (" System.Standard_Library.Adafinal;");
451 WBI (" end " & Ada_Final_Name.all & ";");
459 procedure Gen_Adainit is
460 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
461 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
464 -- Declare the access-to-subprogram type used for initialization of
465 -- of __gnat_finalize_library_objects. This is declared at library
466 -- level for compatibility with the type used in System.Soft_Links.
467 -- The import of the soft link which performs library-level object
468 -- finalization does not work for CodePeer, so regular Ada is used in
469 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
470 -- tasks are non-terminating, so we do not want finalization.
472 if not Suppress_Standard_Library_On_Target
473 and then not CodePeer_Mode
474 and then not Configurable_Run_Time_On_Target
476 WBI (" type No_Param_Proc is access procedure;");
480 WBI (" procedure " & Ada_Init_Name.all & " is");
482 -- In CodePeer mode, simplify adainit procedure by only calling
483 -- elaboration procedures.
485 if CodePeer_Mode then
488 -- When compiling for the AAMP small library, where the standard library
489 -- is no longer suppressed, we still want to exclude the setting of the
490 -- various imported globals, which aren't present for that library.
492 elsif AAMP_On_Target and then Configurable_Run_Time_On_Target then
496 -- If the standard library is suppressed, then the only global variables
497 -- that might be needed (by the Ravenscar profile) are the priority and
498 -- the processor for the environment task.
500 elsif Suppress_Standard_Library_On_Target then
501 if Main_Priority /= No_Main_Priority then
502 WBI (" Main_Priority : Integer;");
503 WBI (" pragma Import (C, Main_Priority," &
504 " ""__gl_main_priority"");");
508 if Main_CPU /= No_Main_CPU then
509 WBI (" Main_CPU : Integer;");
510 WBI (" pragma Import (C, Main_CPU," &
511 " ""__gl_main_cpu"");");
515 if System_Interrupts_Used
516 and then Partition_Elaboration_Policy_Specified = 'S'
518 WBI (" procedure Install_Restricted_Handlers_Sequential;");
519 WBI (" pragma Import (C," &
520 "Install_Restricted_Handlers_Sequential," &
521 " ""__gnat_attach_all_handlers"");");
525 if System_Tasking_Restricted_Stages_Used
526 and then Partition_Elaboration_Policy_Specified = 'S'
528 WBI (" Partition_Elaboration_Policy : Character;");
529 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
530 " ""__gnat_partition_elaboration_policy"");");
532 WBI (" procedure Activate_All_Tasks_Sequential;");
533 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
534 " ""__gnat_activate_all_tasks"");");
539 if Main_Priority /= No_Main_Priority then
540 Set_String (" Main_Priority := ");
541 Set_Int (Main_Priority);
543 Write_Statement_Buffer;
546 if Main_CPU /= No_Main_CPU then
547 Set_String (" Main_CPU := ");
550 Write_Statement_Buffer;
553 if System_Tasking_Restricted_Stages_Used
554 and then Partition_Elaboration_Policy_Specified = 'S'
556 Set_String (" Partition_Elaboration_Policy := '");
557 Set_Char (Partition_Elaboration_Policy_Specified);
559 Write_Statement_Buffer;
562 if Main_Priority = No_Main_Priority
563 and then Main_CPU = No_Main_CPU
564 and then not System_Tasking_Restricted_Stages_Used
569 -- Normal case (standard library not suppressed). Set all global values
570 -- used by the run time.
573 WBI (" Main_Priority : Integer;");
574 WBI (" pragma Import (C, Main_Priority, " &
575 """__gl_main_priority"");");
576 WBI (" Time_Slice_Value : Integer;");
577 WBI (" pragma Import (C, Time_Slice_Value, " &
578 """__gl_time_slice_val"");");
579 WBI (" WC_Encoding : Character;");
580 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
581 WBI (" Locking_Policy : Character;");
582 WBI (" pragma Import (C, Locking_Policy, " &
583 """__gl_locking_policy"");");
584 WBI (" Queuing_Policy : Character;");
585 WBI (" pragma Import (C, Queuing_Policy, " &
586 """__gl_queuing_policy"");");
587 WBI (" Task_Dispatching_Policy : Character;");
588 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
589 """__gl_task_dispatching_policy"");");
590 WBI (" Priority_Specific_Dispatching : System.Address;");
591 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
592 """__gl_priority_specific_dispatching"");");
593 WBI (" Num_Specific_Dispatching : Integer;");
594 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
595 """__gl_num_specific_dispatching"");");
596 WBI (" Main_CPU : Integer;");
597 WBI (" pragma Import (C, Main_CPU, " &
598 """__gl_main_cpu"");");
600 WBI (" Interrupt_States : System.Address;");
601 WBI (" pragma Import (C, Interrupt_States, " &
602 """__gl_interrupt_states"");");
603 WBI (" Num_Interrupt_States : Integer;");
604 WBI (" pragma Import (C, Num_Interrupt_States, " &
605 """__gl_num_interrupt_states"");");
606 WBI (" Unreserve_All_Interrupts : Integer;");
607 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
608 """__gl_unreserve_all_interrupts"");");
610 if Exception_Tracebacks then
611 WBI (" Exception_Tracebacks : Integer;");
612 WBI (" pragma Import (C, Exception_Tracebacks, " &
613 """__gl_exception_tracebacks"");");
616 WBI (" Detect_Blocking : Integer;");
617 WBI (" pragma Import (C, Detect_Blocking, " &
618 """__gl_detect_blocking"");");
619 WBI (" Default_Stack_Size : Integer;");
620 WBI (" pragma Import (C, Default_Stack_Size, " &
621 """__gl_default_stack_size"");");
622 WBI (" Leap_Seconds_Support : Integer;");
623 WBI (" pragma Import (C, Leap_Seconds_Support, " &
624 """__gl_leap_seconds_support"");");
625 WBI (" Bind_Env_Addr : System.Address;");
626 WBI (" pragma Import (C, Bind_Env_Addr, " &
627 """__gl_bind_env_addr"");");
629 -- Import entry point for elaboration time signal handler
630 -- installation, and indication of if it's been called previously.
633 WBI (" procedure Runtime_Initialize " &
634 "(Install_Handler : Integer);");
635 WBI (" pragma Import (C, Runtime_Initialize, " &
636 """__gnat_runtime_initialize"");");
638 -- Import handlers attach procedure for sequential elaboration policy
640 if System_Interrupts_Used
641 and then Partition_Elaboration_Policy_Specified = 'S'
643 WBI (" procedure Install_Restricted_Handlers_Sequential;");
644 WBI (" pragma Import (C," &
645 "Install_Restricted_Handlers_Sequential," &
646 " ""__gnat_attach_all_handlers"");");
650 -- Import task activation procedure for sequential elaboration
653 if System_Tasking_Restricted_Stages_Used
654 and then Partition_Elaboration_Policy_Specified = 'S'
656 WBI (" Partition_Elaboration_Policy : Character;");
657 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
658 " ""__gnat_partition_elaboration_policy"");");
660 WBI (" procedure Activate_All_Tasks_Sequential;");
661 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
662 " ""__gnat_activate_all_tasks"");");
665 -- For restricted run-time libraries (ZFP and Ravenscar)
666 -- tasks are non-terminating, so we do not want finalization.
668 if not Configurable_Run_Time_On_Target then
670 WBI (" Finalize_Library_Objects : No_Param_Proc;");
671 WBI (" pragma Import (C, Finalize_Library_Objects, " &
672 """__gnat_finalize_library_objects"");");
675 -- Initialize stack limit variable of the environment task if the
676 -- stack check method is stack limit and stack check is enabled.
678 if Stack_Check_Limits_On_Target
679 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
682 WBI (" procedure Initialize_Stack_Limit;");
683 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
684 """__gnat_initialize_stack_limit"");");
687 -- When dispatching domains are used then we need to signal it
688 -- before calling the main procedure.
690 if Dispatching_Domains_Used then
691 WBI (" procedure Freeze_Dispatching_Domains;");
692 WBI (" pragma Import");
693 WBI (" (Ada, Freeze_Dispatching_Domains, "
694 & """__gnat_freeze_dispatching_domains"");");
697 -- Start of processing for Adainit
700 WBI (" if Is_Elaborated then");
703 WBI (" Is_Elaborated := True;");
705 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
706 -- restriction No_Standard_Allocators_After_Elaboration is active.
708 if Cumulative_Restrictions.Set
709 (No_Standard_Allocators_After_Elaboration)
711 WBI (" System.Elaboration_Allocators."
712 & "Mark_Start_Of_Elaboration;");
715 -- Generate assignments to initialize globals
717 Set_String (" Main_Priority := ");
718 Set_Int (Main_Priority);
720 Write_Statement_Buffer;
722 Set_String (" Time_Slice_Value := ");
724 if Task_Dispatching_Policy_Specified = 'F'
725 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
729 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
733 Write_Statement_Buffer;
735 Set_String (" WC_Encoding := '");
736 Set_Char (Get_WC_Encoding);
739 Write_Statement_Buffer;
741 Set_String (" Locking_Policy := '");
742 Set_Char (Locking_Policy_Specified);
744 Write_Statement_Buffer;
746 Set_String (" Queuing_Policy := '");
747 Set_Char (Queuing_Policy_Specified);
749 Write_Statement_Buffer;
751 Set_String (" Task_Dispatching_Policy := '");
752 Set_Char (Task_Dispatching_Policy_Specified);
754 Write_Statement_Buffer;
756 if System_Tasking_Restricted_Stages_Used
757 and then Partition_Elaboration_Policy_Specified = 'S'
759 Set_String (" Partition_Elaboration_Policy := '");
760 Set_Char (Partition_Elaboration_Policy_Specified);
762 Write_Statement_Buffer;
767 WBI (" Priority_Specific_Dispatching :=");
768 WBI (" Local_Priority_Specific_Dispatching'Address;");
770 Set_String (" Num_Specific_Dispatching := ");
771 Set_Int (PSD_Pragma_Settings.Last + 1);
773 Write_Statement_Buffer;
775 Set_String (" Main_CPU := ");
778 Write_Statement_Buffer;
780 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
782 Set_String (" Num_Interrupt_States := ");
783 Set_Int (IS_Pragma_Settings.Last + 1);
785 Write_Statement_Buffer;
787 Set_String (" Unreserve_All_Interrupts := ");
789 if Unreserve_All_Interrupts_Specified then
796 Write_Statement_Buffer;
798 if Exception_Tracebacks then
799 WBI (" Exception_Tracebacks := 1;");
802 Set_String (" Detect_Blocking := ");
804 if Detect_Blocking then
811 Write_Statement_Buffer;
813 Set_String (" Default_Stack_Size := ");
814 Set_Int (Default_Stack_Size);
816 Write_Statement_Buffer;
818 Set_String (" Leap_Seconds_Support := ");
820 if Leap_Seconds_Support then
827 Write_Statement_Buffer;
829 if Bind_Env_String_Built then
830 WBI (" Bind_Env_Addr := Bind_Env'Address;");
833 -- Generate call to Install_Handler
836 WBI (" Runtime_Initialize (1);");
839 -- Generate call to set Initialize_Scalar values if active
841 if Initialize_Scalars_Used then
843 Set_String (" System.Scalar_Values.Initialize ('");
844 Set_Char (Initialize_Scalars_Mode1);
846 Set_Char (Initialize_Scalars_Mode2);
848 Write_Statement_Buffer;
851 -- Generate assignment of default secondary stack size if set
853 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
855 Set_String (" System.Secondary_Stack.");
856 Set_String ("Default_Secondary_Stack_Size := ");
857 Set_Int (Opt.Default_Sec_Stack_Size);
859 Write_Statement_Buffer;
862 -- Initialize stack limit variable of the environment task if the
863 -- stack check method is stack limit and stack check is enabled.
865 if Stack_Check_Limits_On_Target
866 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
869 WBI (" Initialize_Stack_Limit;");
872 -- On CodePeer, the finalization of library objects is not relevant
874 if CodePeer_Mode then
877 -- If this is the main program case, attach finalize_library to the soft
878 -- link. Do it only when not using a restricted run time, in which case
879 -- tasks are non-terminating, so we do not want library-level
882 elsif Bind_Main_Program
883 and then not Configurable_Run_Time_On_Target
884 and then not Suppress_Standard_Library_On_Target
888 if Lib_Final_Built then
889 Set_String (" Finalize_Library_Objects := ");
890 Set_String ("finalize_library'access;");
892 Set_String (" Finalize_Library_Objects := null;");
895 Write_Statement_Buffer;
898 -- Generate elaboration calls
900 if not CodePeer_Mode then
906 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
907 -- restriction No_Standard_Allocators_After_Elaboration is active.
909 if Cumulative_Restrictions.Set
910 (No_Standard_Allocators_After_Elaboration)
912 WBI (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
915 -- From this point, no new dispatching domain can be created
917 if Dispatching_Domains_Used then
918 WBI (" Freeze_Dispatching_Domains;");
921 -- Sequential partition elaboration policy
923 if Partition_Elaboration_Policy_Specified = 'S' then
924 if System_Interrupts_Used then
925 WBI (" Install_Restricted_Handlers_Sequential;");
928 if System_Tasking_Restricted_Stages_Used then
929 WBI (" Activate_All_Tasks_Sequential;");
933 WBI (" end " & Ada_Init_Name.all & ";");
937 -------------------------
938 -- Gen_Bind_Env_String --
939 -------------------------
941 procedure Gen_Bind_Env_String is
942 KN, VN : Name_Id := No_Name;
945 procedure Write_Name_With_Len (Nam : Name_Id);
946 -- Write Nam as a string literal, prefixed with one
947 -- character encoding Nam's length.
949 -------------------------
950 -- Write_Name_With_Len --
951 -------------------------
953 procedure Write_Name_With_Len (Nam : Name_Id) is
955 Get_Name_String (Nam);
958 Store_String_Char (Character'Val (Name_Len));
959 Store_String_Chars (Name_Buffer (1 .. Name_Len));
961 Write_String_Table_Entry (End_String);
962 end Write_Name_With_Len;
964 -- Start of processing for Gen_Bind_Env_String
967 Bind_Environment.Get_First (KN, VN);
972 Set_Special_Output (Write_Bind_Line'Access);
974 WBI (" Bind_Env : aliased constant String :=");
976 while VN /= No_Name loop
977 Write_Str (" " & Amp & ' ');
978 Write_Name_With_Len (KN);
980 Write_Name_With_Len (VN);
983 Bind_Environment.Get_Next (KN, VN);
986 WBI (" & ASCII.NUL;");
988 Set_Special_Output (null);
990 Bind_Env_String_Built := True;
991 end Gen_Bind_Env_String;
993 --------------------------
994 -- Gen_CodePeer_Wrapper --
995 --------------------------
997 procedure Gen_CodePeer_Wrapper is
998 Callee_Name : constant String := "Ada_Main_Program";
1001 if ALIs.Table (ALIs.First).Main_Program = Proc then
1002 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
1004 WBI (" " & Callee_Name & ";");
1007 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1009 WBI (" return " & Callee_Name & ";");
1012 WBI (" end " & CodePeer_Wrapper_Name & ";");
1014 end Gen_CodePeer_Wrapper;
1016 --------------------
1017 -- Gen_Elab_Calls --
1018 --------------------
1020 procedure Gen_Elab_Calls is
1021 Check_Elab_Flag : Boolean;
1024 -- Loop through elaboration order entries
1026 for E in Elab_Order.First .. Elab_Order.Last loop
1028 Unum : constant Unit_Id := Elab_Order.Table (E);
1029 U : Unit_Record renames Units.Table (Unum);
1031 Unum_Spec : Unit_Id;
1032 -- This is the unit number of the spec that corresponds to
1033 -- this entry. It is the same as Unum except when the body
1034 -- and spec are different and we are currently processing
1035 -- the body, in which case it is the spec (Unum + 1).
1038 if U.Utype = Is_Body then
1039 Unum_Spec := Unum + 1;
1044 -- Nothing to do if predefined unit in no run time mode
1046 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1049 -- Likewise if this is an interface to a stand alone library
1051 elsif U.SAL_Interface then
1054 -- Case of no elaboration code
1058 -- In CodePeer mode, we special case subprogram bodies which
1059 -- are handled in the 'else' part below, and lead to a call
1060 -- to <subp>'Elab_Subp_Body.
1062 and then (not CodePeer_Mode
1066 or else U.Utype = Is_Spec
1067 or else U.Utype = Is_Spec_Only
1068 or else U.Unit_Kind /= 's')
1070 -- In the case of a body with a separate spec, where the
1071 -- separate spec has an elaboration entity defined, this is
1072 -- where we increment the elaboration entity if one exists
1074 if U.Utype = Is_Body
1075 and then Units.Table (Unum_Spec).Set_Elab_Entity
1076 and then not CodePeer_Mode
1079 Set_Unit_Number (Unum_Spec);
1081 -- The AAMP target has no notion of shared libraries, and
1082 -- there's no possibility of reelaboration, so we treat the
1083 -- the elaboration var as a flag instead of a counter and
1086 if AAMP_On_Target then
1087 Set_String (" := 1;");
1089 -- Otherwise (normal case), increment elaboration counter
1092 Set_String (" := E");
1093 Set_Unit_Number (Unum_Spec);
1094 Set_String (" + 1;");
1097 Write_Statement_Buffer;
1099 -- In the special case where the target is AAMP and the unit is
1100 -- a spec with a body, the elaboration entity is initialized
1101 -- here. This is done because it's the only way to accomplish
1102 -- initialization of such entities, as there is no mechanism
1103 -- for load time global variable initialization on AAMP.
1105 elsif AAMP_On_Target
1106 and then U.Utype = Is_Spec
1107 and then Units.Table (Unum_Spec).Set_Elab_Entity
1110 Set_Unit_Number (Unum_Spec);
1111 Set_String (" := 0;");
1112 Write_Statement_Buffer;
1115 -- Here if elaboration code is present. If binding a library
1116 -- or if there is a non-Ada main subprogram then we generate:
1118 -- if uname_E = 0 then
1119 -- uname'elab_[spec|body];
1121 -- uname_E := uname_E + 1;
1123 -- Otherwise, elaboration routines are called unconditionally:
1125 -- uname'elab_[spec|body];
1126 -- uname_E := uname_E + 1;
1128 -- The uname_E increment is skipped if this is a separate spec,
1129 -- since it will be done when we process the body.
1131 -- In CodePeer mode, we do not generate any reference to xxx_E
1132 -- variables, only calls to 'Elab* subprograms.
1135 -- In the special case where the target is AAMP and the unit is
1136 -- a spec with a body, the elaboration entity is initialized
1137 -- here. This is done because it's the only way to accomplish
1138 -- initialization of such entities, as there is no mechanism
1139 -- for load time global variable initialization on AAMP.
1142 and then U.Utype = Is_Spec
1143 and then Units.Table (Unum_Spec).Set_Elab_Entity
1146 Set_Unit_Number (Unum_Spec);
1147 Set_String (" := 0;");
1148 Write_Statement_Buffer;
1151 -- Check incompatibilities with No_Multiple_Elaboration
1153 if not CodePeer_Mode
1154 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1156 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1158 if Force_Checking_Of_Elaboration_Flags then
1160 ("-F (force elaboration checks) switch not allowed "
1161 & "with restriction No_Multiple_Elaboration active");
1163 -- Interfacing of libraries not allowed
1165 elsif Interface_Library_Unit then
1167 ("binding of interfaced libraries not allowed "
1168 & "with restriction No_Multiple_Elaboration active");
1170 -- Non-Ada main program not allowed
1172 elsif not Bind_Main_Program then
1174 ("non-Ada main program not allowed "
1175 & "with restriction No_Multiple_Elaboration active");
1179 -- OK, see if we need to test elaboration flag
1182 Units.Table (Unum_Spec).Set_Elab_Entity
1183 and then not CodePeer_Mode
1184 and then (Force_Checking_Of_Elaboration_Flags
1185 or Interface_Library_Unit
1186 or not Bind_Main_Program);
1188 if Check_Elab_Flag then
1189 Set_String (" if E");
1190 Set_Unit_Number (Unum_Spec);
1191 Set_String (" = 0 then");
1192 Write_Statement_Buffer;
1197 Get_Decoded_Name_String_With_Brackets (U.Uname);
1199 if Name_Buffer (Name_Len) = 's' then
1200 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1202 Name_Len := Name_Len + 8;
1204 -- Special case in CodePeer mode for subprogram bodies
1205 -- which correspond to CodePeer 'Elab_Subp_Body special
1208 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1209 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1211 Name_Len := Name_Len + 13;
1214 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1216 Name_Len := Name_Len + 8;
1219 Set_Casing (U.Icasing);
1222 Write_Statement_Buffer;
1224 if Check_Elab_Flag then
1228 if U.Utype /= Is_Spec
1229 and then not CodePeer_Mode
1230 and then Units.Table (Unum_Spec).Set_Elab_Entity
1233 Set_Unit_Number (Unum_Spec);
1235 -- The AAMP target has no notion of shared libraries, and
1236 -- there's no possibility of reelaboration, so we treat the
1237 -- the elaboration var as a flag instead of a counter and
1240 if AAMP_On_Target then
1241 Set_String (" := 1;");
1243 -- Otherwise (normal case), increment elaboration counter
1246 Set_String (" := E");
1247 Set_Unit_Number (Unum_Spec);
1248 Set_String (" + 1;");
1251 Write_Statement_Buffer;
1258 ------------------------
1259 -- Gen_Elab_Externals --
1260 ------------------------
1262 procedure Gen_Elab_Externals is
1264 if CodePeer_Mode then
1268 for E in Elab_Order.First .. Elab_Order.Last loop
1270 Unum : constant Unit_Id := Elab_Order.Table (E);
1271 U : Unit_Record renames Units.Table (Unum);
1274 -- Check for Elab_Entity to be set for this unit
1276 if U.Set_Elab_Entity
1278 -- Don't generate reference for stand alone library
1280 and then not U.SAL_Interface
1282 -- Don't generate reference for predefined file in No_Run_Time
1283 -- mode, since we don't include the object files in this case
1287 and then Is_Predefined_File_Name (U.Sfile))
1291 Set_Unit_Number (Unum);
1292 Set_String (" : Short_Integer; pragma Import (Ada, E");
1293 Set_Unit_Number (Unum);
1294 Set_String (", """);
1295 Get_Name_String (U.Uname);
1297 Set_String ("_E"");");
1298 Write_Statement_Buffer;
1304 end Gen_Elab_Externals;
1306 --------------------
1307 -- Gen_Elab_Order --
1308 --------------------
1310 procedure Gen_Elab_Order is
1312 WBI (" -- BEGIN ELABORATION ORDER");
1314 for J in Elab_Order.First .. Elab_Order.Last loop
1315 Set_String (" -- ");
1316 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1318 Write_Statement_Buffer;
1321 WBI (" -- END ELABORATION ORDER");
1325 --------------------------
1326 -- Gen_Finalize_Library --
1327 --------------------------
1329 procedure Gen_Finalize_Library is
1332 Uspec : Unit_Record;
1335 procedure Gen_Header;
1336 -- Generate the header of the finalization routine
1342 procedure Gen_Header is
1344 WBI (" procedure finalize_library is");
1348 -- Start of processing for Gen_Finalize_Library
1351 if CodePeer_Mode then
1355 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1356 Unum := Elab_Order.Table (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
1656 -- not referenced elsewhere in the generated program, but is needed
1657 -- by the debugger (that's why it is generated in the first place).
1658 -- The reference stops Ada_Main_Program_Name from being optimized
1659 -- away by 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
1686 -- needs to be done since the gnat_argc/argv/envp variables are
1687 -- suppressed in this case.
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 is
1790 -- This keeps track of the position in the sorted set of entries
1791 -- in the Linker_Options table of where the first entry from an
1792 -- internal file appears.
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.First .. Elab_Order.Last 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.Table (E)).SAL_Interface
1863 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1867 (Units.Table (Elab_Order.Table (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);
1897 Add_Str_To_Name_Buffer ("-L");
1898 Add_Str_To_Name_Buffer (Dir.all);
1899 Write_Linker_Option;
1903 if not (Opt.No_Run_Time_Mode or Opt.No_Stdlib) then
1906 if Opt.Shared_Libgnat then
1907 Add_Str_To_Name_Buffer ("-shared");
1909 Add_Str_To_Name_Buffer ("-static");
1912 -- Write directly to avoid inclusion in -K output as -static and
1913 -- -shared are not usually specified linker options.
1915 WBI (" -- " & Name_Buffer (1 .. Name_Len));
1918 -- Sort linker options
1920 -- This sort accomplishes two important purposes:
1922 -- a) All application files are sorted to the front, and all GNAT
1923 -- internal files are sorted to the end. This results in a well
1924 -- defined dividing line between the two sets of files, for the
1925 -- purpose of inserting certain standard library references into
1926 -- the linker arguments list.
1928 -- b) Given two different units, we sort the linker options so that
1929 -- those from a unit earlier in the elaboration order comes later
1930 -- in the list. This is a heuristic designed to create a more
1931 -- friendly order of linker options when the operations appear in
1932 -- separate units. The idea is that if unit A must be elaborated
1933 -- before unit B, then it is more likely that B references
1934 -- libraries included by A, than vice versa, so we want libraries
1935 -- included by A to come after libraries included by B.
1937 -- These two criteria are implemented by function Lt_Linker_Option. Note
1938 -- that a special case of b) is that specs are elaborated before bodies,
1939 -- so linker options from specs come after linker options for bodies,
1940 -- and again, the assumption is that libraries used by the body are more
1941 -- likely to reference libraries used by the spec, than vice versa.
1944 (Linker_Options.Last,
1945 Move_Linker_Option'Access,
1946 Lt_Linker_Option'Access);
1948 -- Write user linker options, i.e. the set of linker options that come
1949 -- from all files other than GNAT internal files, Lgnat is left set to
1950 -- point to the first entry from a GNAT internal file, or past the end
1951 -- of the entries if there are no internal files.
1953 Lgnat := Linker_Options.Last + 1;
1955 for J in 1 .. Linker_Options.Last loop
1956 if not Linker_Options.Table (J).Internal_File then
1957 Get_Name_String (Linker_Options.Table (J).Name);
1958 Write_Linker_Option;
1965 -- Now we insert standard linker options that must appear after the
1966 -- entries from user files, and before the entries from GNAT run-time
1967 -- files. The reason for this decision is that libraries referenced
1968 -- by internal routines may reference these standard library entries.
1970 -- Note that we do not insert anything when pragma No_Run_Time has
1971 -- been specified or when the standard libraries are not to be used,
1972 -- otherwise on some platforms, we may get duplicate symbols when
1973 -- linking (not clear if this is still the case, but it is harmless).
1975 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
1979 if Opt.Shared_Libgnat then
1980 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
1982 Add_Str_To_Name_Buffer ("-lgnarl");
1985 Write_Linker_Option;
1990 if Opt.Shared_Libgnat then
1991 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
1993 Add_Str_To_Name_Buffer ("-lgnat");
1996 Write_Linker_Option;
1999 -- Write linker options from all internal files
2001 for J in Lgnat .. Linker_Options.Last loop
2002 Get_Name_String (Linker_Options.Table (J).Name);
2003 Write_Linker_Option;
2006 if Output_Linker_Option_List and then not Zero_Formatting then
2010 WBI ("-- END Object file/option list ");
2011 end Gen_Object_Files_Options;
2013 ---------------------
2014 -- Gen_Output_File --
2015 ---------------------
2017 procedure Gen_Output_File (Filename : String) is
2019 -- Acquire settings for Interrupt_State pragmas
2021 Set_IS_Pragma_Table;
2023 -- Acquire settings for Priority_Specific_Dispatching pragma
2025 Set_PSD_Pragma_Table;
2027 -- Override time slice value if -T switch is set
2029 if Time_Slice_Set then
2030 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2033 -- Count number of elaboration calls
2035 for E in Elab_Order.First .. Elab_Order.Last loop
2036 if Units.Table (Elab_Order.Table (E)).No_Elab then
2039 Num_Elab_Calls := Num_Elab_Calls + 1;
2043 -- Generate output file in appropriate language
2045 Gen_Output_File_Ada (Filename);
2046 end Gen_Output_File;
2048 -------------------------
2049 -- Gen_Output_File_Ada --
2050 -------------------------
2052 procedure Gen_Output_File_Ada (Filename : String) is
2054 Ada_Main : constant String := Get_Ada_Main_Name;
2055 -- Name to be used for generated Ada main program. See the body of
2056 -- function Get_Ada_Main_Name for details on the form of the name.
2058 Needs_Library_Finalization : constant Boolean :=
2059 not Configurable_Run_Time_On_Target and then Has_Finalizer;
2060 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2061 -- non-terminating, so we do not want finalization.
2064 -- Name of generated bind file (spec)
2067 -- Name of generated bind file (body)
2070 -- Create spec first
2072 Create_Binder_Output (Filename, 's', Bfiles);
2074 -- We always compile the binder file in Ada 95 mode so that we properly
2075 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2076 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2078 WBI ("pragma Ada_95;");
2079 WBI ("pragma Warnings (Off);");
2081 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2082 -- then we need to make sure that the binder program is compiled with
2083 -- the same restriction, so that no exception tables are generated.
2085 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2086 WBI ("pragma Restrictions (No_Exception_Handlers);");
2089 -- Same processing for Restrictions (No_Exception_Propagation)
2091 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2092 WBI ("pragma Restrictions (No_Exception_Propagation);");
2095 -- Same processing for pragma No_Run_Time
2097 if No_Run_Time_Mode then
2098 WBI ("pragma No_Run_Time;");
2101 -- Generate with of System so we can reference System.Address
2103 WBI ("with System;");
2105 -- Generate with of System.Initialize_Scalars if active
2107 if Initialize_Scalars_Used then
2108 WBI ("with System.Scalar_Values;");
2111 -- Generate with of System.Secondary_Stack if active
2113 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2114 WBI ("with System.Secondary_Stack;");
2117 Resolve_Binder_Options;
2119 -- Generate standard with's
2121 if not Suppress_Standard_Library_On_Target then
2122 if CodePeer_Mode then
2123 WBI ("with System.Standard_Library;");
2127 WBI ("package " & Ada_Main & " is");
2129 -- Main program case
2131 if Bind_Main_Program then
2132 -- Generate argc/argv stuff unless suppressed
2134 if Command_Line_Args_On_Target
2135 or not Configurable_Run_Time_On_Target
2138 WBI (" gnat_argc : Integer;");
2139 WBI (" gnat_argv : System.Address;");
2140 WBI (" gnat_envp : System.Address;");
2142 -- If the standard library is not suppressed, these variables
2143 -- are in the run-time data area for easy run time access.
2145 if not Suppress_Standard_Library_On_Target then
2147 WBI (" pragma Import (C, gnat_argc);");
2148 WBI (" pragma Import (C, gnat_argv);");
2149 WBI (" pragma Import (C, gnat_envp);");
2153 -- Define exit status. Again in normal mode, this is in the
2154 -- run-time library, and is initialized there, but in the
2155 -- configurable runtime case, the variable is declared and
2156 -- initialized in this file.
2160 if Configurable_Run_Time_Mode then
2161 if Exit_Status_Supported_On_Target then
2162 WBI (" gnat_exit_status : Integer := 0;");
2166 WBI (" gnat_exit_status : Integer;");
2167 WBI (" pragma Import (C, gnat_exit_status);");
2170 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2171 -- the main program. Otherwise, it can lead under some circumstances
2172 -- to a symbol duplication during the link (for instance when a C
2173 -- program uses two Ada libraries). Also zero terminate the string
2174 -- so that its end can be found reliably at run time.
2177 WBI (" GNAT_Version : constant String :=");
2178 WBI (" """ & Ver_Prefix &
2179 Gnat_Version_String &
2181 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2184 Set_String (" Ada_Main_Program_Name : constant String := """);
2185 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2187 Set_Main_Program_Name;
2188 Set_String (""" & ASCII.NUL;");
2190 Write_Statement_Buffer;
2193 (" pragma Export (C, Ada_Main_Program_Name, " &
2194 """__gnat_ada_main_program_name"");");
2198 WBI (" procedure " & Ada_Init_Name.all & ";");
2199 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2200 Ada_Init_Name.all & """);");
2202 -- If -a has been specified use pragma Linker_Constructor for the init
2203 -- procedure and pragma Linker_Destructor for the final procedure.
2205 if Use_Pragma_Linker_Constructor then
2206 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2209 if not Cumulative_Restrictions.Set (No_Finalization) then
2211 WBI (" procedure " & Ada_Final_Name.all & ";");
2212 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2213 Ada_Final_Name.all & """);");
2215 if Use_Pragma_Linker_Constructor then
2216 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2220 if Bind_Main_Program then
2224 if Exit_Status_Supported_On_Target then
2225 Set_String (" function ");
2227 Set_String (" procedure ");
2230 Set_String (Get_Main_Name);
2232 -- Generate argument list if present
2234 if Command_Line_Args_On_Target then
2235 Write_Statement_Buffer;
2236 WBI (" (argc : Integer;");
2237 WBI (" argv : System.Address;");
2239 (" envp : System.Address)");
2241 if Exit_Status_Supported_On_Target then
2242 Write_Statement_Buffer;
2243 WBI (" return Integer;");
2245 Write_Statement_Buffer (";");
2249 if Exit_Status_Supported_On_Target then
2250 Write_Statement_Buffer (" return Integer;");
2252 Write_Statement_Buffer (";");
2256 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2257 Get_Main_Name & """);");
2266 WBI ("end " & Ada_Main & ";");
2267 Close_Binder_Output;
2269 -- Prepare to write body
2271 Create_Binder_Output (Filename, 'b', Bfileb);
2273 -- We always compile the binder file in Ada 95 mode so that we properly
2274 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2275 -- of the Ada 2005/2012 constructs are needed by the binder file.
2277 WBI ("pragma Ada_95;");
2278 WBI ("pragma Warnings (Off);");
2280 -- Output Source_File_Name pragmas which look like
2282 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2283 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2285 -- where sss/bbb are the spec/body file names respectively
2287 Get_Name_String (Bfiles);
2288 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2290 WBI ("pragma Source_File_Name (" &
2292 ", Spec_File_Name => """ &
2293 Name_Buffer (1 .. Name_Len + 3));
2295 Get_Name_String (Bfileb);
2296 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2298 WBI ("pragma Source_File_Name (" &
2300 ", Body_File_Name => """ &
2301 Name_Buffer (1 .. Name_Len + 3));
2303 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2304 -- versions of the compiler which have overflow checks on by default.
2305 -- We do not want overflow checking enabled for the increments of the
2306 -- elaboration variables (since this can cause an unwanted reference to
2307 -- the last chance exception handler for limited run-times).
2309 WBI ("pragma Suppress (Overflow_Check);");
2311 -- Generate with of System.Restrictions to initialize
2312 -- Run_Time_Restrictions.
2314 if System_Restrictions_Used
2315 and not Suppress_Standard_Library_On_Target
2318 WBI ("with System.Restrictions;");
2321 -- Generate with of Ada.Exceptions if needs library finalization
2323 if Needs_Library_Finalization then
2324 WBI ("with Ada.Exceptions;");
2327 -- Generate with of System.Elaboration_Allocators if the restriction
2328 -- No_Standard_Allocators_After_Elaboration was present.
2330 if Cumulative_Restrictions.Set
2331 (No_Standard_Allocators_After_Elaboration)
2333 WBI ("with System.Elaboration_Allocators;");
2336 -- Generate start of package body
2339 WBI ("package body " & Ada_Main & " is");
2342 -- Generate externals for elaboration entities
2346 if not CodePeer_Mode then
2347 if not Suppress_Standard_Library_On_Target then
2349 -- Generate Priority_Specific_Dispatching pragma string
2352 (" Local_Priority_Specific_Dispatching : " &
2353 "constant String := """);
2355 for J in 0 .. PSD_Pragma_Settings.Last loop
2356 Set_Char (PSD_Pragma_Settings.Table (J));
2360 Write_Statement_Buffer;
2362 -- Generate Interrupt_State pragma string
2364 Set_String (" Local_Interrupt_States : constant String := """);
2366 for J in 0 .. IS_Pragma_Settings.Last loop
2367 Set_Char (IS_Pragma_Settings.Table (J));
2371 Write_Statement_Buffer;
2375 if not Suppress_Standard_Library_On_Target then
2377 -- The B.1(39) implementation advice says that the adainit
2378 -- and adafinal routines should be idempotent. Generate a flag to
2379 -- ensure that. This is not needed if we are suppressing the
2380 -- standard library since it would never be referenced.
2382 WBI (" Is_Elaborated : Boolean := False;");
2384 -- Generate bind environment string
2386 Gen_Bind_Env_String;
2392 -- Generate the adafinal routine unless there is no finalization to do
2394 if not Cumulative_Restrictions.Set (No_Finalization) then
2395 if Needs_Library_Finalization then
2396 Gen_Finalize_Library;
2404 if Bind_Main_Program then
2408 -- Output object file list and the Ada body is complete
2410 Gen_Object_Files_Options;
2413 WBI ("end " & Ada_Main & ";");
2415 Close_Binder_Output;
2416 end Gen_Output_File_Ada;
2418 ----------------------
2419 -- Gen_Restrictions --
2420 ----------------------
2422 procedure Gen_Restrictions is
2426 if Suppress_Standard_Library_On_Target
2427 or not System_Restrictions_Used
2432 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2438 for J in Cumulative_Restrictions.Set'Range loop
2439 Set_Boolean (Cumulative_Restrictions.Set (J));
2443 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2444 Write_Statement_Buffer;
2450 Set_String_Replace ("),");
2451 Write_Statement_Buffer;
2452 Set_String (" Value => (");
2454 for J in Cumulative_Restrictions.Value'Range loop
2455 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2459 Set_String_Replace ("),");
2460 Write_Statement_Buffer;
2461 WBI (" Violated =>");
2465 for J in Cumulative_Restrictions.Violated'Range loop
2466 Set_Boolean (Cumulative_Restrictions.Violated (J));
2470 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2471 Write_Statement_Buffer;
2477 Set_String_Replace ("),");
2478 Write_Statement_Buffer;
2479 Set_String (" Count => (");
2481 for J in Cumulative_Restrictions.Count'Range loop
2482 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2486 Set_String_Replace ("),");
2487 Write_Statement_Buffer;
2488 Set_String (" Unknown => (");
2490 for J in Cumulative_Restrictions.Unknown'Range loop
2491 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2495 Set_String_Replace ("))");
2497 Write_Statement_Buffer;
2498 end Gen_Restrictions;
2504 -- This routine generates lines such as:
2506 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2507 -- pragma Export (C, unnnnn, unam);
2509 -- for each unit, where unam is the unit name suffixed by either B or S for
2510 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2511 -- the version number, and nnnnn is a 5-digits serial number.
2513 procedure Gen_Versions is
2514 Ubuf : String (1 .. 6) := "u00000";
2516 procedure Increment_Ubuf;
2517 -- Little procedure to increment the serial number
2519 --------------------
2520 -- Increment_Ubuf --
2521 --------------------
2523 procedure Increment_Ubuf is
2525 for J in reverse Ubuf'Range loop
2526 Ubuf (J) := Character'Succ (Ubuf (J));
2527 exit when Ubuf (J) <= '9';
2532 -- Start of processing for Gen_Versions
2537 WBI (" type Version_32 is mod 2 ** 32;");
2538 for U in Units.First .. Units.Last loop
2539 if not Units.Table (U).SAL_Interface
2541 (not Bind_For_Library or else Units.Table (U).Directly_Scanned)
2544 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2545 Units.Table (U).Version & "#;");
2546 Set_String (" pragma Export (C, ");
2548 Set_String (", """);
2550 Get_Name_String (Units.Table (U).Uname);
2552 for K in 1 .. Name_Len loop
2553 if Name_Buffer (K) = '.' then
2557 elsif Name_Buffer (K) = '%' then
2561 Set_Char (Name_Buffer (K));
2565 if Name_Buffer (Name_Len) = 's' then
2571 Set_String (""");");
2572 Write_Statement_Buffer;
2577 ------------------------
2578 -- Get_Main_Unit_Name --
2579 ------------------------
2581 function Get_Main_Unit_Name (S : String) return String is
2582 Result : String := S;
2585 for J in S'Range loop
2586 if Result (J) = '.' then
2592 end Get_Main_Unit_Name;
2594 -----------------------
2595 -- Get_Ada_Main_Name --
2596 -----------------------
2598 function Get_Ada_Main_Name return String is
2599 Suffix : constant String := "_00";
2600 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2601 Opt.Ada_Main_Name.all & Suffix;
2605 -- For CodePeer, we want reproducible names (independent of other
2606 -- mains that may or may not be present) that don't collide
2607 -- when analyzing multiple mains and which are easily recognizable
2608 -- as "ada_main" names.
2610 if CodePeer_Mode then
2611 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2612 return "ada_main_for_" &
2613 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2616 -- This loop tries the following possibilities in order
2622 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2623 -- it is set to 'ada_main'.
2625 for J in 0 .. 99 loop
2627 Nlen := Name'Length - Suffix'Length;
2629 Nlen := Name'Length;
2630 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2631 Name (Name'Last - 1) :=
2632 Character'Val (J / 10 + Character'Pos ('0'));
2635 for K in ALIs.First .. ALIs.Last loop
2636 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2638 -- Get unit name, removing %b or %e at end
2640 Get_Name_String (Units.Table (L).Uname);
2641 Name_Len := Name_Len - 2;
2643 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2649 return Name (1 .. Nlen);
2655 -- If we fall through, just use a peculiar unlikely name
2657 return ("Qwertyuiop");
2658 end Get_Ada_Main_Name;
2664 function Get_Main_Name return String is
2666 -- Explicit name given with -M switch
2668 if Bind_Alternate_Main_Name then
2669 return Alternate_Main_Name.all;
2671 -- Case of main program name to be used directly
2673 elsif Use_Ada_Main_Program_Name_On_Target then
2675 -- Get main program name
2677 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2679 -- If this is a child name, return only the name of the child, since
2680 -- we can't have dots in a nested program name. Note that we do not
2681 -- include the %b at the end of the unit name.
2683 for J in reverse 1 .. Name_Len - 2 loop
2684 if J = 1 or else Name_Buffer (J - 1) = '.' then
2685 return Name_Buffer (J .. Name_Len - 2);
2689 raise Program_Error; -- impossible exit
2691 -- Case where "main" is to be used as default
2698 ---------------------
2699 -- Get_WC_Encoding --
2700 ---------------------
2702 function Get_WC_Encoding return Character is
2704 -- If encoding method specified by -W switch, then return it
2706 if Wide_Character_Encoding_Method_Specified then
2707 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2709 -- If no main program, and not specified, set brackets, we really have
2710 -- no better choice. If some other encoding is required when there is
2711 -- no main, it must be set explicitly using -Wx.
2713 -- Note: if the ALI file always passed the wide character encoding of
2714 -- every file, then we could use the encoding of the initial specified
2715 -- file, but this information is passed only for potential main
2716 -- programs. We could fix this sometime, but it is a very minor point
2717 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
2718 -- is no main program).
2720 elsif No_Main_Subprogram then
2723 -- Otherwise if there is a main program, take encoding from it
2726 return ALIs.Table (ALIs.First).WC_Encoding;
2728 end Get_WC_Encoding;
2734 function Has_Finalizer return Boolean is
2739 for E in reverse Elab_Order.First .. Elab_Order.Last loop
2740 Unum := Elab_Order.Table (E);
2741 U := Units.Table (Unum);
2743 -- We are only interested in non-generic packages
2745 if U.Unit_Kind = 'p'
2746 and then U.Has_Finalizer
2747 and then not U.Is_Generic
2748 and then not U.No_Elab
2761 function Hash (Nam : Name_Id) return Header_Num is
2763 return Int (Nam - Names_Low_Bound) rem Header_Num'Last;
2766 ----------------------
2767 -- Lt_Linker_Option --
2768 ----------------------
2770 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
2772 -- Sort internal files last
2774 if Linker_Options.Table (Op1).Internal_File
2776 Linker_Options.Table (Op2).Internal_File
2778 -- Note: following test uses False < True
2780 return Linker_Options.Table (Op1).Internal_File
2782 Linker_Options.Table (Op2).Internal_File;
2784 -- If both internal or both non-internal, sort according to the
2785 -- elaboration position. A unit that is elaborated later should come
2786 -- earlier in the linker options list.
2789 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2791 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2794 end Lt_Linker_Option;
2796 ------------------------
2797 -- Move_Linker_Option --
2798 ------------------------
2800 procedure Move_Linker_Option (From : Natural; To : Natural) is
2802 Linker_Options.Table (To) := Linker_Options.Table (From);
2803 end Move_Linker_Option;
2805 ----------------------------
2806 -- Resolve_Binder_Options --
2807 ----------------------------
2809 procedure Resolve_Binder_Options is
2811 procedure Check_Package (Var : in out Boolean; Name : String);
2812 -- Set Var to true iff the current identifier in Namet is Name. Do
2813 -- nothing if it doesn't match. This procedure is just a helper to
2814 -- avoid explicitly dealing with length.
2820 procedure Check_Package (Var : in out Boolean; Name : String) is
2822 if Name_Len = Name'Length
2823 and then Name_Buffer (1 .. Name_Len) = Name
2829 -- Start of processing for Resolve_Binder_Options
2832 for E in Elab_Order.First .. Elab_Order.Last loop
2833 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
2835 -- This is not a perfect approach, but is the current protocol
2836 -- between the run-time and the binder to indicate that tasking is
2837 -- used: System.OS_Interface should always be used by any tasking
2840 Check_Package (With_GNARL, "system.os_interface%s");
2842 -- Ditto for the use of restricted tasking
2845 (System_Tasking_Restricted_Stages_Used,
2846 "system.tasking.restricted.stages%s");
2848 -- Ditto for the use of interrupts
2850 Check_Package (System_Interrupts_Used, "system.interrupts%s");
2852 -- Ditto for the use of dispatching domains
2855 (Dispatching_Domains_Used,
2856 "system.multiprocessors.dispatching_domains%s");
2858 -- Ditto for the use of restrictions
2860 Check_Package (System_Restrictions_Used, "system.restrictions%s");
2862 end Resolve_Binder_Options;
2868 procedure Set_Bind_Env (Key, Value : String) is
2870 -- The lengths of Key and Value are stored as single bytes
2872 if Key'Length > 255 then
2873 Osint.Fail ("bind environment key """ & Key & """ too long");
2876 if Value'Length > 255 then
2877 Osint.Fail ("bind environment value """ & Value & """ too long");
2880 Bind_Environment.Set (Name_Find_Str (Key), Name_Find_Str (Value));
2887 procedure Set_Boolean (B : Boolean) is
2888 True_Str : constant String := "True";
2889 False_Str : constant String := "False";
2892 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
2893 Last := Last + True_Str'Length;
2895 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
2896 Last := Last + False_Str'Length;
2904 procedure Set_Char (C : Character) is
2907 Statement_Buffer (Last) := C;
2914 procedure Set_Int (N : Int) is
2926 Statement_Buffer (Last) :=
2927 Character'Val (N mod 10 + Character'Pos ('0'));
2931 -------------------------
2932 -- Set_IS_Pragma_Table --
2933 -------------------------
2935 procedure Set_IS_Pragma_Table is
2937 for F in ALIs.First .. ALIs.Last loop
2938 for K in ALIs.Table (F).First_Interrupt_State ..
2939 ALIs.Table (F).Last_Interrupt_State
2942 Inum : constant Int :=
2943 Interrupt_States.Table (K).Interrupt_Id;
2944 Stat : constant Character :=
2945 Interrupt_States.Table (K).Interrupt_State;
2948 while IS_Pragma_Settings.Last < Inum loop
2949 IS_Pragma_Settings.Append ('n');
2952 IS_Pragma_Settings.Table (Inum) := Stat;
2956 end Set_IS_Pragma_Table;
2958 ---------------------------
2959 -- Set_Main_Program_Name --
2960 ---------------------------
2962 procedure Set_Main_Program_Name is
2964 -- Note that name has %b on the end which we ignore
2966 -- First we output the initial _ada_ since we know that the main
2967 -- program is a library level subprogram.
2969 Set_String ("_ada_");
2971 -- Copy name, changing dots to double underscores
2973 for J in 1 .. Name_Len - 2 loop
2974 if Name_Buffer (J) = '.' then
2977 Set_Char (Name_Buffer (J));
2980 end Set_Main_Program_Name;
2982 ---------------------
2983 -- Set_Name_Buffer --
2984 ---------------------
2986 procedure Set_Name_Buffer is
2988 for J in 1 .. Name_Len loop
2989 Set_Char (Name_Buffer (J));
2991 end Set_Name_Buffer;
2993 -------------------------
2994 -- Set_PSD_Pragma_Table --
2995 -------------------------
2997 procedure Set_PSD_Pragma_Table is
2999 for F in ALIs.First .. ALIs.Last loop
3000 for K in ALIs.Table (F).First_Specific_Dispatching ..
3001 ALIs.Table (F).Last_Specific_Dispatching
3004 DTK : Specific_Dispatching_Record
3005 renames Specific_Dispatching.Table (K);
3008 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3009 PSD_Pragma_Settings.Append ('F');
3012 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3013 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3018 end Set_PSD_Pragma_Table;
3024 procedure Set_String (S : String) is
3026 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3027 Last := Last + S'Length;
3030 ------------------------
3031 -- Set_String_Replace --
3032 ------------------------
3034 procedure Set_String_Replace (S : String) is
3036 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3037 end Set_String_Replace;
3043 procedure Set_Unit_Name is
3045 for J in 1 .. Name_Len - 2 loop
3046 if Name_Buffer (J) = '.' then
3049 Set_Char (Name_Buffer (J));
3054 ---------------------
3055 -- Set_Unit_Number --
3056 ---------------------
3058 procedure Set_Unit_Number (U : Unit_Id) is
3059 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3060 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3063 if Num_Units >= 10 and then Unum < 10 then
3067 if Num_Units >= 100 and then Unum < 100 then
3072 end Set_Unit_Number;
3074 ---------------------
3075 -- Write_Bind_Line --
3076 ---------------------
3078 procedure Write_Bind_Line (S : String) is
3080 -- Need to strip trailing LF from S
3082 WBI (S (S'First .. S'Last - 1));
3083 end Write_Bind_Line;
3085 ----------------------------
3086 -- Write_Statement_Buffer --
3087 ----------------------------
3089 procedure Write_Statement_Buffer is
3091 WBI (Statement_Buffer (1 .. Last));
3093 end Write_Statement_Buffer;
3095 procedure Write_Statement_Buffer (S : String) is
3098 Write_Statement_Buffer;
3099 end Write_Statement_Buffer;