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 -- WC_Encoding shows the wide character encoding method used for the main
163 -- program. This is one of the encoding letters defined in
164 -- System.WCh_Con.WC_Encoding_Letters.
166 -- Locking_Policy is a space if no locking policy was specified for the
167 -- partition. If a locking policy was specified, the value is the upper
168 -- case first character of the locking policy name, for example, 'C' for
171 -- Queuing_Policy is a space if no queuing policy was specified for the
172 -- partition. If a queuing policy was specified, the value is the upper
173 -- case first character of the queuing policy name for example, 'F' for
176 -- Task_Dispatching_Policy is a space if no task dispatching policy was
177 -- specified for the partition. If a task dispatching policy was specified,
178 -- the value is the upper case first character of the policy name, e.g. 'F'
179 -- for FIFO_Within_Priorities.
181 -- Priority_Specific_Dispatching is the address of a string used to store
182 -- the task dispatching policy specified for the different priorities in
183 -- the partition. The length of this string is determined by the last
184 -- priority for which such a pragma applies (the string will be a null
185 -- string if no specific dispatching policies were used). If pragma were
186 -- present, the entries apply to the priorities in sequence from the first
187 -- priority. The value stored is the upper case first character of the
188 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
189 -- for those priority ranges not specified.
191 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
192 -- string. It will be set to zero if no Priority_Specific_Dispatching
193 -- pragmas are present.
195 -- Restrictions is the address of a null-terminated string specifying the
196 -- restrictions information for the partition. The format is identical to
197 -- that of the parameter string found on R lines in ali files (see Lib.Writ
198 -- spec in lib-writ.ads for full details). The difference is that in this
199 -- context the values are the cumulative ones for the entire partition.
201 -- Interrupt_States is the address of a string used to specify the
202 -- cumulative results of Interrupt_State pragmas used in the partition.
203 -- The length of this string is determined by the last interrupt for which
204 -- such a pragma is given (the string will be a null string if no pragmas
205 -- were used). If pragma were present the entries apply to the interrupts
206 -- in sequence from the first interrupt, and are set to one of four
207 -- possible settings: 'n' for not specified, 'u' for user, 'r' for run
208 -- time, 's' for system, see description of Interrupt_State pragma for
211 -- Num_Interrupt_States is the length of the Interrupt_States string. It
212 -- will be set to zero if no Interrupt_State pragmas are present.
214 -- Unreserve_All_Interrupts is set to one if at least one unit in the
215 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
217 -- Exception_Tracebacks is set to one if the -E parameter was present
218 -- in the bind and to zero otherwise. Note that on some targets exception
219 -- tracebacks are provided by default, so a value of zero for this
220 -- parameter does not necessarily mean no trace backs are available.
222 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
223 -- not. A value of zero indicates that the pragma is not present, while a
224 -- value of 1 signals its presence in the partition.
226 -- Default_Stack_Size is the default stack size used when creating an Ada
227 -- task with no explicit Storage_Size clause.
229 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
230 -- disabled. A value of zero indicates that leap seconds are turned "off",
231 -- while a value of one signifies "on" status.
233 -- Main_CPU is the processor set by pragma CPU in the main program. If no
234 -- such pragma is present, the value is -1.
236 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
237 -- Convenient shorthand used throughout
239 -----------------------
240 -- Local Subprograms --
241 -----------------------
243 procedure Gen_Adainit;
244 -- Generates the Adainit procedure
246 procedure Gen_Adafinal;
247 -- Generate the Adafinal procedure
249 procedure Gen_CodePeer_Wrapper;
250 -- For CodePeer, generate wrapper which calls user-defined main subprogram
252 procedure Gen_Elab_Calls;
253 -- Generate sequence of elaboration calls
255 procedure Gen_Elab_Externals;
256 -- Generate sequence of external declarations for elaboration
258 procedure Gen_Elab_Order;
259 -- Generate comments showing elaboration order chosen
261 procedure Gen_Finalize_Library;
262 -- Generate a sequence of finalization calls to elaborated packages
265 -- Generate procedure main
267 procedure Gen_Object_Files_Options;
268 -- Output comments containing a list of the full names of the object
269 -- files to be linked and the list of linker options supplied by
270 -- Linker_Options pragmas in the source.
272 procedure Gen_Output_File_Ada (Filename : String);
273 -- Generate Ada output file
275 procedure Gen_Restrictions;
276 -- Generate initialization of restrictions variable
278 procedure Gen_Versions;
279 -- Output series of definitions for unit versions
281 function Get_Ada_Main_Name return String;
282 -- This function is used for the Ada main output to compute a usable name
283 -- for the generated main program. The normal main program name is
284 -- Ada_Main, but this won't work if the user has a unit with this name.
285 -- This function tries Ada_Main first, and if there is such a clash, then
286 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
288 function Get_Main_Unit_Name (S : String) return String;
289 -- Return the main unit name corresponding to S by replacing '.' with '_'
291 function Get_Main_Name return String;
292 -- This function is used in the main output case to compute the correct
293 -- external main program. It is "main" by default, unless the flag
294 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
295 -- of the Ada main name without the "_ada". This default can be overridden
296 -- explicitly using the -Mname binder switch.
298 function Get_WC_Encoding return Character;
299 -- Return wide character encoding method to set as WC_Encoding in output.
300 -- If -W has been used, returns the specified encoding, otherwise returns
301 -- the encoding method used for the main program source. If there is no
302 -- main program source (-z switch used), returns brackets ('b').
304 function Has_Finalizer return Boolean;
305 -- Determine whether the current unit has at least one library-level
308 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
309 -- Compare linker options, when sorting, first according to
310 -- Is_Internal_File (internal files come later) and then by
311 -- elaboration order position (latest to earliest).
313 procedure Move_Linker_Option (From : Natural; To : Natural);
314 -- Move routine for sorting linker options
316 procedure Resolve_Binder_Options;
317 -- Set the value of With_GNARL
319 procedure Set_Char (C : Character);
320 -- Set given character in Statement_Buffer at the Last + 1 position
321 -- and increment Last by one to reflect the stored character.
323 procedure Set_Int (N : Int);
324 -- Set given value in decimal in Statement_Buffer with no spaces starting
325 -- at the Last + 1 position, and updating Last past the value. A minus sign
326 -- is output for a negative value.
328 procedure Set_Boolean (B : Boolean);
329 -- Set given boolean value in Statement_Buffer at the Last + 1 position
330 -- and update Last past the value.
332 procedure Set_IS_Pragma_Table;
333 -- Initializes contents of IS_Pragma_Settings table from ALI table
335 procedure Set_Main_Program_Name;
336 -- Given the main program name in Name_Buffer (length in Name_Len) generate
337 -- the name of the routine to be used in the call. The name is generated
338 -- starting at Last + 1, and Last is updated past it.
340 procedure Set_Name_Buffer;
341 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
343 procedure Set_PSD_Pragma_Table;
344 -- Initializes contents of PSD_Pragma_Settings table from ALI table
346 procedure Set_String (S : String);
347 -- Sets characters of given string in Statement_Buffer, starting at the
348 -- Last + 1 position, and updating last past the string value.
350 procedure Set_String_Replace (S : String);
351 -- Replaces the last S'Length characters in the Statement_Buffer with the
352 -- characters of S. The caller must ensure that these characters do in fact
353 -- exist in the Statement_Buffer.
355 type Qualification_Mode is (Dollar_Sign, Dot, Double_Underscores);
357 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores);
358 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
359 -- starting at the Last + 1 position and update Last past the value.
360 -- Depending on parameter Mode, a dot (.) can be qualified into double
361 -- underscores (__), a dollar sign ($) or left as is.
363 procedure Set_Unit_Number (U : Unit_Id);
364 -- Sets unit number (first unit is 1, leading zeroes output to line up all
365 -- output unit numbers nicely as required by the value, and by the total
368 procedure Write_Statement_Buffer;
369 -- Write out contents of statement buffer up to Last, and reset Last to 0
371 procedure Write_Statement_Buffer (S : String);
372 -- First writes its argument (using Set_String (S)), then writes out the
373 -- contents of statement buffer up to Last, and reset Last to 0
379 procedure Gen_Adafinal is
381 WBI (" procedure " & Ada_Final_Name.all & " is");
384 and Bind_Main_Program
385 and not CodePeer_Mode
387 WBI (" procedure s_stalib_adafinal;");
388 Set_String (" pragma Import (C, s_stalib_adafinal, ");
389 Set_String ("""system__standard_library__adafinal"");");
390 Write_Statement_Buffer;
394 WBI (" procedure Runtime_Finalize;");
395 WBI (" pragma Import (C, Runtime_Finalize, " &
396 """__gnat_runtime_finalize"");");
400 if not CodePeer_Mode then
401 WBI (" if not Is_Elaborated then");
404 WBI (" Is_Elaborated := False;");
407 WBI (" Runtime_Finalize;");
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 Runtime_Initialize;");
610 WBI (" pragma Import (C, Runtime_Initialize, " &
611 """__gnat_runtime_initialize"");");
613 -- Import handlers attach procedure for sequential elaboration policy
615 if System_Interrupts_Used
616 and then Partition_Elaboration_Policy_Specified = 'S'
618 WBI (" procedure Install_Restricted_Handlers_Sequential;");
619 WBI (" pragma Import (C," &
620 "Install_Restricted_Handlers_Sequential," &
621 " ""__gnat_attach_all_handlers"");");
625 -- Import task activation procedure for sequential elaboration
628 if System_Tasking_Restricted_Stages_Used
629 and then Partition_Elaboration_Policy_Specified = 'S'
631 WBI (" Partition_Elaboration_Policy : Character;");
632 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
633 " ""__gnat_partition_elaboration_policy"");");
635 WBI (" procedure Activate_All_Tasks_Sequential;");
636 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
637 " ""__gnat_activate_all_tasks"");");
640 -- The import of the soft link which performs library-level object
641 -- finalization is not needed for VM targets; regular Ada is used in
642 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
643 -- tasks are non-terminating, so we do not want finalization.
645 if VM_Target = No_VM and then not Configurable_Run_Time_On_Target then
647 WBI (" Finalize_Library_Objects : No_Param_Proc;");
648 WBI (" pragma Import (C, Finalize_Library_Objects, " &
649 """__gnat_finalize_library_objects"");");
652 -- Initialize stack limit variable of the environment task if the
653 -- stack check method is stack limit and stack check is enabled.
655 if Stack_Check_Limits_On_Target
656 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
659 WBI (" procedure Initialize_Stack_Limit;");
660 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
661 """__gnat_initialize_stack_limit"");");
664 -- Special processing when main program is CIL function/procedure
666 if VM_Target = CLI_Target
667 and then Bind_Main_Program
668 and then not No_Main_Subprogram
672 -- Function case, use Set_Exit_Status to report the returned
673 -- status code, since that is the only mechanism available.
675 if ALIs.Table (ALIs.First).Main_Program = Func then
676 WBI (" Result : Integer;");
677 WBI (" procedure Set_Exit_Status (Code : Integer);");
678 WBI (" pragma Import (C, Set_Exit_Status, " &
679 """__gnat_set_exit_status"");");
681 WBI (" function Ada_Main_Program return Integer;");
686 WBI (" procedure Ada_Main_Program;");
689 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
690 Name_Len := Name_Len - 2;
691 WBI (" pragma Import (CIL, Ada_Main_Program, """
692 & Name_Buffer (1 .. Name_Len) & "."
693 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
696 -- When dispatching domains are used then we need to signal it
697 -- before calling the main procedure.
699 if Dispatching_Domains_Used then
700 WBI (" procedure Freeze_Dispatching_Domains;");
701 WBI (" pragma Import");
702 WBI (" (Ada, Freeze_Dispatching_Domains, "
703 & """__gnat_freeze_dispatching_domains"");");
707 WBI (" if Is_Elaborated then");
710 WBI (" Is_Elaborated := True;");
712 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
713 -- restriction No_Standard_Allocators_After_Elaboration is active.
715 if Cumulative_Restrictions.Set
716 (No_Standard_Allocators_After_Elaboration)
718 WBI (" System.Elaboration_Allocators."
719 & "Mark_Start_Of_Elaboration;");
722 -- Generate assignments to initialize globals
724 Set_String (" Main_Priority := ");
725 Set_Int (Main_Priority);
727 Write_Statement_Buffer;
729 Set_String (" Time_Slice_Value := ");
731 if Task_Dispatching_Policy_Specified = 'F'
732 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
736 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
740 Write_Statement_Buffer;
742 Set_String (" WC_Encoding := '");
743 Set_Char (Get_WC_Encoding);
746 Write_Statement_Buffer;
748 Set_String (" Locking_Policy := '");
749 Set_Char (Locking_Policy_Specified);
751 Write_Statement_Buffer;
753 Set_String (" Queuing_Policy := '");
754 Set_Char (Queuing_Policy_Specified);
756 Write_Statement_Buffer;
758 Set_String (" Task_Dispatching_Policy := '");
759 Set_Char (Task_Dispatching_Policy_Specified);
761 Write_Statement_Buffer;
763 if System_Tasking_Restricted_Stages_Used
764 and then Partition_Elaboration_Policy_Specified = 'S'
766 Set_String (" Partition_Elaboration_Policy := '");
767 Set_Char (Partition_Elaboration_Policy_Specified);
769 Write_Statement_Buffer;
774 WBI (" Priority_Specific_Dispatching :=");
775 WBI (" Local_Priority_Specific_Dispatching'Address;");
777 Set_String (" Num_Specific_Dispatching := ");
778 Set_Int (PSD_Pragma_Settings.Last + 1);
780 Write_Statement_Buffer;
782 Set_String (" Main_CPU := ");
785 Write_Statement_Buffer;
787 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
789 Set_String (" Num_Interrupt_States := ");
790 Set_Int (IS_Pragma_Settings.Last + 1);
792 Write_Statement_Buffer;
794 Set_String (" Unreserve_All_Interrupts := ");
796 if Unreserve_All_Interrupts_Specified then
803 Write_Statement_Buffer;
805 if Exception_Tracebacks then
806 WBI (" Exception_Tracebacks := 1;");
809 Set_String (" Detect_Blocking := ");
811 if Detect_Blocking then
818 Write_Statement_Buffer;
820 Set_String (" Default_Stack_Size := ");
821 Set_Int (Default_Stack_Size);
823 Write_Statement_Buffer;
825 Set_String (" Leap_Seconds_Support := ");
827 if Leap_Seconds_Support then
834 Write_Statement_Buffer;
836 -- Generate call to Install_Handler
838 -- In .NET, when binding with -z, we don't install the signal handler
839 -- to let the caller handle the last exception handler.
841 if Bind_Main_Program then
843 WBI (" Runtime_Initialize;");
847 -- Generate call to set Initialize_Scalar values if active
849 if Initialize_Scalars_Used then
851 Set_String (" System.Scalar_Values.Initialize ('");
852 Set_Char (Initialize_Scalars_Mode1);
854 Set_Char (Initialize_Scalars_Mode2);
856 Write_Statement_Buffer;
859 -- Generate assignment of default secondary stack size if set
861 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
863 Set_String (" System.Secondary_Stack.");
864 Set_String ("Default_Secondary_Stack_Size := ");
865 Set_Int (Opt.Default_Sec_Stack_Size);
867 Write_Statement_Buffer;
870 -- Initialize stack limit variable of the environment task if the
871 -- stack check method is stack limit and stack check is enabled.
873 if Stack_Check_Limits_On_Target
874 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
877 WBI (" Initialize_Stack_Limit;");
880 -- On CodePeer, the finalization of library objects is not relevant
882 if CodePeer_Mode then
885 -- On virtual machine targets, or on non-virtual machine ones if this
886 -- is the main program case, attach finalize_library to the soft link.
887 -- Do it only when not using a restricted run time, in which case tasks
888 -- are non-terminating, so we do not want library-level finalization.
890 elsif (VM_Target /= No_VM or else Bind_Main_Program)
891 and then not Configurable_Run_Time_On_Target
892 and then not Suppress_Standard_Library_On_Target
896 if VM_Target = No_VM then
897 if Lib_Final_Built then
898 Set_String (" Finalize_Library_Objects := ");
899 Set_String ("finalize_library'access;");
901 Set_String (" Finalize_Library_Objects := null;");
904 -- On VM targets use regular Ada to set the soft link
907 if Lib_Final_Built then
909 (" System.Soft_Links.Finalize_Library_Objects");
910 Set_String (" := finalize_library'access;");
913 (" System.Soft_Links.Finalize_Library_Objects");
914 Set_String (" := null;");
918 Write_Statement_Buffer;
921 -- Generate elaboration calls
923 if not CodePeer_Mode then
929 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
930 -- restriction No_Standard_Allocators_After_Elaboration is active.
932 if Cumulative_Restrictions.Set
933 (No_Standard_Allocators_After_Elaboration)
935 WBI (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
938 -- From this point, no new dispatching domain can be created.
940 if Dispatching_Domains_Used then
941 WBI (" Freeze_Dispatching_Domains;");
944 -- Sequential partition elaboration policy
946 if Partition_Elaboration_Policy_Specified = 'S' then
947 if System_Interrupts_Used then
948 WBI (" Install_Restricted_Handlers_Sequential;");
951 if System_Tasking_Restricted_Stages_Used then
952 WBI (" Activate_All_Tasks_Sequential;");
956 -- Case of main program is CIL function or procedure
958 if VM_Target = CLI_Target
959 and then Bind_Main_Program
960 and then not No_Main_Subprogram
962 -- For function case, use Set_Exit_Status to set result
964 if ALIs.Table (ALIs.First).Main_Program = Func then
965 WBI (" Result := Ada_Main_Program;");
966 WBI (" Set_Exit_Status (Result);");
971 WBI (" Ada_Main_Program;");
975 WBI (" end " & Ada_Init_Name.all & ";");
979 --------------------------
980 -- Gen_CodePeer_Wrapper --
981 --------------------------
983 procedure Gen_CodePeer_Wrapper is
984 Callee_Name : constant String := "Ada_Main_Program";
987 if ALIs.Table (ALIs.First).Main_Program = Proc then
988 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
990 WBI (" " & Callee_Name & ";");
993 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
995 WBI (" return " & Callee_Name & ";");
998 WBI (" end " & CodePeer_Wrapper_Name & ";");
1000 end Gen_CodePeer_Wrapper;
1002 --------------------
1003 -- Gen_Elab_Calls --
1004 --------------------
1006 procedure Gen_Elab_Calls is
1007 Check_Elab_Flag : Boolean;
1010 -- Loop through elaboration order entries
1012 for E in Elab_Order.First .. Elab_Order.Last loop
1014 Unum : constant Unit_Id := Elab_Order.Table (E);
1015 U : Unit_Record renames Units.Table (Unum);
1017 Unum_Spec : Unit_Id;
1018 -- This is the unit number of the spec that corresponds to
1019 -- this entry. It is the same as Unum except when the body
1020 -- and spec are different and we are currently processing
1021 -- the body, in which case it is the spec (Unum + 1).
1024 if U.Utype = Is_Body then
1025 Unum_Spec := Unum + 1;
1030 -- Nothing to do if predefined unit in no run time mode
1032 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1035 -- Likewise if this is an interface to a stand alone library
1037 elsif U.SAL_Interface then
1040 -- Case of no elaboration code
1044 -- In CodePeer mode, we special case subprogram bodies which
1045 -- are handled in the 'else' part below, and lead to a call
1046 -- to <subp>'Elab_Subp_Body.
1048 and then (not CodePeer_Mode
1052 or else U.Utype = Is_Spec
1053 or else U.Utype = Is_Spec_Only
1054 or else U.Unit_Kind /= 's')
1056 -- In the case of a body with a separate spec, where the
1057 -- separate spec has an elaboration entity defined, this is
1058 -- where we increment the elaboration entity if one exists
1060 if U.Utype = Is_Body
1061 and then Units.Table (Unum_Spec).Set_Elab_Entity
1062 and then not CodePeer_Mode
1065 Set_Unit_Number (Unum_Spec);
1067 -- The AAMP target has no notion of shared libraries, and
1068 -- there's no possibility of reelaboration, so we treat the
1069 -- the elaboration var as a flag instead of a counter and
1072 if AAMP_On_Target then
1073 Set_String (" := 1;");
1075 -- Otherwise (normal case), increment elaboration counter
1078 Set_String (" := E");
1079 Set_Unit_Number (Unum_Spec);
1080 Set_String (" + 1;");
1083 Write_Statement_Buffer;
1085 -- In the special case where the target is AAMP and the unit is
1086 -- a spec with a body, the elaboration entity is initialized
1087 -- here. This is done because it's the only way to accomplish
1088 -- initialization of such entities, as there is no mechanism
1089 -- for load time global variable initialization on AAMP.
1091 elsif AAMP_On_Target
1092 and then U.Utype = Is_Spec
1093 and then Units.Table (Unum_Spec).Set_Elab_Entity
1096 Set_Unit_Number (Unum_Spec);
1097 Set_String (" := 0;");
1098 Write_Statement_Buffer;
1101 -- Here if elaboration code is present. If binding a library
1102 -- or if there is a non-Ada main subprogram then we generate:
1104 -- if uname_E = 0 then
1105 -- uname'elab_[spec|body];
1107 -- uname_E := uname_E + 1;
1109 -- Otherwise, elaboration routines are called unconditionally:
1111 -- uname'elab_[spec|body];
1112 -- uname_E := uname_E + 1;
1114 -- The uname_E increment is skipped if this is a separate spec,
1115 -- since it will be done when we process the body.
1117 -- In CodePeer mode, we do not generate any reference to xxx_E
1118 -- variables, only calls to 'Elab* subprograms.
1121 -- In the special case where the target is AAMP and the unit is
1122 -- a spec with a body, the elaboration entity is initialized
1123 -- here. This is done because it's the only way to accomplish
1124 -- initialization of such entities, as there is no mechanism
1125 -- for load time global variable initialization on AAMP.
1128 and then U.Utype = Is_Spec
1129 and then Units.Table (Unum_Spec).Set_Elab_Entity
1132 Set_Unit_Number (Unum_Spec);
1133 Set_String (" := 0;");
1134 Write_Statement_Buffer;
1137 -- Check incompatibilities with No_Multiple_Elaboration
1139 if not CodePeer_Mode
1140 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1142 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1144 if Force_Checking_Of_Elaboration_Flags then
1146 ("-F (force elaboration checks) switch not allowed "
1147 & "with restriction No_Multiple_Elaboration active");
1149 -- Interfacing of libraries not allowed
1151 elsif Interface_Library_Unit then
1153 ("binding of interfaced libraries not allowed "
1154 & "with restriction No_Multiple_Elaboration active");
1156 -- Non-Ada main program not allowed
1158 elsif not Bind_Main_Program then
1160 ("non-Ada main program not allowed "
1161 & "with restriction No_Multiple_Elaboration active");
1165 -- OK, see if we need to test elaboration flag
1168 Units.Table (Unum_Spec).Set_Elab_Entity
1169 and then not CodePeer_Mode
1170 and then (Force_Checking_Of_Elaboration_Flags
1171 or Interface_Library_Unit
1172 or not Bind_Main_Program);
1174 if Check_Elab_Flag then
1175 Set_String (" if E");
1176 Set_Unit_Number (Unum_Spec);
1177 Set_String (" = 0 then");
1178 Write_Statement_Buffer;
1183 Get_Decoded_Name_String_With_Brackets (U.Uname);
1185 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1186 if Name_Buffer (Name_Len) = 's' then
1187 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1190 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1194 Name_Len := Name_Len + 12;
1197 if Name_Buffer (Name_Len) = 's' then
1198 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1200 Name_Len := Name_Len + 8;
1202 -- Special case in CodePeer mode for subprogram bodies
1203 -- which correspond to CodePeer 'Elab_Subp_Body special
1206 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1207 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1209 Name_Len := Name_Len + 13;
1212 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1214 Name_Len := Name_Len + 8;
1218 Set_Casing (U.Icasing);
1221 Write_Statement_Buffer;
1223 if Check_Elab_Flag then
1227 if U.Utype /= Is_Spec
1228 and then not CodePeer_Mode
1229 and then Units.Table (Unum_Spec).Set_Elab_Entity
1232 Set_Unit_Number (Unum_Spec);
1234 -- The AAMP target has no notion of shared libraries, and
1235 -- there's no possibility of reelaboration, so we treat the
1236 -- the elaboration var as a flag instead of a counter and
1239 if AAMP_On_Target then
1240 Set_String (" := 1;");
1242 -- Otherwise (normal case), increment elaboration counter
1245 Set_String (" := E");
1246 Set_Unit_Number (Unum_Spec);
1247 Set_String (" + 1;");
1250 Write_Statement_Buffer;
1257 ------------------------
1258 -- Gen_Elab_Externals --
1259 ------------------------
1261 procedure Gen_Elab_Externals is
1263 if CodePeer_Mode then
1267 for E in Elab_Order.First .. Elab_Order.Last loop
1269 Unum : constant Unit_Id := Elab_Order.Table (E);
1270 U : Unit_Record renames Units.Table (Unum);
1273 -- Check for Elab_Entity to be set for this unit
1275 if U.Set_Elab_Entity
1277 -- Don't generate reference for stand alone library
1279 and then not U.SAL_Interface
1281 -- Don't generate reference for predefined file in No_Run_Time
1282 -- mode, since we don't include the object files in this case
1286 and then Is_Predefined_File_Name (U.Sfile))
1290 Set_Unit_Number (Unum);
1293 when No_VM | JVM_Target =>
1294 Set_String (" : Short_Integer; pragma Import (Ada, ");
1296 Set_String (" : Short_Integer; pragma Import (CIL, ");
1300 Set_Unit_Number (Unum);
1301 Set_String (", """);
1302 Get_Name_String (U.Uname);
1304 -- In the case of JGNAT we need to emit an Import name that
1305 -- includes the class name (using '$' separators in the case
1306 -- of a child unit name).
1308 if VM_Target /= No_VM then
1309 for J in 1 .. Name_Len - 2 loop
1310 if VM_Target = CLI_Target
1311 or else Name_Buffer (J) /= '.'
1313 Set_Char (Name_Buffer (J));
1319 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
1322 Set_String ("_pkg.");
1325 -- If the unit name is very long, then split the
1326 -- Import link name across lines using "&" (occurs
1327 -- in some C2 tests).
1329 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
1330 Set_String (""" &");
1331 Write_Statement_Buffer;
1337 Set_String ("_E"");");
1338 Write_Statement_Buffer;
1344 end Gen_Elab_Externals;
1346 --------------------
1347 -- Gen_Elab_Order --
1348 --------------------
1350 procedure Gen_Elab_Order is
1352 WBI (" -- BEGIN ELABORATION ORDER");
1354 for J in Elab_Order.First .. Elab_Order.Last loop
1355 Set_String (" -- ");
1356 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1358 Write_Statement_Buffer;
1361 WBI (" -- END ELABORATION ORDER");
1365 --------------------------
1366 -- Gen_Finalize_Library --
1367 --------------------------
1369 procedure Gen_Finalize_Library is
1372 Uspec : Unit_Record;
1375 procedure Gen_Header;
1376 -- Generate the header of the finalization routine
1382 procedure Gen_Header is
1384 WBI (" procedure finalize_library is");
1388 -- Start of processing for Gen_Finalize_Library
1391 if CodePeer_Mode then
1395 for E in reverse Elab_Order.First .. Elab_Order.Last loop
1396 Unum := Elab_Order.Table (E);
1397 U := Units.Table (Unum);
1399 -- Dealing with package bodies is a little complicated. In such
1400 -- cases we must retrieve the package spec since it contains the
1401 -- spec of the body finalizer.
1403 if U.Utype = Is_Body then
1405 Uspec := Units.Table (Unum);
1410 Get_Name_String (Uspec.Uname);
1412 -- We are only interested in non-generic packages
1414 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1417 -- That aren't an interface to a stand alone library
1419 elsif U.SAL_Interface then
1422 -- Case of no finalization
1424 elsif not U.Has_Finalizer then
1426 -- The only case in which we have to do something is if this
1427 -- is a body, with a separate spec, where the separate spec
1428 -- has a finalizer. In that case, this is where we decrement
1429 -- the elaboration entity.
1431 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1432 if not Lib_Final_Built then
1434 Lib_Final_Built := True;
1438 Set_Unit_Number (Unum);
1439 Set_String (" := E");
1440 Set_Unit_Number (Unum);
1441 Set_String (" - 1;");
1442 Write_Statement_Buffer;
1446 if not Lib_Final_Built then
1448 Lib_Final_Built := True;
1453 -- procedure F<Count>;
1455 Set_String (" declare");
1456 Write_Statement_Buffer;
1458 Set_String (" procedure F");
1461 Write_Statement_Buffer;
1464 -- pragma Import (CIL, F<Count>,
1465 -- "xx.yy_pkg.xx__yy__finalize_[body|spec]");
1466 -- -- for .NET targets
1468 -- pragma Import (Java, F<Count>,
1469 -- "xx$yy.xx__yy__finalize_[body|spec]");
1470 -- -- for JVM targets
1472 -- pragma Import (Ada, F<Count>,
1473 -- "xx__yy__finalize_[body|spec]");
1474 -- -- for default targets
1476 if VM_Target = CLI_Target then
1477 Set_String (" pragma Import (CIL, F");
1478 elsif VM_Target = JVM_Target then
1479 Set_String (" pragma Import (Java, F");
1481 Set_String (" pragma Import (Ada, F");
1485 Set_String (", """);
1487 -- Perform name construction
1489 -- .NET xx.yy_pkg.xx__yy__finalize
1491 if VM_Target = CLI_Target then
1492 Set_Unit_Name (Mode => Dot);
1493 Set_String ("_pkg.");
1495 -- JVM xx$yy.xx__yy__finalize
1497 elsif VM_Target = JVM_Target then
1498 Set_Unit_Name (Mode => Dollar_Sign);
1502 -- Default xx__yy__finalize
1505 Set_String ("__finalize_");
1507 -- Package spec processing
1509 if U.Utype = Is_Spec
1510 or else U.Utype = Is_Spec_Only
1512 Set_String ("spec");
1514 -- Package body processing
1517 Set_String ("body");
1520 Set_String (""");");
1521 Write_Statement_Buffer;
1523 -- If binding a library or if there is a non-Ada main subprogram
1524 -- then we generate:
1527 -- uname_E := uname_E - 1;
1528 -- if uname_E = 0 then
1533 -- Otherwise, finalization routines are called unconditionally:
1536 -- uname_E := uname_E - 1;
1540 -- The uname_E decrement is skipped if this is a separate spec,
1541 -- since it will be done when we process the body.
1545 if U.Utype /= Is_Spec then
1547 Set_Unit_Number (Unum);
1548 Set_String (" := E");
1549 Set_Unit_Number (Unum);
1550 Set_String (" - 1;");
1551 Write_Statement_Buffer;
1554 if Interface_Library_Unit or not Bind_Main_Program then
1555 Set_String (" if E");
1556 Set_Unit_Number (Unum);
1557 Set_String (" = 0 then");
1558 Write_Statement_Buffer;
1565 Write_Statement_Buffer;
1567 if Interface_Library_Unit or not Bind_Main_Program then
1577 if Lib_Final_Built then
1579 -- It is possible that the finalization of a library-level object
1580 -- raised an exception. In that case import the actual exception
1581 -- and the routine necessary to raise it.
1583 if VM_Target = No_VM then
1585 WBI (" procedure Reraise_Library_Exception_If_Any;");
1587 Set_String (" pragma Import (Ada, ");
1588 Set_String ("Reraise_Library_Exception_If_Any, ");
1589 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1590 Write_Statement_Buffer;
1593 WBI (" Reraise_Library_Exception_If_Any;");
1596 -- VM-specific code, use regular Ada to produce the desired behavior
1599 WBI (" if System.Soft_Links.Library_Exception_Set then");
1601 Set_String (" Ada.Exceptions.Reraise_Occurrence (");
1602 Set_String ("System.Soft_Links.Library_Exception);");
1603 Write_Statement_Buffer;
1608 WBI (" end finalize_library;");
1611 end Gen_Finalize_Library;
1617 procedure Gen_Main is
1619 if not No_Main_Subprogram then
1621 -- To call the main program, we declare it using a pragma Import
1622 -- Ada with the right link name.
1624 -- It might seem more obvious to "with" the main program, and call
1625 -- it in the normal Ada manner. We do not do this for three
1628 -- 1. It is more efficient not to recompile the main program
1629 -- 2. We are not entitled to assume the source is accessible
1630 -- 3. We don't know what options to use to compile it
1632 -- It is really reason 3 that is most critical (indeed we used
1633 -- to generate the "with", but several regression tests failed).
1635 if ALIs.Table (ALIs.First).Main_Program = Func then
1636 WBI (" function Ada_Main_Program return Integer;");
1638 WBI (" procedure Ada_Main_Program;");
1641 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1642 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1643 Set_Main_Program_Name;
1644 Set_String (""");");
1646 Write_Statement_Buffer;
1649 -- For CodePeer, declare a wrapper for the user-defined main program
1651 if CodePeer_Mode then
1652 Gen_CodePeer_Wrapper;
1656 if Exit_Status_Supported_On_Target then
1657 Set_String (" function ");
1659 Set_String (" procedure ");
1662 Set_String (Get_Main_Name);
1664 if Command_Line_Args_On_Target then
1665 Write_Statement_Buffer;
1666 WBI (" (argc : Integer;");
1667 WBI (" argv : System.Address;");
1668 WBI (" envp : System.Address)");
1670 if Exit_Status_Supported_On_Target then
1671 WBI (" return Integer");
1677 if Exit_Status_Supported_On_Target then
1678 Set_String (" return Integer is");
1683 Write_Statement_Buffer;
1686 if Opt.Default_Exit_Status /= 0
1687 and then Bind_Main_Program
1688 and then not Configurable_Run_Time_Mode
1690 WBI (" procedure Set_Exit_Status (Status : Integer);");
1691 WBI (" pragma Import (C, Set_Exit_Status, " &
1692 """__gnat_set_exit_status"");");
1696 -- Initialize and Finalize
1698 if not CodePeer_Mode
1699 and then not Cumulative_Restrictions.Set (No_Finalization)
1701 WBI (" procedure Initialize (Addr : System.Address);");
1702 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
1704 WBI (" procedure Finalize;");
1705 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
1708 -- If we want to analyze the stack, we must import corresponding symbols
1710 if Dynamic_Stack_Measurement then
1712 WBI (" procedure Output_Results;");
1713 WBI (" pragma Import (C, Output_Results, " &
1714 """__gnat_stack_usage_output_results"");");
1718 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1719 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1720 """__gnat_stack_usage_initialize"");");
1723 -- Deal with declarations for main program case
1725 if not No_Main_Subprogram then
1726 if ALIs.Table (ALIs.First).Main_Program = Func then
1727 WBI (" Result : Integer;");
1731 if Bind_Main_Program
1732 and not Suppress_Standard_Library_On_Target
1733 and not CodePeer_Mode
1735 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1740 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1741 -- not referenced elsewhere in the generated program, but is needed
1742 -- by the debugger (that's why it is generated in the first place).
1743 -- The reference stops Ada_Main_Program_Name from being optimized
1744 -- away by smart linkers, such as the AiX linker.
1746 -- Because this variable is unused, we make this variable "aliased"
1747 -- with a pragma Volatile in order to tell the compiler to preserve
1748 -- this variable at any level of optimization.
1750 if Bind_Main_Program and not CodePeer_Mode then
1751 WBI (" Ensure_Reference : aliased System.Address := " &
1752 "Ada_Main_Program_Name'Address;");
1753 WBI (" pragma Volatile (Ensure_Reference);");
1759 -- Acquire command line arguments if present on target
1761 if CodePeer_Mode then
1764 elsif Command_Line_Args_On_Target then
1765 WBI (" gnat_argc := argc;");
1766 WBI (" gnat_argv := argv;");
1767 WBI (" gnat_envp := envp;");
1770 -- If configurable run time and no command line args, then nothing
1771 -- needs to be done since the gnat_argc/argv/envp variables are
1772 -- suppressed in this case.
1774 elsif Configurable_Run_Time_On_Target then
1777 -- Otherwise set dummy values (to be filled in by some other unit?)
1780 WBI (" gnat_argc := 0;");
1781 WBI (" gnat_argv := System.Null_Address;");
1782 WBI (" gnat_envp := System.Null_Address;");
1785 if Opt.Default_Exit_Status /= 0
1786 and then Bind_Main_Program
1787 and then not Configurable_Run_Time_Mode
1789 Set_String (" Set_Exit_Status (");
1790 Set_Int (Opt.Default_Exit_Status);
1792 Write_Statement_Buffer;
1795 if Dynamic_Stack_Measurement then
1796 Set_String (" Initialize_Stack_Analysis (");
1797 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1799 Write_Statement_Buffer;
1802 if not Cumulative_Restrictions.Set (No_Finalization)
1803 and then not CodePeer_Mode
1805 if not No_Main_Subprogram
1806 and then Bind_Main_Program
1807 and then not Suppress_Standard_Library_On_Target
1809 WBI (" Initialize (SEH'Address);");
1811 WBI (" Initialize (System.Null_Address);");
1815 WBI (" " & Ada_Init_Name.all & ";");
1817 if not No_Main_Subprogram then
1818 if CodePeer_Mode then
1819 if ALIs.Table (ALIs.First).Main_Program = Proc then
1820 WBI (" " & CodePeer_Wrapper_Name & ";");
1822 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
1825 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
1826 WBI (" Ada_Main_Program;");
1829 WBI (" Result := Ada_Main_Program;");
1833 -- Adafinal call is skipped if no finalization
1835 if not Cumulative_Restrictions.Set (No_Finalization) then
1839 -- Prints the result of static stack analysis
1841 if Dynamic_Stack_Measurement then
1842 WBI (" Output_Results;");
1845 -- Finalize is only called if we have a run time
1847 if not Cumulative_Restrictions.Set (No_Finalization)
1848 and then not CodePeer_Mode
1855 if Exit_Status_Supported_On_Target then
1856 if No_Main_Subprogram
1857 or else ALIs.Table (ALIs.First).Main_Program = Proc
1859 WBI (" return (gnat_exit_status);");
1861 WBI (" return (Result);");
1869 ------------------------------
1870 -- Gen_Object_Files_Options --
1871 ------------------------------
1873 procedure Gen_Object_Files_Options is
1875 -- This keeps track of the position in the sorted set of entries
1876 -- in the Linker_Options table of where the first entry from an
1877 -- internal file appears.
1879 Linker_Option_List_Started : Boolean := False;
1880 -- Set to True when "LINKER OPTION LIST" is displayed
1882 procedure Write_Linker_Option;
1883 -- Write binder info linker option
1885 -------------------------
1886 -- Write_Linker_Option --
1887 -------------------------
1889 procedure Write_Linker_Option is
1894 -- Loop through string, breaking at null's
1897 while Start < Name_Len loop
1899 -- Find null ending this section
1902 while Name_Buffer (Stop) /= ASCII.NUL
1903 and then Stop <= Name_Len loop
1907 -- Process section if non-null
1909 if Stop > Start then
1910 if Output_Linker_Option_List then
1911 if not Zero_Formatting then
1912 if not Linker_Option_List_Started then
1913 Linker_Option_List_Started := True;
1915 Write_Str (" LINKER OPTION LIST");
1923 Write_Str (Name_Buffer (Start .. Stop - 1));
1926 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
1931 end Write_Linker_Option;
1933 -- Start of processing for Gen_Object_Files_Options
1936 WBI ("-- BEGIN Object file/option list");
1938 if Object_List_Filename /= null then
1939 Set_List_File (Object_List_Filename.all);
1942 for E in Elab_Order.First .. Elab_Order.Last loop
1944 -- If not spec that has an associated body, then generate a comment
1945 -- giving the name of the corresponding object file.
1947 if not Units.Table (Elab_Order.Table (E)).SAL_Interface
1948 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1952 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1954 -- If the presence of an object file is necessary or if it exists,
1957 if not Hostparm.Exclude_Missing_Objects
1959 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1961 WBI (" -- " & Name_Buffer (1 .. Name_Len));
1963 if Output_Object_List then
1964 Write_Str (Name_Buffer (1 .. Name_Len));
1971 if Object_List_Filename /= null then
1975 -- Add a "-Ldir" for each directory in the object path
1976 if VM_Target /= CLI_Target then
1977 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1979 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1982 Add_Str_To_Name_Buffer ("-L");
1983 Add_Str_To_Name_Buffer (Dir.all);
1984 Write_Linker_Option;
1989 -- Sort linker options
1991 -- This sort accomplishes two important purposes:
1993 -- a) All application files are sorted to the front, and all GNAT
1994 -- internal files are sorted to the end. This results in a well
1995 -- defined dividing line between the two sets of files, for the
1996 -- purpose of inserting certain standard library references into
1997 -- the linker arguments list.
1999 -- b) Given two different units, we sort the linker options so that
2000 -- those from a unit earlier in the elaboration order comes later
2001 -- in the list. This is a heuristic designed to create a more
2002 -- friendly order of linker options when the operations appear in
2003 -- separate units. The idea is that if unit A must be elaborated
2004 -- before unit B, then it is more likely that B references
2005 -- libraries included by A, than vice versa, so we want libraries
2006 -- included by A to come after libraries included by B.
2008 -- These two criteria are implemented by function Lt_Linker_Option. Note
2009 -- that a special case of b) is that specs are elaborated before bodies,
2010 -- so linker options from specs come after linker options for bodies,
2011 -- and again, the assumption is that libraries used by the body are more
2012 -- likely to reference libraries used by the spec, than vice versa.
2015 (Linker_Options.Last,
2016 Move_Linker_Option'Access,
2017 Lt_Linker_Option'Access);
2019 -- Write user linker options, i.e. the set of linker options that come
2020 -- from all files other than GNAT internal files, Lgnat is left set to
2021 -- point to the first entry from a GNAT internal file, or past the end
2022 -- of the entries if there are no internal files.
2024 Lgnat := Linker_Options.Last + 1;
2026 for J in 1 .. Linker_Options.Last loop
2027 if not Linker_Options.Table (J).Internal_File then
2028 Get_Name_String (Linker_Options.Table (J).Name);
2029 Write_Linker_Option;
2036 -- Now we insert standard linker options that must appear after the
2037 -- entries from user files, and before the entries from GNAT run-time
2038 -- files. The reason for this decision is that libraries referenced
2039 -- by internal routines may reference these standard library entries.
2041 -- Note that we do not insert anything when pragma No_Run_Time has
2042 -- been specified or when the standard libraries are not to be used,
2043 -- otherwise on some platforms, we may get duplicate symbols when
2044 -- linking (not clear if this is still the case, but it is harmless).
2046 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2049 if Opt.Shared_Libgnat then
2050 Add_Str_To_Name_Buffer ("-shared");
2052 Add_Str_To_Name_Buffer ("-static");
2055 -- Write directly to avoid -K output (why???)
2057 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2062 if Opt.Shared_Libgnat then
2063 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2065 Add_Str_To_Name_Buffer ("-lgnarl");
2068 Write_Linker_Option;
2073 if Opt.Shared_Libgnat then
2074 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2076 Add_Str_To_Name_Buffer ("-lgnat");
2079 Write_Linker_Option;
2082 -- Write linker options from all internal files
2084 for J in Lgnat .. Linker_Options.Last loop
2085 Get_Name_String (Linker_Options.Table (J).Name);
2086 Write_Linker_Option;
2089 if Output_Linker_Option_List and then not Zero_Formatting then
2093 WBI ("-- END Object file/option list ");
2094 end Gen_Object_Files_Options;
2096 ---------------------
2097 -- Gen_Output_File --
2098 ---------------------
2100 procedure Gen_Output_File (Filename : String) is
2102 -- Acquire settings for Interrupt_State pragmas
2104 Set_IS_Pragma_Table;
2106 -- Acquire settings for Priority_Specific_Dispatching pragma
2108 Set_PSD_Pragma_Table;
2110 -- For JGNAT the main program is already generated by the compiler
2112 if VM_Target = JVM_Target then
2113 Bind_Main_Program := False;
2116 -- Override time slice value if -T switch is set
2118 if Time_Slice_Set then
2119 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2122 -- Count number of elaboration calls
2124 for E in Elab_Order.First .. Elab_Order.Last loop
2125 if Units.Table (Elab_Order.Table (E)).No_Elab then
2128 Num_Elab_Calls := Num_Elab_Calls + 1;
2132 -- Generate output file in appropriate language
2134 Gen_Output_File_Ada (Filename);
2135 end Gen_Output_File;
2137 -------------------------
2138 -- Gen_Output_File_Ada --
2139 -------------------------
2141 procedure Gen_Output_File_Ada (Filename : String) is
2143 Ada_Main : constant String := Get_Ada_Main_Name;
2144 -- Name to be used for generated Ada main program. See the body of
2145 -- function Get_Ada_Main_Name for details on the form of the name.
2147 Needs_Library_Finalization : constant Boolean :=
2148 not Configurable_Run_Time_On_Target and then Has_Finalizer;
2149 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2150 -- non-terminating, so we do not want finalization.
2153 -- Name of generated bind file (spec)
2156 -- Name of generated bind file (body)
2159 -- Create spec first
2161 Create_Binder_Output (Filename, 's', Bfiles);
2163 -- We always compile the binder file in Ada 95 mode so that we properly
2164 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2165 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2167 WBI ("pragma Ada_95;");
2169 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2170 -- then we need to make sure that the binder program is compiled with
2171 -- the same restriction, so that no exception tables are generated.
2173 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2174 WBI ("pragma Restrictions (No_Exception_Handlers);");
2177 -- Same processing for Restrictions (No_Exception_Propagation)
2179 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2180 WBI ("pragma Restrictions (No_Exception_Propagation);");
2183 -- Same processing for pragma No_Run_Time
2185 if No_Run_Time_Mode then
2186 WBI ("pragma No_Run_Time;");
2189 -- Generate with of System so we can reference System.Address
2191 WBI ("with System;");
2193 -- Generate with of System.Initialize_Scalars if active
2195 if Initialize_Scalars_Used then
2196 WBI ("with System.Scalar_Values;");
2199 -- Generate with of System.Secondary_Stack if active
2201 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2202 WBI ("with System.Secondary_Stack;");
2205 Resolve_Binder_Options;
2207 -- Generate standard with's
2209 if not Suppress_Standard_Library_On_Target then
2210 if CodePeer_Mode then
2211 WBI ("with System.Standard_Library;");
2212 elsif VM_Target /= No_VM then
2213 WBI ("with System.Soft_Links;");
2214 WBI ("with System.Standard_Library;");
2218 WBI ("package " & Ada_Main & " is");
2219 WBI (" pragma Warnings (Off);");
2221 -- Main program case
2223 if Bind_Main_Program then
2224 if VM_Target = No_VM then
2226 -- Generate argc/argv stuff unless suppressed
2228 if Command_Line_Args_On_Target
2229 or not Configurable_Run_Time_On_Target
2232 WBI (" gnat_argc : Integer;");
2233 WBI (" gnat_argv : System.Address;");
2234 WBI (" gnat_envp : System.Address;");
2236 -- If the standard library is not suppressed, these variables
2237 -- are in the run-time data area for easy run time access.
2239 if not Suppress_Standard_Library_On_Target then
2241 WBI (" pragma Import (C, gnat_argc);");
2242 WBI (" pragma Import (C, gnat_argv);");
2243 WBI (" pragma Import (C, gnat_envp);");
2247 -- Define exit status. Again in normal mode, this is in the
2248 -- run-time library, and is initialized there, but in the
2249 -- configurable runtime case, the variable is declared and
2250 -- initialized in this file.
2254 if Configurable_Run_Time_Mode then
2255 if Exit_Status_Supported_On_Target then
2256 WBI (" gnat_exit_status : Integer := 0;");
2260 WBI (" gnat_exit_status : Integer;");
2261 WBI (" pragma Import (C, gnat_exit_status);");
2265 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2266 -- the main program. Otherwise, it can lead under some circumstances
2267 -- to a symbol duplication during the link (for instance when a C
2268 -- program uses two Ada libraries). Also zero terminate the string
2269 -- so that its end can be found reliably at run time.
2272 WBI (" GNAT_Version : constant String :=");
2273 WBI (" """ & Ver_Prefix &
2274 Gnat_Version_String &
2276 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2279 Set_String (" Ada_Main_Program_Name : constant String := """);
2280 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2282 if VM_Target = No_VM then
2283 Set_Main_Program_Name;
2284 Set_String (""" & ASCII.NUL;");
2286 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2289 Write_Statement_Buffer;
2292 (" pragma Export (C, Ada_Main_Program_Name, " &
2293 """__gnat_ada_main_program_name"");");
2297 WBI (" procedure " & Ada_Init_Name.all & ";");
2298 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2299 Ada_Init_Name.all & """);");
2301 -- If -a has been specified use pragma Linker_Constructor for the init
2302 -- procedure and pragma Linker_Destructor for the final procedure.
2304 if Use_Pragma_Linker_Constructor then
2305 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2308 if not Cumulative_Restrictions.Set (No_Finalization) then
2310 WBI (" procedure " & Ada_Final_Name.all & ";");
2311 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2312 Ada_Final_Name.all & """);");
2314 if Use_Pragma_Linker_Constructor then
2315 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2319 if Bind_Main_Program and then VM_Target = No_VM then
2323 if Exit_Status_Supported_On_Target then
2324 Set_String (" function ");
2326 Set_String (" procedure ");
2329 Set_String (Get_Main_Name);
2331 -- Generate argument list if present
2333 if Command_Line_Args_On_Target then
2334 Write_Statement_Buffer;
2335 WBI (" (argc : Integer;");
2336 WBI (" argv : System.Address;");
2338 (" envp : System.Address)");
2340 if Exit_Status_Supported_On_Target then
2341 Write_Statement_Buffer;
2342 WBI (" return Integer;");
2344 Write_Statement_Buffer (";");
2348 if Exit_Status_Supported_On_Target then
2349 Write_Statement_Buffer (" return Integer;");
2351 Write_Statement_Buffer (";");
2355 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2356 Get_Main_Name & """);");
2365 WBI ("end " & Ada_Main & ";");
2366 Close_Binder_Output;
2368 -- Prepare to write body
2370 Create_Binder_Output (Filename, 'b', Bfileb);
2372 -- We always compile the binder file in Ada 95 mode so that we properly
2373 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2374 -- of the Ada 2005/2012 constructs are needed by the binder file.
2376 WBI ("pragma Ada_95;");
2378 -- Output Source_File_Name pragmas which look like
2380 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2381 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2383 -- where sss/bbb are the spec/body file names respectively
2385 Get_Name_String (Bfiles);
2386 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2388 WBI ("pragma Source_File_Name (" &
2390 ", Spec_File_Name => """ &
2391 Name_Buffer (1 .. Name_Len + 3));
2393 Get_Name_String (Bfileb);
2394 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2396 WBI ("pragma Source_File_Name (" &
2398 ", Body_File_Name => """ &
2399 Name_Buffer (1 .. Name_Len + 3));
2401 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2402 -- versions of the compiler which have overflow checks on by default.
2403 -- We do not want overflow checking enabled for the increments of the
2404 -- elaboration variables (since this can cause an unwanted reference to
2405 -- the last chance exception handler for limited run-times).
2407 WBI ("pragma Suppress (Overflow_Check);");
2409 -- Generate with of System.Restrictions to initialize
2410 -- Run_Time_Restrictions.
2412 if System_Restrictions_Used
2413 and not Suppress_Standard_Library_On_Target
2416 WBI ("with System.Restrictions;");
2419 -- Generate with of Ada.Exceptions if needs library finalization
2421 if Needs_Library_Finalization then
2422 WBI ("with Ada.Exceptions;");
2425 -- Generate with of System.Elaboration_Allocators if the restriction
2426 -- No_Standard_Allocators_After_Elaboration was present.
2428 if Cumulative_Restrictions.Set
2429 (No_Standard_Allocators_After_Elaboration)
2431 WBI ("with System.Elaboration_Allocators;");
2434 -- Generate start of package body
2437 WBI ("package body " & Ada_Main & " is");
2438 WBI (" pragma Warnings (Off);");
2441 -- Generate externals for elaboration entities
2445 if not CodePeer_Mode then
2446 if not Suppress_Standard_Library_On_Target then
2448 -- Generate Priority_Specific_Dispatching pragma string
2451 (" Local_Priority_Specific_Dispatching : " &
2452 "constant String := """);
2454 for J in 0 .. PSD_Pragma_Settings.Last loop
2455 Set_Char (PSD_Pragma_Settings.Table (J));
2459 Write_Statement_Buffer;
2461 -- Generate Interrupt_State pragma string
2463 Set_String (" Local_Interrupt_States : constant String := """);
2465 for J in 0 .. IS_Pragma_Settings.Last loop
2466 Set_Char (IS_Pragma_Settings.Table (J));
2470 Write_Statement_Buffer;
2474 -- The B.1 (39) implementation advice says that the adainit/adafinal
2475 -- routines should be idempotent. Generate a flag to ensure that.
2476 -- This is not needed if we are suppressing the standard library
2477 -- since it would never be referenced.
2479 if not Suppress_Standard_Library_On_Target then
2480 WBI (" Is_Elaborated : Boolean := False;");
2486 -- Generate the adafinal routine unless there is no finalization to do
2488 if not Cumulative_Restrictions.Set (No_Finalization) then
2489 if Needs_Library_Finalization then
2490 Gen_Finalize_Library;
2498 if Bind_Main_Program and then VM_Target = No_VM then
2502 -- Output object file list and the Ada body is complete
2504 Gen_Object_Files_Options;
2507 WBI ("end " & Ada_Main & ";");
2509 Close_Binder_Output;
2510 end Gen_Output_File_Ada;
2512 ----------------------
2513 -- Gen_Restrictions --
2514 ----------------------
2516 procedure Gen_Restrictions is
2520 if Suppress_Standard_Library_On_Target
2521 or not System_Restrictions_Used
2526 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2532 for J in Cumulative_Restrictions.Set'Range loop
2533 Set_Boolean (Cumulative_Restrictions.Set (J));
2537 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2538 Write_Statement_Buffer;
2544 Set_String_Replace ("),");
2545 Write_Statement_Buffer;
2546 Set_String (" Value => (");
2548 for J in Cumulative_Restrictions.Value'Range loop
2549 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2553 Set_String_Replace ("),");
2554 Write_Statement_Buffer;
2555 WBI (" Violated =>");
2559 for J in Cumulative_Restrictions.Violated'Range loop
2560 Set_Boolean (Cumulative_Restrictions.Violated (J));
2564 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2565 Write_Statement_Buffer;
2571 Set_String_Replace ("),");
2572 Write_Statement_Buffer;
2573 Set_String (" Count => (");
2575 for J in Cumulative_Restrictions.Count'Range loop
2576 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2580 Set_String_Replace ("),");
2581 Write_Statement_Buffer;
2582 Set_String (" Unknown => (");
2584 for J in Cumulative_Restrictions.Unknown'Range loop
2585 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2589 Set_String_Replace ("))");
2591 Write_Statement_Buffer;
2592 end Gen_Restrictions;
2598 -- This routine generates lines such as:
2600 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2601 -- pragma Export (C, unnnnn, unam);
2603 -- for each unit, where unam is the unit name suffixed by either B or S for
2604 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
2605 -- the version number, and nnnnn is a 5-digits serial number.
2607 procedure Gen_Versions is
2608 Ubuf : String (1 .. 6) := "u00000";
2610 procedure Increment_Ubuf;
2611 -- Little procedure to increment the serial number
2613 --------------------
2614 -- Increment_Ubuf --
2615 --------------------
2617 procedure Increment_Ubuf is
2619 for J in reverse Ubuf'Range loop
2620 Ubuf (J) := Character'Succ (Ubuf (J));
2621 exit when Ubuf (J) <= '9';
2626 -- Start of processing for Gen_Versions
2631 WBI (" type Version_32 is mod 2 ** 32;");
2632 for U in Units.First .. Units.Last loop
2633 if not Units.Table (U).SAL_Interface
2635 (not Bind_For_Library or else Units.Table (U).Directly_Scanned)
2638 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2639 Units.Table (U).Version & "#;");
2640 Set_String (" pragma Export (C, ");
2642 Set_String (", """);
2644 Get_Name_String (Units.Table (U).Uname);
2646 for K in 1 .. Name_Len loop
2647 if Name_Buffer (K) = '.' then
2651 elsif Name_Buffer (K) = '%' then
2655 Set_Char (Name_Buffer (K));
2659 if Name_Buffer (Name_Len) = 's' then
2665 Set_String (""");");
2666 Write_Statement_Buffer;
2671 ------------------------
2672 -- Get_Main_Unit_Name --
2673 ------------------------
2675 function Get_Main_Unit_Name (S : String) return String is
2676 Result : String := S;
2679 for J in S'Range loop
2680 if Result (J) = '.' then
2686 end Get_Main_Unit_Name;
2688 -----------------------
2689 -- Get_Ada_Main_Name --
2690 -----------------------
2692 function Get_Ada_Main_Name return String is
2693 Suffix : constant String := "_00";
2694 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2695 Opt.Ada_Main_Name.all & Suffix;
2699 -- The main program generated by JGNAT expects a package called
2700 -- ada_<main procedure>.
2701 if VM_Target /= No_VM then
2702 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2703 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2706 -- For CodePeer, we want reproducible names (independent of other
2707 -- mains that may or may not be present) that don't collide
2708 -- when analyzing multiple mains and which are easily recognizable
2709 -- as "ada_main" names.
2710 if CodePeer_Mode then
2711 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2712 return "ada_main_for_" &
2713 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
2716 -- This loop tries the following possibilities in order
2722 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2723 -- it is set to 'ada_main'.
2725 for J in 0 .. 99 loop
2727 Nlen := Name'Length - Suffix'Length;
2729 Nlen := Name'Length;
2730 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2731 Name (Name'Last - 1) :=
2732 Character'Val (J / 10 + Character'Pos ('0'));
2735 for K in ALIs.First .. ALIs.Last loop
2736 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2738 -- Get unit name, removing %b or %e at end
2740 Get_Name_String (Units.Table (L).Uname);
2741 Name_Len := Name_Len - 2;
2743 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2749 return Name (1 .. Nlen);
2755 -- If we fall through, just use a peculiar unlikely name
2757 return ("Qwertyuiop");
2758 end Get_Ada_Main_Name;
2764 function Get_Main_Name return String is
2766 -- Explicit name given with -M switch
2768 if Bind_Alternate_Main_Name then
2769 return Alternate_Main_Name.all;
2771 -- Case of main program name to be used directly
2773 elsif Use_Ada_Main_Program_Name_On_Target then
2775 -- Get main program name
2777 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2779 -- If this is a child name, return only the name of the child, since
2780 -- we can't have dots in a nested program name. Note that we do not
2781 -- include the %b at the end of the unit name.
2783 for J in reverse 1 .. Name_Len - 2 loop
2784 if J = 1 or else Name_Buffer (J - 1) = '.' then
2785 return Name_Buffer (J .. Name_Len - 2);
2789 raise Program_Error; -- impossible exit
2791 -- Case where "main" is to be used as default
2798 ---------------------
2799 -- Get_WC_Encoding --
2800 ---------------------
2802 function Get_WC_Encoding return Character is
2804 -- If encoding method specified by -W switch, then return it
2806 if Wide_Character_Encoding_Method_Specified then
2807 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
2809 -- If no main program, and not specified, set brackets, we really have
2810 -- no better choice. If some other encoding is required when there is
2811 -- no main, it must be set explicitly using -Wx.
2813 -- Note: if the ALI file always passed the wide character encoding of
2814 -- every file, then we could use the encoding of the initial specified
2815 -- file, but this information is passed only for potential main
2816 -- programs. We could fix this sometime, but it is a very minor point
2817 -- (wide character default encoding for [Wide_[Wide_]Text_IO when there
2818 -- is no main program).
2820 elsif No_Main_Subprogram then
2823 -- Otherwise if there is a main program, take encoding from it
2826 return ALIs.Table (ALIs.First).WC_Encoding;
2828 end Get_WC_Encoding;
2834 function Has_Finalizer return Boolean is
2839 for E in reverse Elab_Order.First .. Elab_Order.Last loop
2840 Unum := Elab_Order.Table (E);
2841 U := Units.Table (Unum);
2843 -- We are only interested in non-generic packages
2845 if U.Unit_Kind = 'p'
2846 and then U.Has_Finalizer
2847 and then not U.Is_Generic
2848 and then not U.No_Elab
2857 ----------------------
2858 -- Lt_Linker_Option --
2859 ----------------------
2861 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
2863 -- Sort internal files last
2865 if Linker_Options.Table (Op1).Internal_File
2867 Linker_Options.Table (Op2).Internal_File
2869 -- Note: following test uses False < True
2871 return Linker_Options.Table (Op1).Internal_File
2873 Linker_Options.Table (Op2).Internal_File;
2875 -- If both internal or both non-internal, sort according to the
2876 -- elaboration position. A unit that is elaborated later should come
2877 -- earlier in the linker options list.
2880 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2882 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2885 end Lt_Linker_Option;
2887 ------------------------
2888 -- Move_Linker_Option --
2889 ------------------------
2891 procedure Move_Linker_Option (From : Natural; To : Natural) is
2893 Linker_Options.Table (To) := Linker_Options.Table (From);
2894 end Move_Linker_Option;
2896 ----------------------------
2897 -- Resolve_Binder_Options --
2898 ----------------------------
2900 procedure Resolve_Binder_Options is
2902 procedure Check_Package (Var : in out Boolean; Name : String);
2903 -- Set Var to true iff the current identifier in Namet is Name. Do
2904 -- nothing if it doesn't match. This procedure is just an helper to
2905 -- avoid to explicitely deal with length.
2911 procedure Check_Package (Var : in out Boolean; Name : String) is
2913 if Name_Len = Name'Length
2914 and then Name_Buffer (1 .. Name_Len) = Name
2920 -- Start of processing for Check_Package
2923 for E in Elab_Order.First .. Elab_Order.Last loop
2924 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
2926 -- This is not a perfect approach, but is the current protocol
2927 -- between the run-time and the binder to indicate that tasking is
2928 -- used: System.OS_Interface should always be used by any tasking
2931 Check_Package (With_GNARL, "system.os_interface%s");
2933 -- Ditto for the use of restricted tasking
2936 (System_Tasking_Restricted_Stages_Used,
2937 "system.tasking.restricted.stages%s");
2939 -- Ditto for the use of interrupts
2941 Check_Package (System_Interrupts_Used, "system.interrupts%s");
2943 -- Ditto for the use of dispatching domains
2946 (Dispatching_Domains_Used,
2947 "system.multiprocessors.dispatching_domains%s");
2949 -- Ditto for the use of restrictions
2951 Check_Package (System_Restrictions_Used, "system.restrictions%s");
2953 end Resolve_Binder_Options;
2959 procedure Set_Boolean (B : Boolean) is
2960 True_Str : constant String := "True";
2961 False_Str : constant String := "False";
2964 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
2965 Last := Last + True_Str'Length;
2967 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
2968 Last := Last + False_Str'Length;
2976 procedure Set_Char (C : Character) is
2979 Statement_Buffer (Last) := C;
2986 procedure Set_Int (N : Int) is
2998 Statement_Buffer (Last) :=
2999 Character'Val (N mod 10 + Character'Pos ('0'));
3003 -------------------------
3004 -- Set_IS_Pragma_Table --
3005 -------------------------
3007 procedure Set_IS_Pragma_Table is
3009 for F in ALIs.First .. ALIs.Last loop
3010 for K in ALIs.Table (F).First_Interrupt_State ..
3011 ALIs.Table (F).Last_Interrupt_State
3014 Inum : constant Int :=
3015 Interrupt_States.Table (K).Interrupt_Id;
3016 Stat : constant Character :=
3017 Interrupt_States.Table (K).Interrupt_State;
3020 while IS_Pragma_Settings.Last < Inum loop
3021 IS_Pragma_Settings.Append ('n');
3024 IS_Pragma_Settings.Table (Inum) := Stat;
3028 end Set_IS_Pragma_Table;
3030 ---------------------------
3031 -- Set_Main_Program_Name --
3032 ---------------------------
3034 procedure Set_Main_Program_Name is
3036 -- Note that name has %b on the end which we ignore
3038 -- First we output the initial _ada_ since we know that the main
3039 -- program is a library level subprogram.
3041 Set_String ("_ada_");
3043 -- Copy name, changing dots to double underscores
3045 for J in 1 .. Name_Len - 2 loop
3046 if Name_Buffer (J) = '.' then
3049 Set_Char (Name_Buffer (J));
3052 end Set_Main_Program_Name;
3054 ---------------------
3055 -- Set_Name_Buffer --
3056 ---------------------
3058 procedure Set_Name_Buffer is
3060 for J in 1 .. Name_Len loop
3061 Set_Char (Name_Buffer (J));
3063 end Set_Name_Buffer;
3065 -------------------------
3066 -- Set_PSD_Pragma_Table --
3067 -------------------------
3069 procedure Set_PSD_Pragma_Table is
3071 for F in ALIs.First .. ALIs.Last loop
3072 for K in ALIs.Table (F).First_Specific_Dispatching ..
3073 ALIs.Table (F).Last_Specific_Dispatching
3076 DTK : Specific_Dispatching_Record
3077 renames Specific_Dispatching.Table (K);
3080 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3081 PSD_Pragma_Settings.Append ('F');
3084 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3085 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3090 end Set_PSD_Pragma_Table;
3096 procedure Set_String (S : String) is
3098 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3099 Last := Last + S'Length;
3102 ------------------------
3103 -- Set_String_Replace --
3104 ------------------------
3106 procedure Set_String_Replace (S : String) is
3108 Statement_Buffer (Last - S'Length + 1 .. Last) := S;
3109 end Set_String_Replace;
3115 procedure Set_Unit_Name (Mode : Qualification_Mode := Double_Underscores) is
3117 for J in 1 .. Name_Len - 2 loop
3118 if Name_Buffer (J) = '.' then
3119 if Mode = Double_Underscores then
3121 elsif Mode = Dot then
3127 Set_Char (Name_Buffer (J));
3132 ---------------------
3133 -- Set_Unit_Number --
3134 ---------------------
3136 procedure Set_Unit_Number (U : Unit_Id) is
3137 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3138 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3141 if Num_Units >= 10 and then Unum < 10 then
3145 if Num_Units >= 100 and then Unum < 100 then
3150 end Set_Unit_Number;
3152 ----------------------------
3153 -- Write_Statement_Buffer --
3154 ----------------------------
3156 procedure Write_Statement_Buffer is
3158 WBI (Statement_Buffer (1 .. Last));
3160 end Write_Statement_Buffer;
3162 procedure Write_Statement_Buffer (S : String) is
3165 Write_Statement_Buffer;
3166 end Write_Statement_Buffer;