-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecorate.c
208 lines (194 loc) · 5.19 KB
/
decorate.c
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
/* This file is part of IRVM.
Copyright (C) 2014 Pablo de Oliveira
IRVM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
IRVM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with IRVM. If not, see <http://www.gnu.org/licenses/>. */
#include <assert.h>
#include "decorate.h"
#include "error.h"
#include "primitives.h"
/* connect all the nodes in a linked list (throught the ->next pointers)
in the order in which they should be evaluated.*/
static struct Node *
_compute_exec_order (struct Node *n, struct Node *prev)
{
switch (n->kind)
{
case T_BINOP:
{
struct Binop *b = container_of (n, struct Binop, node);
struct Node *left = _compute_exec_order (b->left, prev);
struct Node *right = _compute_exec_order (b->right, left);
right->next = n;
return n;
}
case T_MEM:
{
struct Mem *m = container_of (n, struct Mem, node);
struct Node *exp = _compute_exec_order (m->exp, prev);
exp->next = n;
return n;
}
case T_CALL:
{
struct Call *c = container_of (n, struct Call, node);
struct Node *arg = NULL;
struct Node *last = prev;
list_for_each (c->arguments, arg, list)
{
last = _compute_exec_order (arg, last);
}
last->next = n;
return n;
}
case T_ESEQ:
{
struct Eseq *e = container_of (n, struct Eseq, node);
struct Node *stm = _compute_exec_order (e->stm, prev);
struct Node *exp = _compute_exec_order (e->exp, stm);
exp->next = n;
return n;
}
case T_MOVE_MEM:
{
struct MoveMem *m = container_of (n, struct MoveMem, node);
struct Node *orig = _compute_exec_order (m->orig, prev);
struct Node *dest = _compute_exec_order (m->dest, orig);
dest->next = n;
return n;
}
case T_MOVE_TEMP:
{
struct MoveTemp *m = container_of (n, struct MoveTemp, node);
struct Node *orig = _compute_exec_order (m->orig, prev);
orig->next = n;
return n;
}
case T_SXP:
{
struct Sxp *s = container_of (n, struct Sxp, node);
prev->next = n;
return _compute_exec_order (s->exp, n);
}
case T_CJUMP:
{
struct Cjump *c = container_of (n, struct Cjump, node);
struct Node *left = _compute_exec_order (c->left, prev);
struct Node *right = _compute_exec_order (c->right, left);
right->next = n;
return n;
}
case T_SEQ:
{
struct Seq *s = container_of (n, struct Seq, node);
struct Node *stm = NULL;
struct Node *last = n;
prev->next = n;
list_for_each (s->stms, stm, list)
{
last = _compute_exec_order (stm, last);
};
return last;
}
default:
prev->next = n;
return n;
}
assert (0);
}
/* connect all the nodes in a linked list (throught the ->next pointers)
in the order in which they should be evaluated.*/
void
compute_exec_order (struct Node *n)
{
struct Node sentinel;
_compute_exec_order (n, &sentinel);
}
/* find the label named NAME */
static struct Node *
_find_label (id name, struct Node *n)
{
if (n->kind == T_LABEL)
{
struct Label *l = container_of (n, struct Label, node);
if (l->name == name)
return n;
}
if (n->next != NULL)
return _find_label (name, n->next);
else
return NULL;
}
/* find the label named NAME */
struct Node *
find_label (id name)
{
return _find_label (name, irtree);
}
/* bind each Name to its label */
void
bind_labels (struct Node *n)
{
switch (n->kind)
{
case T_NAME:
{
struct Name *a = container_of (n, struct Name, node);
struct Node *target = find_label (a->name);
if (!target)
errl (&n->loc, 1, "missing label (%s)", a->name);
struct Label *l = container_of (target, struct Label, node);
if (l->lit == 0)
errl (&n->loc, 1,
"target label (%s) has no string literal", a->name);
a->lit = l->lit;
}
break;
case T_CALL:
{
struct Call *c = container_of (n, struct Call, node);
primitive p = is_primitive (c->name);
if (p)
{
c->primitive = p;
}
else
{
c->target = find_label (c->name);
if (c->target == NULL)
errl (&n->loc, 1, "call missing label (%s)", c->name);
}
}
break;
case T_JUMP:
{
struct Jump *j = container_of (n, struct Jump, node);
j->target = find_label (j->name);
if (j->target == NULL)
errl (&n->loc, 1, "jump missing label (%s)", j->name);
}
break;
case T_CJUMP:
{
struct Cjump *c = container_of (n, struct Cjump, node);
c->bthen_target = find_label (c->bthen);
if (c->bthen_target == NULL)
errl (&n->loc, 1, "cjump missing label (%s)", c->bthen);
c->belse_target = find_label (c->belse);
if (c->belse_target == NULL)
errl (&n->loc, 1, "cjump missing label (%s)", c->belse);
}
break;
default:
break;
}
if (n->next != NULL)
bind_labels (n->next);
}