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