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