2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / bindgen.adb
blobd29857fb5fce035236a61ce6690451f283aab808
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-2008, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with ALI; use ALI;
27 with Binde; use Binde;
28 with Casing; use Casing;
29 with Fname; use Fname;
30 with Gnatvsn; use Gnatvsn;
31 with Hostparm;
32 with Namet; use Namet;
33 with Opt; use Opt;
34 with Osint; use Osint;
35 with Osint.B; use Osint.B;
36 with Output; use Output;
37 with Rident; use Rident;
38 with Table; use Table;
39 with Targparm; use Targparm;
40 with Types; use Types;
42 with System.OS_Lib; use System.OS_Lib;
43 with System.WCh_Con; use System.WCh_Con;
45 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
47 package body Bindgen is
49 Statement_Buffer : String (1 .. 1000);
50 -- Buffer used for constructing output statements
52 Last : Natural := 0;
53 -- Last location in Statement_Buffer currently set
55 With_DECGNAT : Boolean := False;
56 -- Flag which indicates whether the program uses the DECGNAT library
57 -- (presence of the unit DEC).
59 With_GNARL : Boolean := False;
60 -- Flag which indicates whether the program uses the GNARL library
61 -- (presence of the unit System.OS_Interface)
63 Num_Elab_Calls : Nat := 0;
64 -- Number of generated calls to elaboration routines
66 System_Restrictions_Used : Boolean;
67 -- Flag indicating whether the unit System.Restrictions is in the closure
68 -- of the partition. This is set by Check_System_Restrictions_Used, and
69 -- is used to determine whether or not to initialize the restrictions
70 -- information in the body of the binder generated file (we do not want
71 -- to do this unconditionally, since it drags in the System.Restrictions
72 -- unit unconditionally, which is unpleasand, especially for ZFP etc.)
74 ----------------------------------
75 -- Interface_State Pragma Table --
76 ----------------------------------
78 -- This table assembles the interface state pragma information from
79 -- all the units in the partition. Note that Bcheck has already checked
80 -- that the information is consistent across units. The entries
81 -- in this table are n/u/r/s for not set/user/runtime/system.
83 package IS_Pragma_Settings is new Table.Table (
84 Table_Component_Type => Character,
85 Table_Index_Type => Int,
86 Table_Low_Bound => 0,
87 Table_Initial => 100,
88 Table_Increment => 200,
89 Table_Name => "IS_Pragma_Settings");
91 -- This table assembles the Priority_Specific_Dispatching pragma
92 -- information from all the units in the partition. Note that Bcheck has
93 -- already checked that the information is consistent across units.
94 -- The entries in this table are the upper case first character of the
95 -- policy name, e.g. 'F' for FIFO_Within_Priorities.
97 package PSD_Pragma_Settings is new Table.Table (
98 Table_Component_Type => Character,
99 Table_Index_Type => Int,
100 Table_Low_Bound => 0,
101 Table_Initial => 100,
102 Table_Increment => 200,
103 Table_Name => "PSD_Pragma_Settings");
105 ----------------------
106 -- Run-Time Globals --
107 ----------------------
109 -- This section documents the global variables that set from the
110 -- generated binder file.
112 -- Main_Priority : Integer;
113 -- Time_Slice_Value : Integer;
114 -- WC_Encoding : Character;
115 -- Locking_Policy : Character;
116 -- Queuing_Policy : Character;
117 -- Task_Dispatching_Policy : Character;
118 -- Priority_Specific_Dispatching : System.Address;
119 -- Num_Specific_Dispatching : Integer;
120 -- Restrictions : System.Address;
121 -- Interrupt_States : System.Address;
122 -- Num_Interrupt_States : Integer;
123 -- Unreserve_All_Interrupts : Integer;
124 -- Exception_Tracebacks : Integer;
125 -- Zero_Cost_Exceptions : Integer;
126 -- Detect_Blocking : Integer;
127 -- Default_Stack_Size : Integer;
128 -- Leap_Seconds_Support : Integer;
130 -- Main_Priority is the priority value set by pragma Priority in the
131 -- main program. If no such pragma is present, the value is -1.
133 -- Time_Slice_Value is the time slice value set by pragma Time_Slice
134 -- in the main program, or by the use of a -Tnnn parameter for the
135 -- binder (if both are present, the binder value overrides). The
136 -- value is in milliseconds. A value of zero indicates that time
137 -- slicing should be suppressed. If no pragma is present, and no
138 -- -T switch was used, the value is -1.
140 -- WC_Encoding shows the wide character encoding method used for
141 -- the main program. This is one of the encoding letters defined
142 -- in System.WCh_Con.WC_Encoding_Letters.
144 -- Locking_Policy is a space if no locking policy was specified
145 -- for the partition. If a locking policy was specified, the value
146 -- is the upper case first character of the locking policy name,
147 -- for example, 'C' for Ceiling_Locking.
149 -- Queuing_Policy is a space if no queuing policy was specified
150 -- for the partition. If a queuing policy was specified, the value
151 -- is the upper case first character of the queuing policy name
152 -- for example, 'F' for FIFO_Queuing.
154 -- Task_Dispatching_Policy is a space if no task dispatching policy
155 -- was specified for the partition. If a task dispatching policy
156 -- was specified, the value is the upper case first character of
157 -- the policy name, e.g. 'F' for FIFO_Within_Priorities.
159 -- Priority_Specific_Dispatching is the address of a string used to
160 -- store the task dispatching policy specified for the different priorities
161 -- in the partition. The length of this string is determined by the last
162 -- priority for which such a pragma applies (the string will be a null
163 -- string if no specific dispatching policies were used). If pragma were
164 -- present, the entries apply to the priorities in sequence from the first
165 -- priority. The value stored is the upper case first character of the
166 -- policy name, or 'F' (for FIFO_Within_Priorities) as the default value
167 -- for those priority ranges not specified.
169 -- Num_Specific_Dispatching is the length of the
170 -- Priority_Specific_Dispatching string. It will be set to zero if no
171 -- Priority_Specific_Dispatching pragmas are present.
173 -- Restrictions is the address of a null-terminated string specifying the
174 -- restrictions information for the partition. The format is identical to
175 -- that of the parameter string found on R lines in ali files (see Lib.Writ
176 -- spec in lib-writ.ads for full details). The difference is that in this
177 -- context the values are the cumulative ones for the entire partition.
179 -- Interrupt_States is the address of a string used to specify the
180 -- cumulative results of Interrupt_State pragmas used in the partition.
181 -- The length of this string is determined by the last interrupt for which
182 -- such a pragma is given (the string will be a null string if no pragmas
183 -- were used). If pragma were present the entries apply to the interrupts
184 -- in sequence from the first interrupt, and are set to one of four
185 -- possible settings: 'n' for not specified, 'u' for user, 'r' for
186 -- run time, 's' for system, see description of Interrupt_State pragma
187 -- for further details.
189 -- Num_Interrupt_States is the length of the Interrupt_States string.
190 -- It will be set to zero if no Interrupt_State pragmas are present.
192 -- Unreserve_All_Interrupts is set to one if at least one unit in the
193 -- partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
195 -- Exception_Tracebacks is set to one if the -E parameter was present
196 -- in the bind and to zero otherwise. Note that on some targets exception
197 -- tracebacks are provided by default, so a value of zero for this
198 -- parameter does not necessarily mean no trace backs are available.
200 -- Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
201 -- this partition, and to zero if longjmp/setjmp exceptions are used.
202 -- the use of zero
204 -- Detect_Blocking indicates whether pragma Detect_Blocking is
205 -- active or not. A value of zero indicates that the pragma is not
206 -- present, while a value of 1 signals its presence in the
207 -- partition.
209 -- Default_Stack_Size is the default stack size used when creating an
210 -- Ada task with no explicit Storize_Size clause.
212 -- Leap_Seconds_Support denotes whether leap seconds have been enabled or
213 -- disabled. A value of zero indicates that leap seconds are turned "off",
214 -- while a value of one signifies "on" status.
216 -----------------------
217 -- Local Subprograms --
218 -----------------------
220 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
221 -- Convenient shorthand used throughout
223 procedure Check_System_Restrictions_Used;
224 -- Sets flag System_Restrictions_Used (Set to True if and only if the unit
225 -- System.Restrictions is present in the partition, otherwise False).
227 procedure Gen_Adainit_Ada;
228 -- Generates the Adainit procedure (Ada code case)
230 procedure Gen_Adainit_C;
231 -- Generates the Adainit procedure (C code case)
233 procedure Gen_Adafinal_Ada;
234 -- Generate the Adafinal procedure (Ada code case)
236 procedure Gen_Adafinal_C;
237 -- Generate the Adafinal procedure (C code case)
239 procedure Gen_Elab_Calls_Ada;
240 -- Generate sequence of elaboration calls (Ada code case)
242 procedure Gen_Elab_Calls_C;
243 -- Generate sequence of elaboration calls (C code case)
245 procedure Gen_Elab_Order_Ada;
246 -- Generate comments showing elaboration order chosen (Ada case)
248 procedure Gen_Elab_Order_C;
249 -- Generate comments showing elaboration order chosen (C case)
251 procedure Gen_Elab_Defs_C;
252 -- Generate sequence of definitions for elaboration routines (C code case)
254 procedure Gen_Main_Ada;
255 -- Generate procedure main (Ada code case)
257 procedure Gen_Main_C;
258 -- Generate main() procedure (C code case)
260 procedure Gen_Object_Files_Options;
261 -- Output comments containing a list of the full names of the object
262 -- files to be linked and the list of linker options supplied by
263 -- Linker_Options pragmas in the source. (C and Ada code case)
265 procedure Gen_Output_File_Ada (Filename : String);
266 -- Generate output file (Ada code case)
268 procedure Gen_Output_File_C (Filename : String);
269 -- Generate output file (C code case)
271 procedure Gen_Restrictions_Ada;
272 -- Generate initialization of restrictions variable (Ada code case)
274 procedure Gen_Restrictions_C;
275 -- Generate initialization of restrictions variable (C code case)
277 procedure Gen_Versions_Ada;
278 -- Output series of definitions for unit versions (Ada code case)
280 procedure Gen_Versions_C;
281 -- Output series of definitions for unit versions (C code case)
283 function Get_Ada_Main_Name return String;
284 -- This function is used in the Ada main output case to compute a usable
285 -- name for the generated main program. The normal main program name is
286 -- Ada_Main, but this won't work if the user has a unit with this name.
287 -- This function tries Ada_Main first, and if there is such a clash, then
288 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
290 function Get_Main_Unit_Name (S : String) return String;
291 -- Return the main unit name corresponding to S by replacing '.' with '_'
293 function Get_Main_Name return String;
294 -- This function is used in the Ada main output case to compute the
295 -- correct external main program. It is "main" by default, unless the
296 -- flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
297 -- is the name of the Ada main name without the "_ada". This default
298 -- can be overridden explicitly using the -Mname binder switch.
300 function Get_WC_Encoding return Character;
301 -- Return wide character encoding method to set as WC_Encoding in output.
302 -- If -W has been used, returns the specified encoding, otherwise returns
303 -- the encoding method used for the main program source. If there is no
304 -- main program source (-z switch used), returns brackets ('b').
306 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
307 -- Compare linker options, when sorting, first according to
308 -- Is_Internal_File (internal files come later) and then by
309 -- elaboration order position (latest to earliest).
311 procedure Move_Linker_Option (From : Natural; To : Natural);
312 -- Move routine for sorting linker options
314 procedure Resolve_Binder_Options;
315 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
316 -- since it tests for a package named "dec" which might cause a conflict
317 -- on non-VMS systems.
319 procedure Set_Char (C : Character);
320 -- Set given character in Statement_Buffer at the Last + 1 position
321 -- and increment Last by one to reflect the stored character.
323 procedure Set_Int (N : Int);
324 -- Set given value in decimal in Statement_Buffer with no spaces
325 -- starting at the Last + 1 position, and updating Last past the value.
326 -- A minus sign is output for a negative value.
328 procedure Set_Boolean (B : Boolean);
329 -- Set given boolean value in Statement_Buffer at the Last + 1 position
330 -- and update Last past the value.
332 procedure Set_IS_Pragma_Table;
333 -- Initializes contents of IS_Pragma_Settings table from ALI table
335 procedure Set_Main_Program_Name;
336 -- Given the main program name in Name_Buffer (length in Name_Len)
337 -- generate the name of the routine to be used in the call. The name
338 -- is generated starting at Last + 1, and Last is updated past it.
340 procedure Set_Name_Buffer;
341 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer
343 procedure Set_PSD_Pragma_Table;
344 -- Initializes contents of PSD_Pragma_Settings table from ALI table
346 procedure Set_String (S : String);
347 -- Sets characters of given string in Statement_Buffer, starting at the
348 -- Last + 1 position, and updating last past the string value.
350 procedure Set_Unit_Name;
351 -- Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
352 -- starting at the Last + 1 position, and updating last past the value.
353 -- changing periods to double underscores, and updating Last appropriately.
355 procedure Set_Unit_Number (U : Unit_Id);
356 -- Sets unit number (first unit is 1, leading zeroes output to line
357 -- up all output unit numbers nicely as required by the value, and
358 -- by the total number of units.
360 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
361 -- For C code case, write C & Common, for Ada case write Ada & Common
362 -- to current binder output file using Write_Binder_Info.
364 procedure Write_Statement_Buffer;
365 -- Write out contents of statement buffer up to Last, and reset Last to 0
367 procedure Write_Statement_Buffer (S : String);
368 -- First writes its argument (using Set_String (S)), then writes out the
369 -- contents of statement buffer up to Last, and reset Last to 0
371 ------------------------------------
372 -- Check_System_Restrictions_Used --
373 ------------------------------------
375 procedure Check_System_Restrictions_Used is
376 begin
377 for J in Units.First .. Units.Last loop
378 if Get_Name_String (Units.Table (J).Sfile) = "s-restri.ads" then
379 System_Restrictions_Used := True;
380 return;
381 end if;
382 end loop;
384 System_Restrictions_Used := False;
385 end Check_System_Restrictions_Used;
387 ----------------------
388 -- Gen_Adafinal_Ada --
389 ----------------------
391 procedure Gen_Adafinal_Ada is
392 begin
393 WBI ("");
394 WBI (" procedure " & Ada_Final_Name.all & " is");
395 WBI (" begin");
397 -- If compiling for the JVM, we directly call Adafinal because
398 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
400 if VM_Target /= No_VM then
401 WBI (" System.Standard_Library.Adafinal;");
403 -- If there is no finalization, there is nothing to do
405 elsif Cumulative_Restrictions.Set (No_Finalization) then
406 WBI (" null;");
407 else
408 WBI (" Do_Finalize;");
409 end if;
411 WBI (" end " & Ada_Final_Name.all & ";");
412 end Gen_Adafinal_Ada;
414 --------------------
415 -- Gen_Adafinal_C --
416 --------------------
418 procedure Gen_Adafinal_C is
419 begin
420 WBI ("void " & Ada_Final_Name.all & " (void) {");
421 WBI (" system__standard_library__adafinal ();");
422 WBI ("}");
423 WBI ("");
424 end Gen_Adafinal_C;
426 ---------------------
427 -- Gen_Adainit_Ada --
428 ---------------------
430 procedure Gen_Adainit_Ada is
431 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
433 begin
434 WBI (" procedure " & Ada_Init_Name.all & " is");
436 -- Generate externals for elaboration entities
438 for E in Elab_Order.First .. Elab_Order.Last loop
439 declare
440 Unum : constant Unit_Id := Elab_Order.Table (E);
441 U : Unit_Record renames Units.Table (Unum);
443 begin
444 -- Check for Elab_Entity to be set for this unit
446 if U.Set_Elab_Entity
448 -- Don't generate reference for stand alone library
450 and then not U.SAL_Interface
452 -- Don't generate reference for predefined file in No_Run_Time
453 -- mode, since we don't include the object files in this case
455 and then not
456 (No_Run_Time_Mode
457 and then Is_Predefined_File_Name (U.Sfile))
458 then
459 Set_String (" ");
460 Set_String ("E");
461 Set_Unit_Number (Unum);
463 case VM_Target is
464 when No_VM | JVM_Target =>
465 Set_String (" : Boolean; pragma Import (Ada, ");
466 when CLI_Target =>
467 Set_String (" : Boolean; pragma Import (CIL, ");
468 end case;
470 Set_String ("E");
471 Set_Unit_Number (Unum);
472 Set_String (", """);
473 Get_Name_String (U.Uname);
475 -- In the case of JGNAT we need to emit an Import name
476 -- that includes the class name (using '$' separators
477 -- in the case of a child unit name).
479 if VM_Target /= No_VM then
480 for J in 1 .. Name_Len - 2 loop
481 if VM_Target = CLI_Target
482 or else Name_Buffer (J) /= '.'
483 then
484 Set_Char (Name_Buffer (J));
485 else
486 Set_String ("$");
487 end if;
488 end loop;
490 if VM_Target /= CLI_Target or else U.Unit_Kind = 's' then
491 Set_String (".");
492 else
493 Set_String ("_pkg.");
494 end if;
496 -- If the unit name is very long, then split the
497 -- Import link name across lines using "&" (occurs
498 -- in some C2 tests).
500 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
501 Set_String (""" &");
502 Write_Statement_Buffer;
503 Set_String (" """);
504 end if;
505 end if;
507 Set_Unit_Name;
508 Set_String ("_E"");");
509 Write_Statement_Buffer;
510 end if;
511 end;
512 end loop;
514 Write_Statement_Buffer;
516 -- If the standard library is suppressed, then the only global variable
517 -- that might be needed (by the Ravenscar profile) is the priority of
518 -- the environment.
520 if Suppress_Standard_Library_On_Target then
521 if Main_Priority /= No_Main_Priority then
522 WBI (" Main_Priority : Integer;");
523 WBI (" pragma Import (C, Main_Priority," &
524 " ""__gl_main_priority"");");
525 WBI ("");
526 end if;
528 WBI (" begin");
530 if Main_Priority /= No_Main_Priority then
531 Set_String (" Main_Priority := ");
532 Set_Int (Main_Priority);
533 Set_Char (';');
534 Write_Statement_Buffer;
536 else
537 WBI (" null;");
538 end if;
540 -- Normal case (standard library not suppressed). Set all global values
541 -- used by the run time.
543 else
544 WBI (" Main_Priority : Integer;");
545 WBI (" pragma Import (C, Main_Priority, " &
546 """__gl_main_priority"");");
547 WBI (" Time_Slice_Value : Integer;");
548 WBI (" pragma Import (C, Time_Slice_Value, " &
549 """__gl_time_slice_val"");");
550 WBI (" WC_Encoding : Character;");
551 WBI (" pragma Import (C, WC_Encoding, ""__gl_wc_encoding"");");
552 WBI (" Locking_Policy : Character;");
553 WBI (" pragma Import (C, Locking_Policy, " &
554 """__gl_locking_policy"");");
555 WBI (" Queuing_Policy : Character;");
556 WBI (" pragma Import (C, Queuing_Policy, " &
557 """__gl_queuing_policy"");");
558 WBI (" Task_Dispatching_Policy : Character;");
559 WBI (" pragma Import (C, Task_Dispatching_Policy, " &
560 """__gl_task_dispatching_policy"");");
561 WBI (" Priority_Specific_Dispatching : System.Address;");
562 WBI (" pragma Import (C, Priority_Specific_Dispatching, " &
563 """__gl_priority_specific_dispatching"");");
564 WBI (" Num_Specific_Dispatching : Integer;");
565 WBI (" pragma Import (C, Num_Specific_Dispatching, " &
566 """__gl_num_specific_dispatching"");");
568 WBI (" Interrupt_States : System.Address;");
569 WBI (" pragma Import (C, Interrupt_States, " &
570 """__gl_interrupt_states"");");
571 WBI (" Num_Interrupt_States : Integer;");
572 WBI (" pragma Import (C, Num_Interrupt_States, " &
573 """__gl_num_interrupt_states"");");
574 WBI (" Unreserve_All_Interrupts : Integer;");
575 WBI (" pragma Import (C, Unreserve_All_Interrupts, " &
576 """__gl_unreserve_all_interrupts"");");
578 if Exception_Tracebacks then
579 WBI (" Exception_Tracebacks : Integer;");
580 WBI (" pragma Import (C, Exception_Tracebacks, " &
581 """__gl_exception_tracebacks"");");
582 end if;
584 WBI (" Zero_Cost_Exceptions : Integer;");
585 WBI (" pragma Import (C, Zero_Cost_Exceptions, " &
586 """__gl_zero_cost_exceptions"");");
587 WBI (" Detect_Blocking : Integer;");
588 WBI (" pragma Import (C, Detect_Blocking, " &
589 """__gl_detect_blocking"");");
590 WBI (" Default_Stack_Size : Integer;");
591 WBI (" pragma Import (C, Default_Stack_Size, " &
592 """__gl_default_stack_size"");");
593 WBI (" Leap_Seconds_Support : Integer;");
594 WBI (" pragma Import (C, Leap_Seconds_Support, " &
595 """__gl_leap_seconds_support"");");
597 -- Import entry point for elaboration time signal handler
598 -- installation, and indication of if it's been called previously.
600 WBI ("");
601 WBI (" procedure Install_Handler;");
602 WBI (" pragma Import (C, Install_Handler, " &
603 """__gnat_install_handler"");");
604 WBI ("");
605 WBI (" Handler_Installed : Integer;");
606 WBI (" pragma Import (C, Handler_Installed, " &
607 """__gnat_handler_installed"");");
609 -- Initialize stack limit variable of the environment task if the
610 -- stack check method is stack limit and stack check is enabled.
612 if Stack_Check_Limits_On_Target
613 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
614 then
615 WBI ("");
616 WBI (" procedure Initialize_Stack_Limit;");
617 WBI (" pragma Import (C, Initialize_Stack_Limit, " &
618 """__gnat_initialize_stack_limit"");");
619 end if;
621 -- Special processing when main program is CIL function/procedure
623 if VM_Target = CLI_Target
624 and then Bind_Main_Program
625 and then not No_Main_Subprogram
626 then
627 WBI ("");
629 -- Function case, use Set_Exit_Status to report the returned
630 -- status code, since that is the only mechanism available.
632 if ALIs.Table (ALIs.First).Main_Program = Func then
633 WBI (" Result : Integer;");
634 WBI (" procedure Set_Exit_Status (Code : Integer);");
635 WBI (" pragma Import (C, Set_Exit_Status, " &
636 """__gnat_set_exit_status"");");
637 WBI ("");
638 WBI (" function Ada_Main_Program return Integer;");
640 -- Procedure case
642 else
643 WBI (" procedure Ada_Main_Program;");
644 end if;
646 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
647 Name_Len := Name_Len - 2;
648 WBI (" pragma Import (CIL, Ada_Main_Program, """
649 & Name_Buffer (1 .. Name_Len) & "."
650 & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len)) & """);");
651 end if;
653 WBI (" begin");
655 Set_String (" Main_Priority := ");
656 Set_Int (Main_Priority);
657 Set_Char (';');
658 Write_Statement_Buffer;
660 Set_String (" Time_Slice_Value := ");
662 if Task_Dispatching_Policy_Specified = 'F'
663 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
664 then
665 Set_Int (0);
666 else
667 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
668 end if;
670 Set_Char (';');
671 Write_Statement_Buffer;
673 Set_String (" WC_Encoding := '");
674 Set_Char (Get_WC_Encoding);
676 Set_String ("';");
677 Write_Statement_Buffer;
679 Set_String (" Locking_Policy := '");
680 Set_Char (Locking_Policy_Specified);
681 Set_String ("';");
682 Write_Statement_Buffer;
684 Set_String (" Queuing_Policy := '");
685 Set_Char (Queuing_Policy_Specified);
686 Set_String ("';");
687 Write_Statement_Buffer;
689 Set_String (" Task_Dispatching_Policy := '");
690 Set_Char (Task_Dispatching_Policy_Specified);
691 Set_String ("';");
692 Write_Statement_Buffer;
694 Gen_Restrictions_Ada;
696 WBI (" Priority_Specific_Dispatching :=");
697 WBI (" Local_Priority_Specific_Dispatching'Address;");
699 Set_String (" Num_Specific_Dispatching := ");
700 Set_Int (PSD_Pragma_Settings.Last + 1);
701 Set_Char (';');
702 Write_Statement_Buffer;
704 WBI (" Interrupt_States := Local_Interrupt_States'Address;");
706 Set_String (" Num_Interrupt_States := ");
707 Set_Int (IS_Pragma_Settings.Last + 1);
708 Set_Char (';');
709 Write_Statement_Buffer;
711 Set_String (" Unreserve_All_Interrupts := ");
713 if Unreserve_All_Interrupts_Specified then
714 Set_String ("1");
715 else
716 Set_String ("0");
717 end if;
719 Set_Char (';');
720 Write_Statement_Buffer;
722 if Exception_Tracebacks then
723 WBI (" Exception_Tracebacks := 1;");
724 end if;
726 Set_String (" Zero_Cost_Exceptions := ");
728 if Zero_Cost_Exceptions_Specified then
729 Set_String ("1");
730 else
731 Set_String ("0");
732 end if;
734 Set_String (";");
735 Write_Statement_Buffer;
737 Set_String (" Detect_Blocking := ");
739 if Detect_Blocking then
740 Set_Int (1);
741 else
742 Set_Int (0);
743 end if;
745 Set_String (";");
746 Write_Statement_Buffer;
748 Set_String (" Default_Stack_Size := ");
749 Set_Int (Default_Stack_Size);
750 Set_String (";");
751 Write_Statement_Buffer;
753 Set_String (" Leap_Seconds_Support := ");
755 if Leap_Seconds_Support then
756 Set_Int (1);
757 else
758 Set_Int (0);
759 end if;
761 Set_String (";");
762 Write_Statement_Buffer;
764 -- Generate call to Install_Handler
766 WBI ("");
767 WBI (" if Handler_Installed = 0 then");
768 WBI (" Install_Handler;");
769 WBI (" end if;");
770 end if;
772 -- Generate call to set Initialize_Scalar values if active
774 if Initialize_Scalars_Used then
775 WBI ("");
776 Set_String (" System.Scalar_Values.Initialize ('");
777 Set_Char (Initialize_Scalars_Mode1);
778 Set_String ("', '");
779 Set_Char (Initialize_Scalars_Mode2);
780 Set_String ("');");
781 Write_Statement_Buffer;
782 end if;
784 -- Generate assignment of default secondary stack size if set
786 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
787 WBI ("");
788 Set_String (" System.Secondary_Stack.");
789 Set_String ("Default_Secondary_Stack_Size := ");
790 Set_Int (Opt.Default_Sec_Stack_Size);
791 Set_Char (';');
792 Write_Statement_Buffer;
793 end if;
795 -- Initialize stack limit variable of the environment task if the
796 -- stack check method is stack limit and stack check is enabled.
798 if Stack_Check_Limits_On_Target
799 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
800 then
801 WBI ("");
802 WBI (" Initialize_Stack_Limit;");
803 end if;
805 -- Generate elaboration calls
807 WBI ("");
808 Gen_Elab_Calls_Ada;
810 -- Case of main program is CIL function or procedure
812 if VM_Target = CLI_Target
813 and then Bind_Main_Program
814 and then not No_Main_Subprogram
815 then
816 -- For function case, use Set_Exit_Status to set result
818 if ALIs.Table (ALIs.First).Main_Program = Func then
819 WBI (" Result := Ada_Main_Program;");
820 WBI (" Set_Exit_Status (Result);");
822 -- Procedure case
824 else
825 WBI (" Ada_Main_Program;");
826 end if;
827 end if;
829 WBI (" end " & Ada_Init_Name.all & ";");
830 end Gen_Adainit_Ada;
832 -------------------
833 -- Gen_Adainit_C --
834 --------------------
836 procedure Gen_Adainit_C is
837 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
839 begin
840 WBI ("void " & Ada_Init_Name.all & " (void)");
841 WBI ("{");
843 -- Generate externals for elaboration entities
845 for E in Elab_Order.First .. Elab_Order.Last loop
846 declare
847 Unum : constant Unit_Id := Elab_Order.Table (E);
848 U : Unit_Record renames Units.Table (Unum);
850 begin
851 -- Check for Elab entity to be set for this unit
853 if U.Set_Elab_Entity
855 -- Don't generate reference for stand alone library
857 and then not U.SAL_Interface
859 -- Don't generate reference for predefined file in No_Run_Time
860 -- mode, since we don't include the object files in this case
862 and then not
863 (No_Run_Time_Mode
864 and then Is_Predefined_File_Name (U.Sfile))
865 then
866 Set_String (" extern char ");
867 Get_Name_String (U.Uname);
868 Set_Unit_Name;
869 Set_String ("_E;");
870 Write_Statement_Buffer;
871 end if;
872 end;
873 end loop;
875 Write_Statement_Buffer;
877 -- Standard library suppressed
879 if Suppress_Standard_Library_On_Target then
881 -- Case of High_Integrity_Mode mode. Set __gl_main_priority if needed
882 -- for the Ravenscar profile.
884 if Main_Priority /= No_Main_Priority then
885 WBI (" extern int __gl_main_priority;");
886 Set_String (" __gl_main_priority = ");
887 Set_Int (Main_Priority);
888 Set_Char (';');
889 Write_Statement_Buffer;
890 end if;
892 -- Normal case (standard library not suppressed)
894 else
895 -- Generate definition for interrupt states string
897 Set_String (" static const char *local_interrupt_states = """);
899 for J in 0 .. IS_Pragma_Settings.Last loop
900 Set_Char (IS_Pragma_Settings.Table (J));
901 end loop;
903 Set_String (""";");
904 Write_Statement_Buffer;
906 -- Generate definition for priority specific dispatching string
908 Set_String
909 (" static const char *local_priority_specific_dispatching = """);
911 for J in 0 .. PSD_Pragma_Settings.Last loop
912 Set_Char (PSD_Pragma_Settings.Table (J));
913 end loop;
915 Set_String (""";");
916 Write_Statement_Buffer;
918 -- Generate declaration for secondary stack default if needed
920 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
921 WBI (" extern int system__secondary_stack__" &
922 "default_secondary_stack_size;");
923 end if;
925 WBI ("");
927 -- Code for normal case (standard library not suppressed)
929 -- We call the routine from inside adainit() because this works for
930 -- both programs with and without binder generated "main" functions.
932 WBI (" extern int __gl_main_priority;");
933 Set_String (" __gl_main_priority = ");
934 Set_Int (Main_Priority);
935 Set_Char (';');
936 Write_Statement_Buffer;
938 WBI (" extern int __gl_time_slice_val;");
939 Set_String (" __gl_time_slice_val = ");
941 if Task_Dispatching_Policy = 'F'
942 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
943 then
944 Set_Int (0);
945 else
946 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
947 end if;
949 Set_Char (';');
950 Write_Statement_Buffer;
952 WBI (" extern char __gl_wc_encoding;");
953 Set_String (" __gl_wc_encoding = '");
954 Set_Char (Get_WC_Encoding);
956 Set_String ("';");
957 Write_Statement_Buffer;
959 WBI (" extern char __gl_locking_policy;");
960 Set_String (" __gl_locking_policy = '");
961 Set_Char (Locking_Policy_Specified);
962 Set_String ("';");
963 Write_Statement_Buffer;
965 WBI (" extern char __gl_queuing_policy;");
966 Set_String (" __gl_queuing_policy = '");
967 Set_Char (Queuing_Policy_Specified);
968 Set_String ("';");
969 Write_Statement_Buffer;
971 WBI (" extern char __gl_task_dispatching_policy;");
972 Set_String (" __gl_task_dispatching_policy = '");
973 Set_Char (Task_Dispatching_Policy_Specified);
974 Set_String ("';");
975 Write_Statement_Buffer;
977 Gen_Restrictions_C;
979 WBI (" extern const void *__gl_interrupt_states;");
980 WBI (" __gl_interrupt_states = local_interrupt_states;");
982 WBI (" extern int __gl_num_interrupt_states;");
983 Set_String (" __gl_num_interrupt_states = ");
984 Set_Int (IS_Pragma_Settings.Last + 1);
985 Set_String (";");
986 Write_Statement_Buffer;
988 WBI (" extern const void *__gl_priority_specific_dispatching;");
989 WBI (" __gl_priority_specific_dispatching =" &
990 " local_priority_specific_dispatching;");
992 WBI (" extern int __gl_num_specific_dispatching;");
993 Set_String (" __gl_num_specific_dispatching = ");
994 Set_Int (PSD_Pragma_Settings.Last + 1);
995 Set_String (";");
996 Write_Statement_Buffer;
998 WBI (" extern int __gl_unreserve_all_interrupts;");
999 Set_String (" __gl_unreserve_all_interrupts = ");
1000 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
1001 Set_String (";");
1002 Write_Statement_Buffer;
1004 if Exception_Tracebacks then
1005 WBI (" extern int __gl_exception_tracebacks;");
1006 WBI (" __gl_exception_tracebacks = 1;");
1007 end if;
1009 WBI (" extern int __gl_zero_cost_exceptions;");
1010 Set_String (" __gl_zero_cost_exceptions = ");
1011 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
1012 Set_String (";");
1013 Write_Statement_Buffer;
1015 WBI (" extern int __gl_detect_blocking;");
1016 Set_String (" __gl_detect_blocking = ");
1018 if Detect_Blocking then
1019 Set_Int (1);
1020 else
1021 Set_Int (0);
1022 end if;
1024 Set_String (";");
1025 Write_Statement_Buffer;
1027 WBI (" extern int __gl_default_stack_size;");
1028 Set_String (" __gl_default_stack_size = ");
1029 Set_Int (Default_Stack_Size);
1030 Set_String (";");
1031 Write_Statement_Buffer;
1033 WBI (" extern int __gl_leap_seconds_support;");
1034 Set_String (" __gl_leap_seconds_support = ");
1036 if Leap_Seconds_Support then
1037 Set_Int (1);
1038 else
1039 Set_Int (0);
1040 end if;
1042 Set_String (";");
1043 Write_Statement_Buffer;
1045 WBI ("");
1047 -- Install elaboration time signal handler
1049 WBI (" if (__gnat_handler_installed == 0)");
1050 WBI (" {");
1051 WBI (" __gnat_install_handler ();");
1052 WBI (" }");
1053 end if;
1055 -- Initialize stack limit for the environment task if the stack
1056 -- check method is stack limit and stack check is enabled.
1058 if Stack_Check_Limits_On_Target
1059 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
1060 then
1061 WBI ("");
1062 WBI (" __gnat_initialize_stack_limit ();");
1063 end if;
1065 -- Generate call to set Initialize_Scalar values if needed
1067 if Initialize_Scalars_Used then
1068 WBI ("");
1069 Set_String (" system__scalar_values__initialize('");
1070 Set_Char (Initialize_Scalars_Mode1);
1071 Set_String ("', '");
1072 Set_Char (Initialize_Scalars_Mode2);
1073 Set_String ("');");
1074 Write_Statement_Buffer;
1075 end if;
1077 -- Generate assignment of default secondary stack size if set
1079 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
1080 WBI ("");
1081 Set_String (" system__secondary_stack__");
1082 Set_String ("default_secondary_stack_size = ");
1083 Set_Int (Opt.Default_Sec_Stack_Size);
1084 Set_Char (';');
1085 Write_Statement_Buffer;
1086 end if;
1088 -- Generate elaboration calls
1090 WBI ("");
1091 Gen_Elab_Calls_C;
1092 WBI ("}");
1093 end Gen_Adainit_C;
1095 ------------------------
1096 -- Gen_Elab_Calls_Ada --
1097 ------------------------
1099 procedure Gen_Elab_Calls_Ada is
1100 begin
1101 for E in Elab_Order.First .. Elab_Order.Last loop
1102 declare
1103 Unum : constant Unit_Id := Elab_Order.Table (E);
1104 U : Unit_Record renames Units.Table (Unum);
1106 Unum_Spec : Unit_Id;
1107 -- This is the unit number of the spec that corresponds to
1108 -- this entry. It is the same as Unum except when the body
1109 -- and spec are different and we are currently processing
1110 -- the body, in which case it is the spec (Unum + 1).
1112 begin
1113 if U.Utype = Is_Body then
1114 Unum_Spec := Unum + 1;
1115 else
1116 Unum_Spec := Unum;
1117 end if;
1119 -- Nothing to do if predefined unit in no run time mode
1121 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1122 null;
1124 -- Case of no elaboration code
1126 elsif U.No_Elab then
1128 -- The only case in which we have to do something is if
1129 -- this is a body, with a separate spec, where the separate
1130 -- spec has an elaboration entity defined.
1132 -- In that case, this is where we set the elaboration entity
1133 -- to True, we do not need to test if this has already been
1134 -- done, since it is quicker to set the flag than to test it.
1136 if not U.SAL_Interface and then U.Utype = Is_Body
1137 and then Units.Table (Unum_Spec).Set_Elab_Entity
1138 then
1139 Set_String (" E");
1140 Set_Unit_Number (Unum_Spec);
1141 Set_String (" := True;");
1142 Write_Statement_Buffer;
1143 end if;
1145 -- Here if elaboration code is present. If binding a library
1146 -- or if there is a non-Ada main subprogram then we generate:
1148 -- if not uname_E then
1149 -- uname'elab_[spec|body];
1150 -- uname_E := True;
1151 -- end if;
1153 -- Otherwise, elaboration routines are called unconditionally:
1155 -- uname'elab_[spec|body];
1156 -- uname_E := True;
1158 -- The uname_E assignment is skipped if this is a separate spec,
1159 -- since the assignment will be done when we process the body.
1161 elsif not U.SAL_Interface then
1162 if Force_Checking_Of_Elaboration_Flags or
1163 Interface_Library_Unit or
1164 (not Bind_Main_Program)
1165 then
1166 Set_String (" if not E");
1167 Set_Unit_Number (Unum_Spec);
1168 Set_String (" then");
1169 Write_Statement_Buffer;
1170 Set_String (" ");
1171 end if;
1173 Set_String (" ");
1174 Get_Decoded_Name_String_With_Brackets (U.Uname);
1176 if VM_Target = CLI_Target and then U.Unit_Kind /= 's' then
1177 if Name_Buffer (Name_Len) = 's' then
1178 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1179 "_pkg'elab_spec";
1180 else
1181 Name_Buffer (Name_Len - 1 .. Name_Len + 12) :=
1182 "_pkg'elab_body";
1183 end if;
1185 Name_Len := Name_Len + 12;
1187 else
1188 if Name_Buffer (Name_Len) = 's' then
1189 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1190 "'elab_spec";
1191 else
1192 Name_Buffer (Name_Len - 1 .. Name_Len + 8) :=
1193 "'elab_body";
1194 end if;
1196 Name_Len := Name_Len + 8;
1197 end if;
1199 Set_Casing (U.Icasing);
1200 Set_Name_Buffer;
1201 Set_Char (';');
1202 Write_Statement_Buffer;
1204 if U.Utype /= Is_Spec then
1205 if Force_Checking_Of_Elaboration_Flags or
1206 Interface_Library_Unit or
1207 (not Bind_Main_Program)
1208 then
1209 Set_String (" ");
1210 end if;
1212 Set_String (" E");
1213 Set_Unit_Number (Unum_Spec);
1214 Set_String (" := True;");
1215 Write_Statement_Buffer;
1216 end if;
1218 if Force_Checking_Of_Elaboration_Flags or
1219 Interface_Library_Unit or
1220 (not Bind_Main_Program)
1221 then
1222 WBI (" end if;");
1223 end if;
1224 end if;
1225 end;
1226 end loop;
1227 end Gen_Elab_Calls_Ada;
1229 ----------------------
1230 -- Gen_Elab_Calls_C --
1231 ----------------------
1233 procedure Gen_Elab_Calls_C is
1234 begin
1235 for E in Elab_Order.First .. Elab_Order.Last loop
1236 declare
1237 Unum : constant Unit_Id := Elab_Order.Table (E);
1238 U : Unit_Record renames Units.Table (Unum);
1240 Unum_Spec : Unit_Id;
1241 -- This is the unit number of the spec that corresponds to
1242 -- this entry. It is the same as Unum except when the body
1243 -- and spec are different and we are currently processing
1244 -- the body, in which case it is the spec (Unum + 1).
1246 begin
1247 if U.Utype = Is_Body then
1248 Unum_Spec := Unum + 1;
1249 else
1250 Unum_Spec := Unum;
1251 end if;
1253 -- Nothing to do if predefined unit in no run time mode
1255 if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1256 null;
1258 -- Case of no elaboration code
1260 elsif U.No_Elab then
1262 -- The only case in which we have to do something is if
1263 -- this is a body, with a separate spec, where the separate
1264 -- spec has an elaboration entity defined.
1266 -- In that case, this is where we set the elaboration entity
1267 -- to True, we do not need to test if this has already been
1268 -- done, since it is quicker to set the flag than to test it.
1270 if not U.SAL_Interface and then U.Utype = Is_Body
1271 and then Units.Table (Unum_Spec).Set_Elab_Entity
1272 then
1273 Set_String (" ");
1274 Get_Name_String (U.Uname);
1275 Set_Unit_Name;
1276 Set_String ("_E = 1;");
1277 Write_Statement_Buffer;
1278 end if;
1280 -- Here if elaboration code is present. If binding a library
1281 -- or if there is a non-Ada main subprogram then we generate:
1283 -- if (uname_E == 0) {
1284 -- uname__elab[s|b] ();
1285 -- uname_E++;
1286 -- }
1288 -- The uname_E assignment is skipped if this is a separate spec,
1289 -- since the assignment will be done when we process the body.
1291 elsif not U.SAL_Interface then
1292 Get_Name_String (U.Uname);
1294 if Force_Checking_Of_Elaboration_Flags or
1295 Interface_Library_Unit or
1296 (not Bind_Main_Program)
1297 then
1298 Set_String (" if (");
1299 Set_Unit_Name;
1300 Set_String ("_E == 0) {");
1301 Write_Statement_Buffer;
1302 Set_String (" ");
1303 end if;
1305 Set_String (" ");
1306 Set_Unit_Name;
1307 Set_String ("___elab");
1308 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1309 Set_String (" ();");
1310 Write_Statement_Buffer;
1312 if U.Utype /= Is_Spec then
1313 if Force_Checking_Of_Elaboration_Flags or
1314 Interface_Library_Unit or
1315 (not Bind_Main_Program)
1316 then
1317 Set_String (" ");
1318 end if;
1320 Set_String (" ");
1321 Set_Unit_Name;
1322 Set_String ("_E++;");
1323 Write_Statement_Buffer;
1324 end if;
1326 if Force_Checking_Of_Elaboration_Flags or
1327 Interface_Library_Unit or
1328 (not Bind_Main_Program)
1329 then
1330 WBI (" }");
1331 end if;
1332 end if;
1333 end;
1334 end loop;
1336 end Gen_Elab_Calls_C;
1338 ----------------------
1339 -- Gen_Elab_Defs_C --
1340 ----------------------
1342 procedure Gen_Elab_Defs_C is
1343 begin
1344 for E in Elab_Order.First .. Elab_Order.Last loop
1346 -- Generate declaration of elaboration procedure if elaboration
1347 -- needed. Note that passive units are always excluded.
1349 if not Units.Table (Elab_Order.Table (E)).No_Elab then
1350 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1351 Set_String ("extern void ");
1352 Set_Unit_Name;
1353 Set_String ("___elab");
1354 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1355 Set_String (" (void);");
1356 Write_Statement_Buffer;
1357 end if;
1359 end loop;
1361 WBI ("");
1362 end Gen_Elab_Defs_C;
1364 ------------------------
1365 -- Gen_Elab_Order_Ada --
1366 ------------------------
1368 procedure Gen_Elab_Order_Ada is
1369 begin
1370 WBI ("");
1371 WBI (" -- BEGIN ELABORATION ORDER");
1373 for J in Elab_Order.First .. Elab_Order.Last loop
1374 Set_String (" -- ");
1375 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1376 Set_Name_Buffer;
1377 Write_Statement_Buffer;
1378 end loop;
1380 WBI (" -- END ELABORATION ORDER");
1381 end Gen_Elab_Order_Ada;
1383 ----------------------
1384 -- Gen_Elab_Order_C --
1385 ----------------------
1387 procedure Gen_Elab_Order_C is
1388 begin
1389 WBI ("");
1390 WBI ("/* BEGIN ELABORATION ORDER");
1392 for J in Elab_Order.First .. Elab_Order.Last loop
1393 Get_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1394 Set_Name_Buffer;
1395 Write_Statement_Buffer;
1396 end loop;
1398 WBI (" END ELABORATION ORDER */");
1399 end Gen_Elab_Order_C;
1401 ------------------
1402 -- Gen_Main_Ada --
1403 ------------------
1405 procedure Gen_Main_Ada is
1406 begin
1407 WBI ("");
1409 if Exit_Status_Supported_On_Target then
1410 Set_String (" function ");
1411 else
1412 Set_String (" procedure ");
1413 end if;
1415 Set_String (Get_Main_Name);
1417 if Command_Line_Args_On_Target then
1418 Write_Statement_Buffer;
1419 WBI (" (argc : Integer;");
1420 WBI (" argv : System.Address;");
1421 WBI (" envp : System.Address)");
1423 if Exit_Status_Supported_On_Target then
1424 WBI (" return Integer");
1425 end if;
1427 WBI (" is");
1429 else
1430 if Exit_Status_Supported_On_Target then
1431 Set_String (" return Integer is");
1432 else
1433 Set_String (" is");
1434 end if;
1436 Write_Statement_Buffer;
1437 end if;
1439 if Opt.Default_Exit_Status /= 0
1440 and then Bind_Main_Program
1441 and then not Configurable_Run_Time_Mode
1442 then
1443 WBI (" procedure Set_Exit_Status (Status : Integer);");
1444 WBI (" pragma Import (C, Set_Exit_Status, " &
1445 """__gnat_set_exit_status"");");
1446 WBI ("");
1447 end if;
1449 -- Initialize and Finalize
1451 if not Cumulative_Restrictions.Set (No_Finalization) then
1452 WBI (" procedure initialize (Addr : System.Address);");
1453 WBI (" pragma Import (C, initialize, ""__gnat_initialize"");");
1454 WBI ("");
1455 WBI (" procedure finalize;");
1456 WBI (" pragma Import (C, finalize, ""__gnat_finalize"");");
1457 end if;
1459 -- If we want to analyze the stack, we have to import corresponding
1460 -- symbols
1462 if Dynamic_Stack_Measurement then
1463 WBI ("");
1464 WBI (" procedure Output_Results;");
1465 WBI (" pragma Import (C, Output_Results, " &
1466 """__gnat_stack_usage_output_results"");");
1468 WBI ("");
1469 WBI (" " &
1470 "procedure Initialize_Stack_Analysis (Buffer_Size : Natural);");
1471 WBI (" pragma Import (C, Initialize_Stack_Analysis, " &
1472 """__gnat_stack_usage_initialize"");");
1473 end if;
1475 -- Deal with declarations for main program case
1477 if not No_Main_Subprogram then
1479 -- To call the main program, we declare it using a pragma Import
1480 -- Ada with the right link name.
1482 -- It might seem more obvious to "with" the main program, and call
1483 -- it in the normal Ada manner. We do not do this for three reasons:
1485 -- 1. It is more efficient not to recompile the main program
1486 -- 2. We are not entitled to assume the source is accessible
1487 -- 3. We don't know what options to use to compile it
1489 -- It is really reason 3 that is most critical (indeed we used
1490 -- to generate the "with", but several regression tests failed).
1492 WBI ("");
1494 if ALIs.Table (ALIs.First).Main_Program = Func then
1495 WBI (" Result : Integer;");
1496 WBI ("");
1497 WBI (" function Ada_Main_Program return Integer;");
1499 else
1500 WBI (" procedure Ada_Main_Program;");
1501 end if;
1503 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1504 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1505 Set_Main_Program_Name;
1506 Set_String (""");");
1508 Write_Statement_Buffer;
1509 WBI ("");
1511 if Bind_Main_Program
1512 and then not Suppress_Standard_Library_On_Target
1513 then
1514 WBI (" SEH : aliased array (1 .. 2) of Integer;");
1515 WBI ("");
1516 end if;
1517 end if;
1519 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1520 -- not referenced elsewhere in the generated program, but is needed
1521 -- by the debugger (that's why it is generated in the first place).
1522 -- The reference stops Ada_Main_Program_Name from being optimized
1523 -- away by smart linkers, such as the AiX linker.
1525 -- Because this variable is unused, we make this variable "aliased"
1526 -- with a pragma Volatile in order to tell the compiler to preserve
1527 -- this variable at any level of optimization.
1529 if Bind_Main_Program then
1531 (" Ensure_Reference : aliased System.Address := " &
1532 "Ada_Main_Program_Name'Address;");
1533 WBI (" pragma Volatile (Ensure_Reference);");
1534 WBI ("");
1535 end if;
1537 WBI (" begin");
1539 -- Acquire command line arguments if present on target
1541 if Command_Line_Args_On_Target then
1542 WBI (" gnat_argc := argc;");
1543 WBI (" gnat_argv := argv;");
1544 WBI (" gnat_envp := envp;");
1545 WBI ("");
1547 -- If configurable run time and no command line args, then nothing
1548 -- needs to be done since the gnat_argc/argv/envp variables are
1549 -- suppressed in this case.
1551 elsif Configurable_Run_Time_On_Target then
1552 null;
1554 -- Otherwise set dummy values (to be filled in by some other unit?)
1556 else
1557 WBI (" gnat_argc := 0;");
1558 WBI (" gnat_argv := System.Null_Address;");
1559 WBI (" gnat_envp := System.Null_Address;");
1560 end if;
1562 if Opt.Default_Exit_Status /= 0
1563 and then Bind_Main_Program
1564 and then not Configurable_Run_Time_Mode
1565 then
1566 Set_String (" Set_Exit_Status (");
1567 Set_Int (Opt.Default_Exit_Status);
1568 Set_String (");");
1569 Write_Statement_Buffer;
1570 end if;
1572 if Dynamic_Stack_Measurement then
1573 Set_String (" Initialize_Stack_Analysis (");
1574 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1575 Set_String (");");
1576 Write_Statement_Buffer;
1577 end if;
1579 if not Cumulative_Restrictions.Set (No_Finalization) then
1580 if not No_Main_Subprogram
1581 and then Bind_Main_Program
1582 and then not Suppress_Standard_Library_On_Target
1583 then
1584 WBI (" Initialize (SEH'Address);");
1585 else
1586 WBI (" Initialize (System.Null_Address);");
1587 end if;
1588 end if;
1590 WBI (" " & Ada_Init_Name.all & ";");
1592 if not No_Main_Subprogram then
1593 WBI (" Break_Start;");
1595 if ALIs.Table (ALIs.First).Main_Program = Proc then
1596 WBI (" Ada_Main_Program;");
1597 else
1598 WBI (" Result := Ada_Main_Program;");
1599 end if;
1600 end if;
1602 -- Adafinal call is skipped if no finalization
1604 if not Cumulative_Restrictions.Set (No_Finalization) then
1606 -- If compiling for the JVM, we directly call Adafinal because
1607 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1609 if VM_Target = No_VM then
1610 WBI (" Do_Finalize;");
1611 else
1612 WBI (" System.Standard_Library.Adafinal;");
1613 end if;
1614 end if;
1616 -- Prints the result of static stack analysis
1618 if Dynamic_Stack_Measurement then
1619 WBI (" Output_Results;");
1620 end if;
1622 -- Finalize is only called if we have a run time
1624 if not Cumulative_Restrictions.Set (No_Finalization) then
1625 WBI (" Finalize;");
1626 end if;
1628 -- Return result
1630 if Exit_Status_Supported_On_Target then
1631 if No_Main_Subprogram
1632 or else ALIs.Table (ALIs.First).Main_Program = Proc
1633 then
1634 WBI (" return (gnat_exit_status);");
1635 else
1636 WBI (" return (Result);");
1637 end if;
1638 end if;
1640 WBI (" end;");
1641 end Gen_Main_Ada;
1643 ----------------
1644 -- Gen_Main_C --
1645 ----------------
1647 procedure Gen_Main_C is
1648 begin
1649 if Exit_Status_Supported_On_Target then
1650 WBI ("#include <stdlib.h>");
1651 Set_String ("int ");
1652 else
1653 Set_String ("void ");
1654 end if;
1656 Set_String (Get_Main_Name);
1658 -- Generate command line args in prototype if present on target
1660 if Command_Line_Args_On_Target then
1661 Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
1663 -- Case of no command line arguments on target
1665 else
1666 Write_Statement_Buffer (" (void)");
1667 end if;
1669 WBI ("{");
1671 -- Generate a reference to __gnat_ada_main_program_name. This symbol
1672 -- is not referenced elsewhere in the generated program, but is
1673 -- needed by the debugger (that's why it is generated in the first
1674 -- place). The reference stops Ada_Main_Program_Name from being
1675 -- optimized away by smart linkers, such as the AiX linker.
1677 -- Because this variable is unused, we declare this variable as
1678 -- volatile in order to tell the compiler to preserve it at any
1679 -- level of optimization.
1681 if Bind_Main_Program then
1682 WBI (" char * volatile ensure_reference " &
1683 "__attribute__ ((__unused__)) = " &
1684 "__gnat_ada_main_program_name;");
1685 WBI ("");
1687 if not Suppress_Standard_Library_On_Target
1688 and then not No_Main_Subprogram
1689 then
1690 WBI (" int SEH [2];");
1691 WBI ("");
1692 end if;
1693 end if;
1695 -- If main program is a function, generate result variable
1697 if ALIs.Table (ALIs.First).Main_Program = Func then
1698 WBI (" int result;");
1699 end if;
1701 -- Set command line argument values from parameters if command line
1702 -- arguments are present on target
1704 if Command_Line_Args_On_Target then
1705 WBI (" gnat_argc = argc;");
1706 WBI (" gnat_argv = argv;");
1707 WBI (" gnat_envp = envp;");
1708 WBI (" ");
1710 -- If configurable run-time, then nothing to do, since in this case
1711 -- the gnat_argc/argv/envp variables are entirely suppressed.
1713 elsif Configurable_Run_Time_On_Target then
1714 null;
1716 -- if no command line arguments on target, set dummy values
1718 else
1719 WBI (" gnat_argc = 0;");
1720 WBI (" gnat_argv = 0;");
1721 WBI (" gnat_envp = 0;");
1722 end if;
1724 if Opt.Default_Exit_Status /= 0
1725 and then Bind_Main_Program
1726 and then not Configurable_Run_Time_Mode
1727 then
1728 Set_String (" __gnat_set_exit_status (");
1729 Set_Int (Opt.Default_Exit_Status);
1730 Set_String (");");
1731 Write_Statement_Buffer;
1732 end if;
1734 -- Initializes dynamic stack measurement if needed
1736 if Dynamic_Stack_Measurement then
1737 Set_String (" __gnat_stack_usage_initialize (");
1738 Set_Int (Dynamic_Stack_Measurement_Array_Size);
1739 Set_String (");");
1740 Write_Statement_Buffer;
1741 end if;
1743 -- The __gnat_initialize routine is used only if we have a run-time
1745 if not Suppress_Standard_Library_On_Target then
1746 if not No_Main_Subprogram and then Bind_Main_Program then
1747 WBI (" __gnat_initialize ((void *)SEH);");
1748 else
1749 WBI (" __gnat_initialize ((void *)0);");
1750 end if;
1751 end if;
1753 WBI (" " & Ada_Init_Name.all & " ();");
1755 if not No_Main_Subprogram then
1756 WBI (" __gnat_break_start ();");
1757 WBI (" ");
1759 -- Output main program name
1761 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1763 -- Main program is procedure case
1765 if ALIs.Table (ALIs.First).Main_Program = Proc then
1766 Set_String (" ");
1767 Set_Main_Program_Name;
1768 Set_String (" ();");
1769 Write_Statement_Buffer;
1771 -- Main program is function case
1773 else -- ALIs.Table (ALIs_First).Main_Program = Func
1774 Set_String (" result = ");
1775 Set_Main_Program_Name;
1776 Set_String (" ();");
1777 Write_Statement_Buffer;
1778 end if;
1780 end if;
1782 -- Call adafinal if finalization active
1784 if not Cumulative_Restrictions.Set (No_Finalization) then
1785 WBI (" ");
1786 WBI (" system__standard_library__adafinal ();");
1787 end if;
1789 -- Outputs the dynamic stack measurement if needed
1791 if Dynamic_Stack_Measurement then
1792 WBI (" __gnat_stack_usage_output_results ();");
1793 end if;
1795 -- The finalize routine is used only if we have a run-time
1797 if not Suppress_Standard_Library_On_Target then
1798 WBI (" __gnat_finalize ();");
1799 end if;
1801 -- Case of main program is a function, so the value it returns
1802 -- is the exit status in this case.
1804 if ALIs.Table (ALIs.First).Main_Program = Func then
1805 if Exit_Status_Supported_On_Target then
1807 -- VMS must use Posix exit routine in order to get the effect
1808 -- of a Unix compatible setting of the program exit status.
1809 -- For all other systems, we use the standard exit routine.
1811 if OpenVMS_On_Target then
1812 WBI (" decc$__posix_exit (result);");
1813 else
1814 WBI (" exit (result);");
1815 end if;
1816 end if;
1818 -- Case of main program is a procedure, in which case the exit
1819 -- status is whatever was set by a Set_Exit call most recently
1821 else
1822 if Exit_Status_Supported_On_Target then
1824 -- VMS must use Posix exit routine in order to get the effect
1825 -- of a Unix compatible setting of the program exit status.
1826 -- For all other systems, we use the standard exit routine.
1828 if OpenVMS_On_Target then
1829 WBI (" decc$__posix_exit (gnat_exit_status);");
1830 else
1831 WBI (" exit (gnat_exit_status);");
1832 end if;
1833 end if;
1834 end if;
1836 WBI ("}");
1837 end Gen_Main_C;
1839 ------------------------------
1840 -- Gen_Object_Files_Options --
1841 ------------------------------
1843 procedure Gen_Object_Files_Options is
1844 Lgnat : Natural;
1845 -- This keeps track of the position in the sorted set of entries
1846 -- in the Linker_Options table of where the first entry from an
1847 -- internal file appears.
1849 Linker_Option_List_Started : Boolean := False;
1850 -- Set to True when "LINKER OPTION LIST" is displayed
1852 procedure Write_Linker_Option;
1853 -- Write binder info linker option
1855 -------------------------
1856 -- Write_Linker_Option --
1857 -------------------------
1859 procedure Write_Linker_Option is
1860 Start : Natural;
1861 Stop : Natural;
1863 begin
1864 -- Loop through string, breaking at null's
1866 Start := 1;
1867 while Start < Name_Len loop
1869 -- Find null ending this section
1871 Stop := Start + 1;
1872 while Name_Buffer (Stop) /= ASCII.NUL
1873 and then Stop <= Name_Len loop
1874 Stop := Stop + 1;
1875 end loop;
1877 -- Process section if non-null
1879 if Stop > Start then
1880 if Output_Linker_Option_List then
1881 if not Zero_Formatting then
1882 if not Linker_Option_List_Started then
1883 Linker_Option_List_Started := True;
1884 Write_Eol;
1885 Write_Str (" LINKER OPTION LIST");
1886 Write_Eol;
1887 Write_Eol;
1888 end if;
1890 Write_Str (" ");
1891 end if;
1893 Write_Str (Name_Buffer (Start .. Stop - 1));
1894 Write_Eol;
1895 end if;
1896 Write_Info_Ada_C
1897 (" -- ", "", Name_Buffer (Start .. Stop - 1));
1898 end if;
1900 Start := Stop + 1;
1901 end loop;
1902 end Write_Linker_Option;
1904 -- Start of processing for Gen_Object_Files_Options
1906 begin
1907 WBI ("");
1908 Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
1910 for E in Elab_Order.First .. Elab_Order.Last loop
1912 -- If not spec that has an associated body, then generate a
1913 -- comment giving the name of the corresponding object file.
1915 if (not Units.Table (Elab_Order.Table (E)).SAL_Interface)
1916 and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1917 then
1918 Get_Name_String
1919 (ALIs.Table
1920 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1922 -- If the presence of an object file is necessary or if it
1923 -- exists, then use it.
1925 if not Hostparm.Exclude_Missing_Objects
1926 or else
1927 System.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1928 then
1929 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1931 if Output_Object_List then
1932 Write_Str (Name_Buffer (1 .. Name_Len));
1933 Write_Eol;
1934 end if;
1936 -- Don't link with the shared library on VMS if an internal
1937 -- filename object is seen. Multiply defined symbols will
1938 -- result.
1940 if OpenVMS_On_Target
1941 and then Is_Internal_File_Name
1942 (ALIs.Table
1943 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
1944 then
1945 -- Special case for g-trasym.obj, which is not included
1946 -- in libgnat.
1948 Get_Name_String (ALIs.Table
1949 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
1951 if Name_Buffer (1 .. 8) /= "g-trasym" then
1952 Opt.Shared_Libgnat := False;
1953 end if;
1954 end if;
1955 end if;
1956 end if;
1957 end loop;
1959 -- Add a "-Ldir" for each directory in the object path
1961 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1962 declare
1963 Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1964 begin
1965 Name_Len := 0;
1966 Add_Str_To_Name_Buffer ("-L");
1967 Add_Str_To_Name_Buffer (Dir.all);
1968 Write_Linker_Option;
1969 end;
1970 end loop;
1972 -- Sort linker options
1974 -- This sort accomplishes two important purposes:
1976 -- a) All application files are sorted to the front, and all
1977 -- GNAT internal files are sorted to the end. This results
1978 -- in a well defined dividing line between the two sets of
1979 -- files, for the purpose of inserting certain standard
1980 -- library references into the linker arguments list.
1982 -- b) Given two different units, we sort the linker options so
1983 -- that those from a unit earlier in the elaboration order
1984 -- comes later in the list. This is a heuristic designed
1985 -- to create a more friendly order of linker options when
1986 -- the operations appear in separate units. The idea is that
1987 -- if unit A must be elaborated before unit B, then it is
1988 -- more likely that B references libraries included by A,
1989 -- than vice versa, so we want the libraries included by
1990 -- A to come after the libraries included by B.
1992 -- These two criteria are implemented by function Lt_Linker_Option.
1993 -- Note that a special case of b) is that specs are elaborated before
1994 -- bodies, so linker options from specs come after linker options
1995 -- for bodies, and again, the assumption is that libraries used by
1996 -- the body are more likely to reference libraries used by the spec,
1997 -- than vice versa.
1999 Sort
2000 (Linker_Options.Last,
2001 Move_Linker_Option'Access,
2002 Lt_Linker_Option'Access);
2004 -- Write user linker options, i.e. the set of linker options that
2005 -- come from all files other than GNAT internal files, Lgnat is
2006 -- left set to point to the first entry from a GNAT internal file,
2007 -- or past the end of the entriers if there are no internal files.
2009 Lgnat := Linker_Options.Last + 1;
2011 for J in 1 .. Linker_Options.Last loop
2012 if not Linker_Options.Table (J).Internal_File then
2013 Get_Name_String (Linker_Options.Table (J).Name);
2014 Write_Linker_Option;
2015 else
2016 Lgnat := J;
2017 exit;
2018 end if;
2019 end loop;
2021 -- Now we insert standard linker options that must appear after the
2022 -- entries from user files, and before the entries from GNAT run-time
2023 -- files. The reason for this decision is that libraries referenced
2024 -- by internal routines may reference these standard library entries.
2026 -- Note that we do not insert anything when pragma No_Run_Time has been
2027 -- specified or when the standard libraries are not to be used,
2028 -- otherwise on some platforms, such as VMS, we may get duplicate
2029 -- symbols when linking.
2031 if not (Opt.No_Run_Time_Mode or else Opt.No_Stdlib) then
2032 Name_Len := 0;
2034 if Opt.Shared_Libgnat then
2035 Add_Str_To_Name_Buffer ("-shared");
2036 else
2037 Add_Str_To_Name_Buffer ("-static");
2038 end if;
2040 -- Write directly to avoid -K output (why???)
2042 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
2044 if With_DECGNAT then
2045 Name_Len := 0;
2047 if Opt.Shared_Libgnat then
2048 Add_Str_To_Name_Buffer (Shared_Lib ("decgnat"));
2049 else
2050 Add_Str_To_Name_Buffer ("-ldecgnat");
2051 end if;
2053 Write_Linker_Option;
2054 end if;
2056 if With_GNARL then
2057 Name_Len := 0;
2059 if Opt.Shared_Libgnat then
2060 Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2061 else
2062 Add_Str_To_Name_Buffer ("-lgnarl");
2063 end if;
2065 Write_Linker_Option;
2066 end if;
2068 Name_Len := 0;
2070 if Opt.Shared_Libgnat then
2071 Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2072 else
2073 Add_Str_To_Name_Buffer ("-lgnat");
2074 end if;
2076 Write_Linker_Option;
2077 end if;
2079 -- Write linker options from all internal files
2081 for J in Lgnat .. Linker_Options.Last loop
2082 Get_Name_String (Linker_Options.Table (J).Name);
2083 Write_Linker_Option;
2084 end loop;
2086 if Output_Linker_Option_List and then not Zero_Formatting then
2087 Write_Eol;
2088 end if;
2090 if Ada_Bind_File then
2091 WBI ("-- END Object file/option list ");
2092 else
2093 WBI (" END Object file/option list */");
2094 end if;
2095 end Gen_Object_Files_Options;
2097 ---------------------
2098 -- Gen_Output_File --
2099 ---------------------
2101 procedure Gen_Output_File (Filename : String) is
2102 begin
2103 -- Acquire settings for Interrupt_State pragmas
2105 Set_IS_Pragma_Table;
2107 -- Acquire settings for Priority_Specific_Dispatching pragma
2109 Set_PSD_Pragma_Table;
2111 -- Override Ada_Bind_File and Bind_Main_Program for VMs since
2112 -- JGNAT only supports Ada code, and the main program is already
2113 -- generated by the compiler.
2115 if VM_Target /= No_VM then
2116 Ada_Bind_File := True;
2118 if VM_Target = JVM_Target then
2119 Bind_Main_Program := False;
2120 end if;
2121 end if;
2123 -- Override time slice value if -T switch is set
2125 if Time_Slice_Set then
2126 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2127 end if;
2129 -- Count number of elaboration calls
2131 for E in Elab_Order.First .. Elab_Order.Last loop
2132 if Units.Table (Elab_Order.Table (E)).No_Elab then
2133 null;
2134 else
2135 Num_Elab_Calls := Num_Elab_Calls + 1;
2136 end if;
2137 end loop;
2139 -- Generate output file in appropriate language
2141 Check_System_Restrictions_Used;
2143 if Ada_Bind_File then
2144 Gen_Output_File_Ada (Filename);
2145 else
2146 Gen_Output_File_C (Filename);
2147 end if;
2148 end Gen_Output_File;
2150 -------------------------
2151 -- Gen_Output_File_Ada --
2152 -------------------------
2154 procedure Gen_Output_File_Ada (Filename : String) is
2156 Bfiles : Name_Id;
2157 -- Name of generated bind file (spec)
2159 Bfileb : Name_Id;
2160 -- Name of generated bind file (body)
2162 Ada_Main : constant String := Get_Ada_Main_Name;
2163 -- Name to be used for generated Ada main program. See the body of
2164 -- function Get_Ada_Main_Name for details on the form of the name.
2166 begin
2167 -- Create spec first
2169 Create_Binder_Output (Filename, 's', Bfiles);
2171 -- We always compile the binder file in Ada 95 mode so that we properly
2172 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2173 -- of the Ada 2005 constructs are needed by the binder file.
2175 WBI ("pragma Ada_95;");
2177 -- If we are operating in Restrictions (No_Exception_Handlers) mode,
2178 -- then we need to make sure that the binder program is compiled with
2179 -- the same restriction, so that no exception tables are generated.
2181 if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2182 WBI ("pragma Restrictions (No_Exception_Handlers);");
2183 end if;
2185 -- Same processing for Restrictions (No_Exception_Propagation)
2187 if Cumulative_Restrictions.Set (No_Exception_Propagation) then
2188 WBI ("pragma Restrictions (No_Exception_Propagation);");
2189 end if;
2191 -- Same processing for pragma No_Run_Time
2193 if No_Run_Time_Mode then
2194 WBI ("pragma No_Run_Time;");
2195 end if;
2197 -- Generate with of System so we can reference System.Address
2199 WBI ("with System;");
2201 -- Generate with of System.Initialize_Scalars if active
2203 if Initialize_Scalars_Used then
2204 WBI ("with System.Scalar_Values;");
2205 end if;
2207 -- Generate with of System.Secondary_Stack if active
2209 if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2210 WBI ("with System.Secondary_Stack;");
2211 end if;
2213 Resolve_Binder_Options;
2215 if VM_Target /= No_VM then
2216 if not Suppress_Standard_Library_On_Target then
2218 -- Usually, adafinal is called using a pragma Import C. Since
2219 -- Import C doesn't have the same semantics for JGNAT, we use
2220 -- standard Ada.
2222 WBI ("with System.Standard_Library;");
2223 end if;
2224 end if;
2226 WBI ("package " & Ada_Main & " is");
2227 WBI (" pragma Warnings (Off);");
2229 -- Main program case
2231 if Bind_Main_Program then
2232 if VM_Target = No_VM then
2234 -- Generate argc/argv stuff unless suppressed
2236 if Command_Line_Args_On_Target
2237 or not Configurable_Run_Time_On_Target
2238 then
2239 WBI ("");
2240 WBI (" gnat_argc : Integer;");
2241 WBI (" gnat_argv : System.Address;");
2242 WBI (" gnat_envp : System.Address;");
2244 -- If the standard library is not suppressed, these variables
2245 -- are in the runtime data area for easy access from the
2246 -- runtime
2248 if not Suppress_Standard_Library_On_Target then
2249 WBI ("");
2250 WBI (" pragma Import (C, gnat_argc);");
2251 WBI (" pragma Import (C, gnat_argv);");
2252 WBI (" pragma Import (C, gnat_envp);");
2253 end if;
2254 end if;
2256 -- Define exit status. Again in normal mode, this is in the
2257 -- run-time library, and is initialized there, but in the
2258 -- configurable runtime case, the variable is declared and
2259 -- initialized in this file.
2261 WBI ("");
2263 if Configurable_Run_Time_Mode then
2264 if Exit_Status_Supported_On_Target then
2265 WBI (" gnat_exit_status : Integer := 0;");
2266 end if;
2268 else
2269 WBI (" gnat_exit_status : Integer;");
2270 WBI (" pragma Import (C, gnat_exit_status);");
2271 end if;
2273 -- Generate the GNAT_Version and Ada_Main_Program_Name info only
2274 -- for the main program. Otherwise, it can lead under some
2275 -- circumstances to a symbol duplication during the link (for
2276 -- instance when a C program uses 2 Ada libraries)
2277 end if;
2279 WBI ("");
2280 WBI (" GNAT_Version : constant String :=");
2281 WBI (" ""GNAT Version: " &
2282 Gnat_Version_String & """;");
2283 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
2285 WBI ("");
2286 Set_String (" Ada_Main_Program_Name : constant String := """);
2287 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2289 if VM_Target = No_VM then
2290 Set_Main_Program_Name;
2291 Set_String (""" & ASCII.NUL;");
2292 else
2293 Set_String (Name_Buffer (1 .. Name_Len - 2) & """;");
2294 end if;
2296 Write_Statement_Buffer;
2299 (" pragma Export (C, Ada_Main_Program_Name, " &
2300 """__gnat_ada_main_program_name"");");
2301 end if;
2303 WBI ("");
2304 WBI (" procedure " & Ada_Final_Name.all & ";");
2305 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
2306 Ada_Final_Name.all & """);");
2307 WBI ("");
2308 WBI (" procedure " & Ada_Init_Name.all & ";");
2309 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
2310 Ada_Init_Name.all & """);");
2312 -- If -a has been specified use pragma Linker_Constructor for the init
2313 -- procedure. No need to use a similar pragma for the final procedure as
2314 -- global finalization will occur when the executable finishes execution
2315 -- and for plugins (shared stand-alone libraries that can be
2316 -- "unloaded"), finalization should not occur automatically, otherwise
2317 -- the main executable may not continue to work properly.
2319 if Use_Pragma_Linker_Constructor then
2320 WBI (" pragma Linker_Constructor (" & Ada_Init_Name.all & ");");
2321 end if;
2323 if Bind_Main_Program and then VM_Target = No_VM then
2325 -- If we have the standard library, then Break_Start is defined
2326 -- there, but when the standard library is suppressed, Break_Start
2327 -- is defined here.
2329 WBI ("");
2330 WBI (" procedure Break_Start;");
2332 if Suppress_Standard_Library_On_Target then
2333 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
2334 else
2335 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
2336 end if;
2338 WBI ("");
2340 if Exit_Status_Supported_On_Target then
2341 Set_String (" function ");
2342 else
2343 Set_String (" procedure ");
2344 end if;
2346 Set_String (Get_Main_Name);
2348 -- Generate argument list if present
2350 if Command_Line_Args_On_Target then
2351 Write_Statement_Buffer;
2352 WBI (" (argc : Integer;");
2353 WBI (" argv : System.Address;");
2354 Set_String
2355 (" envp : System.Address)");
2357 if Exit_Status_Supported_On_Target then
2358 Write_Statement_Buffer;
2359 WBI (" return Integer;");
2360 else
2361 Write_Statement_Buffer (";");
2362 end if;
2364 else
2365 if Exit_Status_Supported_On_Target then
2366 Write_Statement_Buffer (" return Integer;");
2367 else
2368 Write_Statement_Buffer (";");
2369 end if;
2370 end if;
2372 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
2373 Get_Main_Name & """);");
2374 end if;
2376 Gen_Versions_Ada;
2377 Gen_Elab_Order_Ada;
2379 -- Spec is complete
2381 WBI ("");
2382 WBI ("end " & Ada_Main & ";");
2383 Close_Binder_Output;
2385 -- Prepare to write body
2387 Create_Binder_Output (Filename, 'b', Bfileb);
2389 -- We always compile the binder file in Ada 95 mode so that we properly
2390 -- handle use of Ada 2005 keywords as identifiers in Ada 95 mode. None
2391 -- of the Ada 2005 constructs are needed by the binder file.
2393 WBI ("pragma Ada_95;");
2395 -- Output Source_File_Name pragmas which look like
2397 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2398 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2400 -- where sss/bbb are the spec/body file names respectively
2402 Get_Name_String (Bfiles);
2403 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2405 WBI ("pragma Source_File_Name (" &
2406 Ada_Main &
2407 ", Spec_File_Name => """ &
2408 Name_Buffer (1 .. Name_Len + 3));
2410 Get_Name_String (Bfileb);
2411 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2413 WBI ("pragma Source_File_Name (" &
2414 Ada_Main &
2415 ", Body_File_Name => """ &
2416 Name_Buffer (1 .. Name_Len + 3));
2418 -- Generate with of System.Restrictions to initialize
2419 -- Run_Time_Restrictions.
2421 if System_Restrictions_Used
2422 and not Suppress_Standard_Library_On_Target
2423 then
2424 WBI ("");
2425 WBI ("with System.Restrictions;");
2426 end if;
2428 WBI ("");
2429 WBI ("package body " & Ada_Main & " is");
2430 WBI (" pragma Warnings (Off);");
2432 -- Import the finalization procedure only if finalization active
2434 if not Cumulative_Restrictions.Set (No_Finalization) then
2436 -- In the Java case, pragma Import C cannot be used, so the
2437 -- standard Ada constructs will be used instead.
2439 if VM_Target = No_VM then
2440 WBI ("");
2441 WBI (" procedure Do_Finalize;");
2443 (" pragma Import (C, Do_Finalize, " &
2444 """system__standard_library__adafinal"");");
2445 WBI ("");
2446 end if;
2447 end if;
2449 if not Suppress_Standard_Library_On_Target then
2451 -- Generate Priority_Specific_Dispatching pragma string
2453 Set_String
2454 (" Local_Priority_Specific_Dispatching : constant String := """);
2456 for J in 0 .. PSD_Pragma_Settings.Last loop
2457 Set_Char (PSD_Pragma_Settings.Table (J));
2458 end loop;
2460 Set_String (""";");
2461 Write_Statement_Buffer;
2463 -- Generate Interrupt_State pragma string
2465 Set_String (" Local_Interrupt_States : constant String := """);
2467 for J in 0 .. IS_Pragma_Settings.Last loop
2468 Set_Char (IS_Pragma_Settings.Table (J));
2469 end loop;
2471 Set_String (""";");
2472 Write_Statement_Buffer;
2473 WBI ("");
2474 end if;
2476 Gen_Adainit_Ada;
2478 Gen_Adafinal_Ada;
2480 if Bind_Main_Program and then VM_Target = No_VM then
2482 -- When suppressing the standard library then generate dummy body
2483 -- for Break_Start
2485 if Suppress_Standard_Library_On_Target then
2486 WBI ("");
2487 WBI (" procedure Break_Start is");
2488 WBI (" begin");
2489 WBI (" null;");
2490 WBI (" end;");
2491 end if;
2493 Gen_Main_Ada;
2494 end if;
2496 -- Output object file list and the Ada body is complete
2498 Gen_Object_Files_Options;
2500 WBI ("");
2501 WBI ("end " & Ada_Main & ";");
2503 Close_Binder_Output;
2504 end Gen_Output_File_Ada;
2506 -----------------------
2507 -- Gen_Output_File_C --
2508 -----------------------
2510 procedure Gen_Output_File_C (Filename : String) is
2511 Bfile : Name_Id;
2512 pragma Warnings (Off, Bfile);
2513 -- Name of generated bind file (not referenced)
2515 begin
2516 Create_Binder_Output (Filename, 'c', Bfile);
2518 Resolve_Binder_Options;
2520 WBI ("extern void " & Ada_Final_Name.all & " (void);");
2522 -- If -a has been specified use __attribute__((constructor)) for the
2523 -- init procedure. No need to use a similar featute for the final
2524 -- procedure as global finalization will occur when the executable
2525 -- finishes execution and for plugins (shared stand-alone libraries that
2526 -- can be "unloaded"), finalization should not occur automatically,
2527 -- otherwise the main executable may not continue to work properly.
2529 if Use_Pragma_Linker_Constructor then
2530 WBI ("extern void " & Ada_Init_Name.all &
2531 " (void) __attribute__((constructor));");
2532 else
2533 WBI ("extern void " & Ada_Init_Name.all & " (void);");
2534 end if;
2536 WBI ("extern void system__standard_library__adafinal (void);");
2538 if not No_Main_Subprogram then
2539 Set_String ("extern ");
2541 if Exit_Status_Supported_On_Target then
2542 Set_String ("int");
2543 else
2544 Set_String ("void");
2545 end if;
2547 Set_String (" main ");
2549 if Command_Line_Args_On_Target then
2550 Write_Statement_Buffer ("(int, char **, char **);");
2551 else
2552 Write_Statement_Buffer ("(void);");
2553 end if;
2555 if OpenVMS_On_Target then
2556 WBI ("extern void decc$__posix_exit (int);");
2557 else
2558 WBI ("extern void exit (int);");
2559 end if;
2561 WBI ("extern void __gnat_break_start (void);");
2562 Set_String ("extern ");
2564 if ALIs.Table (ALIs.First).Main_Program = Proc then
2565 Set_String ("void ");
2566 else
2567 Set_String ("int ");
2568 end if;
2570 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2571 Set_Main_Program_Name;
2572 Set_String (" (void);");
2573 Write_Statement_Buffer;
2574 end if;
2576 if not Suppress_Standard_Library_On_Target then
2577 WBI ("extern void __gnat_initialize (void *);");
2578 WBI ("extern void __gnat_finalize (void);");
2579 WBI ("extern void __gnat_install_handler (void);");
2580 end if;
2582 if Dynamic_Stack_Measurement then
2583 WBI ("");
2584 WBI ("extern void __gnat_stack_usage_output_results (void);");
2585 WBI ("extern void __gnat_stack_usage_initialize (int size);");
2586 end if;
2588 -- Initialize stack limit for the environment task if the stack
2589 -- check method is stack limit and stack check is enabled.
2591 if Stack_Check_Limits_On_Target
2592 and then (Stack_Check_Default_On_Target or Stack_Check_Switch_Set)
2593 then
2594 WBI ("");
2595 WBI ("extern void __gnat_initialize_stack_limit (void);");
2596 end if;
2598 WBI ("");
2600 Gen_Elab_Defs_C;
2602 -- Imported variable used to track elaboration/finalization phase.
2603 -- Used only when we have a runtime.
2605 if not Suppress_Standard_Library_On_Target then
2606 WBI ("extern int __gnat_handler_installed;");
2607 WBI ("");
2608 end if;
2610 -- Write argv/argc exit status stuff if main program case
2612 if Bind_Main_Program then
2614 -- First deal with argc/argv/envp. In the normal case they
2615 -- are in the run-time library.
2617 if not Configurable_Run_Time_On_Target then
2618 WBI ("extern int gnat_argc;");
2619 WBI ("extern char **gnat_argv;");
2620 WBI ("extern char **gnat_envp;");
2622 -- If configurable run time and no command line args, then the
2623 -- generation of these variables is entirely suppressed.
2625 elsif not Command_Line_Args_On_Target then
2626 null;
2628 -- Otherwise, in the configurable run-time case they are right in
2629 -- the binder file.
2631 else
2632 WBI ("int gnat_argc;");
2633 WBI ("char **gnat_argv;");
2634 WBI ("char **gnat_envp;");
2635 end if;
2637 -- Similarly deal with exit status
2638 -- are in the run-time library.
2640 if not Configurable_Run_Time_On_Target then
2641 WBI ("extern int gnat_exit_status;");
2643 -- If configurable run time and no exit status on target, then
2644 -- the generation of this variables is entirely suppressed.
2646 elsif not Exit_Status_Supported_On_Target then
2647 null;
2649 -- Otherwise, in the configurable run-time case this variable is
2650 -- right in the binder file, and initialized to zero there.
2652 else
2653 WBI ("int gnat_exit_status = 0;");
2654 end if;
2656 WBI ("");
2657 end if;
2659 -- When suppressing the standard library, the __gnat_break_start
2660 -- routine (for the debugger to get initial control) is defined in
2661 -- this file.
2663 if Suppress_Standard_Library_On_Target then
2664 WBI ("");
2665 WBI ("void __gnat_break_start (void) {}");
2666 end if;
2668 -- Generate the __gnat_version and __gnat_ada_main_program_name info
2669 -- only for the main program. Otherwise, it can lead under some
2670 -- circumstances to a symbol duplication during the link (for instance
2671 -- when a C program uses 2 Ada libraries)
2673 if Bind_Main_Program then
2674 WBI ("");
2675 WBI ("char __gnat_version[] = ""GNAT Version: " &
2676 Gnat_Version_String & """;");
2678 Set_String ("char __gnat_ada_main_program_name[] = """);
2679 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2680 Set_Main_Program_Name;
2681 Set_String (""";");
2682 Write_Statement_Buffer;
2683 end if;
2685 -- Generate the adafinal routine. In no runtime mode, this is
2686 -- not needed, since there is no finalization to do.
2688 if not Cumulative_Restrictions.Set (No_Finalization) then
2689 Gen_Adafinal_C;
2690 end if;
2692 Gen_Adainit_C;
2694 -- Main is only present for Ada main case
2696 if Bind_Main_Program then
2697 Gen_Main_C;
2698 end if;
2700 -- Generate versions, elaboration order, list of object files
2702 Gen_Versions_C;
2703 Gen_Elab_Order_C;
2704 Gen_Object_Files_Options;
2706 -- C binder output is complete
2708 Close_Binder_Output;
2709 end Gen_Output_File_C;
2711 --------------------------
2712 -- Gen_Restrictions_Ada --
2713 --------------------------
2715 procedure Gen_Restrictions_Ada is
2716 Count : Integer;
2718 begin
2719 if Suppress_Standard_Library_On_Target
2720 or not System_Restrictions_Used
2721 then
2722 return;
2723 end if;
2725 WBI (" System.Restrictions.Run_Time_Restrictions :=");
2726 WBI (" (Set =>");
2727 Set_String (" (");
2729 Count := 0;
2731 for J in Cumulative_Restrictions.Set'First ..
2732 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2733 loop
2734 Set_Boolean (Cumulative_Restrictions.Set (J));
2735 Set_String (", ");
2736 Count := Count + 1;
2738 if Count = 8 then
2739 Write_Statement_Buffer;
2740 Set_String (" ");
2741 Count := 0;
2742 end if;
2743 end loop;
2745 Set_Boolean
2746 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last));
2747 Set_String ("),");
2748 Write_Statement_Buffer;
2749 Set_String (" Value => (");
2751 for J in Cumulative_Restrictions.Value'First ..
2752 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2753 loop
2754 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2755 Set_String (", ");
2756 end loop;
2758 Set_Int (Int (Cumulative_Restrictions.Value
2759 (Cumulative_Restrictions.Value'Last)));
2760 Set_String ("),");
2761 Write_Statement_Buffer;
2762 WBI (" Violated =>");
2763 Set_String (" (");
2764 Count := 0;
2766 for J in Cumulative_Restrictions.Violated'First ..
2767 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2768 loop
2769 Set_Boolean (Cumulative_Restrictions.Violated (J));
2770 Set_String (", ");
2771 Count := Count + 1;
2773 if Count = 8 then
2774 Write_Statement_Buffer;
2775 Set_String (" ");
2776 Count := 0;
2777 end if;
2778 end loop;
2780 Set_Boolean (Cumulative_Restrictions.Violated
2781 (Cumulative_Restrictions.Violated'Last));
2782 Set_String ("),");
2783 Write_Statement_Buffer;
2784 Set_String (" Count => (");
2786 for J in Cumulative_Restrictions.Count'First ..
2787 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2788 loop
2789 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2790 Set_String (", ");
2791 end loop;
2793 Set_Int (Int (Cumulative_Restrictions.Count
2794 (Cumulative_Restrictions.Count'Last)));
2795 Set_String ("),");
2796 Write_Statement_Buffer;
2797 Set_String (" Unknown => (");
2799 for J in Cumulative_Restrictions.Unknown'First ..
2800 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2801 loop
2802 Set_Boolean (Cumulative_Restrictions.Unknown (J));
2803 Set_String (", ");
2804 end loop;
2806 Set_Boolean
2807 (Cumulative_Restrictions.Unknown
2808 (Cumulative_Restrictions.Unknown'Last));
2809 Set_String ("));");
2810 Write_Statement_Buffer;
2811 end Gen_Restrictions_Ada;
2813 ------------------------
2814 -- Gen_Restrictions_C --
2815 ------------------------
2817 procedure Gen_Restrictions_C is
2818 begin
2819 if Suppress_Standard_Library_On_Target
2820 or not System_Restrictions_Used
2821 then
2822 return;
2823 end if;
2825 WBI (" typedef struct {");
2826 Set_String (" char set [");
2827 Set_Int (Cumulative_Restrictions.Set'Length);
2828 Set_String ("];");
2829 Write_Statement_Buffer;
2831 Set_String (" int value [");
2832 Set_Int (Cumulative_Restrictions.Value'Length);
2833 Set_String ("];");
2834 Write_Statement_Buffer;
2836 Set_String (" char violated [");
2837 Set_Int (Cumulative_Restrictions.Violated'Length);
2838 Set_String ("];");
2839 Write_Statement_Buffer;
2841 Set_String (" int count [");
2842 Set_Int (Cumulative_Restrictions.Count'Length);
2843 Set_String ("];");
2844 Write_Statement_Buffer;
2846 Set_String (" char unknown [");
2847 Set_Int (Cumulative_Restrictions.Unknown'Length);
2848 Set_String ("];");
2849 Write_Statement_Buffer;
2850 WBI (" } restrictions;");
2851 WBI (" extern restrictions " &
2852 "system__restrictions__run_time_restrictions;");
2853 WBI (" restrictions r = {");
2854 Set_String (" {");
2856 for J in Cumulative_Restrictions.Set'First ..
2857 Restriction_Id'Pred (Cumulative_Restrictions.Set'Last)
2858 loop
2859 Set_Int (Boolean'Pos (Cumulative_Restrictions.Set (J)));
2860 Set_String (", ");
2861 end loop;
2863 Set_Int (Boolean'Pos
2864 (Cumulative_Restrictions.Set (Cumulative_Restrictions.Set'Last)));
2865 Set_String ("},");
2866 Write_Statement_Buffer;
2867 Set_String (" {");
2869 for J in Cumulative_Restrictions.Value'First ..
2870 Restriction_Id'Pred (Cumulative_Restrictions.Value'Last)
2871 loop
2872 Set_Int (Int (Cumulative_Restrictions.Value (J)));
2873 Set_String (", ");
2874 end loop;
2876 Set_Int (Int (Cumulative_Restrictions.Value
2877 (Cumulative_Restrictions.Value'Last)));
2878 Set_String ("},");
2879 Write_Statement_Buffer;
2880 Set_String (" {");
2882 for J in Cumulative_Restrictions.Violated'First ..
2883 Restriction_Id'Pred (Cumulative_Restrictions.Violated'Last)
2884 loop
2885 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated (J)));
2886 Set_String (", ");
2887 end loop;
2889 Set_Int (Boolean'Pos (Cumulative_Restrictions.Violated
2890 (Cumulative_Restrictions.Violated'Last)));
2891 Set_String ("},");
2892 Write_Statement_Buffer;
2893 Set_String (" {");
2895 for J in Cumulative_Restrictions.Count'First ..
2896 Restriction_Id'Pred (Cumulative_Restrictions.Count'Last)
2897 loop
2898 Set_Int (Int (Cumulative_Restrictions.Count (J)));
2899 Set_String (", ");
2900 end loop;
2902 Set_Int (Int (Cumulative_Restrictions.Count
2903 (Cumulative_Restrictions.Count'Last)));
2904 Set_String ("},");
2905 Write_Statement_Buffer;
2906 Set_String (" {");
2908 for J in Cumulative_Restrictions.Unknown'First ..
2909 Restriction_Id'Pred (Cumulative_Restrictions.Unknown'Last)
2910 loop
2911 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown (J)));
2912 Set_String (", ");
2913 end loop;
2915 Set_Int (Boolean'Pos (Cumulative_Restrictions.Unknown
2916 (Cumulative_Restrictions.Unknown'Last)));
2917 Set_String ("}};");
2918 Write_Statement_Buffer;
2919 WBI (" system__restrictions__run_time_restrictions = r;");
2920 end Gen_Restrictions_C;
2922 ----------------------
2923 -- Gen_Versions_Ada --
2924 ----------------------
2926 -- This routine generates two sets of lines. The first set has the form:
2928 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2930 -- The second set has the form
2932 -- pragma Export (C, unnnnn, unam);
2934 -- for each unit, where unam is the unit name suffixed by either B or
2935 -- S for body or spec, with dots replaced by double underscores, and
2936 -- hhhhhhhh is the version number, and nnnnn is a 5-digits serial number.
2938 procedure Gen_Versions_Ada is
2939 Ubuf : String (1 .. 6) := "u00000";
2941 procedure Increment_Ubuf;
2942 -- Little procedure to increment the serial number
2944 procedure Increment_Ubuf is
2945 begin
2946 for J in reverse Ubuf'Range loop
2947 Ubuf (J) := Character'Succ (Ubuf (J));
2948 exit when Ubuf (J) <= '9';
2949 Ubuf (J) := '0';
2950 end loop;
2951 end Increment_Ubuf;
2953 -- Start of processing for Gen_Versions_Ada
2955 begin
2956 if Bind_For_Library then
2958 -- When building libraries, the version number of each unit can
2959 -- not be computed, since the binder does not know the full list
2960 -- of units. Therefore, the 'Version and 'Body_Version
2961 -- attributes cannot supported in this case.
2963 return;
2964 end if;
2966 WBI ("");
2968 WBI (" type Version_32 is mod 2 ** 32;");
2969 for U in Units.First .. Units.Last loop
2970 Increment_Ubuf;
2971 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2972 Units.Table (U).Version & "#;");
2973 end loop;
2975 WBI ("");
2976 Ubuf := "u00000";
2978 for U in Units.First .. Units.Last loop
2979 Increment_Ubuf;
2980 Set_String (" pragma Export (C, ");
2981 Set_String (Ubuf);
2982 Set_String (", """);
2984 Get_Name_String (Units.Table (U).Uname);
2986 for K in 1 .. Name_Len loop
2987 if Name_Buffer (K) = '.' then
2988 Set_Char ('_');
2989 Set_Char ('_');
2991 elsif Name_Buffer (K) = '%' then
2992 exit;
2994 else
2995 Set_Char (Name_Buffer (K));
2996 end if;
2997 end loop;
2999 if Name_Buffer (Name_Len) = 's' then
3000 Set_Char ('S');
3001 else
3002 Set_Char ('B');
3003 end if;
3005 Set_String (""");");
3006 Write_Statement_Buffer;
3007 end loop;
3009 end Gen_Versions_Ada;
3011 --------------------
3012 -- Gen_Versions_C --
3013 --------------------
3015 -- This routine generates a line of the form:
3017 -- unsigned unam = 0xhhhhhhhh;
3019 -- for each unit, where unam is the unit name suffixed by either B or
3020 -- S for body or spec, with dots replaced by double underscores.
3022 procedure Gen_Versions_C is
3023 begin
3024 if Bind_For_Library then
3026 -- When building libraries, the version number of each unit can
3027 -- not be computed, since the binder does not know the full list
3028 -- of units. Therefore, the 'Version and 'Body_Version
3029 -- attributes cannot supported.
3031 return;
3032 end if;
3034 for U in Units.First .. Units.Last loop
3035 Set_String ("unsigned ");
3037 Get_Name_String (Units.Table (U).Uname);
3039 for K in 1 .. Name_Len loop
3040 if Name_Buffer (K) = '.' then
3041 Set_String ("__");
3043 elsif Name_Buffer (K) = '%' then
3044 exit;
3046 else
3047 Set_Char (Name_Buffer (K));
3048 end if;
3049 end loop;
3051 if Name_Buffer (Name_Len) = 's' then
3052 Set_Char ('S');
3053 else
3054 Set_Char ('B');
3055 end if;
3057 Set_String (" = 0x");
3058 Set_String (Units.Table (U).Version);
3059 Set_Char (';');
3060 Write_Statement_Buffer;
3061 end loop;
3063 end Gen_Versions_C;
3065 ------------------------
3066 -- Get_Main_Unit_Name --
3067 ------------------------
3069 function Get_Main_Unit_Name (S : String) return String is
3070 Result : String := S;
3072 begin
3073 for J in S'Range loop
3074 if Result (J) = '.' then
3075 Result (J) := '_';
3076 end if;
3077 end loop;
3079 return Result;
3080 end Get_Main_Unit_Name;
3082 -----------------------
3083 -- Get_Ada_Main_Name --
3084 -----------------------
3086 function Get_Ada_Main_Name return String is
3087 Suffix : constant String := "_00";
3088 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
3089 Opt.Ada_Main_Name.all & Suffix;
3090 Nlen : Natural;
3092 begin
3093 -- The main program generated by JGNAT expects a package called
3094 -- ada_<main procedure>.
3096 if VM_Target /= No_VM then
3097 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3098 return "ada_" & Get_Main_Unit_Name (Name_Buffer (1 .. Name_Len - 2));
3099 end if;
3101 -- This loop tries the following possibilities in order
3102 -- <Ada_Main>
3103 -- <Ada_Main>_01
3104 -- <Ada_Main>_02
3105 -- ..
3106 -- <Ada_Main>_99
3107 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
3108 -- it is set to 'ada_main'.
3110 for J in 0 .. 99 loop
3111 if J = 0 then
3112 Nlen := Name'Length - Suffix'Length;
3113 else
3114 Nlen := Name'Length;
3115 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
3116 Name (Name'Last - 1) :=
3117 Character'Val (J / 10 + Character'Pos ('0'));
3118 end if;
3120 for K in ALIs.First .. ALIs.Last loop
3121 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
3123 -- Get unit name, removing %b or %e at end
3125 Get_Name_String (Units.Table (L).Uname);
3126 Name_Len := Name_Len - 2;
3128 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
3129 goto Continue;
3130 end if;
3131 end loop;
3132 end loop;
3134 return Name (1 .. Nlen);
3136 <<Continue>>
3137 null;
3138 end loop;
3140 -- If we fall through, just use a peculiar unlikely name
3142 return ("Qwertyuiop");
3143 end Get_Ada_Main_Name;
3145 -------------------
3146 -- Get_Main_Name --
3147 -------------------
3149 function Get_Main_Name return String is
3150 begin
3151 -- Explicit name given with -M switch
3153 if Bind_Alternate_Main_Name then
3154 return Alternate_Main_Name.all;
3156 -- Case of main program name to be used directly
3158 elsif Use_Ada_Main_Program_Name_On_Target then
3160 -- Get main program name
3162 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
3164 -- If this is a child name, return only the name of the child,
3165 -- since we can't have dots in a nested program name. Note that
3166 -- we do not include the %b at the end of the unit name.
3168 for J in reverse 1 .. Name_Len - 2 loop
3169 if J = 1 or else Name_Buffer (J - 1) = '.' then
3170 return Name_Buffer (J .. Name_Len - 2);
3171 end if;
3172 end loop;
3174 raise Program_Error; -- impossible exit
3176 -- Case where "main" is to be used as default
3178 else
3179 return "main";
3180 end if;
3181 end Get_Main_Name;
3183 ---------------------
3184 -- Get_WC_Encoding --
3185 ---------------------
3187 function Get_WC_Encoding return Character is
3188 begin
3189 -- If encoding method specified by -W switch, then return it
3191 if Wide_Character_Encoding_Method_Specified then
3192 return WC_Encoding_Letters (Wide_Character_Encoding_Method);
3194 -- If no main program, and not specified, set brackets, we really have
3195 -- no better choice. If some other encoding is required when there is
3196 -- no main, it must be set explicitly using -Wx.
3198 -- Note: if the ALI file always passed the wide character encoding
3199 -- of every file, then we could use the encoding of the initial
3200 -- specified file, but this information is passed only for potential
3201 -- main programs. We could fix this sometime, but it is a very minor
3202 -- point (wide character default encoding for [Wide_[Wide_]Text_IO
3203 -- when there is no main program).
3205 elsif No_Main_Subprogram then
3206 return 'b';
3208 -- Otherwise if there is a main program, take encoding from it
3210 else
3211 return ALIs.Table (ALIs.First).WC_Encoding;
3212 end if;
3213 end Get_WC_Encoding;
3215 ----------------------
3216 -- Lt_Linker_Option --
3217 ----------------------
3219 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
3220 begin
3221 -- Sort internal files last
3223 if Linker_Options.Table (Op1).Internal_File
3225 Linker_Options.Table (Op2).Internal_File
3226 then
3227 -- Note: following test uses False < True
3229 return Linker_Options.Table (Op1).Internal_File
3231 Linker_Options.Table (Op2).Internal_File;
3233 -- If both internal or both non-internal, sort according to the
3234 -- elaboration position. A unit that is elaborated later should
3235 -- come earlier in the linker options list.
3237 else
3238 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
3240 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
3242 end if;
3243 end Lt_Linker_Option;
3245 ------------------------
3246 -- Move_Linker_Option --
3247 ------------------------
3249 procedure Move_Linker_Option (From : Natural; To : Natural) is
3250 begin
3251 Linker_Options.Table (To) := Linker_Options.Table (From);
3252 end Move_Linker_Option;
3254 ----------------------------
3255 -- Resolve_Binder_Options --
3256 ----------------------------
3258 procedure Resolve_Binder_Options is
3259 begin
3260 for E in Elab_Order.First .. Elab_Order.Last loop
3261 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3263 -- This is not a perfect approach, but is the current protocol
3264 -- between the run-time and the binder to indicate that tasking
3265 -- is used: system.os_interface should always be used by any
3266 -- tasking application.
3268 if Name_Buffer (1 .. 19) = "system.os_interface" then
3269 With_GNARL := True;
3270 end if;
3272 -- Ditto for declib and the "dec" package
3274 if OpenVMS_On_Target and then Name_Buffer (1 .. 5) = "dec%s" then
3275 With_DECGNAT := True;
3276 end if;
3277 end loop;
3278 end Resolve_Binder_Options;
3280 -----------------
3281 -- Set_Boolean --
3282 -----------------
3284 procedure Set_Boolean (B : Boolean) is
3285 True_Str : constant String := "True";
3286 False_Str : constant String := "False";
3287 begin
3288 if B then
3289 Statement_Buffer (Last + 1 .. Last + True_Str'Length) := True_Str;
3290 Last := Last + True_Str'Length;
3291 else
3292 Statement_Buffer (Last + 1 .. Last + False_Str'Length) := False_Str;
3293 Last := Last + False_Str'Length;
3294 end if;
3295 end Set_Boolean;
3297 --------------
3298 -- Set_Char --
3299 --------------
3301 procedure Set_Char (C : Character) is
3302 begin
3303 Last := Last + 1;
3304 Statement_Buffer (Last) := C;
3305 end Set_Char;
3307 -------------
3308 -- Set_Int --
3309 -------------
3311 procedure Set_Int (N : Int) is
3312 begin
3313 if N < 0 then
3314 Set_String ("-");
3315 Set_Int (-N);
3317 else
3318 if N > 9 then
3319 Set_Int (N / 10);
3320 end if;
3322 Last := Last + 1;
3323 Statement_Buffer (Last) :=
3324 Character'Val (N mod 10 + Character'Pos ('0'));
3325 end if;
3326 end Set_Int;
3328 -------------------------
3329 -- Set_IS_Pragma_Table --
3330 -------------------------
3332 procedure Set_IS_Pragma_Table is
3333 begin
3334 for F in ALIs.First .. ALIs.Last loop
3335 for K in ALIs.Table (F).First_Interrupt_State ..
3336 ALIs.Table (F).Last_Interrupt_State
3337 loop
3338 declare
3339 Inum : constant Int :=
3340 Interrupt_States.Table (K).Interrupt_Id;
3341 Stat : constant Character :=
3342 Interrupt_States.Table (K).Interrupt_State;
3344 begin
3345 while IS_Pragma_Settings.Last < Inum loop
3346 IS_Pragma_Settings.Append ('n');
3347 end loop;
3349 IS_Pragma_Settings.Table (Inum) := Stat;
3350 end;
3351 end loop;
3352 end loop;
3353 end Set_IS_Pragma_Table;
3355 ---------------------------
3356 -- Set_Main_Program_Name --
3357 ---------------------------
3359 procedure Set_Main_Program_Name is
3360 begin
3361 -- Note that name has %b on the end which we ignore
3363 -- First we output the initial _ada_ since we know that the main
3364 -- program is a library level subprogram.
3366 Set_String ("_ada_");
3368 -- Copy name, changing dots to double underscores
3370 for J in 1 .. Name_Len - 2 loop
3371 if Name_Buffer (J) = '.' then
3372 Set_String ("__");
3373 else
3374 Set_Char (Name_Buffer (J));
3375 end if;
3376 end loop;
3377 end Set_Main_Program_Name;
3379 ---------------------
3380 -- Set_Name_Buffer --
3381 ---------------------
3383 procedure Set_Name_Buffer is
3384 begin
3385 for J in 1 .. Name_Len loop
3386 Set_Char (Name_Buffer (J));
3387 end loop;
3388 end Set_Name_Buffer;
3390 -------------------------
3391 -- Set_PSD_Pragma_Table --
3392 -------------------------
3394 procedure Set_PSD_Pragma_Table is
3395 begin
3396 for F in ALIs.First .. ALIs.Last loop
3397 for K in ALIs.Table (F).First_Specific_Dispatching ..
3398 ALIs.Table (F).Last_Specific_Dispatching
3399 loop
3400 declare
3401 DTK : Specific_Dispatching_Record
3402 renames Specific_Dispatching.Table (K);
3404 begin
3405 while PSD_Pragma_Settings.Last < DTK.Last_Priority loop
3406 PSD_Pragma_Settings.Append ('F');
3407 end loop;
3409 for Prio in DTK.First_Priority .. DTK.Last_Priority loop
3410 PSD_Pragma_Settings.Table (Prio) := DTK.Dispatching_Policy;
3411 end loop;
3412 end;
3413 end loop;
3414 end loop;
3415 end Set_PSD_Pragma_Table;
3417 ----------------
3418 -- Set_String --
3419 ----------------
3421 procedure Set_String (S : String) is
3422 begin
3423 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3424 Last := Last + S'Length;
3425 end Set_String;
3427 -------------------
3428 -- Set_Unit_Name --
3429 -------------------
3431 procedure Set_Unit_Name is
3432 begin
3433 for J in 1 .. Name_Len - 2 loop
3434 if Name_Buffer (J) /= '.' then
3435 Set_Char (Name_Buffer (J));
3436 else
3437 Set_String ("__");
3438 end if;
3439 end loop;
3440 end Set_Unit_Name;
3442 ---------------------
3443 -- Set_Unit_Number --
3444 ---------------------
3446 procedure Set_Unit_Number (U : Unit_Id) is
3447 Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3448 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
3450 begin
3451 if Num_Units >= 10 and then Unum < 10 then
3452 Set_Char ('0');
3453 end if;
3455 if Num_Units >= 100 and then Unum < 100 then
3456 Set_Char ('0');
3457 end if;
3459 Set_Int (Unum);
3460 end Set_Unit_Number;
3462 ----------------------
3463 -- Write_Info_Ada_C --
3464 ----------------------
3466 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3467 begin
3468 if Ada_Bind_File then
3469 declare
3470 S : String (1 .. Ada'Length + Common'Length);
3471 begin
3472 S (1 .. Ada'Length) := Ada;
3473 S (Ada'Length + 1 .. S'Length) := Common;
3474 WBI (S);
3475 end;
3477 else
3478 declare
3479 S : String (1 .. C'Length + Common'Length);
3480 begin
3481 S (1 .. C'Length) := C;
3482 S (C'Length + 1 .. S'Length) := Common;
3483 WBI (S);
3484 end;
3485 end if;
3486 end Write_Info_Ada_C;
3488 ----------------------------
3489 -- Write_Statement_Buffer --
3490 ----------------------------
3492 procedure Write_Statement_Buffer is
3493 begin
3494 WBI (Statement_Buffer (1 .. Last));
3495 Last := 0;
3496 end Write_Statement_Buffer;
3498 procedure Write_Statement_Buffer (S : String) is
3499 begin
3500 Set_String (S);
3501 Write_Statement_Buffer;
3502 end Write_Statement_Buffer;
3504 end Bindgen;