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