Add hppa-openbsd target
[official-gcc.git] / gcc / ada / bindgen.adb
blobd2fa1c753e680317bd334838f2545484899c5d68
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- B I N D G E N --
6 -- --
7 -- B o d y --
8 -- --
9 -- --
10 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
11 -- --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
22 -- --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 -- --
26 ------------------------------------------------------------------------------
28 with ALI; use ALI;
29 with Binde; use Binde;
30 with Butil; use Butil;
31 with Casing; use Casing;
32 with Fname; use Fname;
33 with GNAT.OS_Lib; use GNAT.OS_Lib;
34 with Gnatvsn; use Gnatvsn;
35 with Hostparm;
36 with Namet; use Namet;
37 with Opt; use Opt;
38 with Osint; use Osint;
39 with Osint.B; use Osint.B;
40 with Output; use Output;
41 with Types; use Types;
42 with Sdefault; use Sdefault;
44 with GNAT.Heap_Sort_A; use GNAT.Heap_Sort_A;
46 package body Bindgen is
48 Statement_Buffer : String (1 .. 1000);
49 -- Buffer used for constructing output statements
51 Last : Natural := 0;
52 -- Last location in Statement_Buffer currently set
54 With_DECGNAT : Boolean := False;
55 -- Flag which indicates whether the program uses the DECGNAT library
56 -- (presence of the unit System.Aux_DEC.DECLIB)
58 With_GNARL : Boolean := False;
59 -- Flag which indicates whether the program uses the GNARL library
60 -- (presence of the unit System.OS_Interface)
62 Num_Elab_Calls : Nat := 0;
63 -- Number of generated calls to elaboration routines
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure WBI (Info : String) renames Osint.B.Write_Binder_Info;
70 -- Convenient shorthand used throughout
72 procedure Resolve_Binder_Options;
73 -- Set the value of With_GNARL and With_DECGNAT. The latter only on VMS
74 -- since it tests for a package named "dec" which might cause a conflict
75 -- on non-VMS systems.
77 procedure Gen_Adainit_Ada;
78 -- Generates the Adainit procedure (Ada code case)
80 procedure Gen_Adainit_C;
81 -- Generates the Adainit procedure (C code case)
83 procedure Gen_Adafinal_Ada;
84 -- Generate the Adafinal procedure (Ada code case)
86 procedure Gen_Adafinal_C;
87 -- Generate the Adafinal procedure (C code case)
89 procedure Gen_Elab_Calls_Ada;
90 -- Generate sequence of elaboration calls (Ada code case)
92 procedure Gen_Elab_Calls_C;
93 -- Generate sequence of elaboration calls (C code case)
95 procedure Gen_Elab_Order_Ada;
96 -- Generate comments showing elaboration order chosen (Ada case)
98 procedure Gen_Elab_Order_C;
99 -- Generate comments showing elaboration order chosen (C case)
101 procedure Gen_Elab_Defs_C;
102 -- Generate sequence of definitions for elaboration routines (C code case)
104 procedure Gen_Exception_Table_Ada;
105 -- Generate binder exception table (Ada code case). This consists of
106 -- declarations followed by a begin followed by a call. If zero cost
107 -- exceptions are not active, then only the begin is generated.
109 procedure Gen_Exception_Table_C;
110 -- Generate binder exception table (C code case). This has no effect
111 -- if zero cost exceptions are not active, otherwise it generates a
112 -- set of declarations followed by a call.
114 procedure Gen_Main_Ada;
115 -- Generate procedure main (Ada code case)
117 procedure Gen_Main_C;
118 -- Generate main() procedure (C code case)
120 procedure Gen_Object_Files_Options;
121 -- Output comments containing a list of the full names of the object
122 -- files to be linked and the list of linker options supplied by
123 -- Linker_Options pragmas in the source. (C and Ada code case)
125 procedure Gen_Output_File_Ada (Filename : String);
126 -- Generate output file (Ada code case)
128 procedure Gen_Output_File_C (Filename : String);
129 -- Generate output file (C code case)
131 procedure Gen_Scalar_Values;
132 -- Generates scalar initialization values for -Snn. A single procedure
133 -- handles both the Ada and C cases, since there is much common code.
135 procedure Gen_Versions_Ada;
136 -- Output series of definitions for unit versions (Ada code case)
138 procedure Gen_Versions_C;
139 -- Output series of definitions for unit versions (C code case)
141 function Get_Ada_Main_Name return String;
142 -- This function is used in the Ada main output case to compute a usable
143 -- name for the generated main program. The normal main program name is
144 -- Ada_Main, but this won't work if the user has a unit with this name.
145 -- This function tries Ada_Main first, and if there is such a clash, then
146 -- it tries Ada_Name_01, Ada_Name_02 ... Ada_Name_99 in sequence.
148 function Get_Main_Name return String;
149 -- This function is used in the Ada main output case to compute the
150 -- correct external main program. It is "main" by default, except on
151 -- VxWorks where it is the name of the Ada main name without the "_ada".
152 -- the -Mname binder option overrides the default with name.
154 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean;
155 -- Compare linker options, when sorting, first according to
156 -- Is_Internal_File (internal files come later) and then by
157 -- elaboration order position (latest to earliest).
159 procedure Move_Linker_Option (From : Natural; To : Natural);
160 -- Move routine for sorting linker options
162 procedure Public_Version_Warning;
163 -- Emit a warning concerning the use of the Public version under
164 -- certain circumstances. See details in body.
166 procedure Set_Char (C : Character);
167 -- Set given character in Statement_Buffer at the Last + 1 position
168 -- and increment Last by one to reflect the stored character.
170 procedure Set_Int (N : Int);
171 -- Set given value in decimal in Statement_Buffer with no spaces
172 -- starting at the Last + 1 position, and updating Last past the value.
173 -- A minus sign is output for a negative value.
175 procedure Set_Main_Program_Name;
176 -- Given the main program name in Name_Buffer (length in Name_Len)
177 -- generate the name of the routine to be used in the call. The name
178 -- is generated starting at Last + 1, and Last is updated past it.
180 procedure Set_Name_Buffer;
181 -- Set the value stored in positions 1 .. Name_Len of the Name_Buffer.
183 procedure Set_String (S : String);
184 -- Sets characters of given string in Statement_Buffer, starting at the
185 -- Last + 1 position, and updating last past the string value.
187 procedure Set_Unit_Name;
188 -- Given a unit name in the Name_Buffer, copies it to Statement_Buffer,
189 -- starting at the Last + 1 position, and updating last past the value.
190 -- changing periods to double underscores, and updating Last appropriately.
192 procedure Set_Unit_Number (U : Unit_Id);
193 -- Sets unit number (first unit is 1, leading zeroes output to line
194 -- up all output unit numbers nicely as required by the value, and
195 -- by the total number of units.
197 procedure Tab_To (N : Natural);
198 -- If Last is greater than or equal to N, no effect, otherwise store
199 -- blanks in Statement_Buffer bumping Last, until Last = N.
201 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String);
202 -- For C code case, write C & Common, for Ada case write Ada & Common
203 -- to current binder output file using Write_Binder_Info.
205 procedure Write_Statement_Buffer;
206 -- Write out contents of statement buffer up to Last, and reset Last to 0
208 procedure Write_Statement_Buffer (S : String);
209 -- First writes its argument (using Set_String (S)), then writes out the
210 -- contents of statement buffer up to Last, and reset Last to 0
212 ----------------------
213 -- Gen_Adafinal_Ada --
214 ----------------------
216 procedure Gen_Adafinal_Ada is
217 begin
218 WBI ("");
219 WBI (" procedure " & Ada_Final_Name.all & " is");
220 WBI (" begin");
222 -- If compiling for the JVM, we directly call Adafinal because
223 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
225 if Hostparm.Java_VM then
226 WBI (" System.Standard_Library.Adafinal;");
227 else
228 WBI (" Do_Finalize;");
229 end if;
231 WBI (" end " & Ada_Final_Name.all & ";");
232 end Gen_Adafinal_Ada;
234 --------------------
235 -- Gen_Adafinal_C --
236 --------------------
238 procedure Gen_Adafinal_C is
239 begin
240 WBI ("void " & Ada_Final_Name.all & " () {");
241 WBI (" system__standard_library__adafinal ();");
242 WBI ("}");
243 WBI ("");
244 end Gen_Adafinal_C;
246 ---------------------
247 -- Gen_Adainit_Ada --
248 ---------------------
250 procedure Gen_Adainit_Ada is
251 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
253 begin
254 WBI (" procedure " & Ada_Init_Name.all & " is");
256 -- Generate externals for elaboration entities
258 for E in Elab_Order.First .. Elab_Order.Last loop
259 declare
260 Unum : constant Unit_Id := Elab_Order.Table (E);
261 U : Unit_Record renames Units.Table (Unum);
263 begin
264 if U.Set_Elab_Entity then
265 Set_String (" ");
266 Set_String ("E");
267 Set_Unit_Number (Unum);
268 Set_String (" : Boolean; pragma Import (Ada, ");
269 Set_String ("E");
270 Set_Unit_Number (Unum);
271 Set_String (", """);
272 Get_Name_String (U.Uname);
274 -- In the case of JGNAT we need to emit an Import name
275 -- that includes the class name (using '$' separators
276 -- in the case of a child unit name).
278 if Hostparm.Java_VM then
279 for J in 1 .. Name_Len - 2 loop
280 if Name_Buffer (J) /= '.' then
281 Set_Char (Name_Buffer (J));
282 else
283 Set_String ("$");
284 end if;
285 end loop;
287 Set_String (".");
289 -- If the unit name is very long, then split the
290 -- Import link name across lines using "&" (occurs
291 -- in some C2 tests).
293 if 2 * Name_Len + 60 > Hostparm.Max_Line_Length then
294 Set_String (""" &");
295 Write_Statement_Buffer;
296 Set_String (" """);
297 end if;
298 end if;
300 Set_Unit_Name;
301 Set_String ("_E"");");
302 Write_Statement_Buffer;
303 end if;
304 end;
305 end loop;
307 Write_Statement_Buffer;
309 -- Case of No_Run_Time mode. The only global variable that might
310 -- be needed (by the Ravenscar profile) is the priority of the
311 -- environment. Also no exception tables are needed.
313 if No_Run_Time_Specified then
314 if Main_Priority /= No_Main_Priority then
315 WBI (" Main_Priority : Integer;");
316 WBI (" pragma Import (C, Main_Priority," &
317 " ""__gl_main_priority"");");
318 WBI ("");
319 end if;
321 WBI (" begin");
323 if Main_Priority /= No_Main_Priority then
324 Set_String (" Main_Priority := ");
325 Set_Int (Main_Priority);
326 Set_Char (';');
327 Write_Statement_Buffer;
329 else
330 WBI (" null;");
331 end if;
333 -- Normal case (not No_Run_Time mode). The global values are
334 -- assigned using the runtime routine Set_Globals (we have to use
335 -- the routine call, rather than define the globals in the binder
336 -- file to deal with cross-library calls in some systems.
338 else
339 -- Generate restrictions string
341 Set_String (" Restrictions : constant String :=");
342 Write_Statement_Buffer;
343 Set_String (" """);
345 for J in Restrictions'Range loop
346 Set_Char (Restrictions (J));
347 end loop;
349 Set_String (""";");
350 Write_Statement_Buffer;
351 WBI ("");
353 WBI (" procedure Set_Globals");
354 WBI (" (Main_Priority : Integer;");
355 WBI (" Time_Slice_Value : Integer;");
356 WBI (" WC_Encoding : Character;");
357 WBI (" Locking_Policy : Character;");
358 WBI (" Queuing_Policy : Character;");
359 WBI (" Task_Dispatching_Policy : Character;");
360 WBI (" Restrictions : System.Address;");
361 WBI (" Unreserve_All_Interrupts : Integer;");
362 WBI (" Exception_Tracebacks : Integer;");
363 WBI (" Zero_Cost_Exceptions : Integer);");
364 WBI (" pragma Import (C, Set_Globals, ""__gnat_set_globals"");");
366 -- Import entry point for elaboration time signal handler
367 -- installation, and indication of whether it's been called
368 -- previously
370 WBI ("");
371 WBI (" procedure Install_Handler;");
372 WBI (" pragma Import (C, Install_Handler, " &
373 """__gnat_install_handler"");");
374 WBI ("");
375 WBI (" Handler_Installed : Integer;");
376 WBI (" pragma Import (C, Handler_Installed, " &
377 """__gnat_handler_installed"");");
379 -- Generate exception table
381 Gen_Exception_Table_Ada;
383 -- Generate the call to Set_Globals
385 WBI (" Set_Globals");
387 Set_String (" (Main_Priority => ");
388 Set_Int (Main_Priority);
389 Set_Char (',');
390 Write_Statement_Buffer;
392 Set_String (" Time_Slice_Value => ");
394 if Task_Dispatching_Policy_Specified = 'F'
395 and then ALIs.Table (ALIs.First).Time_Slice_Value = -1
396 then
397 Set_Int (0);
398 else
399 Set_Int (ALIs.Table (ALIs.First).Time_Slice_Value);
400 end if;
402 Set_Char (',');
403 Write_Statement_Buffer;
405 Set_String (" WC_Encoding => '");
406 Set_Char (ALIs.Table (ALIs.First).WC_Encoding);
407 Set_String ("',");
408 Write_Statement_Buffer;
410 Set_String (" Locking_Policy => '");
411 Set_Char (Locking_Policy_Specified);
412 Set_String ("',");
413 Write_Statement_Buffer;
415 Set_String (" Queuing_Policy => '");
416 Set_Char (Queuing_Policy_Specified);
417 Set_String ("',");
418 Write_Statement_Buffer;
420 Set_String (" Task_Dispatching_Policy => '");
421 Set_Char (Task_Dispatching_Policy_Specified);
422 Set_String ("',");
423 Write_Statement_Buffer;
425 WBI (" Restrictions => Restrictions'Address,");
427 Set_String (" Unreserve_All_Interrupts => ");
429 if Unreserve_All_Interrupts_Specified then
430 Set_String ("1");
431 else
432 Set_String ("0");
433 end if;
435 Set_String (",");
436 Write_Statement_Buffer;
438 Set_String (" Exception_Tracebacks => ");
440 if Exception_Tracebacks then
441 Set_String ("1");
442 else
443 Set_String ("0");
444 end if;
446 Set_String (",");
447 Write_Statement_Buffer;
449 Set_String (" Zero_Cost_Exceptions => ");
451 if Zero_Cost_Exceptions_Specified then
452 Set_String ("1");
453 else
454 Set_String ("0");
455 end if;
457 Set_String (");");
458 Write_Statement_Buffer;
460 -- Generate call to Install_Handler
461 WBI ("");
462 WBI (" if Handler_Installed = 0 then");
463 WBI (" Install_Handler;");
464 WBI (" end if;");
465 end if;
467 Gen_Elab_Calls_Ada;
469 WBI (" end " & Ada_Init_Name.all & ";");
470 end Gen_Adainit_Ada;
472 -------------------
473 -- Gen_Adainit_C --
474 --------------------
476 procedure Gen_Adainit_C is
477 Main_Priority : Int renames ALIs.Table (ALIs.First).Main_Priority;
479 begin
480 WBI ("void " & Ada_Init_Name.all & " ()");
481 WBI ("{");
483 -- Generate externals for elaboration entities
485 for E in Elab_Order.First .. Elab_Order.Last loop
486 declare
487 Unum : constant Unit_Id := Elab_Order.Table (E);
488 U : Unit_Record renames Units.Table (Unum);
490 begin
491 if U.Set_Elab_Entity then
492 Set_String (" extern char ");
493 Get_Name_String (U.Uname);
494 Set_Unit_Name;
495 Set_String ("_E;");
496 Write_Statement_Buffer;
497 end if;
498 end;
499 end loop;
501 Write_Statement_Buffer;
503 -- No run-time case
505 if No_Run_Time_Specified then
507 -- Case of No_Run_Time mode. Set __gl_main_priority if needed
508 -- for the Ravenscar profile.
510 if Main_Priority /= No_Main_Priority then
511 Set_String (" extern int __gl_main_priority = ");
512 Set_Int (Main_Priority);
513 Set_Char (';');
514 Write_Statement_Buffer;
515 end if;
517 -- Normal case (run time present)
519 else
520 -- Generate definition for restrictions string
522 Set_String (" const char *restrictions = """);
524 for J in Restrictions'Range loop
525 Set_Char (Restrictions (J));
526 end loop;
528 Set_String (""";");
529 Write_Statement_Buffer;
531 -- Code for normal case (not in No_Run_Time mode)
533 Gen_Exception_Table_C;
535 -- Generate call to set the runtime global variables defined in
536 -- a-init.c. We define the varables in a-init.c, rather than in
537 -- the binder generated file itself to avoid undefined externals
538 -- when the runtime is linked as a shareable image library.
540 -- We call the routine from inside adainit() because this works for
541 -- both programs with and without binder generated "main" functions.
543 WBI (" __gnat_set_globals (");
545 Set_String (" ");
546 Set_Int (Main_Priority);
547 Set_Char (',');
548 Tab_To (15);
549 Set_String ("/* Main_Priority */");
550 Write_Statement_Buffer;
552 Set_String (" ");
554 if Task_Dispatching_Policy = '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;
562 Set_Char (',');
563 Tab_To (20);
564 Set_String ("/* Time_Slice_Value */");
565 Write_Statement_Buffer;
567 Set_String (" '");
568 Set_Char (ALIs.Table (ALIs.First).WC_Encoding);
569 Set_String ("',");
570 Tab_To (20);
571 Set_String ("/* WC_Encoding */");
572 Write_Statement_Buffer;
574 Set_String (" '");
575 Set_Char (Locking_Policy_Specified);
576 Set_String ("',");
577 Tab_To (20);
578 Set_String ("/* Locking_Policy */");
579 Write_Statement_Buffer;
581 Set_String (" '");
582 Set_Char (Queuing_Policy_Specified);
583 Set_String ("',");
584 Tab_To (20);
585 Set_String ("/* Queuing_Policy */");
586 Write_Statement_Buffer;
588 Set_String (" '");
589 Set_Char (Task_Dispatching_Policy_Specified);
590 Set_String ("',");
591 Tab_To (20);
592 Set_String ("/* Tasking_Dispatching_Policy */");
593 Write_Statement_Buffer;
595 Set_String (" ");
596 Set_String ("restrictions");
597 Set_String (",");
598 Tab_To (20);
599 Set_String ("/* Restrictions */");
600 Write_Statement_Buffer;
602 Set_String (" ");
603 Set_Int (Boolean'Pos (Unreserve_All_Interrupts_Specified));
604 Set_String (",");
605 Tab_To (20);
606 Set_String ("/* Unreserve_All_Interrupts */");
607 Write_Statement_Buffer;
609 Set_String (" ");
610 Set_Int (Boolean'Pos (Exception_Tracebacks));
611 Set_String (",");
612 Tab_To (20);
613 Set_String ("/* Exception_Tracebacks */");
614 Write_Statement_Buffer;
616 Set_String (" ");
617 Set_Int (Boolean'Pos (Zero_Cost_Exceptions_Specified));
618 Set_String (");");
619 Tab_To (20);
620 Set_String ("/* Zero_Cost_Exceptions */");
621 Write_Statement_Buffer;
623 -- Install elaboration time signal handler
625 WBI (" if (__gnat_handler_installed == 0)");
626 WBI (" {");
627 WBI (" __gnat_install_handler ();");
628 WBI (" }");
629 end if;
631 WBI ("");
632 Gen_Elab_Calls_C;
633 WBI ("}");
634 end Gen_Adainit_C;
636 ------------------------
637 -- Gen_Elab_Calls_Ada --
638 ------------------------
640 procedure Gen_Elab_Calls_Ada is
641 begin
643 for E in Elab_Order.First .. Elab_Order.Last loop
644 declare
645 Unum : constant Unit_Id := Elab_Order.Table (E);
646 U : Unit_Record renames Units.Table (Unum);
648 Unum_Spec : Unit_Id;
649 -- This is the unit number of the spec that corresponds to
650 -- this entry. It is the same as Unum except when the body
651 -- and spec are different and we are currently processing
652 -- the body, in which case it is the spec (Unum + 1).
654 begin
655 if U.Utype = Is_Body then
656 Unum_Spec := Unum + 1;
657 else
658 Unum_Spec := Unum;
659 end if;
661 -- Case of no elaboration code
663 if U.No_Elab then
665 -- The only case in which we have to do something is if
666 -- this is a body, with a separate spec, where the separate
667 -- spec has an elaboration entity defined.
669 -- In that case, this is where we set the elaboration entity
670 -- to True, we do not need to test if this has already been
671 -- done, since it is quicker to set the flag than to test it.
673 if U.Utype = Is_Body
674 and then Units.Table (Unum_Spec).Set_Elab_Entity
675 then
676 Set_String (" E");
677 Set_Unit_Number (Unum_Spec);
678 Set_String (" := True;");
679 Write_Statement_Buffer;
680 end if;
682 -- Here if elaboration code is present. We generate:
684 -- if not uname_E then
685 -- uname'elab_[spec|body];
686 -- uname_E := True;
687 -- end if;
689 -- The uname_E assignment is skipped if this is a separate spec,
690 -- since the assignment will be done when we process the body.
692 else
693 Set_String (" if not E");
694 Set_Unit_Number (Unum_Spec);
695 Set_String (" then");
696 Write_Statement_Buffer;
698 Set_String (" ");
699 Get_Decoded_Name_String_With_Brackets (U.Uname);
701 if Name_Buffer (Name_Len) = 's' then
702 Name_Buffer (Name_Len - 1 .. Name_Len + 8) := "'elab_spec";
703 else
704 Name_Buffer (Name_Len - 1 .. Name_Len + 8) := "'elab_body";
705 end if;
707 Name_Len := Name_Len + 8;
708 Set_Casing (U.Icasing);
709 Set_Name_Buffer;
710 Set_Char (';');
711 Write_Statement_Buffer;
713 if U.Utype /= Is_Spec then
714 Set_String (" E");
715 Set_Unit_Number (Unum_Spec);
716 Set_String (" := True;");
717 Write_Statement_Buffer;
718 end if;
720 WBI (" end if;");
721 end if;
722 end;
723 end loop;
725 end Gen_Elab_Calls_Ada;
727 ----------------------
728 -- Gen_Elab_Calls_C --
729 ----------------------
731 procedure Gen_Elab_Calls_C is
732 begin
734 for E in Elab_Order.First .. Elab_Order.Last loop
735 declare
736 Unum : constant Unit_Id := Elab_Order.Table (E);
737 U : Unit_Record renames Units.Table (Unum);
739 Unum_Spec : Unit_Id;
740 -- This is the unit number of the spec that corresponds to
741 -- this entry. It is the same as Unum except when the body
742 -- and spec are different and we are currently processing
743 -- the body, in which case it is the spec (Unum + 1).
745 begin
746 if U.Utype = Is_Body then
747 Unum_Spec := Unum + 1;
748 else
749 Unum_Spec := Unum;
750 end if;
752 -- Case of no elaboration code
754 if U.No_Elab then
756 -- The only case in which we have to do something is if
757 -- this is a body, with a separate spec, where the separate
758 -- spec has an elaboration entity defined.
760 -- In that case, this is where we set the elaboration entity
761 -- to True, we do not need to test if this has already been
762 -- done, since it is quicker to set the flag than to test it.
764 if U.Utype = Is_Body
765 and then Units.Table (Unum_Spec).Set_Elab_Entity
766 then
767 Set_String (" ");
768 Get_Name_String (U.Uname);
769 Set_Unit_Name;
770 Set_String ("_E = 1;");
771 Write_Statement_Buffer;
772 end if;
774 -- Here if elaboration code is present. We generate:
776 -- if (uname_E == 0) {
777 -- uname__elab[s|b] ();
778 -- uname_E++;
779 -- }
781 -- The uname_E assignment is skipped if this is a separate spec,
782 -- since the assignment will be done when we process the body.
784 else
785 Set_String (" if (");
786 Get_Name_String (U.Uname);
787 Set_Unit_Name;
788 Set_String ("_E == 0) {");
789 Write_Statement_Buffer;
791 Set_String (" ");
792 Set_Unit_Name;
793 Set_String ("___elab");
794 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
795 Set_String (" ();");
796 Write_Statement_Buffer;
798 if U.Utype /= Is_Spec then
799 Set_String (" ");
800 Set_Unit_Name;
801 Set_String ("_E++;");
802 Write_Statement_Buffer;
803 end if;
805 WBI (" }");
806 end if;
807 end;
808 end loop;
810 end Gen_Elab_Calls_C;
812 ----------------------
813 -- Gen_Elab_Defs_C --
814 ----------------------
816 procedure Gen_Elab_Defs_C is
817 begin
818 for E in Elab_Order.First .. Elab_Order.Last loop
820 -- Generate declaration of elaboration procedure if elaboration
821 -- needed. Note that passive units are always excluded.
823 if not Units.Table (Elab_Order.Table (E)).No_Elab then
824 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
825 Set_String ("extern void ");
826 Set_Unit_Name;
827 Set_String ("___elab");
828 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
829 Set_String (" PARAMS ((void));");
830 Write_Statement_Buffer;
831 end if;
833 end loop;
835 WBI ("");
836 end Gen_Elab_Defs_C;
838 ------------------------
839 -- Gen_Elab_Order_Ada --
840 ------------------------
842 procedure Gen_Elab_Order_Ada is
843 begin
844 WBI ("");
845 WBI (" -- BEGIN ELABORATION ORDER");
847 for J in Elab_Order.First .. Elab_Order.Last loop
848 Set_String (" -- ");
849 Get_Unit_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
850 Set_Name_Buffer;
851 Write_Statement_Buffer;
852 end loop;
854 WBI (" -- END ELABORATION ORDER");
855 end Gen_Elab_Order_Ada;
857 ----------------------
858 -- Gen_Elab_Order_C --
859 ----------------------
861 procedure Gen_Elab_Order_C is
862 begin
863 WBI ("");
864 WBI ("/* BEGIN ELABORATION ORDER");
866 for J in Elab_Order.First .. Elab_Order.Last loop
867 Get_Unit_Name_String (Units.Table (Elab_Order.Table (J)).Uname);
868 Set_Name_Buffer;
869 Write_Statement_Buffer;
870 end loop;
872 WBI (" END ELABORATION ORDER */");
873 end Gen_Elab_Order_C;
875 -----------------------------
876 -- Gen_Exception_Table_Ada --
877 -----------------------------
879 procedure Gen_Exception_Table_Ada is
880 Num : Nat;
881 Last : ALI_Id := No_ALI_Id;
883 begin
884 if not Zero_Cost_Exceptions_Specified then
885 WBI (" begin");
886 return;
887 end if;
889 -- The code we generate looks like
891 -- procedure SDP_Table_Build
892 -- (SDP_Addresses : System.Address;
893 -- SDP_Count : Natural;
894 -- Elab_Addresses : System.Address;
895 -- Elab_Addr_Count : Natural);
896 -- pragma Import (C, SDP_Table_Build, "__gnat_SDP_Table_Build");
898 -- ST : aliased constant array (1 .. nnn) of System.Address := (
899 -- unit_name_1'UET_Address,
900 -- unit_name_2'UET_Address,
901 -- ...
902 -- unit_name_3'UET_Address,
904 -- EA : aliased constant array (1 .. eee) of System.Address := (
905 -- adainit'Code_Address,
906 -- adafinal'Code_Address,
907 -- unit_name'elab[spec|body]'Code_Address,
908 -- unit_name'elab[spec|body]'Code_Address,
909 -- unit_name'elab[spec|body]'Code_Address,
910 -- unit_name'elab[spec|body]'Code_Address);
912 -- begin
913 -- SDP_Table_Build (ST'Address, nnn, EA'Address, eee);
915 Num := 0;
916 for A in ALIs.First .. ALIs.Last loop
917 if ALIs.Table (A).Unit_Exception_Table then
918 Num := Num + 1;
919 Last := A;
920 end if;
921 end loop;
923 if Num = 0 then
925 -- Happens with "gnatmake -a -f -gnatL ..."
927 WBI (" ");
928 WBI (" begin");
929 return;
930 end if;
932 WBI (" procedure SDP_Table_Build");
933 WBI (" (SDP_Addresses : System.Address;");
934 WBI (" SDP_Count : Natural;");
935 WBI (" Elab_Addresses : System.Address;");
936 WBI (" Elab_Addr_Count : Natural);");
937 WBI (" " &
938 "pragma Import (C, SDP_Table_Build, ""__gnat_SDP_Table_Build"");");
940 WBI (" ");
941 Set_String (" ST : aliased constant array (1 .. ");
942 Set_Int (Num);
943 Set_String (") of System.Address := (");
945 if Num = 1 then
946 Set_String ("1 => A1);");
947 Write_Statement_Buffer;
949 else
950 Write_Statement_Buffer;
952 for A in ALIs.First .. ALIs.Last loop
953 if ALIs.Table (A).Unit_Exception_Table then
954 Get_Decoded_Name_String_With_Brackets
955 (Units.Table (ALIs.Table (A).First_Unit).Uname);
956 Set_Casing (Mixed_Case);
957 Set_String (" ");
958 Set_String (Name_Buffer (1 .. Name_Len - 2));
959 Set_String ("'UET_Address");
961 if A = Last then
962 Set_String (");");
963 else
964 Set_Char (',');
965 end if;
967 Write_Statement_Buffer;
968 end if;
969 end loop;
970 end if;
972 WBI (" ");
973 Set_String (" EA : aliased constant array (1 .. ");
974 Set_Int (Num_Elab_Calls + 2);
975 Set_String (") of System.Address := (");
976 Write_Statement_Buffer;
977 WBI (" " & Ada_Init_Name.all & "'Code_Address,");
979 -- If compiling for the JVM, we directly reference Adafinal because
980 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
982 if Hostparm.Java_VM then
983 Set_String (" System.Standard_Library.Adafinal'Code_Address");
984 else
985 Set_String (" Do_Finalize'Code_Address");
986 end if;
988 for E in Elab_Order.First .. Elab_Order.Last loop
989 Get_Decoded_Name_String_With_Brackets
990 (Units.Table (Elab_Order.Table (E)).Uname);
992 if Units.Table (Elab_Order.Table (E)).No_Elab then
993 null;
995 else
996 Set_Char (',');
997 Write_Statement_Buffer;
998 Set_String (" ");
1000 if Name_Buffer (Name_Len) = 's' then
1001 Name_Buffer (Name_Len - 1 .. Name_Len + 21) :=
1002 "'elab_spec'code_address";
1003 else
1004 Name_Buffer (Name_Len - 1 .. Name_Len + 21) :=
1005 "'elab_body'code_address";
1006 end if;
1008 Name_Len := Name_Len + 21;
1009 Set_Casing (Units.Table (Elab_Order.Table (E)).Icasing);
1010 Set_Name_Buffer;
1011 end if;
1012 end loop;
1014 Set_String (");");
1015 Write_Statement_Buffer;
1017 WBI (" ");
1018 WBI (" begin");
1020 Set_String (" SDP_Table_Build (ST'Address, ");
1021 Set_Int (Num);
1022 Set_String (", EA'Address, ");
1023 Set_Int (Num_Elab_Calls + 2);
1024 Set_String (");");
1025 Write_Statement_Buffer;
1026 end Gen_Exception_Table_Ada;
1028 ---------------------------
1029 -- Gen_Exception_Table_C --
1030 ---------------------------
1032 procedure Gen_Exception_Table_C is
1033 Num : Nat;
1034 Num2 : Nat;
1036 begin
1037 if not Zero_Cost_Exceptions_Specified then
1038 return;
1039 end if;
1041 -- The code we generate looks like
1043 -- extern void *__gnat_unitname1__SDP;
1044 -- extern void *__gnat_unitname2__SDP;
1045 -- ...
1047 -- void **st[nnn] = {
1048 -- &__gnat_unitname1__SDP,
1049 -- &__gnat_unitname2__SDP,
1050 -- ...
1051 -- &__gnat_unitnamen__SDP};
1053 -- extern void unitname1__elabb ();
1054 -- extern void unitname2__elabb ();
1055 -- ...
1057 -- void (*ea[eee]) () = {
1058 -- adainit,
1059 -- adafinal,
1060 -- unitname1___elab[b,s],
1061 -- unitname2___elab[b,s],
1062 -- ...
1063 -- unitnamen___elab[b,s]};
1065 -- __gnat_SDP_Table_Build (&st, nnn, &ea, eee);
1067 Num := 0;
1068 for A in ALIs.First .. ALIs.Last loop
1069 if ALIs.Table (A).Unit_Exception_Table then
1070 Num := Num + 1;
1072 Set_String (" extern void *__gnat_");
1073 Get_Name_String (Units.Table (ALIs.Table (A).First_Unit).Uname);
1074 Set_Unit_Name;
1075 Set_String ("__SDP");
1076 Set_Char (';');
1077 Write_Statement_Buffer;
1078 end if;
1079 end loop;
1081 if Num = 0 then
1083 -- Happens with "gnatmake -a -f -gnatL ..."
1085 return;
1086 end if;
1088 WBI (" ");
1090 Set_String (" void **st[");
1091 Set_Int (Num);
1092 Set_String ("] = {");
1093 Write_Statement_Buffer;
1095 Num2 := 0;
1096 for A in ALIs.First .. ALIs.Last loop
1097 if ALIs.Table (A).Unit_Exception_Table then
1098 Num2 := Num2 + 1;
1100 Set_String (" &__gnat_");
1101 Get_Name_String (Units.Table (ALIs.Table (A).First_Unit).Uname);
1102 Set_Unit_Name;
1103 Set_String ("__SDP");
1105 if Num = Num2 then
1106 Set_String ("};");
1107 else
1108 Set_Char (',');
1109 end if;
1111 Write_Statement_Buffer;
1112 end if;
1113 end loop;
1115 WBI ("");
1116 for E in Elab_Order.First .. Elab_Order.Last loop
1117 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1119 if Units.Table (Elab_Order.Table (E)).No_Elab then
1120 null;
1122 else
1123 Set_String (" extern void ");
1124 Set_Unit_Name;
1125 Set_String ("___elab");
1126 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1127 Set_String (" ();");
1128 Write_Statement_Buffer;
1129 end if;
1130 end loop;
1132 WBI ("");
1133 Set_String (" void (*ea[");
1134 Set_Int (Num_Elab_Calls + 2);
1135 Set_String ("]) () = {");
1136 Write_Statement_Buffer;
1138 WBI (" " & Ada_Init_Name.all & ",");
1139 Set_String (" system__standard_library__adafinal");
1141 for E in Elab_Order.First .. Elab_Order.Last loop
1142 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
1144 if Units.Table (Elab_Order.Table (E)).No_Elab then
1145 null;
1147 else
1148 Set_Char (',');
1149 Write_Statement_Buffer;
1150 Set_String (" ");
1151 Set_Unit_Name;
1152 Set_String ("___elab");
1153 Set_Char (Name_Buffer (Name_Len)); -- 's' or 'b' for spec/body
1154 end if;
1155 end loop;
1157 Set_String ("};");
1158 Write_Statement_Buffer;
1160 WBI (" ");
1162 Set_String (" __gnat_SDP_Table_Build (&st, ");
1163 Set_Int (Num);
1164 Set_String (", ea, ");
1165 Set_Int (Num_Elab_Calls + 2);
1166 Set_String (");");
1167 Write_Statement_Buffer;
1168 end Gen_Exception_Table_C;
1170 ------------------
1171 -- Gen_Main_Ada --
1172 ------------------
1174 procedure Gen_Main_Ada is
1175 Target : constant String_Ptr := Target_Name;
1176 VxWorks_Target : constant Boolean :=
1177 Target (Target'Last - 7 .. Target'Last) = "vxworks/"
1178 or else Target (Target'Last - 9 .. Target'Last) = "vxworksae/";
1180 begin
1181 WBI ("");
1182 Set_String (" function ");
1183 Set_String (Get_Main_Name);
1185 if VxWorks_Target then
1186 Set_String (" return Integer is");
1187 Write_Statement_Buffer;
1189 else
1190 Write_Statement_Buffer;
1191 WBI (" (argc : Integer;");
1192 WBI (" argv : System.Address;");
1193 WBI (" envp : System.Address)");
1194 WBI (" return Integer");
1195 WBI (" is");
1196 end if;
1198 -- Initialize and Finalize are not used in No_Run_Time mode
1200 if not No_Run_Time_Specified then
1201 WBI (" procedure initialize;");
1202 WBI (" pragma Import (C, initialize, ""__gnat_initialize"");");
1203 WBI ("");
1204 WBI (" procedure finalize;");
1205 WBI (" pragma Import (C, finalize, ""__gnat_finalize"");");
1206 WBI ("");
1207 end if;
1209 -- Deal with declarations for main program case
1211 if not No_Main_Subprogram then
1213 -- To call the main program, we declare it using a pragma Import
1214 -- Ada with the right link name.
1216 -- It might seem more obvious to "with" the main program, and call
1217 -- it in the normal Ada manner. We do not do this for three reasons:
1219 -- 1. It is more efficient not to recompile the main program
1220 -- 2. We are not entitled to assume the source is accessible
1221 -- 3. We don't know what options to use to compile it
1223 -- It is really reason 3 that is most critical (indeed we used
1224 -- to generate the "with", but several regression tests failed).
1226 WBI ("");
1228 if ALIs.Table (ALIs.First).Main_Program = Func then
1229 WBI (" Result : Integer;");
1230 WBI ("");
1231 WBI (" function Ada_Main_Program return Integer;");
1233 else
1234 WBI (" procedure Ada_Main_Program;");
1235 end if;
1237 Set_String (" pragma Import (Ada, Ada_Main_Program, """);
1238 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1239 Set_Main_Program_Name;
1240 Set_String (""");");
1242 Write_Statement_Buffer;
1243 WBI ("");
1244 end if;
1246 -- Generate a reference to Ada_Main_Program_Name. This symbol is
1247 -- not referenced elsewhere in the generated program, but is needed
1248 -- by the debugger (that's why it is generated in the first place).
1249 -- The reference stops Ada_Main_Program_Name from being optimized
1250 -- away by smart linkers, such as the AiX linker.
1252 if Bind_Main_Program then
1254 (" Ensure_Reference : System.Address := " &
1255 "Ada_Main_Program_Name'Address;");
1256 WBI ("");
1257 end if;
1259 WBI (" begin");
1261 -- On VxWorks, there are no command line arguments
1263 if VxWorks_Target then
1264 WBI (" gnat_argc := 0;");
1265 WBI (" gnat_argv := System.Null_Address;");
1266 WBI (" gnat_envp := System.Null_Address;");
1268 -- Normal case of command line arguments present
1270 else
1271 WBI (" gnat_argc := argc;");
1272 WBI (" gnat_argv := argv;");
1273 WBI (" gnat_envp := envp;");
1274 WBI ("");
1275 end if;
1277 if not No_Run_Time_Specified then
1278 WBI (" Initialize;");
1279 end if;
1281 WBI (" " & Ada_Init_Name.all & ";");
1283 if not No_Main_Subprogram then
1284 WBI (" Break_Start;");
1286 if ALIs.Table (ALIs.First).Main_Program = Proc then
1287 WBI (" Ada_Main_Program;");
1288 else
1289 WBI (" Result := Ada_Main_Program;");
1290 end if;
1291 end if;
1293 -- Adafinal is only called if we have a run time
1295 if not No_Run_Time_Specified then
1297 -- If compiling for the JVM, we directly call Adafinal because
1298 -- we don't import it via Do_Finalize (see Gen_Output_File_Ada).
1300 if Hostparm.Java_VM then
1301 WBI (" System.Standard_Library.Adafinal;");
1302 else
1303 WBI (" Do_Finalize;");
1304 end if;
1305 end if;
1307 -- Finalize is only called if we have a run time
1309 if not No_Run_Time_Specified then
1310 WBI (" Finalize;");
1311 end if;
1313 -- Return result
1315 if No_Main_Subprogram
1316 or else ALIs.Table (ALIs.First).Main_Program = Proc
1317 then
1318 WBI (" return (gnat_exit_status);");
1319 else
1320 WBI (" return (Result);");
1321 end if;
1323 WBI (" end;");
1324 end Gen_Main_Ada;
1326 ----------------
1327 -- Gen_Main_C --
1328 ----------------
1330 procedure Gen_Main_C is
1331 Target : constant String_Ptr := Target_Name;
1332 VxWorks_Target : constant Boolean :=
1333 Target (Target'Last - 7 .. Target'Last) = "vxworks/"
1334 or else Target (Target'Last - 9 .. Target'Last) = "vxworksae/";
1336 begin
1337 Set_String ("int ");
1338 Set_String (Get_Main_Name);
1340 -- On VxWorks, there are no command line arguments
1342 if VxWorks_Target then
1343 Set_String (" ()");
1345 -- Normal case with command line arguments present
1347 else
1348 Set_String (" (argc, argv, envp)");
1349 end if;
1351 Write_Statement_Buffer;
1353 -- VxWorks doesn't have the notion of argc/argv
1355 if VxWorks_Target then
1356 WBI ("{");
1357 WBI (" int result;");
1358 WBI (" gnat_argc = 0;");
1359 WBI (" gnat_argv = 0;");
1360 WBI (" gnat_envp = 0;");
1362 -- Normal case of arguments present
1364 else
1365 WBI (" int argc;");
1366 WBI (" char **argv;");
1367 WBI (" char **envp;");
1368 WBI ("{");
1370 -- Generate a reference to __gnat_ada_main_program_name. This symbol
1371 -- is not referenced elsewhere in the generated program, but is
1372 -- needed by the debugger (that's why it is generated in the first
1373 -- place). The reference stops Ada_Main_Program_Name from being
1374 -- optimized away by smart linkers, such as the AiX linker.
1376 if Bind_Main_Program then
1377 WBI (" char *ensure_reference = __gnat_ada_main_program_name;");
1378 WBI ("");
1379 end if;
1381 if ALIs.Table (ALIs.First).Main_Program = Func then
1382 WBI (" int result;");
1383 end if;
1385 WBI (" gnat_argc = argc;");
1386 WBI (" gnat_argv = argv;");
1387 WBI (" gnat_envp = envp;");
1388 WBI (" ");
1389 end if;
1391 -- The __gnat_initialize routine is used only if we have a run-time
1393 if not No_Run_Time_Specified then
1395 (" __gnat_initialize ();");
1396 end if;
1398 WBI (" " & Ada_Init_Name.all & " ();");
1400 if not No_Main_Subprogram then
1402 WBI (" __gnat_break_start ();");
1403 WBI (" ");
1405 -- Output main program name
1407 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1409 -- Main program is procedure case
1411 if ALIs.Table (ALIs.First).Main_Program = Proc then
1412 Set_String (" ");
1413 Set_Main_Program_Name;
1414 Set_String (" ();");
1415 Write_Statement_Buffer;
1417 -- Main program is function case
1419 else -- ALIs.Table (ALIs_First).Main_Program = Func
1420 Set_String (" result = ");
1421 Set_Main_Program_Name;
1422 Set_String (" ();");
1423 Write_Statement_Buffer;
1424 end if;
1426 end if;
1428 -- Adafinal is called only when we have a run-time
1430 if not No_Run_Time_Specified then
1431 WBI (" ");
1432 WBI (" system__standard_library__adafinal ();");
1433 end if;
1435 -- The finalize routine is used only if we have a run-time
1437 if not No_Run_Time_Specified then
1438 WBI (" __gnat_finalize ();");
1439 end if;
1441 if ALIs.Table (ALIs.First).Main_Program = Func then
1443 if Hostparm.OpenVMS then
1445 -- VMS must use the Posix exit routine in order to get an
1446 -- Unix compatible exit status.
1448 WBI (" __posix_exit (result);");
1450 else
1451 WBI (" exit (result);");
1452 end if;
1454 else
1456 if Hostparm.OpenVMS then
1457 -- VMS must use the Posix exit routine in order to get an
1458 -- Unix compatible exit status.
1459 WBI (" __posix_exit (gnat_exit_status);");
1460 else
1461 WBI (" exit (gnat_exit_status);");
1462 end if;
1463 end if;
1465 WBI ("}");
1466 end Gen_Main_C;
1468 ------------------------------
1469 -- Gen_Object_Files_Options --
1470 ------------------------------
1472 procedure Gen_Object_Files_Options is
1473 Lgnat : Natural;
1474 -- This keeps track of the position in the sorted set of entries
1475 -- in the Linker_Options table of where the first entry from an
1476 -- internal file appears.
1478 procedure Write_Linker_Option;
1479 -- Write binder info linker option.
1481 -------------------------
1482 -- Write_Linker_Option --
1483 -------------------------
1485 procedure Write_Linker_Option is
1486 Start : Natural;
1487 Stop : Natural;
1489 begin
1490 -- Loop through string, breaking at null's
1492 Start := 1;
1493 while Start < Name_Len loop
1495 -- Find null ending this section
1497 Stop := Start + 1;
1498 while Name_Buffer (Stop) /= ASCII.NUL
1499 and then Stop <= Name_Len loop
1500 Stop := Stop + 1;
1501 end loop;
1503 -- Process section if non-null
1505 if Stop > Start then
1506 if Output_Linker_Option_List then
1507 Write_Str (Name_Buffer (Start .. Stop - 1));
1508 Write_Eol;
1509 end if;
1510 Write_Info_Ada_C
1511 (" -- ", "", Name_Buffer (Start .. Stop - 1));
1512 end if;
1514 Start := Stop + 1;
1515 end loop;
1516 end Write_Linker_Option;
1518 -- Start of processing for Gen_Object_Files_Options
1520 begin
1521 WBI ("");
1522 Write_Info_Ada_C ("--", "/*", " BEGIN Object file/option list");
1524 for E in Elab_Order.First .. Elab_Order.Last loop
1526 -- If not spec that has an associated body, then generate a
1527 -- comment giving the name of the corresponding object file.
1529 if Units.Table (Elab_Order.Table (E)).Utype /= Is_Spec then
1530 Get_Name_String
1531 (ALIs.Table
1532 (Units.Table (Elab_Order.Table (E)).My_ALI).Ofile_Full_Name);
1534 -- If the presence of an object file is necessary or if it
1535 -- exists, then use it.
1537 if not Hostparm.Exclude_Missing_Objects
1538 or else
1539 GNAT.OS_Lib.Is_Regular_File (Name_Buffer (1 .. Name_Len))
1540 then
1541 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1542 if Output_Object_List then
1543 Write_Str (Name_Buffer (1 .. Name_Len));
1544 Write_Eol;
1545 end if;
1547 -- Don't link with the shared library on VMS if an internal
1548 -- filename object is seen. Multiply defined symbols will
1549 -- result.
1551 if Hostparm.OpenVMS
1552 and then Is_Internal_File_Name
1553 (ALIs.Table
1554 (Units.Table (Elab_Order.Table (E)).My_ALI).Sfile)
1555 then
1556 Opt.Shared_Libgnat := False;
1557 end if;
1559 end if;
1560 end if;
1561 end loop;
1563 -- Add a "-Ldir" for each directory in the object path. We skip this
1564 -- in No_Run_Time mode, where we want more precise control of exactly
1565 -- what goes into the resulting object file
1567 if not No_Run_Time_Specified then
1568 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1569 declare
1570 Dir : String_Ptr := Dir_In_Obj_Search_Path (J);
1572 begin
1573 Name_Len := 0;
1574 Add_Str_To_Name_Buffer ("-L");
1575 Add_Str_To_Name_Buffer (Dir.all);
1576 Write_Linker_Option;
1577 end;
1578 end loop;
1579 end if;
1581 -- Sort linker options
1583 -- This sort accomplishes two important purposes:
1585 -- a) All application files are sorted to the front, and all
1586 -- GNAT internal files are sorted to the end. This results
1587 -- in a well defined dividing line between the two sets of
1588 -- files, for the purpose of inserting certain standard
1589 -- library references into the linker arguments list.
1591 -- b) Given two different units, we sort the linker options so
1592 -- that those from a unit earlier in the elaboration order
1593 -- comes later in the list. This is a heuristic designed
1594 -- to create a more friendly order of linker options when
1595 -- the operations appear in separate units. The idea is that
1596 -- if unit A must be elaborated before unit B, then it is
1597 -- more likely that B references libraries included by A,
1598 -- than vice versa, so we want the libraries included by
1599 -- A to come after the libraries included by B.
1601 -- These two criteria are implemented by function Lt_Linker_Option.
1602 -- Note that a special case of b) is that specs are elaborated before
1603 -- bodies, so linker options from specs come after linker options
1604 -- for bodies, and again, the assumption is that libraries used by
1605 -- the body are more likely to reference libraries used by the spec,
1606 -- than vice versa.
1608 Sort
1609 (Linker_Options.Last,
1610 Move_Linker_Option'Access,
1611 Lt_Linker_Option'Access);
1613 -- Write user linker options, i.e. the set of linker options that
1614 -- come from all files other than GNAT internal files, Lgnat is
1615 -- left set to point to the first entry from a GNAT internal file,
1616 -- or past the end of the entriers if there are no internal files.
1618 Lgnat := Linker_Options.Last + 1;
1620 for J in 1 .. Linker_Options.Last loop
1621 if not Linker_Options.Table (J).Internal_File then
1622 Get_Name_String (Linker_Options.Table (J).Name);
1623 Write_Linker_Option;
1624 else
1625 Lgnat := J;
1626 exit;
1627 end if;
1628 end loop;
1630 -- Now we insert standard linker options that must appear after the
1631 -- entries from user files, and before the entries from GNAT run-time
1632 -- files. The reason for this decision is that libraries referenced
1633 -- by internal routines may reference these standard library entries.
1635 if not (No_Run_Time_Specified or else Opt.No_Stdlib) then
1636 Name_Len := 0;
1638 if Opt.Shared_Libgnat then
1639 Add_Str_To_Name_Buffer ("-shared");
1640 else
1641 Add_Str_To_Name_Buffer ("-static");
1642 end if;
1644 -- Write directly to avoid -K output (why???)
1646 Write_Info_Ada_C (" -- ", "", Name_Buffer (1 .. Name_Len));
1648 if With_DECGNAT then
1649 Name_Len := 0;
1650 Add_Str_To_Name_Buffer ("-ldecgnat");
1651 Write_Linker_Option;
1652 end if;
1654 if With_GNARL then
1655 Name_Len := 0;
1656 Add_Str_To_Name_Buffer ("-lgnarl");
1657 Write_Linker_Option;
1658 end if;
1660 Name_Len := 0;
1661 Add_Str_To_Name_Buffer ("-lgnat");
1662 Write_Linker_Option;
1663 end if;
1665 -- Write linker options from all internal files
1667 for J in Lgnat .. Linker_Options.Last loop
1668 Get_Name_String (Linker_Options.Table (J).Name);
1669 Write_Linker_Option;
1670 end loop;
1672 if Ada_Bind_File then
1673 WBI ("-- END Object file/option list ");
1674 else
1675 WBI (" END Object file/option list */");
1676 end if;
1678 end Gen_Object_Files_Options;
1680 ---------------------
1681 -- Gen_Output_File --
1682 ---------------------
1684 procedure Gen_Output_File (Filename : String) is
1685 Public_Version : constant Boolean := Gnat_Version_Type = "PUBLIC ";
1686 -- Set true if this is the public version of GNAT
1688 begin
1689 -- Override Ada_Bind_File and Bind_Main_Program for Java since
1690 -- JGNAT only supports Ada code, and the main program is already
1691 -- generated by the compiler.
1693 if Hostparm.Java_VM then
1694 Ada_Bind_File := True;
1695 Bind_Main_Program := False;
1696 end if;
1698 -- Override time slice value if -T switch is set
1700 if Time_Slice_Set then
1701 ALIs.Table (ALIs.First).Time_Slice_Value := Opt.Time_Slice_Value;
1702 end if;
1704 -- Count number of elaboration calls
1706 for E in Elab_Order.First .. Elab_Order.Last loop
1707 if Units.Table (Elab_Order.Table (E)).No_Elab then
1708 null;
1709 else
1710 Num_Elab_Calls := Num_Elab_Calls + 1;
1711 end if;
1712 end loop;
1714 -- Get the time stamp of the former bind for public version warning
1716 if Public_Version then
1717 Record_Time_From_Last_Bind;
1718 end if;
1720 -- Generate output file in appropriate language
1722 if Ada_Bind_File then
1723 Gen_Output_File_Ada (Filename);
1724 else
1725 Gen_Output_File_C (Filename);
1726 end if;
1728 -- Periodically issue a warning when the public version is used on
1729 -- big projects
1731 if Public_Version then
1732 Public_Version_Warning;
1733 end if;
1734 end Gen_Output_File;
1736 -------------------------
1737 -- Gen_Output_File_Ada --
1738 -------------------------
1740 procedure Gen_Output_File_Ada (Filename : String) is
1742 Bfiles : Name_Id;
1743 -- Name of generated bind file (spec)
1745 Bfileb : Name_Id;
1746 -- Name of generated bind file (body)
1748 Ada_Main : constant String := Get_Ada_Main_Name;
1749 -- Name to be used for generated Ada main program. See the body of
1750 -- function Get_Ada_Main_Name for details on the form of the name.
1752 Target : constant String_Ptr := Target_Name;
1753 VxWorks_Target : constant Boolean :=
1754 Target (Target'Last - 7 .. Target'Last) = "vxworks/"
1755 or else Target (Target'Last - 9 .. Target'Last) = "vxworksae/";
1757 begin
1758 -- Create spec first
1760 Create_Binder_Output (Filename, 's', Bfiles);
1762 if No_Run_Time_Specified then
1763 WBI ("pragma No_Run_Time;");
1764 end if;
1766 -- Generate with of System so we can reference System.Address, note
1767 -- that such a reference is safe even in No_Run_Time mode, since we
1768 -- do not need any run-time code for such a reference, and we output
1769 -- a pragma No_Run_Time for this compilation above.
1771 WBI ("with System;");
1773 -- Generate with of System.Initialize_Scalars if active
1775 if Initialize_Scalars_Used then
1776 WBI ("with System.Scalar_Values;");
1777 end if;
1779 Resolve_Binder_Options;
1781 if not No_Run_Time_Specified then
1783 -- Usually, adafinal is called using a pragma Import C. Since
1784 -- Import C doesn't have the same semantics for JGNAT, we use
1785 -- standard Ada.
1787 if Hostparm.Java_VM then
1788 WBI ("with System.Standard_Library;");
1789 end if;
1790 end if;
1792 WBI ("package " & Ada_Main & " is");
1794 -- Main program case
1796 if Bind_Main_Program then
1798 -- Generate argc/argv stuff
1800 WBI ("");
1801 WBI (" gnat_argc : Integer;");
1802 WBI (" gnat_argv : System.Address;");
1803 WBI (" gnat_envp : System.Address;");
1805 -- If we have a run time present, these variables are in the
1806 -- runtime data area for easy access from the runtime
1808 if not No_Run_Time_Specified then
1809 WBI ("");
1810 WBI (" pragma Import (C, gnat_argc);");
1811 WBI (" pragma Import (C, gnat_argv);");
1812 WBI (" pragma Import (C, gnat_envp);");
1813 end if;
1815 -- Define exit status. Again in normal mode, this is in the
1816 -- run-time library, and is initialized there, but in the no
1817 -- run time case, the variable is here and initialized here.
1819 WBI ("");
1821 if No_Run_Time_Specified then
1822 WBI (" gnat_exit_status : Integer := 0;");
1823 else
1824 WBI (" gnat_exit_status : Integer;");
1825 WBI (" pragma Import (C, gnat_exit_status);");
1826 end if;
1827 end if;
1829 -- Generate the GNAT_Version and Ada_Main_Program_Name info only for
1830 -- the main program. Otherwise, it can lead under some circumstances
1831 -- to a symbol duplication during the link (for instance when a
1832 -- C program uses 2 Ada libraries)
1834 if Bind_Main_Program then
1835 WBI ("");
1836 WBI (" GNAT_Version : constant String :=");
1837 WBI (" ""GNAT Version: " &
1838 Gnat_Version_String & """;");
1839 WBI (" pragma Export (C, GNAT_Version, ""__gnat_version"");");
1841 WBI ("");
1842 Set_String (" Ada_Main_Program_Name : constant String := """);
1843 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
1844 Set_Main_Program_Name;
1845 Set_String (""" & Ascii.NUL;");
1846 Write_Statement_Buffer;
1849 (" pragma Export (C, Ada_Main_Program_Name, " &
1850 """__gnat_ada_main_program_name"");");
1851 end if;
1853 -- No need to generate a finalization routine if there is no
1854 -- runtime, since there is nothing to do in this case.
1856 if not No_Run_Time_Specified then
1857 WBI ("");
1858 WBI (" procedure " & Ada_Final_Name.all & ";");
1859 WBI (" pragma Export (C, " & Ada_Final_Name.all & ", """ &
1860 Ada_Final_Name.all & """);");
1861 end if;
1863 WBI ("");
1864 WBI (" procedure " & Ada_Init_Name.all & ";");
1865 WBI (" pragma Export (C, " & Ada_Init_Name.all & ", """ &
1866 Ada_Init_Name.all & """);");
1868 if Bind_Main_Program then
1870 -- If we have a run time, then Break_Start is defined there, but
1871 -- if there is no run-time, Break_Start is defined in this file.
1873 WBI ("");
1874 WBI (" procedure Break_Start;");
1876 if No_Run_Time_Specified then
1877 WBI (" pragma Export (C, Break_Start, ""__gnat_break_start"");");
1878 else
1879 WBI (" pragma Import (C, Break_Start, ""__gnat_break_start"");");
1880 end if;
1882 WBI ("");
1883 WBI (" function " & Get_Main_Name);
1885 -- Generate argument list (except on VxWorks, where none is present)
1887 if not VxWorks_Target then
1888 WBI (" (argc : Integer;");
1889 WBI (" argv : System.Address;");
1890 WBI (" envp : System.Address)");
1891 end if;
1893 WBI (" return Integer;");
1894 WBI (" pragma Export (C, " & Get_Main_Name & ", """ &
1895 Get_Main_Name & """);");
1896 end if;
1898 if Initialize_Scalars_Used then
1899 Gen_Scalar_Values;
1900 end if;
1902 Gen_Versions_Ada;
1903 Gen_Elab_Order_Ada;
1905 -- Spec is complete
1907 WBI ("");
1908 WBI ("end " & Ada_Main & ";");
1909 Close_Binder_Output;
1911 -- Prepare to write body
1913 Create_Binder_Output (Filename, 'b', Bfileb);
1915 -- Output Source_File_Name pragmas which look like
1917 -- pragma Source_File_Name (Ada_Main, Spec_File_Name => "sss");
1918 -- pragma Source_File_Name (Ada_Main, Body_File_Name => "bbb");
1920 -- where sss/bbb are the spec/body file names respectively
1922 Get_Name_String (Bfiles);
1923 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
1925 WBI ("pragma Source_File_Name (" &
1926 Ada_Main &
1927 ", Spec_File_Name => """ &
1928 Name_Buffer (1 .. Name_Len + 3));
1930 Get_Name_String (Bfileb);
1931 Name_Buffer (Name_Len + 1 .. Name_Len + 3) := """);";
1933 WBI ("pragma Source_File_Name (" &
1934 Ada_Main &
1935 ", Body_File_Name => """ &
1936 Name_Buffer (1 .. Name_Len + 3));
1938 WBI ("");
1939 WBI ("package body " & Ada_Main & " is");
1941 -- Import the finalization procedure only if there is a runtime.
1943 if not No_Run_Time_Specified then
1945 -- In the Java case, pragma Import C cannot be used, so the
1946 -- standard Ada constructs will be used instead.
1948 if not Hostparm.Java_VM then
1949 WBI ("");
1950 WBI (" procedure Do_Finalize;");
1952 (" pragma Import (C, Do_Finalize, " &
1953 """system__standard_library__adafinal"");");
1954 WBI ("");
1955 end if;
1956 end if;
1958 Gen_Adainit_Ada;
1960 -- No need to generate a finalization routine if there is no
1961 -- runtime, since there is nothing to do in this case.
1963 if not No_Run_Time_Specified then
1964 Gen_Adafinal_Ada;
1965 end if;
1967 if Bind_Main_Program then
1969 -- In No_Run_Time mode, generate dummy body for Break_Start
1971 if No_Run_Time_Specified then
1972 WBI ("");
1973 WBI (" procedure Break_Start is");
1974 WBI (" begin");
1975 WBI (" null;");
1976 WBI (" end;");
1977 end if;
1979 Gen_Main_Ada;
1980 end if;
1982 -- Output object file list and the Ada body is complete
1984 Gen_Object_Files_Options;
1986 WBI ("");
1987 WBI ("end " & Ada_Main & ";");
1989 Close_Binder_Output;
1990 end Gen_Output_File_Ada;
1992 -----------------------
1993 -- Gen_Output_File_C --
1994 -----------------------
1996 procedure Gen_Output_File_C (Filename : String) is
1998 Bfile : Name_Id;
1999 -- Name of generated bind file
2001 begin
2002 Create_Binder_Output (Filename, 'c', Bfile);
2004 Resolve_Binder_Options;
2006 WBI ("#ifdef __STDC__");
2007 WBI ("#define PARAMS(paramlist) paramlist");
2008 WBI ("#else");
2009 WBI ("#define PARAMS(paramlist) ()");
2010 WBI ("#endif");
2011 WBI ("");
2013 WBI ("extern void __gnat_set_globals ");
2014 WBI (" PARAMS ((int, int, int, int, int, int, const char *,");
2015 WBI (" int, int, int));");
2016 WBI ("extern void " & Ada_Final_Name.all & " PARAMS ((void));");
2017 WBI ("extern void " & Ada_Init_Name.all & " PARAMS ((void));");
2019 WBI ("extern void system__standard_library__adafinal PARAMS ((void));");
2021 if not No_Main_Subprogram then
2022 WBI ("extern int main PARAMS ((int, char **, char **));");
2023 if Hostparm.OpenVMS then
2024 WBI ("extern void __posix_exit PARAMS ((int));");
2025 else
2026 WBI ("extern void exit PARAMS ((int));");
2027 end if;
2029 WBI ("extern void __gnat_break_start PARAMS ((void));");
2030 Set_String ("extern ");
2032 if ALIs.Table (ALIs.First).Main_Program = Proc then
2033 Set_String ("void ");
2034 else
2035 Set_String ("int ");
2036 end if;
2038 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2039 Set_Main_Program_Name;
2040 Set_String (" PARAMS ((void));");
2041 Write_Statement_Buffer;
2042 end if;
2044 if not No_Run_Time_Specified then
2045 WBI ("extern void __gnat_initialize PARAMS ((void));");
2046 WBI ("extern void __gnat_finalize PARAMS ((void));");
2047 WBI ("extern void __gnat_install_handler PARAMS ((void));");
2048 end if;
2050 WBI ("");
2052 Gen_Elab_Defs_C;
2054 -- Imported variable used to track elaboration/finalization phase.
2055 -- Used only when we have a runtime.
2057 if not No_Run_Time_Specified then
2058 WBI ("extern int __gnat_handler_installed;");
2059 WBI ("");
2060 end if;
2062 -- Write argv/argc stuff if main program case
2064 if Bind_Main_Program then
2066 -- In the normal case, these are in the runtime library
2068 if not No_Run_Time_Specified then
2069 WBI ("extern int gnat_argc;");
2070 WBI ("extern char **gnat_argv;");
2071 WBI ("extern char **gnat_envp;");
2072 WBI ("extern int gnat_exit_status;");
2074 -- In the No_Run_Time case, they are right in the binder file
2075 -- and we initialize gnat_exit_status in the declaration.
2077 else
2078 WBI ("int gnat_argc;");
2079 WBI ("char **gnat_argv;");
2080 WBI ("char **gnat_envp;");
2081 WBI ("int gnat_exit_status = 0;");
2082 end if;
2084 WBI ("");
2085 end if;
2087 -- In no run-time mode, the __gnat_break_start routine (for the
2088 -- debugger to get initial control) is defined in this file.
2090 if No_Run_Time_Specified then
2091 WBI ("");
2092 WBI ("void __gnat_break_start () {}");
2093 end if;
2095 -- Generate the __gnat_version and __gnat_ada_main_program_name info
2096 -- only for the main program. Otherwise, it can lead under some
2097 -- circumstances to a symbol duplication during the link (for instance
2098 -- when a C program uses 2 Ada libraries)
2100 if Bind_Main_Program then
2101 WBI ("");
2102 WBI ("char __gnat_version[] = ""GNAT Version: " &
2103 Gnat_Version_String & """;");
2105 Set_String ("char __gnat_ada_main_program_name[] = """);
2106 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2107 Set_Main_Program_Name;
2108 Set_String (""";");
2109 Write_Statement_Buffer;
2110 end if;
2112 -- Generate the adafinal routine. In no runtime mode, this is
2113 -- not needed, since there is no finalization to do.
2115 if not No_Run_Time_Specified then
2116 Gen_Adafinal_C;
2117 end if;
2119 Gen_Adainit_C;
2121 -- Main is only present for Ada main case
2123 if Bind_Main_Program then
2124 Gen_Main_C;
2125 end if;
2127 -- Scalar values, versions and object files needed in both cases
2129 if Initialize_Scalars_Used then
2130 Gen_Scalar_Values;
2131 end if;
2133 Gen_Versions_C;
2134 Gen_Elab_Order_C;
2135 Gen_Object_Files_Options;
2137 -- C binder output is complete
2139 Close_Binder_Output;
2140 end Gen_Output_File_C;
2142 -----------------------
2143 -- Gen_Scalar_Values --
2144 -----------------------
2146 procedure Gen_Scalar_Values is
2148 -- Strings to hold hex values of initialization constants. Note that
2149 -- we store these strings in big endian order, but they are actually
2150 -- used to initialize integer values, so the actual generated data
2151 -- will automaticaly have the right endianess.
2153 IS_Is1 : String (1 .. 2);
2154 IS_Is2 : String (1 .. 4);
2155 IS_Is4 : String (1 .. 8);
2156 IS_Is8 : String (1 .. 16);
2157 IS_Iu1 : String (1 .. 2);
2158 IS_Iu2 : String (1 .. 4);
2159 IS_Iu4 : String (1 .. 8);
2160 IS_Iu8 : String (1 .. 16);
2161 IS_Isf : String (1 .. 8);
2162 IS_Ifl : String (1 .. 8);
2163 IS_Ilf : String (1 .. 16);
2165 -- The string for Long_Long_Float is special. This is used only on the
2166 -- ia32 with 80-bit extended float (stored in 96 bits by gcc). The
2167 -- value here is represented little-endian, since that's the only way
2168 -- it is ever generated (this is not used on big-endian machines.
2170 IS_Ill : String (1 .. 24);
2172 begin
2173 -- -Sin (invalid values)
2175 if Opt.Initialize_Scalars_Mode = 'I' then
2176 IS_Is1 := "80";
2177 IS_Is2 := "8000";
2178 IS_Is4 := "80000000";
2179 IS_Is8 := "8000000000000000";
2180 IS_Iu1 := "FF";
2181 IS_Iu2 := "FFFF";
2182 IS_Iu4 := "FFFFFFFF";
2183 IS_Iu8 := "FFFFFFFFFFFFFFFF";
2184 IS_Isf := IS_Iu4;
2185 IS_Ifl := IS_Iu4;
2186 IS_Ilf := IS_Iu8;
2187 IS_Ill := "00000000000000C0FFFF0000";
2189 -- -Slo (low values)
2191 elsif Opt.Initialize_Scalars_Mode = 'L' then
2192 IS_Is1 := "80";
2193 IS_Is2 := "8000";
2194 IS_Is4 := "80000000";
2195 IS_Is8 := "8000000000000000";
2196 IS_Iu1 := "00";
2197 IS_Iu2 := "0000";
2198 IS_Iu4 := "00000000";
2199 IS_Iu8 := "0000000000000000";
2200 IS_Isf := "FF800000";
2201 IS_Ifl := IS_Isf;
2202 IS_Ilf := "FFF0000000000000";
2203 IS_Ill := "0000000000000080FFFF0000";
2205 -- -Shi (high values)
2207 elsif Opt.Initialize_Scalars_Mode = 'H' then
2208 IS_Is1 := "7F";
2209 IS_Is2 := "7FFF";
2210 IS_Is4 := "7FFFFFFF";
2211 IS_Is8 := "7FFFFFFFFFFFFFFF";
2212 IS_Iu1 := "FF";
2213 IS_Iu2 := "FFFF";
2214 IS_Iu4 := "FFFFFFFF";
2215 IS_Iu8 := "FFFFFFFFFFFFFFFF";
2216 IS_Isf := "7F800000";
2217 IS_Ifl := IS_Isf;
2218 IS_Ilf := "7FF0000000000000";
2219 IS_Ill := "0000000000000080FF7F0000";
2221 -- -Shh (hex byte)
2223 else pragma Assert (Opt.Initialize_Scalars_Mode = 'X');
2224 IS_Is1 (1 .. 2) := Opt.Initialize_Scalars_Val;
2225 IS_Is2 (1 .. 2) := Opt.Initialize_Scalars_Val;
2226 IS_Is2 (3 .. 4) := Opt.Initialize_Scalars_Val;
2228 for J in 1 .. 4 loop
2229 IS_Is4 (2 * J - 1 .. 2 * J) := Opt.Initialize_Scalars_Val;
2230 end loop;
2232 for J in 1 .. 8 loop
2233 IS_Is8 (2 * J - 1 .. 2 * J) := Opt.Initialize_Scalars_Val;
2234 end loop;
2236 IS_Iu1 := IS_Is1;
2237 IS_Iu2 := IS_Is2;
2238 IS_Iu4 := IS_Is4;
2239 IS_Iu8 := IS_Is8;
2241 IS_Isf := IS_Is4;
2242 IS_Ifl := IS_Is4;
2243 IS_Ilf := IS_Is8;
2245 for J in 1 .. 12 loop
2246 IS_Ill (2 * J - 1 .. 2 * J) := Opt.Initialize_Scalars_Val;
2247 end loop;
2248 end if;
2250 -- Generate output, Ada case
2252 if Ada_Bind_File then
2253 WBI ("");
2255 Set_String (" IS_Is1 : constant System.Scalar_Values.Byte1 := 16#");
2256 Set_String (IS_Is1);
2257 Write_Statement_Buffer ("#;");
2259 Set_String (" IS_Is2 : constant System.Scalar_Values.Byte2 := 16#");
2260 Set_String (IS_Is2);
2261 Write_Statement_Buffer ("#;");
2263 Set_String (" IS_Is4 : constant System.Scalar_Values.Byte4 := 16#");
2264 Set_String (IS_Is4);
2265 Write_Statement_Buffer ("#;");
2267 Set_String (" IS_Is8 : constant System.Scalar_Values.Byte8 := 16#");
2268 Set_String (IS_Is8);
2269 Write_Statement_Buffer ("#;");
2271 Set_String (" IS_Iu1 : constant System.Scalar_Values.Byte1 := 16#");
2272 Set_String (IS_Iu1);
2273 Write_Statement_Buffer ("#;");
2275 Set_String (" IS_Iu2 : constant System.Scalar_Values.Byte2 := 16#");
2276 Set_String (IS_Iu2);
2277 Write_Statement_Buffer ("#;");
2279 Set_String (" IS_Iu4 : constant System.Scalar_Values.Byte4 := 16#");
2280 Set_String (IS_Iu4);
2281 Write_Statement_Buffer ("#;");
2283 Set_String (" IS_Iu8 : constant System.Scalar_Values.Byte8 := 16#");
2284 Set_String (IS_Iu8);
2285 Write_Statement_Buffer ("#;");
2287 Set_String (" IS_Isf : constant System.Scalar_Values.Byte4 := 16#");
2288 Set_String (IS_Isf);
2289 Write_Statement_Buffer ("#;");
2291 Set_String (" IS_Ifl : constant System.Scalar_Values.Byte4 := 16#");
2292 Set_String (IS_Ifl);
2293 Write_Statement_Buffer ("#;");
2295 Set_String (" IS_Ilf : constant System.Scalar_Values.Byte8 := 16#");
2296 Set_String (IS_Ilf);
2297 Write_Statement_Buffer ("#;");
2299 -- Special case of Long_Long_Float. This is a 10-byte value used
2300 -- only on the x86. We could omit it for other architectures, but
2301 -- we don't easily have that kind of target specialization in the
2302 -- binder, and it's only 10 bytes, and only if -Sxx is used. Note
2303 -- that for architectures where Long_Long_Float is the same as
2304 -- Long_Float, the expander uses the Long_Float constant for the
2305 -- initializations of Long_Long_Float values.
2307 WBI (" IS_Ill : constant array (1 .. 12) of");
2308 WBI (" System.Scalar_Values.Byte1 := (");
2309 Set_String (" ");
2311 for J in 1 .. 6 loop
2312 Set_String (" 16#");
2313 Set_Char (IS_Ill (2 * J - 1));
2314 Set_Char (IS_Ill (2 * J));
2315 Set_String ("#,");
2316 end loop;
2318 Write_Statement_Buffer;
2319 Set_String (" ");
2321 for J in 7 .. 12 loop
2322 Set_String (" 16#");
2323 Set_Char (IS_Ill (2 * J - 1));
2324 Set_Char (IS_Ill (2 * J));
2326 if J = 12 then
2327 Set_String ("#);");
2328 else
2329 Set_String ("#,");
2330 end if;
2331 end loop;
2333 Write_Statement_Buffer;
2335 -- Output export statements to export to System.Scalar_Values
2337 WBI ("");
2339 WBI (" pragma Export (Ada, IS_Is1, ""__gnat_Is1"");");
2340 WBI (" pragma Export (Ada, IS_Is2, ""__gnat_Is2"");");
2341 WBI (" pragma Export (Ada, IS_Is4, ""__gnat_Is4"");");
2342 WBI (" pragma Export (Ada, IS_Is8, ""__gnat_Is8"");");
2343 WBI (" pragma Export (Ada, IS_Iu1, ""__gnat_Iu1"");");
2344 WBI (" pragma Export (Ada, IS_Iu2, ""__gnat_Iu2"");");
2345 WBI (" pragma Export (Ada, IS_Iu4, ""__gnat_Iu4"");");
2346 WBI (" pragma Export (Ada, IS_Iu8, ""__gnat_Iu8"");");
2347 WBI (" pragma Export (Ada, IS_Isf, ""__gnat_Isf"");");
2348 WBI (" pragma Export (Ada, IS_Ifl, ""__gnat_Ifl"");");
2349 WBI (" pragma Export (Ada, IS_Ilf, ""__gnat_Ilf"");");
2350 WBI (" pragma Export (Ada, IS_Ill, ""__gnat_Ill"");");
2352 -- Generate output C case
2354 else
2355 -- The lines we generate in this case are of the form
2356 -- typ __gnat_I?? = 0x??;
2357 -- where typ is appropriate to the length
2359 WBI ("");
2361 Set_String ("unsigned char __gnat_Is1 = 0x");
2362 Set_String (IS_Is1);
2363 Write_Statement_Buffer (";");
2365 Set_String ("unsigned short __gnat_Is2 = 0x");
2366 Set_String (IS_Is2);
2367 Write_Statement_Buffer (";");
2369 Set_String ("unsigned __gnat_Is4 = 0x");
2370 Set_String (IS_Is4);
2371 Write_Statement_Buffer (";");
2373 Set_String ("long long unsigned __gnat_Is8 = 0x");
2374 Set_String (IS_Is8);
2375 Write_Statement_Buffer ("LL;");
2377 Set_String ("unsigned char __gnat_Iu1 = 0x");
2378 Set_String (IS_Is1);
2379 Write_Statement_Buffer (";");
2381 Set_String ("unsigned short __gnat_Iu2 = 0x");
2382 Set_String (IS_Is2);
2383 Write_Statement_Buffer (";");
2385 Set_String ("unsigned __gnat_Iu4 = 0x");
2386 Set_String (IS_Is4);
2387 Write_Statement_Buffer (";");
2389 Set_String ("long long unsigned __gnat_Iu8 = 0x");
2390 Set_String (IS_Is8);
2391 Write_Statement_Buffer ("LL;");
2393 Set_String ("unsigned __gnat_Isf = 0x");
2394 Set_String (IS_Isf);
2395 Write_Statement_Buffer (";");
2397 Set_String ("unsigned __gnat_Ifl = 0x");
2398 Set_String (IS_Ifl);
2399 Write_Statement_Buffer (";");
2401 Set_String ("long long unsigned __gnat_Ilf = 0x");
2402 Set_String (IS_Ilf);
2403 Write_Statement_Buffer ("LL;");
2405 -- For Long_Long_Float, we generate
2406 -- char __gnat_Ill[12] = {0x??, 0x??, 0x??, 0x??, 0x??, 0x??,
2407 -- 0x??, 0x??, 0x??, 0x??, 0x??, 0x??);
2409 Set_String ("unsigned char __gnat_Ill[12] = {");
2411 for J in 1 .. 6 loop
2412 Set_String ("0x");
2413 Set_Char (IS_Ill (2 * J - 1));
2414 Set_Char (IS_Ill (2 * J));
2415 Set_String (", ");
2416 end loop;
2418 Write_Statement_Buffer;
2419 Set_String (" ");
2421 for J in 7 .. 12 loop
2422 Set_String ("0x");
2423 Set_Char (IS_Ill (2 * J - 1));
2424 Set_Char (IS_Ill (2 * J));
2426 if J = 12 then
2427 Set_String ("};");
2428 else
2429 Set_String (", ");
2430 end if;
2431 end loop;
2433 Write_Statement_Buffer;
2434 end if;
2435 end Gen_Scalar_Values;
2437 ----------------------
2438 -- Gen_Versions_Ada --
2439 ----------------------
2441 -- This routine generates two sets of lines. The first set has the form:
2443 -- unnnnn : constant Integer := 16#hhhhhhhh#;
2445 -- The second set has the form
2447 -- pragma Export (C, unnnnn, unam);
2449 -- for each unit, where unam is the unit name suffixed by either B or
2450 -- S for body or spec, with dots replaced by double underscores, and
2451 -- hhhhhhhh is the version number, and nnnnn is a 5-digits serial number.
2453 procedure Gen_Versions_Ada is
2454 Ubuf : String (1 .. 6) := "u00000";
2456 procedure Increment_Ubuf;
2457 -- Little procedure to increment the serial number
2459 procedure Increment_Ubuf is
2460 begin
2461 for J in reverse Ubuf'Range loop
2462 Ubuf (J) := Character'Succ (Ubuf (J));
2463 exit when Ubuf (J) <= '9';
2464 Ubuf (J) := '0';
2465 end loop;
2466 end Increment_Ubuf;
2468 -- Start of processing for Gen_Versions_Ada
2470 begin
2471 if Bind_For_Library then
2473 -- When building libraries, the version number of each unit can
2474 -- not be computed, since the binder does not know the full list
2475 -- of units. Therefore, the 'Version and 'Body_Version
2476 -- attributes can not supported in this case.
2478 return;
2479 end if;
2481 WBI ("");
2483 WBI (" type Version_32 is mod 2 ** 32;");
2484 for U in Units.First .. Units.Last loop
2485 Increment_Ubuf;
2486 WBI (" " & Ubuf & " : constant Version_32 := 16#" &
2487 Units.Table (U).Version & "#;");
2488 end loop;
2490 WBI ("");
2491 Ubuf := "u00000";
2493 for U in Units.First .. Units.Last loop
2494 Increment_Ubuf;
2495 Set_String (" pragma Export (C, ");
2496 Set_String (Ubuf);
2497 Set_String (", """);
2499 Get_Name_String (Units.Table (U).Uname);
2501 for K in 1 .. Name_Len loop
2502 if Name_Buffer (K) = '.' then
2503 Set_Char ('_');
2504 Set_Char ('_');
2506 elsif Name_Buffer (K) = '%' then
2507 exit;
2509 else
2510 Set_Char (Name_Buffer (K));
2511 end if;
2512 end loop;
2514 if Name_Buffer (Name_Len) = 's' then
2515 Set_Char ('S');
2516 else
2517 Set_Char ('B');
2518 end if;
2520 Set_String (""");");
2521 Write_Statement_Buffer;
2522 end loop;
2524 end Gen_Versions_Ada;
2526 --------------------
2527 -- Gen_Versions_C --
2528 --------------------
2530 -- This routine generates a line of the form:
2532 -- unsigned unam = 0xhhhhhhhh;
2534 -- for each unit, where unam is the unit name suffixed by either B or
2535 -- S for body or spec, with dots replaced by double underscores.
2537 procedure Gen_Versions_C is
2538 begin
2539 if Bind_For_Library then
2541 -- When building libraries, the version number of each unit can
2542 -- not be computed, since the binder does not know the full list
2543 -- of units. Therefore, the 'Version and 'Body_Version
2544 -- attributes can not supported.
2546 return;
2547 end if;
2549 for U in Units.First .. Units.Last loop
2550 Set_String ("unsigned ");
2552 Get_Name_String (Units.Table (U).Uname);
2554 for K in 1 .. Name_Len loop
2555 if Name_Buffer (K) = '.' then
2556 Set_String ("__");
2558 elsif Name_Buffer (K) = '%' then
2559 exit;
2561 else
2562 Set_Char (Name_Buffer (K));
2563 end if;
2564 end loop;
2566 if Name_Buffer (Name_Len) = 's' then
2567 Set_Char ('S');
2568 else
2569 Set_Char ('B');
2570 end if;
2572 Set_String (" = 0x");
2573 Set_String (Units.Table (U).Version);
2574 Set_Char (';');
2575 Write_Statement_Buffer;
2576 end loop;
2578 end Gen_Versions_C;
2580 -----------------------
2581 -- Get_Ada_Main_Name --
2582 -----------------------
2584 function Get_Ada_Main_Name return String is
2585 Suffix : constant String := "_00";
2586 Name : String (1 .. Opt.Ada_Main_Name.all'Length + Suffix'Length) :=
2587 Opt.Ada_Main_Name.all & Suffix;
2588 Nlen : Natural;
2590 begin
2591 -- The main program generated by JGNAT expects a package called
2592 -- ada_<main procedure>.
2594 if Hostparm.Java_VM then
2595 -- Get main program name
2597 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2599 -- Remove the %b
2601 return "ada_" & Name_Buffer (1 .. Name_Len - 2);
2602 end if;
2604 -- This loop tries the following possibilities in order
2605 -- <Ada_Main>
2606 -- <Ada_Main>_01
2607 -- <Ada_Main>_02
2608 -- ..
2609 -- <Ada_Main>_99
2610 -- where <Ada_Main> is equal to Opt.Ada_Main_Name. By default,
2611 -- it is set to 'ada_main'.
2613 for J in 0 .. 99 loop
2614 if J = 0 then
2615 Nlen := Name'Length - Suffix'Length;
2616 else
2617 Nlen := Name'Length;
2618 Name (Name'Last) := Character'Val (J mod 10 + Character'Pos ('0'));
2619 Name (Name'Last - 1) :=
2620 Character'Val (J / 10 + Character'Pos ('0'));
2621 end if;
2623 for K in ALIs.First .. ALIs.Last loop
2624 for L in ALIs.Table (K).First_Unit .. ALIs.Table (K).Last_Unit loop
2626 -- Get unit name, removing %b or %e at end
2628 Get_Name_String (Units.Table (L).Uname);
2629 Name_Len := Name_Len - 2;
2631 if Name_Buffer (1 .. Name_Len) = Name (1 .. Nlen) then
2632 goto Continue;
2633 end if;
2634 end loop;
2635 end loop;
2637 return Name (1 .. Nlen);
2639 <<Continue>>
2640 null;
2641 end loop;
2643 -- If we fall through, just use a peculiar unlikely name
2645 return ("Qwertyuiop");
2646 end Get_Ada_Main_Name;
2648 -------------------
2649 -- Get_Main_Name --
2650 -------------------
2652 function Get_Main_Name return String is
2653 Target : constant String_Ptr := Target_Name;
2654 VxWorks_Target : constant Boolean :=
2655 Target (Target'Last - 7 .. Target'Last) = "vxworks/"
2656 or else Target (Target'Last - 9 .. Target'Last) = "vxworksae/";
2658 begin
2659 -- Explicit name given with -M switch
2661 if Bind_Alternate_Main_Name then
2662 return Alternate_Main_Name.all;
2664 -- Case of main program name to be used directly
2666 elsif VxWorks_Target then
2668 -- Get main program name
2670 Get_Name_String (Units.Table (First_Unit_Entry).Uname);
2672 -- If this is a child name, return only the name of the child,
2673 -- since we can't have dots in a nested program name. Note that
2674 -- we do not include the %b at the end of the unit name.
2676 for J in reverse 1 .. Name_Len - 2 loop
2677 if J = 1 or else Name_Buffer (J - 1) = '.' then
2678 return Name_Buffer (J .. Name_Len - 2);
2679 end if;
2680 end loop;
2682 raise Program_Error; -- impossible exit
2684 -- Case where "main" is to be used as default
2686 else
2687 return "main";
2688 end if;
2689 end Get_Main_Name;
2691 ----------------------
2692 -- Lt_Linker_Option --
2693 ----------------------
2695 function Lt_Linker_Option (Op1, Op2 : Natural) return Boolean is
2696 begin
2697 -- Sort internal files last
2699 if Linker_Options.Table (Op1).Internal_File
2701 Linker_Options.Table (Op2).Internal_File
2702 then
2703 -- Note: following test uses False < True
2705 return Linker_Options.Table (Op1).Internal_File
2707 Linker_Options.Table (Op2).Internal_File;
2709 -- If both internal or both non-internal, sort according to the
2710 -- elaboration position. A unit that is elaborated later should
2711 -- come earlier in the linker options list.
2713 else
2714 return Units.Table (Linker_Options.Table (Op1).Unit).Elab_Position
2716 Units.Table (Linker_Options.Table (Op2).Unit).Elab_Position;
2718 end if;
2719 end Lt_Linker_Option;
2721 ------------------------
2722 -- Move_Linker_Option --
2723 ------------------------
2725 procedure Move_Linker_Option (From : Natural; To : Natural) is
2726 begin
2727 Linker_Options.Table (To) := Linker_Options.Table (From);
2728 end Move_Linker_Option;
2730 ----------------------------
2731 -- Public_Version_Warning --
2732 ----------------------------
2734 procedure Public_Version_Warning is
2736 Time : Int := Time_From_Last_Bind;
2738 -- Constants to help defining periods
2740 Hour : constant := 60;
2741 Day : constant := 24 * Hour;
2743 Never : constant := Integer'Last;
2744 -- Special value indicating no warnings should be given
2746 -- Constants defining when the warning is issued. Programs with more
2747 -- than Large Units will issue a warning every Period_Large amount of
2748 -- time. Smaller programs will generate a warning every Period_Small
2749 -- amount of time.
2751 Large : constant := 20;
2752 -- Threshold for considering a program small or large
2754 Period_Large : constant := Day;
2755 -- Periodic warning time for large programs
2757 Period_Small : constant := Never;
2758 -- Periodic warning time for small programs
2760 Nb_Unit : Int;
2762 begin
2763 -- Compute the number of units that are not GNAT internal files
2765 Nb_Unit := 0;
2766 for A in ALIs.First .. ALIs.Last loop
2767 if not Is_Internal_File_Name (ALIs.Table (A).Sfile) then
2768 Nb_Unit := Nb_Unit + 1;
2769 end if;
2770 end loop;
2772 -- Do not emit the message if the last message was emitted in the
2773 -- specified period taking into account the number of units.
2775 if Nb_Unit < Large and then Time <= Period_Small then
2776 return;
2778 elsif Time <= Period_Large then
2779 return;
2780 end if;
2782 Write_Eol;
2783 Write_Str ("IMPORTANT NOTICE:");
2784 Write_Eol;
2785 Write_Str (" This version of GNAT is unsupported"
2786 & " and comes with absolutely no warranty.");
2787 Write_Eol;
2788 Write_Str (" If you intend to evaluate or use GNAT for building "
2789 & "commercial applications,");
2790 Write_Eol;
2791 Write_Str (" please consult http://www.gnat.com/ for information");
2792 Write_Eol;
2793 Write_Str (" on the GNAT Professional product line.");
2794 Write_Eol;
2795 Write_Eol;
2796 end Public_Version_Warning;
2798 ----------------------------
2799 -- Resolve_Binder_Options --
2800 ----------------------------
2802 procedure Resolve_Binder_Options is
2803 begin
2804 for E in Elab_Order.First .. Elab_Order.Last loop
2805 Get_Name_String (Units.Table (Elab_Order.Table (E)).Uname);
2807 -- The procedure of looking for specific packages and setting
2808 -- flags is very wrong, but there isn't a good alternative at
2809 -- this time.
2811 if Name_Buffer (1 .. 19) = "system.os_interface" then
2812 With_GNARL := True;
2813 end if;
2815 if Hostparm.OpenVMS and then Name_Buffer (1 .. 3) = "dec" then
2816 With_DECGNAT := True;
2817 end if;
2818 end loop;
2819 end Resolve_Binder_Options;
2821 --------------
2822 -- Set_Char --
2823 --------------
2825 procedure Set_Char (C : Character) is
2826 begin
2827 Last := Last + 1;
2828 Statement_Buffer (Last) := C;
2829 end Set_Char;
2831 -------------
2832 -- Set_Int --
2833 -------------
2835 procedure Set_Int (N : Int) is
2836 begin
2837 if N < 0 then
2838 Set_String ("-");
2839 Set_Int (-N);
2841 else
2842 if N > 9 then
2843 Set_Int (N / 10);
2844 end if;
2846 Last := Last + 1;
2847 Statement_Buffer (Last) :=
2848 Character'Val (N mod 10 + Character'Pos ('0'));
2849 end if;
2850 end Set_Int;
2852 ---------------------------
2853 -- Set_Main_Program_Name --
2854 ---------------------------
2856 procedure Set_Main_Program_Name is
2857 begin
2858 -- Note that name has %b on the end which we ignore
2860 -- First we output the initial _ada_ since we know that the main
2861 -- program is a library level subprogram.
2863 Set_String ("_ada_");
2865 -- Copy name, changing dots to double underscores
2867 for J in 1 .. Name_Len - 2 loop
2868 if Name_Buffer (J) = '.' then
2869 Set_String ("__");
2870 else
2871 Set_Char (Name_Buffer (J));
2872 end if;
2873 end loop;
2874 end Set_Main_Program_Name;
2876 ---------------------
2877 -- Set_Name_Buffer --
2878 ---------------------
2880 procedure Set_Name_Buffer is
2881 begin
2882 for J in 1 .. Name_Len loop
2883 Set_Char (Name_Buffer (J));
2884 end loop;
2885 end Set_Name_Buffer;
2887 ----------------
2888 -- Set_String --
2889 ----------------
2891 procedure Set_String (S : String) is
2892 begin
2893 Statement_Buffer (Last + 1 .. Last + S'Length) := S;
2894 Last := Last + S'Length;
2895 end Set_String;
2897 -------------------
2898 -- Set_Unit_Name --
2899 -------------------
2901 procedure Set_Unit_Name is
2902 begin
2903 for J in 1 .. Name_Len - 2 loop
2904 if Name_Buffer (J) /= '.' then
2905 Set_Char (Name_Buffer (J));
2906 else
2907 Set_String ("__");
2908 end if;
2909 end loop;
2910 end Set_Unit_Name;
2912 ---------------------
2913 -- Set_Unit_Number --
2914 ---------------------
2916 procedure Set_Unit_Number (U : Unit_Id) is
2917 Num_Units : constant Nat := Nat (Units.Table'Last) - Nat (Unit_Id'First);
2918 Unum : constant Nat := Nat (U) - Nat (Unit_Id'First);
2920 begin
2921 if Num_Units >= 10 and then Unum < 10 then
2922 Set_Char ('0');
2923 end if;
2925 if Num_Units >= 100 and then Unum < 100 then
2926 Set_Char ('0');
2927 end if;
2929 Set_Int (Unum);
2930 end Set_Unit_Number;
2932 ------------
2933 -- Tab_To --
2934 ------------
2936 procedure Tab_To (N : Natural) is
2937 begin
2938 while Last < N loop
2939 Set_Char (' ');
2940 end loop;
2941 end Tab_To;
2943 ----------------------
2944 -- Write_Info_Ada_C --
2945 ----------------------
2947 procedure Write_Info_Ada_C (Ada : String; C : String; Common : String) is
2948 begin
2949 if Ada_Bind_File then
2950 declare
2951 S : String (1 .. Ada'Length + Common'Length);
2953 begin
2954 S (1 .. Ada'Length) := Ada;
2955 S (Ada'Length + 1 .. S'Length) := Common;
2956 WBI (S);
2957 end;
2959 else
2960 declare
2961 S : String (1 .. C'Length + Common'Length);
2963 begin
2964 S (1 .. C'Length) := C;
2965 S (C'Length + 1 .. S'Length) := Common;
2966 WBI (S);
2967 end;
2968 end if;
2969 end Write_Info_Ada_C;
2971 ----------------------------
2972 -- Write_Statement_Buffer --
2973 ----------------------------
2975 procedure Write_Statement_Buffer is
2976 begin
2977 WBI (Statement_Buffer (1 .. Last));
2978 Last := 0;
2979 end Write_Statement_Buffer;
2981 procedure Write_Statement_Buffer (S : String) is
2982 begin
2983 Set_String (S);
2984 Write_Statement_Buffer;
2985 end Write_Statement_Buffer;
2987 end Bindgen;