1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2013-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Debug
; use Debug
;
27 with Get_Targ
; use Get_Targ
;
29 with Output
; use Output
;
31 with System
; use System
;
32 with System
.OS_Lib
; use System
.OS_Lib
;
34 with Unchecked_Conversion
;
36 package body Set_Targ
is
38 --------------------------------------------------------
39 -- Data Used to Read/Write Target Dependent Info File --
40 --------------------------------------------------------
42 -- Table of string names written to file
44 subtype Str
is String;
46 S_Bits_BE
: constant Str
:= "Bits_BE";
47 S_Bits_Per_Unit
: constant Str
:= "Bits_Per_Unit";
48 S_Bits_Per_Word
: constant Str
:= "Bits_Per_Word";
49 S_Bytes_BE
: constant Str
:= "Bytes_BE";
50 S_Char_Size
: constant Str
:= "Char_Size";
51 S_Double_Float_Alignment
: constant Str
:= "Double_Float_Alignment";
52 S_Double_Scalar_Alignment
: constant Str
:= "Double_Scalar_Alignment";
53 S_Double_Size
: constant Str
:= "Double_Size";
54 S_Float_Size
: constant Str
:= "Float_Size";
55 S_Float_Words_BE
: constant Str
:= "Float_Words_BE";
56 S_Int_Size
: constant Str
:= "Int_Size";
57 S_Long_Double_Size
: constant Str
:= "Long_Double_Size";
58 S_Long_Long_Size
: constant Str
:= "Long_Long_Size";
59 S_Long_Size
: constant Str
:= "Long_Size";
60 S_Maximum_Alignment
: constant Str
:= "Maximum_Alignment";
61 S_Max_Unaligned_Field
: constant Str
:= "Max_Unaligned_Field";
62 S_Pointer_Size
: constant Str
:= "Pointer_Size";
63 S_Short_Enums
: constant Str
:= "Short_Enums";
64 S_Short_Size
: constant Str
:= "Short_Size";
65 S_Strict_Alignment
: constant Str
:= "Strict_Alignment";
66 S_System_Allocator_Alignment
: constant Str
:= "System_Allocator_Alignment";
67 S_Wchar_T_Size
: constant Str
:= "Wchar_T_Size";
68 S_Words_BE
: constant Str
:= "Words_BE";
72 type AStr
is access all String;
74 DTN
: constant array (Nat
range <>) of AStr
:= (
75 S_Bits_BE
'Unrestricted_Access,
76 S_Bits_Per_Unit 'Unrestricted_Access
,
77 S_Bits_Per_Word
'Unrestricted_Access,
78 S_Bytes_BE 'Unrestricted_Access
,
79 S_Char_Size
'Unrestricted_Access,
80 S_Double_Float_Alignment 'Unrestricted_Access
,
81 S_Double_Scalar_Alignment
'Unrestricted_Access,
82 S_Double_Size 'Unrestricted_Access
,
83 S_Float_Size
'Unrestricted_Access,
84 S_Float_Words_BE 'Unrestricted_Access
,
85 S_Int_Size
'Unrestricted_Access,
86 S_Long_Double_Size 'Unrestricted_Access
,
87 S_Long_Long_Size
'Unrestricted_Access,
88 S_Long_Size 'Unrestricted_Access
,
89 S_Maximum_Alignment
'Unrestricted_Access,
90 S_Max_Unaligned_Field 'Unrestricted_Access
,
91 S_Pointer_Size
'Unrestricted_Access,
92 S_Short_Enums 'Unrestricted_Access
,
93 S_Short_Size
'Unrestricted_Access,
94 S_Strict_Alignment 'Unrestricted_Access
,
95 S_System_Allocator_Alignment
'Unrestricted_Access,
96 S_Wchar_T_Size 'Unrestricted_Access
,
97 S_Words_BE
'Unrestricted_Access);
99 -- Table of corresponding value pointers
101 DTV : constant array (Nat range <>) of System.Address := (
103 Bits_Per_Unit
'Address,
104 Bits_Per_Word 'Address
,
107 Double_Float_Alignment
'Address,
108 Double_Scalar_Alignment 'Address
,
109 Double_Size
'Address,
111 Float_Words_BE
'Address,
113 Long_Double_Size
'Address,
114 Long_Long_Size 'Address
,
116 Maximum_Alignment 'Address
,
117 Max_Unaligned_Field
'Address,
118 Pointer_Size 'Address
,
119 Short_Enums
'Address,
121 Strict_Alignment
'Address,
122 System_Allocator_Alignment 'Address
,
123 Wchar_T_Size
'Address,
126 DTR
: array (Nat
range DTV
'Range) of Boolean := (others => False);
127 -- Table of flags used to validate that all values are present in file
129 -----------------------
130 -- Local Subprograms --
131 -----------------------
133 procedure Fail
(E
: String);
134 pragma No_Return
(Fail
);
135 -- Terminate program with fatal error message passed as parameter
137 procedure Register_Float_Type
142 Float_Rep
: Float_Rep_Kind
;
143 Precision
: Positive;
145 Alignment
: Natural);
146 pragma Convention
(C
, Register_Float_Type
);
147 -- Call back to allow the back end to register available types. This call
148 -- back makes entries in the FPT_Mode_Table for any floating point types
149 -- reported by the back end. Name is the name of the type as a normal
150 -- format Null-terminated string. Digs is the number of digits, where 0
151 -- means it is not a fpt type (ignored during registration). Complex is
152 -- non-zero if the type has real and imaginary parts (also ignored during
153 -- registration). Count is the number of elements in a vector type (zero =
154 -- not a vector, registration ignores vectors). Float_Rep shows the kind of
155 -- floating-point type, and Precision, Size and Alignment are the precision
156 -- size and alignment in bits.
158 -- So to summarize, the only types that are actually registered have Digs
159 -- non-zero, Complex zero (false), and Count zero (not a vector).
165 procedure Fail
(E
: String) is
166 E_Fatal
: constant := 4;
167 -- Code for fatal error
174 -------------------------
175 -- Register_Float_Type --
176 -------------------------
178 procedure Register_Float_Type
183 Float_Rep
: Float_Rep_Kind
;
184 Precision
: Positive;
188 T
: String (1 .. Name
'Length);
192 -- Dump information given by the back end for the type to register
200 Write_Str
("type " & T
(1 .. Last
) & " is ");
203 Write_Str
("array (1 .. ");
204 Write_Int
(Int
(Count
));
207 Write_Str
(", 1 .. 2");
213 Write_Str
("array (1 .. 2) of ");
217 Write_Str
("digits ");
218 Write_Int
(Int
(Digs
));
221 Write_Str
("pragma Float_Representation (");
240 Write_Int
(Int
(Digs
));
243 when AAMP
=> Write_Str
("AAMP");
246 Write_Line
(", " & T
(1 .. Last
) & ");");
249 Write_Str
("mod 2**");
250 Write_Int
(Int
(Precision
/ Positive'Max (1, Count
)));
254 if Precision
= Size
then
255 Write_Str
("for " & T
(1 .. Last
) & "'Size use ");
256 Write_Int
(Int
(Size
));
260 Write_Str
("for " & T
(1 .. Last
) & "'Value_Size use ");
261 Write_Int
(Int
(Precision
));
264 Write_Str
("for " & T
(1 .. Last
) & "'Object_Size use ");
265 Write_Int
(Int
(Size
));
269 Write_Str
("for " & T
(1 .. Last
) & "'Alignment use ");
270 Write_Int
(Int
(Alignment
/ 8));
275 -- Start of processing for Register_Float_Type
280 for J
in T
'Range loop
281 T
(J
) := Name
(Name
'First + J
- 1);
283 if T
(J
) = ASCII
.NUL
then
289 -- Dump info if debug flag set
291 if Debug_Flag_Dot_B
then
295 -- Acquire entry if non-vector non-complex fpt type (digits non-zero)
297 if Digs
> 0 and then not Complex
and then Count
= 0 then
298 Num_FPT_Modes
:= Num_FPT_Modes
+ 1;
299 FPT_Mode_Table
(Num_FPT_Modes
) :=
300 (NAME
=> new String'(T (1 .. Last)),
302 FLOAT_REP => Float_Rep,
303 PRECISION => Precision,
305 ALIGNMENT => Alignment);
307 end Register_Float_Type;
309 -----------------------------------
310 -- Write_Target_Dependent_Values --
311 -----------------------------------
313 -- We do this at the System.Os_Lib level, since we have to do the read at
314 -- that level anyway, so it is easier and more consistent to follow the
315 -- same path for the write.
317 procedure Write_Target_Dependent_Values is
318 Fdesc : File_Descriptor;
321 Buffer : String (1 .. 80);
323 -- Buffer used to build line one of file
325 type ANat is access all Natural;
326 -- Pointer to Nat or Pos value (it is harmless to treat Pos values and
327 -- Nat values as Natural via Unchecked_Conversion).
329 function To_ANat is new Unchecked_Conversion (Address, ANat);
331 procedure AddC (C : Character);
332 -- Add one character to buffer
334 procedure AddN (N : Natural);
335 -- Add representation of integer N to Buffer, updating Buflen. N
336 -- must be less than 1000, and output is 3 characters with leading
339 procedure Write_Line;
340 -- Output contents of Buffer (1 .. Buflen) followed by a New_Line,
341 -- and set Buflen back to zero, ready to write next line.
347 procedure AddC (C : Character) is
349 Buflen := Buflen + 1;
350 Buffer (Buflen) := C;
357 procedure AddN (N : Natural) is
364 AddC (Character'Val (48 + N / 100));
370 AddC (Character'Val (48 + N / 10 mod 10));
375 AddC (Character'Val (48 + N mod 10));
382 procedure Write_Line is
386 if Buflen /= Write (Fdesc, Buffer'Address, Buflen) then
387 Delete_File (Target_Dependent_Info_Write_Name'Address, OK);
388 Fail ("disk full writing file "
389 & Target_Dependent_Info_Write_Name.all);
395 -- Start of processing for Write_Target_Dependent_Values
399 Create_File (Target_Dependent_Info_Write_Name.all'Address, Text);
401 if Fdesc = Invalid_FD then
402 Fail ("cannot create file " & Target_Dependent_Info_Write_Name.all);
405 -- Loop through values
407 for J in DTN'Range loop
411 Buflen := DTN (J)'Length;
412 Buffer (1 .. Buflen) := DTN (J).all;
416 while Buflen < 26 loop
423 -- Output value and write line
425 AddN (To_ANat (DTV (J)).all);
429 -- Blank line to separate sections
433 -- Write lines for registered FPT types
435 for J in 1 .. Num_FPT_Modes loop
437 E : FPT_Mode_Entry renames FPT_Mode_Table (J);
439 Buflen := E.NAME'Last;
440 Buffer (1 .. Buflen) := E.NAME.all;
442 -- Pad out to line up values
444 while Buflen < 11 loop
479 Fail ("disk full writing file "
480 & Target_Dependent_Info_Write_Name.all);
482 end Write_Target_Dependent_Values;
484 -- Package Initialization, set target dependent values. This must be done
485 -- early on, before we start accessing various compiler packages, since
486 -- these values are used all over the place.
489 -- First step: see if the -gnateT switch is present. As we have noted,
490 -- this has to be done very early, so can not depend on the normal circuit
491 -- for reading switches and setting switches in Opt. The following code
492 -- will set Opt.Target_Dependent_Info_Read_Name if the switch -gnateT=name
493 -- is present in the options string.
496 type Arg_Array is array (Nat) of Big_String_Ptr;
497 type Arg_Array_Ptr is access Arg_Array;
498 -- Types to access compiler arguments
501 pragma Import (C, save_argc);
502 -- Saved value of argc (number of arguments), imported from misc.c
504 save_argv : Arg_Array_Ptr;
505 pragma Import (C, save_argv);
506 -- Saved value of argv (argument pointers), imported from misc.c
509 gnat_argv : Arg_Array_Ptr;
510 pragma Import (C, gnat_argc);
511 pragma Import (C, gnat_argv);
512 -- If save_argv is not set, default to gnat_argc/argv
515 argv : Arg_Array_Ptr;
517 function Len_Arg (Arg : Big_String_Ptr) return Nat;
518 -- Determine length of argument Arg (a nul terminated C string).
524 function Len_Arg (Arg : Big_String_Ptr) return Nat is
526 for J in 1 .. Nat'Last loop
527 if Arg (Natural (J)) = ASCII.NUL then
536 if save_argv /= null then
540 -- Case of a non gcc compiler, e.g. gnat2why or gnat2scil
545 -- Loop through arguments looking for -gnateT, also look for -gnatd.b
547 for Arg in 1 .. argc - 1 loop
549 Argv_Ptr : constant Big_String_Ptr := argv (Arg);
550 Argv_Len : constant Nat := Len_Arg (Argv_Ptr);
554 and then Argv_Ptr (1 .. 8) = "-gnateT="
556 Opt.Target_Dependent_Info_Read_Name :=
557 new String'(Argv_Ptr
(9 .. Natural (Argv_Len
)));
560 and then Argv_Ptr
(1 .. 8) = "-gnatd.b"
562 Debug_Flag_Dot_B
:= True;
568 -- If the switch is not set, we get all values from the back end
570 if Opt
.Target_Dependent_Info_Read_Name
= null then
572 -- Set values by direct calls to the back end
574 Bits_BE
:= Get_Bits_BE
;
575 Bits_Per_Unit
:= Get_Bits_Per_Unit
;
576 Bits_Per_Word
:= Get_Bits_Per_Word
;
577 Bytes_BE
:= Get_Bytes_BE
;
578 Char_Size
:= Get_Char_Size
;
579 Double_Float_Alignment
:= Get_Double_Float_Alignment
;
580 Double_Scalar_Alignment
:= Get_Double_Scalar_Alignment
;
581 Double_Size
:= Get_Double_Size
;
582 Float_Size
:= Get_Float_Size
;
583 Float_Words_BE
:= Get_Float_Words_BE
;
584 Int_Size
:= Get_Int_Size
;
585 Long_Double_Size
:= Get_Long_Double_Size
;
586 Long_Long_Size
:= Get_Long_Long_Size
;
587 Long_Size
:= Get_Long_Size
;
588 Maximum_Alignment
:= Get_Maximum_Alignment
;
589 Max_Unaligned_Field
:= Get_Max_Unaligned_Field
;
590 Pointer_Size
:= Get_Pointer_Size
;
591 Short_Enums
:= Get_Short_Enums
;
592 Short_Size
:= Get_Short_Size
;
593 Strict_Alignment
:= Get_Strict_Alignment
;
594 System_Allocator_Alignment
:= Get_System_Allocator_Alignment
;
595 Wchar_T_Size
:= Get_Wchar_T_Size
;
596 Words_BE
:= Get_Words_BE
;
598 -- Register floating-point types from the back end
600 Register_Back_End_Types
(Register_Float_Type
'Access);
602 -- Case of reading the target dependent values from file
604 -- This is bit more complex than might be expected, because it has to be
605 -- done very early. All kinds of packages depend on these values, and we
606 -- can't wait till the normal processing of reading command line switches
607 -- etc to read the file. We do this at the System.OS_Lib level since it is
608 -- too early to be using Osint directly.
611 Read_Target_Dependent_Values
: declare
612 File_Desc
: File_Descriptor
;
615 type ANat
is access all Natural;
616 -- Pointer to Nat or Pos value (it is harmless to treat Pos values
617 -- as Nat via Unchecked_Conversion).
619 function To_ANat
is new Unchecked_Conversion
(Address
, ANat
);
623 Buffer
: String (1 .. 2000);
625 -- File information and length (2000 easily enough)
627 Nam_Buf
: String (1 .. 40);
630 procedure Check_Spaces
;
631 -- Checks that we have one or more spaces and skips them
633 procedure FailN
(S
: String);
634 -- Calls Fail adding " name in file xxx", where name is the currently
635 -- gathered name in Nam_Buf, surrounded by quotes, and xxx is the
639 -- Scan out name, leaving it in Nam_Buf with Nam_Len set. Calls
640 -- Skip_Spaces to skip any following spaces. Note that the name is
641 -- terminated by a sequence of at least two spaces.
643 function Get_Nat
return Natural;
644 -- N on entry points to decimal integer, scan out decimal integer
645 -- and return it, leaving N pointing to following space or LF.
647 procedure Skip_Spaces
;
654 procedure Check_Spaces
is
656 if N
> Buflen
or else Buffer
(N
) /= ' ' then
657 FailN
("missing space for");
668 procedure FailN
(S
: String) is
670 Fail
(S
& " """ & Nam_Buf
(1 .. Nam_Len
) & """ in file "
671 & Target_Dependent_Info_Read_Name
.all);
678 procedure Get_Name
is
682 -- Scan out name and put it in Nam_Buf
685 if N
> Buflen
or else Buffer
(N
) = ASCII
.LF
then
686 FailN
("incorrectly formatted line for");
689 -- Name is terminated by two blanks
691 exit when N
< Buflen
and then Buffer
(N
.. N
+ 1) = " ";
693 Nam_Len
:= Nam_Len
+ 1;
695 if Nam_Len
> Nam_Buf
'Last then
696 Fail
("name too long");
699 Nam_Buf
(Nam_Len
) := Buffer
(N
);
710 function Get_Nat
return Natural is
711 Result
: Natural := 0;
716 or else Buffer
(N
) not in '0' .. '9'
719 FailN
("bad value for");
722 Result
:= Result
* 10 + (Character'Pos (Buffer
(N
)) - 48);
725 exit when N
<= Buflen
726 and then (Buffer
(N
) = ASCII
.LF
or else Buffer
(N
) = ' ');
736 procedure Skip_Spaces
is
738 while N
<= Buflen
and Buffer
(N
) = ' ' loop
743 -- Start of processing for Read_Target_Dependent_Values
746 File_Desc
:= Open_Read
(Target_Dependent_Info_Read_Name
.all, Text
);
748 if File_Desc
= Invalid_FD
then
749 Fail
("cannot read file " & Target_Dependent_Info_Read_Name
.all);
752 Buflen
:= Read
(File_Desc
, Buffer
'Address, Buffer
'Length);
754 if Buflen
= Buffer
'Length then
755 Fail
("file is too long: " & Target_Dependent_Info_Read_Name
.all);
758 -- Scan through file for properly formatted entries in first section
761 while N
<= Buflen
and then Buffer
(N
) /= ASCII
.LF
loop
764 -- Validate name and get corresponding value pointer
768 for J
in DTN
'Range loop
769 if DTN
(J
).all = Nam_Buf
(1 .. Nam_Len
) then
770 VP
:= To_ANat
(DTV
(J
));
777 FailN
("unrecognized name");
784 if N
> Buflen
or else Buffer
(N
) /= ASCII
.LF
then
785 FailN
("misformatted line for");
788 N
:= N
+ 1; -- skip LF
791 -- Fall through this loop when all lines in first section read.
792 -- Check that values have been supplied for all entries.
794 for J
in DTR
'Range loop
796 Fail
("missing entry for " & DTN
(J
).all & " in file "
797 & Target_Dependent_Info_Read_Name
.all);
801 -- Now acquire FPT entries
804 Fail
("missing entries for FPT modes in file "
805 & Target_Dependent_Info_Read_Name
.all);
808 if Buffer
(N
) = ASCII
.LF
then
811 Fail
("missing blank line in file "
812 & Target_Dependent_Info_Read_Name
.all);
816 while N
<= Buflen
loop
819 Num_FPT_Modes
:= Num_FPT_Modes
+ 1;
822 E
: FPT_Mode_Entry
renames FPT_Mode_Table
(Num_FPT_Modes
);
825 E
.NAME
:= new String'(Nam_Buf (1 .. Nam_Len));
832 E.FLOAT_REP := IEEE_Binary;
834 E.FLOAT_REP := VAX_Native;
838 FailN ("bad float rep field for");
844 E.PRECISION := Get_Nat;
847 E.ALIGNMENT := Get_Nat;
849 if Buffer (N) /= ASCII.LF then
850 FailN ("junk at end of line for");
853 -- ??? We do not read E.SIZE, see Write_Target_Dependent_Values
856 (E.PRECISION + E.ALIGNMENT - 1) / E.ALIGNMENT * E.ALIGNMENT;
861 end Read_Target_Dependent_Values;