eeec4708bc0dae4ee115606f573a1185970bc69b
[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-2011, 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_DECGNAT : Boolean := False;
56 -- Flag which indicates whether the program uses the DECGNAT library
57 -- (presence of the unit DEC).
58
59 With_GNARL : Boolean := False;
60 -- Flag which indicates whether the program uses the GNARL library
61 -- (presence of the unit System.OS_Interface)
62
63 Num_Elab_Calls : Nat := 0;
64 -- Number of generated calls to elaboration routines
65
66 System_Restrictions_Used : Boolean;
67 -- Flag indicating whether the unit System.Restrictions is in the closure
68 -- of the partition. This is set by Check_System_Restrictions_Used, and
69 -- is used to determine whether or not to initialize the restrictions
70 -- information in the body of the binder generated file (we do not want
71 -- to do this unconditionally, since it drags in the System.Restrictions
72 -- unit unconditionally, which is unpleasand, especially for ZFP etc.)
73
74 Lib_Final_Built : Boolean := False;
75
76 ----------------------------------
77 -- Interface_State Pragma Table --
78 ----------------------------------
79
80 -- This table assembles the interface state pragma information from
81 -- all the units in the partition. Note that Bcheck has already checked
82 -- that the information is consistent across units. The entries
83 -- in this table are n/u/r/s for not set/user/runtime/system.
84
85 package IS_Pragma_Settings is new Table.Table (
86 Table_Component_Type => Character,
87 Table_Index_Type => Int,
88 Table_Low_Bound => 0,
89 Table_Initial => 100,
90 Table_Increment => 200,
91 Table_Name => "IS_Pragma_Settings");
92
93 -- This table assembles the Priority_Specific_Dispatching pragma
94 -- information from all the units in the partition. Note that Bcheck has
95 -- already checked that the information is consistent across units.
96 -- The entries in this table are the upper case first character of the
97 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
98
99 package PSD_Pragma_Settings is new Table.Table (
100 Table_Component_Type => Character,
101 Table_Index_Type => Int,
102 Table_Low_Bound => 0,
103 Table_Initial => 100,
104 Table_Increment => 200,
105 Table_Name => "PSD_Pragma_Settings");
106
107 ----------------------
108 -- Run-Time Globals --
109 ----------------------
110
111 -- This section documents the global variables that set from the
112 -- generated binder file.
113
114 -- Main_Priority : Integer;
115 -- Time_Slice_Value : Integer;
116 -- Heap_Size : Natural;
117 -- WC_Encoding : Character;
118 -- Locking_Policy : Character;
119 -- Queuing_Policy : Character;
120 -- Task_Dispatching_Policy : Character;
121 -- Priority_Specific_Dispatching : System.Address;
122 -- Num_Specific_Dispatching : Integer;
123 -- Restrictions : System.Address;
124 -- Interrupt_States : System.Address;
125 -- Num_Interrupt_States : Integer;
126 -- Unreserve_All_Interrupts : Integer;
127 -- Exception_Tracebacks : Integer;
128 -- Zero_Cost_Exceptions : Integer;
129 -- Detect_Blocking : Integer;
130 -- Default_Stack_Size : Integer;
131 -- Leap_Seconds_Support : Integer;
132 -- Main_CPU : Integer;
133
134 -- Main_Priority is the priority value set by pragma Priority in the main
135 -- program. If no such pragma is present, the value is -1.
136
137 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
138 -- main program, or by the use of a -Tnnn parameter for the binder (if both
139 -- are present, the binder value overrides). The value is in milliseconds.
140 -- A value of zero indicates that time slicing should be suppressed. If no
141 -- pragma is present, and no -T switch was used, the value is -1.
142
143 -- Heap_Size is the heap to use for memory allocations set by use of a
144 -- -Hnn parameter for the binder or by the GNAT$NO_MALLOC_64 logical.
145 -- Valid values are 32 and 64. This switch is only effective on VMS.
146
147 -- WC_Encoding shows the wide character encoding method used for the main
148 -- program. This is one of the encoding letters defined in
149 -- System.WCh_Con.WC_Encoding_Letters.
150
151 -- Locking_Policy is a space if no locking policy was specified for the
152 -- partition. If a locking policy was specified, the value is the upper
153 -- case first character of the locking policy name, for example, 'C' for
154 -- Ceiling_Locking.
155
156 -- Queuing_Policy is a space if no queuing policy was specified for the
157 -- partition. If a queuing policy was specified, the value is the upper
158 -- case first character of the queuing policy name for example, 'F' for
159 -- FIFO_Queuing.
160
161 -- Task_Dispatching_Policy is a space if no task dispatching policy was
162 -- specified for the partition. If a task dispatching policy was specified,
163 -- the value is the upper case first character of the policy name, e.g. 'F'
164 -- for FIFO_Within_Priorities.
165
166 -- Priority_Specific_Dispatching is the address of a string used to store
167 -- the task dispatching policy specified for the different priorities in
168 -- the partition. The length of this string is determined by the last
169 -- priority for which such a pragma applies (the string will be a null
170 -- string if no specific dispatching policies were used). If pragma were
171 -- present, the entries apply to the priorities in sequence from the first
172 -- priority. The value stored is the upper case first character of the
173 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
174 -- for those priority ranges not specified.
175
176 -- Num_Specific_Dispatching is the length of the
177 -- Priority_Specific_Dispatching string. It will be set to zero if no
178 -- Priority_Specific_Dispatching pragmas are present.
179
180 -- Restrictions is the address of a null-terminated string specifying the
181 -- restrictions information for the partition. The format is identical to
182 -- that of the parameter string found on R lines in ali files (see Lib.Writ
183 -- spec in lib-writ.ads for full details). The difference is that in this
184 -- context the values are the cumulative ones for the entire partition.
185
186 -- Interrupt_States is the address of a string used to specify the
187 -- cumulative results of Interrupt_State pragmas used in the partition.
188 -- The length of this string is determined by the last interrupt for which
189 -- such a pragma is given (the string will be a null string if no pragmas
190 -- were used). If pragma were present the entries apply to the interrupts
191 -- in sequence from the first interrupt, and are set to one of four
192 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
193 -- time, 's' for system, see description of Interrupt_State pragma for
194 -- further details.
195
196 -- Num_Interrupt_States is the length of the Interrupt_States string. It
197 -- will be set to zero if no Interrupt_State pragmas are present.
198
199 -- Unreserve_All_Interrupts is set to one if at least one unit in the
200 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
201
202 -- Exception_Tracebacks is set to one if the -E parameter was present
203 -- in the bind and to zero otherwise. Note that on some targets exception
204 -- tracebacks are provided by default, so a value of zero for this
205 -- parameter does not necessarily mean no trace backs are available.
206
207 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
208 -- this partition, and to zero if longjmp/setjmp exceptions are used.
209
210 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
211 -- not. A value of zero indicates that the pragma is not present, while a
212 -- value of 1 signals its presence in the partition.
213
214 -- Default_Stack_Size is the default stack size used when creating an Ada
215 -- task with no explicit Storage_Size clause.
216
217 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
218 -- disabled. A value of zero indicates that leap seconds are turned "off",
219 -- while a value of one signifies "on" status.
220
221 -- Main_CPU is the processor set by pragma CPU in the main program. If no
222 -- such pragma is present, the value is -1.
223
224 -----------------------
225 -- Local Subprograms --
226 -----------------------
227
228 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
229 -- Convenient shorthand used throughout
230
231 procedure Check_System_Restrictions_Used;
232 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
233 -- System.Restrictions is present in the partition, otherwise False).
234
235 procedure Gen_Adainit_Ada;
236 -- Generates the Adainit procedure (Ada code case)
237
238 procedure Gen_Adainit_C;
239 -- Generates the Adainit procedure (C code case)
240
241 procedure Gen_Adafinal_Ada;
242 -- Generate the Adafinal procedure (Ada code case)
243
244 procedure Gen_Adafinal_C;
245 -- Generate the Adafinal procedure (C code case)
246
247 procedure Gen_Elab_Calls_Ada;
248 -- Generate sequence of elaboration calls (Ada code case)
249
250 procedure Gen_Elab_Calls_C;
251 -- Generate sequence of elaboration calls (C code case)
252
253 procedure Gen_Elab_Order_Ada;
254 -- Generate comments showing elaboration order chosen (Ada code case)
255
256 procedure Gen_Elab_Order_C;
257 -- Generate comments showing elaboration order chosen (C code case)
258
259 procedure Gen_Elab_Defs_C;
260 -- Generate sequence of definitions for elaboration routines (C code case)
261
262 procedure Gen_Finalize_Library_Ada;
263 -- Generate a sequence of finalization calls to elaborated packages (Ada)
264
265 procedure Gen_Finalize_Library_C;
266 -- Generate a sequence of finalization calls to elaborated packages (C)
267
268 procedure Gen_Finalize_Library_Defs_C;
269 -- Generate a sequence of defininitions for package finalizers (C case)
270
271 procedure Gen_Main_Ada;
272 -- Generate procedure main (Ada code case)
273
274 procedure Gen_Main_C;
275 -- Generate main() procedure (C code case)
276
277 procedure Gen_Object_Files_Options;
278 -- Output comments containing a list of the full names of the object
279 -- files to be linked and the list of linker options supplied by
280 -- Linker_Options pragmas in the source. (C and Ada code case)
281
282 procedure Gen_Output_File_Ada (Filename : String);
283 -- Generate output file (Ada code case)
284
285 procedure Gen_Output_File_C (Filename : String);
286 -- Generate output file (C code case)
287
288 procedure Gen_Restrictions_Ada;
289 -- Generate initialization of restrictions variable (Ada code case)
290
291 procedure Gen_Restrictions_C;
292 -- Generate initialization of restrictions variable (C code case)
293
294 procedure Gen_Versions_Ada;
295 -- Output series of definitions for unit versions (Ada code case)
296
297 procedure Gen_Versions_C;
298 -- Output series of definitions for unit versions (C code case)
299
300 function Get_Ada_Main_Name return String;
301 -- This function is used in the Ada main output case to compute a usable
302 -- name for the generated main program. The normal main program name is
303 -- Ada_Main, but this won't work if the user has a unit with this name.
304 -- This function tries Ada_Main first, and if there is such a clash, then
305 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
306
307 function Get_Main_Unit_Name (S : String) return String;
308 -- Return the main unit name corresponding to S by replacing '.' with '_'
309
310 function Get_Main_Name return String;
311 -- This function is used in the Ada main output case to compute the
312 -- correct external main program. It is "main" by default, unless the
313 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
314 -- is the name of the Ada main name without the "_ada". This default
315 -- can be overridden explicitly using the -Mname binder switch.
316
317 function Get_WC_Encoding return Character;
318 -- Return wide character encoding method to set as WC_Encoding in output.
319 -- If -W has been used, returns the specified encoding, otherwise returns
320 -- the encoding method used for the main program source. If there is no
321 -- main program source (-z switch used), returns brackets ('b').
322
323 function Has_Finalizer return Boolean;
324 -- Determine whether the current unit has at least one library-level
325 -- finalizer.
326
327 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
328 -- Compare linker options, when sorting, first according to
329 -- Is_Internal_File (internal files come later) and then by
330 -- elaboration order position (latest to earliest).
331
332 procedure Move_Linker_Option (From : Natural; To : Natural);
333 -- Move routine for sorting linker options
334
335 procedure Resolve_Binder_Options;
336 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
337 -- since it tests for a package named "dec" which might cause a conflict
338 -- on non-VMS systems.
339
340 procedure Set_Char (C : Character);
341 -- Set given character in Statement_Buffer at the Last + 1 position
342 -- and increment Last by one to reflect the stored character.
343
344 procedure Set_Int (N : Int);
345 -- Set given value in decimal in Statement_Buffer with no spaces
346 -- starting at the Last + 1 position, and updating Last past the value.
347 -- A minus sign is output for a negative value.
348
349 procedure Set_Boolean (B : Boolean);
350 -- Set given boolean value in Statement_Buffer at the Last + 1 position
351 -- and update Last past the value.
352
353 procedure Set_IS_Pragma_Table;
354 -- Initializes contents of IS_Pragma_Settings table from ALI table
355
356 procedure Set_Main_Program_Name;
357 -- Given the main program name in Name_Buffer (length in Name_Len)
358 -- generate the name of the routine to be used in the call. The name
359 -- is generated starting at Last + 1, and Last is updated past it.
360
361 procedure Set_Name_Buffer;
362 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
363
364 procedure Set_PSD_Pragma_Table;
365 -- Initializes contents of PSD_Pragma_Settings table from ALI table
366
367 procedure Set_String (S : String);
368 -- Sets characters of given string in Statement_Buffer, starting at the
369 -- Last + 1 position, and updating last past the string value.
370
371 procedure Set_String_Replace (S : String);
372 -- Replaces the last S'Length characters in the Statement_Buffer with
373 -- the characters of S. The caller must ensure that these characters do
374 -- in fact exist in the Statement_Buffer.
375
376 type Qualification_Mode is (Dollar_Sign, Dot, Double_Underscores);
377
378 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores);
379 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
380 -- starting at the Last + 1 position and update Last past the value.
381 -- Depending on parameter Mode, a dot (.) can be qualified into double
382 -- underscores (__), a dollar sign ($) or left as is.
383
384 procedure Set_Unit_Number (U : Unit_Id);
385 -- Sets unit number (first unit is 1, leading zeroes output to line
386 -- up all output unit numbers nicely as required by the value, and
387 -- by the total number of units.
388
389 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
390 -- For C code case, write C & Common, for Ada case write Ada & Common
391 -- to current binder output file using Write_Binder_Info.
392
393 procedure Write_Statement_Buffer;
394 -- Write out contents of statement buffer up to Last, and reset Last to 0
395
396 procedure Write_Statement_Buffer (S : String);
397 -- First writes its argument (using Set_String (S)), then writes out the
398 -- contents of statement buffer up to Last, and reset Last to 0
399
400 ------------------------------------
401 -- Check_System_Restrictions_Used --
402 ------------------------------------
403
404 procedure Check_System_Restrictions_Used is
405 begin
406 for J in Units.First .. Units.Last loop
407 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
408 System_Restrictions_Used := True;
409 return;
410 end if;
411 end loop;
412
413 System_Restrictions_Used := False;
414 end Check_System_Restrictions_Used;
415
416 ----------------------
417 -- Gen_Adafinal_Ada --
418 ----------------------
419
420 procedure Gen_Adafinal_Ada is
421 begin
422 WBI (" procedure " & Ada_Final_Name.all & " is");
423
424 -- Do nothing if finalization is disabled
425
426 if Cumulative_Restrictions.Set (No_Finalization) then
427 WBI (" begin");
428 WBI (" null;");
429
430 -- General case
431
432 elsif VM_Target = No_VM then
433 WBI (" procedure s_stalib_adafinal;");
434 WBI (" pragma Import (C, s_stalib_adafinal, " &
435 """system__standard_library__adafinal"");");
436 WBI (" begin");
437 WBI (" s_stalib_adafinal;");
438
439 -- Pragma Import C cannot be used on virtual machine targets, therefore
440 -- call the runtime finalization routine directly.
441
442 else
443 WBI (" begin");
444 WBI (" System.Standard_Library.Adafinal;");
445 end if;
446
447 WBI (" end " & Ada_Final_Name.all & ";");
448 WBI ("");
449 end Gen_Adafinal_Ada;
450
451 --------------------
452 -- Gen_Adafinal_C --
453 --------------------
454
455 procedure Gen_Adafinal_C is
456 begin
457 WBI ("void " & Ada_Final_Name.all & " (void) {");
458 WBI (" system__standard_library__adafinal ();");
459 WBI ("}");
460 WBI ("");
461 end Gen_Adafinal_C;
462
463 ---------------------
464 -- Gen_Adainit_Ada --
465 ---------------------
466
467 procedure Gen_Adainit_Ada is
468 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
469 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
470
471 begin
472 WBI (" procedure " & Ada_Init_Name.all & " is");
473
474 -- Generate externals for elaboration entities
475
476 for E in Elab_Order.First .. Elab_Order.Last loop
477 declare
478 Unum : constant Unit_Id := Elab_Order.Table (E);
479 U : Unit_Record renames Units.Table (Unum);
480
481 begin
482 -- Check for Elab_Entity to be set for this unit
483
484 if U.Set_Elab_Entity
485
486 -- Don't generate reference for stand alone library
487
488 and then not U.SAL_Interface
489
490 -- Don't generate reference for predefined file in No_Run_Time
491 -- mode, since we don't include the object files in this case
492
493 and then not
494 (No_Run_Time_Mode
495 and then Is_Predefined_File_Name (U.Sfile))
496 then
497 Set_String (" ");
498 Set_String ("E");
499 Set_Unit_Number (Unum);
500
501 case VM_Target is
502 when No_VM | JVM_Target =>
503 Set_String (" : Boolean; pragma Import (Ada, ");
504 when CLI_Target =>
505 Set_String (" : Boolean; pragma Import (CIL, ");
506 end case;
507
508 Set_String ("E");
509 Set_Unit_Number (Unum);
510 Set_String (", """);
511 Get_Name_String (U.Uname);
512
513 -- In the case of JGNAT we need to emit an Import name that
514 -- includes the class name (using '$' separators in the case
515 -- of a child unit name).
516
517 if VM_Target /= No_VM then
518 for J in 1 .. Name_Len - 2 loop
519 if VM_Target = CLI_Target
520 or else Name_Buffer (J) /= '.'
521 then
522 Set_Char (Name_Buffer (J));
523 else
524 Set_String ("$");
525 end if;
526 end loop;
527
528 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
529 Set_String (".");
530 else
531 Set_String ("_pkg.");
532 end if;
533
534 -- If the unit name is very long, then split the
535 -- Import link name across lines using "&" (occurs
536 -- in some C2 tests).
537
538 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
539 Set_String (""" &");
540 Write_Statement_Buffer;
541 Set_String (" """);
542 end if;
543 end if;
544
545 Set_Unit_Name;
546 Set_String ("_E"");");
547 Write_Statement_Buffer;
548 end if;
549 end;
550 end loop;
551
552 Write_Statement_Buffer;
553
554 -- If the standard library is suppressed, then the only global variables
555 -- that might be needed (by the Ravenscar profile) are the priority and
556 -- the processor for the environment task.
557
558 if Suppress_Standard_Library_On_Target then
559 if Main_Priority /= No_Main_Priority then
560 WBI (" Main_Priority : Integer;");
561 WBI (" pragma Import (C, Main_Priority," &
562 " ""__gl_main_priority"");");
563 WBI ("");
564 end if;
565
566 if Main_CPU /= No_Main_CPU then
567 WBI (" Main_CPU : Integer;");
568 WBI (" pragma Import (C, Main_CPU," &
569 " ""__gl_main_cpu"");");
570 WBI ("");
571 end if;
572
573 WBI (" begin");
574
575 if Main_Priority /= No_Main_Priority then
576 Set_String (" Main_Priority := ");
577 Set_Int (Main_Priority);
578 Set_Char (';');
579 Write_Statement_Buffer;
580 end if;
581
582 if Main_CPU /= No_Main_CPU then
583 Set_String (" Main_CPU := ");
584 Set_Int (Main_CPU);
585 Set_Char (';');
586 Write_Statement_Buffer;
587 end if;
588
589 if Main_Priority = No_Main_Priority
590 and then Main_CPU = No_Main_CPU
591 then
592 WBI (" null;");
593 end if;
594
595 -- Normal case (standard library not suppressed). Set all global values
596 -- used by the run time.
597
598 else
599 WBI (" Main_Priority : Integer;");
600 WBI (" pragma Import (C, Main_Priority, " &
601 """__gl_main_priority"");");
602 WBI (" Time_Slice_Value : Integer;");
603 WBI (" pragma Import (C, Time_Slice_Value, " &
604 """__gl_time_slice_val"");");
605 WBI (" WC_Encoding : Character;");
606 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
607 WBI (" Locking_Policy : Character;");
608 WBI (" pragma Import (C, Locking_Policy, " &
609 """__gl_locking_policy"");");
610 WBI (" Queuing_Policy : Character;");
611 WBI (" pragma Import (C, Queuing_Policy, " &
612 """__gl_queuing_policy"");");
613 WBI (" Task_Dispatching_Policy : Character;");
614 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
615 """__gl_task_dispatching_policy"");");
616 WBI (" Priority_Specific_Dispatching : System.Address;");
617 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
618 """__gl_priority_specific_dispatching"");");
619 WBI (" Num_Specific_Dispatching : Integer;");
620 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
621 """__gl_num_specific_dispatching"");");
622 WBI (" Main_CPU : Integer;");
623 WBI (" pragma Import (C, Main_CPU, " &
624 """__gl_main_cpu"");");
625
626 WBI (" Interrupt_States : System.Address;");
627 WBI (" pragma Import (C, Interrupt_States, " &
628 """__gl_interrupt_states"");");
629 WBI (" Num_Interrupt_States : Integer;");
630 WBI (" pragma Import (C, Num_Interrupt_States, " &
631 """__gl_num_interrupt_states"");");
632 WBI (" Unreserve_All_Interrupts : Integer;");
633 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
634 """__gl_unreserve_all_interrupts"");");
635
636 if Exception_Tracebacks then
637 WBI (" Exception_Tracebacks : Integer;");
638 WBI (" pragma Import (C, Exception_Tracebacks, " &
639 """__gl_exception_tracebacks"");");
640 end if;
641
642 WBI (" Zero_Cost_Exceptions : Integer;");
643 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
644 """__gl_zero_cost_exceptions"");");
645 WBI (" Detect_Blocking : Integer;");
646 WBI (" pragma Import (C, Detect_Blocking, " &
647 """__gl_detect_blocking"");");
648 WBI (" Default_Stack_Size : Integer;");
649 WBI (" pragma Import (C, Default_Stack_Size, " &
650 """__gl_default_stack_size"");");
651 WBI (" Leap_Seconds_Support : Integer;");
652 WBI (" pragma Import (C, Leap_Seconds_Support, " &
653 """__gl_leap_seconds_support"");");
654
655 -- Import entry point for elaboration time signal handler
656 -- installation, and indication of if it's been called previously.
657
658 WBI ("");
659 WBI (" procedure Install_Handler;");
660 WBI (" pragma Import (C, Install_Handler, " &
661 """__gnat_install_handler"");");
662 WBI ("");
663 WBI (" Handler_Installed : Integer;");
664 WBI (" pragma Import (C, Handler_Installed, " &
665 """__gnat_handler_installed"");");
666
667 -- The import of the soft link which performs library-level object
668 -- finalization is not needed for VM targets. Regular Ada is used in
669 -- that case.
670
671 if VM_Target = No_VM then
672 WBI ("");
673 WBI (" type No_Param_Proc is access procedure;");
674 WBI (" Finalize_Library_Objects : No_Param_Proc;");
675 WBI (" pragma Import (C, Finalize_Library_Objects, " &
676 """__gnat_finalize_library_objects"");");
677 end if;
678
679 -- Import entry point for environment feature enable/disable
680 -- routine, and indication that it's been called previously.
681
682 if OpenVMS_On_Target then
683 WBI ("");
684 WBI (" procedure Set_Features;");
685 WBI (" pragma Import (C, Set_Features, " &
686 """__gnat_set_features"");");
687 WBI ("");
688 WBI (" Features_Set : Integer;");
689 WBI (" pragma Import (C, Features_Set, " &
690 """__gnat_features_set"");");
691
692 if Opt.Heap_Size /= 0 then
693 WBI ("");
694 WBI (" Heap_Size : Integer;");
695 WBI (" pragma Import (C, Heap_Size, " &
696 """__gl_heap_size"");");
697
698 Write_Statement_Buffer;
699 end if;
700 end if;
701
702 -- Initialize stack limit variable of the environment task if the
703 -- stack check method is stack limit and stack check is enabled.
704
705 if Stack_Check_Limits_On_Target
706 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
707 then
708 WBI ("");
709 WBI (" procedure Initialize_Stack_Limit;");
710 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
711 """__gnat_initialize_stack_limit"");");
712 end if;
713
714 -- Special processing when main program is CIL function/procedure
715
716 if VM_Target = CLI_Target
717 and then Bind_Main_Program
718 and then not No_Main_Subprogram
719 then
720 WBI ("");
721
722 -- Function case, use Set_Exit_Status to report the returned
723 -- status code, since that is the only mechanism available.
724
725 if ALIs.Table (ALIs.First).Main_Program = Func then
726 WBI (" Result : Integer;");
727 WBI (" procedure Set_Exit_Status (Code : Integer);");
728 WBI (" pragma Import (C, Set_Exit_Status, " &
729 """__gnat_set_exit_status"");");
730 WBI ("");
731 WBI (" function Ada_Main_Program return Integer;");
732
733 -- Procedure case
734
735 else
736 WBI (" procedure Ada_Main_Program;");
737 end if;
738
739 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
740 Name_Len := Name_Len - 2;
741 WBI (" pragma Import (CIL, Ada_Main_Program, """
742 & Name_Buffer (1 .. Name_Len) & "."
743 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
744 end if;
745
746 WBI (" begin");
747
748 Set_String (" Main_Priority := ");
749 Set_Int (Main_Priority);
750 Set_Char (';');
751 Write_Statement_Buffer;
752
753 Set_String (" Time_Slice_Value := ");
754
755 if Task_Dispatching_Policy_Specified = 'F'
756 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
757 then
758 Set_Int (0);
759 else
760 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
761 end if;
762
763 Set_Char (';');
764 Write_Statement_Buffer;
765
766 Set_String (" WC_Encoding := '");
767 Set_Char (Get_WC_Encoding);
768
769 Set_String ("';");
770 Write_Statement_Buffer;
771
772 Set_String (" Locking_Policy := '");
773 Set_Char (Locking_Policy_Specified);
774 Set_String ("';");
775 Write_Statement_Buffer;
776
777 Set_String (" Queuing_Policy := '");
778 Set_Char (Queuing_Policy_Specified);
779 Set_String ("';");
780 Write_Statement_Buffer;
781
782 Set_String (" Task_Dispatching_Policy := '");
783 Set_Char (Task_Dispatching_Policy_Specified);
784 Set_String ("';");
785 Write_Statement_Buffer;
786
787 Gen_Restrictions_Ada;
788
789 WBI (" Priority_Specific_Dispatching :=");
790 WBI (" Local_Priority_Specific_Dispatching'Address;");
791
792 Set_String (" Num_Specific_Dispatching := ");
793 Set_Int (PSD_Pragma_Settings.Last + 1);
794 Set_Char (';');
795 Write_Statement_Buffer;
796
797 Set_String (" Main_CPU := ");
798 Set_Int (Main_CPU);
799 Set_Char (';');
800 Write_Statement_Buffer;
801
802 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
803
804 Set_String (" Num_Interrupt_States := ");
805 Set_Int (IS_Pragma_Settings.Last + 1);
806 Set_Char (';');
807 Write_Statement_Buffer;
808
809 Set_String (" Unreserve_All_Interrupts := ");
810
811 if Unreserve_All_Interrupts_Specified then
812 Set_String ("1");
813 else
814 Set_String ("0");
815 end if;
816
817 Set_Char (';');
818 Write_Statement_Buffer;
819
820 if Exception_Tracebacks then
821 WBI (" Exception_Tracebacks := 1;");
822 end if;
823
824 Set_String (" Zero_Cost_Exceptions := ");
825
826 if Zero_Cost_Exceptions_Specified then
827 Set_String ("1");
828 else
829 Set_String ("0");
830 end if;
831
832 Set_String (";");
833 Write_Statement_Buffer;
834
835 Set_String (" Detect_Blocking := ");
836
837 if Detect_Blocking then
838 Set_Int (1);
839 else
840 Set_Int (0);
841 end if;
842
843 Set_String (";");
844 Write_Statement_Buffer;
845
846 Set_String (" Default_Stack_Size := ");
847 Set_Int (Default_Stack_Size);
848 Set_String (";");
849 Write_Statement_Buffer;
850
851 Set_String (" Leap_Seconds_Support := ");
852
853 if Leap_Seconds_Support then
854 Set_Int (1);
855 else
856 Set_Int (0);
857 end if;
858
859 Set_String (";");
860 Write_Statement_Buffer;
861
862 -- Generate call to Install_Handler
863
864 -- In .NET, when binding with -z, we don't install the signal handler
865 -- to let the caller handle the last exception handler.
866
867 if VM_Target /= CLI_Target
868 or else Bind_Main_Program
869 then
870 WBI ("");
871 WBI (" if Handler_Installed = 0 then");
872 WBI (" Install_Handler;");
873 WBI (" end if;");
874 end if;
875
876 -- Generate call to Set_Features
877
878 if OpenVMS_On_Target then
879 WBI ("");
880 WBI (" if Features_Set = 0 then");
881 WBI (" Set_Features;");
882 WBI (" end if;");
883
884 -- Features_Set may twiddle the heap size according to a logical
885 -- name, but the binder switch must override.
886
887 if Opt.Heap_Size /= 0 then
888 Set_String (" Heap_Size := ");
889 Set_Int (Opt.Heap_Size);
890 Set_Char (';');
891 Write_Statement_Buffer;
892 end if;
893 end if;
894 end if;
895
896 -- Generate call to set Initialize_Scalar values if active
897
898 if Initialize_Scalars_Used then
899 WBI ("");
900 Set_String (" System.Scalar_Values.Initialize ('");
901 Set_Char (Initialize_Scalars_Mode1);
902 Set_String ("', '");
903 Set_Char (Initialize_Scalars_Mode2);
904 Set_String ("');");
905 Write_Statement_Buffer;
906 end if;
907
908 -- Generate assignment of default secondary stack size if set
909
910 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
911 WBI ("");
912 Set_String (" System.Secondary_Stack.");
913 Set_String ("Default_Secondary_Stack_Size := ");
914 Set_Int (Opt.Default_Sec_Stack_Size);
915 Set_Char (';');
916 Write_Statement_Buffer;
917 end if;
918
919 -- Initialize stack limit variable of the environment task if the
920 -- stack check method is stack limit and stack check is enabled.
921
922 if Stack_Check_Limits_On_Target
923 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
924 then
925 WBI ("");
926 WBI (" Initialize_Stack_Limit;");
927 end if;
928
929 -- Attach Finalize_Library to the right softlink
930
931 if not Suppress_Standard_Library_On_Target then
932 WBI ("");
933
934 if VM_Target = No_VM then
935 if Lib_Final_Built then
936 Set_String (" Finalize_Library_Objects := ");
937 Set_String ("Finalize_Library'access;");
938 else
939 Set_String (" Finalize_Library_Objects := null;");
940 end if;
941
942 -- On VM targets use regular Ada to set the soft link
943
944 else
945 if Lib_Final_Built then
946 Set_String (" System.Soft_Links.Finalize_Library_Objects");
947 Set_String (" := Finalize_Library'access;");
948 else
949 Set_String (" System.Soft_Links.Finalize_Library_Objects");
950 Set_String (" := null;");
951 end if;
952 end if;
953
954 Write_Statement_Buffer;
955 end if;
956
957 -- Generate elaboration calls
958
959 WBI ("");
960 Gen_Elab_Calls_Ada;
961
962 -- Case of main program is CIL function or procedure
963
964 if VM_Target = CLI_Target
965 and then Bind_Main_Program
966 and then not No_Main_Subprogram
967 then
968 -- For function case, use Set_Exit_Status to set result
969
970 if ALIs.Table (ALIs.First).Main_Program = Func then
971 WBI (" Result := Ada_Main_Program;");
972 WBI (" Set_Exit_Status (Result);");
973
974 -- Procedure case
975
976 else
977 WBI (" Ada_Main_Program;");
978 end if;
979 end if;
980
981 WBI (" end " & Ada_Init_Name.all & ";");
982 WBI ("");
983 end Gen_Adainit_Ada;
984
985 -------------------
986 -- Gen_Adainit_C --
987 --------------------
988
989 procedure Gen_Adainit_C is
990 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
991 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
992
993 begin
994 WBI ("void " & Ada_Init_Name.all & " (void)");
995 WBI ("{");
996
997 -- Generate externals for elaboration entities
998
999 for E in Elab_Order.First .. Elab_Order.Last loop
1000 declare
1001 Unum : constant Unit_Id := Elab_Order.Table (E);
1002 U : Unit_Record renames Units.Table (Unum);
1003
1004 begin
1005 -- Check for Elab entity to be set for this unit
1006
1007 if U.Set_Elab_Entity
1008
1009 -- Don't generate reference for stand alone library
1010
1011 and then not U.SAL_Interface
1012
1013 -- Don't generate reference for predefined file in No_Run_Time
1014 -- mode, since we don't include the object files in this case
1015
1016 and then not
1017 (No_Run_Time_Mode
1018 and then Is_Predefined_File_Name (U.Sfile))
1019 then
1020 Set_String (" extern char ");
1021 Get_Name_String (U.Uname);
1022 Set_Unit_Name;
1023 Set_String ("_E;");
1024 Write_Statement_Buffer;
1025 end if;
1026 end;
1027 end loop;
1028
1029 Write_Statement_Buffer;
1030
1031 -- Standard library suppressed
1032
1033 if Suppress_Standard_Library_On_Target then
1034
1035 -- Case of High_Integrity_Mode mode. Set __gl_main_priority and
1036 -- __gl_main_cpu if needed for the Ravenscar profile.
1037
1038 if Main_Priority /= No_Main_Priority then
1039 WBI (" extern int __gl_main_priority;");
1040 Set_String (" __gl_main_priority = ");
1041 Set_Int (Main_Priority);
1042 Set_Char (';');
1043 Write_Statement_Buffer;
1044 end if;
1045
1046 if Main_CPU /= No_Main_CPU then
1047 WBI (" extern int __gl_main_cpu;");
1048 Set_String (" __gl_main_cpu = ");
1049 Set_Int (Main_CPU);
1050 Set_Char (';');
1051 Write_Statement_Buffer;
1052 end if;
1053
1054 -- Normal case (standard library not suppressed)
1055
1056 else
1057 -- Generate definition for interrupt states string
1058
1059 Set_String (" static const char *local_interrupt_states = """);
1060
1061 for J in 0 .. IS_Pragma_Settings.Last loop
1062 Set_Char (IS_Pragma_Settings.Table (J));
1063 end loop;
1064
1065 Set_String (""";");
1066 Write_Statement_Buffer;
1067
1068 -- Generate definition for priority specific dispatching string
1069
1070 Set_String
1071 (" static const char *local_priority_specific_dispatching = """);
1072
1073 for J in 0 .. PSD_Pragma_Settings.Last loop
1074 Set_Char (PSD_Pragma_Settings.Table (J));
1075 end loop;
1076
1077 Set_String (""";");
1078 Write_Statement_Buffer;
1079
1080 -- Generate declaration for secondary stack default if needed
1081
1082 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1083 WBI (" extern int system__secondary_stack__" &
1084 "default_secondary_stack_size;");
1085 end if;
1086
1087 WBI ("");
1088
1089 -- Code for normal case (standard library not suppressed)
1090
1091 -- We call the routine from inside adainit() because this works for
1092 -- both programs with and without binder generated "main" functions.
1093
1094 WBI (" extern int __gl_main_priority;");
1095 Set_String (" __gl_main_priority = ");
1096 Set_Int (Main_Priority);
1097 Set_Char (';');
1098 Write_Statement_Buffer;
1099
1100 WBI (" extern int __gl_time_slice_val;");
1101 Set_String (" __gl_time_slice_val = ");
1102
1103 if Task_Dispatching_Policy = 'F'
1104 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
1105 then
1106 Set_Int (0);
1107 else
1108 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
1109 end if;
1110
1111 Set_Char (';');
1112 Write_Statement_Buffer;
1113
1114 WBI (" extern char __gl_wc_encoding;");
1115 Set_String (" __gl_wc_encoding = '");
1116 Set_Char (Get_WC_Encoding);
1117
1118 Set_String ("';");
1119 Write_Statement_Buffer;
1120
1121 WBI (" extern char __gl_locking_policy;");
1122 Set_String (" __gl_locking_policy = '");
1123 Set_Char (Locking_Policy_Specified);
1124 Set_String ("';");
1125 Write_Statement_Buffer;
1126
1127 WBI (" extern char __gl_queuing_policy;");
1128 Set_String (" __gl_queuing_policy = '");
1129 Set_Char (Queuing_Policy_Specified);
1130 Set_String ("';");
1131 Write_Statement_Buffer;
1132
1133 WBI (" extern char __gl_task_dispatching_policy;");
1134 Set_String (" __gl_task_dispatching_policy = '");
1135 Set_Char (Task_Dispatching_Policy_Specified);
1136 Set_String ("';");
1137 Write_Statement_Buffer;
1138
1139 WBI (" extern int __gl_main_cpu;");
1140 Set_String (" __gl_main_cpu = ");
1141 Set_Int (Main_CPU);
1142 Set_Char (';');
1143 Write_Statement_Buffer;
1144
1145 Gen_Restrictions_C;
1146
1147 WBI (" extern const void *__gl_interrupt_states;");
1148 WBI (" __gl_interrupt_states = local_interrupt_states;");
1149
1150 WBI (" extern int __gl_num_interrupt_states;");
1151 Set_String (" __gl_num_interrupt_states = ");
1152 Set_Int (IS_Pragma_Settings.Last + 1);
1153 Set_String (";");
1154 Write_Statement_Buffer;
1155
1156 WBI (" extern const void *__gl_priority_specific_dispatching;");
1157 WBI (" __gl_priority_specific_dispatching =" &
1158 " local_priority_specific_dispatching;");
1159
1160 WBI (" extern int __gl_num_specific_dispatching;");
1161 Set_String (" __gl_num_specific_dispatching = ");
1162 Set_Int (PSD_Pragma_Settings.Last + 1);
1163 Set_String (";");
1164 Write_Statement_Buffer;
1165
1166 WBI (" extern int __gl_unreserve_all_interrupts;");
1167 Set_String (" __gl_unreserve_all_interrupts = ");
1168 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
1169 Set_String (";");
1170 Write_Statement_Buffer;
1171
1172 if Exception_Tracebacks then
1173 WBI (" extern int __gl_exception_tracebacks;");
1174 WBI (" __gl_exception_tracebacks = 1;");
1175 end if;
1176
1177 WBI (" extern int __gl_zero_cost_exceptions;");
1178 Set_String (" __gl_zero_cost_exceptions = ");
1179 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
1180 Set_String (";");
1181 Write_Statement_Buffer;
1182
1183 WBI (" extern int __gl_detect_blocking;");
1184 Set_String (" __gl_detect_blocking = ");
1185
1186 if Detect_Blocking then
1187 Set_Int (1);
1188 else
1189 Set_Int (0);
1190 end if;
1191
1192 Set_String (";");
1193 Write_Statement_Buffer;
1194
1195 WBI (" extern int __gl_default_stack_size;");
1196 Set_String (" __gl_default_stack_size = ");
1197 Set_Int (Default_Stack_Size);
1198 Set_String (";");
1199 Write_Statement_Buffer;
1200
1201 WBI (" extern int __gl_leap_seconds_support;");
1202 Set_String (" __gl_leap_seconds_support = ");
1203
1204 if Leap_Seconds_Support then
1205 Set_Int (1);
1206 else
1207 Set_Int (0);
1208 end if;
1209
1210 Set_String (";");
1211 Write_Statement_Buffer;
1212
1213 WBI ("");
1214
1215 -- Install elaboration time signal handler
1216
1217 WBI (" if (__gnat_handler_installed == 0)");
1218 WBI (" {");
1219 WBI (" __gnat_install_handler ();");
1220 WBI (" }");
1221
1222 -- Call feature enable/disable routine
1223
1224 if OpenVMS_On_Target then
1225 WBI (" if (__gnat_features_set == 0)");
1226 WBI (" {");
1227 WBI (" __gnat_set_features ();");
1228 WBI (" }");
1229 end if;
1230 end if;
1231
1232 -- Initialize stack limit for the environment task if the stack
1233 -- check method is stack limit and stack check is enabled.
1234
1235 if Stack_Check_Limits_On_Target
1236 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1237 then
1238 WBI ("");
1239 WBI (" __gnat_initialize_stack_limit ();");
1240 end if;
1241
1242 -- Generate call to set Initialize_Scalar values if needed
1243
1244 if Initialize_Scalars_Used then
1245 WBI ("");
1246 Set_String (" system__scalar_values__initialize('");
1247 Set_Char (Initialize_Scalars_Mode1);
1248 Set_String ("', '");
1249 Set_Char (Initialize_Scalars_Mode2);
1250 Set_String ("');");
1251 Write_Statement_Buffer;
1252 end if;
1253
1254 -- Generate assignment of default secondary stack size if set
1255
1256 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1257 WBI ("");
1258 Set_String (" system__secondary_stack__");
1259 Set_String ("default_secondary_stack_size = ");
1260 Set_Int (Opt.Default_Sec_Stack_Size);
1261 Set_Char (';');
1262 Write_Statement_Buffer;
1263 end if;
1264
1265 -- Generate elaboration calls
1266
1267 WBI ("");
1268 Gen_Elab_Calls_C;
1269 WBI ("}");
1270 WBI ("");
1271 end Gen_Adainit_C;
1272
1273 ------------------------
1274 -- Gen_Elab_Calls_Ada --
1275 ------------------------
1276
1277 procedure Gen_Elab_Calls_Ada is
1278 begin
1279 for E in Elab_Order.First .. Elab_Order.Last loop
1280 declare
1281 Unum : constant Unit_Id := Elab_Order.Table (E);
1282 U : Unit_Record renames Units.Table (Unum);
1283
1284 Unum_Spec : Unit_Id;
1285 -- This is the unit number of the spec that corresponds to
1286 -- this entry. It is the same as Unum except when the body
1287 -- and spec are different and we are currently processing
1288 -- the body, in which case it is the spec (Unum + 1).
1289
1290 begin
1291 if U.Utype = Is_Body then
1292 Unum_Spec := Unum + 1;
1293 else
1294 Unum_Spec := Unum;
1295 end if;
1296
1297 -- Nothing to do if predefined unit in no run time mode
1298
1299 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1300 null;
1301
1302 -- Case of no elaboration code
1303
1304 elsif U.No_Elab then
1305
1306 -- The only case in which we have to do something is if
1307 -- this is a body, with a separate spec, where the separate
1308 -- spec has an elaboration entity defined.
1309
1310 -- In that case, this is where we set the elaboration entity
1311 -- to True, we do not need to test if this has already been
1312 -- done, since it is quicker to set the flag than to test it.
1313
1314 if not U.SAL_Interface and then U.Utype = Is_Body
1315 and then Units.Table (Unum_Spec).Set_Elab_Entity
1316 then
1317 Set_String (" E");
1318 Set_Unit_Number (Unum_Spec);
1319 Set_String (" := True;");
1320 Write_Statement_Buffer;
1321 end if;
1322
1323 -- Here if elaboration code is present. If binding a library
1324 -- or if there is a non-Ada main subprogram then we generate:
1325
1326 -- if not uname_E then
1327 -- uname'elab_[spec|body];
1328 -- uname_E := True;
1329 -- end if;
1330
1331 -- Otherwise, elaboration routines are called unconditionally:
1332
1333 -- uname'elab_[spec|body];
1334 -- uname_E := True;
1335
1336 -- The uname_E assignment is skipped if this is a separate spec,
1337 -- since the assignment will be done when we process the body.
1338
1339 elsif not U.SAL_Interface then
1340 if Force_Checking_Of_Elaboration_Flags or
1341 Interface_Library_Unit or
1342 (not Bind_Main_Program)
1343 then
1344 Set_String (" if not E");
1345 Set_Unit_Number (Unum_Spec);
1346 Set_String (" then");
1347 Write_Statement_Buffer;
1348 Set_String (" ");
1349 end if;
1350
1351 Set_String (" ");
1352 Get_Decoded_Name_String_With_Brackets (U.Uname);
1353
1354 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1355 if Name_Buffer (Name_Len) = 's' then
1356 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1357 "_pkg'elab_spec";
1358 else
1359 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1360 "_pkg'elab_body";
1361 end if;
1362
1363 Name_Len := Name_Len + 12;
1364
1365 else
1366 if Name_Buffer (Name_Len) = 's' then
1367 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1368 "'elab_spec";
1369 else
1370 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1371 "'elab_body";
1372 end if;
1373
1374 Name_Len := Name_Len + 8;
1375 end if;
1376
1377 Set_Casing (U.Icasing);
1378 Set_Name_Buffer;
1379 Set_Char (';');
1380 Write_Statement_Buffer;
1381
1382 if U.Utype /= Is_Spec then
1383 if Force_Checking_Of_Elaboration_Flags or
1384 Interface_Library_Unit or
1385 (not Bind_Main_Program)
1386 then
1387 Set_String (" ");
1388 end if;
1389
1390 Set_String (" E");
1391 Set_Unit_Number (Unum_Spec);
1392 Set_String (" := True;");
1393 Write_Statement_Buffer;
1394 end if;
1395
1396 if Force_Checking_Of_Elaboration_Flags or
1397 Interface_Library_Unit or
1398 (not Bind_Main_Program)
1399 then
1400 WBI (" end if;");
1401 end if;
1402 end if;
1403 end;
1404 end loop;
1405 end Gen_Elab_Calls_Ada;
1406
1407 ----------------------
1408 -- Gen_Elab_Calls_C --
1409 ----------------------
1410
1411 procedure Gen_Elab_Calls_C is
1412 begin
1413 for E in Elab_Order.First .. Elab_Order.Last loop
1414 declare
1415 Unum : constant Unit_Id := Elab_Order.Table (E);
1416 U : Unit_Record renames Units.Table (Unum);
1417
1418 Unum_Spec : Unit_Id;
1419 -- This is the unit number of the spec that corresponds to
1420 -- this entry. It is the same as Unum except when the body
1421 -- and spec are different and we are currently processing
1422 -- the body, in which case it is the spec (Unum + 1).
1423
1424 begin
1425 if U.Utype = Is_Body then
1426 Unum_Spec := Unum + 1;
1427 else
1428 Unum_Spec := Unum;
1429 end if;
1430
1431 -- Nothing to do if predefined unit in no run time mode
1432
1433 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1434 null;
1435
1436 -- Case of no elaboration code
1437
1438 elsif U.No_Elab then
1439
1440 -- The only case in which we have to do something is if
1441 -- this is a body, with a separate spec, where the separate
1442 -- spec has an elaboration entity defined.
1443
1444 -- In that case, this is where we set the elaboration entity
1445 -- to True, we do not need to test if this has already been
1446 -- done, since it is quicker to set the flag than to test it.
1447
1448 if not U.SAL_Interface and then U.Utype = Is_Body
1449 and then Units.Table (Unum_Spec).Set_Elab_Entity
1450 then
1451 Set_String (" ");
1452 Get_Name_String (U.Uname);
1453 Set_Unit_Name;
1454 Set_String ("_E = 1;");
1455 Write_Statement_Buffer;
1456 end if;
1457
1458 -- Here if elaboration code is present. If binding a library
1459 -- or if there is a non-Ada main subprogram then we generate:
1460
1461 -- if (uname_E == 0) {
1462 -- uname__elab[s|b] ();
1463 -- uname_E++;
1464 -- }
1465
1466 -- The uname_E assignment is skipped if this is a separate spec,
1467 -- since the assignment will be done when we process the body.
1468
1469 elsif not U.SAL_Interface then
1470 Get_Name_String (U.Uname);
1471
1472 if Force_Checking_Of_Elaboration_Flags or
1473 Interface_Library_Unit or
1474 (not Bind_Main_Program)
1475 then
1476 Set_String (" if (");
1477 Set_Unit_Name;
1478 Set_String ("_E == 0) {");
1479 Write_Statement_Buffer;
1480 Set_String (" ");
1481 end if;
1482
1483 Set_String (" ");
1484 Set_Unit_Name;
1485 Set_String ("___elab");
1486 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1487 Set_String (" ();");
1488 Write_Statement_Buffer;
1489
1490 if U.Utype /= Is_Spec then
1491 if Force_Checking_Of_Elaboration_Flags or
1492 Interface_Library_Unit or
1493 (not Bind_Main_Program)
1494 then
1495 Set_String (" ");
1496 end if;
1497
1498 Set_String (" ");
1499 Set_Unit_Name;
1500 Set_String ("_E++;");
1501 Write_Statement_Buffer;
1502 end if;
1503
1504 if Force_Checking_Of_Elaboration_Flags or
1505 Interface_Library_Unit or
1506 (not Bind_Main_Program)
1507 then
1508 WBI (" }");
1509 end if;
1510 end if;
1511 end;
1512 end loop;
1513 end Gen_Elab_Calls_C;
1514
1515 ----------------------
1516 -- Gen_Elab_Defs_C --
1517 ----------------------
1518
1519 procedure Gen_Elab_Defs_C is
1520 begin
1521 WBI ("/* BEGIN ELABORATION DEFINITIONS */");
1522
1523 for E in Elab_Order.First .. Elab_Order.Last loop
1524
1525 -- Generate declaration of elaboration procedure if elaboration
1526 -- needed. Note that passive units are always excluded.
1527
1528 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1529 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1530 Set_String ("extern void ");
1531 Set_Unit_Name;
1532 Set_String ("___elab");
1533 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1534 Set_String (" (void);");
1535 Write_Statement_Buffer;
1536 end if;
1537 end loop;
1538 WBI ("");
1539 end Gen_Elab_Defs_C;
1540
1541 ------------------------
1542 -- Gen_Elab_Order_Ada --
1543 ------------------------
1544
1545 procedure Gen_Elab_Order_Ada is
1546 begin
1547 WBI (" -- BEGIN ELABORATION ORDER");
1548
1549 for J in Elab_Order.First .. Elab_Order.Last loop
1550 Set_String (" -- ");
1551 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1552 Set_Name_Buffer;
1553 Write_Statement_Buffer;
1554 end loop;
1555
1556 WBI (" -- END ELABORATION ORDER");
1557 WBI ("");
1558 end Gen_Elab_Order_Ada;
1559
1560 ----------------------
1561 -- Gen_Elab_Order_C --
1562 ----------------------
1563
1564 procedure Gen_Elab_Order_C is
1565 begin
1566 WBI ("/* BEGIN ELABORATION ORDER");
1567
1568 for J in Elab_Order.First .. Elab_Order.Last loop
1569 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1570 Set_Name_Buffer;
1571 Write_Statement_Buffer;
1572 end loop;
1573
1574 WBI (" END ELABORATION ORDER */");
1575 WBI ("");
1576 end Gen_Elab_Order_C;
1577
1578 ------------------------------
1579 -- Gen_Finalize_Library_Ada --
1580 ------------------------------
1581
1582 procedure Gen_Finalize_Library_Ada is
1583 Count : Int := 1;
1584 U : Unit_Record;
1585 Uspec : Unit_Record;
1586 Unum : Unit_Id;
1587
1588 begin
1589 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1590 Unum := Elab_Order.Table (E);
1591 U := Units.Table (Unum);
1592
1593 -- We are only interested in non-generic packages
1594
1595 if U.Unit_Kind = 'p'
1596 and then U.Has_Finalizer
1597 and then not U.Is_Generic
1598 and then not U.No_Elab
1599 then
1600 if not Lib_Final_Built then
1601 Lib_Final_Built := True;
1602
1603 WBI (" procedure Finalize_Library is");
1604
1605 -- The following flag is used to check for library-level
1606 -- exceptions raised during finalization. The symbol comes
1607 -- from System.Soft_Links. VM targets use regular Ada to
1608 -- reference the entity.
1609
1610 if VM_Target = No_VM then
1611 WBI (" LE_Set : Boolean;");
1612
1613 Set_String (" pragma Import (Ada, LE_Set, ");
1614 Set_String ("""__gnat_library_exception_set"");");
1615 Write_Statement_Buffer;
1616 end if;
1617
1618 WBI (" begin");
1619 end if;
1620
1621 -- Generate:
1622 -- declare
1623 -- procedure F<Count>;
1624
1625 Set_String (" declare");
1626 Write_Statement_Buffer;
1627
1628 Set_String (" procedure F");
1629 Set_Int (Count);
1630 Set_Char (';');
1631 Write_Statement_Buffer;
1632
1633 -- Generate:
1634 -- pragma Import (CIL, F<Count>, "xx.yy_pkg.Finalize[B/S]");
1635 -- -- for .NET targets
1636
1637 -- pragma Import (Java, F<Count>, "xx$yy.Finalize[B/S]");
1638 -- -- for JVM targets
1639
1640 -- pragma Import (Ada, F<Count>, "xx__yy__Finalize[B/S]");
1641 -- -- for default targets
1642
1643 if VM_Target = CLI_Target then
1644 Set_String (" pragma Import (CIL, F");
1645 elsif VM_Target = JVM_Target then
1646 Set_String (" pragma Import (Java, F");
1647 else
1648 Set_String (" pragma Import (Ada, F");
1649 end if;
1650
1651 Set_Int (Count);
1652 Set_String (", """);
1653
1654 -- Dealing with package bodies is a little complicated. In such
1655 -- cases we must retrieve the package spec since it contains the
1656 -- spec of the body finalizer.
1657
1658 if U.Utype = Is_Body then
1659 Unum := Unum + 1;
1660 Uspec := Units.Table (Unum);
1661 else
1662 Uspec := U;
1663 end if;
1664
1665 Get_Name_String (Uspec.Uname);
1666
1667 -- Perform name construction
1668
1669 -- .NET xx.yy_pkg.finalize
1670
1671 if VM_Target = CLI_Target then
1672 Set_Unit_Name (Mode => Dot);
1673 Set_String ("_pkg.finalize");
1674
1675 -- JVM xx$yy.finalize
1676
1677 elsif VM_Target = JVM_Target then
1678 Set_Unit_Name (Mode => Dollar_Sign);
1679 Set_String (".finalize");
1680
1681 -- Default xx__yy__finalize
1682
1683 else
1684 Set_Unit_Name;
1685 Set_String ("__finalize");
1686 end if;
1687
1688 -- Package spec processing
1689
1690 if U.Utype = Is_Spec
1691 or else U.Utype = Is_Spec_Only
1692 then
1693 Set_Char ('S');
1694
1695 -- Package body processing
1696
1697 else
1698 Set_Char ('B');
1699 end if;
1700
1701 Set_String (""");");
1702 Write_Statement_Buffer;
1703
1704 WBI (" begin");
1705
1706 -- Generate:
1707 -- F<Count>;
1708 -- end;
1709
1710 Set_String (" F");
1711 Set_Int (Count);
1712 Set_Char (';');
1713 Write_Statement_Buffer;
1714 WBI (" end;");
1715
1716 Count := Count + 1;
1717 end if;
1718 end loop;
1719
1720 if Lib_Final_Built then
1721
1722 -- It is possible that the finalization of a library-level object
1723 -- raised an exception. In that case import the actual exception
1724 -- and the routine necessary to raise it.
1725
1726 if VM_Target = No_VM then
1727 WBI (" if LE_Set then");
1728 WBI (" declare");
1729 WBI (" LE : Ada.Exceptions.Exception_Occurrence;");
1730
1731 Set_String (" pragma Import (Ada, LE, ");
1732 Set_String ("""__gnat_library_exception"");");
1733 Write_Statement_Buffer;
1734
1735 Set_String (" procedure Raise_Controlled ");
1736 Set_String ("(E : Ada.Exceptions.Exception_Occurrence);");
1737 Write_Statement_Buffer;
1738
1739 Set_String (" pragma Import (Ada, Raise_Controlled, ");
1740 Set_String ("""__gnat_raise_from_controlled_operation"");");
1741 Write_Statement_Buffer;
1742
1743 WBI (" begin");
1744 WBI (" Raise_Controlled (LE);");
1745 WBI (" end;");
1746
1747 -- VM-specific code, use regular Ada to produce the desired behavior
1748
1749 else
1750 WBI (" if System.Soft_Links.Library_Exception_Set then");
1751
1752 Set_String (" Ada.Exceptions.Reraise_Occurrence (");
1753 Set_String ("System.Soft_Links.Library_Exception);");
1754 Write_Statement_Buffer;
1755 end if;
1756
1757 WBI (" end if;");
1758 WBI (" end Finalize_Library;");
1759 WBI ("");
1760 end if;
1761 end Gen_Finalize_Library_Ada;
1762
1763 ----------------------------
1764 -- Gen_Finalize_Library_C --
1765 ----------------------------
1766
1767 procedure Gen_Finalize_Library_C is
1768 U : Unit_Record;
1769 Uspec : Unit_Record;
1770 Unum : Unit_Id;
1771
1772 begin
1773 WBI (" /* BEGIN FINALIZE */");
1774
1775 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1776 Unum := Elab_Order.Table (E);
1777 U := Units.Table (Unum);
1778
1779 -- We are only interested in non-generic packages
1780
1781 if U.Unit_Kind = 'p'
1782 and then U.Has_Finalizer
1783 and then not U.Is_Generic
1784 and then not U.No_Elab
1785 then
1786 Set_String (" ");
1787
1788 -- Dealing with package bodies is a little complicated. In such
1789 -- cases we must retrieve the package spec since it contains the
1790 -- spec of the body finalizer.
1791
1792 if U.Utype = Is_Body then
1793 Unum := Unum + 1;
1794 Uspec := Units.Table (Unum);
1795 else
1796 Uspec := U;
1797 end if;
1798
1799 Get_Name_String (Uspec.Uname);
1800 Set_Unit_Name;
1801 Set_String ("__finalize");
1802
1803 -- Package spec processing
1804
1805 if U.Utype = Is_Spec
1806 or else U.Utype = Is_Spec_Only
1807 then
1808 Set_Char ('S');
1809
1810 -- Package body processing
1811
1812 else
1813 Set_Char ('B');
1814 end if;
1815
1816 Set_String (" ();");
1817
1818 Write_Statement_Buffer;
1819 end if;
1820 end loop;
1821
1822 WBI (" /* END FINALIZE */");
1823 WBI ("");
1824 end Gen_Finalize_Library_C;
1825
1826 ---------------------------------
1827 -- Gen_Finalize_Library_Defs_C --
1828 ---------------------------------
1829
1830 procedure Gen_Finalize_Library_Defs_C is
1831 U : Unit_Record;
1832 Uspec : Unit_Record;
1833 Unum : Unit_Id;
1834
1835 begin
1836 WBI ("/* BEGIN FINALIZE DEFINITIONS */");
1837
1838 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1839 Unum := Elab_Order.Table (E);
1840 U := Units.Table (Unum);
1841
1842 -- We are only interested in non-generic packages
1843
1844 if U.Unit_Kind = 'p'
1845 and then U.Has_Finalizer
1846 and then not U.Is_Generic
1847 and then not U.No_Elab
1848 then
1849 -- Dealing with package bodies is a little complicated. In such
1850 -- cases we must retrieve the package spec since it contains the
1851 -- spec of the body finalizer.
1852
1853 if U.Utype = Is_Body then
1854 Unum := Unum + 1;
1855 Uspec := Units.Table (Unum);
1856 else
1857 Uspec := U;
1858 end if;
1859
1860 Set_String ("extern void ");
1861 Get_Name_String (Uspec.Uname);
1862 Set_Unit_Name;
1863 Set_String ("__finalize");
1864
1865 if U.Utype = Is_Spec
1866 or else U.Utype = Is_Spec_Only
1867 then
1868 Set_Char ('S');
1869 else
1870 Set_Char ('B');
1871 end if;
1872
1873 Set_String (" (void);");
1874 Write_Statement_Buffer;
1875 end if;
1876 end loop;
1877
1878 WBI ("/* END FINALIZE DEFINITIONS */");
1879 WBI ("");
1880 end Gen_Finalize_Library_Defs_C;
1881
1882 ------------------
1883 -- Gen_Main_Ada --
1884 ------------------
1885
1886 procedure Gen_Main_Ada is
1887 begin
1888 if Exit_Status_Supported_On_Target then
1889 Set_String (" function ");
1890 else
1891 Set_String (" procedure ");
1892 end if;
1893
1894 Set_String (Get_Main_Name);
1895
1896 if Command_Line_Args_On_Target then
1897 Write_Statement_Buffer;
1898 WBI (" (argc : Integer;");
1899 WBI (" argv : System.Address;");
1900 WBI (" envp : System.Address)");
1901
1902 if Exit_Status_Supported_On_Target then
1903 WBI (" return Integer");
1904 end if;
1905
1906 WBI (" is");
1907
1908 else
1909 if Exit_Status_Supported_On_Target then
1910 Set_String (" return Integer is");
1911 else
1912 Set_String (" is");
1913 end if;
1914
1915 Write_Statement_Buffer;
1916 end if;
1917
1918 if Opt.Default_Exit_Status /= 0
1919 and then Bind_Main_Program
1920 and then not Configurable_Run_Time_Mode
1921 then
1922 WBI (" procedure Set_Exit_Status (Status : Integer);");
1923 WBI (" pragma Import (C, Set_Exit_Status, " &
1924 """__gnat_set_exit_status"");");
1925 WBI ("");
1926 end if;
1927
1928 -- Initialize and Finalize
1929
1930 if not Cumulative_Restrictions.Set (No_Finalization) then
1931 WBI (" procedure Initialize (Addr : System.Address);");
1932 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1933 WBI ("");
1934 WBI (" procedure Finalize;");
1935 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1936 end if;
1937
1938 -- If we want to analyze the stack, we have to import corresponding
1939 -- symbols
1940
1941 if Dynamic_Stack_Measurement then
1942 WBI ("");
1943 WBI (" procedure Output_Results;");
1944 WBI (" pragma Import (C, Output_Results, " &
1945 """__gnat_stack_usage_output_results"");");
1946
1947 WBI ("");
1948 WBI (" " &
1949 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1950 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1951 """__gnat_stack_usage_initialize"");");
1952 end if;
1953
1954 -- Deal with declarations for main program case
1955
1956 if not No_Main_Subprogram then
1957
1958 -- To call the main program, we declare it using a pragma Import
1959 -- Ada with the right link name.
1960
1961 -- It might seem more obvious to "with" the main program, and call
1962 -- it in the normal Ada manner. We do not do this for three reasons:
1963
1964 -- 1. It is more efficient not to recompile the main program
1965 -- 2. We are not entitled to assume the source is accessible
1966 -- 3. We don't know what options to use to compile it
1967
1968 -- It is really reason 3 that is most critical (indeed we used
1969 -- to generate the "with", but several regression tests failed).
1970
1971 WBI ("");
1972
1973 if ALIs.Table (ALIs.First).Main_Program = Func then
1974 WBI (" Result : Integer;");
1975 WBI ("");
1976 WBI (" function Ada_Main_Program return Integer;");
1977
1978 else
1979 WBI (" procedure Ada_Main_Program;");
1980 end if;
1981
1982 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1983 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1984 Set_Main_Program_Name;
1985 Set_String (""");");
1986
1987 Write_Statement_Buffer;
1988 WBI ("");
1989
1990 if Bind_Main_Program
1991 and then not Suppress_Standard_Library_On_Target
1992 then
1993 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1994 WBI ("");
1995 end if;
1996 end if;
1997
1998 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1999 -- not referenced elsewhere in the generated program, but is needed
2000 -- by the debugger (that's why it is generated in the first place).
2001 -- The reference stops Ada_Main_Program_Name from being optimized
2002 -- away by smart linkers, such as the AiX linker.
2003
2004 -- Because this variable is unused, we make this variable "aliased"
2005 -- with a pragma Volatile in order to tell the compiler to preserve
2006 -- this variable at any level of optimization.
2007
2008 if Bind_Main_Program then
2009 WBI
2010 (" Ensure_Reference : aliased System.Address := " &
2011 "Ada_Main_Program_Name'Address;");
2012 WBI (" pragma Volatile (Ensure_Reference);");
2013 WBI ("");
2014 end if;
2015
2016 WBI (" begin");
2017
2018 -- Acquire command line arguments if present on target
2019
2020 if Command_Line_Args_On_Target then
2021 WBI (" gnat_argc := argc;");
2022 WBI (" gnat_argv := argv;");
2023 WBI (" gnat_envp := envp;");
2024 WBI ("");
2025
2026 -- If configurable run time and no command line args, then nothing
2027 -- needs to be done since the gnat_argc/argv/envp variables are
2028 -- suppressed in this case.
2029
2030 elsif Configurable_Run_Time_On_Target then
2031 null;
2032
2033 -- Otherwise set dummy values (to be filled in by some other unit?)
2034
2035 else
2036 WBI (" gnat_argc := 0;");
2037 WBI (" gnat_argv := System.Null_Address;");
2038 WBI (" gnat_envp := System.Null_Address;");
2039 end if;
2040
2041 if Opt.Default_Exit_Status /= 0
2042 and then Bind_Main_Program
2043 and then not Configurable_Run_Time_Mode
2044 then
2045 Set_String (" Set_Exit_Status (");
2046 Set_Int (Opt.Default_Exit_Status);
2047 Set_String (");");
2048 Write_Statement_Buffer;
2049 end if;
2050
2051 if Dynamic_Stack_Measurement then
2052 Set_String (" Initialize_Stack_Analysis (");
2053 Set_Int (Dynamic_Stack_Measurement_Array_Size);
2054 Set_String (");");
2055 Write_Statement_Buffer;
2056 end if;
2057
2058 if not Cumulative_Restrictions.Set (No_Finalization) then
2059 if not No_Main_Subprogram
2060 and then Bind_Main_Program
2061 and then not Suppress_Standard_Library_On_Target
2062 then
2063 WBI (" Initialize (SEH'Address);");
2064 else
2065 WBI (" Initialize (System.Null_Address);");
2066 end if;
2067 end if;
2068
2069 WBI (" " & Ada_Init_Name.all & ";");
2070
2071 if not No_Main_Subprogram then
2072 WBI (" Break_Start;");
2073
2074 if ALIs.Table (ALIs.First).Main_Program = Proc then
2075 WBI (" Ada_Main_Program;");
2076 else
2077 WBI (" Result := Ada_Main_Program;");
2078 end if;
2079 end if;
2080
2081 -- Adafinal call is skipped if no finalization
2082
2083 if not Cumulative_Restrictions.Set (No_Finalization) then
2084 WBI (" adafinal;");
2085 end if;
2086
2087 -- Prints the result of static stack analysis
2088
2089 if Dynamic_Stack_Measurement then
2090 WBI (" Output_Results;");
2091 end if;
2092
2093 -- Finalize is only called if we have a run time
2094
2095 if not Cumulative_Restrictions.Set (No_Finalization) then
2096 WBI (" Finalize;");
2097 end if;
2098
2099 -- Return result
2100
2101 if Exit_Status_Supported_On_Target then
2102 if No_Main_Subprogram
2103 or else ALIs.Table (ALIs.First).Main_Program = Proc
2104 then
2105 WBI (" return (gnat_exit_status);");
2106 else
2107 WBI (" return (Result);");
2108 end if;
2109 end if;
2110
2111 WBI (" end;");
2112 WBI ("");
2113 end Gen_Main_Ada;
2114
2115 ----------------
2116 -- Gen_Main_C --
2117 ----------------
2118
2119 procedure Gen_Main_C is
2120 Needs_Library_Finalization : constant Boolean := Has_Finalizer;
2121
2122 begin
2123 if Exit_Status_Supported_On_Target then
2124 WBI ("#include <stdlib.h>");
2125 Set_String ("int ");
2126 else
2127 Set_String ("void ");
2128 end if;
2129
2130 Set_String (Get_Main_Name);
2131
2132 -- Generate command line args in prototype if present on target
2133
2134 if Command_Line_Args_On_Target then
2135 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
2136
2137 -- Case of no command line arguments on target
2138
2139 else
2140 Write_Statement_Buffer (" (void)");
2141 end if;
2142
2143 WBI ("{");
2144
2145 -- Generate a reference to __gnat_ada_main_program_name. This symbol
2146 -- is not referenced elsewhere in the generated program, but is
2147 -- needed by the debugger (that's why it is generated in the first
2148 -- place). The reference stops Ada_Main_Program_Name from being
2149 -- optimized away by smart linkers, such as the AiX linker.
2150
2151 -- Because this variable is unused, we declare this variable as
2152 -- volatile in order to tell the compiler to preserve it at any
2153 -- level of optimization.
2154
2155 if Bind_Main_Program then
2156 WBI (" char * volatile ensure_reference " &
2157 "__attribute__ ((__unused__)) = " &
2158 "__gnat_ada_main_program_name;");
2159 WBI ("");
2160
2161 if not Suppress_Standard_Library_On_Target
2162 and then not No_Main_Subprogram
2163 then
2164 WBI (" int SEH [2];");
2165 WBI ("");
2166 end if;
2167 end if;
2168
2169 -- If main program is a function, generate result variable
2170
2171 if ALIs.Table (ALIs.First).Main_Program = Func then
2172 WBI (" int result;");
2173 end if;
2174
2175 -- Set command line argument values from parameters if command line
2176 -- arguments are present on target
2177
2178 if Command_Line_Args_On_Target then
2179 WBI (" gnat_argc = argc;");
2180 WBI (" gnat_argv = argv;");
2181 WBI (" gnat_envp = envp;");
2182 WBI (" ");
2183
2184 -- If configurable run-time, then nothing to do, since in this case
2185 -- the gnat_argc/argv/envp variables are entirely suppressed.
2186
2187 elsif Configurable_Run_Time_On_Target then
2188 null;
2189
2190 -- if no command line arguments on target, set dummy values
2191
2192 else
2193 WBI (" gnat_argc = 0;");
2194 WBI (" gnat_argv = 0;");
2195 WBI (" gnat_envp = 0;");
2196 end if;
2197
2198 if Opt.Default_Exit_Status /= 0
2199 and then Bind_Main_Program
2200 and then not Configurable_Run_Time_Mode
2201 then
2202 Set_String (" __gnat_set_exit_status (");
2203 Set_Int (Opt.Default_Exit_Status);
2204 Set_String (");");
2205 Write_Statement_Buffer;
2206 end if;
2207
2208 -- Initializes dynamic stack measurement if needed
2209
2210 if Dynamic_Stack_Measurement then
2211 Set_String (" __gnat_stack_usage_initialize (");
2212 Set_Int (Dynamic_Stack_Measurement_Array_Size);
2213 Set_String (");");
2214 Write_Statement_Buffer;
2215 end if;
2216
2217 -- The __gnat_initialize routine is used only if we have a run-time
2218
2219 if not Suppress_Standard_Library_On_Target then
2220 if not No_Main_Subprogram and then Bind_Main_Program then
2221 WBI (" __gnat_initialize ((void *)SEH);");
2222 else
2223 WBI (" __gnat_initialize ((void *)0);");
2224 end if;
2225 end if;
2226
2227 WBI (" " & Ada_Init_Name.all & " ();");
2228
2229 if not No_Main_Subprogram then
2230 WBI (" __gnat_break_start ();");
2231 WBI (" ");
2232
2233 -- Output main program name
2234
2235 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2236
2237 -- Main program is procedure case
2238
2239 if ALIs.Table (ALIs.First).Main_Program = Proc then
2240 Set_String (" ");
2241 Set_Main_Program_Name;
2242 Set_String (" ();");
2243 Write_Statement_Buffer;
2244
2245 -- Main program is function case
2246
2247 else -- ALIs.Table (ALIs_First).Main_Program = Func
2248 Set_String (" result = ");
2249 Set_Main_Program_Name;
2250 Set_String (" ();");
2251 Write_Statement_Buffer;
2252 end if;
2253
2254 end if;
2255
2256 -- Call adafinal if finalization active
2257
2258 if not Cumulative_Restrictions.Set (No_Finalization)
2259 and then Needs_Library_Finalization
2260 then
2261 Gen_Finalize_Library_C;
2262 end if;
2263
2264 -- Outputs the dynamic stack measurement if needed
2265
2266 if Dynamic_Stack_Measurement then
2267 WBI (" __gnat_stack_usage_output_results ();");
2268 end if;
2269
2270 -- The finalize routine is used only if we have a run-time
2271
2272 if not Suppress_Standard_Library_On_Target then
2273 WBI (" __gnat_finalize ();");
2274 end if;
2275
2276 -- Case of main program is a function, so the value it returns
2277 -- is the exit status in this case.
2278
2279 if ALIs.Table (ALIs.First).Main_Program = Func then
2280 if Exit_Status_Supported_On_Target then
2281
2282 -- VMS must use Posix exit routine in order to get the effect
2283 -- of a Unix compatible setting of the program exit status.
2284 -- For all other systems, we use the standard exit routine.
2285
2286 if OpenVMS_On_Target then
2287 WBI (" decc$__posix_exit (result);");
2288 else
2289 WBI (" exit (result);");
2290 end if;
2291 end if;
2292
2293 -- Case of main program is a procedure, in which case the exit
2294 -- status is whatever was set by a Set_Exit call most recently
2295
2296 else
2297 if Exit_Status_Supported_On_Target then
2298
2299 -- VMS must use Posix exit routine in order to get the effect
2300 -- of a Unix compatible setting of the program exit status.
2301 -- For all other systems, we use the standard exit routine.
2302
2303 if OpenVMS_On_Target then
2304 WBI (" decc$__posix_exit (gnat_exit_status);");
2305 else
2306 WBI (" exit (gnat_exit_status);");
2307 end if;
2308 end if;
2309 end if;
2310
2311 WBI ("}");
2312 WBI ("");
2313 end Gen_Main_C;
2314
2315 ------------------------------
2316 -- Gen_Object_Files_Options --
2317 ------------------------------
2318
2319 procedure Gen_Object_Files_Options is
2320 Lgnat : Natural;
2321 -- This keeps track of the position in the sorted set of entries
2322 -- in the Linker_Options table of where the first entry from an
2323 -- internal file appears.
2324
2325 Linker_Option_List_Started : Boolean := False;
2326 -- Set to True when "LINKER OPTION LIST" is displayed
2327
2328 procedure Write_Linker_Option;
2329 -- Write binder info linker option
2330
2331 -------------------------
2332 -- Write_Linker_Option --
2333 -------------------------
2334
2335 procedure Write_Linker_Option is
2336 Start : Natural;
2337 Stop : Natural;
2338
2339 begin
2340 -- Loop through string, breaking at null's
2341
2342 Start := 1;
2343 while Start < Name_Len loop
2344
2345 -- Find null ending this section
2346
2347 Stop := Start + 1;
2348 while Name_Buffer (Stop) /= ASCII.NUL
2349 and then Stop <= Name_Len loop
2350 Stop := Stop + 1;
2351 end loop;
2352
2353 -- Process section if non-null
2354
2355 if Stop > Start then
2356 if Output_Linker_Option_List then
2357 if not Zero_Formatting then
2358 if not Linker_Option_List_Started then
2359 Linker_Option_List_Started := True;
2360 Write_Eol;
2361 Write_Str (" LINKER OPTION LIST");
2362 Write_Eol;
2363 Write_Eol;
2364 end if;
2365
2366 Write_Str (" ");
2367 end if;
2368
2369 Write_Str (Name_Buffer (Start .. Stop - 1));
2370 Write_Eol;
2371 end if;
2372 Write_Info_Ada_C
2373 (" -- ", "", Name_Buffer (Start .. Stop - 1));
2374 end if;
2375
2376 Start := Stop + 1;
2377 end loop;
2378 end Write_Linker_Option;
2379
2380 -- Start of processing for Gen_Object_Files_Options
2381
2382 begin
2383 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
2384
2385 if Object_List_Filename /= null then
2386 Set_List_File (Object_List_Filename.all);
2387 end if;
2388
2389 for E in Elab_Order.First .. Elab_Order.Last loop
2390
2391 -- If not spec that has an associated body, then generate a comment
2392 -- giving the name of the corresponding object file.
2393
2394 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
2395 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
2396 then
2397 Get_Name_String
2398 (ALIs.Table
2399 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
2400
2401 -- If the presence of an object file is necessary or if it exists,
2402 -- then use it.
2403
2404 if not Hostparm.Exclude_Missing_Objects
2405 or else
2406 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
2407 then
2408 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2409
2410 if Output_Object_List then
2411 Write_Str (Name_Buffer (1 .. Name_Len));
2412 Write_Eol;
2413 end if;
2414
2415 -- Don't link with the shared library on VMS if an internal
2416 -- filename object is seen. Multiply defined symbols will
2417 -- result.
2418
2419 if OpenVMS_On_Target
2420 and then Is_Internal_File_Name
2421 (ALIs.Table
2422 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
2423 then
2424 -- Special case for g-trasym.obj (not included in libgnat)
2425
2426 Get_Name_String (ALIs.Table
2427 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
2428
2429 if Name_Buffer (1 .. 8) /= "g-trasym" then
2430 Opt.Shared_Libgnat := False;
2431 end if;
2432 end if;
2433 end if;
2434 end if;
2435 end loop;
2436
2437 if Object_List_Filename /= null then
2438 Close_List_File;
2439 end if;
2440
2441 -- Add a "-Ldir" for each directory in the object path
2442 if VM_Target /= CLI_Target then
2443 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2444 declare
2445 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2446 begin
2447 Name_Len := 0;
2448 Add_Str_To_Name_Buffer ("-L");
2449 Add_Str_To_Name_Buffer (Dir.all);
2450 Write_Linker_Option;
2451 end;
2452 end loop;
2453 end if;
2454
2455 -- Sort linker options
2456
2457 -- This sort accomplishes two important purposes:
2458
2459 -- a) All application files are sorted to the front, and all GNAT
2460 -- internal files are sorted to the end. This results in a well
2461 -- defined dividing line between the two sets of files, for the
2462 -- purpose of inserting certain standard library references into
2463 -- the linker arguments list.
2464
2465 -- b) Given two different units, we sort the linker options so that
2466 -- those from a unit earlier in the elaboration order comes later
2467 -- in the list. This is a heuristic designed to create a more
2468 -- friendly order of linker options when the operations appear in
2469 -- separate units. The idea is that if unit A must be elaborated
2470 -- before unit B, then it is more likely that B references
2471 -- libraries included by A, than vice versa, so we want libraries
2472 -- included by A to come after libraries included by B.
2473
2474 -- These two criteria are implemented by function Lt_Linker_Option. Note
2475 -- that a special case of b) is that specs are elaborated before bodies,
2476 -- so linker options from specs come after linker options for bodies,
2477 -- and again, the assumption is that libraries used by the body are more
2478 -- likely to reference libraries used by the spec, than vice versa.
2479
2480 Sort
2481 (Linker_Options.Last,
2482 Move_Linker_Option'Access,
2483 Lt_Linker_Option'Access);
2484
2485 -- Write user linker options, i.e. the set of linker options that come
2486 -- from all files other than GNAT internal files, Lgnat is left set to
2487 -- point to the first entry from a GNAT internal file, or past the end
2488 -- of the entries if there are no internal files.
2489
2490 Lgnat := Linker_Options.Last + 1;
2491
2492 for J in 1 .. Linker_Options.Last loop
2493 if not Linker_Options.Table (J).Internal_File then
2494 Get_Name_String (Linker_Options.Table (J).Name);
2495 Write_Linker_Option;
2496 else
2497 Lgnat := J;
2498 exit;
2499 end if;
2500 end loop;
2501
2502 -- Now we insert standard linker options that must appear after the
2503 -- entries from user files, and before the entries from GNAT run-time
2504 -- files. The reason for this decision is that libraries referenced
2505 -- by internal routines may reference these standard library entries.
2506
2507 -- Note that we do not insert anything when pragma No_Run_Time has been
2508 -- specified or when the standard libraries are not to be used,
2509 -- otherwise on some platforms, such as VMS, we may get duplicate
2510 -- symbols when linking.
2511
2512 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2513 Name_Len := 0;
2514
2515 if Opt.Shared_Libgnat then
2516 Add_Str_To_Name_Buffer ("-shared");
2517 else
2518 Add_Str_To_Name_Buffer ("-static");
2519 end if;
2520
2521 -- Write directly to avoid -K output (why???)
2522
2523 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2524
2525 if With_DECGNAT then
2526 Name_Len := 0;
2527
2528 if Opt.Shared_Libgnat then
2529 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2530 else
2531 Add_Str_To_Name_Buffer ("-ldecgnat");
2532 end if;
2533
2534 Write_Linker_Option;
2535 end if;
2536
2537 if With_GNARL then
2538 Name_Len := 0;
2539
2540 if Opt.Shared_Libgnat then
2541 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2542 else
2543 Add_Str_To_Name_Buffer ("-lgnarl");
2544 end if;
2545
2546 Write_Linker_Option;
2547 end if;
2548
2549 Name_Len := 0;
2550
2551 if Opt.Shared_Libgnat then
2552 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2553 else
2554 Add_Str_To_Name_Buffer ("-lgnat");
2555 end if;
2556
2557 Write_Linker_Option;
2558 end if;
2559
2560 -- Write linker options from all internal files
2561
2562 for J in Lgnat .. Linker_Options.Last loop
2563 Get_Name_String (Linker_Options.Table (J).Name);
2564 Write_Linker_Option;
2565 end loop;
2566
2567 if Output_Linker_Option_List and then not Zero_Formatting then
2568 Write_Eol;
2569 end if;
2570
2571 if Ada_Bind_File then
2572 WBI ("-- END Object file/option list ");
2573 else
2574 WBI (" END Object file/option list */");
2575 end if;
2576 end Gen_Object_Files_Options;
2577
2578 ---------------------
2579 -- Gen_Output_File --
2580 ---------------------
2581
2582 procedure Gen_Output_File (Filename : String) is
2583 begin
2584 -- Acquire settings for Interrupt_State pragmas
2585
2586 Set_IS_Pragma_Table;
2587
2588 -- Acquire settings for Priority_Specific_Dispatching pragma
2589
2590 Set_PSD_Pragma_Table;
2591
2592 -- Override Ada_Bind_File and Bind_Main_Program for VMs since JGNAT only
2593 -- supports Ada code, and the main program is already generated by the
2594 -- compiler.
2595
2596 if VM_Target /= No_VM then
2597 Ada_Bind_File := True;
2598
2599 if VM_Target = JVM_Target then
2600 Bind_Main_Program := False;
2601 end if;
2602 end if;
2603
2604 -- Override time slice value if -T switch is set
2605
2606 if Time_Slice_Set then
2607 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2608 end if;
2609
2610 -- Count number of elaboration calls
2611
2612 for E in Elab_Order.First .. Elab_Order.Last loop
2613 if Units.Table (Elab_Order.Table (E)).No_Elab then
2614 null;
2615 else
2616 Num_Elab_Calls := Num_Elab_Calls + 1;
2617 end if;
2618 end loop;
2619
2620 -- Generate output file in appropriate language
2621
2622 Check_System_Restrictions_Used;
2623
2624 if Ada_Bind_File then
2625 Gen_Output_File_Ada (Filename);
2626 else
2627 Gen_Output_File_C (Filename);
2628 end if;
2629 end Gen_Output_File;
2630
2631 -------------------------
2632 -- Gen_Output_File_Ada --
2633 -------------------------
2634
2635 procedure Gen_Output_File_Ada (Filename : String) is
2636
2637 Ada_Main : constant String := Get_Ada_Main_Name;
2638 -- Name to be used for generated Ada main program. See the body of
2639 -- function Get_Ada_Main_Name for details on the form of the name.
2640
2641 Needs_Library_Finalization : constant Boolean := Has_Finalizer;
2642
2643 Bfiles : Name_Id;
2644 -- Name of generated bind file (spec)
2645
2646 Bfileb : Name_Id;
2647 -- Name of generated bind file (body)
2648
2649 begin
2650 -- Create spec first
2651
2652 Create_Binder_Output (Filename, 's', Bfiles);
2653
2654 -- We always compile the binder file in Ada 95 mode so that we properly
2655 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2656 -- of the Ada 2005 constructs are needed by the binder file.
2657
2658 WBI ("pragma Ada_95;");
2659
2660 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2661 -- then we need to make sure that the binder program is compiled with
2662 -- the same restriction, so that no exception tables are generated.
2663
2664 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2665 WBI ("pragma Restrictions (No_Exception_Handlers);");
2666 end if;
2667
2668 -- Same processing for Restrictions (No_Exception_Propagation)
2669
2670 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2671 WBI ("pragma Restrictions (No_Exception_Propagation);");
2672 end if;
2673
2674 -- Same processing for pragma No_Run_Time
2675
2676 if No_Run_Time_Mode then
2677 WBI ("pragma No_Run_Time;");
2678 end if;
2679
2680 -- Generate with of System so we can reference System.Address
2681
2682 WBI ("with System;");
2683
2684 -- Generate with of System.Initialize_Scalars if active
2685
2686 if Initialize_Scalars_Used then
2687 WBI ("with System.Scalar_Values;");
2688 end if;
2689
2690 -- Generate with of System.Secondary_Stack if active
2691
2692 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2693 WBI ("with System.Secondary_Stack;");
2694 end if;
2695
2696 Resolve_Binder_Options;
2697
2698 -- Usually, adafinal is called using a pragma Import C. Since Import C
2699 -- doesn't have the same semantics for JGNAT, we use standard Ada.
2700
2701 if VM_Target /= No_VM
2702 and then not Suppress_Standard_Library_On_Target
2703 then
2704 WBI ("with System.Soft_Links;");
2705 WBI ("with System.Standard_Library;");
2706 end if;
2707
2708 WBI ("package " & Ada_Main & " is");
2709 WBI (" pragma Warnings (Off);");
2710
2711 -- Main program case
2712
2713 if Bind_Main_Program then
2714 if VM_Target = No_VM then
2715
2716 -- Generate argc/argv stuff unless suppressed
2717
2718 if Command_Line_Args_On_Target
2719 or not Configurable_Run_Time_On_Target
2720 then
2721 WBI ("");
2722 WBI (" gnat_argc : Integer;");
2723 WBI (" gnat_argv : System.Address;");
2724 WBI (" gnat_envp : System.Address;");
2725
2726 -- If the standard library is not suppressed, these variables
2727 -- are in the run-time data area for easy run time access.
2728
2729 if not Suppress_Standard_Library_On_Target then
2730 WBI ("");
2731 WBI (" pragma Import (C, gnat_argc);");
2732 WBI (" pragma Import (C, gnat_argv);");
2733 WBI (" pragma Import (C, gnat_envp);");
2734 end if;
2735 end if;
2736
2737 -- Define exit status. Again in normal mode, this is in the
2738 -- run-time library, and is initialized there, but in the
2739 -- configurable runtime case, the variable is declared and
2740 -- initialized in this file.
2741
2742 WBI ("");
2743
2744 if Configurable_Run_Time_Mode then
2745 if Exit_Status_Supported_On_Target then
2746 WBI (" gnat_exit_status : Integer := 0;");
2747 end if;
2748
2749 else
2750 WBI (" gnat_exit_status : Integer;");
2751 WBI (" pragma Import (C, gnat_exit_status);");
2752 end if;
2753 end if;
2754
2755 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2756 -- the main program. Otherwise, it can lead under some circumstances
2757 -- to a symbol duplication during the link (for instance when a C
2758 -- program uses two Ada libraries). Also zero terminate the string
2759 -- so that its end can be found reliably at run time.
2760
2761 WBI ("");
2762 WBI (" GNAT_Version : constant String :=");
2763 WBI (" """ & Ver_Prefix &
2764 Gnat_Version_String &
2765 """ & ASCII.NUL;");
2766 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2767
2768 WBI ("");
2769 Set_String (" Ada_Main_Program_Name : constant String := """);
2770 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2771
2772 if VM_Target = No_VM then
2773 Set_Main_Program_Name;
2774 Set_String (""" & ASCII.NUL;");
2775 else
2776 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2777 end if;
2778
2779 Write_Statement_Buffer;
2780
2781 WBI
2782 (" pragma Export (C, Ada_Main_Program_Name, " &
2783 """__gnat_ada_main_program_name"");");
2784 end if;
2785
2786 if not Cumulative_Restrictions.Set (No_Finalization) then
2787 WBI ("");
2788 WBI (" procedure " & Ada_Final_Name.all & ";");
2789 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2790 Ada_Final_Name.all & """);");
2791 end if;
2792
2793 WBI ("");
2794 WBI (" procedure " & Ada_Init_Name.all & ";");
2795 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2796 Ada_Init_Name.all & """);");
2797
2798 -- If -a has been specified use pragma Linker_Constructor for the init
2799 -- procedure. No need to use a similar pragma for the final procedure as
2800 -- global finalization will occur when the executable finishes execution
2801 -- and for plugins (shared stand-alone libraries that can be
2802 -- "unloaded"), finalization should not occur automatically, otherwise
2803 -- the main executable may not continue to work properly.
2804
2805 if Use_Pragma_Linker_Constructor then
2806 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2807 end if;
2808
2809 if Bind_Main_Program and then VM_Target = No_VM then
2810
2811 -- If we have the standard library, then Break_Start is defined
2812 -- there, but when the standard library is suppressed, Break_Start
2813 -- is defined here.
2814
2815 WBI ("");
2816 WBI (" procedure Break_Start;");
2817
2818 if Suppress_Standard_Library_On_Target then
2819 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2820 else
2821 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2822 end if;
2823
2824 WBI ("");
2825
2826 if Exit_Status_Supported_On_Target then
2827 Set_String (" function ");
2828 else
2829 Set_String (" procedure ");
2830 end if;
2831
2832 Set_String (Get_Main_Name);
2833
2834 -- Generate argument list if present
2835
2836 if Command_Line_Args_On_Target then
2837 Write_Statement_Buffer;
2838 WBI (" (argc : Integer;");
2839 WBI (" argv : System.Address;");
2840 Set_String
2841 (" envp : System.Address)");
2842
2843 if Exit_Status_Supported_On_Target then
2844 Write_Statement_Buffer;
2845 WBI (" return Integer;");
2846 else
2847 Write_Statement_Buffer (";");
2848 end if;
2849
2850 else
2851 if Exit_Status_Supported_On_Target then
2852 Write_Statement_Buffer (" return Integer;");
2853 else
2854 Write_Statement_Buffer (";");
2855 end if;
2856 end if;
2857
2858 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2859 Get_Main_Name & """);");
2860 end if;
2861
2862 Gen_Versions_Ada;
2863 Gen_Elab_Order_Ada;
2864
2865 -- Spec is complete
2866
2867 WBI ("");
2868 WBI ("end " & Ada_Main & ";");
2869 Close_Binder_Output;
2870
2871 -- Prepare to write body
2872
2873 Create_Binder_Output (Filename, 'b', Bfileb);
2874
2875 -- We always compile the binder file in Ada 95 mode so that we properly
2876 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2877 -- of the Ada 2005 constructs are needed by the binder file.
2878
2879 WBI ("pragma Ada_95;");
2880
2881 -- Output Source_File_Name pragmas which look like
2882
2883 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2884 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2885
2886 -- where sss/bbb are the spec/body file names respectively
2887
2888 Get_Name_String (Bfiles);
2889 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2890
2891 WBI ("pragma Source_File_Name (" &
2892 Ada_Main &
2893 ", Spec_File_Name => """ &
2894 Name_Buffer (1 .. Name_Len + 3));
2895
2896 Get_Name_String (Bfileb);
2897 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2898
2899 WBI ("pragma Source_File_Name (" &
2900 Ada_Main &
2901 ", Body_File_Name => """ &
2902 Name_Buffer (1 .. Name_Len + 3));
2903
2904 -- Generate with of System.Restrictions to initialize
2905 -- Run_Time_Restrictions.
2906
2907 if System_Restrictions_Used
2908 and not Suppress_Standard_Library_On_Target
2909 then
2910 WBI ("");
2911 WBI ("with System.Restrictions;");
2912 end if;
2913
2914 if Needs_Library_Finalization then
2915 WBI ("with Ada.Exceptions;");
2916 end if;
2917
2918 WBI ("");
2919 WBI ("package body " & Ada_Main & " is");
2920 WBI (" pragma Warnings (Off);");
2921
2922 if not Suppress_Standard_Library_On_Target then
2923
2924 -- Generate Priority_Specific_Dispatching pragma string
2925
2926 Set_String
2927 (" Local_Priority_Specific_Dispatching : constant String := """);
2928
2929 for J in 0 .. PSD_Pragma_Settings.Last loop
2930 Set_Char (PSD_Pragma_Settings.Table (J));
2931 end loop;
2932
2933 Set_String (""";");
2934 Write_Statement_Buffer;
2935
2936 -- Generate Interrupt_State pragma string
2937
2938 Set_String (" Local_Interrupt_States : constant String := """);
2939
2940 for J in 0 .. IS_Pragma_Settings.Last loop
2941 Set_Char (IS_Pragma_Settings.Table (J));
2942 end loop;
2943
2944 Set_String (""";");
2945 Write_Statement_Buffer;
2946 WBI ("");
2947 end if;
2948
2949 -- Generate the adafinal routine unless there is no finalization to do
2950
2951 if not Cumulative_Restrictions.Set (No_Finalization) then
2952 Gen_Adafinal_Ada;
2953
2954 if Needs_Library_Finalization then
2955 Gen_Finalize_Library_Ada;
2956 end if;
2957 end if;
2958
2959 Gen_Adainit_Ada;
2960
2961 if Bind_Main_Program and then VM_Target = No_VM then
2962
2963 -- When suppressing the standard library then generate dummy body
2964 -- for Break_Start
2965
2966 if Suppress_Standard_Library_On_Target then
2967 WBI ("");
2968 WBI (" procedure Break_Start is");
2969 WBI (" begin");
2970 WBI (" null;");
2971 WBI (" end;");
2972 end if;
2973
2974 Gen_Main_Ada;
2975 end if;
2976
2977 -- Output object file list and the Ada body is complete
2978
2979 Gen_Object_Files_Options;
2980
2981 WBI ("");
2982 WBI ("end " & Ada_Main & ";");
2983
2984 Close_Binder_Output;
2985 end Gen_Output_File_Ada;
2986
2987 -----------------------
2988 -- Gen_Output_File_C --
2989 -----------------------
2990
2991 procedure Gen_Output_File_C (Filename : String) is
2992
2993 Needs_Library_Finalization : constant Boolean := Has_Finalizer;
2994
2995 Bfile : Name_Id;
2996 pragma Warnings (Off, Bfile);
2997 -- Name of generated bind file (not referenced)
2998
2999 begin
3000 Create_Binder_Output (Filename, 'c', Bfile);
3001
3002 Resolve_Binder_Options;
3003
3004 WBI ("extern void " & Ada_Final_Name.all & " (void);");
3005
3006 -- If -a has been specified use __attribute__((constructor)) for the
3007 -- init procedure. No need to use a similar featute for the final
3008 -- procedure as global finalization will occur when the executable
3009 -- finishes execution and for plugins (shared stand-alone libraries that
3010 -- can be "unloaded"), finalization should not occur automatically,
3011 -- otherwise the main executable may not continue to work properly.
3012
3013 if Use_Pragma_Linker_Constructor then
3014 WBI ("extern void " & Ada_Init_Name.all &
3015 " (void) __attribute__((constructor));");
3016 else
3017 WBI ("extern void " & Ada_Init_Name.all & " (void);");
3018 end if;
3019
3020 WBI ("extern void system__standard_library__adafinal (void);");
3021
3022 if not No_Main_Subprogram then
3023 Set_String ("extern ");
3024
3025 if Exit_Status_Supported_On_Target then
3026 Set_String ("int");
3027 else
3028 Set_String ("void");
3029 end if;
3030
3031 Set_String (" main ");
3032
3033 if Command_Line_Args_On_Target then
3034 Write_Statement_Buffer ("(int, char **, char **);");
3035 else
3036 Write_Statement_Buffer ("(void);");
3037 end if;
3038
3039 if OpenVMS_On_Target then
3040 WBI ("extern void decc$__posix_exit (int);");
3041 else
3042 WBI ("extern void exit (int);");
3043 end if;
3044
3045 WBI ("extern void __gnat_break_start (void);");
3046 Set_String ("extern ");
3047
3048 if ALIs.Table (ALIs.First).Main_Program = Proc then
3049 Set_String ("void ");
3050 else
3051 Set_String ("int ");
3052 end if;
3053
3054 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3055 Set_Main_Program_Name;
3056 Set_String (" (void);");
3057 Write_Statement_Buffer;
3058 end if;
3059
3060 if not Suppress_Standard_Library_On_Target then
3061 WBI ("extern void __gnat_initialize (void *);");
3062 WBI ("extern void __gnat_finalize (void);");
3063 WBI ("extern void __gnat_install_handler (void);");
3064 end if;
3065
3066 if Dynamic_Stack_Measurement then
3067 WBI ("");
3068 WBI ("extern void __gnat_stack_usage_output_results (void);");
3069 WBI ("extern void __gnat_stack_usage_initialize (int size);");
3070 end if;
3071
3072 -- Initialize stack limit for the environment task if the stack check
3073 -- method is stack limit and stack check is enabled.
3074
3075 if Stack_Check_Limits_On_Target
3076 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
3077 then
3078 WBI ("");
3079 WBI ("extern void __gnat_initialize_stack_limit (void);");
3080 end if;
3081
3082 WBI ("");
3083
3084 Gen_Elab_Defs_C;
3085
3086 if Needs_Library_Finalization then
3087 Gen_Finalize_Library_Defs_C;
3088 end if;
3089
3090 -- Imported variables used only when we have a runtime
3091
3092 if not Suppress_Standard_Library_On_Target then
3093
3094 -- Track elaboration/finalization phase
3095
3096 WBI ("extern int __gnat_handler_installed;");
3097 WBI ("");
3098
3099 -- Track feature enable/disable on VMS
3100
3101 if OpenVMS_On_Target then
3102 WBI ("extern int __gnat_features_set;");
3103 WBI ("");
3104 end if;
3105 end if;
3106
3107 -- Write argv/argc exit status stuff if main program case
3108
3109 if Bind_Main_Program then
3110
3111 -- First deal with argc/argv/envp. In the normal case they are in the
3112 -- run-time library.
3113
3114 if not Configurable_Run_Time_On_Target then
3115 WBI ("extern int gnat_argc;");
3116 WBI ("extern char **gnat_argv;");
3117 WBI ("extern char **gnat_envp;");
3118
3119 -- If configurable run time and no command line args, then the
3120 -- generation of these variables is entirely suppressed.
3121
3122 elsif not Command_Line_Args_On_Target then
3123 null;
3124
3125 -- Otherwise, in the configurable run-time case they are right in the
3126 -- binder file.
3127
3128 else
3129 WBI ("int gnat_argc;");
3130 WBI ("char **gnat_argv;");
3131 WBI ("char **gnat_envp;");
3132 end if;
3133
3134 -- Similarly deal with exit status
3135
3136 if not Configurable_Run_Time_On_Target then
3137 WBI ("extern int gnat_exit_status;");
3138
3139 -- If configurable run time and no exit status on target, then the
3140 -- generation of this variables is entirely suppressed.
3141
3142 elsif not Exit_Status_Supported_On_Target then
3143 null;
3144
3145 -- Otherwise, in the configurable run-time case this variable is
3146 -- right in the binder file, and initialized to zero there.
3147
3148 else
3149 WBI ("int gnat_exit_status = 0;");
3150 end if;
3151
3152 WBI ("");
3153 end if;
3154
3155 -- When suppressing the standard library, the __gnat_break_start routine
3156 -- (for the debugger to get initial control) is defined in this file.
3157
3158 if Suppress_Standard_Library_On_Target then
3159 WBI ("");
3160 WBI ("void __gnat_break_start (void) {}");
3161 end if;
3162
3163 -- Generate the __gnat_version and __gnat_ada_main_program_name info
3164 -- only for the main program. Otherwise, it can lead under some
3165 -- circumstances to a symbol duplication during the link (for instance
3166 -- when a C program uses 2 Ada libraries)
3167
3168 if Bind_Main_Program then
3169 WBI ("");
3170 WBI ("char __gnat_version[] = """ & Ver_Prefix &
3171 Gnat_Version_String & """;");
3172
3173 Set_String ("char __gnat_ada_main_program_name[] = """);
3174 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3175 Set_Main_Program_Name;
3176 Set_String (""";");
3177 Write_Statement_Buffer;
3178 end if;
3179
3180 -- Generate the adafinal routine. In no runtime mode, this is not
3181 -- needed, since there is no finalization to do.
3182
3183 if not Cumulative_Restrictions.Set (No_Finalization) then
3184 Gen_Adafinal_C;
3185 end if;
3186
3187 Gen_Adainit_C;
3188
3189 -- Main is only present for Ada main case
3190
3191 if Bind_Main_Program then
3192 Gen_Main_C;
3193 end if;
3194
3195 -- Generate versions, elaboration order, list of object files
3196
3197 Gen_Versions_C;
3198 Gen_Elab_Order_C;
3199 Gen_Object_Files_Options;
3200
3201 -- C binder output is complete
3202
3203 Close_Binder_Output;
3204 end Gen_Output_File_C;
3205
3206 --------------------------
3207 -- Gen_Restrictions_Ada --
3208 --------------------------
3209
3210 procedure Gen_Restrictions_Ada is
3211 Count : Integer;
3212
3213 begin
3214 if Suppress_Standard_Library_On_Target
3215 or not System_Restrictions_Used
3216 then
3217 return;
3218 end if;
3219
3220 WBI (" System.Restrictions.Run_Time_Restrictions :=");
3221 WBI (" (Set =>");
3222 Set_String (" (");
3223
3224 Count := 0;
3225
3226 for J in Cumulative_Restrictions.Set'Range loop
3227 Set_Boolean (Cumulative_Restrictions.Set (J));
3228 Set_String (", ");
3229 Count := Count + 1;
3230
3231 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
3232 Write_Statement_Buffer;
3233 Set_String (" ");
3234 Count := 0;
3235 end if;
3236 end loop;
3237
3238 Set_String_Replace ("),");
3239 Write_Statement_Buffer;
3240 Set_String (" Value => (");
3241
3242 for J in Cumulative_Restrictions.Value'Range loop
3243 Set_Int (Int (Cumulative_Restrictions.Value (J)));
3244 Set_String (", ");
3245 end loop;
3246
3247 Set_String_Replace ("),");
3248 Write_Statement_Buffer;
3249 WBI (" Violated =>");
3250 Set_String (" (");
3251 Count := 0;
3252
3253 for J in Cumulative_Restrictions.Violated'Range loop
3254 Set_Boolean (Cumulative_Restrictions.Violated (J));
3255 Set_String (", ");
3256 Count := Count + 1;
3257
3258 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
3259 Write_Statement_Buffer;
3260 Set_String (" ");
3261 Count := 0;
3262 end if;
3263 end loop;
3264
3265 Set_String_Replace ("),");
3266 Write_Statement_Buffer;
3267 Set_String (" Count => (");
3268
3269 for J in Cumulative_Restrictions.Count'Range loop
3270 Set_Int (Int (Cumulative_Restrictions.Count (J)));
3271 Set_String (", ");
3272 end loop;
3273
3274 Set_String_Replace ("),");
3275 Write_Statement_Buffer;
3276 Set_String (" Unknown => (");
3277
3278 for J in Cumulative_Restrictions.Unknown'Range loop
3279 Set_Boolean (Cumulative_Restrictions.Unknown (J));
3280 Set_String (", ");
3281 end loop;
3282
3283 Set_String_Replace ("))");
3284 Set_String (";");
3285 Write_Statement_Buffer;
3286 end Gen_Restrictions_Ada;
3287
3288 ------------------------
3289 -- Gen_Restrictions_C --
3290 ------------------------
3291
3292 procedure Gen_Restrictions_C is
3293 begin
3294 if Suppress_Standard_Library_On_Target
3295 or not System_Restrictions_Used
3296 then
3297 return;
3298 end if;
3299
3300 WBI (" typedef struct {");
3301 Set_String (" char set [");
3302 Set_Int (Cumulative_Restrictions.Set'Length);
3303 Set_String ("];");
3304 Write_Statement_Buffer;
3305
3306 Set_String (" int value [");
3307 Set_Int (Cumulative_Restrictions.Value'Length);
3308 Set_String ("];");
3309 Write_Statement_Buffer;
3310
3311 Set_String (" char violated [");
3312 Set_Int (Cumulative_Restrictions.Violated'Length);
3313 Set_String ("];");
3314 Write_Statement_Buffer;
3315
3316 Set_String (" int count [");
3317 Set_Int (Cumulative_Restrictions.Count'Length);
3318 Set_String ("];");
3319 Write_Statement_Buffer;
3320
3321 Set_String (" char unknown [");
3322 Set_Int (Cumulative_Restrictions.Unknown'Length);
3323 Set_String ("];");
3324 Write_Statement_Buffer;
3325 WBI (" } restrictions;");
3326 WBI (" extern restrictions " &
3327 "system__restrictions__run_time_restrictions;");
3328 WBI (" restrictions r = {");
3329 Set_String (" {");
3330
3331 for J in Cumulative_Restrictions.Set'Range loop
3332 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
3333 Set_String (", ");
3334 end loop;
3335
3336 Set_String_Replace ("},");
3337 Write_Statement_Buffer;
3338 Set_String (" {");
3339
3340 for J in Cumulative_Restrictions.Value'Range loop
3341 Set_Int (Int (Cumulative_Restrictions.Value (J)));
3342 Set_String (", ");
3343 end loop;
3344
3345 Set_String_Replace ("},");
3346 Write_Statement_Buffer;
3347 Set_String (" {");
3348
3349 for J in Cumulative_Restrictions.Violated'Range loop
3350 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
3351 Set_String (", ");
3352 end loop;
3353
3354 Set_String_Replace ("},");
3355 Write_Statement_Buffer;
3356 Set_String (" {");
3357
3358 for J in Cumulative_Restrictions.Count'Range loop
3359 Set_Int (Int (Cumulative_Restrictions.Count (J)));
3360 Set_String (", ");
3361 end loop;
3362
3363 Set_String_Replace ("},");
3364 Write_Statement_Buffer;
3365 Set_String (" {");
3366
3367 for J in Cumulative_Restrictions.Unknown'Range loop
3368 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
3369 Set_String (", ");
3370 end loop;
3371
3372 Set_String_Replace ("}}");
3373 Set_String (";");
3374 Write_Statement_Buffer;
3375 WBI (" system__restrictions__run_time_restrictions = r;");
3376 end Gen_Restrictions_C;
3377
3378 ----------------------
3379 -- Gen_Versions_Ada --
3380 ----------------------
3381
3382 -- This routine generates lines such as:
3383
3384 -- unnnnn : constant Integer := 16#hhhhhhhh#;
3385 -- pragma Export (C, unnnnn, unam);
3386
3387 -- for each unit, where unam is the unit name suffixed by either B or S for
3388 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
3389 -- the version number, and nnnnn is a 5-digits serial number.
3390
3391 procedure Gen_Versions_Ada is
3392 Ubuf : String (1 .. 6) := "u00000";
3393
3394 procedure Increment_Ubuf;
3395 -- Little procedure to increment the serial number
3396
3397 --------------------
3398 -- Increment_Ubuf --
3399 --------------------
3400
3401 procedure Increment_Ubuf is
3402 begin
3403 for J in reverse Ubuf'Range loop
3404 Ubuf (J) := Character'Succ (Ubuf (J));
3405 exit when Ubuf (J) <= '9';
3406 Ubuf (J) := '0';
3407 end loop;
3408 end Increment_Ubuf;
3409
3410 -- Start of processing for Gen_Versions_Ada
3411
3412 begin
3413 WBI ("");
3414
3415 WBI (" type Version_32 is mod 2 ** 32;");
3416 for U in Units.First .. Units.Last loop
3417 if not Units.Table (U).SAL_Interface and then
3418 ((not Bind_For_Library) or else Units.Table (U).Directly_Scanned)
3419 then
3420 Increment_Ubuf;
3421 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
3422 Units.Table (U).Version & "#;");
3423 Set_String (" pragma Export (C, ");
3424 Set_String (Ubuf);
3425 Set_String (", """);
3426
3427 Get_Name_String (Units.Table (U).Uname);
3428
3429 for K in 1 .. Name_Len loop
3430 if Name_Buffer (K) = '.' then
3431 Set_Char ('_');
3432 Set_Char ('_');
3433
3434 elsif Name_Buffer (K) = '%' then
3435 exit;
3436
3437 else
3438 Set_Char (Name_Buffer (K));
3439 end if;
3440 end loop;
3441
3442 if Name_Buffer (Name_Len) = 's' then
3443 Set_Char ('S');
3444 else
3445 Set_Char ('B');
3446 end if;
3447
3448 Set_String (""");");
3449 Write_Statement_Buffer;
3450 end if;
3451 end loop;
3452 end Gen_Versions_Ada;
3453
3454 --------------------
3455 -- Gen_Versions_C --
3456 --------------------
3457
3458 -- This routine generates a line of the form:
3459
3460 -- unsigned unam = 0xhhhhhhhh;
3461
3462 -- for each unit, where unam is the unit name suffixed by either B or S for
3463 -- body or spec, with dots replaced by double underscores.
3464
3465 procedure Gen_Versions_C is
3466 begin
3467 for U in Units.First .. Units.Last loop
3468 if not Units.Table (U).SAL_Interface and then
3469 ((not Bind_For_Library) or else Units.Table (U).Directly_Scanned)
3470 then
3471 Set_String ("unsigned ");
3472
3473 Get_Name_String (Units.Table (U).Uname);
3474
3475 for K in 1 .. Name_Len loop
3476 if Name_Buffer (K) = '.' then
3477 Set_String ("__");
3478
3479 elsif Name_Buffer (K) = '%' then
3480 exit;
3481
3482 else
3483 Set_Char (Name_Buffer (K));
3484 end if;
3485 end loop;
3486
3487 if Name_Buffer (Name_Len) = 's' then
3488 Set_Char ('S');
3489 else
3490 Set_Char ('B');
3491 end if;
3492
3493 Set_String (" = 0x");
3494 Set_String (Units.Table (U).Version);
3495 Set_Char (';');
3496 Write_Statement_Buffer;
3497 end if;
3498 end loop;
3499 end Gen_Versions_C;
3500
3501 ------------------------
3502 -- Get_Main_Unit_Name --
3503 ------------------------
3504
3505 function Get_Main_Unit_Name (S : String) return String is
3506 Result : String := S;
3507
3508 begin
3509 for J in S'Range loop
3510 if Result (J) = '.' then
3511 Result (J) := '_';
3512 end if;
3513 end loop;
3514
3515 return Result;
3516 end Get_Main_Unit_Name;
3517
3518 -----------------------
3519 -- Get_Ada_Main_Name --
3520 -----------------------
3521
3522 function Get_Ada_Main_Name return String is
3523 Suffix : constant String := "_00";
3524 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3525 Opt.Ada_Main_Name.all & Suffix;
3526 Nlen : Natural;
3527
3528 begin
3529 -- The main program generated by JGNAT expects a package called
3530 -- ada_<main procedure>.
3531
3532 if VM_Target /= No_VM then
3533 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3534 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3535 end if;
3536
3537 -- This loop tries the following possibilities in order
3538 -- <Ada_Main>
3539 -- <Ada_Main>_01
3540 -- <Ada_Main>_02
3541 -- ..
3542 -- <Ada_Main>_99
3543 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3544 -- it is set to 'ada_main'.
3545
3546 for J in 0 .. 99 loop
3547 if J = 0 then
3548 Nlen := Name'Length - Suffix'Length;
3549 else
3550 Nlen := Name'Length;
3551 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3552 Name (Name'Last - 1) :=
3553 Character'Val (J / 10 + Character'Pos ('0'));
3554 end if;
3555
3556 for K in ALIs.First .. ALIs.Last loop
3557 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3558
3559 -- Get unit name, removing %b or %e at end
3560
3561 Get_Name_String (Units.Table (L).Uname);
3562 Name_Len := Name_Len - 2;
3563
3564 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3565 goto Continue;
3566 end if;
3567 end loop;
3568 end loop;
3569
3570 return Name (1 .. Nlen);
3571
3572 <<Continue>>
3573 null;
3574 end loop;
3575
3576 -- If we fall through, just use a peculiar unlikely name
3577
3578 return ("Qwertyuiop");
3579 end Get_Ada_Main_Name;
3580
3581 -------------------
3582 -- Get_Main_Name --
3583 -------------------
3584
3585 function Get_Main_Name return String is
3586 begin
3587 -- Explicit name given with -M switch
3588
3589 if Bind_Alternate_Main_Name then
3590 return Alternate_Main_Name.all;
3591
3592 -- Case of main program name to be used directly
3593
3594 elsif Use_Ada_Main_Program_Name_On_Target then
3595
3596 -- Get main program name
3597
3598 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3599
3600 -- If this is a child name, return only the name of the child, since
3601 -- we can't have dots in a nested program name. Note that we do not
3602 -- include the %b at the end of the unit name.
3603
3604 for J in reverse 1 .. Name_Len - 2 loop
3605 if J = 1 or else Name_Buffer (J - 1) = '.' then
3606 return Name_Buffer (J .. Name_Len - 2);
3607 end if;
3608 end loop;
3609
3610 raise Program_Error; -- impossible exit
3611
3612 -- Case where "main" is to be used as default
3613
3614 else
3615 return "main";
3616 end if;
3617 end Get_Main_Name;
3618
3619 ---------------------
3620 -- Get_WC_Encoding --
3621 ---------------------
3622
3623 function Get_WC_Encoding return Character is
3624 begin
3625 -- If encoding method specified by -W switch, then return it
3626
3627 if Wide_Character_Encoding_Method_Specified then
3628 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3629
3630 -- If no main program, and not specified, set brackets, we really have
3631 -- no better choice. If some other encoding is required when there is
3632 -- no main, it must be set explicitly using -Wx.
3633
3634 -- Note: if the ALI file always passed the wide character encoding of
3635 -- every file, then we could use the encoding of the initial specified
3636 -- file, but this information is passed only for potential main
3637 -- programs. We could fix this sometime, but it is a very minor point
3638 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
3639 -- is no main program).
3640
3641 elsif No_Main_Subprogram then
3642 return 'b';
3643
3644 -- Otherwise if there is a main program, take encoding from it
3645
3646 else
3647 return ALIs.Table (ALIs.First).WC_Encoding;
3648 end if;
3649 end Get_WC_Encoding;
3650
3651 -------------------
3652 -- Has_Finalizer --
3653 -------------------
3654
3655 function Has_Finalizer return Boolean is
3656 U : Unit_Record;
3657 Unum : Unit_Id;
3658
3659 begin
3660 for E in reverse Elab_Order.First .. Elab_Order.Last loop
3661 Unum := Elab_Order.Table (E);
3662 U := Units.Table (Unum);
3663
3664 -- We are only interested in non-generic packages
3665
3666 if U.Unit_Kind = 'p'
3667 and then U.Has_Finalizer
3668 and then not U.Is_Generic
3669 and then not U.No_Elab
3670 then
3671 return True;
3672 end if;
3673 end loop;
3674
3675 return False;
3676 end Has_Finalizer;
3677
3678 ----------------------
3679 -- Lt_Linker_Option --
3680 ----------------------
3681
3682 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3683 begin
3684 -- Sort internal files last
3685
3686 if Linker_Options.Table (Op1).Internal_File
3687 /=
3688 Linker_Options.Table (Op2).Internal_File
3689 then
3690 -- Note: following test uses False < True
3691
3692 return Linker_Options.Table (Op1).Internal_File
3693 <
3694 Linker_Options.Table (Op2).Internal_File;
3695
3696 -- If both internal or both non-internal, sort according to the
3697 -- elaboration position. A unit that is elaborated later should come
3698 -- earlier in the linker options list.
3699
3700 else
3701 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3702 >
3703 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3704
3705 end if;
3706 end Lt_Linker_Option;
3707
3708 ------------------------
3709 -- Move_Linker_Option --
3710 ------------------------
3711
3712 procedure Move_Linker_Option (From : Natural; To : Natural) is
3713 begin
3714 Linker_Options.Table (To) := Linker_Options.Table (From);
3715 end Move_Linker_Option;
3716
3717 ----------------------------
3718 -- Resolve_Binder_Options --
3719 ----------------------------
3720
3721 procedure Resolve_Binder_Options is
3722 begin
3723 for E in Elab_Order.First .. Elab_Order.Last loop
3724 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3725
3726 -- This is not a perfect approach, but is the current protocol
3727 -- between the run-time and the binder to indicate that tasking is
3728 -- used: system.os_interface should always be used by any tasking
3729 -- application.
3730
3731 if Name_Buffer (1 .. 19) = "system.os_interface" then
3732 With_GNARL := True;
3733 end if;
3734
3735 -- Ditto for declib and the "dec" package
3736
3737 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3738 With_DECGNAT := True;
3739 end if;
3740 end loop;
3741 end Resolve_Binder_Options;
3742
3743 -----------------
3744 -- Set_Boolean --
3745 -----------------
3746
3747 procedure Set_Boolean (B : Boolean) is
3748 True_Str : constant String := "True";
3749 False_Str : constant String := "False";
3750 begin
3751 if B then
3752 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3753 Last := Last + True_Str'Length;
3754 else
3755 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3756 Last := Last + False_Str'Length;
3757 end if;
3758 end Set_Boolean;
3759
3760 --------------
3761 -- Set_Char --
3762 --------------
3763
3764 procedure Set_Char (C : Character) is
3765 begin
3766 Last := Last + 1;
3767 Statement_Buffer (Last) := C;
3768 end Set_Char;
3769
3770 -------------
3771 -- Set_Int --
3772 -------------
3773
3774 procedure Set_Int (N : Int) is
3775 begin
3776 if N < 0 then
3777 Set_String ("-");
3778 Set_Int (-N);
3779
3780 else
3781 if N > 9 then
3782 Set_Int (N / 10);
3783 end if;
3784
3785 Last := Last + 1;
3786 Statement_Buffer (Last) :=
3787 Character'Val (N mod 10 + Character'Pos ('0'));
3788 end if;
3789 end Set_Int;
3790
3791 -------------------------
3792 -- Set_IS_Pragma_Table --
3793 -------------------------
3794
3795 procedure Set_IS_Pragma_Table is
3796 begin
3797 for F in ALIs.First .. ALIs.Last loop
3798 for K in ALIs.Table (F).First_Interrupt_State ..
3799 ALIs.Table (F).Last_Interrupt_State
3800 loop
3801 declare
3802 Inum : constant Int :=
3803 Interrupt_States.Table (K).Interrupt_Id;
3804 Stat : constant Character :=
3805 Interrupt_States.Table (K).Interrupt_State;
3806
3807 begin
3808 while IS_Pragma_Settings.Last < Inum loop
3809 IS_Pragma_Settings.Append ('n');
3810 end loop;
3811
3812 IS_Pragma_Settings.Table (Inum) := Stat;
3813 end;
3814 end loop;
3815 end loop;
3816 end Set_IS_Pragma_Table;
3817
3818 ---------------------------
3819 -- Set_Main_Program_Name --
3820 ---------------------------
3821
3822 procedure Set_Main_Program_Name is
3823 begin
3824 -- Note that name has %b on the end which we ignore
3825
3826 -- First we output the initial _ada_ since we know that the main
3827 -- program is a library level subprogram.
3828
3829 Set_String ("_ada_");
3830
3831 -- Copy name, changing dots to double underscores
3832
3833 for J in 1 .. Name_Len - 2 loop
3834 if Name_Buffer (J) = '.' then
3835 Set_String ("__");
3836 else
3837 Set_Char (Name_Buffer (J));
3838 end if;
3839 end loop;
3840 end Set_Main_Program_Name;
3841
3842 ---------------------
3843 -- Set_Name_Buffer --
3844 ---------------------
3845
3846 procedure Set_Name_Buffer is
3847 begin
3848 for J in 1 .. Name_Len loop
3849 Set_Char (Name_Buffer (J));
3850 end loop;
3851 end Set_Name_Buffer;
3852
3853 -------------------------
3854 -- Set_PSD_Pragma_Table --
3855 -------------------------
3856
3857 procedure Set_PSD_Pragma_Table is
3858 begin
3859 for F in ALIs.First .. ALIs.Last loop
3860 for K in ALIs.Table (F).First_Specific_Dispatching ..
3861 ALIs.Table (F).Last_Specific_Dispatching
3862 loop
3863 declare
3864 DTK : Specific_Dispatching_Record
3865 renames Specific_Dispatching.Table (K);
3866
3867 begin
3868 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3869 PSD_Pragma_Settings.Append ('F');
3870 end loop;
3871
3872 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3873 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3874 end loop;
3875 end;
3876 end loop;
3877 end loop;
3878 end Set_PSD_Pragma_Table;
3879
3880 ----------------
3881 -- Set_String --
3882 ----------------
3883
3884 procedure Set_String (S : String) is
3885 begin
3886 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3887 Last := Last + S'Length;
3888 end Set_String;
3889
3890 ------------------------
3891 -- Set_String_Replace --
3892 ------------------------
3893
3894 procedure Set_String_Replace (S : String) is
3895 begin
3896 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3897 end Set_String_Replace;
3898
3899 -------------------
3900 -- Set_Unit_Name --
3901 -------------------
3902
3903 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores) is
3904 begin
3905 for J in 1 .. Name_Len - 2 loop
3906 if Name_Buffer (J) = '.' then
3907 if Mode = Double_Underscores then
3908 Set_String ("__");
3909 elsif Mode = Dot then
3910 Set_Char ('.');
3911 else
3912 Set_Char ('$');
3913 end if;
3914 else
3915 Set_Char (Name_Buffer (J));
3916 end if;
3917 end loop;
3918 end Set_Unit_Name;
3919
3920 ---------------------
3921 -- Set_Unit_Number --
3922 ---------------------
3923
3924 procedure Set_Unit_Number (U : Unit_Id) is
3925 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3926 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3927
3928 begin
3929 if Num_Units >= 10 and then Unum < 10 then
3930 Set_Char ('0');
3931 end if;
3932
3933 if Num_Units >= 100 and then Unum < 100 then
3934 Set_Char ('0');
3935 end if;
3936
3937 Set_Int (Unum);
3938 end Set_Unit_Number;
3939
3940 ----------------------
3941 -- Write_Info_Ada_C --
3942 ----------------------
3943
3944 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3945 begin
3946 if Ada_Bind_File then
3947 declare
3948 S : String (1 .. Ada'Length + Common'Length);
3949 begin
3950 S (1 .. Ada'Length) := Ada;
3951 S (Ada'Length + 1 .. S'Length) := Common;
3952 WBI (S);
3953 end;
3954
3955 else
3956 declare
3957 S : String (1 .. C'Length + Common'Length);
3958 begin
3959 S (1 .. C'Length) := C;
3960 S (C'Length + 1 .. S'Length) := Common;
3961 WBI (S);
3962 end;
3963 end if;
3964 end Write_Info_Ada_C;
3965
3966 ----------------------------
3967 -- Write_Statement_Buffer --
3968 ----------------------------
3969
3970 procedure Write_Statement_Buffer is
3971 begin
3972 WBI (Statement_Buffer (1 .. Last));
3973 Last := 0;
3974 end Write_Statement_Buffer;
3975
3976 procedure Write_Statement_Buffer (S : String) is
3977 begin
3978 Set_String (S);
3979 Write_Statement_Buffer;
3980 end Write_Statement_Buffer;
3981
3982 end Bindgen;