OSDN Git Service

Add Fariborz to my last change.
[pf3gnuchains/gcc-fork.git] / gcc / ada / bindgen.adb
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-2004 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 2,  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 COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with ALI;         use ALI;
28 with Binde;       use Binde;
29 with Butil;       use Butil;
30 with Casing;      use Casing;
31 with Fname;       use Fname;
32 with GNAT.OS_Lib; use GNAT.OS_Lib;
33 with Gnatvsn;     use Gnatvsn;
34 with Hostparm;
35 with Namet;       use Namet;
36 with Opt;         use Opt;
37 with Osint;       use Osint;
38 with Osint.B;     use Osint.B;
39 with Output;      use Output;
40 with Rident;      use Rident;
41 with Table;       use Table;
42 with Targparm;    use Targparm;
43 with Types;       use Types;
44
45 with GNAT.Heap_Sort_A;     use GNAT.Heap_Sort_A;
46
47 package body Bindgen is
48
49    Statement_Buffer : String (1 .. 1000);
50    --  Buffer used for constructing output statements
51
52    Last : Natural := 0;
53    --  Last location in Statement_Buffer currently set
54
55    With_DECGNAT : Boolean := False;
56    --  Flag which indicates whether the program uses the DECGNAT library
57    --  (presence of the unit DEC).
58
59    With_GNARL : Boolean := False;
60    --  Flag which indicates whether the program uses the GNARL library
61    --  (presence of the unit System.OS_Interface)
62
63    Num_Elab_Calls : Nat := 0;
64    --  Number of generated calls to elaboration routines
65
66    ----------------------------------
67    -- Interface_State Pragma Table --
68    ----------------------------------
69
70    --  This table assembles the interface state pragma information from
71    --  all the units in the partition. Note that Bcheck has already checked
72    --  that the information is consistent across partitions. The entries
73    --  in this table are n/u/r/s for not set/user/runtime/system.
74
75    package IS_Pragma_Settings is new Table.Table (
76      Table_Component_Type => Character,
77      Table_Index_Type     => Int,
78      Table_Low_Bound      => 0,
79      Table_Initial        => 100,
80      Table_Increment      => 200,
81      Table_Name           => "IS_Pragma_Settings");
82
83    ----------------------
84    -- Run-Time Globals --
85    ----------------------
86
87    --  This section documents the global variables that are passed to the
88    --  run time from the generated binder file. The call that is made is
89    --  to the routine Set_Globals, which has the following spec:
90
91    --   procedure Set_Globals
92    --     (Main_Priority            : Integer;
93    --      Time_Slice_Value         : Integer;
94    --      WC_Encoding              : Character;
95    --      Locking_Policy           : Character;
96    --      Queuing_Policy           : Character;
97    --      Task_Dispatching_Policy  : Character;
98    --      Restrictions             : System.Address;
99    --      Interrupt_States         : System.Address;
100    --      Num_Interrupt_States     : Integer;
101    --      Unreserve_All_Interrupts : Integer;
102    --      Exception_Tracebacks     : Integer;
103    --      Zero_Cost_Exceptions     : Integer);
104
105    --  Main_Priority is the priority value set by pragma Priority in the
106    --  main program. If no such pragma is present, the value is -1.
107
108    --  Time_Slice_Value is the time slice value set by pragma Time_Slice
109    --  in the main program, or by the use of a -Tnnn parameter for the
110    --  binder (if both are present, the binder value overrides). The
111    --  value is in milliseconds. A value of zero indicates that time
112    --  slicing should be suppressed. If no pragma is present, and no
113    --  -T switch was used, the value is -1.
114
115    --  WC_Encoding shows the wide character encoding method used for
116    --  the main program. This is one of the encoding letters defined
117    --  in System.WCh_Con.WC_Encoding_Letters.
118
119    --  Locking_Policy is a space if no locking policy was specified
120    --  for the partition. If a locking policy was specified, the value
121    --  is the upper case first character of the locking policy name,
122    --  for example, 'C' for Ceiling_Locking.
123
124    --  Queuing_Policy is a space if no queuing policy was specified
125    --  for the partition. If a queuing policy was specified, the value
126    --  is the upper case first character of the queuing policy name
127    --  for example, 'F' for FIFO_Queuing.
128
129    --  Task_Dispatching_Policy is a space if no task dispatching policy
130    --  was specified for the partition. If a task dispatching policy
131    --  was specified, the value is the upper case first character of
132    --  the policy name, e.g. 'F' for FIFO_Within_Priorities.
133
134    --  Restrictions is the address of a null-terminated string specifying the
135    --  restrictions information for the partition. The format is identical to
136    --  that of the parameter string found on R lines in ali files (see Lib.Writ
137    --  spec in lib-writ.ads for full details). The difference is that in this
138    --  context the values are the cumulative ones for the entire partition.
139
140    --  Interrupt_States is the address of a string used to specify the
141    --  cumulative results of Interrupt_State pragmas used in the partition.
142    --  The length of this string is determined by the last interrupt for which
143    --  such a pragma is given (the string will be a null string if no pragmas
144    --  were used). If pragma were present the entries apply to the interrupts
145    --  in sequence from the first interrupt, and are set to one of four
146    --  possible settings: 'n' for not specified, 'u' for user, 'r' for
147    --  run time, 's' for system, see description of Interrupt_State pragma
148    --  for further details.
149
150    --  Num_Interrupt_States is the length of the Interrupt_States string.
151    --  It will be set to zero if no Interrupt_State pragmas are present.
152
153    --  Unreserve_All_Interrupts is set to one if at least one unit in the
154    --  partition had a pragma Unreserve_All_Interrupts, and zero otherwise.
155
156    --  Exception_Tracebacks is set to one if the -E parameter was present
157    --  in the bind and to zero otherwise. Note that on some targets exception
158    --  tracebacks are provided by default, so a value of zero for this
159    --  parameter does not necessarily mean no trace backs are available.
160
161    --  Zero_Cost_Exceptions is set to one if zero cost exceptions are used for
162    --  this partition, and to zero if longjmp/setjmp exceptions are used.
163    --  the use of zero
164
165    -----------------------
166    -- Local Subprograms --
167    -----------------------
168
169    procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
170    --  Convenient shorthand used throughout
171
172    procedure Gen_Adainit_Ada;
173    --  Generates the Adainit procedure (Ada code case)
174
175    procedure Gen_Adainit_C;
176    --  Generates the Adainit procedure (C code case)
177
178    procedure Gen_Adafinal_Ada;
179    --  Generate the Adafinal procedure (Ada code case)
180
181    procedure Gen_Adafinal_C;
182    --  Generate the Adafinal procedure (C code case)
183
184    procedure Gen_Elab_Calls_Ada;
185    --  Generate sequence of elaboration calls (Ada code case)
186
187    procedure Gen_Elab_Calls_C;
188    --  Generate sequence of elaboration calls (C code case)
189
190    procedure Gen_Elab_Order_Ada;
191    --  Generate comments showing elaboration order chosen (Ada case)
192
193    procedure Gen_Elab_Order_C;
194    --  Generate comments showing elaboration order chosen (C case)
195
196    procedure Gen_Elab_Defs_C;
197    --  Generate sequence of definitions for elaboration routines (C code case)
198
199    procedure Gen_Exception_Table_Ada;
200    --  Generate binder exception table (Ada code case). This consists of
201    --  declarations followed by a begin followed by a call. If zero cost
202    --  exceptions are not active, then only the begin is generated.
203
204    procedure Gen_Exception_Table_C;
205    --  Generate binder exception table (C code case). This has no effect
206    --  if zero cost exceptions are not active, otherwise it generates a
207    --  set of declarations followed by a call.
208
209    procedure Gen_Main_Ada;
210    --  Generate procedure main (Ada code case)
211
212    procedure Gen_Main_C;
213    --  Generate main() procedure (C code case)
214
215    procedure Gen_Object_Files_Options;
216    --  Output comments containing a list of the full names of the object
217    --  files to be linked and the list of linker options supplied by
218    --  Linker_Options pragmas in the source. (C and Ada code case)
219
220    procedure Gen_Output_File_Ada (Filename : String);
221    --  Generate output file (Ada code case)
222
223    procedure Gen_Output_File_C (Filename : String);
224    --  Generate output file (C code case)
225
226    procedure Gen_Restrictions_String_1;
227    --  Generate first restrictions string, which consists of the parameters
228    --  the first R line, as described in lib-writ.ads, with the restrictions
229    --  being those for the entire partition (from Cumulative_Restrictions).
230
231    procedure Gen_Restrictions_String_2;
232    --  Generate first restrictions string, which consists of the parameters
233    --  the second R line, as described in lib-writ.ads, with the restrictions
234    --  being those for the entire partition (from Cumulative_Restrictions).
235
236    procedure Gen_Versions_Ada;
237    --  Output series of definitions for unit versions (Ada code case)
238
239    procedure Gen_Versions_C;
240    --  Output series of definitions for unit versions (C code case)
241
242    function Get_Ada_Main_Name return String;
243    --  This function is used in the Ada main output case to compute a usable
244    --  name for the generated main program. The normal main program name is
245    --  Ada_Main, but this won't work if the user has a unit with this name.
246    --  This function tries Ada_Main first, and if there is such a clash, then
247    --  it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
248
249    function Get_Main_Name return String;
250    --  This function is used in the Ada main output case to compute the
251    --  correct external main program. It is "main" by default, unless the
252    --  flag Use_Ada_Main_Program_Name_On_Target is set, in which case it
253    --  is the name of the Ada main name without the "_ada". This default
254    --  can be overridden explicitly using the -Mname binder switch.
255
256    function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
257    --  Compare linker options, when sorting, first according to
258    --  Is_Internal_File (internal files come later) and then by
259    --  elaboration order position (latest to earliest).
260
261    procedure Move_Linker_Option (From : Natural; To : Natural);
262    --  Move routine for sorting linker options
263
264    procedure Public_Version_Warning;
265    --  Emit a warning concerning the use of the Public version under
266    --  certain circumstances. See details in body.
267
268    procedure Resolve_Binder_Options;
269    --  Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
270    --  since it tests for a package named "dec" which might cause a conflict
271    --  on non-VMS systems.
272
273    procedure Set_Char (C : Character);
274    --  Set given character in Statement_Buffer at the Last + 1 position
275    --  and increment Last by one to reflect the stored character.
276
277    procedure Set_Int (N : Int);
278    --  Set given value in decimal in Statement_Buffer with no spaces
279    --  starting at the Last + 1 position, and updating Last past the value.
280    --  A minus sign is output for a negative value.
281
282    procedure Set_IS_Pragma_Table;
283    --  Initializes contents of IS_Pragma_Settings table from ALI table
284
285    procedure Set_Main_Program_Name;
286    --  Given the main program name in Name_Buffer (length in Name_Len)
287    --  generate the name of the routine to be used in the call. The name
288    --  is generated starting at Last + 1, and Last is updated past it.
289
290    procedure Set_Name_Buffer;
291    --  Set the value stored in positions 1 .. Name_Len of the Name_Buffer.
292
293    procedure Set_String (S : String);
294    --  Sets characters of given string in Statement_Buffer, starting at the
295    --  Last + 1 position, and updating last past the string value.
296
297    procedure Set_Unit_Name;
298    --  Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
299    --  starting at the Last + 1 position, and updating last past the value.
300    --  changing periods to double underscores, and updating Last appropriately.
301
302    procedure Set_Unit_Number (U : Unit_Id);
303    --  Sets unit number (first unit is 1, leading zeroes output to line
304    --  up all output unit numbers nicely as required by the value, and
305    --  by the total number of units.
306
307    procedure Tab_To (N : Natural);
308    --  If Last is greater than or equal to N, no effect, otherwise store
309    --  blanks in Statement_Buffer bumping Last, until Last = N.
310
311    procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
312    --  For C code case, write C & Common, for Ada case write Ada & Common
313    --  to current binder output file using Write_Binder_Info.
314
315    procedure Write_Statement_Buffer;
316    --  Write out contents of statement buffer up to Last, and reset Last to 0
317
318    procedure Write_Statement_Buffer (S : String);
319    --  First writes its argument (using Set_String (S)), then writes out the
320    --  contents of statement buffer up to Last, and reset Last to 0
321
322    ----------------------
323    -- Gen_Adafinal_Ada --
324    ----------------------
325
326    procedure Gen_Adafinal_Ada is
327    begin
328       WBI ("");
329       WBI ("   procedure " & Ada_Final_Name.all & " is");
330       WBI ("   begin");
331
332       --  If compiling for the JVM, we directly call Adafinal because
333       --  we don't import it via Do_Finalize (see Gen_Output_File_Ada).
334
335       if Hostparm.Java_VM then
336          WBI ("      System.Standard_Library.Adafinal;");
337       else
338          WBI ("      Do_Finalize;");
339       end if;
340
341       WBI ("   end " & Ada_Final_Name.all & ";");
342    end Gen_Adafinal_Ada;
343
344    --------------------
345    -- Gen_Adafinal_C --
346    --------------------
347
348    procedure Gen_Adafinal_C is
349    begin
350       WBI ("void " & Ada_Final_Name.all & " () {");
351       WBI ("   system__standard_library__adafinal ();");
352       WBI ("}");
353       WBI ("");
354    end Gen_Adafinal_C;
355
356    ---------------------
357    -- Gen_Adainit_Ada --
358    ---------------------
359
360    procedure Gen_Adainit_Ada is
361       Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
362
363    begin
364       WBI ("   procedure " & Ada_Init_Name.all & " is");
365
366       --  Generate externals for elaboration entities
367
368       for E in Elab_Order.First .. Elab_Order.Last loop
369          declare
370             Unum : constant Unit_Id := Elab_Order.Table (E);
371             U    : Unit_Record renames Units.Table (Unum);
372
373          begin
374             --  Check for Elab_Entity to be set for this unit
375
376             if U.Set_Elab_Entity
377
378             --  Don't generate reference for stand alone library
379
380               and then not U.Interface
381
382             --  Don't generate reference for predefined file in No_Run_Time
383             --  mode, since we don't include the object files in this case
384
385               and then not
386                 (No_Run_Time_Mode
387                    and then Is_Predefined_File_Name (U.Sfile))
388             then
389                Set_String ("      ");
390                Set_String ("E");
391                Set_Unit_Number (Unum);
392                Set_String (" : Boolean; pragma Import (Ada, ");
393                Set_String ("E");
394                Set_Unit_Number (Unum);
395                Set_String (", """);
396                Get_Name_String (U.Uname);
397
398                --  In the case of JGNAT we need to emit an Import name
399                --  that includes the class name (using '$' separators
400                --  in the case of a child unit name).
401
402                if Hostparm.Java_VM then
403                   for J in 1 .. Name_Len - 2 loop
404                      if Name_Buffer (J) /= '.' then
405                         Set_Char (Name_Buffer (J));
406                      else
407                         Set_String ("$");
408                      end if;
409                   end loop;
410
411                   Set_String (".");
412
413                   --  If the unit name is very long, then split the
414                   --  Import link name across lines using "&" (occurs
415                   --  in some C2 tests).
416
417                   if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
418                      Set_String (""" &");
419                      Write_Statement_Buffer;
420                      Set_String ("         """);
421                   end if;
422                end if;
423
424                Set_Unit_Name;
425                Set_String ("_E"");");
426                Write_Statement_Buffer;
427             end if;
428          end;
429       end loop;
430
431       Write_Statement_Buffer;
432
433       --  If the standard library is suppressed, then the only global variable
434       --  that might be needed (by the Ravenscar profile) is the priority of
435       --  the environment. Also no exception tables are needed.
436
437       if Suppress_Standard_Library_On_Target then
438          if Main_Priority /= No_Main_Priority then
439             WBI ("      Main_Priority : Integer;");
440             WBI ("      pragma Import (C, Main_Priority," &
441                  " ""__gl_main_priority"");");
442             WBI ("");
443          end if;
444
445          WBI ("   begin");
446
447          if Main_Priority /= No_Main_Priority then
448             Set_String ("      Main_Priority := ");
449             Set_Int    (Main_Priority);
450             Set_Char   (';');
451             Write_Statement_Buffer;
452
453          else
454             WBI ("      null;");
455          end if;
456
457       --  Normal case (standard library not suppressed). Global values are
458       --  assigned using the runtime routine Set_Globals (we have to use
459       --  the routine call, rather than define the globals in the binder
460       --  file to deal with cross-library calls in some systems.
461
462       else
463          --  Generate restrictions string
464
465          Set_String ("      Restrictions : constant String :=");
466          Write_Statement_Buffer;
467
468          Set_String ("        """);
469          Gen_Restrictions_String_1;
470          Set_String (""" &");
471          Write_Statement_Buffer;
472
473          Set_String ("        """);
474          Gen_Restrictions_String_2;
475          Set_String (""" & ASCII.Nul;");
476          Write_Statement_Buffer;
477          WBI ("");
478
479          --  Generate Interrupt_State pragma string
480
481          Set_String ("      Interrupt_States : constant String :=");
482          Write_Statement_Buffer;
483
484          declare
485             Col : Natural;
486
487          begin
488             Set_String ("        """);
489             Col := 9;
490
491             for J in 0 .. IS_Pragma_Settings.Last loop
492                if Col > 72 then
493                   Set_String (""" &");
494                   Write_Statement_Buffer;
495                   Set_String ("        """);
496                   Col := 9;
497
498                else
499                   Col := Col + 1;
500                end if;
501
502                Set_Char (IS_Pragma_Settings.Table (J));
503             end loop;
504          end;
505
506          Set_String (""";");
507          Write_Statement_Buffer;
508          WBI ("");
509
510          --  Generate spec for Set_Globals procedure
511
512          WBI ("      procedure Set_Globals");
513          WBI ("        (Main_Priority            : Integer;");
514          WBI ("         Time_Slice_Value         : Integer;");
515          WBI ("         WC_Encoding              : Character;");
516          WBI ("         Locking_Policy           : Character;");
517          WBI ("         Queuing_Policy           : Character;");
518          WBI ("         Task_Dispatching_Policy  : Character;");
519          WBI ("         Restrictions             : System.Address;");
520          WBI ("         Interrupt_States         : System.Address;");
521          WBI ("         Num_Interrupt_States     : Integer;");
522          WBI ("         Unreserve_All_Interrupts : Integer;");
523          WBI ("         Exception_Tracebacks     : Integer;");
524          WBI ("         Zero_Cost_Exceptions     : Integer);");
525          WBI ("      pragma Import (C, Set_Globals, ""__gnat_set_globals"");");
526
527          --  Import entry point for elaboration time signal handler
528          --  installation, and indication of if it's been called previously.
529
530          WBI ("");
531          WBI ("      procedure Install_Handler;");
532          WBI ("      pragma Import (C, Install_Handler, " &
533               """__gnat_install_handler"");");
534          WBI ("");
535          WBI ("      Handler_Installed : Integer;");
536          WBI ("      pragma Import (C, Handler_Installed, " &
537               """__gnat_handler_installed"");");
538
539          --  Generate exception table
540
541          Gen_Exception_Table_Ada;
542
543          --  Generate the call to Set_Globals
544
545          WBI ("      Set_Globals");
546
547          Set_String ("        (Main_Priority            => ");
548          Set_Int    (Main_Priority);
549          Set_Char   (',');
550          Write_Statement_Buffer;
551
552          Set_String ("         Time_Slice_Value         => ");
553
554          if Task_Dispatching_Policy_Specified = 'F'
555            and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
556          then
557             Set_Int (0);
558          else
559             Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
560          end if;
561
562          Set_Char   (',');
563          Write_Statement_Buffer;
564
565          Set_String ("         WC_Encoding              => '");
566          Set_Char   (ALIs.Table (ALIs.First).WC_Encoding);
567          Set_String ("',");
568          Write_Statement_Buffer;
569
570          Set_String ("         Locking_Policy           => '");
571          Set_Char   (Locking_Policy_Specified);
572          Set_String ("',");
573          Write_Statement_Buffer;
574
575          Set_String ("         Queuing_Policy           => '");
576          Set_Char   (Queuing_Policy_Specified);
577          Set_String ("',");
578          Write_Statement_Buffer;
579
580          Set_String ("         Task_Dispatching_Policy  => '");
581          Set_Char   (Task_Dispatching_Policy_Specified);
582          Set_String ("',");
583          Write_Statement_Buffer;
584
585          WBI ("         Restrictions             => Restrictions'Address,");
586
587          WBI ("         Interrupt_States         => " &
588                                                  "Interrupt_States'Address,");
589
590          Set_String ("         Num_Interrupt_States     => ");
591          Set_Int (IS_Pragma_Settings.Last + 1);
592          Set_Char (',');
593          Write_Statement_Buffer;
594
595          Set_String ("         Unreserve_All_Interrupts => ");
596
597          if Unreserve_All_Interrupts_Specified then
598             Set_String ("1");
599          else
600             Set_String ("0");
601          end if;
602
603          Set_Char (',');
604          Write_Statement_Buffer;
605
606          Set_String ("         Exception_Tracebacks     => ");
607
608          if Exception_Tracebacks then
609             Set_String ("1");
610          else
611             Set_String ("0");
612          end if;
613
614          Set_String (",");
615          Write_Statement_Buffer;
616
617          Set_String ("         Zero_Cost_Exceptions     => ");
618
619          if Zero_Cost_Exceptions_Specified then
620             Set_String ("1");
621          else
622             Set_String ("0");
623          end if;
624
625          Set_String (");");
626          Write_Statement_Buffer;
627
628          --  Generate call to Install_Handler
629
630          WBI ("");
631          WBI ("      if Handler_Installed = 0 then");
632          WBI ("         Install_Handler;");
633          WBI ("      end if;");
634       end if;
635
636       --  Generate call to set Initialize_Scalar values if active
637
638       if Initialize_Scalars_Used then
639          WBI ("");
640          Set_String ("      System.Scalar_Values.Initialize ('");
641          Set_Char (Initialize_Scalars_Mode1);
642          Set_String ("', '");
643          Set_Char (Initialize_Scalars_Mode2);
644          Set_String ("');");
645          Write_Statement_Buffer;
646       end if;
647
648       --  Generate assignment of default secondary stack size if set
649
650       if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
651          WBI ("");
652          Set_String ("      System.Secondary_Stack.");
653          Set_String ("Default_Secondary_Stack_Size := ");
654          Set_Int (Opt.Default_Sec_Stack_Size);
655          Set_Char (';');
656          Write_Statement_Buffer;
657       end if;
658
659       --  Generate elaboration calls
660
661       WBI ("");
662       Gen_Elab_Calls_Ada;
663
664       WBI ("   end " & Ada_Init_Name.all & ";");
665    end Gen_Adainit_Ada;
666
667    -------------------
668    -- Gen_Adainit_C --
669    --------------------
670
671    procedure Gen_Adainit_C is
672       Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
673
674    begin
675       WBI ("void " & Ada_Init_Name.all & " (void)");
676       WBI ("{");
677
678       --  Generate externals for elaboration entities
679
680       for E in Elab_Order.First .. Elab_Order.Last loop
681          declare
682             Unum : constant Unit_Id := Elab_Order.Table (E);
683             U    : Unit_Record renames Units.Table (Unum);
684
685          begin
686             --  Check for Elab entity to be set for this unit
687
688             if U.Set_Elab_Entity
689
690             --  Don't generate reference for stand alone library
691
692               and then not U.Interface
693
694             --  Don't generate reference for predefined file in No_Run_Time
695             --  mode, since we don't include the object files in this case
696
697               and then not
698                 (No_Run_Time_Mode
699                    and then Is_Predefined_File_Name (U.Sfile))
700             then
701                Set_String ("   extern char ");
702                Get_Name_String (U.Uname);
703                Set_Unit_Name;
704                Set_String ("_E;");
705                Write_Statement_Buffer;
706             end if;
707          end;
708       end loop;
709
710       Write_Statement_Buffer;
711
712       --  Standard library suppressed
713
714       if Suppress_Standard_Library_On_Target then
715
716          --  Case of High_Integrity_Mode mode. Set __gl_main_priority if needed
717          --  for the Ravenscar profile.
718
719          if Main_Priority /= No_Main_Priority then
720             Set_String ("   extern int __gl_main_priority = ");
721             Set_Int    (Main_Priority);
722             Set_Char   (';');
723             Write_Statement_Buffer;
724          end if;
725
726       --  Normal case (standard library not suppressed)
727
728       else
729          --  Generate definition for restrictions string
730
731          Set_String ("   const char *restrictions = """);
732          Gen_Restrictions_String_1;
733          Gen_Restrictions_String_2;
734          Set_String (""";");
735          Write_Statement_Buffer;
736
737          --  Generate definition for interrupt states string
738
739          Set_String ("   const char *interrupt_states = """);
740
741          for J in 0 .. IS_Pragma_Settings.Last loop
742             Set_Char (IS_Pragma_Settings.Table (J));
743          end loop;
744
745          Set_String (""";");
746          Write_Statement_Buffer;
747
748          --  Generate declaration for secondary stack default if needed
749
750          if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
751             WBI ("   extern int system__secondary_stack__" &
752                  "default_secondary_stack_size;");
753          end if;
754
755          WBI ("");
756
757          --  Code for normal case (standard library not suppressed)
758
759          Gen_Exception_Table_C;
760
761          --  Generate call to set the runtime global variables defined in
762          --  a-init.c. We define the varables in a-init.c, rather than in
763          --  the binder generated file itself to avoid undefined externals
764          --  when the runtime is linked as a shareable image library.
765
766          --  We call the routine from inside adainit() because this works for
767          --  both programs with and without binder generated "main" functions.
768
769          WBI ("   __gnat_set_globals (");
770
771          Set_String ("      ");
772          Set_Int (Main_Priority);
773          Set_Char (',');
774          Tab_To (24);
775          Set_String ("/* Main_Priority              */");
776          Write_Statement_Buffer;
777
778          Set_String ("      ");
779
780          if Task_Dispatching_Policy = 'F'
781            and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
782          then
783             Set_Int (0);
784          else
785             Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
786          end if;
787
788          Set_Char   (',');
789          Tab_To (24);
790          Set_String ("/* Time_Slice_Value           */");
791          Write_Statement_Buffer;
792
793          Set_String ("      '");
794          Set_Char   (ALIs.Table (ALIs.First).WC_Encoding);
795          Set_String ("',");
796          Tab_To (24);
797          Set_String ("/* WC_Encoding                */");
798          Write_Statement_Buffer;
799
800          Set_String ("      '");
801          Set_Char (Locking_Policy_Specified);
802          Set_String ("',");
803          Tab_To (24);
804          Set_String ("/* Locking_Policy             */");
805          Write_Statement_Buffer;
806
807          Set_String ("      '");
808          Set_Char (Queuing_Policy_Specified);
809          Set_String ("',");
810          Tab_To (24);
811          Set_String ("/* Queuing_Policy             */");
812          Write_Statement_Buffer;
813
814          Set_String ("      '");
815          Set_Char (Task_Dispatching_Policy_Specified);
816          Set_String ("',");
817          Tab_To (24);
818          Set_String ("/* Tasking_Dispatching_Policy */");
819          Write_Statement_Buffer;
820
821          Set_String ("      ");
822          Set_String ("restrictions");
823          Set_String (",");
824          Tab_To (24);
825          Set_String ("/* Restrictions               */");
826          Write_Statement_Buffer;
827
828          Set_String ("      ");
829          Set_String ("interrupt_states");
830          Set_String (",");
831          Tab_To (24);
832          Set_String ("/* Interrupt_States           */");
833          Write_Statement_Buffer;
834
835          Set_String ("      ");
836          Set_Int (IS_Pragma_Settings.Last + 1);
837          Set_String (",");
838          Tab_To (24);
839          Set_String ("/* Num_Interrupt_States       */");
840          Write_Statement_Buffer;
841
842          Set_String ("      ");
843          Set_Int    (Boolean'Pos (Unreserve_All_Interrupts_Specified));
844          Set_String (",");
845          Tab_To (24);
846          Set_String ("/* Unreserve_All_Interrupts   */");
847          Write_Statement_Buffer;
848
849          Set_String ("      ");
850          Set_Int    (Boolean'Pos (Exception_Tracebacks));
851          Set_String (",");
852          Tab_To (24);
853          Set_String ("/* Exception_Tracebacks       */");
854          Write_Statement_Buffer;
855
856          Set_String ("      ");
857          Set_Int    (Boolean'Pos (Zero_Cost_Exceptions_Specified));
858          Set_String (");");
859          Tab_To (24);
860          Set_String ("/* Zero_Cost_Exceptions       */");
861          Write_Statement_Buffer;
862          WBI ("");
863
864          --  Install elaboration time signal handler
865
866          WBI ("   if (__gnat_handler_installed == 0)");
867          WBI ("     {");
868          WBI ("        __gnat_install_handler ();");
869          WBI ("     }");
870       end if;
871
872       --  Generate call to set Initialize_Scalar values if needed
873
874       if Initialize_Scalars_Used then
875          WBI ("");
876          Set_String ("      system__scalar_values__initialize('");
877          Set_Char (Initialize_Scalars_Mode1);
878          Set_String ("', '");
879          Set_Char (Initialize_Scalars_Mode2);
880          Set_String ("');");
881          Write_Statement_Buffer;
882       end if;
883
884       --  Generate assignment of default secondary stack size if set
885
886       if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
887          WBI ("");
888          Set_String ("   system__secondary_stack__");
889          Set_String ("default_secondary_stack_size = ");
890          Set_Int (Opt.Default_Sec_Stack_Size);
891          Set_Char (';');
892          Write_Statement_Buffer;
893       end if;
894
895       --  Generate elaboration calls
896
897       WBI ("");
898       Gen_Elab_Calls_C;
899       WBI ("}");
900    end Gen_Adainit_C;
901
902    ------------------------
903    -- Gen_Elab_Calls_Ada --
904    ------------------------
905
906    procedure Gen_Elab_Calls_Ada is
907    begin
908       for E in Elab_Order.First .. Elab_Order.Last loop
909          declare
910             Unum : constant Unit_Id := Elab_Order.Table (E);
911             U    : Unit_Record renames Units.Table (Unum);
912
913             Unum_Spec : Unit_Id;
914             --  This is the unit number of the spec that corresponds to
915             --  this entry. It is the same as Unum except when the body
916             --  and spec are different and we are currently processing
917             --  the body, in which case it is the spec (Unum + 1).
918
919          begin
920             if U.Utype = Is_Body then
921                Unum_Spec := Unum + 1;
922             else
923                Unum_Spec := Unum;
924             end if;
925
926             --  Nothing to do if predefined unit in no run time mode
927
928             if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
929                null;
930
931             --  Case of no elaboration code
932
933             elsif U.No_Elab then
934
935                --  The only case in which we have to do something is if
936                --  this is a body, with a separate spec, where the separate
937                --  spec has an elaboration entity defined.
938
939                --  In that case, this is where we set the elaboration entity
940                --  to True, we do not need to test if this has already been
941                --  done, since it is quicker to set the flag than to test it.
942
943                if not U.Interface and then U.Utype = Is_Body
944                  and then Units.Table (Unum_Spec).Set_Elab_Entity
945                then
946                   Set_String ("      E");
947                   Set_Unit_Number (Unum_Spec);
948                   Set_String (" := True;");
949                   Write_Statement_Buffer;
950                end if;
951
952             --  Here if elaboration code is present. If binding a library
953             --  or if there is a non-Ada main subprogram then we generate:
954
955             --    if not uname_E then
956             --       uname'elab_[spec|body];
957             --       uname_E := True;
958             --    end if;
959
960             --  Otherwise, elaboration routines are called unconditionally:
961
962             --    uname'elab_[spec|body];
963             --    uname_E := True;
964
965             --  The uname_E assignment is skipped if this is a separate spec,
966             --  since the assignment will be done when we process the body.
967
968             elsif not U.Interface then
969                if Force_Checking_Of_Elaboration_Flags or
970                   Interface_Library_Unit or
971                   (not Bind_Main_Program)
972                then
973                   Set_String ("      if not E");
974                   Set_Unit_Number (Unum_Spec);
975                   Set_String (" then");
976                   Write_Statement_Buffer;
977                   Set_String ("   ");
978                end if;
979
980                Set_String ("      ");
981                Get_Decoded_Name_String_With_Brackets (U.Uname);
982
983                if Name_Buffer (Name_Len) = 's' then
984                   Name_Buffer (Name_Len - 1 .. Name_Len + 8) := "'elab_spec";
985                else
986                   Name_Buffer (Name_Len - 1 .. Name_Len + 8) := "'elab_body";
987                end if;
988
989                Name_Len := Name_Len + 8;
990                Set_Casing (U.Icasing);
991                Set_Name_Buffer;
992                Set_Char (';');
993                Write_Statement_Buffer;
994
995                if U.Utype /= Is_Spec then
996                   if Force_Checking_Of_Elaboration_Flags or
997                      Interface_Library_Unit or
998                      (not Bind_Main_Program)
999                   then
1000                      Set_String ("   ");
1001                   end if;
1002
1003                   Set_String ("      E");
1004                   Set_Unit_Number (Unum_Spec);
1005                   Set_String (" := True;");
1006                   Write_Statement_Buffer;
1007                end if;
1008
1009                if Force_Checking_Of_Elaboration_Flags or
1010                   Interface_Library_Unit or
1011                   (not Bind_Main_Program)
1012                then
1013                   WBI ("      end if;");
1014                end if;
1015             end if;
1016          end;
1017       end loop;
1018    end Gen_Elab_Calls_Ada;
1019
1020    ----------------------
1021    -- Gen_Elab_Calls_C --
1022    ----------------------
1023
1024    procedure Gen_Elab_Calls_C is
1025    begin
1026       for E in Elab_Order.First .. Elab_Order.Last loop
1027          declare
1028             Unum : constant Unit_Id := Elab_Order.Table (E);
1029             U    : Unit_Record renames Units.Table (Unum);
1030
1031             Unum_Spec : Unit_Id;
1032             --  This is the unit number of the spec that corresponds to
1033             --  this entry. It is the same as Unum except when the body
1034             --  and spec are different and we are currently processing
1035             --  the body, in which case it is the spec (Unum + 1).
1036
1037          begin
1038             if U.Utype = Is_Body then
1039                Unum_Spec := Unum + 1;
1040             else
1041                Unum_Spec := Unum;
1042             end if;
1043
1044             --  Nothing to do if predefined unit in no run time mode
1045
1046             if No_Run_Time_Mode and then Is_Predefined_File_Name (U.Sfile) then
1047                null;
1048
1049             --  Case of no elaboration code
1050
1051             elsif U.No_Elab then
1052
1053                --  The only case in which we have to do something is if
1054                --  this is a body, with a separate spec, where the separate
1055                --  spec has an elaboration entity defined.
1056
1057                --  In that case, this is where we set the elaboration entity
1058                --  to True, we do not need to test if this has already been
1059                --  done, since it is quicker to set the flag than to test it.
1060
1061                if not U.Interface and then U.Utype = Is_Body
1062                  and then Units.Table (Unum_Spec).Set_Elab_Entity
1063                then
1064                   Set_String ("   ");
1065                   Get_Name_String (U.Uname);
1066                   Set_Unit_Name;
1067                   Set_String ("_E = 1;");
1068                   Write_Statement_Buffer;
1069                end if;
1070
1071             --  Here if elaboration code is present. If binding a library
1072             --  or if there is a non-Ada main subprogram then we generate:
1073
1074             --    if (uname_E == 0) {
1075             --       uname__elab[s|b] ();
1076             --       uname_E++;
1077             --    }
1078
1079             --  The uname_E assignment is skipped if this is a separate spec,
1080             --  since the assignment will be done when we process the body.
1081
1082             elsif not U.Interface then
1083                Get_Name_String (U.Uname);
1084
1085                if Force_Checking_Of_Elaboration_Flags or
1086                   Interface_Library_Unit or
1087                   (not Bind_Main_Program)
1088                then
1089                   Set_String ("   if (");
1090                   Set_Unit_Name;
1091                   Set_String ("_E == 0) {");
1092                   Write_Statement_Buffer;
1093                   Set_String ("   ");
1094                end if;
1095
1096                Set_String ("   ");
1097                Set_Unit_Name;
1098                Set_String ("___elab");
1099                Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1100                Set_String (" ();");
1101                Write_Statement_Buffer;
1102
1103                if U.Utype /= Is_Spec then
1104                   if Force_Checking_Of_Elaboration_Flags or
1105                      Interface_Library_Unit or
1106                      (not Bind_Main_Program)
1107                   then
1108                      Set_String ("   ");
1109                   end if;
1110
1111                   Set_String ("   ");
1112                   Set_Unit_Name;
1113                   Set_String ("_E++;");
1114                   Write_Statement_Buffer;
1115                end if;
1116
1117                if Force_Checking_Of_Elaboration_Flags or
1118                   Interface_Library_Unit or
1119                   (not Bind_Main_Program)
1120                then
1121                   WBI ("   }");
1122                end if;
1123             end if;
1124          end;
1125       end loop;
1126
1127    end Gen_Elab_Calls_C;
1128
1129    ----------------------
1130    -- Gen_Elab_Defs_C --
1131    ----------------------
1132
1133    procedure Gen_Elab_Defs_C is
1134    begin
1135       for E in Elab_Order.First .. Elab_Order.Last loop
1136
1137          --  Generate declaration of elaboration procedure if elaboration
1138          --  needed. Note that passive units are always excluded.
1139
1140          if not Units.Table (Elab_Order.Table (E)).No_Elab then
1141             Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1142             Set_String ("extern void ");
1143             Set_Unit_Name;
1144             Set_String ("___elab");
1145             Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1146             Set_String (" (void);");
1147             Write_Statement_Buffer;
1148          end if;
1149
1150       end loop;
1151
1152       WBI ("");
1153    end Gen_Elab_Defs_C;
1154
1155    ------------------------
1156    -- Gen_Elab_Order_Ada --
1157    ------------------------
1158
1159    procedure Gen_Elab_Order_Ada is
1160    begin
1161       WBI ("");
1162       WBI ("   --  BEGIN ELABORATION ORDER");
1163
1164       for J in Elab_Order.First .. Elab_Order.Last loop
1165          Set_String ("   --  ");
1166          Get_Unit_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1167          Set_Name_Buffer;
1168          Write_Statement_Buffer;
1169       end loop;
1170
1171       WBI ("   --  END ELABORATION ORDER");
1172    end Gen_Elab_Order_Ada;
1173
1174    ----------------------
1175    -- Gen_Elab_Order_C --
1176    ----------------------
1177
1178    procedure Gen_Elab_Order_C is
1179    begin
1180       WBI ("");
1181       WBI ("/* BEGIN ELABORATION ORDER");
1182
1183       for J in Elab_Order.First .. Elab_Order.Last loop
1184          Get_Unit_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
1185          Set_Name_Buffer;
1186          Write_Statement_Buffer;
1187       end loop;
1188
1189       WBI ("   END ELABORATION ORDER */");
1190    end Gen_Elab_Order_C;
1191
1192    -----------------------------
1193    -- Gen_Exception_Table_Ada --
1194    -----------------------------
1195
1196    procedure Gen_Exception_Table_Ada is
1197       Num  : Nat;
1198       Last : ALI_Id := No_ALI_Id;
1199
1200    begin
1201       if not Zero_Cost_Exceptions_Specified then
1202          WBI ("   begin");
1203          return;
1204       end if;
1205
1206       --  The code we generate looks like
1207
1208       --        procedure SDP_Table_Build
1209       --          (SDP_Addresses   : System.Address;
1210       --           SDP_Count       : Natural;
1211       --           Elab_Addresses  : System.Address;
1212       --           Elab_Addr_Count : Natural);
1213       --        pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
1214       --
1215       --        ST : aliased constant array (1 .. nnn) of System.Address := (
1216       --               unit_name_1'UET_Address,
1217       --               unit_name_2'UET_Address,
1218       --               ...
1219       --               unit_name_3'UET_Address,
1220       --
1221       --        EA : aliased constant array (1 .. eee) of System.Address := (
1222       --               adainit'Code_Address,
1223       --               adafinal'Code_Address,
1224       --               unit_name'elab[spec|body]'Code_Address,
1225       --               unit_name'elab[spec|body]'Code_Address,
1226       --               unit_name'elab[spec|body]'Code_Address,
1227       --               unit_name'elab[spec|body]'Code_Address);
1228       --
1229       --     begin
1230       --        SDP_Table_Build (ST'Address, nnn, EA'Address, eee);
1231
1232       Num := 0;
1233       for A in ALIs.First .. ALIs.Last loop
1234          if not ALIs.Table (A).Interface
1235            and then ALIs.Table (A).Unit_Exception_Table
1236          then
1237             Num := Num + 1;
1238             Last := A;
1239          end if;
1240       end loop;
1241
1242       if Num = 0 then
1243
1244          --  Happens with "gnatmake -a -f -gnatL ..."
1245
1246          WBI (" ");
1247          WBI ("   begin");
1248          return;
1249       end if;
1250
1251       WBI ("      procedure SDP_Table_Build");
1252       WBI ("        (SDP_Addresses   : System.Address;");
1253       WBI ("         SDP_Count       : Natural;");
1254       WBI ("         Elab_Addresses  : System.Address;");
1255       WBI ("         Elab_Addr_Count : Natural);");
1256       WBI ("      " &
1257            "pragma Import (C, SDP_Table_Build, ""__gnat_SDP_Table_Build"");");
1258
1259       WBI (" ");
1260       Set_String ("      ST : aliased constant array (1 .. ");
1261       Set_Int (Num);
1262       Set_String (") of System.Address := (");
1263
1264       if Num = 1 then
1265          Set_String ("1 => A1);");
1266          Write_Statement_Buffer;
1267
1268       else
1269          Write_Statement_Buffer;
1270
1271          for A in ALIs.First .. ALIs.Last loop
1272             if not ALIs.Table (A).Interface
1273               and then ALIs.Table (A).Unit_Exception_Table
1274             then
1275                Get_Decoded_Name_String_With_Brackets
1276                  (Units.Table (ALIs.Table (A).First_Unit).Uname);
1277                Set_Casing (Mixed_Case);
1278                Set_String ("        ");
1279                Set_String (Name_Buffer (1 .. Name_Len - 2));
1280                Set_String ("'UET_Address");
1281
1282                if A = Last then
1283                   Set_String (");");
1284                else
1285                   Set_Char (',');
1286                end if;
1287
1288                Write_Statement_Buffer;
1289             end if;
1290          end loop;
1291       end if;
1292
1293       WBI (" ");
1294       Set_String ("      EA : aliased constant array (1 .. ");
1295       Set_Int (Num_Elab_Calls + 2);
1296       Set_String (") of System.Address := (");
1297       Write_Statement_Buffer;
1298       WBI ("        " & Ada_Init_Name.all & "'Code_Address,");
1299
1300       --  If compiling for the JVM, we directly reference Adafinal because
1301       --  we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1302
1303       if not Cumulative_Restrictions.Set (No_Finalization) then
1304          if Hostparm.Java_VM then
1305             Set_String
1306               ("        System.Standard_Library.Adafinal'Code_Address");
1307          else
1308             Set_String
1309               ("        Do_Finalize'Code_Address");
1310          end if;
1311       end if;
1312
1313       for E in Elab_Order.First .. Elab_Order.Last loop
1314          Get_Decoded_Name_String_With_Brackets
1315            (Units.Table (Elab_Order.Table (E)).Uname);
1316
1317          if Units.Table (Elab_Order.Table (E)).No_Elab then
1318             null;
1319
1320          else
1321             Set_Char (',');
1322             Write_Statement_Buffer;
1323             Set_String ("        ");
1324
1325             if Name_Buffer (Name_Len) = 's' then
1326                Name_Buffer (Name_Len - 1 .. Name_Len + 21) :=
1327                                         "'elab_spec'code_address";
1328             else
1329                Name_Buffer (Name_Len - 1 .. Name_Len + 21) :=
1330                                         "'elab_body'code_address";
1331             end if;
1332
1333             Name_Len := Name_Len + 21;
1334             Set_Casing (Units.Table (Elab_Order.Table (E)).Icasing);
1335             Set_Name_Buffer;
1336          end if;
1337       end loop;
1338
1339       Set_String (");");
1340       Write_Statement_Buffer;
1341
1342       WBI (" ");
1343       WBI ("   begin");
1344
1345       Set_String ("      SDP_Table_Build (ST'Address, ");
1346       Set_Int (Num);
1347       Set_String (", EA'Address, ");
1348       Set_Int (Num_Elab_Calls + 2);
1349       Set_String (");");
1350       Write_Statement_Buffer;
1351    end Gen_Exception_Table_Ada;
1352
1353    ---------------------------
1354    -- Gen_Exception_Table_C --
1355    ---------------------------
1356
1357    procedure Gen_Exception_Table_C is
1358       Num  : Nat;
1359       Num2 : Nat;
1360
1361    begin
1362       if not Zero_Cost_Exceptions_Specified then
1363          return;
1364       end if;
1365
1366       --  The code we generate looks like
1367
1368       --     extern void *__gnat_unitname1__SDP;
1369       --     extern void *__gnat_unitname2__SDP;
1370       --     ...
1371       --
1372       --     void **st[nnn] = {
1373       --       &__gnat_unitname1__SDP,
1374       --       &__gnat_unitname2__SDP,
1375       --       ...
1376       --       &__gnat_unitnamen__SDP};
1377       --
1378       --     extern void unitname1__elabb ();
1379       --     extern void unitname2__elabb ();
1380       --     ...
1381       --
1382       --     void (*ea[eee]) () = {
1383       --       adainit,
1384       --       adafinal,
1385       --       unitname1___elab[b,s],
1386       --       unitname2___elab[b,s],
1387       --       ...
1388       --       unitnamen___elab[b,s]};
1389       --
1390       --     __gnat_SDP_Table_Build (&st, nnn, &ea, eee);
1391
1392       Num := 0;
1393       for A in ALIs.First .. ALIs.Last loop
1394          if not ALIs.Table (A).Interface
1395            and then ALIs.Table (A).Unit_Exception_Table
1396          then
1397             Num := Num + 1;
1398
1399             Set_String ("   extern void *__gnat_");
1400             Get_Name_String (Units.Table (ALIs.Table (A).First_Unit).Uname);
1401             Set_Unit_Name;
1402             Set_String ("__SDP");
1403             Set_Char (';');
1404             Write_Statement_Buffer;
1405          end if;
1406       end loop;
1407
1408       if Num = 0 then
1409
1410          --  Happens with "gnatmake -a -f -gnatL ..."
1411
1412          return;
1413       end if;
1414
1415       WBI (" ");
1416
1417       Set_String ("   void **st[");
1418       Set_Int (Num);
1419       Set_String ("] = {");
1420       Write_Statement_Buffer;
1421
1422       Num2 := 0;
1423       for A in ALIs.First .. ALIs.Last loop
1424          if not ALIs.Table (A).Interface
1425            and then ALIs.Table (A).Unit_Exception_Table
1426          then
1427             Num2 := Num2 + 1;
1428
1429             Set_String ("     &__gnat_");
1430             Get_Name_String (Units.Table (ALIs.Table (A).First_Unit).Uname);
1431             Set_Unit_Name;
1432             Set_String ("__SDP");
1433
1434             if Num = Num2 then
1435                Set_String ("};");
1436             else
1437                Set_Char (',');
1438             end if;
1439
1440             Write_Statement_Buffer;
1441          end if;
1442       end loop;
1443
1444       WBI ("");
1445       for E in Elab_Order.First .. Elab_Order.Last loop
1446          Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1447
1448          if Units.Table (Elab_Order.Table (E)).No_Elab then
1449             null;
1450
1451          else
1452             Set_String ("   extern void ");
1453             Set_Unit_Name;
1454             Set_String ("___elab");
1455             Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1456             Set_String (" ();");
1457             Write_Statement_Buffer;
1458          end if;
1459       end loop;
1460
1461       WBI ("");
1462       Set_String ("   void (*ea[");
1463       Set_Int (Num_Elab_Calls + 2);
1464       Set_String ("]) () = {");
1465       Write_Statement_Buffer;
1466
1467       WBI ("     " & Ada_Init_Name.all & ",");
1468
1469       if not Cumulative_Restrictions.Set (No_Finalization) then
1470          Set_String ("     system__standard_library__adafinal");
1471       end if;
1472
1473       for E in Elab_Order.First .. Elab_Order.Last loop
1474          Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1475
1476          if Units.Table (Elab_Order.Table (E)).No_Elab then
1477             null;
1478
1479          else
1480             Set_Char (',');
1481             Write_Statement_Buffer;
1482             Set_String ("     ");
1483             Set_Unit_Name;
1484             Set_String ("___elab");
1485             Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1486          end if;
1487       end loop;
1488
1489       Set_String ("};");
1490       Write_Statement_Buffer;
1491
1492       WBI (" ");
1493
1494       Set_String ("   __gnat_SDP_Table_Build (&st, ");
1495       Set_Int (Num);
1496       Set_String (", ea, ");
1497       Set_Int (Num_Elab_Calls + 2);
1498       Set_String (");");
1499       Write_Statement_Buffer;
1500    end Gen_Exception_Table_C;
1501
1502    ------------------
1503    -- Gen_Main_Ada --
1504    ------------------
1505
1506    procedure Gen_Main_Ada is
1507    begin
1508       WBI ("");
1509
1510       if Exit_Status_Supported_On_Target then
1511          Set_String ("   function ");
1512       else
1513          Set_String ("   procedure ");
1514       end if;
1515
1516       Set_String (Get_Main_Name);
1517
1518       if Command_Line_Args_On_Target then
1519          Write_Statement_Buffer;
1520          WBI ("     (argc : Integer;");
1521          WBI ("      argv : System.Address;");
1522          WBI ("      envp : System.Address)");
1523
1524          if Exit_Status_Supported_On_Target then
1525             WBI ("      return Integer");
1526          end if;
1527
1528          WBI ("   is");
1529
1530       else
1531          if Exit_Status_Supported_On_Target then
1532             Set_String (" return Integer is");
1533          else
1534             Set_String (" is");
1535          end if;
1536
1537          Write_Statement_Buffer;
1538       end if;
1539
1540       --  Initialize and Finalize
1541
1542       if not Cumulative_Restrictions.Set (No_Finalization) then
1543          WBI ("      procedure initialize;");
1544          WBI ("      pragma Import (C, initialize, ""__gnat_initialize"");");
1545          WBI ("");
1546          WBI ("      procedure finalize;");
1547          WBI ("      pragma Import (C, finalize, ""__gnat_finalize"");");
1548          WBI ("");
1549       end if;
1550
1551       --  Deal with declarations for main program case
1552
1553       if not No_Main_Subprogram then
1554
1555          --  To call the main program, we declare it using a pragma Import
1556          --  Ada with the right link name.
1557
1558          --  It might seem more obvious to "with" the main program, and call
1559          --  it in the normal Ada manner. We do not do this for three reasons:
1560
1561          --    1. It is more efficient not to recompile the main program
1562          --    2. We are not entitled to assume the source is accessible
1563          --    3. We don't know what options to use to compile it
1564
1565          --  It is really reason 3 that is most critical (indeed we used
1566          --  to generate the "with", but several regression tests failed).
1567
1568          WBI ("");
1569
1570          if ALIs.Table (ALIs.First).Main_Program = Func then
1571             WBI ("      Result : Integer;");
1572             WBI ("");
1573             WBI ("      function Ada_Main_Program return Integer;");
1574
1575          else
1576             WBI ("      procedure Ada_Main_Program;");
1577          end if;
1578
1579          Set_String ("      pragma Import (Ada, Ada_Main_Program, """);
1580          Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1581          Set_Main_Program_Name;
1582          Set_String (""");");
1583
1584          Write_Statement_Buffer;
1585          WBI ("");
1586       end if;
1587
1588       --  Generate a reference to Ada_Main_Program_Name. This symbol is
1589       --  not referenced elsewhere in the generated program, but is needed
1590       --  by the debugger (that's why it is generated in the first place).
1591       --  The reference stops Ada_Main_Program_Name from being optimized
1592       --  away by smart linkers, such as the AiX linker.
1593
1594       if Bind_Main_Program then
1595          WBI
1596            ("      Ensure_Reference : System.Address := " &
1597             "Ada_Main_Program_Name'Address;");
1598          WBI ("");
1599       end if;
1600
1601       WBI ("   begin");
1602
1603       --  Acquire command line arguments if present on target
1604
1605       if Command_Line_Args_On_Target then
1606          WBI ("      gnat_argc := argc;");
1607          WBI ("      gnat_argv := argv;");
1608          WBI ("      gnat_envp := envp;");
1609          WBI ("");
1610
1611       --  If configurable run time and no command line args, then nothing
1612       --  needs to be done since the gnat_argc/argv/envp variables are
1613       --  suppressed in this case.
1614
1615       elsif Configurable_Run_Time_On_Target then
1616          null;
1617
1618       --  Otherwise set dummy values (to be filled in by some other unit?)
1619
1620       else
1621          WBI ("      gnat_argc := 0;");
1622          WBI ("      gnat_argv := System.Null_Address;");
1623          WBI ("      gnat_envp := System.Null_Address;");
1624       end if;
1625
1626       if not Cumulative_Restrictions.Set (No_Finalization) then
1627          WBI ("      Initialize;");
1628       end if;
1629
1630       WBI ("      " & Ada_Init_Name.all & ";");
1631
1632       if not No_Main_Subprogram then
1633          WBI ("      Break_Start;");
1634
1635          if ALIs.Table (ALIs.First).Main_Program = Proc then
1636             WBI ("      Ada_Main_Program;");
1637          else
1638             WBI ("      Result := Ada_Main_Program;");
1639          end if;
1640       end if;
1641
1642       --  Adafinal call is skipped if no finalization
1643
1644       if not Cumulative_Restrictions.Set (No_Finalization) then
1645
1646          --  If compiling for the JVM, we directly call Adafinal because
1647          --  we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1648
1649          if Hostparm.Java_VM then
1650             WBI ("      System.Standard_Library.Adafinal;");
1651          else
1652             WBI ("      Do_Finalize;");
1653          end if;
1654       end if;
1655
1656       --  Finalize is only called if we have a run time
1657
1658       if not Cumulative_Restrictions.Set (No_Finalization) then
1659          WBI ("      Finalize;");
1660       end if;
1661
1662       --  Return result
1663
1664       if Exit_Status_Supported_On_Target then
1665          if No_Main_Subprogram
1666            or else ALIs.Table (ALIs.First).Main_Program = Proc
1667          then
1668             WBI ("      return (gnat_exit_status);");
1669          else
1670             WBI ("      return (Result);");
1671          end if;
1672       end if;
1673
1674       WBI ("   end;");
1675    end Gen_Main_Ada;
1676
1677    ----------------
1678    -- Gen_Main_C --
1679    ----------------
1680
1681    procedure Gen_Main_C is
1682    begin
1683       if Exit_Status_Supported_On_Target then
1684          Set_String ("int ");
1685       else
1686          Set_String ("void ");
1687       end if;
1688
1689       Set_String (Get_Main_Name);
1690
1691       --  Generate command line args in prototype if present on target
1692
1693       if Command_Line_Args_On_Target then
1694          Write_Statement_Buffer (" (int argc, char **argv, char **envp)");
1695
1696       --  Case of no command line arguments on target
1697
1698       else
1699          Write_Statement_Buffer (" ()");
1700       end if;
1701
1702       WBI ("{");
1703
1704       --  Generate a reference to __gnat_ada_main_program_name. This symbol
1705       --  is  not referenced elsewhere in the generated program, but is
1706       --  needed by the debugger (that's why it is generated in the first
1707       --  place). The reference stops Ada_Main_Program_Name from being
1708       --  optimized away by smart linkers, such as the AiX linker.
1709
1710       if Bind_Main_Program then
1711          WBI ("   char *ensure_reference __attribute__ ((__unused__)) = " &
1712               "__gnat_ada_main_program_name;");
1713          WBI ("");
1714       end if;
1715
1716       --  If main program is a function, generate result variable
1717
1718       if ALIs.Table (ALIs.First).Main_Program = Func then
1719          WBI ("   int result;");
1720       end if;
1721
1722       --  Set command line argument values from parameters if command line
1723       --  arguments are present on target
1724
1725       if Command_Line_Args_On_Target then
1726          WBI ("   gnat_argc = argc;");
1727          WBI ("   gnat_argv = argv;");
1728          WBI ("   gnat_envp = envp;");
1729          WBI (" ");
1730
1731       --  If configurable run-time, then nothing to do, since in this case
1732       --  the gnat_argc/argv/envp variables are entirely suppressed.
1733
1734       elsif Configurable_Run_Time_On_Target then
1735          null;
1736
1737       --  if no command line arguments on target, set dummy values
1738
1739       else
1740          WBI ("   int result;");
1741          WBI ("   gnat_argc = 0;");
1742          WBI ("   gnat_argv = 0;");
1743          WBI ("   gnat_envp = 0;");
1744       end if;
1745
1746       --  The __gnat_initialize routine is used only if we have a run-time
1747
1748       if not Suppress_Standard_Library_On_Target then
1749          WBI
1750           ("   __gnat_initialize ();");
1751       end if;
1752
1753       WBI ("   " & Ada_Init_Name.all & " ();");
1754
1755       if not No_Main_Subprogram then
1756          WBI ("   __gnat_break_start ();");
1757          WBI (" ");
1758
1759          --  Output main program name
1760
1761          Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1762
1763          --  Main program is procedure case
1764
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;
1770
1771          --  Main program is function case
1772
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;
1779
1780       end if;
1781
1782       --  Call adafinal if finalization active
1783
1784       if not Cumulative_Restrictions.Set (No_Finalization) then
1785          WBI (" ");
1786          WBI ("   system__standard_library__adafinal ();");
1787       end if;
1788
1789       --  The finalize routine is used only if we have a run-time
1790
1791       if not Suppress_Standard_Library_On_Target then
1792          WBI ("   __gnat_finalize ();");
1793       end if;
1794
1795       --  Case of main program is a function, so the value it returns
1796       --  is the exit status in this case.
1797
1798       if ALIs.Table (ALIs.First).Main_Program = Func then
1799          if Exit_Status_Supported_On_Target then
1800
1801             --  VMS must use Posix exit routine in order to get the effect
1802             --  of a Unix compatible setting of the program exit status.
1803             --  For all other systems, we use the standard exit routine.
1804
1805             if OpenVMS_On_Target then
1806                WBI ("   __posix_exit (result);");
1807             else
1808                WBI ("   exit (result);");
1809             end if;
1810          end if;
1811
1812       --  Case of main program is a procedure, in which case the exit
1813       --  status is whatever was set by a Set_Exit call most recently
1814
1815       else
1816          if Exit_Status_Supported_On_Target then
1817
1818             --  VMS must use Posix exit routine in order to get the effect
1819             --  of a Unix compatible setting of the program exit status.
1820             --  For all other systems, we use the standard exit routine.
1821
1822             if OpenVMS_On_Target then
1823                WBI ("   __posix_exit (gnat_exit_status);");
1824             else
1825                WBI ("   exit (gnat_exit_status);");
1826             end if;
1827          end if;
1828       end if;
1829
1830       WBI ("}");
1831    end Gen_Main_C;
1832
1833    ------------------------------
1834    -- Gen_Object_Files_Options --
1835    ------------------------------
1836
1837    procedure Gen_Object_Files_Options is
1838       Lgnat : Natural;
1839       --  This keeps track of the position in the sorted set of entries
1840       --  in the Linker_Options table of where the first entry from an
1841       --  internal file appears.
1842
1843       procedure Write_Linker_Option;
1844       --  Write binder info linker option.
1845
1846       -------------------------
1847       -- Write_Linker_Option --
1848       -------------------------
1849
1850       procedure Write_Linker_Option is
1851          Start : Natural;
1852          Stop  : Natural;
1853
1854       begin
1855          --  Loop through string, breaking at null's
1856
1857          Start := 1;
1858          while Start < Name_Len loop
1859
1860             --  Find null ending this section
1861
1862             Stop := Start + 1;
1863             while Name_Buffer (Stop) /= ASCII.NUL
1864               and then Stop <= Name_Len loop
1865                Stop := Stop + 1;
1866             end loop;
1867
1868             --  Process section if non-null
1869
1870             if Stop > Start then
1871                   if Output_Linker_Option_List then
1872                      Write_Str (Name_Buffer (Start .. Stop - 1));
1873                      Write_Eol;
1874                   end if;
1875                   Write_Info_Ada_C
1876                     ("   --   ", "", Name_Buffer (Start .. Stop - 1));
1877             end if;
1878
1879             Start := Stop + 1;
1880          end loop;
1881       end Write_Linker_Option;
1882
1883    --  Start of processing for Gen_Object_Files_Options
1884
1885    begin
1886       WBI ("");
1887       Write_Info_Ada_C ("-- ", "/* ", " BEGIN Object file/option list");
1888
1889       for E in Elab_Order.First .. Elab_Order.Last loop
1890
1891          --  If not spec that has an associated body, then generate a
1892          --  comment giving the name of the corresponding object file.
1893
1894          if (not Units.Table (Elab_Order.Table (E)).Interface)
1895            and then Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec
1896          then
1897             Get_Name_String
1898               (ALIs.Table
1899                 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1900
1901             --  If the presence of an object file is necessary or if it
1902             --  exists, then use it.
1903
1904             if not Hostparm.Exclude_Missing_Objects
1905               or else GNAT.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1906             then
1907                Write_Info_Ada_C ("   --   ", "", Name_Buffer (1 .. Name_Len));
1908
1909                if Output_Object_List then
1910                   Write_Str (Name_Buffer (1 .. Name_Len));
1911                   Write_Eol;
1912                end if;
1913
1914                --  Don't link with the shared library on VMS if an internal
1915                --  filename object is seen. Multiply defined symbols will
1916                --  result.
1917
1918                if Hostparm.OpenVMS
1919                  and then Is_Internal_File_Name
1920                   (ALIs.Table
1921                    (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
1922                then
1923                   --  Special case for g-trasym.obj, which is not included
1924                   --  in libgnat.
1925
1926                   Get_Name_String (ALIs.Table
1927                             (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile);
1928
1929                   if Name_Buffer (1 .. 8) /= "g-trasym" then
1930                      Opt.Shared_Libgnat := False;
1931                   end if;
1932                end if;
1933             end if;
1934          end if;
1935       end loop;
1936
1937       --  Add a "-Ldir" for each directory in the object path
1938
1939       for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1940          declare
1941             Dir : constant String_Ptr := Dir_In_Obj_Search_Path (J);
1942          begin
1943             Name_Len := 0;
1944             Add_Str_To_Name_Buffer ("-L");
1945             Add_Str_To_Name_Buffer (Dir.all);
1946             Write_Linker_Option;
1947          end;
1948       end loop;
1949
1950       --  Sort linker options
1951
1952       --  This sort accomplishes two important purposes:
1953
1954       --    a) All application files are sorted to the front, and all
1955       --       GNAT internal files are sorted to the end. This results
1956       --       in a well defined dividing line between the two sets of
1957       --       files, for the purpose of inserting certain standard
1958       --       library references into the linker arguments list.
1959
1960       --    b) Given two different units, we sort the linker options so
1961       --       that those from a unit earlier in the elaboration order
1962       --       comes later in the list. This is a heuristic designed
1963       --       to create a more friendly order of linker options when
1964       --       the operations appear in separate units. The idea is that
1965       --       if unit A must be elaborated before unit B, then it is
1966       --       more likely that B references libraries included by A,
1967       --       than vice versa, so we want the libraries included by
1968       --       A to come after the libraries included by B.
1969
1970       --  These two criteria are implemented by function Lt_Linker_Option.
1971       --  Note that a special case of b) is that specs are elaborated before
1972       --  bodies, so linker options from specs come after linker options
1973       --  for bodies, and again, the assumption is that libraries used by
1974       --  the body are more likely to reference libraries used by the spec,
1975       --  than vice versa.
1976
1977       Sort
1978         (Linker_Options.Last,
1979          Move_Linker_Option'Access,
1980          Lt_Linker_Option'Access);
1981
1982       --  Write user linker options, i.e. the set of linker options that
1983       --  come from all files other than GNAT internal files, Lgnat is
1984       --  left set to point to the first entry from a GNAT internal file,
1985       --  or past the end of the entriers if there are no internal files.
1986
1987       Lgnat := Linker_Options.Last + 1;
1988
1989       for J in 1 .. Linker_Options.Last loop
1990          if not Linker_Options.Table (J).Internal_File then
1991             Get_Name_String (Linker_Options.Table (J).Name);
1992             Write_Linker_Option;
1993          else
1994             Lgnat := J;
1995             exit;
1996          end if;
1997       end loop;
1998
1999       --  Now we insert standard linker options that must appear after the
2000       --  entries from user files, and before the entries from GNAT run-time
2001       --  files. The reason for this decision is that libraries referenced
2002       --  by internal routines may reference these standard library entries.
2003
2004       if not Opt.No_Stdlib then
2005          Name_Len := 0;
2006
2007          if Opt.Shared_Libgnat then
2008             Add_Str_To_Name_Buffer ("-shared");
2009          else
2010             Add_Str_To_Name_Buffer ("-static");
2011          end if;
2012
2013          --  Write directly to avoid -K output (why???)
2014
2015          Write_Info_Ada_C ("   --   ", "", Name_Buffer (1 .. Name_Len));
2016
2017          if With_DECGNAT then
2018             Name_Len := 0;
2019             Add_Str_To_Name_Buffer ("-ldecgnat");
2020             Write_Linker_Option;
2021          end if;
2022
2023          if With_GNARL then
2024             Name_Len := 0;
2025
2026             if Opt.Shared_Libgnat then
2027                Add_Str_To_Name_Buffer (Shared_Lib ("gnarl"));
2028             else
2029                Add_Str_To_Name_Buffer ("-lgnarl");
2030             end if;
2031
2032             Write_Linker_Option;
2033          end if;
2034
2035          Name_Len := 0;
2036
2037          if Opt.Shared_Libgnat then
2038             Add_Str_To_Name_Buffer (Shared_Lib ("gnat"));
2039          else
2040             Add_Str_To_Name_Buffer ("-lgnat");
2041          end if;
2042
2043          Write_Linker_Option;
2044       end if;
2045
2046       --  Write linker options from all internal files
2047
2048       for J in Lgnat .. Linker_Options.Last loop
2049          Get_Name_String (Linker_Options.Table (J).Name);
2050          Write_Linker_Option;
2051       end loop;
2052
2053       if Ada_Bind_File then
2054          WBI ("--  END Object file/option list   ");
2055       else
2056          WBI ("    END Object file/option list */");
2057       end if;
2058    end Gen_Object_Files_Options;
2059
2060    ---------------------
2061    -- Gen_Output_File --
2062    ---------------------
2063
2064    procedure Gen_Output_File (Filename : String) is
2065       Is_Public_Version : constant Boolean := Get_Gnat_Build_Type = Public;
2066       Is_GAP_Version    : constant Boolean := Get_Gnat_Build_Type = GAP;
2067
2068    begin
2069       --  Acquire settings for Interrupt_State pragmas
2070
2071       Set_IS_Pragma_Table;
2072
2073       --  Override Ada_Bind_File and Bind_Main_Program for Java since
2074       --  JGNAT only supports Ada code, and the main program is already
2075       --  generated by the compiler.
2076
2077       if Hostparm.Java_VM then
2078          Ada_Bind_File := True;
2079          Bind_Main_Program := False;
2080       end if;
2081
2082       --  Override time slice value if -T switch is set
2083
2084       if Time_Slice_Set then
2085          ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
2086       end if;
2087
2088       --  Count number of elaboration calls
2089
2090       for E in Elab_Order.First .. Elab_Order.Last loop
2091          if Units.Table (Elab_Order.Table (E)).No_Elab then
2092             null;
2093          else
2094             Num_Elab_Calls := Num_Elab_Calls + 1;
2095          end if;
2096       end loop;
2097
2098       --  Get the time stamp of the former bind for public version warning
2099
2100       if Is_Public_Version or Is_GAP_Version then
2101          Record_Time_From_Last_Bind;
2102       end if;
2103
2104       --  Generate output file in appropriate language
2105
2106       if Ada_Bind_File then
2107          Gen_Output_File_Ada (Filename);
2108       else
2109          Gen_Output_File_C (Filename);
2110       end if;
2111
2112       --  Periodically issue a warning when the public version is used on
2113       --  big projects
2114
2115       if Is_Public_Version then
2116          Public_Version_Warning;
2117       end if;
2118    end Gen_Output_File;
2119
2120    -------------------------
2121    -- Gen_Output_File_Ada --
2122    -------------------------
2123
2124    procedure Gen_Output_File_Ada (Filename : String) is
2125
2126       Bfiles : Name_Id;
2127       --  Name of generated bind file (spec)
2128
2129       Bfileb : Name_Id;
2130       --  Name of generated bind file (body)
2131
2132       Ada_Main : constant String := Get_Ada_Main_Name;
2133       --  Name to be used for generated Ada main program. See the body of
2134       --  function Get_Ada_Main_Name for details on the form of the name.
2135
2136    begin
2137       --  Create spec first
2138
2139       Create_Binder_Output (Filename, 's', Bfiles);
2140
2141       --  If we are operating in Restrictions (No_Exception_Handlers) mode,
2142       --  then we need to make sure that the binder program is compiled with
2143       --  the same restriction, so that no exception tables are generated.
2144
2145       if Cumulative_Restrictions.Set (No_Exception_Handlers) then
2146          WBI ("pragma Restrictions (No_Exception_Handlers);");
2147       end if;
2148
2149       --  Generate with of System so we can reference System.Address
2150
2151       WBI ("with System;");
2152
2153       --  Generate with of System.Initialize_Scalars if active
2154
2155       if Initialize_Scalars_Used then
2156          WBI ("with System.Scalar_Values;");
2157       end if;
2158
2159       --  Generate with of System.Secondary_Stack if active
2160
2161       if Sec_Stack_Used and then Default_Sec_Stack_Size /= -1 then
2162          WBI ("with System.Secondary_Stack;");
2163       end if;
2164
2165       Resolve_Binder_Options;
2166
2167       if not Suppress_Standard_Library_On_Target then
2168
2169          --  Usually, adafinal is called using a pragma Import C. Since
2170          --  Import C doesn't have the same semantics for JGNAT, we use
2171          --  standard Ada.
2172
2173          if Hostparm.Java_VM then
2174             WBI ("with System.Standard_Library;");
2175          end if;
2176       end if;
2177
2178       WBI ("package " & Ada_Main & " is");
2179       WBI ("   pragma Warnings (Off);");
2180
2181       --  Main program case
2182
2183       if Bind_Main_Program then
2184
2185          --  Generate argc/argv stuff unless suppressed
2186
2187          if Command_Line_Args_On_Target
2188            or not Configurable_Run_Time_On_Target
2189          then
2190             WBI ("");
2191             WBI ("   gnat_argc : Integer;");
2192             WBI ("   gnat_argv : System.Address;");
2193             WBI ("   gnat_envp : System.Address;");
2194
2195             --  If the standard library is not suppressed, these variables are
2196             --  in the runtime data area for easy access from the runtime
2197
2198             if not Suppress_Standard_Library_On_Target then
2199                WBI ("");
2200                WBI ("   pragma Import (C, gnat_argc);");
2201                WBI ("   pragma Import (C, gnat_argv);");
2202                WBI ("   pragma Import (C, gnat_envp);");
2203             end if;
2204          end if;
2205
2206          --  Define exit status. Again in normal mode, this is in the
2207          --  run-time library, and is initialized there, but in the
2208          --  configurable runtime case, the variable is declared and
2209          --  initialized in this file.
2210
2211          WBI ("");
2212
2213          if Configurable_Run_Time_Mode then
2214             if Exit_Status_Supported_On_Target then
2215                WBI ("   gnat_exit_status : Integer := 0;");
2216             end if;
2217          else
2218             WBI ("   gnat_exit_status : Integer;");
2219             WBI ("   pragma Import (C, gnat_exit_status);");
2220          end if;
2221       end if;
2222
2223       --  Generate the GNAT_Version and Ada_Main_Program_Name info only for
2224       --  the main program. Otherwise, it can lead under some circumstances
2225       --  to a symbol duplication during the link (for instance when a
2226       --  C program uses 2 Ada libraries)
2227
2228       if Bind_Main_Program then
2229          WBI ("");
2230          WBI ("   GNAT_Version : constant String :=");
2231          WBI ("                    ""GNAT Version: " &
2232                                    Gnat_Version_String & """;");
2233          WBI ("   pragma Export (C, GNAT_Version, ""__gnat_version"");");
2234
2235          WBI ("");
2236          Set_String ("   Ada_Main_Program_Name : constant String := """);
2237          Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2238          Set_Main_Program_Name;
2239          Set_String (""" & Ascii.NUL;");
2240          Write_Statement_Buffer;
2241
2242          WBI
2243            ("   pragma Export (C, Ada_Main_Program_Name, " &
2244             """__gnat_ada_main_program_name"");");
2245       end if;
2246
2247       --  No need to generate a finalization routine if finalization
2248       --  is restricted, since there is nothing to do in this case.
2249
2250       if not Cumulative_Restrictions.Set (No_Finalization) then
2251          WBI ("");
2252          WBI ("   procedure " & Ada_Final_Name.all & ";");
2253          WBI ("   pragma Export (C, " & Ada_Final_Name.all & ", """ &
2254               Ada_Final_Name.all & """);");
2255       end if;
2256
2257       WBI ("");
2258       WBI ("   procedure " & Ada_Init_Name.all & ";");
2259       WBI ("   pragma Export (C, " & Ada_Init_Name.all & ", """ &
2260            Ada_Init_Name.all & """);");
2261
2262       if Bind_Main_Program then
2263
2264          --  If we have the standard library, then Break_Start is defined
2265          --  there, but when the standard library is suppressed, Break_Start
2266          --  is defined here.
2267
2268          WBI ("");
2269          WBI ("   procedure Break_Start;");
2270
2271          if Suppress_Standard_Library_On_Target then
2272             WBI ("   pragma Export (C, Break_Start, ""__gnat_break_start"");");
2273          else
2274             WBI ("   pragma Import (C, Break_Start, ""__gnat_break_start"");");
2275          end if;
2276
2277          WBI ("");
2278
2279          if Exit_Status_Supported_On_Target then
2280             Set_String ("   function ");
2281          else
2282             Set_String ("   procedure ");
2283          end if;
2284
2285          Set_String (Get_Main_Name);
2286
2287          --  Generate argument list if present
2288
2289          if Command_Line_Args_On_Target then
2290             Write_Statement_Buffer;
2291             WBI ("     (argc : Integer;");
2292             WBI ("      argv : System.Address;");
2293             Set_String
2294                 ("      envp : System.Address)");
2295
2296             if Exit_Status_Supported_On_Target then
2297                Write_Statement_Buffer;
2298                WBI ("      return Integer;");
2299             else
2300                Write_Statement_Buffer (";");
2301             end if;
2302
2303          else
2304             if Exit_Status_Supported_On_Target then
2305                Write_Statement_Buffer (" return Integer;");
2306             else
2307                Write_Statement_Buffer (";");
2308             end if;
2309          end if;
2310
2311          WBI ("   pragma Export (C, " & Get_Main_Name & ", """ &
2312            Get_Main_Name & """);");
2313       end if;
2314
2315       Gen_Versions_Ada;
2316       Gen_Elab_Order_Ada;
2317
2318       --  Spec is complete
2319
2320       WBI ("");
2321       WBI ("end " & Ada_Main & ";");
2322       Close_Binder_Output;
2323
2324       --  Prepare to write body
2325
2326       Create_Binder_Output (Filename, 'b', Bfileb);
2327
2328       --  Output Source_File_Name pragmas which look like
2329
2330       --    pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
2331       --    pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
2332
2333       --  where sss/bbb are the spec/body file names respectively
2334
2335       Get_Name_String (Bfiles);
2336       Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2337
2338       WBI ("pragma Source_File_Name (" &
2339            Ada_Main &
2340            ", Spec_File_Name => """ &
2341            Name_Buffer (1 .. Name_Len + 3));
2342
2343       Get_Name_String (Bfileb);
2344       Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
2345
2346       WBI ("pragma Source_File_Name (" &
2347            Ada_Main &
2348            ", Body_File_Name => """ &
2349            Name_Buffer (1 .. Name_Len + 3));
2350
2351       WBI ("");
2352       WBI ("package body " & Ada_Main & " is");
2353       WBI ("   pragma Warnings (Off);");
2354
2355       --  Import the finalization procedure only if finalization active
2356
2357       if not Cumulative_Restrictions.Set (No_Finalization) then
2358
2359          --  In the Java case, pragma Import C cannot be used, so the
2360          --  standard Ada constructs will be used instead.
2361
2362          if not Hostparm.Java_VM then
2363             WBI ("");
2364             WBI ("   procedure Do_Finalize;");
2365             WBI
2366               ("   pragma Import (C, Do_Finalize, " &
2367                """system__standard_library__adafinal"");");
2368             WBI ("");
2369          end if;
2370       end if;
2371
2372       Gen_Adainit_Ada;
2373
2374       --  No need to generate a finalization routine if no finalization
2375
2376       if not Cumulative_Restrictions.Set (No_Finalization) then
2377          Gen_Adafinal_Ada;
2378       end if;
2379
2380       if Bind_Main_Program then
2381
2382          --  When suppressing the standard library then generate dummy body
2383          --  for Break_Start
2384
2385          if Suppress_Standard_Library_On_Target then
2386             WBI ("");
2387             WBI ("   procedure Break_Start is");
2388             WBI ("   begin");
2389             WBI ("      null;");
2390             WBI ("   end;");
2391          end if;
2392
2393          Gen_Main_Ada;
2394       end if;
2395
2396       --  Output object file list and the Ada body is complete
2397
2398       Gen_Object_Files_Options;
2399
2400       WBI ("");
2401       WBI ("end " & Ada_Main & ";");
2402
2403       Close_Binder_Output;
2404    end Gen_Output_File_Ada;
2405
2406    -----------------------
2407    -- Gen_Output_File_C --
2408    -----------------------
2409
2410    procedure Gen_Output_File_C (Filename : String) is
2411
2412       Bfile : Name_Id;
2413       --  Name of generated bind file
2414
2415    begin
2416       Create_Binder_Output (Filename, 'c', Bfile);
2417
2418       Resolve_Binder_Options;
2419
2420       WBI ("extern void __gnat_set_globals");
2421       WBI ("  (int, int, char, char, char, char,");
2422       WBI ("   const char *, const char *,");
2423       WBI ("   int, int, int, int);");
2424       WBI ("extern void " & Ada_Final_Name.all & " (void);");
2425       WBI ("extern void " & Ada_Init_Name.all & " (void);");
2426       WBI ("extern void system__standard_library__adafinal (void);");
2427
2428       if not No_Main_Subprogram then
2429          Set_String ("extern ");
2430
2431          if Exit_Status_Supported_On_Target then
2432             Set_String ("int");
2433          else
2434             Set_String ("void");
2435          end if;
2436
2437          Set_String (" main ");
2438
2439          if Command_Line_Args_On_Target then
2440             Write_Statement_Buffer ("(int, char **, char **);");
2441          else
2442             Write_Statement_Buffer ("(void);");
2443          end if;
2444
2445          if OpenVMS_On_Target then
2446             WBI ("extern void __posix_exit (int);");
2447          else
2448             WBI ("extern void exit (int);");
2449          end if;
2450
2451          WBI ("extern void __gnat_break_start (void);");
2452          Set_String ("extern ");
2453
2454          if ALIs.Table (ALIs.First).Main_Program = Proc then
2455             Set_String ("void ");
2456          else
2457             Set_String ("int ");
2458          end if;
2459
2460          Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2461          Set_Main_Program_Name;
2462          Set_String (" (void);");
2463          Write_Statement_Buffer;
2464       end if;
2465
2466       if not Suppress_Standard_Library_On_Target then
2467          WBI ("extern void __gnat_initialize (void);");
2468          WBI ("extern void __gnat_finalize (void);");
2469          WBI ("extern void __gnat_install_handler (void);");
2470       end if;
2471
2472       WBI ("");
2473
2474       Gen_Elab_Defs_C;
2475
2476       --  Imported variable used to track elaboration/finalization phase.
2477       --  Used only when we have a runtime.
2478
2479       if not Suppress_Standard_Library_On_Target then
2480          WBI ("extern int  __gnat_handler_installed;");
2481          WBI ("");
2482       end if;
2483
2484       --  Write argv/argc exit status stuff if main program case
2485
2486       if Bind_Main_Program then
2487
2488          --  First deal with argc/argv/envp. In the normal case they
2489          --  are in the run-time library.
2490
2491          if not Configurable_Run_Time_On_Target then
2492             WBI ("extern int gnat_argc;");
2493             WBI ("extern char **gnat_argv;");
2494             WBI ("extern char **gnat_envp;");
2495             WBI ("extern int gnat_exit_status;");
2496
2497          --  If configurable run time and no command line args, then the
2498          --  generation of these variables is entirely suppressed.
2499
2500          elsif not Command_Line_Args_On_Target then
2501             null;
2502
2503          --  Otherwise, in the configurable run-time case they are right in
2504          --  the binder file.
2505
2506          else
2507             WBI ("int gnat_argc;");
2508             WBI ("char **gnat_argv;");
2509             WBI ("char **gnat_envp;");
2510             WBI ("int gnat_exit_status = 0;");
2511          end if;
2512
2513          --  Similarly deal with exit status
2514          --  are in the run-time library.
2515
2516          if not Configurable_Run_Time_On_Target then
2517             WBI ("extern int gnat_exit_status;");
2518
2519          --  If configurable run time and no exit status on target, then
2520          --  the generation of this variables is entirely suppressed.
2521
2522          elsif not Exit_Status_Supported_On_Target then
2523             null;
2524
2525          --  Otherwise, in the configurable run-time case this variable is
2526          --  right in the binder file, and initialized to zero there.
2527
2528          else
2529             WBI ("int gnat_exit_status = 0;");
2530          end if;
2531
2532          WBI ("");
2533       end if;
2534
2535       --  When suppressing the standard library, the __gnat_break_start
2536       --  routine (for the debugger to get initial control) is defined in
2537       --  this file.
2538
2539       if Suppress_Standard_Library_On_Target then
2540          WBI ("");
2541          WBI ("void __gnat_break_start () {}");
2542       end if;
2543
2544       --  Generate the __gnat_version and __gnat_ada_main_program_name info
2545       --  only for the main program. Otherwise, it can lead under some
2546       --  circumstances to a symbol duplication during the link (for instance
2547       --  when a C program uses 2 Ada libraries)
2548
2549       if Bind_Main_Program then
2550          WBI ("");
2551          WBI ("char __gnat_version[] = ""GNAT Version: " &
2552                                    Gnat_Version_String & """;");
2553
2554          Set_String ("char __gnat_ada_main_program_name[] = """);
2555          Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2556          Set_Main_Program_Name;
2557          Set_String (""";");
2558          Write_Statement_Buffer;
2559       end if;
2560
2561       --  Generate the adafinal routine. In no runtime mode, this is
2562       --  not needed, since there is no finalization to do.
2563
2564       if not Cumulative_Restrictions.Set (No_Finalization) then
2565          Gen_Adafinal_C;
2566       end if;
2567
2568       Gen_Adainit_C;
2569
2570       --  Main is only present for Ada main case
2571
2572       if Bind_Main_Program then
2573          Gen_Main_C;
2574       end if;
2575
2576       --  Generate versions, elaboration order, list of object files
2577
2578       Gen_Versions_C;
2579       Gen_Elab_Order_C;
2580       Gen_Object_Files_Options;
2581
2582       --  C binder output is complete
2583
2584       Close_Binder_Output;
2585    end Gen_Output_File_C;
2586
2587    -------------------------------
2588    -- Gen_Restrictions_String_1 --
2589    -------------------------------
2590
2591    procedure Gen_Restrictions_String_1 is
2592    begin
2593       for R in All_Boolean_Restrictions loop
2594          if Cumulative_Restrictions.Set (R) then
2595             Set_Char ('r');
2596          elsif Cumulative_Restrictions.Violated (R) then
2597             Set_Char ('v');
2598          else
2599             Set_Char ('n');
2600          end if;
2601       end loop;
2602    end Gen_Restrictions_String_1;
2603
2604    -------------------------------
2605    -- Gen_Restrictions_String_2 --
2606    -------------------------------
2607
2608    procedure Gen_Restrictions_String_2 is
2609    begin
2610       for RP in All_Parameter_Restrictions loop
2611          if Cumulative_Restrictions.Set (RP) then
2612             Set_Char ('r');
2613             Set_Int (Int (Cumulative_Restrictions.Value (RP)));
2614          else
2615             Set_Char ('n');
2616          end if;
2617
2618          if not Cumulative_Restrictions.Violated (RP)
2619            or else RP not in Checked_Parameter_Restrictions
2620          then
2621             Set_Char ('n');
2622          else
2623             Set_Char ('v');
2624             Set_Int (Int (Cumulative_Restrictions.Count (RP)));
2625
2626             if Cumulative_Restrictions.Unknown (RP) then
2627                Set_Char ('+');
2628             end if;
2629          end if;
2630       end loop;
2631    end Gen_Restrictions_String_2;
2632
2633    ----------------------
2634    -- Gen_Versions_Ada --
2635    ----------------------
2636
2637    --  This routine generates two sets of lines. The first set has the form:
2638
2639    --    unnnnn : constant Integer := 16#hhhhhhhh#;
2640
2641    --  The second set has the form
2642
2643    --    pragma Export (C, unnnnn, unam);
2644
2645    --  for each unit, where unam is the unit name suffixed by either B or
2646    --  S for body or spec, with dots replaced by double underscores, and
2647    --  hhhhhhhh is the version number, and nnnnn is a 5-digits serial number.
2648
2649    procedure Gen_Versions_Ada is
2650       Ubuf : String (1 .. 6) := "u00000";
2651
2652       procedure Increment_Ubuf;
2653       --  Little procedure to increment the serial number
2654
2655       procedure Increment_Ubuf is
2656       begin
2657          for J in reverse Ubuf'Range loop
2658             Ubuf (J) := Character'Succ (Ubuf (J));
2659             exit when Ubuf (J) <= '9';
2660             Ubuf (J) := '0';
2661          end loop;
2662       end Increment_Ubuf;
2663
2664    --  Start of processing for Gen_Versions_Ada
2665
2666    begin
2667       if Bind_For_Library then
2668
2669          --  When building libraries, the version number of each unit can
2670          --  not be computed, since the binder does not know the full list
2671          --  of units. Therefore, the 'Version and 'Body_Version
2672          --  attributes can not supported in this case.
2673
2674          return;
2675       end if;
2676
2677       WBI ("");
2678
2679       WBI ("   type Version_32 is mod 2 ** 32;");
2680       for U in Units.First .. Units.Last loop
2681          Increment_Ubuf;
2682          WBI ("   " & Ubuf & " : constant Version_32 := 16#" &
2683               Units.Table (U).Version & "#;");
2684       end loop;
2685
2686       WBI ("");
2687       Ubuf := "u00000";
2688
2689       for U in Units.First .. Units.Last loop
2690          Increment_Ubuf;
2691          Set_String ("   pragma Export (C, ");
2692          Set_String (Ubuf);
2693          Set_String (", """);
2694
2695          Get_Name_String (Units.Table (U).Uname);
2696
2697          for K in 1 .. Name_Len loop
2698             if Name_Buffer (K) = '.' then
2699                Set_Char ('_');
2700                Set_Char ('_');
2701
2702             elsif Name_Buffer (K) = '%' then
2703                exit;
2704
2705             else
2706                Set_Char (Name_Buffer (K));
2707             end if;
2708          end loop;
2709
2710          if Name_Buffer (Name_Len) = 's' then
2711             Set_Char ('S');
2712          else
2713             Set_Char ('B');
2714          end if;
2715
2716          Set_String (""");");
2717          Write_Statement_Buffer;
2718       end loop;
2719
2720    end Gen_Versions_Ada;
2721
2722    --------------------
2723    -- Gen_Versions_C --
2724    --------------------
2725
2726    --  This routine generates a line of the form:
2727
2728    --    unsigned unam = 0xhhhhhhhh;
2729
2730    --  for each unit, where unam is the unit name suffixed by either B or
2731    --  S for body or spec, with dots replaced by double underscores.
2732
2733    procedure Gen_Versions_C is
2734    begin
2735       if Bind_For_Library then
2736
2737          --  When building libraries, the version number of each unit can
2738          --  not be computed, since the binder does not know the full list
2739          --  of units. Therefore, the 'Version and 'Body_Version
2740          --  attributes can not supported.
2741
2742          return;
2743       end if;
2744
2745       for U in Units.First .. Units.Last loop
2746          Set_String ("unsigned ");
2747
2748          Get_Name_String (Units.Table (U).Uname);
2749
2750          for K in 1 .. Name_Len loop
2751             if Name_Buffer (K) = '.' then
2752                Set_String ("__");
2753
2754             elsif Name_Buffer (K) = '%' then
2755                exit;
2756
2757             else
2758                Set_Char (Name_Buffer (K));
2759             end if;
2760          end loop;
2761
2762          if Name_Buffer (Name_Len) = 's' then
2763             Set_Char ('S');
2764          else
2765             Set_Char ('B');
2766          end if;
2767
2768          Set_String (" = 0x");
2769          Set_String (Units.Table (U).Version);
2770          Set_Char   (';');
2771          Write_Statement_Buffer;
2772       end loop;
2773
2774    end Gen_Versions_C;
2775
2776    -----------------------
2777    -- Get_Ada_Main_Name --
2778    -----------------------
2779
2780    function Get_Ada_Main_Name return String is
2781       Suffix : constant String := "_00";
2782       Name   : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2783                  Opt.Ada_Main_Name.all & Suffix;
2784       Nlen   : Natural;
2785
2786    begin
2787       --  The main program generated by JGNAT expects a package called
2788       --  ada_<main procedure>.
2789
2790       if Hostparm.Java_VM then
2791          --  Get main program name
2792
2793          Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2794
2795          --  Remove the %b
2796
2797          return "ada_" & Name_Buffer (1 .. Name_Len - 2);
2798       end if;
2799
2800       --  This loop tries the following possibilities in order
2801       --    <Ada_Main>
2802       --    <Ada_Main>_01
2803       --    <Ada_Main>_02
2804       --    ..
2805       --    <Ada_Main>_99
2806       --  where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2807       --  it is set to 'ada_main'.
2808
2809       for J in 0 .. 99 loop
2810          if J = 0 then
2811             Nlen := Name'Length - Suffix'Length;
2812          else
2813             Nlen := Name'Length;
2814             Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2815             Name (Name'Last - 1) :=
2816               Character'Val (J /   10 + Character'Pos ('0'));
2817          end if;
2818
2819          for K in ALIs.First .. ALIs.Last loop
2820             for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2821
2822                --  Get unit name, removing %b or %e at end
2823
2824                Get_Name_String (Units.Table (L).Uname);
2825                Name_Len := Name_Len - 2;
2826
2827                if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2828                   goto Continue;
2829                end if;
2830             end loop;
2831          end loop;
2832
2833          return Name (1 .. Nlen);
2834
2835       <<Continue>>
2836          null;
2837       end loop;
2838
2839       --  If we fall through, just use a peculiar unlikely name
2840
2841       return ("Qwertyuiop");
2842    end Get_Ada_Main_Name;
2843
2844    -------------------
2845    -- Get_Main_Name --
2846    -------------------
2847
2848    function Get_Main_Name return String is
2849    begin
2850       --  Explicit name given with -M switch
2851
2852       if Bind_Alternate_Main_Name then
2853          return Alternate_Main_Name.all;
2854
2855       --  Case of main program name to be used directly
2856
2857       elsif Use_Ada_Main_Program_Name_On_Target then
2858
2859          --  Get main program name
2860
2861          Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2862
2863          --  If this is a child name, return only the name of the child,
2864          --  since we can't have dots in a nested program name. Note that
2865          --  we do not include the %b at the end of the unit name.
2866
2867          for J in reverse 1 .. Name_Len - 2 loop
2868             if J = 1 or else Name_Buffer (J - 1) = '.' then
2869                return Name_Buffer (J .. Name_Len - 2);
2870             end if;
2871          end loop;
2872
2873          raise Program_Error; -- impossible exit
2874
2875       --  Case where "main" is to be used as default
2876
2877       else
2878          return "main";
2879       end if;
2880    end Get_Main_Name;
2881
2882    ----------------------
2883    -- Lt_Linker_Option --
2884    ----------------------
2885
2886    function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
2887    begin
2888       --  Sort internal files last
2889
2890       if Linker_Options.Table (Op1).Internal_File
2891            /=
2892          Linker_Options.Table (Op2).Internal_File
2893       then
2894          --  Note: following test uses False < True
2895
2896          return Linker_Options.Table (Op1).Internal_File
2897                   <
2898                 Linker_Options.Table (Op2).Internal_File;
2899
2900       --  If both internal or both non-internal, sort according to the
2901       --  elaboration position. A unit that is elaborated later should
2902       --  come earlier in the linker options list.
2903
2904       else
2905          return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2906                   >
2907                 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2908
2909       end if;
2910    end Lt_Linker_Option;
2911
2912    ------------------------
2913    -- Move_Linker_Option --
2914    ------------------------
2915
2916    procedure Move_Linker_Option (From : Natural; To : Natural) is
2917    begin
2918       Linker_Options.Table (To) := Linker_Options.Table (From);
2919    end Move_Linker_Option;
2920
2921    ----------------------------
2922    -- Public_Version_Warning --
2923    ----------------------------
2924
2925    procedure Public_Version_Warning is
2926       Time : constant Int := Time_From_Last_Bind;
2927
2928       --  Constants to help defining periods
2929
2930       Hour : constant := 60;
2931       Day  : constant := 24 * Hour;
2932
2933       Never : constant := Integer'Last;
2934       --  Special value indicating no warnings should be given
2935
2936       --  Constants defining when the warning is issued. Programs with more
2937       --  than Large Units will issue a warning every Period_Large amount of
2938       --  time. Smaller programs will generate a warning every Period_Small
2939       --  amount of time.
2940
2941       Large : constant := 20;
2942       --  Threshold for considering a program small or large
2943
2944       Period_Large : constant := Day;
2945       --  Periodic warning time for large programs
2946
2947       Period_Small : constant := Never;
2948       --  Periodic warning time for small programs
2949
2950       Nb_Unit : Int;
2951
2952    begin
2953       --  Compute the number of units that are not GNAT internal files
2954
2955       Nb_Unit := 0;
2956       for A in ALIs.First .. ALIs.Last loop
2957          if not Is_Internal_File_Name (ALIs.Table (A).Sfile) then
2958             Nb_Unit := Nb_Unit + 1;
2959          end if;
2960       end loop;
2961
2962       --  Do not emit the message if the last message was emitted in the
2963       --  specified period taking into account the number of units.
2964
2965       pragma Warnings (Off);
2966       --  Turn off warning of constant condition, which may happen here
2967       --  depending on the choice of constants in the above declarations.
2968
2969       if Nb_Unit < Large and then Time <= Period_Small then
2970          return;
2971       elsif Time <= Period_Large then
2972          return;
2973       end if;
2974
2975       pragma Warnings (On);
2976
2977       Write_Eol;
2978       Write_Str ("IMPORTANT NOTICE:");
2979       Write_Eol;
2980       Write_Str ("    This version of GNAT is unsupported"
2981         &                        " and comes with absolutely no warranty.");
2982       Write_Eol;
2983       Write_Str ("    If you intend to evaluate or use GNAT for building "
2984         &                                       "commercial applications,");
2985       Write_Eol;
2986       Write_Str ("    please consult http://www.gnat.com/ for information");
2987       Write_Eol;
2988       Write_Str ("    on the GNAT Professional product line.");
2989       Write_Eol;
2990       Write_Eol;
2991    end Public_Version_Warning;
2992
2993    ----------------------------
2994    -- Resolve_Binder_Options --
2995    ----------------------------
2996
2997    procedure Resolve_Binder_Options is
2998    begin
2999       for E in Elab_Order.First .. Elab_Order.Last loop
3000          Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
3001
3002          --  The procedure of looking for specific packages and setting
3003          --  flags is somewhat dubious, but there isn't a good alternative
3004          --  at the current time ???
3005
3006          if Name_Buffer (1 .. 19) = "system.os_interface" then
3007             With_GNARL := True;
3008          end if;
3009
3010          if Hostparm.OpenVMS and then Name_Buffer (1 .. 5) = "dec%s" then
3011             With_DECGNAT := True;
3012          end if;
3013       end loop;
3014    end Resolve_Binder_Options;
3015
3016    --------------
3017    -- Set_Char --
3018    --------------
3019
3020    procedure Set_Char (C : Character) is
3021    begin
3022       Last := Last + 1;
3023       Statement_Buffer (Last) := C;
3024    end Set_Char;
3025
3026    -------------
3027    -- Set_Int --
3028    -------------
3029
3030    procedure Set_Int (N : Int) is
3031    begin
3032       if N < 0 then
3033          Set_String ("-");
3034          Set_Int (-N);
3035
3036       else
3037          if N > 9 then
3038             Set_Int (N / 10);
3039          end if;
3040
3041          Last := Last + 1;
3042          Statement_Buffer (Last) :=
3043            Character'Val (N mod 10 + Character'Pos ('0'));
3044       end if;
3045    end Set_Int;
3046
3047    -------------------------
3048    -- Set_IS_Pragma_Table --
3049    -------------------------
3050
3051    procedure Set_IS_Pragma_Table is
3052    begin
3053       for F in ALIs.First .. ALIs.Last loop
3054          for K in ALIs.Table (F).First_Interrupt_State ..
3055                   ALIs.Table (F).Last_Interrupt_State
3056          loop
3057             declare
3058                Inum : constant Int :=
3059                         Interrupt_States.Table (K).Interrupt_Id;
3060                Stat : constant Character :=
3061                         Interrupt_States.Table (K).Interrupt_State;
3062
3063             begin
3064                while IS_Pragma_Settings.Last < Inum loop
3065                   IS_Pragma_Settings.Append ('n');
3066                end loop;
3067
3068                IS_Pragma_Settings.Table (Inum) := Stat;
3069             end;
3070          end loop;
3071       end loop;
3072    end Set_IS_Pragma_Table;
3073
3074    ---------------------------
3075    -- Set_Main_Program_Name --
3076    ---------------------------
3077
3078    procedure Set_Main_Program_Name is
3079    begin
3080       --  Note that name has %b on the end which we ignore
3081
3082       --  First we output the initial _ada_ since we know that the main
3083       --  program is a library level subprogram.
3084
3085       Set_String ("_ada_");
3086
3087       --  Copy name, changing dots to double underscores
3088
3089       for J in 1 .. Name_Len - 2 loop
3090          if Name_Buffer (J) = '.' then
3091             Set_String ("__");
3092          else
3093             Set_Char (Name_Buffer (J));
3094          end if;
3095       end loop;
3096    end Set_Main_Program_Name;
3097
3098    ---------------------
3099    -- Set_Name_Buffer --
3100    ---------------------
3101
3102    procedure Set_Name_Buffer is
3103    begin
3104       for J in 1 .. Name_Len loop
3105          Set_Char (Name_Buffer (J));
3106       end loop;
3107    end Set_Name_Buffer;
3108
3109    ----------------
3110    -- Set_String --
3111    ----------------
3112
3113    procedure Set_String (S : String) is
3114    begin
3115       Statement_Buffer (Last + 1 .. Last + S'Length) := S;
3116       Last := Last + S'Length;
3117    end Set_String;
3118
3119    -------------------
3120    -- Set_Unit_Name --
3121    -------------------
3122
3123    procedure Set_Unit_Name is
3124    begin
3125       for J in 1 .. Name_Len - 2 loop
3126          if Name_Buffer (J) /= '.' then
3127             Set_Char (Name_Buffer (J));
3128          else
3129             Set_String ("__");
3130          end if;
3131       end loop;
3132    end Set_Unit_Name;
3133
3134    ---------------------
3135    -- Set_Unit_Number --
3136    ---------------------
3137
3138    procedure Set_Unit_Number (U : Unit_Id) is
3139       Num_Units : constant Nat := Nat (Units.Last) - Nat (Unit_Id'First);
3140       Unum      : constant Nat := Nat (U) - Nat (Unit_Id'First);
3141
3142    begin
3143       if Num_Units >= 10 and then Unum < 10 then
3144          Set_Char ('0');
3145       end if;
3146
3147       if Num_Units >= 100 and then Unum < 100 then
3148          Set_Char ('0');
3149       end if;
3150
3151       Set_Int (Unum);
3152    end Set_Unit_Number;
3153
3154    ------------
3155    -- Tab_To --
3156    ------------
3157
3158    procedure Tab_To (N : Natural) is
3159    begin
3160       while Last < N loop
3161          Set_Char (' ');
3162       end loop;
3163    end Tab_To;
3164
3165    ----------------------
3166    -- Write_Info_Ada_C --
3167    ----------------------
3168
3169    procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
3170    begin
3171       if Ada_Bind_File then
3172          declare
3173             S : String (1 .. Ada'Length + Common'Length);
3174          begin
3175             S (1 .. Ada'Length) := Ada;
3176             S (Ada'Length + 1 .. S'Length) := Common;
3177             WBI (S);
3178          end;
3179
3180       else
3181          declare
3182             S : String (1 .. C'Length + Common'Length);
3183          begin
3184             S (1 .. C'Length) := C;
3185             S (C'Length + 1 .. S'Length) := Common;
3186             WBI (S);
3187          end;
3188       end if;
3189    end Write_Info_Ada_C;
3190
3191    ----------------------------
3192    -- Write_Statement_Buffer --
3193    ----------------------------
3194
3195    procedure Write_Statement_Buffer is
3196    begin
3197       WBI (Statement_Buffer (1 .. Last));
3198       Last := 0;
3199    end Write_Statement_Buffer;
3200
3201    procedure Write_Statement_Buffer (S : String) is
3202    begin
3203       Set_String (S);
3204       Write_Statement_Buffer;
3205    end Write_Statement_Buffer;
3206
3207 end Bindgen;