Debian Codegraph @

gcc/ada/bindgen.adb

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