1 /* $Id: topgen.g,v 1.7 1994/06/24 10:42:36 ceriel Exp $ */
3 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4 * See the copyright notice in the ACK home directory, in the file "Copyright".
8 * Grammar of optimizer description, and some code generation
11 %token LETTER, DIGIT, OTHER, SPACE;
12 %token LINE_TERMINATOR, OPERAND_SEPARATOR, INSTRUCTION_SEPARATOR,
13 PATTERN_SEPARATOR, OPEN_BRACKET, CLOSE_BRACKET;
15 %start LLparse, optim_description;
23 char idbuf[BUFSIZ], buf[BUFSIZ];
24 int countid; /* # of variables */
25 int countpat; /* # of patterns */
28 static int patlen; /* Maximum number of instructions in pattern */
29 static int maxoperand; /* Maximum number of operands of instruction */
30 extern FILE *input; /* file descriptor of inputfile */
34 { struct symtab *p; } :
35 SPACE* parameter_line*
36 { p = findident("MAXOP",LOOKING,&deftable);
37 if (p == 0) maxoperand = 2; /* default */
38 else maxoperand = p->s_num;
40 separator SPACE* mode_definitions
41 separator SPACE* patterns
44 fprintf(genc, linedir, lineno, inpfile);
45 while ((c = getc(input)) != EOF) {
52 { struct symtab *p;} :
54 { p = findident(idbuf,ENTERING,&deftable);}
57 { p->s_num = atoi(buf);}
58 /* This action in fact only needed for MAXOP */
61 { fprintf(genh,"#define %s %s\n",p->s_name,buf);}
71 | INSTRUCTION_SEPARATOR
78 { *p1++ = dot.t_attrib;}
85 { fputs("tok_chk(varno) {\n\tint r;\n", genc);
86 fputs("\tchar *VAL;\n\n",genc);
87 fputs("\tVAL = var[varno].value;\n",genc);
88 fputs("\tswitch(varno) {\n",genc);
93 { fprintf(genc,linedir,lin,inpfile);
94 fprintf(genc,"\t\tr = (%s); break;\n",buf);
99 { fputs("\tdefault :\n\t\tassert(0);\n",genc);
100 fputs("\t}\n\treturn r;\n}\n\n",genc);
116 { struct symtab *p;} :
118 { p = findident(idbuf,ENTERING,&idtable);
119 p->s_num = ++countid;
120 fprintf(genc,"\tcase %d:\n", countid);
124 constraint (int *lin;)
132 | INSTRUCTION_SEPARATOR
139 { *p++ = dot.t_attrib;}
142 if (onlyspace(buf)) strcpy(buf,"TRUE");
156 fprintf(genc,"struct instr_descr pat%d[] = {\n",
159 instruction_list(&np)
160 { if (np > patlen) patlen = np;
161 fputs("\n};\n\n",genc);
165 { /* Save the constraint, we need it later on */
166 constr = malloc((unsigned)(strlen(buf)+1));
171 { fprintf(genc,"struct instr_descr rep%d[] = {\n",
175 { fputs("\n};\n\n",genc);}
178 { addpattern(constr,lin,np,nr);}
182 fprintf(genh,"#define NRVARS %d\n",countid);
183 fprintf(genh,"#define NRPATTERNS %d\n",countpat);
184 fprintf(genh,"#define MIN_WINDOW_SIZE %d\n",
190 instruction_list(int *n;) :
194 INSTRUCTION_SEPARATOR
195 { fputs(",\n",genc);}
202 instruction(int opt;)
205 { if (strcmp(buf,"ANY") != 0) {
206 fprintf(genc,"\t{\"%s\", {",buf);
208 else fputs("\t{(char *) 0, {",genc);
220 { if (count > maxoperand) {
221 error("Too many operands");
225 { while (count++ < maxoperand) {
226 fprintf(genc,"%c{\"\",-1,\"\"}",count == 1 ? ' ' : ',');
240 { *p++ = dot.t_attrib;}
244 if (opt) addtohashtable(buf,countpat);
249 { register struct symtab *p = 0;} :
250 { fprintf(genc, "%c{\"", c);}
254 p = findident(idbuf,LOOKING,&idtable);
255 if (p) fprintf(genc,"\",%d,\"",p->s_num);
256 else fputs(idbuf,genc);
258 else fputs(idbuf,genc);
261 { putc(dot.t_attrib,genc);}
263 { putc(dot.t_attrib,genc);}
265 { if (p) fputs("\"}",genc);
266 else fputs("\",0,\"\"}",genc);
271 replacement (int *n;)
279 INSTRUCTION_SEPARATOR
280 { fputs(",\n", genc);}
285 | /* empty replacement, but there must be a
286 * structure initializer anyway
288 { fputs("\t{\"\", {",genc);
289 for (i = 0; i < maxoperand; i++) {
290 fprintf(genc, "%c{\"\",-1,\"\"}",i?',':' ');
299 { char *p = idbuf; } :
301 { *p++ = dot.t_attrib;}
303 LETTER { *p++ = dot.t_attrib;}
304 | DIGIT { *p++ = dot.t_attrib;}
310 '%' '%' SPACE* LINE_TERMINATOR