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