1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
9 -- Copyright (C) 2008, 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 -- We need to deal with integer values that can be signed or unsigned,
63 -- so we need to cater for the maximum range of both cases.
65 type String_Access is access all String;
66 -- Note: we can't use GNAT.Strings for this definition, since that unit
67 -- is not available in older base compilers.
69 type Int_Value_Type is record
71 Abs_Value : Long_Unsigned := 0;
75 (CND, -- Constant (decimal)
76 CNS, -- Constant (freeform string)
78 -- Recognized markers found in assembly file. These markers are produced
79 -- by the same-named macros from the C template.
81 type Asm_Info (Kind : Asm_Info_Kind := TXT) is record
82 Line_Number : Integer;
83 -- Line number in C source file
85 Constant_Name : String_Access;
86 -- Name of constant to be defined
88 Value_Len : Natural := 0;
89 -- Length of text representation of constant's value
91 Text_Value : String_Access;
92 -- Value for CNS constant
94 Int_Value : Int_Value_Type;
95 -- Value for CND constant
97 Comment : String_Access;
98 -- Additional descriptive comment for constant, or free-form text (TXT)
101 package Asm_Infos is new GNAT.Table (
102 Table_Component_Type => Asm_Info,
103 Table_Index_Type => Integer,
104 Table_Low_Bound => 1,
105 Table_Initial => 100,
106 Table_Increment => 10);
108 Max_Constant_Name_Len : Natural := 0;
109 Max_Constant_Value_Len : Natural := 0;
110 -- Longest name and longest value lengths
112 procedure Output_Info (OFile : Sfile; Info_Index : Integer);
113 -- Output information from the indicated asm info line
115 procedure Parse_Asm_Line (Line : String);
116 -- Parse one information line from the assembly source
118 function Contains_Template_Name (S : String) return Boolean;
119 -- True if S contains Tmpl_Name, possibly with different casing
121 function Spaces (Count : Integer) return String;
122 -- If Count is positive, return a string of Count spaces, else return an
125 ----------------------------
126 -- Contains_Template_Name --
127 ----------------------------
129 function Contains_Template_Name (S : String) return Boolean is
131 return Index (Source => To_Lower (S), Pattern => Tmpl_Name) > 0;
132 end Contains_Template_Name;
138 procedure Output_Info (OFile : Sfile; Info_Index : Integer) is
139 Info : Asm_Info renames Asm_Infos.Table (Info_Index);
141 procedure Put (S : String);
147 procedure Put (S : String) is
153 if Info.Kind /= TXT then
154 -- TXT case is handled by the common code below
157 Put (Info.Constant_Name.all);
158 Put (Spaces (Max_Constant_Name_Len - Info.Constant_Name'Length));
160 Put (" : constant := ");
162 if Info.Kind = CND then
163 if not Info.Int_Value.Positive then
166 Put (Trim (Info.Int_Value.Abs_Value'Img, Side => Left));
168 Put (Info.Text_Value.all);
173 if Info.Comment'Length > 0 then
174 Put (Spaces (Max_Constant_Value_Len - Info.Value_Len));
179 Put (Info.Comment.all);
187 procedure Parse_Asm_Line (Line : String) is
188 Index1, Index2 : Integer := Line'First;
190 function Field_Alloc return String_Access;
191 -- Allocate and return a copy of Line (Index1 .. Index2 - 1)
193 procedure Find_Colon (Index : in out Integer);
194 -- Increment Index until the next colon in Line
196 function Parse_Int (S : String) return Int_Value_Type;
197 -- Parse a decimal number, preceded by an optional '$' or '#' character,
198 -- and return its value.
204 function Field_Alloc return String_Access is
206 return new String'(Line (Index1 .. Index2 - 1));
213 procedure Find_Colon (Index : in out Integer) is
217 exit when Index > Line'Last or else Line (Index) = ':';
225 function Parse_Int (S : String) return Int_Value_Type is
226 First : Integer := S'First;
229 -- On some platforms, immediate integer values are prefixed with
230 -- a $ or # character in assembly output.
233 or else S (First) = '#'
238 if S (First) = '-' then
245 return (Positive => Positive,
246 Abs_Value => Long_Unsigned'Value (S (First .. S'Last)));
250 Put_Line (Standard_Error, "can't parse decimal value: " & S);
254 -- Start of processing for Parse_Asm_Line
260 Info : Asm_Info (Kind => Asm_Info_Kind'Value
261 (Line (Line'First .. Index2 - 1)));
263 Index1 := Index2 + 1;
267 Integer (Parse_Int (Line (Index1 .. Index2 - 1)).Abs_Value);
271 Index1 := Index2 + 1;
274 Info.Constant_Name := Field_Alloc;
275 if Info.Constant_Name'Length > Max_Constant_Name_Len then
276 Max_Constant_Name_Len := Info.Constant_Name'Length;
279 Index1 := Index2 + 1;
282 if Info.Kind = CND then
283 Info.Int_Value := Parse_Int (Line (Index1 .. Index2 - 1));
284 Info.Value_Len := Index2 - Index1 - 1;
286 Info.Text_Value := Field_Alloc;
287 Info.Value_Len := Info.Text_Value'Length;
294 Index1 := Index2 + 1;
295 Index2 := Line'Last + 1;
296 Info.Comment := Field_Alloc;
298 if Info.Kind = TXT then
299 Info.Text_Value := Info.Comment;
301 -- Update Max_Constant_Value_Len, but only if this constant has
302 -- a comment (else the value is allowed to be longer).
304 elsif Info.Comment'Length > 0 then
305 if Info.Value_Len > Max_Constant_Value_Len then
306 Max_Constant_Value_Len := Info.Value_Len;
310 Asm_Infos.Append (Info);
314 Put_Line (Standard_Error,
315 "can't parse " & Line);
316 Put_Line (Standard_Error,
317 "exception raised: " & Exception_Information (E));
324 function Spaces (Count : Integer) return String is
329 return (1 .. Count => ' ');
333 -- Local declarations
335 Asm_File_Name : constant String := Tmpl_Name & ".s";
336 Tmpl_File_Name : constant String := Tmpl_Name & ".i";
337 Ada_File_Name : constant String := Unit_Name & ".ads";
339 Asm_File : Ada.Text_IO.File_Type;
340 Tmpl_File : Ada.Text_IO.File_Type;
343 Line : String (1 .. 256);
345 -- Line being processed
347 Current_Line : Integer;
348 Current_Info : Integer;
349 In_Comment : Boolean;
350 In_Template : Boolean;
352 -- Start of processing for XOSCons
355 -- Load values from assembly file
357 Open (Asm_File, In_File, Asm_File_Name);
359 while not End_Of_File (Asm_File) loop
360 Get_Line (Asm_File, Line, Last);
361 if Last > 2 and then Line (1 .. 2) = "->" then
362 Parse_Asm_Line (Line (3 .. Last));
368 -- Load C template and output definitions
370 Open (Tmpl_File, In_File, Tmpl_File_Name);
371 Create (OFile, Out_File, Ada_File_Name);
374 Current_Info := Asm_Infos.First;
377 while not End_Of_File (Tmpl_File) loop
379 Get_Line (Tmpl_File, Line, Last);
381 if Last >= 2 and then Line (1 .. 2) = "# " then
383 Index : Integer := 3;
385 while Index <= Last and then Line (Index) in '0' .. '9' loop
389 if Contains_Template_Name (Line (Index + 1 .. Last)) then
390 Current_Line := Integer'Value (Line (3 .. Index - 1));
394 In_Template := False;
398 elsif In_Template then
400 if Line (1 .. Last) = "*/" then
403 Put_Line (OFile, Line (1 .. Last));
406 elsif Line (1 .. Last) = "/*" then
409 elsif Asm_Infos.Table (Current_Info).Line_Number = Current_Line then
410 Output_Info (OFile, Current_Info);
411 Current_Info := Current_Info + 1;
413 Current_Line := Current_Line + 1;