2017-12-12 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[official-gcc.git] / gcc / ada / bindgen.adb
blob44d521e8c46bfb48cd786c6d3f8f3073d5bc6586
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- B I N D G E N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with ALI; use ALI;
27 with Casing; use Casing;
28 with Fname; use Fname;
29 with Gnatvsn; use Gnatvsn;
30 with Hostparm;
31 with Namet; use Namet;
32 with Opt; use Opt;
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;
39 with Targparm; use Targparm;
40 with Types; use Types;
42 with System.OS_Lib;
43 with System.WCh_Con; use System.WCh_Con;
45 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
46 with GNAT.HTable;
48 package body Bindgen is
49 Statement_Buffer : String (1 .. 1000);
50 -- Buffer used for constructing output statements
52 Stm_Last : Natural := 0;
53 -- Stm_Last location in Statement_Buffer currently set
55 With_GNARL : Boolean := False;
56 -- Flag which indicates whether the program uses the GNARL library
57 -- (presence of the unit System.OS_Interface)
59 Num_Elab_Calls : Nat := 0;
60 -- Number of generated calls to elaboration routines
62 Num_Primary_Stacks : Int := 0;
63 -- Number of default-sized primary stacks the binder needs to allocate for
64 -- task objects declared in the program.
66 Num_Sec_Stacks : Int := 0;
67 -- Number of default-sized primary stacks the binder needs to allocate for
68 -- task objects declared in the program.
70 System_Restrictions_Used : Boolean := False;
71 -- Flag indicating whether the unit System.Restrictions is in the closure
72 -- of the partition. This is set by Resolve_Binder_Options, and is used
73 -- to determine whether or not to initialize the restrictions information
74 -- in the body of the binder generated file (we do not want to do this
75 -- unconditionally, since it drags in the System.Restrictions unit
76 -- unconditionally, which is unpleasand, especially for ZFP etc.)
78 Dispatching_Domains_Used : Boolean := False;
79 -- Flag indicating whether multiprocessor dispatching domains are used in
80 -- the closure of the partition. This is set by Resolve_Binder_Options, and
81 -- is used to call the routine to disallow the creation of new dispatching
82 -- domains just before calling the main procedure from the environment
83 -- task.
85 System_Secondary_Stack_Used : Boolean := False;
86 -- Flag indicating whether the unit System.Secondary_Stack is in the
87 -- closure of the partition. This is set by Resolve_Binder_Options, and
88 -- is used to initialize the package in cases where the run-time brings
89 -- in package but the secondary stack is not used.
91 System_Tasking_Restricted_Stages_Used : Boolean := False;
92 -- Flag indicating whether the unit System.Tasking.Restricted.Stages is in
93 -- the closure of the partition. This is set by Resolve_Binder_Options,
94 -- and it used to call a routine to active all the tasks at the end of
95 -- the elaboration when partition elaboration policy is sequential.
97 System_Interrupts_Used : Boolean := False;
98 -- Flag indicating whether the unit System.Interrups is in the closure of
99 -- the partition. This is set by Resolve_Binder_Options, and it used to
100 -- attach interrupt handlers at the end of the elaboration when partition
101 -- elaboration policy is sequential.
103 System_BB_CPU_Primitives_Multiprocessors_Used : Boolean := False;
104 -- Flag indicating whether unit System.BB.CPU_Primitives.Multiprocessors
105 -- is in the closure of the partition. This is set by procedure
106 -- Resolve_Binder_Options, and it is used to call a procedure that starts
107 -- slave processors.
109 System_Version_Control_Used : Boolean := False;
110 -- Flag indicating whether unit System.Version_Control is in the closure.
111 -- This unit is implicitly withed by the compiler when Version or
112 -- Body_Version attributes are used. If the package is not in the closure,
113 -- the version definitions can be removed.
115 Lib_Final_Built : Boolean := False;
116 -- Flag indicating whether the finalize_library rountine has been built
118 Bind_Env_String_Built : Boolean := False;
119 -- Flag indicating whether a bind environment string has been built
121 CodePeer_Wrapper_Name : constant String := "call_main_subprogram";
122 -- For CodePeer, introduce a wrapper subprogram which calls the
123 -- user-defined main subprogram.
125 ----------------------------------
126 -- Interface_State Pragma Table --
127 ----------------------------------
129 -- This table assembles the interface state pragma information from
130 -- all the units in the partition. Note that Bcheck has already checked
131 -- that the information is consistent across units. The entries
132 -- in this table are n/u/r/s for not set/user/runtime/system.
134 package IS_Pragma_Settings is new Table.Table
135 (Table_Component_Type => Character,
136 Table_Index_Type => Int,
137 Table_Low_Bound => 0,
138 Table_Initial => 100,
139 Table_Increment => 200,
140 Table_Name => "IS_Pragma_Settings");
142 -- This table assembles the Priority_Specific_Dispatching pragma
143 -- information from all the units in the partition. Note that Bcheck has
144 -- already checked that the information is consistent across units.
145 -- The entries in this table are the upper case first character of the
146 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
148 package PSD_Pragma_Settings is new Table.Table
149 (Table_Component_Type => Character,
150 Table_Index_Type => Int,
151 Table_Low_Bound => 0,
152 Table_Initial => 100,
153 Table_Increment => 200,
154 Table_Name => "PSD_Pragma_Settings");
156 ----------------------------
157 -- Bind_Environment Table --
158 ----------------------------
160 subtype Header_Num is Int range 0 .. 36;
162 function Hash (Nam : Name_Id) return Header_Num;
164 package Bind_Environment is new GNAT.HTable.Simple_HTable
165 (Header_Num => Header_Num,
166 Element => Name_Id,
167 No_Element => No_Name,
168 Key => Name_Id,
169 Hash => Hash,
170 Equal => "=");
172 ----------------------
173 -- Run-Time Globals --
174 ----------------------
176 -- This section documents the global variables that are set from the
177 -- generated binder file.
179 -- Main_Priority : Integer;
180 -- Time_Slice_Value : Integer;
181 -- Heap_Size : Natural;
182 -- WC_Encoding : Character;
183 -- Locking_Policy : Character;
184 -- Queuing_Policy : Character;
185 -- Task_Dispatching_Policy : Character;
186 -- Priority_Specific_Dispatching : System.Address;
187 -- Num_Specific_Dispatching : Integer;
188 -- Restrictions : System.Address;
189 -- Interrupt_States : System.Address;
190 -- Num_Interrupt_States : Integer;
191 -- Unreserve_All_Interrupts : Integer;
192 -- Exception_Tracebacks : Integer;
193 -- Exception_Tracebacks_Symbolic : Integer;
194 -- Detect_Blocking : Integer;
195 -- Default_Stack_Size : Integer;
196 -- Default_Secondary_Stack_Size : System.Parameters.Size_Type;
197 -- Leap_Seconds_Support : Integer;
198 -- Main_CPU : Integer;
199 -- Default_Sized_SS_Pool : System.Address;
200 -- Binder_Sec_Stacks_Count : Natural;
202 -- Main_Priority is the priority value set by pragma Priority in the main
203 -- program. If no such pragma is present, the value is -1.
205 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
206 -- main program, or by the use of a -Tnnn parameter for the binder (if both
207 -- are present, the binder value overrides). The value is in milliseconds.
208 -- A value of zero indicates that time slicing should be suppressed. If no
209 -- pragma is present, and no -T switch was used, the value is -1.
211 -- WC_Encoding shows the wide character encoding method used for the main
212 -- program. This is one of the encoding letters defined in
213 -- System.WCh_Con.WC_Encoding_Letters.
215 -- Locking_Policy is a space if no locking policy was specified for the
216 -- partition. If a locking policy was specified, the value is the upper
217 -- case first character of the locking policy name, for example, 'C' for
218 -- Ceiling_Locking.
220 -- Queuing_Policy is a space if no queuing policy was specified for the
221 -- partition. If a queuing policy was specified, the value is the upper
222 -- case first character of the queuing policy name for example, 'F' for
223 -- FIFO_Queuing.
225 -- Task_Dispatching_Policy is a space if no task dispatching policy was
226 -- specified for the partition. If a task dispatching policy was specified,
227 -- the value is the upper case first character of the policy name, e.g. 'F'
228 -- for FIFO_Within_Priorities.
230 -- Priority_Specific_Dispatching is the address of a string used to store
231 -- the task dispatching policy specified for the different priorities in
232 -- the partition. The length of this string is determined by the last
233 -- priority for which such a pragma applies (the string will be a null
234 -- string if no specific dispatching policies were used). If pragma were
235 -- present, the entries apply to the priorities in sequence from the first
236 -- priority. The value stored is the upper case first character of the
237 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
238 -- for those priority ranges not specified.
240 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
241 -- string. It will be set to zero if no Priority_Specific_Dispatching
242 -- pragmas are present.
244 -- Restrictions is the address of a null-terminated string specifying the
245 -- restrictions information for the partition. The format is identical to
246 -- that of the parameter string found on R lines in ali files (see Lib.Writ
247 -- spec in lib-writ.ads for full details). The difference is that in this
248 -- context the values are the cumulative ones for the entire partition.
250 -- Interrupt_States is the address of a string used to specify the
251 -- cumulative results of Interrupt_State pragmas used in the partition.
252 -- The length of this string is determined by the last interrupt for which
253 -- such a pragma is given (the string will be a null string if no pragmas
254 -- were used). If pragma were present the entries apply to the interrupts
255 -- in sequence from the first interrupt, and are set to one of four
256 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
257 -- time, 's' for system, see description of Interrupt_State pragma for
258 -- further details.
260 -- Num_Interrupt_States is the length of the Interrupt_States string. It
261 -- will be set to zero if no Interrupt_State pragmas are present.
263 -- Unreserve_All_Interrupts is set to one if at least one unit in the
264 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
266 -- Exception_Tracebacks is set to one if the -Ea or -E parameter was
267 -- present in the bind and to zero otherwise. Note that on some targets
268 -- exception tracebacks are provided by default, so a value of zero for
269 -- this parameter does not necessarily mean no trace backs are available.
271 -- Exception_Tracebacks_Symbolic is set to one if the -Es parameter was
272 -- present in the bind and to zero otherwise.
274 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
275 -- not. A value of zero indicates that the pragma is not present, while a
276 -- value of 1 signals its presence in the partition.
278 -- Default_Stack_Size is the default stack size used when creating an Ada
279 -- task with no explicit Storage_Size clause.
281 -- Default_Secondary_Stack_Size is the default secondary stack size used
282 -- when creating an Ada task with no explicit Secondary_Stack_Size clause.
284 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
285 -- disabled. A value of zero indicates that leap seconds are turned "off",
286 -- while a value of one signifies "on" status.
288 -- Main_CPU is the processor set by pragma CPU in the main program. If no
289 -- such pragma is present, the value is -1.
291 -- Default_Sized_SS_Pool is set to the address of the default-sized
292 -- secondary stacks array generated by the binder. This pool of stacks is
293 -- generated when either the restriction No_Implicit_Heap_Allocations
294 -- or No_Implicit_Task_Allocations is active.
296 -- Binder_Sec_Stacks_Count is the number of generated secondary stacks in
297 -- the Default_Sized_SS_Pool.
299 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
300 -- Convenient shorthand used throughout
302 -----------------------
303 -- Local Subprograms --
304 -----------------------
306 procedure Gen_Adainit (Elab_Order : Unit_Id_Array);
307 -- Generates the Adainit procedure
309 procedure Gen_Adafinal;
310 -- Generate the Adafinal procedure
312 procedure Gen_Bind_Env_String;
313 -- Generate the bind environment buffer
315 procedure Gen_CodePeer_Wrapper;
316 -- For CodePeer, generate wrapper which calls user-defined main subprogram
318 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array);
319 -- Generate sequence of elaboration calls
321 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array);
322 -- Generate sequence of external declarations for elaboration
324 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array);
325 -- Generate comments showing elaboration order chosen
327 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array);
328 -- Generate a sequence of finalization calls to elaborated packages
330 procedure Gen_Main;
331 -- Generate procedure main
333 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array);
334 -- Output comments containing a list of the full names of the object
335 -- files to be linked and the list of linker options supplied by
336 -- Linker_Options pragmas in the source.
338 procedure Gen_Output_File_Ada
339 (Filename : String;
340 Elab_Order : Unit_Id_Array);
341 -- Generate Ada output file
343 procedure Gen_Restrictions;
344 -- Generate initialization of restrictions variable
346 procedure Gen_Versions;
347 -- Output series of definitions for unit versions
349 function Get_Ada_Main_Name return String;
350 -- This function is used for the Ada main output to compute a usable name
351 -- for the generated main program. The normal main program name is
352 -- Ada_Main, but this won't work if the user has a unit with this name.
353 -- This function tries Ada_Main first, and if there is such a clash, then
354 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
356 function Get_Main_Unit_Name (S : String) return String;
357 -- Return the main unit name corresponding to S by replacing '.' with '_'
359 function Get_Main_Name return String;
360 -- This function is used in the main output case to compute the correct
361 -- external main program. It is "main" by default, unless the flag
362 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
363 -- of the Ada main name without the "_ada". This default can be overridden
364 -- explicitly using the -Mname binder switch.
366 function Get_WC_Encoding return Character;
367 -- Return wide character encoding method to set as WC_Encoding in output.
368 -- If -W has been used, returns the specified encoding, otherwise returns
369 -- the encoding method used for the main program source. If there is no
370 -- main program source (-z switch used), returns brackets ('b').
372 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean;
373 -- Determine whether the current unit has at least one library-level
374 -- finalizer.
376 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean;
377 -- Compare linker options, when sorting, first according to
378 -- Is_Internal_File (internal files come later) and then by
379 -- elaboration order position (latest to earliest).
381 procedure Move_Linker_Option (From : Natural; To : Natural);
382 -- Move routine for sorting linker options
384 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array);
385 -- Set the value of With_GNARL
387 procedure Set_Char (C : Character);
388 -- Set given character in Statement_Buffer at the Stm_Last + 1 position
389 -- and increment Stm_Last by one to reflect the stored character.
391 procedure Set_Int (N : Int);
392 -- Set given value in decimal in Statement_Buffer with no spaces starting
393 -- at the Stm_Last + 1 position, and updating Stm_Last past the value. A
394 -- minus sign is output for a negative value.
396 procedure Set_Boolean (B : Boolean);
397 -- Set given boolean value in Statement_Buffer at the Stm_Last + 1 position
398 -- and update Stm_Last past the value.
400 procedure Set_IS_Pragma_Table;
401 -- Initializes contents of IS_Pragma_Settings table from ALI table
403 procedure Set_Main_Program_Name;
404 -- Given the main program name in Name_Buffer (length in Name_Len) generate
405 -- the name of the routine to be used in the call. The name is generated
406 -- starting at Stm_Last + 1, and Stm_Last is updated past it.
408 procedure Set_Name_Buffer;
409 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
411 procedure Set_PSD_Pragma_Table;
412 -- Initializes contents of PSD_Pragma_Settings table from ALI table
414 procedure Set_String (S : String);
415 -- Sets characters of given string in Statement_Buffer, starting at the
416 -- Stm_Last + 1 position, and updating last past the string value.
418 procedure Set_String_Replace (S : String);
419 -- Replaces the last S'Length characters in the Statement_Buffer with the
420 -- characters of S. The caller must ensure that these characters do in fact
421 -- exist in the Statement_Buffer.
423 procedure Set_Unit_Name;
424 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
425 -- starting at the Stm_Last + 1 position and update Stm_Last past the
426 -- value. Each dot (.) will be qualified into double underscores (__).
428 procedure Set_Unit_Number (U : Unit_Id);
429 -- Sets unit number (first unit is 1, leading zeroes output to line up all
430 -- output unit numbers nicely as required by the value, and by the total
431 -- number of units.
433 procedure Write_Statement_Buffer;
434 -- Write out contents of statement buffer up to Stm_Last, and reset
435 -- Stm_Last to 0.
437 procedure Write_Statement_Buffer (S : String);
438 -- First writes its argument (using Set_String (S)), then writes out the
439 -- contents of statement buffer up to Stm_Last, and resets Stm_Last to 0.
441 procedure Write_Bind_Line (S : String);
442 -- Write S (an LF-terminated string) to the binder file (for use with
443 -- Set_Special_Output).
445 ------------------
446 -- Gen_Adafinal --
447 ------------------
449 procedure Gen_Adafinal is
450 begin
451 WBI (" procedure " & Ada_Final_Name.all & " is");
453 -- If task termination is not allowed, as is the case in restricted
454 -- runtimes, such as Ravenscar or ZFP, but may not be the case for all
455 -- configurable runtimes, we do not need program-level finalization.
457 if Cumulative_Restrictions.Set (No_Task_Termination) then
458 WBI (" begin");
459 WBI (" null;");
460 WBI (" end " & Ada_Final_Name.all & ";");
461 WBI ("");
462 return;
463 end if;
465 -- Call s_stalib_adafinal to await termination of tasks and so on. We
466 -- want to do this if there is a main program, either in Ada or in some
467 -- other language. (Note that Bind_Main_Program is True for Ada mains,
468 -- but False for mains in other languages.) We do not want to do this if
469 -- we're binding a library.
471 if not Bind_For_Library and not CodePeer_Mode then
472 WBI (" procedure s_stalib_adafinal;");
473 Set_String (" pragma Import (C, s_stalib_adafinal, ");
474 Set_String ("""system__standard_library__adafinal"");");
475 Write_Statement_Buffer;
476 end if;
478 WBI ("");
479 WBI (" procedure Runtime_Finalize;");
480 WBI (" pragma Import (C, Runtime_Finalize, " &
481 """__gnat_runtime_finalize"");");
482 WBI ("");
483 WBI (" begin");
485 if not CodePeer_Mode then
486 WBI (" if not Is_Elaborated then");
487 WBI (" return;");
488 WBI (" end if;");
489 WBI (" Is_Elaborated := False;");
490 end if;
492 WBI (" Runtime_Finalize;");
494 -- By default (real targets), finalization is done differently depending
495 -- on whether this is the main program or a library.
497 if not CodePeer_Mode then
498 if not Bind_For_Library then
499 WBI (" s_stalib_adafinal;");
500 elsif Lib_Final_Built then
501 WBI (" finalize_library;");
502 else
503 WBI (" null;");
504 end if;
506 -- Pragma Import C cannot be used on virtual targets, therefore call the
507 -- runtime finalization routine directly in CodePeer mode, where
508 -- imported functions are ignored.
510 else
511 WBI (" System.Standard_Library.Adafinal;");
512 end if;
514 WBI (" end " & Ada_Final_Name.all & ";");
515 WBI ("");
516 end Gen_Adafinal;
518 -----------------
519 -- Gen_Adainit --
520 -----------------
522 procedure Gen_Adainit (Elab_Order : Unit_Id_Array) is
523 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
524 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
526 begin
527 -- Declare the access-to-subprogram type used for initialization of
528 -- of __gnat_finalize_library_objects. This is declared at library
529 -- level for compatibility with the type used in System.Soft_Links.
530 -- The import of the soft link which performs library-level object
531 -- finalization does not work for CodePeer, so regular Ada is used in
532 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
533 -- tasks are non-terminating, so we do not want finalization.
535 if not Suppress_Standard_Library_On_Target
536 and then not CodePeer_Mode
537 and then not Configurable_Run_Time_On_Target
538 then
539 WBI (" type No_Param_Proc is access procedure;");
540 WBI ("");
541 end if;
543 WBI (" procedure " & Ada_Init_Name.all & " is");
545 -- In CodePeer mode, simplify adainit procedure by only calling
546 -- elaboration procedures.
548 if CodePeer_Mode then
549 WBI (" begin");
551 -- If the standard library is suppressed, then the only global variables
552 -- that might be needed (by the Ravenscar profile) are the priority and
553 -- the processor for the environment task.
555 elsif Suppress_Standard_Library_On_Target then
556 if Main_Priority /= No_Main_Priority then
557 WBI (" Main_Priority : Integer;");
558 WBI (" pragma Import (C, Main_Priority," &
559 " ""__gl_main_priority"");");
560 WBI ("");
561 end if;
563 if Main_CPU /= No_Main_CPU then
564 WBI (" Main_CPU : Integer;");
565 WBI (" pragma Import (C, Main_CPU," &
566 " ""__gl_main_cpu"");");
567 WBI ("");
568 end if;
570 if System_Interrupts_Used
571 and then Partition_Elaboration_Policy_Specified = 'S'
572 then
573 WBI (" procedure Install_Restricted_Handlers_Sequential;");
574 WBI (" pragma Import (C," &
575 "Install_Restricted_Handlers_Sequential," &
576 " ""__gnat_attach_all_handlers"");");
577 WBI ("");
578 end if;
580 if System_Tasking_Restricted_Stages_Used
581 and then Partition_Elaboration_Policy_Specified = 'S'
582 then
583 WBI (" Partition_Elaboration_Policy : Character;");
584 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
585 " ""__gnat_partition_elaboration_policy"");");
586 WBI ("");
587 WBI (" procedure Activate_All_Tasks_Sequential;");
588 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
589 " ""__gnat_activate_all_tasks"");");
590 WBI ("");
591 end if;
593 if System_BB_CPU_Primitives_Multiprocessors_Used then
594 WBI (" procedure Start_Slave_CPUs;");
595 WBI (" pragma Import (C, Start_Slave_CPUs," &
596 " ""__gnat_start_slave_cpus"");");
597 WBI ("");
598 end if;
600 -- A restricted run-time may attempt to initialize the main task's
601 -- secondary stack even if the stack is not used. Consequently,
602 -- the binder needs to initialize Binder_Sec_Stacks_Count anytime
603 -- System.Secondary_Stack is in the enclosure of the partition.
605 if System_Secondary_Stack_Used then
606 WBI (" Binder_Sec_Stacks_Count : Natural;");
607 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
608 """__gnat_binder_ss_count"");");
609 WBI ("");
610 end if;
612 if Sec_Stack_Used then
613 WBI (" Default_Secondary_Stack_Size : " &
614 "System.Parameters.Size_Type;");
615 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
616 """__gnat_default_ss_size"");");
618 WBI (" Default_Sized_SS_Pool : System.Address;");
619 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
620 """__gnat_default_ss_pool"");");
622 WBI ("");
623 end if;
625 WBI (" begin");
627 if Main_Priority /= No_Main_Priority then
628 Set_String (" Main_Priority := ");
629 Set_Int (Main_Priority);
630 Set_Char (';');
631 Write_Statement_Buffer;
632 end if;
634 if Main_CPU /= No_Main_CPU then
635 Set_String (" Main_CPU := ");
636 Set_Int (Main_CPU);
637 Set_Char (';');
638 Write_Statement_Buffer;
639 end if;
641 if System_Tasking_Restricted_Stages_Used
642 and then Partition_Elaboration_Policy_Specified = 'S'
643 then
644 Set_String (" Partition_Elaboration_Policy := '");
645 Set_Char (Partition_Elaboration_Policy_Specified);
646 Set_String ("';");
647 Write_Statement_Buffer;
648 end if;
650 if Main_Priority = No_Main_Priority
651 and then Main_CPU = No_Main_CPU
652 and then not System_Tasking_Restricted_Stages_Used
653 then
654 WBI (" null;");
655 end if;
657 -- Generate default-sized secondary stack pool and set secondary
658 -- stack globals.
660 if Sec_Stack_Used then
662 -- Elaborate the body of the binder to initialize the default-
663 -- sized secondary stack pool.
665 WBI ("");
666 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
668 -- Generate the default-sized secondary stack pool and set the
669 -- related secondary stack globals.
671 Set_String (" Default_Secondary_Stack_Size := ");
673 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
674 Set_Int (Opt.Default_Sec_Stack_Size);
675 else
676 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
677 end if;
679 Set_Char (';');
680 Write_Statement_Buffer;
682 Set_String (" Binder_Sec_Stacks_Count := ");
683 Set_Int (Num_Sec_Stacks);
684 Set_Char (';');
685 Write_Statement_Buffer;
687 WBI (" Default_Sized_SS_Pool := " &
688 "Sec_Default_Sized_Stacks'Address;");
689 WBI ("");
691 -- When a restricted run-time initializes the main task's secondary
692 -- stack but the program does not use it, no secondary stack is
693 -- generated. Binder_Sec_Stacks_Count is set to zero so the run-time
694 -- is aware that the lack of pre-allocated secondary stack is
695 -- expected.
697 elsif System_Secondary_Stack_Used then
698 WBI (" Binder_Sec_Stacks_Count := 0;");
699 end if;
701 -- Normal case (standard library not suppressed). Set all global values
702 -- used by the run time.
704 else
705 WBI (" Main_Priority : Integer;");
706 WBI (" pragma Import (C, Main_Priority, " &
707 """__gl_main_priority"");");
708 WBI (" Time_Slice_Value : Integer;");
709 WBI (" pragma Import (C, Time_Slice_Value, " &
710 """__gl_time_slice_val"");");
711 WBI (" WC_Encoding : Character;");
712 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
713 WBI (" Locking_Policy : Character;");
714 WBI (" pragma Import (C, Locking_Policy, " &
715 """__gl_locking_policy"");");
716 WBI (" Queuing_Policy : Character;");
717 WBI (" pragma Import (C, Queuing_Policy, " &
718 """__gl_queuing_policy"");");
719 WBI (" Task_Dispatching_Policy : Character;");
720 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
721 """__gl_task_dispatching_policy"");");
722 WBI (" Priority_Specific_Dispatching : System.Address;");
723 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
724 """__gl_priority_specific_dispatching"");");
725 WBI (" Num_Specific_Dispatching : Integer;");
726 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
727 """__gl_num_specific_dispatching"");");
728 WBI (" Main_CPU : Integer;");
729 WBI (" pragma Import (C, Main_CPU, " &
730 """__gl_main_cpu"");");
732 WBI (" Interrupt_States : System.Address;");
733 WBI (" pragma Import (C, Interrupt_States, " &
734 """__gl_interrupt_states"");");
735 WBI (" Num_Interrupt_States : Integer;");
736 WBI (" pragma Import (C, Num_Interrupt_States, " &
737 """__gl_num_interrupt_states"");");
738 WBI (" Unreserve_All_Interrupts : Integer;");
739 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
740 """__gl_unreserve_all_interrupts"");");
742 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
743 WBI (" Exception_Tracebacks : Integer;");
744 WBI (" pragma Import (C, Exception_Tracebacks, " &
745 """__gl_exception_tracebacks"");");
747 if Exception_Tracebacks_Symbolic then
748 WBI (" Exception_Tracebacks_Symbolic : Integer;");
749 WBI (" pragma Import (C, Exception_Tracebacks_Symbolic, " &
750 """__gl_exception_tracebacks_symbolic"");");
751 end if;
752 end if;
754 WBI (" Detect_Blocking : Integer;");
755 WBI (" pragma Import (C, Detect_Blocking, " &
756 """__gl_detect_blocking"");");
757 WBI (" Default_Stack_Size : Integer;");
758 WBI (" pragma Import (C, Default_Stack_Size, " &
759 """__gl_default_stack_size"");");
760 WBI (" Default_Secondary_Stack_Size : " &
761 "System.Parameters.Size_Type;");
762 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
763 """__gnat_default_ss_size"");");
764 WBI (" Leap_Seconds_Support : Integer;");
765 WBI (" pragma Import (C, Leap_Seconds_Support, " &
766 """__gl_leap_seconds_support"");");
767 WBI (" Bind_Env_Addr : System.Address;");
768 WBI (" pragma Import (C, Bind_Env_Addr, " &
769 """__gl_bind_env_addr"");");
771 -- Import entry point for elaboration time signal handler
772 -- installation, and indication of if it's been called previously.
774 WBI ("");
775 WBI (" procedure Runtime_Initialize " &
776 "(Install_Handler : Integer);");
777 WBI (" pragma Import (C, Runtime_Initialize, " &
778 """__gnat_runtime_initialize"");");
780 -- Import handlers attach procedure for sequential elaboration policy
782 if System_Interrupts_Used
783 and then Partition_Elaboration_Policy_Specified = 'S'
784 then
785 WBI (" procedure Install_Restricted_Handlers_Sequential;");
786 WBI (" pragma Import (C," &
787 "Install_Restricted_Handlers_Sequential," &
788 " ""__gnat_attach_all_handlers"");");
789 WBI ("");
790 end if;
792 -- Import task activation procedure for sequential elaboration
793 -- policy.
795 if System_Tasking_Restricted_Stages_Used
796 and then Partition_Elaboration_Policy_Specified = 'S'
797 then
798 WBI (" Partition_Elaboration_Policy : Character;");
799 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
800 " ""__gnat_partition_elaboration_policy"");");
801 WBI ("");
802 WBI (" procedure Activate_All_Tasks_Sequential;");
803 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
804 " ""__gnat_activate_all_tasks"");");
805 end if;
807 -- Import procedure to start slave cpus for bareboard runtime
809 if System_BB_CPU_Primitives_Multiprocessors_Used then
810 WBI (" procedure Start_Slave_CPUs;");
811 WBI (" pragma Import (C, Start_Slave_CPUs," &
812 " ""__gnat_start_slave_cpus"");");
813 end if;
815 -- For restricted run-time libraries (ZFP and Ravenscar)
816 -- tasks are non-terminating, so we do not want finalization.
818 if not Configurable_Run_Time_On_Target then
819 WBI ("");
820 WBI (" Finalize_Library_Objects : No_Param_Proc;");
821 WBI (" pragma Import (C, Finalize_Library_Objects, " &
822 """__gnat_finalize_library_objects"");");
823 end if;
825 -- Initialize stack limit variable of the environment task if the
826 -- stack check method is stack limit and stack check is enabled.
828 if Stack_Check_Limits_On_Target
829 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
830 then
831 WBI ("");
832 WBI (" procedure Initialize_Stack_Limit;");
833 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
834 """__gnat_initialize_stack_limit"");");
835 end if;
837 -- When dispatching domains are used then we need to signal it
838 -- before calling the main procedure.
840 if Dispatching_Domains_Used then
841 WBI (" procedure Freeze_Dispatching_Domains;");
842 WBI (" pragma Import");
843 WBI (" (Ada, Freeze_Dispatching_Domains, "
844 & """__gnat_freeze_dispatching_domains"");");
845 end if;
847 -- Secondary stack global variables
849 WBI (" Binder_Sec_Stacks_Count : Natural;");
850 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
851 """__gnat_binder_ss_count"");");
853 WBI (" Default_Sized_SS_Pool : System.Address;");
854 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
855 """__gnat_default_ss_pool"");");
857 WBI ("");
859 -- Start of processing for Adainit
861 WBI (" begin");
862 WBI (" if Is_Elaborated then");
863 WBI (" return;");
864 WBI (" end if;");
865 WBI (" Is_Elaborated := True;");
867 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
868 -- restriction No_Standard_Allocators_After_Elaboration is active.
870 if Cumulative_Restrictions.Set
871 (No_Standard_Allocators_After_Elaboration)
872 then
873 WBI (" System.Elaboration_Allocators."
874 & "Mark_Start_Of_Elaboration;");
875 end if;
877 -- Generate assignments to initialize globals
879 Set_String (" Main_Priority := ");
880 Set_Int (Main_Priority);
881 Set_Char (';');
882 Write_Statement_Buffer;
884 Set_String (" Time_Slice_Value := ");
886 if Task_Dispatching_Policy_Specified = 'F'
887 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
888 then
889 Set_Int (0);
890 else
891 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
892 end if;
894 Set_Char (';');
895 Write_Statement_Buffer;
897 Set_String (" WC_Encoding := '");
898 Set_Char (Get_WC_Encoding);
900 Set_String ("';");
901 Write_Statement_Buffer;
903 Set_String (" Locking_Policy := '");
904 Set_Char (Locking_Policy_Specified);
905 Set_String ("';");
906 Write_Statement_Buffer;
908 Set_String (" Queuing_Policy := '");
909 Set_Char (Queuing_Policy_Specified);
910 Set_String ("';");
911 Write_Statement_Buffer;
913 Set_String (" Task_Dispatching_Policy := '");
914 Set_Char (Task_Dispatching_Policy_Specified);
915 Set_String ("';");
916 Write_Statement_Buffer;
918 if System_Tasking_Restricted_Stages_Used
919 and then Partition_Elaboration_Policy_Specified = 'S'
920 then
921 Set_String (" Partition_Elaboration_Policy := '");
922 Set_Char (Partition_Elaboration_Policy_Specified);
923 Set_String ("';");
924 Write_Statement_Buffer;
925 end if;
927 Gen_Restrictions;
929 WBI (" Priority_Specific_Dispatching :=");
930 WBI (" Local_Priority_Specific_Dispatching'Address;");
932 Set_String (" Num_Specific_Dispatching := ");
933 Set_Int (PSD_Pragma_Settings.Last + 1);
934 Set_Char (';');
935 Write_Statement_Buffer;
937 Set_String (" Main_CPU := ");
938 Set_Int (Main_CPU);
939 Set_Char (';');
940 Write_Statement_Buffer;
942 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
944 Set_String (" Num_Interrupt_States := ");
945 Set_Int (IS_Pragma_Settings.Last + 1);
946 Set_Char (';');
947 Write_Statement_Buffer;
949 Set_String (" Unreserve_All_Interrupts := ");
951 if Unreserve_All_Interrupts_Specified then
952 Set_String ("1");
953 else
954 Set_String ("0");
955 end if;
957 Set_Char (';');
958 Write_Statement_Buffer;
960 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
961 WBI (" Exception_Tracebacks := 1;");
963 if Exception_Tracebacks_Symbolic then
964 WBI (" Exception_Tracebacks_Symbolic := 1;");
965 end if;
966 end if;
968 Set_String (" Detect_Blocking := ");
970 if Detect_Blocking then
971 Set_Int (1);
972 else
973 Set_Int (0);
974 end if;
976 Set_String (";");
977 Write_Statement_Buffer;
979 Set_String (" Default_Stack_Size := ");
980 Set_Int (Default_Stack_Size);
981 Set_String (";");
982 Write_Statement_Buffer;
984 Set_String (" Leap_Seconds_Support := ");
986 if Leap_Seconds_Support then
987 Set_Int (1);
988 else
989 Set_Int (0);
990 end if;
992 Set_String (";");
993 Write_Statement_Buffer;
995 if Bind_Env_String_Built then
996 WBI (" Bind_Env_Addr := Bind_Env'Address;");
997 end if;
999 WBI ("");
1001 -- Generate default-sized secondary stack pool and set secondary
1002 -- stack globals.
1004 if Sec_Stack_Used then
1006 -- Elaborate the body of the binder to initialize the default-
1007 -- sized secondary stack pool.
1009 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
1011 -- Generate the default-sized secondary stack pool and set the
1012 -- related secondary stack globals.
1014 Set_String (" Default_Secondary_Stack_Size := ");
1016 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
1017 Set_Int (Opt.Default_Sec_Stack_Size);
1018 else
1019 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
1020 end if;
1022 Set_Char (';');
1023 Write_Statement_Buffer;
1025 Set_String (" Binder_Sec_Stacks_Count := ");
1026 Set_Int (Num_Sec_Stacks);
1027 Set_Char (';');
1028 Write_Statement_Buffer;
1030 Set_String (" Default_Sized_SS_Pool := ");
1032 if Num_Sec_Stacks > 0 then
1033 Set_String ("Sec_Default_Sized_Stacks'Address;");
1034 else
1035 Set_String ("System.Null_Address;");
1036 end if;
1038 Write_Statement_Buffer;
1039 WBI ("");
1040 end if;
1042 -- Generate call to Runtime_Initialize
1044 WBI (" Runtime_Initialize (1);");
1045 end if;
1047 -- Generate call to set Initialize_Scalar values if active
1049 if Initialize_Scalars_Used then
1050 WBI ("");
1051 Set_String (" System.Scalar_Values.Initialize ('");
1052 Set_Char (Initialize_Scalars_Mode1);
1053 Set_String ("', '");
1054 Set_Char (Initialize_Scalars_Mode2);
1055 Set_String ("');");
1056 Write_Statement_Buffer;
1057 end if;
1059 -- Initialize stack limit variable of the environment task if the stack
1060 -- check method is stack limit and stack check is enabled.
1062 if Stack_Check_Limits_On_Target
1063 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1064 then
1065 WBI ("");
1066 WBI (" Initialize_Stack_Limit;");
1067 end if;
1069 -- On CodePeer, the finalization of library objects is not relevant
1071 if CodePeer_Mode then
1072 null;
1074 -- If this is the main program case, attach finalize_library to the soft
1075 -- link. Do it only when not using a restricted run time, in which case
1076 -- tasks are non-terminating, so we do not want library-level
1077 -- finalization.
1079 elsif not Bind_For_Library
1080 and then not Configurable_Run_Time_On_Target
1081 and then not Suppress_Standard_Library_On_Target
1082 then
1083 WBI ("");
1085 if Lib_Final_Built then
1086 Set_String (" Finalize_Library_Objects := ");
1087 Set_String ("finalize_library'access;");
1088 else
1089 Set_String (" Finalize_Library_Objects := null;");
1090 end if;
1092 Write_Statement_Buffer;
1093 end if;
1095 -- Generate elaboration calls
1097 if not CodePeer_Mode then
1098 WBI ("");
1099 end if;
1101 Gen_Elab_Calls (Elab_Order);
1103 if not CodePeer_Mode then
1105 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
1106 -- restriction No_Standard_Allocators_After_Elaboration is active.
1108 if Cumulative_Restrictions.Set
1109 (No_Standard_Allocators_After_Elaboration)
1110 then
1112 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
1113 end if;
1115 -- From this point, no new dispatching domain can be created
1117 if Dispatching_Domains_Used then
1118 WBI (" Freeze_Dispatching_Domains;");
1119 end if;
1121 -- Sequential partition elaboration policy
1123 if Partition_Elaboration_Policy_Specified = 'S' then
1124 if System_Interrupts_Used then
1125 WBI (" Install_Restricted_Handlers_Sequential;");
1126 end if;
1128 if System_Tasking_Restricted_Stages_Used then
1129 WBI (" Activate_All_Tasks_Sequential;");
1130 end if;
1131 end if;
1133 if System_BB_CPU_Primitives_Multiprocessors_Used then
1134 WBI (" Start_Slave_CPUs;");
1135 end if;
1136 end if;
1138 WBI (" end " & Ada_Init_Name.all & ";");
1139 WBI ("");
1140 end Gen_Adainit;
1142 -------------------------
1143 -- Gen_Bind_Env_String --
1144 -------------------------
1146 procedure Gen_Bind_Env_String is
1147 procedure Write_Name_With_Len (Nam : Name_Id);
1148 -- Write Nam as a string literal, prefixed with one
1149 -- character encoding Nam's length.
1151 -------------------------
1152 -- Write_Name_With_Len --
1153 -------------------------
1155 procedure Write_Name_With_Len (Nam : Name_Id) is
1156 begin
1157 Get_Name_String (Nam);
1159 Start_String;
1160 Store_String_Char (Character'Val (Name_Len));
1161 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1163 Write_String_Table_Entry (End_String);
1164 end Write_Name_With_Len;
1166 -- Local variables
1168 Amp : Character;
1169 KN : Name_Id := No_Name;
1170 VN : Name_Id := No_Name;
1172 -- Start of processing for Gen_Bind_Env_String
1174 begin
1175 Bind_Environment.Get_First (KN, VN);
1177 if VN = No_Name then
1178 return;
1179 end if;
1181 Set_Special_Output (Write_Bind_Line'Access);
1183 WBI (" Bind_Env : aliased constant String :=");
1184 Amp := ' ';
1185 while VN /= No_Name loop
1186 Write_Str (" " & Amp & ' ');
1187 Write_Name_With_Len (KN);
1188 Write_Str (" & ");
1189 Write_Name_With_Len (VN);
1190 Write_Eol;
1192 Bind_Environment.Get_Next (KN, VN);
1193 Amp := '&';
1194 end loop;
1195 WBI (" & ASCII.NUL;");
1197 Set_Special_Output (null);
1199 Bind_Env_String_Built := True;
1200 end Gen_Bind_Env_String;
1202 --------------------------
1203 -- Gen_CodePeer_Wrapper --
1204 --------------------------
1206 procedure Gen_CodePeer_Wrapper is
1207 Callee_Name : constant String := "Ada_Main_Program";
1209 begin
1210 if ALIs.Table (ALIs.First).Main_Program = Proc then
1211 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
1212 WBI (" begin");
1213 WBI (" " & Callee_Name & ";");
1215 else
1216 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1217 WBI (" begin");
1218 WBI (" return " & Callee_Name & ";");
1219 end if;
1221 WBI (" end " & CodePeer_Wrapper_Name & ";");
1222 WBI ("");
1223 end Gen_CodePeer_Wrapper;
1225 --------------------
1226 -- Gen_Elab_Calls --
1227 --------------------
1229 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array) is
1230 Check_Elab_Flag : Boolean;
1232 begin
1233 -- Loop through elaboration order entries
1235 for E in Elab_Order'Range loop
1236 declare
1237 Unum : constant Unit_Id := Elab_Order (E);
1238 U : Unit_Record renames Units.Table (Unum);
1240 Unum_Spec : Unit_Id;
1241 -- This is the unit number of the spec that corresponds to
1242 -- this entry. It is the same as Unum except when the body
1243 -- and spec are different and we are currently processing
1244 -- the body, in which case it is the spec (Unum + 1).
1246 begin
1247 if U.Utype = Is_Body then
1248 Unum_Spec := Unum + 1;
1249 else
1250 Unum_Spec := Unum;
1251 end if;
1253 -- Nothing to do if predefined unit in no run time mode
1255 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1256 null;
1258 -- Likewise if this is an interface to a stand alone library
1260 elsif U.SAL_Interface then
1261 null;
1263 -- Case of no elaboration code
1265 elsif U.No_Elab
1267 -- In CodePeer mode, we special case subprogram bodies which
1268 -- are handled in the 'else' part below, and lead to a call
1269 -- to <subp>'Elab_Subp_Body.
1271 and then (not CodePeer_Mode
1273 -- Test for spec
1275 or else U.Utype = Is_Spec
1276 or else U.Utype = Is_Spec_Only
1277 or else U.Unit_Kind /= 's')
1278 then
1279 -- In the case of a body with a separate spec, where the
1280 -- separate spec has an elaboration entity defined, this is
1281 -- where we increment the elaboration entity if one exists.
1283 -- Likewise for lone specs with an elaboration entity defined
1284 -- despite No_Elaboration_Code, e.g. when requested to preserve
1285 -- control flow.
1287 if (U.Utype = Is_Body or else U.Utype = Is_Spec_Only)
1288 and then Units.Table (Unum_Spec).Set_Elab_Entity
1289 and then not CodePeer_Mode
1290 then
1291 Set_String (" E");
1292 Set_Unit_Number (Unum_Spec);
1293 Set_String (" := E");
1294 Set_Unit_Number (Unum_Spec);
1295 Set_String (" + 1;");
1296 Write_Statement_Buffer;
1297 end if;
1299 -- Here if elaboration code is present. If binding a library
1300 -- or if there is a non-Ada main subprogram then we generate:
1302 -- if uname_E = 0 then
1303 -- uname'elab_[spec|body];
1304 -- end if;
1305 -- uname_E := uname_E + 1;
1307 -- Otherwise, elaboration routines are called unconditionally:
1309 -- uname'elab_[spec|body];
1310 -- uname_E := uname_E + 1;
1312 -- The uname_E increment is skipped if this is a separate spec,
1313 -- since it will be done when we process the body.
1315 -- In CodePeer mode, we do not generate any reference to xxx_E
1316 -- variables, only calls to 'Elab* subprograms.
1318 else
1319 -- Check incompatibilities with No_Multiple_Elaboration
1321 if not CodePeer_Mode
1322 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1323 then
1324 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1326 if Force_Checking_Of_Elaboration_Flags then
1327 Osint.Fail
1328 ("-F (force elaboration checks) switch not allowed "
1329 & "with restriction No_Multiple_Elaboration active");
1331 -- Interfacing of libraries not allowed
1333 elsif Interface_Library_Unit then
1334 Osint.Fail
1335 ("binding of interfaced libraries not allowed "
1336 & "with restriction No_Multiple_Elaboration active");
1338 -- Non-Ada main program not allowed
1340 elsif not Bind_Main_Program then
1341 Osint.Fail
1342 ("non-Ada main program not allowed "
1343 & "with restriction No_Multiple_Elaboration active");
1344 end if;
1345 end if;
1347 -- OK, see if we need to test elaboration flag
1349 Check_Elab_Flag :=
1350 Units.Table (Unum_Spec).Set_Elab_Entity
1351 and then not CodePeer_Mode
1352 and then (Force_Checking_Of_Elaboration_Flags
1353 or Interface_Library_Unit
1354 or not Bind_Main_Program);
1356 if Check_Elab_Flag then
1357 Set_String (" if E");
1358 Set_Unit_Number (Unum_Spec);
1359 Set_String (" = 0 then");
1360 Write_Statement_Buffer;
1361 Set_String (" ");
1362 end if;
1364 Set_String (" ");
1365 Get_Decoded_Name_String_With_Brackets (U.Uname);
1367 if Name_Buffer (Name_Len) = 's' then
1368 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1369 "'elab_spec";
1370 Name_Len := Name_Len + 8;
1372 -- Special case in CodePeer mode for subprogram bodies
1373 -- which correspond to CodePeer 'Elab_Subp_Body special
1374 -- init procedure.
1376 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1377 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1378 "'elab_subp_body";
1379 Name_Len := Name_Len + 13;
1381 else
1382 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1383 "'elab_body";
1384 Name_Len := Name_Len + 8;
1385 end if;
1387 Set_Casing (U.Icasing);
1388 Set_Name_Buffer;
1389 Set_Char (';');
1390 Write_Statement_Buffer;
1392 if Check_Elab_Flag then
1393 WBI (" end if;");
1394 end if;
1396 if U.Utype /= Is_Spec
1397 and then not CodePeer_Mode
1398 and then Units.Table (Unum_Spec).Set_Elab_Entity
1399 then
1400 Set_String (" E");
1401 Set_Unit_Number (Unum_Spec);
1402 Set_String (" := E");
1403 Set_Unit_Number (Unum_Spec);
1404 Set_String (" + 1;");
1405 Write_Statement_Buffer;
1406 end if;
1407 end if;
1408 end;
1409 end loop;
1410 end Gen_Elab_Calls;
1412 ------------------------
1413 -- Gen_Elab_Externals --
1414 ------------------------
1416 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array) is
1417 begin
1418 if CodePeer_Mode then
1419 return;
1420 end if;
1422 for E in Elab_Order'Range loop
1423 declare
1424 Unum : constant Unit_Id := Elab_Order (E);
1425 U : Unit_Record renames Units.Table (Unum);
1427 begin
1428 -- Check for Elab_Entity to be set for this unit
1430 if U.Set_Elab_Entity
1432 -- Don't generate reference for stand alone library
1434 and then not U.SAL_Interface
1436 -- Don't generate reference for predefined file in No_Run_Time
1437 -- mode, since we don't include the object files in this case
1439 and then not
1440 (No_Run_Time_Mode
1441 and then Is_Predefined_File_Name (U.Sfile))
1442 then
1443 Get_Name_String (U.Sfile);
1444 Set_String (" ");
1445 Set_String ("E");
1446 Set_Unit_Number (Unum);
1447 Set_String (" : Short_Integer; pragma Import (Ada, E");
1448 Set_Unit_Number (Unum);
1449 Set_String (", """);
1450 Get_Name_String (U.Uname);
1451 Set_Unit_Name;
1452 Set_String ("_E"");");
1453 Write_Statement_Buffer;
1454 end if;
1455 end;
1456 end loop;
1458 WBI ("");
1459 end Gen_Elab_Externals;
1461 --------------------
1462 -- Gen_Elab_Order --
1463 --------------------
1465 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array) is
1466 begin
1467 WBI ("");
1468 WBI (" -- BEGIN ELABORATION ORDER");
1470 for J in Elab_Order'Range loop
1471 Set_String (" -- ");
1472 Get_Name_String (Units.Table (Elab_Order (J)).Uname);
1473 Set_Name_Buffer;
1474 Write_Statement_Buffer;
1475 end loop;
1477 WBI (" -- END ELABORATION ORDER");
1478 end Gen_Elab_Order;
1480 --------------------------
1481 -- Gen_Finalize_Library --
1482 --------------------------
1484 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array) is
1485 procedure Gen_Header;
1486 -- Generate the header of the finalization routine
1488 ----------------
1489 -- Gen_Header --
1490 ----------------
1492 procedure Gen_Header is
1493 begin
1494 WBI (" procedure finalize_library is");
1495 WBI (" begin");
1496 end Gen_Header;
1498 -- Local variables
1500 Count : Int := 1;
1501 U : Unit_Record;
1502 Uspec : Unit_Record;
1503 Unum : Unit_Id;
1505 -- Start of processing for Gen_Finalize_Library
1507 begin
1508 if CodePeer_Mode then
1509 return;
1510 end if;
1512 for E in reverse Elab_Order'Range loop
1513 Unum := Elab_Order (E);
1514 U := Units.Table (Unum);
1516 -- Dealing with package bodies is a little complicated. In such
1517 -- cases we must retrieve the package spec since it contains the
1518 -- spec of the body finalizer.
1520 if U.Utype = Is_Body then
1521 Unum := Unum + 1;
1522 Uspec := Units.Table (Unum);
1523 else
1524 Uspec := U;
1525 end if;
1527 Get_Name_String (Uspec.Uname);
1529 -- We are only interested in non-generic packages
1531 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1532 null;
1534 -- That aren't an interface to a stand alone library
1536 elsif U.SAL_Interface then
1537 null;
1539 -- Case of no finalization
1541 elsif not U.Has_Finalizer then
1543 -- The only case in which we have to do something is if this
1544 -- is a body, with a separate spec, where the separate spec
1545 -- has a finalizer. In that case, this is where we decrement
1546 -- the elaboration entity.
1548 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1549 if not Lib_Final_Built then
1550 Gen_Header;
1551 Lib_Final_Built := True;
1552 end if;
1554 Set_String (" E");
1555 Set_Unit_Number (Unum);
1556 Set_String (" := E");
1557 Set_Unit_Number (Unum);
1558 Set_String (" - 1;");
1559 Write_Statement_Buffer;
1560 end if;
1562 else
1563 if not Lib_Final_Built then
1564 Gen_Header;
1565 Lib_Final_Built := True;
1566 end if;
1568 -- Generate:
1569 -- declare
1570 -- procedure F<Count>;
1572 Set_String (" declare");
1573 Write_Statement_Buffer;
1575 Set_String (" procedure F");
1576 Set_Int (Count);
1577 Set_Char (';');
1578 Write_Statement_Buffer;
1580 -- Generate:
1581 -- pragma Import (Ada, F<Count>,
1582 -- "xx__yy__finalize_[body|spec]");
1584 Set_String (" pragma Import (Ada, F");
1585 Set_Int (Count);
1586 Set_String (", """);
1588 -- Perform name construction
1590 Set_Unit_Name;
1591 Set_String ("__finalize_");
1593 -- Package spec processing
1595 if U.Utype = Is_Spec
1596 or else U.Utype = Is_Spec_Only
1597 then
1598 Set_String ("spec");
1600 -- Package body processing
1602 else
1603 Set_String ("body");
1604 end if;
1606 Set_String (""");");
1607 Write_Statement_Buffer;
1609 -- If binding a library or if there is a non-Ada main subprogram
1610 -- then we generate:
1612 -- begin
1613 -- uname_E := uname_E - 1;
1614 -- if uname_E = 0 then
1615 -- F<Count>;
1616 -- end if;
1617 -- end;
1619 -- Otherwise, finalization routines are called unconditionally:
1621 -- begin
1622 -- uname_E := uname_E - 1;
1623 -- F<Count>;
1624 -- end;
1626 -- The uname_E decrement is skipped if this is a separate spec,
1627 -- since it will be done when we process the body.
1629 WBI (" begin");
1631 if U.Utype /= Is_Spec then
1632 Set_String (" E");
1633 Set_Unit_Number (Unum);
1634 Set_String (" := E");
1635 Set_Unit_Number (Unum);
1636 Set_String (" - 1;");
1637 Write_Statement_Buffer;
1638 end if;
1640 if Interface_Library_Unit or not Bind_Main_Program then
1641 Set_String (" if E");
1642 Set_Unit_Number (Unum);
1643 Set_String (" = 0 then");
1644 Write_Statement_Buffer;
1645 Set_String (" ");
1646 end if;
1648 Set_String (" F");
1649 Set_Int (Count);
1650 Set_Char (';');
1651 Write_Statement_Buffer;
1653 if Interface_Library_Unit or not Bind_Main_Program then
1654 WBI (" end if;");
1655 end if;
1657 WBI (" end;");
1659 Count := Count + 1;
1660 end if;
1661 end loop;
1663 if Lib_Final_Built then
1665 -- It is possible that the finalization of a library-level object
1666 -- raised an exception. In that case import the actual exception
1667 -- and the routine necessary to raise it.
1669 WBI (" declare");
1670 WBI (" procedure Reraise_Library_Exception_If_Any;");
1672 Set_String (" pragma Import (Ada, ");
1673 Set_String ("Reraise_Library_Exception_If_Any, ");
1674 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1675 Write_Statement_Buffer;
1677 WBI (" begin");
1678 WBI (" Reraise_Library_Exception_If_Any;");
1679 WBI (" end;");
1680 WBI (" end finalize_library;");
1681 WBI ("");
1682 end if;
1683 end Gen_Finalize_Library;
1685 --------------
1686 -- Gen_Main --
1687 --------------
1689 procedure Gen_Main is
1690 begin
1691 if not No_Main_Subprogram then
1693 -- To call the main program, we declare it using a pragma Import
1694 -- Ada with the right link name.
1696 -- It might seem more obvious to "with" the main program, and call
1697 -- it in the normal Ada manner. We do not do this for three
1698 -- reasons:
1700 -- 1. It is more efficient not to recompile the main program
1701 -- 2. We are not entitled to assume the source is accessible
1702 -- 3. We don't know what options to use to compile it
1704 -- It is really reason 3 that is most critical (indeed we used
1705 -- to generate the "with", but several regression tests failed).
1707 if ALIs.Table (ALIs.First).Main_Program = Func then
1708 WBI (" function Ada_Main_Program return Integer;");
1709 else
1710 WBI (" procedure Ada_Main_Program;");
1711 end if;
1713 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1714 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1715 Set_Main_Program_Name;
1716 Set_String (""");");
1718 Write_Statement_Buffer;
1719 WBI ("");
1721 -- For CodePeer, declare a wrapper for the user-defined main program
1723 if CodePeer_Mode then
1724 Gen_CodePeer_Wrapper;
1725 end if;
1726 end if;
1728 if Exit_Status_Supported_On_Target then
1729 Set_String (" function ");
1730 else
1731 Set_String (" procedure ");
1732 end if;
1734 Set_String (Get_Main_Name);
1736 if Command_Line_Args_On_Target then
1737 Write_Statement_Buffer;
1738 WBI (" (argc : Integer;");
1739 WBI (" argv : System.Address;");
1740 WBI (" envp : System.Address)");
1742 if Exit_Status_Supported_On_Target then
1743 WBI (" return Integer");
1744 end if;
1746 WBI (" is");
1748 else
1749 if Exit_Status_Supported_On_Target then
1750 Set_String (" return Integer is");
1751 else
1752 Set_String (" is");
1753 end if;
1755 Write_Statement_Buffer;
1756 end if;
1758 if Opt.Default_Exit_Status /= 0
1759 and then Bind_Main_Program
1760 and then not Configurable_Run_Time_Mode
1761 then
1762 WBI (" procedure Set_Exit_Status (Status : Integer);");
1763 WBI (" pragma Import (C, Set_Exit_Status, " &
1764 """__gnat_set_exit_status"");");
1765 WBI ("");
1766 end if;
1768 -- Initialize and Finalize
1770 if not CodePeer_Mode
1771 and then not Cumulative_Restrictions.Set (No_Finalization)
1772 then
1773 WBI (" procedure Initialize (Addr : System.Address);");
1774 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1775 WBI ("");
1776 WBI (" procedure Finalize;");
1777 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1778 end if;
1780 -- If we want to analyze the stack, we must import corresponding symbols
1782 if Dynamic_Stack_Measurement then
1783 WBI ("");
1784 WBI (" procedure Output_Results;");
1785 WBI (" pragma Import (C, Output_Results, " &
1786 """__gnat_stack_usage_output_results"");");
1788 WBI ("");
1789 WBI (" " &
1790 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1791 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1792 """__gnat_stack_usage_initialize"");");
1793 end if;
1795 -- Deal with declarations for main program case
1797 if not No_Main_Subprogram then
1798 if ALIs.Table (ALIs.First).Main_Program = Func then
1799 WBI (" Result : Integer;");
1800 WBI ("");
1801 end if;
1803 if Bind_Main_Program
1804 and not Suppress_Standard_Library_On_Target
1805 and not CodePeer_Mode
1806 then
1807 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1808 WBI ("");
1809 end if;
1810 end if;
1812 -- Generate a reference to Ada_Main_Program_Name. This symbol is not
1813 -- referenced elsewhere in the generated program, but is needed by
1814 -- the debugger (that's why it is generated in the first place). The
1815 -- reference stops Ada_Main_Program_Name from being optimized away by
1816 -- smart linkers, such as the AiX linker.
1818 -- Because this variable is unused, we make this variable "aliased"
1819 -- with a pragma Volatile in order to tell the compiler to preserve
1820 -- this variable at any level of optimization.
1822 if Bind_Main_Program and not CodePeer_Mode then
1823 WBI (" Ensure_Reference : aliased System.Address := " &
1824 "Ada_Main_Program_Name'Address;");
1825 WBI (" pragma Volatile (Ensure_Reference);");
1826 WBI ("");
1827 end if;
1829 WBI (" begin");
1831 -- Acquire command line arguments if present on target
1833 if CodePeer_Mode then
1834 null;
1836 elsif Command_Line_Args_On_Target then
1837 WBI (" gnat_argc := argc;");
1838 WBI (" gnat_argv := argv;");
1839 WBI (" gnat_envp := envp;");
1840 WBI ("");
1842 -- If configurable run time and no command line args, then nothing needs
1843 -- to be done since the gnat_argc/argv/envp variables are suppressed in
1844 -- this case.
1846 elsif Configurable_Run_Time_On_Target then
1847 null;
1849 -- Otherwise set dummy values (to be filled in by some other unit?)
1851 else
1852 WBI (" gnat_argc := 0;");
1853 WBI (" gnat_argv := System.Null_Address;");
1854 WBI (" gnat_envp := System.Null_Address;");
1855 end if;
1857 if Opt.Default_Exit_Status /= 0
1858 and then Bind_Main_Program
1859 and then not Configurable_Run_Time_Mode
1860 then
1861 Set_String (" Set_Exit_Status (");
1862 Set_Int (Opt.Default_Exit_Status);
1863 Set_String (");");
1864 Write_Statement_Buffer;
1865 end if;
1867 if Dynamic_Stack_Measurement then
1868 Set_String (" Initialize_Stack_Analysis (");
1869 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1870 Set_String (");");
1871 Write_Statement_Buffer;
1872 end if;
1874 if not Cumulative_Restrictions.Set (No_Finalization)
1875 and then not CodePeer_Mode
1876 then
1877 if not No_Main_Subprogram
1878 and then Bind_Main_Program
1879 and then not Suppress_Standard_Library_On_Target
1880 then
1881 WBI (" Initialize (SEH'Address);");
1882 else
1883 WBI (" Initialize (System.Null_Address);");
1884 end if;
1885 end if;
1887 WBI (" " & Ada_Init_Name.all & ";");
1889 if not No_Main_Subprogram then
1890 if CodePeer_Mode then
1891 if ALIs.Table (ALIs.First).Main_Program = Proc then
1892 WBI (" " & CodePeer_Wrapper_Name & ";");
1893 else
1894 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1895 end if;
1897 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1898 WBI (" Ada_Main_Program;");
1900 else
1901 WBI (" Result := Ada_Main_Program;");
1902 end if;
1903 end if;
1905 -- Adafinal call is skipped if no finalization
1907 if not Cumulative_Restrictions.Set (No_Finalization) then
1908 WBI (" adafinal;");
1909 end if;
1911 -- Prints the result of static stack analysis
1913 if Dynamic_Stack_Measurement then
1914 WBI (" Output_Results;");
1915 end if;
1917 -- Finalize is only called if we have a run time
1919 if not Cumulative_Restrictions.Set (No_Finalization)
1920 and then not CodePeer_Mode
1921 then
1922 WBI (" Finalize;");
1923 end if;
1925 -- Return result
1927 if Exit_Status_Supported_On_Target then
1928 if No_Main_Subprogram
1929 or else ALIs.Table (ALIs.First).Main_Program = Proc
1930 then
1931 WBI (" return (gnat_exit_status);");
1932 else
1933 WBI (" return (Result);");
1934 end if;
1935 end if;
1937 WBI (" end;");
1938 WBI ("");
1939 end Gen_Main;
1941 ------------------------------
1942 -- Gen_Object_Files_Options --
1943 ------------------------------
1945 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array) is
1946 Lgnat : Natural;
1947 -- This keeps track of the position in the sorted set of entries in the
1948 -- Linker_Options table of where the first entry from an internal file
1949 -- appears.
1951 Linker_Option_List_Started : Boolean := False;
1952 -- Set to True when "LINKER OPTION LIST" is displayed
1954 procedure Write_Linker_Option;
1955 -- Write binder info linker option
1957 -------------------------
1958 -- Write_Linker_Option --
1959 -------------------------
1961 procedure Write_Linker_Option is
1962 Start : Natural;
1963 Stop : Natural;
1965 begin
1966 -- Loop through string, breaking at null's
1968 Start := 1;
1969 while Start < Name_Len loop
1971 -- Find null ending this section
1973 Stop := Start + 1;
1974 while Name_Buffer (Stop) /= ASCII.NUL
1975 and then Stop <= Name_Len loop
1976 Stop := Stop + 1;
1977 end loop;
1979 -- Process section if non-null
1981 if Stop > Start then
1982 if Output_Linker_Option_List then
1983 if not Zero_Formatting then
1984 if not Linker_Option_List_Started then
1985 Linker_Option_List_Started := True;
1986 Write_Eol;
1987 Write_Str (" LINKER OPTION LIST");
1988 Write_Eol;
1989 Write_Eol;
1990 end if;
1992 Write_Str (" ");
1993 end if;
1995 Write_Str (Name_Buffer (Start .. Stop - 1));
1996 Write_Eol;
1997 end if;
1998 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
1999 end if;
2001 Start := Stop + 1;
2002 end loop;
2003 end Write_Linker_Option;
2005 -- Start of processing for Gen_Object_Files_Options
2007 begin
2008 WBI ("-- BEGIN Object file/option list");
2010 if Object_List_Filename /= null then
2011 Set_List_File (Object_List_Filename.all);
2012 end if;
2014 for E in Elab_Order'Range loop
2016 -- If not spec that has an associated body, then generate a comment
2017 -- giving the name of the corresponding object file.
2019 if not Units.Table (Elab_Order (E)).SAL_Interface
2020 and then Units.Table (Elab_Order (E)).Utype /= Is_Spec
2021 then
2022 Get_Name_String
2023 (ALIs.Table
2024 (Units.Table (Elab_Order (E)).My_ALI).Ofile_Full_Name);
2026 -- If the presence of an object file is necessary or if it exists,
2027 -- then use it.
2029 if not Hostparm.Exclude_Missing_Objects
2030 or else
2031 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
2032 then
2033 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2035 if Output_Object_List then
2036 Write_Str (Name_Buffer (1 .. Name_Len));
2037 Write_Eol;
2038 end if;
2039 end if;
2040 end if;
2041 end loop;
2043 if Object_List_Filename /= null then
2044 Close_List_File;
2045 end if;
2047 -- Add a "-Ldir" for each directory in the object path
2049 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2050 declare
2051 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2053 begin
2054 Name_Len := 0;
2055 Add_Str_To_Name_Buffer ("-L");
2056 Add_Str_To_Name_Buffer (Dir.all);
2057 Write_Linker_Option;
2058 end;
2059 end loop;
2061 if not (Opt.No_Run_Time_Mode or Opt.No_Stdlib) then
2062 Name_Len := 0;
2064 if Opt.Shared_Libgnat then
2065 Add_Str_To_Name_Buffer ("-shared");
2066 else
2067 Add_Str_To_Name_Buffer ("-static");
2068 end if;
2070 -- Write directly to avoid inclusion in -K output as -static and
2071 -- -shared are not usually specified linker options.
2073 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2074 end if;
2076 -- Sort linker options
2078 -- This sort accomplishes two important purposes:
2080 -- a) All application files are sorted to the front, and all GNAT
2081 -- internal files are sorted to the end. This results in a well
2082 -- defined dividing line between the two sets of files, for the
2083 -- purpose of inserting certain standard library references into
2084 -- the linker arguments list.
2086 -- b) Given two different units, we sort the linker options so that
2087 -- those from a unit earlier in the elaboration order comes later
2088 -- in the list. This is a heuristic designed to create a more
2089 -- friendly order of linker options when the operations appear in
2090 -- separate units. The idea is that if unit A must be elaborated
2091 -- before unit B, then it is more likely that B references
2092 -- libraries included by A, than vice versa, so we want libraries
2093 -- included by A to come after libraries included by B.
2095 -- These two criteria are implemented by function Lt_Linker_Option. Note
2096 -- that a special case of b) is that specs are elaborated before bodies,
2097 -- so linker options from specs come after linker options for bodies,
2098 -- and again, the assumption is that libraries used by the body are more
2099 -- likely to reference libraries used by the spec, than vice versa.
2101 Sort
2102 (Linker_Options.Last,
2103 Move_Linker_Option'Access,
2104 Lt_Linker_Option'Access);
2106 -- Write user linker options, i.e. the set of linker options that come
2107 -- from all files other than GNAT internal files, Lgnat is left set to
2108 -- point to the first entry from a GNAT internal file, or past the end
2109 -- of the entries if there are no internal files.
2111 Lgnat := Linker_Options.Last + 1;
2113 for J in 1 .. Linker_Options.Last loop
2114 if not Linker_Options.Table (J).Internal_File then
2115 Get_Name_String (Linker_Options.Table (J).Name);
2116 Write_Linker_Option;
2117 else
2118 Lgnat := J;
2119 exit;
2120 end if;
2121 end loop;
2123 -- Now we insert standard linker options that must appear after the
2124 -- entries from user files, and before the entries from GNAT run-time
2125 -- files. The reason for this decision is that libraries referenced
2126 -- by internal routines may reference these standard library entries.
2128 -- Note that we do not insert anything when pragma No_Run_Time has
2129 -- been specified or when the standard libraries are not to be used,
2130 -- otherwise on some platforms, we may get duplicate symbols when
2131 -- linking (not clear if this is still the case, but it is harmless).
2133 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2134 if With_GNARL then
2135 Name_Len := 0;
2137 if Opt.Shared_Libgnat then
2138 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2139 else
2140 Add_Str_To_Name_Buffer ("-lgnarl");
2141 end if;
2143 Write_Linker_Option;
2144 end if;
2146 Name_Len := 0;
2148 if Opt.Shared_Libgnat then
2149 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2150 else
2151 Add_Str_To_Name_Buffer ("-lgnat");
2152 end if;
2154 Write_Linker_Option;
2155 end if;
2157 -- Write linker options from all internal files
2159 for J in Lgnat .. Linker_Options.Last loop
2160 Get_Name_String (Linker_Options.Table (J).Name);
2161 Write_Linker_Option;
2162 end loop;
2164 if Output_Linker_Option_List and then not Zero_Formatting then
2165 Write_Eol;
2166 end if;
2168 WBI ("-- END Object file/option list ");
2169 end Gen_Object_Files_Options;
2171 ---------------------
2172 -- Gen_Output_File --
2173 ---------------------
2175 procedure Gen_Output_File
2176 (Filename : String;
2177 Elab_Order : Unit_Id_Array)
2179 begin
2180 -- Acquire settings for Interrupt_State pragmas
2182 Set_IS_Pragma_Table;
2184 -- Acquire settings for Priority_Specific_Dispatching pragma
2186 Set_PSD_Pragma_Table;
2188 -- Override time slice value if -T switch is set
2190 if Time_Slice_Set then
2191 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2192 end if;
2194 -- Count number of elaboration calls
2196 for E in Elab_Order'Range loop
2197 if Units.Table (Elab_Order (E)).No_Elab then
2198 null;
2199 else
2200 Num_Elab_Calls := Num_Elab_Calls + 1;
2201 end if;
2202 end loop;
2204 -- Count the number of statically allocated stacks to be generated by
2205 -- the binder. If the user has specified the number of default-sized
2206 -- secondary stacks, use that number. Otherwise start the count at one
2207 -- as the binder is responsible for creating a secondary stack for the
2208 -- main task.
2210 if Opt.Quantity_Of_Default_Size_Sec_Stacks /= -1 then
2211 Num_Sec_Stacks := Quantity_Of_Default_Size_Sec_Stacks;
2212 elsif Sec_Stack_Used then
2213 Num_Sec_Stacks := 1;
2214 end if;
2216 for J in Units.First .. Units.Last loop
2217 Num_Primary_Stacks :=
2218 Num_Primary_Stacks + Units.Table (J).Primary_Stack_Count;
2220 Num_Sec_Stacks :=
2221 Num_Sec_Stacks + Units.Table (J).Sec_Stack_Count;
2222 end loop;
2224 -- Generate output file in appropriate language
2226 Gen_Output_File_Ada (Filename, Elab_Order);
2227 end Gen_Output_File;
2229 -------------------------
2230 -- Gen_Output_File_Ada --
2231 -------------------------
2233 procedure Gen_Output_File_Ada
2234 (Filename : String; Elab_Order : Unit_Id_Array)
2236 Ada_Main : constant String := Get_Ada_Main_Name;
2237 -- Name to be used for generated Ada main program. See the body of
2238 -- function Get_Ada_Main_Name for details on the form of the name.
2240 Needs_Library_Finalization : constant Boolean :=
2241 not Configurable_Run_Time_On_Target
2242 and then Has_Finalizer (Elab_Order);
2243 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2244 -- non-terminating, so we do not want finalization.
2246 Bfiles : Name_Id;
2247 -- Name of generated bind file (spec)
2249 Bfileb : Name_Id;
2250 -- Name of generated bind file (body)
2252 begin
2253 -- Create spec first
2255 Create_Binder_Output (Filename, 's', Bfiles);
2257 -- We always compile the binder file in Ada 95 mode so that we properly
2258 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2259 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2261 WBI ("pragma Warnings (Off);");
2262 WBI ("pragma Ada_95;");
2264 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2265 -- then we need to make sure that the binder program is compiled with
2266 -- the same restriction, so that no exception tables are generated.
2268 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2269 WBI ("pragma Restrictions (No_Exception_Handlers);");
2270 end if;
2272 -- Same processing for Restrictions (No_Exception_Propagation)
2274 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2275 WBI ("pragma Restrictions (No_Exception_Propagation);");
2276 end if;
2278 -- Same processing for pragma No_Run_Time
2280 if No_Run_Time_Mode then
2281 WBI ("pragma No_Run_Time;");
2282 end if;
2284 -- Generate with of System so we can reference System.Address
2286 WBI ("with System;");
2288 -- Generate with of System.Initialize_Scalars if active
2290 if Initialize_Scalars_Used then
2291 WBI ("with System.Scalar_Values;");
2292 end if;
2294 -- Generate withs of System.Secondary_Stack and System.Parameters to
2295 -- allow the generation of the default-sized secondary stack pool.
2297 if Sec_Stack_Used then
2298 WBI ("with System.Parameters;");
2299 WBI ("with System.Secondary_Stack;");
2300 end if;
2302 Resolve_Binder_Options (Elab_Order);
2304 -- Generate standard with's
2306 if not Suppress_Standard_Library_On_Target then
2307 if CodePeer_Mode then
2308 WBI ("with System.Standard_Library;");
2309 end if;
2310 end if;
2312 WBI ("package " & Ada_Main & " is");
2314 -- Main program case
2316 if Bind_Main_Program then
2317 -- Generate argc/argv stuff unless suppressed
2319 if Command_Line_Args_On_Target
2320 or not Configurable_Run_Time_On_Target
2321 then
2322 WBI ("");
2323 WBI (" gnat_argc : Integer;");
2324 WBI (" gnat_argv : System.Address;");
2325 WBI (" gnat_envp : System.Address;");
2327 -- If the standard library is not suppressed, these variables
2328 -- are in the run-time data area for easy run time access.
2330 if not Suppress_Standard_Library_On_Target then
2331 WBI ("");
2332 WBI (" pragma Import (C, gnat_argc);");
2333 WBI (" pragma Import (C, gnat_argv);");
2334 WBI (" pragma Import (C, gnat_envp);");
2335 end if;
2336 end if;
2338 -- Define exit status. Again in normal mode, this is in the run-time
2339 -- library, and is initialized there, but in the configurable
2340 -- run-time case, the variable is declared and initialized in this
2341 -- file.
2343 WBI ("");
2345 if Configurable_Run_Time_Mode then
2346 if Exit_Status_Supported_On_Target then
2347 WBI (" gnat_exit_status : Integer := 0;");
2348 end if;
2350 else
2351 WBI (" gnat_exit_status : Integer;");
2352 WBI (" pragma Import (C, gnat_exit_status);");
2353 end if;
2355 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2356 -- the main program. Otherwise, it can lead under some circumstances
2357 -- to a symbol duplication during the link (for instance when a C
2358 -- program uses two Ada libraries). Also zero terminate the string
2359 -- so that its end can be found reliably at run time.
2361 WBI ("");
2362 WBI (" GNAT_Version : constant String :=");
2363 WBI (" """ & Ver_Prefix &
2364 Gnat_Version_String &
2365 """ & ASCII.NUL;");
2366 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2368 WBI ("");
2369 Set_String (" Ada_Main_Program_Name : constant String := """);
2370 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2372 Set_Main_Program_Name;
2373 Set_String (""" & ASCII.NUL;");
2375 Write_Statement_Buffer;
2378 (" pragma Export (C, Ada_Main_Program_Name, " &
2379 """__gnat_ada_main_program_name"");");
2380 end if;
2382 WBI ("");
2383 WBI (" procedure " & Ada_Init_Name.all & ";");
2384 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2385 Ada_Init_Name.all & """);");
2387 -- If -a has been specified use pragma Linker_Constructor for the init
2388 -- procedure and pragma Linker_Destructor for the final procedure.
2390 if Use_Pragma_Linker_Constructor then
2391 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2392 end if;
2394 if not Cumulative_Restrictions.Set (No_Finalization) then
2395 WBI ("");
2396 WBI (" procedure " & Ada_Final_Name.all & ";");
2397 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2398 Ada_Final_Name.all & """);");
2400 if Use_Pragma_Linker_Constructor then
2401 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2402 end if;
2403 end if;
2405 if Bind_Main_Program then
2407 WBI ("");
2409 if Exit_Status_Supported_On_Target then
2410 Set_String (" function ");
2411 else
2412 Set_String (" procedure ");
2413 end if;
2415 Set_String (Get_Main_Name);
2417 -- Generate argument list if present
2419 if Command_Line_Args_On_Target then
2420 Write_Statement_Buffer;
2421 WBI (" (argc : Integer;");
2422 WBI (" argv : System.Address;");
2423 Set_String
2424 (" envp : System.Address)");
2426 if Exit_Status_Supported_On_Target then
2427 Write_Statement_Buffer;
2428 WBI (" return Integer;");
2429 else
2430 Write_Statement_Buffer (";");
2431 end if;
2433 else
2434 if Exit_Status_Supported_On_Target then
2435 Write_Statement_Buffer (" return Integer;");
2436 else
2437 Write_Statement_Buffer (";");
2438 end if;
2439 end if;
2441 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2442 Get_Main_Name & """);");
2443 end if;
2445 -- Generate version numbers for units, only if needed. Be very safe on
2446 -- the condition.
2448 if not Configurable_Run_Time_On_Target
2449 or else System_Version_Control_Used
2450 or else not Bind_Main_Program
2451 then
2452 Gen_Versions;
2453 end if;
2455 Gen_Elab_Order (Elab_Order);
2457 -- Spec is complete
2459 WBI ("");
2460 WBI ("end " & Ada_Main & ";");
2461 Close_Binder_Output;
2463 -- Prepare to write body
2465 Create_Binder_Output (Filename, 'b', Bfileb);
2467 -- We always compile the binder file in Ada 95 mode so that we properly
2468 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2469 -- of the Ada 2005/2012 constructs are needed by the binder file.
2471 WBI ("pragma Warnings (Off);");
2472 WBI ("pragma Ada_95;");
2474 -- Output Source_File_Name pragmas which look like
2476 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2477 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2479 -- where sss/bbb are the spec/body file names respectively
2481 Get_Name_String (Bfiles);
2482 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2484 WBI ("pragma Source_File_Name (" &
2485 Ada_Main &
2486 ", Spec_File_Name => """ &
2487 Name_Buffer (1 .. Name_Len + 3));
2489 Get_Name_String (Bfileb);
2490 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2492 WBI ("pragma Source_File_Name (" &
2493 Ada_Main &
2494 ", Body_File_Name => """ &
2495 Name_Buffer (1 .. Name_Len + 3));
2497 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2498 -- versions of the compiler which have overflow checks on by default.
2499 -- We do not want overflow checking enabled for the increments of the
2500 -- elaboration variables (since this can cause an unwanted reference to
2501 -- the last chance exception handler for limited run-times).
2503 WBI ("pragma Suppress (Overflow_Check);");
2505 -- Generate with of System.Restrictions to initialize
2506 -- Run_Time_Restrictions.
2508 if System_Restrictions_Used
2509 and not Suppress_Standard_Library_On_Target
2510 then
2511 WBI ("");
2512 WBI ("with System.Restrictions;");
2513 end if;
2515 -- Generate with of Ada.Exceptions if needs library finalization
2517 if Needs_Library_Finalization then
2518 WBI ("with Ada.Exceptions;");
2519 end if;
2521 -- Generate with of System.Elaboration_Allocators if the restriction
2522 -- No_Standard_Allocators_After_Elaboration was present.
2524 if Cumulative_Restrictions.Set
2525 (No_Standard_Allocators_After_Elaboration)
2526 then
2527 WBI ("with System.Elaboration_Allocators;");
2528 end if;
2530 -- Generate start of package body
2532 WBI ("");
2533 WBI ("package body " & Ada_Main & " is");
2534 WBI ("");
2536 -- Generate externals for elaboration entities
2538 Gen_Elab_Externals (Elab_Order);
2540 -- Generate default-sized secondary stacks pool. At least one stack is
2541 -- created and assigned to the environment task if secondary stacks are
2542 -- used by the program.
2544 if Sec_Stack_Used then
2545 Set_String (" Sec_Default_Sized_Stacks");
2546 Set_String (" : array (1 .. ");
2547 Set_Int (Num_Sec_Stacks);
2548 Set_String (") of aliased System.Secondary_Stack.SS_Stack (");
2550 if Opt.Default_Sec_Stack_Size /= No_Stack_Size then
2551 Set_Int (Opt.Default_Sec_Stack_Size);
2552 else
2553 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
2554 end if;
2556 Set_String (");");
2557 Write_Statement_Buffer;
2558 WBI ("");
2559 end if;
2561 -- Generate reference
2563 if not CodePeer_Mode then
2564 if not Suppress_Standard_Library_On_Target then
2566 -- Generate Priority_Specific_Dispatching pragma string
2568 Set_String
2569 (" Local_Priority_Specific_Dispatching : " &
2570 "constant String := """);
2572 for J in 0 .. PSD_Pragma_Settings.Last loop
2573 Set_Char (PSD_Pragma_Settings.Table (J));
2574 end loop;
2576 Set_String (""";");
2577 Write_Statement_Buffer;
2579 -- Generate Interrupt_State pragma string
2581 Set_String (" Local_Interrupt_States : constant String := """);
2583 for J in 0 .. IS_Pragma_Settings.Last loop
2584 Set_Char (IS_Pragma_Settings.Table (J));
2585 end loop;
2587 Set_String (""";");
2588 Write_Statement_Buffer;
2589 WBI ("");
2590 end if;
2592 if not Suppress_Standard_Library_On_Target then
2594 -- The B.1(39) implementation advice says that the adainit and
2595 -- adafinal routines should be idempotent. Generate a flag to
2596 -- ensure that. This is not needed if we are suppressing the
2597 -- standard library since it would never be referenced.
2599 WBI (" Is_Elaborated : Boolean := False;");
2601 -- Generate bind environment string
2603 Gen_Bind_Env_String;
2604 end if;
2606 WBI ("");
2607 end if;
2609 -- Generate the adafinal routine unless there is no finalization to do
2611 if not Cumulative_Restrictions.Set (No_Finalization) then
2612 if Needs_Library_Finalization then
2613 Gen_Finalize_Library (Elab_Order);
2614 end if;
2616 Gen_Adafinal;
2617 end if;
2619 Gen_Adainit (Elab_Order);
2621 if Bind_Main_Program then
2622 Gen_Main;
2623 end if;
2625 -- Output object file list and the Ada body is complete
2627 Gen_Object_Files_Options (Elab_Order);
2629 WBI ("");
2630 WBI ("end " & Ada_Main & ";");
2632 Close_Binder_Output;
2633 end Gen_Output_File_Ada;
2635 ----------------------
2636 -- Gen_Restrictions --
2637 ----------------------
2639 procedure Gen_Restrictions is
2640 Count : Integer;
2642 begin
2643 if Suppress_Standard_Library_On_Target
2644 or not System_Restrictions_Used
2645 then
2646 return;
2647 end if;
2649 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2650 WBI (" (Set =>");
2651 Set_String (" (");
2653 Count := 0;
2655 for J in Cumulative_Restrictions.Set'Range loop
2656 Set_Boolean (Cumulative_Restrictions.Set (J));
2657 Set_String (", ");
2658 Count := Count + 1;
2660 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2661 Write_Statement_Buffer;
2662 Set_String (" ");
2663 Count := 0;
2664 end if;
2665 end loop;
2667 Set_String_Replace ("),");
2668 Write_Statement_Buffer;
2669 Set_String (" Value => (");
2671 for J in Cumulative_Restrictions.Value'Range loop
2672 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2673 Set_String (", ");
2674 end loop;
2676 Set_String_Replace ("),");
2677 Write_Statement_Buffer;
2678 WBI (" Violated =>");
2679 Set_String (" (");
2680 Count := 0;
2682 for J in Cumulative_Restrictions.Violated'Range loop
2683 Set_Boolean (Cumulative_Restrictions.Violated (J));
2684 Set_String (", ");
2685 Count := Count + 1;
2687 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2688 Write_Statement_Buffer;
2689 Set_String (" ");
2690 Count := 0;
2691 end if;
2692 end loop;
2694 Set_String_Replace ("),");
2695 Write_Statement_Buffer;
2696 Set_String (" Count => (");
2698 for J in Cumulative_Restrictions.Count'Range loop
2699 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2700 Set_String (", ");
2701 end loop;
2703 Set_String_Replace ("),");
2704 Write_Statement_Buffer;
2705 Set_String (" Unknown => (");
2707 for J in Cumulative_Restrictions.Unknown'Range loop
2708 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2709 Set_String (", ");
2710 end loop;
2712 Set_String_Replace ("))");
2713 Set_String (";");
2714 Write_Statement_Buffer;
2715 end Gen_Restrictions;
2717 ------------------
2718 -- Gen_Versions --
2719 ------------------
2721 -- This routine generates lines such as:
2723 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2724 -- pragma Export (C, unnnnn, unam);
2726 -- for each unit, where unam is the unit name suffixed by either B or S for
2727 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2728 -- the version number, and nnnnn is a 5-digits serial number.
2730 procedure Gen_Versions is
2731 Ubuf : String (1 .. 6) := "u00000";
2733 procedure Increment_Ubuf;
2734 -- Little procedure to increment the serial number
2736 --------------------
2737 -- Increment_Ubuf --
2738 --------------------
2740 procedure Increment_Ubuf is
2741 begin
2742 for J in reverse Ubuf'Range loop
2743 Ubuf (J) := Character'Succ (Ubuf (J));
2744 exit when Ubuf (J) <= '9';
2745 Ubuf (J) := '0';
2746 end loop;
2747 end Increment_Ubuf;
2749 -- Start of processing for Gen_Versions
2751 begin
2752 WBI ("");
2754 WBI (" type Version_32 is mod 2 ** 32;");
2755 for U in Units.First .. Units.Last loop
2756 if not Units.Table (U).SAL_Interface
2757 and then (not Bind_For_Library
2758 or else Units.Table (U).Directly_Scanned)
2759 then
2760 Increment_Ubuf;
2761 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2762 Units.Table (U).Version & "#;");
2763 Set_String (" pragma Export (C, ");
2764 Set_String (Ubuf);
2765 Set_String (", """);
2767 Get_Name_String (Units.Table (U).Uname);
2769 for K in 1 .. Name_Len loop
2770 if Name_Buffer (K) = '.' then
2771 Set_Char ('_');
2772 Set_Char ('_');
2774 elsif Name_Buffer (K) = '%' then
2775 exit;
2777 else
2778 Set_Char (Name_Buffer (K));
2779 end if;
2780 end loop;
2782 if Name_Buffer (Name_Len) = 's' then
2783 Set_Char ('S');
2784 else
2785 Set_Char ('B');
2786 end if;
2788 Set_String (""");");
2789 Write_Statement_Buffer;
2790 end if;
2791 end loop;
2792 end Gen_Versions;
2794 ------------------------
2795 -- Get_Main_Unit_Name --
2796 ------------------------
2798 function Get_Main_Unit_Name (S : String) return String is
2799 Result : String := S;
2801 begin
2802 for J in S'Range loop
2803 if Result (J) = '.' then
2804 Result (J) := '_';
2805 end if;
2806 end loop;
2808 return Result;
2809 end Get_Main_Unit_Name;
2811 -----------------------
2812 -- Get_Ada_Main_Name --
2813 -----------------------
2815 function Get_Ada_Main_Name return String is
2816 Suffix : constant String := "_00";
2817 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2818 Opt.Ada_Main_Name.all & Suffix;
2819 Nlen : Natural;
2821 begin
2822 -- For CodePeer, we want reproducible names (independent of other mains
2823 -- that may or may not be present) that don't collide when analyzing
2824 -- multiple mains and which are easily recognizable as "ada_main" names.
2826 if CodePeer_Mode then
2827 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2829 return
2830 "ada_main_for_" &
2831 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2832 end if;
2834 -- This loop tries the following possibilities in order
2835 -- <Ada_Main>
2836 -- <Ada_Main>_01
2837 -- <Ada_Main>_02
2838 -- ..
2839 -- <Ada_Main>_99
2840 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2841 -- it is set to 'ada_main'.
2843 for J in 0 .. 99 loop
2844 if J = 0 then
2845 Nlen := Name'Length - Suffix'Length;
2846 else
2847 Nlen := Name'Length;
2848 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2849 Name (Name'Last - 1) :=
2850 Character'Val (J / 10 + Character'Pos ('0'));
2851 end if;
2853 for K in ALIs.First .. ALIs.Last loop
2854 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2856 -- Get unit name, removing %b or %e at end
2858 Get_Name_String (Units.Table (L).Uname);
2859 Name_Len := Name_Len - 2;
2861 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2862 goto Continue;
2863 end if;
2864 end loop;
2865 end loop;
2867 return Name (1 .. Nlen);
2869 <<Continue>>
2870 null;
2871 end loop;
2873 -- If we fall through, just use a peculiar unlikely name
2875 return ("Qwertyuiop");
2876 end Get_Ada_Main_Name;
2878 -------------------
2879 -- Get_Main_Name --
2880 -------------------
2882 function Get_Main_Name return String is
2883 begin
2884 -- Explicit name given with -M switch
2886 if Bind_Alternate_Main_Name then
2887 return Alternate_Main_Name.all;
2889 -- Case of main program name to be used directly
2891 elsif Use_Ada_Main_Program_Name_On_Target then
2893 -- Get main program name
2895 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2897 -- If this is a child name, return only the name of the child, since
2898 -- we can't have dots in a nested program name. Note that we do not
2899 -- include the %b at the end of the unit name.
2901 for J in reverse 1 .. Name_Len - 2 loop
2902 if J = 1 or else Name_Buffer (J - 1) = '.' then
2903 return Name_Buffer (J .. Name_Len - 2);
2904 end if;
2905 end loop;
2907 raise Program_Error; -- impossible exit
2909 -- Case where "main" is to be used as default
2911 else
2912 return "main";
2913 end if;
2914 end Get_Main_Name;
2916 ---------------------
2917 -- Get_WC_Encoding --
2918 ---------------------
2920 function Get_WC_Encoding return Character is
2921 begin
2922 -- If encoding method specified by -W switch, then return it
2924 if Wide_Character_Encoding_Method_Specified then
2925 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2927 -- If no main program, and not specified, set brackets, we really have
2928 -- no better choice. If some other encoding is required when there is
2929 -- no main, it must be set explicitly using -Wx.
2931 -- Note: if the ALI file always passed the wide character encoding of
2932 -- every file, then we could use the encoding of the initial specified
2933 -- file, but this information is passed only for potential main
2934 -- programs. We could fix this sometime, but it is a very minor point
2935 -- (wide character default encoding for [Wide_[Wide_]]Text_IO when there
2936 -- is no main program).
2938 elsif No_Main_Subprogram then
2939 return 'b';
2941 -- Otherwise if there is a main program, take encoding from it
2943 else
2944 return ALIs.Table (ALIs.First).WC_Encoding;
2945 end if;
2946 end Get_WC_Encoding;
2948 -------------------
2949 -- Has_Finalizer --
2950 -------------------
2952 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean is
2953 U : Unit_Record;
2954 Unum : Unit_Id;
2956 begin
2957 for E in reverse Elab_Order'Range loop
2958 Unum := Elab_Order (E);
2959 U := Units.Table (Unum);
2961 -- We are only interested in non-generic packages
2963 if U.Unit_Kind = 'p'
2964 and then U.Has_Finalizer
2965 and then not U.Is_Generic
2966 and then not U.No_Elab
2967 then
2968 return True;
2969 end if;
2970 end loop;
2972 return False;
2973 end Has_Finalizer;
2975 ----------
2976 -- Hash --
2977 ----------
2979 function Hash (Nam : Name_Id) return Header_Num is
2980 begin
2981 return Int (Nam - Names_Low_Bound) rem Header_Num'Last;
2982 end Hash;
2984 ----------------------
2985 -- Lt_Linker_Option --
2986 ----------------------
2988 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean is
2989 begin
2990 -- Sort internal files last
2992 if Linker_Options.Table (Op1).Internal_File
2994 Linker_Options.Table (Op2).Internal_File
2995 then
2996 -- Note: following test uses False < True
2998 return Linker_Options.Table (Op1).Internal_File
3000 Linker_Options.Table (Op2).Internal_File;
3002 -- If both internal or both non-internal, sort according to the
3003 -- elaboration position. A unit that is elaborated later should come
3004 -- earlier in the linker options list.
3006 else
3007 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3009 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3010 end if;
3011 end Lt_Linker_Option;
3013 ------------------------
3014 -- Move_Linker_Option --
3015 ------------------------
3017 procedure Move_Linker_Option (From : Natural; To : Natural) is
3018 begin
3019 Linker_Options.Table (To) := Linker_Options.Table (From);
3020 end Move_Linker_Option;
3022 ----------------------------
3023 -- Resolve_Binder_Options --
3024 ----------------------------
3026 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array) is
3027 procedure Check_Package (Var : in out Boolean; Name : String);
3028 -- Set Var to true iff the current identifier in Namet is Name. Do
3029 -- nothing if it doesn't match. This procedure is just a helper to
3030 -- avoid explicitly dealing with length.
3032 -------------------
3033 -- Check_Package --
3034 -------------------
3036 procedure Check_Package (Var : in out Boolean; Name : String) is
3037 begin
3038 if Name_Len = Name'Length
3039 and then Name_Buffer (1 .. Name_Len) = Name
3040 then
3041 Var := True;
3042 end if;
3043 end Check_Package;
3045 -- Start of processing for Resolve_Binder_Options
3047 begin
3048 for E in Elab_Order'Range loop
3049 Get_Name_String (Units.Table (Elab_Order (E)).Uname);
3051 -- This is not a perfect approach, but is the current protocol
3052 -- between the run-time and the binder to indicate that tasking is
3053 -- used: System.OS_Interface should always be used by any tasking
3054 -- application.
3056 Check_Package (With_GNARL, "system.os_interface%s");
3058 -- Ditto for the use of restricted tasking
3060 Check_Package
3061 (System_Tasking_Restricted_Stages_Used,
3062 "system.tasking.restricted.stages%s");
3064 -- Ditto for the use of interrupts
3066 Check_Package (System_Interrupts_Used, "system.interrupts%s");
3068 -- Ditto for the use of dispatching domains
3070 Check_Package
3071 (Dispatching_Domains_Used,
3072 "system.multiprocessors.dispatching_domains%s");
3074 -- Ditto for the use of restrictions
3076 Check_Package (System_Restrictions_Used, "system.restrictions%s");
3078 -- Ditto for the use of System.Secondary_Stack
3080 Check_Package
3081 (System_Secondary_Stack_Used, "system.secondary_stack%s");
3083 -- Ditto for use of an SMP bareboard runtime
3085 Check_Package (System_BB_CPU_Primitives_Multiprocessors_Used,
3086 "system.bb.cpu_primitives.multiprocessors%s");
3088 -- Ditto for System.Version_Control, which is used for Version and
3089 -- Body_Version attributes.
3091 Check_Package (System_Version_Control_Used,
3092 "system.version_control%s");
3093 end loop;
3094 end Resolve_Binder_Options;
3096 ------------------
3097 -- Set_Bind_Env --
3098 ------------------
3100 procedure Set_Bind_Env (Key, Value : String) is
3101 begin
3102 -- The lengths of Key and Value are stored as single bytes
3104 if Key'Length > 255 then
3105 Osint.Fail ("bind environment key """ & Key & """ too long");
3106 end if;
3108 if Value'Length > 255 then
3109 Osint.Fail ("bind environment value """ & Value & """ too long");
3110 end if;
3112 Bind_Environment.Set (Name_Find (Key), Name_Find (Value));
3113 end Set_Bind_Env;
3115 -----------------
3116 -- Set_Boolean --
3117 -----------------
3119 procedure Set_Boolean (B : Boolean) is
3120 False_Str : constant String := "False";
3121 True_Str : constant String := "True";
3123 begin
3124 if B then
3125 Statement_Buffer (Stm_Last + 1 .. Stm_Last + True_Str'Length) :=
3126 True_Str;
3127 Stm_Last := Stm_Last + True_Str'Length;
3128 else
3129 Statement_Buffer (Stm_Last + 1 .. Stm_Last + False_Str'Length) :=
3130 False_Str;
3131 Stm_Last := Stm_Last + False_Str'Length;
3132 end if;
3133 end Set_Boolean;
3135 --------------
3136 -- Set_Char --
3137 --------------
3139 procedure Set_Char (C : Character) is
3140 begin
3141 Stm_Last := Stm_Last + 1;
3142 Statement_Buffer (Stm_Last) := C;
3143 end Set_Char;
3145 -------------
3146 -- Set_Int --
3147 -------------
3149 procedure Set_Int (N : Int) is
3150 begin
3151 if N < 0 then
3152 Set_String ("-");
3153 Set_Int (-N);
3155 else
3156 if N > 9 then
3157 Set_Int (N / 10);
3158 end if;
3160 Stm_Last := Stm_Last + 1;
3161 Statement_Buffer (Stm_Last) :=
3162 Character'Val (N mod 10 + Character'Pos ('0'));
3163 end if;
3164 end Set_Int;
3166 -------------------------
3167 -- Set_IS_Pragma_Table --
3168 -------------------------
3170 procedure Set_IS_Pragma_Table is
3171 begin
3172 for F in ALIs.First .. ALIs.Last loop
3173 for K in ALIs.Table (F).First_Interrupt_State ..
3174 ALIs.Table (F).Last_Interrupt_State
3175 loop
3176 declare
3177 Inum : constant Int :=
3178 Interrupt_States.Table (K).Interrupt_Id;
3179 Stat : constant Character :=
3180 Interrupt_States.Table (K).Interrupt_State;
3182 begin
3183 while IS_Pragma_Settings.Last < Inum loop
3184 IS_Pragma_Settings.Append ('n');
3185 end loop;
3187 IS_Pragma_Settings.Table (Inum) := Stat;
3188 end;
3189 end loop;
3190 end loop;
3191 end Set_IS_Pragma_Table;
3193 ---------------------------
3194 -- Set_Main_Program_Name --
3195 ---------------------------
3197 procedure Set_Main_Program_Name is
3198 begin
3199 -- Note that name has %b on the end which we ignore
3201 -- First we output the initial _ada_ since we know that the main program
3202 -- is a library level subprogram.
3204 Set_String ("_ada_");
3206 -- Copy name, changing dots to double underscores
3208 for J in 1 .. Name_Len - 2 loop
3209 if Name_Buffer (J) = '.' then
3210 Set_String ("__");
3211 else
3212 Set_Char (Name_Buffer (J));
3213 end if;
3214 end loop;
3215 end Set_Main_Program_Name;
3217 ---------------------
3218 -- Set_Name_Buffer --
3219 ---------------------
3221 procedure Set_Name_Buffer is
3222 begin
3223 for J in 1 .. Name_Len loop
3224 Set_Char (Name_Buffer (J));
3225 end loop;
3226 end Set_Name_Buffer;
3228 -------------------------
3229 -- Set_PSD_Pragma_Table --
3230 -------------------------
3232 procedure Set_PSD_Pragma_Table is
3233 begin
3234 for F in ALIs.First .. ALIs.Last loop
3235 for K in ALIs.Table (F).First_Specific_Dispatching ..
3236 ALIs.Table (F).Last_Specific_Dispatching
3237 loop
3238 declare
3239 DTK : Specific_Dispatching_Record
3240 renames Specific_Dispatching.Table (K);
3242 begin
3243 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3244 PSD_Pragma_Settings.Append ('F');
3245 end loop;
3247 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3248 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3249 end loop;
3250 end;
3251 end loop;
3252 end loop;
3253 end Set_PSD_Pragma_Table;
3255 ----------------
3256 -- Set_String --
3257 ----------------
3259 procedure Set_String (S : String) is
3260 begin
3261 Statement_Buffer (Stm_Last + 1 .. Stm_Last + S'Length) := S;
3262 Stm_Last := Stm_Last + S'Length;
3263 end Set_String;
3265 ------------------------
3266 -- Set_String_Replace --
3267 ------------------------
3269 procedure Set_String_Replace (S : String) is
3270 begin
3271 Statement_Buffer (Stm_Last - S'Length + 1 .. Stm_Last) := S;
3272 end Set_String_Replace;
3274 -------------------
3275 -- Set_Unit_Name --
3276 -------------------
3278 procedure Set_Unit_Name is
3279 begin
3280 for J in 1 .. Name_Len - 2 loop
3281 if Name_Buffer (J) = '.' then
3282 Set_String ("__");
3283 else
3284 Set_Char (Name_Buffer (J));
3285 end if;
3286 end loop;
3287 end Set_Unit_Name;
3289 ---------------------
3290 -- Set_Unit_Number --
3291 ---------------------
3293 procedure Set_Unit_Number (U : Unit_Id) is
3294 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3295 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3297 begin
3298 if Num_Units >= 10 and then Unum < 10 then
3299 Set_Char ('0');
3300 end if;
3302 if Num_Units >= 100 and then Unum < 100 then
3303 Set_Char ('0');
3304 end if;
3306 Set_Int (Unum);
3307 end Set_Unit_Number;
3309 ---------------------
3310 -- Write_Bind_Line --
3311 ---------------------
3313 procedure Write_Bind_Line (S : String) is
3314 begin
3315 -- Need to strip trailing LF from S
3317 WBI (S (S'First .. S'Last - 1));
3318 end Write_Bind_Line;
3320 ----------------------------
3321 -- Write_Statement_Buffer --
3322 ----------------------------
3324 procedure Write_Statement_Buffer is
3325 begin
3326 WBI (Statement_Buffer (1 .. Stm_Last));
3327 Stm_Last := 0;
3328 end Write_Statement_Buffer;
3330 procedure Write_Statement_Buffer (S : String) is
3331 begin
3332 Set_String (S);
3333 Write_Statement_Buffer;
3334 end Write_Statement_Buffer;
3336 end Bindgen;