1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
27 with Binde; use Binde;
28 with Casing; use Casing;
29 with Fname; use Fname;
30 with Gnatvsn; use Gnatvsn;
32 with Namet; use Namet;
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
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
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 -- Float_Format is the float representation in use. Currently the only
163 -- valid value is 'I' for IEEE. We needed this field in the past for other
164 -- floating-point formats, and it is retained for possible future use.
166 -- WC_Encoding shows the wide character encoding method used for the main
167 -- program. This is one of the encoding letters defined in
168 -- System.WCh_Con.WC_Encoding_Letters.
170 -- Locking_Policy is a space if no locking policy was specified for the
171 -- partition. If a locking policy was specified, the value is the upper
172 -- case first character of the locking policy name, for example, 'C' for
175 -- Queuing_Policy is a space if no queuing policy was specified for the
176 -- partition. If a queuing policy was specified, the value is the upper
177 -- case first character of the queuing policy name for example, 'F' for
180 -- Task_Dispatching_Policy is a space if no task dispatching policy was
181 -- specified for the partition. If a task dispatching policy was specified,
182 -- the value is the upper case first character of the policy name, e.g. 'F'
183 -- for FIFO_Within_Priorities.
185 -- Priority_Specific_Dispatching is the address of a string used to store
186 -- the task dispatching policy specified for the different priorities in
187 -- the partition. The length of this string is determined by the last
188 -- priority for which such a pragma applies (the string will be a null
189 -- string if no specific dispatching policies were used). If pragma were
190 -- present, the entries apply to the priorities in sequence from the first
191 -- priority. The value stored is the upper case first character of the
192 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
193 -- for those priority ranges not specified.
195 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
196 -- string. It will be set to zero if no Priority_Specific_Dispatching
197 -- pragmas are present.
199 -- Restrictions is the address of a null-terminated string specifying the
200 -- restrictions information for the partition. The format is identical to
201 -- that of the parameter string found on R lines in ali files (see Lib.Writ
202 -- spec in lib-writ.ads for full details). The difference is that in this
203 -- context the values are the cumulative ones for the entire partition.
205 -- Interrupt_States is the address of a string used to specify the
206 -- cumulative results of Interrupt_State pragmas used in the partition.
207 -- The length of this string is determined by the last interrupt for which
208 -- such a pragma is given (the string will be a null string if no pragmas
209 -- were used). If pragma were present the entries apply to the interrupts
210 -- in sequence from the first interrupt, and are set to one of four
211 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
212 -- time, 's' for system, see description of Interrupt_State pragma for
215 -- Num_Interrupt_States is the length of the Interrupt_States string. It
216 -- will be set to zero if no Interrupt_State pragmas are present.
218 -- Unreserve_All_Interrupts is set to one if at least one unit in the
219 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
221 -- Exception_Tracebacks is set to one if the -E parameter was present
222 -- in the bind and to zero otherwise. Note that on some targets exception
223 -- tracebacks are provided by default, so a value of zero for this
224 -- parameter does not necessarily mean no trace backs are available.
226 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
227 -- not. A value of zero indicates that the pragma is not present, while a
228 -- value of 1 signals its presence in the partition.
230 -- Default_Stack_Size is the default stack size used when creating an Ada
231 -- task with no explicit Storage_Size clause.
233 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
234 -- disabled. A value of zero indicates that leap seconds are turned "off",
235 -- while a value of one signifies "on" status.
237 -- Main_CPU is the processor set by pragma CPU in the main program. If no
238 -- such pragma is present, the value is -1.
240 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
241 -- Convenient shorthand used throughout
243 -----------------------
244 -- Local Subprograms --
245 -----------------------
247 procedure Gen_Adainit;
248 -- Generates the Adainit procedure
250 procedure Gen_Adafinal;
251 -- Generate the Adafinal procedure
253 procedure Gen_CodePeer_Wrapper;
254 -- For CodePeer, generate wrapper which calls user-defined main subprogram
256 procedure Gen_Elab_Calls;
257 -- Generate sequence of elaboration calls
259 procedure Gen_Elab_Externals;
260 -- Generate sequence of external declarations for elaboration
262 procedure Gen_Elab_Order;
263 -- Generate comments showing elaboration order chosen
265 procedure Gen_Finalize_Library;
266 -- Generate a sequence of finalization calls to elaborated packages
269 -- Generate procedure main
271 procedure Gen_Object_Files_Options;
272 -- Output comments containing a list of the full names of the object
273 -- files to be linked and the list of linker options supplied by
274 -- Linker_Options pragmas in the source.
276 procedure Gen_Output_File_Ada (Filename : String);
277 -- Generate Ada output file
279 procedure Gen_Restrictions;
280 -- Generate initialization of restrictions variable
282 procedure Gen_Versions;
283 -- Output series of definitions for unit versions
285 function Get_Ada_Main_Name return String;
286 -- This function is used for the Ada main output to compute a usable name
287 -- for the generated main program. The normal main program name is
288 -- Ada_Main, but this won't work if the user has a unit with this name.
289 -- This function tries Ada_Main first, and if there is such a clash, then
290 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
292 function Get_Main_Unit_Name (S : String) return String;
293 -- Return the main unit name corresponding to S by replacing '.' with '_'
295 function Get_Main_Name return String;
296 -- This function is used in the main output case to compute the correct
297 -- external main program. It is "main" by default, unless the flag
298 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
299 -- of the Ada main name without the "_ada". This default can be overridden
300 -- explicitly using the -Mname binder switch.
302 function Get_WC_Encoding return Character;
303 -- Return wide character encoding method to set as WC_Encoding in output.
304 -- If -W has been used, returns the specified encoding, otherwise returns
305 -- the encoding method used for the main program source. If there is no
306 -- main program source (-z switch used), returns brackets ('b').
308 function Has_Finalizer return Boolean;
309 -- Determine whether the current unit has at least one library-level
312 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
313 -- Compare linker options, when sorting, first according to
314 -- Is_Internal_File (internal files come later) and then by
315 -- elaboration order position (latest to earliest).
317 procedure Move_Linker_Option (From : Natural; To : Natural);
318 -- Move routine for sorting linker options
320 procedure Resolve_Binder_Options;
321 -- Set the value of With_GNARL
323 procedure Set_Char (C : Character);
324 -- Set given character in Statement_Buffer at the Last + 1 position
325 -- and increment Last by one to reflect the stored character.
327 procedure Set_Int (N : Int);
328 -- Set given value in decimal in Statement_Buffer with no spaces starting
329 -- at the Last + 1 position, and updating Last past the value. A minus sign
330 -- is output for a negative value.
332 procedure Set_Boolean (B : Boolean);
333 -- Set given boolean value in Statement_Buffer at the Last + 1 position
334 -- and update Last past the value.
336 procedure Set_IS_Pragma_Table;
337 -- Initializes contents of IS_Pragma_Settings table from ALI table
339 procedure Set_Main_Program_Name;
340 -- Given the main program name in Name_Buffer (length in Name_Len) generate
341 -- the name of the routine to be used in the call. The name is generated
342 -- starting at Last + 1, and Last is updated past it.
344 procedure Set_Name_Buffer;
345 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
347 procedure Set_PSD_Pragma_Table;
348 -- Initializes contents of PSD_Pragma_Settings table from ALI table
350 procedure Set_String (S : String);
351 -- Sets characters of given string in Statement_Buffer, starting at the
352 -- Last + 1 position, and updating last past the string value.
354 procedure Set_String_Replace (S : String);
355 -- Replaces the last S'Length characters in the Statement_Buffer with the
356 -- characters of S. The caller must ensure that these characters do in fact
357 -- exist in the Statement_Buffer.
359 type Qualification_Mode is (Dollar_Sign, Dot, Double_Underscores);
361 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores);
362 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
363 -- starting at the Last + 1 position and update Last past the value.
364 -- Depending on parameter Mode, a dot (.) can be qualified into double
365 -- underscores (__), a dollar sign ($) or left as is.
367 procedure Set_Unit_Number (U : Unit_Id);
368 -- Sets unit number (first unit is 1, leading zeroes output to line up all
369 -- output unit numbers nicely as required by the value, and by the total
372 procedure Write_Statement_Buffer;
373 -- Write out contents of statement buffer up to Last, and reset Last to 0
375 procedure Write_Statement_Buffer (S : String);
376 -- First writes its argument (using Set_String (S)), then writes out the
377 -- contents of statement buffer up to Last, and reset Last to 0
383 procedure Gen_Adafinal is
385 WBI (" procedure " & Ada_Final_Name.all & " is");
388 and Bind_Main_Program
389 and not CodePeer_Mode
391 WBI (" procedure s_stalib_adafinal;");
392 Set_String (" pragma Import (C, s_stalib_adafinal, ");
393 Set_String ("""system__standard_library__adafinal"");");
394 Write_Statement_Buffer;
399 if not CodePeer_Mode then
400 WBI (" if not Is_Elaborated then");
403 WBI (" Is_Elaborated := False;");
406 -- On non-virtual machine targets, finalization is done differently
407 -- depending on whether this is the main program or a library.
409 if VM_Target = No_VM and then not CodePeer_Mode then
410 if Bind_Main_Program then
411 WBI (" s_stalib_adafinal;");
412 elsif Lib_Final_Built then
413 WBI (" finalize_library;");
418 -- Pragma Import C cannot be used on virtual machine targets, therefore
419 -- call the runtime finalization routine directly. Similarly in CodePeer
420 -- mode, where imported functions are ignored.
423 WBI (" System.Standard_Library.Adafinal;");
426 WBI (" end " & Ada_Final_Name.all & ";");
434 procedure Gen_Adainit is
435 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
436 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
439 -- Declare the access-to-subprogram type used for initialization of
440 -- of __gnat_finalize_library_objects. This is declared at library
441 -- level for compatibility with the type used in System.Soft_Links.
442 -- The import of the soft link which performs library-level object
443 -- finalization is not needed for VM targets; regular Ada is used in
444 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
445 -- tasks are non-terminating, so we do not want finalization.
447 if not Suppress_Standard_Library_On_Target
448 and then VM_Target = No_VM
449 and then not CodePeer_Mode
450 and then not Configurable_Run_Time_On_Target
452 WBI (" type No_Param_Proc is access procedure;");
456 WBI (" procedure " & Ada_Init_Name.all & " is");
458 -- In CodePeer mode, simplify adainit procedure by only calling
459 -- elaboration procedures.
461 if CodePeer_Mode then
464 -- When compiling for the AAMP small library, where the standard library
465 -- is no longer suppressed, we still want to exclude the setting of the
466 -- various imported globals, which aren't present for that library.
468 elsif AAMP_On_Target and then Configurable_Run_Time_On_Target then
472 -- If the standard library is suppressed, then the only global variables
473 -- that might be needed (by the Ravenscar profile) are the priority and
474 -- the processor for the environment task.
476 elsif Suppress_Standard_Library_On_Target then
477 if Main_Priority /= No_Main_Priority then
478 WBI (" Main_Priority : Integer;");
479 WBI (" pragma Import (C, Main_Priority," &
480 " ""__gl_main_priority"");");
484 if Main_CPU /= No_Main_CPU then
485 WBI (" Main_CPU : Integer;");
486 WBI (" pragma Import (C, Main_CPU," &
487 " ""__gl_main_cpu"");");
491 if System_Interrupts_Used
492 and then Partition_Elaboration_Policy_Specified = 'S'
494 WBI (" procedure Install_Restricted_Handlers_Sequential;");
495 WBI (" pragma Import (C," &
496 "Install_Restricted_Handlers_Sequential," &
497 " ""__gnat_attach_all_handlers"");");
501 if System_Tasking_Restricted_Stages_Used
502 and then Partition_Elaboration_Policy_Specified = 'S'
504 WBI (" Partition_Elaboration_Policy : Character;");
505 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
506 " ""__gnat_partition_elaboration_policy"");");
508 WBI (" procedure Activate_All_Tasks_Sequential;");
509 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
510 " ""__gnat_activate_all_tasks"");");
515 if Main_Priority /= No_Main_Priority then
516 Set_String (" Main_Priority := ");
517 Set_Int (Main_Priority);
519 Write_Statement_Buffer;
522 if Main_CPU /= No_Main_CPU then
523 Set_String (" Main_CPU := ");
526 Write_Statement_Buffer;
529 if System_Tasking_Restricted_Stages_Used
530 and then Partition_Elaboration_Policy_Specified = 'S'
532 Set_String (" Partition_Elaboration_Policy := '");
533 Set_Char (Partition_Elaboration_Policy_Specified);
535 Write_Statement_Buffer;
538 if Main_Priority = No_Main_Priority
539 and then Main_CPU = No_Main_CPU
540 and then not System_Tasking_Restricted_Stages_Used
545 -- Normal case (standard library not suppressed). Set all global values
546 -- used by the run time.
549 WBI (" Main_Priority : Integer;");
550 WBI (" pragma Import (C, Main_Priority, " &
551 """__gl_main_priority"");");
552 WBI (" Time_Slice_Value : Integer;");
553 WBI (" pragma Import (C, Time_Slice_Value, " &
554 """__gl_time_slice_val"");");
555 WBI (" WC_Encoding : Character;");
556 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
557 WBI (" Locking_Policy : Character;");
558 WBI (" pragma Import (C, Locking_Policy, " &
559 """__gl_locking_policy"");");
560 WBI (" Queuing_Policy : Character;");
561 WBI (" pragma Import (C, Queuing_Policy, " &
562 """__gl_queuing_policy"");");
563 WBI (" Task_Dispatching_Policy : Character;");
564 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
565 """__gl_task_dispatching_policy"");");
566 WBI (" Priority_Specific_Dispatching : System.Address;");
567 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
568 """__gl_priority_specific_dispatching"");");
569 WBI (" Num_Specific_Dispatching : Integer;");
570 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
571 """__gl_num_specific_dispatching"");");
572 WBI (" Main_CPU : Integer;");
573 WBI (" pragma Import (C, Main_CPU, " &
574 """__gl_main_cpu"");");
576 WBI (" Interrupt_States : System.Address;");
577 WBI (" pragma Import (C, Interrupt_States, " &
578 """__gl_interrupt_states"");");
579 WBI (" Num_Interrupt_States : Integer;");
580 WBI (" pragma Import (C, Num_Interrupt_States, " &
581 """__gl_num_interrupt_states"");");
582 WBI (" Unreserve_All_Interrupts : Integer;");
583 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
584 """__gl_unreserve_all_interrupts"");");
586 if Exception_Tracebacks then
587 WBI (" Exception_Tracebacks : Integer;");
588 WBI (" pragma Import (C, Exception_Tracebacks, " &
589 """__gl_exception_tracebacks"");");
592 WBI (" Detect_Blocking : Integer;");
593 WBI (" pragma Import (C, Detect_Blocking, " &
594 """__gl_detect_blocking"");");
595 WBI (" Default_Stack_Size : Integer;");
596 WBI (" pragma Import (C, Default_Stack_Size, " &
597 """__gl_default_stack_size"");");
598 WBI (" Leap_Seconds_Support : Integer;");
599 WBI (" pragma Import (C, Leap_Seconds_Support, " &
600 """__gl_leap_seconds_support"");");
602 -- Import entry point for elaboration time signal handler
603 -- installation, and indication of if it's been called previously.
606 WBI (" procedure Install_Handler;");
607 WBI (" pragma Import (C, Install_Handler, " &
608 """__gnat_install_handler"");");
610 WBI (" Handler_Installed : Integer;");
611 WBI (" pragma Import (C, Handler_Installed, " &
612 """__gnat_handler_installed"");");
614 -- Import handlers attach procedure for sequential elaboration policy
616 if System_Interrupts_Used
617 and then Partition_Elaboration_Policy_Specified = 'S'
619 WBI (" procedure Install_Restricted_Handlers_Sequential;");
620 WBI (" pragma Import (C," &
621 "Install_Restricted_Handlers_Sequential," &
622 " ""__gnat_attach_all_handlers"");");
626 -- Import task activation procedure for sequential elaboration
629 if System_Tasking_Restricted_Stages_Used
630 and then Partition_Elaboration_Policy_Specified = 'S'
632 WBI (" Partition_Elaboration_Policy : Character;");
633 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
634 " ""__gnat_partition_elaboration_policy"");");
636 WBI (" procedure Activate_All_Tasks_Sequential;");
637 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
638 " ""__gnat_activate_all_tasks"");");
641 -- The import of the soft link which performs library-level object
642 -- finalization is not needed for VM targets; regular Ada is used in
643 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
644 -- tasks are non-terminating, so we do not want finalization.
646 if VM_Target = No_VM and then not Configurable_Run_Time_On_Target then
648 WBI (" Finalize_Library_Objects : No_Param_Proc;");
649 WBI (" pragma Import (C, Finalize_Library_Objects, " &
650 """__gnat_finalize_library_objects"");");
653 -- Initialize stack limit variable of the environment task if the
654 -- stack check method is stack limit and stack check is enabled.
656 if Stack_Check_Limits_On_Target
657 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
660 WBI (" procedure Initialize_Stack_Limit;");
661 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
662 """__gnat_initialize_stack_limit"");");
665 -- Special processing when main program is CIL function/procedure
667 if VM_Target = CLI_Target
668 and then Bind_Main_Program
669 and then not No_Main_Subprogram
673 -- Function case, use Set_Exit_Status to report the returned
674 -- status code, since that is the only mechanism available.
676 if ALIs.Table (ALIs.First).Main_Program = Func then
677 WBI (" Result : Integer;");
678 WBI (" procedure Set_Exit_Status (Code : Integer);");
679 WBI (" pragma Import (C, Set_Exit_Status, " &
680 """__gnat_set_exit_status"");");
682 WBI (" function Ada_Main_Program return Integer;");
687 WBI (" procedure Ada_Main_Program;");
690 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
691 Name_Len := Name_Len - 2;
692 WBI (" pragma Import (CIL, Ada_Main_Program, """
693 & Name_Buffer (1 .. Name_Len) & "."
694 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
697 -- When dispatching domains are used then we need to signal it
698 -- before calling the main procedure.
700 if Dispatching_Domains_Used then
701 WBI (" procedure Freeze_Dispatching_Domains;");
702 WBI (" pragma Import");
703 WBI (" (Ada, Freeze_Dispatching_Domains, "
704 & """__gnat_freeze_dispatching_domains"");");
708 WBI (" if Is_Elaborated then");
711 WBI (" Is_Elaborated := True;");
713 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
714 -- restriction No_Standard_Allocators_After_Elaboration is active.
716 if Cumulative_Restrictions.Set
717 (No_Standard_Allocators_After_Elaboration)
719 WBI (" System.Elaboration_Allocators."
720 & "Mark_Start_Of_Elaboration;");
723 -- Generate assignments to initialize globals
725 Set_String (" Main_Priority := ");
726 Set_Int (Main_Priority);
728 Write_Statement_Buffer;
730 Set_String (" Time_Slice_Value := ");
732 if Task_Dispatching_Policy_Specified = 'F'
733 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
737 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
741 Write_Statement_Buffer;
743 Set_String (" WC_Encoding := '");
744 Set_Char (Get_WC_Encoding);
747 Write_Statement_Buffer;
749 Set_String (" Locking_Policy := '");
750 Set_Char (Locking_Policy_Specified);
752 Write_Statement_Buffer;
754 Set_String (" Queuing_Policy := '");
755 Set_Char (Queuing_Policy_Specified);
757 Write_Statement_Buffer;
759 Set_String (" Task_Dispatching_Policy := '");
760 Set_Char (Task_Dispatching_Policy_Specified);
762 Write_Statement_Buffer;
764 if System_Tasking_Restricted_Stages_Used
765 and then Partition_Elaboration_Policy_Specified = 'S'
767 Set_String (" Partition_Elaboration_Policy := '");
768 Set_Char (Partition_Elaboration_Policy_Specified);
770 Write_Statement_Buffer;
775 WBI (" Priority_Specific_Dispatching :=");
776 WBI (" Local_Priority_Specific_Dispatching'Address;");
778 Set_String (" Num_Specific_Dispatching := ");
779 Set_Int (PSD_Pragma_Settings.Last + 1);
781 Write_Statement_Buffer;
783 Set_String (" Main_CPU := ");
786 Write_Statement_Buffer;
788 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
790 Set_String (" Num_Interrupt_States := ");
791 Set_Int (IS_Pragma_Settings.Last + 1);
793 Write_Statement_Buffer;
795 Set_String (" Unreserve_All_Interrupts := ");
797 if Unreserve_All_Interrupts_Specified then
804 Write_Statement_Buffer;
806 if Exception_Tracebacks then
807 WBI (" Exception_Tracebacks := 1;");
810 Set_String (" Detect_Blocking := ");
812 if Detect_Blocking then
819 Write_Statement_Buffer;
821 Set_String (" Default_Stack_Size := ");
822 Set_Int (Default_Stack_Size);
824 Write_Statement_Buffer;
826 Set_String (" Leap_Seconds_Support := ");
828 if Leap_Seconds_Support then
835 Write_Statement_Buffer;
837 -- Generate call to Install_Handler
839 -- In .NET, when binding with -z, we don't install the signal handler
840 -- to let the caller handle the last exception handler.
842 if VM_Target /= CLI_Target
843 or else Bind_Main_Program
846 WBI (" if Handler_Installed = 0 then");
847 WBI (" Install_Handler;");
852 -- Generate call to set Initialize_Scalar values if active
854 if Initialize_Scalars_Used then
856 Set_String (" System.Scalar_Values.Initialize ('");
857 Set_Char (Initialize_Scalars_Mode1);
859 Set_Char (Initialize_Scalars_Mode2);
861 Write_Statement_Buffer;
864 -- Generate assignment of default secondary stack size if set
866 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
868 Set_String (" System.Secondary_Stack.");
869 Set_String ("Default_Secondary_Stack_Size := ");
870 Set_Int (Opt.Default_Sec_Stack_Size);
872 Write_Statement_Buffer;
875 -- Initialize stack limit variable of the environment task if the
876 -- stack check method is stack limit and stack check is enabled.
878 if Stack_Check_Limits_On_Target
879 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
882 WBI (" Initialize_Stack_Limit;");
885 -- On CodePeer, the finalization of library objects is not relevant
887 if CodePeer_Mode then
890 -- On virtual machine targets, or on non-virtual machine ones if this
891 -- is the main program case, attach finalize_library to the soft link.
892 -- Do it only when not using a restricted run time, in which case tasks
893 -- are non-terminating, so we do not want library-level finalization.
895 elsif (VM_Target /= No_VM or else Bind_Main_Program)
896 and then not Configurable_Run_Time_On_Target
897 and then not Suppress_Standard_Library_On_Target
901 if VM_Target = No_VM then
902 if Lib_Final_Built then
903 Set_String (" Finalize_Library_Objects := ");
904 Set_String ("finalize_library'access;");
906 Set_String (" Finalize_Library_Objects := null;");
909 -- On VM targets use regular Ada to set the soft link
912 if Lib_Final_Built then
914 (" System.Soft_Links.Finalize_Library_Objects");
915 Set_String (" := finalize_library'access;");
918 (" System.Soft_Links.Finalize_Library_Objects");
919 Set_String (" := null;");
923 Write_Statement_Buffer;
926 -- Generate elaboration calls
928 if not CodePeer_Mode then
934 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
935 -- restriction No_Standard_Allocators_After_Elaboration is active.
937 if Cumulative_Restrictions.Set
938 (No_Standard_Allocators_After_Elaboration)
940 WBI (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
943 -- From this point, no new dispatching domain can be created.
945 if Dispatching_Domains_Used then
946 WBI (" Freeze_Dispatching_Domains;");
949 -- Sequential partition elaboration policy
951 if Partition_Elaboration_Policy_Specified = 'S' then
952 if System_Interrupts_Used then
953 WBI (" Install_Restricted_Handlers_Sequential;");
956 if System_Tasking_Restricted_Stages_Used then
957 WBI (" Activate_All_Tasks_Sequential;");
961 -- Case of main program is CIL function or procedure
963 if VM_Target = CLI_Target
964 and then Bind_Main_Program
965 and then not No_Main_Subprogram
967 -- For function case, use Set_Exit_Status to set result
969 if ALIs.Table (ALIs.First).Main_Program = Func then
970 WBI (" Result := Ada_Main_Program;");
971 WBI (" Set_Exit_Status (Result);");
976 WBI (" Ada_Main_Program;");
980 WBI (" end " & Ada_Init_Name.all & ";");
984 --------------------------
985 -- Gen_CodePeer_Wrapper --
986 --------------------------
988 procedure Gen_CodePeer_Wrapper is
989 Callee_Name : constant String := "Ada_Main_Program";
992 if ALIs.Table (ALIs.First).Main_Program = Proc then
993 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
995 WBI (" " & Callee_Name & ";");
998 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1000 WBI (" return " & Callee_Name & ";");
1003 WBI (" end " & CodePeer_Wrapper_Name & ";");
1005 end Gen_CodePeer_Wrapper;
1007 --------------------
1008 -- Gen_Elab_Calls --
1009 --------------------
1011 procedure Gen_Elab_Calls is
1012 Check_Elab_Flag : Boolean;
1015 -- Loop through elaboration order entries
1017 for E in Elab_Order.First .. Elab_Order.Last loop
1019 Unum : constant Unit_Id := Elab_Order.Table (E);
1020 U : Unit_Record renames Units.Table (Unum);
1022 Unum_Spec : Unit_Id;
1023 -- This is the unit number of the spec that corresponds to
1024 -- this entry. It is the same as Unum except when the body
1025 -- and spec are different and we are currently processing
1026 -- the body, in which case it is the spec (Unum + 1).
1029 if U.Utype = Is_Body then
1030 Unum_Spec := Unum + 1;
1035 -- Nothing to do if predefined unit in no run time mode
1037 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1040 -- Likewise if this is an interface to a stand alone library
1042 elsif U.SAL_Interface then
1045 -- Case of no elaboration code
1049 -- In CodePeer mode, we special case subprogram bodies which
1050 -- are handled in the 'else' part below, and lead to a call
1051 -- to <subp>'Elab_Subp_Body.
1053 and then (not CodePeer_Mode
1057 or else U.Utype = Is_Spec
1058 or else U.Utype = Is_Spec_Only
1059 or else U.Unit_Kind /= 's')
1061 -- In the case of a body with a separate spec, where the
1062 -- separate spec has an elaboration entity defined, this is
1063 -- where we increment the elaboration entity if one exists
1065 if U.Utype = Is_Body
1066 and then Units.Table (Unum_Spec).Set_Elab_Entity
1067 and then not CodePeer_Mode
1070 Set_Unit_Number (Unum_Spec);
1072 -- The AAMP target has no notion of shared libraries, and
1073 -- there's no possibility of reelaboration, so we treat the
1074 -- the elaboration var as a flag instead of a counter and
1077 if AAMP_On_Target then
1078 Set_String (" := 1;");
1080 -- Otherwise (normal case), increment elaboration counter
1083 Set_String (" := E");
1084 Set_Unit_Number (Unum_Spec);
1085 Set_String (" + 1;");
1088 Write_Statement_Buffer;
1090 -- In the special case where the target is AAMP and the unit is
1091 -- a spec with a body, the elaboration entity is initialized
1092 -- here. This is done because it's the only way to accomplish
1093 -- initialization of such entities, as there is no mechanism
1094 -- for load time global variable initialization on AAMP.
1096 elsif AAMP_On_Target
1097 and then U.Utype = Is_Spec
1098 and then Units.Table (Unum_Spec).Set_Elab_Entity
1101 Set_Unit_Number (Unum_Spec);
1102 Set_String (" := 0;");
1103 Write_Statement_Buffer;
1106 -- Here if elaboration code is present. If binding a library
1107 -- or if there is a non-Ada main subprogram then we generate:
1109 -- if uname_E = 0 then
1110 -- uname'elab_[spec|body];
1112 -- uname_E := uname_E + 1;
1114 -- Otherwise, elaboration routines are called unconditionally:
1116 -- uname'elab_[spec|body];
1117 -- uname_E := uname_E + 1;
1119 -- The uname_E increment is skipped if this is a separate spec,
1120 -- since it will be done when we process the body.
1122 -- In CodePeer mode, we do not generate any reference to xxx_E
1123 -- variables, only calls to 'Elab* subprograms.
1126 -- In the special case where the target is AAMP and the unit is
1127 -- a spec with a body, the elaboration entity is initialized
1128 -- here. This is done because it's the only way to accomplish
1129 -- initialization of such entities, as there is no mechanism
1130 -- for load time global variable initialization on AAMP.
1133 and then U.Utype = Is_Spec
1134 and then Units.Table (Unum_Spec).Set_Elab_Entity
1137 Set_Unit_Number (Unum_Spec);
1138 Set_String (" := 0;");
1139 Write_Statement_Buffer;
1142 -- Check incompatibilities with No_Multiple_Elaboration
1144 if not CodePeer_Mode
1145 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1147 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1149 if Force_Checking_Of_Elaboration_Flags then
1151 ("-F (force elaboration checks) switch not allowed "
1152 & "with restriction No_Multiple_Elaboration active");
1154 -- Interfacing of libraries not allowed
1156 elsif Interface_Library_Unit then
1158 ("binding of interfaced libraries not allowed "
1159 & "with restriction No_Multiple_Elaboration active");
1161 -- Non-Ada main program not allowed
1163 elsif not Bind_Main_Program then
1165 ("non-Ada main program not allowed "
1166 & "with restriction No_Multiple_Elaboration active");
1170 -- OK, see if we need to test elaboration flag
1173 Units.Table (Unum_Spec).Set_Elab_Entity
1174 and then not CodePeer_Mode
1175 and then (Force_Checking_Of_Elaboration_Flags
1176 or Interface_Library_Unit
1177 or not Bind_Main_Program);
1179 if Check_Elab_Flag then
1180 Set_String (" if E");
1181 Set_Unit_Number (Unum_Spec);
1182 Set_String (" = 0 then");
1183 Write_Statement_Buffer;
1188 Get_Decoded_Name_String_With_Brackets (U.Uname);
1190 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1191 if Name_Buffer (Name_Len) = 's' then
1192 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1195 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1199 Name_Len := Name_Len + 12;
1202 if Name_Buffer (Name_Len) = 's' then
1203 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1205 Name_Len := Name_Len + 8;
1207 -- Special case in CodePeer mode for subprogram bodies
1208 -- which correspond to CodePeer 'Elab_Subp_Body special
1211 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1212 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1214 Name_Len := Name_Len + 13;
1217 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1219 Name_Len := Name_Len + 8;
1223 Set_Casing (U.Icasing);
1226 Write_Statement_Buffer;
1228 if Check_Elab_Flag then
1232 if U.Utype /= Is_Spec
1233 and then not CodePeer_Mode
1234 and then Units.Table (Unum_Spec).Set_Elab_Entity
1237 Set_Unit_Number (Unum_Spec);
1239 -- The AAMP target has no notion of shared libraries, and
1240 -- there's no possibility of reelaboration, so we treat the
1241 -- the elaboration var as a flag instead of a counter and
1244 if AAMP_On_Target then
1245 Set_String (" := 1;");
1247 -- Otherwise (normal case), increment elaboration counter
1250 Set_String (" := E");
1251 Set_Unit_Number (Unum_Spec);
1252 Set_String (" + 1;");
1255 Write_Statement_Buffer;
1262 ------------------------
1263 -- Gen_Elab_Externals --
1264 ------------------------
1266 procedure Gen_Elab_Externals is
1268 if CodePeer_Mode then
1272 for E in Elab_Order.First .. Elab_Order.Last loop
1274 Unum : constant Unit_Id := Elab_Order.Table (E);
1275 U : Unit_Record renames Units.Table (Unum);
1278 -- Check for Elab_Entity to be set for this unit
1280 if U.Set_Elab_Entity
1282 -- Don't generate reference for stand alone library
1284 and then not U.SAL_Interface
1286 -- Don't generate reference for predefined file in No_Run_Time
1287 -- mode, since we don't include the object files in this case
1291 and then Is_Predefined_File_Name (U.Sfile))
1295 Set_Unit_Number (Unum);
1298 when No_VM | JVM_Target =>
1299 Set_String (" : Short_Integer; pragma Import (Ada, ");
1301 Set_String (" : Short_Integer; pragma Import (CIL, ");
1305 Set_Unit_Number (Unum);
1306 Set_String (", """);
1307 Get_Name_String (U.Uname);
1309 -- In the case of JGNAT we need to emit an Import name that
1310 -- includes the class name (using '$' separators in the case
1311 -- of a child unit name).
1313 if VM_Target /= No_VM then
1314 for J in 1 .. Name_Len - 2 loop
1315 if VM_Target = CLI_Target
1316 or else Name_Buffer (J) /= '.'
1318 Set_Char (Name_Buffer (J));
1324 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
1327 Set_String ("_pkg.");
1330 -- If the unit name is very long, then split the
1331 -- Import link name across lines using "&" (occurs
1332 -- in some C2 tests).
1334 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
1335 Set_String (""" &");
1336 Write_Statement_Buffer;
1342 Set_String ("_E"");");
1343 Write_Statement_Buffer;
1349 end Gen_Elab_Externals;
1351 --------------------
1352 -- Gen_Elab_Order --
1353 --------------------
1355 procedure Gen_Elab_Order is
1357 WBI (" -- BEGIN ELABORATION ORDER");
1359 for J in Elab_Order.First .. Elab_Order.Last loop
1360 Set_String (" -- ");
1361 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1363 Write_Statement_Buffer;
1366 WBI (" -- END ELABORATION ORDER");
1370 --------------------------
1371 -- Gen_Finalize_Library --
1372 --------------------------
1374 procedure Gen_Finalize_Library is
1377 Uspec : Unit_Record;
1380 procedure Gen_Header;
1381 -- Generate the header of the finalization routine
1387 procedure Gen_Header is
1389 WBI (" procedure finalize_library is");
1393 -- Start of processing for Gen_Finalize_Library
1396 if CodePeer_Mode then
1400 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1401 Unum := Elab_Order.Table (E);
1402 U := Units.Table (Unum);
1404 -- Dealing with package bodies is a little complicated. In such
1405 -- cases we must retrieve the package spec since it contains the
1406 -- spec of the body finalizer.
1408 if U.Utype = Is_Body then
1410 Uspec := Units.Table (Unum);
1415 Get_Name_String (Uspec.Uname);
1417 -- We are only interested in non-generic packages
1419 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1422 -- That aren't an interface to a stand alone library
1424 elsif U.SAL_Interface then
1427 -- Case of no finalization
1429 elsif not U.Has_Finalizer then
1431 -- The only case in which we have to do something is if this
1432 -- is a body, with a separate spec, where the separate spec
1433 -- has a finalizer. In that case, this is where we decrement
1434 -- the elaboration entity.
1436 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1437 if not Lib_Final_Built then
1439 Lib_Final_Built := True;
1443 Set_Unit_Number (Unum);
1444 Set_String (" := E");
1445 Set_Unit_Number (Unum);
1446 Set_String (" - 1;");
1447 Write_Statement_Buffer;
1451 if not Lib_Final_Built then
1453 Lib_Final_Built := True;
1458 -- procedure F<Count>;
1460 Set_String (" declare");
1461 Write_Statement_Buffer;
1463 Set_String (" procedure F");
1466 Write_Statement_Buffer;
1469 -- pragma Import (CIL, F<Count>,
1470 -- "xx.yy_pkg.xx__yy__finalize_[body|spec]");
1471 -- -- for .NET targets
1473 -- pragma Import (Java, F<Count>,
1474 -- "xx$yy.xx__yy__finalize_[body|spec]");
1475 -- -- for JVM targets
1477 -- pragma Import (Ada, F<Count>,
1478 -- "xx__yy__finalize_[body|spec]");
1479 -- -- for default targets
1481 if VM_Target = CLI_Target then
1482 Set_String (" pragma Import (CIL, F");
1483 elsif VM_Target = JVM_Target then
1484 Set_String (" pragma Import (Java, F");
1486 Set_String (" pragma Import (Ada, F");
1490 Set_String (", """);
1492 -- Perform name construction
1494 -- .NET xx.yy_pkg.xx__yy__finalize
1496 if VM_Target = CLI_Target then
1497 Set_Unit_Name (Mode => Dot);
1498 Set_String ("_pkg.");
1500 -- JVM xx$yy.xx__yy__finalize
1502 elsif VM_Target = JVM_Target then
1503 Set_Unit_Name (Mode => Dollar_Sign);
1507 -- Default xx__yy__finalize
1510 Set_String ("__finalize_");
1512 -- Package spec processing
1514 if U.Utype = Is_Spec
1515 or else U.Utype = Is_Spec_Only
1517 Set_String ("spec");
1519 -- Package body processing
1522 Set_String ("body");
1525 Set_String (""");");
1526 Write_Statement_Buffer;
1528 -- If binding a library or if there is a non-Ada main subprogram
1529 -- then we generate:
1532 -- uname_E := uname_E - 1;
1533 -- if uname_E = 0 then
1538 -- Otherwise, finalization routines are called unconditionally:
1541 -- uname_E := uname_E - 1;
1545 -- The uname_E decrement is skipped if this is a separate spec,
1546 -- since it will be done when we process the body.
1550 if U.Utype /= Is_Spec then
1552 Set_Unit_Number (Unum);
1553 Set_String (" := E");
1554 Set_Unit_Number (Unum);
1555 Set_String (" - 1;");
1556 Write_Statement_Buffer;
1559 if Interface_Library_Unit or not Bind_Main_Program then
1560 Set_String (" if E");
1561 Set_Unit_Number (Unum);
1562 Set_String (" = 0 then");
1563 Write_Statement_Buffer;
1570 Write_Statement_Buffer;
1572 if Interface_Library_Unit or not Bind_Main_Program then
1582 if Lib_Final_Built then
1584 -- It is possible that the finalization of a library-level object
1585 -- raised an exception. In that case import the actual exception
1586 -- and the routine necessary to raise it.
1588 if VM_Target = No_VM then
1590 WBI (" procedure Reraise_Library_Exception_If_Any;");
1592 Set_String (" pragma Import (Ada, ");
1593 Set_String ("Reraise_Library_Exception_If_Any, ");
1594 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1595 Write_Statement_Buffer;
1598 WBI (" Reraise_Library_Exception_If_Any;");
1601 -- VM-specific code, use regular Ada to produce the desired behavior
1604 WBI (" if System.Soft_Links.Library_Exception_Set then");
1606 Set_String (" Ada.Exceptions.Reraise_Occurrence (");
1607 Set_String ("System.Soft_Links.Library_Exception);");
1608 Write_Statement_Buffer;
1613 WBI (" end finalize_library;");
1616 end Gen_Finalize_Library;
1622 procedure Gen_Main is
1624 if not No_Main_Subprogram then
1626 -- To call the main program, we declare it using a pragma Import
1627 -- Ada with the right link name.
1629 -- It might seem more obvious to "with" the main program, and call
1630 -- it in the normal Ada manner. We do not do this for three
1633 -- 1. It is more efficient not to recompile the main program
1634 -- 2. We are not entitled to assume the source is accessible
1635 -- 3. We don't know what options to use to compile it
1637 -- It is really reason 3 that is most critical (indeed we used
1638 -- to generate the "with", but several regression tests failed).
1640 if ALIs.Table (ALIs.First).Main_Program = Func then
1641 WBI (" function Ada_Main_Program return Integer;");
1643 WBI (" procedure Ada_Main_Program;");
1646 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1647 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1648 Set_Main_Program_Name;
1649 Set_String (""");");
1651 Write_Statement_Buffer;
1654 -- For CodePeer, declare a wrapper for the user-defined main program
1656 if CodePeer_Mode then
1657 Gen_CodePeer_Wrapper;
1661 if Exit_Status_Supported_On_Target then
1662 Set_String (" function ");
1664 Set_String (" procedure ");
1667 Set_String (Get_Main_Name);
1669 if Command_Line_Args_On_Target then
1670 Write_Statement_Buffer;
1671 WBI (" (argc : Integer;");
1672 WBI (" argv : System.Address;");
1673 WBI (" envp : System.Address)");
1675 if Exit_Status_Supported_On_Target then
1676 WBI (" return Integer");
1682 if Exit_Status_Supported_On_Target then
1683 Set_String (" return Integer is");
1688 Write_Statement_Buffer;
1691 if Opt.Default_Exit_Status /= 0
1692 and then Bind_Main_Program
1693 and then not Configurable_Run_Time_Mode
1695 WBI (" procedure Set_Exit_Status (Status : Integer);");
1696 WBI (" pragma Import (C, Set_Exit_Status, " &
1697 """__gnat_set_exit_status"");");
1701 -- Initialize and Finalize
1703 if not CodePeer_Mode
1704 and then not Cumulative_Restrictions.Set (No_Finalization)
1706 WBI (" procedure Initialize (Addr : System.Address);");
1707 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1709 WBI (" procedure Finalize;");
1710 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1713 -- If we want to analyze the stack, we must import corresponding symbols
1715 if Dynamic_Stack_Measurement then
1717 WBI (" procedure Output_Results;");
1718 WBI (" pragma Import (C, Output_Results, " &
1719 """__gnat_stack_usage_output_results"");");
1723 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1724 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1725 """__gnat_stack_usage_initialize"");");
1728 -- Deal with declarations for main program case
1730 if not No_Main_Subprogram then
1731 if ALIs.Table (ALIs.First).Main_Program = Func then
1732 WBI (" Result : Integer;");
1736 if Bind_Main_Program
1737 and not Suppress_Standard_Library_On_Target
1738 and not CodePeer_Mode
1740 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1745 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1746 -- not referenced elsewhere in the generated program, but is needed
1747 -- by the debugger (that's why it is generated in the first place).
1748 -- The reference stops Ada_Main_Program_Name from being optimized
1749 -- away by smart linkers, such as the AiX linker.
1751 -- Because this variable is unused, we make this variable "aliased"
1752 -- with a pragma Volatile in order to tell the compiler to preserve
1753 -- this variable at any level of optimization.
1755 if Bind_Main_Program and not CodePeer_Mode then
1756 WBI (" Ensure_Reference : aliased System.Address := " &
1757 "Ada_Main_Program_Name'Address;");
1758 WBI (" pragma Volatile (Ensure_Reference);");
1764 -- Acquire command line arguments if present on target
1766 if CodePeer_Mode then
1769 elsif Command_Line_Args_On_Target then
1770 WBI (" gnat_argc := argc;");
1771 WBI (" gnat_argv := argv;");
1772 WBI (" gnat_envp := envp;");
1775 -- If configurable run time and no command line args, then nothing
1776 -- needs to be done since the gnat_argc/argv/envp variables are
1777 -- suppressed in this case.
1779 elsif Configurable_Run_Time_On_Target then
1782 -- Otherwise set dummy values (to be filled in by some other unit?)
1785 WBI (" gnat_argc := 0;");
1786 WBI (" gnat_argv := System.Null_Address;");
1787 WBI (" gnat_envp := System.Null_Address;");
1790 if Opt.Default_Exit_Status /= 0
1791 and then Bind_Main_Program
1792 and then not Configurable_Run_Time_Mode
1794 Set_String (" Set_Exit_Status (");
1795 Set_Int (Opt.Default_Exit_Status);
1797 Write_Statement_Buffer;
1800 if Dynamic_Stack_Measurement then
1801 Set_String (" Initialize_Stack_Analysis (");
1802 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1804 Write_Statement_Buffer;
1807 if not Cumulative_Restrictions.Set (No_Finalization)
1808 and then not CodePeer_Mode
1810 if not No_Main_Subprogram
1811 and then Bind_Main_Program
1812 and then not Suppress_Standard_Library_On_Target
1814 WBI (" Initialize (SEH'Address);");
1816 WBI (" Initialize (System.Null_Address);");
1820 WBI (" " & Ada_Init_Name.all & ";");
1822 if not No_Main_Subprogram then
1823 if CodePeer_Mode then
1824 if ALIs.Table (ALIs.First).Main_Program = Proc then
1825 WBI (" " & CodePeer_Wrapper_Name & ";");
1827 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1830 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1831 WBI (" Ada_Main_Program;");
1834 WBI (" Result := Ada_Main_Program;");
1838 -- Adafinal call is skipped if no finalization
1840 if not Cumulative_Restrictions.Set (No_Finalization) then
1844 -- Prints the result of static stack analysis
1846 if Dynamic_Stack_Measurement then
1847 WBI (" Output_Results;");
1850 -- Finalize is only called if we have a run time
1852 if not Cumulative_Restrictions.Set (No_Finalization)
1853 and then not CodePeer_Mode
1860 if Exit_Status_Supported_On_Target then
1861 if No_Main_Subprogram
1862 or else ALIs.Table (ALIs.First).Main_Program = Proc
1864 WBI (" return (gnat_exit_status);");
1866 WBI (" return (Result);");
1874 ------------------------------
1875 -- Gen_Object_Files_Options --
1876 ------------------------------
1878 procedure Gen_Object_Files_Options is
1880 -- This keeps track of the position in the sorted set of entries
1881 -- in the Linker_Options table of where the first entry from an
1882 -- internal file appears.
1884 Linker_Option_List_Started : Boolean := False;
1885 -- Set to True when "LINKER OPTION LIST" is displayed
1887 procedure Write_Linker_Option;
1888 -- Write binder info linker option
1890 -------------------------
1891 -- Write_Linker_Option --
1892 -------------------------
1894 procedure Write_Linker_Option is
1899 -- Loop through string, breaking at null's
1902 while Start < Name_Len loop
1904 -- Find null ending this section
1907 while Name_Buffer (Stop) /= ASCII.NUL
1908 and then Stop <= Name_Len loop
1912 -- Process section if non-null
1914 if Stop > Start then
1915 if Output_Linker_Option_List then
1916 if not Zero_Formatting then
1917 if not Linker_Option_List_Started then
1918 Linker_Option_List_Started := True;
1920 Write_Str (" LINKER OPTION LIST");
1928 Write_Str (Name_Buffer (Start .. Stop - 1));
1931 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
1936 end Write_Linker_Option;
1938 -- Start of processing for Gen_Object_Files_Options
1941 WBI ("-- BEGIN Object file/option list");
1943 if Object_List_Filename /= null then
1944 Set_List_File (Object_List_Filename.all);
1947 for E in Elab_Order.First .. Elab_Order.Last loop
1949 -- If not spec that has an associated body, then generate a comment
1950 -- giving the name of the corresponding object file.
1952 if not Units.Table (Elab_Order.Table (E)).SAL_Interface
1953 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1957 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1959 -- If the presence of an object file is necessary or if it exists,
1962 if not Hostparm.Exclude_Missing_Objects
1964 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1966 WBI (" -- " & Name_Buffer (1 .. Name_Len));
1968 if Output_Object_List then
1969 Write_Str (Name_Buffer (1 .. Name_Len));
1976 if Object_List_Filename /= null then
1980 -- Add a "-Ldir" for each directory in the object path
1981 if VM_Target /= CLI_Target then
1982 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1984 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1987 Add_Str_To_Name_Buffer ("-L");
1988 Add_Str_To_Name_Buffer (Dir.all);
1989 Write_Linker_Option;
1994 -- Sort linker options
1996 -- This sort accomplishes two important purposes:
1998 -- a) All application files are sorted to the front, and all GNAT
1999 -- internal files are sorted to the end. This results in a well
2000 -- defined dividing line between the two sets of files, for the
2001 -- purpose of inserting certain standard library references into
2002 -- the linker arguments list.
2004 -- b) Given two different units, we sort the linker options so that
2005 -- those from a unit earlier in the elaboration order comes later
2006 -- in the list. This is a heuristic designed to create a more
2007 -- friendly order of linker options when the operations appear in
2008 -- separate units. The idea is that if unit A must be elaborated
2009 -- before unit B, then it is more likely that B references
2010 -- libraries included by A, than vice versa, so we want libraries
2011 -- included by A to come after libraries included by B.
2013 -- These two criteria are implemented by function Lt_Linker_Option. Note
2014 -- that a special case of b) is that specs are elaborated before bodies,
2015 -- so linker options from specs come after linker options for bodies,
2016 -- and again, the assumption is that libraries used by the body are more
2017 -- likely to reference libraries used by the spec, than vice versa.
2020 (Linker_Options.Last,
2021 Move_Linker_Option'Access,
2022 Lt_Linker_Option'Access);
2024 -- Write user linker options, i.e. the set of linker options that come
2025 -- from all files other than GNAT internal files, Lgnat is left set to
2026 -- point to the first entry from a GNAT internal file, or past the end
2027 -- of the entries if there are no internal files.
2029 Lgnat := Linker_Options.Last + 1;
2031 for J in 1 .. Linker_Options.Last loop
2032 if not Linker_Options.Table (J).Internal_File then
2033 Get_Name_String (Linker_Options.Table (J).Name);
2034 Write_Linker_Option;
2041 -- Now we insert standard linker options that must appear after the
2042 -- entries from user files, and before the entries from GNAT run-time
2043 -- files. The reason for this decision is that libraries referenced
2044 -- by internal routines may reference these standard library entries.
2046 -- Note that we do not insert anything when pragma No_Run_Time has
2047 -- been specified or when the standard libraries are not to be used,
2048 -- otherwise on some platforms, we may get duplicate symbols when
2049 -- linking (not clear if this is still the case, but it is harmless).
2051 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2054 if Opt.Shared_Libgnat then
2055 Add_Str_To_Name_Buffer ("-shared");
2057 Add_Str_To_Name_Buffer ("-static");
2060 -- Write directly to avoid -K output (why???)
2062 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2067 if Opt.Shared_Libgnat then
2068 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2070 Add_Str_To_Name_Buffer ("-lgnarl");
2073 Write_Linker_Option;
2078 if Opt.Shared_Libgnat then
2079 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2081 Add_Str_To_Name_Buffer ("-lgnat");
2084 Write_Linker_Option;
2087 -- Write linker options from all internal files
2089 for J in Lgnat .. Linker_Options.Last loop
2090 Get_Name_String (Linker_Options.Table (J).Name);
2091 Write_Linker_Option;
2094 if Output_Linker_Option_List and then not Zero_Formatting then
2098 WBI ("-- END Object file/option list ");
2099 end Gen_Object_Files_Options;
2101 ---------------------
2102 -- Gen_Output_File --
2103 ---------------------
2105 procedure Gen_Output_File (Filename : String) is
2107 -- Acquire settings for Interrupt_State pragmas
2109 Set_IS_Pragma_Table;
2111 -- Acquire settings for Priority_Specific_Dispatching pragma
2113 Set_PSD_Pragma_Table;
2115 -- For JGNAT the main program is already generated by the compiler
2117 if VM_Target = JVM_Target then
2118 Bind_Main_Program := False;
2121 -- Override time slice value if -T switch is set
2123 if Time_Slice_Set then
2124 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2127 -- Count number of elaboration calls
2129 for E in Elab_Order.First .. Elab_Order.Last loop
2130 if Units.Table (Elab_Order.Table (E)).No_Elab then
2133 Num_Elab_Calls := Num_Elab_Calls + 1;
2137 -- Generate output file in appropriate language
2139 Gen_Output_File_Ada (Filename);
2140 end Gen_Output_File;
2142 -------------------------
2143 -- Gen_Output_File_Ada --
2144 -------------------------
2146 procedure Gen_Output_File_Ada (Filename : String) is
2148 Ada_Main : constant String := Get_Ada_Main_Name;
2149 -- Name to be used for generated Ada main program. See the body of
2150 -- function Get_Ada_Main_Name for details on the form of the name.
2152 Needs_Library_Finalization : constant Boolean :=
2153 not Configurable_Run_Time_On_Target and then Has_Finalizer;
2154 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2155 -- non-terminating, so we do not want finalization.
2158 -- Name of generated bind file (spec)
2161 -- Name of generated bind file (body)
2164 -- Create spec first
2166 Create_Binder_Output (Filename, 's', Bfiles);
2168 -- We always compile the binder file in Ada 95 mode so that we properly
2169 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2170 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2172 WBI ("pragma Ada_95;");
2174 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2175 -- then we need to make sure that the binder program is compiled with
2176 -- the same restriction, so that no exception tables are generated.
2178 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2179 WBI ("pragma Restrictions (No_Exception_Handlers);");
2182 -- Same processing for Restrictions (No_Exception_Propagation)
2184 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2185 WBI ("pragma Restrictions (No_Exception_Propagation);");
2188 -- Same processing for pragma No_Run_Time
2190 if No_Run_Time_Mode then
2191 WBI ("pragma No_Run_Time;");
2194 -- Generate with of System so we can reference System.Address
2196 WBI ("with System;");
2198 -- Generate with of System.Initialize_Scalars if active
2200 if Initialize_Scalars_Used then
2201 WBI ("with System.Scalar_Values;");
2204 -- Generate with of System.Secondary_Stack if active
2206 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2207 WBI ("with System.Secondary_Stack;");
2210 Resolve_Binder_Options;
2212 -- Generate standard with's
2214 if not Suppress_Standard_Library_On_Target then
2215 if CodePeer_Mode then
2216 WBI ("with System.Standard_Library;");
2217 elsif VM_Target /= No_VM then
2218 WBI ("with System.Soft_Links;");
2219 WBI ("with System.Standard_Library;");
2223 WBI ("package " & Ada_Main & " is");
2224 WBI (" pragma Warnings (Off);");
2226 -- Main program case
2228 if Bind_Main_Program then
2229 if VM_Target = No_VM then
2231 -- Generate argc/argv stuff unless suppressed
2233 if Command_Line_Args_On_Target
2234 or not Configurable_Run_Time_On_Target
2237 WBI (" gnat_argc : Integer;");
2238 WBI (" gnat_argv : System.Address;");
2239 WBI (" gnat_envp : System.Address;");
2241 -- If the standard library is not suppressed, these variables
2242 -- are in the run-time data area for easy run time access.
2244 if not Suppress_Standard_Library_On_Target then
2246 WBI (" pragma Import (C, gnat_argc);");
2247 WBI (" pragma Import (C, gnat_argv);");
2248 WBI (" pragma Import (C, gnat_envp);");
2252 -- Define exit status. Again in normal mode, this is in the
2253 -- run-time library, and is initialized there, but in the
2254 -- configurable runtime case, the variable is declared and
2255 -- initialized in this file.
2259 if Configurable_Run_Time_Mode then
2260 if Exit_Status_Supported_On_Target then
2261 WBI (" gnat_exit_status : Integer := 0;");
2265 WBI (" gnat_exit_status : Integer;");
2266 WBI (" pragma Import (C, gnat_exit_status);");
2270 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2271 -- the main program. Otherwise, it can lead under some circumstances
2272 -- to a symbol duplication during the link (for instance when a C
2273 -- program uses two Ada libraries). Also zero terminate the string
2274 -- so that its end can be found reliably at run time.
2277 WBI (" GNAT_Version : constant String :=");
2278 WBI (" """ & Ver_Prefix &
2279 Gnat_Version_String &
2281 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2284 Set_String (" Ada_Main_Program_Name : constant String := """);
2285 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2287 if VM_Target = No_VM then
2288 Set_Main_Program_Name;
2289 Set_String (""" & ASCII.NUL;");
2291 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2294 Write_Statement_Buffer;
2297 (" pragma Export (C, Ada_Main_Program_Name, " &
2298 """__gnat_ada_main_program_name"");");
2302 WBI (" procedure " & Ada_Init_Name.all & ";");
2303 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2304 Ada_Init_Name.all & """);");
2306 -- If -a has been specified use pragma Linker_Constructor for the init
2307 -- procedure and pragma Linker_Destructor for the final procedure.
2309 if Use_Pragma_Linker_Constructor then
2310 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2313 if not Cumulative_Restrictions.Set (No_Finalization) then
2315 WBI (" procedure " & Ada_Final_Name.all & ";");
2316 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2317 Ada_Final_Name.all & """);");
2319 if Use_Pragma_Linker_Constructor then
2320 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2324 if Bind_Main_Program and then VM_Target = No_VM then
2328 if Exit_Status_Supported_On_Target then
2329 Set_String (" function ");
2331 Set_String (" procedure ");
2334 Set_String (Get_Main_Name);
2336 -- Generate argument list if present
2338 if Command_Line_Args_On_Target then
2339 Write_Statement_Buffer;
2340 WBI (" (argc : Integer;");
2341 WBI (" argv : System.Address;");
2343 (" envp : System.Address)");
2345 if Exit_Status_Supported_On_Target then
2346 Write_Statement_Buffer;
2347 WBI (" return Integer;");
2349 Write_Statement_Buffer (";");
2353 if Exit_Status_Supported_On_Target then
2354 Write_Statement_Buffer (" return Integer;");
2356 Write_Statement_Buffer (";");
2360 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2361 Get_Main_Name & """);");
2370 WBI ("end " & Ada_Main & ";");
2371 Close_Binder_Output;
2373 -- Prepare to write body
2375 Create_Binder_Output (Filename, 'b', Bfileb);
2377 -- We always compile the binder file in Ada 95 mode so that we properly
2378 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2379 -- of the Ada 2005/2012 constructs are needed by the binder file.
2381 WBI ("pragma Ada_95;");
2383 -- Output Source_File_Name pragmas which look like
2385 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2386 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2388 -- where sss/bbb are the spec/body file names respectively
2390 Get_Name_String (Bfiles);
2391 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2393 WBI ("pragma Source_File_Name (" &
2395 ", Spec_File_Name => """ &
2396 Name_Buffer (1 .. Name_Len + 3));
2398 Get_Name_String (Bfileb);
2399 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2401 WBI ("pragma Source_File_Name (" &
2403 ", Body_File_Name => """ &
2404 Name_Buffer (1 .. Name_Len + 3));
2406 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2407 -- versions of the compiler which have overflow checks on by default.
2408 -- We do not want overflow checking enabled for the increments of the
2409 -- elaboration variables (since this can cause an unwanted reference to
2410 -- the last chance exception handler for limited run-times).
2412 WBI ("pragma Suppress (Overflow_Check);");
2414 -- Generate with of System.Restrictions to initialize
2415 -- Run_Time_Restrictions.
2417 if System_Restrictions_Used
2418 and not Suppress_Standard_Library_On_Target
2421 WBI ("with System.Restrictions;");
2424 -- Generate with of Ada.Exceptions if needs library finalization
2426 if Needs_Library_Finalization then
2427 WBI ("with Ada.Exceptions;");
2430 -- Generate with of System.Elaboration_Allocators if the restriction
2431 -- No_Standard_Allocators_After_Elaboration was present.
2433 if Cumulative_Restrictions.Set
2434 (No_Standard_Allocators_After_Elaboration)
2436 WBI ("with System.Elaboration_Allocators;");
2439 -- Generate start of package body
2442 WBI ("package body " & Ada_Main & " is");
2443 WBI (" pragma Warnings (Off);");
2446 -- Generate externals for elaboration entities
2450 if not CodePeer_Mode then
2451 if not Suppress_Standard_Library_On_Target then
2453 -- Generate Priority_Specific_Dispatching pragma string
2456 (" Local_Priority_Specific_Dispatching : " &
2457 "constant String := """);
2459 for J in 0 .. PSD_Pragma_Settings.Last loop
2460 Set_Char (PSD_Pragma_Settings.Table (J));
2464 Write_Statement_Buffer;
2466 -- Generate Interrupt_State pragma string
2468 Set_String (" Local_Interrupt_States : constant String := """);
2470 for J in 0 .. IS_Pragma_Settings.Last loop
2471 Set_Char (IS_Pragma_Settings.Table (J));
2475 Write_Statement_Buffer;
2479 -- The B.1 (39) implementation advice says that the adainit/adafinal
2480 -- routines should be idempotent. Generate a flag to ensure that.
2481 -- This is not needed if we are suppressing the standard library
2482 -- since it would never be referenced.
2484 if not Suppress_Standard_Library_On_Target then
2485 WBI (" Is_Elaborated : Boolean := False;");
2491 -- Generate the adafinal routine unless there is no finalization to do
2493 if not Cumulative_Restrictions.Set (No_Finalization) then
2494 if Needs_Library_Finalization then
2495 Gen_Finalize_Library;
2503 if Bind_Main_Program and then VM_Target = No_VM then
2507 -- Output object file list and the Ada body is complete
2509 Gen_Object_Files_Options;
2512 WBI ("end " & Ada_Main & ";");
2514 Close_Binder_Output;
2515 end Gen_Output_File_Ada;
2517 ----------------------
2518 -- Gen_Restrictions --
2519 ----------------------
2521 procedure Gen_Restrictions is
2525 if Suppress_Standard_Library_On_Target
2526 or not System_Restrictions_Used
2531 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2537 for J in Cumulative_Restrictions.Set'Range loop
2538 Set_Boolean (Cumulative_Restrictions.Set (J));
2542 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2543 Write_Statement_Buffer;
2549 Set_String_Replace ("),");
2550 Write_Statement_Buffer;
2551 Set_String (" Value => (");
2553 for J in Cumulative_Restrictions.Value'Range loop
2554 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2558 Set_String_Replace ("),");
2559 Write_Statement_Buffer;
2560 WBI (" Violated =>");
2564 for J in Cumulative_Restrictions.Violated'Range loop
2565 Set_Boolean (Cumulative_Restrictions.Violated (J));
2569 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2570 Write_Statement_Buffer;
2576 Set_String_Replace ("),");
2577 Write_Statement_Buffer;
2578 Set_String (" Count => (");
2580 for J in Cumulative_Restrictions.Count'Range loop
2581 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2585 Set_String_Replace ("),");
2586 Write_Statement_Buffer;
2587 Set_String (" Unknown => (");
2589 for J in Cumulative_Restrictions.Unknown'Range loop
2590 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2594 Set_String_Replace ("))");
2596 Write_Statement_Buffer;
2597 end Gen_Restrictions;
2603 -- This routine generates lines such as:
2605 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2606 -- pragma Export (C, unnnnn, unam);
2608 -- for each unit, where unam is the unit name suffixed by either B or S for
2609 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2610 -- the version number, and nnnnn is a 5-digits serial number.
2612 procedure Gen_Versions is
2613 Ubuf : String (1 .. 6) := "u00000";
2615 procedure Increment_Ubuf;
2616 -- Little procedure to increment the serial number
2618 --------------------
2619 -- Increment_Ubuf --
2620 --------------------
2622 procedure Increment_Ubuf is
2624 for J in reverse Ubuf'Range loop
2625 Ubuf (J) := Character'Succ (Ubuf (J));
2626 exit when Ubuf (J) <= '9';
2631 -- Start of processing for Gen_Versions
2636 WBI (" type Version_32 is mod 2 ** 32;");
2637 for U in Units.First .. Units.Last loop
2638 if not Units.Table (U).SAL_Interface
2640 (not Bind_For_Library or else Units.Table (U).Directly_Scanned)
2643 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2644 Units.Table (U).Version & "#;");
2645 Set_String (" pragma Export (C, ");
2647 Set_String (", """);
2649 Get_Name_String (Units.Table (U).Uname);
2651 for K in 1 .. Name_Len loop
2652 if Name_Buffer (K) = '.' then
2656 elsif Name_Buffer (K) = '%' then
2660 Set_Char (Name_Buffer (K));
2664 if Name_Buffer (Name_Len) = 's' then
2670 Set_String (""");");
2671 Write_Statement_Buffer;
2676 ------------------------
2677 -- Get_Main_Unit_Name --
2678 ------------------------
2680 function Get_Main_Unit_Name (S : String) return String is
2681 Result : String := S;
2684 for J in S'Range loop
2685 if Result (J) = '.' then
2691 end Get_Main_Unit_Name;
2693 -----------------------
2694 -- Get_Ada_Main_Name --
2695 -----------------------
2697 function Get_Ada_Main_Name return String is
2698 Suffix : constant String := "_00";
2699 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2700 Opt.Ada_Main_Name.all & Suffix;
2704 -- The main program generated by JGNAT expects a package called
2705 -- ada_<main procedure>.
2706 if VM_Target /= No_VM then
2707 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2708 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2711 -- For CodePeer, we want reproducible names (independent of other
2712 -- mains that may or may not be present) that don't collide
2713 -- when analyzing multiple mains and which are easily recognizable
2714 -- as "ada_main" names.
2715 if CodePeer_Mode then
2716 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2717 return "ada_main_for_" &
2718 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2721 -- This loop tries the following possibilities in order
2727 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2728 -- it is set to 'ada_main'.
2730 for J in 0 .. 99 loop
2732 Nlen := Name'Length - Suffix'Length;
2734 Nlen := Name'Length;
2735 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2736 Name (Name'Last - 1) :=
2737 Character'Val (J / 10 + Character'Pos ('0'));
2740 for K in ALIs.First .. ALIs.Last loop
2741 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2743 -- Get unit name, removing %b or %e at end
2745 Get_Name_String (Units.Table (L).Uname);
2746 Name_Len := Name_Len - 2;
2748 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2754 return Name (1 .. Nlen);
2760 -- If we fall through, just use a peculiar unlikely name
2762 return ("Qwertyuiop");
2763 end Get_Ada_Main_Name;
2769 function Get_Main_Name return String is
2771 -- Explicit name given with -M switch
2773 if Bind_Alternate_Main_Name then
2774 return Alternate_Main_Name.all;
2776 -- Case of main program name to be used directly
2778 elsif Use_Ada_Main_Program_Name_On_Target then
2780 -- Get main program name
2782 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2784 -- If this is a child name, return only the name of the child, since
2785 -- we can't have dots in a nested program name. Note that we do not
2786 -- include the %b at the end of the unit name.
2788 for J in reverse 1 .. Name_Len - 2 loop
2789 if J = 1 or else Name_Buffer (J - 1) = '.' then
2790 return Name_Buffer (J .. Name_Len - 2);
2794 raise Program_Error; -- impossible exit
2796 -- Case where "main" is to be used as default
2803 ---------------------
2804 -- Get_WC_Encoding --
2805 ---------------------
2807 function Get_WC_Encoding return Character is
2809 -- If encoding method specified by -W switch, then return it
2811 if Wide_Character_Encoding_Method_Specified then
2812 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2814 -- If no main program, and not specified, set brackets, we really have
2815 -- no better choice. If some other encoding is required when there is
2816 -- no main, it must be set explicitly using -Wx.
2818 -- Note: if the ALI file always passed the wide character encoding of
2819 -- every file, then we could use the encoding of the initial specified
2820 -- file, but this information is passed only for potential main
2821 -- programs. We could fix this sometime, but it is a very minor point
2822 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
2823 -- is no main program).
2825 elsif No_Main_Subprogram then
2828 -- Otherwise if there is a main program, take encoding from it
2831 return ALIs.Table (ALIs.First).WC_Encoding;
2833 end Get_WC_Encoding;
2839 function Has_Finalizer return Boolean is
2844 for E in reverse Elab_Order.First .. Elab_Order.Last loop
2845 Unum := Elab_Order.Table (E);
2846 U := Units.Table (Unum);
2848 -- We are only interested in non-generic packages
2850 if U.Unit_Kind = 'p'
2851 and then U.Has_Finalizer
2852 and then not U.Is_Generic
2853 and then not U.No_Elab
2862 ----------------------
2863 -- Lt_Linker_Option --
2864 ----------------------
2866 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
2868 -- Sort internal files last
2870 if Linker_Options.Table (Op1).Internal_File
2872 Linker_Options.Table (Op2).Internal_File
2874 -- Note: following test uses False < True
2876 return Linker_Options.Table (Op1).Internal_File
2878 Linker_Options.Table (Op2).Internal_File;
2880 -- If both internal or both non-internal, sort according to the
2881 -- elaboration position. A unit that is elaborated later should come
2882 -- earlier in the linker options list.
2885 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2887 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2890 end Lt_Linker_Option;
2892 ------------------------
2893 -- Move_Linker_Option --
2894 ------------------------
2896 procedure Move_Linker_Option (From : Natural; To : Natural) is
2898 Linker_Options.Table (To) := Linker_Options.Table (From);
2899 end Move_Linker_Option;
2901 ----------------------------
2902 -- Resolve_Binder_Options --
2903 ----------------------------
2905 procedure Resolve_Binder_Options is
2907 procedure Check_Package (Var : in out Boolean; Name : String);
2908 -- Set Var to true iff the current identifier in Namet is Name. Do
2909 -- nothing if it doesn't match. This procedure is just an helper to
2910 -- avoid to explicitely deal with length.
2916 procedure Check_Package (Var : in out Boolean; Name : String) is
2918 if Name_Len = Name'Length
2919 and then Name_Buffer (1 .. Name_Len) = Name
2925 -- Start of processing for Check_Package
2928 for E in Elab_Order.First .. Elab_Order.Last loop
2929 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
2931 -- This is not a perfect approach, but is the current protocol
2932 -- between the run-time and the binder to indicate that tasking is
2933 -- used: System.OS_Interface should always be used by any tasking
2936 Check_Package (With_GNARL, "system.os_interface%s");
2938 -- Ditto for the use of restricted tasking
2941 (System_Tasking_Restricted_Stages_Used,
2942 "system.tasking.restricted.stages%s");
2944 -- Ditto for the use of interrupts
2946 Check_Package (System_Interrupts_Used, "system.interrupts%s");
2948 -- Ditto for the use of dispatching domains
2951 (Dispatching_Domains_Used,
2952 "system.multiprocessors.dispatching_domains%s");
2954 -- Ditto for the use of restrictions
2956 Check_Package (System_Restrictions_Used, "system.restrictions%s");
2958 end Resolve_Binder_Options;
2964 procedure Set_Boolean (B : Boolean) is
2965 True_Str : constant String := "True";
2966 False_Str : constant String := "False";
2969 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
2970 Last := Last + True_Str'Length;
2972 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
2973 Last := Last + False_Str'Length;
2981 procedure Set_Char (C : Character) is
2984 Statement_Buffer (Last) := C;
2991 procedure Set_Int (N : Int) is
3003 Statement_Buffer (Last) :=
3004 Character'Val (N mod 10 + Character'Pos ('0'));
3008 -------------------------
3009 -- Set_IS_Pragma_Table --
3010 -------------------------
3012 procedure Set_IS_Pragma_Table is
3014 for F in ALIs.First .. ALIs.Last loop
3015 for K in ALIs.Table (F).First_Interrupt_State ..
3016 ALIs.Table (F).Last_Interrupt_State
3019 Inum : constant Int :=
3020 Interrupt_States.Table (K).Interrupt_Id;
3021 Stat : constant Character :=
3022 Interrupt_States.Table (K).Interrupt_State;
3025 while IS_Pragma_Settings.Last < Inum loop
3026 IS_Pragma_Settings.Append ('n');
3029 IS_Pragma_Settings.Table (Inum) := Stat;
3033 end Set_IS_Pragma_Table;
3035 ---------------------------
3036 -- Set_Main_Program_Name --
3037 ---------------------------
3039 procedure Set_Main_Program_Name is
3041 -- Note that name has %b on the end which we ignore
3043 -- First we output the initial _ada_ since we know that the main
3044 -- program is a library level subprogram.
3046 Set_String ("_ada_");
3048 -- Copy name, changing dots to double underscores
3050 for J in 1 .. Name_Len - 2 loop
3051 if Name_Buffer (J) = '.' then
3054 Set_Char (Name_Buffer (J));
3057 end Set_Main_Program_Name;
3059 ---------------------
3060 -- Set_Name_Buffer --
3061 ---------------------
3063 procedure Set_Name_Buffer is
3065 for J in 1 .. Name_Len loop
3066 Set_Char (Name_Buffer (J));
3068 end Set_Name_Buffer;
3070 -------------------------
3071 -- Set_PSD_Pragma_Table --
3072 -------------------------
3074 procedure Set_PSD_Pragma_Table is
3076 for F in ALIs.First .. ALIs.Last loop
3077 for K in ALIs.Table (F).First_Specific_Dispatching ..
3078 ALIs.Table (F).Last_Specific_Dispatching
3081 DTK : Specific_Dispatching_Record
3082 renames Specific_Dispatching.Table (K);
3085 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3086 PSD_Pragma_Settings.Append ('F');
3089 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3090 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3095 end Set_PSD_Pragma_Table;
3101 procedure Set_String (S : String) is
3103 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3104 Last := Last + S'Length;
3107 ------------------------
3108 -- Set_String_Replace --
3109 ------------------------
3111 procedure Set_String_Replace (S : String) is
3113 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3114 end Set_String_Replace;
3120 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores) is
3122 for J in 1 .. Name_Len - 2 loop
3123 if Name_Buffer (J) = '.' then
3124 if Mode = Double_Underscores then
3126 elsif Mode = Dot then
3132 Set_Char (Name_Buffer (J));
3137 ---------------------
3138 -- Set_Unit_Number --
3139 ---------------------
3141 procedure Set_Unit_Number (U : Unit_Id) is
3142 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3143 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3146 if Num_Units >= 10 and then Unum < 10 then
3150 if Num_Units >= 100 and then Unum < 100 then
3155 end Set_Unit_Number;
3157 ----------------------------
3158 -- Write_Statement_Buffer --
3159 ----------------------------
3161 procedure Write_Statement_Buffer is
3163 WBI (Statement_Buffer (1 .. Last));
3165 end Write_Statement_Buffer;
3167 procedure Write_Statement_Buffer (S : String) is
3170 Write_Statement_Buffer;
3171 end Write_Statement_Buffer;