platid.awk revision 1.1.2.2 1 # $NetBSD: platid.awk,v 1.1.2.2 2001/02/11 19:09:25 bouyer Exp $
2 #
3 # Copyright (c) 1999
4 # Shin Takemura and PocketBSD Project. All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3. All advertising materials mentioning features or use of this software
15 # must display the following acknowledgement:
16 # This product includes software developed by the PocketBSD project
17 # and its contributors.
18 # 4. Neither the name of the project nor the names of its contributors
19 # may be used to endorse or promote products derived from this software
20 # without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 # SUCH DAMAGE.
33 #
34
35 #
36 # sub routines
37 #
38
39 function gen_header(file) {
40 printf("/*\n") >> file
41 printf(" * Do not edit.\n") > file
42 printf(" * This file is automatically generated by platid.awk.\n") > file
43 printf(" */\n") > file
44 }
45
46 function error() {
47 printf("ERROR!\n") > stderr
48 exit
49 }
50
51 function mode_check() {
52 if (mode != MACH && mode != CPU) {
53 printf("ERROR: invalid mode( = %d)\n", mode) > stderr
54 exit
55 }
56 }
57
58 function defname() {
59 mode_check()
60
61 #
62 # master header
63 #
64 enumerator[mode] += 1
65 printf("#define PLATID_%s_%s%s_NUM\t%d\n",
66 mode_name[mode], nm, saved_name, enumerator[mode]) > out_h
67 printf("#define PLATID_%s_%s%s\t\\\n",
68 mode_name[mode], nm, saved_name) > out_h
69 printf(" ((PLATID_%s_%s%s_NUM << %s)",
70 mode_name[mode], nm, saved_name, shifts[mode, nest]) > out_h
71 if (nest) {
72 printf("| \\\n PLATID_%s", mode_name[mode]) > out_h
73 for (i = 0; i < nest; i++) {
74 printf("_%s", names[i]) > out_h
75 }
76 }
77 printf(")\n") > out_h
78
79 #
80 # mask header
81 #
82 printf("extern platid_t platid_mask_%s_%s%s;\n",
83 mode_name[mode], nm, saved_name) > out_mask_h
84 printf("#ifdef PLATID_DEFINE_MASK_NICKNAME\n") > out_mask_h
85 if (mode == CPU) {
86 printf("# define GENERIC_%s%s ((int)&platid_mask_%s_%s%s)\n",
87 nm, saved_name,
88 mode_name[mode], nm, saved_name) > out_mask_h
89 } else {
90 printf("# define %s%s ((int)&platid_mask_%s_%s%s)\n",
91 nm, saved_name,
92 mode_name[mode], nm, saved_name) > out_mask_h
93 }
94 printf("#endif\n") > out_mask_h
95
96 #
97 # mask holder
98 #
99 printf("platid_t platid_mask_%s_%s%s = {{\n",
100 mode_name[mode], nm, saved_name) > out_mask_c
101 if (mode == CPU) {
102 printf("\tPLATID_%s_%s%s,\n",
103 mode_name[mode], nm, saved_name) > out_mask_c
104 printf("\tPLATID_WILD\n") > out_mask_c
105 }
106 if (mode == MACH) {
107 if (saved_cpu != "") {
108 printf("\tPLATID_CPU_%s,\n", saved_cpu) > out_mask_c
109 } else {
110 printf("\tPLATID_WILD,\n") > out_mask_c
111 }
112 printf("\tPLATID_%s_%s%s\n",
113 mode_name[mode], nm, saved_name) > out_mask_c
114
115 }
116 printf("}};\n") > out_mask_c
117
118 }
119
120 function flushname() {
121 if ( saved_name != "" ) {
122 defname()
123 }
124 }
125
126 BEGIN {
127 CPU = 1
128 MACH = 2
129
130 nest = 0
131 nm = ""
132 saved_name = ""
133 mode = NA
134 change_cpu = 0
135 saved_cpu = ""
136
137 mode_name[CPU] = "CPU"
138 mode_name[MACH] = "MACH"
139
140 shifts[CPU, 0] = "PLATID_CPU_ARCH_SHIFT"
141 shifts[CPU, 1] = "PLATID_CPU_SERIES_SHIFT"
142 shifts[CPU, 2] = "PLATID_CPU_MODEL_SHIFT"
143 shifts[CPU, 3] = "PLATID_CPU_SUBMODEL_SHIFT"
144
145 shifts[MACH, 0] = "PLATID_VENDOR_SHIFT"
146 shifts[MACH, 1] = "PLATID_SERIES_SHIFT"
147 shifts[MACH, 2] = "PLATID_MODEL_SHIFT"
148 shifts[MACH, 3] = "PLATID_SUBMODEL_SHIFT"
149
150 enumerator[CPU] = 0
151 enumerator[MACH] = 0
152
153 if (stderr == "") {
154 stderr = "/dev/stderr"
155 }
156 if (out_h == "") {
157 out_h = "platid_generated.h"
158 }
159 if (out_mask_h == "") {
160 out_mask_h = "platid_mask.h"
161 }
162 if (out_mask_c == "") {
163 out_mask_c = "platid_mask.c"
164 }
165
166 printf("files: %s %s %s\n", out_h, out_mask_h, out_mask_c) > stderr
167
168 gen_header(out_h)
169 gen_header(out_mask_h)
170 gen_header(out_mask_c)
171 printf("#include <machine/platid.h>\n") > out_mask_c
172 }
173
174 /^[ \t]*$/ {
175 next
176 }
177 /\/\*/ {
178 commentout = 1
179 next
180 }
181 /\*\// {
182 commentout = 0
183 next
184 }
185 /:/{
186 if (commentout) {
187 next
188 }
189 if (nest != 0) {
190 error()
191 }
192 if (saved_name == "CPU") {
193 mode = CPU
194 } else
195 if (saved_name == "MACHINE") {
196 mode = MACH
197 } else {
198 error()
199 }
200 saved_name = ""
201 next
202 }
203 /=/{
204 if (commentout) {
205 next
206 }
207 if (saved_name == "CPU") {
208 change_cpu = 1
209 } else {
210 error()
211 }
212 saved_name = ""
213 next
214 }
215 /\?/{
216 if (commentout) {
217 next
218 }
219 if (change_cpu) {
220 saved_cpu = ""
221 change_cpu = 0
222 } else {
223 error()
224 }
225 next
226 }
227 /{/{
228 if (commentout) {
229 next
230 }
231 flushname()
232
233 nms[nest] = nm
234 enums[nest] = enumerator[mode]
235 names[nest] = saved_name
236 cpus[nest] = saved_cpu
237
238 if (nest == 2) {
239 } else
240 if (nest == 0) {
241 nm = sprintf("%s_", saved_name)
242 } else {
243 nm = sprintf("%s%s_", nm, saved_name)
244 }
245 nest++
246
247 mode_check()
248 enumerator[mode] = 0
249 saved_name = ""
250
251 next
252 }
253 /}/{
254 if (commentout) {
255 next
256 }
257 flushname()
258 saved_name = ""
259 --nest
260 nm = nms[nest]
261 enumerator[mode] = enums[nest]
262 saved_cpu = cpus[nest]
263 next
264 }
265 /[a-zA-Z0-9]*/{
266 if (commentout) {
267 next
268 }
269 if (change_cpu) {
270 saved_cpu = $1
271 change_cpu = 0
272 next
273 }
274
275 flushname()
276 saved_name = toupper($1)
277 next
278 }
279 {
280 printf("SYNTAX ERROR: %s\n", $0) > stderr
281 }
282