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 -- Heap_Size is the heap to use for memory allocations set by use of a
163 -- -Hnn parameter for the binder or by the GNAT$NO_MALLOC_64 logical.
164 -- Valid values are 32 and 64. This switch is only effective on VMS.
166 -- Float_Format is the float representation in use. Valid values are
167 -- 'I' for IEEE and 'V' for VAX Float. This is only for VMS.
169 -- WC_Encoding shows the wide character encoding method used for the main
170 -- program. This is one of the encoding letters defined in
171 -- System.WCh_Con.WC_Encoding_Letters.
173 -- Locking_Policy is a space if no locking policy was specified for the
174 -- partition. If a locking policy was specified, the value is the upper
175 -- case first character of the locking policy name, for example, 'C' for
178 -- Queuing_Policy is a space if no queuing policy was specified for the
179 -- partition. If a queuing policy was specified, the value is the upper
180 -- case first character of the queuing policy name for example, 'F' for
183 -- Task_Dispatching_Policy is a space if no task dispatching policy was
184 -- specified for the partition. If a task dispatching policy was specified,
185 -- the value is the upper case first character of the policy name, e.g. 'F'
186 -- for FIFO_Within_Priorities.
188 -- Priority_Specific_Dispatching is the address of a string used to store
189 -- the task dispatching policy specified for the different priorities in
190 -- the partition. The length of this string is determined by the last
191 -- priority for which such a pragma applies (the string will be a null
192 -- string if no specific dispatching policies were used). If pragma were
193 -- present, the entries apply to the priorities in sequence from the first
194 -- priority. The value stored is the upper case first character of the
195 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
196 -- for those priority ranges not specified.
198 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
199 -- string. It will be set to zero if no Priority_Specific_Dispatching
200 -- pragmas are present.
202 -- Restrictions is the address of a null-terminated string specifying the
203 -- restrictions information for the partition. The format is identical to
204 -- that of the parameter string found on R lines in ali files (see Lib.Writ
205 -- spec in lib-writ.ads for full details). The difference is that in this
206 -- context the values are the cumulative ones for the entire partition.
208 -- Interrupt_States is the address of a string used to specify the
209 -- cumulative results of Interrupt_State pragmas used in the partition.
210 -- The length of this string is determined by the last interrupt for which
211 -- such a pragma is given (the string will be a null string if no pragmas
212 -- were used). If pragma were present the entries apply to the interrupts
213 -- in sequence from the first interrupt, and are set to one of four
214 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
215 -- time, 's' for system, see description of Interrupt_State pragma for
218 -- Num_Interrupt_States is the length of the Interrupt_States string. It
219 -- will be set to zero if no Interrupt_State pragmas are present.
221 -- Unreserve_All_Interrupts is set to one if at least one unit in the
222 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
224 -- Exception_Tracebacks is set to one if the -E parameter was present
225 -- in the bind and to zero otherwise. Note that on some targets exception
226 -- tracebacks are provided by default, so a value of zero for this
227 -- parameter does not necessarily mean no trace backs are available.
229 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
230 -- not. A value of zero indicates that the pragma is not present, while a
231 -- value of 1 signals its presence in the partition.
233 -- Default_Stack_Size is the default stack size used when creating an Ada
234 -- task with no explicit Storage_Size clause.
236 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
237 -- disabled. A value of zero indicates that leap seconds are turned "off",
238 -- while a value of one signifies "on" status.
240 -- Main_CPU is the processor set by pragma CPU in the main program. If no
241 -- such pragma is present, the value is -1.
243 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
244 -- Convenient shorthand used throughout
246 -----------------------
247 -- Local Subprograms --
248 -----------------------
250 procedure Gen_Adainit;
251 -- Generates the Adainit procedure
253 procedure Gen_Adafinal;
254 -- Generate the Adafinal procedure
256 procedure Gen_CodePeer_Wrapper;
257 -- For CodePeer, generate wrapper which calls user-defined main subprogram
259 procedure Gen_Elab_Calls;
260 -- Generate sequence of elaboration calls
262 procedure Gen_Elab_Externals;
263 -- Generate sequence of external declarations for elaboration
265 procedure Gen_Elab_Order;
266 -- Generate comments showing elaboration order chosen
268 procedure Gen_Finalize_Library;
269 -- Generate a sequence of finalization calls to elaborated packages
272 -- Generate procedure main
274 procedure Gen_Object_Files_Options;
275 -- Output comments containing a list of the full names of the object
276 -- files to be linked and the list of linker options supplied by
277 -- Linker_Options pragmas in the source.
279 procedure Gen_Output_File_Ada (Filename : String);
280 -- Generate Ada output file
282 procedure Gen_Restrictions;
283 -- Generate initialization of restrictions variable
285 procedure Gen_Versions;
286 -- Output series of definitions for unit versions
288 function Get_Ada_Main_Name return String;
289 -- This function is used for the Ada main output to compute a usable name
290 -- for the generated main program. The normal main program name is
291 -- Ada_Main, but this won't work if the user has a unit with this name.
292 -- This function tries Ada_Main first, and if there is such a clash, then
293 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
295 function Get_Main_Unit_Name (S : String) return String;
296 -- Return the main unit name corresponding to S by replacing '.' with '_'
298 function Get_Main_Name return String;
299 -- This function is used in the main output case to compute the correct
300 -- external main program. It is "main" by default, unless the flag
301 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
302 -- of the Ada main name without the "_ada". This default can be overridden
303 -- explicitly using the -Mname binder switch.
305 function Get_WC_Encoding return Character;
306 -- Return wide character encoding method to set as WC_Encoding in output.
307 -- If -W has been used, returns the specified encoding, otherwise returns
308 -- the encoding method used for the main program source. If there is no
309 -- main program source (-z switch used), returns brackets ('b').
311 function Has_Finalizer return Boolean;
312 -- Determine whether the current unit has at least one library-level
315 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
316 -- Compare linker options, when sorting, first according to
317 -- Is_Internal_File (internal files come later) and then by
318 -- elaboration order position (latest to earliest).
320 procedure Move_Linker_Option (From : Natural; To : Natural);
321 -- Move routine for sorting linker options
323 procedure Resolve_Binder_Options;
324 -- Set the value of With_GNARL
326 procedure Set_Char (C : Character);
327 -- Set given character in Statement_Buffer at the Last + 1 position
328 -- and increment Last by one to reflect the stored character.
330 procedure Set_Int (N : Int);
331 -- Set given value in decimal in Statement_Buffer with no spaces starting
332 -- at the Last + 1 position, and updating Last past the value. A minus sign
333 -- is output for a negative value.
335 procedure Set_Boolean (B : Boolean);
336 -- Set given boolean value in Statement_Buffer at the Last + 1 position
337 -- and update Last past the value.
339 procedure Set_IS_Pragma_Table;
340 -- Initializes contents of IS_Pragma_Settings table from ALI table
342 procedure Set_Main_Program_Name;
343 -- Given the main program name in Name_Buffer (length in Name_Len) generate
344 -- the name of the routine to be used in the call. The name is generated
345 -- starting at Last + 1, and Last is updated past it.
347 procedure Set_Name_Buffer;
348 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
350 procedure Set_PSD_Pragma_Table;
351 -- Initializes contents of PSD_Pragma_Settings table from ALI table
353 procedure Set_String (S : String);
354 -- Sets characters of given string in Statement_Buffer, starting at the
355 -- Last + 1 position, and updating last past the string value.
357 procedure Set_String_Replace (S : String);
358 -- Replaces the last S'Length characters in the Statement_Buffer with the
359 -- characters of S. The caller must ensure that these characters do in fact
360 -- exist in the Statement_Buffer.
362 type Qualification_Mode is (Dollar_Sign, Dot, Double_Underscores);
364 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores);
365 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
366 -- starting at the Last + 1 position and update Last past the value.
367 -- Depending on parameter Mode, a dot (.) can be qualified into double
368 -- underscores (__), a dollar sign ($) or left as is.
370 procedure Set_Unit_Number (U : Unit_Id);
371 -- Sets unit number (first unit is 1, leading zeroes output to line up all
372 -- output unit numbers nicely as required by the value, and by the total
375 procedure Write_Statement_Buffer;
376 -- Write out contents of statement buffer up to Last, and reset Last to 0
378 procedure Write_Statement_Buffer (S : String);
379 -- First writes its argument (using Set_String (S)), then writes out the
380 -- contents of statement buffer up to Last, and reset Last to 0
386 procedure Gen_Adafinal is
388 WBI (" procedure " & Ada_Final_Name.all & " is");
391 and Bind_Main_Program
392 and not CodePeer_Mode
394 WBI (" procedure s_stalib_adafinal;");
395 Set_String (" pragma Import (C, s_stalib_adafinal, ");
396 Set_String ("""system__standard_library__adafinal"");");
397 Write_Statement_Buffer;
402 if not CodePeer_Mode then
403 WBI (" if not Is_Elaborated then");
406 WBI (" Is_Elaborated := False;");
409 -- On non-virtual machine targets, finalization is done differently
410 -- depending on whether this is the main program or a library.
412 if VM_Target = No_VM and then not CodePeer_Mode then
413 if Bind_Main_Program then
414 WBI (" s_stalib_adafinal;");
415 elsif Lib_Final_Built then
416 WBI (" finalize_library;");
421 -- Pragma Import C cannot be used on virtual machine targets, therefore
422 -- call the runtime finalization routine directly. Similarly in CodePeer
423 -- mode, where imported functions are ignored.
426 WBI (" System.Standard_Library.Adafinal;");
429 WBI (" end " & Ada_Final_Name.all & ";");
437 procedure Gen_Adainit is
438 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
439 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
442 -- Declare the access-to-subprogram type used for initialization of
443 -- of __gnat_finalize_library_objects. This is declared at library
444 -- level for compatibility with the type used in System.Soft_Links.
445 -- The import of the soft link which performs library-level object
446 -- finalization is not needed for VM targets; regular Ada is used in
447 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
448 -- tasks are non-terminating, so we do not want finalization.
450 if not Suppress_Standard_Library_On_Target
451 and then VM_Target = No_VM
452 and then not CodePeer_Mode
453 and then not Configurable_Run_Time_On_Target
455 WBI (" type No_Param_Proc is access procedure;");
459 WBI (" procedure " & Ada_Init_Name.all & " is");
461 -- In CodePeer mode, simplify adainit procedure by only calling
462 -- elaboration procedures.
464 if CodePeer_Mode then
467 -- When compiling for the AAMP small library, where the standard library
468 -- is no longer suppressed, we still want to exclude the setting of the
469 -- various imported globals, which aren't present for that library.
471 elsif AAMP_On_Target and then Configurable_Run_Time_On_Target then
475 -- If the standard library is suppressed, then the only global variables
476 -- that might be needed (by the Ravenscar profile) are the priority and
477 -- the processor for the environment task.
479 elsif Suppress_Standard_Library_On_Target then
480 if Main_Priority /= No_Main_Priority then
481 WBI (" Main_Priority : Integer;");
482 WBI (" pragma Import (C, Main_Priority," &
483 " ""__gl_main_priority"");");
487 if Main_CPU /= No_Main_CPU then
488 WBI (" Main_CPU : Integer;");
489 WBI (" pragma Import (C, Main_CPU," &
490 " ""__gl_main_cpu"");");
494 if System_Interrupts_Used
495 and then Partition_Elaboration_Policy_Specified = 'S'
497 WBI (" procedure Install_Restricted_Handlers_Sequential;");
498 WBI (" pragma Import (C," &
499 "Install_Restricted_Handlers_Sequential," &
500 " ""__gnat_attach_all_handlers"");");
504 if System_Tasking_Restricted_Stages_Used
505 and then Partition_Elaboration_Policy_Specified = 'S'
507 WBI (" Partition_Elaboration_Policy : Character;");
508 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
509 " ""__gnat_partition_elaboration_policy"");");
511 WBI (" procedure Activate_All_Tasks_Sequential;");
512 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
513 " ""__gnat_activate_all_tasks"");");
518 if Main_Priority /= No_Main_Priority then
519 Set_String (" Main_Priority := ");
520 Set_Int (Main_Priority);
522 Write_Statement_Buffer;
525 if Main_CPU /= No_Main_CPU then
526 Set_String (" Main_CPU := ");
529 Write_Statement_Buffer;
532 if System_Tasking_Restricted_Stages_Used
533 and then Partition_Elaboration_Policy_Specified = 'S'
535 Set_String (" Partition_Elaboration_Policy := '");
536 Set_Char (Partition_Elaboration_Policy_Specified);
538 Write_Statement_Buffer;
541 if Main_Priority = No_Main_Priority
542 and then Main_CPU = No_Main_CPU
543 and then not System_Tasking_Restricted_Stages_Used
548 -- Normal case (standard library not suppressed). Set all global values
549 -- used by the run time.
552 WBI (" Main_Priority : Integer;");
553 WBI (" pragma Import (C, Main_Priority, " &
554 """__gl_main_priority"");");
555 WBI (" Time_Slice_Value : Integer;");
556 WBI (" pragma Import (C, Time_Slice_Value, " &
557 """__gl_time_slice_val"");");
558 WBI (" WC_Encoding : Character;");
559 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
560 WBI (" Locking_Policy : Character;");
561 WBI (" pragma Import (C, Locking_Policy, " &
562 """__gl_locking_policy"");");
563 WBI (" Queuing_Policy : Character;");
564 WBI (" pragma Import (C, Queuing_Policy, " &
565 """__gl_queuing_policy"");");
566 WBI (" Task_Dispatching_Policy : Character;");
567 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
568 """__gl_task_dispatching_policy"");");
569 WBI (" Priority_Specific_Dispatching : System.Address;");
570 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
571 """__gl_priority_specific_dispatching"");");
572 WBI (" Num_Specific_Dispatching : Integer;");
573 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
574 """__gl_num_specific_dispatching"");");
575 WBI (" Main_CPU : Integer;");
576 WBI (" pragma Import (C, Main_CPU, " &
577 """__gl_main_cpu"");");
579 WBI (" Interrupt_States : System.Address;");
580 WBI (" pragma Import (C, Interrupt_States, " &
581 """__gl_interrupt_states"");");
582 WBI (" Num_Interrupt_States : Integer;");
583 WBI (" pragma Import (C, Num_Interrupt_States, " &
584 """__gl_num_interrupt_states"");");
585 WBI (" Unreserve_All_Interrupts : Integer;");
586 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
587 """__gl_unreserve_all_interrupts"");");
589 if Exception_Tracebacks then
590 WBI (" Exception_Tracebacks : Integer;");
591 WBI (" pragma Import (C, Exception_Tracebacks, " &
592 """__gl_exception_tracebacks"");");
595 WBI (" Detect_Blocking : Integer;");
596 WBI (" pragma Import (C, Detect_Blocking, " &
597 """__gl_detect_blocking"");");
598 WBI (" Default_Stack_Size : Integer;");
599 WBI (" pragma Import (C, Default_Stack_Size, " &
600 """__gl_default_stack_size"");");
601 WBI (" Leap_Seconds_Support : Integer;");
602 WBI (" pragma Import (C, Leap_Seconds_Support, " &
603 """__gl_leap_seconds_support"");");
605 -- Import entry point for elaboration time signal handler
606 -- installation, and indication of if it's been called previously.
609 WBI (" procedure Install_Handler;");
610 WBI (" pragma Import (C, Install_Handler, " &
611 """__gnat_install_handler"");");
613 WBI (" Handler_Installed : Integer;");
614 WBI (" pragma Import (C, Handler_Installed, " &
615 """__gnat_handler_installed"");");
617 -- Import handlers attach procedure for sequential elaboration policy
619 if System_Interrupts_Used
620 and then Partition_Elaboration_Policy_Specified = 'S'
622 WBI (" procedure Install_Restricted_Handlers_Sequential;");
623 WBI (" pragma Import (C," &
624 "Install_Restricted_Handlers_Sequential," &
625 " ""__gnat_attach_all_handlers"");");
629 -- Import task activation procedure for sequential elaboration
632 if System_Tasking_Restricted_Stages_Used
633 and then Partition_Elaboration_Policy_Specified = 'S'
635 WBI (" Partition_Elaboration_Policy : Character;");
636 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
637 " ""__gnat_partition_elaboration_policy"");");
639 WBI (" procedure Activate_All_Tasks_Sequential;");
640 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
641 " ""__gnat_activate_all_tasks"");");
644 -- The import of the soft link which performs library-level object
645 -- finalization is not needed for VM targets; regular Ada is used in
646 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
647 -- tasks are non-terminating, so we do not want finalization.
649 if VM_Target = No_VM and then not Configurable_Run_Time_On_Target then
651 WBI (" Finalize_Library_Objects : No_Param_Proc;");
652 WBI (" pragma Import (C, Finalize_Library_Objects, " &
653 """__gnat_finalize_library_objects"");");
656 -- Initialize stack limit variable of the environment task if the
657 -- stack check method is stack limit and stack check is enabled.
659 if Stack_Check_Limits_On_Target
660 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
663 WBI (" procedure Initialize_Stack_Limit;");
664 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
665 """__gnat_initialize_stack_limit"");");
668 -- Special processing when main program is CIL function/procedure
670 if VM_Target = CLI_Target
671 and then Bind_Main_Program
672 and then not No_Main_Subprogram
676 -- Function case, use Set_Exit_Status to report the returned
677 -- status code, since that is the only mechanism available.
679 if ALIs.Table (ALIs.First).Main_Program = Func then
680 WBI (" Result : Integer;");
681 WBI (" procedure Set_Exit_Status (Code : Integer);");
682 WBI (" pragma Import (C, Set_Exit_Status, " &
683 """__gnat_set_exit_status"");");
685 WBI (" function Ada_Main_Program return Integer;");
690 WBI (" procedure Ada_Main_Program;");
693 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
694 Name_Len := Name_Len - 2;
695 WBI (" pragma Import (CIL, Ada_Main_Program, """
696 & Name_Buffer (1 .. Name_Len) & "."
697 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
700 -- When dispatching domains are used then we need to signal it
701 -- before calling the main procedure.
703 if Dispatching_Domains_Used then
704 WBI (" procedure Freeze_Dispatching_Domains;");
705 WBI (" pragma Import");
706 WBI (" (Ada, Freeze_Dispatching_Domains, "
707 & """__gnat_freeze_dispatching_domains"");");
711 WBI (" if Is_Elaborated then");
714 WBI (" Is_Elaborated := True;");
716 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
717 -- restriction No_Standard_Allocators_After_Elaboration is active.
719 if Cumulative_Restrictions.Set
720 (No_Standard_Allocators_After_Elaboration)
722 WBI (" System.Elaboration_Allocators."
723 & "Mark_Start_Of_Elaboration;");
726 -- Generate assignments to initialize globals
728 Set_String (" Main_Priority := ");
729 Set_Int (Main_Priority);
731 Write_Statement_Buffer;
733 Set_String (" Time_Slice_Value := ");
735 if Task_Dispatching_Policy_Specified = 'F'
736 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
740 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
744 Write_Statement_Buffer;
746 Set_String (" WC_Encoding := '");
747 Set_Char (Get_WC_Encoding);
750 Write_Statement_Buffer;
752 Set_String (" Locking_Policy := '");
753 Set_Char (Locking_Policy_Specified);
755 Write_Statement_Buffer;
757 Set_String (" Queuing_Policy := '");
758 Set_Char (Queuing_Policy_Specified);
760 Write_Statement_Buffer;
762 Set_String (" Task_Dispatching_Policy := '");
763 Set_Char (Task_Dispatching_Policy_Specified);
765 Write_Statement_Buffer;
767 if System_Tasking_Restricted_Stages_Used
768 and then Partition_Elaboration_Policy_Specified = 'S'
770 Set_String (" Partition_Elaboration_Policy := '");
771 Set_Char (Partition_Elaboration_Policy_Specified);
773 Write_Statement_Buffer;
778 WBI (" Priority_Specific_Dispatching :=");
779 WBI (" Local_Priority_Specific_Dispatching'Address;");
781 Set_String (" Num_Specific_Dispatching := ");
782 Set_Int (PSD_Pragma_Settings.Last + 1);
784 Write_Statement_Buffer;
786 Set_String (" Main_CPU := ");
789 Write_Statement_Buffer;
791 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
793 Set_String (" Num_Interrupt_States := ");
794 Set_Int (IS_Pragma_Settings.Last + 1);
796 Write_Statement_Buffer;
798 Set_String (" Unreserve_All_Interrupts := ");
800 if Unreserve_All_Interrupts_Specified then
807 Write_Statement_Buffer;
809 if Exception_Tracebacks then
810 WBI (" Exception_Tracebacks := 1;");
813 Set_String (" Detect_Blocking := ");
815 if Detect_Blocking then
822 Write_Statement_Buffer;
824 Set_String (" Default_Stack_Size := ");
825 Set_Int (Default_Stack_Size);
827 Write_Statement_Buffer;
829 Set_String (" Leap_Seconds_Support := ");
831 if Leap_Seconds_Support then
838 Write_Statement_Buffer;
840 -- Generate call to Install_Handler
842 -- In .NET, when binding with -z, we don't install the signal handler
843 -- to let the caller handle the last exception handler.
845 if VM_Target /= CLI_Target
846 or else Bind_Main_Program
849 WBI (" if Handler_Installed = 0 then");
850 WBI (" Install_Handler;");
855 -- Generate call to set Initialize_Scalar values if active
857 if Initialize_Scalars_Used then
859 Set_String (" System.Scalar_Values.Initialize ('");
860 Set_Char (Initialize_Scalars_Mode1);
862 Set_Char (Initialize_Scalars_Mode2);
864 Write_Statement_Buffer;
867 -- Generate assignment of default secondary stack size if set
869 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
871 Set_String (" System.Secondary_Stack.");
872 Set_String ("Default_Secondary_Stack_Size := ");
873 Set_Int (Opt.Default_Sec_Stack_Size);
875 Write_Statement_Buffer;
878 -- Initialize stack limit variable of the environment task if the
879 -- stack check method is stack limit and stack check is enabled.
881 if Stack_Check_Limits_On_Target
882 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
885 WBI (" Initialize_Stack_Limit;");
888 -- On CodePeer, the finalization of library objects is not relevant
890 if CodePeer_Mode then
893 -- On virtual machine targets, or on non-virtual machine ones if this
894 -- is the main program case, attach finalize_library to the soft link.
895 -- Do it only when not using a restricted run time, in which case tasks
896 -- are non-terminating, so we do not want library-level finalization.
898 elsif (VM_Target /= No_VM or else Bind_Main_Program)
899 and then not Configurable_Run_Time_On_Target
900 and then not Suppress_Standard_Library_On_Target
904 if VM_Target = No_VM then
905 if Lib_Final_Built then
906 Set_String (" Finalize_Library_Objects := ");
907 Set_String ("finalize_library'access;");
909 Set_String (" Finalize_Library_Objects := null;");
912 -- On VM targets use regular Ada to set the soft link
915 if Lib_Final_Built then
917 (" System.Soft_Links.Finalize_Library_Objects");
918 Set_String (" := finalize_library'access;");
921 (" System.Soft_Links.Finalize_Library_Objects");
922 Set_String (" := null;");
926 Write_Statement_Buffer;
929 -- Generate elaboration calls
931 if not CodePeer_Mode then
937 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
938 -- restriction No_Standard_Allocators_After_Elaboration is active.
940 if Cumulative_Restrictions.Set
941 (No_Standard_Allocators_After_Elaboration)
943 WBI (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
946 -- From this point, no new dispatching domain can be created.
948 if Dispatching_Domains_Used then
949 WBI (" Freeze_Dispatching_Domains;");
952 -- Sequential partition elaboration policy
954 if Partition_Elaboration_Policy_Specified = 'S' then
955 if System_Interrupts_Used then
956 WBI (" Install_Restricted_Handlers_Sequential;");
959 if System_Tasking_Restricted_Stages_Used then
960 WBI (" Activate_All_Tasks_Sequential;");
964 -- Case of main program is CIL function or procedure
966 if VM_Target = CLI_Target
967 and then Bind_Main_Program
968 and then not No_Main_Subprogram
970 -- For function case, use Set_Exit_Status to set result
972 if ALIs.Table (ALIs.First).Main_Program = Func then
973 WBI (" Result := Ada_Main_Program;");
974 WBI (" Set_Exit_Status (Result);");
979 WBI (" Ada_Main_Program;");
983 WBI (" end " & Ada_Init_Name.all & ";");
987 --------------------------
988 -- Gen_CodePeer_Wrapper --
989 --------------------------
991 procedure Gen_CodePeer_Wrapper is
992 Callee_Name : constant String := "Ada_Main_Program";
995 if ALIs.Table (ALIs.First).Main_Program = Proc then
996 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
998 WBI (" " & Callee_Name & ";");
1001 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1003 WBI (" return " & Callee_Name & ";");
1006 WBI (" end " & CodePeer_Wrapper_Name & ";");
1008 end Gen_CodePeer_Wrapper;
1010 --------------------
1011 -- Gen_Elab_Calls --
1012 --------------------
1014 procedure Gen_Elab_Calls is
1015 Check_Elab_Flag : Boolean;
1018 -- Loop through elaboration order entries
1020 for E in Elab_Order.First .. Elab_Order.Last loop
1022 Unum : constant Unit_Id := Elab_Order.Table (E);
1023 U : Unit_Record renames Units.Table (Unum);
1025 Unum_Spec : Unit_Id;
1026 -- This is the unit number of the spec that corresponds to
1027 -- this entry. It is the same as Unum except when the body
1028 -- and spec are different and we are currently processing
1029 -- the body, in which case it is the spec (Unum + 1).
1032 if U.Utype = Is_Body then
1033 Unum_Spec := Unum + 1;
1038 -- Nothing to do if predefined unit in no run time mode
1040 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1043 -- Likewise if this is an interface to a stand alone library
1045 elsif U.SAL_Interface then
1048 -- Case of no elaboration code
1052 -- In CodePeer mode, we special case subprogram bodies which
1053 -- are handled in the 'else' part below, and lead to a call
1054 -- to <subp>'Elab_Subp_Body.
1056 and then (not CodePeer_Mode
1060 or else U.Utype = Is_Spec
1061 or else U.Utype = Is_Spec_Only
1062 or else U.Unit_Kind /= 's')
1064 -- In the case of a body with a separate spec, where the
1065 -- separate spec has an elaboration entity defined, this is
1066 -- where we increment the elaboration entity if one exists
1068 if U.Utype = Is_Body
1069 and then Units.Table (Unum_Spec).Set_Elab_Entity
1070 and then not CodePeer_Mode
1073 Set_Unit_Number (Unum_Spec);
1075 -- The AAMP target has no notion of shared libraries, and
1076 -- there's no possibility of reelaboration, so we treat the
1077 -- the elaboration var as a flag instead of a counter and
1080 if AAMP_On_Target then
1081 Set_String (" := 1;");
1083 -- Otherwise (normal case), increment elaboration counter
1086 Set_String (" := E");
1087 Set_Unit_Number (Unum_Spec);
1088 Set_String (" + 1;");
1091 Write_Statement_Buffer;
1093 -- In the special case where the target is AAMP and the unit is
1094 -- a spec with a body, the elaboration entity is initialized
1095 -- here. This is done because it's the only way to accomplish
1096 -- initialization of such entities, as there is no mechanism
1097 -- for load time global variable initialization on AAMP.
1099 elsif AAMP_On_Target
1100 and then U.Utype = Is_Spec
1101 and then Units.Table (Unum_Spec).Set_Elab_Entity
1104 Set_Unit_Number (Unum_Spec);
1105 Set_String (" := 0;");
1106 Write_Statement_Buffer;
1109 -- Here if elaboration code is present. If binding a library
1110 -- or if there is a non-Ada main subprogram then we generate:
1112 -- if uname_E = 0 then
1113 -- uname'elab_[spec|body];
1115 -- uname_E := uname_E + 1;
1117 -- Otherwise, elaboration routines are called unconditionally:
1119 -- uname'elab_[spec|body];
1120 -- uname_E := uname_E + 1;
1122 -- The uname_E increment is skipped if this is a separate spec,
1123 -- since it will be done when we process the body.
1125 -- In CodePeer mode, we do not generate any reference to xxx_E
1126 -- variables, only calls to 'Elab* subprograms.
1129 -- In the special case where the target is AAMP and the unit is
1130 -- a spec with a body, the elaboration entity is initialized
1131 -- here. This is done because it's the only way to accomplish
1132 -- initialization of such entities, as there is no mechanism
1133 -- for load time global variable initialization on AAMP.
1136 and then U.Utype = Is_Spec
1137 and then Units.Table (Unum_Spec).Set_Elab_Entity
1140 Set_Unit_Number (Unum_Spec);
1141 Set_String (" := 0;");
1142 Write_Statement_Buffer;
1145 -- Check incompatibilities with No_Multiple_Elaboration
1147 if not CodePeer_Mode
1148 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1150 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1152 if Force_Checking_Of_Elaboration_Flags then
1154 ("-F (force elaboration checks) switch not allowed "
1155 & "with restriction No_Multiple_Elaboration active");
1157 -- Interfacing of libraries not allowed
1159 elsif Interface_Library_Unit then
1161 ("binding of interfaced libraries not allowed "
1162 & "with restriction No_Multiple_Elaboration active");
1164 -- Non-Ada main program not allowed
1166 elsif not Bind_Main_Program then
1168 ("non-Ada main program not allowed "
1169 & "with restriction No_Multiple_Elaboration active");
1173 -- OK, see if we need to test elaboration flag
1176 Units.Table (Unum_Spec).Set_Elab_Entity
1177 and then not CodePeer_Mode
1178 and then (Force_Checking_Of_Elaboration_Flags
1179 or Interface_Library_Unit
1180 or not Bind_Main_Program);
1182 if Check_Elab_Flag then
1183 Set_String (" if E");
1184 Set_Unit_Number (Unum_Spec);
1185 Set_String (" = 0 then");
1186 Write_Statement_Buffer;
1191 Get_Decoded_Name_String_With_Brackets (U.Uname);
1193 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1194 if Name_Buffer (Name_Len) = 's' then
1195 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1198 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1202 Name_Len := Name_Len + 12;
1205 if Name_Buffer (Name_Len) = 's' then
1206 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1208 Name_Len := Name_Len + 8;
1210 -- Special case in CodePeer mode for subprogram bodies
1211 -- which correspond to CodePeer 'Elab_Subp_Body special
1214 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1215 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1217 Name_Len := Name_Len + 13;
1220 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1222 Name_Len := Name_Len + 8;
1226 Set_Casing (U.Icasing);
1229 Write_Statement_Buffer;
1231 if Check_Elab_Flag then
1235 if U.Utype /= Is_Spec
1236 and then not CodePeer_Mode
1237 and then Units.Table (Unum_Spec).Set_Elab_Entity
1240 Set_Unit_Number (Unum_Spec);
1242 -- The AAMP target has no notion of shared libraries, and
1243 -- there's no possibility of reelaboration, so we treat the
1244 -- the elaboration var as a flag instead of a counter and
1247 if AAMP_On_Target then
1248 Set_String (" := 1;");
1250 -- Otherwise (normal case), increment elaboration counter
1253 Set_String (" := E");
1254 Set_Unit_Number (Unum_Spec);
1255 Set_String (" + 1;");
1258 Write_Statement_Buffer;
1265 ------------------------
1266 -- Gen_Elab_Externals --
1267 ------------------------
1269 procedure Gen_Elab_Externals is
1271 if CodePeer_Mode then
1275 for E in Elab_Order.First .. Elab_Order.Last loop
1277 Unum : constant Unit_Id := Elab_Order.Table (E);
1278 U : Unit_Record renames Units.Table (Unum);
1281 -- Check for Elab_Entity to be set for this unit
1283 if U.Set_Elab_Entity
1285 -- Don't generate reference for stand alone library
1287 and then not U.SAL_Interface
1289 -- Don't generate reference for predefined file in No_Run_Time
1290 -- mode, since we don't include the object files in this case
1294 and then Is_Predefined_File_Name (U.Sfile))
1298 Set_Unit_Number (Unum);
1301 when No_VM | JVM_Target =>
1302 Set_String (" : Short_Integer; pragma Import (Ada, ");
1304 Set_String (" : Short_Integer; pragma Import (CIL, ");
1308 Set_Unit_Number (Unum);
1309 Set_String (", """);
1310 Get_Name_String (U.Uname);
1312 -- In the case of JGNAT we need to emit an Import name that
1313 -- includes the class name (using '$' separators in the case
1314 -- of a child unit name).
1316 if VM_Target /= No_VM then
1317 for J in 1 .. Name_Len - 2 loop
1318 if VM_Target = CLI_Target
1319 or else Name_Buffer (J) /= '.'
1321 Set_Char (Name_Buffer (J));
1327 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
1330 Set_String ("_pkg.");
1333 -- If the unit name is very long, then split the
1334 -- Import link name across lines using "&" (occurs
1335 -- in some C2 tests).
1337 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
1338 Set_String (""" &");
1339 Write_Statement_Buffer;
1345 Set_String ("_E"");");
1346 Write_Statement_Buffer;
1352 end Gen_Elab_Externals;
1354 --------------------
1355 -- Gen_Elab_Order --
1356 --------------------
1358 procedure Gen_Elab_Order is
1360 WBI (" -- BEGIN ELABORATION ORDER");
1362 for J in Elab_Order.First .. Elab_Order.Last loop
1363 Set_String (" -- ");
1364 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1366 Write_Statement_Buffer;
1369 WBI (" -- END ELABORATION ORDER");
1373 --------------------------
1374 -- Gen_Finalize_Library --
1375 --------------------------
1377 procedure Gen_Finalize_Library is
1380 Uspec : Unit_Record;
1383 procedure Gen_Header;
1384 -- Generate the header of the finalization routine
1390 procedure Gen_Header is
1392 WBI (" procedure finalize_library is");
1396 -- Start of processing for Gen_Finalize_Library
1399 if CodePeer_Mode then
1403 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1404 Unum := Elab_Order.Table (E);
1405 U := Units.Table (Unum);
1407 -- Dealing with package bodies is a little complicated. In such
1408 -- cases we must retrieve the package spec since it contains the
1409 -- spec of the body finalizer.
1411 if U.Utype = Is_Body then
1413 Uspec := Units.Table (Unum);
1418 Get_Name_String (Uspec.Uname);
1420 -- We are only interested in non-generic packages
1422 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1425 -- That aren't an interface to a stand alone library
1427 elsif U.SAL_Interface then
1430 -- Case of no finalization
1432 elsif not U.Has_Finalizer then
1434 -- The only case in which we have to do something is if this
1435 -- is a body, with a separate spec, where the separate spec
1436 -- has a finalizer. In that case, this is where we decrement
1437 -- the elaboration entity.
1439 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1440 if not Lib_Final_Built then
1442 Lib_Final_Built := True;
1446 Set_Unit_Number (Unum);
1447 Set_String (" := E");
1448 Set_Unit_Number (Unum);
1449 Set_String (" - 1;");
1450 Write_Statement_Buffer;
1454 if not Lib_Final_Built then
1456 Lib_Final_Built := True;
1461 -- procedure F<Count>;
1463 Set_String (" declare");
1464 Write_Statement_Buffer;
1466 Set_String (" procedure F");
1469 Write_Statement_Buffer;
1472 -- pragma Import (CIL, F<Count>,
1473 -- "xx.yy_pkg.xx__yy__finalize_[body|spec]");
1474 -- -- for .NET targets
1476 -- pragma Import (Java, F<Count>,
1477 -- "xx$yy.xx__yy__finalize_[body|spec]");
1478 -- -- for JVM targets
1480 -- pragma Import (Ada, F<Count>,
1481 -- "xx__yy__finalize_[body|spec]");
1482 -- -- for default targets
1484 if VM_Target = CLI_Target then
1485 Set_String (" pragma Import (CIL, F");
1486 elsif VM_Target = JVM_Target then
1487 Set_String (" pragma Import (Java, F");
1489 Set_String (" pragma Import (Ada, F");
1493 Set_String (", """);
1495 -- Perform name construction
1497 -- .NET xx.yy_pkg.xx__yy__finalize
1499 if VM_Target = CLI_Target then
1500 Set_Unit_Name (Mode => Dot);
1501 Set_String ("_pkg.");
1503 -- JVM xx$yy.xx__yy__finalize
1505 elsif VM_Target = JVM_Target then
1506 Set_Unit_Name (Mode => Dollar_Sign);
1510 -- Default xx__yy__finalize
1513 Set_String ("__finalize_");
1515 -- Package spec processing
1517 if U.Utype = Is_Spec
1518 or else U.Utype = Is_Spec_Only
1520 Set_String ("spec");
1522 -- Package body processing
1525 Set_String ("body");
1528 Set_String (""");");
1529 Write_Statement_Buffer;
1531 -- If binding a library or if there is a non-Ada main subprogram
1532 -- then we generate:
1535 -- uname_E := uname_E - 1;
1536 -- if uname_E = 0 then
1541 -- Otherwise, finalization routines are called unconditionally:
1544 -- uname_E := uname_E - 1;
1548 -- The uname_E decrement is skipped if this is a separate spec,
1549 -- since it will be done when we process the body.
1553 if U.Utype /= Is_Spec then
1555 Set_Unit_Number (Unum);
1556 Set_String (" := E");
1557 Set_Unit_Number (Unum);
1558 Set_String (" - 1;");
1559 Write_Statement_Buffer;
1562 if Interface_Library_Unit or not Bind_Main_Program then
1563 Set_String (" if E");
1564 Set_Unit_Number (Unum);
1565 Set_String (" = 0 then");
1566 Write_Statement_Buffer;
1573 Write_Statement_Buffer;
1575 if Interface_Library_Unit or not Bind_Main_Program then
1585 if Lib_Final_Built then
1587 -- It is possible that the finalization of a library-level object
1588 -- raised an exception. In that case import the actual exception
1589 -- and the routine necessary to raise it.
1591 if VM_Target = No_VM then
1593 WBI (" procedure Reraise_Library_Exception_If_Any;");
1595 Set_String (" pragma Import (Ada, ");
1596 Set_String ("Reraise_Library_Exception_If_Any, ");
1597 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1598 Write_Statement_Buffer;
1601 WBI (" Reraise_Library_Exception_If_Any;");
1604 -- VM-specific code, use regular Ada to produce the desired behavior
1607 WBI (" if System.Soft_Links.Library_Exception_Set then");
1609 Set_String (" Ada.Exceptions.Reraise_Occurrence (");
1610 Set_String ("System.Soft_Links.Library_Exception);");
1611 Write_Statement_Buffer;
1616 WBI (" end finalize_library;");
1619 end Gen_Finalize_Library;
1625 procedure Gen_Main is
1627 if not No_Main_Subprogram then
1629 -- To call the main program, we declare it using a pragma Import
1630 -- Ada with the right link name.
1632 -- It might seem more obvious to "with" the main program, and call
1633 -- it in the normal Ada manner. We do not do this for three
1636 -- 1. It is more efficient not to recompile the main program
1637 -- 2. We are not entitled to assume the source is accessible
1638 -- 3. We don't know what options to use to compile it
1640 -- It is really reason 3 that is most critical (indeed we used
1641 -- to generate the "with", but several regression tests failed).
1643 if ALIs.Table (ALIs.First).Main_Program = Func then
1644 WBI (" function Ada_Main_Program return Integer;");
1646 WBI (" procedure Ada_Main_Program;");
1649 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1650 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1651 Set_Main_Program_Name;
1652 Set_String (""");");
1654 Write_Statement_Buffer;
1657 -- For CodePeer, declare a wrapper for the user-defined main program
1659 if CodePeer_Mode then
1660 Gen_CodePeer_Wrapper;
1664 if Exit_Status_Supported_On_Target then
1665 Set_String (" function ");
1667 Set_String (" procedure ");
1670 Set_String (Get_Main_Name);
1672 if Command_Line_Args_On_Target then
1673 Write_Statement_Buffer;
1674 WBI (" (argc : Integer;");
1675 WBI (" argv : System.Address;");
1676 WBI (" envp : System.Address)");
1678 if Exit_Status_Supported_On_Target then
1679 WBI (" return Integer");
1685 if Exit_Status_Supported_On_Target then
1686 Set_String (" return Integer is");
1691 Write_Statement_Buffer;
1694 if Opt.Default_Exit_Status /= 0
1695 and then Bind_Main_Program
1696 and then not Configurable_Run_Time_Mode
1698 WBI (" procedure Set_Exit_Status (Status : Integer);");
1699 WBI (" pragma Import (C, Set_Exit_Status, " &
1700 """__gnat_set_exit_status"");");
1704 -- Initialize and Finalize
1706 if not CodePeer_Mode
1707 and then not Cumulative_Restrictions.Set (No_Finalization)
1709 WBI (" procedure Initialize (Addr : System.Address);");
1710 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1712 WBI (" procedure Finalize;");
1713 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1716 -- If we want to analyze the stack, we must import corresponding symbols
1718 if Dynamic_Stack_Measurement then
1720 WBI (" procedure Output_Results;");
1721 WBI (" pragma Import (C, Output_Results, " &
1722 """__gnat_stack_usage_output_results"");");
1726 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1727 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1728 """__gnat_stack_usage_initialize"");");
1731 -- Deal with declarations for main program case
1733 if not No_Main_Subprogram then
1734 if ALIs.Table (ALIs.First).Main_Program = Func then
1735 WBI (" Result : Integer;");
1739 if Bind_Main_Program
1740 and not Suppress_Standard_Library_On_Target
1741 and not CodePeer_Mode
1743 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1748 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1749 -- not referenced elsewhere in the generated program, but is needed
1750 -- by the debugger (that's why it is generated in the first place).
1751 -- The reference stops Ada_Main_Program_Name from being optimized
1752 -- away by smart linkers, such as the AiX linker.
1754 -- Because this variable is unused, we make this variable "aliased"
1755 -- with a pragma Volatile in order to tell the compiler to preserve
1756 -- this variable at any level of optimization.
1758 if Bind_Main_Program and not CodePeer_Mode then
1759 WBI (" Ensure_Reference : aliased System.Address := " &
1760 "Ada_Main_Program_Name'Address;");
1761 WBI (" pragma Volatile (Ensure_Reference);");
1767 -- Acquire command line arguments if present on target
1769 if CodePeer_Mode then
1772 elsif Command_Line_Args_On_Target then
1773 WBI (" gnat_argc := argc;");
1774 WBI (" gnat_argv := argv;");
1775 WBI (" gnat_envp := envp;");
1778 -- If configurable run time and no command line args, then nothing
1779 -- needs to be done since the gnat_argc/argv/envp variables are
1780 -- suppressed in this case.
1782 elsif Configurable_Run_Time_On_Target then
1785 -- Otherwise set dummy values (to be filled in by some other unit?)
1788 WBI (" gnat_argc := 0;");
1789 WBI (" gnat_argv := System.Null_Address;");
1790 WBI (" gnat_envp := System.Null_Address;");
1793 if Opt.Default_Exit_Status /= 0
1794 and then Bind_Main_Program
1795 and then not Configurable_Run_Time_Mode
1797 Set_String (" Set_Exit_Status (");
1798 Set_Int (Opt.Default_Exit_Status);
1800 Write_Statement_Buffer;
1803 if Dynamic_Stack_Measurement then
1804 Set_String (" Initialize_Stack_Analysis (");
1805 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1807 Write_Statement_Buffer;
1810 if not Cumulative_Restrictions.Set (No_Finalization)
1811 and then not CodePeer_Mode
1813 if not No_Main_Subprogram
1814 and then Bind_Main_Program
1815 and then not Suppress_Standard_Library_On_Target
1817 WBI (" Initialize (SEH'Address);");
1819 WBI (" Initialize (System.Null_Address);");
1823 WBI (" " & Ada_Init_Name.all & ";");
1825 if not No_Main_Subprogram then
1826 if CodePeer_Mode then
1827 if ALIs.Table (ALIs.First).Main_Program = Proc then
1828 WBI (" " & CodePeer_Wrapper_Name & ";");
1830 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1833 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1834 WBI (" Ada_Main_Program;");
1837 WBI (" Result := Ada_Main_Program;");
1841 -- Adafinal call is skipped if no finalization
1843 if not Cumulative_Restrictions.Set (No_Finalization) then
1847 -- Prints the result of static stack analysis
1849 if Dynamic_Stack_Measurement then
1850 WBI (" Output_Results;");
1853 -- Finalize is only called if we have a run time
1855 if not Cumulative_Restrictions.Set (No_Finalization)
1856 and then not CodePeer_Mode
1863 if Exit_Status_Supported_On_Target then
1864 if No_Main_Subprogram
1865 or else ALIs.Table (ALIs.First).Main_Program = Proc
1867 WBI (" return (gnat_exit_status);");
1869 WBI (" return (Result);");
1877 ------------------------------
1878 -- Gen_Object_Files_Options --
1879 ------------------------------
1881 procedure Gen_Object_Files_Options is
1883 -- This keeps track of the position in the sorted set of entries
1884 -- in the Linker_Options table of where the first entry from an
1885 -- internal file appears.
1887 Linker_Option_List_Started : Boolean := False;
1888 -- Set to True when "LINKER OPTION LIST" is displayed
1890 procedure Write_Linker_Option;
1891 -- Write binder info linker option
1893 -------------------------
1894 -- Write_Linker_Option --
1895 -------------------------
1897 procedure Write_Linker_Option is
1902 -- Loop through string, breaking at null's
1905 while Start < Name_Len loop
1907 -- Find null ending this section
1910 while Name_Buffer (Stop) /= ASCII.NUL
1911 and then Stop <= Name_Len loop
1915 -- Process section if non-null
1917 if Stop > Start then
1918 if Output_Linker_Option_List then
1919 if not Zero_Formatting then
1920 if not Linker_Option_List_Started then
1921 Linker_Option_List_Started := True;
1923 Write_Str (" LINKER OPTION LIST");
1931 Write_Str (Name_Buffer (Start .. Stop - 1));
1934 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
1939 end Write_Linker_Option;
1941 -- Start of processing for Gen_Object_Files_Options
1944 WBI ("-- BEGIN Object file/option list");
1946 if Object_List_Filename /= null then
1947 Set_List_File (Object_List_Filename.all);
1950 for E in Elab_Order.First .. Elab_Order.Last loop
1952 -- If not spec that has an associated body, then generate a comment
1953 -- giving the name of the corresponding object file.
1955 if not Units.Table (Elab_Order.Table (E)).SAL_Interface
1956 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1960 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1962 -- If the presence of an object file is necessary or if it exists,
1965 if not Hostparm.Exclude_Missing_Objects
1967 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1969 WBI (" -- " & Name_Buffer (1 .. Name_Len));
1971 if Output_Object_List then
1972 Write_Str (Name_Buffer (1 .. Name_Len));
1979 if Object_List_Filename /= null then
1983 -- Add a "-Ldir" for each directory in the object path
1984 if VM_Target /= CLI_Target then
1985 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1987 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1990 Add_Str_To_Name_Buffer ("-L");
1991 Add_Str_To_Name_Buffer (Dir.all);
1992 Write_Linker_Option;
1997 -- Sort linker options
1999 -- This sort accomplishes two important purposes:
2001 -- a) All application files are sorted to the front, and all GNAT
2002 -- internal files are sorted to the end. This results in a well
2003 -- defined dividing line between the two sets of files, for the
2004 -- purpose of inserting certain standard library references into
2005 -- the linker arguments list.
2007 -- b) Given two different units, we sort the linker options so that
2008 -- those from a unit earlier in the elaboration order comes later
2009 -- in the list. This is a heuristic designed to create a more
2010 -- friendly order of linker options when the operations appear in
2011 -- separate units. The idea is that if unit A must be elaborated
2012 -- before unit B, then it is more likely that B references
2013 -- libraries included by A, than vice versa, so we want libraries
2014 -- included by A to come after libraries included by B.
2016 -- These two criteria are implemented by function Lt_Linker_Option. Note
2017 -- that a special case of b) is that specs are elaborated before bodies,
2018 -- so linker options from specs come after linker options for bodies,
2019 -- and again, the assumption is that libraries used by the body are more
2020 -- likely to reference libraries used by the spec, than vice versa.
2023 (Linker_Options.Last,
2024 Move_Linker_Option'Access,
2025 Lt_Linker_Option'Access);
2027 -- Write user linker options, i.e. the set of linker options that come
2028 -- from all files other than GNAT internal files, Lgnat is left set to
2029 -- point to the first entry from a GNAT internal file, or past the end
2030 -- of the entries if there are no internal files.
2032 Lgnat := Linker_Options.Last + 1;
2034 for J in 1 .. Linker_Options.Last loop
2035 if not Linker_Options.Table (J).Internal_File then
2036 Get_Name_String (Linker_Options.Table (J).Name);
2037 Write_Linker_Option;
2044 -- Now we insert standard linker options that must appear after the
2045 -- entries from user files, and before the entries from GNAT run-time
2046 -- files. The reason for this decision is that libraries referenced
2047 -- by internal routines may reference these standard library entries.
2049 -- Note that we do not insert anything when pragma No_Run_Time has been
2050 -- specified or when the standard libraries are not to be used,
2051 -- otherwise on some platforms, such as VMS, we may get duplicate
2052 -- symbols when linking.
2054 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2057 if Opt.Shared_Libgnat then
2058 Add_Str_To_Name_Buffer ("-shared");
2060 Add_Str_To_Name_Buffer ("-static");
2063 -- Write directly to avoid -K output (why???)
2065 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2070 if Opt.Shared_Libgnat then
2071 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2073 Add_Str_To_Name_Buffer ("-lgnarl");
2076 Write_Linker_Option;
2081 if Opt.Shared_Libgnat then
2082 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2084 Add_Str_To_Name_Buffer ("-lgnat");
2087 Write_Linker_Option;
2090 -- Write linker options from all internal files
2092 for J in Lgnat .. Linker_Options.Last loop
2093 Get_Name_String (Linker_Options.Table (J).Name);
2094 Write_Linker_Option;
2097 if Output_Linker_Option_List and then not Zero_Formatting then
2101 WBI ("-- END Object file/option list ");
2102 end Gen_Object_Files_Options;
2104 ---------------------
2105 -- Gen_Output_File --
2106 ---------------------
2108 procedure Gen_Output_File (Filename : String) is
2110 -- Acquire settings for Interrupt_State pragmas
2112 Set_IS_Pragma_Table;
2114 -- Acquire settings for Priority_Specific_Dispatching pragma
2116 Set_PSD_Pragma_Table;
2118 -- For JGNAT the main program is already generated by the compiler
2120 if VM_Target = JVM_Target then
2121 Bind_Main_Program := False;
2124 -- Override time slice value if -T switch is set
2126 if Time_Slice_Set then
2127 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2130 -- Count number of elaboration calls
2132 for E in Elab_Order.First .. Elab_Order.Last loop
2133 if Units.Table (Elab_Order.Table (E)).No_Elab then
2136 Num_Elab_Calls := Num_Elab_Calls + 1;
2140 -- Generate output file in appropriate language
2142 Gen_Output_File_Ada (Filename);
2143 end Gen_Output_File;
2145 -------------------------
2146 -- Gen_Output_File_Ada --
2147 -------------------------
2149 procedure Gen_Output_File_Ada (Filename : String) is
2151 Ada_Main : constant String := Get_Ada_Main_Name;
2152 -- Name to be used for generated Ada main program. See the body of
2153 -- function Get_Ada_Main_Name for details on the form of the name.
2155 Needs_Library_Finalization : constant Boolean :=
2156 not Configurable_Run_Time_On_Target and then Has_Finalizer;
2157 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2158 -- non-terminating, so we do not want finalization.
2161 -- Name of generated bind file (spec)
2164 -- Name of generated bind file (body)
2167 -- Create spec first
2169 Create_Binder_Output (Filename, 's', Bfiles);
2171 -- We always compile the binder file in Ada 95 mode so that we properly
2172 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2173 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2175 WBI ("pragma Ada_95;");
2177 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2178 -- then we need to make sure that the binder program is compiled with
2179 -- the same restriction, so that no exception tables are generated.
2181 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2182 WBI ("pragma Restrictions (No_Exception_Handlers);");
2185 -- Same processing for Restrictions (No_Exception_Propagation)
2187 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2188 WBI ("pragma Restrictions (No_Exception_Propagation);");
2191 -- Same processing for pragma No_Run_Time
2193 if No_Run_Time_Mode then
2194 WBI ("pragma No_Run_Time;");
2197 -- Generate with of System so we can reference System.Address
2199 WBI ("with System;");
2201 -- Generate with of System.Initialize_Scalars if active
2203 if Initialize_Scalars_Used then
2204 WBI ("with System.Scalar_Values;");
2207 -- Generate with of System.Secondary_Stack if active
2209 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2210 WBI ("with System.Secondary_Stack;");
2213 Resolve_Binder_Options;
2215 -- Usually, adafinal is called using a pragma Import C. Since Import C
2216 -- doesn't have the same semantics for VMs or CodePeer use standard Ada.
2218 if not Suppress_Standard_Library_On_Target then
2219 if CodePeer_Mode then
2220 WBI ("with System.Standard_Library;");
2221 elsif VM_Target /= No_VM then
2222 WBI ("with System.Soft_Links;");
2223 WBI ("with System.Standard_Library;");
2227 WBI ("package " & Ada_Main & " is");
2228 WBI (" pragma Warnings (Off);");
2230 -- Main program case
2232 if Bind_Main_Program then
2233 if VM_Target = No_VM then
2235 -- Generate argc/argv stuff unless suppressed
2237 if Command_Line_Args_On_Target
2238 or not Configurable_Run_Time_On_Target
2241 WBI (" gnat_argc : Integer;");
2242 WBI (" gnat_argv : System.Address;");
2243 WBI (" gnat_envp : System.Address;");
2245 -- If the standard library is not suppressed, these variables
2246 -- are in the run-time data area for easy run time access.
2248 if not Suppress_Standard_Library_On_Target then
2250 WBI (" pragma Import (C, gnat_argc);");
2251 WBI (" pragma Import (C, gnat_argv);");
2252 WBI (" pragma Import (C, gnat_envp);");
2256 -- Define exit status. Again in normal mode, this is in the
2257 -- run-time library, and is initialized there, but in the
2258 -- configurable runtime case, the variable is declared and
2259 -- initialized in this file.
2263 if Configurable_Run_Time_Mode then
2264 if Exit_Status_Supported_On_Target then
2265 WBI (" gnat_exit_status : Integer := 0;");
2269 WBI (" gnat_exit_status : Integer;");
2270 WBI (" pragma Import (C, gnat_exit_status);");
2274 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2275 -- the main program. Otherwise, it can lead under some circumstances
2276 -- to a symbol duplication during the link (for instance when a C
2277 -- program uses two Ada libraries). Also zero terminate the string
2278 -- so that its end can be found reliably at run time.
2281 WBI (" GNAT_Version : constant String :=");
2282 WBI (" """ & Ver_Prefix &
2283 Gnat_Version_String &
2285 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2288 Set_String (" Ada_Main_Program_Name : constant String := """);
2289 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2291 if VM_Target = No_VM then
2292 Set_Main_Program_Name;
2293 Set_String (""" & ASCII.NUL;");
2295 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2298 Write_Statement_Buffer;
2301 (" pragma Export (C, Ada_Main_Program_Name, " &
2302 """__gnat_ada_main_program_name"");");
2306 WBI (" procedure " & Ada_Init_Name.all & ";");
2307 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2308 Ada_Init_Name.all & """);");
2310 -- If -a has been specified use pragma Linker_Constructor for the init
2311 -- procedure and pragma Linker_Destructor for the final procedure.
2313 if Use_Pragma_Linker_Constructor then
2314 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2317 if not Cumulative_Restrictions.Set (No_Finalization) then
2319 WBI (" procedure " & Ada_Final_Name.all & ";");
2320 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2321 Ada_Final_Name.all & """);");
2323 if Use_Pragma_Linker_Constructor then
2324 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2328 if Bind_Main_Program and then VM_Target = No_VM then
2332 if Exit_Status_Supported_On_Target then
2333 Set_String (" function ");
2335 Set_String (" procedure ");
2338 Set_String (Get_Main_Name);
2340 -- Generate argument list if present
2342 if Command_Line_Args_On_Target then
2343 Write_Statement_Buffer;
2344 WBI (" (argc : Integer;");
2345 WBI (" argv : System.Address;");
2347 (" envp : System.Address)");
2349 if Exit_Status_Supported_On_Target then
2350 Write_Statement_Buffer;
2351 WBI (" return Integer;");
2353 Write_Statement_Buffer (";");
2357 if Exit_Status_Supported_On_Target then
2358 Write_Statement_Buffer (" return Integer;");
2360 Write_Statement_Buffer (";");
2364 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2365 Get_Main_Name & """);");
2374 WBI ("end " & Ada_Main & ";");
2375 Close_Binder_Output;
2377 -- Prepare to write body
2379 Create_Binder_Output (Filename, 'b', Bfileb);
2381 -- We always compile the binder file in Ada 95 mode so that we properly
2382 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2383 -- of the Ada 2005/2012 constructs are needed by the binder file.
2385 WBI ("pragma Ada_95;");
2387 -- Output Source_File_Name pragmas which look like
2389 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2390 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2392 -- where sss/bbb are the spec/body file names respectively
2394 Get_Name_String (Bfiles);
2395 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2397 WBI ("pragma Source_File_Name (" &
2399 ", Spec_File_Name => """ &
2400 Name_Buffer (1 .. Name_Len + 3));
2402 Get_Name_String (Bfileb);
2403 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2405 WBI ("pragma Source_File_Name (" &
2407 ", Body_File_Name => """ &
2408 Name_Buffer (1 .. Name_Len + 3));
2410 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2411 -- versions of the compiler which have overflow checks on by default.
2412 -- We do not want overflow checking enabled for the increments of the
2413 -- elaboration variables (since this can cause an unwanted reference to
2414 -- the last chance exception handler for limited run-times).
2416 WBI ("pragma Suppress (Overflow_Check);");
2418 -- Generate with of System.Restrictions to initialize
2419 -- Run_Time_Restrictions.
2421 if System_Restrictions_Used
2422 and not Suppress_Standard_Library_On_Target
2425 WBI ("with System.Restrictions;");
2428 -- Generate with of Ada.Exceptions if needs library finalization
2430 if Needs_Library_Finalization then
2431 WBI ("with Ada.Exceptions;");
2434 -- Generate with of System.Elaboration_Allocators if the restriction
2435 -- No_Standard_Allocators_After_Elaboration was present.
2437 if Cumulative_Restrictions.Set
2438 (No_Standard_Allocators_After_Elaboration)
2440 WBI ("with System.Elaboration_Allocators;");
2443 -- Generate start of package body
2446 WBI ("package body " & Ada_Main & " is");
2447 WBI (" pragma Warnings (Off);");
2450 -- Generate externals for elaboration entities
2454 if not CodePeer_Mode then
2455 if not Suppress_Standard_Library_On_Target then
2457 -- Generate Priority_Specific_Dispatching pragma string
2460 (" Local_Priority_Specific_Dispatching : " &
2461 "constant String := """);
2463 for J in 0 .. PSD_Pragma_Settings.Last loop
2464 Set_Char (PSD_Pragma_Settings.Table (J));
2468 Write_Statement_Buffer;
2470 -- Generate Interrupt_State pragma string
2472 Set_String (" Local_Interrupt_States : constant String := """);
2474 for J in 0 .. IS_Pragma_Settings.Last loop
2475 Set_Char (IS_Pragma_Settings.Table (J));
2479 Write_Statement_Buffer;
2483 -- The B.1 (39) implementation advice says that the adainit/adafinal
2484 -- routines should be idempotent. Generate a flag to ensure that.
2485 -- This is not needed if we are suppressing the standard library
2486 -- since it would never be referenced.
2488 if not Suppress_Standard_Library_On_Target then
2489 WBI (" Is_Elaborated : Boolean := False;");
2495 -- Generate the adafinal routine unless there is no finalization to do
2497 if not Cumulative_Restrictions.Set (No_Finalization) then
2498 if Needs_Library_Finalization then
2499 Gen_Finalize_Library;
2507 if Bind_Main_Program and then VM_Target = No_VM then
2511 -- Output object file list and the Ada body is complete
2513 Gen_Object_Files_Options;
2516 WBI ("end " & Ada_Main & ";");
2518 Close_Binder_Output;
2519 end Gen_Output_File_Ada;
2521 ----------------------
2522 -- Gen_Restrictions --
2523 ----------------------
2525 procedure Gen_Restrictions is
2529 if Suppress_Standard_Library_On_Target
2530 or not System_Restrictions_Used
2535 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2541 for J in Cumulative_Restrictions.Set'Range loop
2542 Set_Boolean (Cumulative_Restrictions.Set (J));
2546 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2547 Write_Statement_Buffer;
2553 Set_String_Replace ("),");
2554 Write_Statement_Buffer;
2555 Set_String (" Value => (");
2557 for J in Cumulative_Restrictions.Value'Range loop
2558 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2562 Set_String_Replace ("),");
2563 Write_Statement_Buffer;
2564 WBI (" Violated =>");
2568 for J in Cumulative_Restrictions.Violated'Range loop
2569 Set_Boolean (Cumulative_Restrictions.Violated (J));
2573 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2574 Write_Statement_Buffer;
2580 Set_String_Replace ("),");
2581 Write_Statement_Buffer;
2582 Set_String (" Count => (");
2584 for J in Cumulative_Restrictions.Count'Range loop
2585 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2589 Set_String_Replace ("),");
2590 Write_Statement_Buffer;
2591 Set_String (" Unknown => (");
2593 for J in Cumulative_Restrictions.Unknown'Range loop
2594 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2598 Set_String_Replace ("))");
2600 Write_Statement_Buffer;
2601 end Gen_Restrictions;
2607 -- This routine generates lines such as:
2609 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2610 -- pragma Export (C, unnnnn, unam);
2612 -- for each unit, where unam is the unit name suffixed by either B or S for
2613 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2614 -- the version number, and nnnnn is a 5-digits serial number.
2616 procedure Gen_Versions is
2617 Ubuf : String (1 .. 6) := "u00000";
2619 procedure Increment_Ubuf;
2620 -- Little procedure to increment the serial number
2622 --------------------
2623 -- Increment_Ubuf --
2624 --------------------
2626 procedure Increment_Ubuf is
2628 for J in reverse Ubuf'Range loop
2629 Ubuf (J) := Character'Succ (Ubuf (J));
2630 exit when Ubuf (J) <= '9';
2635 -- Start of processing for Gen_Versions
2640 WBI (" type Version_32 is mod 2 ** 32;");
2641 for U in Units.First .. Units.Last loop
2642 if not Units.Table (U).SAL_Interface
2644 (not Bind_For_Library or else Units.Table (U).Directly_Scanned)
2647 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2648 Units.Table (U).Version & "#;");
2649 Set_String (" pragma Export (C, ");
2651 Set_String (", """);
2653 Get_Name_String (Units.Table (U).Uname);
2655 for K in 1 .. Name_Len loop
2656 if Name_Buffer (K) = '.' then
2660 elsif Name_Buffer (K) = '%' then
2664 Set_Char (Name_Buffer (K));
2668 if Name_Buffer (Name_Len) = 's' then
2674 Set_String (""");");
2675 Write_Statement_Buffer;
2680 ------------------------
2681 -- Get_Main_Unit_Name --
2682 ------------------------
2684 function Get_Main_Unit_Name (S : String) return String is
2685 Result : String := S;
2688 for J in S'Range loop
2689 if Result (J) = '.' then
2695 end Get_Main_Unit_Name;
2697 -----------------------
2698 -- Get_Ada_Main_Name --
2699 -----------------------
2701 function Get_Ada_Main_Name return String is
2702 Suffix : constant String := "_00";
2703 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2704 Opt.Ada_Main_Name.all & Suffix;
2708 -- The main program generated by JGNAT expects a package called
2709 -- ada_<main procedure>.
2710 if VM_Target /= No_VM then
2711 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2712 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2715 -- For CodePeer, we want reproducible names (independent of other
2716 -- mains that may or may not be present) that don't collide
2717 -- when analyzing multiple mains and which are easily recognizable
2718 -- as "ada_main" names.
2719 if CodePeer_Mode then
2720 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2721 return "ada_main_for_" &
2722 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2725 -- This loop tries the following possibilities in order
2731 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2732 -- it is set to 'ada_main'.
2734 for J in 0 .. 99 loop
2736 Nlen := Name'Length - Suffix'Length;
2738 Nlen := Name'Length;
2739 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2740 Name (Name'Last - 1) :=
2741 Character'Val (J / 10 + Character'Pos ('0'));
2744 for K in ALIs.First .. ALIs.Last loop
2745 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2747 -- Get unit name, removing %b or %e at end
2749 Get_Name_String (Units.Table (L).Uname);
2750 Name_Len := Name_Len - 2;
2752 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2758 return Name (1 .. Nlen);
2764 -- If we fall through, just use a peculiar unlikely name
2766 return ("Qwertyuiop");
2767 end Get_Ada_Main_Name;
2773 function Get_Main_Name return String is
2775 -- Explicit name given with -M switch
2777 if Bind_Alternate_Main_Name then
2778 return Alternate_Main_Name.all;
2780 -- Case of main program name to be used directly
2782 elsif Use_Ada_Main_Program_Name_On_Target then
2784 -- Get main program name
2786 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2788 -- If this is a child name, return only the name of the child, since
2789 -- we can't have dots in a nested program name. Note that we do not
2790 -- include the %b at the end of the unit name.
2792 for J in reverse 1 .. Name_Len - 2 loop
2793 if J = 1 or else Name_Buffer (J - 1) = '.' then
2794 return Name_Buffer (J .. Name_Len - 2);
2798 raise Program_Error; -- impossible exit
2800 -- Case where "main" is to be used as default
2807 ---------------------
2808 -- Get_WC_Encoding --
2809 ---------------------
2811 function Get_WC_Encoding return Character is
2813 -- If encoding method specified by -W switch, then return it
2815 if Wide_Character_Encoding_Method_Specified then
2816 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2818 -- If no main program, and not specified, set brackets, we really have
2819 -- no better choice. If some other encoding is required when there is
2820 -- no main, it must be set explicitly using -Wx.
2822 -- Note: if the ALI file always passed the wide character encoding of
2823 -- every file, then we could use the encoding of the initial specified
2824 -- file, but this information is passed only for potential main
2825 -- programs. We could fix this sometime, but it is a very minor point
2826 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
2827 -- is no main program).
2829 elsif No_Main_Subprogram then
2832 -- Otherwise if there is a main program, take encoding from it
2835 return ALIs.Table (ALIs.First).WC_Encoding;
2837 end Get_WC_Encoding;
2843 function Has_Finalizer return Boolean is
2848 for E in reverse Elab_Order.First .. Elab_Order.Last loop
2849 Unum := Elab_Order.Table (E);
2850 U := Units.Table (Unum);
2852 -- We are only interested in non-generic packages
2854 if U.Unit_Kind = 'p'
2855 and then U.Has_Finalizer
2856 and then not U.Is_Generic
2857 and then not U.No_Elab
2866 ----------------------
2867 -- Lt_Linker_Option --
2868 ----------------------
2870 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
2872 -- Sort internal files last
2874 if Linker_Options.Table (Op1).Internal_File
2876 Linker_Options.Table (Op2).Internal_File
2878 -- Note: following test uses False < True
2880 return Linker_Options.Table (Op1).Internal_File
2882 Linker_Options.Table (Op2).Internal_File;
2884 -- If both internal or both non-internal, sort according to the
2885 -- elaboration position. A unit that is elaborated later should come
2886 -- earlier in the linker options list.
2889 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2891 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2894 end Lt_Linker_Option;
2896 ------------------------
2897 -- Move_Linker_Option --
2898 ------------------------
2900 procedure Move_Linker_Option (From : Natural; To : Natural) is
2902 Linker_Options.Table (To) := Linker_Options.Table (From);
2903 end Move_Linker_Option;
2905 ----------------------------
2906 -- Resolve_Binder_Options --
2907 ----------------------------
2909 procedure Resolve_Binder_Options is
2911 procedure Check_Package (Var : in out Boolean; Name : String);
2912 -- Set Var to true iff the current identifier in Namet is Name. Do
2913 -- nothing if it doesn't match. This procedure is just an helper to
2914 -- avoid to explicitely deal with length.
2920 procedure Check_Package (Var : in out Boolean; Name : String) is
2922 if Name_Len = Name'Length
2923 and then Name_Buffer (1 .. Name_Len) = Name
2929 -- Start of processing for Check_Package
2932 for E in Elab_Order.First .. Elab_Order.Last loop
2933 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
2935 -- This is not a perfect approach, but is the current protocol
2936 -- between the run-time and the binder to indicate that tasking is
2937 -- used: System.OS_Interface should always be used by any tasking
2940 Check_Package (With_GNARL, "system.os_interface%s");
2942 -- Ditto for the use of restricted tasking
2945 (System_Tasking_Restricted_Stages_Used,
2946 "system.tasking.restricted.stages%s");
2948 -- Ditto for the use of interrupts
2950 Check_Package (System_Interrupts_Used, "system.interrupts%s");
2952 -- Ditto for the use of dispatching domains
2955 (Dispatching_Domains_Used,
2956 "system.multiprocessors.dispatching_domains%s");
2958 -- Ditto for the use of restrictions
2960 Check_Package (System_Restrictions_Used, "system.restrictions%s");
2962 end Resolve_Binder_Options;
2968 procedure Set_Boolean (B : Boolean) is
2969 True_Str : constant String := "True";
2970 False_Str : constant String := "False";
2973 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
2974 Last := Last + True_Str'Length;
2976 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
2977 Last := Last + False_Str'Length;
2985 procedure Set_Char (C : Character) is
2988 Statement_Buffer (Last) := C;
2995 procedure Set_Int (N : Int) is
3007 Statement_Buffer (Last) :=
3008 Character'Val (N mod 10 + Character'Pos ('0'));
3012 -------------------------
3013 -- Set_IS_Pragma_Table --
3014 -------------------------
3016 procedure Set_IS_Pragma_Table is
3018 for F in ALIs.First .. ALIs.Last loop
3019 for K in ALIs.Table (F).First_Interrupt_State ..
3020 ALIs.Table (F).Last_Interrupt_State
3023 Inum : constant Int :=
3024 Interrupt_States.Table (K).Interrupt_Id;
3025 Stat : constant Character :=
3026 Interrupt_States.Table (K).Interrupt_State;
3029 while IS_Pragma_Settings.Last < Inum loop
3030 IS_Pragma_Settings.Append ('n');
3033 IS_Pragma_Settings.Table (Inum) := Stat;
3037 end Set_IS_Pragma_Table;
3039 ---------------------------
3040 -- Set_Main_Program_Name --
3041 ---------------------------
3043 procedure Set_Main_Program_Name is
3045 -- Note that name has %b on the end which we ignore
3047 -- First we output the initial _ada_ since we know that the main
3048 -- program is a library level subprogram.
3050 Set_String ("_ada_");
3052 -- Copy name, changing dots to double underscores
3054 for J in 1 .. Name_Len - 2 loop
3055 if Name_Buffer (J) = '.' then
3058 Set_Char (Name_Buffer (J));
3061 end Set_Main_Program_Name;
3063 ---------------------
3064 -- Set_Name_Buffer --
3065 ---------------------
3067 procedure Set_Name_Buffer is
3069 for J in 1 .. Name_Len loop
3070 Set_Char (Name_Buffer (J));
3072 end Set_Name_Buffer;
3074 -------------------------
3075 -- Set_PSD_Pragma_Table --
3076 -------------------------
3078 procedure Set_PSD_Pragma_Table is
3080 for F in ALIs.First .. ALIs.Last loop
3081 for K in ALIs.Table (F).First_Specific_Dispatching ..
3082 ALIs.Table (F).Last_Specific_Dispatching
3085 DTK : Specific_Dispatching_Record
3086 renames Specific_Dispatching.Table (K);
3089 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3090 PSD_Pragma_Settings.Append ('F');
3093 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3094 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3099 end Set_PSD_Pragma_Table;
3105 procedure Set_String (S : String) is
3107 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3108 Last := Last + S'Length;
3111 ------------------------
3112 -- Set_String_Replace --
3113 ------------------------
3115 procedure Set_String_Replace (S : String) is
3117 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3118 end Set_String_Replace;
3124 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores) is
3126 for J in 1 .. Name_Len - 2 loop
3127 if Name_Buffer (J) = '.' then
3128 if Mode = Double_Underscores then
3130 elsif Mode = Dot then
3136 Set_Char (Name_Buffer (J));
3141 ---------------------
3142 -- Set_Unit_Number --
3143 ---------------------
3145 procedure Set_Unit_Number (U : Unit_Id) is
3146 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3147 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3150 if Num_Units >= 10 and then Unum < 10 then
3154 if Num_Units >= 100 and then Unum < 100 then
3159 end Set_Unit_Number;
3161 ----------------------------
3162 -- Write_Statement_Buffer --
3163 ----------------------------
3165 procedure Write_Statement_Buffer is
3167 WBI (Statement_Buffer (1 .. Last));
3169 end Write_Statement_Buffer;
3171 procedure Write_Statement_Buffer (S : String) is
3174 Write_Statement_Buffer;
3175 end Write_Statement_Buffer;