996
|
1 /*****************************************************************************
|
|
2
|
|
3 NAME
|
|
4 oil.y -- compiler for Optimizer Idiom Language files
|
|
5
|
|
6 LICENSE TERMS
|
|
7 Copyright (C) 2007 Alex Smith
|
|
8
|
|
9 This program is free software; you can redistribute it and/or modify
|
|
10 it under the terms of the GNU General Public License as published by
|
|
11 the Free Software Foundation; either version 2 of the License, or
|
|
12 (at your option) any later version.
|
|
13
|
|
14 This program is distributed in the hope that it will be useful,
|
|
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 GNU General Public License for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with this program; if not, write to the Free Software
|
|
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
22
|
|
23 ***************************************************************************/
|
|
24
|
|
25 %{
|
|
26
|
|
27 #include "bconfig.h"
|
|
28 #include <stdio.h>
|
|
29 #include <stdlib.h>
|
|
30 #include <string.h>
|
|
31 #include <ctype.h>
|
|
32
|
|
33 /* Autoconf lets us know if we have _Bool available, usually. On DJGPP there's
|
|
34 an occasional bug that I haven't managed to reproduce that leaves
|
|
35 SIZEOF_BOOL as the null string, so detect that ick_first. */
|
|
36 #if 100 - SIZEOF__BOOL - 1 == 101
|
|
37 /* The bug happened, so play safe. */
|
|
38 typedef int mybool;
|
|
39 #else
|
|
40 # if SIZEOF__BOOL > 0
|
|
41 typedef _Bool mybool;
|
|
42 # else
|
|
43 typedef int mybool;
|
|
44 # endif
|
|
45 #endif
|
|
46
|
|
47 /* Define strdup if it isn't available */
|
|
48 #ifndef HAVE_STRDUP
|
|
49 char* strdup(const char* s)
|
|
50 {
|
|
51 char* t=malloc(strlen(s)+1);
|
|
52 if(t) strcpy(t,s);
|
|
53 return t;
|
|
54 }
|
|
55 #endif
|
|
56
|
|
57 /*
|
|
58 #define YYDEBUG 1
|
|
59 int yydebug=1;
|
|
60 */
|
|
61
|
|
62 /* Each semantic value represents either a constraint on a node that needs to
|
|
63 be true for the optimization template to match, or part of the replacement
|
|
64 for the optimization template. */
|
|
65 struct ickstype
|
|
66 {
|
|
67 unsigned depth; /* The number of child nodes this is from the root */
|
|
68 unsigned long path; /* Bits from the end of this number represent the
|
|
69 sequence of lvals (=0) or rvals (=1) this is from the
|
|
70 root; depth bits from the end are relevant, and the
|
|
71 bit nearest the root is least significant */
|
|
72 const char* condition; /* Points to a constant string; the condition to generate
|
|
73 on that node. Could also point to something the lexer
|
|
74 mallocs, but then the lexer has to keep track of being
|
|
75 able to free it again. If this is NULL, it means that
|
|
76 there isn't a condition other than maybe mustbemesh */
|
|
77 const char* nodetypename; /* The name of the type of node this condition matches,
|
|
78 or NULL for a LEXERLEAF */
|
|
79 mybool usec; /* Whether to append the value of c to the condition */
|
|
80 mybool mustbemesh; /* Must this node be a mesh or mesh32? */
|
|
81 mybool cxneeded; /* True means calculate c and x for the node and the
|
|
82 condition is on those; false means append 'condition'
|
|
83 to the node itself to form the condition and c and x
|
|
84 aren't needed */
|
|
85 mybool width32; /* Generate a 32-bit node? */
|
|
86 mybool ublo; /* Is this a UBLO (if set, generate conditions
|
|
87 to check width)? */
|
|
88 unsigned long c; /* The value to append to the condition */
|
|
89 int replnum; /* Number of this group for replacements */
|
|
90 struct ickstype *n1;/* n1 and n2 are pointers to other conditions that */
|
|
91 struct ickstype *n2;/* also have to be satisified */
|
|
92 };
|
|
93
|
|
94 #define YYSTYPE YYSTYPE
|
|
95
|
|
96 #define MAXOPTNAMELEN 64
|
|
97 char optname[MAXOPTNAMELEN]="undefined";
|
|
98
|
|
99 typedef struct ickstype *YYSTYPE;
|
|
100
|
|
101 void splitend(void);
|
|
102 void splitstart(void);
|
|
103
|
|
104 void treedepthup(YYSTYPE, mybool);
|
|
105 void treefree(YYSTYPE);
|
|
106 void gennodepath(unsigned, unsigned long);
|
|
107 mybool treeshapecond(YYSTYPE, mybool);
|
|
108 YYSTYPE treenscheck(YYSTYPE, YYSTYPE, int);
|
|
109 void treecxcond(YYSTYPE);
|
|
110 void treerepcount(YYSTYPE, int*);
|
|
111 void treerepgen(YYSTYPE, YYSTYPE*, int*);
|
|
112
|
|
113 int countgetchar(void);
|
|
114 int countungetc(int, FILE*);
|
|
115 int cgccol;
|
|
116 int cgcrow;
|
|
117
|
|
118 /* #defines for chaining together template expressions; here, s is the type
|
|
119 of expression (e.g. select, bitwise and, unary and) that's chaining the
|
|
120 expressions together and n is the nonterminal that's the rval */
|
|
121 #define BINARYEXPR(s,m,l,r,w) do{ \
|
|
122 m=malloc(sizeof(struct ickstype)); \
|
|
123 m->n1=l; \
|
|
124 m->n2=r; \
|
|
125 m->usec=0; \
|
|
126 m->condition="->opcode==" s; \
|
|
127 m->nodetypename=s; \
|
|
128 m->mustbemesh=0; \
|
|
129 m->cxneeded=0; \
|
|
130 m->depth=0; \
|
|
131 m->path=0; \
|
|
132 m->replnum=0; \
|
|
133 m->width32=w; \
|
|
134 m->ublo=0; \
|
|
135 treedepthup(m->n1,0); \
|
|
136 treedepthup(m->n2,1); \
|
|
137 } while(0)
|
|
138
|
|
139 #define UNARYEXPR(s,m,r,w,u) do{ \
|
|
140 m=malloc(sizeof(struct ickstype)); \
|
|
141 m->n1=0; \
|
|
142 m->n2=r; \
|
|
143 m->usec=0; \
|
|
144 m->condition="->opcode==" s; \
|
|
145 m->nodetypename=s; \
|
|
146 m->mustbemesh=0; \
|
|
147 m->cxneeded=0; \
|
|
148 m->depth=0; \
|
|
149 m->path=0; \
|
|
150 m->replnum=0; \
|
|
151 m->width32=w; \
|
|
152 m->ublo=u; \
|
|
153 treedepthup(m->n2,1); \
|
|
154 } while(0)
|
|
155
|
|
156 /* Error handling and lexing */
|
|
157 int yylex(void);
|
|
158 int yyerror(char const *);
|
|
159
|
|
160 /* Split the output file */
|
|
161 #define SPLITMAX 20
|
|
162 int splitcount=SPLITMAX;
|
|
163 int filenumber=0;
|
|
164 mybool inloop=0;
|
|
165 %}
|
|
166
|
|
167 /* Various conditions can come out from the lexer. The most common is a char,
|
|
168 which represents that C or INTERCAL operation in a template. Non-character
|
|
169 tokens are used for the various possible leaves in a template, though:
|
|
170 .1 A 16-bit expression (number 1, for replacements)
|
|
171 :1 A 32-bit expression (number 1, for replacements)
|
|
172 _1 An expression of any width (again, number 1 for replacements)
|
|
173 #1 A constant with the value 1
|
|
174 #{x==1}3 A constant which equals 1 (number 3, for replacements)
|
|
175 Note that in the last example, the expression is written strictly in C;
|
|
176 for instance,
|
|
177 #{ick_iselect(x,x)==1}4
|
|
178 would select a constant that's a power of 2 (ick_iselect is the C name for the
|
|
179 INTERCAL operation 'select').
|
|
180 .{c&0xfffffffe==0}5 A 16-bit expression (number 5, for replacements) which
|
|
181 has been analysed to not possibly have any bits other than the least
|
|
182 significant set (c is here the list of all potentially set bits)
|
|
183 All of these come out as correctly set LEXERLEAFs.
|
|
184
|
|
185 Expressions with identical numbers must be node-for-node identical, except
|
|
186 for number 0 (which is like _ in Prolog, it means 'can be anything and
|
|
187 disregard the value). This holds true even if they have different sigils.
|
|
188 Expressions can use each other's c and x values with the notation c5, x4,
|
|
189 and so on; replacement numbers are limited to 1 digit.
|
|
190
|
|
191 The other things that can come out from the lexer are sparks, ears, and
|
|
192 parentheses (any mix, we're not fussy). */
|
|
193
|
|
194 %token LEXERLEAF
|
|
195
|
|
196 %%
|
|
197
|
|
198 input: /**/ | input optimization ;
|
|
199
|
|
200 optimization: template '-' '>' replacement
|
|
201 {
|
|
202 static YYSTYPE tempmem[10];
|
|
203 static int replcount[10];
|
|
204 /* Handle splitting the file. */
|
|
205 if(splitcount) splitcount--;
|
|
206 if(!splitcount && !inloop)
|
|
207 {
|
|
208 splitcount=SPLITMAX;
|
|
209 splitend();
|
|
210 ++filenumber;
|
|
211 splitstart();
|
|
212 }
|
|
213 /* This is where we actually generate the optimizer code. */
|
|
214 /* Tree-shape and is-constant conditions */
|
|
215 printf(" checknodeactbits(np);\n");
|
|
216 if(treeshapecond($1,1)) printf(" if(1"); printf(")\n do\n {\n");
|
|
217 /* Nodesame and cxdata conditions */
|
|
218 {
|
|
219 int i=10;
|
|
220 YYSTYPE temp;
|
|
221 while(--i)
|
|
222 {
|
|
223 temp=treenscheck($1,0,i);
|
|
224 if(temp)
|
|
225 {
|
|
226 printf(" x%d=np",i);
|
|
227 gennodepath(temp->depth,temp->path);
|
|
228 printf("->constant; c%d=np",i);
|
|
229 gennodepath(temp->depth,temp->path);
|
|
230 printf("->optdata;\n");
|
|
231 }
|
|
232 tempmem[i]=temp;
|
|
233 replcount[i]=0; /* we need to zero this somewhere, may as well be here */
|
|
234 }
|
|
235 treecxcond($1);
|
|
236 }
|
|
237 /* If we reach this point in the generated code, we have an optimizer
|
|
238 template match. */
|
|
239 printf(" OPTING(%s);\n",optname);
|
|
240
|
|
241 /* We now need to replace np with its replacement. This is done by creating
|
|
242 a new nodetree, copying across tempmem'd nodes where necessary, and then
|
|
243 substituting one for the other. (This is an inefficient but general way
|
|
244 to do this.) One special case is needed; because pointers into the root
|
|
245 node need to continue pointing there, the temporary node tp is copied
|
|
246 member-for-member and then freed again. To make coding optimizations
|
|
247 easier, the root stays as the same width no matter what. */
|
|
248 printf(" tp=newnode();\n");
|
|
249 treerepcount($4,replcount);
|
|
250 treerepgen($4,tempmem,replcount);
|
|
251 printf(" nodefree(np->lval); nodefree(np->rval);\n");
|
|
252 printf(" tempw=np->width; *np=*tp; np->width=tempw; free(tp);\n");
|
|
253 printf(" } while(0);\n\n");
|
|
254 /* Free the template and replacement now they're finished being used. */
|
|
255 treefree($1);
|
|
256 treefree($4);
|
|
257 }
|
|
258 | '<' LEXERLEAF '-' LEXERLEAF
|
|
259 {
|
|
260 if(!$2->mustbemesh||!$4->mustbemesh)
|
|
261 {
|
|
262 yyerror("syntax error in <#..#()->()> construct");
|
|
263 free($2); free($4);
|
|
264 YYERROR;
|
|
265 }
|
|
266 printf(" r=%luLU; while(r<=%luLU) {\n",$2->c,$4->c);
|
|
267 free($2); free($4);
|
|
268 inloop=1;
|
|
269 }
|
|
270 | '>' {printf(" r++;\n }\n"); inloop=0;};
|
|
271
|
|
272 template: expr3 ;
|
|
273
|
|
274 expr3: '(' expr2 ')' {$$=$2;}
|
|
275 | '"' expr2 '"' {$$=$2;}
|
|
276 | '\'' expr2 '\'' {$$=$2;}
|
|
277
|
|
278 expr: expr3
|
|
279 | LEXERLEAF ;
|
|
280
|
|
281 expr2: expr '$' expr {BINARYEXPR("MINGLE",$$,$1,$3,1);}
|
|
282 | expr '~' expr {BINARYEXPR("SELECT",$$,$1,$3,1);}
|
|
283 | expr '~' '1' '6' expr {BINARYEXPR("SELECT",$$,$1,$5,0);}
|
|
284 | expr '~' '3' '2' expr {BINARYEXPR("SELECT",$$,$1,$5,1);}
|
|
285 | '&' '1' '6' expr {UNARYEXPR("AND",$$,$4,0,1);}
|
|
286 | '&' '3' '2' expr {UNARYEXPR("AND",$$,$4,1,1);}
|
|
287 | 'V' '1' '6' expr {UNARYEXPR("OR",$$,$4,0,1);}
|
|
288 | 'V' '3' '2' expr {UNARYEXPR("OR",$$,$4,1,1);}
|
|
289 | '?' '1' '6' expr {UNARYEXPR("XOR",$$,$4,0,1);}
|
|
290 | '?' '3' '2' expr {UNARYEXPR("XOR",$$,$4,1,1);}
|
|
291 | '^' '1' '6' expr {UNARYEXPR("FIN",$$,$4,0,1);}
|
|
292 | '^' '3' '2' expr {UNARYEXPR("FIN",$$,$4,1,1);}
|
|
293 | '@' '1' '6' expr {UNARYEXPR("WHIRL",$$,$4,0,1);}
|
|
294 | '@' '2' '1' '6' expr {UNARYEXPR("WHIRL2",$$,$5,0,1);}
|
|
295 | '@' '3' '1' '6' expr {UNARYEXPR("WHIRL3",$$,$5,0,1);}
|
|
296 | '@' '4' '1' '6' expr {UNARYEXPR("WHIRL4",$$,$5,0,1);}
|
|
297 | '@' '5' '1' '6' expr {UNARYEXPR("WHIRL5",$$,$5,0,1);}
|
|
298 | '@' '3' '2' expr {UNARYEXPR("WHIRL",$$,$4,1,1);}
|
|
299 | '@' '2' '3' '2' expr {UNARYEXPR("WHIRL2",$$,$5,1,1);}
|
|
300 | '@' '3' '3' '2' expr {UNARYEXPR("WHIRL3",$$,$5,1,1);}
|
|
301 | '@' '4' '3' '2' expr {UNARYEXPR("WHIRL4",$$,$5,1,1);}
|
|
302 | '@' '5' '3' '2' expr {UNARYEXPR("WHIRL5",$$,$5,1,1);}
|
|
303 | expr '&' expr {BINARYEXPR("C_AND",$$,$1,$3,1);}
|
|
304 | expr '&' '1' '6' expr {BINARYEXPR("C_AND",$$,$1,$5,0);}
|
|
305 | expr '&' '3' '2' expr {BINARYEXPR("C_AND",$$,$1,$5,1);}
|
|
306 | expr '|' expr {BINARYEXPR("C_OR",$$,$1,$3,1);}
|
|
307 | expr '|' '1' '6' expr {BINARYEXPR("C_OR",$$,$1,$5,0);}
|
|
308 | expr '|' '3' '2' expr {BINARYEXPR("C_OR",$$,$1,$5,1);}
|
|
309 | expr '^' expr {BINARYEXPR("C_XOR",$$,$1,$3,1);}
|
|
310 | expr '^' '1' '6' expr {BINARYEXPR("C_XOR",$$,$1,$5,0);}
|
|
311 | expr '^' '3' '2' expr {BINARYEXPR("C_XOR",$$,$1,$5,1);}
|
|
312 | expr '+' expr {BINARYEXPR("C_PLUS",$$,$1,$3,1);}
|
|
313 | expr '+' '1' '6' expr {BINARYEXPR("C_PLUS",$$,$1,$5,0);}
|
|
314 | expr '+' '3' '2' expr {BINARYEXPR("C_PLUS",$$,$1,$5,1);}
|
|
315 | expr '-' expr {BINARYEXPR("C_MINUS",$$,$1,$3,1);}
|
|
316 | expr '-' '1' '6' expr {BINARYEXPR("C_MINUS",$$,$1,$5,0);}
|
|
317 | expr '-' '3' '2' expr {BINARYEXPR("C_MINUS",$$,$1,$5,1);}
|
|
318 | expr '*' expr {BINARYEXPR("C_TIMES",$$,$1,$3,1);}
|
|
319 | expr '*' '1' '6' expr {BINARYEXPR("C_TIMES",$$,$1,$5,0);}
|
|
320 | expr '*' '3' '2' expr {BINARYEXPR("C_TIMES",$$,$1,$5,1);}
|
|
321 | expr '/' expr {BINARYEXPR("C_DIVIDEBY",$$,$1,$3,1);}
|
|
322 | expr '/' '1' '6' expr {BINARYEXPR("C_DIVIDEBY",$$,$1,$5,0);}
|
|
323 | expr '/' '3' '2' expr {BINARYEXPR("C_DIVIDEBY",$$,$1,$5,1);}
|
|
324 | expr '%' expr {BINARYEXPR("C_MODULUS",$$,$1,$3,1);}
|
|
325 | expr '%' '1' '6' expr {BINARYEXPR("C_MODULUS",$$,$1,$5,0);}
|
|
326 | expr '%' '3' '2' expr {BINARYEXPR("C_MODULUS",$$,$1,$5,1);}
|
|
327 | expr '>' expr {BINARYEXPR("C_GREATER",$$,$1,$3,1);}
|
|
328 | expr '>' '1' '6' expr {BINARYEXPR("C_GREATER",$$,$1,$5,0);}
|
|
329 | expr '>' '3' '2' expr {BINARYEXPR("C_GREATER",$$,$1,$5,1);}
|
|
330 | expr '<' expr {BINARYEXPR("C_LESS",$$,$1,$3,1);}
|
|
331 | expr '<' '1' '6' expr {BINARYEXPR("C_LESS",$$,$1,$5,0);}
|
|
332 | expr '<' '3' '2' expr {BINARYEXPR("C_LESS",$$,$1,$5,1);}
|
|
333 | '~' '1' '6' expr {UNARYEXPR("C_NOT",$$,$4,0,1);}
|
|
334 | '~' '3' '2' expr {UNARYEXPR("C_NOT",$$,$4,1,1);}
|
|
335 | expr '!' '=' expr {BINARYEXPR("C_NOTEQUAL",$$,$1,$4,0);}
|
|
336 | expr '!' '=' '1' '6' expr {BINARYEXPR("C_NOTEQUAL",$$,$1,$6,0);}
|
|
337 | expr '!' '=' '3' '2' expr {BINARYEXPR("C_NOTEQUAL",$$,$1,$6,1);}
|
|
338 | expr '=' '=' expr {BINARYEXPR("C_ISEQUAL",$$,$1,$4,0);}
|
|
339 | expr '=' '=' '1' '6' expr {BINARYEXPR("C_ISEQUAL",$$,$1,$6,0);}
|
|
340 | expr '=' '=' '3' '2' expr {BINARYEXPR("C_ISEQUAL",$$,$1,$6,1);}
|
|
341 | expr '&' '&' expr {BINARYEXPR("C_LOGICALAND",$$,$1,$4,0);}
|
|
342 | expr '&' '&' '1' '6' expr {BINARYEXPR("C_LOGICALAND",$$,$1,$6,0);}
|
|
343 | expr '&' '&' '3' '2' expr {BINARYEXPR("C_LOGICALAND",$$,$1,$6,1);}
|
|
344 | expr '|' '|' expr {BINARYEXPR("C_LOGICALOR",$$,$1,$4,0);}
|
|
345 | expr '|' '|' '1' '6' expr {BINARYEXPR("C_LOGICALOR",$$,$1,$6,0);}
|
|
346 | expr '|' '|' '3' '2' expr {BINARYEXPR("C_LOGICALOR",$$,$1,$6,1);}
|
|
347 | expr '>' '>' expr {BINARYEXPR("C_RSHIFTBY",$$,$1,$4,1);}
|
|
348 | expr '>' '>' '1' '6' expr {BINARYEXPR("C_RSHIFTBY",$$,$1,$6,0);}
|
|
349 | expr '>' '>' '3' '2' expr {BINARYEXPR("C_RSHIFTBY",$$,$1,$6,1);}
|
|
350 | expr '<' '<' expr {BINARYEXPR("C_LSHIFTBY",$$,$1,$4,1);}
|
|
351 | expr '<' '<' '1' '6' expr {BINARYEXPR("C_LSHIFTBY",$$,$1,$6,0);}
|
|
352 | expr '<' '<' '3' '2' expr {BINARYEXPR("C_LSHIFTBY",$$,$1,$6,1);}
|
|
353 | '!' expr {UNARYEXPR("C_LOGICALNOT",$$,$2,0,0);}
|
|
354 | '!' '1' '6' expr {UNARYEXPR("C_LOGICALNOT",$$,$4,0,0);}
|
|
355 | '!' '3' '2' expr {UNARYEXPR("C_LOGICALNOT",$$,$4,1,0);}
|
|
356 | expr ;
|
|
357
|
|
358 replacement: expr3;
|
|
359
|
|
360 %%
|
|
361
|
|
362 #define MAXTOFREE 1000
|
|
363
|
|
364 char* tofree[MAXTOFREE]={0};
|
|
365 int tfi=0;
|
|
366
|
|
367 int yylex(void)
|
|
368 {
|
|
369 int c;
|
|
370 unsigned long acc;
|
|
371
|
|
372 /* Whitespace is completely insignificant here, even inside && and other
|
|
373 two-character operators. Just to be different, though, it /is/ significant
|
|
374 inside constructs like .1 and #{1}2; in such cases, it isn't allowed. */
|
|
375 c=countgetchar();
|
|
376 while(isspace(c)) c=countgetchar();
|
|
377 while(c==';'||c=='[')
|
|
378 {
|
|
379 /* Comments go from a semicolon/hybrid to the end of the line. */
|
|
380 if(c==';')
|
|
381 {
|
|
382 c=countgetchar();
|
|
383 while(c!='\n') c=countgetchar();
|
|
384 while(isspace(c)) c=countgetchar();
|
|
385 }
|
|
386 /* Square brackets set the name for optimizations. */
|
|
387 if(c=='[')
|
|
388 {
|
|
389 int i=0;
|
|
390 c=countgetchar();
|
|
391 while(c!=']')
|
|
392 {
|
|
393 optname[i++]=c;
|
|
394 c=countgetchar();
|
|
395 if(i==MAXOPTNAMELEN-1) {i=0; yyerror("optimization name too long");}
|
|
396 }
|
|
397 optname[i]=0;
|
|
398 c=countgetchar();
|
|
399 while(isspace(c)) c=countgetchar();
|
|
400 }
|
|
401 }
|
|
402 if(c==EOF) return 0;
|
|
403 switch(c)
|
|
404 {
|
|
405 case '#':
|
|
406 c=countgetchar();
|
|
407 if(c!='{')
|
|
408 {
|
|
409 acc=0;
|
|
410 while(isdigit(c))
|
|
411 {
|
|
412 acc*=10;
|
|
413 acc+=(c-'0');
|
|
414 c=countgetchar();
|
|
415 }
|
|
416 yylval=malloc(sizeof(struct ickstype));
|
|
417 yylval->depth=0;
|
|
418 yylval->path=0;
|
|
419 yylval->condition="->constant==";
|
|
420 yylval->nodetypename=0;
|
|
421 yylval->usec=1;
|
|
422 yylval->mustbemesh=1;
|
|
423 yylval->cxneeded=0;
|
|
424 yylval->c=acc;
|
|
425 yylval->replnum=0;
|
|
426 yylval->n1=0;
|
|
427 yylval->n2=0;
|
|
428 yylval->ublo=0;
|
|
429 yylval->width32=1; /* generate MESH32 not MESH; we can still AND16 it,
|
|
430 etc., if necessary */
|
|
431 countungetc(c, stdin);
|
|
432 return LEXERLEAF;
|
|
433 }
|
|
434 countungetc(c, stdin);
|
|
435 c='#';
|
|
436 /* fall through */
|
|
437 case '_':
|
|
438 case ':':
|
|
439 case '.':
|
|
440 yylval=malloc(sizeof(struct ickstype));
|
|
441 yylval->depth=0;
|
|
442 yylval->path=0;
|
|
443 yylval->condition=0; /* _ or # */
|
|
444 yylval->width32=1; /* should never matter, but you never
|
|
445 know... */
|
|
446 yylval->ublo=0;
|
|
447 if(c==':') yylval->condition="->width==32";
|
|
448 if(c=='.') {yylval->condition="->width==16"; yylval->width32=0;}
|
|
449 yylval->nodetypename=0;
|
|
450 yylval->usec=0;
|
|
451 yylval->mustbemesh=c=='#';
|
|
452 yylval->cxneeded=0;
|
|
453 c=countgetchar();
|
|
454 if(c=='{')
|
|
455 {
|
|
456 /* Create a new node to hold the c/x condition */
|
|
457 yylval->n1=malloc(sizeof(struct ickstype));
|
|
458 yylval->n1->depth=0;
|
|
459 yylval->n1->path=0;
|
|
460 {
|
|
461 static char buf[512];
|
|
462 int bi=0;
|
|
463 c=countgetchar();
|
|
464 while(c!='}')
|
|
465 {
|
|
466 buf[bi++]=c;
|
|
467 if(bi==511) {yyerror("{quoted} string too long"); bi=0;}
|
|
468 c=countgetchar();
|
|
469 }
|
|
470 buf[bi]=0;
|
|
471 yylval->n1->condition=tofree[tfi++]=strdup(buf);
|
|
472 if(tfi==MAXTOFREE) {yyerror("Too many {quoted} strings"); tfi--;}
|
|
473 c=countgetchar();
|
|
474 }
|
|
475 yylval->n1->nodetypename=0;
|
|
476 yylval->n1->usec=0;
|
|
477 yylval->n1->mustbemesh=0;
|
|
478 yylval->n1->cxneeded=1;
|
|
479 yylval->n1->n1=0;
|
|
480 yylval->n1->n2=0;
|
|
481 yylval->n1->width32=yylval->width32;
|
|
482 yylval->n1->ublo=0;
|
|
483 }
|
|
484 else yylval->n1=0;
|
|
485 yylval->replnum=0;
|
|
486 if(yylval->n1) yylval->n1->replnum=c-'0'; else yylval->replnum=c-'0';
|
|
487 yylval->n2=0;
|
|
488 return LEXERLEAF;
|
|
489 default:
|
|
490 return c;
|
|
491 }
|
|
492 }
|
|
493
|
|
494 void treedepthup(YYSTYPE v, mybool i)
|
|
495 {
|
|
496 if(!v) return;
|
|
497 treedepthup(v->n1,i);
|
|
498 treedepthup(v->n2,i);
|
|
499 v->depth++;
|
|
500 v->path<<=1;
|
|
501 v->path|=i;
|
|
502 if(v->depth>30) yyerror("Nesting too deep in template or replacement\n");
|
|
503 }
|
|
504
|
|
505 void treefree(YYSTYPE v)
|
|
506 {
|
|
507 if(!v) return;
|
|
508 treefree(v->n1);
|
|
509 treefree(v->n2);
|
|
510 free(v);
|
|
511 }
|
|
512
|
|
513 void gennodepath(unsigned depth, unsigned long path)
|
|
514 {
|
|
515 while(depth--)
|
|
516 {
|
|
517 if(path&1) printf("->rval"); else printf("->lval");
|
|
518 path>>=1;
|
|
519 }
|
|
520 }
|
|
521
|
|
522 mybool treeshapecond(YYSTYPE v, mybool firstopt)
|
|
523 {
|
|
524 if(!v) return firstopt;
|
|
525 /* To prevent possibly dereferencing a null pointer, check the root ick_first */
|
|
526 if(v->mustbemesh) /* it's a must-be-constant constraint */
|
|
527 {
|
|
528 printf(firstopt?" if((np":" &&\n (np");
|
|
529 gennodepath(v->depth,v->path);
|
|
530 printf("->opcode==MESH || np");
|
|
531 gennodepath(v->depth,v->path);
|
|
532 printf("->opcode==MESH32)");
|
|
533 firstopt=0;
|
|
534 }
|
|
535 if(v->condition&&!v->cxneeded) /* it's a tree-shape constraint */
|
|
536 {
|
|
537 printf(firstopt?" if(np":" &&\n np");
|
|
538 gennodepath(v->depth,v->path);
|
|
539 printf("%s",v->condition);
|
|
540 if(v->usec) printf("%luLU",v->c);
|
|
541 firstopt=0;
|
|
542 }
|
|
543 if(v->ublo) /* generate a width check */
|
|
544 {
|
|
545 printf(firstopt?" if(np":" &&\n np");
|
|
546 gennodepath(v->depth,v->path);
|
|
547 printf("->width==%d",v->width32?32:16);
|
|
548 firstopt=0;
|
|
549 }
|
|
550 firstopt=treeshapecond(v->n1,firstopt);
|
|
551 return treeshapecond(v->n2,firstopt);
|
|
552 }
|
|
553
|
|
554 YYSTYPE treenscheck(YYSTYPE v, YYSTYPE prev, int replnum)
|
|
555 {
|
|
556 if(!v) return prev;
|
|
557 prev=treenscheck(v->n1,prev,replnum);
|
|
558 prev=treenscheck(v->n2,prev,replnum);
|
|
559 if(v->replnum!=replnum) return prev;
|
|
560 if(prev)
|
|
561 {
|
|
562 printf(" if(!nodessame(np");
|
|
563 gennodepath(prev->depth,prev->path);
|
|
564 printf(",np");
|
|
565 gennodepath(v->depth,v->path);
|
|
566 printf(")) break;\n");
|
|
567 }
|
|
568 return v;
|
|
569 }
|
|
570
|
|
571 void treecxcond(YYSTYPE v)
|
|
572 {
|
|
573 if(!v) return;
|
|
574 if(v->cxneeded&&strcmp(v->condition,"1"))
|
|
575 {
|
|
576 if(v->replnum)
|
|
577 printf(" x=x%d; c=c%d; ",v->replnum,v->replnum);
|
|
578 else
|
|
579 {
|
|
580 printf(" x=np");
|
|
581 gennodepath(v->depth,v->path);
|
|
582 printf("->constant;\n c=np");
|
|
583 gennodepath(v->depth,v->path);
|
|
584 printf("->optdata;\n ");
|
|
585 }
|
|
586 printf("if(!(%s)) break;\n",v->condition);
|
|
587 }
|
|
588 treecxcond(v->n1);
|
|
589 treecxcond(v->n2);
|
|
590 }
|
|
591
|
|
592 void treerepcount(YYSTYPE v, int* rc)
|
|
593 {
|
|
594 if(!v) return;
|
|
595 if(!(v->nodetypename)&&v->replnum&&!(v->cxneeded)) rc[v->replnum]++;
|
|
596 treerepcount(v->n1, rc);
|
|
597 treerepcount(v->n2, rc);
|
|
598 }
|
|
599
|
|
600 void treerepgen(YYSTYPE v, YYSTYPE* refs, int* rc)
|
|
601 {
|
|
602 if(!v) return;
|
|
603 /* We absolutely have to generate the root node ick_first here, because otherwise
|
|
604 the nodes in question won't exist. */
|
|
605 if(v->nodetypename) /* Generate an intermediate node */
|
|
606 {
|
|
607 printf(" MAYBENEWNODE(tp");
|
|
608 gennodepath(v->depth,v->path);
|
|
609 printf(");\n tp");
|
|
610 gennodepath(v->depth,v->path);
|
|
611 printf("->opcode=%s;\n tp",v->nodetypename);
|
|
612 gennodepath(v->depth,v->path);
|
|
613 printf("->width=%d;\n",v->width32?32:16);
|
|
614 /* optdata will be filled in by checknodeactbits before the ick_next idiom is
|
|
615 tested; constant is irrelevant, lval and rval are NULL at present and
|
|
616 will be filled in by later recursions of this function, and I seriously
|
|
617 hope that nextslat is never filled in by an optimizer idiom. */
|
|
618 }
|
|
619 else if(v->replnum&&!(v->cxneeded))
|
|
620 {
|
|
621 /* Copy a node from the template. The node ought not to be allocated at
|
|
622 this point, so we can safely just ick_assign to it with a new malloced
|
|
623 node. */
|
|
624 if(refs[v->replnum])
|
|
625 {
|
|
626 if(rc[v->replnum]>1||!refs[v->replnum]->depth)
|
|
627 {
|
|
628 /* The node actually has to be copied, either because another copy is
|
|
629 needed or because it's np itself that's being copied over. */
|
|
630 rc[v->replnum]--;
|
|
631 printf(" tp");
|
|
632 gennodepath(v->depth,v->path);
|
|
633 printf("=nodecopy(np");
|
|
634 gennodepath(refs[v->replnum]->depth,refs[v->replnum]->path);
|
|
635 printf(");\n");
|
|
636 }
|
|
637 else
|
|
638 {
|
|
639 /* This can be optimized slightly by moving rather than copying,
|
|
640 zeroing backlinks so that the node won't be freed. */
|
|
641 rc[v->replnum]--;
|
|
642 printf(" tp");
|
|
643 gennodepath(v->depth,v->path);
|
|
644 printf("=np");
|
|
645 gennodepath(refs[v->replnum]->depth,refs[v->replnum]->path);
|
|
646 printf(";\n np");
|
|
647 gennodepath(refs[v->replnum]->depth,refs[v->replnum]->path);
|
|
648 printf("=0;\n");
|
|
649 }
|
|
650 }
|
|
651 else yyerror("Replacement isn't in the template");
|
|
652 }
|
|
653 else if(v->cxneeded)
|
|
654 {
|
|
655 /* Generate a constant node based on an expression (#{expr}0). */
|
|
656 printf(" MAYBENEWNODE(tp");
|
|
657 gennodepath(v->depth,v->path);
|
|
658 printf(");\n tp");
|
|
659 gennodepath(v->depth,v->path);
|
|
660 printf("->opcode=MESH32;\n tp");
|
|
661 gennodepath(v->depth,v->path);
|
|
662 printf("->width=32;\n tp");
|
|
663 gennodepath(v->depth,v->path);
|
|
664 printf("->constant=(%s);\n",v->condition);
|
|
665 }
|
|
666 else if(v->mustbemesh&&!v->n1)
|
|
667 {
|
|
668 /* Generate a constant node based on a constant (#65535). */
|
|
669 printf(" MAYBENEWNODE(tp");
|
|
670 gennodepath(v->depth,v->path);
|
|
671 printf(");\n tp");
|
|
672 gennodepath(v->depth,v->path);
|
|
673 printf("->opcode=MESH32;\n tp");
|
|
674 gennodepath(v->depth,v->path);
|
|
675 printf("->width=32;\n tp");
|
|
676 gennodepath(v->depth,v->path);
|
|
677 printf("->constant=(%luLU);\n",v->c);
|
|
678 }
|
|
679 else if(v->mustbemesh&&v->n1)
|
|
680 /* let this node's n1 handle it */ ;
|
|
681 else
|
|
682 yyerror("can't figure out how to generate a replacement node\n");
|
|
683 treerepgen(v->n1,refs,rc);
|
|
684 treerepgen(v->n2,refs,rc);
|
|
685 }
|
|
686
|
|
687 int yyerror(char const *s)
|
|
688 {
|
|
689 fprintf(stderr,"Error at (%d:%d): \"%s\"\n",cgcrow,cgccol,s);
|
|
690 return 0; /* this return value is ignored anyway */
|
|
691 }
|
|
692
|
|
693 static int cgcpushback=0;
|
|
694
|
|
695 int countgetchar(void)
|
|
696 {
|
|
697 int c=getchar();
|
|
698 if(cgcpushback) {cgcpushback=0; return c;}
|
|
699 cgccol++;
|
|
700 if(c=='\n') {cgccol=0; cgcrow++;}
|
|
701 return c;
|
|
702 }
|
|
703
|
|
704 int countungetc(int c, FILE* f)
|
|
705 {
|
|
706 ungetc(c,f);
|
|
707 cgcpushback=1;
|
|
708 return c;
|
|
709 }
|
|
710
|
|
711 void splitstart(void)
|
|
712 {
|
|
713 static char fname[]="oilout00.c";
|
|
714 if(filenumber>255)
|
|
715 {
|
|
716 filenumber=255;
|
|
717 fprintf(stdout,"Input file too long.\n");
|
|
718 }
|
|
719 sprintf(fname,"oilout%02x.c",filenumber);
|
|
720 freopen(fname,"w",stdout);
|
|
721 puts("/* Automatically generated output, edit source and recompile to "
|
|
722 "change */");
|
|
723 printf("#include \"oil.h\"\n"
|
|
724 "int optimize_pass1_%x(node *np)\n"
|
|
725 "{"
|
|
726 " int opted=0;\n"
|
|
727 " unsigned long c,c1,c2,c3,c4,c5,c6,c7,c8,c9;\n"
|
|
728 " unsigned long x,x1,x2,x3,x4,x5,x6,x7,x8,x9,r;\n"
|
|
729 " int tempw;\n"
|
|
730 " node *tp;\n", filenumber);
|
|
731 }
|
|
732
|
|
733 void splitend(void)
|
|
734 {
|
|
735 /* Disabling warnings about unused variables. gcc will optimize this right
|
|
736 out, and in any case the raise(SIGSEGV) will be unreachable (but will
|
|
737 cause a pretty recognizable error because it'll be caught by the handler
|
|
738 for SIGSEGV and output an internal error, with an obvious debug backtrace
|
|
739 if -U is used). */
|
|
740 printf(" c=c1=c2=c3=c4=c5=c6=c7=c8=c9=0;\n"
|
|
741 " x=x1=x2=x3=x4=x5=x6=x7=x8=x9=r=0;\n"
|
|
742 " if(c+c1+c2+c3+c4+c5+c6+c7+c8+c9+r+\n"
|
|
743 " x+x1+x2+x3+x4+x5+x6+x7+x8+x9) raise(SIGSEGV);\n");
|
|
744 printf(" return opted;\n}\n");
|
|
745 fclose(stdout);
|
|
746 }
|
|
747
|
|
748 int main(void)
|
|
749 {
|
|
750 int e,i;
|
|
751 /*
|
|
752 " if(!np) return 0;\n"
|
|
753 " if(np->lval) opted|=optimize_pass1(np->lval);\n"
|
|
754 " if(np->rval) opted|=optimize_pass1(np->rval);\n"
|
|
755 */
|
|
756 splitstart();
|
|
757 cgccol=0;
|
|
758 cgcrow=1;
|
|
759 e=yyparse();
|
|
760 while(tfi--) free(tofree[tfi]);
|
|
761 splitend();
|
|
762 freopen("oilout-m.c","w",stdout);
|
|
763 puts("/* Automatically generated output, edit source and recompile to "
|
|
764 "change */");
|
|
765 puts("#include \"ick.h\"");
|
|
766 i=filenumber+1;
|
|
767 while(i--) printf("extern int optimize_pass1_%x(node*);\n",i);
|
|
768 puts("int optimize_pass1(node* np)\n"
|
|
769 "{\n"
|
|
770 " int opted=0;\n"
|
|
771 " if(!np) return 0;\n"
|
|
772 " if(np->lval) opted|=optimize_pass1(np->lval);\n"
|
|
773 " if(np->rval) opted|=optimize_pass1(np->rval);");
|
|
774 i=filenumber+1;
|
|
775 while(i--) printf(" opted|=optimize_pass1_%x(np);\n",i);
|
|
776 puts(" return opted;\n"
|
|
777 "}");
|
|
778 return e;
|
|
779 }
|