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