1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
9 -- Copyright (C) 2008-2009, 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 -- This program generates the spec of System.OS_Constants (s-oscons.ads)
28 -- It works in conjunction with a C template file which must be pre-processed
29 -- and compiled using the cross compiler. Two input files are used:
30 -- - the preprocessed C file: s-oscons-tmplt.i
31 -- - the generated assembly file: s-oscons-tmplt.s
33 -- The contents of s-oscons.ads is written on standard output
35 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
36 with Ada
.Exceptions
; use Ada
.Exceptions
;
37 with Ada
.Strings
.Fixed
; use Ada
.Strings
.Fixed
;
38 with Ada
.Text_IO
; use Ada
.Text_IO
;
39 with Ada
.Streams
.Stream_IO
; use Ada
.Streams
.Stream_IO
;
41 pragma Warnings
(Off
);
42 -- System.Unsigned_Types is an internal GNAT unit
43 with System
.Unsigned_Types
; use System
.Unsigned_Types
;
48 with XUtil
; use XUtil
;
55 Unit_Name
: constant String := "s-oscons";
56 Tmpl_Name
: constant String := Unit_Name
& "-tmplt";
58 -------------------------------------------------
59 -- Information retrieved from assembly listing --
60 -------------------------------------------------
62 type String_Access
is access all String;
63 -- Note: we can't use GNAT.Strings for this definition, since that unit
64 -- is not available in older base compilers.
66 -- We need to deal with integer values that can be signed or unsigned, so
67 -- we need to accomodate the maximum range of both cases.
69 type Int_Value_Type
is record
71 Abs_Value
: Long_Unsigned
:= 0;
75 (CND
, -- Named number (decimal)
76 CNS
, -- Named number (freeform text)
79 -- Recognized markers found in assembly file. These markers are produced by
80 -- the same-named macros from the C template.
82 subtype Named_Number
is Asm_Info_Kind
range CND
.. CNS
;
84 type Asm_Info
(Kind
: Asm_Info_Kind
:= TXT
) is record
85 Line_Number
: Integer;
86 -- Line number in C source file
88 Constant_Name
: String_Access
;
89 -- Name of constant to be defined
91 Constant_Type
: String_Access
;
92 -- Type of constant (case of Kind = C)
94 Value_Len
: Natural := 0;
95 -- Length of text representation of constant's value
97 Text_Value
: String_Access
;
98 -- Value for CNS / C constant
100 Int_Value
: Int_Value_Type
;
101 -- Value for CND constant
103 Comment
: String_Access
;
104 -- Additional descriptive comment for constant, or free-form text (TXT)
107 package Asm_Infos
is new GNAT
.Table
108 (Table_Component_Type
=> Asm_Info
,
109 Table_Index_Type
=> Integer,
110 Table_Low_Bound
=> 1,
111 Table_Initial
=> 100,
112 Table_Increment
=> 10);
114 Max_Constant_Name_Len
: Natural := 0;
115 Max_Constant_Value_Len
: Natural := 0;
116 Max_Constant_Type_Len
: Natural := 0;
117 -- Lengths of longest name and longest value
119 type Language
is (Lang_Ada
, Lang_C
);
121 procedure Output_Info
124 Info_Index
: Integer);
125 -- Output information from the indicated asm info line
127 procedure Parse_Asm_Line
(Line
: String);
128 -- Parse one information line from the assembly source
130 function Contains_Template_Name
(S
: String) return Boolean;
131 -- True if S contains Tmpl_Name, possibly with different casing
133 function Spaces
(Count
: Integer) return String;
134 -- If Count is positive, return a string of Count spaces, else return an
137 ----------------------------
138 -- Contains_Template_Name --
139 ----------------------------
141 function Contains_Template_Name
(S
: String) return Boolean is
143 if Index
(Source
=> To_Lower
(S
), Pattern
=> Tmpl_Name
) > 0 then
148 end Contains_Template_Name
;
154 procedure Output_Info
157 Info_Index
: Integer)
159 Info
: Asm_Info
renames Asm_Infos
.Table
(Info_Index
);
161 procedure Put
(S
: String);
168 procedure Put
(S
: String) is
174 if Info
.Kind
/= TXT
then
175 -- TXT case is handled by the common code below
179 Put
(" " & Info
.Constant_Name
.all);
180 Put
(Spaces
(Max_Constant_Name_Len
181 - Info
.Constant_Name
'Length));
183 if Info
.Kind
in Named_Number
then
184 Put
(" : constant := ");
186 Put
(" : constant " & Info
.Constant_Type
.all);
187 Put
(Spaces
(Max_Constant_Type_Len
188 - Info
.Constant_Type
'Length));
193 Put
("#define " & Info
.Constant_Name
.all & " ");
194 Put
(Spaces
(Max_Constant_Name_Len
195 - Info
.Constant_Name
'Length));
198 if Info
.Kind
= CND
then
199 if not Info
.Int_Value
.Positive then
202 Put
(Trim
(Info
.Int_Value
.Abs_Value
'Img, Side
=> Left
));
205 Is_String
: constant Boolean :=
207 and then Info
.Constant_Type
.all = "String";
212 Put
(Info
.Text_Value
.all);
219 if Lang
= Lang_Ada
then
222 if Info
.Comment
'Length > 0 then
223 Put
(Spaces
(Max_Constant_Value_Len
- Info
.Value_Len
));
229 if Lang
= Lang_Ada
then
230 Put
(Info
.Comment
.all);
240 procedure Parse_Asm_Line
(Line
: String) is
241 Index1
, Index2
: Integer := Line
'First;
243 function Field_Alloc
return String_Access
;
244 -- Allocate and return a copy of Line (Index1 .. Index2 - 1)
246 procedure Find_Colon
(Index
: in out Integer);
247 -- Increment Index until the next colon in Line
249 function Parse_Int
(S
: String) return Int_Value_Type
;
250 -- Parse a decimal number, preceded by an optional '$' or '#' character,
251 -- and return its value.
257 function Field_Alloc
return String_Access
is
259 return new String'(Line (Index1 .. Index2 - 1));
266 procedure Find_Colon (Index : in out Integer) is
270 exit when Index > Line'Last or else Line (Index) = ':';
278 function Parse_Int (S : String) return Int_Value_Type is
279 First : Integer := S'First;
282 -- On some platforms, immediate integer values are prefixed with
283 -- a $ or # character in assembly output.
285 if S (First) = '$
' or else S (First) = '#
' then
289 if S (First) = '-' then
296 return (Positive => Positive,
297 Abs_Value => Long_Unsigned'Value (S (First .. S'Last)));
301 Put_Line (Standard_Error, "can't parse decimal value: " & S);
305 -- Start of processing for Parse_Asm_Line
311 Info : Asm_Info (Kind => Asm_Info_Kind'Value
312 (Line (Line'First .. Index2 - 1)));
314 Index1 := Index2 + 1;
318 Integer (Parse_Int (Line (Index1 .. Index2 - 1)).Abs_Value);
321 when CND | CNS | C =>
322 Index1 := Index2 + 1;
325 Info.Constant_Name := Field_Alloc;
326 if Info.Constant_Name'Length > Max_Constant_Name_Len then
327 Max_Constant_Name_Len := Info.Constant_Name'Length;
330 Index1 := Index2 + 1;
333 if Info.Kind = C then
334 Info.Constant_Type := Field_Alloc;
335 if Info.Constant_Type'Length > Max_Constant_Type_Len then
336 Max_Constant_Type_Len := Info.Constant_Type'Length;
339 Index1 := Index2 + 1;
343 if Info.Kind = CND then
344 Info.Int_Value := Parse_Int (Line (Index1 .. Index2 - 1));
345 Info.Value_Len := Index2 - Index1 - 1;
348 Info.Text_Value := Field_Alloc;
349 Info.Value_Len := Info.Text_Value'Length;
356 Index1 := Index2 + 1;
357 Index2 := Line'Last + 1;
358 Info.Comment := Field_Alloc;
360 if Info.Kind = TXT then
361 Info.Text_Value := Info.Comment;
363 -- Update Max_Constant_Value_Len, but only if this constant has a
364 -- comment (else the value is allowed to be longer).
366 elsif Info.Comment'Length > 0 then
367 if Info.Value_Len > Max_Constant_Value_Len then
368 Max_Constant_Value_Len := Info.Value_Len;
372 Asm_Infos.Append (Info);
376 Put_Line (Standard_Error,
377 "can't parse " & Line);
378 Put_Line (Standard_Error,
379 "exception raised: " & Exception_Information (E));
386 function Spaces (Count : Integer) return String is
391 return (1 .. Count => ' ');
395 -- Local declarations
399 Tmpl_File_Name : constant String := Tmpl_Name & ".i";
400 Asm_File_Name : constant String := Tmpl_Name & ".s";
404 Ada_File_Name : constant String := Unit_Name & ".ads";
405 C_File_Name : constant String := Unit_Name & ".h";
407 Asm_File : Ada.Text_IO.File_Type;
408 Tmpl_File : Ada.Text_IO.File_Type;
412 Line : String (1 .. 256);
414 -- Line being processed
416 Current_Line : Integer;
417 Current_Info : Integer;
418 In_Comment : Boolean;
419 In_Template : Boolean;
421 -- Start of processing for XOSCons
424 -- Load values from assembly file
426 Open (Asm_File, In_File, Asm_File_Name);
428 while not End_Of_File (Asm_File) loop
429 Get_Line (Asm_File, Line, Last);
430 if Last > 2 and then Line (1 .. 2) = "->" then
431 Parse_Asm_Line (Line (3 .. Last));
437 -- Load C template and output definitions
439 Open (Tmpl_File, In_File, Tmpl_File_Name);
440 Create (Ada_OFile, Out_File, Ada_File_Name);
441 Create (C_OFile, Out_File, C_File_Name);
444 Current_Info := Asm_Infos.First;
447 while not End_Of_File (Tmpl_File) loop
449 Get_Line (Tmpl_File, Line, Last);
451 if Last >= 2 and then Line (1 .. 2) = "# " then
453 Index : Integer := 3;
455 while Index <= Last and then Line (Index) in '0' .. '9' loop
459 if Contains_Template_Name (Line (Index + 1 .. Last)) then
460 Current_Line := Integer'Value (Line (3 .. Index - 1));
464 In_Template := False;
468 elsif In_Template then
470 if Line (1 .. Last) = "*/" then
471 Put_Line (C_OFile, Line (1 .. Last));
474 Put_Line (Ada_OFile, Line (1 .. Last));
475 Put_Line (C_OFile, Line (1 .. Last));
478 elsif Line (1 .. Last) = "/*" then
479 Put_Line (C_OFile, Line (1 .. Last));
482 elsif Asm_Infos.Table (Current_Info).Line_Number = Current_Line then
483 Output_Info (Lang_Ada, Ada_OFile, Current_Info);
484 Output_Info (Lang_C, C_OFile, Current_Info);
485 Current_Info := Current_Info + 1;
488 Current_Line := Current_Line + 1;