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