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