-
Notifications
You must be signed in to change notification settings - Fork 0
/
minor.brg
326 lines (266 loc) · 13.3 KB
/
minor.brg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
%{
/*
* selecção de instruções com postfix
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
#include "tabid.h"
#include "postfix.h"
#include "y.tab.h"
#include "minor.h"
#define CMPVAL 6969
#define EXTMAX 100
#define IFMAX 20
#define FORMAX 20
#define ALLOCMAX 50
#ifndef U_
#define U_ "_"
#endif
#define TRACE
static void yytrace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost);
extern FILE *yyout;
static int ifvec[IFMAX][2]; /* [ { if_end, current_elif }, {...iflvl2...}, ... ] */
static int iflvl = -1;
static int forvec[FORMAX];
static int forlvl = -1;
static char* toalloc[ALLOCMAX];
static int tacnt = 0;
static char* extvec[EXTMAX];
static int extcnt = 0;
int lbl;
int lclvars;
static char *mklbl(int n) {
static char buf[20];
sprintf(buf, "%cL%d", n < 0 ? '.' : '_', n);
return strcpy(malloc(strlen(buf)+1),buf);
}
static char *mkfunc(char *s) {
static char buf[80];
strcpy(buf, "_");
strcat(buf, s);
return buf;
}
static void strtopf(char *s) {
while (*s) fprintf(yyout, pfCHAR, (unsigned char)*s++);
}
static void cmp(Node* n, char *op) {
if (tTYPE(LEFT_CHILD(n)->info) == tSTR)
fprintf(yyout, pfSWAP pfCALL pfTRASH pfPUSH pfIMM, "mstrcmp", pfWORD * 2, 0);
fprintf(yyout, "%s", op);
}
static void globalvar(Node* p) {
int type = p->info;
if (p->attrib == VAR) p = LEFT_CHILD(p);
char* id = LEFT_CHILD(p)->value.s;
Node *val = RIGHT_CHILD(p);
if (type & tFWD) { extvec[extcnt++] = strdup(id); return; }
if (type & tPUB) fprintf(yyout, pfTEXT pfALIGN pfGLOBL, id, pfOBJ);
if (type & tCNST) fprintf(yyout, pfRODATA);
else if (RIGHT_CHILD(p)->type != nodeNil) fprintf(yyout, pfDATA);
else {
fprintf(yyout, pfBSS pfALIGN pfLABEL pfBYTE, id, tTYPE(type) == tINT ? 4 : pfWORD);
return;
}
fprintf(yyout, pfALIGN pfLABEL, id);
if (tTYPE(type) == tINT) fprintf(yyout, pfINTEGER, val->value.i);
else fprintf(yyout, pfID, mklbl(lbl));
for (int i = 0; i < extcnt; i++)
if(extvec[i] && !strcmp(extvec[i], id))
extvec[i] = 0;
}
static void allocvecs() {
for (int i = 0; i < tacnt; i++) {
Node *dim;
int off = IDfind(toalloc[i], (void**)&dim);
if (dim != 0 && dim->attrib == DIM && dim->value.i > 0) {
fprintf(yyout, pfIMM pfALLOC pfSP pfLOCA, dim->value.i * 4, off*-4);
IDreplace(off, toalloc[i], 0);
}
}
}
static void handlevar(char* id, int type, int index) {
int off = IDfind(id, (void**)IDtest);
if (index && tTYPE(type) == tVEC) fprintf(yyout, pfIMM pfMUL, 4);
if(off == -1) fprintf(yyout, pfADDR, id);
else fprintf(yyout, pfLOCAL, off*-4);
if (index) fprintf(yyout, pfLOAD pfADD);
}
static void aritmvecs(Node *n) {
if (tTYPE(n->info) == tVEC) {
if (tTYPE(LEFT_CHILD(n)->info) == tINT)
fprintf(yyout, pfSWAP pfIMM pfMUL pfSWAP, 4);
else if (tTYPE(RIGHT_CHILD(n)->info) == tINT)
fprintf(yyout, pfIMM pfMUL, 4);
}
}
static int noDim(Node *n) { return LEFT_CHILD(RIGHT_CHILD(n))->value.i == 0 ? 1 : CMPVAL; }
static int isFwd(Node *n) { return (RIGHT_CHILD(LEFT_CHILD(n))->value.i & tFWD) ? 1 : CMPVAL; }
static int isFunc(Node *n) { return n->info & tFUNC ? 1 : CMPVAL; }
static int isCnst(Node *n) { return n->info & tCNST ? 1 : CMPVAL; }
static int isInt(Node *n) { return tTYPE(n->info) == tINT ? 1 : CMPVAL; }
static int isStr(Node *n) { return tTYPE(n->info) == tSTR ? 1 : CMPVAL; }
static int leftInt(Node *n) { return tTYPE(LEFT_CHILD(n)->info) == tINT ? 1 : CMPVAL; }
static int leftStr(Node *n) { return tTYPE(LEFT_CHILD(n)->info) == tSTR ? 1 : CMPVAL; }
static int leftZero(Node *n) { return LEFT_CHILD(n)->value.i == 0 ? 1 : CMPVAL; }
static int rightStr(Node *n) { return tTYPE(RIGHT_CHILD(n)->info) == tSTR ? 1 : CMPVAL; }
%}
%term STMT=';' PRINT='!' ALLOC='#' INDEX='[' SCAN='?' CALL='(' PARAM=','
%term ADD='+' SUB='-' MUL='*' DIV='/' MOD='%' POW='^'
%term EQ='=' GT='>' LT='<' AND='&' OR='|' NOT='~'
%include "y.tab.h"
%%
file : PROGRAM(decls, decl) 1 { fprintf(yyout, pfIMM pfPOP pfLEAVE pfRET, 0); }
file : MODULE(decls) 1 {}
fcname : END(ID, INT) 1 { if (RIGHT_CHILD(p)->value.i & tPUB) fprintf(yyout, pfGLOBL, mkfunc(LEFT_CHILD(p)->value.s), pfFUNC);
fprintf(yyout, pfTEXT pfALIGN pfLABEL, mkfunc(LEFT_CHILD(p)->value.s));
for (int i = 0; i < extcnt; i++)
if(extvec[i] && !strcmp(extvec[i], mkfunc(LEFT_CHILD(p)->value.s)))
extvec[i] = 0;
IDpush(); }
fwdname : END(ID, INT) 1 { extvec[extcnt++] = strdup(mkfunc(LEFT_CHILD(p)->value.s)); IDpush(); }
fcv : NIL 1 { lclvars = 0; }
fcv : ARGS(fcv, vardecl) 1 {}
fcvars : fcv 1 { fprintf(yyout, pfENTER, lclvars*4); allocvecs(); }
fcargs : ARGS(fcargs, fcarg) 1 {}
fcargs : NIL 1 { lclvars = -1; }
fcarg : NUMBER(ID, NIL) 1 { IDnew(--lclvars, LEFT_CHILD(p)->value.s, 0); }
fcarg : STRING(ID, NIL) 1 { IDnew(--lclvars, LEFT_CHILD(p)->value.s, 0); }
fcarg : ARRAY(ID, INTS(INT, NIL)) noDim { IDnew(--lclvars, LEFT_CHILD(p)->value.s, 0); }
fcarg : ARRAY(ID, INTS(INT, NIL)) 2 { toalloc[tacnt++] = LEFT_CHILD(p)->value.s; IDnew(--lclvars, LEFT_CHILD(p)->value.s, intNode(DIM, LEFT_CHILD(RIGHT_CHILD(p))->value.i)); }
fcbody : NIL 1 {}
fcbody : body 1 {}
body : START(fcvars, instret) 1 {}
instret : instrs 1 {}
instret : STMT(instrs, ret) 1 {}
%! declarations
decls : NIL 1 {}
decls : DECL(decls, decl) 1 {}
decl : NIL 1 {}
decl : FUNCTION(fcname, FARGS(fcargs, fcbody)) 2 { fprintf(yyout, pfIMM pfPOP pfLEAVE pfRET, 0); IDpop(); }
decl : FUNCTION(fwdname, FARGS(fcargs, fcbody)) isFwd { IDpop(); }
decl : VAR(NUMBER(ID, int)) 1 { globalvar(p); }
decl : VAR(STRING(ID, str)) 2 { globalvar(p); }
decl : VAR(ARRAY(ID, arr)) 2 { globalvar(p); }
decl : VAR(STRING(ID, strcnst)) isCnst { globalvar(p); }
decl : VAR(ARRAY(ID, arrcnst)) isCnst { globalvar(p); }
vardecl : NUMBER(ID, NIL) 1 { IDnew(++lclvars, LEFT_CHILD(p)->value.s, 0); }
vardecl : STRING(ID, NIL) 1 { IDnew(++lclvars, LEFT_CHILD(p)->value.s, 0); }
vardecl : ARRAY(ID, NIL) 1 { IDnew(++lclvars, LEFT_CHILD(p)->value.s, 0); }
vardecl : ARRAY(ID, INTS(INT, NIL)) 1 { toalloc[tacnt++] = LEFT_CHILD(p)->value.s; IDnew(++lclvars, LEFT_CHILD(p)->value.s, intNode(DIM, LEFT_CHILD(RIGHT_CHILD(p))->value.i)); }
int : INT 1 {}
int : NIL 1 {}
arrcnst : NIL 1 {}
arrcnst : INTS(INT, intsc) leftZero {}
arrcnst : INTS(INT, intsc) 2 { fprintf(yyout, pfBYTE, 4*(LEFT_CHILD(p)->value.i - ((int)RIGHT_CHILD(p)->place))); }
intsc : NIL 1 { fprintf(yyout, pfRODATA pfALIGN pfLABEL, mklbl(++lbl)); p->place = 0; }
intsc : INTS(intsc, INT) 1 { fprintf(yyout, pfINTEGER, RIGHT_CHILD(p)->value.i); p->place = LEFT_CHILD(p)->place + 1; }
arr : NIL 1 {}
arr : INTS(INT, ints) leftZero {}
arr : INTS(INT, ints) 2 { fprintf(yyout, pfBYTE, 4*(LEFT_CHILD(p)->value.i - ((int)RIGHT_CHILD(p)->place))); }
ints : NIL 1 { fprintf(yyout, pfDATA pfALIGN pfLABEL, mklbl(++lbl)); p->place = 0; }
ints : INTS(ints, INT) 1 { fprintf(yyout, pfINTEGER, RIGHT_CHILD(p)->value.i); p->place = LEFT_CHILD(p)->place + 1; }
strcnst : charsc 1 { fprintf(yyout, pfCHAR, 0); }
charsc : CHARS(charsc, char) 1 {}
charsc : NIL 1 { fprintf(yyout, pfRODATA pfALIGN pfLABEL, mklbl(++lbl)); }
str : chars 1 { fprintf(yyout, pfCHAR, 0); }
chars : CHARS(chars, char) 1 {}
chars : NIL 1 { fprintf(yyout, pfDATA pfALIGN pfLABEL, mklbl(++lbl)); }
char : INT 1 { fprintf(yyout, pfCHAR, (char) p->value.i); }
char : CHAR 1 { fprintf(yyout, pfCHAR, (char) p->value.i); }
char : STR 1 { strtopf(p->value.s); }
ret : RETURN(NIL) 1 { fprintf(yyout, pfIMM pfPOP pfLEAVE pfRET, 0); }
ret : RETURN(expr) 1 { fprintf(yyout, pfPOP pfLEAVE pfRET); }
ret : NIL 1 {}
instrs : NIL 1 { fprintf(yyout, pfTEXT pfALIGN); }
instrs : STMT(instrs, instr) 1 {}
%! if elif else
ifcond : expr 1 { ifvec[++iflvl][0] = ++lbl; fprintf(yyout, pfJZ, mklbl(ifvec[iflvl][1] = ++lbl));}
eicond : expr 1 { fprintf(yyout, pfJZ, mklbl(ifvec[iflvl][1] = ++lbl)); }
instr : FI(THEN(IF(ifcond, ifbody), elifs), else) 1 { fprintf(yyout, pfLABEL, mklbl(ifvec[iflvl][0])); iflvl--; }
elifs : ELIF(elifs, IF(eicond, ifbody)) 1 {}
elifs : NIL 1 {}
else : STMT(instrs, loop) 1 {}
else : NIL 1 {}
ifbody : STMT(instrs, loop) 1 { fprintf(yyout, pfJMP pfLABEL, mklbl(ifvec[iflvl][0]), mklbl(ifvec[iflvl][1])); }
%! for loop
instr : FOR(UNTIL(frbegin, frcond), STEP(frbody, instr)) 1 { fprintf(yyout, pfJMP pfLABEL, mklbl(LEFT_CHILD(LEFT_CHILD(p))->place), mklbl(forvec[forlvl])); forlvl--; }
frbegin : instr 1 { fprintf(yyout, pfLABEL, mklbl(p->place = ++lbl)); }
frcond : expr 1 { fprintf(yyout, pfJNZ, mklbl(forvec[++forlvl] = ++lbl)); lbl++; }
frbody : STMT(instrs, loop) 1 { fprintf(yyout, pfLABEL, mklbl(forvec[forlvl]+1)); }
loop : REPEAT 1 { fprintf(yyout, pfJMP, mklbl(forvec[forlvl]+1)); }
loop : STOP 1 { fprintf(yyout, pfJMP, mklbl(forvec[forlvl])); }
loop : ret 1 {}
instr : expr 1 { fprintf(yyout, pfTRASH, 4); }
instr : ALLOC(expr, ID) 1 { if (tTYPE(RIGHT_CHILD(p)->info) == tVEC) fprintf(yyout, pfIMM pfMUL, 4);
fprintf(yyout, pfALLOC pfSP);
int off = IDfind(RIGHT_CHILD(p)->value.s, (void**)IDtest);
if (off == -1)
fprintf(yyout, pfADDRA, RIGHT_CHILD(p)->value.s);
else fprintf(yyout, pfLOCA, off*-4); }
instr : PRINT(expr) leftStr { fprintf(yyout, pfCALL pfTRASH pfPUSH, "_prints", pfWORD); }
instr : PRINT(expr) 2 { fprintf(yyout, pfCALL pfTRASH pfPUSH, "_printi", 4); }
lval : ID 1 { handlevar(p->value.s, p->info, 0); }
lval : INDEX(expr, ID) 1 { p->info = tINT; p->place = tTYPE(RIGHT_CHILD(p)->info) == tSTR;
handlevar(RIGHT_CHILD(p)->value.s, RIGHT_CHILD(p)->info, 1); }
lits : CHARS(NIL, INT) 1 { fprintf(yyout, pfIMM, RIGHT_CHILD(p)->value.i); }
lits : CHARS(NIL, CHAR) 1 { fprintf(yyout, pfIMM, RIGHT_CHILD(p)->value.i); }
lits : strcnst 1 { fprintf(yyout, pfTEXT pfALIGN pfADDR, mklbl(lbl)); }
expr : lits 1 {}
expr : ID isFunc { fprintf(yyout, pfCALL pfPUSH, mkfunc(p->value.s)); }
expr : lval 1 { fprintf(yyout, (p->attrib == '[' && tTYPE(RIGHT_CHILD(p)->info) == tSTR) ? pfLDCHR : pfLOAD); }
expr : SCAN 1 { fprintf(yyout, pfCALL pfPUSH, "_readi"); }
expr : ADDR(lval) 1 {}
expr : DEF(rval, lval) 1 { fprintf(yyout, (RIGHT_CHILD(p)->attrib == '[' && tTYPE(RIGHT_CHILD(RIGHT_CHILD(p))->info) == tSTR) ? pfSTCHR : pfSTORE); }
rval : expr 1 { fprintf(yyout, pfDUP); }
expr : CALL(ID, args) 1 { fprintf(yyout, pfCALL pfTRASH pfPUSH, mkfunc(LEFT_CHILD(p)->value.s), (int)RIGHT_CHILD(p)->place * 4); }
args : PARAM(expr, NIL) 1 { p->place = 1; }
args : PARAM(expr, args) 1 { p->place = 1 + RIGHT_CHILD(p)->place; }
expr : ADD(expr, expr) 1 { aritmvecs(p); fprintf(yyout, pfADD); }
expr : SUB(expr, expr) 1 { aritmvecs(p); fprintf(yyout, pfSUB); if (tTYPE(LEFT_CHILD(p)->info) == tVEC && tTYPE(RIGHT_CHILD(p)->info) == tVEC) fprintf(yyout, pfIMM pfDIV, 4); }
expr : MUL(expr, expr) 1 { fprintf(yyout, pfMUL); }
expr : DIV(expr, expr) 1 { fprintf(yyout, pfDIV); }
expr : MOD(expr, expr) 1 { fprintf(yyout, pfMOD); }
expr : POW(expr, expr) 1 { fprintf(yyout, pfCALL pfTRASH pfPUSH, "mpow", 8); }
expr : EQ(expr, expr) 1 { cmp(p, pfEQ); }
expr : NE(expr, expr) 1 { cmp(p, pfNE); }
expr : GE(expr, expr) 1 { cmp(p, pfGE); }
expr : LE(expr, expr) 1 { cmp(p, pfLE); }
expr : GT(expr, expr) 1 { cmp(p, pfGT); }
expr : LT(expr, expr) 1 { cmp(p, pfLT); }
expr : AND(and,expr) 1 { fprintf(yyout, pfLABEL, mklbl(LEFT_CHILD(p)->place)); }
and : expr 1 { p->place = ++lbl; fprintf(yyout, pfDUP pfJZ pfTRASH, mklbl(p->place), pfWORD); }
expr : OR(or,expr) 1 { fprintf(yyout, pfLABEL, mklbl(LEFT_CHILD(p)->place)); }
or : expr 1 { p->place = ++lbl; fprintf(yyout, pfDUP pfJNZ pfTRASH, mklbl(p->place), pfWORD); }
expr : NOT(expr) 1 { fprintf(yyout, pfIMM pfEQ, 0); }
expr : UMINUS(expr) 1 { fprintf(yyout, pfNEG); }
%%
extern char **yynames;
extern int trace, errors, debugNode;
void evaluate(Node *p) {
int i;
if (errors) return;
/* if (trace) */ printNode(p, stdout, yynames);
if (!yyselect(p) && trace) printf("selection successful\n");
extvec[extcnt++] = strdup("_prints");
extvec[extcnt++] = strdup("_printi");
extvec[extcnt++] = strdup("_readi");
extvec[extcnt++] = strdup("mpow");
extvec[extcnt++] = strdup("mstrcmp");
/* puts("\n\nPILHA:\n"); for (i=0; i < extcnt; i++) printf("pilha_elem: %s\n", extvec[i]); */
for (i = 0; i < extcnt; i++) if (extvec[i]) fprintf(yyout, pfEXTRN, extvec[i]);
}
#ifndef NOTRACE
static void yytrace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost)
{
int op = OP_LABEL(p);
YYCONST char *tname = yytermname[op] ? yytermname[op] : "?";
if (debugNode) fprintf(stderr, "0x%p:%s matched %s with cost %d vs. %d\n", p, tname, yystring[eruleno], cost, bestcost);
if (cost >= MAX_COST && bestcost >= MAX_COST) {
fprintf(stderr, "0x%p:%s NO MATCH %s with cost %d vs. %d\n", p, tname, yystring[eruleno], cost, bestcost);
printNode(p, stderr, yynames);
}
}
#endif