-
Notifications
You must be signed in to change notification settings - Fork 241
/
brilck.ts
419 lines (373 loc) · 9.93 KB
/
brilck.ts
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import * as bril from './bril-ts/bril.ts';
import {Signature, PolySignature, FuncType, OP_SIGS, TVar, BaseSignature, PolyType} from './bril-ts/types.ts';
import {readStdin, unreachable} from './bril-ts/util.ts';
/**
* The JavaScript types of Bril constant values.
*/
const CONST_TYPES: {[key: string]: string} = {
'int': 'number',
'float': 'number',
'bool': 'boolean',
'char': 'string'
};
type VarEnv = Map<bril.Ident, bril.Type>;
type FuncEnv = Map<bril.Ident, FuncType>;
type TypeEnv = Map<string, bril.Type>;
/**
* A typing environment that we can use to check instructions within
* a single function.
*/
interface Env {
/**
* The types of all variables defined in the function.
*/
vars: VarEnv;
/**
* The names of all the labels in the function.
*/
labels: Set<bril.Ident>;
/**
* The defined functions in the program.
*/
funcs: FuncEnv;
/**
* The return type of the current function.
*/
ret: bril.Type | undefined;
}
/**
* An optional filename for error messages.
*/
let CHECK_FILE: string | undefined;
/**
* The total number of errors we encounter.
*/
let ERRORS: number = 0;
/**
* Print an error message, possibly with a source position.
*/
function err(msg: string, pos: bril.Position | undefined) {
ERRORS++;
if (pos) {
msg = `${pos.row}:${pos.col}: ${msg}`;
}
if (CHECK_FILE) {
msg = `${CHECK_FILE}:${msg}`;
}
console.error(msg);
}
/**
* Set the type of variable `id` to `type` in `env`, checking for conflicts
* with the old type for the variable.
*/
function addType(env: VarEnv, id: bril.Ident, type: bril.Type, pos: bril.Position | undefined) {
let oldType = env.get(id);
if (oldType) {
if (!typeEq(oldType, type)) {
err(
`new type ${type} for ${id} conflicts with old type ${oldType}`,
pos
);
}
} else {
env.set(id, type);
}
}
/**
* Look up type variables in TypeEnv, leaving non-variable types and undefined
* type variables unchanged.
*/
function typeLookup(type: PolyType, tenv: TypeEnv | undefined): PolyType {
if (!tenv) {
return type;
}
// Do we have a type variable to look up?
if (typeof type === 'object' && 'tv' in type) {
let res = tenv.get(type.tv);
if (res) {
return res;
} else {
return type;
}
}
// Do we need to recursively look up inside this type?
if (typeof type === 'object' && 'ptr' in type) {
return {ptr: typeLookup(type.ptr, tenv)};
}
return type;
}
/**
* Check for type equality.
*
* If a type environemnt is supplied, attempt to unify any unset type
* variables occuring in `b` to make the types match.
*/
function typeEq(a: bril.Type, b: PolyType, tenv?: TypeEnv): boolean {
// Shall we bind a type variable in b?
b = typeLookup(b, tenv);
if (typeof b === "object" && 'tv' in b) {
if (!tenv) {
throw `got type variable ${b.tv} but no type environment`;
}
tenv.set(b.tv, a);
return true;
}
// Normal type comparison.
if (typeof a === "string" && typeof b === "string") {
return a == b;
} else if (typeof a === "object" && typeof b === "object") {
return typeEq(a.ptr, b.ptr, tenv);
} else {
return false;
}
}
/**
* Format a type as a human-readable string.
*/
function typeFmt(t: PolyType): string {
if (typeof t === "string") {
return t;
} else if (typeof t === "object") {
if ('tv' in t) {
return t.tv;
} else {
return `ptr<${typeFmt(t.ptr)}>`;
}
}
unreachable(t);
}
/**
* Check an instruction's arguments and labels against a type signature.
*
* `sig` may be either a concrete signature or a polymorphic one, in which case
* we try unify the quantified type. `name` optionally gives a name for the
* operation to use in error messages; otherwise, we use `instr`'s opcode.
*/
function checkSig(env: Env, instr: bril.Operation, psig: Signature | PolySignature, name?: string) {
name = name ?? instr.op;
// Are we handling a polymorphic signature?
let sig: BaseSignature<PolyType>;
let tenv: TypeEnv = new Map();
if ('tvar' in psig) {
sig = psig.sig;
} else {
sig = psig;
}
// Check destination type.
if ('type' in instr) {
if (sig.dest) {
if (!typeEq(instr.type, sig.dest, tenv)) {
err(
`result type of ${name} should be ${typeFmt(typeLookup(sig.dest, tenv))}, ` +
`but found ${typeFmt(instr.type)}`,
instr.pos
);
}
} else {
err(`${name} should have no result type`, instr.pos);
}
} else {
if (sig.dest) {
err(
`missing result type ${typeFmt(typeLookup(sig.dest, tenv))} for ${name}`,
instr.pos
);
}
}
// Check arguments.
let args = instr.args ?? [];
if (args.length !== sig.args.length) {
err(
`${name} expects ${sig.args.length} args, not ${args.length}`,
instr.pos
);
} else {
for (let i = 0; i < args.length; ++i) {
let argType = env.vars.get(args[i]);
if (!argType) {
err(`${args[i]} (arg ${i}) undefined`, instr.pos);
continue;
}
if (!typeEq(argType, sig.args[i], tenv)) {
err(
`${args[i]} has type ${typeFmt(argType)}, but arg ${i} for ${name} ` +
`should have type ${typeFmt(typeLookup(sig.args[i], tenv))}`,
instr.pos
);
}
}
}
// Check labels.
let labs = instr.labels ?? [];
let labCount = sig.labels ?? 0;
if (labs.length !== labCount) {
err(`${instr.op} needs ${labCount} labels; found ${labs.length}`, instr.pos);
} else {
for (let lab of labs) {
if (!env.labels.has(lab)) {
err(`label .${lab} undefined`, instr.pos);
}
}
}
}
type CheckFunc = (env: Env, instr: bril.Operation) => void;
/**
* Special-case logic for checking some special functions.
*/
const INSTR_CHECKS: {[key: string]: CheckFunc} = {
print: (env, instr) => {
if ('type' in instr) {
err(`print should have no result type`, instr.pos);
}
},
call: (env, instr) => {
let funcs = instr.funcs ?? [];
if (funcs.length !== 1) {
err(`call should have one function, not ${funcs.length}`, instr.pos);
return;
}
let funcType = env.funcs.get(funcs[0]);
if (!funcType) {
err(`function @${funcs[0]} undefined`, instr.pos);
return;
}
checkSig(env, instr, {
args: funcType.args,
dest: funcType.ret,
}, `@${funcs[0]}`);
return;
},
ret: (env, instr) => {
let args = instr.args ?? [];
if (env.ret) {
if (args.length === 0) {
err(`missing return value in function with return type`, instr.pos);
} else if (args.length !== 1) {
err(`cannot return multiple values`, instr.pos);
} else {
checkSig(env, instr, {args: [env.ret]});
}
} else {
if (args.length !== 0) {
err(`returning value in function without a return type`, instr.pos);
}
}
return;
},
phi: (env, instr) => {
let args = instr.args ?? [];
if (!('type' in instr)) {
err(`phi needs a result type`, instr.pos);
return;
}
// Construct a signature with uniform argument types.
let argTypes: bril.Type[] = [];
for (let i = 0; i < args.length; ++i) {
argTypes.push(instr.type);
}
checkSig(env, instr, {args: argTypes, dest: instr.type, labels: args.length});
},
};
function checkOp(env: Env, instr: bril.Operation) {
let args = instr.args ?? [];
// Check for special cases.
let check_func = INSTR_CHECKS[instr.op];
if (check_func) {
check_func(env, instr);
return;
}
// General case: use the operation's signature.
let sig = OP_SIGS[instr.op];
if (!sig) {
err(`unknown opcode ${instr.op}`, instr.pos);
return;
}
checkSig(env, instr, sig);
}
function checkConst(instr: bril.Constant) {
if (!(instr as any)) {
err(`const missing type`, instr!.pos);
return;
}
if (typeof instr.type !== 'string') {
err(`const of non-primitive type ${typeFmt(instr.type)}`, instr.pos);
return;
}
let valType = CONST_TYPES[instr.type];
if (!valType) {
err(`unknown const type ${typeFmt(instr.type)}`, instr.pos);
return;
}
if (typeof instr.value !== valType) {
err(
`const value ${instr.value} does not match type ${typeFmt(instr.type)}`,
instr.pos
);
}
}
function checkFunc(funcs: FuncEnv, func: bril.Function) {
let vars: VarEnv = new Map();
let labels = new Set<bril.Ident>();
// Initilize the type environment with the arguments.
if (func.args) {
for (let arg of func.args) {
addType(vars, arg.name, arg.type, func.pos);
}
}
// Gather up all the types of the local variables and all the label names.
if (func.instrs){
for (let instr of func.instrs) {
if ('dest' in instr) {
addType(vars, instr.dest, instr.type, instr.pos);
} else if ('label' in instr) {
if (labels.has(instr.label)) {
err(`multiply defined label .${instr.label}`, instr.pos);
} else {
labels.add(instr.label);
}
}
}
// Check each instruction.
for (let instr of func.instrs) {
if ('op' in instr) {
if (instr.op === 'const') {
checkConst(instr);
} else {
checkOp({vars, labels, funcs, ret: func.type}, instr);
}
}
}
}
}
function checkProg(prog: bril.Program) {
// Gather up function types.
let funcEnv: FuncEnv = new Map();
for (let func of prog.functions) {
funcEnv.set(func.name, {
ret: func.type,
args: func.args?.map(a => a.type) ?? [],
});
}
// Check each function.
for (let func of prog.functions) {
checkFunc(funcEnv, func);
// The @main function must not return anything.
if (func.name === 'main') {
if (func.type) {
err(`@main must have no return type; found ${typeFmt(func.type)}`,
func.pos);
}
}
}
}
async function main() {
if (Deno.args[0]) {
CHECK_FILE = Deno.args[0];
}
let prog = JSON.parse(await readStdin()) as bril.Program;
checkProg(prog);
if (ERRORS) {
Deno.exit(1);
}
}
main();