Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: reggen.l | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | /* | |
24 | * Copyright 2005 Sun Microsystems, Inc. All rights reserved. | |
25 | * Use is subject to license terms. | |
26 | * | |
27 | * ident "@(#)reggen.l 1.5 05/01/12 SMI" | |
28 | */ | |
29 | %{ | |
30 | ||
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <unistd.h> | |
34 | #include <stdarg.h> | |
35 | #include <errno.h> | |
36 | #include <sys/param.h> /* for MAXPATHLEN */ | |
37 | #include <string.h> /* for strchr */ | |
38 | #include <ctype.h> /* for islower / toupper */ | |
39 | ||
40 | ||
41 | #define MAXBUF 1024 | |
42 | ||
43 | void fatal(char *, ...); | |
44 | void strcasecpy(char * top, char * fromp); | |
45 | int yywrap(); | |
46 | ||
47 | FILE * dotc, * doth; | |
48 | char * basenamep; | |
49 | char * structnamep; | |
50 | char * specfilep; | |
51 | ||
52 | int enumidx; | |
53 | int alias; | |
54 | char aliasname[MAXBUF]; | |
55 | ||
56 | %} | |
57 | ||
58 | %% | |
59 | ||
60 | <<EOF>> return 0; | |
61 | ||
62 | [ \t]+ { } | |
63 | ||
64 | \n { | |
65 | if (alias > 0) { | |
66 | alias = 0; | |
67 | enumidx++; | |
68 | } | |
69 | } | |
70 | ||
71 | [A-Za-z][A-Za-z0-9]* { | |
72 | if (!alias) { | |
73 | sprintf(aliasname, "Reg_%s_%s", structnamep, yytext); | |
74 | fprintf(doth,"\t%s = %d, \n", aliasname, enumidx); | |
75 | fprintf(dotc,"\t{ \"%s\", Reg_%s_%s },\n", yytext, structnamep, yytext); | |
76 | } else { | |
77 | fprintf(doth,"\t\tReg_%s_%s = %s,\n", | |
78 | structnamep, yytext, aliasname); | |
79 | fprintf(dotc,"\t{ \"%s\", %s },\n", yytext, aliasname); | |
80 | } | |
81 | ||
82 | alias ++; | |
83 | } | |
84 | ||
85 | #.* { /* swallow comments */ } | |
86 | ||
87 | . { | |
88 | fatal("Illegal spec text %s", yytext); | |
89 | } | |
90 | ||
91 | ||
92 | %% | |
93 | ||
94 | int main(int argc, char ** argv) | |
95 | { | |
96 | char bufp[MAXPATHLEN]; | |
97 | char caps[MAXPATHLEN]; | |
98 | char * bnamep; | |
99 | ||
100 | if (argc != 3) fatal("usage: %s <basefilename> <basestructname>\n", | |
101 | argv[0]); | |
102 | ||
103 | basenamep = argv[1]; | |
104 | structnamep = argv[2]; | |
105 | if (strlen(structnamep)>MAXBUF-100) fatal("basestructname is too long!"); | |
106 | ||
107 | /* Initialise the .h and .c files */ | |
108 | ||
109 | sprintf(bufp, "%s.c", basenamep); | |
110 | dotc = fopen(bufp, "w"); | |
111 | if (dotc == NULL) fatal("creating %s", bufp); | |
112 | ||
113 | sprintf(bufp, "%s.h", basenamep); | |
114 | doth = fopen(bufp, "w"); | |
115 | if (doth == NULL) fatal("creating %s", bufp); | |
116 | ||
117 | /* remove any earlier path ... */ | |
118 | bnamep = strrchr(basenamep, '/'); | |
119 | if (bnamep == (char*)0) | |
120 | bnamep = basenamep; | |
121 | else | |
122 | bnamep ++; | |
123 | ||
124 | strcasecpy(caps, bnamep); | |
125 | ||
126 | ||
127 | fprintf(dotc,"\ | |
128 | /* Autogenerated file - DO NOT EDIT */\n\ | |
129 | #include \"basics.h\"\n\ | |
130 | #include \"simcore.h\"\n\ | |
131 | #include \"config.h\"\n\ | |
132 | #include \"regmap.h\"\n\ | |
133 | #include \"%s.h\"\n\ | |
134 | \n\ | |
135 | reg_map_t %s_reg_map[]={\n", | |
136 | bnamep, structnamep); | |
137 | ||
138 | ||
139 | fprintf(doth,"\ | |
140 | #ifndef _%s_H_\n\ | |
141 | #define _%s_H_\n\ | |
142 | \n\ | |
143 | typedef enum {\n", caps, caps); | |
144 | ||
145 | alias = 0; | |
146 | enumidx = 0; | |
147 | ||
148 | yylex(); | |
149 | ||
150 | fprintf(dotc,"\n\ | |
151 | \t{ (char *)0, -1 }\n\ | |
152 | };\n" ); | |
153 | ||
154 | fclose(dotc); | |
155 | ||
156 | fprintf(doth,"\ | |
157 | \tReg_%s_TOTAL = %d\n\ | |
158 | } %s_reg_t;\n\ | |
159 | \n\ | |
160 | #endif\n", structnamep, enumidx, structnamep); | |
161 | ||
162 | fclose(doth); | |
163 | exit(0); | |
164 | ||
165 | return 0; /* compiler joy */ | |
166 | } | |
167 | ||
168 | ||
169 | ||
170 | ||
171 | ||
172 | void fatal(char* fmt, ...) | |
173 | { | |
174 | va_list args; | |
175 | ||
176 | if (errno!=0) perror("FATAL: "); else fprintf(stderr,"FATAL: "); | |
177 | if (fmt) { | |
178 | va_start(args, fmt); | |
179 | (void)vfprintf(stderr, fmt, args); | |
180 | ||
181 | va_end(args); | |
182 | } | |
183 | ||
184 | fprintf(stderr,"\n"); | |
185 | fflush(stderr); | |
186 | fflush(stdout); | |
187 | ||
188 | exit(1); | |
189 | } | |
190 | ||
191 | ||
192 | ||
193 | ||
194 | void strcasecpy(char * top, char * fromp) | |
195 | { | |
196 | do { | |
197 | *top++ = islower((unsigned)*fromp) ? toupper((unsigned)*fromp) : *fromp; | |
198 | } while (*fromp++ != '\0'); | |
199 | } | |
200 | ||
201 | ||
202 | ||
203 | int yywrap() | |
204 | { | |
205 | return 1; | |
206 | } | |
207 |