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