-
Notifications
You must be signed in to change notification settings - Fork 27
/
sample4.brg
109 lines (97 loc) · 2.33 KB
/
sample4.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
%{
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
enum {
ADDI=309, ADDRLP=295, ASGNI=53,
CNSTI=21, CVCI=85, I0I=661, INDIRC=67
};
#define STATE_TYPE long
typedef struct tree {
int op;
struct tree *kids[2];
int val;
struct { STATE_TYPE state; } x;
} *NODEPTR_TYPE, *Tree;
#define LEFT_CHILD(p) ((p)->kids[0])
#define RIGHT_CHILD(p) ((p)->kids[1])
#define PANIC printf
#define STATE_LABEL(p) ((p)->x.state)
int OP_LABEL(NODEPTR_TYPE p) {
switch (p->op) {
case CNSTI: if (p->val == 0) return 661 /* I0I */;
default: return p->op;
}
}
static void burm_trace(NODEPTR_TYPE, int, int, int);
%}
%term ADDI=309 ADDRLP=295 ASGNI=53
%term CNSTI=21 CVCI=85 I0I=661 INDIRC=67
%%
stmt: ASGNI(disp,reg) = 4 (1);
stmt: reg = 5;
reg: ADDI(reg,rc) = 6 (1);
reg: CVCI(INDIRC(disp)) = 7 (1);
reg: I0I = 8;
reg: disp = 9 (1);
disp: ADDI(reg,con) = 10;
disp: ADDRLP = 11;
rc: con = 12;
rc: reg = 13;
con: CNSTI = 14;
con: I0I = 15;
%%
static int trace;
/* burm_trace - print trace message for matching p; decrement trace */
static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) {
if (trace < 0)
fprintf(stderr, "0x%p matched %s = %d with cost %d vs. %d\n", p,
burm_string[eruleno], eruleno, cost, bestcost);
else if (trace > 0 && cost < bestcost) {
--trace;
fprintf(stderr, "0x%p matched %s = %d with cost %d\n", p,
burm_string[eruleno], eruleno, cost);
}
}
/* dumpCover - print the matched cover for p */
static void dumpCover(Tree p, int goalnt, int indent) {
int eruleno = burm_rule(p->x.state, goalnt);
short *nts = burm_nts[eruleno];
Tree kids[10];
int i;
for (i = 0; i < indent; i++)
fprintf(stderr, " ");
fprintf(stderr, "%s\n", burm_string[eruleno]);
burm_kids(p, eruleno, kids);
for (i = 0; nts[i]; i++)
dumpCover(kids[i], nts[i], indent + 1);
}
static void gen(NODEPTR_TYPE p) {
if (burm_label(p) == 0)
fprintf(stderr, "no cover\n");
else
dumpCover(p, 1, 0);
}
static Tree tree(int op, Tree l, Tree r) {
Tree t = malloc(sizeof *t);
t->op = op;
t->kids[0] = l; t->kids[1] = r;
t->val = 0;
t->x.state = 0;
return t;
}
main(void) {
Tree t;
if (getenv("Trace"))
trace = atoi(getenv("Trace"));
printf("i = c + 4;\n");
t = tree(ASGNI,
tree(ADDRLP, 0, 0),
tree(ADDI,
tree(CVCI, tree(INDIRC, tree(ADDRLP, 0, 0), 0), 0),
(t = tree(CNSTI, 0, 0), t->val = 4, t)
)
);
gen(t);
return 0;
}