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