Properly regenerate gcc/configure.
[official-gcc.git] / gcc / ada / bindgen.adb
blob8979b7736bf3f5e54f5db1e5f61d864389579077
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-2014, 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 ------------------------------------------------------------------------------
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;
42 with System.OS_Lib; use System.OS_Lib;
43 with System.WCh_Con; use System.WCh_Con;
45 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
47 package body Bindgen is
49 Statement_Buffer : String (1 .. 1000);
50 -- Buffer used for constructing output statements
52 Last : Natural := 0;
53 -- Last location in Statement_Buffer currently set
55 With_GNARL : Boolean := False;
56 -- Flag which indicates whether the program uses the GNARL library
57 -- (presence of the unit System.OS_Interface)
59 Num_Elab_Calls : Nat := 0;
60 -- Number of generated calls to elaboration routines
62 System_Restrictions_Used : Boolean := False;
63 -- Flag indicating whether the unit System.Restrictions is in the closure
64 -- of the partition. This is set by Resolve_Binder_Options, and is used
65 -- to determine whether or not to initialize the restrictions information
66 -- in the body of the binder generated file (we do not want to do this
67 -- unconditionally, since it drags in the System.Restrictions unit
68 -- unconditionally, which is unpleasand, especially for ZFP etc.)
70 Dispatching_Domains_Used : Boolean := False;
71 -- Flag indicating whether multiprocessor dispatching domains are used in
72 -- the closure of the partition. This is set by Resolve_Binder_Options, and
73 -- is used to call the routine to disallow the creation of new dispatching
74 -- domains just before calling the main procedure from the environment
75 -- task.
77 System_Tasking_Restricted_Stages_Used : Boolean := False;
78 -- Flag indicating whether the unit System.Tasking.Restricted.Stages is in
79 -- the closure of the partition. This is set by Resolve_Binder_Options,
80 -- and it used to call a routine to active all the tasks at the end of
81 -- the elaboration when partition elaboration policy is sequential.
83 System_Interrupts_Used : Boolean := False;
84 -- Flag indicating whether the unit System.Interrups is in the closure of
85 -- the partition. This is set by Resolve_Binder_Options, and it used to
86 -- attach interrupt handlers at the end of the elaboration when partition
87 -- elaboration policy is sequential.
89 Lib_Final_Built : Boolean := False;
90 -- Flag indicating whether the finalize_library rountine has been built
92 CodePeer_Wrapper_Name : constant String := "call_main_subprogram";
93 -- For CodePeer, introduce a wrapper subprogram which calls the
94 -- user-defined main subprogram.
96 ----------------------------------
97 -- Interface_State Pragma Table --
98 ----------------------------------
100 -- This table assembles the interface state pragma information from
101 -- all the units in the partition. Note that Bcheck has already checked
102 -- that the information is consistent across units. The entries
103 -- in this table are n/u/r/s for not set/user/runtime/system.
105 package IS_Pragma_Settings is new Table.Table (
106 Table_Component_Type => Character,
107 Table_Index_Type => Int,
108 Table_Low_Bound => 0,
109 Table_Initial => 100,
110 Table_Increment => 200,
111 Table_Name => "IS_Pragma_Settings");
113 -- This table assembles the Priority_Specific_Dispatching pragma
114 -- information from all the units in the partition. Note that Bcheck has
115 -- already checked that the information is consistent across units.
116 -- The entries in this table are the upper case first character of the
117 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
119 package PSD_Pragma_Settings is new Table.Table (
120 Table_Component_Type => Character,
121 Table_Index_Type => Int,
122 Table_Low_Bound => 0,
123 Table_Initial => 100,
124 Table_Increment => 200,
125 Table_Name => "PSD_Pragma_Settings");
127 ----------------------
128 -- Run-Time Globals --
129 ----------------------
131 -- This section documents the global variables that are set from the
132 -- generated binder file.
134 -- Main_Priority : Integer;
135 -- Time_Slice_Value : Integer;
136 -- Heap_Size : Natural;
137 -- WC_Encoding : Character;
138 -- Locking_Policy : Character;
139 -- Queuing_Policy : Character;
140 -- Task_Dispatching_Policy : Character;
141 -- Priority_Specific_Dispatching : System.Address;
142 -- Num_Specific_Dispatching : Integer;
143 -- Restrictions : System.Address;
144 -- Interrupt_States : System.Address;
145 -- Num_Interrupt_States : Integer;
146 -- Unreserve_All_Interrupts : Integer;
147 -- Exception_Tracebacks : Integer;
148 -- Detect_Blocking : Integer;
149 -- Default_Stack_Size : Integer;
150 -- Leap_Seconds_Support : Integer;
151 -- Main_CPU : Integer;
153 -- Main_Priority is the priority value set by pragma Priority in the main
154 -- program. If no such pragma is present, the value is -1.
156 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
157 -- main program, or by the use of a -Tnnn parameter for the binder (if both
158 -- are present, the binder value overrides). The value is in milliseconds.
159 -- A value of zero indicates that time slicing should be suppressed. If no
160 -- pragma is present, and no -T switch was used, the value is -1.
162 -- WC_Encoding shows the wide character encoding method used for the main
163 -- program. This is one of the encoding letters defined in
164 -- System.WCh_Con.WC_Encoding_Letters.
166 -- Locking_Policy is a space if no locking policy was specified for the
167 -- partition. If a locking policy was specified, the value is the upper
168 -- case first character of the locking policy name, for example, 'C' for
169 -- Ceiling_Locking.
171 -- Queuing_Policy is a space if no queuing policy was specified for the
172 -- partition. If a queuing policy was specified, the value is the upper
173 -- case first character of the queuing policy name for example, 'F' for
174 -- FIFO_Queuing.
176 -- Task_Dispatching_Policy is a space if no task dispatching policy was
177 -- specified for the partition. If a task dispatching policy was specified,
178 -- the value is the upper case first character of the policy name, e.g. 'F'
179 -- for FIFO_Within_Priorities.
181 -- Priority_Specific_Dispatching is the address of a string used to store
182 -- the task dispatching policy specified for the different priorities in
183 -- the partition. The length of this string is determined by the last
184 -- priority for which such a pragma applies (the string will be a null
185 -- string if no specific dispatching policies were used). If pragma were
186 -- present, the entries apply to the priorities in sequence from the first
187 -- priority. The value stored is the upper case first character of the
188 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
189 -- for those priority ranges not specified.
191 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
192 -- string. It will be set to zero if no Priority_Specific_Dispatching
193 -- pragmas are present.
195 -- Restrictions is the address of a null-terminated string specifying the
196 -- restrictions information for the partition. The format is identical to
197 -- that of the parameter string found on R lines in ali files (see Lib.Writ
198 -- spec in lib-writ.ads for full details). The difference is that in this
199 -- context the values are the cumulative ones for the entire partition.
201 -- Interrupt_States is the address of a string used to specify the
202 -- cumulative results of Interrupt_State pragmas used in the partition.
203 -- The length of this string is determined by the last interrupt for which
204 -- such a pragma is given (the string will be a null string if no pragmas
205 -- were used). If pragma were present the entries apply to the interrupts
206 -- in sequence from the first interrupt, and are set to one of four
207 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
208 -- time, 's' for system, see description of Interrupt_State pragma for
209 -- further details.
211 -- Num_Interrupt_States is the length of the Interrupt_States string. It
212 -- will be set to zero if no Interrupt_State pragmas are present.
214 -- Unreserve_All_Interrupts is set to one if at least one unit in the
215 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
217 -- Exception_Tracebacks is set to one if the -E parameter was present
218 -- in the bind and to zero otherwise. Note that on some targets exception
219 -- tracebacks are provided by default, so a value of zero for this
220 -- parameter does not necessarily mean no trace backs are available.
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.
226 -- Default_Stack_Size is the default stack size used when creating an Ada
227 -- task with no explicit Storage_Size clause.
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.
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.
236 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
237 -- Convenient shorthand used throughout
239 -----------------------
240 -- Local Subprograms --
241 -----------------------
243 procedure Gen_Adainit;
244 -- Generates the Adainit procedure
246 procedure Gen_Adafinal;
247 -- Generate the Adafinal procedure
249 procedure Gen_CodePeer_Wrapper;
250 -- For CodePeer, generate wrapper which calls user-defined main subprogram
252 procedure Gen_Elab_Calls;
253 -- Generate sequence of elaboration calls
255 procedure Gen_Elab_Externals;
256 -- Generate sequence of external declarations for elaboration
258 procedure Gen_Elab_Order;
259 -- Generate comments showing elaboration order chosen
261 procedure Gen_Finalize_Library;
262 -- Generate a sequence of finalization calls to elaborated packages
264 procedure Gen_Main;
265 -- Generate procedure main
267 procedure Gen_Object_Files_Options;
268 -- Output comments containing a list of the full names of the object
269 -- files to be linked and the list of linker options supplied by
270 -- Linker_Options pragmas in the source.
272 procedure Gen_Output_File_Ada (Filename : String);
273 -- Generate Ada output file
275 procedure Gen_Restrictions;
276 -- Generate initialization of restrictions variable
278 procedure Gen_Versions;
279 -- Output series of definitions for unit versions
281 function Get_Ada_Main_Name return String;
282 -- This function is used for the Ada main output to compute a usable name
283 -- for the generated main program. The normal main program name is
284 -- Ada_Main, but this won't work if the user has a unit with this name.
285 -- This function tries Ada_Main first, and if there is such a clash, then
286 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
288 function Get_Main_Unit_Name (S : String) return String;
289 -- Return the main unit name corresponding to S by replacing '.' with '_'
291 function Get_Main_Name return String;
292 -- This function is used in the main output case to compute the correct
293 -- external main program. It is "main" by default, unless the flag
294 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
295 -- of the Ada main name without the "_ada". This default can be overridden
296 -- explicitly using the -Mname binder switch.
298 function Get_WC_Encoding return Character;
299 -- Return wide character encoding method to set as WC_Encoding in output.
300 -- If -W has been used, returns the specified encoding, otherwise returns
301 -- the encoding method used for the main program source. If there is no
302 -- main program source (-z switch used), returns brackets ('b').
304 function Has_Finalizer return Boolean;
305 -- Determine whether the current unit has at least one library-level
306 -- finalizer.
308 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
309 -- Compare linker options, when sorting, first according to
310 -- Is_Internal_File (internal files come later) and then by
311 -- elaboration order position (latest to earliest).
313 procedure Move_Linker_Option (From : Natural; To : Natural);
314 -- Move routine for sorting linker options
316 procedure Resolve_Binder_Options;
317 -- Set the value of With_GNARL
319 procedure Set_Char (C : Character);
320 -- Set given character in Statement_Buffer at the Last + 1 position
321 -- and increment Last by one to reflect the stored character.
323 procedure Set_Int (N : Int);
324 -- Set given value in decimal in Statement_Buffer with no spaces starting
325 -- at the Last + 1 position, and updating Last past the value. A minus sign
326 -- is output for a negative value.
328 procedure Set_Boolean (B : Boolean);
329 -- Set given boolean value in Statement_Buffer at the Last + 1 position
330 -- and update Last past the value.
332 procedure Set_IS_Pragma_Table;
333 -- Initializes contents of IS_Pragma_Settings table from ALI table
335 procedure Set_Main_Program_Name;
336 -- Given the main program name in Name_Buffer (length in Name_Len) generate
337 -- the name of the routine to be used in the call. The name is generated
338 -- starting at Last + 1, and Last is updated past it.
340 procedure Set_Name_Buffer;
341 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
343 procedure Set_PSD_Pragma_Table;
344 -- Initializes contents of PSD_Pragma_Settings table from ALI table
346 procedure Set_String (S : String);
347 -- Sets characters of given string in Statement_Buffer, starting at the
348 -- Last + 1 position, and updating last past the string value.
350 procedure Set_String_Replace (S : String);
351 -- Replaces the last S'Length characters in the Statement_Buffer with the
352 -- characters of S. The caller must ensure that these characters do in fact
353 -- exist in the Statement_Buffer.
355 type Qualification_Mode is (Dollar_Sign, Dot, Double_Underscores);
357 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores);
358 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
359 -- starting at the Last + 1 position and update Last past the value.
360 -- Depending on parameter Mode, a dot (.) can be qualified into double
361 -- underscores (__), a dollar sign ($) or left as is.
363 procedure Set_Unit_Number (U : Unit_Id);
364 -- Sets unit number (first unit is 1, leading zeroes output to line up all
365 -- output unit numbers nicely as required by the value, and by the total
366 -- number of units.
368 procedure Write_Statement_Buffer;
369 -- Write out contents of statement buffer up to Last, and reset Last to 0
371 procedure Write_Statement_Buffer (S : String);
372 -- First writes its argument (using Set_String (S)), then writes out the
373 -- contents of statement buffer up to Last, and reset Last to 0
375 ------------------
376 -- Gen_Adafinal --
377 ------------------
379 procedure Gen_Adafinal is
380 begin
381 WBI (" procedure " & Ada_Final_Name.all & " is");
383 if VM_Target = No_VM
384 and Bind_Main_Program
385 and not CodePeer_Mode
386 then
387 WBI (" procedure s_stalib_adafinal;");
388 Set_String (" pragma Import (C, s_stalib_adafinal, ");
389 Set_String ("""system__standard_library__adafinal"");");
390 Write_Statement_Buffer;
391 end if;
393 WBI (" begin");
395 if not CodePeer_Mode then
396 WBI (" if not Is_Elaborated then");
397 WBI (" return;");
398 WBI (" end if;");
399 WBI (" Is_Elaborated := False;");
400 end if;
402 -- On non-virtual machine targets, finalization is done differently
403 -- depending on whether this is the main program or a library.
405 if VM_Target = No_VM and then not CodePeer_Mode then
406 if Bind_Main_Program then
407 WBI (" s_stalib_adafinal;");
408 elsif Lib_Final_Built then
409 WBI (" finalize_library;");
410 else
411 WBI (" null;");
412 end if;
414 -- Pragma Import C cannot be used on virtual machine targets, therefore
415 -- call the runtime finalization routine directly. Similarly in CodePeer
416 -- mode, where imported functions are ignored.
418 else
419 WBI (" System.Standard_Library.Adafinal;");
420 end if;
422 WBI (" end " & Ada_Final_Name.all & ";");
423 WBI ("");
424 end Gen_Adafinal;
426 -----------------
427 -- Gen_Adainit --
428 -----------------
430 procedure Gen_Adainit is
431 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
432 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
434 begin
435 -- Declare the access-to-subprogram type used for initialization of
436 -- of __gnat_finalize_library_objects. This is declared at library
437 -- level for compatibility with the type used in System.Soft_Links.
438 -- The import of the soft link which performs library-level object
439 -- finalization is not needed for VM targets; regular Ada is used in
440 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
441 -- tasks are non-terminating, so we do not want finalization.
443 if not Suppress_Standard_Library_On_Target
444 and then VM_Target = No_VM
445 and then not CodePeer_Mode
446 and then not Configurable_Run_Time_On_Target
447 then
448 WBI (" type No_Param_Proc is access procedure;");
449 WBI ("");
450 end if;
452 WBI (" procedure " & Ada_Init_Name.all & " is");
454 -- In CodePeer mode, simplify adainit procedure by only calling
455 -- elaboration procedures.
457 if CodePeer_Mode then
458 WBI (" begin");
460 -- When compiling for the AAMP small library, where the standard library
461 -- is no longer suppressed, we still want to exclude the setting of the
462 -- various imported globals, which aren't present for that library.
464 elsif AAMP_On_Target and then Configurable_Run_Time_On_Target then
465 WBI (" begin");
466 WBI (" null;");
468 -- If the standard library is suppressed, then the only global variables
469 -- that might be needed (by the Ravenscar profile) are the priority and
470 -- the processor for the environment task.
472 elsif Suppress_Standard_Library_On_Target then
473 if Main_Priority /= No_Main_Priority then
474 WBI (" Main_Priority : Integer;");
475 WBI (" pragma Import (C, Main_Priority," &
476 " ""__gl_main_priority"");");
477 WBI ("");
478 end if;
480 if Main_CPU /= No_Main_CPU then
481 WBI (" Main_CPU : Integer;");
482 WBI (" pragma Import (C, Main_CPU," &
483 " ""__gl_main_cpu"");");
484 WBI ("");
485 end if;
487 if System_Interrupts_Used
488 and then Partition_Elaboration_Policy_Specified = 'S'
489 then
490 WBI (" procedure Install_Restricted_Handlers_Sequential;");
491 WBI (" pragma Import (C," &
492 "Install_Restricted_Handlers_Sequential," &
493 " ""__gnat_attach_all_handlers"");");
494 WBI ("");
495 end if;
497 if System_Tasking_Restricted_Stages_Used
498 and then Partition_Elaboration_Policy_Specified = 'S'
499 then
500 WBI (" Partition_Elaboration_Policy : Character;");
501 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
502 " ""__gnat_partition_elaboration_policy"");");
503 WBI ("");
504 WBI (" procedure Activate_All_Tasks_Sequential;");
505 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
506 " ""__gnat_activate_all_tasks"");");
507 end if;
509 WBI (" begin");
511 if Main_Priority /= No_Main_Priority then
512 Set_String (" Main_Priority := ");
513 Set_Int (Main_Priority);
514 Set_Char (';');
515 Write_Statement_Buffer;
516 end if;
518 if Main_CPU /= No_Main_CPU then
519 Set_String (" Main_CPU := ");
520 Set_Int (Main_CPU);
521 Set_Char (';');
522 Write_Statement_Buffer;
523 end if;
525 if System_Tasking_Restricted_Stages_Used
526 and then Partition_Elaboration_Policy_Specified = 'S'
527 then
528 Set_String (" Partition_Elaboration_Policy := '");
529 Set_Char (Partition_Elaboration_Policy_Specified);
530 Set_String ("';");
531 Write_Statement_Buffer;
532 end if;
534 if Main_Priority = No_Main_Priority
535 and then Main_CPU = No_Main_CPU
536 and then not System_Tasking_Restricted_Stages_Used
537 then
538 WBI (" null;");
539 end if;
541 -- Normal case (standard library not suppressed). Set all global values
542 -- used by the run time.
544 else
545 WBI (" Main_Priority : Integer;");
546 WBI (" pragma Import (C, Main_Priority, " &
547 """__gl_main_priority"");");
548 WBI (" Time_Slice_Value : Integer;");
549 WBI (" pragma Import (C, Time_Slice_Value, " &
550 """__gl_time_slice_val"");");
551 WBI (" WC_Encoding : Character;");
552 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
553 WBI (" Locking_Policy : Character;");
554 WBI (" pragma Import (C, Locking_Policy, " &
555 """__gl_locking_policy"");");
556 WBI (" Queuing_Policy : Character;");
557 WBI (" pragma Import (C, Queuing_Policy, " &
558 """__gl_queuing_policy"");");
559 WBI (" Task_Dispatching_Policy : Character;");
560 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
561 """__gl_task_dispatching_policy"");");
562 WBI (" Priority_Specific_Dispatching : System.Address;");
563 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
564 """__gl_priority_specific_dispatching"");");
565 WBI (" Num_Specific_Dispatching : Integer;");
566 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
567 """__gl_num_specific_dispatching"");");
568 WBI (" Main_CPU : Integer;");
569 WBI (" pragma Import (C, Main_CPU, " &
570 """__gl_main_cpu"");");
572 WBI (" Interrupt_States : System.Address;");
573 WBI (" pragma Import (C, Interrupt_States, " &
574 """__gl_interrupt_states"");");
575 WBI (" Num_Interrupt_States : Integer;");
576 WBI (" pragma Import (C, Num_Interrupt_States, " &
577 """__gl_num_interrupt_states"");");
578 WBI (" Unreserve_All_Interrupts : Integer;");
579 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
580 """__gl_unreserve_all_interrupts"");");
582 if Exception_Tracebacks then
583 WBI (" Exception_Tracebacks : Integer;");
584 WBI (" pragma Import (C, Exception_Tracebacks, " &
585 """__gl_exception_tracebacks"");");
586 end if;
588 WBI (" Detect_Blocking : Integer;");
589 WBI (" pragma Import (C, Detect_Blocking, " &
590 """__gl_detect_blocking"");");
591 WBI (" Default_Stack_Size : Integer;");
592 WBI (" pragma Import (C, Default_Stack_Size, " &
593 """__gl_default_stack_size"");");
594 WBI (" Leap_Seconds_Support : Integer;");
595 WBI (" pragma Import (C, Leap_Seconds_Support, " &
596 """__gl_leap_seconds_support"");");
598 -- Import entry point for elaboration time signal handler
599 -- installation, and indication of if it's been called previously.
601 WBI ("");
602 WBI (" procedure Install_Handler;");
603 WBI (" pragma Import (C, Install_Handler, " &
604 """__gnat_install_handler"");");
605 WBI ("");
606 WBI (" Handler_Installed : Integer;");
607 WBI (" pragma Import (C, Handler_Installed, " &
608 """__gnat_handler_installed"");");
610 -- Import handlers attach procedure for sequential elaboration policy
612 if System_Interrupts_Used
613 and then Partition_Elaboration_Policy_Specified = 'S'
614 then
615 WBI (" procedure Install_Restricted_Handlers_Sequential;");
616 WBI (" pragma Import (C," &
617 "Install_Restricted_Handlers_Sequential," &
618 " ""__gnat_attach_all_handlers"");");
619 WBI ("");
620 end if;
622 -- Import task activation procedure for sequential elaboration
623 -- policy.
625 if System_Tasking_Restricted_Stages_Used
626 and then Partition_Elaboration_Policy_Specified = 'S'
627 then
628 WBI (" Partition_Elaboration_Policy : Character;");
629 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
630 " ""__gnat_partition_elaboration_policy"");");
631 WBI ("");
632 WBI (" procedure Activate_All_Tasks_Sequential;");
633 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
634 " ""__gnat_activate_all_tasks"");");
635 end if;
637 -- The import of the soft link which performs library-level object
638 -- finalization is not needed for VM targets; regular Ada is used in
639 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
640 -- tasks are non-terminating, so we do not want finalization.
642 if VM_Target = No_VM and then not Configurable_Run_Time_On_Target then
643 WBI ("");
644 WBI (" Finalize_Library_Objects : No_Param_Proc;");
645 WBI (" pragma Import (C, Finalize_Library_Objects, " &
646 """__gnat_finalize_library_objects"");");
647 end if;
649 -- Initialize stack limit variable of the environment task if the
650 -- stack check method is stack limit and stack check is enabled.
652 if Stack_Check_Limits_On_Target
653 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
654 then
655 WBI ("");
656 WBI (" procedure Initialize_Stack_Limit;");
657 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
658 """__gnat_initialize_stack_limit"");");
659 end if;
661 -- Special processing when main program is CIL function/procedure
663 if VM_Target = CLI_Target
664 and then Bind_Main_Program
665 and then not No_Main_Subprogram
666 then
667 WBI ("");
669 -- Function case, use Set_Exit_Status to report the returned
670 -- status code, since that is the only mechanism available.
672 if ALIs.Table (ALIs.First).Main_Program = Func then
673 WBI (" Result : Integer;");
674 WBI (" procedure Set_Exit_Status (Code : Integer);");
675 WBI (" pragma Import (C, Set_Exit_Status, " &
676 """__gnat_set_exit_status"");");
677 WBI ("");
678 WBI (" function Ada_Main_Program return Integer;");
680 -- Procedure case
682 else
683 WBI (" procedure Ada_Main_Program;");
684 end if;
686 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
687 Name_Len := Name_Len - 2;
688 WBI (" pragma Import (CIL, Ada_Main_Program, """
689 & Name_Buffer (1 .. Name_Len) & "."
690 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
691 end if;
693 -- When dispatching domains are used then we need to signal it
694 -- before calling the main procedure.
696 if Dispatching_Domains_Used then
697 WBI (" procedure Freeze_Dispatching_Domains;");
698 WBI (" pragma Import");
699 WBI (" (Ada, Freeze_Dispatching_Domains, "
700 & """__gnat_freeze_dispatching_domains"");");
701 end if;
703 WBI (" begin");
704 WBI (" if Is_Elaborated then");
705 WBI (" return;");
706 WBI (" end if;");
707 WBI (" Is_Elaborated := True;");
709 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
710 -- restriction No_Standard_Allocators_After_Elaboration is active.
712 if Cumulative_Restrictions.Set
713 (No_Standard_Allocators_After_Elaboration)
714 then
715 WBI (" System.Elaboration_Allocators."
716 & "Mark_Start_Of_Elaboration;");
717 end if;
719 -- Generate assignments to initialize globals
721 Set_String (" Main_Priority := ");
722 Set_Int (Main_Priority);
723 Set_Char (';');
724 Write_Statement_Buffer;
726 Set_String (" Time_Slice_Value := ");
728 if Task_Dispatching_Policy_Specified = 'F'
729 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
730 then
731 Set_Int (0);
732 else
733 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
734 end if;
736 Set_Char (';');
737 Write_Statement_Buffer;
739 Set_String (" WC_Encoding := '");
740 Set_Char (Get_WC_Encoding);
742 Set_String ("';");
743 Write_Statement_Buffer;
745 Set_String (" Locking_Policy := '");
746 Set_Char (Locking_Policy_Specified);
747 Set_String ("';");
748 Write_Statement_Buffer;
750 Set_String (" Queuing_Policy := '");
751 Set_Char (Queuing_Policy_Specified);
752 Set_String ("';");
753 Write_Statement_Buffer;
755 Set_String (" Task_Dispatching_Policy := '");
756 Set_Char (Task_Dispatching_Policy_Specified);
757 Set_String ("';");
758 Write_Statement_Buffer;
760 if System_Tasking_Restricted_Stages_Used
761 and then Partition_Elaboration_Policy_Specified = 'S'
762 then
763 Set_String (" Partition_Elaboration_Policy := '");
764 Set_Char (Partition_Elaboration_Policy_Specified);
765 Set_String ("';");
766 Write_Statement_Buffer;
767 end if;
769 Gen_Restrictions;
771 WBI (" Priority_Specific_Dispatching :=");
772 WBI (" Local_Priority_Specific_Dispatching'Address;");
774 Set_String (" Num_Specific_Dispatching := ");
775 Set_Int (PSD_Pragma_Settings.Last + 1);
776 Set_Char (';');
777 Write_Statement_Buffer;
779 Set_String (" Main_CPU := ");
780 Set_Int (Main_CPU);
781 Set_Char (';');
782 Write_Statement_Buffer;
784 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
786 Set_String (" Num_Interrupt_States := ");
787 Set_Int (IS_Pragma_Settings.Last + 1);
788 Set_Char (';');
789 Write_Statement_Buffer;
791 Set_String (" Unreserve_All_Interrupts := ");
793 if Unreserve_All_Interrupts_Specified then
794 Set_String ("1");
795 else
796 Set_String ("0");
797 end if;
799 Set_Char (';');
800 Write_Statement_Buffer;
802 if Exception_Tracebacks then
803 WBI (" Exception_Tracebacks := 1;");
804 end if;
806 Set_String (" Detect_Blocking := ");
808 if Detect_Blocking then
809 Set_Int (1);
810 else
811 Set_Int (0);
812 end if;
814 Set_String (";");
815 Write_Statement_Buffer;
817 Set_String (" Default_Stack_Size := ");
818 Set_Int (Default_Stack_Size);
819 Set_String (";");
820 Write_Statement_Buffer;
822 Set_String (" Leap_Seconds_Support := ");
824 if Leap_Seconds_Support then
825 Set_Int (1);
826 else
827 Set_Int (0);
828 end if;
830 Set_String (";");
831 Write_Statement_Buffer;
833 -- Generate call to Install_Handler
835 -- In .NET, when binding with -z, we don't install the signal handler
836 -- to let the caller handle the last exception handler.
838 if VM_Target /= CLI_Target
839 or else Bind_Main_Program
840 then
841 WBI ("");
842 WBI (" if Handler_Installed = 0 then");
843 WBI (" Install_Handler;");
844 WBI (" end if;");
845 end if;
846 end if;
848 -- Generate call to set Initialize_Scalar values if active
850 if Initialize_Scalars_Used then
851 WBI ("");
852 Set_String (" System.Scalar_Values.Initialize ('");
853 Set_Char (Initialize_Scalars_Mode1);
854 Set_String ("', '");
855 Set_Char (Initialize_Scalars_Mode2);
856 Set_String ("');");
857 Write_Statement_Buffer;
858 end if;
860 -- Generate assignment of default secondary stack size if set
862 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
863 WBI ("");
864 Set_String (" System.Secondary_Stack.");
865 Set_String ("Default_Secondary_Stack_Size := ");
866 Set_Int (Opt.Default_Sec_Stack_Size);
867 Set_Char (';');
868 Write_Statement_Buffer;
869 end if;
871 -- Initialize stack limit variable of the environment task if the
872 -- stack check method is stack limit and stack check is enabled.
874 if Stack_Check_Limits_On_Target
875 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
876 then
877 WBI ("");
878 WBI (" Initialize_Stack_Limit;");
879 end if;
881 -- On CodePeer, the finalization of library objects is not relevant
883 if CodePeer_Mode then
884 null;
886 -- On virtual machine targets, or on non-virtual machine ones if this
887 -- is the main program case, attach finalize_library to the soft link.
888 -- Do it only when not using a restricted run time, in which case tasks
889 -- are non-terminating, so we do not want library-level finalization.
891 elsif (VM_Target /= No_VM or else Bind_Main_Program)
892 and then not Configurable_Run_Time_On_Target
893 and then not Suppress_Standard_Library_On_Target
894 then
895 WBI ("");
897 if VM_Target = No_VM then
898 if Lib_Final_Built then
899 Set_String (" Finalize_Library_Objects := ");
900 Set_String ("finalize_library'access;");
901 else
902 Set_String (" Finalize_Library_Objects := null;");
903 end if;
905 -- On VM targets use regular Ada to set the soft link
907 else
908 if Lib_Final_Built then
909 Set_String
910 (" System.Soft_Links.Finalize_Library_Objects");
911 Set_String (" := finalize_library'access;");
912 else
913 Set_String
914 (" System.Soft_Links.Finalize_Library_Objects");
915 Set_String (" := null;");
916 end if;
917 end if;
919 Write_Statement_Buffer;
920 end if;
922 -- Generate elaboration calls
924 if not CodePeer_Mode then
925 WBI ("");
926 end if;
928 Gen_Elab_Calls;
930 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
931 -- restriction No_Standard_Allocators_After_Elaboration is active.
933 if Cumulative_Restrictions.Set
934 (No_Standard_Allocators_After_Elaboration)
935 then
936 WBI (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
937 end if;
939 -- From this point, no new dispatching domain can be created.
941 if Dispatching_Domains_Used then
942 WBI (" Freeze_Dispatching_Domains;");
943 end if;
945 -- Sequential partition elaboration policy
947 if Partition_Elaboration_Policy_Specified = 'S' then
948 if System_Interrupts_Used then
949 WBI (" Install_Restricted_Handlers_Sequential;");
950 end if;
952 if System_Tasking_Restricted_Stages_Used then
953 WBI (" Activate_All_Tasks_Sequential;");
954 end if;
955 end if;
957 -- Case of main program is CIL function or procedure
959 if VM_Target = CLI_Target
960 and then Bind_Main_Program
961 and then not No_Main_Subprogram
962 then
963 -- For function case, use Set_Exit_Status to set result
965 if ALIs.Table (ALIs.First).Main_Program = Func then
966 WBI (" Result := Ada_Main_Program;");
967 WBI (" Set_Exit_Status (Result);");
969 -- Procedure case
971 else
972 WBI (" Ada_Main_Program;");
973 end if;
974 end if;
976 WBI (" end " & Ada_Init_Name.all & ";");
977 WBI ("");
978 end Gen_Adainit;
980 --------------------------
981 -- Gen_CodePeer_Wrapper --
982 --------------------------
984 procedure Gen_CodePeer_Wrapper is
985 Callee_Name : constant String := "Ada_Main_Program";
987 begin
988 if ALIs.Table (ALIs.First).Main_Program = Proc then
989 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
990 WBI (" begin");
991 WBI (" " & Callee_Name & ";");
993 else
994 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
995 WBI (" begin");
996 WBI (" return " & Callee_Name & ";");
997 end if;
999 WBI (" end " & CodePeer_Wrapper_Name & ";");
1000 WBI ("");
1001 end Gen_CodePeer_Wrapper;
1003 --------------------
1004 -- Gen_Elab_Calls --
1005 --------------------
1007 procedure Gen_Elab_Calls is
1008 Check_Elab_Flag : Boolean;
1010 begin
1011 -- Loop through elaboration order entries
1013 for E in Elab_Order.First .. Elab_Order.Last loop
1014 declare
1015 Unum : constant Unit_Id := Elab_Order.Table (E);
1016 U : Unit_Record renames Units.Table (Unum);
1018 Unum_Spec : Unit_Id;
1019 -- This is the unit number of the spec that corresponds to
1020 -- this entry. It is the same as Unum except when the body
1021 -- and spec are different and we are currently processing
1022 -- the body, in which case it is the spec (Unum + 1).
1024 begin
1025 if U.Utype = Is_Body then
1026 Unum_Spec := Unum + 1;
1027 else
1028 Unum_Spec := Unum;
1029 end if;
1031 -- Nothing to do if predefined unit in no run time mode
1033 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1034 null;
1036 -- Likewise if this is an interface to a stand alone library
1038 elsif U.SAL_Interface then
1039 null;
1041 -- Case of no elaboration code
1043 elsif U.No_Elab
1045 -- In CodePeer mode, we special case subprogram bodies which
1046 -- are handled in the 'else' part below, and lead to a call
1047 -- to <subp>'Elab_Subp_Body.
1049 and then (not CodePeer_Mode
1051 -- Test for spec
1053 or else U.Utype = Is_Spec
1054 or else U.Utype = Is_Spec_Only
1055 or else U.Unit_Kind /= 's')
1056 then
1057 -- In the case of a body with a separate spec, where the
1058 -- separate spec has an elaboration entity defined, this is
1059 -- where we increment the elaboration entity if one exists
1061 if U.Utype = Is_Body
1062 and then Units.Table (Unum_Spec).Set_Elab_Entity
1063 and then not CodePeer_Mode
1064 then
1065 Set_String (" E");
1066 Set_Unit_Number (Unum_Spec);
1068 -- The AAMP target has no notion of shared libraries, and
1069 -- there's no possibility of reelaboration, so we treat the
1070 -- the elaboration var as a flag instead of a counter and
1071 -- simply set it.
1073 if AAMP_On_Target then
1074 Set_String (" := 1;");
1076 -- Otherwise (normal case), increment elaboration counter
1078 else
1079 Set_String (" := E");
1080 Set_Unit_Number (Unum_Spec);
1081 Set_String (" + 1;");
1082 end if;
1084 Write_Statement_Buffer;
1086 -- In the special case where the target is AAMP and the unit is
1087 -- a spec with a body, the elaboration entity is initialized
1088 -- here. This is done because it's the only way to accomplish
1089 -- initialization of such entities, as there is no mechanism
1090 -- for load time global variable initialization on AAMP.
1092 elsif AAMP_On_Target
1093 and then U.Utype = Is_Spec
1094 and then Units.Table (Unum_Spec).Set_Elab_Entity
1095 then
1096 Set_String (" E");
1097 Set_Unit_Number (Unum_Spec);
1098 Set_String (" := 0;");
1099 Write_Statement_Buffer;
1100 end if;
1102 -- Here if elaboration code is present. If binding a library
1103 -- or if there is a non-Ada main subprogram then we generate:
1105 -- if uname_E = 0 then
1106 -- uname'elab_[spec|body];
1107 -- end if;
1108 -- uname_E := uname_E + 1;
1110 -- Otherwise, elaboration routines are called unconditionally:
1112 -- uname'elab_[spec|body];
1113 -- uname_E := uname_E + 1;
1115 -- The uname_E increment is skipped if this is a separate spec,
1116 -- since it will be done when we process the body.
1118 -- In CodePeer mode, we do not generate any reference to xxx_E
1119 -- variables, only calls to 'Elab* subprograms.
1121 else
1122 -- In the special case where the target is AAMP and the unit is
1123 -- a spec with a body, the elaboration entity is initialized
1124 -- here. This is done because it's the only way to accomplish
1125 -- initialization of such entities, as there is no mechanism
1126 -- for load time global variable initialization on AAMP.
1128 if AAMP_On_Target
1129 and then U.Utype = Is_Spec
1130 and then Units.Table (Unum_Spec).Set_Elab_Entity
1131 then
1132 Set_String (" E");
1133 Set_Unit_Number (Unum_Spec);
1134 Set_String (" := 0;");
1135 Write_Statement_Buffer;
1136 end if;
1138 -- Check incompatibilities with No_Multiple_Elaboration
1140 if not CodePeer_Mode
1141 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1142 then
1143 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1145 if Force_Checking_Of_Elaboration_Flags then
1146 Osint.Fail
1147 ("-F (force elaboration checks) switch not allowed "
1148 & "with restriction No_Multiple_Elaboration active");
1150 -- Interfacing of libraries not allowed
1152 elsif Interface_Library_Unit then
1153 Osint.Fail
1154 ("binding of interfaced libraries not allowed "
1155 & "with restriction No_Multiple_Elaboration active");
1157 -- Non-Ada main program not allowed
1159 elsif not Bind_Main_Program then
1160 Osint.Fail
1161 ("non-Ada main program not allowed "
1162 & "with restriction No_Multiple_Elaboration active");
1163 end if;
1164 end if;
1166 -- OK, see if we need to test elaboration flag
1168 Check_Elab_Flag :=
1169 Units.Table (Unum_Spec).Set_Elab_Entity
1170 and then not CodePeer_Mode
1171 and then (Force_Checking_Of_Elaboration_Flags
1172 or Interface_Library_Unit
1173 or not Bind_Main_Program);
1175 if Check_Elab_Flag then
1176 Set_String (" if E");
1177 Set_Unit_Number (Unum_Spec);
1178 Set_String (" = 0 then");
1179 Write_Statement_Buffer;
1180 Set_String (" ");
1181 end if;
1183 Set_String (" ");
1184 Get_Decoded_Name_String_With_Brackets (U.Uname);
1186 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1187 if Name_Buffer (Name_Len) = 's' then
1188 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1189 "_pkg'elab_spec";
1190 else
1191 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1192 "_pkg'elab_body";
1193 end if;
1195 Name_Len := Name_Len + 12;
1197 else
1198 if Name_Buffer (Name_Len) = 's' then
1199 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1200 "'elab_spec";
1201 Name_Len := Name_Len + 8;
1203 -- Special case in CodePeer mode for subprogram bodies
1204 -- which correspond to CodePeer 'Elab_Subp_Body special
1205 -- init procedure.
1207 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1208 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1209 "'elab_subp_body";
1210 Name_Len := Name_Len + 13;
1212 else
1213 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1214 "'elab_body";
1215 Name_Len := Name_Len + 8;
1216 end if;
1217 end if;
1219 Set_Casing (U.Icasing);
1220 Set_Name_Buffer;
1221 Set_Char (';');
1222 Write_Statement_Buffer;
1224 if Check_Elab_Flag then
1225 WBI (" end if;");
1226 end if;
1228 if U.Utype /= Is_Spec
1229 and then not CodePeer_Mode
1230 and then Units.Table (Unum_Spec).Set_Elab_Entity
1231 then
1232 Set_String (" E");
1233 Set_Unit_Number (Unum_Spec);
1235 -- The AAMP target has no notion of shared libraries, and
1236 -- there's no possibility of reelaboration, so we treat the
1237 -- the elaboration var as a flag instead of a counter and
1238 -- simply set it.
1240 if AAMP_On_Target then
1241 Set_String (" := 1;");
1243 -- Otherwise (normal case), increment elaboration counter
1245 else
1246 Set_String (" := E");
1247 Set_Unit_Number (Unum_Spec);
1248 Set_String (" + 1;");
1249 end if;
1251 Write_Statement_Buffer;
1252 end if;
1253 end if;
1254 end;
1255 end loop;
1256 end Gen_Elab_Calls;
1258 ------------------------
1259 -- Gen_Elab_Externals --
1260 ------------------------
1262 procedure Gen_Elab_Externals is
1263 begin
1264 if CodePeer_Mode then
1265 return;
1266 end if;
1268 for E in Elab_Order.First .. Elab_Order.Last loop
1269 declare
1270 Unum : constant Unit_Id := Elab_Order.Table (E);
1271 U : Unit_Record renames Units.Table (Unum);
1273 begin
1274 -- Check for Elab_Entity to be set for this unit
1276 if U.Set_Elab_Entity
1278 -- Don't generate reference for stand alone library
1280 and then not U.SAL_Interface
1282 -- Don't generate reference for predefined file in No_Run_Time
1283 -- mode, since we don't include the object files in this case
1285 and then not
1286 (No_Run_Time_Mode
1287 and then Is_Predefined_File_Name (U.Sfile))
1288 then
1289 Set_String (" ");
1290 Set_String ("E");
1291 Set_Unit_Number (Unum);
1293 case VM_Target is
1294 when No_VM | JVM_Target =>
1295 Set_String (" : Short_Integer; pragma Import (Ada, ");
1296 when CLI_Target =>
1297 Set_String (" : Short_Integer; pragma Import (CIL, ");
1298 end case;
1300 Set_String ("E");
1301 Set_Unit_Number (Unum);
1302 Set_String (", """);
1303 Get_Name_String (U.Uname);
1305 -- In the case of JGNAT we need to emit an Import name that
1306 -- includes the class name (using '$' separators in the case
1307 -- of a child unit name).
1309 if VM_Target /= No_VM then
1310 for J in 1 .. Name_Len - 2 loop
1311 if VM_Target = CLI_Target
1312 or else Name_Buffer (J) /= '.'
1313 then
1314 Set_Char (Name_Buffer (J));
1315 else
1316 Set_String ("$");
1317 end if;
1318 end loop;
1320 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
1321 Set_String (".");
1322 else
1323 Set_String ("_pkg.");
1324 end if;
1326 -- If the unit name is very long, then split the
1327 -- Import link name across lines using "&" (occurs
1328 -- in some C2 tests).
1330 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
1331 Set_String (""" &");
1332 Write_Statement_Buffer;
1333 Set_String (" """);
1334 end if;
1335 end if;
1337 Set_Unit_Name;
1338 Set_String ("_E"");");
1339 Write_Statement_Buffer;
1340 end if;
1341 end;
1342 end loop;
1344 WBI ("");
1345 end Gen_Elab_Externals;
1347 --------------------
1348 -- Gen_Elab_Order --
1349 --------------------
1351 procedure Gen_Elab_Order is
1352 begin
1353 WBI (" -- BEGIN ELABORATION ORDER");
1355 for J in Elab_Order.First .. Elab_Order.Last loop
1356 Set_String (" -- ");
1357 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1358 Set_Name_Buffer;
1359 Write_Statement_Buffer;
1360 end loop;
1362 WBI (" -- END ELABORATION ORDER");
1363 WBI ("");
1364 end Gen_Elab_Order;
1366 --------------------------
1367 -- Gen_Finalize_Library --
1368 --------------------------
1370 procedure Gen_Finalize_Library is
1371 Count : Int := 1;
1372 U : Unit_Record;
1373 Uspec : Unit_Record;
1374 Unum : Unit_Id;
1376 procedure Gen_Header;
1377 -- Generate the header of the finalization routine
1379 ----------------
1380 -- Gen_Header --
1381 ----------------
1383 procedure Gen_Header is
1384 begin
1385 WBI (" procedure finalize_library is");
1386 WBI (" begin");
1387 end Gen_Header;
1389 -- Start of processing for Gen_Finalize_Library
1391 begin
1392 if CodePeer_Mode then
1393 return;
1394 end if;
1396 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1397 Unum := Elab_Order.Table (E);
1398 U := Units.Table (Unum);
1400 -- Dealing with package bodies is a little complicated. In such
1401 -- cases we must retrieve the package spec since it contains the
1402 -- spec of the body finalizer.
1404 if U.Utype = Is_Body then
1405 Unum := Unum + 1;
1406 Uspec := Units.Table (Unum);
1407 else
1408 Uspec := U;
1409 end if;
1411 Get_Name_String (Uspec.Uname);
1413 -- We are only interested in non-generic packages
1415 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1416 null;
1418 -- That aren't an interface to a stand alone library
1420 elsif U.SAL_Interface then
1421 null;
1423 -- Case of no finalization
1425 elsif not U.Has_Finalizer then
1427 -- The only case in which we have to do something is if this
1428 -- is a body, with a separate spec, where the separate spec
1429 -- has a finalizer. In that case, this is where we decrement
1430 -- the elaboration entity.
1432 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1433 if not Lib_Final_Built then
1434 Gen_Header;
1435 Lib_Final_Built := True;
1436 end if;
1438 Set_String (" E");
1439 Set_Unit_Number (Unum);
1440 Set_String (" := E");
1441 Set_Unit_Number (Unum);
1442 Set_String (" - 1;");
1443 Write_Statement_Buffer;
1444 end if;
1446 else
1447 if not Lib_Final_Built then
1448 Gen_Header;
1449 Lib_Final_Built := True;
1450 end if;
1452 -- Generate:
1453 -- declare
1454 -- procedure F<Count>;
1456 Set_String (" declare");
1457 Write_Statement_Buffer;
1459 Set_String (" procedure F");
1460 Set_Int (Count);
1461 Set_Char (';');
1462 Write_Statement_Buffer;
1464 -- Generate:
1465 -- pragma Import (CIL, F<Count>,
1466 -- "xx.yy_pkg.xx__yy__finalize_[body|spec]");
1467 -- -- for .NET targets
1469 -- pragma Import (Java, F<Count>,
1470 -- "xx$yy.xx__yy__finalize_[body|spec]");
1471 -- -- for JVM targets
1473 -- pragma Import (Ada, F<Count>,
1474 -- "xx__yy__finalize_[body|spec]");
1475 -- -- for default targets
1477 if VM_Target = CLI_Target then
1478 Set_String (" pragma Import (CIL, F");
1479 elsif VM_Target = JVM_Target then
1480 Set_String (" pragma Import (Java, F");
1481 else
1482 Set_String (" pragma Import (Ada, F");
1483 end if;
1485 Set_Int (Count);
1486 Set_String (", """);
1488 -- Perform name construction
1490 -- .NET xx.yy_pkg.xx__yy__finalize
1492 if VM_Target = CLI_Target then
1493 Set_Unit_Name (Mode => Dot);
1494 Set_String ("_pkg.");
1496 -- JVM xx$yy.xx__yy__finalize
1498 elsif VM_Target = JVM_Target then
1499 Set_Unit_Name (Mode => Dollar_Sign);
1500 Set_Char ('.');
1501 end if;
1503 -- Default xx__yy__finalize
1505 Set_Unit_Name;
1506 Set_String ("__finalize_");
1508 -- Package spec processing
1510 if U.Utype = Is_Spec
1511 or else U.Utype = Is_Spec_Only
1512 then
1513 Set_String ("spec");
1515 -- Package body processing
1517 else
1518 Set_String ("body");
1519 end if;
1521 Set_String (""");");
1522 Write_Statement_Buffer;
1524 -- If binding a library or if there is a non-Ada main subprogram
1525 -- then we generate:
1527 -- begin
1528 -- uname_E := uname_E - 1;
1529 -- if uname_E = 0 then
1530 -- F<Count>;
1531 -- end if;
1532 -- end;
1534 -- Otherwise, finalization routines are called unconditionally:
1536 -- begin
1537 -- uname_E := uname_E - 1;
1538 -- F<Count>;
1539 -- end;
1541 -- The uname_E decrement is skipped if this is a separate spec,
1542 -- since it will be done when we process the body.
1544 WBI (" begin");
1546 if U.Utype /= Is_Spec then
1547 Set_String (" E");
1548 Set_Unit_Number (Unum);
1549 Set_String (" := E");
1550 Set_Unit_Number (Unum);
1551 Set_String (" - 1;");
1552 Write_Statement_Buffer;
1553 end if;
1555 if Interface_Library_Unit or not Bind_Main_Program then
1556 Set_String (" if E");
1557 Set_Unit_Number (Unum);
1558 Set_String (" = 0 then");
1559 Write_Statement_Buffer;
1560 Set_String (" ");
1561 end if;
1563 Set_String (" F");
1564 Set_Int (Count);
1565 Set_Char (';');
1566 Write_Statement_Buffer;
1568 if Interface_Library_Unit or not Bind_Main_Program then
1569 WBI (" end if;");
1570 end if;
1572 WBI (" end;");
1574 Count := Count + 1;
1575 end if;
1576 end loop;
1578 if Lib_Final_Built then
1580 -- It is possible that the finalization of a library-level object
1581 -- raised an exception. In that case import the actual exception
1582 -- and the routine necessary to raise it.
1584 if VM_Target = No_VM then
1585 WBI (" declare");
1586 WBI (" procedure Reraise_Library_Exception_If_Any;");
1588 Set_String (" pragma Import (Ada, ");
1589 Set_String ("Reraise_Library_Exception_If_Any, ");
1590 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1591 Write_Statement_Buffer;
1593 WBI (" begin");
1594 WBI (" Reraise_Library_Exception_If_Any;");
1595 WBI (" end;");
1597 -- VM-specific code, use regular Ada to produce the desired behavior
1599 else
1600 WBI (" if System.Soft_Links.Library_Exception_Set then");
1602 Set_String (" Ada.Exceptions.Reraise_Occurrence (");
1603 Set_String ("System.Soft_Links.Library_Exception);");
1604 Write_Statement_Buffer;
1606 WBI (" end if;");
1607 end if;
1609 WBI (" end finalize_library;");
1610 WBI ("");
1611 end if;
1612 end Gen_Finalize_Library;
1614 --------------
1615 -- Gen_Main --
1616 --------------
1618 procedure Gen_Main is
1619 begin
1620 if not No_Main_Subprogram then
1622 -- To call the main program, we declare it using a pragma Import
1623 -- Ada with the right link name.
1625 -- It might seem more obvious to "with" the main program, and call
1626 -- it in the normal Ada manner. We do not do this for three
1627 -- reasons:
1629 -- 1. It is more efficient not to recompile the main program
1630 -- 2. We are not entitled to assume the source is accessible
1631 -- 3. We don't know what options to use to compile it
1633 -- It is really reason 3 that is most critical (indeed we used
1634 -- to generate the "with", but several regression tests failed).
1636 if ALIs.Table (ALIs.First).Main_Program = Func then
1637 WBI (" function Ada_Main_Program return Integer;");
1638 else
1639 WBI (" procedure Ada_Main_Program;");
1640 end if;
1642 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1643 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1644 Set_Main_Program_Name;
1645 Set_String (""");");
1647 Write_Statement_Buffer;
1648 WBI ("");
1650 -- For CodePeer, declare a wrapper for the user-defined main program
1652 if CodePeer_Mode then
1653 Gen_CodePeer_Wrapper;
1654 end if;
1655 end if;
1657 if Exit_Status_Supported_On_Target then
1658 Set_String (" function ");
1659 else
1660 Set_String (" procedure ");
1661 end if;
1663 Set_String (Get_Main_Name);
1665 if Command_Line_Args_On_Target then
1666 Write_Statement_Buffer;
1667 WBI (" (argc : Integer;");
1668 WBI (" argv : System.Address;");
1669 WBI (" envp : System.Address)");
1671 if Exit_Status_Supported_On_Target then
1672 WBI (" return Integer");
1673 end if;
1675 WBI (" is");
1677 else
1678 if Exit_Status_Supported_On_Target then
1679 Set_String (" return Integer is");
1680 else
1681 Set_String (" is");
1682 end if;
1684 Write_Statement_Buffer;
1685 end if;
1687 if Opt.Default_Exit_Status /= 0
1688 and then Bind_Main_Program
1689 and then not Configurable_Run_Time_Mode
1690 then
1691 WBI (" procedure Set_Exit_Status (Status : Integer);");
1692 WBI (" pragma Import (C, Set_Exit_Status, " &
1693 """__gnat_set_exit_status"");");
1694 WBI ("");
1695 end if;
1697 -- Initialize and Finalize
1699 if not CodePeer_Mode
1700 and then not Cumulative_Restrictions.Set (No_Finalization)
1701 then
1702 WBI (" procedure Initialize (Addr : System.Address);");
1703 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1704 WBI ("");
1705 WBI (" procedure Finalize;");
1706 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1707 end if;
1709 -- If we want to analyze the stack, we must import corresponding symbols
1711 if Dynamic_Stack_Measurement then
1712 WBI ("");
1713 WBI (" procedure Output_Results;");
1714 WBI (" pragma Import (C, Output_Results, " &
1715 """__gnat_stack_usage_output_results"");");
1717 WBI ("");
1718 WBI (" " &
1719 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1720 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1721 """__gnat_stack_usage_initialize"");");
1722 end if;
1724 -- Deal with declarations for main program case
1726 if not No_Main_Subprogram then
1727 if ALIs.Table (ALIs.First).Main_Program = Func then
1728 WBI (" Result : Integer;");
1729 WBI ("");
1730 end if;
1732 if Bind_Main_Program
1733 and not Suppress_Standard_Library_On_Target
1734 and not CodePeer_Mode
1735 then
1736 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1737 WBI ("");
1738 end if;
1739 end if;
1741 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1742 -- not referenced elsewhere in the generated program, but is needed
1743 -- by the debugger (that's why it is generated in the first place).
1744 -- The reference stops Ada_Main_Program_Name from being optimized
1745 -- away by smart linkers, such as the AiX linker.
1747 -- Because this variable is unused, we make this variable "aliased"
1748 -- with a pragma Volatile in order to tell the compiler to preserve
1749 -- this variable at any level of optimization.
1751 if Bind_Main_Program and not CodePeer_Mode then
1752 WBI (" Ensure_Reference : aliased System.Address := " &
1753 "Ada_Main_Program_Name'Address;");
1754 WBI (" pragma Volatile (Ensure_Reference);");
1755 WBI ("");
1756 end if;
1758 WBI (" begin");
1760 -- Acquire command line arguments if present on target
1762 if CodePeer_Mode then
1763 null;
1765 elsif Command_Line_Args_On_Target then
1766 WBI (" gnat_argc := argc;");
1767 WBI (" gnat_argv := argv;");
1768 WBI (" gnat_envp := envp;");
1769 WBI ("");
1771 -- If configurable run time and no command line args, then nothing
1772 -- needs to be done since the gnat_argc/argv/envp variables are
1773 -- suppressed in this case.
1775 elsif Configurable_Run_Time_On_Target then
1776 null;
1778 -- Otherwise set dummy values (to be filled in by some other unit?)
1780 else
1781 WBI (" gnat_argc := 0;");
1782 WBI (" gnat_argv := System.Null_Address;");
1783 WBI (" gnat_envp := System.Null_Address;");
1784 end if;
1786 if Opt.Default_Exit_Status /= 0
1787 and then Bind_Main_Program
1788 and then not Configurable_Run_Time_Mode
1789 then
1790 Set_String (" Set_Exit_Status (");
1791 Set_Int (Opt.Default_Exit_Status);
1792 Set_String (");");
1793 Write_Statement_Buffer;
1794 end if;
1796 if Dynamic_Stack_Measurement then
1797 Set_String (" Initialize_Stack_Analysis (");
1798 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1799 Set_String (");");
1800 Write_Statement_Buffer;
1801 end if;
1803 if not Cumulative_Restrictions.Set (No_Finalization)
1804 and then not CodePeer_Mode
1805 then
1806 if not No_Main_Subprogram
1807 and then Bind_Main_Program
1808 and then not Suppress_Standard_Library_On_Target
1809 then
1810 WBI (" Initialize (SEH'Address);");
1811 else
1812 WBI (" Initialize (System.Null_Address);");
1813 end if;
1814 end if;
1816 WBI (" " & Ada_Init_Name.all & ";");
1818 if not No_Main_Subprogram then
1819 if CodePeer_Mode then
1820 if ALIs.Table (ALIs.First).Main_Program = Proc then
1821 WBI (" " & CodePeer_Wrapper_Name & ";");
1822 else
1823 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1824 end if;
1826 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1827 WBI (" Ada_Main_Program;");
1829 else
1830 WBI (" Result := Ada_Main_Program;");
1831 end if;
1832 end if;
1834 -- Adafinal call is skipped if no finalization
1836 if not Cumulative_Restrictions.Set (No_Finalization) then
1837 WBI (" adafinal;");
1838 end if;
1840 -- Prints the result of static stack analysis
1842 if Dynamic_Stack_Measurement then
1843 WBI (" Output_Results;");
1844 end if;
1846 -- Finalize is only called if we have a run time
1848 if not Cumulative_Restrictions.Set (No_Finalization)
1849 and then not CodePeer_Mode
1850 then
1851 WBI (" Finalize;");
1852 end if;
1854 -- Return result
1856 if Exit_Status_Supported_On_Target then
1857 if No_Main_Subprogram
1858 or else ALIs.Table (ALIs.First).Main_Program = Proc
1859 then
1860 WBI (" return (gnat_exit_status);");
1861 else
1862 WBI (" return (Result);");
1863 end if;
1864 end if;
1866 WBI (" end;");
1867 WBI ("");
1868 end Gen_Main;
1870 ------------------------------
1871 -- Gen_Object_Files_Options --
1872 ------------------------------
1874 procedure Gen_Object_Files_Options is
1875 Lgnat : Natural;
1876 -- This keeps track of the position in the sorted set of entries
1877 -- in the Linker_Options table of where the first entry from an
1878 -- internal file appears.
1880 Linker_Option_List_Started : Boolean := False;
1881 -- Set to True when "LINKER OPTION LIST" is displayed
1883 procedure Write_Linker_Option;
1884 -- Write binder info linker option
1886 -------------------------
1887 -- Write_Linker_Option --
1888 -------------------------
1890 procedure Write_Linker_Option is
1891 Start : Natural;
1892 Stop : Natural;
1894 begin
1895 -- Loop through string, breaking at null's
1897 Start := 1;
1898 while Start < Name_Len loop
1900 -- Find null ending this section
1902 Stop := Start + 1;
1903 while Name_Buffer (Stop) /= ASCII.NUL
1904 and then Stop <= Name_Len loop
1905 Stop := Stop + 1;
1906 end loop;
1908 -- Process section if non-null
1910 if Stop > Start then
1911 if Output_Linker_Option_List then
1912 if not Zero_Formatting then
1913 if not Linker_Option_List_Started then
1914 Linker_Option_List_Started := True;
1915 Write_Eol;
1916 Write_Str (" LINKER OPTION LIST");
1917 Write_Eol;
1918 Write_Eol;
1919 end if;
1921 Write_Str (" ");
1922 end if;
1924 Write_Str (Name_Buffer (Start .. Stop - 1));
1925 Write_Eol;
1926 end if;
1927 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
1928 end if;
1930 Start := Stop + 1;
1931 end loop;
1932 end Write_Linker_Option;
1934 -- Start of processing for Gen_Object_Files_Options
1936 begin
1937 WBI ("-- BEGIN Object file/option list");
1939 if Object_List_Filename /= null then
1940 Set_List_File (Object_List_Filename.all);
1941 end if;
1943 for E in Elab_Order.First .. Elab_Order.Last loop
1945 -- If not spec that has an associated body, then generate a comment
1946 -- giving the name of the corresponding object file.
1948 if not Units.Table (Elab_Order.Table (E)).SAL_Interface
1949 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1950 then
1951 Get_Name_String
1952 (ALIs.Table
1953 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1955 -- If the presence of an object file is necessary or if it exists,
1956 -- then use it.
1958 if not Hostparm.Exclude_Missing_Objects
1959 or else
1960 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1961 then
1962 WBI (" -- " & Name_Buffer (1 .. Name_Len));
1964 if Output_Object_List then
1965 Write_Str (Name_Buffer (1 .. Name_Len));
1966 Write_Eol;
1967 end if;
1968 end if;
1969 end if;
1970 end loop;
1972 if Object_List_Filename /= null then
1973 Close_List_File;
1974 end if;
1976 -- Add a "-Ldir" for each directory in the object path
1977 if VM_Target /= CLI_Target then
1978 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1979 declare
1980 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1981 begin
1982 Name_Len := 0;
1983 Add_Str_To_Name_Buffer ("-L");
1984 Add_Str_To_Name_Buffer (Dir.all);
1985 Write_Linker_Option;
1986 end;
1987 end loop;
1988 end if;
1990 -- Sort linker options
1992 -- This sort accomplishes two important purposes:
1994 -- a) All application files are sorted to the front, and all GNAT
1995 -- internal files are sorted to the end. This results in a well
1996 -- defined dividing line between the two sets of files, for the
1997 -- purpose of inserting certain standard library references into
1998 -- the linker arguments list.
2000 -- b) Given two different units, we sort the linker options so that
2001 -- those from a unit earlier in the elaboration order comes later
2002 -- in the list. This is a heuristic designed to create a more
2003 -- friendly order of linker options when the operations appear in
2004 -- separate units. The idea is that if unit A must be elaborated
2005 -- before unit B, then it is more likely that B references
2006 -- libraries included by A, than vice versa, so we want libraries
2007 -- included by A to come after libraries included by B.
2009 -- These two criteria are implemented by function Lt_Linker_Option. Note
2010 -- that a special case of b) is that specs are elaborated before bodies,
2011 -- so linker options from specs come after linker options for bodies,
2012 -- and again, the assumption is that libraries used by the body are more
2013 -- likely to reference libraries used by the spec, than vice versa.
2015 Sort
2016 (Linker_Options.Last,
2017 Move_Linker_Option'Access,
2018 Lt_Linker_Option'Access);
2020 -- Write user linker options, i.e. the set of linker options that come
2021 -- from all files other than GNAT internal files, Lgnat is left set to
2022 -- point to the first entry from a GNAT internal file, or past the end
2023 -- of the entries if there are no internal files.
2025 Lgnat := Linker_Options.Last + 1;
2027 for J in 1 .. Linker_Options.Last loop
2028 if not Linker_Options.Table (J).Internal_File then
2029 Get_Name_String (Linker_Options.Table (J).Name);
2030 Write_Linker_Option;
2031 else
2032 Lgnat := J;
2033 exit;
2034 end if;
2035 end loop;
2037 -- Now we insert standard linker options that must appear after the
2038 -- entries from user files, and before the entries from GNAT run-time
2039 -- files. The reason for this decision is that libraries referenced
2040 -- by internal routines may reference these standard library entries.
2042 -- Note that we do not insert anything when pragma No_Run_Time has
2043 -- been specified or when the standard libraries are not to be used,
2044 -- otherwise on some platforms, we may get duplicate symbols when
2045 -- linking (not clear if this is still the case, but it is harmless).
2047 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2048 Name_Len := 0;
2050 if Opt.Shared_Libgnat then
2051 Add_Str_To_Name_Buffer ("-shared");
2052 else
2053 Add_Str_To_Name_Buffer ("-static");
2054 end if;
2056 -- Write directly to avoid -K output (why???)
2058 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2060 if With_GNARL then
2061 Name_Len := 0;
2063 if Opt.Shared_Libgnat then
2064 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2065 else
2066 Add_Str_To_Name_Buffer ("-lgnarl");
2067 end if;
2069 Write_Linker_Option;
2070 end if;
2072 Name_Len := 0;
2074 if Opt.Shared_Libgnat then
2075 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2076 else
2077 Add_Str_To_Name_Buffer ("-lgnat");
2078 end if;
2080 Write_Linker_Option;
2081 end if;
2083 -- Write linker options from all internal files
2085 for J in Lgnat .. Linker_Options.Last loop
2086 Get_Name_String (Linker_Options.Table (J).Name);
2087 Write_Linker_Option;
2088 end loop;
2090 if Output_Linker_Option_List and then not Zero_Formatting then
2091 Write_Eol;
2092 end if;
2094 WBI ("-- END Object file/option list ");
2095 end Gen_Object_Files_Options;
2097 ---------------------
2098 -- Gen_Output_File --
2099 ---------------------
2101 procedure Gen_Output_File (Filename : String) is
2102 begin
2103 -- Acquire settings for Interrupt_State pragmas
2105 Set_IS_Pragma_Table;
2107 -- Acquire settings for Priority_Specific_Dispatching pragma
2109 Set_PSD_Pragma_Table;
2111 -- For JGNAT the main program is already generated by the compiler
2113 if VM_Target = JVM_Target then
2114 Bind_Main_Program := False;
2115 end if;
2117 -- Override time slice value if -T switch is set
2119 if Time_Slice_Set then
2120 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2121 end if;
2123 -- Count number of elaboration calls
2125 for E in Elab_Order.First .. Elab_Order.Last loop
2126 if Units.Table (Elab_Order.Table (E)).No_Elab then
2127 null;
2128 else
2129 Num_Elab_Calls := Num_Elab_Calls + 1;
2130 end if;
2131 end loop;
2133 -- Generate output file in appropriate language
2135 Gen_Output_File_Ada (Filename);
2136 end Gen_Output_File;
2138 -------------------------
2139 -- Gen_Output_File_Ada --
2140 -------------------------
2142 procedure Gen_Output_File_Ada (Filename : String) is
2144 Ada_Main : constant String := Get_Ada_Main_Name;
2145 -- Name to be used for generated Ada main program. See the body of
2146 -- function Get_Ada_Main_Name for details on the form of the name.
2148 Needs_Library_Finalization : constant Boolean :=
2149 not Configurable_Run_Time_On_Target and then Has_Finalizer;
2150 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2151 -- non-terminating, so we do not want finalization.
2153 Bfiles : Name_Id;
2154 -- Name of generated bind file (spec)
2156 Bfileb : Name_Id;
2157 -- Name of generated bind file (body)
2159 begin
2160 -- Create spec first
2162 Create_Binder_Output (Filename, 's', Bfiles);
2164 -- We always compile the binder file in Ada 95 mode so that we properly
2165 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2166 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2168 WBI ("pragma Ada_95;");
2170 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2171 -- then we need to make sure that the binder program is compiled with
2172 -- the same restriction, so that no exception tables are generated.
2174 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2175 WBI ("pragma Restrictions (No_Exception_Handlers);");
2176 end if;
2178 -- Same processing for Restrictions (No_Exception_Propagation)
2180 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2181 WBI ("pragma Restrictions (No_Exception_Propagation);");
2182 end if;
2184 -- Same processing for pragma No_Run_Time
2186 if No_Run_Time_Mode then
2187 WBI ("pragma No_Run_Time;");
2188 end if;
2190 -- Generate with of System so we can reference System.Address
2192 WBI ("with System;");
2194 -- Generate with of System.Initialize_Scalars if active
2196 if Initialize_Scalars_Used then
2197 WBI ("with System.Scalar_Values;");
2198 end if;
2200 -- Generate with of System.Secondary_Stack if active
2202 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2203 WBI ("with System.Secondary_Stack;");
2204 end if;
2206 Resolve_Binder_Options;
2208 -- Generate standard with's
2210 if not Suppress_Standard_Library_On_Target then
2211 if CodePeer_Mode then
2212 WBI ("with System.Standard_Library;");
2213 elsif VM_Target /= No_VM then
2214 WBI ("with System.Soft_Links;");
2215 WBI ("with System.Standard_Library;");
2216 end if;
2217 end if;
2219 WBI ("package " & Ada_Main & " is");
2220 WBI (" pragma Warnings (Off);");
2222 -- Main program case
2224 if Bind_Main_Program then
2225 if VM_Target = No_VM then
2227 -- Generate argc/argv stuff unless suppressed
2229 if Command_Line_Args_On_Target
2230 or not Configurable_Run_Time_On_Target
2231 then
2232 WBI ("");
2233 WBI (" gnat_argc : Integer;");
2234 WBI (" gnat_argv : System.Address;");
2235 WBI (" gnat_envp : System.Address;");
2237 -- If the standard library is not suppressed, these variables
2238 -- are in the run-time data area for easy run time access.
2240 if not Suppress_Standard_Library_On_Target then
2241 WBI ("");
2242 WBI (" pragma Import (C, gnat_argc);");
2243 WBI (" pragma Import (C, gnat_argv);");
2244 WBI (" pragma Import (C, gnat_envp);");
2245 end if;
2246 end if;
2248 -- Define exit status. Again in normal mode, this is in the
2249 -- run-time library, and is initialized there, but in the
2250 -- configurable runtime case, the variable is declared and
2251 -- initialized in this file.
2253 WBI ("");
2255 if Configurable_Run_Time_Mode then
2256 if Exit_Status_Supported_On_Target then
2257 WBI (" gnat_exit_status : Integer := 0;");
2258 end if;
2260 else
2261 WBI (" gnat_exit_status : Integer;");
2262 WBI (" pragma Import (C, gnat_exit_status);");
2263 end if;
2264 end if;
2266 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2267 -- the main program. Otherwise, it can lead under some circumstances
2268 -- to a symbol duplication during the link (for instance when a C
2269 -- program uses two Ada libraries). Also zero terminate the string
2270 -- so that its end can be found reliably at run time.
2272 WBI ("");
2273 WBI (" GNAT_Version : constant String :=");
2274 WBI (" """ & Ver_Prefix &
2275 Gnat_Version_String &
2276 """ & ASCII.NUL;");
2277 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2279 WBI ("");
2280 Set_String (" Ada_Main_Program_Name : constant String := """);
2281 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2283 if VM_Target = No_VM then
2284 Set_Main_Program_Name;
2285 Set_String (""" & ASCII.NUL;");
2286 else
2287 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2288 end if;
2290 Write_Statement_Buffer;
2293 (" pragma Export (C, Ada_Main_Program_Name, " &
2294 """__gnat_ada_main_program_name"");");
2295 end if;
2297 WBI ("");
2298 WBI (" procedure " & Ada_Init_Name.all & ";");
2299 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2300 Ada_Init_Name.all & """);");
2302 -- If -a has been specified use pragma Linker_Constructor for the init
2303 -- procedure and pragma Linker_Destructor for the final procedure.
2305 if Use_Pragma_Linker_Constructor then
2306 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2307 end if;
2309 if not Cumulative_Restrictions.Set (No_Finalization) then
2310 WBI ("");
2311 WBI (" procedure " & Ada_Final_Name.all & ";");
2312 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2313 Ada_Final_Name.all & """);");
2315 if Use_Pragma_Linker_Constructor then
2316 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2317 end if;
2318 end if;
2320 if Bind_Main_Program and then VM_Target = No_VM then
2322 WBI ("");
2324 if Exit_Status_Supported_On_Target then
2325 Set_String (" function ");
2326 else
2327 Set_String (" procedure ");
2328 end if;
2330 Set_String (Get_Main_Name);
2332 -- Generate argument list if present
2334 if Command_Line_Args_On_Target then
2335 Write_Statement_Buffer;
2336 WBI (" (argc : Integer;");
2337 WBI (" argv : System.Address;");
2338 Set_String
2339 (" envp : System.Address)");
2341 if Exit_Status_Supported_On_Target then
2342 Write_Statement_Buffer;
2343 WBI (" return Integer;");
2344 else
2345 Write_Statement_Buffer (";");
2346 end if;
2348 else
2349 if Exit_Status_Supported_On_Target then
2350 Write_Statement_Buffer (" return Integer;");
2351 else
2352 Write_Statement_Buffer (";");
2353 end if;
2354 end if;
2356 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2357 Get_Main_Name & """);");
2358 end if;
2360 Gen_Versions;
2361 Gen_Elab_Order;
2363 -- Spec is complete
2365 WBI ("");
2366 WBI ("end " & Ada_Main & ";");
2367 Close_Binder_Output;
2369 -- Prepare to write body
2371 Create_Binder_Output (Filename, 'b', Bfileb);
2373 -- We always compile the binder file in Ada 95 mode so that we properly
2374 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2375 -- of the Ada 2005/2012 constructs are needed by the binder file.
2377 WBI ("pragma Ada_95;");
2379 -- Output Source_File_Name pragmas which look like
2381 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2382 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2384 -- where sss/bbb are the spec/body file names respectively
2386 Get_Name_String (Bfiles);
2387 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2389 WBI ("pragma Source_File_Name (" &
2390 Ada_Main &
2391 ", Spec_File_Name => """ &
2392 Name_Buffer (1 .. Name_Len + 3));
2394 Get_Name_String (Bfileb);
2395 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2397 WBI ("pragma Source_File_Name (" &
2398 Ada_Main &
2399 ", Body_File_Name => """ &
2400 Name_Buffer (1 .. Name_Len + 3));
2402 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2403 -- versions of the compiler which have overflow checks on by default.
2404 -- We do not want overflow checking enabled for the increments of the
2405 -- elaboration variables (since this can cause an unwanted reference to
2406 -- the last chance exception handler for limited run-times).
2408 WBI ("pragma Suppress (Overflow_Check);");
2410 -- Generate with of System.Restrictions to initialize
2411 -- Run_Time_Restrictions.
2413 if System_Restrictions_Used
2414 and not Suppress_Standard_Library_On_Target
2415 then
2416 WBI ("");
2417 WBI ("with System.Restrictions;");
2418 end if;
2420 -- Generate with of Ada.Exceptions if needs library finalization
2422 if Needs_Library_Finalization then
2423 WBI ("with Ada.Exceptions;");
2424 end if;
2426 -- Generate with of System.Elaboration_Allocators if the restriction
2427 -- No_Standard_Allocators_After_Elaboration was present.
2429 if Cumulative_Restrictions.Set
2430 (No_Standard_Allocators_After_Elaboration)
2431 then
2432 WBI ("with System.Elaboration_Allocators;");
2433 end if;
2435 -- Generate start of package body
2437 WBI ("");
2438 WBI ("package body " & Ada_Main & " is");
2439 WBI (" pragma Warnings (Off);");
2440 WBI ("");
2442 -- Generate externals for elaboration entities
2444 Gen_Elab_Externals;
2446 if not CodePeer_Mode then
2447 if not Suppress_Standard_Library_On_Target then
2449 -- Generate Priority_Specific_Dispatching pragma string
2451 Set_String
2452 (" Local_Priority_Specific_Dispatching : " &
2453 "constant String := """);
2455 for J in 0 .. PSD_Pragma_Settings.Last loop
2456 Set_Char (PSD_Pragma_Settings.Table (J));
2457 end loop;
2459 Set_String (""";");
2460 Write_Statement_Buffer;
2462 -- Generate Interrupt_State pragma string
2464 Set_String (" Local_Interrupt_States : constant String := """);
2466 for J in 0 .. IS_Pragma_Settings.Last loop
2467 Set_Char (IS_Pragma_Settings.Table (J));
2468 end loop;
2470 Set_String (""";");
2471 Write_Statement_Buffer;
2472 WBI ("");
2473 end if;
2475 -- The B.1 (39) implementation advice says that the adainit/adafinal
2476 -- routines should be idempotent. Generate a flag to ensure that.
2477 -- This is not needed if we are suppressing the standard library
2478 -- since it would never be referenced.
2480 if not Suppress_Standard_Library_On_Target then
2481 WBI (" Is_Elaborated : Boolean := False;");
2482 end if;
2484 WBI ("");
2485 end if;
2487 -- Generate the adafinal routine unless there is no finalization to do
2489 if not Cumulative_Restrictions.Set (No_Finalization) then
2490 if Needs_Library_Finalization then
2491 Gen_Finalize_Library;
2492 end if;
2494 Gen_Adafinal;
2495 end if;
2497 Gen_Adainit;
2499 if Bind_Main_Program and then VM_Target = No_VM then
2500 Gen_Main;
2501 end if;
2503 -- Output object file list and the Ada body is complete
2505 Gen_Object_Files_Options;
2507 WBI ("");
2508 WBI ("end " & Ada_Main & ";");
2510 Close_Binder_Output;
2511 end Gen_Output_File_Ada;
2513 ----------------------
2514 -- Gen_Restrictions --
2515 ----------------------
2517 procedure Gen_Restrictions is
2518 Count : Integer;
2520 begin
2521 if Suppress_Standard_Library_On_Target
2522 or not System_Restrictions_Used
2523 then
2524 return;
2525 end if;
2527 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2528 WBI (" (Set =>");
2529 Set_String (" (");
2531 Count := 0;
2533 for J in Cumulative_Restrictions.Set'Range loop
2534 Set_Boolean (Cumulative_Restrictions.Set (J));
2535 Set_String (", ");
2536 Count := Count + 1;
2538 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2539 Write_Statement_Buffer;
2540 Set_String (" ");
2541 Count := 0;
2542 end if;
2543 end loop;
2545 Set_String_Replace ("),");
2546 Write_Statement_Buffer;
2547 Set_String (" Value => (");
2549 for J in Cumulative_Restrictions.Value'Range loop
2550 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2551 Set_String (", ");
2552 end loop;
2554 Set_String_Replace ("),");
2555 Write_Statement_Buffer;
2556 WBI (" Violated =>");
2557 Set_String (" (");
2558 Count := 0;
2560 for J in Cumulative_Restrictions.Violated'Range loop
2561 Set_Boolean (Cumulative_Restrictions.Violated (J));
2562 Set_String (", ");
2563 Count := Count + 1;
2565 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2566 Write_Statement_Buffer;
2567 Set_String (" ");
2568 Count := 0;
2569 end if;
2570 end loop;
2572 Set_String_Replace ("),");
2573 Write_Statement_Buffer;
2574 Set_String (" Count => (");
2576 for J in Cumulative_Restrictions.Count'Range loop
2577 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2578 Set_String (", ");
2579 end loop;
2581 Set_String_Replace ("),");
2582 Write_Statement_Buffer;
2583 Set_String (" Unknown => (");
2585 for J in Cumulative_Restrictions.Unknown'Range loop
2586 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2587 Set_String (", ");
2588 end loop;
2590 Set_String_Replace ("))");
2591 Set_String (";");
2592 Write_Statement_Buffer;
2593 end Gen_Restrictions;
2595 ------------------
2596 -- Gen_Versions --
2597 ------------------
2599 -- This routine generates lines such as:
2601 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2602 -- pragma Export (C, unnnnn, unam);
2604 -- for each unit, where unam is the unit name suffixed by either B or S for
2605 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2606 -- the version number, and nnnnn is a 5-digits serial number.
2608 procedure Gen_Versions is
2609 Ubuf : String (1 .. 6) := "u00000";
2611 procedure Increment_Ubuf;
2612 -- Little procedure to increment the serial number
2614 --------------------
2615 -- Increment_Ubuf --
2616 --------------------
2618 procedure Increment_Ubuf is
2619 begin
2620 for J in reverse Ubuf'Range loop
2621 Ubuf (J) := Character'Succ (Ubuf (J));
2622 exit when Ubuf (J) <= '9';
2623 Ubuf (J) := '0';
2624 end loop;
2625 end Increment_Ubuf;
2627 -- Start of processing for Gen_Versions
2629 begin
2630 WBI ("");
2632 WBI (" type Version_32 is mod 2 ** 32;");
2633 for U in Units.First .. Units.Last loop
2634 if not Units.Table (U).SAL_Interface
2635 and then
2636 (not Bind_For_Library or else Units.Table (U).Directly_Scanned)
2637 then
2638 Increment_Ubuf;
2639 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2640 Units.Table (U).Version & "#;");
2641 Set_String (" pragma Export (C, ");
2642 Set_String (Ubuf);
2643 Set_String (", """);
2645 Get_Name_String (Units.Table (U).Uname);
2647 for K in 1 .. Name_Len loop
2648 if Name_Buffer (K) = '.' then
2649 Set_Char ('_');
2650 Set_Char ('_');
2652 elsif Name_Buffer (K) = '%' then
2653 exit;
2655 else
2656 Set_Char (Name_Buffer (K));
2657 end if;
2658 end loop;
2660 if Name_Buffer (Name_Len) = 's' then
2661 Set_Char ('S');
2662 else
2663 Set_Char ('B');
2664 end if;
2666 Set_String (""");");
2667 Write_Statement_Buffer;
2668 end if;
2669 end loop;
2670 end Gen_Versions;
2672 ------------------------
2673 -- Get_Main_Unit_Name --
2674 ------------------------
2676 function Get_Main_Unit_Name (S : String) return String is
2677 Result : String := S;
2679 begin
2680 for J in S'Range loop
2681 if Result (J) = '.' then
2682 Result (J) := '_';
2683 end if;
2684 end loop;
2686 return Result;
2687 end Get_Main_Unit_Name;
2689 -----------------------
2690 -- Get_Ada_Main_Name --
2691 -----------------------
2693 function Get_Ada_Main_Name return String is
2694 Suffix : constant String := "_00";
2695 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2696 Opt.Ada_Main_Name.all & Suffix;
2697 Nlen : Natural;
2699 begin
2700 -- The main program generated by JGNAT expects a package called
2701 -- ada_<main procedure>.
2702 if VM_Target /= No_VM then
2703 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2704 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2705 end if;
2707 -- For CodePeer, we want reproducible names (independent of other
2708 -- mains that may or may not be present) that don't collide
2709 -- when analyzing multiple mains and which are easily recognizable
2710 -- as "ada_main" names.
2711 if CodePeer_Mode then
2712 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2713 return "ada_main_for_" &
2714 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2715 end if;
2717 -- This loop tries the following possibilities in order
2718 -- <Ada_Main>
2719 -- <Ada_Main>_01
2720 -- <Ada_Main>_02
2721 -- ..
2722 -- <Ada_Main>_99
2723 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2724 -- it is set to 'ada_main'.
2726 for J in 0 .. 99 loop
2727 if J = 0 then
2728 Nlen := Name'Length - Suffix'Length;
2729 else
2730 Nlen := Name'Length;
2731 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2732 Name (Name'Last - 1) :=
2733 Character'Val (J / 10 + Character'Pos ('0'));
2734 end if;
2736 for K in ALIs.First .. ALIs.Last loop
2737 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2739 -- Get unit name, removing %b or %e at end
2741 Get_Name_String (Units.Table (L).Uname);
2742 Name_Len := Name_Len - 2;
2744 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2745 goto Continue;
2746 end if;
2747 end loop;
2748 end loop;
2750 return Name (1 .. Nlen);
2752 <<Continue>>
2753 null;
2754 end loop;
2756 -- If we fall through, just use a peculiar unlikely name
2758 return ("Qwertyuiop");
2759 end Get_Ada_Main_Name;
2761 -------------------
2762 -- Get_Main_Name --
2763 -------------------
2765 function Get_Main_Name return String is
2766 begin
2767 -- Explicit name given with -M switch
2769 if Bind_Alternate_Main_Name then
2770 return Alternate_Main_Name.all;
2772 -- Case of main program name to be used directly
2774 elsif Use_Ada_Main_Program_Name_On_Target then
2776 -- Get main program name
2778 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2780 -- If this is a child name, return only the name of the child, since
2781 -- we can't have dots in a nested program name. Note that we do not
2782 -- include the %b at the end of the unit name.
2784 for J in reverse 1 .. Name_Len - 2 loop
2785 if J = 1 or else Name_Buffer (J - 1) = '.' then
2786 return Name_Buffer (J .. Name_Len - 2);
2787 end if;
2788 end loop;
2790 raise Program_Error; -- impossible exit
2792 -- Case where "main" is to be used as default
2794 else
2795 return "main";
2796 end if;
2797 end Get_Main_Name;
2799 ---------------------
2800 -- Get_WC_Encoding --
2801 ---------------------
2803 function Get_WC_Encoding return Character is
2804 begin
2805 -- If encoding method specified by -W switch, then return it
2807 if Wide_Character_Encoding_Method_Specified then
2808 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2810 -- If no main program, and not specified, set brackets, we really have
2811 -- no better choice. If some other encoding is required when there is
2812 -- no main, it must be set explicitly using -Wx.
2814 -- Note: if the ALI file always passed the wide character encoding of
2815 -- every file, then we could use the encoding of the initial specified
2816 -- file, but this information is passed only for potential main
2817 -- programs. We could fix this sometime, but it is a very minor point
2818 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
2819 -- is no main program).
2821 elsif No_Main_Subprogram then
2822 return 'b';
2824 -- Otherwise if there is a main program, take encoding from it
2826 else
2827 return ALIs.Table (ALIs.First).WC_Encoding;
2828 end if;
2829 end Get_WC_Encoding;
2831 -------------------
2832 -- Has_Finalizer --
2833 -------------------
2835 function Has_Finalizer return Boolean is
2836 U : Unit_Record;
2837 Unum : Unit_Id;
2839 begin
2840 for E in reverse Elab_Order.First .. Elab_Order.Last loop
2841 Unum := Elab_Order.Table (E);
2842 U := Units.Table (Unum);
2844 -- We are only interested in non-generic packages
2846 if U.Unit_Kind = 'p'
2847 and then U.Has_Finalizer
2848 and then not U.Is_Generic
2849 and then not U.No_Elab
2850 then
2851 return True;
2852 end if;
2853 end loop;
2855 return False;
2856 end Has_Finalizer;
2858 ----------------------
2859 -- Lt_Linker_Option --
2860 ----------------------
2862 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
2863 begin
2864 -- Sort internal files last
2866 if Linker_Options.Table (Op1).Internal_File
2868 Linker_Options.Table (Op2).Internal_File
2869 then
2870 -- Note: following test uses False < True
2872 return Linker_Options.Table (Op1).Internal_File
2874 Linker_Options.Table (Op2).Internal_File;
2876 -- If both internal or both non-internal, sort according to the
2877 -- elaboration position. A unit that is elaborated later should come
2878 -- earlier in the linker options list.
2880 else
2881 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2883 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2885 end if;
2886 end Lt_Linker_Option;
2888 ------------------------
2889 -- Move_Linker_Option --
2890 ------------------------
2892 procedure Move_Linker_Option (From : Natural; To : Natural) is
2893 begin
2894 Linker_Options.Table (To) := Linker_Options.Table (From);
2895 end Move_Linker_Option;
2897 ----------------------------
2898 -- Resolve_Binder_Options --
2899 ----------------------------
2901 procedure Resolve_Binder_Options is
2903 procedure Check_Package (Var : in out Boolean; Name : String);
2904 -- Set Var to true iff the current identifier in Namet is Name. Do
2905 -- nothing if it doesn't match. This procedure is just an helper to
2906 -- avoid to explicitely deal with length.
2908 -------------------
2909 -- Check_Package --
2910 -------------------
2912 procedure Check_Package (Var : in out Boolean; Name : String) is
2913 begin
2914 if Name_Len = Name'Length
2915 and then Name_Buffer (1 .. Name_Len) = Name
2916 then
2917 Var := True;
2918 end if;
2919 end Check_Package;
2921 -- Start of processing for Check_Package
2923 begin
2924 for E in Elab_Order.First .. Elab_Order.Last loop
2925 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
2927 -- This is not a perfect approach, but is the current protocol
2928 -- between the run-time and the binder to indicate that tasking is
2929 -- used: System.OS_Interface should always be used by any tasking
2930 -- application.
2932 Check_Package (With_GNARL, "system.os_interface%s");
2934 -- Ditto for the use of restricted tasking
2936 Check_Package
2937 (System_Tasking_Restricted_Stages_Used,
2938 "system.tasking.restricted.stages%s");
2940 -- Ditto for the use of interrupts
2942 Check_Package (System_Interrupts_Used, "system.interrupts%s");
2944 -- Ditto for the use of dispatching domains
2946 Check_Package
2947 (Dispatching_Domains_Used,
2948 "system.multiprocessors.dispatching_domains%s");
2950 -- Ditto for the use of restrictions
2952 Check_Package (System_Restrictions_Used, "system.restrictions%s");
2953 end loop;
2954 end Resolve_Binder_Options;
2956 -----------------
2957 -- Set_Boolean --
2958 -----------------
2960 procedure Set_Boolean (B : Boolean) is
2961 True_Str : constant String := "True";
2962 False_Str : constant String := "False";
2963 begin
2964 if B then
2965 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
2966 Last := Last + True_Str'Length;
2967 else
2968 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
2969 Last := Last + False_Str'Length;
2970 end if;
2971 end Set_Boolean;
2973 --------------
2974 -- Set_Char --
2975 --------------
2977 procedure Set_Char (C : Character) is
2978 begin
2979 Last := Last + 1;
2980 Statement_Buffer (Last) := C;
2981 end Set_Char;
2983 -------------
2984 -- Set_Int --
2985 -------------
2987 procedure Set_Int (N : Int) is
2988 begin
2989 if N < 0 then
2990 Set_String ("-");
2991 Set_Int (-N);
2993 else
2994 if N > 9 then
2995 Set_Int (N / 10);
2996 end if;
2998 Last := Last + 1;
2999 Statement_Buffer (Last) :=
3000 Character'Val (N mod 10 + Character'Pos ('0'));
3001 end if;
3002 end Set_Int;
3004 -------------------------
3005 -- Set_IS_Pragma_Table --
3006 -------------------------
3008 procedure Set_IS_Pragma_Table is
3009 begin
3010 for F in ALIs.First .. ALIs.Last loop
3011 for K in ALIs.Table (F).First_Interrupt_State ..
3012 ALIs.Table (F).Last_Interrupt_State
3013 loop
3014 declare
3015 Inum : constant Int :=
3016 Interrupt_States.Table (K).Interrupt_Id;
3017 Stat : constant Character :=
3018 Interrupt_States.Table (K).Interrupt_State;
3020 begin
3021 while IS_Pragma_Settings.Last < Inum loop
3022 IS_Pragma_Settings.Append ('n');
3023 end loop;
3025 IS_Pragma_Settings.Table (Inum) := Stat;
3026 end;
3027 end loop;
3028 end loop;
3029 end Set_IS_Pragma_Table;
3031 ---------------------------
3032 -- Set_Main_Program_Name --
3033 ---------------------------
3035 procedure Set_Main_Program_Name is
3036 begin
3037 -- Note that name has %b on the end which we ignore
3039 -- First we output the initial _ada_ since we know that the main
3040 -- program is a library level subprogram.
3042 Set_String ("_ada_");
3044 -- Copy name, changing dots to double underscores
3046 for J in 1 .. Name_Len - 2 loop
3047 if Name_Buffer (J) = '.' then
3048 Set_String ("__");
3049 else
3050 Set_Char (Name_Buffer (J));
3051 end if;
3052 end loop;
3053 end Set_Main_Program_Name;
3055 ---------------------
3056 -- Set_Name_Buffer --
3057 ---------------------
3059 procedure Set_Name_Buffer is
3060 begin
3061 for J in 1 .. Name_Len loop
3062 Set_Char (Name_Buffer (J));
3063 end loop;
3064 end Set_Name_Buffer;
3066 -------------------------
3067 -- Set_PSD_Pragma_Table --
3068 -------------------------
3070 procedure Set_PSD_Pragma_Table is
3071 begin
3072 for F in ALIs.First .. ALIs.Last loop
3073 for K in ALIs.Table (F).First_Specific_Dispatching ..
3074 ALIs.Table (F).Last_Specific_Dispatching
3075 loop
3076 declare
3077 DTK : Specific_Dispatching_Record
3078 renames Specific_Dispatching.Table (K);
3080 begin
3081 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3082 PSD_Pragma_Settings.Append ('F');
3083 end loop;
3085 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3086 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3087 end loop;
3088 end;
3089 end loop;
3090 end loop;
3091 end Set_PSD_Pragma_Table;
3093 ----------------
3094 -- Set_String --
3095 ----------------
3097 procedure Set_String (S : String) is
3098 begin
3099 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3100 Last := Last + S'Length;
3101 end Set_String;
3103 ------------------------
3104 -- Set_String_Replace --
3105 ------------------------
3107 procedure Set_String_Replace (S : String) is
3108 begin
3109 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3110 end Set_String_Replace;
3112 -------------------
3113 -- Set_Unit_Name --
3114 -------------------
3116 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores) is
3117 begin
3118 for J in 1 .. Name_Len - 2 loop
3119 if Name_Buffer (J) = '.' then
3120 if Mode = Double_Underscores then
3121 Set_String ("__");
3122 elsif Mode = Dot then
3123 Set_Char ('.');
3124 else
3125 Set_Char ('$');
3126 end if;
3127 else
3128 Set_Char (Name_Buffer (J));
3129 end if;
3130 end loop;
3131 end Set_Unit_Name;
3133 ---------------------
3134 -- Set_Unit_Number --
3135 ---------------------
3137 procedure Set_Unit_Number (U : Unit_Id) is
3138 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3139 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3141 begin
3142 if Num_Units >= 10 and then Unum < 10 then
3143 Set_Char ('0');
3144 end if;
3146 if Num_Units >= 100 and then Unum < 100 then
3147 Set_Char ('0');
3148 end if;
3150 Set_Int (Unum);
3151 end Set_Unit_Number;
3153 ----------------------------
3154 -- Write_Statement_Buffer --
3155 ----------------------------
3157 procedure Write_Statement_Buffer is
3158 begin
3159 WBI (Statement_Buffer (1 .. Last));
3160 Last := 0;
3161 end Write_Statement_Buffer;
3163 procedure Write_Statement_Buffer (S : String) is
3164 begin
3165 Set_String (S);
3166 Write_Statement_Buffer;
3167 end Write_Statement_Buffer;
3169 end Bindgen;