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 Binde
; use Binde
;
28 with Casing
; use Casing
;
29 with Fname
; use Fname
;
30 with Gnatvsn
; use Gnatvsn
;
32 with Namet
; use Namet
;
34 with Osint
; use Osint
;
35 with Osint
.B
; use Osint
.B
;
36 with Output
; use Output
;
37 with Rident
; use Rident
;
38 with Stringt
; use Stringt
;
39 with Table
; use Table
;
40 with Targparm
; use Targparm
;
41 with Types
; use Types
;
43 with System
.OS_Lib
; use System
.OS_Lib
;
44 with System
.WCh_Con
; use System
.WCh_Con
;
46 with GNAT
.Heap_Sort_A
; use GNAT
.Heap_Sort_A
;
49 package body Bindgen
is
51 Statement_Buffer
: String (1 .. 1000);
52 -- Buffer used for constructing output statements
55 -- Last location in Statement_Buffer currently set
57 With_GNARL
: Boolean := False;
58 -- Flag which indicates whether the program uses the GNARL library
59 -- (presence of the unit System.OS_Interface)
61 Num_Elab_Calls
: Nat
:= 0;
62 -- Number of generated calls to elaboration routines
64 System_Restrictions_Used
: Boolean := False;
65 -- Flag indicating whether the unit System.Restrictions is in the closure
66 -- of the partition. This is set by Resolve_Binder_Options, and is used
67 -- to determine whether or not to initialize the restrictions information
68 -- in the body of the binder generated file (we do not want to do this
69 -- unconditionally, since it drags in the System.Restrictions unit
70 -- unconditionally, which is unpleasand, especially for ZFP etc.)
72 Dispatching_Domains_Used
: Boolean := False;
73 -- Flag indicating whether multiprocessor dispatching domains are used in
74 -- the closure of the partition. This is set by Resolve_Binder_Options, and
75 -- is used to call the routine to disallow the creation of new dispatching
76 -- domains just before calling the main procedure from the environment
79 System_Tasking_Restricted_Stages_Used
: Boolean := False;
80 -- Flag indicating whether the unit System.Tasking.Restricted.Stages is in
81 -- the closure of the partition. This is set by Resolve_Binder_Options,
82 -- and it used to call a routine to active all the tasks at the end of
83 -- the elaboration when partition elaboration policy is sequential.
85 System_Interrupts_Used
: Boolean := False;
86 -- Flag indicating whether the unit System.Interrups is in the closure of
87 -- the partition. This is set by Resolve_Binder_Options, and it used to
88 -- attach interrupt handlers at the end of the elaboration when partition
89 -- elaboration policy is sequential.
91 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
;
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
;
287 -- Generate sequence of elaboration calls
289 procedure Gen_Elab_Externals
;
290 -- Generate sequence of external declarations for elaboration
292 procedure Gen_Elab_Order
;
293 -- Generate comments showing elaboration order chosen
295 procedure Gen_Finalize_Library
;
296 -- Generate a sequence of finalization calls to elaborated packages
299 -- Generate procedure main
301 procedure Gen_Object_Files_Options
;
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
(Filename
: String);
307 -- Generate Ada output file
309 procedure Gen_Restrictions
;
310 -- Generate initialization of restrictions variable
312 procedure Gen_Versions
;
313 -- Output series of definitions for unit versions
315 function Get_Ada_Main_Name
return String;
316 -- This function is used for the Ada main output to compute a usable name
317 -- for the generated main program. The normal main program name is
318 -- Ada_Main, but this won't work if the user has a unit with this name.
319 -- This function tries Ada_Main first, and if there is such a clash, then
320 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
322 function Get_Main_Unit_Name
(S
: String) return String;
323 -- Return the main unit name corresponding to S by replacing '.' with '_'
325 function Get_Main_Name
return String;
326 -- This function is used in the main output case to compute the correct
327 -- external main program. It is "main" by default, unless the flag
328 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
329 -- of the Ada main name without the "_ada". This default can be overridden
330 -- explicitly using the -Mname binder switch.
332 function Get_WC_Encoding
return Character;
333 -- Return wide character encoding method to set as WC_Encoding in output.
334 -- If -W has been used, returns the specified encoding, otherwise returns
335 -- the encoding method used for the main program source. If there is no
336 -- main program source (-z switch used), returns brackets ('b').
338 function Has_Finalizer
return Boolean;
339 -- Determine whether the current unit has at least one library-level
342 function Lt_Linker_Option
(Op1
, Op2
: Natural) return Boolean;
343 -- Compare linker options, when sorting, first according to
344 -- Is_Internal_File (internal files come later) and then by
345 -- elaboration order position (latest to earliest).
347 procedure Move_Linker_Option
(From
: Natural; To
: Natural);
348 -- Move routine for sorting linker options
350 procedure Resolve_Binder_Options
;
351 -- Set the value of With_GNARL
353 procedure Set_Char
(C
: Character);
354 -- Set given character in Statement_Buffer at the Last + 1 position
355 -- and increment Last by one to reflect the stored character.
357 procedure Set_Int
(N
: Int
);
358 -- Set given value in decimal in Statement_Buffer with no spaces starting
359 -- at the Last + 1 position, and updating Last past the value. A minus sign
360 -- is output for a negative value.
362 procedure Set_Boolean
(B
: Boolean);
363 -- Set given boolean value in Statement_Buffer at the Last + 1 position
364 -- and update Last past the value.
366 procedure Set_IS_Pragma_Table
;
367 -- Initializes contents of IS_Pragma_Settings table from ALI table
369 procedure Set_Main_Program_Name
;
370 -- Given the main program name in Name_Buffer (length in Name_Len) generate
371 -- the name of the routine to be used in the call. The name is generated
372 -- starting at Last + 1, and Last is updated past it.
374 procedure Set_Name_Buffer
;
375 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
377 procedure Set_PSD_Pragma_Table
;
378 -- Initializes contents of PSD_Pragma_Settings table from ALI table
380 procedure Set_String
(S
: String);
381 -- Sets characters of given string in Statement_Buffer, starting at the
382 -- Last + 1 position, and updating last past the string value.
384 procedure Set_String_Replace
(S
: String);
385 -- Replaces the last S'Length characters in the Statement_Buffer with the
386 -- characters of S. The caller must ensure that these characters do in fact
387 -- exist in the Statement_Buffer.
389 procedure Set_Unit_Name
;
390 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
391 -- starting at the Last + 1 position and update Last past the value.
392 -- Each dot (.) will be qualified into double underscores (__).
394 procedure Set_Unit_Number
(U
: Unit_Id
);
395 -- Sets unit number (first unit is 1, leading zeroes output to line up all
396 -- output unit numbers nicely as required by the value, and by the total
399 procedure Write_Statement_Buffer
;
400 -- Write out contents of statement buffer up to Last, and reset Last to 0
402 procedure Write_Statement_Buffer
(S
: String);
403 -- First writes its argument (using Set_String (S)), then writes out the
404 -- contents of statement buffer up to Last, and reset Last to 0
406 procedure Write_Bind_Line
(S
: String);
407 -- Write S (an LF-terminated string) to the binder file (for use with
408 -- Set_Special_Output).
414 procedure Gen_Adafinal
is
416 WBI
(" procedure " & Ada_Final_Name
.all & " is");
418 if Bind_Main_Program
and not CodePeer_Mode
then
419 WBI
(" procedure s_stalib_adafinal;");
420 Set_String
(" pragma Import (C, s_stalib_adafinal, ");
421 Set_String
("""system__standard_library__adafinal"");");
422 Write_Statement_Buffer
;
426 WBI
(" procedure Runtime_Finalize;");
427 WBI
(" pragma Import (C, Runtime_Finalize, " &
428 """__gnat_runtime_finalize"");");
432 if not CodePeer_Mode
then
433 WBI
(" if not Is_Elaborated then");
436 WBI
(" Is_Elaborated := False;");
439 WBI
(" Runtime_Finalize;");
441 -- By default (real targets), finalization is done differently depending
442 -- on whether this is the main program or a library.
444 if not CodePeer_Mode
then
445 if Bind_Main_Program
then
446 WBI
(" s_stalib_adafinal;");
447 elsif Lib_Final_Built
then
448 WBI
(" finalize_library;");
453 -- Pragma Import C cannot be used on virtual targets, therefore call the
454 -- runtime finalization routine directly in CodePeer mode, where
455 -- imported functions are ignored.
458 WBI
(" System.Standard_Library.Adafinal;");
461 WBI
(" end " & Ada_Final_Name
.all & ";");
469 procedure Gen_Adainit
is
470 Main_Priority
: Int
renames ALIs
.Table
(ALIs
.First
).Main_Priority
;
471 Main_CPU
: Int
renames ALIs
.Table
(ALIs
.First
).Main_CPU
;
474 -- Declare the access-to-subprogram type used for initialization of
475 -- of __gnat_finalize_library_objects. This is declared at library
476 -- level for compatibility with the type used in System.Soft_Links.
477 -- The import of the soft link which performs library-level object
478 -- finalization does not work for CodePeer, so regular Ada is used in
479 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
480 -- tasks are non-terminating, so we do not want finalization.
482 if not Suppress_Standard_Library_On_Target
483 and then not CodePeer_Mode
484 and then not Configurable_Run_Time_On_Target
486 WBI
(" type No_Param_Proc is access procedure;");
490 WBI
(" procedure " & Ada_Init_Name
.all & " is");
492 -- In CodePeer mode, simplify adainit procedure by only calling
493 -- elaboration procedures.
495 if CodePeer_Mode
then
498 -- If the standard library is suppressed, then the only global variables
499 -- that might be needed (by the Ravenscar profile) are the priority and
500 -- the processor for the environment task.
502 elsif Suppress_Standard_Library_On_Target
then
503 if Main_Priority
/= No_Main_Priority
then
504 WBI
(" Main_Priority : Integer;");
505 WBI
(" pragma Import (C, Main_Priority," &
506 " ""__gl_main_priority"");");
510 if Main_CPU
/= No_Main_CPU
then
511 WBI
(" Main_CPU : Integer;");
512 WBI
(" pragma Import (C, Main_CPU," &
513 " ""__gl_main_cpu"");");
517 if System_Interrupts_Used
518 and then Partition_Elaboration_Policy_Specified
= 'S'
520 WBI
(" procedure Install_Restricted_Handlers_Sequential;");
521 WBI
(" pragma Import (C," &
522 "Install_Restricted_Handlers_Sequential," &
523 " ""__gnat_attach_all_handlers"");");
527 if System_Tasking_Restricted_Stages_Used
528 and then Partition_Elaboration_Policy_Specified
= 'S'
530 WBI
(" Partition_Elaboration_Policy : Character;");
531 WBI
(" pragma Import (C, Partition_Elaboration_Policy," &
532 " ""__gnat_partition_elaboration_policy"");");
534 WBI
(" procedure Activate_All_Tasks_Sequential;");
535 WBI
(" pragma Import (C, Activate_All_Tasks_Sequential," &
536 " ""__gnat_activate_all_tasks"");");
540 if System_BB_CPU_Primitives_Multiprocessors_Used
then
541 WBI
(" procedure Start_Slave_CPUs;");
542 WBI
(" pragma Import (C, Start_Slave_CPUs," &
543 " ""__gnat_start_slave_cpus"");");
548 if Main_Priority
/= No_Main_Priority
then
549 Set_String
(" Main_Priority := ");
550 Set_Int
(Main_Priority
);
552 Write_Statement_Buffer
;
555 if Main_CPU
/= No_Main_CPU
then
556 Set_String
(" Main_CPU := ");
559 Write_Statement_Buffer
;
562 if System_Tasking_Restricted_Stages_Used
563 and then Partition_Elaboration_Policy_Specified
= 'S'
565 Set_String
(" Partition_Elaboration_Policy := '");
566 Set_Char
(Partition_Elaboration_Policy_Specified
);
568 Write_Statement_Buffer
;
571 if Main_Priority
= No_Main_Priority
572 and then Main_CPU
= No_Main_CPU
573 and then not System_Tasking_Restricted_Stages_Used
578 -- Normal case (standard library not suppressed). Set all global values
579 -- used by the run time.
582 WBI
(" Main_Priority : Integer;");
583 WBI
(" pragma Import (C, Main_Priority, " &
584 """__gl_main_priority"");");
585 WBI
(" Time_Slice_Value : Integer;");
586 WBI
(" pragma Import (C, Time_Slice_Value, " &
587 """__gl_time_slice_val"");");
588 WBI
(" WC_Encoding : Character;");
589 WBI
(" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
590 WBI
(" Locking_Policy : Character;");
591 WBI
(" pragma Import (C, Locking_Policy, " &
592 """__gl_locking_policy"");");
593 WBI
(" Queuing_Policy : Character;");
594 WBI
(" pragma Import (C, Queuing_Policy, " &
595 """__gl_queuing_policy"");");
596 WBI
(" Task_Dispatching_Policy : Character;");
597 WBI
(" pragma Import (C, Task_Dispatching_Policy, " &
598 """__gl_task_dispatching_policy"");");
599 WBI
(" Priority_Specific_Dispatching : System.Address;");
600 WBI
(" pragma Import (C, Priority_Specific_Dispatching, " &
601 """__gl_priority_specific_dispatching"");");
602 WBI
(" Num_Specific_Dispatching : Integer;");
603 WBI
(" pragma Import (C, Num_Specific_Dispatching, " &
604 """__gl_num_specific_dispatching"");");
605 WBI
(" Main_CPU : Integer;");
606 WBI
(" pragma Import (C, Main_CPU, " &
607 """__gl_main_cpu"");");
609 WBI
(" Interrupt_States : System.Address;");
610 WBI
(" pragma Import (C, Interrupt_States, " &
611 """__gl_interrupt_states"");");
612 WBI
(" Num_Interrupt_States : Integer;");
613 WBI
(" pragma Import (C, Num_Interrupt_States, " &
614 """__gl_num_interrupt_states"");");
615 WBI
(" Unreserve_All_Interrupts : Integer;");
616 WBI
(" pragma Import (C, Unreserve_All_Interrupts, " &
617 """__gl_unreserve_all_interrupts"");");
619 if Exception_Tracebacks
or Exception_Tracebacks_Symbolic
then
620 WBI
(" Exception_Tracebacks : Integer;");
621 WBI
(" pragma Import (C, Exception_Tracebacks, " &
622 """__gl_exception_tracebacks"");");
624 if Exception_Tracebacks_Symbolic
then
625 WBI
(" Exception_Tracebacks_Symbolic : Integer;");
626 WBI
(" pragma Import (C, Exception_Tracebacks_Symbolic, " &
627 """__gl_exception_tracebacks_symbolic"");");
631 WBI
(" Detect_Blocking : Integer;");
632 WBI
(" pragma Import (C, Detect_Blocking, " &
633 """__gl_detect_blocking"");");
634 WBI
(" Default_Stack_Size : Integer;");
635 WBI
(" pragma Import (C, Default_Stack_Size, " &
636 """__gl_default_stack_size"");");
637 WBI
(" Leap_Seconds_Support : Integer;");
638 WBI
(" pragma Import (C, Leap_Seconds_Support, " &
639 """__gl_leap_seconds_support"");");
640 WBI
(" Bind_Env_Addr : System.Address;");
641 WBI
(" pragma Import (C, Bind_Env_Addr, " &
642 """__gl_bind_env_addr"");");
644 -- Import entry point for elaboration time signal handler
645 -- installation, and indication of if it's been called previously.
648 WBI
(" procedure Runtime_Initialize " &
649 "(Install_Handler : Integer);");
650 WBI
(" pragma Import (C, Runtime_Initialize, " &
651 """__gnat_runtime_initialize"");");
653 -- Import handlers attach procedure for sequential elaboration policy
655 if System_Interrupts_Used
656 and then Partition_Elaboration_Policy_Specified
= 'S'
658 WBI
(" procedure Install_Restricted_Handlers_Sequential;");
659 WBI
(" pragma Import (C," &
660 "Install_Restricted_Handlers_Sequential," &
661 " ""__gnat_attach_all_handlers"");");
665 -- Import task activation procedure for sequential elaboration
668 if System_Tasking_Restricted_Stages_Used
669 and then Partition_Elaboration_Policy_Specified
= 'S'
671 WBI
(" Partition_Elaboration_Policy : Character;");
672 WBI
(" pragma Import (C, Partition_Elaboration_Policy," &
673 " ""__gnat_partition_elaboration_policy"");");
675 WBI
(" procedure Activate_All_Tasks_Sequential;");
676 WBI
(" pragma Import (C, Activate_All_Tasks_Sequential," &
677 " ""__gnat_activate_all_tasks"");");
680 -- Import procedure to start slave cpus for bareboard runtime
682 if System_BB_CPU_Primitives_Multiprocessors_Used
then
683 WBI
(" procedure Start_Slave_CPUs;");
684 WBI
(" pragma Import (C, Start_Slave_CPUs," &
685 " ""__gnat_start_slave_cpus"");");
688 -- For restricted run-time libraries (ZFP and Ravenscar)
689 -- tasks are non-terminating, so we do not want finalization.
691 if not Configurable_Run_Time_On_Target
then
693 WBI
(" Finalize_Library_Objects : No_Param_Proc;");
694 WBI
(" pragma Import (C, Finalize_Library_Objects, " &
695 """__gnat_finalize_library_objects"");");
698 -- Initialize stack limit variable of the environment task if the
699 -- stack check method is stack limit and stack check is enabled.
701 if Stack_Check_Limits_On_Target
702 and then (Stack_Check_Default_On_Target
or Stack_Check_Switch_Set
)
705 WBI
(" procedure Initialize_Stack_Limit;");
706 WBI
(" pragma Import (C, Initialize_Stack_Limit, " &
707 """__gnat_initialize_stack_limit"");");
710 -- When dispatching domains are used then we need to signal it
711 -- before calling the main procedure.
713 if Dispatching_Domains_Used
then
714 WBI
(" procedure Freeze_Dispatching_Domains;");
715 WBI
(" pragma Import");
716 WBI
(" (Ada, Freeze_Dispatching_Domains, "
717 & """__gnat_freeze_dispatching_domains"");");
720 -- Start of processing for Adainit
723 WBI
(" if Is_Elaborated then");
726 WBI
(" Is_Elaborated := True;");
728 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
729 -- restriction No_Standard_Allocators_After_Elaboration is active.
731 if Cumulative_Restrictions
.Set
732 (No_Standard_Allocators_After_Elaboration
)
734 WBI
(" System.Elaboration_Allocators."
735 & "Mark_Start_Of_Elaboration;");
738 -- Generate assignments to initialize globals
740 Set_String
(" Main_Priority := ");
741 Set_Int
(Main_Priority
);
743 Write_Statement_Buffer
;
745 Set_String
(" Time_Slice_Value := ");
747 if Task_Dispatching_Policy_Specified
= 'F'
748 and then ALIs
.Table
(ALIs
.First
).Time_Slice_Value
= -1
752 Set_Int
(ALIs
.Table
(ALIs
.First
).Time_Slice_Value
);
756 Write_Statement_Buffer
;
758 Set_String
(" WC_Encoding := '");
759 Set_Char
(Get_WC_Encoding
);
762 Write_Statement_Buffer
;
764 Set_String
(" Locking_Policy := '");
765 Set_Char
(Locking_Policy_Specified
);
767 Write_Statement_Buffer
;
769 Set_String
(" Queuing_Policy := '");
770 Set_Char
(Queuing_Policy_Specified
);
772 Write_Statement_Buffer
;
774 Set_String
(" Task_Dispatching_Policy := '");
775 Set_Char
(Task_Dispatching_Policy_Specified
);
777 Write_Statement_Buffer
;
779 if System_Tasking_Restricted_Stages_Used
780 and then Partition_Elaboration_Policy_Specified
= 'S'
782 Set_String
(" Partition_Elaboration_Policy := '");
783 Set_Char
(Partition_Elaboration_Policy_Specified
);
785 Write_Statement_Buffer
;
790 WBI
(" Priority_Specific_Dispatching :=");
791 WBI
(" Local_Priority_Specific_Dispatching'Address;");
793 Set_String
(" Num_Specific_Dispatching := ");
794 Set_Int
(PSD_Pragma_Settings
.Last
+ 1);
796 Write_Statement_Buffer
;
798 Set_String
(" Main_CPU := ");
801 Write_Statement_Buffer
;
803 WBI
(" Interrupt_States := Local_Interrupt_States'Address;");
805 Set_String
(" Num_Interrupt_States := ");
806 Set_Int
(IS_Pragma_Settings
.Last
+ 1);
808 Write_Statement_Buffer
;
810 Set_String
(" Unreserve_All_Interrupts := ");
812 if Unreserve_All_Interrupts_Specified
then
819 Write_Statement_Buffer
;
821 if Exception_Tracebacks
or Exception_Tracebacks_Symbolic
then
822 WBI
(" Exception_Tracebacks := 1;");
824 if Exception_Tracebacks_Symbolic
then
825 WBI
(" Exception_Tracebacks_Symbolic := 1;");
829 Set_String
(" Detect_Blocking := ");
831 if Detect_Blocking
then
838 Write_Statement_Buffer
;
840 Set_String
(" Default_Stack_Size := ");
841 Set_Int
(Default_Stack_Size
);
843 Write_Statement_Buffer
;
845 Set_String
(" Leap_Seconds_Support := ");
847 if Leap_Seconds_Support
then
854 Write_Statement_Buffer
;
856 if Bind_Env_String_Built
then
857 WBI
(" Bind_Env_Addr := Bind_Env'Address;");
860 -- Generate call to Install_Handler
863 WBI
(" Runtime_Initialize (1);");
866 -- Generate call to set Initialize_Scalar values if active
868 if Initialize_Scalars_Used
then
870 Set_String
(" System.Scalar_Values.Initialize ('");
871 Set_Char
(Initialize_Scalars_Mode1
);
873 Set_Char
(Initialize_Scalars_Mode2
);
875 Write_Statement_Buffer
;
878 -- Generate assignment of default secondary stack size if set
880 if Sec_Stack_Used
and then Default_Sec_Stack_Size
/= -1 then
882 Set_String
(" System.Secondary_Stack.");
883 Set_String
("Default_Secondary_Stack_Size := ");
884 Set_Int
(Opt
.Default_Sec_Stack_Size
);
886 Write_Statement_Buffer
;
889 -- Initialize stack limit variable of the environment task if the
890 -- stack check method is stack limit and stack check is enabled.
892 if Stack_Check_Limits_On_Target
893 and then (Stack_Check_Default_On_Target
or Stack_Check_Switch_Set
)
896 WBI
(" Initialize_Stack_Limit;");
899 -- On CodePeer, the finalization of library objects is not relevant
901 if CodePeer_Mode
then
904 -- If this is the main program case, attach finalize_library to the soft
905 -- link. Do it only when not using a restricted run time, in which case
906 -- tasks are non-terminating, so we do not want library-level
909 elsif Bind_Main_Program
910 and then not Configurable_Run_Time_On_Target
911 and then not Suppress_Standard_Library_On_Target
915 if Lib_Final_Built
then
916 Set_String
(" Finalize_Library_Objects := ");
917 Set_String
("finalize_library'access;");
919 Set_String
(" Finalize_Library_Objects := null;");
922 Write_Statement_Buffer
;
925 -- Generate elaboration calls
927 if not CodePeer_Mode
then
933 if not CodePeer_Mode
then
935 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
936 -- restriction No_Standard_Allocators_After_Elaboration is active.
938 if Cumulative_Restrictions
.Set
939 (No_Standard_Allocators_After_Elaboration
)
942 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
945 -- From this point, no new dispatching domain can be created
947 if Dispatching_Domains_Used
then
948 WBI
(" Freeze_Dispatching_Domains;");
951 -- Sequential partition elaboration policy
953 if Partition_Elaboration_Policy_Specified
= 'S' then
954 if System_Interrupts_Used
then
955 WBI
(" Install_Restricted_Handlers_Sequential;");
958 if System_Tasking_Restricted_Stages_Used
then
959 WBI
(" Activate_All_Tasks_Sequential;");
963 if System_BB_CPU_Primitives_Multiprocessors_Used
then
964 WBI
(" Start_Slave_CPUs;");
968 WBI
(" end " & Ada_Init_Name
.all & ";");
972 -------------------------
973 -- Gen_Bind_Env_String --
974 -------------------------
976 procedure Gen_Bind_Env_String
is
977 KN
, VN
: Name_Id
:= No_Name
;
980 procedure Write_Name_With_Len
(Nam
: Name_Id
);
981 -- Write Nam as a string literal, prefixed with one
982 -- character encoding Nam's length.
984 -------------------------
985 -- Write_Name_With_Len --
986 -------------------------
988 procedure Write_Name_With_Len
(Nam
: Name_Id
) is
990 Get_Name_String
(Nam
);
993 Store_String_Char
(Character'Val (Name_Len
));
994 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
996 Write_String_Table_Entry
(End_String
);
997 end Write_Name_With_Len
;
999 -- Start of processing for Gen_Bind_Env_String
1002 Bind_Environment
.Get_First
(KN
, VN
);
1003 if VN
= No_Name
then
1007 Set_Special_Output
(Write_Bind_Line
'Access);
1009 WBI
(" Bind_Env : aliased constant String :=");
1011 while VN
/= No_Name
loop
1012 Write_Str
(" " & Amp
& ' ');
1013 Write_Name_With_Len
(KN
);
1015 Write_Name_With_Len
(VN
);
1018 Bind_Environment
.Get_Next
(KN
, VN
);
1021 WBI
(" & ASCII.NUL;");
1023 Set_Special_Output
(null);
1025 Bind_Env_String_Built
:= True;
1026 end Gen_Bind_Env_String
;
1028 --------------------------
1029 -- Gen_CodePeer_Wrapper --
1030 --------------------------
1032 procedure Gen_CodePeer_Wrapper
is
1033 Callee_Name
: constant String := "Ada_Main_Program";
1036 if ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1037 WBI
(" procedure " & CodePeer_Wrapper_Name
& " is ");
1039 WBI
(" " & Callee_Name
& ";");
1042 WBI
(" function " & CodePeer_Wrapper_Name
& " return Integer is");
1044 WBI
(" return " & Callee_Name
& ";");
1047 WBI
(" end " & CodePeer_Wrapper_Name
& ";");
1049 end Gen_CodePeer_Wrapper
;
1051 --------------------
1052 -- Gen_Elab_Calls --
1053 --------------------
1055 procedure Gen_Elab_Calls
is
1056 Check_Elab_Flag
: Boolean;
1059 -- Loop through elaboration order entries
1061 for E
in Elab_Order
.First
.. Elab_Order
.Last
loop
1063 Unum
: constant Unit_Id
:= Elab_Order
.Table
(E
);
1064 U
: Unit_Record
renames Units
.Table
(Unum
);
1066 Unum_Spec
: Unit_Id
;
1067 -- This is the unit number of the spec that corresponds to
1068 -- this entry. It is the same as Unum except when the body
1069 -- and spec are different and we are currently processing
1070 -- the body, in which case it is the spec (Unum + 1).
1073 if U
.Utype
= Is_Body
then
1074 Unum_Spec
:= Unum
+ 1;
1079 -- Nothing to do if predefined unit in no run time mode
1081 if No_Run_Time_Mode
and then Is_Predefined_File_Name
(U
.Sfile
) then
1084 -- Likewise if this is an interface to a stand alone library
1086 elsif U
.SAL_Interface
then
1089 -- Case of no elaboration code
1093 -- In CodePeer mode, we special case subprogram bodies which
1094 -- are handled in the 'else' part below, and lead to a call
1095 -- to <subp>'Elab_Subp_Body.
1097 and then (not CodePeer_Mode
1101 or else U
.Utype
= Is_Spec
1102 or else U
.Utype
= Is_Spec_Only
1103 or else U
.Unit_Kind
/= 's')
1105 -- In the case of a body with a separate spec, where the
1106 -- separate spec has an elaboration entity defined, this is
1107 -- where we increment the elaboration entity if one exists
1109 if U
.Utype
= Is_Body
1110 and then Units
.Table
(Unum_Spec
).Set_Elab_Entity
1111 and then not CodePeer_Mode
1114 Set_Unit_Number
(Unum_Spec
);
1115 Set_String
(" := E");
1116 Set_Unit_Number
(Unum_Spec
);
1117 Set_String
(" + 1;");
1118 Write_Statement_Buffer
;
1121 -- Here if elaboration code is present. If binding a library
1122 -- or if there is a non-Ada main subprogram then we generate:
1124 -- if uname_E = 0 then
1125 -- uname'elab_[spec|body];
1127 -- uname_E := uname_E + 1;
1129 -- Otherwise, elaboration routines are called unconditionally:
1131 -- uname'elab_[spec|body];
1132 -- uname_E := uname_E + 1;
1134 -- The uname_E increment is skipped if this is a separate spec,
1135 -- since it will be done when we process the body.
1137 -- In CodePeer mode, we do not generate any reference to xxx_E
1138 -- variables, only calls to 'Elab* subprograms.
1141 -- Check incompatibilities with No_Multiple_Elaboration
1143 if not CodePeer_Mode
1144 and then Cumulative_Restrictions
.Set
(No_Multiple_Elaboration
)
1146 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1148 if Force_Checking_Of_Elaboration_Flags
then
1150 ("-F (force elaboration checks) switch not allowed "
1151 & "with restriction No_Multiple_Elaboration active");
1153 -- Interfacing of libraries not allowed
1155 elsif Interface_Library_Unit
then
1157 ("binding of interfaced libraries not allowed "
1158 & "with restriction No_Multiple_Elaboration active");
1160 -- Non-Ada main program not allowed
1162 elsif not Bind_Main_Program
then
1164 ("non-Ada main program not allowed "
1165 & "with restriction No_Multiple_Elaboration active");
1169 -- OK, see if we need to test elaboration flag
1172 Units
.Table
(Unum_Spec
).Set_Elab_Entity
1173 and then not CodePeer_Mode
1174 and then (Force_Checking_Of_Elaboration_Flags
1175 or Interface_Library_Unit
1176 or not Bind_Main_Program
);
1178 if Check_Elab_Flag
then
1179 Set_String
(" if E");
1180 Set_Unit_Number
(Unum_Spec
);
1181 Set_String
(" = 0 then");
1182 Write_Statement_Buffer
;
1187 Get_Decoded_Name_String_With_Brackets
(U
.Uname
);
1189 if Name_Buffer
(Name_Len
) = 's' then
1190 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 8) :=
1192 Name_Len
:= Name_Len
+ 8;
1194 -- Special case in CodePeer mode for subprogram bodies
1195 -- which correspond to CodePeer 'Elab_Subp_Body special
1198 elsif U
.Unit_Kind
= 's' and CodePeer_Mode
then
1199 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 13) :=
1201 Name_Len
:= Name_Len
+ 13;
1204 Name_Buffer
(Name_Len
- 1 .. Name_Len
+ 8) :=
1206 Name_Len
:= Name_Len
+ 8;
1209 Set_Casing
(U
.Icasing
);
1212 Write_Statement_Buffer
;
1214 if Check_Elab_Flag
then
1218 if U
.Utype
/= Is_Spec
1219 and then not CodePeer_Mode
1220 and then Units
.Table
(Unum_Spec
).Set_Elab_Entity
1223 Set_Unit_Number
(Unum_Spec
);
1224 Set_String
(" := E");
1225 Set_Unit_Number
(Unum_Spec
);
1226 Set_String
(" + 1;");
1227 Write_Statement_Buffer
;
1234 ------------------------
1235 -- Gen_Elab_Externals --
1236 ------------------------
1238 procedure Gen_Elab_Externals
is
1240 if CodePeer_Mode
then
1244 for E
in Elab_Order
.First
.. Elab_Order
.Last
loop
1246 Unum
: constant Unit_Id
:= Elab_Order
.Table
(E
);
1247 U
: Unit_Record
renames Units
.Table
(Unum
);
1250 -- Check for Elab_Entity to be set for this unit
1252 if U
.Set_Elab_Entity
1254 -- Don't generate reference for stand alone library
1256 and then not U
.SAL_Interface
1258 -- Don't generate reference for predefined file in No_Run_Time
1259 -- mode, since we don't include the object files in this case
1263 and then Is_Predefined_File_Name
(U
.Sfile
))
1267 Set_Unit_Number
(Unum
);
1268 Set_String
(" : Short_Integer; pragma Import (Ada, E");
1269 Set_Unit_Number
(Unum
);
1270 Set_String
(", """);
1271 Get_Name_String
(U
.Uname
);
1273 Set_String
("_E"");");
1274 Write_Statement_Buffer
;
1280 end Gen_Elab_Externals
;
1282 --------------------
1283 -- Gen_Elab_Order --
1284 --------------------
1286 procedure Gen_Elab_Order
is
1288 WBI
(" -- BEGIN ELABORATION ORDER");
1290 for J
in Elab_Order
.First
.. Elab_Order
.Last
loop
1291 Set_String
(" -- ");
1292 Get_Name_String
(Units
.Table
(Elab_Order
.Table
(J
)).Uname
);
1294 Write_Statement_Buffer
;
1297 WBI
(" -- END ELABORATION ORDER");
1301 --------------------------
1302 -- Gen_Finalize_Library --
1303 --------------------------
1305 procedure Gen_Finalize_Library
is
1308 Uspec
: Unit_Record
;
1311 procedure Gen_Header
;
1312 -- Generate the header of the finalization routine
1318 procedure Gen_Header
is
1320 WBI
(" procedure finalize_library is");
1324 -- Start of processing for Gen_Finalize_Library
1327 if CodePeer_Mode
then
1331 for E
in reverse Elab_Order
.First
.. Elab_Order
.Last
loop
1332 Unum
:= Elab_Order
.Table
(E
);
1333 U
:= Units
.Table
(Unum
);
1335 -- Dealing with package bodies is a little complicated. In such
1336 -- cases we must retrieve the package spec since it contains the
1337 -- spec of the body finalizer.
1339 if U
.Utype
= Is_Body
then
1341 Uspec
:= Units
.Table
(Unum
);
1346 Get_Name_String
(Uspec
.Uname
);
1348 -- We are only interested in non-generic packages
1350 if U
.Unit_Kind
/= 'p' or else U
.Is_Generic
then
1353 -- That aren't an interface to a stand alone library
1355 elsif U
.SAL_Interface
then
1358 -- Case of no finalization
1360 elsif not U
.Has_Finalizer
then
1362 -- The only case in which we have to do something is if this
1363 -- is a body, with a separate spec, where the separate spec
1364 -- has a finalizer. In that case, this is where we decrement
1365 -- the elaboration entity.
1367 if U
.Utype
= Is_Body
and then Uspec
.Has_Finalizer
then
1368 if not Lib_Final_Built
then
1370 Lib_Final_Built
:= True;
1374 Set_Unit_Number
(Unum
);
1375 Set_String
(" := E");
1376 Set_Unit_Number
(Unum
);
1377 Set_String
(" - 1;");
1378 Write_Statement_Buffer
;
1382 if not Lib_Final_Built
then
1384 Lib_Final_Built
:= True;
1389 -- procedure F<Count>;
1391 Set_String
(" declare");
1392 Write_Statement_Buffer
;
1394 Set_String
(" procedure F");
1397 Write_Statement_Buffer
;
1400 -- pragma Import (Ada, F<Count>,
1401 -- "xx__yy__finalize_[body|spec]");
1403 Set_String
(" pragma Import (Ada, F");
1405 Set_String
(", """);
1407 -- Perform name construction
1410 Set_String
("__finalize_");
1412 -- Package spec processing
1414 if U
.Utype
= Is_Spec
1415 or else U
.Utype
= Is_Spec_Only
1417 Set_String
("spec");
1419 -- Package body processing
1422 Set_String
("body");
1425 Set_String
(""");");
1426 Write_Statement_Buffer
;
1428 -- If binding a library or if there is a non-Ada main subprogram
1429 -- then we generate:
1432 -- uname_E := uname_E - 1;
1433 -- if uname_E = 0 then
1438 -- Otherwise, finalization routines are called unconditionally:
1441 -- uname_E := uname_E - 1;
1445 -- The uname_E decrement is skipped if this is a separate spec,
1446 -- since it will be done when we process the body.
1450 if U
.Utype
/= Is_Spec
then
1452 Set_Unit_Number
(Unum
);
1453 Set_String
(" := E");
1454 Set_Unit_Number
(Unum
);
1455 Set_String
(" - 1;");
1456 Write_Statement_Buffer
;
1459 if Interface_Library_Unit
or not Bind_Main_Program
then
1460 Set_String
(" if E");
1461 Set_Unit_Number
(Unum
);
1462 Set_String
(" = 0 then");
1463 Write_Statement_Buffer
;
1470 Write_Statement_Buffer
;
1472 if Interface_Library_Unit
or not Bind_Main_Program
then
1482 if Lib_Final_Built
then
1484 -- It is possible that the finalization of a library-level object
1485 -- raised an exception. In that case import the actual exception
1486 -- and the routine necessary to raise it.
1489 WBI
(" procedure Reraise_Library_Exception_If_Any;");
1491 Set_String
(" pragma Import (Ada, ");
1492 Set_String
("Reraise_Library_Exception_If_Any, ");
1493 Set_String
("""__gnat_reraise_library_exception_if_any"");");
1494 Write_Statement_Buffer
;
1497 WBI
(" Reraise_Library_Exception_If_Any;");
1499 WBI
(" end finalize_library;");
1502 end Gen_Finalize_Library
;
1508 procedure Gen_Main
is
1510 if not No_Main_Subprogram
then
1512 -- To call the main program, we declare it using a pragma Import
1513 -- Ada with the right link name.
1515 -- It might seem more obvious to "with" the main program, and call
1516 -- it in the normal Ada manner. We do not do this for three
1519 -- 1. It is more efficient not to recompile the main program
1520 -- 2. We are not entitled to assume the source is accessible
1521 -- 3. We don't know what options to use to compile it
1523 -- It is really reason 3 that is most critical (indeed we used
1524 -- to generate the "with", but several regression tests failed).
1526 if ALIs
.Table
(ALIs
.First
).Main_Program
= Func
then
1527 WBI
(" function Ada_Main_Program return Integer;");
1529 WBI
(" procedure Ada_Main_Program;");
1532 Set_String
(" pragma Import (Ada, Ada_Main_Program, """);
1533 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
1534 Set_Main_Program_Name
;
1535 Set_String
(""");");
1537 Write_Statement_Buffer
;
1540 -- For CodePeer, declare a wrapper for the user-defined main program
1542 if CodePeer_Mode
then
1543 Gen_CodePeer_Wrapper
;
1547 if Exit_Status_Supported_On_Target
then
1548 Set_String
(" function ");
1550 Set_String
(" procedure ");
1553 Set_String
(Get_Main_Name
);
1555 if Command_Line_Args_On_Target
then
1556 Write_Statement_Buffer
;
1557 WBI
(" (argc : Integer;");
1558 WBI
(" argv : System.Address;");
1559 WBI
(" envp : System.Address)");
1561 if Exit_Status_Supported_On_Target
then
1562 WBI
(" return Integer");
1568 if Exit_Status_Supported_On_Target
then
1569 Set_String
(" return Integer is");
1574 Write_Statement_Buffer
;
1577 if Opt
.Default_Exit_Status
/= 0
1578 and then Bind_Main_Program
1579 and then not Configurable_Run_Time_Mode
1581 WBI
(" procedure Set_Exit_Status (Status : Integer);");
1582 WBI
(" pragma Import (C, Set_Exit_Status, " &
1583 """__gnat_set_exit_status"");");
1587 -- Initialize and Finalize
1589 if not CodePeer_Mode
1590 and then not Cumulative_Restrictions
.Set
(No_Finalization
)
1592 WBI
(" procedure Initialize (Addr : System.Address);");
1593 WBI
(" pragma Import (C, Initialize, ""__gnat_initialize"");");
1595 WBI
(" procedure Finalize;");
1596 WBI
(" pragma Import (C, Finalize, ""__gnat_finalize"");");
1599 -- If we want to analyze the stack, we must import corresponding symbols
1601 if Dynamic_Stack_Measurement
then
1603 WBI
(" procedure Output_Results;");
1604 WBI
(" pragma Import (C, Output_Results, " &
1605 """__gnat_stack_usage_output_results"");");
1609 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1610 WBI
(" pragma Import (C, Initialize_Stack_Analysis, " &
1611 """__gnat_stack_usage_initialize"");");
1614 -- Deal with declarations for main program case
1616 if not No_Main_Subprogram
then
1617 if ALIs
.Table
(ALIs
.First
).Main_Program
= Func
then
1618 WBI
(" Result : Integer;");
1622 if Bind_Main_Program
1623 and not Suppress_Standard_Library_On_Target
1624 and not CodePeer_Mode
1626 WBI
(" SEH : aliased array (1 .. 2) of Integer;");
1631 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1632 -- not referenced elsewhere in the generated program, but is needed
1633 -- by the debugger (that's why it is generated in the first place).
1634 -- The reference stops Ada_Main_Program_Name from being optimized
1635 -- away by smart linkers, such as the AiX linker.
1637 -- Because this variable is unused, we make this variable "aliased"
1638 -- with a pragma Volatile in order to tell the compiler to preserve
1639 -- this variable at any level of optimization.
1641 if Bind_Main_Program
and not CodePeer_Mode
then
1642 WBI
(" Ensure_Reference : aliased System.Address := " &
1643 "Ada_Main_Program_Name'Address;");
1644 WBI
(" pragma Volatile (Ensure_Reference);");
1650 -- Acquire command line arguments if present on target
1652 if CodePeer_Mode
then
1655 elsif Command_Line_Args_On_Target
then
1656 WBI
(" gnat_argc := argc;");
1657 WBI
(" gnat_argv := argv;");
1658 WBI
(" gnat_envp := envp;");
1661 -- If configurable run time and no command line args, then nothing
1662 -- needs to be done since the gnat_argc/argv/envp variables are
1663 -- suppressed in this case.
1665 elsif Configurable_Run_Time_On_Target
then
1668 -- Otherwise set dummy values (to be filled in by some other unit?)
1671 WBI
(" gnat_argc := 0;");
1672 WBI
(" gnat_argv := System.Null_Address;");
1673 WBI
(" gnat_envp := System.Null_Address;");
1676 if Opt
.Default_Exit_Status
/= 0
1677 and then Bind_Main_Program
1678 and then not Configurable_Run_Time_Mode
1680 Set_String
(" Set_Exit_Status (");
1681 Set_Int
(Opt
.Default_Exit_Status
);
1683 Write_Statement_Buffer
;
1686 if Dynamic_Stack_Measurement
then
1687 Set_String
(" Initialize_Stack_Analysis (");
1688 Set_Int
(Dynamic_Stack_Measurement_Array_Size
);
1690 Write_Statement_Buffer
;
1693 if not Cumulative_Restrictions
.Set
(No_Finalization
)
1694 and then not CodePeer_Mode
1696 if not No_Main_Subprogram
1697 and then Bind_Main_Program
1698 and then not Suppress_Standard_Library_On_Target
1700 WBI
(" Initialize (SEH'Address);");
1702 WBI
(" Initialize (System.Null_Address);");
1706 WBI
(" " & Ada_Init_Name
.all & ";");
1708 if not No_Main_Subprogram
then
1709 if CodePeer_Mode
then
1710 if ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1711 WBI
(" " & CodePeer_Wrapper_Name
& ";");
1713 WBI
(" Result := " & CodePeer_Wrapper_Name
& ";");
1716 elsif ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
then
1717 WBI
(" Ada_Main_Program;");
1720 WBI
(" Result := Ada_Main_Program;");
1724 -- Adafinal call is skipped if no finalization
1726 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
1730 -- Prints the result of static stack analysis
1732 if Dynamic_Stack_Measurement
then
1733 WBI
(" Output_Results;");
1736 -- Finalize is only called if we have a run time
1738 if not Cumulative_Restrictions
.Set
(No_Finalization
)
1739 and then not CodePeer_Mode
1746 if Exit_Status_Supported_On_Target
then
1747 if No_Main_Subprogram
1748 or else ALIs
.Table
(ALIs
.First
).Main_Program
= Proc
1750 WBI
(" return (gnat_exit_status);");
1752 WBI
(" return (Result);");
1760 ------------------------------
1761 -- Gen_Object_Files_Options --
1762 ------------------------------
1764 procedure Gen_Object_Files_Options
is
1766 -- This keeps track of the position in the sorted set of entries
1767 -- in the Linker_Options table of where the first entry from an
1768 -- internal file appears.
1770 Linker_Option_List_Started
: Boolean := False;
1771 -- Set to True when "LINKER OPTION LIST" is displayed
1773 procedure Write_Linker_Option
;
1774 -- Write binder info linker option
1776 -------------------------
1777 -- Write_Linker_Option --
1778 -------------------------
1780 procedure Write_Linker_Option
is
1785 -- Loop through string, breaking at null's
1788 while Start
< Name_Len
loop
1790 -- Find null ending this section
1793 while Name_Buffer
(Stop
) /= ASCII
.NUL
1794 and then Stop
<= Name_Len
loop
1798 -- Process section if non-null
1800 if Stop
> Start
then
1801 if Output_Linker_Option_List
then
1802 if not Zero_Formatting
then
1803 if not Linker_Option_List_Started
then
1804 Linker_Option_List_Started
:= True;
1806 Write_Str
(" LINKER OPTION LIST");
1814 Write_Str
(Name_Buffer
(Start
.. Stop
- 1));
1817 WBI
(" -- " & Name_Buffer
(Start
.. Stop
- 1));
1822 end Write_Linker_Option
;
1824 -- Start of processing for Gen_Object_Files_Options
1827 WBI
("-- BEGIN Object file/option list");
1829 if Object_List_Filename
/= null then
1830 Set_List_File
(Object_List_Filename
.all);
1833 for E
in Elab_Order
.First
.. Elab_Order
.Last
loop
1835 -- If not spec that has an associated body, then generate a comment
1836 -- giving the name of the corresponding object file.
1838 if not Units
.Table
(Elab_Order
.Table
(E
)).SAL_Interface
1839 and then Units
.Table
(Elab_Order
.Table
(E
)).Utype
/= Is_Spec
1843 (Units
.Table
(Elab_Order
.Table
(E
)).My_ALI
).Ofile_Full_Name
);
1845 -- If the presence of an object file is necessary or if it exists,
1848 if not Hostparm
.Exclude_Missing_Objects
1850 System
.OS_Lib
.Is_Regular_File
(Name_Buffer
(1 .. Name_Len
))
1852 WBI
(" -- " & Name_Buffer
(1 .. Name_Len
));
1854 if Output_Object_List
then
1855 Write_Str
(Name_Buffer
(1 .. Name_Len
));
1862 if Object_List_Filename
/= null then
1866 -- Add a "-Ldir" for each directory in the object path
1868 for J
in 1 .. Nb_Dir_In_Obj_Search_Path
loop
1870 Dir
: constant String_Ptr
:= Dir_In_Obj_Search_Path
(J
);
1873 Add_Str_To_Name_Buffer
("-L");
1874 Add_Str_To_Name_Buffer
(Dir
.all);
1875 Write_Linker_Option
;
1879 if not (Opt
.No_Run_Time_Mode
or Opt
.No_Stdlib
) then
1882 if Opt
.Shared_Libgnat
then
1883 Add_Str_To_Name_Buffer
("-shared");
1885 Add_Str_To_Name_Buffer
("-static");
1888 -- Write directly to avoid inclusion in -K output as -static and
1889 -- -shared are not usually specified linker options.
1891 WBI
(" -- " & Name_Buffer
(1 .. Name_Len
));
1894 -- Sort linker options
1896 -- This sort accomplishes two important purposes:
1898 -- a) All application files are sorted to the front, and all GNAT
1899 -- internal files are sorted to the end. This results in a well
1900 -- defined dividing line between the two sets of files, for the
1901 -- purpose of inserting certain standard library references into
1902 -- the linker arguments list.
1904 -- b) Given two different units, we sort the linker options so that
1905 -- those from a unit earlier in the elaboration order comes later
1906 -- in the list. This is a heuristic designed to create a more
1907 -- friendly order of linker options when the operations appear in
1908 -- separate units. The idea is that if unit A must be elaborated
1909 -- before unit B, then it is more likely that B references
1910 -- libraries included by A, than vice versa, so we want libraries
1911 -- included by A to come after libraries included by B.
1913 -- These two criteria are implemented by function Lt_Linker_Option. Note
1914 -- that a special case of b) is that specs are elaborated before bodies,
1915 -- so linker options from specs come after linker options for bodies,
1916 -- and again, the assumption is that libraries used by the body are more
1917 -- likely to reference libraries used by the spec, than vice versa.
1920 (Linker_Options
.Last
,
1921 Move_Linker_Option
'Access,
1922 Lt_Linker_Option
'Access);
1924 -- Write user linker options, i.e. the set of linker options that come
1925 -- from all files other than GNAT internal files, Lgnat is left set to
1926 -- point to the first entry from a GNAT internal file, or past the end
1927 -- of the entries if there are no internal files.
1929 Lgnat
:= Linker_Options
.Last
+ 1;
1931 for J
in 1 .. Linker_Options
.Last
loop
1932 if not Linker_Options
.Table
(J
).Internal_File
then
1933 Get_Name_String
(Linker_Options
.Table
(J
).Name
);
1934 Write_Linker_Option
;
1941 -- Now we insert standard linker options that must appear after the
1942 -- entries from user files, and before the entries from GNAT run-time
1943 -- files. The reason for this decision is that libraries referenced
1944 -- by internal routines may reference these standard library entries.
1946 -- Note that we do not insert anything when pragma No_Run_Time has
1947 -- been specified or when the standard libraries are not to be used,
1948 -- otherwise on some platforms, we may get duplicate symbols when
1949 -- linking (not clear if this is still the case, but it is harmless).
1951 if not (Opt
.No_Run_Time_Mode
or else Opt
.No_Stdlib
) then
1955 if Opt
.Shared_Libgnat
then
1956 Add_Str_To_Name_Buffer
(Shared_Lib
("gnarl"));
1958 Add_Str_To_Name_Buffer
("-lgnarl");
1961 Write_Linker_Option
;
1966 if Opt
.Shared_Libgnat
then
1967 Add_Str_To_Name_Buffer
(Shared_Lib
("gnat"));
1969 Add_Str_To_Name_Buffer
("-lgnat");
1972 Write_Linker_Option
;
1975 -- Write linker options from all internal files
1977 for J
in Lgnat
.. Linker_Options
.Last
loop
1978 Get_Name_String
(Linker_Options
.Table
(J
).Name
);
1979 Write_Linker_Option
;
1982 if Output_Linker_Option_List
and then not Zero_Formatting
then
1986 WBI
("-- END Object file/option list ");
1987 end Gen_Object_Files_Options
;
1989 ---------------------
1990 -- Gen_Output_File --
1991 ---------------------
1993 procedure Gen_Output_File
(Filename
: String) is
1995 -- Acquire settings for Interrupt_State pragmas
1997 Set_IS_Pragma_Table
;
1999 -- Acquire settings for Priority_Specific_Dispatching pragma
2001 Set_PSD_Pragma_Table
;
2003 -- Override time slice value if -T switch is set
2005 if Time_Slice_Set
then
2006 ALIs
.Table
(ALIs
.First
).Time_Slice_Value
:= Opt
.Time_Slice_Value
;
2009 -- Count number of elaboration calls
2011 for E
in Elab_Order
.First
.. Elab_Order
.Last
loop
2012 if Units
.Table
(Elab_Order
.Table
(E
)).No_Elab
then
2015 Num_Elab_Calls
:= Num_Elab_Calls
+ 1;
2019 -- Generate output file in appropriate language
2021 Gen_Output_File_Ada
(Filename
);
2022 end Gen_Output_File
;
2024 -------------------------
2025 -- Gen_Output_File_Ada --
2026 -------------------------
2028 procedure Gen_Output_File_Ada
(Filename
: String) is
2030 Ada_Main
: constant String := Get_Ada_Main_Name
;
2031 -- Name to be used for generated Ada main program. See the body of
2032 -- function Get_Ada_Main_Name for details on the form of the name.
2034 Needs_Library_Finalization
: constant Boolean :=
2035 not Configurable_Run_Time_On_Target
and then Has_Finalizer
;
2036 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2037 -- non-terminating, so we do not want finalization.
2040 -- Name of generated bind file (spec)
2043 -- Name of generated bind file (body)
2046 -- Create spec first
2048 Create_Binder_Output
(Filename
, 's', Bfiles
);
2050 -- We always compile the binder file in Ada 95 mode so that we properly
2051 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2052 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2054 WBI
("pragma Ada_95;");
2055 WBI
("pragma Warnings (Off);");
2057 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2058 -- then we need to make sure that the binder program is compiled with
2059 -- the same restriction, so that no exception tables are generated.
2061 if Cumulative_Restrictions
.Set
(No_Exception_Handlers
) then
2062 WBI
("pragma Restrictions (No_Exception_Handlers);");
2065 -- Same processing for Restrictions (No_Exception_Propagation)
2067 if Cumulative_Restrictions
.Set
(No_Exception_Propagation
) then
2068 WBI
("pragma Restrictions (No_Exception_Propagation);");
2071 -- Same processing for pragma No_Run_Time
2073 if No_Run_Time_Mode
then
2074 WBI
("pragma No_Run_Time;");
2077 -- Generate with of System so we can reference System.Address
2079 WBI
("with System;");
2081 -- Generate with of System.Initialize_Scalars if active
2083 if Initialize_Scalars_Used
then
2084 WBI
("with System.Scalar_Values;");
2087 -- Generate with of System.Secondary_Stack if active
2089 if Sec_Stack_Used
and then Default_Sec_Stack_Size
/= -1 then
2090 WBI
("with System.Secondary_Stack;");
2093 Resolve_Binder_Options
;
2095 -- Generate standard with's
2097 if not Suppress_Standard_Library_On_Target
then
2098 if CodePeer_Mode
then
2099 WBI
("with System.Standard_Library;");
2103 WBI
("package " & Ada_Main
& " is");
2105 -- Main program case
2107 if Bind_Main_Program
then
2108 -- Generate argc/argv stuff unless suppressed
2110 if Command_Line_Args_On_Target
2111 or not Configurable_Run_Time_On_Target
2114 WBI
(" gnat_argc : Integer;");
2115 WBI
(" gnat_argv : System.Address;");
2116 WBI
(" gnat_envp : System.Address;");
2118 -- If the standard library is not suppressed, these variables
2119 -- are in the run-time data area for easy run time access.
2121 if not Suppress_Standard_Library_On_Target
then
2123 WBI
(" pragma Import (C, gnat_argc);");
2124 WBI
(" pragma Import (C, gnat_argv);");
2125 WBI
(" pragma Import (C, gnat_envp);");
2129 -- Define exit status. Again in normal mode, this is in the
2130 -- run-time library, and is initialized there, but in the
2131 -- configurable runtime case, the variable is declared and
2132 -- initialized in this file.
2136 if Configurable_Run_Time_Mode
then
2137 if Exit_Status_Supported_On_Target
then
2138 WBI
(" gnat_exit_status : Integer := 0;");
2142 WBI
(" gnat_exit_status : Integer;");
2143 WBI
(" pragma Import (C, gnat_exit_status);");
2146 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2147 -- the main program. Otherwise, it can lead under some circumstances
2148 -- to a symbol duplication during the link (for instance when a C
2149 -- program uses two Ada libraries). Also zero terminate the string
2150 -- so that its end can be found reliably at run time.
2153 WBI
(" GNAT_Version : constant String :=");
2154 WBI
(" """ & Ver_Prefix
&
2155 Gnat_Version_String
&
2157 WBI
(" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2160 Set_String
(" Ada_Main_Program_Name : constant String := """);
2161 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2163 Set_Main_Program_Name
;
2164 Set_String
(""" & ASCII.NUL;");
2166 Write_Statement_Buffer
;
2169 (" pragma Export (C, Ada_Main_Program_Name, " &
2170 """__gnat_ada_main_program_name"");");
2174 WBI
(" procedure " & Ada_Init_Name
.all & ";");
2175 WBI
(" pragma Export (C, " & Ada_Init_Name
.all & ", """ &
2176 Ada_Init_Name
.all & """);");
2178 -- If -a has been specified use pragma Linker_Constructor for the init
2179 -- procedure and pragma Linker_Destructor for the final procedure.
2181 if Use_Pragma_Linker_Constructor
then
2182 WBI
(" pragma Linker_Constructor (" & Ada_Init_Name
.all & ");");
2185 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
2187 WBI
(" procedure " & Ada_Final_Name
.all & ";");
2188 WBI
(" pragma Export (C, " & Ada_Final_Name
.all & ", """ &
2189 Ada_Final_Name
.all & """);");
2191 if Use_Pragma_Linker_Constructor
then
2192 WBI
(" pragma Linker_Destructor (" & Ada_Final_Name
.all & ");");
2196 if Bind_Main_Program
then
2200 if Exit_Status_Supported_On_Target
then
2201 Set_String
(" function ");
2203 Set_String
(" procedure ");
2206 Set_String
(Get_Main_Name
);
2208 -- Generate argument list if present
2210 if Command_Line_Args_On_Target
then
2211 Write_Statement_Buffer
;
2212 WBI
(" (argc : Integer;");
2213 WBI
(" argv : System.Address;");
2215 (" envp : System.Address)");
2217 if Exit_Status_Supported_On_Target
then
2218 Write_Statement_Buffer
;
2219 WBI
(" return Integer;");
2221 Write_Statement_Buffer
(";");
2225 if Exit_Status_Supported_On_Target
then
2226 Write_Statement_Buffer
(" return Integer;");
2228 Write_Statement_Buffer
(";");
2232 WBI
(" pragma Export (C, " & Get_Main_Name
& ", """ &
2233 Get_Main_Name
& """);");
2242 WBI
("end " & Ada_Main
& ";");
2243 Close_Binder_Output
;
2245 -- Prepare to write body
2247 Create_Binder_Output
(Filename
, 'b', Bfileb
);
2249 -- We always compile the binder file in Ada 95 mode so that we properly
2250 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2251 -- of the Ada 2005/2012 constructs are needed by the binder file.
2253 WBI
("pragma Ada_95;");
2254 WBI
("pragma Warnings (Off);");
2256 -- Output Source_File_Name pragmas which look like
2258 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2259 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2261 -- where sss/bbb are the spec/body file names respectively
2263 Get_Name_String
(Bfiles
);
2264 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 3) := """);";
2266 WBI
("pragma Source_File_Name (" &
2268 ", Spec_File_Name => """ &
2269 Name_Buffer
(1 .. Name_Len
+ 3));
2271 Get_Name_String
(Bfileb
);
2272 Name_Buffer
(Name_Len
+ 1 .. Name_Len
+ 3) := """);";
2274 WBI
("pragma Source_File_Name (" &
2276 ", Body_File_Name => """ &
2277 Name_Buffer
(1 .. Name_Len
+ 3));
2279 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2280 -- versions of the compiler which have overflow checks on by default.
2281 -- We do not want overflow checking enabled for the increments of the
2282 -- elaboration variables (since this can cause an unwanted reference to
2283 -- the last chance exception handler for limited run-times).
2285 WBI
("pragma Suppress (Overflow_Check);");
2287 -- Generate with of System.Restrictions to initialize
2288 -- Run_Time_Restrictions.
2290 if System_Restrictions_Used
2291 and not Suppress_Standard_Library_On_Target
2294 WBI
("with System.Restrictions;");
2297 -- Generate with of Ada.Exceptions if needs library finalization
2299 if Needs_Library_Finalization
then
2300 WBI
("with Ada.Exceptions;");
2303 -- Generate with of System.Elaboration_Allocators if the restriction
2304 -- No_Standard_Allocators_After_Elaboration was present.
2306 if Cumulative_Restrictions
.Set
2307 (No_Standard_Allocators_After_Elaboration
)
2309 WBI
("with System.Elaboration_Allocators;");
2312 -- Generate start of package body
2315 WBI
("package body " & Ada_Main
& " is");
2318 -- Generate externals for elaboration entities
2322 if not CodePeer_Mode
then
2323 if not Suppress_Standard_Library_On_Target
then
2325 -- Generate Priority_Specific_Dispatching pragma string
2328 (" Local_Priority_Specific_Dispatching : " &
2329 "constant String := """);
2331 for J
in 0 .. PSD_Pragma_Settings
.Last
loop
2332 Set_Char
(PSD_Pragma_Settings
.Table
(J
));
2336 Write_Statement_Buffer
;
2338 -- Generate Interrupt_State pragma string
2340 Set_String
(" Local_Interrupt_States : constant String := """);
2342 for J
in 0 .. IS_Pragma_Settings
.Last
loop
2343 Set_Char
(IS_Pragma_Settings
.Table
(J
));
2347 Write_Statement_Buffer
;
2351 if not Suppress_Standard_Library_On_Target
then
2353 -- The B.1(39) implementation advice says that the adainit
2354 -- and adafinal routines should be idempotent. Generate a flag to
2355 -- ensure that. This is not needed if we are suppressing the
2356 -- standard library since it would never be referenced.
2358 WBI
(" Is_Elaborated : Boolean := False;");
2360 -- Generate bind environment string
2362 Gen_Bind_Env_String
;
2368 -- Generate the adafinal routine unless there is no finalization to do
2370 if not Cumulative_Restrictions
.Set
(No_Finalization
) then
2371 if Needs_Library_Finalization
then
2372 Gen_Finalize_Library
;
2380 if Bind_Main_Program
then
2384 -- Output object file list and the Ada body is complete
2386 Gen_Object_Files_Options
;
2389 WBI
("end " & Ada_Main
& ";");
2391 Close_Binder_Output
;
2392 end Gen_Output_File_Ada
;
2394 ----------------------
2395 -- Gen_Restrictions --
2396 ----------------------
2398 procedure Gen_Restrictions
is
2402 if Suppress_Standard_Library_On_Target
2403 or not System_Restrictions_Used
2408 WBI
(" System.Restrictions.Run_Time_Restrictions :=");
2414 for J
in Cumulative_Restrictions
.Set
'Range loop
2415 Set_Boolean
(Cumulative_Restrictions
.Set
(J
));
2419 if J
/= Cumulative_Restrictions
.Set
'Last and then Count
= 8 then
2420 Write_Statement_Buffer
;
2426 Set_String_Replace
("),");
2427 Write_Statement_Buffer
;
2428 Set_String
(" Value => (");
2430 for J
in Cumulative_Restrictions
.Value
'Range loop
2431 Set_Int
(Int
(Cumulative_Restrictions
.Value
(J
)));
2435 Set_String_Replace
("),");
2436 Write_Statement_Buffer
;
2437 WBI
(" Violated =>");
2441 for J
in Cumulative_Restrictions
.Violated
'Range loop
2442 Set_Boolean
(Cumulative_Restrictions
.Violated
(J
));
2446 if J
/= Cumulative_Restrictions
.Set
'Last and then Count
= 8 then
2447 Write_Statement_Buffer
;
2453 Set_String_Replace
("),");
2454 Write_Statement_Buffer
;
2455 Set_String
(" Count => (");
2457 for J
in Cumulative_Restrictions
.Count
'Range loop
2458 Set_Int
(Int
(Cumulative_Restrictions
.Count
(J
)));
2462 Set_String_Replace
("),");
2463 Write_Statement_Buffer
;
2464 Set_String
(" Unknown => (");
2466 for J
in Cumulative_Restrictions
.Unknown
'Range loop
2467 Set_Boolean
(Cumulative_Restrictions
.Unknown
(J
));
2471 Set_String_Replace
("))");
2473 Write_Statement_Buffer
;
2474 end Gen_Restrictions
;
2480 -- This routine generates lines such as:
2482 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2483 -- pragma Export (C, unnnnn, unam);
2485 -- for each unit, where unam is the unit name suffixed by either B or S for
2486 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2487 -- the version number, and nnnnn is a 5-digits serial number.
2489 procedure Gen_Versions
is
2490 Ubuf
: String (1 .. 6) := "u00000";
2492 procedure Increment_Ubuf
;
2493 -- Little procedure to increment the serial number
2495 --------------------
2496 -- Increment_Ubuf --
2497 --------------------
2499 procedure Increment_Ubuf
is
2501 for J
in reverse Ubuf
'Range loop
2502 Ubuf
(J
) := Character'Succ (Ubuf
(J
));
2503 exit when Ubuf
(J
) <= '9';
2508 -- Start of processing for Gen_Versions
2513 WBI
(" type Version_32 is mod 2 ** 32;");
2514 for U
in Units
.First
.. Units
.Last
loop
2515 if not Units
.Table
(U
).SAL_Interface
2517 (not Bind_For_Library
or else Units
.Table
(U
).Directly_Scanned
)
2520 WBI
(" " & Ubuf
& " : constant Version_32 := 16#" &
2521 Units
.Table
(U
).Version
& "#;");
2522 Set_String
(" pragma Export (C, ");
2524 Set_String
(", """);
2526 Get_Name_String
(Units
.Table
(U
).Uname
);
2528 for K
in 1 .. Name_Len
loop
2529 if Name_Buffer
(K
) = '.' then
2533 elsif Name_Buffer
(K
) = '%' then
2537 Set_Char
(Name_Buffer
(K
));
2541 if Name_Buffer
(Name_Len
) = 's' then
2547 Set_String
(""");");
2548 Write_Statement_Buffer
;
2553 ------------------------
2554 -- Get_Main_Unit_Name --
2555 ------------------------
2557 function Get_Main_Unit_Name
(S
: String) return String is
2558 Result
: String := S
;
2561 for J
in S
'Range loop
2562 if Result
(J
) = '.' then
2568 end Get_Main_Unit_Name
;
2570 -----------------------
2571 -- Get_Ada_Main_Name --
2572 -----------------------
2574 function Get_Ada_Main_Name
return String is
2575 Suffix
: constant String := "_00";
2576 Name
: String (1 .. Opt
.Ada_Main_Name
.all'Length + Suffix
'Length) :=
2577 Opt
.Ada_Main_Name
.all & Suffix
;
2581 -- For CodePeer, we want reproducible names (independent of other
2582 -- mains that may or may not be present) that don't collide
2583 -- when analyzing multiple mains and which are easily recognizable
2584 -- as "ada_main" names.
2586 if CodePeer_Mode
then
2587 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2588 return "ada_main_for_" &
2589 Get_Main_Unit_Name
(Name_Buffer
(1 .. Name_Len
- 2));
2592 -- This loop tries the following possibilities in order
2598 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2599 -- it is set to 'ada_main'.
2601 for J
in 0 .. 99 loop
2603 Nlen
:= Name
'Length - Suffix
'Length;
2605 Nlen
:= Name
'Length;
2606 Name
(Name
'Last) := Character'Val (J
mod 10 + Character'Pos ('0'));
2607 Name
(Name
'Last - 1) :=
2608 Character'Val (J
/ 10 + Character'Pos ('0'));
2611 for K
in ALIs
.First
.. ALIs
.Last
loop
2612 for L
in ALIs
.Table
(K
).First_Unit
.. ALIs
.Table
(K
).Last_Unit
loop
2614 -- Get unit name, removing %b or %e at end
2616 Get_Name_String
(Units
.Table
(L
).Uname
);
2617 Name_Len
:= Name_Len
- 2;
2619 if Name_Buffer
(1 .. Name_Len
) = Name
(1 .. Nlen
) then
2625 return Name
(1 .. Nlen
);
2631 -- If we fall through, just use a peculiar unlikely name
2633 return ("Qwertyuiop");
2634 end Get_Ada_Main_Name
;
2640 function Get_Main_Name
return String is
2642 -- Explicit name given with -M switch
2644 if Bind_Alternate_Main_Name
then
2645 return Alternate_Main_Name
.all;
2647 -- Case of main program name to be used directly
2649 elsif Use_Ada_Main_Program_Name_On_Target
then
2651 -- Get main program name
2653 Get_Name_String
(Units
.Table
(First_Unit_Entry
).Uname
);
2655 -- If this is a child name, return only the name of the child, since
2656 -- we can't have dots in a nested program name. Note that we do not
2657 -- include the %b at the end of the unit name.
2659 for J
in reverse 1 .. Name_Len
- 2 loop
2660 if J
= 1 or else Name_Buffer
(J
- 1) = '.' then
2661 return Name_Buffer
(J
.. Name_Len
- 2);
2665 raise Program_Error
; -- impossible exit
2667 -- Case where "main" is to be used as default
2674 ---------------------
2675 -- Get_WC_Encoding --
2676 ---------------------
2678 function Get_WC_Encoding
return Character is
2680 -- If encoding method specified by -W switch, then return it
2682 if Wide_Character_Encoding_Method_Specified
then
2683 return WC_Encoding_Letters
(Wide_Character_Encoding_Method
);
2685 -- If no main program, and not specified, set brackets, we really have
2686 -- no better choice. If some other encoding is required when there is
2687 -- no main, it must be set explicitly using -Wx.
2689 -- Note: if the ALI file always passed the wide character encoding of
2690 -- every file, then we could use the encoding of the initial specified
2691 -- file, but this information is passed only for potential main
2692 -- programs. We could fix this sometime, but it is a very minor point
2693 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
2694 -- is no main program).
2696 elsif No_Main_Subprogram
then
2699 -- Otherwise if there is a main program, take encoding from it
2702 return ALIs
.Table
(ALIs
.First
).WC_Encoding
;
2704 end Get_WC_Encoding
;
2710 function Has_Finalizer
return Boolean is
2715 for E
in reverse Elab_Order
.First
.. Elab_Order
.Last
loop
2716 Unum
:= Elab_Order
.Table
(E
);
2717 U
:= Units
.Table
(Unum
);
2719 -- We are only interested in non-generic packages
2721 if U
.Unit_Kind
= 'p'
2722 and then U
.Has_Finalizer
2723 and then not U
.Is_Generic
2724 and then not U
.No_Elab
2737 function Hash
(Nam
: Name_Id
) return Header_Num
is
2739 return Int
(Nam
- Names_Low_Bound
) rem Header_Num
'Last;
2742 ----------------------
2743 -- Lt_Linker_Option --
2744 ----------------------
2746 function Lt_Linker_Option
(Op1
, Op2
: Natural) return Boolean is
2748 -- Sort internal files last
2750 if Linker_Options
.Table
(Op1
).Internal_File
2752 Linker_Options
.Table
(Op2
).Internal_File
2754 -- Note: following test uses False < True
2756 return Linker_Options
.Table
(Op1
).Internal_File
2758 Linker_Options
.Table
(Op2
).Internal_File
;
2760 -- If both internal or both non-internal, sort according to the
2761 -- elaboration position. A unit that is elaborated later should come
2762 -- earlier in the linker options list.
2765 return Units
.Table
(Linker_Options
.Table
(Op1
).Unit
).Elab_Position
2767 Units
.Table
(Linker_Options
.Table
(Op2
).Unit
).Elab_Position
;
2770 end Lt_Linker_Option
;
2772 ------------------------
2773 -- Move_Linker_Option --
2774 ------------------------
2776 procedure Move_Linker_Option
(From
: Natural; To
: Natural) is
2778 Linker_Options
.Table
(To
) := Linker_Options
.Table
(From
);
2779 end Move_Linker_Option
;
2781 ----------------------------
2782 -- Resolve_Binder_Options --
2783 ----------------------------
2785 procedure Resolve_Binder_Options
is
2787 procedure Check_Package
(Var
: in out Boolean; Name
: String);
2788 -- Set Var to true iff the current identifier in Namet is Name. Do
2789 -- nothing if it doesn't match. This procedure is just a helper to
2790 -- avoid explicitly dealing with length.
2796 procedure Check_Package
(Var
: in out Boolean; Name
: String) is
2798 if Name_Len
= Name
'Length
2799 and then Name_Buffer
(1 .. Name_Len
) = Name
2805 -- Start of processing for Resolve_Binder_Options
2808 for E
in Elab_Order
.First
.. Elab_Order
.Last
loop
2809 Get_Name_String
(Units
.Table
(Elab_Order
.Table
(E
)).Uname
);
2811 -- This is not a perfect approach, but is the current protocol
2812 -- between the run-time and the binder to indicate that tasking is
2813 -- used: System.OS_Interface should always be used by any tasking
2816 Check_Package
(With_GNARL
, "system.os_interface%s");
2818 -- Ditto for the use of restricted tasking
2821 (System_Tasking_Restricted_Stages_Used
,
2822 "system.tasking.restricted.stages%s");
2824 -- Ditto for the use of interrupts
2826 Check_Package
(System_Interrupts_Used
, "system.interrupts%s");
2828 -- Ditto for the use of dispatching domains
2831 (Dispatching_Domains_Used
,
2832 "system.multiprocessors.dispatching_domains%s");
2834 -- Ditto for the use of restrictions
2836 Check_Package
(System_Restrictions_Used
, "system.restrictions%s");
2838 -- Ditto for use of an SMP bareboard runtime
2840 Check_Package
(System_BB_CPU_Primitives_Multiprocessors_Used
,
2841 "system.bb.cpu_primitives.multiprocessors%s");
2844 end Resolve_Binder_Options
;
2850 procedure Set_Bind_Env
(Key
, Value
: String) is
2852 -- The lengths of Key and Value are stored as single bytes
2854 if Key
'Length > 255 then
2855 Osint
.Fail
("bind environment key """ & Key
& """ too long");
2858 if Value
'Length > 255 then
2859 Osint
.Fail
("bind environment value """ & Value
& """ too long");
2862 Bind_Environment
.Set
(Name_Find
(Key
), Name_Find
(Value
));
2869 procedure Set_Boolean
(B
: Boolean) is
2870 True_Str
: constant String := "True";
2871 False_Str
: constant String := "False";
2874 Statement_Buffer
(Last
+ 1 .. Last
+ True_Str
'Length) := True_Str
;
2875 Last
:= Last
+ True_Str
'Length;
2877 Statement_Buffer
(Last
+ 1 .. Last
+ False_Str
'Length) := False_Str
;
2878 Last
:= Last
+ False_Str
'Length;
2886 procedure Set_Char
(C
: Character) is
2889 Statement_Buffer
(Last
) := C
;
2896 procedure Set_Int
(N
: Int
) is
2908 Statement_Buffer
(Last
) :=
2909 Character'Val (N
mod 10 + Character'Pos ('0'));
2913 -------------------------
2914 -- Set_IS_Pragma_Table --
2915 -------------------------
2917 procedure Set_IS_Pragma_Table
is
2919 for F
in ALIs
.First
.. ALIs
.Last
loop
2920 for K
in ALIs
.Table
(F
).First_Interrupt_State
..
2921 ALIs
.Table
(F
).Last_Interrupt_State
2924 Inum
: constant Int
:=
2925 Interrupt_States
.Table
(K
).Interrupt_Id
;
2926 Stat
: constant Character :=
2927 Interrupt_States
.Table
(K
).Interrupt_State
;
2930 while IS_Pragma_Settings
.Last
< Inum
loop
2931 IS_Pragma_Settings
.Append
('n');
2934 IS_Pragma_Settings
.Table
(Inum
) := Stat
;
2938 end Set_IS_Pragma_Table
;
2940 ---------------------------
2941 -- Set_Main_Program_Name --
2942 ---------------------------
2944 procedure Set_Main_Program_Name
is
2946 -- Note that name has %b on the end which we ignore
2948 -- First we output the initial _ada_ since we know that the main
2949 -- program is a library level subprogram.
2951 Set_String
("_ada_");
2953 -- Copy name, changing dots to double underscores
2955 for J
in 1 .. Name_Len
- 2 loop
2956 if Name_Buffer
(J
) = '.' then
2959 Set_Char
(Name_Buffer
(J
));
2962 end Set_Main_Program_Name
;
2964 ---------------------
2965 -- Set_Name_Buffer --
2966 ---------------------
2968 procedure Set_Name_Buffer
is
2970 for J
in 1 .. Name_Len
loop
2971 Set_Char
(Name_Buffer
(J
));
2973 end Set_Name_Buffer
;
2975 -------------------------
2976 -- Set_PSD_Pragma_Table --
2977 -------------------------
2979 procedure Set_PSD_Pragma_Table
is
2981 for F
in ALIs
.First
.. ALIs
.Last
loop
2982 for K
in ALIs
.Table
(F
).First_Specific_Dispatching
..
2983 ALIs
.Table
(F
).Last_Specific_Dispatching
2986 DTK
: Specific_Dispatching_Record
2987 renames Specific_Dispatching
.Table
(K
);
2990 while PSD_Pragma_Settings
.Last
< DTK
.Last_Priority
loop
2991 PSD_Pragma_Settings
.Append
('F');
2994 for Prio
in DTK
.First_Priority
.. DTK
.Last_Priority
loop
2995 PSD_Pragma_Settings
.Table
(Prio
) := DTK
.Dispatching_Policy
;
3000 end Set_PSD_Pragma_Table
;
3006 procedure Set_String
(S
: String) is
3008 Statement_Buffer
(Last
+ 1 .. Last
+ S
'Length) := S
;
3009 Last
:= Last
+ S
'Length;
3012 ------------------------
3013 -- Set_String_Replace --
3014 ------------------------
3016 procedure Set_String_Replace
(S
: String) is
3018 Statement_Buffer
(Last
- S
'Length + 1 .. Last
) := S
;
3019 end Set_String_Replace
;
3025 procedure Set_Unit_Name
is
3027 for J
in 1 .. Name_Len
- 2 loop
3028 if Name_Buffer
(J
) = '.' then
3031 Set_Char
(Name_Buffer
(J
));
3036 ---------------------
3037 -- Set_Unit_Number --
3038 ---------------------
3040 procedure Set_Unit_Number
(U
: Unit_Id
) is
3041 Num_Units
: constant Nat
:= Nat
(Units
.Last
) - Nat
(Unit_Id
'First);
3042 Unum
: constant Nat
:= Nat
(U
) - Nat
(Unit_Id
'First);
3045 if Num_Units
>= 10 and then Unum
< 10 then
3049 if Num_Units
>= 100 and then Unum
< 100 then
3054 end Set_Unit_Number
;
3056 ---------------------
3057 -- Write_Bind_Line --
3058 ---------------------
3060 procedure Write_Bind_Line
(S
: String) is
3062 -- Need to strip trailing LF from S
3064 WBI
(S
(S
'First .. S
'Last - 1));
3065 end Write_Bind_Line
;
3067 ----------------------------
3068 -- Write_Statement_Buffer --
3069 ----------------------------
3071 procedure Write_Statement_Buffer
is
3073 WBI
(Statement_Buffer
(1 .. Last
));
3075 end Write_Statement_Buffer
;
3077 procedure Write_Statement_Buffer
(S
: String) is
3080 Write_Statement_Buffer
;
3081 end Write_Statement_Buffer
;