1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Casing
; use Casing
;
28 with Fname
; use Fname
;
29 with Gnatvsn
; use Gnatvsn
;
31 with Namet
; use Namet
;
33 with Osint
; use Osint
;
34 with Osint
.B
; use Osint
.B
;
35 with Output
; use Output
;
36 with Rident
; use Rident
;
37 with Stringt
; use Stringt
;
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 System_Version_Control_Used
: Boolean := False;
98 -- Flag indicating whether unit System.Version_Control is in the closure.
99 -- This unit is implicitly withed by the compiler when Version or
100 -- Body_Version attributes are used. If the package is not in the closure,
101 -- the version definitions can be removed.
103 Lib_Final_Built
: Boolean := False;
104 -- Flag indicating whether the finalize_library rountine has been built
106 Bind_Env_String_Built
: Boolean := False;
107 -- Flag indicating whether a bind environment string has been built
109 CodePeer_Wrapper_Name
: constant String := "call_main_subprogram";
110 -- For CodePeer, introduce a wrapper subprogram which calls the
111 -- user-defined main subprogram.
113 ----------------------------------
114 -- Interface_State Pragma Table --
115 ----------------------------------
117 -- This table assembles the interface state pragma information from
118 -- all the units in the partition. Note that Bcheck has already checked
119 -- that the information is consistent across units. The entries
120 -- in this table are n/u/r/s for not set/user/runtime/system.
122 package IS_Pragma_Settings
is new Table
.Table
123 (Table_Component_Type
=> Character,
124 Table_Index_Type
=> Int
,
125 Table_Low_Bound
=> 0,
126 Table_Initial
=> 100,
127 Table_Increment
=> 200,
128 Table_Name
=> "IS_Pragma_Settings");
130 -- This table assembles the Priority_Specific_Dispatching pragma
131 -- information from all the units in the partition. Note that Bcheck has
132 -- already checked that the information is consistent across units.
133 -- The entries in this table are the upper case first character of the
134 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
136 package PSD_Pragma_Settings
is new Table
.Table
137 (Table_Component_Type
=> Character,
138 Table_Index_Type
=> Int
,
139 Table_Low_Bound
=> 0,
140 Table_Initial
=> 100,
141 Table_Increment
=> 200,
142 Table_Name
=> "PSD_Pragma_Settings");
144 ----------------------------
145 -- Bind_Environment Table --
146 ----------------------------
148 subtype Header_Num
is Int
range 0 .. 36;
150 function Hash
(Nam
: Name_Id
) return Header_Num
;
152 package Bind_Environment
is new GNAT
.HTable
.Simple_HTable
153 (Header_Num
=> Header_Num
,
155 No_Element
=> No_Name
,
160 ----------------------
161 -- Run-Time Globals --
162 ----------------------
164 -- This section documents the global variables that are set from the
165 -- generated binder file.
167 -- Main_Priority : Integer;
168 -- Time_Slice_Value : Integer;
169 -- Heap_Size : Natural;
170 -- WC_Encoding : Character;
171 -- Locking_Policy : Character;
172 -- Queuing_Policy : Character;
173 -- Task_Dispatching_Policy : Character;
174 -- Priority_Specific_Dispatching : System.Address;
175 -- Num_Specific_Dispatching : Integer;
176 -- Restrictions : System.Address;
177 -- Interrupt_States : System.Address;
178 -- Num_Interrupt_States : Integer;
179 -- Unreserve_All_Interrupts : Integer;
180 -- Exception_Tracebacks : Integer;
181 -- Exception_Tracebacks_Symbolic : Integer;
182 -- Detect_Blocking : Integer;
183 -- Default_Stack_Size : Integer;
184 -- Leap_Seconds_Support : Integer;
185 -- Main_CPU : Integer;
187 -- Main_Priority is the priority value set by pragma Priority in the main
188 -- program. If no such pragma is present, the value is -1.
190 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
191 -- main program, or by the use of a -Tnnn parameter for the binder (if both
192 -- are present, the binder value overrides). The value is in milliseconds.
193 -- A value of zero indicates that time slicing should be suppressed. If no
194 -- pragma is present, and no -T switch was used, the value is -1.
196 -- WC_Encoding shows the wide character encoding method used for the main
197 -- program. This is one of the encoding letters defined in
198 -- System.WCh_Con.WC_Encoding_Letters.
200 -- Locking_Policy is a space if no locking policy was specified for the
201 -- partition. If a locking policy was specified, the value is the upper
202 -- case first character of the locking policy name, for example, 'C' for
205 -- Queuing_Policy is a space if no queuing policy was specified for the
206 -- partition. If a queuing policy was specified, the value is the upper
207 -- case first character of the queuing policy name for example, 'F' for
210 -- Task_Dispatching_Policy is a space if no task dispatching policy was
211 -- specified for the partition. If a task dispatching policy was specified,
212 -- the value is the upper case first character of the policy name, e.g. 'F'
213 -- for FIFO_Within_Priorities.
215 -- Priority_Specific_Dispatching is the address of a string used to store
216 -- the task dispatching policy specified for the different priorities in
217 -- the partition. The length of this string is determined by the last
218 -- priority for which such a pragma applies (the string will be a null
219 -- string if no specific dispatching policies were used). If pragma were
220 -- present, the entries apply to the priorities in sequence from the first
221 -- priority. The value stored is the upper case first character of the
222 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
223 -- for those priority ranges not specified.
225 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
226 -- string. It will be set to zero if no Priority_Specific_Dispatching
227 -- pragmas are present.
229 -- Restrictions is the address of a null-terminated string specifying the
230 -- restrictions information for the partition. The format is identical to
231 -- that of the parameter string found on R lines in ali files (see Lib.Writ
232 -- spec in lib-writ.ads for full details). The difference is that in this
233 -- context the values are the cumulative ones for the entire partition.
235 -- Interrupt_States is the address of a string used to specify the
236 -- cumulative results of Interrupt_State pragmas used in the partition.
237 -- The length of this string is determined by the last interrupt for which
238 -- such a pragma is given (the string will be a null string if no pragmas
239 -- were used). If pragma were present the entries apply to the interrupts
240 -- in sequence from the first interrupt, and are set to one of four
241 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
242 -- time, 's' for system, see description of Interrupt_State pragma for
245 -- Num_Interrupt_States is the length of the Interrupt_States string. It
246 -- will be set to zero if no Interrupt_State pragmas are present.
248 -- Unreserve_All_Interrupts is set to one if at least one unit in the
249 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
251 -- Exception_Tracebacks is set to one if the -Ea or -E parameter was
252 -- present in the bind and to zero otherwise. Note that on some targets
253 -- exception tracebacks are provided by default, so a value of zero for
254 -- this parameter does not necessarily mean no trace backs are available.
256 -- Exception_Tracebacks_Symbolic is set to one if the -Es parameter was
257 -- present in the bind and to zero otherwise.
259 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
260 -- not. A value of zero indicates that the pragma is not present, while a
261 -- value of 1 signals its presence in the partition.
263 -- Default_Stack_Size is the default stack size used when creating an Ada
264 -- task with no explicit Storage_Size clause.
266 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
267 -- disabled. A value of zero indicates that leap seconds are turned "off",
268 -- while a value of one signifies "on" status.
270 -- Main_CPU is the processor set by pragma CPU in the main program. If no
271 -- such pragma is present, the value is -1.
273 procedure WBI
(Info
: String) renames Osint
.B
.Write_Binder_Info
;
274 -- Convenient shorthand used throughout
276 -----------------------
277 -- Local Subprograms --
278 -----------------------
280 procedure Gen_Adainit
(Elab_Order
: Unit_Id_Array
);
281 -- Generates the Adainit procedure
283 procedure Gen_Adafinal
;
284 -- Generate the Adafinal procedure
286 procedure Gen_Bind_Env_String
;
287 -- Generate the bind environment buffer
289 procedure Gen_CodePeer_Wrapper
;
290 -- For CodePeer, generate wrapper which calls user-defined main subprogram
292 procedure Gen_Elab_Calls
(Elab_Order
: Unit_Id_Array
);
293 -- Generate sequence of elaboration calls
295 procedure Gen_Elab_Externals
(Elab_Order
: Unit_Id_Array
);
296 -- Generate sequence of external declarations for elaboration
298 procedure Gen_Elab_Order
(Elab_Order
: Unit_Id_Array
);
299 -- Generate comments showing elaboration order chosen
301 procedure Gen_Finalize_Library
(Elab_Order
: Unit_Id_Array
);
302 -- Generate a sequence of finalization calls to elaborated packages
305 -- Generate procedure main
307 procedure Gen_Object_Files_Options
(Elab_Order
: Unit_Id_Array
);
308 -- Output comments containing a list of the full names of the object
309 -- files to be linked and the list of linker options supplied by
310 -- Linker_Options pragmas in the source.
312 procedure Gen_Output_File_Ada
314 Elab_Order
: Unit_Id_Array
);
315 -- Generate Ada output file
317 procedure Gen_Restrictions
;
318 -- Generate initialization of restrictions variable
320 procedure Gen_Versions
;
321 -- Output series of definitions for unit versions
323 function Get_Ada_Main_Name
return String;
324 -- This function is used for the Ada main output to compute a usable name
325 -- for the generated main program. The normal main program name is
326 -- Ada_Main, but this won't work if the user has a unit with this name.
327 -- This function tries Ada_Main first, and if there is such a clash, then
328 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
330 function Get_Main_Unit_Name
(S
: String) return String;
331 -- Return the main unit name corresponding to S by replacing '.' with '_'
333 function Get_Main_Name
return String;
334 -- This function is used in the main output case to compute the correct
335 -- external main program. It is "main" by default, unless the flag
336 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
337 -- of the Ada main name without the "_ada". This default can be overridden
338 -- explicitly using the -Mname binder switch.
340 function Get_WC_Encoding
return Character;
341 -- Return wide character encoding method to set as WC_Encoding in output.
342 -- If -W has been used, returns the specified encoding, otherwise returns
343 -- the encoding method used for the main program source. If there is no
344 -- main program source (-z switch used), returns brackets ('b').
346 function Has_Finalizer
(Elab_Order
: Unit_Id_Array
) return Boolean;
347 -- Determine whether the current unit has at least one library-level
350 function Lt_Linker_Option
(Op1
: Natural; Op2
: Natural) return Boolean;
351 -- Compare linker options, when sorting, first according to
352 -- Is_Internal_File (internal files come later) and then by
353 -- elaboration order position (latest to earliest).
355 procedure Move_Linker_Option
(From
: Natural; To
: Natural);
356 -- Move routine for sorting linker options
358 procedure Resolve_Binder_Options
(Elab_Order
: Unit_Id_Array
);
359 -- Set the value of With_GNARL
361 procedure Set_Char
(C
: Character);
362 -- Set given character in Statement_Buffer at the Stm_Last + 1 position
363 -- and increment Stm_Last by one to reflect the stored character.
365 procedure Set_Int
(N
: Int
);
366 -- Set given value in decimal in Statement_Buffer with no spaces starting
367 -- at the Stm_Last + 1 position, and updating Stm_Last past the value. A
368 -- minus sign is output for a negative value.
370 procedure Set_Boolean
(B
: Boolean);
371 -- Set given boolean value in Statement_Buffer at the Stm_Last + 1 position
372 -- and update Stm_Last past the value.
374 procedure Set_IS_Pragma_Table
;
375 -- Initializes contents of IS_Pragma_Settings table from ALI table
377 procedure Set_Main_Program_Name
;
378 -- Given the main program name in Name_Buffer (length in Name_Len) generate
379 -- the name of the routine to be used in the call. The name is generated
380 -- starting at Stm_Last + 1, and Stm_Last is updated past it.
382 procedure Set_Name_Buffer
;
383 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
385 procedure Set_PSD_Pragma_Table
;
386 -- Initializes contents of PSD_Pragma_Settings table from ALI table
388 procedure Set_String
(S
: String);
389 -- Sets characters of given string in Statement_Buffer, starting at the
390 -- Stm_Last + 1 position, and updating last past the string value.
392 procedure Set_String_Replace
(S
: String);
393 -- Replaces the last S'Length characters in the Statement_Buffer with the
394 -- characters of S. The caller must ensure that these characters do in fact
395 -- exist in the Statement_Buffer.
397 procedure Set_Unit_Name
;
398 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
399 -- starting at the Stm_Last + 1 position and update Stm_Last past the
400 -- value. Each dot (.) will be qualified into double underscores (__).
402 procedure Set_Unit_Number
(U
: Unit_Id
);
403 -- Sets unit number (first unit is 1, leading zeroes output to line up all
404 -- output unit numbers nicely as required by the value, and by the total
407 procedure Write_Statement_Buffer
;
408 -- Write out contents of statement buffer up to Stm_Last, and reset
411 procedure Write_Statement_Buffer
(S
: String);
412 -- First writes its argument (using Set_String (S)), then writes out the
413 -- contents of statement buffer up to Stm_Last, and resets Stm_Last to 0.
415 procedure Write_Bind_Line
(S
: String);
416 -- Write S (an LF-terminated string) to the binder file (for use with
417 -- Set_Special_Output).
423 procedure Gen_Adafinal
is
425 WBI
(" procedure " & Ada_Final_Name
.all & " is");
427 -- Call s_stalib_adafinal to await termination of tasks and so on. We
428 -- want to do this if there is a main program, either in Ada or in some
429 -- other language. (Note that Bind_Main_Program is True for Ada mains,
430 -- but False for mains in other languages.) We do not want to do this if
431 -- we're binding a library.
433 if not Bind_For_Library
and not CodePeer_Mode
then
434 WBI
(" procedure s_stalib_adafinal;");
435 Set_String
(" pragma Import (C, s_stalib_adafinal, ");
436 Set_String
("""system__standard_library__adafinal"");");
437 Write_Statement_Buffer
;
441 WBI
(" procedure Runtime_Finalize;");
442 WBI
(" pragma Import (C, Runtime_Finalize, " &
443 """__gnat_runtime_finalize"");");
447 if not CodePeer_Mode
then
448 WBI
(" if not Is_Elaborated then");
451 WBI
(" Is_Elaborated := False;");
454 WBI
(" Runtime_Finalize;");
456 -- By default (real targets), finalization is done differently depending
457 -- on whether this is the main program or a library.
459 if not CodePeer_Mode
then
460 if not Bind_For_Library
then
461 WBI
(" s_stalib_adafinal;");
462 elsif Lib_Final_Built
then
463 WBI
(" finalize_library;");
468 -- Pragma Import C cannot be used on virtual targets, therefore call the
469 -- runtime finalization routine directly in CodePeer mode, where
470 -- imported functions are ignored.
473 WBI
(" System.Standard_Library.Adafinal;");
476 WBI
(" end " & Ada_Final_Name
.all & ";");
484 procedure Gen_Adainit
(Elab_Order
: Unit_Id_Array
) is
485 Main_Priority
: Int
renames ALIs
.Table
(ALIs
.First
).Main_Priority
;
486 Main_CPU
: Int
renames ALIs
.Table
(ALIs
.First
).Main_CPU
;
489 -- Declare the access-to-subprogram type used for initialization of
490 -- of __gnat_finalize_library_objects. This is declared at library
491 -- level for compatibility with the type used in System.Soft_Links.
492 -- The import of the soft link which performs library-level object
493 -- finalization does not work for CodePeer, so regular Ada is used in
494 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
495 -- tasks are non-terminating, so we do not want finalization.
497 if not Suppress_Standard_Library_On_Target
498 and then not CodePeer_Mode
499 and then not Configurable_Run_Time_On_Target
501 WBI
(" type No_Param_Proc is access procedure;");
505 WBI
(" procedure " & Ada_Init_Name
.all & " is");
507 -- In CodePeer mode, simplify adainit procedure by only calling
508 -- elaboration procedures.
510 if CodePeer_Mode
then
513 -- If the standard library is suppressed, then the only global variables
514 -- that might be needed (by the Ravenscar profile) are the priority and
515 -- the processor for the environment task.
517 elsif Suppress_Standard_Library_On_Target
then
518 if Main_Priority
/= No_Main_Priority
then
519 WBI
(" Main_Priority : Integer;");
520 WBI
(" pragma Import (C, Main_Priority," &
521 " ""__gl_main_priority"");");
525 if Main_CPU
/= No_Main_CPU
then
526 WBI
(" Main_CPU : Integer;");
527 WBI
(" pragma Import (C, Main_CPU," &
528 " ""__gl_main_cpu"");");
532 if System_Interrupts_Used
533 and then Partition_Elaboration_Policy_Specified
= 'S'
535 WBI
(" procedure Install_Restricted_Handlers_Sequential;");
536 WBI
(" pragma Import (C," &
537 "Install_Restricted_Handlers_Sequential," &
538 " ""__gnat_attach_all_handlers"");");
542 if System_Tasking_Restricted_Stages_Used
543 and then Partition_Elaboration_Policy_Specified
= 'S'
545 WBI
(" Partition_Elaboration_Policy : Character;");
546 WBI
(" pragma Import (C, Partition_Elaboration_Policy," &
547 " ""__gnat_partition_elaboration_policy"");");
549 WBI
(" procedure Activate_All_Tasks_Sequential;");
550 WBI
(" pragma Import (C, Activate_All_Tasks_Sequential," &
551 " ""__gnat_activate_all_tasks"");");
555 if System_BB_CPU_Primitives_Multiprocessors_Used
then
556 WBI
(" procedure Start_Slave_CPUs;");
557 WBI
(" pragma Import (C, Start_Slave_CPUs," &
558 " ""__gnat_start_slave_cpus"");");
563 if Main_Priority
/= No_Main_Priority
then
564 Set_String
(" Main_Priority := ");
565 Set_Int
(Main_Priority
);
567 Write_Statement_Buffer
;
570 if Main_CPU
/= No_Main_CPU
then
571 Set_String
(" Main_CPU := ");
574 Write_Statement_Buffer
;
577 if System_Tasking_Restricted_Stages_Used
578 and then Partition_Elaboration_Policy_Specified
= 'S'
580 Set_String
(" Partition_Elaboration_Policy := '");
581 Set_Char
(Partition_Elaboration_Policy_Specified
);
583 Write_Statement_Buffer
;
586 if Main_Priority
= No_Main_Priority
587 and then Main_CPU
= No_Main_CPU
588 and then not System_Tasking_Restricted_Stages_Used
593 -- Normal case (standard library not suppressed). Set all global values
594 -- used by the run time.
597 WBI
(" Main_Priority : Integer;");
598 WBI
(" pragma Import (C, Main_Priority, " &
599 """__gl_main_priority"");");
600 WBI
(" Time_Slice_Value : Integer;");
601 WBI
(" pragma Import (C, Time_Slice_Value, " &
602 """__gl_time_slice_val"");");
603 WBI
(" WC_Encoding : Character;");
604 WBI
(" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
605 WBI
(" Locking_Policy : Character;");
606 WBI
(" pragma Import (C, Locking_Policy, " &
607 """__gl_locking_policy"");");
608 WBI
(" Queuing_Policy : Character;");
609 WBI
(" pragma Import (C, Queuing_Policy, " &
610 """__gl_queuing_policy"");");
611 WBI
(" Task_Dispatching_Policy : Character;");
612 WBI
(" pragma Import (C, Task_Dispatching_Policy, " &
613 """__gl_task_dispatching_policy"");");
614 WBI
(" Priority_Specific_Dispatching : System.Address;");
615 WBI
(" pragma Import (C, Priority_Specific_Dispatching, " &
616 """__gl_priority_specific_dispatching"");");
617 WBI
(" Num_Specific_Dispatching : Integer;");
618 WBI
(" pragma Import (C, Num_Specific_Dispatching, " &
619 """__gl_num_specific_dispatching"");");
620 WBI
(" Main_CPU : Integer;");
621 WBI
(" pragma Import (C, Main_CPU, " &
622 """__gl_main_cpu"");");
624 WBI
(" Interrupt_States : System.Address;");
625 WBI
(" pragma Import (C, Interrupt_States, " &
626 """__gl_interrupt_states"");");
627 WBI
(" Num_Interrupt_States : Integer;");
628 WBI
(" pragma Import (C, Num_Interrupt_States, " &
629 """__gl_num_interrupt_states"");");
630 WBI
(" Unreserve_All_Interrupts : Integer;");
631 WBI
(" pragma Import (C, Unreserve_All_Interrupts, " &
632 """__gl_unreserve_all_interrupts"");");
634 if Exception_Tracebacks
or Exception_Tracebacks_Symbolic
then
635 WBI
(" Exception_Tracebacks : Integer;");
636 WBI
(" pragma Import (C, Exception_Tracebacks, " &
637 """__gl_exception_tracebacks"");");
639 if Exception_Tracebacks_Symbolic
then
640 WBI
(" Exception_Tracebacks_Symbolic : Integer;");
641 WBI
(" pragma Import (C, Exception_Tracebacks_Symbolic, " &
642 """__gl_exception_tracebacks_symbolic"");");
646 WBI
(" Detect_Blocking : Integer;");
647 WBI
(" pragma Import (C, Detect_Blocking, " &
648 """__gl_detect_blocking"");");
649 WBI
(" Default_Stack_Size : Integer;");
650 WBI
(" pragma Import (C, Default_Stack_Size, " &
651 """__gl_default_stack_size"");");
652 WBI
(" Leap_Seconds_Support : Integer;");
653 WBI
(" pragma Import (C, Leap_Seconds_Support, " &
654 """__gl_leap_seconds_support"");");
655 WBI
(" Bind_Env_Addr : System.Address;");
656 WBI
(" pragma Import (C, Bind_Env_Addr, " &
657 """__gl_bind_env_addr"");");
659 -- Import entry point for elaboration time signal handler
660 -- installation, and indication of if it's been called previously.
663 WBI
(" procedure Runtime_Initialize " &
664 "(Install_Handler : Integer);");
665 WBI
(" pragma Import (C, Runtime_Initialize, " &
666 """__gnat_runtime_initialize"");");
668 -- Import handlers attach procedure for sequential elaboration policy
670 if System_Interrupts_Used
671 and then Partition_Elaboration_Policy_Specified
= 'S'
673 WBI
(" procedure Install_Restricted_Handlers_Sequential;");
674 WBI
(" pragma Import (C," &
675 "Install_Restricted_Handlers_Sequential," &
676 " ""__gnat_attach_all_handlers"");");
680 -- Import task activation procedure for sequential elaboration
683 if System_Tasking_Restricted_Stages_Used
684 and then Partition_Elaboration_Policy_Specified
= 'S'
686 WBI
(" Partition_Elaboration_Policy : Character;");
687 WBI
(" pragma Import (C, Partition_Elaboration_Policy," &
688 " ""__gnat_partition_elaboration_policy"");");
690 WBI
(" procedure Activate_All_Tasks_Sequential;");
691 WBI
(" pragma Import (C, Activate_All_Tasks_Sequential," &
692 " ""__gnat_activate_all_tasks"");");
695 -- Import procedure to start slave cpus for bareboard runtime
697 if System_BB_CPU_Primitives_Multiprocessors_Used
then
698 WBI
(" procedure Start_Slave_CPUs;");
699 WBI
(" pragma Import (C, Start_Slave_CPUs," &
700 " ""__gnat_start_slave_cpus"");");
703 -- For restricted run-time libraries (ZFP and Ravenscar)
704 -- tasks are non-terminating, so we do not want finalization.
706 if not Configurable_Run_Time_On_Target
then
708 WBI
(" Finalize_Library_Objects : No_Param_Proc;");
709 WBI
(" pragma Import (C, Finalize_Library_Objects, " &
710 """__gnat_finalize_library_objects"");");
713 -- Initialize stack limit variable of the environment task if the
714 -- stack check method is stack limit and stack check is enabled.
716 if Stack_Check_Limits_On_Target
717 and then (Stack_Check_Default_On_Target
or Stack_Check_Switch_Set
)
720 WBI
(" procedure Initialize_Stack_Limit;");
721 WBI
(" pragma Import (C, Initialize_Stack_Limit, " &
722 """__gnat_initialize_stack_limit"");");
725 -- When dispatching domains are used then we need to signal it
726 -- before calling the main procedure.
728 if Dispatching_Domains_Used
then
729 WBI
(" procedure Freeze_Dispatching_Domains;");
730 WBI
(" pragma Import");
731 WBI
(" (Ada, Freeze_Dispatching_Domains, "
732 & """__gnat_freeze_dispatching_domains"");");
735 -- Start of processing for Adainit
738 WBI
(" if Is_Elaborated then");
741 WBI
(" Is_Elaborated := True;");
743 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
744 -- restriction No_Standard_Allocators_After_Elaboration is active.
746 if Cumulative_Restrictions
.Set
747 (No_Standard_Allocators_After_Elaboration
)
749 WBI
(" System.Elaboration_Allocators."
750 & "Mark_Start_Of_Elaboration;");
753 -- Generate assignments to initialize globals
755 Set_String
(" Main_Priority := ");
756 Set_Int
(Main_Priority
);
758 Write_Statement_Buffer
;
760 Set_String
(" Time_Slice_Value := ");
762 if Task_Dispatching_Policy_Specified
= 'F'
763 and then ALIs
.Table
(ALIs
.First
).Time_Slice_Value
= -1
767 Set_Int
(ALIs
.Table
(ALIs
.First
).Time_Slice_Value
);
771 Write_Statement_Buffer
;
773 Set_String
(" WC_Encoding := '");
774 Set_Char
(Get_WC_Encoding
);
777 Write_Statement_Buffer
;
779 Set_String
(" Locking_Policy := '");
780 Set_Char
(Locking_Policy_Specified
);
782 Write_Statement_Buffer
;
784 Set_String
(" Queuing_Policy := '");
785 Set_Char
(Queuing_Policy_Specified
);
787 Write_Statement_Buffer
;
789 Set_String
(" Task_Dispatching_Policy := '");
790 Set_Char
(Task_Dispatching_Policy_Specified
);
792 Write_Statement_Buffer
;
794 if System_Tasking_Restricted_Stages_Used
795 and then Partition_Elaboration_Policy_Specified
= 'S'
797 Set_String
(" Partition_Elaboration_Policy := '");
798 Set_Char
(Partition_Elaboration_Policy_Specified
);
800 Write_Statement_Buffer
;
805 WBI
(" Priority_Specific_Dispatching :=");
806 WBI
(" Local_Priority_Specific_Dispatching'Address;");
808 Set_String
(" Num_Specific_Dispatching := ");
809 Set_Int
(PSD_Pragma_Settings
.Last
+ 1);
811 Write_Statement_Buffer
;
813 Set_String
(" Main_CPU := ");
816 Write_Statement_Buffer
;
818 WBI
(" Interrupt_States := Local_Interrupt_States'Address;");
820 Set_String
(" Num_Interrupt_States := ");
821 Set_Int
(IS_Pragma_Settings
.Last
+ 1);
823 Write_Statement_Buffer
;
825 Set_String
(" Unreserve_All_Interrupts := ");
827 if Unreserve_All_Interrupts_Specified
then
834 Write_Statement_Buffer
;
836 if Exception_Tracebacks
or Exception_Tracebacks_Symbolic
then
837 WBI
(" Exception_Tracebacks := 1;");
839 if Exception_Tracebacks_Symbolic
then
840 WBI
(" Exception_Tracebacks_Symbolic := 1;");
844 Set_String
(" Detect_Blocking := ");
846 if Detect_Blocking
then
853 Write_Statement_Buffer
;
855 Set_String
(" Default_Stack_Size := ");
856 Set_Int
(Default_Stack_Size
);
858 Write_Statement_Buffer
;
860 Set_String
(" Leap_Seconds_Support := ");
862 if Leap_Seconds_Support
then
869 Write_Statement_Buffer
;
871 if Bind_Env_String_Built
then
872 WBI
(" Bind_Env_Addr := Bind_Env'Address;");
875 -- Generate call to Install_Handler
878 WBI
(" Runtime_Initialize (1);");
881 -- Generate call to set Initialize_Scalar values if active
883 if Initialize_Scalars_Used
then
885 Set_String
(" System.Scalar_Values.Initialize ('");
886 Set_Char
(Initialize_Scalars_Mode1
);
888 Set_Char
(Initialize_Scalars_Mode2
);
890 Write_Statement_Buffer
;
893 -- Generate assignment of default secondary stack size if set
895 if Sec_Stack_Used
and then Default_Sec_Stack_Size
/= -1 then
897 Set_String
(" System.Secondary_Stack.");
898 Set_String
("Default_Secondary_Stack_Size := ");
899 Set_Int
(Opt
.Default_Sec_Stack_Size
);
901 Write_Statement_Buffer
;
904 -- Initialize stack limit variable of the environment task if the stack
905 -- check method is stack limit and stack check is enabled.
907 if Stack_Check_Limits_On_Target
908 and then (Stack_Check_Default_On_Target
or Stack_Check_Switch_Set
)
911 WBI
(" Initialize_Stack_Limit;");
914 -- On CodePeer, the finalization of library objects is not relevant
916 if CodePeer_Mode
then
919 -- If this is the main program case, attach finalize_library to the soft
920 -- link. Do it only when not using a restricted run time, in which case
921 -- tasks are non-terminating, so we do not want library-level
924 elsif not Bind_For_Library
925 and then not Configurable_Run_Time_On_Target
926 and then not Suppress_Standard_Library_On_Target
930 if Lib_Final_Built
then
931 Set_String
(" Finalize_Library_Objects := ");
932 Set_String
("finalize_library'access;");
934 Set_String
(" Finalize_Library_Objects := null;");
937 Write_Statement_Buffer
;
940 -- Generate elaboration calls
942 if not CodePeer_Mode
then
946 Gen_Elab_Calls
(Elab_Order
);
948 if not CodePeer_Mode
then
950 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
951 -- restriction No_Standard_Allocators_After_Elaboration is active.
953 if Cumulative_Restrictions
.Set
954 (No_Standard_Allocators_After_Elaboration
)
957 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
960 -- From this point, no new dispatching domain can be created
962 if Dispatching_Domains_Used
then
963 WBI
(" Freeze_Dispatching_Domains;");
966 -- Sequential partition elaboration policy
968 if Partition_Elaboration_Policy_Specified
= 'S' then
969 if System_Interrupts_Used
then
970 WBI
(" Install_Restricted_Handlers_Sequential;");
973 if System_Tasking_Restricted_Stages_Used
then
974 WBI
(" Activate_All_Tasks_Sequential;");
978 if System_BB_CPU_Primitives_Multiprocessors_Used
then
979 WBI
(" Start_Slave_CPUs;");
983 WBI
(" end " & Ada_Init_Name
.all & ";");
987 -------------------------
988 -- Gen_Bind_Env_String --
989 -------------------------
991 procedure Gen_Bind_Env_String
is
992 procedure Write_Name_With_Len
(Nam
: Name_Id
);
993 -- Write Nam as a string literal, prefixed with one
994 -- character encoding Nam's length.
996 -------------------------
997 -- Write_Name_With_Len --
998 -------------------------
1000 procedure Write_Name_With_Len
(Nam
: Name_Id
) is
1002 Get_Name_String
(Nam
);
1005 Store_String_Char
(Character'Val (Name_Len
));
1006 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
1008 Write_String_Table_Entry
(End_String
);
1009 end Write_Name_With_Len
;
1014 KN
: Name_Id
:= No_Name
;
1015 VN
: Name_Id
:= No_Name
;
1017 -- Start of processing for Gen_Bind_Env_String
1020 Bind_Environment
.Get_First
(KN
, VN
);
1022 if VN
= No_Name
then
1026 Set_Special_Output
(Write_Bind_Line
'Access);
1028 WBI
(" Bind_Env : aliased constant String :=");
1030 while VN
/= No_Name
loop
1031 Write_Str
(" " & Amp
& ' ');
1032 Write_Name_With_Len
(KN
);
1034 Write_Name_With_Len
(VN
);
1037 Bind_Environment
.Get_Next
(KN
, VN
);
1040 WBI
(" & ASCII.NUL;");
1042 Set_Special_Output
(null);
1044 Bind_Env_String_Built
:= True;
1045 end Gen_Bind_Env_String
;
1047 --------------------------
1048 -- Gen_CodePeer_Wrapper --
1049 --------------------------
1051 procedure Gen_CodePeer_Wrapper
is
1052 Callee_Name
: constant String := "Ada_Main_Program";
1055 if ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1056 WBI
(" procedure " & CodePeer_Wrapper_Name
& " is ");
1058 WBI
(" " & Callee_Name
& ";");
1061 WBI
(" function " & CodePeer_Wrapper_Name
& " return Integer is");
1063 WBI
(" return " & Callee_Name
& ";");
1066 WBI
(" end " & CodePeer_Wrapper_Name
& ";");
1068 end Gen_CodePeer_Wrapper
;
1070 --------------------
1071 -- Gen_Elab_Calls --
1072 --------------------
1074 procedure Gen_Elab_Calls
(Elab_Order
: Unit_Id_Array
) is
1075 Check_Elab_Flag
: Boolean;
1078 -- Loop through elaboration order entries
1080 for E
in Elab_Order
'Range loop
1082 Unum
: constant Unit_Id
:= Elab_Order
(E
);
1083 U
: Unit_Record
renames Units
.Table
(Unum
);
1085 Unum_Spec
: Unit_Id
;
1086 -- This is the unit number of the spec that corresponds to
1087 -- this entry. It is the same as Unum except when the body
1088 -- and spec are different and we are currently processing
1089 -- the body, in which case it is the spec (Unum + 1).
1092 if U
.Utype
= Is_Body
then
1093 Unum_Spec
:= Unum
+ 1;
1098 -- Nothing to do if predefined unit in no run time mode
1100 if No_Run_Time_Mode
and then Is_Predefined_File_Name
(U
.Sfile
) then
1103 -- Likewise if this is an interface to a stand alone library
1105 elsif U
.SAL_Interface
then
1108 -- Case of no elaboration code
1112 -- In CodePeer mode, we special case subprogram bodies which
1113 -- are handled in the 'else' part below, and lead to a call
1114 -- to <subp>'Elab_Subp_Body.
1116 and then (not CodePeer_Mode
1120 or else U
.Utype
= Is_Spec
1121 or else U
.Utype
= Is_Spec_Only
1122 or else U
.Unit_Kind
/= 's')
1124 -- In the case of a body with a separate spec, where the
1125 -- separate spec has an elaboration entity defined, this is
1126 -- where we increment the elaboration entity if one exists.
1128 -- Likewise for lone specs with an elaboration entity defined
1129 -- despite No_Elaboration_Code, e.g. when requested to preserve
1132 if (U
.Utype
= Is_Body
or else U
.Utype
= Is_Spec_Only
)
1133 and then Units
.Table
(Unum_Spec
).Set_Elab_Entity
1134 and then not CodePeer_Mode
1137 Set_Unit_Number
(Unum_Spec
);
1138 Set_String
(" := E");
1139 Set_Unit_Number
(Unum_Spec
);
1140 Set_String
(" + 1;");
1141 Write_Statement_Buffer
;
1144 -- Here if elaboration code is present. If binding a library
1145 -- or if there is a non-Ada main subprogram then we generate:
1147 -- if uname_E = 0 then
1148 -- uname'elab_[spec|body];
1150 -- uname_E := uname_E + 1;
1152 -- Otherwise, elaboration routines are called unconditionally:
1154 -- uname'elab_[spec|body];
1155 -- uname_E := uname_E + 1;
1157 -- The uname_E increment is skipped if this is a separate spec,
1158 -- since it will be done when we process the body.
1160 -- In CodePeer mode, we do not generate any reference to xxx_E
1161 -- variables, only calls to 'Elab* subprograms.
1164 -- Check incompatibilities with No_Multiple_Elaboration
1166 if not CodePeer_Mode
1167 and then Cumulative_Restrictions
.Set
(No_Multiple_Elaboration
)
1169 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1171 if Force_Checking_Of_Elaboration_Flags
then
1173 ("-F (force elaboration checks) switch not allowed "
1174 & "with restriction No_Multiple_Elaboration active");
1176 -- Interfacing of libraries not allowed
1178 elsif Interface_Library_Unit
then
1180 ("binding of interfaced libraries not allowed "
1181 & "with restriction No_Multiple_Elaboration active");
1183 -- Non-Ada main program not allowed
1185 elsif not Bind_Main_Program
then
1187 ("non-Ada main program not allowed "
1188 & "with restriction No_Multiple_Elaboration active");
1192 -- OK, see if we need to test elaboration flag
1195 Units
.Table
(Unum_Spec
).Set_Elab_Entity
1196 and then not CodePeer_Mode
1197 and then (Force_Checking_Of_Elaboration_Flags
1198 or Interface_Library_Unit
1199 or not Bind_Main_Program
);
1201 if Check_Elab_Flag
then
1202 Set_String
(" if E");
1203 Set_Unit_Number
(Unum_Spec
);
1204 Set_String
(" = 0 then");
1205 Write_Statement_Buffer
;
1210 Get_Decoded_Name_String_With_Brackets
(U
.Uname
);
1212 if Name_Buffer
(Name_Len
) = 's' then
1213 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 8) :=
1215 Name_Len
:= Name_Len
+ 8;
1217 -- Special case in CodePeer mode for subprogram bodies
1218 -- which correspond to CodePeer 'Elab_Subp_Body special
1221 elsif U
.Unit_Kind
= 's' and CodePeer_Mode
then
1222 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 13) :=
1224 Name_Len
:= Name_Len
+ 13;
1227 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 8) :=
1229 Name_Len
:= Name_Len
+ 8;
1232 Set_Casing
(U
.Icasing
);
1235 Write_Statement_Buffer
;
1237 if Check_Elab_Flag
then
1241 if U
.Utype
/= Is_Spec
1242 and then not CodePeer_Mode
1243 and then Units
.Table
(Unum_Spec
).Set_Elab_Entity
1246 Set_Unit_Number
(Unum_Spec
);
1247 Set_String
(" := E");
1248 Set_Unit_Number
(Unum_Spec
);
1249 Set_String
(" + 1;");
1250 Write_Statement_Buffer
;
1257 ------------------------
1258 -- Gen_Elab_Externals --
1259 ------------------------
1261 procedure Gen_Elab_Externals
(Elab_Order
: Unit_Id_Array
) is
1263 if CodePeer_Mode
then
1267 for E
in Elab_Order
'Range loop
1269 Unum
: constant Unit_Id
:= Elab_Order
(E
);
1270 U
: Unit_Record
renames Units
.Table
(Unum
);
1273 -- Check for Elab_Entity to be set for this unit
1275 if U
.Set_Elab_Entity
1277 -- Don't generate reference for stand alone library
1279 and then not U
.SAL_Interface
1281 -- Don't generate reference for predefined file in No_Run_Time
1282 -- mode, since we don't include the object files in this case
1286 and then Is_Predefined_File_Name
(U
.Sfile
))
1288 Get_Name_String
(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
(Elab_Order
: Unit_Id_Array
) is
1313 WBI
(" -- BEGIN ELABORATION ORDER");
1315 for J
in Elab_Order
'Range loop
1316 Set_String
(" -- ");
1317 Get_Name_String
(Units
.Table
(Elab_Order
(J
)).Uname
);
1319 Write_Statement_Buffer
;
1322 WBI
(" -- END ELABORATION ORDER");
1325 --------------------------
1326 -- Gen_Finalize_Library --
1327 --------------------------
1329 procedure Gen_Finalize_Library
(Elab_Order
: Unit_Id_Array
) is
1330 procedure Gen_Header
;
1331 -- Generate the header of the finalization routine
1337 procedure Gen_Header
is
1339 WBI
(" procedure finalize_library is");
1347 Uspec
: Unit_Record
;
1350 -- Start of processing for Gen_Finalize_Library
1353 if CodePeer_Mode
then
1357 for E
in reverse Elab_Order
'Range loop
1358 Unum
:= Elab_Order
(E
);
1359 U
:= Units
.Table
(Unum
);
1361 -- Dealing with package bodies is a little complicated. In such
1362 -- cases we must retrieve the package spec since it contains the
1363 -- spec of the body finalizer.
1365 if U
.Utype
= Is_Body
then
1367 Uspec
:= Units
.Table
(Unum
);
1372 Get_Name_String
(Uspec
.Uname
);
1374 -- We are only interested in non-generic packages
1376 if U
.Unit_Kind
/= 'p' or else U
.Is_Generic
then
1379 -- That aren't an interface to a stand alone library
1381 elsif U
.SAL_Interface
then
1384 -- Case of no finalization
1386 elsif not U
.Has_Finalizer
then
1388 -- The only case in which we have to do something is if this
1389 -- is a body, with a separate spec, where the separate spec
1390 -- has a finalizer. In that case, this is where we decrement
1391 -- the elaboration entity.
1393 if U
.Utype
= Is_Body
and then Uspec
.Has_Finalizer
then
1394 if not Lib_Final_Built
then
1396 Lib_Final_Built
:= True;
1400 Set_Unit_Number
(Unum
);
1401 Set_String
(" := E");
1402 Set_Unit_Number
(Unum
);
1403 Set_String
(" - 1;");
1404 Write_Statement_Buffer
;
1408 if not Lib_Final_Built
then
1410 Lib_Final_Built
:= True;
1415 -- procedure F<Count>;
1417 Set_String
(" declare");
1418 Write_Statement_Buffer
;
1420 Set_String
(" procedure F");
1423 Write_Statement_Buffer
;
1426 -- pragma Import (Ada, F<Count>,
1427 -- "xx__yy__finalize_[body|spec]");
1429 Set_String
(" pragma Import (Ada, F");
1431 Set_String
(", """);
1433 -- Perform name construction
1436 Set_String
("__finalize_");
1438 -- Package spec processing
1440 if U
.Utype
= Is_Spec
1441 or else U
.Utype
= Is_Spec_Only
1443 Set_String
("spec");
1445 -- Package body processing
1448 Set_String
("body");
1451 Set_String
(""");");
1452 Write_Statement_Buffer
;
1454 -- If binding a library or if there is a non-Ada main subprogram
1455 -- then we generate:
1458 -- uname_E := uname_E - 1;
1459 -- if uname_E = 0 then
1464 -- Otherwise, finalization routines are called unconditionally:
1467 -- uname_E := uname_E - 1;
1471 -- The uname_E decrement is skipped if this is a separate spec,
1472 -- since it will be done when we process the body.
1476 if U
.Utype
/= Is_Spec
then
1478 Set_Unit_Number
(Unum
);
1479 Set_String
(" := E");
1480 Set_Unit_Number
(Unum
);
1481 Set_String
(" - 1;");
1482 Write_Statement_Buffer
;
1485 if Interface_Library_Unit
or not Bind_Main_Program
then
1486 Set_String
(" if E");
1487 Set_Unit_Number
(Unum
);
1488 Set_String
(" = 0 then");
1489 Write_Statement_Buffer
;
1496 Write_Statement_Buffer
;
1498 if Interface_Library_Unit
or not Bind_Main_Program
then
1508 if Lib_Final_Built
then
1510 -- It is possible that the finalization of a library-level object
1511 -- raised an exception. In that case import the actual exception
1512 -- and the routine necessary to raise it.
1515 WBI
(" procedure Reraise_Library_Exception_If_Any;");
1517 Set_String
(" pragma Import (Ada, ");
1518 Set_String
("Reraise_Library_Exception_If_Any, ");
1519 Set_String
("""__gnat_reraise_library_exception_if_any"");");
1520 Write_Statement_Buffer
;
1523 WBI
(" Reraise_Library_Exception_If_Any;");
1525 WBI
(" end finalize_library;");
1528 end Gen_Finalize_Library
;
1534 procedure Gen_Main
is
1536 if not No_Main_Subprogram
then
1538 -- To call the main program, we declare it using a pragma Import
1539 -- Ada with the right link name.
1541 -- It might seem more obvious to "with" the main program, and call
1542 -- it in the normal Ada manner. We do not do this for three
1545 -- 1. It is more efficient not to recompile the main program
1546 -- 2. We are not entitled to assume the source is accessible
1547 -- 3. We don't know what options to use to compile it
1549 -- It is really reason 3 that is most critical (indeed we used
1550 -- to generate the "with", but several regression tests failed).
1552 if ALIs
.Table
(ALIs
.First
).Main_Program
= Func
then
1553 WBI
(" function Ada_Main_Program return Integer;");
1555 WBI
(" procedure Ada_Main_Program;");
1558 Set_String
(" pragma Import (Ada, Ada_Main_Program, """);
1559 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
1560 Set_Main_Program_Name
;
1561 Set_String
(""");");
1563 Write_Statement_Buffer
;
1566 -- For CodePeer, declare a wrapper for the user-defined main program
1568 if CodePeer_Mode
then
1569 Gen_CodePeer_Wrapper
;
1573 if Exit_Status_Supported_On_Target
then
1574 Set_String
(" function ");
1576 Set_String
(" procedure ");
1579 Set_String
(Get_Main_Name
);
1581 if Command_Line_Args_On_Target
then
1582 Write_Statement_Buffer
;
1583 WBI
(" (argc : Integer;");
1584 WBI
(" argv : System.Address;");
1585 WBI
(" envp : System.Address)");
1587 if Exit_Status_Supported_On_Target
then
1588 WBI
(" return Integer");
1594 if Exit_Status_Supported_On_Target
then
1595 Set_String
(" return Integer is");
1600 Write_Statement_Buffer
;
1603 if Opt
.Default_Exit_Status
/= 0
1604 and then Bind_Main_Program
1605 and then not Configurable_Run_Time_Mode
1607 WBI
(" procedure Set_Exit_Status (Status : Integer);");
1608 WBI
(" pragma Import (C, Set_Exit_Status, " &
1609 """__gnat_set_exit_status"");");
1613 -- Initialize and Finalize
1615 if not CodePeer_Mode
1616 and then not Cumulative_Restrictions
.Set
(No_Finalization
)
1618 WBI
(" procedure Initialize (Addr : System.Address);");
1619 WBI
(" pragma Import (C, Initialize, ""__gnat_initialize"");");
1621 WBI
(" procedure Finalize;");
1622 WBI
(" pragma Import (C, Finalize, ""__gnat_finalize"");");
1625 -- If we want to analyze the stack, we must import corresponding symbols
1627 if Dynamic_Stack_Measurement
then
1629 WBI
(" procedure Output_Results;");
1630 WBI
(" pragma Import (C, Output_Results, " &
1631 """__gnat_stack_usage_output_results"");");
1635 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1636 WBI
(" pragma Import (C, Initialize_Stack_Analysis, " &
1637 """__gnat_stack_usage_initialize"");");
1640 -- Deal with declarations for main program case
1642 if not No_Main_Subprogram
then
1643 if ALIs
.Table
(ALIs
.First
).Main_Program
= Func
then
1644 WBI
(" Result : Integer;");
1648 if Bind_Main_Program
1649 and not Suppress_Standard_Library_On_Target
1650 and not CodePeer_Mode
1652 WBI
(" SEH : aliased array (1 .. 2) of Integer;");
1657 -- Generate a reference to Ada_Main_Program_Name. This symbol is not
1658 -- referenced elsewhere in the generated program, but is needed by
1659 -- the debugger (that's why it is generated in the first place). The
1660 -- reference stops Ada_Main_Program_Name from being optimized away by
1661 -- smart linkers, such as the AiX linker.
1663 -- Because this variable is unused, we make this variable "aliased"
1664 -- with a pragma Volatile in order to tell the compiler to preserve
1665 -- this variable at any level of optimization.
1667 if Bind_Main_Program
and not CodePeer_Mode
then
1668 WBI
(" Ensure_Reference : aliased System.Address := " &
1669 "Ada_Main_Program_Name'Address;");
1670 WBI
(" pragma Volatile (Ensure_Reference);");
1676 -- Acquire command line arguments if present on target
1678 if CodePeer_Mode
then
1681 elsif Command_Line_Args_On_Target
then
1682 WBI
(" gnat_argc := argc;");
1683 WBI
(" gnat_argv := argv;");
1684 WBI
(" gnat_envp := envp;");
1687 -- If configurable run time and no command line args, then nothing needs
1688 -- to be done since the gnat_argc/argv/envp variables are suppressed in
1691 elsif Configurable_Run_Time_On_Target
then
1694 -- Otherwise set dummy values (to be filled in by some other unit?)
1697 WBI
(" gnat_argc := 0;");
1698 WBI
(" gnat_argv := System.Null_Address;");
1699 WBI
(" gnat_envp := System.Null_Address;");
1702 if Opt
.Default_Exit_Status
/= 0
1703 and then Bind_Main_Program
1704 and then not Configurable_Run_Time_Mode
1706 Set_String
(" Set_Exit_Status (");
1707 Set_Int
(Opt
.Default_Exit_Status
);
1709 Write_Statement_Buffer
;
1712 if Dynamic_Stack_Measurement
then
1713 Set_String
(" Initialize_Stack_Analysis (");
1714 Set_Int
(Dynamic_Stack_Measurement_Array_Size
);
1716 Write_Statement_Buffer
;
1719 if not Cumulative_Restrictions
.Set
(No_Finalization
)
1720 and then not CodePeer_Mode
1722 if not No_Main_Subprogram
1723 and then Bind_Main_Program
1724 and then not Suppress_Standard_Library_On_Target
1726 WBI
(" Initialize (SEH'Address);");
1728 WBI
(" Initialize (System.Null_Address);");
1732 WBI
(" " & Ada_Init_Name
.all & ";");
1734 if not No_Main_Subprogram
then
1735 if CodePeer_Mode
then
1736 if ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1737 WBI
(" " & CodePeer_Wrapper_Name
& ";");
1739 WBI
(" Result := " & CodePeer_Wrapper_Name
& ";");
1742 elsif ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1743 WBI
(" Ada_Main_Program;");
1746 WBI
(" Result := Ada_Main_Program;");
1750 -- Adafinal call is skipped if no finalization
1752 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
1756 -- Prints the result of static stack analysis
1758 if Dynamic_Stack_Measurement
then
1759 WBI
(" Output_Results;");
1762 -- Finalize is only called if we have a run time
1764 if not Cumulative_Restrictions
.Set
(No_Finalization
)
1765 and then not CodePeer_Mode
1772 if Exit_Status_Supported_On_Target
then
1773 if No_Main_Subprogram
1774 or else ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
1776 WBI
(" return (gnat_exit_status);");
1778 WBI
(" return (Result);");
1786 ------------------------------
1787 -- Gen_Object_Files_Options --
1788 ------------------------------
1790 procedure Gen_Object_Files_Options
(Elab_Order
: Unit_Id_Array
) is
1792 -- This keeps track of the position in the sorted set of entries in the
1793 -- Linker_Options table of where the first entry from an internal file
1796 Linker_Option_List_Started
: Boolean := False;
1797 -- Set to True when "LINKER OPTION LIST" is displayed
1799 procedure Write_Linker_Option
;
1800 -- Write binder info linker option
1802 -------------------------
1803 -- Write_Linker_Option --
1804 -------------------------
1806 procedure Write_Linker_Option
is
1811 -- Loop through string, breaking at null's
1814 while Start
< Name_Len
loop
1816 -- Find null ending this section
1819 while Name_Buffer
(Stop
) /= ASCII
.NUL
1820 and then Stop
<= Name_Len
loop
1824 -- Process section if non-null
1826 if Stop
> Start
then
1827 if Output_Linker_Option_List
then
1828 if not Zero_Formatting
then
1829 if not Linker_Option_List_Started
then
1830 Linker_Option_List_Started
:= True;
1832 Write_Str
(" LINKER OPTION LIST");
1840 Write_Str
(Name_Buffer
(Start
.. Stop
- 1));
1843 WBI
(" -- " & Name_Buffer
(Start
.. Stop
- 1));
1848 end Write_Linker_Option
;
1850 -- Start of processing for Gen_Object_Files_Options
1853 WBI
("-- BEGIN Object file/option list");
1855 if Object_List_Filename
/= null then
1856 Set_List_File
(Object_List_Filename
.all);
1859 for E
in Elab_Order
'Range loop
1861 -- If not spec that has an associated body, then generate a comment
1862 -- giving the name of the corresponding object file.
1864 if not Units
.Table
(Elab_Order
(E
)).SAL_Interface
1865 and then Units
.Table
(Elab_Order
(E
)).Utype
/= Is_Spec
1869 (Units
.Table
(Elab_Order
(E
)).My_ALI
).Ofile_Full_Name
);
1871 -- If the presence of an object file is necessary or if it exists,
1874 if not Hostparm
.Exclude_Missing_Objects
1876 System
.OS_Lib
.Is_Regular_File
(Name_Buffer
(1 .. Name_Len
))
1878 WBI
(" -- " & Name_Buffer
(1 .. Name_Len
));
1880 if Output_Object_List
then
1881 Write_Str
(Name_Buffer
(1 .. Name_Len
));
1888 if Object_List_Filename
/= null then
1892 -- Add a "-Ldir" for each directory in the object path
1894 for J
in 1 .. Nb_Dir_In_Obj_Search_Path
loop
1896 Dir
: constant String_Ptr
:= Dir_In_Obj_Search_Path
(J
);
1900 Add_Str_To_Name_Buffer
("-L");
1901 Add_Str_To_Name_Buffer
(Dir
.all);
1902 Write_Linker_Option
;
1906 if not (Opt
.No_Run_Time_Mode
or Opt
.No_Stdlib
) then
1909 if Opt
.Shared_Libgnat
then
1910 Add_Str_To_Name_Buffer
("-shared");
1912 Add_Str_To_Name_Buffer
("-static");
1915 -- Write directly to avoid inclusion in -K output as -static and
1916 -- -shared are not usually specified linker options.
1918 WBI
(" -- " & Name_Buffer
(1 .. Name_Len
));
1921 -- Sort linker options
1923 -- This sort accomplishes two important purposes:
1925 -- a) All application files are sorted to the front, and all GNAT
1926 -- internal files are sorted to the end. This results in a well
1927 -- defined dividing line between the two sets of files, for the
1928 -- purpose of inserting certain standard library references into
1929 -- the linker arguments list.
1931 -- b) Given two different units, we sort the linker options so that
1932 -- those from a unit earlier in the elaboration order comes later
1933 -- in the list. This is a heuristic designed to create a more
1934 -- friendly order of linker options when the operations appear in
1935 -- separate units. The idea is that if unit A must be elaborated
1936 -- before unit B, then it is more likely that B references
1937 -- libraries included by A, than vice versa, so we want libraries
1938 -- included by A to come after libraries included by B.
1940 -- These two criteria are implemented by function Lt_Linker_Option. Note
1941 -- that a special case of b) is that specs are elaborated before bodies,
1942 -- so linker options from specs come after linker options for bodies,
1943 -- and again, the assumption is that libraries used by the body are more
1944 -- likely to reference libraries used by the spec, than vice versa.
1947 (Linker_Options
.Last
,
1948 Move_Linker_Option
'Access,
1949 Lt_Linker_Option
'Access);
1951 -- Write user linker options, i.e. the set of linker options that come
1952 -- from all files other than GNAT internal files, Lgnat is left set to
1953 -- point to the first entry from a GNAT internal file, or past the end
1954 -- of the entries if there are no internal files.
1956 Lgnat
:= Linker_Options
.Last
+ 1;
1958 for J
in 1 .. Linker_Options
.Last
loop
1959 if not Linker_Options
.Table
(J
).Internal_File
then
1960 Get_Name_String
(Linker_Options
.Table
(J
).Name
);
1961 Write_Linker_Option
;
1968 -- Now we insert standard linker options that must appear after the
1969 -- entries from user files, and before the entries from GNAT run-time
1970 -- files. The reason for this decision is that libraries referenced
1971 -- by internal routines may reference these standard library entries.
1973 -- Note that we do not insert anything when pragma No_Run_Time has
1974 -- been specified or when the standard libraries are not to be used,
1975 -- otherwise on some platforms, we may get duplicate symbols when
1976 -- linking (not clear if this is still the case, but it is harmless).
1978 if not (Opt
.No_Run_Time_Mode
or else Opt
.No_Stdlib
) then
1982 if Opt
.Shared_Libgnat
then
1983 Add_Str_To_Name_Buffer
(Shared_Lib
("gnarl"));
1985 Add_Str_To_Name_Buffer
("-lgnarl");
1988 Write_Linker_Option
;
1993 if Opt
.Shared_Libgnat
then
1994 Add_Str_To_Name_Buffer
(Shared_Lib
("gnat"));
1996 Add_Str_To_Name_Buffer
("-lgnat");
1999 Write_Linker_Option
;
2002 -- Write linker options from all internal files
2004 for J
in Lgnat
.. Linker_Options
.Last
loop
2005 Get_Name_String
(Linker_Options
.Table
(J
).Name
);
2006 Write_Linker_Option
;
2009 if Output_Linker_Option_List
and then not Zero_Formatting
then
2013 WBI
("-- END Object file/option list ");
2014 end Gen_Object_Files_Options
;
2016 ---------------------
2017 -- Gen_Output_File --
2018 ---------------------
2020 procedure Gen_Output_File
2022 Elab_Order
: Unit_Id_Array
)
2025 -- Acquire settings for Interrupt_State pragmas
2027 Set_IS_Pragma_Table
;
2029 -- Acquire settings for Priority_Specific_Dispatching pragma
2031 Set_PSD_Pragma_Table
;
2033 -- Override time slice value if -T switch is set
2035 if Time_Slice_Set
then
2036 ALIs
.Table
(ALIs
.First
).Time_Slice_Value
:= Opt
.Time_Slice_Value
;
2039 -- Count number of elaboration calls
2041 for E
in Elab_Order
'Range loop
2042 if Units
.Table
(Elab_Order
(E
)).No_Elab
then
2045 Num_Elab_Calls
:= Num_Elab_Calls
+ 1;
2049 -- Generate output file in appropriate language
2051 Gen_Output_File_Ada
(Filename
, Elab_Order
);
2052 end Gen_Output_File
;
2054 -------------------------
2055 -- Gen_Output_File_Ada --
2056 -------------------------
2058 procedure Gen_Output_File_Ada
2059 (Filename
: String; Elab_Order
: Unit_Id_Array
)
2061 Ada_Main
: constant String := Get_Ada_Main_Name
;
2062 -- Name to be used for generated Ada main program. See the body of
2063 -- function Get_Ada_Main_Name for details on the form of the name.
2065 Needs_Library_Finalization
: constant Boolean :=
2066 not Configurable_Run_Time_On_Target
2067 and then Has_Finalizer
(Elab_Order
);
2068 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2069 -- non-terminating, so we do not want finalization.
2072 -- Name of generated bind file (spec)
2075 -- Name of generated bind file (body)
2078 -- Create spec first
2080 Create_Binder_Output
(Filename
, 's', Bfiles
);
2082 -- We always compile the binder file in Ada 95 mode so that we properly
2083 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2084 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2086 WBI
("pragma Warnings (Off);");
2087 WBI
("pragma Ada_95;");
2089 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2090 -- then we need to make sure that the binder program is compiled with
2091 -- the same restriction, so that no exception tables are generated.
2093 if Cumulative_Restrictions
.Set
(No_Exception_Handlers
) then
2094 WBI
("pragma Restrictions (No_Exception_Handlers);");
2097 -- Same processing for Restrictions (No_Exception_Propagation)
2099 if Cumulative_Restrictions
.Set
(No_Exception_Propagation
) then
2100 WBI
("pragma Restrictions (No_Exception_Propagation);");
2103 -- Same processing for pragma No_Run_Time
2105 if No_Run_Time_Mode
then
2106 WBI
("pragma No_Run_Time;");
2109 -- Generate with of System so we can reference System.Address
2111 WBI
("with System;");
2113 -- Generate with of System.Initialize_Scalars if active
2115 if Initialize_Scalars_Used
then
2116 WBI
("with System.Scalar_Values;");
2119 -- Generate with of System.Secondary_Stack if active
2121 if Sec_Stack_Used
and then Default_Sec_Stack_Size
/= -1 then
2122 WBI
("with System.Secondary_Stack;");
2125 Resolve_Binder_Options
(Elab_Order
);
2127 -- Generate standard with's
2129 if not Suppress_Standard_Library_On_Target
then
2130 if CodePeer_Mode
then
2131 WBI
("with System.Standard_Library;");
2135 WBI
("package " & Ada_Main
& " is");
2137 -- Main program case
2139 if Bind_Main_Program
then
2140 -- Generate argc/argv stuff unless suppressed
2142 if Command_Line_Args_On_Target
2143 or not Configurable_Run_Time_On_Target
2146 WBI
(" gnat_argc : Integer;");
2147 WBI
(" gnat_argv : System.Address;");
2148 WBI
(" gnat_envp : System.Address;");
2150 -- If the standard library is not suppressed, these variables
2151 -- are in the run-time data area for easy run time access.
2153 if not Suppress_Standard_Library_On_Target
then
2155 WBI
(" pragma Import (C, gnat_argc);");
2156 WBI
(" pragma Import (C, gnat_argv);");
2157 WBI
(" pragma Import (C, gnat_envp);");
2161 -- Define exit status. Again in normal mode, this is in the
2162 -- run-time library, and is initialized there, but in the
2163 -- configurable runtime case, the variable is declared and
2164 -- initialized in this file.
2168 if Configurable_Run_Time_Mode
then
2169 if Exit_Status_Supported_On_Target
then
2170 WBI
(" gnat_exit_status : Integer := 0;");
2174 WBI
(" gnat_exit_status : Integer;");
2175 WBI
(" pragma Import (C, gnat_exit_status);");
2178 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2179 -- the main program. Otherwise, it can lead under some circumstances
2180 -- to a symbol duplication during the link (for instance when a C
2181 -- program uses two Ada libraries). Also zero terminate the string
2182 -- so that its end can be found reliably at run time.
2185 WBI
(" GNAT_Version : constant String :=");
2186 WBI
(" """ & Ver_Prefix
&
2187 Gnat_Version_String
&
2189 WBI
(" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2192 Set_String
(" Ada_Main_Program_Name : constant String := """);
2193 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2195 Set_Main_Program_Name
;
2196 Set_String
(""" & ASCII.NUL;");
2198 Write_Statement_Buffer
;
2201 (" pragma Export (C, Ada_Main_Program_Name, " &
2202 """__gnat_ada_main_program_name"");");
2206 WBI
(" procedure " & Ada_Init_Name
.all & ";");
2207 WBI
(" pragma Export (C, " & Ada_Init_Name
.all & ", """ &
2208 Ada_Init_Name
.all & """);");
2210 -- If -a has been specified use pragma Linker_Constructor for the init
2211 -- procedure and pragma Linker_Destructor for the final procedure.
2213 if Use_Pragma_Linker_Constructor
then
2214 WBI
(" pragma Linker_Constructor (" & Ada_Init_Name
.all & ");");
2217 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
2219 WBI
(" procedure " & Ada_Final_Name
.all & ";");
2220 WBI
(" pragma Export (C, " & Ada_Final_Name
.all & ", """ &
2221 Ada_Final_Name
.all & """);");
2223 if Use_Pragma_Linker_Constructor
then
2224 WBI
(" pragma Linker_Destructor (" & Ada_Final_Name
.all & ");");
2228 if Bind_Main_Program
then
2232 if Exit_Status_Supported_On_Target
then
2233 Set_String
(" function ");
2235 Set_String
(" procedure ");
2238 Set_String
(Get_Main_Name
);
2240 -- Generate argument list if present
2242 if Command_Line_Args_On_Target
then
2243 Write_Statement_Buffer
;
2244 WBI
(" (argc : Integer;");
2245 WBI
(" argv : System.Address;");
2247 (" envp : System.Address)");
2249 if Exit_Status_Supported_On_Target
then
2250 Write_Statement_Buffer
;
2251 WBI
(" return Integer;");
2253 Write_Statement_Buffer
(";");
2257 if Exit_Status_Supported_On_Target
then
2258 Write_Statement_Buffer
(" return Integer;");
2260 Write_Statement_Buffer
(";");
2264 WBI
(" pragma Export (C, " & Get_Main_Name
& ", """ &
2265 Get_Main_Name
& """);");
2268 -- Generate version numbers for units, only if needed. Be very safe on
2271 if not Configurable_Run_Time_On_Target
2272 or else System_Version_Control_Used
2273 or else not Bind_Main_Program
2278 Gen_Elab_Order
(Elab_Order
);
2283 WBI
("end " & Ada_Main
& ";");
2284 Close_Binder_Output
;
2286 -- Prepare to write body
2288 Create_Binder_Output
(Filename
, 'b', Bfileb
);
2290 -- We always compile the binder file in Ada 95 mode so that we properly
2291 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2292 -- of the Ada 2005/2012 constructs are needed by the binder file.
2294 WBI
("pragma Warnings (Off);");
2295 WBI
("pragma Ada_95;");
2297 -- Output Source_File_Name pragmas which look like
2299 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2300 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2302 -- where sss/bbb are the spec/body file names respectively
2304 Get_Name_String
(Bfiles
);
2305 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 3) := """);";
2307 WBI
("pragma Source_File_Name (" &
2309 ", Spec_File_Name => """ &
2310 Name_Buffer
(1 .. Name_Len
+ 3));
2312 Get_Name_String
(Bfileb
);
2313 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 3) := """);";
2315 WBI
("pragma Source_File_Name (" &
2317 ", Body_File_Name => """ &
2318 Name_Buffer
(1 .. Name_Len
+ 3));
2320 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2321 -- versions of the compiler which have overflow checks on by default.
2322 -- We do not want overflow checking enabled for the increments of the
2323 -- elaboration variables (since this can cause an unwanted reference to
2324 -- the last chance exception handler for limited run-times).
2326 WBI
("pragma Suppress (Overflow_Check);");
2328 -- Generate with of System.Restrictions to initialize
2329 -- Run_Time_Restrictions.
2331 if System_Restrictions_Used
2332 and not Suppress_Standard_Library_On_Target
2335 WBI
("with System.Restrictions;");
2338 -- Generate with of Ada.Exceptions if needs library finalization
2340 if Needs_Library_Finalization
then
2341 WBI
("with Ada.Exceptions;");
2344 -- Generate with of System.Elaboration_Allocators if the restriction
2345 -- No_Standard_Allocators_After_Elaboration was present.
2347 if Cumulative_Restrictions
.Set
2348 (No_Standard_Allocators_After_Elaboration
)
2350 WBI
("with System.Elaboration_Allocators;");
2353 -- Generate start of package body
2356 WBI
("package body " & Ada_Main
& " is");
2359 -- Generate externals for elaboration entities
2361 Gen_Elab_Externals
(Elab_Order
);
2363 if not CodePeer_Mode
then
2364 if not Suppress_Standard_Library_On_Target
then
2366 -- Generate Priority_Specific_Dispatching pragma string
2369 (" Local_Priority_Specific_Dispatching : " &
2370 "constant String := """);
2372 for J
in 0 .. PSD_Pragma_Settings
.Last
loop
2373 Set_Char
(PSD_Pragma_Settings
.Table
(J
));
2377 Write_Statement_Buffer
;
2379 -- Generate Interrupt_State pragma string
2381 Set_String
(" Local_Interrupt_States : constant String := """);
2383 for J
in 0 .. IS_Pragma_Settings
.Last
loop
2384 Set_Char
(IS_Pragma_Settings
.Table
(J
));
2388 Write_Statement_Buffer
;
2392 if not Suppress_Standard_Library_On_Target
then
2394 -- The B.1(39) implementation advice says that the adainit
2395 -- and adafinal routines should be idempotent. Generate a flag to
2396 -- ensure that. This is not needed if we are suppressing the
2397 -- standard library since it would never be referenced.
2399 WBI
(" Is_Elaborated : Boolean := False;");
2401 -- Generate bind environment string
2403 Gen_Bind_Env_String
;
2409 -- Generate the adafinal routine unless there is no finalization to do
2411 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
2412 if Needs_Library_Finalization
then
2413 Gen_Finalize_Library
(Elab_Order
);
2419 Gen_Adainit
(Elab_Order
);
2421 if Bind_Main_Program
then
2425 -- Output object file list and the Ada body is complete
2427 Gen_Object_Files_Options
(Elab_Order
);
2430 WBI
("end " & Ada_Main
& ";");
2432 Close_Binder_Output
;
2433 end Gen_Output_File_Ada
;
2435 ----------------------
2436 -- Gen_Restrictions --
2437 ----------------------
2439 procedure Gen_Restrictions
is
2443 if Suppress_Standard_Library_On_Target
2444 or not System_Restrictions_Used
2449 WBI
(" System.Restrictions.Run_Time_Restrictions :=");
2455 for J
in Cumulative_Restrictions
.Set
'Range loop
2456 Set_Boolean
(Cumulative_Restrictions
.Set
(J
));
2460 if J
/= Cumulative_Restrictions
.Set
'Last and then Count
= 8 then
2461 Write_Statement_Buffer
;
2467 Set_String_Replace
("),");
2468 Write_Statement_Buffer
;
2469 Set_String
(" Value => (");
2471 for J
in Cumulative_Restrictions
.Value
'Range loop
2472 Set_Int
(Int
(Cumulative_Restrictions
.Value
(J
)));
2476 Set_String_Replace
("),");
2477 Write_Statement_Buffer
;
2478 WBI
(" Violated =>");
2482 for J
in Cumulative_Restrictions
.Violated
'Range loop
2483 Set_Boolean
(Cumulative_Restrictions
.Violated
(J
));
2487 if J
/= Cumulative_Restrictions
.Set
'Last and then Count
= 8 then
2488 Write_Statement_Buffer
;
2494 Set_String_Replace
("),");
2495 Write_Statement_Buffer
;
2496 Set_String
(" Count => (");
2498 for J
in Cumulative_Restrictions
.Count
'Range loop
2499 Set_Int
(Int
(Cumulative_Restrictions
.Count
(J
)));
2503 Set_String_Replace
("),");
2504 Write_Statement_Buffer
;
2505 Set_String
(" Unknown => (");
2507 for J
in Cumulative_Restrictions
.Unknown
'Range loop
2508 Set_Boolean
(Cumulative_Restrictions
.Unknown
(J
));
2512 Set_String_Replace
("))");
2514 Write_Statement_Buffer
;
2515 end Gen_Restrictions
;
2521 -- This routine generates lines such as:
2523 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2524 -- pragma Export (C, unnnnn, unam);
2526 -- for each unit, where unam is the unit name suffixed by either B or S for
2527 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2528 -- the version number, and nnnnn is a 5-digits serial number.
2530 procedure Gen_Versions
is
2531 Ubuf
: String (1 .. 6) := "u00000";
2533 procedure Increment_Ubuf
;
2534 -- Little procedure to increment the serial number
2536 --------------------
2537 -- Increment_Ubuf --
2538 --------------------
2540 procedure Increment_Ubuf
is
2542 for J
in reverse Ubuf
'Range loop
2543 Ubuf
(J
) := Character'Succ (Ubuf
(J
));
2544 exit when Ubuf
(J
) <= '9';
2549 -- Start of processing for Gen_Versions
2554 WBI
(" type Version_32 is mod 2 ** 32;");
2555 for U
in Units
.First
.. Units
.Last
loop
2556 if not Units
.Table
(U
).SAL_Interface
2557 and then (not Bind_For_Library
2558 or else Units
.Table
(U
).Directly_Scanned
)
2561 WBI
(" " & Ubuf
& " : constant Version_32 := 16#" &
2562 Units
.Table
(U
).Version
& "#;");
2563 Set_String
(" pragma Export (C, ");
2565 Set_String
(", """);
2567 Get_Name_String
(Units
.Table
(U
).Uname
);
2569 for K
in 1 .. Name_Len
loop
2570 if Name_Buffer
(K
) = '.' then
2574 elsif Name_Buffer
(K
) = '%' then
2578 Set_Char
(Name_Buffer
(K
));
2582 if Name_Buffer
(Name_Len
) = 's' then
2588 Set_String
(""");");
2589 Write_Statement_Buffer
;
2594 ------------------------
2595 -- Get_Main_Unit_Name --
2596 ------------------------
2598 function Get_Main_Unit_Name
(S
: String) return String is
2599 Result
: String := S
;
2602 for J
in S
'Range loop
2603 if Result
(J
) = '.' then
2609 end Get_Main_Unit_Name
;
2611 -----------------------
2612 -- Get_Ada_Main_Name --
2613 -----------------------
2615 function Get_Ada_Main_Name
return String is
2616 Suffix
: constant String := "_00";
2617 Name
: String (1 .. Opt
.Ada_Main_Name
.all'Length + Suffix
'Length) :=
2618 Opt
.Ada_Main_Name
.all & Suffix
;
2622 -- For CodePeer, we want reproducible names (independent of other mains
2623 -- that may or may not be present) that don't collide when analyzing
2624 -- multiple mains and which are easily recognizable as "ada_main" names.
2626 if CodePeer_Mode
then
2627 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2631 Get_Main_Unit_Name
(Name_Buffer
(1 .. Name_Len
- 2));
2634 -- This loop tries the following possibilities in order
2640 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2641 -- it is set to 'ada_main'.
2643 for J
in 0 .. 99 loop
2645 Nlen
:= Name
'Length - Suffix
'Length;
2647 Nlen
:= Name
'Length;
2648 Name
(Name
'Last) := Character'Val (J
mod 10 + Character'Pos ('0'));
2649 Name
(Name
'Last - 1) :=
2650 Character'Val (J
/ 10 + Character'Pos ('0'));
2653 for K
in ALIs
.First
.. ALIs
.Last
loop
2654 for L
in ALIs
.Table
(K
).First_Unit
.. ALIs
.Table
(K
).Last_Unit
loop
2656 -- Get unit name, removing %b or %e at end
2658 Get_Name_String
(Units
.Table
(L
).Uname
);
2659 Name_Len
:= Name_Len
- 2;
2661 if Name_Buffer
(1 .. Name_Len
) = Name
(1 .. Nlen
) then
2667 return Name
(1 .. Nlen
);
2673 -- If we fall through, just use a peculiar unlikely name
2675 return ("Qwertyuiop");
2676 end Get_Ada_Main_Name
;
2682 function Get_Main_Name
return String is
2684 -- Explicit name given with -M switch
2686 if Bind_Alternate_Main_Name
then
2687 return Alternate_Main_Name
.all;
2689 -- Case of main program name to be used directly
2691 elsif Use_Ada_Main_Program_Name_On_Target
then
2693 -- Get main program name
2695 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2697 -- If this is a child name, return only the name of the child, since
2698 -- we can't have dots in a nested program name. Note that we do not
2699 -- include the %b at the end of the unit name.
2701 for J
in reverse 1 .. Name_Len
- 2 loop
2702 if J
= 1 or else Name_Buffer
(J
- 1) = '.' then
2703 return Name_Buffer
(J
.. Name_Len
- 2);
2707 raise Program_Error
; -- impossible exit
2709 -- Case where "main" is to be used as default
2716 ---------------------
2717 -- Get_WC_Encoding --
2718 ---------------------
2720 function Get_WC_Encoding
return Character is
2722 -- If encoding method specified by -W switch, then return it
2724 if Wide_Character_Encoding_Method_Specified
then
2725 return WC_Encoding_Letters
(Wide_Character_Encoding_Method
);
2727 -- If no main program, and not specified, set brackets, we really have
2728 -- no better choice. If some other encoding is required when there is
2729 -- no main, it must be set explicitly using -Wx.
2731 -- Note: if the ALI file always passed the wide character encoding of
2732 -- every file, then we could use the encoding of the initial specified
2733 -- file, but this information is passed only for potential main
2734 -- programs. We could fix this sometime, but it is a very minor point
2735 -- (wide character default encoding for [Wide_[Wide_]]Text_IO when there
2736 -- is no main program).
2738 elsif No_Main_Subprogram
then
2741 -- Otherwise if there is a main program, take encoding from it
2744 return ALIs
.Table
(ALIs
.First
).WC_Encoding
;
2746 end Get_WC_Encoding
;
2752 function Has_Finalizer
(Elab_Order
: Unit_Id_Array
) return Boolean is
2757 for E
in reverse Elab_Order
'Range loop
2758 Unum
:= Elab_Order
(E
);
2759 U
:= Units
.Table
(Unum
);
2761 -- We are only interested in non-generic packages
2763 if U
.Unit_Kind
= 'p'
2764 and then U
.Has_Finalizer
2765 and then not U
.Is_Generic
2766 and then not U
.No_Elab
2779 function Hash
(Nam
: Name_Id
) return Header_Num
is
2781 return Int
(Nam
- Names_Low_Bound
) rem Header_Num
'Last;
2784 ----------------------
2785 -- Lt_Linker_Option --
2786 ----------------------
2788 function Lt_Linker_Option
(Op1
: Natural; Op2
: Natural) return Boolean is
2790 -- Sort internal files last
2792 if Linker_Options
.Table
(Op1
).Internal_File
2794 Linker_Options
.Table
(Op2
).Internal_File
2796 -- Note: following test uses False < True
2798 return Linker_Options
.Table
(Op1
).Internal_File
2800 Linker_Options
.Table
(Op2
).Internal_File
;
2802 -- If both internal or both non-internal, sort according to the
2803 -- elaboration position. A unit that is elaborated later should come
2804 -- earlier in the linker options list.
2807 return Units
.Table
(Linker_Options
.Table
(Op1
).Unit
).Elab_Position
2809 Units
.Table
(Linker_Options
.Table
(Op2
).Unit
).Elab_Position
;
2811 end Lt_Linker_Option
;
2813 ------------------------
2814 -- Move_Linker_Option --
2815 ------------------------
2817 procedure Move_Linker_Option
(From
: Natural; To
: Natural) is
2819 Linker_Options
.Table
(To
) := Linker_Options
.Table
(From
);
2820 end Move_Linker_Option
;
2822 ----------------------------
2823 -- Resolve_Binder_Options --
2824 ----------------------------
2826 procedure Resolve_Binder_Options
(Elab_Order
: Unit_Id_Array
) is
2827 procedure Check_Package
(Var
: in out Boolean; Name
: String);
2828 -- Set Var to true iff the current identifier in Namet is Name. Do
2829 -- nothing if it doesn't match. This procedure is just a helper to
2830 -- avoid explicitly dealing with length.
2836 procedure Check_Package
(Var
: in out Boolean; Name
: String) is
2838 if Name_Len
= Name
'Length
2839 and then Name_Buffer
(1 .. Name_Len
) = Name
2845 -- Start of processing for Resolve_Binder_Options
2848 for E
in Elab_Order
'Range loop
2849 Get_Name_String
(Units
.Table
(Elab_Order
(E
)).Uname
);
2851 -- This is not a perfect approach, but is the current protocol
2852 -- between the run-time and the binder to indicate that tasking is
2853 -- used: System.OS_Interface should always be used by any tasking
2856 Check_Package
(With_GNARL
, "system.os_interface%s");
2858 -- Ditto for the use of restricted tasking
2861 (System_Tasking_Restricted_Stages_Used
,
2862 "system.tasking.restricted.stages%s");
2864 -- Ditto for the use of interrupts
2866 Check_Package
(System_Interrupts_Used
, "system.interrupts%s");
2868 -- Ditto for the use of dispatching domains
2871 (Dispatching_Domains_Used
,
2872 "system.multiprocessors.dispatching_domains%s");
2874 -- Ditto for the use of restrictions
2876 Check_Package
(System_Restrictions_Used
, "system.restrictions%s");
2878 -- Ditto for use of an SMP bareboard runtime
2880 Check_Package
(System_BB_CPU_Primitives_Multiprocessors_Used
,
2881 "system.bb.cpu_primitives.multiprocessors%s");
2883 -- Ditto for System.Version_Control, which is used for Version and
2884 -- Body_Version attributes.
2886 Check_Package
(System_Version_Control_Used
,
2887 "system.version_control%s");
2889 end Resolve_Binder_Options
;
2895 procedure Set_Bind_Env
(Key
, Value
: String) is
2897 -- The lengths of Key and Value are stored as single bytes
2899 if Key
'Length > 255 then
2900 Osint
.Fail
("bind environment key """ & Key
& """ too long");
2903 if Value
'Length > 255 then
2904 Osint
.Fail
("bind environment value """ & Value
& """ too long");
2907 Bind_Environment
.Set
(Name_Find
(Key
), Name_Find
(Value
));
2914 procedure Set_Boolean
(B
: Boolean) is
2915 False_Str
: constant String := "False";
2916 True_Str
: constant String := "True";
2920 Statement_Buffer
(Stm_Last
+ 1 .. Stm_Last
+ True_Str
'Length) :=
2922 Stm_Last
:= Stm_Last
+ True_Str
'Length;
2924 Statement_Buffer
(Stm_Last
+ 1 .. Stm_Last
+ False_Str
'Length) :=
2926 Stm_Last
:= Stm_Last
+ False_Str
'Length;
2934 procedure Set_Char
(C
: Character) is
2936 Stm_Last
:= Stm_Last
+ 1;
2937 Statement_Buffer
(Stm_Last
) := C
;
2944 procedure Set_Int
(N
: Int
) is
2955 Stm_Last
:= Stm_Last
+ 1;
2956 Statement_Buffer
(Stm_Last
) :=
2957 Character'Val (N
mod 10 + Character'Pos ('0'));
2961 -------------------------
2962 -- Set_IS_Pragma_Table --
2963 -------------------------
2965 procedure Set_IS_Pragma_Table
is
2967 for F
in ALIs
.First
.. ALIs
.Last
loop
2968 for K
in ALIs
.Table
(F
).First_Interrupt_State
..
2969 ALIs
.Table
(F
).Last_Interrupt_State
2972 Inum
: constant Int
:=
2973 Interrupt_States
.Table
(K
).Interrupt_Id
;
2974 Stat
: constant Character :=
2975 Interrupt_States
.Table
(K
).Interrupt_State
;
2978 while IS_Pragma_Settings
.Last
< Inum
loop
2979 IS_Pragma_Settings
.Append
('n');
2982 IS_Pragma_Settings
.Table
(Inum
) := Stat
;
2986 end Set_IS_Pragma_Table
;
2988 ---------------------------
2989 -- Set_Main_Program_Name --
2990 ---------------------------
2992 procedure Set_Main_Program_Name
is
2994 -- Note that name has %b on the end which we ignore
2996 -- First we output the initial _ada_ since we know that the main program
2997 -- is a library level subprogram.
2999 Set_String
("_ada_");
3001 -- Copy name, changing dots to double underscores
3003 for J
in 1 .. Name_Len
- 2 loop
3004 if Name_Buffer
(J
) = '.' then
3007 Set_Char
(Name_Buffer
(J
));
3010 end Set_Main_Program_Name
;
3012 ---------------------
3013 -- Set_Name_Buffer --
3014 ---------------------
3016 procedure Set_Name_Buffer
is
3018 for J
in 1 .. Name_Len
loop
3019 Set_Char
(Name_Buffer
(J
));
3021 end Set_Name_Buffer
;
3023 -------------------------
3024 -- Set_PSD_Pragma_Table --
3025 -------------------------
3027 procedure Set_PSD_Pragma_Table
is
3029 for F
in ALIs
.First
.. ALIs
.Last
loop
3030 for K
in ALIs
.Table
(F
).First_Specific_Dispatching
..
3031 ALIs
.Table
(F
).Last_Specific_Dispatching
3034 DTK
: Specific_Dispatching_Record
3035 renames Specific_Dispatching
.Table
(K
);
3038 while PSD_Pragma_Settings
.Last
< DTK
.Last_Priority
loop
3039 PSD_Pragma_Settings
.Append
('F');
3042 for Prio
in DTK
.First_Priority
.. DTK
.Last_Priority
loop
3043 PSD_Pragma_Settings
.Table
(Prio
) := DTK
.Dispatching_Policy
;
3048 end Set_PSD_Pragma_Table
;
3054 procedure Set_String
(S
: String) is
3056 Statement_Buffer
(Stm_Last
+ 1 .. Stm_Last
+ S
'Length) := S
;
3057 Stm_Last
:= Stm_Last
+ S
'Length;
3060 ------------------------
3061 -- Set_String_Replace --
3062 ------------------------
3064 procedure Set_String_Replace
(S
: String) is
3066 Statement_Buffer
(Stm_Last
- S
'Length + 1 .. Stm_Last
) := S
;
3067 end Set_String_Replace
;
3073 procedure Set_Unit_Name
is
3075 for J
in 1 .. Name_Len
- 2 loop
3076 if Name_Buffer
(J
) = '.' then
3079 Set_Char
(Name_Buffer
(J
));
3084 ---------------------
3085 -- Set_Unit_Number --
3086 ---------------------
3088 procedure Set_Unit_Number
(U
: Unit_Id
) is
3089 Num_Units
: constant Nat
:= Nat
(Units
.Last
) - Nat
(Unit_Id
'First);
3090 Unum
: constant Nat
:= Nat
(U
) - Nat
(Unit_Id
'First);
3093 if Num_Units
>= 10 and then Unum
< 10 then
3097 if Num_Units
>= 100 and then Unum
< 100 then
3102 end Set_Unit_Number
;
3104 ---------------------
3105 -- Write_Bind_Line --
3106 ---------------------
3108 procedure Write_Bind_Line
(S
: String) is
3110 -- Need to strip trailing LF from S
3112 WBI
(S
(S
'First .. S
'Last - 1));
3113 end Write_Bind_Line
;
3115 ----------------------------
3116 -- Write_Statement_Buffer --
3117 ----------------------------
3119 procedure Write_Statement_Buffer
is
3121 WBI
(Statement_Buffer
(1 .. Stm_Last
));
3123 end Write_Statement_Buffer
;
3125 procedure Write_Statement_Buffer
(S
: String) is
3128 Write_Statement_Buffer
;
3129 end Write_Statement_Buffer
;