1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, 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
;
38 with Table
; use Table
;
39 with Targparm
; use Targparm
;
40 with Types
; use Types
;
42 with System
.OS_Lib
; use System
.OS_Lib
;
43 with System
.WCh_Con
; use System
.WCh_Con
;
45 with GNAT
.Heap_Sort_A
; use GNAT
.Heap_Sort_A
;
48 package body Bindgen
is
49 use Binde
.Unit_Id_Tables
;
51 Statement_Buffer
: String (1 .. 1000);
52 -- Buffer used for constructing output statements
54 Stm_Last
: Natural := 0;
55 -- Stm_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 System_BB_CPU_Primitives_Multiprocessors_Used
: Boolean := False;
92 -- Flag indicating whether unit System.BB.CPU_Primitives.Multiprocessors
93 -- is in the closure of the partition. This is set by procedure
94 -- Resolve_Binder_Options, and it is used to call a procedure that starts
97 Lib_Final_Built
: Boolean := False;
98 -- Flag indicating whether the finalize_library rountine has been built
100 Bind_Env_String_Built
: Boolean := False;
101 -- Flag indicating whether a bind environment string has been built
103 CodePeer_Wrapper_Name
: constant String := "call_main_subprogram";
104 -- For CodePeer, introduce a wrapper subprogram which calls the
105 -- user-defined main subprogram.
107 ----------------------------------
108 -- Interface_State Pragma Table --
109 ----------------------------------
111 -- This table assembles the interface state pragma information from
112 -- all the units in the partition. Note that Bcheck has already checked
113 -- that the information is consistent across units. The entries
114 -- in this table are n/u/r/s for not set/user/runtime/system.
116 package IS_Pragma_Settings
is new Table
.Table
117 (Table_Component_Type
=> Character,
118 Table_Index_Type
=> Int
,
119 Table_Low_Bound
=> 0,
120 Table_Initial
=> 100,
121 Table_Increment
=> 200,
122 Table_Name
=> "IS_Pragma_Settings");
124 -- This table assembles the Priority_Specific_Dispatching pragma
125 -- information from all the units in the partition. Note that Bcheck has
126 -- already checked that the information is consistent across units.
127 -- The entries in this table are the upper case first character of the
128 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
130 package PSD_Pragma_Settings
is new Table
.Table
131 (Table_Component_Type
=> Character,
132 Table_Index_Type
=> Int
,
133 Table_Low_Bound
=> 0,
134 Table_Initial
=> 100,
135 Table_Increment
=> 200,
136 Table_Name
=> "PSD_Pragma_Settings");
138 ----------------------------
139 -- Bind_Environment Table --
140 ----------------------------
142 subtype Header_Num
is Int
range 0 .. 36;
144 function Hash
(Nam
: Name_Id
) return Header_Num
;
146 package Bind_Environment
is new GNAT
.HTable
.Simple_HTable
147 (Header_Num
=> Header_Num
,
149 No_Element
=> No_Name
,
154 ----------------------
155 -- Run-Time Globals --
156 ----------------------
158 -- This section documents the global variables that are set from the
159 -- generated binder file.
161 -- Main_Priority : Integer;
162 -- Time_Slice_Value : Integer;
163 -- Heap_Size : Natural;
164 -- WC_Encoding : Character;
165 -- Locking_Policy : Character;
166 -- Queuing_Policy : Character;
167 -- Task_Dispatching_Policy : Character;
168 -- Priority_Specific_Dispatching : System.Address;
169 -- Num_Specific_Dispatching : Integer;
170 -- Restrictions : System.Address;
171 -- Interrupt_States : System.Address;
172 -- Num_Interrupt_States : Integer;
173 -- Unreserve_All_Interrupts : Integer;
174 -- Exception_Tracebacks : Integer;
175 -- Exception_Tracebacks_Symbolic : Integer;
176 -- Detect_Blocking : Integer;
177 -- Default_Stack_Size : Integer;
178 -- Leap_Seconds_Support : Integer;
179 -- Main_CPU : Integer;
181 -- Main_Priority is the priority value set by pragma Priority in the main
182 -- program. If no such pragma is present, the value is -1.
184 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
185 -- main program, or by the use of a -Tnnn parameter for the binder (if both
186 -- are present, the binder value overrides). The value is in milliseconds.
187 -- A value of zero indicates that time slicing should be suppressed. If no
188 -- pragma is present, and no -T switch was used, the value is -1.
190 -- WC_Encoding shows the wide character encoding method used for the main
191 -- program. This is one of the encoding letters defined in
192 -- System.WCh_Con.WC_Encoding_Letters.
194 -- Locking_Policy is a space if no locking policy was specified for the
195 -- partition. If a locking policy was specified, the value is the upper
196 -- case first character of the locking policy name, for example, 'C' for
199 -- Queuing_Policy is a space if no queuing policy was specified for the
200 -- partition. If a queuing policy was specified, the value is the upper
201 -- case first character of the queuing policy name for example, 'F' for
204 -- Task_Dispatching_Policy is a space if no task dispatching policy was
205 -- specified for the partition. If a task dispatching policy was specified,
206 -- the value is the upper case first character of the policy name, e.g. 'F'
207 -- for FIFO_Within_Priorities.
209 -- Priority_Specific_Dispatching is the address of a string used to store
210 -- the task dispatching policy specified for the different priorities in
211 -- the partition. The length of this string is determined by the last
212 -- priority for which such a pragma applies (the string will be a null
213 -- string if no specific dispatching policies were used). If pragma were
214 -- present, the entries apply to the priorities in sequence from the first
215 -- priority. The value stored is the upper case first character of the
216 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
217 -- for those priority ranges not specified.
219 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
220 -- string. It will be set to zero if no Priority_Specific_Dispatching
221 -- pragmas are present.
223 -- Restrictions is the address of a null-terminated string specifying the
224 -- restrictions information for the partition. The format is identical to
225 -- that of the parameter string found on R lines in ali files (see Lib.Writ
226 -- spec in lib-writ.ads for full details). The difference is that in this
227 -- context the values are the cumulative ones for the entire partition.
229 -- Interrupt_States is the address of a string used to specify the
230 -- cumulative results of Interrupt_State pragmas used in the partition.
231 -- The length of this string is determined by the last interrupt for which
232 -- such a pragma is given (the string will be a null string if no pragmas
233 -- were used). If pragma were present the entries apply to the interrupts
234 -- in sequence from the first interrupt, and are set to one of four
235 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
236 -- time, 's' for system, see description of Interrupt_State pragma for
239 -- Num_Interrupt_States is the length of the Interrupt_States string. It
240 -- will be set to zero if no Interrupt_State pragmas are present.
242 -- Unreserve_All_Interrupts is set to one if at least one unit in the
243 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
245 -- Exception_Tracebacks is set to one if the -Ea or -E parameter was
246 -- present in the bind and to zero otherwise. Note that on some targets
247 -- exception tracebacks are provided by default, so a value of zero for
248 -- this parameter does not necessarily mean no trace backs are available.
250 -- Exception_Tracebacks_Symbolic is set to one if the -Es parameter was
251 -- present in the bind and to zero otherwise.
253 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
254 -- not. A value of zero indicates that the pragma is not present, while a
255 -- value of 1 signals its presence in the partition.
257 -- Default_Stack_Size is the default stack size used when creating an Ada
258 -- task with no explicit Storage_Size clause.
260 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
261 -- disabled. A value of zero indicates that leap seconds are turned "off",
262 -- while a value of one signifies "on" status.
264 -- Main_CPU is the processor set by pragma CPU in the main program. If no
265 -- such pragma is present, the value is -1.
267 procedure WBI
(Info
: String) renames Osint
.B
.Write_Binder_Info
;
268 -- Convenient shorthand used throughout
270 -----------------------
271 -- Local Subprograms --
272 -----------------------
274 procedure Gen_Adainit
(Elab_Order
: Unit_Id_Array
);
275 -- Generates the Adainit procedure
277 procedure Gen_Adafinal
;
278 -- Generate the Adafinal procedure
280 procedure Gen_Bind_Env_String
;
281 -- Generate the bind environment buffer
283 procedure Gen_CodePeer_Wrapper
;
284 -- For CodePeer, generate wrapper which calls user-defined main subprogram
286 procedure Gen_Elab_Calls
(Elab_Order
: Unit_Id_Array
);
287 -- Generate sequence of elaboration calls
289 procedure Gen_Elab_Externals
(Elab_Order
: Unit_Id_Array
);
290 -- Generate sequence of external declarations for elaboration
292 procedure Gen_Elab_Order
(Elab_Order
: Unit_Id_Array
);
293 -- Generate comments showing elaboration order chosen
295 procedure Gen_Finalize_Library
(Elab_Order
: Unit_Id_Array
);
296 -- Generate a sequence of finalization calls to elaborated packages
299 -- Generate procedure main
301 procedure Gen_Object_Files_Options
(Elab_Order
: Unit_Id_Array
);
302 -- Output comments containing a list of the full names of the object
303 -- files to be linked and the list of linker options supplied by
304 -- Linker_Options pragmas in the source.
306 procedure Gen_Output_File_Ada
308 Elab_Order
: Unit_Id_Array
);
309 -- Generate Ada output file
311 procedure Gen_Restrictions
;
312 -- Generate initialization of restrictions variable
314 procedure Gen_Versions
;
315 -- Output series of definitions for unit versions
317 function Get_Ada_Main_Name
return String;
318 -- This function is used for the Ada main output to compute a usable name
319 -- for the generated main program. The normal main program name is
320 -- Ada_Main, but this won't work if the user has a unit with this name.
321 -- This function tries Ada_Main first, and if there is such a clash, then
322 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
324 function Get_Main_Unit_Name
(S
: String) return String;
325 -- Return the main unit name corresponding to S by replacing '.' with '_'
327 function Get_Main_Name
return String;
328 -- This function is used in the main output case to compute the correct
329 -- external main program. It is "main" by default, unless the flag
330 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
331 -- of the Ada main name without the "_ada". This default can be overridden
332 -- explicitly using the -Mname binder switch.
334 function Get_WC_Encoding
return Character;
335 -- Return wide character encoding method to set as WC_Encoding in output.
336 -- If -W has been used, returns the specified encoding, otherwise returns
337 -- the encoding method used for the main program source. If there is no
338 -- main program source (-z switch used), returns brackets ('b').
340 function Has_Finalizer
(Elab_Order
: Unit_Id_Array
) return Boolean;
341 -- Determine whether the current unit has at least one library-level
344 function Lt_Linker_Option
(Op1
: Natural; Op2
: Natural) return Boolean;
345 -- Compare linker options, when sorting, first according to
346 -- Is_Internal_File (internal files come later) and then by
347 -- elaboration order position (latest to earliest).
349 procedure Move_Linker_Option
(From
: Natural; To
: Natural);
350 -- Move routine for sorting linker options
352 procedure Resolve_Binder_Options
(Elab_Order
: Unit_Id_Array
);
353 -- Set the value of With_GNARL
355 procedure Set_Char
(C
: Character);
356 -- Set given character in Statement_Buffer at the Stm_Last + 1 position
357 -- and increment Stm_Last by one to reflect the stored character.
359 procedure Set_Int
(N
: Int
);
360 -- Set given value in decimal in Statement_Buffer with no spaces starting
361 -- at the Stm_Last + 1 position, and updating Stm_Last past the value. A
362 -- minus sign is output for a negative value.
364 procedure Set_Boolean
(B
: Boolean);
365 -- Set given boolean value in Statement_Buffer at the Stm_Last + 1 position
366 -- and update Stm_Last past the value.
368 procedure Set_IS_Pragma_Table
;
369 -- Initializes contents of IS_Pragma_Settings table from ALI table
371 procedure Set_Main_Program_Name
;
372 -- Given the main program name in Name_Buffer (length in Name_Len) generate
373 -- the name of the routine to be used in the call. The name is generated
374 -- starting at Stm_Last + 1, and Stm_Last is updated past it.
376 procedure Set_Name_Buffer
;
377 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
379 procedure Set_PSD_Pragma_Table
;
380 -- Initializes contents of PSD_Pragma_Settings table from ALI table
382 procedure Set_String
(S
: String);
383 -- Sets characters of given string in Statement_Buffer, starting at the
384 -- Stm_Last + 1 position, and updating last past the string value.
386 procedure Set_String_Replace
(S
: String);
387 -- Replaces the last S'Length characters in the Statement_Buffer with the
388 -- characters of S. The caller must ensure that these characters do in fact
389 -- exist in the Statement_Buffer.
391 procedure Set_Unit_Name
;
392 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
393 -- starting at the Stm_Last + 1 position and update Stm_Last past the
394 -- value. Each dot (.) will be qualified into double underscores (__).
396 procedure Set_Unit_Number
(U
: Unit_Id
);
397 -- Sets unit number (first unit is 1, leading zeroes output to line up all
398 -- output unit numbers nicely as required by the value, and by the total
401 procedure Write_Statement_Buffer
;
402 -- Write out contents of statement buffer up to Stm_Last, and reset
405 procedure Write_Statement_Buffer
(S
: String);
406 -- First writes its argument (using Set_String (S)), then writes out the
407 -- contents of statement buffer up to Stm_Last, and resets Stm_Last to 0.
409 procedure Write_Bind_Line
(S
: String);
410 -- Write S (an LF-terminated string) to the binder file (for use with
411 -- Set_Special_Output).
417 procedure Gen_Adafinal
is
419 WBI
(" procedure " & Ada_Final_Name
.all & " is");
421 -- Call s_stalib_adafinal to await termination of tasks and so on. We
422 -- want to do this if there is a main program, either in Ada or in some
423 -- other language. (Note that Bind_Main_Program is True for Ada mains,
424 -- but False for mains in other languages.) We do not want to do this if
425 -- we're binding a library.
427 if not Bind_For_Library
and not CodePeer_Mode
then
428 WBI
(" procedure s_stalib_adafinal;");
429 Set_String
(" pragma Import (C, s_stalib_adafinal, ");
430 Set_String
("""system__standard_library__adafinal"");");
431 Write_Statement_Buffer
;
435 WBI
(" procedure Runtime_Finalize;");
436 WBI
(" pragma Import (C, Runtime_Finalize, " &
437 """__gnat_runtime_finalize"");");
441 if not CodePeer_Mode
then
442 WBI
(" if not Is_Elaborated then");
445 WBI
(" Is_Elaborated := False;");
448 WBI
(" Runtime_Finalize;");
450 -- By default (real targets), finalization is done differently depending
451 -- on whether this is the main program or a library.
453 if not CodePeer_Mode
then
454 if not Bind_For_Library
then
455 WBI
(" s_stalib_adafinal;");
456 elsif Lib_Final_Built
then
457 WBI
(" finalize_library;");
462 -- Pragma Import C cannot be used on virtual targets, therefore call the
463 -- runtime finalization routine directly in CodePeer mode, where
464 -- imported functions are ignored.
467 WBI
(" System.Standard_Library.Adafinal;");
470 WBI
(" end " & Ada_Final_Name
.all & ";");
478 procedure Gen_Adainit
(Elab_Order
: Unit_Id_Array
) is
479 Main_Priority
: Int
renames ALIs
.Table
(ALIs
.First
).Main_Priority
;
480 Main_CPU
: Int
renames ALIs
.Table
(ALIs
.First
).Main_CPU
;
483 -- Declare the access-to-subprogram type used for initialization of
484 -- of __gnat_finalize_library_objects. This is declared at library
485 -- level for compatibility with the type used in System.Soft_Links.
486 -- The import of the soft link which performs library-level object
487 -- finalization does not work for CodePeer, so regular Ada is used in
488 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
489 -- tasks are non-terminating, so we do not want finalization.
491 if not Suppress_Standard_Library_On_Target
492 and then not CodePeer_Mode
493 and then not Configurable_Run_Time_On_Target
495 WBI
(" type No_Param_Proc is access procedure;");
499 WBI
(" procedure " & Ada_Init_Name
.all & " is");
501 -- In CodePeer mode, simplify adainit procedure by only calling
502 -- elaboration procedures.
504 if CodePeer_Mode
then
507 -- If the standard library is suppressed, then the only global variables
508 -- that might be needed (by the Ravenscar profile) are the priority and
509 -- the processor for the environment task.
511 elsif Suppress_Standard_Library_On_Target
then
512 if Main_Priority
/= No_Main_Priority
then
513 WBI
(" Main_Priority : Integer;");
514 WBI
(" pragma Import (C, Main_Priority," &
515 " ""__gl_main_priority"");");
519 if Main_CPU
/= No_Main_CPU
then
520 WBI
(" Main_CPU : Integer;");
521 WBI
(" pragma Import (C, Main_CPU," &
522 " ""__gl_main_cpu"");");
526 if System_Interrupts_Used
527 and then Partition_Elaboration_Policy_Specified
= 'S'
529 WBI
(" procedure Install_Restricted_Handlers_Sequential;");
530 WBI
(" pragma Import (C," &
531 "Install_Restricted_Handlers_Sequential," &
532 " ""__gnat_attach_all_handlers"");");
536 if System_Tasking_Restricted_Stages_Used
537 and then Partition_Elaboration_Policy_Specified
= 'S'
539 WBI
(" Partition_Elaboration_Policy : Character;");
540 WBI
(" pragma Import (C, Partition_Elaboration_Policy," &
541 " ""__gnat_partition_elaboration_policy"");");
543 WBI
(" procedure Activate_All_Tasks_Sequential;");
544 WBI
(" pragma Import (C, Activate_All_Tasks_Sequential," &
545 " ""__gnat_activate_all_tasks"");");
549 if System_BB_CPU_Primitives_Multiprocessors_Used
then
550 WBI
(" procedure Start_Slave_CPUs;");
551 WBI
(" pragma Import (C, Start_Slave_CPUs," &
552 " ""__gnat_start_slave_cpus"");");
557 if Main_Priority
/= No_Main_Priority
then
558 Set_String
(" Main_Priority := ");
559 Set_Int
(Main_Priority
);
561 Write_Statement_Buffer
;
564 if Main_CPU
/= No_Main_CPU
then
565 Set_String
(" Main_CPU := ");
568 Write_Statement_Buffer
;
571 if System_Tasking_Restricted_Stages_Used
572 and then Partition_Elaboration_Policy_Specified
= 'S'
574 Set_String
(" Partition_Elaboration_Policy := '");
575 Set_Char
(Partition_Elaboration_Policy_Specified
);
577 Write_Statement_Buffer
;
580 if Main_Priority
= No_Main_Priority
581 and then Main_CPU
= No_Main_CPU
582 and then not System_Tasking_Restricted_Stages_Used
587 -- Normal case (standard library not suppressed). Set all global values
588 -- used by the run time.
591 WBI
(" Main_Priority : Integer;");
592 WBI
(" pragma Import (C, Main_Priority, " &
593 """__gl_main_priority"");");
594 WBI
(" Time_Slice_Value : Integer;");
595 WBI
(" pragma Import (C, Time_Slice_Value, " &
596 """__gl_time_slice_val"");");
597 WBI
(" WC_Encoding : Character;");
598 WBI
(" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
599 WBI
(" Locking_Policy : Character;");
600 WBI
(" pragma Import (C, Locking_Policy, " &
601 """__gl_locking_policy"");");
602 WBI
(" Queuing_Policy : Character;");
603 WBI
(" pragma Import (C, Queuing_Policy, " &
604 """__gl_queuing_policy"");");
605 WBI
(" Task_Dispatching_Policy : Character;");
606 WBI
(" pragma Import (C, Task_Dispatching_Policy, " &
607 """__gl_task_dispatching_policy"");");
608 WBI
(" Priority_Specific_Dispatching : System.Address;");
609 WBI
(" pragma Import (C, Priority_Specific_Dispatching, " &
610 """__gl_priority_specific_dispatching"");");
611 WBI
(" Num_Specific_Dispatching : Integer;");
612 WBI
(" pragma Import (C, Num_Specific_Dispatching, " &
613 """__gl_num_specific_dispatching"");");
614 WBI
(" Main_CPU : Integer;");
615 WBI
(" pragma Import (C, Main_CPU, " &
616 """__gl_main_cpu"");");
618 WBI
(" Interrupt_States : System.Address;");
619 WBI
(" pragma Import (C, Interrupt_States, " &
620 """__gl_interrupt_states"");");
621 WBI
(" Num_Interrupt_States : Integer;");
622 WBI
(" pragma Import (C, Num_Interrupt_States, " &
623 """__gl_num_interrupt_states"");");
624 WBI
(" Unreserve_All_Interrupts : Integer;");
625 WBI
(" pragma Import (C, Unreserve_All_Interrupts, " &
626 """__gl_unreserve_all_interrupts"");");
628 if Exception_Tracebacks
or Exception_Tracebacks_Symbolic
then
629 WBI
(" Exception_Tracebacks : Integer;");
630 WBI
(" pragma Import (C, Exception_Tracebacks, " &
631 """__gl_exception_tracebacks"");");
633 if Exception_Tracebacks_Symbolic
then
634 WBI
(" Exception_Tracebacks_Symbolic : Integer;");
635 WBI
(" pragma Import (C, Exception_Tracebacks_Symbolic, " &
636 """__gl_exception_tracebacks_symbolic"");");
640 WBI
(" Detect_Blocking : Integer;");
641 WBI
(" pragma Import (C, Detect_Blocking, " &
642 """__gl_detect_blocking"");");
643 WBI
(" Default_Stack_Size : Integer;");
644 WBI
(" pragma Import (C, Default_Stack_Size, " &
645 """__gl_default_stack_size"");");
646 WBI
(" Leap_Seconds_Support : Integer;");
647 WBI
(" pragma Import (C, Leap_Seconds_Support, " &
648 """__gl_leap_seconds_support"");");
649 WBI
(" Bind_Env_Addr : System.Address;");
650 WBI
(" pragma Import (C, Bind_Env_Addr, " &
651 """__gl_bind_env_addr"");");
653 -- Import entry point for elaboration time signal handler
654 -- installation, and indication of if it's been called previously.
657 WBI
(" procedure Runtime_Initialize " &
658 "(Install_Handler : Integer);");
659 WBI
(" pragma Import (C, Runtime_Initialize, " &
660 """__gnat_runtime_initialize"");");
662 -- Import handlers attach procedure for sequential elaboration policy
664 if System_Interrupts_Used
665 and then Partition_Elaboration_Policy_Specified
= 'S'
667 WBI
(" procedure Install_Restricted_Handlers_Sequential;");
668 WBI
(" pragma Import (C," &
669 "Install_Restricted_Handlers_Sequential," &
670 " ""__gnat_attach_all_handlers"");");
674 -- Import task activation procedure for sequential elaboration
677 if System_Tasking_Restricted_Stages_Used
678 and then Partition_Elaboration_Policy_Specified
= 'S'
680 WBI
(" Partition_Elaboration_Policy : Character;");
681 WBI
(" pragma Import (C, Partition_Elaboration_Policy," &
682 " ""__gnat_partition_elaboration_policy"");");
684 WBI
(" procedure Activate_All_Tasks_Sequential;");
685 WBI
(" pragma Import (C, Activate_All_Tasks_Sequential," &
686 " ""__gnat_activate_all_tasks"");");
689 -- Import procedure to start slave cpus for bareboard runtime
691 if System_BB_CPU_Primitives_Multiprocessors_Used
then
692 WBI
(" procedure Start_Slave_CPUs;");
693 WBI
(" pragma Import (C, Start_Slave_CPUs," &
694 " ""__gnat_start_slave_cpus"");");
697 -- For restricted run-time libraries (ZFP and Ravenscar)
698 -- tasks are non-terminating, so we do not want finalization.
700 if not Configurable_Run_Time_On_Target
then
702 WBI
(" Finalize_Library_Objects : No_Param_Proc;");
703 WBI
(" pragma Import (C, Finalize_Library_Objects, " &
704 """__gnat_finalize_library_objects"");");
707 -- Initialize stack limit variable of the environment task if the
708 -- stack check method is stack limit and stack check is enabled.
710 if Stack_Check_Limits_On_Target
711 and then (Stack_Check_Default_On_Target
or Stack_Check_Switch_Set
)
714 WBI
(" procedure Initialize_Stack_Limit;");
715 WBI
(" pragma Import (C, Initialize_Stack_Limit, " &
716 """__gnat_initialize_stack_limit"");");
719 -- When dispatching domains are used then we need to signal it
720 -- before calling the main procedure.
722 if Dispatching_Domains_Used
then
723 WBI
(" procedure Freeze_Dispatching_Domains;");
724 WBI
(" pragma Import");
725 WBI
(" (Ada, Freeze_Dispatching_Domains, "
726 & """__gnat_freeze_dispatching_domains"");");
729 -- Start of processing for Adainit
732 WBI
(" if Is_Elaborated then");
735 WBI
(" Is_Elaborated := True;");
737 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
738 -- restriction No_Standard_Allocators_After_Elaboration is active.
740 if Cumulative_Restrictions
.Set
741 (No_Standard_Allocators_After_Elaboration
)
743 WBI
(" System.Elaboration_Allocators."
744 & "Mark_Start_Of_Elaboration;");
747 -- Generate assignments to initialize globals
749 Set_String
(" Main_Priority := ");
750 Set_Int
(Main_Priority
);
752 Write_Statement_Buffer
;
754 Set_String
(" Time_Slice_Value := ");
756 if Task_Dispatching_Policy_Specified
= 'F'
757 and then ALIs
.Table
(ALIs
.First
).Time_Slice_Value
= -1
761 Set_Int
(ALIs
.Table
(ALIs
.First
).Time_Slice_Value
);
765 Write_Statement_Buffer
;
767 Set_String
(" WC_Encoding := '");
768 Set_Char
(Get_WC_Encoding
);
771 Write_Statement_Buffer
;
773 Set_String
(" Locking_Policy := '");
774 Set_Char
(Locking_Policy_Specified
);
776 Write_Statement_Buffer
;
778 Set_String
(" Queuing_Policy := '");
779 Set_Char
(Queuing_Policy_Specified
);
781 Write_Statement_Buffer
;
783 Set_String
(" Task_Dispatching_Policy := '");
784 Set_Char
(Task_Dispatching_Policy_Specified
);
786 Write_Statement_Buffer
;
788 if System_Tasking_Restricted_Stages_Used
789 and then Partition_Elaboration_Policy_Specified
= 'S'
791 Set_String
(" Partition_Elaboration_Policy := '");
792 Set_Char
(Partition_Elaboration_Policy_Specified
);
794 Write_Statement_Buffer
;
799 WBI
(" Priority_Specific_Dispatching :=");
800 WBI
(" Local_Priority_Specific_Dispatching'Address;");
802 Set_String
(" Num_Specific_Dispatching := ");
803 Set_Int
(PSD_Pragma_Settings
.Last
+ 1);
805 Write_Statement_Buffer
;
807 Set_String
(" Main_CPU := ");
810 Write_Statement_Buffer
;
812 WBI
(" Interrupt_States := Local_Interrupt_States'Address;");
814 Set_String
(" Num_Interrupt_States := ");
815 Set_Int
(IS_Pragma_Settings
.Last
+ 1);
817 Write_Statement_Buffer
;
819 Set_String
(" Unreserve_All_Interrupts := ");
821 if Unreserve_All_Interrupts_Specified
then
828 Write_Statement_Buffer
;
830 if Exception_Tracebacks
or Exception_Tracebacks_Symbolic
then
831 WBI
(" Exception_Tracebacks := 1;");
833 if Exception_Tracebacks_Symbolic
then
834 WBI
(" Exception_Tracebacks_Symbolic := 1;");
838 Set_String
(" Detect_Blocking := ");
840 if Detect_Blocking
then
847 Write_Statement_Buffer
;
849 Set_String
(" Default_Stack_Size := ");
850 Set_Int
(Default_Stack_Size
);
852 Write_Statement_Buffer
;
854 Set_String
(" Leap_Seconds_Support := ");
856 if Leap_Seconds_Support
then
863 Write_Statement_Buffer
;
865 if Bind_Env_String_Built
then
866 WBI
(" Bind_Env_Addr := Bind_Env'Address;");
869 -- Generate call to Install_Handler
872 WBI
(" Runtime_Initialize (1);");
875 -- Generate call to set Initialize_Scalar values if active
877 if Initialize_Scalars_Used
then
879 Set_String
(" System.Scalar_Values.Initialize ('");
880 Set_Char
(Initialize_Scalars_Mode1
);
882 Set_Char
(Initialize_Scalars_Mode2
);
884 Write_Statement_Buffer
;
887 -- Generate assignment of default secondary stack size if set
889 if Sec_Stack_Used
and then Default_Sec_Stack_Size
/= -1 then
891 Set_String
(" System.Secondary_Stack.");
892 Set_String
("Default_Secondary_Stack_Size := ");
893 Set_Int
(Opt
.Default_Sec_Stack_Size
);
895 Write_Statement_Buffer
;
898 -- Initialize stack limit variable of the environment task if the stack
899 -- check method is stack limit and stack check is enabled.
901 if Stack_Check_Limits_On_Target
902 and then (Stack_Check_Default_On_Target
or Stack_Check_Switch_Set
)
905 WBI
(" Initialize_Stack_Limit;");
908 -- On CodePeer, the finalization of library objects is not relevant
910 if CodePeer_Mode
then
913 -- If this is the main program case, attach finalize_library to the soft
914 -- link. Do it only when not using a restricted run time, in which case
915 -- tasks are non-terminating, so we do not want library-level
918 elsif not Bind_For_Library
919 and then not Configurable_Run_Time_On_Target
920 and then not Suppress_Standard_Library_On_Target
924 if Lib_Final_Built
then
925 Set_String
(" Finalize_Library_Objects := ");
926 Set_String
("finalize_library'access;");
928 Set_String
(" Finalize_Library_Objects := null;");
931 Write_Statement_Buffer
;
934 -- Generate elaboration calls
936 if not CodePeer_Mode
then
940 Gen_Elab_Calls
(Elab_Order
);
942 if not CodePeer_Mode
then
944 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
945 -- restriction No_Standard_Allocators_After_Elaboration is active.
947 if Cumulative_Restrictions
.Set
948 (No_Standard_Allocators_After_Elaboration
)
951 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
954 -- From this point, no new dispatching domain can be created
956 if Dispatching_Domains_Used
then
957 WBI
(" Freeze_Dispatching_Domains;");
960 -- Sequential partition elaboration policy
962 if Partition_Elaboration_Policy_Specified
= 'S' then
963 if System_Interrupts_Used
then
964 WBI
(" Install_Restricted_Handlers_Sequential;");
967 if System_Tasking_Restricted_Stages_Used
then
968 WBI
(" Activate_All_Tasks_Sequential;");
972 if System_BB_CPU_Primitives_Multiprocessors_Used
then
973 WBI
(" Start_Slave_CPUs;");
977 WBI
(" end " & Ada_Init_Name
.all & ";");
981 -------------------------
982 -- Gen_Bind_Env_String --
983 -------------------------
985 procedure Gen_Bind_Env_String
is
986 procedure Write_Name_With_Len
(Nam
: Name_Id
);
987 -- Write Nam as a string literal, prefixed with one
988 -- character encoding Nam's length.
990 -------------------------
991 -- Write_Name_With_Len --
992 -------------------------
994 procedure Write_Name_With_Len
(Nam
: Name_Id
) is
996 Get_Name_String
(Nam
);
999 Store_String_Char
(Character'Val (Name_Len
));
1000 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
1002 Write_String_Table_Entry
(End_String
);
1003 end Write_Name_With_Len
;
1008 KN
: Name_Id
:= No_Name
;
1009 VN
: Name_Id
:= No_Name
;
1011 -- Start of processing for Gen_Bind_Env_String
1014 Bind_Environment
.Get_First
(KN
, VN
);
1016 if VN
= No_Name
then
1020 Set_Special_Output
(Write_Bind_Line
'Access);
1022 WBI
(" Bind_Env : aliased constant String :=");
1024 while VN
/= No_Name
loop
1025 Write_Str
(" " & Amp
& ' ');
1026 Write_Name_With_Len
(KN
);
1028 Write_Name_With_Len
(VN
);
1031 Bind_Environment
.Get_Next
(KN
, VN
);
1034 WBI
(" & ASCII.NUL;");
1036 Set_Special_Output
(null);
1038 Bind_Env_String_Built
:= True;
1039 end Gen_Bind_Env_String
;
1041 --------------------------
1042 -- Gen_CodePeer_Wrapper --
1043 --------------------------
1045 procedure Gen_CodePeer_Wrapper
is
1046 Callee_Name
: constant String := "Ada_Main_Program";
1049 if ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1050 WBI
(" procedure " & CodePeer_Wrapper_Name
& " is ");
1052 WBI
(" " & Callee_Name
& ";");
1055 WBI
(" function " & CodePeer_Wrapper_Name
& " return Integer is");
1057 WBI
(" return " & Callee_Name
& ";");
1060 WBI
(" end " & CodePeer_Wrapper_Name
& ";");
1062 end Gen_CodePeer_Wrapper
;
1064 --------------------
1065 -- Gen_Elab_Calls --
1066 --------------------
1068 procedure Gen_Elab_Calls
(Elab_Order
: Unit_Id_Array
) is
1069 Check_Elab_Flag
: Boolean;
1072 -- Loop through elaboration order entries
1074 for E
in Elab_Order
'Range loop
1076 Unum
: constant Unit_Id
:= Elab_Order
(E
);
1077 U
: Unit_Record
renames Units
.Table
(Unum
);
1079 Unum_Spec
: Unit_Id
;
1080 -- This is the unit number of the spec that corresponds to
1081 -- this entry. It is the same as Unum except when the body
1082 -- and spec are different and we are currently processing
1083 -- the body, in which case it is the spec (Unum + 1).
1086 if U
.Utype
= Is_Body
then
1087 Unum_Spec
:= Unum
+ 1;
1092 -- Nothing to do if predefined unit in no run time mode
1094 if No_Run_Time_Mode
and then Is_Predefined_File_Name
(U
.Sfile
) then
1097 -- Likewise if this is an interface to a stand alone library
1099 elsif U
.SAL_Interface
then
1102 -- Case of no elaboration code
1106 -- In CodePeer mode, we special case subprogram bodies which
1107 -- are handled in the 'else' part below, and lead to a call
1108 -- to <subp>'Elab_Subp_Body.
1110 and then (not CodePeer_Mode
1114 or else U
.Utype
= Is_Spec
1115 or else U
.Utype
= Is_Spec_Only
1116 or else U
.Unit_Kind
/= 's')
1118 -- In the case of a body with a separate spec, where the
1119 -- separate spec has an elaboration entity defined, this is
1120 -- where we increment the elaboration entity if one exists
1122 if U
.Utype
= Is_Body
1123 and then Units
.Table
(Unum_Spec
).Set_Elab_Entity
1124 and then not CodePeer_Mode
1127 Set_Unit_Number
(Unum_Spec
);
1128 Set_String
(" := E");
1129 Set_Unit_Number
(Unum_Spec
);
1130 Set_String
(" + 1;");
1131 Write_Statement_Buffer
;
1134 -- Here if elaboration code is present. If binding a library
1135 -- or if there is a non-Ada main subprogram then we generate:
1137 -- if uname_E = 0 then
1138 -- uname'elab_[spec|body];
1140 -- uname_E := uname_E + 1;
1142 -- Otherwise, elaboration routines are called unconditionally:
1144 -- uname'elab_[spec|body];
1145 -- uname_E := uname_E + 1;
1147 -- The uname_E increment is skipped if this is a separate spec,
1148 -- since it will be done when we process the body.
1150 -- In CodePeer mode, we do not generate any reference to xxx_E
1151 -- variables, only calls to 'Elab* subprograms.
1154 -- Check incompatibilities with No_Multiple_Elaboration
1156 if not CodePeer_Mode
1157 and then Cumulative_Restrictions
.Set
(No_Multiple_Elaboration
)
1159 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1161 if Force_Checking_Of_Elaboration_Flags
then
1163 ("-F (force elaboration checks) switch not allowed "
1164 & "with restriction No_Multiple_Elaboration active");
1166 -- Interfacing of libraries not allowed
1168 elsif Interface_Library_Unit
then
1170 ("binding of interfaced libraries not allowed "
1171 & "with restriction No_Multiple_Elaboration active");
1173 -- Non-Ada main program not allowed
1175 elsif not Bind_Main_Program
then
1177 ("non-Ada main program not allowed "
1178 & "with restriction No_Multiple_Elaboration active");
1182 -- OK, see if we need to test elaboration flag
1185 Units
.Table
(Unum_Spec
).Set_Elab_Entity
1186 and then not CodePeer_Mode
1187 and then (Force_Checking_Of_Elaboration_Flags
1188 or Interface_Library_Unit
1189 or not Bind_Main_Program
);
1191 if Check_Elab_Flag
then
1192 Set_String
(" if E");
1193 Set_Unit_Number
(Unum_Spec
);
1194 Set_String
(" = 0 then");
1195 Write_Statement_Buffer
;
1200 Get_Decoded_Name_String_With_Brackets
(U
.Uname
);
1202 if Name_Buffer
(Name_Len
) = 's' then
1203 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 8) :=
1205 Name_Len
:= Name_Len
+ 8;
1207 -- Special case in CodePeer mode for subprogram bodies
1208 -- which correspond to CodePeer 'Elab_Subp_Body special
1211 elsif U
.Unit_Kind
= 's' and CodePeer_Mode
then
1212 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 13) :=
1214 Name_Len
:= Name_Len
+ 13;
1217 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 8) :=
1219 Name_Len
:= Name_Len
+ 8;
1222 Set_Casing
(U
.Icasing
);
1225 Write_Statement_Buffer
;
1227 if Check_Elab_Flag
then
1231 if U
.Utype
/= Is_Spec
1232 and then not CodePeer_Mode
1233 and then Units
.Table
(Unum_Spec
).Set_Elab_Entity
1236 Set_Unit_Number
(Unum_Spec
);
1237 Set_String
(" := E");
1238 Set_Unit_Number
(Unum_Spec
);
1239 Set_String
(" + 1;");
1240 Write_Statement_Buffer
;
1247 ------------------------
1248 -- Gen_Elab_Externals --
1249 ------------------------
1251 procedure Gen_Elab_Externals
(Elab_Order
: Unit_Id_Array
) is
1253 if CodePeer_Mode
then
1257 for E
in Elab_Order
'Range loop
1259 Unum
: constant Unit_Id
:= Elab_Order
(E
);
1260 U
: Unit_Record
renames Units
.Table
(Unum
);
1263 -- Check for Elab_Entity to be set for this unit
1265 if U
.Set_Elab_Entity
1267 -- Don't generate reference for stand alone library
1269 and then not U
.SAL_Interface
1271 -- Don't generate reference for predefined file in No_Run_Time
1272 -- mode, since we don't include the object files in this case
1276 and then Is_Predefined_File_Name
(U
.Sfile
))
1280 Set_Unit_Number
(Unum
);
1281 Set_String
(" : Short_Integer; pragma Import (Ada, E");
1282 Set_Unit_Number
(Unum
);
1283 Set_String
(", """);
1284 Get_Name_String
(U
.Uname
);
1286 Set_String
("_E"");");
1287 Write_Statement_Buffer
;
1293 end Gen_Elab_Externals
;
1295 --------------------
1296 -- Gen_Elab_Order --
1297 --------------------
1299 procedure Gen_Elab_Order
(Elab_Order
: Unit_Id_Array
) is
1301 WBI
(" -- BEGIN ELABORATION ORDER");
1303 for J
in Elab_Order
'Range loop
1304 Set_String
(" -- ");
1305 Get_Name_String
(Units
.Table
(Elab_Order
(J
)).Uname
);
1307 Write_Statement_Buffer
;
1310 WBI
(" -- END ELABORATION ORDER");
1314 --------------------------
1315 -- Gen_Finalize_Library --
1316 --------------------------
1318 procedure Gen_Finalize_Library
(Elab_Order
: Unit_Id_Array
) is
1319 procedure Gen_Header
;
1320 -- Generate the header of the finalization routine
1326 procedure Gen_Header
is
1328 WBI
(" procedure finalize_library is");
1336 Uspec
: Unit_Record
;
1339 -- Start of processing for Gen_Finalize_Library
1342 if CodePeer_Mode
then
1346 for E
in reverse Elab_Order
'Range loop
1347 Unum
:= Elab_Order
(E
);
1348 U
:= Units
.Table
(Unum
);
1350 -- Dealing with package bodies is a little complicated. In such
1351 -- cases we must retrieve the package spec since it contains the
1352 -- spec of the body finalizer.
1354 if U
.Utype
= Is_Body
then
1356 Uspec
:= Units
.Table
(Unum
);
1361 Get_Name_String
(Uspec
.Uname
);
1363 -- We are only interested in non-generic packages
1365 if U
.Unit_Kind
/= 'p' or else U
.Is_Generic
then
1368 -- That aren't an interface to a stand alone library
1370 elsif U
.SAL_Interface
then
1373 -- Case of no finalization
1375 elsif not U
.Has_Finalizer
then
1377 -- The only case in which we have to do something is if this
1378 -- is a body, with a separate spec, where the separate spec
1379 -- has a finalizer. In that case, this is where we decrement
1380 -- the elaboration entity.
1382 if U
.Utype
= Is_Body
and then Uspec
.Has_Finalizer
then
1383 if not Lib_Final_Built
then
1385 Lib_Final_Built
:= True;
1389 Set_Unit_Number
(Unum
);
1390 Set_String
(" := E");
1391 Set_Unit_Number
(Unum
);
1392 Set_String
(" - 1;");
1393 Write_Statement_Buffer
;
1397 if not Lib_Final_Built
then
1399 Lib_Final_Built
:= True;
1404 -- procedure F<Count>;
1406 Set_String
(" declare");
1407 Write_Statement_Buffer
;
1409 Set_String
(" procedure F");
1412 Write_Statement_Buffer
;
1415 -- pragma Import (Ada, F<Count>,
1416 -- "xx__yy__finalize_[body|spec]");
1418 Set_String
(" pragma Import (Ada, F");
1420 Set_String
(", """);
1422 -- Perform name construction
1425 Set_String
("__finalize_");
1427 -- Package spec processing
1429 if U
.Utype
= Is_Spec
1430 or else U
.Utype
= Is_Spec_Only
1432 Set_String
("spec");
1434 -- Package body processing
1437 Set_String
("body");
1440 Set_String
(""");");
1441 Write_Statement_Buffer
;
1443 -- If binding a library or if there is a non-Ada main subprogram
1444 -- then we generate:
1447 -- uname_E := uname_E - 1;
1448 -- if uname_E = 0 then
1453 -- Otherwise, finalization routines are called unconditionally:
1456 -- uname_E := uname_E - 1;
1460 -- The uname_E decrement is skipped if this is a separate spec,
1461 -- since it will be done when we process the body.
1465 if U
.Utype
/= Is_Spec
then
1467 Set_Unit_Number
(Unum
);
1468 Set_String
(" := E");
1469 Set_Unit_Number
(Unum
);
1470 Set_String
(" - 1;");
1471 Write_Statement_Buffer
;
1474 if Interface_Library_Unit
or not Bind_Main_Program
then
1475 Set_String
(" if E");
1476 Set_Unit_Number
(Unum
);
1477 Set_String
(" = 0 then");
1478 Write_Statement_Buffer
;
1485 Write_Statement_Buffer
;
1487 if Interface_Library_Unit
or not Bind_Main_Program
then
1497 if Lib_Final_Built
then
1499 -- It is possible that the finalization of a library-level object
1500 -- raised an exception. In that case import the actual exception
1501 -- and the routine necessary to raise it.
1504 WBI
(" procedure Reraise_Library_Exception_If_Any;");
1506 Set_String
(" pragma Import (Ada, ");
1507 Set_String
("Reraise_Library_Exception_If_Any, ");
1508 Set_String
("""__gnat_reraise_library_exception_if_any"");");
1509 Write_Statement_Buffer
;
1512 WBI
(" Reraise_Library_Exception_If_Any;");
1514 WBI
(" end finalize_library;");
1517 end Gen_Finalize_Library
;
1523 procedure Gen_Main
is
1525 if not No_Main_Subprogram
then
1527 -- To call the main program, we declare it using a pragma Import
1528 -- Ada with the right link name.
1530 -- It might seem more obvious to "with" the main program, and call
1531 -- it in the normal Ada manner. We do not do this for three
1534 -- 1. It is more efficient not to recompile the main program
1535 -- 2. We are not entitled to assume the source is accessible
1536 -- 3. We don't know what options to use to compile it
1538 -- It is really reason 3 that is most critical (indeed we used
1539 -- to generate the "with", but several regression tests failed).
1541 if ALIs
.Table
(ALIs
.First
).Main_Program
= Func
then
1542 WBI
(" function Ada_Main_Program return Integer;");
1544 WBI
(" procedure Ada_Main_Program;");
1547 Set_String
(" pragma Import (Ada, Ada_Main_Program, """);
1548 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
1549 Set_Main_Program_Name
;
1550 Set_String
(""");");
1552 Write_Statement_Buffer
;
1555 -- For CodePeer, declare a wrapper for the user-defined main program
1557 if CodePeer_Mode
then
1558 Gen_CodePeer_Wrapper
;
1562 if Exit_Status_Supported_On_Target
then
1563 Set_String
(" function ");
1565 Set_String
(" procedure ");
1568 Set_String
(Get_Main_Name
);
1570 if Command_Line_Args_On_Target
then
1571 Write_Statement_Buffer
;
1572 WBI
(" (argc : Integer;");
1573 WBI
(" argv : System.Address;");
1574 WBI
(" envp : System.Address)");
1576 if Exit_Status_Supported_On_Target
then
1577 WBI
(" return Integer");
1583 if Exit_Status_Supported_On_Target
then
1584 Set_String
(" return Integer is");
1589 Write_Statement_Buffer
;
1592 if Opt
.Default_Exit_Status
/= 0
1593 and then Bind_Main_Program
1594 and then not Configurable_Run_Time_Mode
1596 WBI
(" procedure Set_Exit_Status (Status : Integer);");
1597 WBI
(" pragma Import (C, Set_Exit_Status, " &
1598 """__gnat_set_exit_status"");");
1602 -- Initialize and Finalize
1604 if not CodePeer_Mode
1605 and then not Cumulative_Restrictions
.Set
(No_Finalization
)
1607 WBI
(" procedure Initialize (Addr : System.Address);");
1608 WBI
(" pragma Import (C, Initialize, ""__gnat_initialize"");");
1610 WBI
(" procedure Finalize;");
1611 WBI
(" pragma Import (C, Finalize, ""__gnat_finalize"");");
1614 -- If we want to analyze the stack, we must import corresponding symbols
1616 if Dynamic_Stack_Measurement
then
1618 WBI
(" procedure Output_Results;");
1619 WBI
(" pragma Import (C, Output_Results, " &
1620 """__gnat_stack_usage_output_results"");");
1624 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1625 WBI
(" pragma Import (C, Initialize_Stack_Analysis, " &
1626 """__gnat_stack_usage_initialize"");");
1629 -- Deal with declarations for main program case
1631 if not No_Main_Subprogram
then
1632 if ALIs
.Table
(ALIs
.First
).Main_Program
= Func
then
1633 WBI
(" Result : Integer;");
1637 if Bind_Main_Program
1638 and not Suppress_Standard_Library_On_Target
1639 and not CodePeer_Mode
1641 WBI
(" SEH : aliased array (1 .. 2) of Integer;");
1646 -- Generate a reference to Ada_Main_Program_Name. This symbol is not
1647 -- referenced elsewhere in the generated program, but is needed by
1648 -- the debugger (that's why it is generated in the first place). The
1649 -- reference stops Ada_Main_Program_Name from being optimized away by
1650 -- smart linkers, such as the AiX linker.
1652 -- Because this variable is unused, we make this variable "aliased"
1653 -- with a pragma Volatile in order to tell the compiler to preserve
1654 -- this variable at any level of optimization.
1656 if Bind_Main_Program
and not CodePeer_Mode
then
1657 WBI
(" Ensure_Reference : aliased System.Address := " &
1658 "Ada_Main_Program_Name'Address;");
1659 WBI
(" pragma Volatile (Ensure_Reference);");
1665 -- Acquire command line arguments if present on target
1667 if CodePeer_Mode
then
1670 elsif Command_Line_Args_On_Target
then
1671 WBI
(" gnat_argc := argc;");
1672 WBI
(" gnat_argv := argv;");
1673 WBI
(" gnat_envp := envp;");
1676 -- If configurable run time and no command line args, then nothing needs
1677 -- to be done since the gnat_argc/argv/envp variables are suppressed in
1680 elsif Configurable_Run_Time_On_Target
then
1683 -- Otherwise set dummy values (to be filled in by some other unit?)
1686 WBI
(" gnat_argc := 0;");
1687 WBI
(" gnat_argv := System.Null_Address;");
1688 WBI
(" gnat_envp := System.Null_Address;");
1691 if Opt
.Default_Exit_Status
/= 0
1692 and then Bind_Main_Program
1693 and then not Configurable_Run_Time_Mode
1695 Set_String
(" Set_Exit_Status (");
1696 Set_Int
(Opt
.Default_Exit_Status
);
1698 Write_Statement_Buffer
;
1701 if Dynamic_Stack_Measurement
then
1702 Set_String
(" Initialize_Stack_Analysis (");
1703 Set_Int
(Dynamic_Stack_Measurement_Array_Size
);
1705 Write_Statement_Buffer
;
1708 if not Cumulative_Restrictions
.Set
(No_Finalization
)
1709 and then not CodePeer_Mode
1711 if not No_Main_Subprogram
1712 and then Bind_Main_Program
1713 and then not Suppress_Standard_Library_On_Target
1715 WBI
(" Initialize (SEH'Address);");
1717 WBI
(" Initialize (System.Null_Address);");
1721 WBI
(" " & Ada_Init_Name
.all & ";");
1723 if not No_Main_Subprogram
then
1724 if CodePeer_Mode
then
1725 if ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1726 WBI
(" " & CodePeer_Wrapper_Name
& ";");
1728 WBI
(" Result := " & CodePeer_Wrapper_Name
& ";");
1731 elsif ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1732 WBI
(" Ada_Main_Program;");
1735 WBI
(" Result := Ada_Main_Program;");
1739 -- Adafinal call is skipped if no finalization
1741 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
1745 -- Prints the result of static stack analysis
1747 if Dynamic_Stack_Measurement
then
1748 WBI
(" Output_Results;");
1751 -- Finalize is only called if we have a run time
1753 if not Cumulative_Restrictions
.Set
(No_Finalization
)
1754 and then not CodePeer_Mode
1761 if Exit_Status_Supported_On_Target
then
1762 if No_Main_Subprogram
1763 or else ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
1765 WBI
(" return (gnat_exit_status);");
1767 WBI
(" return (Result);");
1775 ------------------------------
1776 -- Gen_Object_Files_Options --
1777 ------------------------------
1779 procedure Gen_Object_Files_Options
(Elab_Order
: Unit_Id_Array
) is
1781 -- This keeps track of the position in the sorted set of entries in the
1782 -- Linker_Options table of where the first entry from an internal file
1785 Linker_Option_List_Started
: Boolean := False;
1786 -- Set to True when "LINKER OPTION LIST" is displayed
1788 procedure Write_Linker_Option
;
1789 -- Write binder info linker option
1791 -------------------------
1792 -- Write_Linker_Option --
1793 -------------------------
1795 procedure Write_Linker_Option
is
1800 -- Loop through string, breaking at null's
1803 while Start
< Name_Len
loop
1805 -- Find null ending this section
1808 while Name_Buffer
(Stop
) /= ASCII
.NUL
1809 and then Stop
<= Name_Len
loop
1813 -- Process section if non-null
1815 if Stop
> Start
then
1816 if Output_Linker_Option_List
then
1817 if not Zero_Formatting
then
1818 if not Linker_Option_List_Started
then
1819 Linker_Option_List_Started
:= True;
1821 Write_Str
(" LINKER OPTION LIST");
1829 Write_Str
(Name_Buffer
(Start
.. Stop
- 1));
1832 WBI
(" -- " & Name_Buffer
(Start
.. Stop
- 1));
1837 end Write_Linker_Option
;
1839 -- Start of processing for Gen_Object_Files_Options
1842 WBI
("-- BEGIN Object file/option list");
1844 if Object_List_Filename
/= null then
1845 Set_List_File
(Object_List_Filename
.all);
1848 for E
in Elab_Order
'Range loop
1850 -- If not spec that has an associated body, then generate a comment
1851 -- giving the name of the corresponding object file.
1853 if not Units
.Table
(Elab_Order
(E
)).SAL_Interface
1854 and then Units
.Table
(Elab_Order
(E
)).Utype
/= Is_Spec
1858 (Units
.Table
(Elab_Order
(E
)).My_ALI
).Ofile_Full_Name
);
1860 -- If the presence of an object file is necessary or if it exists,
1863 if not Hostparm
.Exclude_Missing_Objects
1865 System
.OS_Lib
.Is_Regular_File
(Name_Buffer
(1 .. Name_Len
))
1867 WBI
(" -- " & Name_Buffer
(1 .. Name_Len
));
1869 if Output_Object_List
then
1870 Write_Str
(Name_Buffer
(1 .. Name_Len
));
1877 if Object_List_Filename
/= null then
1881 -- Add a "-Ldir" for each directory in the object path
1883 for J
in 1 .. Nb_Dir_In_Obj_Search_Path
loop
1885 Dir
: constant String_Ptr
:= Dir_In_Obj_Search_Path
(J
);
1889 Add_Str_To_Name_Buffer
("-L");
1890 Add_Str_To_Name_Buffer
(Dir
.all);
1891 Write_Linker_Option
;
1895 if not (Opt
.No_Run_Time_Mode
or Opt
.No_Stdlib
) then
1898 if Opt
.Shared_Libgnat
then
1899 Add_Str_To_Name_Buffer
("-shared");
1901 Add_Str_To_Name_Buffer
("-static");
1904 -- Write directly to avoid inclusion in -K output as -static and
1905 -- -shared are not usually specified linker options.
1907 WBI
(" -- " & Name_Buffer
(1 .. Name_Len
));
1910 -- Sort linker options
1912 -- This sort accomplishes two important purposes:
1914 -- a) All application files are sorted to the front, and all GNAT
1915 -- internal files are sorted to the end. This results in a well
1916 -- defined dividing line between the two sets of files, for the
1917 -- purpose of inserting certain standard library references into
1918 -- the linker arguments list.
1920 -- b) Given two different units, we sort the linker options so that
1921 -- those from a unit earlier in the elaboration order comes later
1922 -- in the list. This is a heuristic designed to create a more
1923 -- friendly order of linker options when the operations appear in
1924 -- separate units. The idea is that if unit A must be elaborated
1925 -- before unit B, then it is more likely that B references
1926 -- libraries included by A, than vice versa, so we want libraries
1927 -- included by A to come after libraries included by B.
1929 -- These two criteria are implemented by function Lt_Linker_Option. Note
1930 -- that a special case of b) is that specs are elaborated before bodies,
1931 -- so linker options from specs come after linker options for bodies,
1932 -- and again, the assumption is that libraries used by the body are more
1933 -- likely to reference libraries used by the spec, than vice versa.
1936 (Linker_Options
.Last
,
1937 Move_Linker_Option
'Access,
1938 Lt_Linker_Option
'Access);
1940 -- Write user linker options, i.e. the set of linker options that come
1941 -- from all files other than GNAT internal files, Lgnat is left set to
1942 -- point to the first entry from a GNAT internal file, or past the end
1943 -- of the entries if there are no internal files.
1945 Lgnat
:= Linker_Options
.Last
+ 1;
1947 for J
in 1 .. Linker_Options
.Last
loop
1948 if not Linker_Options
.Table
(J
).Internal_File
then
1949 Get_Name_String
(Linker_Options
.Table
(J
).Name
);
1950 Write_Linker_Option
;
1957 -- Now we insert standard linker options that must appear after the
1958 -- entries from user files, and before the entries from GNAT run-time
1959 -- files. The reason for this decision is that libraries referenced
1960 -- by internal routines may reference these standard library entries.
1962 -- Note that we do not insert anything when pragma No_Run_Time has
1963 -- been specified or when the standard libraries are not to be used,
1964 -- otherwise on some platforms, we may get duplicate symbols when
1965 -- linking (not clear if this is still the case, but it is harmless).
1967 if not (Opt
.No_Run_Time_Mode
or else Opt
.No_Stdlib
) then
1971 if Opt
.Shared_Libgnat
then
1972 Add_Str_To_Name_Buffer
(Shared_Lib
("gnarl"));
1974 Add_Str_To_Name_Buffer
("-lgnarl");
1977 Write_Linker_Option
;
1982 if Opt
.Shared_Libgnat
then
1983 Add_Str_To_Name_Buffer
(Shared_Lib
("gnat"));
1985 Add_Str_To_Name_Buffer
("-lgnat");
1988 Write_Linker_Option
;
1991 -- Write linker options from all internal files
1993 for J
in Lgnat
.. Linker_Options
.Last
loop
1994 Get_Name_String
(Linker_Options
.Table
(J
).Name
);
1995 Write_Linker_Option
;
1998 if Output_Linker_Option_List
and then not Zero_Formatting
then
2002 WBI
("-- END Object file/option list ");
2003 end Gen_Object_Files_Options
;
2005 ---------------------
2006 -- Gen_Output_File --
2007 ---------------------
2009 procedure Gen_Output_File
2011 Elab_Order
: Unit_Id_Array
)
2014 -- Acquire settings for Interrupt_State pragmas
2016 Set_IS_Pragma_Table
;
2018 -- Acquire settings for Priority_Specific_Dispatching pragma
2020 Set_PSD_Pragma_Table
;
2022 -- Override time slice value if -T switch is set
2024 if Time_Slice_Set
then
2025 ALIs
.Table
(ALIs
.First
).Time_Slice_Value
:= Opt
.Time_Slice_Value
;
2028 -- Count number of elaboration calls
2030 for E
in Elab_Order
'Range loop
2031 if Units
.Table
(Elab_Order
(E
)).No_Elab
then
2034 Num_Elab_Calls
:= Num_Elab_Calls
+ 1;
2038 -- Generate output file in appropriate language
2040 Gen_Output_File_Ada
(Filename
, Elab_Order
);
2041 end Gen_Output_File
;
2043 -------------------------
2044 -- Gen_Output_File_Ada --
2045 -------------------------
2047 procedure Gen_Output_File_Ada
2048 (Filename
: String; Elab_Order
: Unit_Id_Array
)
2050 Ada_Main
: constant String := Get_Ada_Main_Name
;
2051 -- Name to be used for generated Ada main program. See the body of
2052 -- function Get_Ada_Main_Name for details on the form of the name.
2054 Needs_Library_Finalization
: constant Boolean :=
2055 not Configurable_Run_Time_On_Target
2056 and then Has_Finalizer
(Elab_Order
);
2057 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2058 -- non-terminating, so we do not want finalization.
2061 -- Name of generated bind file (spec)
2064 -- Name of generated bind file (body)
2067 -- Create spec first
2069 Create_Binder_Output
(Filename
, 's', Bfiles
);
2071 -- We always compile the binder file in Ada 95 mode so that we properly
2072 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2073 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2075 WBI
("pragma Ada_95;");
2076 WBI
("pragma Warnings (Off);");
2078 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2079 -- then we need to make sure that the binder program is compiled with
2080 -- the same restriction, so that no exception tables are generated.
2082 if Cumulative_Restrictions
.Set
(No_Exception_Handlers
) then
2083 WBI
("pragma Restrictions (No_Exception_Handlers);");
2086 -- Same processing for Restrictions (No_Exception_Propagation)
2088 if Cumulative_Restrictions
.Set
(No_Exception_Propagation
) then
2089 WBI
("pragma Restrictions (No_Exception_Propagation);");
2092 -- Same processing for pragma No_Run_Time
2094 if No_Run_Time_Mode
then
2095 WBI
("pragma No_Run_Time;");
2098 -- Generate with of System so we can reference System.Address
2100 WBI
("with System;");
2102 -- Generate with of System.Initialize_Scalars if active
2104 if Initialize_Scalars_Used
then
2105 WBI
("with System.Scalar_Values;");
2108 -- Generate with of System.Secondary_Stack if active
2110 if Sec_Stack_Used
and then Default_Sec_Stack_Size
/= -1 then
2111 WBI
("with System.Secondary_Stack;");
2114 Resolve_Binder_Options
(Elab_Order
);
2116 -- Generate standard with's
2118 if not Suppress_Standard_Library_On_Target
then
2119 if CodePeer_Mode
then
2120 WBI
("with System.Standard_Library;");
2124 WBI
("package " & Ada_Main
& " is");
2126 -- Main program case
2128 if Bind_Main_Program
then
2129 -- Generate argc/argv stuff unless suppressed
2131 if Command_Line_Args_On_Target
2132 or not Configurable_Run_Time_On_Target
2135 WBI
(" gnat_argc : Integer;");
2136 WBI
(" gnat_argv : System.Address;");
2137 WBI
(" gnat_envp : System.Address;");
2139 -- If the standard library is not suppressed, these variables
2140 -- are in the run-time data area for easy run time access.
2142 if not Suppress_Standard_Library_On_Target
then
2144 WBI
(" pragma Import (C, gnat_argc);");
2145 WBI
(" pragma Import (C, gnat_argv);");
2146 WBI
(" pragma Import (C, gnat_envp);");
2150 -- Define exit status. Again in normal mode, this is in the
2151 -- run-time library, and is initialized there, but in the
2152 -- configurable runtime case, the variable is declared and
2153 -- initialized in this file.
2157 if Configurable_Run_Time_Mode
then
2158 if Exit_Status_Supported_On_Target
then
2159 WBI
(" gnat_exit_status : Integer := 0;");
2163 WBI
(" gnat_exit_status : Integer;");
2164 WBI
(" pragma Import (C, gnat_exit_status);");
2167 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2168 -- the main program. Otherwise, it can lead under some circumstances
2169 -- to a symbol duplication during the link (for instance when a C
2170 -- program uses two Ada libraries). Also zero terminate the string
2171 -- so that its end can be found reliably at run time.
2174 WBI
(" GNAT_Version : constant String :=");
2175 WBI
(" """ & Ver_Prefix
&
2176 Gnat_Version_String
&
2178 WBI
(" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2181 Set_String
(" Ada_Main_Program_Name : constant String := """);
2182 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2184 Set_Main_Program_Name
;
2185 Set_String
(""" & ASCII.NUL;");
2187 Write_Statement_Buffer
;
2190 (" pragma Export (C, Ada_Main_Program_Name, " &
2191 """__gnat_ada_main_program_name"");");
2195 WBI
(" procedure " & Ada_Init_Name
.all & ";");
2196 WBI
(" pragma Export (C, " & Ada_Init_Name
.all & ", """ &
2197 Ada_Init_Name
.all & """);");
2199 -- If -a has been specified use pragma Linker_Constructor for the init
2200 -- procedure and pragma Linker_Destructor for the final procedure.
2202 if Use_Pragma_Linker_Constructor
then
2203 WBI
(" pragma Linker_Constructor (" & Ada_Init_Name
.all & ");");
2206 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
2208 WBI
(" procedure " & Ada_Final_Name
.all & ";");
2209 WBI
(" pragma Export (C, " & Ada_Final_Name
.all & ", """ &
2210 Ada_Final_Name
.all & """);");
2212 if Use_Pragma_Linker_Constructor
then
2213 WBI
(" pragma Linker_Destructor (" & Ada_Final_Name
.all & ");");
2217 if Bind_Main_Program
then
2221 if Exit_Status_Supported_On_Target
then
2222 Set_String
(" function ");
2224 Set_String
(" procedure ");
2227 Set_String
(Get_Main_Name
);
2229 -- Generate argument list if present
2231 if Command_Line_Args_On_Target
then
2232 Write_Statement_Buffer
;
2233 WBI
(" (argc : Integer;");
2234 WBI
(" argv : System.Address;");
2236 (" envp : System.Address)");
2238 if Exit_Status_Supported_On_Target
then
2239 Write_Statement_Buffer
;
2240 WBI
(" return Integer;");
2242 Write_Statement_Buffer
(";");
2246 if Exit_Status_Supported_On_Target
then
2247 Write_Statement_Buffer
(" return Integer;");
2249 Write_Statement_Buffer
(";");
2253 WBI
(" pragma Export (C, " & Get_Main_Name
& ", """ &
2254 Get_Main_Name
& """);");
2258 Gen_Elab_Order
(Elab_Order
);
2263 WBI
("end " & Ada_Main
& ";");
2264 Close_Binder_Output
;
2266 -- Prepare to write body
2268 Create_Binder_Output
(Filename
, 'b', Bfileb
);
2270 -- We always compile the binder file in Ada 95 mode so that we properly
2271 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2272 -- of the Ada 2005/2012 constructs are needed by the binder file.
2274 WBI
("pragma Ada_95;");
2275 WBI
("pragma Warnings (Off);");
2277 -- Output Source_File_Name pragmas which look like
2279 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2280 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2282 -- where sss/bbb are the spec/body file names respectively
2284 Get_Name_String
(Bfiles
);
2285 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 3) := """);";
2287 WBI
("pragma Source_File_Name (" &
2289 ", Spec_File_Name => """ &
2290 Name_Buffer
(1 .. Name_Len
+ 3));
2292 Get_Name_String
(Bfileb
);
2293 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 3) := """);";
2295 WBI
("pragma Source_File_Name (" &
2297 ", Body_File_Name => """ &
2298 Name_Buffer
(1 .. Name_Len
+ 3));
2300 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2301 -- versions of the compiler which have overflow checks on by default.
2302 -- We do not want overflow checking enabled for the increments of the
2303 -- elaboration variables (since this can cause an unwanted reference to
2304 -- the last chance exception handler for limited run-times).
2306 WBI
("pragma Suppress (Overflow_Check);");
2308 -- Generate with of System.Restrictions to initialize
2309 -- Run_Time_Restrictions.
2311 if System_Restrictions_Used
2312 and not Suppress_Standard_Library_On_Target
2315 WBI
("with System.Restrictions;");
2318 -- Generate with of Ada.Exceptions if needs library finalization
2320 if Needs_Library_Finalization
then
2321 WBI
("with Ada.Exceptions;");
2324 -- Generate with of System.Elaboration_Allocators if the restriction
2325 -- No_Standard_Allocators_After_Elaboration was present.
2327 if Cumulative_Restrictions
.Set
2328 (No_Standard_Allocators_After_Elaboration
)
2330 WBI
("with System.Elaboration_Allocators;");
2333 -- Generate start of package body
2336 WBI
("package body " & Ada_Main
& " is");
2339 -- Generate externals for elaboration entities
2341 Gen_Elab_Externals
(Elab_Order
);
2343 if not CodePeer_Mode
then
2344 if not Suppress_Standard_Library_On_Target
then
2346 -- Generate Priority_Specific_Dispatching pragma string
2349 (" Local_Priority_Specific_Dispatching : " &
2350 "constant String := """);
2352 for J
in 0 .. PSD_Pragma_Settings
.Last
loop
2353 Set_Char
(PSD_Pragma_Settings
.Table
(J
));
2357 Write_Statement_Buffer
;
2359 -- Generate Interrupt_State pragma string
2361 Set_String
(" Local_Interrupt_States : constant String := """);
2363 for J
in 0 .. IS_Pragma_Settings
.Last
loop
2364 Set_Char
(IS_Pragma_Settings
.Table
(J
));
2368 Write_Statement_Buffer
;
2372 if not Suppress_Standard_Library_On_Target
then
2374 -- The B.1(39) implementation advice says that the adainit
2375 -- and adafinal routines should be idempotent. Generate a flag to
2376 -- ensure that. This is not needed if we are suppressing the
2377 -- standard library since it would never be referenced.
2379 WBI
(" Is_Elaborated : Boolean := False;");
2381 -- Generate bind environment string
2383 Gen_Bind_Env_String
;
2389 -- Generate the adafinal routine unless there is no finalization to do
2391 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
2392 if Needs_Library_Finalization
then
2393 Gen_Finalize_Library
(Elab_Order
);
2399 Gen_Adainit
(Elab_Order
);
2401 if Bind_Main_Program
then
2405 -- Output object file list and the Ada body is complete
2407 Gen_Object_Files_Options
(Elab_Order
);
2410 WBI
("end " & Ada_Main
& ";");
2412 Close_Binder_Output
;
2413 end Gen_Output_File_Ada
;
2415 ----------------------
2416 -- Gen_Restrictions --
2417 ----------------------
2419 procedure Gen_Restrictions
is
2423 if Suppress_Standard_Library_On_Target
2424 or not System_Restrictions_Used
2429 WBI
(" System.Restrictions.Run_Time_Restrictions :=");
2435 for J
in Cumulative_Restrictions
.Set
'Range loop
2436 Set_Boolean
(Cumulative_Restrictions
.Set
(J
));
2440 if J
/= Cumulative_Restrictions
.Set
'Last and then Count
= 8 then
2441 Write_Statement_Buffer
;
2447 Set_String_Replace
("),");
2448 Write_Statement_Buffer
;
2449 Set_String
(" Value => (");
2451 for J
in Cumulative_Restrictions
.Value
'Range loop
2452 Set_Int
(Int
(Cumulative_Restrictions
.Value
(J
)));
2456 Set_String_Replace
("),");
2457 Write_Statement_Buffer
;
2458 WBI
(" Violated =>");
2462 for J
in Cumulative_Restrictions
.Violated
'Range loop
2463 Set_Boolean
(Cumulative_Restrictions
.Violated
(J
));
2467 if J
/= Cumulative_Restrictions
.Set
'Last and then Count
= 8 then
2468 Write_Statement_Buffer
;
2474 Set_String_Replace
("),");
2475 Write_Statement_Buffer
;
2476 Set_String
(" Count => (");
2478 for J
in Cumulative_Restrictions
.Count
'Range loop
2479 Set_Int
(Int
(Cumulative_Restrictions
.Count
(J
)));
2483 Set_String_Replace
("),");
2484 Write_Statement_Buffer
;
2485 Set_String
(" Unknown => (");
2487 for J
in Cumulative_Restrictions
.Unknown
'Range loop
2488 Set_Boolean
(Cumulative_Restrictions
.Unknown
(J
));
2492 Set_String_Replace
("))");
2494 Write_Statement_Buffer
;
2495 end Gen_Restrictions
;
2501 -- This routine generates lines such as:
2503 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2504 -- pragma Export (C, unnnnn, unam);
2506 -- for each unit, where unam is the unit name suffixed by either B or S for
2507 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2508 -- the version number, and nnnnn is a 5-digits serial number.
2510 procedure Gen_Versions
is
2511 Ubuf
: String (1 .. 6) := "u00000";
2513 procedure Increment_Ubuf
;
2514 -- Little procedure to increment the serial number
2516 --------------------
2517 -- Increment_Ubuf --
2518 --------------------
2520 procedure Increment_Ubuf
is
2522 for J
in reverse Ubuf
'Range loop
2523 Ubuf
(J
) := Character'Succ (Ubuf
(J
));
2524 exit when Ubuf
(J
) <= '9';
2529 -- Start of processing for Gen_Versions
2534 WBI
(" type Version_32 is mod 2 ** 32;");
2535 for U
in Units
.First
.. Units
.Last
loop
2536 if not Units
.Table
(U
).SAL_Interface
2537 and then (not Bind_For_Library
2538 or else Units
.Table
(U
).Directly_Scanned
)
2541 WBI
(" " & Ubuf
& " : constant Version_32 := 16#" &
2542 Units
.Table
(U
).Version
& "#;");
2543 Set_String
(" pragma Export (C, ");
2545 Set_String
(", """);
2547 Get_Name_String
(Units
.Table
(U
).Uname
);
2549 for K
in 1 .. Name_Len
loop
2550 if Name_Buffer
(K
) = '.' then
2554 elsif Name_Buffer
(K
) = '%' then
2558 Set_Char
(Name_Buffer
(K
));
2562 if Name_Buffer
(Name_Len
) = 's' then
2568 Set_String
(""");");
2569 Write_Statement_Buffer
;
2574 ------------------------
2575 -- Get_Main_Unit_Name --
2576 ------------------------
2578 function Get_Main_Unit_Name
(S
: String) return String is
2579 Result
: String := S
;
2582 for J
in S
'Range loop
2583 if Result
(J
) = '.' then
2589 end Get_Main_Unit_Name
;
2591 -----------------------
2592 -- Get_Ada_Main_Name --
2593 -----------------------
2595 function Get_Ada_Main_Name
return String is
2596 Suffix
: constant String := "_00";
2597 Name
: String (1 .. Opt
.Ada_Main_Name
.all'Length + Suffix
'Length) :=
2598 Opt
.Ada_Main_Name
.all & Suffix
;
2602 -- For CodePeer, we want reproducible names (independent of other mains
2603 -- that may or may not be present) that don't collide when analyzing
2604 -- multiple mains and which are easily recognizable as "ada_main" names.
2606 if CodePeer_Mode
then
2607 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2611 Get_Main_Unit_Name
(Name_Buffer
(1 .. Name_Len
- 2));
2614 -- This loop tries the following possibilities in order
2620 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2621 -- it is set to 'ada_main'.
2623 for J
in 0 .. 99 loop
2625 Nlen
:= Name
'Length - Suffix
'Length;
2627 Nlen
:= Name
'Length;
2628 Name
(Name
'Last) := Character'Val (J
mod 10 + Character'Pos ('0'));
2629 Name
(Name
'Last - 1) :=
2630 Character'Val (J
/ 10 + Character'Pos ('0'));
2633 for K
in ALIs
.First
.. ALIs
.Last
loop
2634 for L
in ALIs
.Table
(K
).First_Unit
.. ALIs
.Table
(K
).Last_Unit
loop
2636 -- Get unit name, removing %b or %e at end
2638 Get_Name_String
(Units
.Table
(L
).Uname
);
2639 Name_Len
:= Name_Len
- 2;
2641 if Name_Buffer
(1 .. Name_Len
) = Name
(1 .. Nlen
) then
2647 return Name
(1 .. Nlen
);
2653 -- If we fall through, just use a peculiar unlikely name
2655 return ("Qwertyuiop");
2656 end Get_Ada_Main_Name
;
2662 function Get_Main_Name
return String is
2664 -- Explicit name given with -M switch
2666 if Bind_Alternate_Main_Name
then
2667 return Alternate_Main_Name
.all;
2669 -- Case of main program name to be used directly
2671 elsif Use_Ada_Main_Program_Name_On_Target
then
2673 -- Get main program name
2675 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2677 -- If this is a child name, return only the name of the child, since
2678 -- we can't have dots in a nested program name. Note that we do not
2679 -- include the %b at the end of the unit name.
2681 for J
in reverse 1 .. Name_Len
- 2 loop
2682 if J
= 1 or else Name_Buffer
(J
- 1) = '.' then
2683 return Name_Buffer
(J
.. Name_Len
- 2);
2687 raise Program_Error
; -- impossible exit
2689 -- Case where "main" is to be used as default
2696 ---------------------
2697 -- Get_WC_Encoding --
2698 ---------------------
2700 function Get_WC_Encoding
return Character is
2702 -- If encoding method specified by -W switch, then return it
2704 if Wide_Character_Encoding_Method_Specified
then
2705 return WC_Encoding_Letters
(Wide_Character_Encoding_Method
);
2707 -- If no main program, and not specified, set brackets, we really have
2708 -- no better choice. If some other encoding is required when there is
2709 -- no main, it must be set explicitly using -Wx.
2711 -- Note: if the ALI file always passed the wide character encoding of
2712 -- every file, then we could use the encoding of the initial specified
2713 -- file, but this information is passed only for potential main
2714 -- programs. We could fix this sometime, but it is a very minor point
2715 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
2716 -- is no main program).
2718 elsif No_Main_Subprogram
then
2721 -- Otherwise if there is a main program, take encoding from it
2724 return ALIs
.Table
(ALIs
.First
).WC_Encoding
;
2726 end Get_WC_Encoding
;
2732 function Has_Finalizer
(Elab_Order
: Unit_Id_Array
) return Boolean is
2737 for E
in reverse Elab_Order
'Range loop
2738 Unum
:= Elab_Order
(E
);
2739 U
:= Units
.Table
(Unum
);
2741 -- We are only interested in non-generic packages
2743 if U
.Unit_Kind
= 'p'
2744 and then U
.Has_Finalizer
2745 and then not U
.Is_Generic
2746 and then not U
.No_Elab
2759 function Hash
(Nam
: Name_Id
) return Header_Num
is
2761 return Int
(Nam
- Names_Low_Bound
) rem Header_Num
'Last;
2764 ----------------------
2765 -- Lt_Linker_Option --
2766 ----------------------
2768 function Lt_Linker_Option
(Op1
: Natural; Op2
: Natural) return Boolean is
2770 -- Sort internal files last
2772 if Linker_Options
.Table
(Op1
).Internal_File
2774 Linker_Options
.Table
(Op2
).Internal_File
2776 -- Note: following test uses False < True
2778 return Linker_Options
.Table
(Op1
).Internal_File
2780 Linker_Options
.Table
(Op2
).Internal_File
;
2782 -- If both internal or both non-internal, sort according to the
2783 -- elaboration position. A unit that is elaborated later should come
2784 -- earlier in the linker options list.
2787 return Units
.Table
(Linker_Options
.Table
(Op1
).Unit
).Elab_Position
2789 Units
.Table
(Linker_Options
.Table
(Op2
).Unit
).Elab_Position
;
2791 end Lt_Linker_Option
;
2793 ------------------------
2794 -- Move_Linker_Option --
2795 ------------------------
2797 procedure Move_Linker_Option
(From
: Natural; To
: Natural) is
2799 Linker_Options
.Table
(To
) := Linker_Options
.Table
(From
);
2800 end Move_Linker_Option
;
2802 ----------------------------
2803 -- Resolve_Binder_Options --
2804 ----------------------------
2806 procedure Resolve_Binder_Options
(Elab_Order
: Unit_Id_Array
) is
2807 procedure Check_Package
(Var
: in out Boolean; Name
: String);
2808 -- Set Var to true iff the current identifier in Namet is Name. Do
2809 -- nothing if it doesn't match. This procedure is just a helper to
2810 -- avoid explicitly dealing with length.
2816 procedure Check_Package
(Var
: in out Boolean; Name
: String) is
2818 if Name_Len
= Name
'Length
2819 and then Name_Buffer
(1 .. Name_Len
) = Name
2825 -- Start of processing for Resolve_Binder_Options
2828 for E
in Elab_Order
'Range loop
2829 Get_Name_String
(Units
.Table
(Elab_Order
(E
)).Uname
);
2831 -- This is not a perfect approach, but is the current protocol
2832 -- between the run-time and the binder to indicate that tasking is
2833 -- used: System.OS_Interface should always be used by any tasking
2836 Check_Package
(With_GNARL
, "system.os_interface%s");
2838 -- Ditto for the use of restricted tasking
2841 (System_Tasking_Restricted_Stages_Used
,
2842 "system.tasking.restricted.stages%s");
2844 -- Ditto for the use of interrupts
2846 Check_Package
(System_Interrupts_Used
, "system.interrupts%s");
2848 -- Ditto for the use of dispatching domains
2851 (Dispatching_Domains_Used
,
2852 "system.multiprocessors.dispatching_domains%s");
2854 -- Ditto for the use of restrictions
2856 Check_Package
(System_Restrictions_Used
, "system.restrictions%s");
2858 -- Ditto for use of an SMP bareboard runtime
2860 Check_Package
(System_BB_CPU_Primitives_Multiprocessors_Used
,
2861 "system.bb.cpu_primitives.multiprocessors%s");
2864 end Resolve_Binder_Options
;
2870 procedure Set_Bind_Env
(Key
, Value
: String) is
2872 -- The lengths of Key and Value are stored as single bytes
2874 if Key
'Length > 255 then
2875 Osint
.Fail
("bind environment key """ & Key
& """ too long");
2878 if Value
'Length > 255 then
2879 Osint
.Fail
("bind environment value """ & Value
& """ too long");
2882 Bind_Environment
.Set
(Name_Find
(Key
), Name_Find
(Value
));
2889 procedure Set_Boolean
(B
: Boolean) is
2890 False_Str
: constant String := "False";
2891 True_Str
: constant String := "True";
2895 Statement_Buffer
(Stm_Last
+ 1 .. Stm_Last
+ True_Str
'Length) :=
2897 Stm_Last
:= Stm_Last
+ True_Str
'Length;
2899 Statement_Buffer
(Stm_Last
+ 1 .. Stm_Last
+ False_Str
'Length) :=
2901 Stm_Last
:= Stm_Last
+ False_Str
'Length;
2909 procedure Set_Char
(C
: Character) is
2911 Stm_Last
:= Stm_Last
+ 1;
2912 Statement_Buffer
(Stm_Last
) := C
;
2919 procedure Set_Int
(N
: Int
) is
2930 Stm_Last
:= Stm_Last
+ 1;
2931 Statement_Buffer
(Stm_Last
) :=
2932 Character'Val (N
mod 10 + Character'Pos ('0'));
2936 -------------------------
2937 -- Set_IS_Pragma_Table --
2938 -------------------------
2940 procedure Set_IS_Pragma_Table
is
2942 for F
in ALIs
.First
.. ALIs
.Last
loop
2943 for K
in ALIs
.Table
(F
).First_Interrupt_State
..
2944 ALIs
.Table
(F
).Last_Interrupt_State
2947 Inum
: constant Int
:=
2948 Interrupt_States
.Table
(K
).Interrupt_Id
;
2949 Stat
: constant Character :=
2950 Interrupt_States
.Table
(K
).Interrupt_State
;
2953 while IS_Pragma_Settings
.Last
< Inum
loop
2954 IS_Pragma_Settings
.Append
('n');
2957 IS_Pragma_Settings
.Table
(Inum
) := Stat
;
2961 end Set_IS_Pragma_Table
;
2963 ---------------------------
2964 -- Set_Main_Program_Name --
2965 ---------------------------
2967 procedure Set_Main_Program_Name
is
2969 -- Note that name has %b on the end which we ignore
2971 -- First we output the initial _ada_ since we know that the main program
2972 -- is a library level subprogram.
2974 Set_String
("_ada_");
2976 -- Copy name, changing dots to double underscores
2978 for J
in 1 .. Name_Len
- 2 loop
2979 if Name_Buffer
(J
) = '.' then
2982 Set_Char
(Name_Buffer
(J
));
2985 end Set_Main_Program_Name
;
2987 ---------------------
2988 -- Set_Name_Buffer --
2989 ---------------------
2991 procedure Set_Name_Buffer
is
2993 for J
in 1 .. Name_Len
loop
2994 Set_Char
(Name_Buffer
(J
));
2996 end Set_Name_Buffer
;
2998 -------------------------
2999 -- Set_PSD_Pragma_Table --
3000 -------------------------
3002 procedure Set_PSD_Pragma_Table
is
3004 for F
in ALIs
.First
.. ALIs
.Last
loop
3005 for K
in ALIs
.Table
(F
).First_Specific_Dispatching
..
3006 ALIs
.Table
(F
).Last_Specific_Dispatching
3009 DTK
: Specific_Dispatching_Record
3010 renames Specific_Dispatching
.Table
(K
);
3013 while PSD_Pragma_Settings
.Last
< DTK
.Last_Priority
loop
3014 PSD_Pragma_Settings
.Append
('F');
3017 for Prio
in DTK
.First_Priority
.. DTK
.Last_Priority
loop
3018 PSD_Pragma_Settings
.Table
(Prio
) := DTK
.Dispatching_Policy
;
3023 end Set_PSD_Pragma_Table
;
3029 procedure Set_String
(S
: String) is
3031 Statement_Buffer
(Stm_Last
+ 1 .. Stm_Last
+ S
'Length) := S
;
3032 Stm_Last
:= Stm_Last
+ S
'Length;
3035 ------------------------
3036 -- Set_String_Replace --
3037 ------------------------
3039 procedure Set_String_Replace
(S
: String) is
3041 Statement_Buffer
(Stm_Last
- S
'Length + 1 .. Stm_Last
) := S
;
3042 end Set_String_Replace
;
3048 procedure Set_Unit_Name
is
3050 for J
in 1 .. Name_Len
- 2 loop
3051 if Name_Buffer
(J
) = '.' then
3054 Set_Char
(Name_Buffer
(J
));
3059 ---------------------
3060 -- Set_Unit_Number --
3061 ---------------------
3063 procedure Set_Unit_Number
(U
: Unit_Id
) is
3064 Num_Units
: constant Nat
:= Nat
(Units
.Last
) - Nat
(Unit_Id
'First);
3065 Unum
: constant Nat
:= Nat
(U
) - Nat
(Unit_Id
'First);
3068 if Num_Units
>= 10 and then Unum
< 10 then
3072 if Num_Units
>= 100 and then Unum
< 100 then
3077 end Set_Unit_Number
;
3079 ---------------------
3080 -- Write_Bind_Line --
3081 ---------------------
3083 procedure Write_Bind_Line
(S
: String) is
3085 -- Need to strip trailing LF from S
3087 WBI
(S
(S
'First .. S
'Last - 1));
3088 end Write_Bind_Line
;
3090 ----------------------------
3091 -- Write_Statement_Buffer --
3092 ----------------------------
3094 procedure Write_Statement_Buffer
is
3096 WBI
(Statement_Buffer
(1 .. Stm_Last
));
3098 end Write_Statement_Buffer
;
3100 procedure Write_Statement_Buffer
(S
: String) is
3103 Write_Statement_Buffer
;
3104 end Write_Statement_Buffer
;