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