-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSymbolTable.d
353 lines (351 loc) · 11.1 KB
/
SymbolTable.d
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import std.stdio : writeln, stderr;
import std.algorithm : countUntil, map;
import std.typecons : Tuple, tuple;
import std.variant : Variant;
import std.format : format;
import std.conv : to;
import std.string : indexOf;
import Expr : Expr;
import Node : For;
public enum Edition {
First = 1, Second, Third, Fourth, Fifth, Sixth,
}
class SymbolTable {
private enum Line { Unknown, Exists, BadRef, Referenced }
private Line[ushort] lines;
private int current_line = -1;
private int nerrs = 0;
immutable int max_errs = 5;
private bool end_of_program = false, need_data = false, need_str_data = false;
private string[] id_list, string_list;
private int[] data_str;
private double[] constant_list, data_num;
private ushort[int] dims, str_dims;
private Tuple!(ushort,ushort)[int] dims2;
public struct Function { int[] param_idents; Expr fn_expr; int fn_line; ulong nparams; }
private Function[int] functions;
private bool[int] vars, mats, strings;
immutable int max_depth = 8;
private int ngosubs = 0;
private bool[int] returns;
private int basic_edition;
this(Edition edition) {
basic_edition = edition;
}
@property Edition edition() {
return cast(Edition)basic_edition;
}
void error(string msg) {
if (nerrs < max_errs) {
stderr.writeln(msg, " IN ", current_line);
++nerrs;
}
}
@property auto errors() {
return nerrs;
}
void checkReferences() {
foreach (l, ty; lines) {
if (ty == Line.BadRef) {
current_line = l;
error("NO SUCH LINE");
}
}
foreach (fn, f; functions) {
if (f.fn_expr is null && f.fn_line == -1) {
error("NO SUCH FUNCTION");
}
}
if (For.pop() != -1) {
error("FOR WITHOUT NEXT");
}
if (need_data && !data_num.length) {
error("NO NUMERIC DATA");
}
if (need_str_data && !data_str.length) {
error("NO STRING DATA");
}
end_of_program = true;
}
@property auto end() {
return end_of_program;
}
void setLine(ushort l, bool first_pass = true) {
if (first_pass && (l <= current_line)) {
error("BAD LINE NUMBER");
}
current_line = l;
}
@property ushort line() {
return cast(ushort)current_line;
}
void registerLine(ushort l) {
if (l !in lines) {
lines[l] = Line.Exists;
}
else if (lines[l] == Line.BadRef) {
lines[l] = Line.Referenced;
}
}
void registerFlow(ushort l) {
if (l !in lines) {
lines[l] = Line.BadRef;
}
else if (lines[l] == Line.Exists) {
lines[l] = Line.Referenced;
}
}
bool isReferenced(ushort l) {
return l in lines && lines[l] == Line.Referenced;
}
int installId(string id) {
auto pos = countUntil(id_list, id);
if (pos != -1) {
return cast(int)pos;
}
else {
id_list ~= id;
return cast(int)(id_list.length - 1);
}
}
void initializeId(int id) {
if (id !in vars) {
vars[id] = true;
}
}
void initializeDim(int id, bool explicit_dim = false, ushort sz = 10) {
if (id !in dims) {
dims[id] = sz;
}
else if (explicit_dim) {
error("DIM ALREADY USED");
}
}
void initializeDim2(int id, bool explicit_dim = false, ushort sz1 = 10, ushort sz2 = 10) {
if (id !in dims2) {
dims2[id] = tuple(sz1, sz2);
}
else if (explicit_dim) {
error("DIM ALREADY USED");
}
}
void initializeMat(int id, bool explicit_mat = false) {
if (id !in dims2) {
error("DIM NOT DEFINED");
}
if (id !in mats) {
if (explicit_mat) {
mats[id] = true;
}
else {
error("MAT NOT INITIALIZED");
}
}
}
int getMatType(int id) {
if (id in mats) {
return 2;
}
else if (id in dims) {
return 1;
}
else {
return 0;
}
}
void initializeString(int id, bool explicit_str = false) {
if (id !in strings) {
if (explicit_str) {
strings[id] = true;
}
else {
error("STRING NOT INITIALIZED");
}
}
}
void initializeDimString(int id, bool explicit_dim = false, ushort sz = 10) {
if (id !in str_dims) {
str_dims[id] = sz;
}
else if (explicit_dim) {
error("DIM ALREADY USED");
}
}
int installString(string str) {
auto pos = countUntil(string_list, str);
if (pos != -1) {
return cast(int)pos;
}
else {
string_list ~= str;
return cast(int)(string_list.length - 1);
}
}
int installConstant(double num) {
auto pos = countUntil(constant_list, num);
if (pos != -1) {
return cast(int)pos;
}
else {
constant_list ~= num;
return cast(int)(constant_list.length - 1);
}
}
void installData(double num) {
data_num ~= num;
}
void installData(int str) {
data_str ~= str;
}
void useData(bool num = false, bool str = false) {
if (num) {
need_data = true;
}
if (str) {
need_str_data = true;
}
}
bool referencedLine(ushort l) {
if (l !in lines) {
throw new Exception("BAD LINE");
}
if (lines[l] == Line.BadRef) {
throw new Exception("NON-EXISTENT LINE");
}
return lines[l] == Line.Referenced;
}
string getId(int i) {
if ((i < 0) || (i >= id_list.length)) {
throw new Exception("BAD IDENT");
}
if (i !in vars && i !in mats && i !in dims && i !in dims2 && i !in strings && i !in functions && i !in str_dims) {
error("NO SUCH VARIABLE");
}
return id_list[i];
}
void addFunction(int name_id, Function f) {
if (name_id !in functions) {
functions[name_id] = f;
}
else {
if (functions[name_id].fn_expr is null && functions[name_id].fn_line == -1) {
if (f.nparams != functions[name_id].nparams) {
error("INCORRECT NUMBER OF PARAMETERS");
}
if (f.fn_expr !is null || f.fn_line != -1) {
functions[name_id] = f;
}
}
else {
if (f.fn_expr !is null || f.fn_line != -1) {
error("FUNCTION ALREADY DEFINED");
}
}
}
}
ref Function getFunction(int name_id) {
if (name_id !in functions) {
error("NO SUCH FUNCTION");
functions[name_id] = Function(new int[0], null, -1);
return functions[name_id];
}
else {
return functions[name_id];
}
}
auto getStrLen(int str_id) { // hack for necessary bitcast
return string_list[str_id].length;
}
@property auto dataN() { // hacks for get element pointer
return data_num.length;
}
@property auto dataStrN() {
return data_str.length;
}
@property auto gosub() {
returns[++ngosubs] = true;
return ngosubs;
}
auto getReturns() {
return returns;
}
void simpleFunction(int r) {
returns[r] = false;
}
auto DimSize(int id) {
return dims[id];
}
auto Dim2Size(int id) {
return dims2[id];
}
auto strDimSize(int id) {
return str_dims[id];
}
void codegen() {
auto g = (double value) {
string s = to!string(value);
return (s.indexOf('.') != -1) ? s : s ~ ".0";
};
auto gs = (int str_id) {
auto l = getStrLen(str_id) + 1;
return format("([%d x i8], [%d x i8]* @_S%d, i32 0, i32 0)", l, l, str_id);
};
foreach (i, c; constant_list) {
writeln(format("@_C%u = private constant double %s", i, g(c)));
}
foreach (i, s; string_list) {
writeln(format("@_S%u = private constant [ %u x i8 ] c\"%s\\00\"", i, s.length + 1, s));
}
if (dataN) {
auto gen = data_num.map!(g);
writeln(format("@_DATA = private constant [ %u x double ] [ %-(double %s, %) ]", dataN, gen));
}
if (dataStrN) {
auto gen_str = data_str.map!(gs);
writeln(format("@_DATA_STR = private constant [ %u x i8* ] [ %-(i8* getelementptr inbounds %s, %) ]", dataStrN, gen_str));
}
writeln("\ndefine i32 @main() {");
writeln(" entry:");
foreach (k, _; vars) {
writeln(format(" %%%s = alloca double", id_list[k]));
writeln(format(" store double 0.0, double* %%%s", id_list[k]));
}
foreach (k, _; strings) {
writeln(format(" %%%s_ = alloca i8*", id_list[k]));
writeln(format(" store i8* null, i8** %%%s_", id_list[k]));
}
if (data_num.length) {
writeln(" %_DATA_NUM_P = alloca i32");
writeln(" store i32 0, i32* %_DATA_NUM_P");
}
if (data_str.length) {
writeln(" %_DATA_STR_P = alloca i32");
writeln(" store i32 0, i32* %_DATA_STR_P");
}
foreach (k, v; dims) {
writeln(format(" %%_DATA1_%s = alloca [ %d x double ]", id_list[k], v + 1));
writeln(format(" store [ %d x double ] zeroinitializer, [ %d x double ]* %%_DATA1_%s",
v + 1, v + 1, id_list[k]));
}
foreach (k, v; dims2) {
writeln(format(" %%_DATA2_%s = alloca [ %d x double ]", id_list[k], (v[0] + 1) * (v[1] + 1)));
writeln(format(" store [ %d x double ] zeroinitializer, [ %d x double ]* %%_DATA2_%s",
(v[0] + 1) * (v[1] + 1), (v[0] + 1) * (v[1] + 1), id_list[k]));
}
foreach (k, v; str_dims) {
writeln(format(" %%_DATAS_%s = alloca [ %d x i8* ]", id_list[k], v + 1));
writeln(format(" store [ %d x i8* ] zeroinitializer, [ %d x i8* ]* %%_DATAS_%s",
v + 1, v + 1, id_list[k]));
}
foreach (k, _; mats) {
writeln(format(" %%_MAT_%s = alloca %%struct.Dims", id_list[k]));
writeln(format(" store %%struct.Dims zeroinitializer, %%struct.Dims* %%_MAT_%s", id_list[k]));
}
if (returns.length) { // FIXME: shouldn't generate for only simple functions
writeln(format(" %%_RETURN = alloca [ %d x i32 ]", max_depth));
writeln(format(" store [ %d x i32 ] zeroinitializer, [ %d x i32 ]* %%_RETURN", max_depth, max_depth));
writeln(" %_RETURN_P = alloca i32");
writeln(" store i32 0, i32* %_RETURN_P");
}
}
}