OSDN Git Service

* config/i386/i386-builtin-types.awk: New file.
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386-builtin-types.awk
1 #  Copyright (C) 2009 Free Software Foundation, Inc.
2 #
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation; either version 3, or (at your option) any
6 # later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; see the file COPYING3.  If not see
15 # <http://www.gnu.org/licenses/>.
16
17 # Generates compressed tables for types for i386 builtin functions.
18
19 function do_error(string) {
20     print FILENAME ":" FNR ": " string > "/dev/stderr"
21     errors = 1
22 }
23
24 function check_type(string) {
25     if (!(string in type_hash))
26         do_error("undefined type code " string)
27 }
28
29 # We can significantly reduce the size of the read-only tables
30 # by forcing the compiler to use a smaller implementation type
31 # for the enumerations.
32 function attribute_mode(count) {
33     # ??? Except that we get strange "comparison always false" warnings
34     # for comparisons between different elements of the enumeration.
35     #    print "#ifdef __GNUC__"
36     #    if (count < 256)
37     #   print "  __attribute__((__mode__(__QI__)))"
38     #    else
39     #   print "  __attribute__((__mode__(__HI__)))"
40     #    print "#endif"
41 }
42
43 BEGIN {
44     FS = "[() \t,]+"
45    
46     prim_defs = 0
47     vect_defs = 0
48     ptr_defs = 0
49     cptr_defs = 0
50     func_defs = 0
51     func_args = 0
52     alias_defs = 0
53 }
54
55 # Skip blank lines or comments.
56 /^[ \t]*(#|$)/ {
57     next
58 }
59
60 $1 == "DEF_PRIMITIVE_TYPE" {
61     if (NF == 4) {
62         type_hash[$2] = 1
63         prim_name[prim_defs] = $2
64         prim_base[prim_defs] = $3
65         prim_defs++
66     } else
67         do_error("DEF_PRIMITIVE_TYPE expected 2 arguments")
68     next
69 }
70
71 $1 == "DEF_VECTOR_TYPE" {
72     if (NF == 4) {
73         check_type($3)
74         type_hash[$2] = 1
75         vect_mode[vect_defs] = $2
76         vect_base[vect_defs] = $3
77         vect_defs++
78     } else
79         do_error("DEF_VECTOR_TYPE expected 2 arguments")
80     next
81 }
82
83 $1 == "DEF_POINTER_TYPE" {
84     if (NF == 4) {
85         check_type($3)
86         type_hash[$2] = 1
87         ptr_name[ptr_defs] = $2
88         ptr_base[ptr_defs] = $3
89         ptr_defs++
90     } else if (NF == 5) {
91         check_type($3)
92         if ($4 == "CONST") {
93             type_hash[$2] = 1
94             cptr_name[cptr_defs] = $2
95             cptr_base[cptr_defs] = $3
96             cptr_defs++
97         } else
98             do_error("invalid qualifier \"" $4 "\"")
99     }
100     else
101         do_error("DEF_POINTER_TYPE expected 2 or 3 arguments")
102     next
103 }
104
105 $1 == "DEF_FUNCTION_TYPE" {
106     func_start[func_defs] = func_args
107     for (i = 2; i < NF; ++i) {
108         check_type($i)
109         func_types[func_args++] = $i
110     }
111
112     if (NF < 3)
113         do_error("DEF_FUNCTION_TYPE expected at least 1 argument")
114     else if (NF == 3)
115         name = $2 "_FTYPE_VOID"
116     else {
117         name = $2 "_FTYPE"
118         for (i = 3; i < NF; ++i)
119             name = name "_" $i
120     }
121     func_hash[name] = 1
122     func_name[func_defs++] = name
123     next
124 }
125
126 $1 == "DEF_FUNCTION_TYPE_ALIAS" {
127     if (NF == 4) {
128         if ($2 in func_hash) {
129             alias_base[alias_defs] = $2
130             alias_name[alias_defs] = $2 "_" $3
131             alias_defs++
132         } else
133             do_error("undefined function code " $2)
134     } else
135         do_error("DEF_FUNCTION_TYPE_ALIAS expected 2 arguments")
136     next
137 }
138
139 {
140     do_error("unknown directive \"" $1 "\"");
141 }
142
143 END {
144     if (errors)
145         exit 1
146
147     print "/* This file is auto-generated by i386-builtin-types.awk.  */\n"
148
149     # This first enumeration contains all of the non-function types.
150     print "enum ix86_builtin_type {"
151     for (i = 0; i < prim_defs; ++i)
152         print "  IX86_BT_" prim_name[i] ","
153     print "  IX86_BT_LAST_PRIM = IX86_BT_" prim_name[i-1] ","
154     for (i = 0; i < vect_defs; ++i)
155         print "  IX86_BT_" vect_mode[i] ","
156     print "  IX86_BT_LAST_VECT = IX86_BT_" vect_mode[i-1] ","
157     for (i = 0; i < ptr_defs; ++i)
158         print "  IX86_BT_" ptr_name[i] ","
159     print "  IX86_BT_LAST_PTR = IX86_BT_" ptr_name[i-1] ","
160     for (i = 0; i < cptr_defs; ++i)
161         print "  IX86_BT_" cptr_name[i] ","
162     print "  IX86_BT_LAST_CPTR = IX86_BT_" cptr_name[i-1] "\n}"
163     attribute_mode(prim_defs + vect_defs + ptr_defs + cptr_defs)
164     print ";\n\n"
165
166     # We can't tabularize the initialization of the primitives, since
167     # at least one of them is created via a local variable.  That's ok,
168     # just create a nice big macro to do all the work.
169     print "#define DEFINE_BUILTIN_PRIMITIVE_TYPES \\"
170     for (i = 0; i < prim_defs; ++i) {
171         printf "  ix86_builtin_type_tab[(int)IX86_BT_" prim_name[i] \
172             "] = " prim_base[i]
173         if (i < prim_defs - 1)
174             print ", \\"
175     }
176     print "\n\n"
177
178     # The vector types are defined via two tables defining the real
179     # machine mode and the builtin primitive type.  We use two tables
180     # rather than a structure to avoid structure padding and save space.
181     print "static const enum machine_mode ix86_builtin_type_vect_mode[] = {"
182     for (i = 0; i < vect_defs; ++i) {
183         if (i == 0)
184             printf "  "
185         else if (i % 6 == 0)
186             printf ",\n  "
187         else
188             printf ", "
189         printf vect_mode[i] "mode"
190     }
191     print "\n};\n\n"
192
193     print "static const enum ix86_builtin_type " \
194         "ix86_builtin_type_vect_base[] = {"
195     for (i = 0; i < vect_defs; ++i) {
196         if (i == 0)
197             printf "  "
198         else if (i % 4 == 0)
199             printf ",\n  "
200         else
201             printf ", "
202         printf "IX86_BT_" vect_base[i]
203     }
204     print "\n};\n\n"
205
206     # The pointer types are defined via a single table defining the
207     # builtin primitive type.  The const-ness of the pointer is taken
208     # from the enumeration value > IX86_BT_LAST_PTR.
209     print "static const enum ix86_builtin_type " \
210         "ix86_builtin_type_ptr_base[] = {"
211     for (i = 0; i < ptr_defs; ++i) {
212         if (i == 0)
213             printf " "
214         else if (i % 4 == 0)
215             printf "\n "
216         printf " IX86_BT_" ptr_base[i] ","
217     }
218     print "\n  /* pointer-to-constant defs start here */"
219     for (i = 0; i < cptr_defs; ++i) {
220         if (i == 0)
221             printf "  "
222         else if (i % 4 == 0)
223             printf ",\n  "
224         else
225             printf ", "
226         printf "IX86_BT_" cptr_base[i]
227     }
228     print "\n};\n\n"
229
230     # This second enumeration contains all of the function types.
231     print "enum ix86_builtin_func_type {"
232     for (i = 0; i < func_defs; ++i)
233         print "  " func_name[i] ","
234     print "  IX86_BT_LAST_FUNC = " func_name[i-1] ","
235     for (i = 0; i < alias_defs; ++i)
236         print "  " alias_name[i] ","
237     print "  IX86_BT_LAST_ALIAS = " alias_name[i-1] "\n}"
238     attribute_mode(func_defs + alias_defs)
239     print ";\n\n"
240
241     # The function types are defined via two tables.  The first contains
242     # ranges consiting of the function's return type, followed by all of
243     # the function argument types.  The ranges for all of the builtin
244     # functions are smooshed together in the same array.  The second array
245     # contains, for each builtin, the index of the function's return type
246     # within the first array.
247     print "static const enum ix86_builtin_type ix86_builtin_func_args[] = {"
248     for (i = 0; i < func_args; ++i) {
249         if (i == 0)
250             printf "  "
251         else if (i % 4 == 0)
252             printf ",\n  "
253         else
254             printf ", "
255         printf "IX86_BT_" func_types[i]
256     }
257     print "\n};\n\n"
258
259     print "static const unsigned short ix86_builtin_func_start[] = {"
260     for (i = 0; i < func_defs; ++i) {
261         if (i == 0)
262             printf " "
263         else if (i % 10 == 0)
264             printf "\n "
265         printf " " func_start[i] ","
266     }
267     print " " func_args "\n};\n\n"
268
269     print "static const enum ix86_builtin_func_type " \
270         "ix86_builtin_func_alias_base[] = {"
271     for (i = 0; i < alias_defs; ++i) {
272         if (i == 0)
273             printf "  "
274         else
275             printf ",\n  "
276         printf alias_base[i]
277     }
278     print "\n};"
279 }