[Ada] Add a gnatbind option to generate C code
[gcc.git] / gcc / ada / bindgen.adb
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-2019, 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 ------------------------------------------------------------------------------
25
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;
41
42 with System.OS_Lib;
43 with System.WCh_Con; use System.WCh_Con;
44
45 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
46 with GNAT.HTable;
47
48 package body Bindgen is
49 Statement_Buffer : String (1 .. 1000);
50 -- Buffer used for constructing output statements
51
52 Stm_Last : Natural := 0;
53 -- Stm_Last location in Statement_Buffer currently set
54
55 With_GNARL : Boolean := False;
56 -- Flag which indicates whether the program uses the GNARL library
57 -- (presence of the unit System.OS_Interface)
58
59 Num_Elab_Calls : Nat := 0;
60 -- Number of generated calls to elaboration routines
61
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.
65
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.
69
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.)
77
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.
84
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.
90
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.
96
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.
102
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.
108
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.
114
115 Lib_Final_Built : Boolean := False;
116 -- Flag indicating whether the finalize_library rountine has been built
117
118 Bind_Env_String_Built : Boolean := False;
119 -- Flag indicating whether a bind environment string has been built
120
121 CodePeer_Wrapper_Name : constant String := "call_main_subprogram";
122 -- For CodePeer, introduce a wrapper subprogram which calls the
123 -- user-defined main subprogram.
124
125 ----------------------------------
126 -- Interface_State Pragma Table --
127 ----------------------------------
128
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.
133
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");
141
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.
147
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");
155
156 ----------------------------
157 -- Bind_Environment Table --
158 ----------------------------
159
160 subtype Header_Num is Int range 0 .. 36;
161
162 function Hash (Nam : Name_Id) return Header_Num;
163
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 => "=");
171
172 ----------------------
173 -- Run-Time Globals --
174 ----------------------
175
176 -- This section documents the global variables that are set from the
177 -- generated binder file.
178
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;
201
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.
204
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.
210
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.
214
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.
219
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.
224
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.
229
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.
239
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.
243
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.
249
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.
259
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.
262
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.
265
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.
270
271 -- Exception_Tracebacks_Symbolic is set to one if the -Es parameter was
272 -- present in the bind and to zero otherwise.
273
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.
277
278 -- Default_Stack_Size is the default stack size used when creating an Ada
279 -- task with no explicit Storage_Size clause.
280
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.
283
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.
287
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.
290
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.
295
296 -- Binder_Sec_Stacks_Count is the number of generated secondary stacks in
297 -- the Default_Sized_SS_Pool.
298
299 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
300 -- Convenient shorthand used throughout
301
302 -----------------------
303 -- Local Subprograms --
304 -----------------------
305
306 procedure Gen_Adainit (Elab_Order : Unit_Id_Array);
307 -- Generates the Adainit procedure
308
309 procedure Gen_Adafinal;
310 -- Generate the Adafinal procedure
311
312 procedure Gen_Bind_Env_String;
313 -- Generate the bind environment buffer
314
315 procedure Gen_CodePeer_Wrapper;
316 -- For CodePeer, generate wrapper which calls user-defined main subprogram
317
318 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array);
319 -- Generate sequence of elaboration calls
320
321 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array);
322 -- Generate sequence of external declarations for elaboration
323
324 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array);
325 -- Generate comments showing elaboration order chosen
326
327 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array);
328 -- Generate a sequence of finalization calls to elaborated packages
329
330 procedure Gen_Main;
331 -- Generate procedure main
332
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.
337
338 procedure Gen_Output_File_Ada
339 (Filename : String;
340 Elab_Order : Unit_Id_Array);
341 -- Generate Ada output file
342
343 procedure Gen_Restrictions;
344 -- Generate initialization of restrictions variable
345
346 procedure Gen_Versions;
347 -- Output series of definitions for unit versions
348
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.
355
356 function Get_Main_Unit_Name (S : String) return String;
357 -- Return the main unit name corresponding to S by replacing '.' with '_'
358
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.
365
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').
371
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.
375
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).
380
381 procedure Move_Linker_Option (From : Natural; To : Natural);
382 -- Move routine for sorting linker options
383
384 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array);
385 -- Set the value of With_GNARL
386
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.
390
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.
395
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.
399
400 procedure Set_IS_Pragma_Table;
401 -- Initializes contents of IS_Pragma_Settings table from ALI table
402
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.
407
408 procedure Set_Name_Buffer;
409 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
410
411 procedure Set_PSD_Pragma_Table;
412 -- Initializes contents of PSD_Pragma_Settings table from ALI table
413
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.
417
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.
422
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 (__).
427
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.
432
433 procedure Write_Statement_Buffer;
434 -- Write out contents of statement buffer up to Stm_Last, and reset
435 -- Stm_Last to 0.
436
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.
440
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).
444
445 ------------------
446 -- Gen_Adafinal --
447 ------------------
448
449 procedure Gen_Adafinal is
450 begin
451 WBI (" procedure " & Ada_Final_Name.all & " is");
452
453 -- Call s_stalib_adafinal to await termination of tasks and so on. We
454 -- want to do this if there is a main program, either in Ada or in some
455 -- other language. (Note that Bind_Main_Program is True for Ada mains,
456 -- but False for mains in other languages.) We do not want to do this if
457 -- we're binding a library.
458
459 if not Bind_For_Library and not CodePeer_Mode then
460 WBI (" procedure s_stalib_adafinal;");
461 Set_String (" pragma Import (C, s_stalib_adafinal, ");
462 Set_String ("""system__standard_library__adafinal"");");
463 Write_Statement_Buffer;
464 end if;
465
466 WBI ("");
467 WBI (" procedure Runtime_Finalize;");
468 WBI (" pragma Import (C, Runtime_Finalize, " &
469 """__gnat_runtime_finalize"");");
470 WBI ("");
471 WBI (" begin");
472
473 if not CodePeer_Mode then
474 WBI (" if not Is_Elaborated then");
475 WBI (" return;");
476 WBI (" end if;");
477 WBI (" Is_Elaborated := False;");
478 end if;
479
480 WBI (" Runtime_Finalize;");
481
482 -- By default (real targets), finalization is done differently depending
483 -- on whether this is the main program or a library.
484
485 if not CodePeer_Mode then
486 if not Bind_For_Library then
487 WBI (" s_stalib_adafinal;");
488 elsif Lib_Final_Built then
489 WBI (" finalize_library;");
490 else
491 WBI (" null;");
492 end if;
493
494 -- Pragma Import C cannot be used on virtual targets, therefore call the
495 -- runtime finalization routine directly in CodePeer mode, where
496 -- imported functions are ignored.
497
498 else
499 WBI (" System.Standard_Library.Adafinal;");
500 end if;
501
502 WBI (" end " & Ada_Final_Name.all & ";");
503 WBI ("");
504 end Gen_Adafinal;
505
506 -----------------
507 -- Gen_Adainit --
508 -----------------
509
510 procedure Gen_Adainit (Elab_Order : Unit_Id_Array) is
511 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
512 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
513
514 begin
515 -- Declare the access-to-subprogram type used for initialization of
516 -- of __gnat_finalize_library_objects. This is declared at library
517 -- level for compatibility with the type used in System.Soft_Links.
518 -- The import of the soft link which performs library-level object
519 -- finalization does not work for CodePeer, so regular Ada is used in
520 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
521 -- tasks are non-terminating, so we do not want finalization.
522
523 if not Suppress_Standard_Library_On_Target
524 and then not CodePeer_Mode
525 and then not Configurable_Run_Time_On_Target
526 then
527 WBI (" type No_Param_Proc is access procedure;");
528 WBI ("");
529 end if;
530
531 WBI (" procedure " & Ada_Init_Name.all & " is");
532
533 -- In CodePeer mode, simplify adainit procedure by only calling
534 -- elaboration procedures.
535
536 if CodePeer_Mode then
537 WBI (" begin");
538
539 -- If the standard library is suppressed, then the only global variables
540 -- that might be needed (by the Ravenscar profile) are the priority and
541 -- the processor for the environment task.
542
543 elsif Suppress_Standard_Library_On_Target then
544 if Main_Priority /= No_Main_Priority then
545 WBI (" Main_Priority : Integer;");
546 WBI (" pragma Import (C, Main_Priority," &
547 " ""__gl_main_priority"");");
548 WBI ("");
549 end if;
550
551 if Main_CPU /= No_Main_CPU then
552 WBI (" Main_CPU : Integer;");
553 WBI (" pragma Import (C, Main_CPU," &
554 " ""__gl_main_cpu"");");
555 WBI ("");
556 end if;
557
558 if System_Interrupts_Used
559 and then Partition_Elaboration_Policy_Specified = 'S'
560 then
561 WBI (" procedure Install_Restricted_Handlers_Sequential;");
562 WBI (" pragma Import (C," &
563 "Install_Restricted_Handlers_Sequential," &
564 " ""__gnat_attach_all_handlers"");");
565 WBI ("");
566 end if;
567
568 if System_Tasking_Restricted_Stages_Used
569 and then Partition_Elaboration_Policy_Specified = 'S'
570 then
571 WBI (" Partition_Elaboration_Policy : Character;");
572 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
573 " ""__gnat_partition_elaboration_policy"");");
574 WBI ("");
575 WBI (" procedure Activate_All_Tasks_Sequential;");
576 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
577 " ""__gnat_activate_all_tasks"");");
578 WBI ("");
579 end if;
580
581 if System_BB_CPU_Primitives_Multiprocessors_Used then
582 WBI (" procedure Start_Slave_CPUs;");
583 WBI (" pragma Import (C, Start_Slave_CPUs," &
584 " ""__gnat_start_slave_cpus"");");
585 WBI ("");
586 end if;
587
588 -- A restricted run-time may attempt to initialize the main task's
589 -- secondary stack even if the stack is not used. Consequently,
590 -- the binder needs to initialize Binder_Sec_Stacks_Count anytime
591 -- System.Secondary_Stack is in the enclosure of the partition.
592
593 if System_Secondary_Stack_Used then
594 WBI (" Binder_Sec_Stacks_Count : Natural;");
595 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
596 """__gnat_binder_ss_count"");");
597 WBI ("");
598 end if;
599
600 if Sec_Stack_Used then
601 WBI (" Default_Secondary_Stack_Size : " &
602 "System.Parameters.Size_Type;");
603 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
604 """__gnat_default_ss_size"");");
605
606 WBI (" Default_Sized_SS_Pool : System.Address;");
607 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
608 """__gnat_default_ss_pool"");");
609
610 WBI ("");
611 end if;
612
613 WBI (" begin");
614
615 if Main_Priority /= No_Main_Priority then
616 Set_String (" Main_Priority := ");
617 Set_Int (Main_Priority);
618 Set_Char (';');
619 Write_Statement_Buffer;
620 end if;
621
622 if Main_CPU /= No_Main_CPU then
623 Set_String (" Main_CPU := ");
624 Set_Int (Main_CPU);
625 Set_Char (';');
626 Write_Statement_Buffer;
627 end if;
628
629 if System_Tasking_Restricted_Stages_Used
630 and then Partition_Elaboration_Policy_Specified = 'S'
631 then
632 Set_String (" Partition_Elaboration_Policy := '");
633 Set_Char (Partition_Elaboration_Policy_Specified);
634 Set_String ("';");
635 Write_Statement_Buffer;
636 end if;
637
638 if Main_Priority = No_Main_Priority
639 and then Main_CPU = No_Main_CPU
640 and then not System_Tasking_Restricted_Stages_Used
641 then
642 WBI (" null;");
643 end if;
644
645 -- Generate default-sized secondary stack pool and set secondary
646 -- stack globals.
647
648 if Sec_Stack_Used then
649
650 -- Elaborate the body of the binder to initialize the default-
651 -- sized secondary stack pool.
652
653 WBI ("");
654 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
655
656 -- Generate the default-sized secondary stack pool and set the
657 -- related secondary stack globals.
658
659 Set_String (" Default_Secondary_Stack_Size := ");
660
661 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
662 Set_Int (Opt.Default_Sec_Stack_Size);
663 else
664 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
665 end if;
666
667 Set_Char (';');
668 Write_Statement_Buffer;
669
670 Set_String (" Binder_Sec_Stacks_Count := ");
671 Set_Int (Num_Sec_Stacks);
672 Set_Char (';');
673 Write_Statement_Buffer;
674
675 WBI (" Default_Sized_SS_Pool := " &
676 "Sec_Default_Sized_Stacks'Address;");
677 WBI ("");
678
679 -- When a restricted run-time initializes the main task's secondary
680 -- stack but the program does not use it, no secondary stack is
681 -- generated. Binder_Sec_Stacks_Count is set to zero so the run-time
682 -- is aware that the lack of pre-allocated secondary stack is
683 -- expected.
684
685 elsif System_Secondary_Stack_Used then
686 WBI (" Binder_Sec_Stacks_Count := 0;");
687 end if;
688
689 -- Normal case (standard library not suppressed). Set all global values
690 -- used by the run time.
691
692 else
693 WBI (" Main_Priority : Integer;");
694 WBI (" pragma Import (C, Main_Priority, " &
695 """__gl_main_priority"");");
696 WBI (" Time_Slice_Value : Integer;");
697 WBI (" pragma Import (C, Time_Slice_Value, " &
698 """__gl_time_slice_val"");");
699 WBI (" WC_Encoding : Character;");
700 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
701 WBI (" Locking_Policy : Character;");
702 WBI (" pragma Import (C, Locking_Policy, " &
703 """__gl_locking_policy"");");
704 WBI (" Queuing_Policy : Character;");
705 WBI (" pragma Import (C, Queuing_Policy, " &
706 """__gl_queuing_policy"");");
707 WBI (" Task_Dispatching_Policy : Character;");
708 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
709 """__gl_task_dispatching_policy"");");
710 WBI (" Priority_Specific_Dispatching : System.Address;");
711 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
712 """__gl_priority_specific_dispatching"");");
713 WBI (" Num_Specific_Dispatching : Integer;");
714 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
715 """__gl_num_specific_dispatching"");");
716 WBI (" Main_CPU : Integer;");
717 WBI (" pragma Import (C, Main_CPU, " &
718 """__gl_main_cpu"");");
719
720 WBI (" Interrupt_States : System.Address;");
721 WBI (" pragma Import (C, Interrupt_States, " &
722 """__gl_interrupt_states"");");
723 WBI (" Num_Interrupt_States : Integer;");
724 WBI (" pragma Import (C, Num_Interrupt_States, " &
725 """__gl_num_interrupt_states"");");
726 WBI (" Unreserve_All_Interrupts : Integer;");
727 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
728 """__gl_unreserve_all_interrupts"");");
729
730 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
731 WBI (" Exception_Tracebacks : Integer;");
732 WBI (" pragma Import (C, Exception_Tracebacks, " &
733 """__gl_exception_tracebacks"");");
734
735 if Exception_Tracebacks_Symbolic then
736 WBI (" Exception_Tracebacks_Symbolic : Integer;");
737 WBI (" pragma Import (C, Exception_Tracebacks_Symbolic, " &
738 """__gl_exception_tracebacks_symbolic"");");
739 end if;
740 end if;
741
742 WBI (" Detect_Blocking : Integer;");
743 WBI (" pragma Import (C, Detect_Blocking, " &
744 """__gl_detect_blocking"");");
745 WBI (" Default_Stack_Size : Integer;");
746 WBI (" pragma Import (C, Default_Stack_Size, " &
747 """__gl_default_stack_size"");");
748
749 if Sec_Stack_Used then
750 WBI (" Default_Secondary_Stack_Size : " &
751 "System.Parameters.Size_Type;");
752 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
753 """__gnat_default_ss_size"");");
754 end if;
755
756 WBI (" Leap_Seconds_Support : Integer;");
757 WBI (" pragma Import (C, Leap_Seconds_Support, " &
758 """__gl_leap_seconds_support"");");
759 WBI (" Bind_Env_Addr : System.Address;");
760 WBI (" pragma Import (C, Bind_Env_Addr, " &
761 """__gl_bind_env_addr"");");
762
763 -- Import entry point for elaboration time signal handler
764 -- installation, and indication of if it's been called previously.
765
766 WBI ("");
767 WBI (" procedure Runtime_Initialize " &
768 "(Install_Handler : Integer);");
769 WBI (" pragma Import (C, Runtime_Initialize, " &
770 """__gnat_runtime_initialize"");");
771
772 -- Import handlers attach procedure for sequential elaboration policy
773
774 if System_Interrupts_Used
775 and then Partition_Elaboration_Policy_Specified = 'S'
776 then
777 WBI (" procedure Install_Restricted_Handlers_Sequential;");
778 WBI (" pragma Import (C," &
779 "Install_Restricted_Handlers_Sequential," &
780 " ""__gnat_attach_all_handlers"");");
781 WBI ("");
782 end if;
783
784 -- Import task activation procedure for sequential elaboration
785 -- policy.
786
787 if System_Tasking_Restricted_Stages_Used
788 and then Partition_Elaboration_Policy_Specified = 'S'
789 then
790 WBI (" Partition_Elaboration_Policy : Character;");
791 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
792 " ""__gnat_partition_elaboration_policy"");");
793 WBI ("");
794 WBI (" procedure Activate_All_Tasks_Sequential;");
795 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
796 " ""__gnat_activate_all_tasks"");");
797 end if;
798
799 -- Import procedure to start slave cpus for bareboard runtime
800
801 if System_BB_CPU_Primitives_Multiprocessors_Used then
802 WBI (" procedure Start_Slave_CPUs;");
803 WBI (" pragma Import (C, Start_Slave_CPUs," &
804 " ""__gnat_start_slave_cpus"");");
805 end if;
806
807 -- For restricted run-time libraries (ZFP and Ravenscar)
808 -- tasks are non-terminating, so we do not want finalization.
809
810 if not Configurable_Run_Time_On_Target then
811 WBI ("");
812 WBI (" Finalize_Library_Objects : No_Param_Proc;");
813 WBI (" pragma Import (C, Finalize_Library_Objects, " &
814 """__gnat_finalize_library_objects"");");
815 end if;
816
817 -- Initialize stack limit variable of the environment task if the
818 -- stack check method is stack limit and stack check is enabled.
819
820 if Stack_Check_Limits_On_Target
821 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
822 then
823 WBI ("");
824 WBI (" procedure Initialize_Stack_Limit;");
825 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
826 """__gnat_initialize_stack_limit"");");
827 end if;
828
829 -- When dispatching domains are used then we need to signal it
830 -- before calling the main procedure.
831
832 if Dispatching_Domains_Used then
833 WBI (" procedure Freeze_Dispatching_Domains;");
834 WBI (" pragma Import");
835 WBI (" (Ada, Freeze_Dispatching_Domains, "
836 & """__gnat_freeze_dispatching_domains"");");
837 end if;
838
839 -- Secondary stack global variables
840
841 WBI (" Binder_Sec_Stacks_Count : Natural;");
842 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
843 """__gnat_binder_ss_count"");");
844
845 WBI (" Default_Sized_SS_Pool : System.Address;");
846 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
847 """__gnat_default_ss_pool"");");
848
849 WBI ("");
850
851 -- Start of processing for Adainit
852
853 WBI (" begin");
854 WBI (" if Is_Elaborated then");
855 WBI (" return;");
856 WBI (" end if;");
857 WBI (" Is_Elaborated := True;");
858
859 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
860 -- restriction No_Standard_Allocators_After_Elaboration is active.
861
862 if Cumulative_Restrictions.Set
863 (No_Standard_Allocators_After_Elaboration)
864 then
865 WBI (" System.Elaboration_Allocators."
866 & "Mark_Start_Of_Elaboration;");
867 end if;
868
869 -- Generate assignments to initialize globals
870
871 Set_String (" Main_Priority := ");
872 Set_Int (Main_Priority);
873 Set_Char (';');
874 Write_Statement_Buffer;
875
876 Set_String (" Time_Slice_Value := ");
877
878 if Task_Dispatching_Policy_Specified = 'F'
879 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
880 then
881 Set_Int (0);
882 else
883 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
884 end if;
885
886 Set_Char (';');
887 Write_Statement_Buffer;
888
889 Set_String (" WC_Encoding := '");
890 Set_Char (Get_WC_Encoding);
891
892 Set_String ("';");
893 Write_Statement_Buffer;
894
895 Set_String (" Locking_Policy := '");
896 Set_Char (Locking_Policy_Specified);
897 Set_String ("';");
898 Write_Statement_Buffer;
899
900 Set_String (" Queuing_Policy := '");
901 Set_Char (Queuing_Policy_Specified);
902 Set_String ("';");
903 Write_Statement_Buffer;
904
905 Set_String (" Task_Dispatching_Policy := '");
906 Set_Char (Task_Dispatching_Policy_Specified);
907 Set_String ("';");
908 Write_Statement_Buffer;
909
910 if System_Tasking_Restricted_Stages_Used
911 and then Partition_Elaboration_Policy_Specified = 'S'
912 then
913 Set_String (" Partition_Elaboration_Policy := '");
914 Set_Char (Partition_Elaboration_Policy_Specified);
915 Set_String ("';");
916 Write_Statement_Buffer;
917 end if;
918
919 Gen_Restrictions;
920
921 WBI (" Priority_Specific_Dispatching :=");
922 WBI (" Local_Priority_Specific_Dispatching'Address;");
923
924 Set_String (" Num_Specific_Dispatching := ");
925 Set_Int (PSD_Pragma_Settings.Last + 1);
926 Set_Char (';');
927 Write_Statement_Buffer;
928
929 Set_String (" Main_CPU := ");
930 Set_Int (Main_CPU);
931 Set_Char (';');
932 Write_Statement_Buffer;
933
934 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
935
936 Set_String (" Num_Interrupt_States := ");
937 Set_Int (IS_Pragma_Settings.Last + 1);
938 Set_Char (';');
939 Write_Statement_Buffer;
940
941 Set_String (" Unreserve_All_Interrupts := ");
942
943 if Unreserve_All_Interrupts_Specified then
944 Set_String ("1");
945 else
946 Set_String ("0");
947 end if;
948
949 Set_Char (';');
950 Write_Statement_Buffer;
951
952 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
953 WBI (" Exception_Tracebacks := 1;");
954
955 if Exception_Tracebacks_Symbolic then
956 WBI (" Exception_Tracebacks_Symbolic := 1;");
957 end if;
958 end if;
959
960 Set_String (" Detect_Blocking := ");
961
962 if Detect_Blocking then
963 Set_Int (1);
964 else
965 Set_Int (0);
966 end if;
967
968 Set_String (";");
969 Write_Statement_Buffer;
970
971 Set_String (" Default_Stack_Size := ");
972 Set_Int (Default_Stack_Size);
973 Set_String (";");
974 Write_Statement_Buffer;
975
976 Set_String (" Leap_Seconds_Support := ");
977
978 if Leap_Seconds_Support then
979 Set_Int (1);
980 else
981 Set_Int (0);
982 end if;
983
984 Set_String (";");
985 Write_Statement_Buffer;
986
987 if Bind_Env_String_Built then
988 WBI (" Bind_Env_Addr := Bind_Env'Address;");
989 end if;
990
991 WBI ("");
992
993 -- Generate default-sized secondary stack pool and set secondary
994 -- stack globals.
995
996 if Sec_Stack_Used then
997
998 -- Elaborate the body of the binder to initialize the default-
999 -- sized secondary stack pool.
1000
1001 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
1002
1003 -- Generate the default-sized secondary stack pool and set the
1004 -- related secondary stack globals.
1005
1006 Set_String (" Default_Secondary_Stack_Size := ");
1007
1008 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
1009 Set_Int (Opt.Default_Sec_Stack_Size);
1010 else
1011 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
1012 end if;
1013
1014 Set_Char (';');
1015 Write_Statement_Buffer;
1016
1017 Set_String (" Binder_Sec_Stacks_Count := ");
1018 Set_Int (Num_Sec_Stacks);
1019 Set_Char (';');
1020 Write_Statement_Buffer;
1021
1022 Set_String (" Default_Sized_SS_Pool := ");
1023
1024 if Num_Sec_Stacks > 0 then
1025 Set_String ("Sec_Default_Sized_Stacks'Address;");
1026 else
1027 Set_String ("System.Null_Address;");
1028 end if;
1029
1030 Write_Statement_Buffer;
1031 WBI ("");
1032 end if;
1033
1034 -- Generate call to Runtime_Initialize
1035
1036 WBI (" Runtime_Initialize (1);");
1037 end if;
1038
1039 -- Generate call to set Initialize_Scalar values if active
1040
1041 if Initialize_Scalars_Used then
1042 WBI ("");
1043 Set_String (" System.Scalar_Values.Initialize ('");
1044 Set_Char (Initialize_Scalars_Mode1);
1045 Set_String ("', '");
1046 Set_Char (Initialize_Scalars_Mode2);
1047 Set_String ("');");
1048 Write_Statement_Buffer;
1049 end if;
1050
1051 -- Initialize stack limit variable of the environment task if the stack
1052 -- check method is stack limit and stack check is enabled.
1053
1054 if Stack_Check_Limits_On_Target
1055 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1056 then
1057 WBI ("");
1058 WBI (" Initialize_Stack_Limit;");
1059 end if;
1060
1061 -- On CodePeer, the finalization of library objects is not relevant
1062
1063 if CodePeer_Mode then
1064 null;
1065
1066 -- If this is the main program case, attach finalize_library to the soft
1067 -- link. Do it only when not using a restricted run time, in which case
1068 -- tasks are non-terminating, so we do not want library-level
1069 -- finalization.
1070
1071 elsif not Bind_For_Library
1072 and then not Configurable_Run_Time_On_Target
1073 and then not Suppress_Standard_Library_On_Target
1074 then
1075 WBI ("");
1076
1077 if Lib_Final_Built then
1078 Set_String (" Finalize_Library_Objects := ");
1079 Set_String ("finalize_library'access;");
1080 else
1081 Set_String (" Finalize_Library_Objects := null;");
1082 end if;
1083
1084 Write_Statement_Buffer;
1085 end if;
1086
1087 -- Generate elaboration calls
1088
1089 if not CodePeer_Mode then
1090 WBI ("");
1091 end if;
1092
1093 Gen_Elab_Calls (Elab_Order);
1094
1095 if not CodePeer_Mode then
1096
1097 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
1098 -- restriction No_Standard_Allocators_After_Elaboration is active.
1099
1100 if Cumulative_Restrictions.Set
1101 (No_Standard_Allocators_After_Elaboration)
1102 then
1103 WBI
1104 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
1105 end if;
1106
1107 -- From this point, no new dispatching domain can be created
1108
1109 if Dispatching_Domains_Used then
1110 WBI (" Freeze_Dispatching_Domains;");
1111 end if;
1112
1113 -- Sequential partition elaboration policy
1114
1115 if Partition_Elaboration_Policy_Specified = 'S' then
1116 if System_Interrupts_Used then
1117 WBI (" Install_Restricted_Handlers_Sequential;");
1118 end if;
1119
1120 if System_Tasking_Restricted_Stages_Used then
1121 WBI (" Activate_All_Tasks_Sequential;");
1122 end if;
1123 end if;
1124
1125 if System_BB_CPU_Primitives_Multiprocessors_Used then
1126 WBI (" Start_Slave_CPUs;");
1127 end if;
1128 end if;
1129
1130 WBI (" end " & Ada_Init_Name.all & ";");
1131 WBI ("");
1132 end Gen_Adainit;
1133
1134 -------------------------
1135 -- Gen_Bind_Env_String --
1136 -------------------------
1137
1138 procedure Gen_Bind_Env_String is
1139 procedure Write_Name_With_Len (Nam : Name_Id);
1140 -- Write Nam as a string literal, prefixed with one
1141 -- character encoding Nam's length.
1142
1143 -------------------------
1144 -- Write_Name_With_Len --
1145 -------------------------
1146
1147 procedure Write_Name_With_Len (Nam : Name_Id) is
1148 begin
1149 Get_Name_String (Nam);
1150
1151 Start_String;
1152 Store_String_Char (Character'Val (Name_Len));
1153 Store_String_Chars (Name_Buffer (1 .. Name_Len));
1154
1155 Write_String_Table_Entry (End_String);
1156 end Write_Name_With_Len;
1157
1158 -- Local variables
1159
1160 Amp : Character;
1161 KN : Name_Id := No_Name;
1162 VN : Name_Id := No_Name;
1163
1164 -- Start of processing for Gen_Bind_Env_String
1165
1166 begin
1167 Bind_Environment.Get_First (KN, VN);
1168
1169 if VN = No_Name then
1170 return;
1171 end if;
1172
1173 Set_Special_Output (Write_Bind_Line'Access);
1174
1175 WBI (" Bind_Env : aliased constant String :=");
1176 Amp := ' ';
1177 while VN /= No_Name loop
1178 Write_Str (" " & Amp & ' ');
1179 Write_Name_With_Len (KN);
1180 Write_Str (" & ");
1181 Write_Name_With_Len (VN);
1182 Write_Eol;
1183
1184 Bind_Environment.Get_Next (KN, VN);
1185 Amp := '&';
1186 end loop;
1187 WBI (" & ASCII.NUL;");
1188
1189 Cancel_Special_Output;
1190
1191 Bind_Env_String_Built := True;
1192 end Gen_Bind_Env_String;
1193
1194 --------------------------
1195 -- Gen_CodePeer_Wrapper --
1196 --------------------------
1197
1198 procedure Gen_CodePeer_Wrapper is
1199 Callee_Name : constant String := "Ada_Main_Program";
1200
1201 begin
1202 if ALIs.Table (ALIs.First).Main_Program = Proc then
1203 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
1204 WBI (" begin");
1205 WBI (" " & Callee_Name & ";");
1206
1207 else
1208 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1209 WBI (" begin");
1210 WBI (" return " & Callee_Name & ";");
1211 end if;
1212
1213 WBI (" end " & CodePeer_Wrapper_Name & ";");
1214 WBI ("");
1215 end Gen_CodePeer_Wrapper;
1216
1217 --------------------
1218 -- Gen_Elab_Calls --
1219 --------------------
1220
1221 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array) is
1222 Check_Elab_Flag : Boolean;
1223
1224 begin
1225 -- Loop through elaboration order entries
1226
1227 for E in Elab_Order'Range loop
1228 declare
1229 Unum : constant Unit_Id := Elab_Order (E);
1230 U : Unit_Record renames Units.Table (Unum);
1231
1232 Unum_Spec : Unit_Id;
1233 -- This is the unit number of the spec that corresponds to
1234 -- this entry. It is the same as Unum except when the body
1235 -- and spec are different and we are currently processing
1236 -- the body, in which case it is the spec (Unum + 1).
1237
1238 begin
1239 if U.Utype = Is_Body then
1240 Unum_Spec := Unum + 1;
1241 else
1242 Unum_Spec := Unum;
1243 end if;
1244
1245 -- Nothing to do if predefined unit in no run time mode
1246
1247 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1248 null;
1249
1250 -- Likewise if this is an interface to a stand alone library
1251
1252 elsif U.SAL_Interface then
1253 null;
1254
1255 -- Case of no elaboration code
1256
1257 elsif U.No_Elab
1258
1259 -- In CodePeer mode, we special case subprogram bodies which
1260 -- are handled in the 'else' part below, and lead to a call
1261 -- to <subp>'Elab_Subp_Body.
1262
1263 and then (not CodePeer_Mode
1264
1265 -- Test for spec
1266
1267 or else U.Utype = Is_Spec
1268 or else U.Utype = Is_Spec_Only
1269 or else U.Unit_Kind /= 's')
1270 then
1271 -- In the case of a body with a separate spec, where the
1272 -- separate spec has an elaboration entity defined, this is
1273 -- where we increment the elaboration entity if one exists.
1274
1275 -- Likewise for lone specs with an elaboration entity defined
1276 -- despite No_Elaboration_Code, e.g. when requested to preserve
1277 -- control flow.
1278
1279 if (U.Utype = Is_Body or else U.Utype = Is_Spec_Only)
1280 and then Units.Table (Unum_Spec).Set_Elab_Entity
1281 and then not CodePeer_Mode
1282 then
1283 Set_String (" E");
1284 Set_Unit_Number (Unum_Spec);
1285 Set_String (" := E");
1286 Set_Unit_Number (Unum_Spec);
1287 Set_String (" + 1;");
1288 Write_Statement_Buffer;
1289 end if;
1290
1291 -- Here if elaboration code is present. If binding a library
1292 -- or if there is a non-Ada main subprogram then we generate:
1293
1294 -- if uname_E = 0 then
1295 -- uname'elab_[spec|body];
1296 -- end if;
1297 -- uname_E := uname_E + 1;
1298
1299 -- Otherwise, elaboration routines are called unconditionally:
1300
1301 -- uname'elab_[spec|body];
1302 -- uname_E := uname_E + 1;
1303
1304 -- The uname_E increment is skipped if this is a separate spec,
1305 -- since it will be done when we process the body.
1306
1307 -- In CodePeer mode, we do not generate any reference to xxx_E
1308 -- variables, only calls to 'Elab* subprograms.
1309
1310 else
1311 -- Check incompatibilities with No_Multiple_Elaboration
1312
1313 if not CodePeer_Mode
1314 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1315 then
1316 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1317
1318 if Force_Checking_Of_Elaboration_Flags then
1319 Osint.Fail
1320 ("-F (force elaboration checks) switch not allowed "
1321 & "with restriction No_Multiple_Elaboration active");
1322
1323 -- Interfacing of libraries not allowed
1324
1325 elsif Interface_Library_Unit then
1326 Osint.Fail
1327 ("binding of interfaced libraries not allowed "
1328 & "with restriction No_Multiple_Elaboration active");
1329
1330 -- Non-Ada main program not allowed
1331
1332 elsif not Bind_Main_Program then
1333 Osint.Fail
1334 ("non-Ada main program not allowed "
1335 & "with restriction No_Multiple_Elaboration active");
1336 end if;
1337 end if;
1338
1339 -- OK, see if we need to test elaboration flag
1340
1341 Check_Elab_Flag :=
1342 Units.Table (Unum_Spec).Set_Elab_Entity
1343 and then not CodePeer_Mode
1344 and then (Force_Checking_Of_Elaboration_Flags
1345 or Interface_Library_Unit
1346 or not Bind_Main_Program);
1347
1348 if Check_Elab_Flag then
1349 Set_String (" if E");
1350 Set_Unit_Number (Unum_Spec);
1351 Set_String (" = 0 then");
1352 Write_Statement_Buffer;
1353 Set_String (" ");
1354 end if;
1355
1356 Set_String (" ");
1357 Get_Decoded_Name_String_With_Brackets (U.Uname);
1358
1359 if Name_Buffer (Name_Len) = 's' then
1360 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1361 "'elab_spec";
1362 Name_Len := Name_Len + 8;
1363
1364 -- Special case in CodePeer mode for subprogram bodies
1365 -- which correspond to CodePeer 'Elab_Subp_Body special
1366 -- init procedure.
1367
1368 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1369 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1370 "'elab_subp_body";
1371 Name_Len := Name_Len + 13;
1372
1373 else
1374 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1375 "'elab_body";
1376 Name_Len := Name_Len + 8;
1377 end if;
1378
1379 Set_Casing (U.Icasing);
1380 Set_Name_Buffer;
1381 Set_Char (';');
1382 Write_Statement_Buffer;
1383
1384 if Check_Elab_Flag then
1385 WBI (" end if;");
1386 end if;
1387
1388 if U.Utype /= Is_Spec
1389 and then not CodePeer_Mode
1390 and then Units.Table (Unum_Spec).Set_Elab_Entity
1391 then
1392 Set_String (" E");
1393 Set_Unit_Number (Unum_Spec);
1394 Set_String (" := E");
1395 Set_Unit_Number (Unum_Spec);
1396 Set_String (" + 1;");
1397 Write_Statement_Buffer;
1398 end if;
1399 end if;
1400 end;
1401 end loop;
1402 end Gen_Elab_Calls;
1403
1404 ------------------------
1405 -- Gen_Elab_Externals --
1406 ------------------------
1407
1408 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array) is
1409 begin
1410 if CodePeer_Mode then
1411 return;
1412 end if;
1413
1414 for E in Elab_Order'Range loop
1415 declare
1416 Unum : constant Unit_Id := Elab_Order (E);
1417 U : Unit_Record renames Units.Table (Unum);
1418
1419 begin
1420 -- Check for Elab_Entity to be set for this unit
1421
1422 if U.Set_Elab_Entity
1423
1424 -- Don't generate reference for stand alone library
1425
1426 and then not U.SAL_Interface
1427
1428 -- Don't generate reference for predefined file in No_Run_Time
1429 -- mode, since we don't include the object files in this case
1430
1431 and then not
1432 (No_Run_Time_Mode
1433 and then Is_Predefined_File_Name (U.Sfile))
1434 then
1435 Get_Name_String (U.Sfile);
1436 Set_String (" ");
1437 Set_String ("E");
1438 Set_Unit_Number (Unum);
1439 Set_String (" : Short_Integer; pragma Import (Ada, E");
1440 Set_Unit_Number (Unum);
1441 Set_String (", """);
1442 Get_Name_String (U.Uname);
1443 Set_Unit_Name;
1444 Set_String ("_E"");");
1445 Write_Statement_Buffer;
1446 end if;
1447 end;
1448 end loop;
1449
1450 WBI ("");
1451 end Gen_Elab_Externals;
1452
1453 --------------------
1454 -- Gen_Elab_Order --
1455 --------------------
1456
1457 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array) is
1458 begin
1459 WBI ("");
1460 WBI (" -- BEGIN ELABORATION ORDER");
1461
1462 for J in Elab_Order'Range loop
1463 Set_String (" -- ");
1464 Get_Name_String (Units.Table (Elab_Order (J)).Uname);
1465 Set_Name_Buffer;
1466 Write_Statement_Buffer;
1467 end loop;
1468
1469 WBI (" -- END ELABORATION ORDER");
1470 end Gen_Elab_Order;
1471
1472 --------------------------
1473 -- Gen_Finalize_Library --
1474 --------------------------
1475
1476 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array) is
1477 procedure Gen_Header;
1478 -- Generate the header of the finalization routine
1479
1480 ----------------
1481 -- Gen_Header --
1482 ----------------
1483
1484 procedure Gen_Header is
1485 begin
1486 WBI (" procedure finalize_library is");
1487 WBI (" begin");
1488 end Gen_Header;
1489
1490 -- Local variables
1491
1492 Count : Int := 1;
1493 U : Unit_Record;
1494 Uspec : Unit_Record;
1495 Unum : Unit_Id;
1496
1497 -- Start of processing for Gen_Finalize_Library
1498
1499 begin
1500 if CodePeer_Mode then
1501 return;
1502 end if;
1503
1504 for E in reverse Elab_Order'Range loop
1505 Unum := Elab_Order (E);
1506 U := Units.Table (Unum);
1507
1508 -- Dealing with package bodies is a little complicated. In such
1509 -- cases we must retrieve the package spec since it contains the
1510 -- spec of the body finalizer.
1511
1512 if U.Utype = Is_Body then
1513 Unum := Unum + 1;
1514 Uspec := Units.Table (Unum);
1515 else
1516 Uspec := U;
1517 end if;
1518
1519 Get_Name_String (Uspec.Uname);
1520
1521 -- We are only interested in non-generic packages
1522
1523 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1524 null;
1525
1526 -- That aren't an interface to a stand alone library
1527
1528 elsif U.SAL_Interface then
1529 null;
1530
1531 -- Case of no finalization
1532
1533 elsif not U.Has_Finalizer then
1534
1535 -- The only case in which we have to do something is if this
1536 -- is a body, with a separate spec, where the separate spec
1537 -- has a finalizer. In that case, this is where we decrement
1538 -- the elaboration entity.
1539
1540 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1541 if not Lib_Final_Built then
1542 Gen_Header;
1543 Lib_Final_Built := True;
1544 end if;
1545
1546 Set_String (" E");
1547 Set_Unit_Number (Unum);
1548 Set_String (" := E");
1549 Set_Unit_Number (Unum);
1550 Set_String (" - 1;");
1551 Write_Statement_Buffer;
1552 end if;
1553
1554 else
1555 if not Lib_Final_Built then
1556 Gen_Header;
1557 Lib_Final_Built := True;
1558 end if;
1559
1560 -- Generate:
1561 -- declare
1562 -- procedure F<Count>;
1563
1564 Set_String (" declare");
1565 Write_Statement_Buffer;
1566
1567 Set_String (" procedure F");
1568 Set_Int (Count);
1569 Set_Char (';');
1570 Write_Statement_Buffer;
1571
1572 -- Generate:
1573 -- pragma Import (Ada, F<Count>,
1574 -- "xx__yy__finalize_[body|spec]");
1575
1576 Set_String (" pragma Import (Ada, F");
1577 Set_Int (Count);
1578 Set_String (", """);
1579
1580 -- Perform name construction
1581
1582 Set_Unit_Name;
1583 Set_String ("__finalize_");
1584
1585 -- Package spec processing
1586
1587 if U.Utype = Is_Spec
1588 or else U.Utype = Is_Spec_Only
1589 then
1590 Set_String ("spec");
1591
1592 -- Package body processing
1593
1594 else
1595 Set_String ("body");
1596 end if;
1597
1598 Set_String (""");");
1599 Write_Statement_Buffer;
1600
1601 -- If binding a library or if there is a non-Ada main subprogram
1602 -- then we generate:
1603
1604 -- begin
1605 -- uname_E := uname_E - 1;
1606 -- if uname_E = 0 then
1607 -- F<Count>;
1608 -- end if;
1609 -- end;
1610
1611 -- Otherwise, finalization routines are called unconditionally:
1612
1613 -- begin
1614 -- uname_E := uname_E - 1;
1615 -- F<Count>;
1616 -- end;
1617
1618 -- The uname_E decrement is skipped if this is a separate spec,
1619 -- since it will be done when we process the body.
1620
1621 WBI (" begin");
1622
1623 if U.Utype /= Is_Spec then
1624 Set_String (" E");
1625 Set_Unit_Number (Unum);
1626 Set_String (" := E");
1627 Set_Unit_Number (Unum);
1628 Set_String (" - 1;");
1629 Write_Statement_Buffer;
1630 end if;
1631
1632 if Interface_Library_Unit or not Bind_Main_Program then
1633 Set_String (" if E");
1634 Set_Unit_Number (Unum);
1635 Set_String (" = 0 then");
1636 Write_Statement_Buffer;
1637 Set_String (" ");
1638 end if;
1639
1640 Set_String (" F");
1641 Set_Int (Count);
1642 Set_Char (';');
1643 Write_Statement_Buffer;
1644
1645 if Interface_Library_Unit or not Bind_Main_Program then
1646 WBI (" end if;");
1647 end if;
1648
1649 WBI (" end;");
1650
1651 Count := Count + 1;
1652 end if;
1653 end loop;
1654
1655 if Lib_Final_Built then
1656
1657 -- It is possible that the finalization of a library-level object
1658 -- raised an exception. In that case import the actual exception
1659 -- and the routine necessary to raise it.
1660
1661 WBI (" declare");
1662 WBI (" procedure Reraise_Library_Exception_If_Any;");
1663
1664 Set_String (" pragma Import (Ada, ");
1665 Set_String ("Reraise_Library_Exception_If_Any, ");
1666 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1667 Write_Statement_Buffer;
1668
1669 WBI (" begin");
1670 WBI (" Reraise_Library_Exception_If_Any;");
1671 WBI (" end;");
1672 WBI (" end finalize_library;");
1673 WBI ("");
1674 end if;
1675 end Gen_Finalize_Library;
1676
1677 --------------
1678 -- Gen_Main --
1679 --------------
1680
1681 procedure Gen_Main is
1682 begin
1683 if not No_Main_Subprogram then
1684
1685 -- To call the main program, we declare it using a pragma Import
1686 -- Ada with the right link name.
1687
1688 -- It might seem more obvious to "with" the main program, and call
1689 -- it in the normal Ada manner. We do not do this for three
1690 -- reasons:
1691
1692 -- 1. It is more efficient not to recompile the main program
1693 -- 2. We are not entitled to assume the source is accessible
1694 -- 3. We don't know what options to use to compile it
1695
1696 -- It is really reason 3 that is most critical (indeed we used
1697 -- to generate the "with", but several regression tests failed).
1698
1699 if ALIs.Table (ALIs.First).Main_Program = Func then
1700 WBI (" function Ada_Main_Program return Integer;");
1701 else
1702 WBI (" procedure Ada_Main_Program;");
1703 end if;
1704
1705 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1706 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1707 Set_Main_Program_Name;
1708 Set_String (""");");
1709
1710 Write_Statement_Buffer;
1711 WBI ("");
1712
1713 -- For CodePeer, declare a wrapper for the user-defined main program
1714
1715 if CodePeer_Mode then
1716 Gen_CodePeer_Wrapper;
1717 end if;
1718 end if;
1719
1720 if Exit_Status_Supported_On_Target then
1721 Set_String (" function ");
1722 else
1723 Set_String (" procedure ");
1724 end if;
1725
1726 Set_String (Get_Main_Name);
1727
1728 if Command_Line_Args_On_Target then
1729 Write_Statement_Buffer;
1730 WBI (" (argc : Integer;");
1731 WBI (" argv : System.Address;");
1732 WBI (" envp : System.Address)");
1733
1734 if Exit_Status_Supported_On_Target then
1735 WBI (" return Integer");
1736 end if;
1737
1738 WBI (" is");
1739
1740 else
1741 if Exit_Status_Supported_On_Target then
1742 Set_String (" return Integer is");
1743 else
1744 Set_String (" is");
1745 end if;
1746
1747 Write_Statement_Buffer;
1748 end if;
1749
1750 if Opt.Default_Exit_Status /= 0
1751 and then Bind_Main_Program
1752 and then not Configurable_Run_Time_Mode
1753 then
1754 WBI (" procedure Set_Exit_Status (Status : Integer);");
1755 WBI (" pragma Import (C, Set_Exit_Status, " &
1756 """__gnat_set_exit_status"");");
1757 WBI ("");
1758 end if;
1759
1760 -- Initialize and Finalize
1761
1762 if not CodePeer_Mode
1763 and then not Cumulative_Restrictions.Set (No_Finalization)
1764 then
1765 WBI (" procedure Initialize (Addr : System.Address);");
1766 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1767 WBI ("");
1768 WBI (" procedure Finalize;");
1769 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1770 end if;
1771
1772 -- If we want to analyze the stack, we must import corresponding symbols
1773
1774 if Dynamic_Stack_Measurement then
1775 WBI ("");
1776 WBI (" procedure Output_Results;");
1777 WBI (" pragma Import (C, Output_Results, " &
1778 """__gnat_stack_usage_output_results"");");
1779
1780 WBI ("");
1781 WBI (" " &
1782 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1783 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1784 """__gnat_stack_usage_initialize"");");
1785 end if;
1786
1787 -- Deal with declarations for main program case
1788
1789 if not No_Main_Subprogram then
1790 if ALIs.Table (ALIs.First).Main_Program = Func then
1791 WBI (" Result : Integer;");
1792 WBI ("");
1793 end if;
1794
1795 if Bind_Main_Program
1796 and not Suppress_Standard_Library_On_Target
1797 and not CodePeer_Mode
1798 then
1799 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1800 WBI ("");
1801 end if;
1802 end if;
1803
1804 -- Generate a reference to Ada_Main_Program_Name. This symbol is not
1805 -- referenced elsewhere in the generated program, but is needed by
1806 -- the debugger (that's why it is generated in the first place). The
1807 -- reference stops Ada_Main_Program_Name from being optimized away by
1808 -- smart linkers.
1809
1810 -- Because this variable is unused, we make this variable "aliased"
1811 -- with a pragma Volatile in order to tell the compiler to preserve
1812 -- this variable at any level of optimization.
1813
1814 -- CodePeer and CCG do not need this extra code on the other hand.
1815
1816 if Bind_Main_Program
1817 and then not CodePeer_Mode
1818 and then not Generate_C_Code
1819 then
1820 WBI (" Ensure_Reference : aliased System.Address := " &
1821 "Ada_Main_Program_Name'Address;");
1822 WBI (" pragma Volatile (Ensure_Reference);");
1823 WBI ("");
1824 end if;
1825
1826 WBI (" begin");
1827
1828 -- Acquire command line arguments if present on target
1829
1830 if CodePeer_Mode then
1831 null;
1832
1833 elsif Command_Line_Args_On_Target then
1834 WBI (" gnat_argc := argc;");
1835 WBI (" gnat_argv := argv;");
1836 WBI (" gnat_envp := envp;");
1837 WBI ("");
1838
1839 -- If configurable run time and no command line args, then nothing needs
1840 -- to be done since the gnat_argc/argv/envp variables are suppressed in
1841 -- this case.
1842
1843 elsif Configurable_Run_Time_On_Target then
1844 null;
1845
1846 -- Otherwise set dummy values (to be filled in by some other unit?)
1847
1848 else
1849 WBI (" gnat_argc := 0;");
1850 WBI (" gnat_argv := System.Null_Address;");
1851 WBI (" gnat_envp := System.Null_Address;");
1852 end if;
1853
1854 if Opt.Default_Exit_Status /= 0
1855 and then Bind_Main_Program
1856 and then not Configurable_Run_Time_Mode
1857 then
1858 Set_String (" Set_Exit_Status (");
1859 Set_Int (Opt.Default_Exit_Status);
1860 Set_String (");");
1861 Write_Statement_Buffer;
1862 end if;
1863
1864 if Dynamic_Stack_Measurement then
1865 Set_String (" Initialize_Stack_Analysis (");
1866 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1867 Set_String (");");
1868 Write_Statement_Buffer;
1869 end if;
1870
1871 if not Cumulative_Restrictions.Set (No_Finalization)
1872 and then not CodePeer_Mode
1873 then
1874 if not No_Main_Subprogram
1875 and then Bind_Main_Program
1876 and then not Suppress_Standard_Library_On_Target
1877 then
1878 WBI (" Initialize (SEH'Address);");
1879 else
1880 WBI (" Initialize (System.Null_Address);");
1881 end if;
1882 end if;
1883
1884 WBI (" " & Ada_Init_Name.all & ";");
1885
1886 if not No_Main_Subprogram then
1887 if CodePeer_Mode then
1888 if ALIs.Table (ALIs.First).Main_Program = Proc then
1889 WBI (" " & CodePeer_Wrapper_Name & ";");
1890 else
1891 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1892 end if;
1893
1894 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1895 WBI (" Ada_Main_Program;");
1896
1897 else
1898 WBI (" Result := Ada_Main_Program;");
1899 end if;
1900 end if;
1901
1902 -- Adafinal call is skipped if no finalization
1903
1904 if not Cumulative_Restrictions.Set (No_Finalization) then
1905 WBI (" adafinal;");
1906 end if;
1907
1908 -- Prints the result of static stack analysis
1909
1910 if Dynamic_Stack_Measurement then
1911 WBI (" Output_Results;");
1912 end if;
1913
1914 -- Finalize is only called if we have a run time
1915
1916 if not Cumulative_Restrictions.Set (No_Finalization)
1917 and then not CodePeer_Mode
1918 then
1919 WBI (" Finalize;");
1920 end if;
1921
1922 -- Return result
1923
1924 if Exit_Status_Supported_On_Target then
1925 if No_Main_Subprogram
1926 or else ALIs.Table (ALIs.First).Main_Program = Proc
1927 then
1928 WBI (" return (gnat_exit_status);");
1929 else
1930 WBI (" return (Result);");
1931 end if;
1932 end if;
1933
1934 WBI (" end;");
1935 WBI ("");
1936 end Gen_Main;
1937
1938 ------------------------------
1939 -- Gen_Object_Files_Options --
1940 ------------------------------
1941
1942 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array) is
1943 Lgnat : Natural;
1944 -- This keeps track of the position in the sorted set of entries in the
1945 -- Linker_Options table of where the first entry from an internal file
1946 -- appears.
1947
1948 Linker_Option_List_Started : Boolean := False;
1949 -- Set to True when "LINKER OPTION LIST" is displayed
1950
1951 procedure Write_Linker_Option;
1952 -- Write binder info linker option
1953
1954 -------------------------
1955 -- Write_Linker_Option --
1956 -------------------------
1957
1958 procedure Write_Linker_Option is
1959 Start : Natural;
1960 Stop : Natural;
1961
1962 begin
1963 -- Loop through string, breaking at null's
1964
1965 Start := 1;
1966 while Start < Name_Len loop
1967
1968 -- Find null ending this section
1969
1970 Stop := Start + 1;
1971 while Name_Buffer (Stop) /= ASCII.NUL
1972 and then Stop <= Name_Len loop
1973 Stop := Stop + 1;
1974 end loop;
1975
1976 -- Process section if non-null
1977
1978 if Stop > Start then
1979 if Output_Linker_Option_List then
1980 if not Zero_Formatting then
1981 if not Linker_Option_List_Started then
1982 Linker_Option_List_Started := True;
1983 Write_Eol;
1984 Write_Str (" LINKER OPTION LIST");
1985 Write_Eol;
1986 Write_Eol;
1987 end if;
1988
1989 Write_Str (" ");
1990 end if;
1991
1992 Write_Str (Name_Buffer (Start .. Stop - 1));
1993 Write_Eol;
1994 end if;
1995 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
1996 end if;
1997
1998 Start := Stop + 1;
1999 end loop;
2000 end Write_Linker_Option;
2001
2002 -- Start of processing for Gen_Object_Files_Options
2003
2004 begin
2005 WBI ("-- BEGIN Object file/option list");
2006
2007 if Object_List_Filename /= null then
2008 Set_List_File (Object_List_Filename.all);
2009 end if;
2010
2011 for E in Elab_Order'Range loop
2012
2013 -- If not spec that has an associated body, then generate a comment
2014 -- giving the name of the corresponding object file.
2015
2016 if not Units.Table (Elab_Order (E)).SAL_Interface
2017 and then Units.Table (Elab_Order (E)).Utype /= Is_Spec
2018 then
2019 Get_Name_String
2020 (ALIs.Table
2021 (Units.Table (Elab_Order (E)).My_ALI).Ofile_Full_Name);
2022
2023 -- If the presence of an object file is necessary or if it exists,
2024 -- then use it.
2025
2026 if not Hostparm.Exclude_Missing_Objects
2027 or else
2028 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
2029 then
2030 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2031
2032 if Output_Object_List then
2033 Write_Str (Name_Buffer (1 .. Name_Len));
2034 Write_Eol;
2035 end if;
2036 end if;
2037 end if;
2038 end loop;
2039
2040 if Object_List_Filename /= null then
2041 Close_List_File;
2042 end if;
2043
2044 -- Add a "-Ldir" for each directory in the object path
2045
2046 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2047 declare
2048 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2049
2050 begin
2051 Name_Len := 0;
2052 Add_Str_To_Name_Buffer ("-L");
2053 Add_Str_To_Name_Buffer (Dir.all);
2054 Write_Linker_Option;
2055 end;
2056 end loop;
2057
2058 if not (Opt.No_Run_Time_Mode or Opt.No_Stdlib) then
2059 Name_Len := 0;
2060
2061 if Opt.Shared_Libgnat then
2062 Add_Str_To_Name_Buffer ("-shared");
2063 else
2064 Add_Str_To_Name_Buffer ("-static");
2065 end if;
2066
2067 -- Write directly to avoid inclusion in -K output as -static and
2068 -- -shared are not usually specified linker options.
2069
2070 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2071 end if;
2072
2073 -- Sort linker options
2074
2075 -- This sort accomplishes two important purposes:
2076
2077 -- a) All application files are sorted to the front, and all GNAT
2078 -- internal files are sorted to the end. This results in a well
2079 -- defined dividing line between the two sets of files, for the
2080 -- purpose of inserting certain standard library references into
2081 -- the linker arguments list.
2082
2083 -- b) Given two different units, we sort the linker options so that
2084 -- those from a unit earlier in the elaboration order comes later
2085 -- in the list. This is a heuristic designed to create a more
2086 -- friendly order of linker options when the operations appear in
2087 -- separate units. The idea is that if unit A must be elaborated
2088 -- before unit B, then it is more likely that B references
2089 -- libraries included by A, than vice versa, so we want libraries
2090 -- included by A to come after libraries included by B.
2091
2092 -- These two criteria are implemented by function Lt_Linker_Option. Note
2093 -- that a special case of b) is that specs are elaborated before bodies,
2094 -- so linker options from specs come after linker options for bodies,
2095 -- and again, the assumption is that libraries used by the body are more
2096 -- likely to reference libraries used by the spec, than vice versa.
2097
2098 Sort
2099 (Linker_Options.Last,
2100 Move_Linker_Option'Access,
2101 Lt_Linker_Option'Access);
2102
2103 -- Write user linker options, i.e. the set of linker options that come
2104 -- from all files other than GNAT internal files, Lgnat is left set to
2105 -- point to the first entry from a GNAT internal file, or past the end
2106 -- of the entries if there are no internal files.
2107
2108 Lgnat := Linker_Options.Last + 1;
2109
2110 for J in 1 .. Linker_Options.Last loop
2111 if not Linker_Options.Table (J).Internal_File then
2112 Get_Name_String (Linker_Options.Table (J).Name);
2113 Write_Linker_Option;
2114 else
2115 Lgnat := J;
2116 exit;
2117 end if;
2118 end loop;
2119
2120 -- Now we insert standard linker options that must appear after the
2121 -- entries from user files, and before the entries from GNAT run-time
2122 -- files. The reason for this decision is that libraries referenced
2123 -- by internal routines may reference these standard library entries.
2124
2125 -- Note that we do not insert anything when pragma No_Run_Time has
2126 -- been specified or when the standard libraries are not to be used,
2127 -- otherwise on some platforms, we may get duplicate symbols when
2128 -- linking (not clear if this is still the case, but it is harmless).
2129
2130 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2131 if With_GNARL then
2132 Name_Len := 0;
2133
2134 if Opt.Shared_Libgnat then
2135 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2136 else
2137 Add_Str_To_Name_Buffer ("-lgnarl");
2138 end if;
2139
2140 Write_Linker_Option;
2141 end if;
2142
2143 Name_Len := 0;
2144
2145 if Opt.Shared_Libgnat then
2146 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2147 else
2148 Add_Str_To_Name_Buffer ("-lgnat");
2149 end if;
2150
2151 Write_Linker_Option;
2152 end if;
2153
2154 -- Write linker options from all internal files
2155
2156 for J in Lgnat .. Linker_Options.Last loop
2157 Get_Name_String (Linker_Options.Table (J).Name);
2158 Write_Linker_Option;
2159 end loop;
2160
2161 if Output_Linker_Option_List and then not Zero_Formatting then
2162 Write_Eol;
2163 end if;
2164
2165 WBI ("-- END Object file/option list ");
2166 end Gen_Object_Files_Options;
2167
2168 ---------------------
2169 -- Gen_Output_File --
2170 ---------------------
2171
2172 procedure Gen_Output_File
2173 (Filename : String;
2174 Elab_Order : Unit_Id_Array)
2175 is
2176 begin
2177 -- Acquire settings for Interrupt_State pragmas
2178
2179 Set_IS_Pragma_Table;
2180
2181 -- Acquire settings for Priority_Specific_Dispatching pragma
2182
2183 Set_PSD_Pragma_Table;
2184
2185 -- Override time slice value if -T switch is set
2186
2187 if Time_Slice_Set then
2188 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2189 end if;
2190
2191 -- Count number of elaboration calls
2192
2193 for E in Elab_Order'Range loop
2194 if Units.Table (Elab_Order (E)).No_Elab then
2195 null;
2196 else
2197 Num_Elab_Calls := Num_Elab_Calls + 1;
2198 end if;
2199 end loop;
2200
2201 -- Count the number of statically allocated stacks to be generated by
2202 -- the binder. If the user has specified the number of default-sized
2203 -- secondary stacks, use that number. Otherwise start the count at one
2204 -- as the binder is responsible for creating a secondary stack for the
2205 -- main task.
2206
2207 if Opt.Quantity_Of_Default_Size_Sec_Stacks /= -1 then
2208 Num_Sec_Stacks := Quantity_Of_Default_Size_Sec_Stacks;
2209 elsif Sec_Stack_Used then
2210 Num_Sec_Stacks := 1;
2211 end if;
2212
2213 for J in Units.First .. Units.Last loop
2214 Num_Primary_Stacks :=
2215 Num_Primary_Stacks + Units.Table (J).Primary_Stack_Count;
2216
2217 Num_Sec_Stacks :=
2218 Num_Sec_Stacks + Units.Table (J).Sec_Stack_Count;
2219 end loop;
2220
2221 -- Generate output file in appropriate language
2222
2223 Gen_Output_File_Ada (Filename, Elab_Order);
2224 end Gen_Output_File;
2225
2226 -------------------------
2227 -- Gen_Output_File_Ada --
2228 -------------------------
2229
2230 procedure Gen_Output_File_Ada
2231 (Filename : String; Elab_Order : Unit_Id_Array)
2232 is
2233 Ada_Main : constant String := Get_Ada_Main_Name;
2234 -- Name to be used for generated Ada main program. See the body of
2235 -- function Get_Ada_Main_Name for details on the form of the name.
2236
2237 Needs_Library_Finalization : constant Boolean :=
2238 not Configurable_Run_Time_On_Target
2239 and then Has_Finalizer (Elab_Order);
2240 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2241 -- non-terminating, so we do not want finalization.
2242
2243 Bfiles : Name_Id;
2244 -- Name of generated bind file (spec)
2245
2246 Bfileb : Name_Id;
2247 -- Name of generated bind file (body)
2248
2249 begin
2250 -- Create spec first
2251
2252 Create_Binder_Output (Filename, 's', Bfiles);
2253
2254 -- We always compile the binder file in Ada 95 mode so that we properly
2255 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2256 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2257
2258 WBI ("pragma Warnings (Off);");
2259 WBI ("pragma Ada_95;");
2260
2261 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2262 -- then we need to make sure that the binder program is compiled with
2263 -- the same restriction, so that no exception tables are generated.
2264
2265 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2266 WBI ("pragma Restrictions (No_Exception_Handlers);");
2267 end if;
2268
2269 -- Same processing for Restrictions (No_Exception_Propagation)
2270
2271 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2272 WBI ("pragma Restrictions (No_Exception_Propagation);");
2273 end if;
2274
2275 -- Same processing for pragma No_Run_Time
2276
2277 if No_Run_Time_Mode then
2278 WBI ("pragma No_Run_Time;");
2279 end if;
2280
2281 -- Generate with of System so we can reference System.Address
2282
2283 WBI ("with System;");
2284
2285 -- Generate with of System.Initialize_Scalars if active
2286
2287 if Initialize_Scalars_Used then
2288 WBI ("with System.Scalar_Values;");
2289 end if;
2290
2291 -- Generate withs of System.Secondary_Stack and System.Parameters to
2292 -- allow the generation of the default-sized secondary stack pool.
2293
2294 if Sec_Stack_Used then
2295 WBI ("with System.Parameters;");
2296 WBI ("with System.Secondary_Stack;");
2297 end if;
2298
2299 Resolve_Binder_Options (Elab_Order);
2300
2301 -- Generate standard with's
2302
2303 if not Suppress_Standard_Library_On_Target then
2304 if CodePeer_Mode then
2305 WBI ("with System.Standard_Library;");
2306 end if;
2307 end if;
2308
2309 WBI ("package " & Ada_Main & " is");
2310
2311 -- Main program case
2312
2313 if Bind_Main_Program then
2314 -- Generate argc/argv stuff unless suppressed
2315
2316 if Command_Line_Args_On_Target
2317 or not Configurable_Run_Time_On_Target
2318 then
2319 WBI ("");
2320 WBI (" gnat_argc : Integer;");
2321 WBI (" gnat_argv : System.Address;");
2322 WBI (" gnat_envp : System.Address;");
2323
2324 -- If the standard library is not suppressed, these variables
2325 -- are in the run-time data area for easy run time access.
2326
2327 if not Suppress_Standard_Library_On_Target then
2328 WBI ("");
2329 WBI (" pragma Import (C, gnat_argc);");
2330 WBI (" pragma Import (C, gnat_argv);");
2331 WBI (" pragma Import (C, gnat_envp);");
2332 end if;
2333 end if;
2334
2335 -- Define exit status. Again in normal mode, this is in the run-time
2336 -- library, and is initialized there, but in the configurable
2337 -- run-time case, the variable is declared and initialized in this
2338 -- file.
2339
2340 WBI ("");
2341
2342 if Configurable_Run_Time_Mode then
2343 if Exit_Status_Supported_On_Target then
2344 WBI (" gnat_exit_status : Integer := 0;");
2345 end if;
2346
2347 else
2348 WBI (" gnat_exit_status : Integer;");
2349 WBI (" pragma Import (C, gnat_exit_status);");
2350 end if;
2351
2352 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2353 -- the main program. Otherwise, it can lead under some circumstances
2354 -- to a symbol duplication during the link (for instance when a C
2355 -- program uses two Ada libraries). Also zero terminate the string
2356 -- so that its end can be found reliably at run time.
2357
2358 WBI ("");
2359 WBI (" GNAT_Version : constant String :=");
2360 WBI (" """ & Ver_Prefix &
2361 Gnat_Version_String &
2362 """ & ASCII.NUL;");
2363 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2364
2365 WBI ("");
2366 Set_String (" Ada_Main_Program_Name : constant String := """);
2367 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2368
2369 Set_Main_Program_Name;
2370 Set_String (""" & ASCII.NUL;");
2371
2372 Write_Statement_Buffer;
2373
2374 WBI
2375 (" pragma Export (C, Ada_Main_Program_Name, " &
2376 """__gnat_ada_main_program_name"");");
2377 end if;
2378
2379 WBI ("");
2380 WBI (" procedure " & Ada_Init_Name.all & ";");
2381 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2382 Ada_Init_Name.all & """);");
2383
2384 -- If -a has been specified use pragma Linker_Constructor for the init
2385 -- procedure and pragma Linker_Destructor for the final procedure.
2386
2387 if Use_Pragma_Linker_Constructor then
2388 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2389 end if;
2390
2391 if not Cumulative_Restrictions.Set (No_Finalization) then
2392 WBI ("");
2393 WBI (" procedure " & Ada_Final_Name.all & ";");
2394 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2395 Ada_Final_Name.all & """);");
2396
2397 if Use_Pragma_Linker_Constructor then
2398 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2399 end if;
2400 end if;
2401
2402 if Bind_Main_Program then
2403
2404 WBI ("");
2405
2406 if Exit_Status_Supported_On_Target then
2407 Set_String (" function ");
2408 else
2409 Set_String (" procedure ");
2410 end if;
2411
2412 Set_String (Get_Main_Name);
2413
2414 -- Generate argument list if present
2415
2416 if Command_Line_Args_On_Target then
2417 Write_Statement_Buffer;
2418 WBI (" (argc : Integer;");
2419 WBI (" argv : System.Address;");
2420 Set_String
2421 (" envp : System.Address)");
2422
2423 if Exit_Status_Supported_On_Target then
2424 Write_Statement_Buffer;
2425 WBI (" return Integer;");
2426 else
2427 Write_Statement_Buffer (";");
2428 end if;
2429
2430 else
2431 if Exit_Status_Supported_On_Target then
2432 Write_Statement_Buffer (" return Integer;");
2433 else
2434 Write_Statement_Buffer (";");
2435 end if;
2436 end if;
2437
2438 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2439 Get_Main_Name & """);");
2440 end if;
2441
2442 -- Generate version numbers for units, only if needed. Be very safe on
2443 -- the condition.
2444
2445 if not Configurable_Run_Time_On_Target
2446 or else System_Version_Control_Used
2447 or else not Bind_Main_Program
2448 then
2449 Gen_Versions;
2450 end if;
2451
2452 Gen_Elab_Order (Elab_Order);
2453
2454 -- Spec is complete
2455
2456 WBI ("");
2457 WBI ("end " & Ada_Main & ";");
2458 Close_Binder_Output;
2459
2460 -- Prepare to write body
2461
2462 Create_Binder_Output (Filename, 'b', Bfileb);
2463
2464 -- We always compile the binder file in Ada 95 mode so that we properly
2465 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2466 -- of the Ada 2005/2012 constructs are needed by the binder file.
2467
2468 WBI ("pragma Warnings (Off);");
2469 WBI ("pragma Ada_95;");
2470
2471 -- Output Source_File_Name pragmas which look like
2472
2473 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2474 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2475
2476 -- where sss/bbb are the spec/body file names respectively
2477
2478 Get_Name_String (Bfiles);
2479 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2480
2481 WBI ("pragma Source_File_Name (" &
2482 Ada_Main &
2483 ", Spec_File_Name => """ &
2484 Name_Buffer (1 .. Name_Len + 3));
2485
2486 Get_Name_String (Bfileb);
2487 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2488
2489 WBI ("pragma Source_File_Name (" &
2490 Ada_Main &
2491 ", Body_File_Name => """ &
2492 Name_Buffer (1 .. Name_Len + 3));
2493
2494 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2495 -- versions of the compiler which have overflow checks on by default.
2496 -- We do not want overflow checking enabled for the increments of the
2497 -- elaboration variables (since this can cause an unwanted reference to
2498 -- the last chance exception handler for limited run-times).
2499
2500 WBI ("pragma Suppress (Overflow_Check);");
2501
2502 -- Generate with of System.Restrictions to initialize
2503 -- Run_Time_Restrictions.
2504
2505 if System_Restrictions_Used
2506 and not Suppress_Standard_Library_On_Target
2507 then
2508 WBI ("");
2509 WBI ("with System.Restrictions;");
2510 end if;
2511
2512 -- Generate with of Ada.Exceptions if needs library finalization
2513
2514 if Needs_Library_Finalization then
2515 WBI ("with Ada.Exceptions;");
2516 end if;
2517
2518 -- Generate with of System.Elaboration_Allocators if the restriction
2519 -- No_Standard_Allocators_After_Elaboration was present.
2520
2521 if Cumulative_Restrictions.Set
2522 (No_Standard_Allocators_After_Elaboration)
2523 then
2524 WBI ("with System.Elaboration_Allocators;");
2525 end if;
2526
2527 -- Generate start of package body
2528
2529 WBI ("");
2530 WBI ("package body " & Ada_Main & " is");
2531 WBI ("");
2532
2533 -- Generate externals for elaboration entities
2534
2535 Gen_Elab_Externals (Elab_Order);
2536
2537 -- Generate default-sized secondary stacks pool. At least one stack is
2538 -- created and assigned to the environment task if secondary stacks are
2539 -- used by the program.
2540
2541 if Sec_Stack_Used then
2542 Set_String (" Sec_Default_Sized_Stacks");
2543 Set_String (" : array (1 .. ");
2544 Set_Int (Num_Sec_Stacks);
2545 Set_String (") of aliased System.Secondary_Stack.SS_Stack (");
2546
2547 if Opt.Default_Sec_Stack_Size /= No_Stack_Size then
2548 Set_Int (Opt.Default_Sec_Stack_Size);
2549 else
2550 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
2551 end if;
2552
2553 Set_String (");");
2554 Write_Statement_Buffer;
2555 WBI ("");
2556 end if;
2557
2558 -- Generate reference
2559
2560 if not CodePeer_Mode then
2561 if not Suppress_Standard_Library_On_Target then
2562
2563 -- Generate Priority_Specific_Dispatching pragma string
2564
2565 Set_String
2566 (" Local_Priority_Specific_Dispatching : " &
2567 "constant String := """);
2568
2569 for J in 0 .. PSD_Pragma_Settings.Last loop
2570 Set_Char (PSD_Pragma_Settings.Table (J));
2571 end loop;
2572
2573 Set_String (""";");
2574 Write_Statement_Buffer;
2575
2576 -- Generate Interrupt_State pragma string
2577
2578 Set_String (" Local_Interrupt_States : constant String := """);
2579
2580 for J in 0 .. IS_Pragma_Settings.Last loop
2581 Set_Char (IS_Pragma_Settings.Table (J));
2582 end loop;
2583
2584 Set_String (""";");
2585 Write_Statement_Buffer;
2586 WBI ("");
2587 end if;
2588
2589 if not Suppress_Standard_Library_On_Target then
2590
2591 -- The B.1(39) implementation advice says that the adainit and
2592 -- adafinal routines should be idempotent. Generate a flag to
2593 -- ensure that. This is not needed if we are suppressing the
2594 -- standard library since it would never be referenced.
2595
2596 WBI (" Is_Elaborated : Boolean := False;");
2597
2598 -- Generate bind environment string
2599
2600 Gen_Bind_Env_String;
2601 end if;
2602
2603 WBI ("");
2604 end if;
2605
2606 -- Generate the adafinal routine unless there is no finalization to do
2607
2608 if not Cumulative_Restrictions.Set (No_Finalization) then
2609 if Needs_Library_Finalization then
2610 Gen_Finalize_Library (Elab_Order);
2611 end if;
2612
2613 Gen_Adafinal;
2614 end if;
2615
2616 Gen_Adainit (Elab_Order);
2617
2618 if Bind_Main_Program then
2619 Gen_Main;
2620 end if;
2621
2622 -- Output object file list and the Ada body is complete
2623
2624 Gen_Object_Files_Options (Elab_Order);
2625
2626 WBI ("");
2627 WBI ("end " & Ada_Main & ";");
2628
2629 Close_Binder_Output;
2630 end Gen_Output_File_Ada;
2631
2632 ----------------------
2633 -- Gen_Restrictions --
2634 ----------------------
2635
2636 procedure Gen_Restrictions is
2637 Count : Integer;
2638
2639 begin
2640 if Suppress_Standard_Library_On_Target
2641 or not System_Restrictions_Used
2642 then
2643 return;
2644 end if;
2645
2646 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2647 WBI (" (Set =>");
2648 Set_String (" (");
2649
2650 Count := 0;
2651
2652 for J in Cumulative_Restrictions.Set'Range loop
2653 Set_Boolean (Cumulative_Restrictions.Set (J));
2654 Set_String (", ");
2655 Count := Count + 1;
2656
2657 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2658 Write_Statement_Buffer;
2659 Set_String (" ");
2660 Count := 0;
2661 end if;
2662 end loop;
2663
2664 Set_String_Replace ("),");
2665 Write_Statement_Buffer;
2666 Set_String (" Value => (");
2667
2668 for J in Cumulative_Restrictions.Value'Range loop
2669 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2670 Set_String (", ");
2671 end loop;
2672
2673 Set_String_Replace ("),");
2674 Write_Statement_Buffer;
2675 WBI (" Violated =>");
2676 Set_String (" (");
2677 Count := 0;
2678
2679 for J in Cumulative_Restrictions.Violated'Range loop
2680 Set_Boolean (Cumulative_Restrictions.Violated (J));
2681 Set_String (", ");
2682 Count := Count + 1;
2683
2684 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2685 Write_Statement_Buffer;
2686 Set_String (" ");
2687 Count := 0;
2688 end if;
2689 end loop;
2690
2691 Set_String_Replace ("),");
2692 Write_Statement_Buffer;
2693 Set_String (" Count => (");
2694
2695 for J in Cumulative_Restrictions.Count'Range loop
2696 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2697 Set_String (", ");
2698 end loop;
2699
2700 Set_String_Replace ("),");
2701 Write_Statement_Buffer;
2702 Set_String (" Unknown => (");
2703
2704 for J in Cumulative_Restrictions.Unknown'Range loop
2705 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2706 Set_String (", ");
2707 end loop;
2708
2709 Set_String_Replace ("))");
2710 Set_String (";");
2711 Write_Statement_Buffer;
2712 end Gen_Restrictions;
2713
2714 ------------------
2715 -- Gen_Versions --
2716 ------------------
2717
2718 -- This routine generates lines such as:
2719
2720 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2721 -- pragma Export (C, unnnnn, unam);
2722
2723 -- for each unit, where unam is the unit name suffixed by either B or S for
2724 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2725 -- the version number, and nnnnn is a 5-digits serial number.
2726
2727 procedure Gen_Versions is
2728 Ubuf : String (1 .. 6) := "u00000";
2729
2730 procedure Increment_Ubuf;
2731 -- Little procedure to increment the serial number
2732
2733 --------------------
2734 -- Increment_Ubuf --
2735 --------------------
2736
2737 procedure Increment_Ubuf is
2738 begin
2739 for J in reverse Ubuf'Range loop
2740 Ubuf (J) := Character'Succ (Ubuf (J));
2741 exit when Ubuf (J) <= '9';
2742 Ubuf (J) := '0';
2743 end loop;
2744 end Increment_Ubuf;
2745
2746 -- Start of processing for Gen_Versions
2747
2748 begin
2749 WBI ("");
2750
2751 WBI (" type Version_32 is mod 2 ** 32;");
2752 for U in Units.First .. Units.Last loop
2753 if not Units.Table (U).SAL_Interface
2754 and then (not Bind_For_Library
2755 or else Units.Table (U).Directly_Scanned)
2756 then
2757 Increment_Ubuf;
2758 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2759 Units.Table (U).Version & "#;");
2760 Set_String (" pragma Export (C, ");
2761 Set_String (Ubuf);
2762 Set_String (", """);
2763
2764 Get_Name_String (Units.Table (U).Uname);
2765
2766 for K in 1 .. Name_Len loop
2767 if Name_Buffer (K) = '.' then
2768 Set_Char ('_');
2769 Set_Char ('_');
2770
2771 elsif Name_Buffer (K) = '%' then
2772 exit;
2773
2774 else
2775 Set_Char (Name_Buffer (K));
2776 end if;
2777 end loop;
2778
2779 if Name_Buffer (Name_Len) = 's' then
2780 Set_Char ('S');
2781 else
2782 Set_Char ('B');
2783 end if;
2784
2785 Set_String (""");");
2786 Write_Statement_Buffer;
2787 end if;
2788 end loop;
2789 end Gen_Versions;
2790
2791 ------------------------
2792 -- Get_Main_Unit_Name --
2793 ------------------------
2794
2795 function Get_Main_Unit_Name (S : String) return String is
2796 Result : String := S;
2797
2798 begin
2799 for J in S'Range loop
2800 if Result (J) = '.' then
2801 Result (J) := '_';
2802 end if;
2803 end loop;
2804
2805 return Result;
2806 end Get_Main_Unit_Name;
2807
2808 -----------------------
2809 -- Get_Ada_Main_Name --
2810 -----------------------
2811
2812 function Get_Ada_Main_Name return String is
2813 Suffix : constant String := "_00";
2814 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2815 Opt.Ada_Main_Name.all & Suffix;
2816 Nlen : Natural;
2817
2818 begin
2819 -- For CodePeer, we want reproducible names (independent of other mains
2820 -- that may or may not be present) that don't collide when analyzing
2821 -- multiple mains and which are easily recognizable as "ada_main" names.
2822
2823 if CodePeer_Mode then
2824 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2825
2826 return
2827 "ada_main_for_" &
2828 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2829 end if;
2830
2831 -- This loop tries the following possibilities in order
2832 -- <Ada_Main>
2833 -- <Ada_Main>_01
2834 -- <Ada_Main>_02
2835 -- ..
2836 -- <Ada_Main>_99
2837 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2838 -- it is set to 'ada_main'.
2839
2840 for J in 0 .. 99 loop
2841 if J = 0 then
2842 Nlen := Name'Length - Suffix'Length;
2843 else
2844 Nlen := Name'Length;
2845 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2846 Name (Name'Last - 1) :=
2847 Character'Val (J / 10 + Character'Pos ('0'));
2848 end if;
2849
2850 for K in ALIs.First .. ALIs.Last loop
2851 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2852
2853 -- Get unit name, removing %b or %e at end
2854
2855 Get_Name_String (Units.Table (L).Uname);
2856 Name_Len := Name_Len - 2;
2857
2858 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2859 goto Continue;
2860 end if;
2861 end loop;
2862 end loop;
2863
2864 return Name (1 .. Nlen);
2865
2866 <<Continue>>
2867 null;
2868 end loop;
2869
2870 -- If we fall through, just use a peculiar unlikely name
2871
2872 return ("Qwertyuiop");
2873 end Get_Ada_Main_Name;
2874
2875 -------------------
2876 -- Get_Main_Name --
2877 -------------------
2878
2879 function Get_Main_Name return String is
2880 begin
2881 -- Explicit name given with -M switch
2882
2883 if Bind_Alternate_Main_Name then
2884 return Alternate_Main_Name.all;
2885
2886 -- Case of main program name to be used directly
2887
2888 elsif Use_Ada_Main_Program_Name_On_Target then
2889
2890 -- Get main program name
2891
2892 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2893
2894 -- If this is a child name, return only the name of the child, since
2895 -- we can't have dots in a nested program name. Note that we do not
2896 -- include the %b at the end of the unit name.
2897
2898 for J in reverse 1 .. Name_Len - 2 loop
2899 if J = 1 or else Name_Buffer (J - 1) = '.' then
2900 return Name_Buffer (J .. Name_Len - 2);
2901 end if;
2902 end loop;
2903
2904 raise Program_Error; -- impossible exit
2905
2906 -- Case where "main" is to be used as default
2907
2908 else
2909 return "main";
2910 end if;
2911 end Get_Main_Name;
2912
2913 ---------------------
2914 -- Get_WC_Encoding --
2915 ---------------------
2916
2917 function Get_WC_Encoding return Character is
2918 begin
2919 -- If encoding method specified by -W switch, then return it
2920
2921 if Wide_Character_Encoding_Method_Specified then
2922 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2923
2924 -- If no main program, and not specified, set brackets, we really have
2925 -- no better choice. If some other encoding is required when there is
2926 -- no main, it must be set explicitly using -Wx.
2927
2928 -- Note: if the ALI file always passed the wide character encoding of
2929 -- every file, then we could use the encoding of the initial specified
2930 -- file, but this information is passed only for potential main
2931 -- programs. We could fix this sometime, but it is a very minor point
2932 -- (wide character default encoding for [Wide_[Wide_]]Text_IO when there
2933 -- is no main program).
2934
2935 elsif No_Main_Subprogram then
2936 return 'b';
2937
2938 -- Otherwise if there is a main program, take encoding from it
2939
2940 else
2941 return ALIs.Table (ALIs.First).WC_Encoding;
2942 end if;
2943 end Get_WC_Encoding;
2944
2945 -------------------
2946 -- Has_Finalizer --
2947 -------------------
2948
2949 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean is
2950 U : Unit_Record;
2951 Unum : Unit_Id;
2952
2953 begin
2954 for E in reverse Elab_Order'Range loop
2955 Unum := Elab_Order (E);
2956 U := Units.Table (Unum);
2957
2958 -- We are only interested in non-generic packages
2959
2960 if U.Unit_Kind = 'p'
2961 and then U.Has_Finalizer
2962 and then not U.Is_Generic
2963 and then not U.No_Elab
2964 then
2965 return True;
2966 end if;
2967 end loop;
2968
2969 return False;
2970 end Has_Finalizer;
2971
2972 ----------
2973 -- Hash --
2974 ----------
2975
2976 function Hash (Nam : Name_Id) return Header_Num is
2977 begin
2978 return Int (Nam - Names_Low_Bound) rem Header_Num'Last;
2979 end Hash;
2980
2981 ----------------------
2982 -- Lt_Linker_Option --
2983 ----------------------
2984
2985 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean is
2986 begin
2987 -- Sort internal files last
2988
2989 if Linker_Options.Table (Op1).Internal_File
2990 /=
2991 Linker_Options.Table (Op2).Internal_File
2992 then
2993 -- Note: following test uses False < True
2994
2995 return Linker_Options.Table (Op1).Internal_File
2996 <
2997 Linker_Options.Table (Op2).Internal_File;
2998
2999 -- If both internal or both non-internal, sort according to the
3000 -- elaboration position. A unit that is elaborated later should come
3001 -- earlier in the linker options list.
3002
3003 else
3004 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3005 >
3006 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3007 end if;
3008 end Lt_Linker_Option;
3009
3010 ------------------------
3011 -- Move_Linker_Option --
3012 ------------------------
3013
3014 procedure Move_Linker_Option (From : Natural; To : Natural) is
3015 begin
3016 Linker_Options.Table (To) := Linker_Options.Table (From);
3017 end Move_Linker_Option;
3018
3019 ----------------------------
3020 -- Resolve_Binder_Options --
3021 ----------------------------
3022
3023 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array) is
3024 procedure Check_Package (Var : in out Boolean; Name : String);
3025 -- Set Var to true iff the current identifier in Namet is Name. Do
3026 -- nothing if it doesn't match. This procedure is just a helper to
3027 -- avoid explicitly dealing with length.
3028
3029 -------------------
3030 -- Check_Package --
3031 -------------------
3032
3033 procedure Check_Package (Var : in out Boolean; Name : String) is
3034 begin
3035 if Name_Len = Name'Length
3036 and then Name_Buffer (1 .. Name_Len) = Name
3037 then
3038 Var := True;
3039 end if;
3040 end Check_Package;
3041
3042 -- Start of processing for Resolve_Binder_Options
3043
3044 begin
3045 for E in Elab_Order'Range loop
3046 Get_Name_String (Units.Table (Elab_Order (E)).Uname);
3047
3048 -- This is not a perfect approach, but is the current protocol
3049 -- between the run-time and the binder to indicate that tasking is
3050 -- used: System.OS_Interface should always be used by any tasking
3051 -- application.
3052
3053 Check_Package (With_GNARL, "system.os_interface%s");
3054
3055 -- Ditto for the use of restricted tasking
3056
3057 Check_Package
3058 (System_Tasking_Restricted_Stages_Used,
3059 "system.tasking.restricted.stages%s");
3060
3061 -- Ditto for the use of interrupts
3062
3063 Check_Package (System_Interrupts_Used, "system.interrupts%s");
3064
3065 -- Ditto for the use of dispatching domains
3066
3067 Check_Package
3068 (Dispatching_Domains_Used,
3069 "system.multiprocessors.dispatching_domains%s");
3070
3071 -- Ditto for the use of restrictions
3072
3073 Check_Package (System_Restrictions_Used, "system.restrictions%s");
3074
3075 -- Ditto for the use of System.Secondary_Stack
3076
3077 Check_Package
3078 (System_Secondary_Stack_Used, "system.secondary_stack%s");
3079
3080 -- Ditto for use of an SMP bareboard runtime
3081
3082 Check_Package (System_BB_CPU_Primitives_Multiprocessors_Used,
3083 "system.bb.cpu_primitives.multiprocessors%s");
3084
3085 -- Ditto for System.Version_Control, which is used for Version and
3086 -- Body_Version attributes.
3087
3088 Check_Package (System_Version_Control_Used,
3089 "system.version_control%s");
3090 end loop;
3091 end Resolve_Binder_Options;
3092
3093 ------------------
3094 -- Set_Bind_Env --
3095 ------------------
3096
3097 procedure Set_Bind_Env (Key, Value : String) is
3098 begin
3099 -- The lengths of Key and Value are stored as single bytes
3100
3101 if Key'Length > 255 then
3102 Osint.Fail ("bind environment key """ & Key & """ too long");
3103 end if;
3104
3105 if Value'Length > 255 then
3106 Osint.Fail ("bind environment value """ & Value & """ too long");
3107 end if;
3108
3109 Bind_Environment.Set (Name_Find (Key), Name_Find (Value));
3110 end Set_Bind_Env;
3111
3112 -----------------
3113 -- Set_Boolean --
3114 -----------------
3115
3116 procedure Set_Boolean (B : Boolean) is
3117 False_Str : constant String := "False";
3118 True_Str : constant String := "True";
3119
3120 begin
3121 if B then
3122 Statement_Buffer (Stm_Last + 1 .. Stm_Last + True_Str'Length) :=
3123 True_Str;
3124 Stm_Last := Stm_Last + True_Str'Length;
3125 else
3126 Statement_Buffer (Stm_Last + 1 .. Stm_Last + False_Str'Length) :=
3127 False_Str;
3128 Stm_Last := Stm_Last + False_Str'Length;
3129 end if;
3130 end Set_Boolean;
3131
3132 --------------
3133 -- Set_Char --
3134 --------------
3135
3136 procedure Set_Char (C : Character) is
3137 begin
3138 Stm_Last := Stm_Last + 1;
3139 Statement_Buffer (Stm_Last) := C;
3140 end Set_Char;
3141
3142 -------------
3143 -- Set_Int --
3144 -------------
3145
3146 procedure Set_Int (N : Int) is
3147 begin
3148 if N < 0 then
3149 Set_String ("-");
3150 Set_Int (-N);
3151
3152 else
3153 if N > 9 then
3154 Set_Int (N / 10);
3155 end if;
3156
3157 Stm_Last := Stm_Last + 1;
3158 Statement_Buffer (Stm_Last) :=
3159 Character'Val (N mod 10 + Character'Pos ('0'));
3160 end if;
3161 end Set_Int;
3162
3163 -------------------------
3164 -- Set_IS_Pragma_Table --
3165 -------------------------
3166
3167 procedure Set_IS_Pragma_Table is
3168 begin
3169 for F in ALIs.First .. ALIs.Last loop
3170 for K in ALIs.Table (F).First_Interrupt_State ..
3171 ALIs.Table (F).Last_Interrupt_State
3172 loop
3173 declare
3174 Inum : constant Int :=
3175 Interrupt_States.Table (K).Interrupt_Id;
3176 Stat : constant Character :=
3177 Interrupt_States.Table (K).Interrupt_State;
3178
3179 begin
3180 while IS_Pragma_Settings.Last < Inum loop
3181 IS_Pragma_Settings.Append ('n');
3182 end loop;
3183
3184 IS_Pragma_Settings.Table (Inum) := Stat;
3185 end;
3186 end loop;
3187 end loop;
3188 end Set_IS_Pragma_Table;
3189
3190 ---------------------------
3191 -- Set_Main_Program_Name --
3192 ---------------------------
3193
3194 procedure Set_Main_Program_Name is
3195 begin
3196 -- Note that name has %b on the end which we ignore
3197
3198 -- First we output the initial _ada_ since we know that the main program
3199 -- is a library level subprogram.
3200
3201 Set_String ("_ada_");
3202
3203 -- Copy name, changing dots to double underscores
3204
3205 for J in 1 .. Name_Len - 2 loop
3206 if Name_Buffer (J) = '.' then
3207 Set_String ("__");
3208 else
3209 Set_Char (Name_Buffer (J));
3210 end if;
3211 end loop;
3212 end Set_Main_Program_Name;
3213
3214 ---------------------
3215 -- Set_Name_Buffer --
3216 ---------------------
3217
3218 procedure Set_Name_Buffer is
3219 begin
3220 for J in 1 .. Name_Len loop
3221 Set_Char (Name_Buffer (J));
3222 end loop;
3223 end Set_Name_Buffer;
3224
3225 -------------------------
3226 -- Set_PSD_Pragma_Table --
3227 -------------------------
3228
3229 procedure Set_PSD_Pragma_Table is
3230 begin
3231 for F in ALIs.First .. ALIs.Last loop
3232 for K in ALIs.Table (F).First_Specific_Dispatching ..
3233 ALIs.Table (F).Last_Specific_Dispatching
3234 loop
3235 declare
3236 DTK : Specific_Dispatching_Record
3237 renames Specific_Dispatching.Table (K);
3238
3239 begin
3240 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3241 PSD_Pragma_Settings.Append ('F');
3242 end loop;
3243
3244 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3245 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3246 end loop;
3247 end;
3248 end loop;
3249 end loop;
3250 end Set_PSD_Pragma_Table;
3251
3252 ----------------
3253 -- Set_String --
3254 ----------------
3255
3256 procedure Set_String (S : String) is
3257 begin
3258 Statement_Buffer (Stm_Last + 1 .. Stm_Last + S'Length) := S;
3259 Stm_Last := Stm_Last + S'Length;
3260 end Set_String;
3261
3262 ------------------------
3263 -- Set_String_Replace --
3264 ------------------------
3265
3266 procedure Set_String_Replace (S : String) is
3267 begin
3268 Statement_Buffer (Stm_Last - S'Length + 1 .. Stm_Last) := S;
3269 end Set_String_Replace;
3270
3271 -------------------
3272 -- Set_Unit_Name --
3273 -------------------
3274
3275 procedure Set_Unit_Name is
3276 begin
3277 for J in 1 .. Name_Len - 2 loop
3278 if Name_Buffer (J) = '.' then
3279 Set_String ("__");
3280 else
3281 Set_Char (Name_Buffer (J));
3282 end if;
3283 end loop;
3284 end Set_Unit_Name;
3285
3286 ---------------------
3287 -- Set_Unit_Number --
3288 ---------------------
3289
3290 procedure Set_Unit_Number (U : Unit_Id) is
3291 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3292 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3293
3294 begin
3295 if Num_Units >= 10 and then Unum < 10 then
3296 Set_Char ('0');
3297 end if;
3298
3299 if Num_Units >= 100 and then Unum < 100 then
3300 Set_Char ('0');
3301 end if;
3302
3303 Set_Int (Unum);
3304 end Set_Unit_Number;
3305
3306 ---------------------
3307 -- Write_Bind_Line --
3308 ---------------------
3309
3310 procedure Write_Bind_Line (S : String) is
3311 begin
3312 -- Need to strip trailing LF from S
3313
3314 WBI (S (S'First .. S'Last - 1));
3315 end Write_Bind_Line;
3316
3317 ----------------------------
3318 -- Write_Statement_Buffer --
3319 ----------------------------
3320
3321 procedure Write_Statement_Buffer is
3322 begin
3323 WBI (Statement_Buffer (1 .. Stm_Last));
3324 Stm_Last := 0;
3325 end Write_Statement_Buffer;
3326
3327 procedure Write_Statement_Buffer (S : String) is
3328 begin
3329 Set_String (S);
3330 Write_Statement_Buffer;
3331 end Write_Statement_Buffer;
3332
3333 end Bindgen;