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, -- Constant (decimal)
76 CNS, -- Constant (freeform string)
78 -- Recognized markers found in assembly file. These markers are produced by
79 -- 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_Const_Name_Len : Natural := 0;
109 Max_Constant_Value_Len : Natural := 0;
110 -- Lengths of longest name and longest value
112 type Language is (Lang_Ada, Lang_C);
114 procedure Output_Info
117 Info_Index : Integer);
118 -- Output information from the indicated asm info line
120 procedure Parse_Asm_Line (Line : String);
121 -- Parse one information line from the assembly source
123 function Contains_Template_Name (S : String) return Boolean;
124 -- True if S contains Tmpl_Name, possibly with different casing
126 function Spaces (Count : Integer) return String;
127 -- If Count is positive, return a string of Count spaces, else return an
130 ----------------------------
131 -- Contains_Template_Name --
132 ----------------------------
134 function Contains_Template_Name (S : String) return Boolean is
136 if Index (Source => To_Lower (S), Pattern => Tmpl_Name) > 0 then
141 end Contains_Template_Name;
147 procedure Output_Info
150 Info_Index : Integer)
152 Info : Asm_Info renames Asm_Infos.Table (Info_Index);
154 procedure Put (S : String);
161 procedure Put (S : String) is
167 if Info.Kind /= TXT then
168 -- TXT case is handled by the common code below
172 Put (" " & Info.Constant_Name.all);
173 Put (Spaces (Max_Const_Name_Len - Info.Constant_Name'Length));
175 Put (" : constant := ");
178 Put ("#define " & Info.Constant_Name.all & " ");
179 Put (Spaces (Max_Const_Name_Len - Info.Constant_Name'Length));
182 if Info.Kind = CND then
183 if not Info.Int_Value.Positive then
186 Put (Trim (Info.Int_Value.Abs_Value'Img, Side => Left));
188 Put (Info.Text_Value.all);
191 if Lang = Lang_Ada then
194 if Info.Comment'Length > 0 then
195 Put (Spaces (Max_Constant_Value_Len - Info.Value_Len));
201 if Lang = Lang_Ada then
202 Put (Info.Comment.all);
212 procedure Parse_Asm_Line (Line : String) is
213 Index1, Index2 : Integer := Line'First;
215 function Field_Alloc return String_Access;
216 -- Allocate and return a copy of Line (Index1 .. Index2 - 1)
218 procedure Find_Colon (Index : in out Integer);
219 -- Increment Index until the next colon in Line
221 function Parse_Int (S : String) return Int_Value_Type;
222 -- Parse a decimal number, preceded by an optional '$' or '#' character,
223 -- and return its value.
229 function Field_Alloc return String_Access is
231 return new String'(Line (Index1 .. Index2 - 1));
238 procedure Find_Colon (Index : in out Integer) is
242 exit when Index > Line'Last or else Line (Index) = ':';
250 function Parse_Int (S : String) return Int_Value_Type is
251 First : Integer := S'First;
254 -- On some platforms, immediate integer values are prefixed with
255 -- a $ or # character in assembly output.
257 if S (First) = '$' or else S (First) = '#' then
261 if S (First) = '-' then
268 return (Positive => Positive,
269 Abs_Value => Long_Unsigned'Value (S (First .. S'Last)));
273 Put_Line (Standard_Error, "can't parse decimal value: " & S);
277 -- Start of processing for Parse_Asm_Line
283 Info : Asm_Info (Kind => Asm_Info_Kind'Value
284 (Line (Line'First .. Index2 - 1)));
286 Index1 := Index2 + 1;
290 Integer (Parse_Int (Line (Index1 .. Index2 - 1)).Abs_Value);
294 Index1 := Index2 + 1;
297 Info.Constant_Name := Field_Alloc;
298 if Info.Constant_Name'Length > Max_Const_Name_Len then
299 Max_Const_Name_Len := Info.Constant_Name'Length;
302 Index1 := Index2 + 1;
305 if Info.Kind = CND then
306 Info.Int_Value := Parse_Int (Line (Index1 .. Index2 - 1));
307 Info.Value_Len := Index2 - Index1 - 1;
310 Info.Text_Value := Field_Alloc;
311 Info.Value_Len := Info.Text_Value'Length;
318 Index1 := Index2 + 1;
319 Index2 := Line'Last + 1;
320 Info.Comment := Field_Alloc;
322 if Info.Kind = TXT then
323 Info.Text_Value := Info.Comment;
325 -- Update Max_Constant_Value_Len, but only if this constant has a
326 -- comment (else the value is allowed to be longer).
328 elsif Info.Comment'Length > 0 then
329 if Info.Value_Len > Max_Constant_Value_Len then
330 Max_Constant_Value_Len := Info.Value_Len;
334 Asm_Infos.Append (Info);
338 Put_Line (Standard_Error,
339 "can't parse " & Line);
340 Put_Line (Standard_Error,
341 "exception raised: " & Exception_Information (E));
348 function Spaces (Count : Integer) return String is
353 return (1 .. Count => ' ');
357 -- Local declarations
361 Tmpl_File_Name : constant String := Tmpl_Name & ".i";
362 Asm_File_Name : constant String := Tmpl_Name & ".s";
366 Ada_File_Name : constant String := Unit_Name & ".ads";
367 C_File_Name : constant String := Unit_Name & ".h";
369 Asm_File : Ada.Text_IO.File_Type;
370 Tmpl_File : Ada.Text_IO.File_Type;
374 Line : String (1 .. 256);
376 -- Line being processed
378 Current_Line : Integer;
379 Current_Info : Integer;
380 In_Comment : Boolean;
381 In_Template : Boolean;
383 -- Start of processing for XOSCons
386 -- Load values from assembly file
388 Open (Asm_File, In_File, Asm_File_Name);
390 while not End_Of_File (Asm_File) loop
391 Get_Line (Asm_File, Line, Last);
392 if Last > 2 and then Line (1 .. 2) = "->" then
393 Parse_Asm_Line (Line (3 .. Last));
399 -- Load C template and output definitions
401 Open (Tmpl_File, In_File, Tmpl_File_Name);
402 Create (Ada_OFile, Out_File, Ada_File_Name);
403 Create (C_OFile, Out_File, C_File_Name);
406 Current_Info := Asm_Infos.First;
409 while not End_Of_File (Tmpl_File) loop
411 Get_Line (Tmpl_File, Line, Last);
413 if Last >= 2 and then Line (1 .. 2) = "# " then
415 Index : Integer := 3;
417 while Index <= Last and then Line (Index) in '0' .. '9' loop
421 if Contains_Template_Name (Line (Index + 1 .. Last)) then
422 Current_Line := Integer'Value (Line (3 .. Index - 1));
426 In_Template := False;
430 elsif In_Template then
432 if Line (1 .. Last) = "*/" then
433 Put_Line (C_OFile, Line (1 .. Last));
436 Put_Line (Ada_OFile, Line (1 .. Last));
437 Put_Line (C_OFile, Line (1 .. Last));
440 elsif Line (1 .. Last) = "/*" then
441 Put_Line (C_OFile, Line (1 .. Last));
444 elsif Asm_Infos.Table (Current_Info).Line_Number = Current_Line then
445 Output_Info (Lang_Ada, Ada_OFile, Current_Info);
446 Output_Info (Lang_C, C_OFile, Current_Info);
447 Current_Info := Current_Info + 1;
450 Current_Line := Current_Line + 1;