AVX-512: Pacify -Wshift-overflow=2. [PR115409]
[official-gcc.git] / gcc / ada / bindgen.adb
blob89b2b88395bcd22621a1388bd84452b38f3d0cae
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- B I N D G E N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2024, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Casing; use Casing;
27 with Fname; use Fname;
28 with Gnatvsn; use Gnatvsn;
29 with Hostparm;
30 with Namet; use Namet;
31 with Opt; use Opt;
32 with Osint; use Osint;
33 with Osint.B; use Osint.B;
34 with Output; use Output;
35 with Rident; use Rident;
36 with Table;
37 with Targparm; use Targparm;
38 with Types; use Types;
40 with System.OS_Lib;
41 with System.WCh_Con; use System.WCh_Con;
43 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
44 with GNAT.HTable;
46 package body Bindgen is
47 Statement_Buffer : String (1 .. 1000);
48 -- Buffer used for constructing output statements
50 Stm_Last : Natural := 0;
51 -- Stm_Last location in Statement_Buffer currently set
53 With_GNARL : Boolean := False;
54 -- Flag which indicates whether the program uses the GNARL library
55 -- (presence of the unit System.OS_Interface)
57 Num_Elab_Calls : Nat := 0;
58 -- Number of generated calls to elaboration routines
60 Num_Primary_Stacks : Nat := 0;
61 -- Number of default-sized primary stacks the binder needs to allocate for
62 -- task objects declared in the program.
64 Num_Sec_Stacks : Nat := 0;
65 -- Number of default-sized primary stacks the binder needs to allocate for
66 -- task objects declared in the program.
68 Command_Line_Used : Boolean := False;
69 -- Flag indicating whether the unit Ada.Command_Line is in the closure of
70 -- the partition. This is set by Resolve_Binder_Options, and is used to
71 -- determine whether or not to import and use symbols defined in
72 -- Ada.Command_Line's support packages (gnat_argc, gnat_argv, gnat_envp
73 -- and gnat_exit_status). Conservatively, it is always set to True for
74 -- non-configurable run-times as parts of the compiler and run-time assume
75 -- these symbols are available and can be imported directly.
77 System_Restrictions_Used : Boolean := False;
78 -- Flag indicating whether the unit System.Restrictions is in the closure
79 -- of the partition. This is set by Resolve_Binder_Options, and is used
80 -- to determine whether or not to initialize the restrictions information
81 -- in the body of the binder generated file (we do not want to do this
82 -- unconditionally, since it drags in the System.Restrictions unit
83 -- unconditionally, which is unpleasand, especially for ZFP etc.)
85 Dispatching_Domains_Used : Boolean := False;
86 -- Flag indicating whether multiprocessor dispatching domains are used in
87 -- the closure of the partition. This is set by Resolve_Binder_Options, and
88 -- is used to call the routine to disallow the creation of new dispatching
89 -- domains just before calling the main procedure from the environment
90 -- task.
92 System_Tasking_Restricted_Stages_Used : Boolean := False;
93 -- Flag indicating whether the unit System.Tasking.Restricted.Stages is in
94 -- the closure of the partition. This is set by Resolve_Binder_Options,
95 -- and it used to call a routine to active all the tasks at the end of
96 -- the elaboration when partition elaboration policy is sequential.
98 System_Interrupts_Used : Boolean := False;
99 -- Flag indicating whether the unit System.Interrups is in the closure of
100 -- the partition. This is set by Resolve_Binder_Options, and it used to
101 -- attach interrupt handlers at the end of the elaboration when partition
102 -- elaboration policy is sequential.
104 System_BB_CPU_Primitives_Multiprocessors_Used : Boolean := False;
105 -- Flag indicating whether unit System.BB.CPU_Primitives.Multiprocessors
106 -- is in the closure of the partition. This is set by procedure
107 -- Resolve_Binder_Options, and it is used to call a procedure that starts
108 -- slave processors.
110 System_Version_Control_Used : Boolean := False;
111 -- Flag indicating whether unit System.Version_Control is in the closure.
112 -- This unit is implicitly withed by the compiler when Version or
113 -- Body_Version attributes are used. If the package is not in the closure,
114 -- the version definitions can be removed.
116 Lib_Final_Built : Boolean := False;
117 -- Flag indicating whether the finalize_library rountine has been built
119 Bind_Env_String_Built : Boolean := False;
120 -- Flag indicating whether a bind environment string has been built
122 CodePeer_Wrapper_Name : constant String := "call_main_subprogram";
123 -- For CodePeer, introduce a wrapper subprogram which calls the
124 -- user-defined main subprogram.
126 -- Name for local C-String variable
128 Adainit_String_Obj_Name : constant String := "Adainit_Name_C_String";
130 -- Name and link_name for CUDA device initialization procedure
132 Device_Ada_Init_Subp_Name : constant String := "Device_Initialization";
133 Device_Link_Name_Prefix : constant String := "__device_";
135 function Device_Link_Name (Suffix : String) return String is
136 (Device_Link_Name_Prefix &
137 (if CUDA_Device_Library_Name = null
138 then "ada" -- is this an error path?
139 else CUDA_Device_Library_Name.all) & Suffix);
141 function Device_Ada_Init_Link_Name return String
142 is (Device_Link_Name (Suffix => "init"));
144 ----------------------------------
145 -- Interface_State Pragma Table --
146 ----------------------------------
148 -- This table assembles the interface state pragma information from
149 -- all the units in the partition. Note that Bcheck has already checked
150 -- that the information is consistent across units. The entries
151 -- in this table are n/u/r/s for not set/user/runtime/system.
153 package IS_Pragma_Settings is new Table.Table
154 (Table_Component_Type => Character,
155 Table_Index_Type => Int,
156 Table_Low_Bound => 0,
157 Table_Initial => 100,
158 Table_Increment => 200,
159 Table_Name => "IS_Pragma_Settings");
161 -- This table assembles the Priority_Specific_Dispatching pragma
162 -- information from all the units in the partition. Note that Bcheck has
163 -- already checked that the information is consistent across units.
164 -- The entries in this table are the upper case first character of the
165 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
167 package PSD_Pragma_Settings is new Table.Table
168 (Table_Component_Type => Character,
169 Table_Index_Type => Int,
170 Table_Low_Bound => 0,
171 Table_Initial => 100,
172 Table_Increment => 200,
173 Table_Name => "PSD_Pragma_Settings");
175 ----------------------------
176 -- Bind_Environment Table --
177 ----------------------------
179 subtype Header_Num is Int range 0 .. 36;
181 function Hash (Nam : Name_Id) return Header_Num;
183 package Bind_Environment is new GNAT.HTable.Simple_HTable
184 (Header_Num => Header_Num,
185 Element => Name_Id,
186 No_Element => No_Name,
187 Key => Name_Id,
188 Hash => Hash,
189 Equal => "=");
191 ----------------------
192 -- Run-Time Globals --
193 ----------------------
195 -- This section documents the global variables that are set from the
196 -- generated binder file.
198 -- Main_Priority : Integer;
199 -- Time_Slice_Value : Integer;
200 -- Heap_Size : Natural;
201 -- WC_Encoding : Character;
202 -- Locking_Policy : Character;
203 -- Queuing_Policy : Character;
204 -- Task_Dispatching_Policy : Character;
205 -- Priority_Specific_Dispatching : System.Address;
206 -- Num_Specific_Dispatching : Integer;
207 -- Restrictions : System.Address;
208 -- Interrupt_States : System.Address;
209 -- Num_Interrupt_States : Integer;
210 -- Unreserve_All_Interrupts : Integer;
211 -- Exception_Tracebacks : Integer;
212 -- Exception_Tracebacks_Symbolic : Integer;
213 -- Detect_Blocking : Integer;
214 -- Default_Stack_Size : Integer;
215 -- Default_Secondary_Stack_Size : System.Parameters.Size_Type;
216 -- Leap_Seconds_Support : Integer;
217 -- Main_CPU : Integer;
218 -- Default_Sized_SS_Pool : System.Address;
219 -- Binder_Sec_Stacks_Count : Natural;
220 -- XDR_Stream : Integer;
222 -- Main_Priority is the priority value set by pragma Priority in the main
223 -- program. If no such pragma is present, the value is -1.
225 -- Time_Slice_Value is the time slice value set by pragma Time_Slice in the
226 -- main program, or by the use of a -Tnnn parameter for the binder (if both
227 -- are present, the binder value overrides). The value is in milliseconds.
228 -- A value of zero indicates that time slicing should be suppressed. If no
229 -- pragma is present, and no -T switch was used, the value is -1.
231 -- WC_Encoding shows the wide character encoding method used for the main
232 -- program. This is one of the encoding letters defined in
233 -- System.WCh_Con.WC_Encoding_Letters.
235 -- Locking_Policy is a space if no locking policy was specified for the
236 -- partition. If a locking policy was specified, the value is the upper
237 -- case first character of the locking policy name, for example, 'C' for
238 -- Ceiling_Locking.
240 -- Queuing_Policy is a space if no queuing policy was specified for the
241 -- partition. If a queuing policy was specified, the value is the upper
242 -- case first character of the queuing policy name for example, 'F' for
243 -- FIFO_Queuing.
245 -- Task_Dispatching_Policy is a space if no task dispatching policy was
246 -- specified for the partition. If a task dispatching policy was specified,
247 -- the value is the upper case first character of the policy name, e.g. 'F'
248 -- for FIFO_Within_Priorities.
250 -- Priority_Specific_Dispatching is the address of a string used to store
251 -- the task dispatching policy specified for the different priorities in
252 -- the partition. The length of this string is determined by the last
253 -- priority for which such a pragma applies (the string will be a null
254 -- string if no specific dispatching policies were used). If pragma were
255 -- present, the entries apply to the priorities in sequence from the first
256 -- priority. The value stored is the upper case first character of the
257 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
258 -- for those priority ranges not specified.
260 -- Num_Specific_Dispatching is length of the Priority_Specific_Dispatching
261 -- string. It will be set to zero if no Priority_Specific_Dispatching
262 -- pragmas are present.
264 -- Restrictions is the address of a null-terminated string specifying the
265 -- restrictions information for the partition. The format is identical to
266 -- that of the parameter string found on R lines in ali files (see Lib.Writ
267 -- spec in lib-writ.ads for full details). The difference is that in this
268 -- context the values are the cumulative ones for the entire partition.
270 -- Interrupt_States is the address of a string used to specify the
271 -- cumulative results of Interrupt_State pragmas used in the partition.
272 -- The length of this string is determined by the last interrupt for which
273 -- such a pragma is given (the string will be a null string if no pragmas
274 -- were used). If pragma were present the entries apply to the interrupts
275 -- in sequence from the first interrupt, and are set to one of four
276 -- possible settings: 'n', 'u', 'r', 's', see description in init.c
277 -- (__gnat_get_interrupt_state) for further details.
279 -- Num_Interrupt_States is the length of the Interrupt_States string. It
280 -- will be set to zero if no Interrupt_State pragmas are present.
282 -- Unreserve_All_Interrupts is set to one if at least one unit in the
283 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
285 -- Exception_Tracebacks is set to one if the -Ea or -E parameter was
286 -- present in the bind and to zero otherwise. Note that on some targets
287 -- exception tracebacks are provided by default, so a value of zero for
288 -- this parameter does not necessarily mean no trace backs are available.
290 -- Exception_Tracebacks_Symbolic is set to one if the -Es parameter was
291 -- present in the bind and to zero otherwise.
293 -- Detect_Blocking indicates whether pragma Detect_Blocking is active or
294 -- not. A value of zero indicates that the pragma is not present, while a
295 -- value of 1 signals its presence in the partition.
297 -- Default_Stack_Size is the default stack size used when creating an Ada
298 -- task with no explicit Storage_Size clause.
300 -- Default_Secondary_Stack_Size is the default secondary stack size used
301 -- when creating an Ada task with no explicit Secondary_Stack_Size clause.
303 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
304 -- disabled. A value of zero indicates that leap seconds are turned "off",
305 -- while a value of one signifies "on" status.
307 -- Main_CPU is the processor set by pragma CPU in the main program. If no
308 -- such pragma is present, the value is -1.
310 -- Default_Sized_SS_Pool is set to the address of the default-sized
311 -- secondary stacks array generated by the binder. This pool of stacks is
312 -- generated when either the restriction No_Implicit_Heap_Allocations
313 -- or No_Implicit_Task_Allocations is active.
315 -- Binder_Sec_Stacks_Count is the number of generated secondary stacks in
316 -- the Default_Sized_SS_Pool.
318 -- XDR_Stream indicates whether streaming should be performed using the
319 -- XDR protocol. A value of one indicates that XDR streaming is enabled.
321 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
322 -- Convenient shorthand used throughout
324 -----------------------
325 -- Local Subprograms --
326 -----------------------
328 procedure Gen_Adainit (Elab_Order : Unit_Id_Array);
329 -- Generates the Adainit procedure
331 procedure Gen_Adafinal;
332 -- Generate the Adafinal procedure
334 procedure Gen_Bind_Env_String;
335 -- Generate the bind environment buffer
337 procedure Gen_CodePeer_Wrapper;
338 -- For CodePeer, generate wrapper which calls user-defined main subprogram
340 procedure Gen_CUDA_Defs;
341 -- Generate definitions needed in order to register kernels
343 procedure Gen_CUDA_Init;
344 -- Generate calls needed in order to register kernels
346 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array);
347 -- Generate sequence of elaboration calls
349 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array);
350 -- Generate sequence of external declarations for elaboration
352 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array);
353 -- Generate comments showing elaboration order chosen
355 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array);
356 -- Generate a sequence of finalization calls to elaborated packages
358 procedure Gen_Main;
359 -- Generate procedure main
361 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array);
362 -- Output comments containing a list of the full names of the object
363 -- files to be linked and the list of linker options supplied by
364 -- Linker_Options pragmas in the source.
366 procedure Gen_Output_File_Ada
367 (Filename : String;
368 Elab_Order : Unit_Id_Array);
369 -- Generate Ada output file
371 procedure Gen_Restrictions;
372 -- Generate initialization of restrictions variable
374 procedure Gen_Versions;
375 -- Output series of definitions for unit versions
377 function Get_Ada_Main_Name return String;
378 -- This function is used for the Ada main output to compute a usable name
379 -- for the generated main program. The normal main program name is
380 -- Ada_Main, but this won't work if the user has a unit with this name.
381 -- This function tries Ada_Main first, and if there is such a clash, then
382 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
384 function Get_Main_Unit_Name (S : String) return String;
385 -- Return the main unit name corresponding to S by replacing '.' with '_'
387 function Get_Main_Name return String;
388 -- This function is used in the main output case to compute the correct
389 -- external main program. It is "main" by default, unless the flag
390 -- Use_Ada_Main_Program_Name_On_Target is set, in which case it is the name
391 -- of the Ada main name without the "_ada". This default can be overridden
392 -- explicitly using the -Mname binder switch.
394 function Get_WC_Encoding return Character;
395 -- Return wide character encoding method to set as WC_Encoding in output.
396 -- If -W has been used, returns the specified encoding, otherwise returns
397 -- the encoding method used for the main program source. If there is no
398 -- main program source (-z switch used), returns brackets ('b').
400 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean;
401 -- Determine whether the current unit has at least one library-level
402 -- finalizer.
404 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean;
405 -- Compare linker options, when sorting, first according to
406 -- Is_Internal_File (internal files come later) and then by
407 -- elaboration order position (latest to earliest).
409 procedure Move_Linker_Option (From : Natural; To : Natural);
410 -- Move routine for sorting linker options
412 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array);
413 -- Set the value of With_GNARL
415 procedure Set_Char (C : Character);
416 -- Set given character in Statement_Buffer at the Stm_Last + 1 position
417 -- and increment Stm_Last by one to reflect the stored character.
419 procedure Set_Int (N : Int);
420 -- Set given value in decimal in Statement_Buffer with no spaces starting
421 -- at the Stm_Last + 1 position, and updating Stm_Last past the value. A
422 -- minus sign is output for a negative value.
424 procedure Set_Boolean (B : Boolean);
425 -- Set given boolean value in Statement_Buffer at the Stm_Last + 1 position
426 -- and update Stm_Last past the value.
428 procedure Set_IS_Pragma_Table;
429 -- Initializes contents of IS_Pragma_Settings table from ALI table
431 procedure Set_Main_Program_Name;
432 -- Given the main program name in Name_Buffer (length in Name_Len) generate
433 -- the name of the routine to be used in the call. The name is generated
434 -- starting at Stm_Last + 1, and Stm_Last is updated past it.
436 procedure Set_Name_Buffer;
437 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
439 procedure Set_PSD_Pragma_Table;
440 -- Initializes contents of PSD_Pragma_Settings table from ALI table
442 procedure Set_String (S : String);
443 -- Sets characters of given string in Statement_Buffer, starting at the
444 -- Stm_Last + 1 position, and updating last past the string value.
446 procedure Set_String_Replace (S : String);
447 -- Replaces the last S'Length characters in the Statement_Buffer with the
448 -- characters of S. The caller must ensure that these characters do in fact
449 -- exist in the Statement_Buffer.
451 procedure Set_Unit_Name;
452 -- Given a unit name in the Name_Buffer, copy it into Statement_Buffer,
453 -- starting at the Stm_Last + 1 position and update Stm_Last past the
454 -- value. Each dot (.) will be qualified into double underscores (__).
456 procedure Set_Unit_Number (U : Unit_Id);
457 -- Sets unit number (first unit is 1, leading zeroes output to line up all
458 -- output unit numbers nicely as required by the value, and by the total
459 -- number of units.
461 procedure Write_Statement_Buffer;
462 -- Write out contents of statement buffer up to Stm_Last, and reset
463 -- Stm_Last to 0.
465 procedure Write_Statement_Buffer (S : String);
466 -- First writes its argument (using Set_String (S)), then writes out the
467 -- contents of statement buffer up to Stm_Last, and resets Stm_Last to 0.
469 procedure Write_Bind_Line (S : String);
470 -- Write S (an LF-terminated string) to the binder file (for use with
471 -- Set_Special_Output).
473 ------------------
474 -- Gen_Adafinal --
475 ------------------
477 procedure Gen_Adafinal is
478 begin
479 WBI (" procedure " & Ada_Final_Name.all & " is");
481 -- Call s_stalib_adafinal to await termination of tasks and so on. We
482 -- want to do this if there is a main program, either in Ada or in some
483 -- other language. (Note that Bind_Main_Program is True for Ada mains,
484 -- but False for mains in other languages.) We do not want to do this if
485 -- we're binding a library.
487 if not Bind_For_Library and not CodePeer_Mode then
488 WBI (" procedure s_stalib_adafinal;");
489 Set_String (" pragma Import (Ada, s_stalib_adafinal, ");
490 Set_String ("""system__standard_library__adafinal"");");
491 Write_Statement_Buffer;
492 end if;
494 WBI ("");
495 WBI (" procedure Runtime_Finalize;");
496 WBI (" pragma Import (C, Runtime_Finalize, " &
497 """__gnat_runtime_finalize"");");
498 WBI ("");
499 WBI (" begin");
501 if not CodePeer_Mode then
502 WBI (" if not Is_Elaborated then");
503 WBI (" return;");
504 WBI (" end if;");
505 WBI (" Is_Elaborated := False;");
506 end if;
508 WBI (" Runtime_Finalize;");
510 -- By default (real targets), finalization is done differently depending
511 -- on whether this is the main program or a library.
513 if not CodePeer_Mode then
514 if not Bind_For_Library then
515 WBI (" s_stalib_adafinal;");
516 elsif Lib_Final_Built then
517 WBI (" finalize_library;");
518 else
519 WBI (" null;");
520 end if;
522 -- Pragma Import C cannot be used on virtual targets, therefore call the
523 -- runtime finalization routine directly in CodePeer mode, where
524 -- imported functions are ignored.
526 else
527 WBI (" System.Standard_Library.Adafinal;");
528 end if;
530 WBI (" end " & Ada_Final_Name.all & ";");
531 WBI ("");
532 end Gen_Adafinal;
534 -----------------
535 -- Gen_Adainit --
536 -----------------
538 procedure Gen_Adainit (Elab_Order : Unit_Id_Array) is
539 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
540 Main_CPU : Int renames ALIs.Table (ALIs.First).Main_CPU;
541 begin
542 -- Declare the access-to-subprogram type used for initialization of
543 -- of __gnat_finalize_library_objects. This is declared at library
544 -- level for compatibility with the type used in System.Soft_Links.
545 -- The import of the soft link which performs library-level object
546 -- finalization does not work for CodePeer, so regular Ada is used in
547 -- that case. For restricted run-time libraries (ZFP and Ravenscar)
548 -- tasks are non-terminating, so we do not want finalization.
550 if not Suppress_Standard_Library_On_Target
551 and then not CodePeer_Mode
552 and then not Configurable_Run_Time_On_Target
553 then
554 WBI (" type No_Param_Proc is access procedure;");
555 WBI (" pragma Favor_Top_Level (No_Param_Proc);");
556 WBI ("");
557 end if;
559 WBI (" procedure " & Ada_Init_Name.all & " is");
561 -- In CodePeer mode, simplify adainit procedure by only calling
562 -- elaboration procedures.
564 if CodePeer_Mode then
565 WBI (" begin");
567 -- If the standard library is suppressed, then the only global variables
568 -- that might be needed (by the Ravenscar profile) are the priority and
569 -- the processor for the environment task.
571 elsif Suppress_Standard_Library_On_Target then
572 if Main_Priority /= No_Main_Priority then
573 WBI (" Main_Priority : Integer;");
574 WBI (" pragma Import (C, Main_Priority," &
575 " ""__gl_main_priority"");");
576 WBI ("");
577 end if;
579 if Main_CPU /= No_Main_CPU then
580 WBI (" Main_CPU : Integer;");
581 WBI (" pragma Import (C, Main_CPU," &
582 " ""__gl_main_cpu"");");
583 WBI ("");
584 end if;
586 if System_Interrupts_Used
587 and then Partition_Elaboration_Policy_Specified = 'S'
588 then
589 WBI (" procedure Install_Restricted_Handlers_Sequential;");
590 WBI (" pragma Import (C," &
591 "Install_Restricted_Handlers_Sequential," &
592 " ""__gnat_attach_all_handlers"");");
593 WBI ("");
594 end if;
596 if System_Tasking_Restricted_Stages_Used
597 and then Partition_Elaboration_Policy_Specified = 'S'
598 then
599 WBI (" Partition_Elaboration_Policy : Character;");
600 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
601 " ""__gnat_partition_elaboration_policy"");");
602 WBI ("");
603 WBI (" procedure Activate_All_Tasks_Sequential;");
604 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
605 " ""__gnat_activate_all_tasks"");");
606 WBI ("");
607 end if;
609 if System_BB_CPU_Primitives_Multiprocessors_Used then
610 WBI (" procedure Start_Slave_CPUs;");
611 WBI (" pragma Import (C, Start_Slave_CPUs," &
612 " ""__gnat_start_slave_cpus"");");
613 WBI ("");
614 end if;
616 -- Import the default stack object if a size has been provided to the
617 -- binder.
619 if Opt.Default_Stack_Size /= Opt.No_Stack_Size then
620 WBI (" Default_Stack_Size : Integer;");
621 WBI (" pragma Import (C, Default_Stack_Size, " &
622 """__gl_default_stack_size"");");
623 end if;
625 -- Initialize stack limit variable of the environment task if the
626 -- stack check method is stack limit and stack check is enabled.
628 if Stack_Check_Limits_On_Target
629 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
630 then
631 WBI ("");
632 WBI (" procedure Initialize_Stack_Limit;");
633 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
634 """__gnat_initialize_stack_limit"");");
635 end if;
637 if Num_Sec_Stacks > 0 then
638 WBI (" Binder_Sec_Stacks_Count : Natural;");
639 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
640 """__gnat_binder_ss_count"");");
641 WBI ("");
642 end if;
644 -- Import secondary stack pool variables if the secondary stack is
645 -- used. They are not referenced otherwise.
647 if Sec_Stack_Used then
648 WBI (" Default_Secondary_Stack_Size : " &
649 "System.Parameters.Size_Type;");
650 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
651 """__gnat_default_ss_size"");");
653 WBI (" Default_Sized_SS_Pool : System.Address;");
654 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
655 """__gnat_default_ss_pool"");");
657 WBI ("");
658 end if;
660 WBI (" begin");
662 -- Set the default stack size if provided to the binder
664 if Opt.Default_Stack_Size /= Opt.No_Stack_Size then
665 Set_String (" Default_Stack_Size := ");
666 Set_Int (Default_Stack_Size);
667 Set_String (";");
668 Write_Statement_Buffer;
669 end if;
671 if Main_Priority /= No_Main_Priority then
672 Set_String (" Main_Priority := ");
673 Set_Int (Main_Priority);
674 Set_Char (';');
675 Write_Statement_Buffer;
676 end if;
678 if Main_CPU /= No_Main_CPU then
679 Set_String (" Main_CPU := ");
680 Set_Int (Main_CPU);
681 Set_Char (';');
682 Write_Statement_Buffer;
683 end if;
685 if System_Tasking_Restricted_Stages_Used
686 and then Partition_Elaboration_Policy_Specified = 'S'
687 then
688 Set_String (" Partition_Elaboration_Policy := '");
689 Set_Char (Partition_Elaboration_Policy_Specified);
690 Set_String ("';");
691 Write_Statement_Buffer;
692 end if;
694 if Main_Priority = No_Main_Priority
695 and then Opt.Default_Stack_Size = Opt.No_Stack_Size
696 and then Main_CPU = No_Main_CPU
697 and then not System_Tasking_Restricted_Stages_Used
698 then
699 WBI (" null;");
700 end if;
702 Gen_Restrictions;
704 -- Generate the default-sized secondary stack pool if the secondary
705 -- stack is used by the program.
707 if Sec_Stack_Used then
708 -- Elaborate the body of the binder to initialize the default-
709 -- sized secondary stack pool.
711 WBI ("");
712 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
714 -- Generate the default-sized secondary stack pool and set the
715 -- related secondary stack globals.
717 Set_String (" Default_Secondary_Stack_Size := ");
719 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
720 Set_Int (Opt.Default_Sec_Stack_Size);
721 else
722 Set_String
723 ("System.Parameters.Runtime_Default_Sec_Stack_Size");
724 end if;
726 Set_Char (';');
727 Write_Statement_Buffer;
729 Set_String (" Binder_Sec_Stacks_Count := ");
730 Set_Int (Num_Sec_Stacks);
731 Set_Char (';');
732 Write_Statement_Buffer;
734 WBI (" Default_Sized_SS_Pool := " &
735 "Sec_Default_Sized_Stacks'Address;");
736 WBI ("");
737 end if;
739 -- Normal case (standard library not suppressed). Set all global values
740 -- used by the run time.
742 else
743 WBI (" Main_Priority : Integer;");
744 WBI (" pragma Import (C, Main_Priority, " &
745 """__gl_main_priority"");");
746 WBI (" Time_Slice_Value : Integer;");
747 WBI (" pragma Import (C, Time_Slice_Value, " &
748 """__gl_time_slice_val"");");
749 WBI (" WC_Encoding : Character;");
750 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
751 WBI (" Locking_Policy : Character;");
752 WBI (" pragma Import (C, Locking_Policy, " &
753 """__gl_locking_policy"");");
754 WBI (" Queuing_Policy : Character;");
755 WBI (" pragma Import (C, Queuing_Policy, " &
756 """__gl_queuing_policy"");");
757 WBI (" Task_Dispatching_Policy : Character;");
758 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
759 """__gl_task_dispatching_policy"");");
760 WBI (" Priority_Specific_Dispatching : System.Address;");
761 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
762 """__gl_priority_specific_dispatching"");");
763 WBI (" Num_Specific_Dispatching : Integer;");
764 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
765 """__gl_num_specific_dispatching"");");
766 WBI (" Main_CPU : Integer;");
767 WBI (" pragma Import (C, Main_CPU, " &
768 """__gl_main_cpu"");");
770 WBI (" Interrupt_States : System.Address;");
771 WBI (" pragma Import (C, Interrupt_States, " &
772 """__gl_interrupt_states"");");
773 WBI (" Num_Interrupt_States : Integer;");
774 WBI (" pragma Import (C, Num_Interrupt_States, " &
775 """__gl_num_interrupt_states"");");
776 WBI (" Unreserve_All_Interrupts : Integer;");
777 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
778 """__gl_unreserve_all_interrupts"");");
780 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
781 WBI (" Exception_Tracebacks : Integer;");
782 WBI (" pragma Import (C, Exception_Tracebacks, " &
783 """__gl_exception_tracebacks"");");
785 if Exception_Tracebacks_Symbolic then
786 WBI (" Exception_Tracebacks_Symbolic : Integer;");
787 WBI (" pragma Import (C, Exception_Tracebacks_Symbolic, " &
788 """__gl_exception_tracebacks_symbolic"");");
789 end if;
790 end if;
792 WBI (" Detect_Blocking : Integer;");
793 WBI (" pragma Import (C, Detect_Blocking, " &
794 """__gl_detect_blocking"");");
795 WBI (" Default_Stack_Size : Integer;");
796 WBI (" pragma Import (C, Default_Stack_Size, " &
797 """__gl_default_stack_size"");");
799 if Sec_Stack_Used then
800 WBI (" Default_Secondary_Stack_Size : " &
801 "System.Parameters.Size_Type;");
802 WBI (" pragma Import (C, Default_Secondary_Stack_Size, " &
803 """__gnat_default_ss_size"");");
804 end if;
806 if Leap_Seconds_Support then
807 WBI (" Leap_Seconds_Support : Integer;");
808 WBI (" pragma Import (C, Leap_Seconds_Support, " &
809 """__gl_leap_seconds_support"");");
810 end if;
812 WBI (" Bind_Env_Addr : System.Address;");
813 WBI (" pragma Import (C, Bind_Env_Addr, " &
814 """__gl_bind_env_addr"");");
816 if XDR_Stream then
817 WBI (" XDR_Stream : Integer;");
818 WBI (" pragma Import (C, XDR_Stream, ""__gl_xdr_stream"");");
819 end if;
821 WBI (" Interrupts_Default_To_System : Integer;");
822 WBI (" pragma Import (C, Interrupts_Default_To_System, " &
823 """__gl_interrupts_default_to_system"");");
825 -- Import entry point for initialization of the runtime
827 WBI ("");
828 WBI (" procedure Runtime_Initialize " &
829 "(Install_Handler : Integer);");
830 WBI (" pragma Import (C, Runtime_Initialize, " &
831 """__gnat_runtime_initialize"");");
833 -- Import entry point for initialization of the tasking runtime
835 if With_GNARL then
836 WBI ("");
837 WBI (" procedure Tasking_Runtime_Initialize;");
838 WBI (" pragma Import (C, Tasking_Runtime_Initialize, " &
839 """__gnat_tasking_runtime_initialize"");");
840 end if;
842 -- Import handlers attach procedure for sequential elaboration policy
844 if System_Interrupts_Used
845 and then Partition_Elaboration_Policy_Specified = 'S'
846 then
847 WBI (" procedure Install_Restricted_Handlers_Sequential;");
848 WBI (" pragma Import (C," &
849 "Install_Restricted_Handlers_Sequential," &
850 " ""__gnat_attach_all_handlers"");");
851 WBI ("");
852 end if;
854 -- Import task activation procedure for sequential elaboration
855 -- policy.
857 if System_Tasking_Restricted_Stages_Used
858 and then Partition_Elaboration_Policy_Specified = 'S'
859 then
860 WBI (" Partition_Elaboration_Policy : Character;");
861 WBI (" pragma Import (C, Partition_Elaboration_Policy," &
862 " ""__gnat_partition_elaboration_policy"");");
863 WBI ("");
864 WBI (" procedure Activate_All_Tasks_Sequential;");
865 WBI (" pragma Import (C, Activate_All_Tasks_Sequential," &
866 " ""__gnat_activate_all_tasks"");");
867 end if;
869 -- Import procedure to start slave cpus for bareboard runtime
871 if System_BB_CPU_Primitives_Multiprocessors_Used then
872 WBI (" procedure Start_Slave_CPUs;");
873 WBI (" pragma Import (C, Start_Slave_CPUs," &
874 " ""__gnat_start_slave_cpus"");");
875 end if;
877 -- For restricted run-time libraries (ZFP and Ravenscar)
878 -- tasks are non-terminating, so we do not want finalization.
880 if not Configurable_Run_Time_On_Target then
881 WBI ("");
882 WBI (" Finalize_Library_Objects : No_Param_Proc;");
883 WBI (" pragma Import (C, Finalize_Library_Objects, " &
884 """__gnat_finalize_library_objects"");");
885 end if;
887 -- Initialize stack limit variable of the environment task if the
888 -- stack check method is stack limit and stack check is enabled.
890 if Stack_Check_Limits_On_Target
891 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
892 then
893 WBI ("");
894 WBI (" procedure Initialize_Stack_Limit;");
895 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
896 """__gnat_initialize_stack_limit"");");
897 end if;
899 -- When dispatching domains are used then we need to signal it
900 -- before calling the main procedure.
902 if Dispatching_Domains_Used then
903 WBI (" procedure Freeze_Dispatching_Domains;");
904 WBI (" pragma Import");
905 WBI (" (Ada, Freeze_Dispatching_Domains, "
906 & """__gnat_freeze_dispatching_domains"");");
907 end if;
909 -- Secondary stack global variables
911 WBI (" Binder_Sec_Stacks_Count : Natural;");
912 WBI (" pragma Import (Ada, Binder_Sec_Stacks_Count, " &
913 """__gnat_binder_ss_count"");");
915 WBI (" Default_Sized_SS_Pool : System.Address;");
916 WBI (" pragma Import (Ada, Default_Sized_SS_Pool, " &
917 """__gnat_default_ss_pool"");");
919 WBI ("");
921 -- Start of processing for Adainit
923 WBI (" begin");
924 WBI (" if Is_Elaborated then");
925 WBI (" return;");
926 WBI (" end if;");
927 WBI (" Is_Elaborated := True;");
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)
934 then
935 WBI (" System.Elaboration_Allocators."
936 & "Mark_Start_Of_Elaboration;");
937 end if;
939 -- Generate assignments to initialize globals
941 Set_String (" Main_Priority := ");
942 Set_Int (Main_Priority);
943 Set_Char (';');
944 Write_Statement_Buffer;
946 Set_String (" Time_Slice_Value := ");
948 if Task_Dispatching_Policy_Specified = 'F'
949 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
950 then
951 Set_Int (0);
952 else
953 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
954 end if;
956 Set_Char (';');
957 Write_Statement_Buffer;
959 Set_String (" WC_Encoding := '");
960 Set_Char (Get_WC_Encoding);
962 Set_String ("';");
963 Write_Statement_Buffer;
965 Set_String (" Locking_Policy := '");
966 Set_Char (Locking_Policy_Specified);
967 Set_String ("';");
968 Write_Statement_Buffer;
970 Set_String (" Queuing_Policy := '");
971 Set_Char (Queuing_Policy_Specified);
972 Set_String ("';");
973 Write_Statement_Buffer;
975 Set_String (" Task_Dispatching_Policy := '");
976 Set_Char (Task_Dispatching_Policy_Specified);
977 Set_String ("';");
978 Write_Statement_Buffer;
980 if System_Tasking_Restricted_Stages_Used
981 and then Partition_Elaboration_Policy_Specified = 'S'
982 then
983 Set_String (" Partition_Elaboration_Policy := '");
984 Set_Char (Partition_Elaboration_Policy_Specified);
985 Set_String ("';");
986 Write_Statement_Buffer;
987 end if;
989 Gen_Restrictions;
991 WBI (" Priority_Specific_Dispatching :=");
992 WBI (" Local_Priority_Specific_Dispatching'Address;");
994 Set_String (" Num_Specific_Dispatching := ");
995 Set_Int (PSD_Pragma_Settings.Last + 1);
996 Set_Char (';');
997 Write_Statement_Buffer;
999 Set_String (" Main_CPU := ");
1000 Set_Int (Main_CPU);
1001 Set_Char (';');
1002 Write_Statement_Buffer;
1004 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
1006 Set_String (" Num_Interrupt_States := ");
1007 Set_Int (IS_Pragma_Settings.Last + 1);
1008 Set_Char (';');
1009 Write_Statement_Buffer;
1011 Set_String (" Unreserve_All_Interrupts := ");
1013 if Unreserve_All_Interrupts_Specified then
1014 Set_String ("1");
1015 else
1016 Set_String ("0");
1017 end if;
1019 Set_Char (';');
1020 Write_Statement_Buffer;
1022 if Exception_Tracebacks or Exception_Tracebacks_Symbolic then
1023 WBI (" Exception_Tracebacks := 1;");
1025 if Exception_Tracebacks_Symbolic then
1026 WBI (" Exception_Tracebacks_Symbolic := 1;");
1027 end if;
1028 end if;
1030 Set_String (" Detect_Blocking := ");
1032 if Detect_Blocking then
1033 Set_Int (1);
1034 else
1035 Set_Int (0);
1036 end if;
1038 Set_String (";");
1039 Write_Statement_Buffer;
1041 Set_String (" Default_Stack_Size := ");
1042 Set_Int (Default_Stack_Size);
1043 Set_String (";");
1044 Write_Statement_Buffer;
1046 if Interrupts_Default_To_System_Specified then
1047 Set_String (" Interrupts_Default_To_System := 1;");
1048 Write_Statement_Buffer;
1049 end if;
1051 if Leap_Seconds_Support then
1052 WBI (" Leap_Seconds_Support := 1;");
1053 end if;
1055 if XDR_Stream then
1056 WBI (" XDR_Stream := 1;");
1057 end if;
1059 if Bind_Env_String_Built then
1060 WBI (" Bind_Env_Addr := Bind_Env'Address;");
1061 end if;
1063 WBI ("");
1065 -- Generate default-sized secondary stack pool and set secondary
1066 -- stack globals.
1068 if Sec_Stack_Used then
1070 -- Elaborate the body of the binder to initialize the default-
1071 -- sized secondary stack pool.
1073 WBI (" " & Get_Ada_Main_Name & "'Elab_Body;");
1075 -- Generate the default-sized secondary stack pool and set the
1076 -- related secondary stack globals.
1078 Set_String (" Default_Secondary_Stack_Size := ");
1080 if Opt.Default_Sec_Stack_Size /= Opt.No_Stack_Size then
1081 Set_Int (Opt.Default_Sec_Stack_Size);
1082 else
1083 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
1084 end if;
1086 Set_Char (';');
1087 Write_Statement_Buffer;
1089 Set_String (" Binder_Sec_Stacks_Count := ");
1090 Set_Int (Num_Sec_Stacks);
1091 Set_Char (';');
1092 Write_Statement_Buffer;
1094 Set_String (" Default_Sized_SS_Pool := ");
1096 if Num_Sec_Stacks > 0 then
1097 Set_String ("Sec_Default_Sized_Stacks'Address;");
1098 else
1099 Set_String ("System.Null_Address;");
1100 end if;
1102 Write_Statement_Buffer;
1103 WBI ("");
1104 end if;
1106 -- Generate call to Runtime_Initialize
1108 WBI (" Runtime_Initialize (1);");
1110 -- Generate call to Tasking_Runtime_Initialize
1112 if With_GNARL then
1113 WBI (" Tasking_Runtime_Initialize;");
1114 end if;
1115 end if;
1117 -- Generate call to set Initialize_Scalar values if active
1119 if Initialize_Scalars_Used then
1120 WBI ("");
1121 Set_String (" System.Scalar_Values.Initialize ('");
1122 Set_Char (Initialize_Scalars_Mode1);
1123 Set_String ("', '");
1124 Set_Char (Initialize_Scalars_Mode2);
1125 Set_String ("');");
1126 Write_Statement_Buffer;
1127 end if;
1129 -- Initialize stack limit variable of the environment task if the stack
1130 -- check method is stack limit and stack check is enabled.
1132 if Stack_Check_Limits_On_Target
1133 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1134 then
1135 WBI ("");
1136 WBI (" Initialize_Stack_Limit;");
1137 end if;
1139 -- On CodePeer, the finalization of library objects is not relevant
1141 if CodePeer_Mode then
1142 null;
1144 -- If this is the main program case, attach finalize_library to the soft
1145 -- link. Do it only when not using a restricted run time, in which case
1146 -- tasks are non-terminating, so we do not want library-level
1147 -- finalization.
1149 elsif not Bind_For_Library
1150 and then not Configurable_Run_Time_On_Target
1151 and then not Suppress_Standard_Library_On_Target
1152 then
1153 WBI ("");
1155 if Lib_Final_Built then
1156 Set_String (" Finalize_Library_Objects := ");
1157 Set_String ("finalize_library'access;");
1158 else
1159 Set_String (" Finalize_Library_Objects := null;");
1160 end if;
1162 Write_Statement_Buffer;
1163 end if;
1165 -- Generate elaboration calls
1167 if not CodePeer_Mode then
1168 WBI ("");
1169 end if;
1171 Gen_CUDA_Init;
1173 Gen_Elab_Calls (Elab_Order);
1175 if not CodePeer_Mode then
1177 -- Call System.Elaboration_Allocators.Mark_Start_Of_Elaboration if
1178 -- restriction No_Standard_Allocators_After_Elaboration is active.
1180 if Cumulative_Restrictions.Set
1181 (No_Standard_Allocators_After_Elaboration)
1182 then
1184 (" System.Elaboration_Allocators.Mark_End_Of_Elaboration;");
1185 end if;
1187 -- From this point, no new dispatching domain can be created
1189 if Dispatching_Domains_Used then
1190 WBI (" Freeze_Dispatching_Domains;");
1191 end if;
1193 -- Sequential partition elaboration policy
1195 if Partition_Elaboration_Policy_Specified = 'S' then
1196 if System_Interrupts_Used then
1197 WBI (" Install_Restricted_Handlers_Sequential;");
1198 end if;
1200 if System_Tasking_Restricted_Stages_Used then
1201 WBI (" Activate_All_Tasks_Sequential;");
1202 end if;
1203 end if;
1205 if System_BB_CPU_Primitives_Multiprocessors_Used then
1206 WBI (" Start_Slave_CPUs;");
1207 end if;
1208 end if;
1210 WBI (" end " & Ada_Init_Name.all & ";");
1211 WBI ("");
1212 end Gen_Adainit;
1214 -------------------------
1215 -- Gen_Bind_Env_String --
1216 -------------------------
1218 procedure Gen_Bind_Env_String is
1219 procedure Write_Name_With_Len (Nam : Name_Id);
1220 -- Write Nam as a string literal, prefixed with one
1221 -- character encoding Nam's length.
1223 -------------------------
1224 -- Write_Name_With_Len --
1225 -------------------------
1227 procedure Write_Name_With_Len (Nam : Name_Id) is
1228 begin
1229 Get_Name_String (Nam);
1230 Write_Str ("Character'Val (");
1231 Write_Int (Int (Name_Len));
1232 Write_Str (") & """);
1233 Write_Str (Name_Buffer (1 .. Name_Len));
1234 Write_Char ('"');
1235 end Write_Name_With_Len;
1237 -- Local variables
1239 First : Boolean := True;
1240 KN : Name_Id := No_Name;
1241 VN : Name_Id := No_Name;
1243 -- Start of processing for Gen_Bind_Env_String
1245 begin
1246 Bind_Environment.Get_First (KN, VN);
1248 if VN = No_Name then
1249 return;
1250 end if;
1252 Set_Special_Output (Write_Bind_Line'Access);
1254 WBI (" Bind_Env : aliased constant String :=");
1256 while VN /= No_Name loop
1257 if First then
1258 Write_Str (" ");
1259 else
1260 Write_Str (" & ");
1261 end if;
1263 Write_Name_With_Len (KN);
1264 Write_Str (" & ");
1265 Write_Name_With_Len (VN);
1266 Write_Eol;
1268 Bind_Environment.Get_Next (KN, VN);
1269 First := False;
1270 end loop;
1272 WBI (" & ASCII.NUL;");
1274 Cancel_Special_Output;
1275 Bind_Env_String_Built := True;
1276 end Gen_Bind_Env_String;
1278 -------------------
1279 -- Gen_CUDA_Defs --
1280 -------------------
1282 procedure Gen_CUDA_Defs is
1283 Unit_Name : constant String :=
1284 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1285 Unit : constant String :=
1286 Unit_Name (Unit_Name'First .. Unit_Name'Last - 2);
1287 begin
1288 if not Enable_CUDA_Expansion then
1289 return;
1290 end if;
1292 WBI ("");
1293 WBI (" ");
1295 WBI (" procedure CUDA_Register_Function");
1296 WBI (" (Fat_Binary_Handle : System.Address;");
1297 WBI (" Func : System.Address;");
1298 WBI (" Kernel_Name : Interfaces.C.Strings.chars_ptr;");
1299 WBI (" Kernel_Name_2 : Interfaces.C.Strings.chars_ptr;");
1300 WBI (" Minus_One : Integer;");
1301 WBI (" Nullptr1 : System.Address;");
1302 WBI (" Nullptr2 : System.Address;");
1303 WBI (" Nullptr3 : System.Address;");
1304 WBI (" Nullptr4 : System.Address;");
1305 WBI (" Nullptr5 : System.Address);");
1306 WBI (" pragma Import");
1307 WBI (" (Convention => C,");
1308 WBI (" Entity => CUDA_Register_Function,");
1309 WBI (" External_Name => ""__cudaRegisterFunction"");");
1310 WBI ("");
1311 WBI (" function CUDA_Register_Fat_Binary");
1312 WBI (" (Fat_Binary : System.Address)");
1313 WBI (" return System.Address;");
1314 WBI (" pragma Import");
1315 WBI (" (Convention => C,");
1316 WBI (" Entity => CUDA_Register_Fat_Binary,");
1317 WBI (" External_Name => ""__cudaRegisterFatBinary"");");
1318 WBI ("");
1319 WBI (" procedure CUDA_Register_Fat_Binary_End");
1320 WBI (" (Fat_Binary : System.Address);");
1321 WBI (" pragma Import");
1322 WBI (" (Convention => C,");
1323 WBI (" Entity => CUDA_Register_Fat_Binary_End,");
1324 WBI (" External_Name => ""__cudaRegisterFatBinaryEnd"");");
1325 WBI ("");
1326 WBI (" type Fatbin_Wrapper is record");
1327 WBI (" Magic : Interfaces.C.int;");
1328 WBI (" Version : Interfaces.C.int;");
1329 WBI (" Data : System.Address;");
1330 WBI (" Filename_Or_Fatbins : System.Address;");
1331 WBI (" end record;");
1332 WBI ("");
1333 WBI (" Fat_Binary : System.Address;");
1334 WBI (" pragma Import");
1335 WBI (" (Convention => C,");
1336 WBI (" Entity => Fat_Binary,");
1337 WBI (" External_Name => ""_binary_" & Unit & "_fatbin_start"");");
1338 WBI ("");
1339 WBI (" Wrapper : Fatbin_Wrapper :=");
1340 WBI (" (16#466243b1#,");
1341 WBI (" 1,");
1342 WBI (" Fat_Binary'Address,");
1343 WBI (" System.Null_Address);");
1344 WBI ("");
1345 WBI (" Fat_Binary_Handle : System.Address;");
1346 WBI ("");
1348 for K in CUDA_Kernels.First .. CUDA_Kernels.Last loop
1349 declare
1350 K_String : constant String := CUDA_Kernel_Id'Image (K);
1351 N : constant String :=
1352 K_String (K_String'First + 1 .. K_String'Last);
1353 Kernel_Symbol : constant String := "Kernel_" & N;
1354 -- K_Symbol is a unique identifier used to derive all symbol names
1355 -- related to kernel K.
1357 Kernel_Proc : constant String := Kernel_Symbol & "_Proc";
1358 -- Kernel_Proc is the name of the symbol representing the
1359 -- host-side procedure of the kernel. The address is
1360 -- pragma-imported and then used while registering the kernel with
1361 -- the CUDA runtime.
1362 Kernel_String : constant String := Kernel_Symbol & "_String";
1363 -- Kernel_String is the name of the C-string containing the name
1364 -- of the kernel. It is used for registering the kernel with the
1365 -- CUDA runtime.
1366 Kernel_Name : constant String :=
1367 Get_Name_String (CUDA_Kernels.Table (K).Kernel_Name);
1368 -- Kernel_Name is the name of the kernel, after package expansion.
1370 begin
1371 -- Import host-side kernel address.
1372 WBI (" procedure " & Kernel_Proc & ";");
1373 WBI (" pragma Import");
1374 WBI (" (Convention => C,");
1375 WBI (" Entity => " & Kernel_Proc & ",");
1376 WBI (" External_Name => """ & Kernel_Name & """);");
1377 WBI ("");
1379 -- Generate C-string containing name of kernel.
1381 (" " & Kernel_String & " : Interfaces.C.Strings.Chars_Ptr;");
1382 WBI ("");
1384 end;
1385 end loop;
1387 WBI (" procedure " & Device_Ada_Init_Subp_Name & ";");
1388 WBI (" pragma Export (C, " & Device_Ada_Init_Subp_Name &
1389 ", Link_Name => """ & Device_Ada_Init_Link_Name & """);");
1391 -- C-string declaration for adainit
1392 WBI (" " & Adainit_String_Obj_Name
1393 & " : Interfaces.C.Strings.Chars_Ptr;");
1394 WBI ("");
1396 WBI ("");
1397 end Gen_CUDA_Defs;
1399 -------------------
1400 -- Gen_CUDA_Init --
1401 -------------------
1403 procedure Gen_CUDA_Init is
1404 -- Generate call to register one function
1405 procedure Gen_CUDA_Register_Function_Call
1406 (Kernel_Name : String;
1407 Kernel_String : String;
1408 Kernel_Proc : String);
1410 -------------------------------------
1411 -- Gen_CUDA_Register_Function_Call --
1412 -------------------------------------
1414 procedure Gen_CUDA_Register_Function_Call
1415 (Kernel_Name : String;
1416 Kernel_String : String;
1417 Kernel_Proc : String) is
1418 begin
1419 WBI (" " & Kernel_String & " :=");
1420 WBI (" Interfaces.C.Strings.New_Char_Array ("""
1421 & Kernel_Name
1422 & """);");
1424 -- Generate call to CUDA runtime to register function.
1425 WBI (" CUDA_Register_Function (");
1426 WBI (" Fat_Binary_Handle, ");
1427 WBI (" " & Kernel_Proc & "'Address,");
1428 WBI (" " & Kernel_String & ",");
1429 WBI (" " & Kernel_String & ",");
1430 WBI (" -1,");
1431 WBI (" System.Null_Address,");
1432 WBI (" System.Null_Address,");
1433 WBI (" System.Null_Address,");
1434 WBI (" System.Null_Address,");
1435 WBI (" System.Null_Address);");
1436 WBI ("");
1437 end Gen_CUDA_Register_Function_Call;
1439 begin
1440 if not Enable_CUDA_Expansion then
1441 return;
1442 end if;
1444 WBI (" Fat_Binary_Handle :=");
1445 WBI (" CUDA_Register_Fat_Binary (Wrapper'Address);");
1447 for K in CUDA_Kernels.First .. CUDA_Kernels.Last loop
1448 declare
1449 K_String : constant String := CUDA_Kernel_Id'Image (K);
1450 N : constant String :=
1451 K_String (K_String'First + 1 .. K_String'Last);
1452 Kernel_Symbol : constant String := "Kernel_" & N;
1453 -- K_Symbol is a unique identifier used to derive all symbol names
1454 -- related to kernel K.
1456 Kernel_Proc : constant String := Kernel_Symbol & "_Proc";
1457 -- Kernel_Proc is the name of the symbol representing the
1458 -- host-side procedure of the kernel. The address is
1459 -- pragma-imported and then used while registering the kernel with
1460 -- the CUDA runtime.
1461 Kernel_String : constant String := Kernel_Symbol & "_String";
1462 -- Kernel_String is the name of the C-string containing the name
1463 -- of the kernel. It is used for registering the kernel with the
1464 -- CUDA runtime.
1465 Kernel_Name : constant String :=
1466 Get_Name_String (CUDA_Kernels.Table (K).Kernel_Name);
1467 -- Kernel_Name is the name of the kernel, after package expansion.
1468 begin
1469 Gen_CUDA_Register_Function_Call
1470 (Kernel_Name => Kernel_Name,
1471 Kernel_String => Kernel_String,
1472 Kernel_Proc => Kernel_Proc);
1473 end;
1474 end loop;
1476 -- Register device-side Adainit
1477 Gen_CUDA_Register_Function_Call
1478 (Kernel_Name => Device_Ada_Init_Link_Name,
1479 Kernel_String => Adainit_String_Obj_Name,
1480 Kernel_Proc => Device_Ada_Init_Subp_Name);
1482 WBI (" CUDA_Register_Fat_Binary_End (Fat_Binary_Handle);");
1484 -- perform device (as opposed to host) elaboration
1485 WBI (" pragma CUDA_Execute (" &
1486 Device_Ada_Init_Subp_Name & ", 1, 1);");
1487 end Gen_CUDA_Init;
1489 --------------------------
1490 -- Gen_CodePeer_Wrapper --
1491 --------------------------
1493 procedure Gen_CodePeer_Wrapper is
1494 Callee_Name : constant String := "Ada_Main_Program";
1496 begin
1497 if ALIs.Table (ALIs.First).Main_Program = Proc then
1498 WBI (" procedure " & CodePeer_Wrapper_Name & " is ");
1499 WBI (" begin");
1500 WBI (" " & Callee_Name & ";");
1502 else
1503 WBI (" function " & CodePeer_Wrapper_Name & " return Integer is");
1504 WBI (" begin");
1505 WBI (" return " & Callee_Name & ";");
1506 end if;
1508 WBI (" end " & CodePeer_Wrapper_Name & ";");
1509 WBI ("");
1510 end Gen_CodePeer_Wrapper;
1512 --------------------
1513 -- Gen_Elab_Calls --
1514 --------------------
1516 procedure Gen_Elab_Calls (Elab_Order : Unit_Id_Array) is
1517 Check_Elab_Flag : Boolean;
1519 begin
1520 -- Loop through elaboration order entries
1522 for E in Elab_Order'Range loop
1523 declare
1524 Unum : constant Unit_Id := Elab_Order (E);
1525 U : Unit_Record renames Units.Table (Unum);
1527 Unum_Spec : Unit_Id;
1528 -- This is the unit number of the spec that corresponds to
1529 -- this entry. It is the same as Unum except when the body
1530 -- and spec are different and we are currently processing
1531 -- the body, in which case it is the spec (Unum + 1).
1533 begin
1534 if U.Utype = Is_Body then
1535 Unum_Spec := Unum + 1;
1536 else
1537 Unum_Spec := Unum;
1538 end if;
1540 -- Nothing to do if predefined unit in no run time mode
1542 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1543 null;
1545 -- Likewise if this is an interface to a stand alone library
1547 elsif U.SAL_Interface then
1548 null;
1550 -- Case of no elaboration code
1552 elsif U.No_Elab
1554 -- In CodePeer mode, we special case subprogram bodies which
1555 -- are handled in the 'else' part below, and lead to a call
1556 -- to <subp>'Elab_Subp_Body.
1558 and then (not CodePeer_Mode
1560 -- Test for spec
1562 or else U.Utype = Is_Spec
1563 or else U.Utype = Is_Spec_Only
1564 or else U.Unit_Kind /= 's')
1565 then
1566 -- In the case of a body with a separate spec, where the
1567 -- separate spec has an elaboration entity defined, this is
1568 -- where we increment the elaboration entity if one exists.
1570 -- Likewise for lone specs with an elaboration entity defined
1571 -- despite No_Elaboration_Code, e.g. when requested to preserve
1572 -- control flow.
1574 if (U.Utype = Is_Body or else U.Utype = Is_Spec_Only)
1575 and then Units.Table (Unum_Spec).Set_Elab_Entity
1576 and then not CodePeer_Mode
1577 then
1578 Set_String (" E");
1579 Set_Unit_Number (Unum_Spec);
1580 Set_String (" := E");
1581 Set_Unit_Number (Unum_Spec);
1582 Set_String (" + 1;");
1583 Write_Statement_Buffer;
1584 end if;
1586 -- Here if elaboration code is present. If binding a library
1587 -- or if there is a non-Ada main subprogram then we generate:
1589 -- if uname_E = 0 then
1590 -- uname'elab_[spec|body];
1591 -- end if;
1592 -- uname_E := uname_E + 1;
1594 -- Otherwise, elaboration routines are called unconditionally:
1596 -- uname'elab_[spec|body];
1597 -- uname_E := uname_E + 1;
1599 -- The uname_E increment is skipped if this is a separate spec,
1600 -- since it will be done when we process the body.
1602 -- In CodePeer mode, we do not generate any reference to xxx_E
1603 -- variables, only calls to 'Elab* subprograms.
1605 else
1606 -- Check incompatibilities with No_Multiple_Elaboration
1608 if not CodePeer_Mode
1609 and then Cumulative_Restrictions.Set (No_Multiple_Elaboration)
1610 then
1611 -- Force_Checking_Of_Elaboration_Flags (-F) not allowed
1613 if Force_Checking_Of_Elaboration_Flags then
1614 Osint.Fail
1615 ("-F (force elaboration checks) switch not allowed "
1616 & "with restriction No_Multiple_Elaboration active");
1618 -- Interfacing of libraries not allowed
1620 elsif Interface_Library_Unit then
1621 Osint.Fail
1622 ("binding of interfaced libraries not allowed "
1623 & "with restriction No_Multiple_Elaboration active");
1625 -- Non-Ada main program not allowed
1627 elsif not Bind_Main_Program then
1628 Osint.Fail
1629 ("non-Ada main program not allowed "
1630 & "with restriction No_Multiple_Elaboration active");
1631 end if;
1632 end if;
1634 -- OK, see if we need to test elaboration flag
1636 Check_Elab_Flag :=
1637 Units.Table (Unum_Spec).Set_Elab_Entity
1638 and then Check_Elaboration_Flags
1639 and then not CodePeer_Mode
1640 and then (Force_Checking_Of_Elaboration_Flags
1641 or Interface_Library_Unit
1642 or not Bind_Main_Program);
1644 if Check_Elab_Flag then
1645 Set_String (" if E");
1646 Set_Unit_Number (Unum_Spec);
1647 Set_String (" = 0 then");
1648 Write_Statement_Buffer;
1649 Set_String (" ");
1650 end if;
1652 Set_String (" ");
1653 Get_Decoded_Name_String_With_Brackets (U.Uname);
1655 if Name_Buffer (Name_Len) = 's' then
1656 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1657 "'elab_spec";
1658 Name_Len := Name_Len + 8;
1660 -- Special case in CodePeer mode for subprogram bodies
1661 -- which correspond to CodePeer 'Elab_Subp_Body special
1662 -- init procedure.
1664 elsif U.Unit_Kind = 's' and CodePeer_Mode then
1665 Name_Buffer (Name_Len - 1 .. Name_Len + 13) :=
1666 "'elab_subp_body";
1667 Name_Len := Name_Len + 13;
1669 else
1670 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1671 "'elab_body";
1672 Name_Len := Name_Len + 8;
1673 end if;
1675 Set_Casing (U.Icasing);
1676 Set_Name_Buffer;
1677 Set_Char (';');
1678 Write_Statement_Buffer;
1680 if Check_Elab_Flag then
1681 WBI (" end if;");
1682 end if;
1684 if U.Utype /= Is_Spec
1685 and then not CodePeer_Mode
1686 and then Units.Table (Unum_Spec).Set_Elab_Entity
1687 then
1688 Set_String (" E");
1689 Set_Unit_Number (Unum_Spec);
1690 Set_String (" := E");
1691 Set_Unit_Number (Unum_Spec);
1692 Set_String (" + 1;");
1693 Write_Statement_Buffer;
1694 end if;
1695 end if;
1696 end;
1697 end loop;
1698 end Gen_Elab_Calls;
1700 ------------------------
1701 -- Gen_Elab_Externals --
1702 ------------------------
1704 procedure Gen_Elab_Externals (Elab_Order : Unit_Id_Array) is
1705 begin
1706 if CodePeer_Mode then
1707 return;
1708 end if;
1710 for E in Elab_Order'Range loop
1711 declare
1712 Unum : constant Unit_Id := Elab_Order (E);
1713 U : Unit_Record renames Units.Table (Unum);
1715 begin
1716 -- Check for Elab_Entity to be set for this unit
1718 if U.Set_Elab_Entity
1720 -- Don't generate reference for stand alone library
1722 and then not U.SAL_Interface
1724 -- Don't generate reference for predefined file in No_Run_Time
1725 -- mode, since we don't include the object files in this case
1727 and then not
1728 (No_Run_Time_Mode
1729 and then Is_Predefined_File_Name (U.Sfile))
1730 then
1731 Get_Name_String (U.Sfile);
1732 Set_String (" ");
1733 Set_String ("E");
1734 Set_Unit_Number (Unum);
1735 Set_String (" : Short_Integer; pragma Import (Ada, E");
1736 Set_Unit_Number (Unum);
1737 Set_String (", """);
1738 Get_Name_String (U.Uname);
1739 Set_Unit_Name;
1740 Set_String ("_E"");");
1741 Write_Statement_Buffer;
1742 end if;
1743 end;
1744 end loop;
1746 WBI ("");
1747 end Gen_Elab_Externals;
1749 --------------------
1750 -- Gen_Elab_Order --
1751 --------------------
1753 procedure Gen_Elab_Order (Elab_Order : Unit_Id_Array) is
1754 begin
1755 WBI ("");
1756 WBI (" -- BEGIN ELABORATION ORDER");
1758 for J in Elab_Order'Range loop
1759 Set_String (" -- ");
1760 Get_Name_String (Units.Table (Elab_Order (J)).Uname);
1761 Set_Name_Buffer;
1762 Write_Statement_Buffer;
1763 end loop;
1765 WBI (" -- END ELABORATION ORDER");
1766 end Gen_Elab_Order;
1768 --------------------------
1769 -- Gen_Finalize_Library --
1770 --------------------------
1772 procedure Gen_Finalize_Library (Elab_Order : Unit_Id_Array) is
1773 procedure Gen_Header;
1774 -- Generate the header of the finalization routine
1776 ----------------
1777 -- Gen_Header --
1778 ----------------
1780 procedure Gen_Header is
1781 begin
1782 WBI (" procedure finalize_library is");
1783 WBI (" begin");
1784 end Gen_Header;
1786 -- Local variables
1788 Count : Int := 1;
1789 U : Unit_Record;
1790 Uspec : Unit_Record;
1791 Unum : Unit_Id;
1793 -- Start of processing for Gen_Finalize_Library
1795 begin
1796 if CodePeer_Mode then
1797 return;
1798 end if;
1800 for E in reverse Elab_Order'Range loop
1801 Unum := Elab_Order (E);
1802 U := Units.Table (Unum);
1804 -- Dealing with package bodies is a little complicated. In such
1805 -- cases we must retrieve the package spec since it contains the
1806 -- spec of the body finalizer.
1808 if U.Utype = Is_Body then
1809 Unum := Unum + 1;
1810 Uspec := Units.Table (Unum);
1811 else
1812 Uspec := U;
1813 end if;
1815 Get_Name_String (Uspec.Uname);
1817 -- We are only interested in non-generic packages
1819 if U.Unit_Kind /= 'p' or else U.Is_Generic then
1820 null;
1822 -- That aren't an interface to a stand alone library
1824 elsif U.SAL_Interface then
1825 null;
1827 -- Case of no finalization
1829 elsif not U.Has_Finalizer then
1831 -- The only case in which we have to do something is if this
1832 -- is a body, with a separate spec, where the separate spec
1833 -- has a finalizer. In that case, this is where we decrement
1834 -- the elaboration entity.
1836 if U.Utype = Is_Body and then Uspec.Has_Finalizer then
1837 if not Lib_Final_Built then
1838 Gen_Header;
1839 Lib_Final_Built := True;
1840 end if;
1842 Set_String (" E");
1843 Set_Unit_Number (Unum);
1844 Set_String (" := E");
1845 Set_Unit_Number (Unum);
1846 Set_String (" - 1;");
1847 Write_Statement_Buffer;
1848 end if;
1850 else
1851 if not Lib_Final_Built then
1852 Gen_Header;
1853 Lib_Final_Built := True;
1854 end if;
1856 -- Generate:
1857 -- declare
1858 -- procedure F<Count>;
1860 Set_String (" declare");
1861 Write_Statement_Buffer;
1863 Set_String (" procedure F");
1864 Set_Int (Count);
1865 Set_Char (';');
1866 Write_Statement_Buffer;
1868 -- Generate:
1869 -- pragma Import (Ada, F<Count>,
1870 -- "xx__yy__finalize_[body|spec]");
1872 Set_String (" pragma Import (Ada, F");
1873 Set_Int (Count);
1874 Set_String (", """);
1876 -- Perform name construction
1878 Set_Unit_Name;
1879 Set_String ("__finalize_");
1881 -- Package spec processing
1883 if U.Utype = Is_Spec
1884 or else U.Utype = Is_Spec_Only
1885 then
1886 Set_String ("spec");
1888 -- Package body processing
1890 else
1891 Set_String ("body");
1892 end if;
1894 Set_String (""");");
1895 Write_Statement_Buffer;
1897 -- If binding a library or if there is a non-Ada main subprogram
1898 -- then we generate:
1900 -- begin
1901 -- uname_E := uname_E - 1;
1902 -- if uname_E = 0 then
1903 -- F<Count>;
1904 -- end if;
1905 -- end;
1907 -- Otherwise, finalization routines are called unconditionally:
1909 -- begin
1910 -- uname_E := uname_E - 1;
1911 -- F<Count>;
1912 -- end;
1914 -- The uname_E decrement is skipped if this is a separate spec,
1915 -- since it will be done when we process the body.
1917 WBI (" begin");
1919 if U.Utype /= Is_Spec then
1920 Set_String (" E");
1921 Set_Unit_Number (Unum);
1922 Set_String (" := E");
1923 Set_Unit_Number (Unum);
1924 Set_String (" - 1;");
1925 Write_Statement_Buffer;
1926 end if;
1928 if Interface_Library_Unit or not Bind_Main_Program then
1929 Set_String (" if E");
1930 Set_Unit_Number (Unum);
1931 Set_String (" = 0 then");
1932 Write_Statement_Buffer;
1933 Set_String (" ");
1934 end if;
1936 Set_String (" F");
1937 Set_Int (Count);
1938 Set_Char (';');
1939 Write_Statement_Buffer;
1941 if Interface_Library_Unit or not Bind_Main_Program then
1942 WBI (" end if;");
1943 end if;
1945 WBI (" end;");
1947 Count := Count + 1;
1948 end if;
1949 end loop;
1951 if Lib_Final_Built then
1953 -- It is possible that the finalization of a library-level object
1954 -- raised an exception. In that case import the actual exception
1955 -- and the routine necessary to raise it.
1957 WBI (" declare");
1958 WBI (" procedure Reraise_Library_Exception_If_Any;");
1960 Set_String (" pragma Import (Ada, ");
1961 Set_String ("Reraise_Library_Exception_If_Any, ");
1962 Set_String ("""__gnat_reraise_library_exception_if_any"");");
1963 Write_Statement_Buffer;
1965 WBI (" begin");
1966 WBI (" Reraise_Library_Exception_If_Any;");
1967 WBI (" end;");
1968 WBI (" end finalize_library;");
1969 WBI ("");
1970 end if;
1971 end Gen_Finalize_Library;
1973 --------------
1974 -- Gen_Main --
1975 --------------
1977 procedure Gen_Main is
1978 begin
1979 if not No_Main_Subprogram then
1981 -- To call the main program, we declare it using a pragma Import
1982 -- Ada with the right link name.
1984 -- It might seem more obvious to "with" the main program, and call
1985 -- it in the normal Ada manner. We do not do this for three
1986 -- reasons:
1988 -- 1. It is more efficient not to recompile the main program
1989 -- 2. We are not entitled to assume the source is accessible
1990 -- 3. We don't know what options to use to compile it
1992 -- It is really reason 3 that is most critical (indeed we used
1993 -- to generate the "with", but several regression tests failed).
1995 if ALIs.Table (ALIs.First).Main_Program = Func then
1996 WBI (" function Ada_Main_Program return Integer;");
1997 else
1998 WBI (" procedure Ada_Main_Program;");
1999 end if;
2001 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
2002 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2003 Set_Main_Program_Name;
2004 Set_String (""");");
2006 Write_Statement_Buffer;
2007 WBI ("");
2009 -- For CodePeer, declare a wrapper for the user-defined main program
2011 if CodePeer_Mode then
2012 Gen_CodePeer_Wrapper;
2013 end if;
2014 end if;
2016 if Exit_Status_Supported_On_Target then
2017 Set_String (" function ");
2018 else
2019 Set_String (" procedure ");
2020 end if;
2022 Set_String (Get_Main_Name);
2024 if Command_Line_Args_On_Target then
2025 Write_Statement_Buffer;
2026 WBI (" (argc : Integer;");
2027 WBI (" argv : System.Address;");
2028 WBI (" envp : System.Address)");
2030 if Exit_Status_Supported_On_Target then
2031 WBI (" return Integer");
2032 end if;
2034 WBI (" is");
2036 else
2037 if Exit_Status_Supported_On_Target then
2038 Set_String (" return Integer is");
2039 else
2040 Set_String (" is");
2041 end if;
2043 Write_Statement_Buffer;
2044 end if;
2046 if Opt.Default_Exit_Status /= 0
2047 and then Bind_Main_Program
2048 and then not Configurable_Run_Time_Mode
2049 then
2050 WBI (" procedure Set_Exit_Status (Status : Integer);");
2051 WBI (" pragma Import (C, Set_Exit_Status, " &
2052 """__gnat_set_exit_status"");");
2053 WBI ("");
2054 end if;
2056 -- Initialize and Finalize
2058 if not CodePeer_Mode
2059 and then not Cumulative_Restrictions.Set (No_Finalization)
2060 then
2061 WBI (" procedure Initialize (Addr : System.Address);");
2062 WBI (" pragma Import (C, Initialize, ""__gnat_initialize"");");
2063 WBI ("");
2064 WBI (" procedure Finalize;");
2065 WBI (" pragma Import (C, Finalize, ""__gnat_finalize"");");
2066 end if;
2068 -- If we want to analyze the stack, we must import corresponding symbols
2070 if Dynamic_Stack_Measurement then
2071 WBI ("");
2072 WBI (" procedure Output_Results;");
2073 WBI (" pragma Import (C, Output_Results, " &
2074 """__gnat_stack_usage_output_results"");");
2076 WBI ("");
2077 WBI (" " &
2078 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
2079 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
2080 """__gnat_stack_usage_initialize"");");
2081 end if;
2083 -- Deal with declarations for main program case
2085 if not No_Main_Subprogram then
2086 if ALIs.Table (ALIs.First).Main_Program = Func then
2087 WBI (" Result : Integer;");
2088 WBI ("");
2089 end if;
2091 if Bind_Main_Program
2092 and not Suppress_Standard_Library_On_Target
2093 and not CodePeer_Mode
2094 then
2095 WBI (" SEH : aliased array (1 .. 2) of Integer;");
2096 WBI ("");
2097 end if;
2098 end if;
2100 -- Generate a reference to Ada_Main_Program_Name. This symbol is not
2101 -- referenced elsewhere in the generated program, but is needed by
2102 -- the debugger (that's why it is generated in the first place). The
2103 -- reference stops Ada_Main_Program_Name from being optimized away by
2104 -- smart linkers.
2106 -- Because this variable is unused, we make this variable "aliased"
2107 -- with a pragma Volatile in order to tell the compiler to preserve
2108 -- this variable at any level of optimization.
2110 -- CodePeer and CCG do not need this extra code. The code is also not
2111 -- needed if the binder is in "Minimal Binder" mode.
2113 if Bind_Main_Program
2114 and then not Minimal_Binder
2115 and then not CodePeer_Mode
2116 and then not Generate_C_Code
2117 then
2118 WBI (" Ensure_Reference : aliased System.Address := " &
2119 "Ada_Main_Program_Name'Address;");
2120 WBI (" pragma Volatile (Ensure_Reference);");
2121 WBI ("");
2122 end if;
2124 WBI (" begin");
2126 -- Acquire command-line arguments if supported on the target and used
2127 -- by the program.
2129 if CodePeer_Mode then
2130 null;
2132 elsif Command_Line_Args_On_Target and then Command_Line_Used then
2133 -- Initialize gnat_argc/gnat_argv only if not already initialized,
2134 -- to avoid losing the result of any command-line processing done by
2135 -- earlier GNAT run-time initialization.
2137 WBI (" if gnat_argc = 0 then");
2138 WBI (" gnat_argc := argc;");
2139 WBI (" gnat_argv := argv;");
2140 WBI (" end if;");
2141 WBI (" gnat_envp := envp;");
2142 WBI ("");
2143 end if;
2145 if Opt.Default_Exit_Status /= 0
2146 and then Bind_Main_Program
2147 and then not Configurable_Run_Time_Mode
2148 then
2149 Set_String (" Set_Exit_Status (");
2150 Set_Int (Opt.Default_Exit_Status);
2151 Set_String (");");
2152 Write_Statement_Buffer;
2153 end if;
2155 if Dynamic_Stack_Measurement then
2156 Set_String (" Initialize_Stack_Analysis (");
2157 Set_Int (Dynamic_Stack_Measurement_Array_Size);
2158 Set_String (");");
2159 Write_Statement_Buffer;
2160 end if;
2162 if not Cumulative_Restrictions.Set (No_Finalization)
2163 and then not CodePeer_Mode
2164 then
2165 if not No_Main_Subprogram
2166 and then Bind_Main_Program
2167 and then not Suppress_Standard_Library_On_Target
2168 then
2169 WBI (" Initialize (SEH'Address);");
2170 else
2171 WBI (" Initialize (System.Null_Address);");
2172 end if;
2173 end if;
2175 WBI (" " & Ada_Init_Name.all & ";");
2177 if not No_Main_Subprogram then
2178 if CodePeer_Mode then
2179 if ALIs.Table (ALIs.First).Main_Program = Proc then
2180 WBI (" " & CodePeer_Wrapper_Name & ";");
2181 else
2182 WBI (" Result := " & CodePeer_Wrapper_Name & ";");
2183 end if;
2185 elsif ALIs.Table (ALIs.First).Main_Program = Proc then
2186 WBI (" Ada_Main_Program;");
2188 else
2189 WBI (" Result := Ada_Main_Program;");
2190 end if;
2191 end if;
2193 -- Adafinal call is skipped if no finalization
2195 if not Cumulative_Restrictions.Set (No_Finalization) then
2196 WBI (" adafinal;");
2197 end if;
2199 -- Prints the result of static stack analysis
2201 if Dynamic_Stack_Measurement then
2202 WBI (" Output_Results;");
2203 end if;
2205 -- Finalize is only called if we have a run time
2207 if not Cumulative_Restrictions.Set (No_Finalization)
2208 and then not CodePeer_Mode
2209 then
2210 WBI (" Finalize;");
2211 end if;
2213 -- Return result
2215 if Exit_Status_Supported_On_Target then
2216 if No_Main_Subprogram
2217 or else ALIs.Table (ALIs.First).Main_Program = Proc
2218 then
2219 -- Return gnat_exit_status if Ada.Command_Line is used otherwise
2220 -- return 0.
2222 if Command_Line_Used then
2223 WBI (" return (gnat_exit_status);");
2224 else
2225 WBI (" return (0);");
2226 end if;
2227 else
2228 WBI (" return (Result);");
2229 end if;
2230 end if;
2232 WBI (" end;");
2233 WBI ("");
2234 end Gen_Main;
2236 ------------------------------
2237 -- Gen_Object_Files_Options --
2238 ------------------------------
2240 procedure Gen_Object_Files_Options (Elab_Order : Unit_Id_Array) is
2241 Lgnat : Natural;
2242 -- This keeps track of the position in the sorted set of entries in the
2243 -- Linker_Options table of where the first entry from an internal file
2244 -- appears.
2246 Linker_Option_List_Started : Boolean := False;
2247 -- Set to True when "LINKER OPTION LIST" is displayed
2249 procedure Write_Linker_Option;
2250 -- Write binder info linker option
2252 -------------------------
2253 -- Write_Linker_Option --
2254 -------------------------
2256 procedure Write_Linker_Option is
2257 Start : Natural;
2258 Stop : Natural;
2260 begin
2261 -- Loop through string, breaking at null's
2263 Start := 1;
2264 while Start < Name_Len loop
2266 -- Find null ending this section
2268 Stop := Start + 1;
2269 while Name_Buffer (Stop) /= ASCII.NUL
2270 and then Stop <= Name_Len loop
2271 Stop := Stop + 1;
2272 end loop;
2274 -- Process section if non-null
2276 if Stop > Start then
2277 if Output_Linker_Option_List then
2278 if not Zero_Formatting then
2279 if not Linker_Option_List_Started then
2280 Linker_Option_List_Started := True;
2281 Write_Eol;
2282 Write_Str (" LINKER OPTION LIST");
2283 Write_Eol;
2284 Write_Eol;
2285 end if;
2287 Write_Str (" ");
2288 end if;
2290 Write_Str (Name_Buffer (Start .. Stop - 1));
2291 Write_Eol;
2292 end if;
2293 WBI (" -- " & Name_Buffer (Start .. Stop - 1));
2294 end if;
2296 Start := Stop + 1;
2297 end loop;
2298 end Write_Linker_Option;
2300 -- Start of processing for Gen_Object_Files_Options
2302 begin
2303 WBI ("-- BEGIN Object file/option list");
2305 if Object_List_Filename /= null then
2306 Set_List_File (Object_List_Filename.all);
2307 end if;
2309 for E in Elab_Order'Range loop
2311 -- If not spec that has an associated body, then generate a comment
2312 -- giving the name of the corresponding object file.
2314 if not Units.Table (Elab_Order (E)).SAL_Interface
2315 and then Units.Table (Elab_Order (E)).Utype /= Is_Spec
2316 then
2317 Get_Name_String
2318 (ALIs.Table
2319 (Units.Table (Elab_Order (E)).My_ALI).Ofile_Full_Name);
2321 -- If the presence of an object file is necessary or if it exists,
2322 -- then use it.
2324 if not Hostparm.Exclude_Missing_Objects
2325 or else
2326 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
2327 then
2328 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2330 if Output_Object_List then
2331 Write_Str (Name_Buffer (1 .. Name_Len));
2332 Write_Eol;
2333 end if;
2334 end if;
2335 end if;
2336 end loop;
2338 if Object_List_Filename /= null then
2339 Close_List_File;
2340 end if;
2342 -- Add a "-Ldir" for each directory in the object path
2344 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
2345 declare
2346 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
2348 begin
2349 Name_Len := 0;
2350 Add_Str_To_Name_Buffer ("-L");
2351 Add_Str_To_Name_Buffer (Dir.all);
2352 Write_Linker_Option;
2353 end;
2354 end loop;
2356 if not (Opt.No_Run_Time_Mode or Opt.No_Stdlib) then
2357 Name_Len := 0;
2359 if Opt.Shared_Libgnat then
2360 Add_Str_To_Name_Buffer ("-shared");
2361 else
2362 Add_Str_To_Name_Buffer ("-static");
2363 end if;
2365 -- Write directly to avoid inclusion in -K output as -static and
2366 -- -shared are not usually specified linker options.
2368 WBI (" -- " & Name_Buffer (1 .. Name_Len));
2369 end if;
2371 -- Sort linker options
2373 -- This sort accomplishes two important purposes:
2375 -- a) All application files are sorted to the front, and all GNAT
2376 -- internal files are sorted to the end. This results in a well
2377 -- defined dividing line between the two sets of files, for the
2378 -- purpose of inserting certain standard library references into
2379 -- the linker arguments list.
2381 -- b) Given two different units, we sort the linker options so that
2382 -- those from a unit earlier in the elaboration order comes later
2383 -- in the list. This is a heuristic designed to create a more
2384 -- friendly order of linker options when the operations appear in
2385 -- separate units. The idea is that if unit A must be elaborated
2386 -- before unit B, then it is more likely that B references
2387 -- libraries included by A, than vice versa, so we want libraries
2388 -- included by A to come after libraries included by B.
2390 -- These two criteria are implemented by function Lt_Linker_Option. Note
2391 -- that a special case of b) is that specs are elaborated before bodies,
2392 -- so linker options from specs come after linker options for bodies,
2393 -- and again, the assumption is that libraries used by the body are more
2394 -- likely to reference libraries used by the spec, than vice versa.
2396 Sort
2397 (Linker_Options.Last,
2398 Move_Linker_Option'Access,
2399 Lt_Linker_Option'Access);
2401 -- Write user linker options, i.e. the set of linker options that come
2402 -- from all files other than GNAT internal files, Lgnat is left set to
2403 -- point to the first entry from a GNAT internal file, or past the end
2404 -- of the entries if there are no internal files.
2406 Lgnat := Linker_Options.Last + 1;
2408 for J in 1 .. Linker_Options.Last loop
2409 if not Linker_Options.Table (J).Internal_File then
2410 Get_Name_String (Linker_Options.Table (J).Name);
2411 Write_Linker_Option;
2412 else
2413 Lgnat := J;
2414 exit;
2415 end if;
2416 end loop;
2418 -- Now we insert standard linker options that must appear after the
2419 -- entries from user files, and before the entries from GNAT run-time
2420 -- files. The reason for this decision is that libraries referenced
2421 -- by internal routines may reference these standard library entries.
2423 -- Note that we do not insert anything when pragma No_Run_Time has
2424 -- been specified or when the standard libraries are not to be used,
2425 -- otherwise on some platforms, we may get duplicate symbols when
2426 -- linking (not clear if this is still the case, but it is harmless).
2428 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2429 if With_GNARL then
2430 Name_Len := 0;
2432 if Opt.Shared_Libgnat then
2433 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2434 else
2435 Add_Str_To_Name_Buffer ("-lgnarl");
2436 end if;
2438 Write_Linker_Option;
2439 end if;
2441 Name_Len := 0;
2443 if Opt.Shared_Libgnat then
2444 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2445 else
2446 Add_Str_To_Name_Buffer ("-lgnat");
2447 end if;
2449 Write_Linker_Option;
2450 end if;
2452 -- Write linker options from all internal files
2454 for J in Lgnat .. Linker_Options.Last loop
2455 Get_Name_String (Linker_Options.Table (J).Name);
2456 Write_Linker_Option;
2457 end loop;
2459 if Output_Linker_Option_List and then not Zero_Formatting then
2460 Write_Eol;
2461 end if;
2463 WBI ("-- END Object file/option list ");
2464 end Gen_Object_Files_Options;
2466 ---------------------
2467 -- Gen_Output_File --
2468 ---------------------
2470 procedure Gen_Output_File
2471 (Filename : String;
2472 Elab_Order : Unit_Id_Array)
2474 begin
2475 -- Acquire settings for Interrupt_State pragmas
2477 Set_IS_Pragma_Table;
2479 -- Acquire settings for Priority_Specific_Dispatching pragma
2481 Set_PSD_Pragma_Table;
2483 -- Override time slice value if -T switch is set
2485 if Time_Slice_Set then
2486 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2487 end if;
2489 -- Count number of elaboration calls
2491 for E in Elab_Order'Range loop
2492 if Units.Table (Elab_Order (E)).No_Elab then
2493 null;
2494 else
2495 Num_Elab_Calls := Num_Elab_Calls + 1;
2496 end if;
2497 end loop;
2499 -- Count the number of statically allocated stacks to be generated by
2500 -- the binder. If the user has specified the number of default-sized
2501 -- secondary stacks, use that number. Otherwise start the count at one
2502 -- as the binder is responsible for creating a secondary stack for the
2503 -- main task.
2505 if Opt.Quantity_Of_Default_Size_Sec_Stacks /= -1 then
2506 Num_Sec_Stacks := Quantity_Of_Default_Size_Sec_Stacks;
2507 elsif Sec_Stack_Used then
2508 Num_Sec_Stacks := 1;
2509 end if;
2511 for J in Units.First .. Units.Last loop
2512 Num_Primary_Stacks :=
2513 Num_Primary_Stacks + Units.Table (J).Primary_Stack_Count;
2515 Num_Sec_Stacks :=
2516 Num_Sec_Stacks + Units.Table (J).Sec_Stack_Count;
2517 end loop;
2519 -- Generate output file in appropriate language
2521 Gen_Output_File_Ada (Filename, Elab_Order);
2522 end Gen_Output_File;
2524 -------------------------
2525 -- Gen_Output_File_Ada --
2526 -------------------------
2528 procedure Gen_Output_File_Ada
2529 (Filename : String; Elab_Order : Unit_Id_Array)
2531 Ada_Main : constant String := Get_Ada_Main_Name;
2532 -- Name to be used for generated Ada main program. See the body of
2533 -- function Get_Ada_Main_Name for details on the form of the name.
2535 Needs_Library_Finalization : constant Boolean :=
2536 not Configurable_Run_Time_On_Target
2537 and then Has_Finalizer (Elab_Order);
2538 -- For restricted run-time libraries (ZFP and Ravenscar) tasks are
2539 -- non-terminating, so we do not want finalization.
2541 Bfiles : Name_Id;
2542 -- Name of generated bind file (spec)
2544 Bfileb : Name_Id;
2545 -- Name of generated bind file (body)
2547 begin
2548 -- Create spec first
2550 Create_Binder_Output (Filename, 's', Bfiles);
2552 -- We always compile the binder file in Ada 95 mode so that we properly
2553 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2554 -- of the Ada 2005 or Ada 2012 constructs are needed by the binder file.
2556 WBI ("pragma Warnings (Off);");
2557 WBI ("pragma Ada_95;");
2559 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2560 -- then we need to make sure that the binder program is compiled with
2561 -- the same restriction, so that no exception tables are generated.
2563 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2564 WBI ("pragma Restrictions (No_Exception_Handlers);");
2565 end if;
2567 -- Same processing for Restrictions (No_Exception_Propagation)
2569 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2570 WBI ("pragma Restrictions (No_Exception_Propagation);");
2571 end if;
2573 -- Same processing for pragma No_Run_Time
2575 if No_Run_Time_Mode then
2576 WBI ("pragma No_Run_Time;");
2577 end if;
2579 -- Generate with of System so we can reference System.Address
2581 WBI ("with System;");
2583 -- Generate with of System.Initialize_Scalars if active
2585 if Initialize_Scalars_Used then
2586 WBI ("with System.Scalar_Values;");
2587 end if;
2589 -- Generate withs of System.Secondary_Stack and System.Parameters to
2590 -- allow the generation of the default-sized secondary stack pool.
2592 if Sec_Stack_Used then
2593 WBI ("with System.Parameters;");
2594 WBI ("with System.Secondary_Stack;");
2595 end if;
2597 if Enable_CUDA_Expansion then
2598 WBI ("with Interfaces.C;");
2599 WBI ("with Interfaces.C.Strings;");
2601 -- with of CUDA.Internal needed for CUDA_Execute pragma expansion
2602 WBI ("with CUDA.Internal;");
2603 end if;
2605 Resolve_Binder_Options (Elab_Order);
2607 -- Generate standard with's
2609 if not Suppress_Standard_Library_On_Target then
2610 if CodePeer_Mode then
2611 WBI ("with System.Standard_Library;");
2612 end if;
2613 end if;
2615 WBI ("package " & Ada_Main & " is");
2617 -- Main program case
2619 if Bind_Main_Program then
2620 -- Generate argc/argv stuff unless suppressed
2622 -- A run-time configured to support command line arguments defines
2623 -- a number of internal symbols that need to be set by the binder.
2624 -- We do not do this in cases where the program does not use
2625 -- Ada.Command_Line, as the package and it's support files may not be
2626 -- present.
2628 if Command_Line_Args_On_Target and then Command_Line_Used then
2629 WBI ("");
2630 WBI (" gnat_argc : Integer;");
2631 WBI (" gnat_argv : System.Address;");
2632 WBI (" gnat_envp : System.Address;");
2634 WBI ("");
2635 WBI (" pragma Import (C, gnat_argc);");
2636 WBI (" pragma Import (C, gnat_argv);");
2637 WBI (" pragma Import (C, gnat_envp);");
2638 end if;
2640 -- Define exit status if supported by the target. The exit status is
2641 -- stored in the run-time library to allow applications set the state
2642 -- through Ada.Command_Line and is initialized in the run-time. Like
2643 -- command line arguments, skip if Ada.Command_Line is not used in
2644 -- the enclosure of the partition because this package may not be
2645 -- available in the runtime.
2647 WBI ("");
2649 if Exit_Status_Supported_On_Target and then Command_Line_Used
2650 then
2651 WBI (" gnat_exit_status : Integer;");
2652 WBI (" pragma Import (C, gnat_exit_status);");
2653 end if;
2655 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
2656 -- the main program. Otherwise, it can lead under some circumstances
2657 -- to a symbol duplication during the link (for instance when a C
2658 -- program uses two Ada libraries). Also zero terminate the string
2659 -- so that its end can be found reliably at run time.
2661 if not Minimal_Binder then
2662 WBI ("");
2663 WBI (" GNAT_Version : constant String :=");
2664 WBI (" """ & Ver_Prefix &
2665 Gnat_Version_String &
2666 """ & ASCII.NUL;");
2667 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2668 WBI ("");
2669 WBI (" GNAT_Version_Address : constant System.Address := " &
2670 "GNAT_Version'Address;");
2671 WBI (" pragma Export (C, GNAT_Version_Address, " &
2672 """__gnat_version_address"");");
2673 WBI ("");
2674 Set_String (" Ada_Main_Program_Name : constant String := """);
2675 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2677 Set_Main_Program_Name;
2678 Set_String (""" & ASCII.NUL;");
2680 Write_Statement_Buffer;
2683 (" pragma Export (C, Ada_Main_Program_Name, " &
2684 """__gnat_ada_main_program_name"");");
2685 end if;
2686 end if;
2688 WBI ("");
2689 WBI (" procedure " & Ada_Init_Name.all & ";");
2690 if Enable_CUDA_Device_Expansion then
2691 WBI (" pragma Export (C, " & Ada_Init_Name.all &
2692 ", Link_Name => """ & Device_Link_Name_Prefix
2693 & Ada_Init_Name.all & """);");
2694 WBI (" pragma CUDA_Global (" & Ada_Init_Name.all & ");");
2695 else
2696 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2697 Ada_Init_Name.all & """);");
2698 end if;
2700 -- If -a has been specified use pragma Linker_Constructor for the init
2701 -- procedure and pragma Linker_Destructor for the final procedure.
2703 if Use_Pragma_Linker_Constructor then
2704 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2705 end if;
2707 if not Cumulative_Restrictions.Set (No_Finalization) then
2708 WBI ("");
2709 WBI (" procedure " & Ada_Final_Name.all & ";");
2710 if Enable_CUDA_Device_Expansion then
2711 WBI (" pragma Export (C, " & Ada_Final_Name.all &
2712 ", Link_Name => """ & Device_Link_Name_Prefix &
2713 Ada_Final_Name.all & """);");
2714 WBI (" pragma CUDA_Global (" & Ada_Final_Name.all & ");");
2715 else
2716 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2717 Ada_Final_Name.all & """);");
2718 end if;
2720 if Use_Pragma_Linker_Constructor then
2721 WBI (" pragma Linker_Destructor (" & Ada_Final_Name.all & ");");
2722 end if;
2723 end if;
2725 if Bind_Main_Program then
2727 WBI ("");
2729 if Exit_Status_Supported_On_Target then
2730 Set_String (" function ");
2731 else
2732 Set_String (" procedure ");
2733 end if;
2735 Set_String (Get_Main_Name);
2737 -- Generate argument list if present
2739 if Command_Line_Args_On_Target then
2740 Write_Statement_Buffer;
2741 WBI (" (argc : Integer;");
2742 WBI (" argv : System.Address;");
2743 Set_String
2744 (" envp : System.Address)");
2746 if Exit_Status_Supported_On_Target then
2747 Write_Statement_Buffer;
2748 WBI (" return Integer;");
2749 else
2750 Write_Statement_Buffer (";");
2751 end if;
2753 else
2754 if Exit_Status_Supported_On_Target then
2755 Write_Statement_Buffer (" return Integer;");
2756 else
2757 Write_Statement_Buffer (";");
2758 end if;
2759 end if;
2761 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2762 Get_Main_Name & """);");
2763 end if;
2765 Gen_CUDA_Defs;
2767 -- Generate version numbers for units, only if needed. Be very safe on
2768 -- the condition.
2770 if not Configurable_Run_Time_On_Target
2771 or else System_Version_Control_Used
2772 or else not Bind_Main_Program
2773 then
2774 Gen_Versions;
2775 end if;
2777 Gen_Elab_Order (Elab_Order);
2779 -- Spec is complete
2781 WBI ("");
2782 WBI ("end " & Ada_Main & ";");
2783 Close_Binder_Output;
2785 -- Prepare to write body
2787 Create_Binder_Output (Filename, 'b', Bfileb);
2789 -- We always compile the binder file in Ada 95 mode so that we properly
2790 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2791 -- of the Ada 2005/2012 constructs are needed by the binder file.
2793 WBI ("pragma Warnings (Off);");
2794 WBI ("pragma Ada_95;");
2796 -- Output Source_File_Name pragmas which look like
2798 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2799 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2801 -- where sss/bbb are the spec/body file names respectively
2803 Get_Name_String (Bfiles);
2804 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2806 WBI ("pragma Source_File_Name (" &
2807 Ada_Main &
2808 ", Spec_File_Name => """ &
2809 Name_Buffer (1 .. Name_Len + 3));
2811 Get_Name_String (Bfileb);
2812 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2814 WBI ("pragma Source_File_Name (" &
2815 Ada_Main &
2816 ", Body_File_Name => """ &
2817 Name_Buffer (1 .. Name_Len + 3));
2819 -- Generate pragma Suppress (Overflow_Check). This is needed for recent
2820 -- versions of the compiler which have overflow checks on by default.
2821 -- We do not want overflow checking enabled for the increments of the
2822 -- elaboration variables (since this can cause an unwanted reference to
2823 -- the last chance exception handler for limited run-times).
2825 WBI ("pragma Suppress (Overflow_Check);");
2827 -- Generate with of System.Restrictions to initialize
2828 -- Run_Time_Restrictions.
2830 if System_Restrictions_Used then
2831 WBI ("");
2832 WBI ("with System.Restrictions;");
2833 end if;
2835 -- Generate with of Ada.Exceptions if needs library finalization
2837 if Needs_Library_Finalization then
2838 WBI ("with Ada.Exceptions;");
2839 end if;
2841 -- Generate with of System.Elaboration_Allocators if the restriction
2842 -- No_Standard_Allocators_After_Elaboration was present.
2844 if Cumulative_Restrictions.Set
2845 (No_Standard_Allocators_After_Elaboration)
2846 then
2847 WBI ("with System.Elaboration_Allocators;");
2848 end if;
2850 -- Generate start of package body
2852 WBI ("");
2853 WBI ("package body " & Ada_Main & " is");
2854 WBI ("");
2856 -- Generate externals for elaboration entities
2858 Gen_Elab_Externals (Elab_Order);
2860 -- Generate default-sized secondary stacks pool. At least one stack is
2861 -- created and assigned to the environment task if secondary stacks are
2862 -- used by the program.
2864 if Sec_Stack_Used then
2865 Set_String (" Sec_Default_Sized_Stacks");
2866 Set_String (" : array (1 .. ");
2867 Set_Int (Num_Sec_Stacks);
2868 Set_String (") of aliased System.Secondary_Stack.SS_Stack (");
2870 if Opt.Default_Sec_Stack_Size /= No_Stack_Size then
2871 Set_Int (Opt.Default_Sec_Stack_Size);
2872 else
2873 Set_String ("System.Parameters.Runtime_Default_Sec_Stack_Size");
2874 end if;
2876 Set_String (");");
2877 Write_Statement_Buffer;
2878 WBI ("");
2879 end if;
2881 -- Generate reference
2883 if not CodePeer_Mode then
2884 if not Suppress_Standard_Library_On_Target then
2886 -- Generate Priority_Specific_Dispatching pragma string
2888 Set_String
2889 (" Local_Priority_Specific_Dispatching : " &
2890 "constant String := """);
2892 for J in 0 .. PSD_Pragma_Settings.Last loop
2893 Set_Char (PSD_Pragma_Settings.Table (J));
2894 end loop;
2896 Set_String (""";");
2897 Write_Statement_Buffer;
2899 -- Generate Interrupt_State pragma string
2901 Set_String (" Local_Interrupt_States : constant String := """);
2903 for J in 0 .. IS_Pragma_Settings.Last loop
2904 Set_Char (IS_Pragma_Settings.Table (J));
2905 end loop;
2907 Set_String (""";");
2908 Write_Statement_Buffer;
2909 WBI ("");
2910 end if;
2912 if not Suppress_Standard_Library_On_Target then
2914 -- The B.1(39) implementation advice says that the adainit and
2915 -- adafinal routines should be idempotent. Generate a flag to
2916 -- ensure that. This is not needed if we are suppressing the
2917 -- standard library since it would never be referenced.
2919 WBI (" Is_Elaborated : Boolean := False;");
2921 -- Generate bind environment string
2923 Gen_Bind_Env_String;
2924 end if;
2926 WBI ("");
2927 end if;
2929 -- Generate the adafinal routine unless there is no finalization to do
2931 if not Cumulative_Restrictions.Set (No_Finalization) then
2932 if Needs_Library_Finalization then
2933 Gen_Finalize_Library (Elab_Order);
2934 end if;
2936 Gen_Adafinal;
2937 end if;
2939 Gen_Adainit (Elab_Order);
2941 if Enable_CUDA_Expansion then
2942 WBI (" procedure " & Device_Ada_Init_Subp_Name & " is");
2943 WBI (" begin");
2944 WBI (" raise Program_Error;");
2945 WBI (" end " & Device_Ada_Init_Subp_Name & ";");
2946 end if;
2948 if Bind_Main_Program then
2949 Gen_Main;
2950 end if;
2952 -- Output object file list and the Ada body is complete
2954 Gen_Object_Files_Options (Elab_Order);
2956 WBI ("");
2957 WBI ("end " & Ada_Main & ";");
2959 Close_Binder_Output;
2960 end Gen_Output_File_Ada;
2962 ----------------------
2963 -- Gen_Restrictions --
2964 ----------------------
2966 procedure Gen_Restrictions is
2967 Count : Integer;
2969 begin
2970 if not System_Restrictions_Used then
2971 return;
2972 end if;
2974 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2975 WBI (" (Set =>");
2976 Set_String (" (");
2978 Count := 0;
2980 for J in Cumulative_Restrictions.Set'Range loop
2981 Set_Boolean (Cumulative_Restrictions.Set (J));
2982 Set_String (", ");
2983 Count := Count + 1;
2985 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
2986 Write_Statement_Buffer;
2987 Set_String (" ");
2988 Count := 0;
2989 end if;
2990 end loop;
2992 Set_String_Replace ("),");
2993 Write_Statement_Buffer;
2994 Set_String (" Value => (");
2996 for J in Cumulative_Restrictions.Value'Range loop
2997 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2998 Set_String (", ");
2999 end loop;
3001 Set_String_Replace ("),");
3002 Write_Statement_Buffer;
3003 WBI (" Violated =>");
3004 Set_String (" (");
3005 Count := 0;
3007 for J in Cumulative_Restrictions.Violated'Range loop
3008 Set_Boolean (Cumulative_Restrictions.Violated (J));
3009 Set_String (", ");
3010 Count := Count + 1;
3012 if J /= Cumulative_Restrictions.Set'Last and then Count = 8 then
3013 Write_Statement_Buffer;
3014 Set_String (" ");
3015 Count := 0;
3016 end if;
3017 end loop;
3019 Set_String_Replace ("),");
3020 Write_Statement_Buffer;
3021 Set_String (" Count => (");
3023 for J in Cumulative_Restrictions.Count'Range loop
3024 Set_Int (Int (Cumulative_Restrictions.Count (J)));
3025 Set_String (", ");
3026 end loop;
3028 Set_String_Replace ("),");
3029 Write_Statement_Buffer;
3030 Set_String (" Unknown => (");
3032 for J in Cumulative_Restrictions.Unknown'Range loop
3033 Set_Boolean (Cumulative_Restrictions.Unknown (J));
3034 Set_String (", ");
3035 end loop;
3037 Set_String_Replace ("))");
3038 Set_String (";");
3039 Write_Statement_Buffer;
3040 end Gen_Restrictions;
3042 ------------------
3043 -- Gen_Versions --
3044 ------------------
3046 -- This routine generates lines such as:
3048 -- unnnnn : constant Integer := 16#hhhhhhhh#;
3049 -- pragma Export (C, unnnnn, unam);
3051 -- for each unit, where unam is the unit name suffixed by either B or S for
3052 -- body or spec, with dots replaced by double underscores, and hhhhhhhh is
3053 -- the version number, and nnnnn is a 5-digits serial number.
3055 procedure Gen_Versions is
3056 Ubuf : String (1 .. 6) := "u00000";
3058 procedure Increment_Ubuf;
3059 -- Little procedure to increment the serial number
3061 --------------------
3062 -- Increment_Ubuf --
3063 --------------------
3065 procedure Increment_Ubuf is
3066 begin
3067 for J in reverse Ubuf'Range loop
3068 Ubuf (J) := Character'Succ (Ubuf (J));
3069 exit when Ubuf (J) <= '9';
3070 Ubuf (J) := '0';
3071 end loop;
3072 end Increment_Ubuf;
3074 -- Start of processing for Gen_Versions
3076 begin
3077 WBI ("");
3079 WBI (" type Version_32 is mod 2 ** 32;");
3080 for U in Units.First .. Units.Last loop
3081 if not Units.Table (U).SAL_Interface
3082 and then (not Bind_For_Library
3083 or else Units.Table (U).Directly_Scanned)
3084 then
3085 Increment_Ubuf;
3086 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
3087 Units.Table (U).Version & "#;");
3088 Set_String (" pragma Export (C, ");
3089 Set_String (Ubuf);
3090 Set_String (", """);
3092 Get_Name_String (Units.Table (U).Uname);
3094 for K in 1 .. Name_Len loop
3095 if Name_Buffer (K) = '.' then
3096 Set_Char ('_');
3097 Set_Char ('_');
3099 elsif Name_Buffer (K) = '%' then
3100 exit;
3102 else
3103 Set_Char (Name_Buffer (K));
3104 end if;
3105 end loop;
3107 if Name_Buffer (Name_Len) = 's' then
3108 Set_Char ('S');
3109 else
3110 Set_Char ('B');
3111 end if;
3113 Set_String (""");");
3114 Write_Statement_Buffer;
3115 end if;
3116 end loop;
3117 end Gen_Versions;
3119 ------------------------
3120 -- Get_Main_Unit_Name --
3121 ------------------------
3123 function Get_Main_Unit_Name (S : String) return String is
3124 Result : String := S;
3126 begin
3127 for J in S'Range loop
3128 if Result (J) = '.' then
3129 Result (J) := '_';
3130 end if;
3131 end loop;
3133 return Result;
3134 end Get_Main_Unit_Name;
3136 -----------------------
3137 -- Get_Ada_Main_Name --
3138 -----------------------
3140 function Get_Ada_Main_Name return String is
3141 Suffix : constant String := "_00";
3142 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3143 Opt.Ada_Main_Name.all & Suffix;
3144 Nlen : Natural;
3146 begin
3147 -- For CodePeer, we want reproducible names (independent of other mains
3148 -- that may or may not be present) that don't collide when analyzing
3149 -- multiple mains and which are easily recognizable as "ada_main" names.
3151 if CodePeer_Mode then
3152 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3154 return
3155 "ada_main_for_" &
3156 Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3157 end if;
3159 -- This loop tries the following possibilities in order
3160 -- <Ada_Main>
3161 -- <Ada_Main>_01
3162 -- <Ada_Main>_02
3163 -- ..
3164 -- <Ada_Main>_99
3165 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3166 -- it is set to 'ada_main'.
3168 for J in 0 .. 99 loop
3169 if J = 0 then
3170 Nlen := Name'Length - Suffix'Length;
3171 else
3172 Nlen := Name'Length;
3173 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3174 Name (Name'Last - 1) :=
3175 Character'Val (J / 10 + Character'Pos ('0'));
3176 end if;
3178 for K in ALIs.First .. ALIs.Last loop
3179 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3181 -- Get unit name, removing %b or %e at end
3183 Get_Name_String (Units.Table (L).Uname);
3184 Name_Len := Name_Len - 2;
3186 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3187 goto Continue;
3188 end if;
3189 end loop;
3190 end loop;
3192 return Name (1 .. Nlen);
3194 <<Continue>>
3195 null;
3196 end loop;
3198 -- If we fall through, just use a peculiar unlikely name
3200 return ("Qwertyuiop");
3201 end Get_Ada_Main_Name;
3203 -------------------
3204 -- Get_Main_Name --
3205 -------------------
3207 function Get_Main_Name return String is
3208 begin
3209 -- Explicit name given with -M switch
3211 if Bind_Alternate_Main_Name then
3212 return Alternate_Main_Name.all;
3214 -- Case of main program name to be used directly
3216 elsif Use_Ada_Main_Program_Name_On_Target then
3218 -- Get main program name
3220 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3222 -- If this is a child name, return only the name of the child, since
3223 -- we can't have dots in a nested program name. Note that we do not
3224 -- include the %b at the end of the unit name.
3226 for J in reverse 1 .. Name_Len - 2 loop
3227 if J = 1 or else Name_Buffer (J - 1) = '.' then
3228 return Name_Buffer (J .. Name_Len - 2);
3229 end if;
3230 end loop;
3232 raise Program_Error; -- impossible exit
3234 -- Case where "main" is to be used as default
3236 else
3237 return "main";
3238 end if;
3239 end Get_Main_Name;
3241 ---------------------
3242 -- Get_WC_Encoding --
3243 ---------------------
3245 function Get_WC_Encoding return Character is
3246 begin
3247 -- If encoding method specified by -W switch, then return it
3249 if Wide_Character_Encoding_Method_Specified then
3250 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3252 -- If no main program, and not specified, set brackets, we really have
3253 -- no better choice. If some other encoding is required when there is
3254 -- no main, it must be set explicitly using -Wx.
3256 -- Note: if the ALI file always passed the wide character encoding of
3257 -- every file, then we could use the encoding of the initial specified
3258 -- file, but this information is passed only for potential main
3259 -- programs. We could fix this sometime, but it is a very minor point
3260 -- (wide character default encoding for [Wide_[Wide_]]Text_IO when there
3261 -- is no main program).
3263 elsif No_Main_Subprogram then
3264 return 'b';
3266 -- Otherwise if there is a main program, take encoding from it
3268 else
3269 return ALIs.Table (ALIs.First).WC_Encoding;
3270 end if;
3271 end Get_WC_Encoding;
3273 -------------------
3274 -- Has_Finalizer --
3275 -------------------
3277 function Has_Finalizer (Elab_Order : Unit_Id_Array) return Boolean is
3278 U : Unit_Record;
3279 Unum : Unit_Id;
3281 begin
3282 for E in reverse Elab_Order'Range loop
3283 Unum := Elab_Order (E);
3284 U := Units.Table (Unum);
3286 -- We are only interested in non-generic packages
3288 if U.Unit_Kind = 'p'
3289 and then U.Has_Finalizer
3290 and then not U.Is_Generic
3291 and then not U.No_Elab
3292 then
3293 return True;
3294 end if;
3295 end loop;
3297 return False;
3298 end Has_Finalizer;
3300 ----------
3301 -- Hash --
3302 ----------
3304 function Hash (Nam : Name_Id) return Header_Num is
3305 begin
3306 return Int (Nam - Names_Low_Bound) rem Header_Num'Last;
3307 end Hash;
3309 ----------------------
3310 -- Lt_Linker_Option --
3311 ----------------------
3313 function Lt_Linker_Option (Op1 : Natural; Op2 : Natural) return Boolean is
3314 begin
3315 -- Sort internal files last
3317 if Linker_Options.Table (Op1).Internal_File
3319 Linker_Options.Table (Op2).Internal_File
3320 then
3321 -- Note: following test uses False < True
3323 return Linker_Options.Table (Op1).Internal_File
3325 Linker_Options.Table (Op2).Internal_File;
3327 -- If both internal or both non-internal, sort according to the
3328 -- elaboration position. A unit that is elaborated later should come
3329 -- earlier in the linker options list.
3331 else
3332 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3334 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3335 end if;
3336 end Lt_Linker_Option;
3338 ------------------------
3339 -- Move_Linker_Option --
3340 ------------------------
3342 procedure Move_Linker_Option (From : Natural; To : Natural) is
3343 begin
3344 Linker_Options.Table (To) := Linker_Options.Table (From);
3345 end Move_Linker_Option;
3347 ----------------------------
3348 -- Resolve_Binder_Options --
3349 ----------------------------
3351 procedure Resolve_Binder_Options (Elab_Order : Unit_Id_Array) is
3352 procedure Check_Package (Var : in out Boolean; Name : String);
3353 -- Set Var to true iff the current identifier in Namet is Name. Do
3354 -- nothing if it doesn't match. This procedure is just a helper to
3355 -- avoid explicitly dealing with length.
3357 -------------------
3358 -- Check_Package --
3359 -------------------
3361 procedure Check_Package (Var : in out Boolean; Name : String) is
3362 begin
3363 if Name_Len = Name'Length
3364 and then Name_Buffer (1 .. Name_Len) = Name
3365 then
3366 Var := True;
3367 end if;
3368 end Check_Package;
3370 -- Start of processing for Resolve_Binder_Options
3372 begin
3373 for E in Elab_Order'Range loop
3374 Get_Name_String (Units.Table (Elab_Order (E)).Uname);
3376 -- This is not a perfect approach, but is the current protocol
3377 -- between the run-time and the binder to indicate that tasking is
3378 -- used: System.OS_Interface should always be used by any tasking
3379 -- application.
3381 Check_Package (With_GNARL, "system.os_interface%s");
3383 -- Ditto for the use of restricted tasking
3385 Check_Package
3386 (System_Tasking_Restricted_Stages_Used,
3387 "system.tasking.restricted.stages%s");
3389 -- Ditto for the use of interrupts
3391 Check_Package (System_Interrupts_Used, "system.interrupts%s");
3393 -- Ditto for the use of dispatching domains
3395 Check_Package
3396 (Dispatching_Domains_Used,
3397 "system.multiprocessors.dispatching_domains%s");
3399 -- Ditto for the use of restrictions
3401 Check_Package (System_Restrictions_Used, "system.restrictions%s");
3403 -- Ditto for use of an SMP bareboard runtime
3405 Check_Package (System_BB_CPU_Primitives_Multiprocessors_Used,
3406 "system.bb.cpu_primitives.multiprocessors%s");
3408 -- Ditto for System.Version_Control, which is used for Version and
3409 -- Body_Version attributes.
3411 Check_Package (System_Version_Control_Used,
3412 "system.version_control%s");
3414 -- Ditto for the use of Ada.Command_Line, except we always set
3415 -- Command_Line_Used to True if on a non-configurable run-time
3416 -- as parts of the compiler and run-time assume the GNAT command
3417 -- line symbols are available and can be imported directly (as
3418 -- long as No_Run_Time mode is not set).
3420 if Configurable_Run_Time_On_Target then
3421 Check_Package (Command_Line_Used, "ada.command_line%s");
3422 elsif not No_Run_Time_Mode then
3423 Command_Line_Used := True;
3424 end if;
3425 end loop;
3426 end Resolve_Binder_Options;
3428 ------------------
3429 -- Set_Bind_Env --
3430 ------------------
3432 procedure Set_Bind_Env (Key, Value : String) is
3433 begin
3434 -- The lengths of Key and Value are stored as single bytes
3436 if Key'Length > 255 then
3437 Osint.Fail ("bind environment key """ & Key & """ too long");
3438 end if;
3440 if Value'Length > 255 then
3441 Osint.Fail ("bind environment value """ & Value & """ too long");
3442 end if;
3444 Bind_Environment.Set (Name_Find (Key), Name_Find (Value));
3445 end Set_Bind_Env;
3447 -----------------
3448 -- Set_Boolean --
3449 -----------------
3451 procedure Set_Boolean (B : Boolean) is
3452 False_Str : constant String := "False";
3453 True_Str : constant String := "True";
3455 begin
3456 if B then
3457 Statement_Buffer (Stm_Last + 1 .. Stm_Last + True_Str'Length) :=
3458 True_Str;
3459 Stm_Last := Stm_Last + True_Str'Length;
3460 else
3461 Statement_Buffer (Stm_Last + 1 .. Stm_Last + False_Str'Length) :=
3462 False_Str;
3463 Stm_Last := Stm_Last + False_Str'Length;
3464 end if;
3465 end Set_Boolean;
3467 --------------
3468 -- Set_Char --
3469 --------------
3471 procedure Set_Char (C : Character) is
3472 begin
3473 Stm_Last := Stm_Last + 1;
3474 Statement_Buffer (Stm_Last) := C;
3475 end Set_Char;
3477 -------------
3478 -- Set_Int --
3479 -------------
3481 procedure Set_Int (N : Int) is
3482 begin
3483 if N < 0 then
3484 Set_String ("-");
3485 Set_Int (-N);
3487 else
3488 if N > 9 then
3489 Set_Int (N / 10);
3490 end if;
3492 Stm_Last := Stm_Last + 1;
3493 Statement_Buffer (Stm_Last) :=
3494 Character'Val (N mod 10 + Character'Pos ('0'));
3495 end if;
3496 end Set_Int;
3498 -------------------------
3499 -- Set_IS_Pragma_Table --
3500 -------------------------
3502 procedure Set_IS_Pragma_Table is
3503 begin
3504 for F in ALIs.First .. ALIs.Last loop
3505 for K in ALIs.Table (F).First_Interrupt_State ..
3506 ALIs.Table (F).Last_Interrupt_State
3507 loop
3508 declare
3509 Inum : constant Int :=
3510 Interrupt_States.Table (K).Interrupt_Id;
3511 Stat : constant Character :=
3512 Interrupt_States.Table (K).Interrupt_State;
3514 begin
3515 while IS_Pragma_Settings.Last < Inum loop
3516 if Interrupts_Default_To_System_Specified then
3517 IS_Pragma_Settings.Append ('s');
3518 else
3519 IS_Pragma_Settings.Append ('n');
3520 end if;
3521 end loop;
3523 IS_Pragma_Settings.Table (Inum) := Stat;
3524 end;
3525 end loop;
3526 end loop;
3527 end Set_IS_Pragma_Table;
3529 ---------------------------
3530 -- Set_Main_Program_Name --
3531 ---------------------------
3533 procedure Set_Main_Program_Name is
3534 begin
3535 -- Note that name has %b on the end which we ignore
3537 -- First we output the initial _ada_ since we know that the main program
3538 -- is a library level subprogram.
3540 Set_String ("_ada_");
3542 -- Copy name, changing dots to double underscores
3544 for J in 1 .. Name_Len - 2 loop
3545 if Name_Buffer (J) = '.' then
3546 Set_String ("__");
3547 else
3548 Set_Char (Name_Buffer (J));
3549 end if;
3550 end loop;
3551 end Set_Main_Program_Name;
3553 ---------------------
3554 -- Set_Name_Buffer --
3555 ---------------------
3557 procedure Set_Name_Buffer is
3558 begin
3559 for J in 1 .. Name_Len loop
3560 Set_Char (Name_Buffer (J));
3561 end loop;
3562 end Set_Name_Buffer;
3564 -------------------------
3565 -- Set_PSD_Pragma_Table --
3566 -------------------------
3568 procedure Set_PSD_Pragma_Table is
3569 begin
3570 for F in ALIs.First .. ALIs.Last loop
3571 for K in ALIs.Table (F).First_Specific_Dispatching ..
3572 ALIs.Table (F).Last_Specific_Dispatching
3573 loop
3574 declare
3575 DTK : Specific_Dispatching_Record
3576 renames Specific_Dispatching.Table (K);
3578 begin
3579 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3580 PSD_Pragma_Settings.Append ('F');
3581 end loop;
3583 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3584 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3585 end loop;
3586 end;
3587 end loop;
3588 end loop;
3589 end Set_PSD_Pragma_Table;
3591 ----------------
3592 -- Set_String --
3593 ----------------
3595 procedure Set_String (S : String) is
3596 begin
3597 Statement_Buffer (Stm_Last + 1 .. Stm_Last + S'Length) := S;
3598 Stm_Last := Stm_Last + S'Length;
3599 end Set_String;
3601 ------------------------
3602 -- Set_String_Replace --
3603 ------------------------
3605 procedure Set_String_Replace (S : String) is
3606 begin
3607 Statement_Buffer (Stm_Last - S'Length + 1 .. Stm_Last) := S;
3608 end Set_String_Replace;
3610 -------------------
3611 -- Set_Unit_Name --
3612 -------------------
3614 procedure Set_Unit_Name is
3615 begin
3616 for J in 1 .. Name_Len - 2 loop
3617 if Name_Buffer (J) = '.' then
3618 Set_String ("__");
3619 else
3620 Set_Char (Name_Buffer (J));
3621 end if;
3622 end loop;
3623 end Set_Unit_Name;
3625 ---------------------
3626 -- Set_Unit_Number --
3627 ---------------------
3629 procedure Set_Unit_Number (U : Unit_Id) is
3630 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3631 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3633 begin
3634 if Num_Units >= 10 and then Unum < 10 then
3635 Set_Char ('0');
3636 end if;
3638 if Num_Units >= 100 and then Unum < 100 then
3639 Set_Char ('0');
3640 end if;
3642 Set_Int (Unum);
3643 end Set_Unit_Number;
3645 ---------------------
3646 -- Write_Bind_Line --
3647 ---------------------
3649 procedure Write_Bind_Line (S : String) is
3650 begin
3651 -- Need to strip trailing LF from S
3653 WBI (S (S'First .. S'Last - 1));
3654 end Write_Bind_Line;
3656 ----------------------------
3657 -- Write_Statement_Buffer --
3658 ----------------------------
3660 procedure Write_Statement_Buffer is
3661 begin
3662 WBI (Statement_Buffer (1 .. Stm_Last));
3663 Stm_Last := 0;
3664 end Write_Statement_Buffer;
3666 procedure Write_Statement_Buffer (S : String) is
3667 begin
3668 Set_String (S);
3669 Write_Statement_Buffer;
3670 end Write_Statement_Buffer;
3672 end Bindgen;