-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhh_opt.c
264 lines (235 loc) · 7.05 KB
/
hh_opt.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
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
/* This file is part of Hedgehog LISP.
* Copyright (C) 2003, 2004 Oliotalo Ltd.
* See file LICENSE.LGPL for pertinent licensing conditions.
*
* Author: Kenneth Oksanen <[email protected]>
*/
/* This file implements algebraic optimizations, such as constant
folding, let hoisting, inlining, etc.
For the time being the only algebraic optimization implemented is
constant folding of boolean expressions, but more will hopefully
come. */
#define HH_COMPILER 1
#include "hh_common.h"
#include "hh_ast.h"
/* Note that the constant folder operates on three-value logic: true,
false and don't know. Hence `!hh_expr_is_true(x)' does not imply
`hh_expr_is_false(x)', and vice versa. */
static int hh_expr_is_true(hh_ast_t *expr)
{
return expr != NULL
&& ((expr->arity == HH_AST_INTEGER && expr->u.integer != 0)
|| (expr->arity == HH_AST_UNSIGNED_INTEGER
&& expr->u.unsigned_integer != 0)
|| (expr->arity == HH_AST_SYMBOL && expr->u.symbol == hh_symbol_true));
}
static int hh_expr_is_false(hh_ast_t *expr)
{
return expr != NULL
&& ((expr->arity == HH_AST_INTEGER && expr->u.integer == 0)
|| (expr->arity == HH_AST_UNSIGNED_INTEGER
&& expr->u.unsigned_integer == 0)
|| expr->arity == HH_AST_NIL);
}
static hh_ast_t *hh_rec_fold(hh_ast_t *expr)
{
int i, j;
hh_signed_word_t sw;
hh_ast_t *n;
int n_true, n_false;
if (expr == NULL)
return NULL;
switch (expr->arity) {
case HH_AST_NIL:
case HH_AST_STRING:
case HH_AST_SYMBOL:
case HH_AST_INTEGER:
case HH_AST_UNSIGNED_INTEGER:
return expr;
break;
case 2:
/* Remove quoting from strings, integers, nil and t. */
if (expr->u.ast[0]->arity == HH_AST_SYMBOL
&& expr->u.ast[0]->u.symbol == hh_symbol_quote) {
if (expr->u.ast[1]->arity == HH_AST_STRING
|| expr->u.ast[1]->arity == HH_AST_INTEGER
|| expr->u.ast[1]->arity == HH_AST_UNSIGNED_INTEGER
|| expr->u.ast[1]->arity == HH_AST_NIL
|| (expr->u.ast[1]->arity == HH_AST_SYMBOL
&& expr->u.ast[1]->u.symbol == hh_symbol_true))
return expr->u.ast[1];
/* Otherwise return expr itself - we mustn't do any
simplifications into anything quoted. */
return expr;
}
/*FALLTHROUGH*/
default:
/* Apply `hh_rec_fold' recursively bottom-up. */
for (i = 0; i < expr->arity; i++)
expr->u.ast[i] = hh_rec_fold(expr->u.ast[i]);
if (expr->u.ast[0]->arity == HH_AST_SYMBOL) {
/* Simplification of boolean if, not, and, or. */
if (expr->u.ast[0]->u.symbol == hh_symbol_if) {
if (expr->arity < 3 || expr->arity > 4)
hh_fatal(expr, "if expects 2 or 3 arguments");
if (hh_expr_is_true(expr->u.ast[1]))
return expr->u.ast[2];
if (hh_expr_is_false(expr->u.ast[1])) {
if (expr->arity == 4)
return expr->u.ast[3];
else {
n = hh_alloc_node(HH_AST_NIL);
hh_ast_copy_location(n, expr);
return n;
}
}
} else if (expr->u.ast[0]->u.symbol == hh_symbol_not) {
if (expr->arity != 2)
hh_fatal(expr, "not expects one argument");
if (hh_expr_is_true(expr->u.ast[1])) {
n = hh_alloc_node(HH_AST_NIL);
hh_ast_copy_location(n, expr);
return n;
}
if (hh_expr_is_false(expr->u.ast[1])) {
n = hh_alloc_node(HH_AST_SYMBOL);
hh_ast_copy_location(n, expr);
n->u.symbol = hh_symbol_true;
return n;
}
} else if (expr->u.ast[0]->u.symbol == hh_symbol_and) {
/* Drop out all true subexpressions. */
n_true = 0;
n = NULL;
for (i = 1; i < expr->arity; i++) {
if (hh_expr_is_true(expr->u.ast[i])) {
n_true++;
n = expr->u.ast[i];
} else
expr->u.ast[i - n_true] = expr->u.ast[i];
}
expr->arity -= n_true;
if (expr->arity == 1) {
/* The value of `(and)' is true. */
if (n == NULL) {
expr->arity = HH_AST_SYMBOL;
expr->u.symbol = hh_symbol_true;
} else
expr = n;
return expr;
}
/* Check if there's a false. If one is first, the whole expr
is false. Otherwise cut the expression to the arity of the
first false. */
if (hh_expr_is_false(expr->u.ast[1]))
return expr->u.ast[1];
for (i = 2; i < expr->arity; i++)
if (hh_expr_is_false(expr->u.ast[i])) {
expr->arity = i + 1;
return expr;
}
} else if (expr->u.ast[0]->u.symbol == hh_symbol_or) {
/* This is essentially a converse of the folding of ands. */
n_false = 0;
n = NULL;
for (i = 1; i < expr->arity; i++) {
if (hh_expr_is_false(expr->u.ast[i])) {
n_false++;
n = expr->u.ast[i];
} else
expr->u.ast[i - n_false] = expr->u.ast[i];
}
expr->arity -= n_false;
if (expr->arity == 1) {
/* The value of `(or)' is false. */
if (n == NULL)
expr->arity = HH_AST_NIL;
else
expr = n;
return expr;
}
/* Check if there's a true, etc... See the folding of
ands. */
if (hh_expr_is_true(expr->u.ast[1]))
return expr->u.ast[1];
for (i = 2; i < expr->arity; i++)
if (hh_expr_is_true(expr->u.ast[i])) {
expr->arity = i + 1;
return expr;
}
} else if (expr->u.ast[0]->u.symbol == hh_symbol_add) {
/* Simple constant folding of addition. It can fold any
expression containing only integer constants, e.g.
(+ 1 (+ 2 -3)) is folded into zero. But there are
limitations - for example if -3 were replaced by a
non-constant expression, nothing could be done to it. */
sw = 0;
for (i = j = 1; i < expr->arity; i++) {
if (expr->u.ast[i]->arity == HH_AST_INTEGER)
sw += expr->u.ast[i]->u.integer;
else
expr->u.ast[j++] = expr->u.ast[i];
}
HH_ASSERT(j <= i);
if (sw != 0)
if (j == 1) {
expr->arity = HH_AST_INTEGER;
expr->u.integer = sw;
} else {
expr->u.ast[j] = hh_alloc_node(HH_AST_INTEGER);
hh_ast_copy_location(expr->u.ast[j], expr);
expr->u.ast[j]->arity = HH_AST_INTEGER;
expr->u.ast[j++]->u.integer = sw;
expr->arity -= i - j;
}
else {
/* sw == 0 */
if (j == 2)
return expr->u.ast[1];
else if (j == 1) {
expr->arity = HH_AST_INTEGER;
expr->u.integer = 0;
} else
expr->arity -= i - j;
}
return expr;
} else if (expr->u.ast[0]->u.symbol == hh_symbol_eq) {
if (expr->arity == 1) {
make_true:
expr->arity = HH_AST_SYMBOL;
expr->u.symbol = hh_symbol_true;
return expr;
}
if (expr->arity == 3
&& ((expr->u.ast[1]->arity == HH_AST_UNSIGNED_INTEGER
&& expr->u.ast[2]->arity == HH_AST_UNSIGNED_INTEGER
&& expr->u.ast[1]->u.unsigned_integer ==
expr->u.ast[2]->u.unsigned_integer)
|| (expr->u.ast[1]->arity == HH_AST_INTEGER
&& expr->u.ast[2]->arity == HH_AST_INTEGER
&& expr->u.ast[1]->u.integer ==
expr->u.ast[2]->u.integer)))
goto make_true;
if (expr->arity == 3
&& ((expr->u.ast[1]->arity == HH_AST_UNSIGNED_INTEGER
&& expr->u.ast[2]->arity == HH_AST_UNSIGNED_INTEGER
&& expr->u.ast[1]->u.unsigned_integer !=
expr->u.ast[2]->u.unsigned_integer)
|| (expr->u.ast[1]->arity == HH_AST_INTEGER
&& expr->u.ast[2]->arity == HH_AST_INTEGER
&& expr->u.ast[1]->u.integer !=
expr->u.ast[2]->u.integer))) {
/* make_false: */
expr->arity = HH_AST_SYMBOL;
expr->u.symbol = hh_symbol_nil;
return expr;
}
}
}
return expr;
}
}
hh_ast_t *hh_opt(hh_ast_t *expr)
{
return hh_rec_fold(expr);
}