-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrunner.ts
173 lines (160 loc) · 7.35 KB
/
runner.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
// This is a mashup of tutorials from:
//
// - https://github.com/AssemblyScript/wabt.js/
// - https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API
import wabt from 'wabt';
import { compile, GlobalEnv } from './compiler';
import {parse} from './parser';
import {emptyLocalTypeEnv, GlobalTypeEnv, tc, tcStmt} from './type-check';
import { Annotation, FunDef, Program, Type, Value } from './ast';
import { PyValue, NONE, BOOL, NUM, CLASS, makeWasmFunType } from "./utils";
import { closureName, lowerProgram } from './lower';
import { monomorphizeProgram } from './monomorphizer';
import { optimizeProgram } from './optimization';
import { wasmErrorImports } from './errors';
export type Config = {
importObject: any;
// env: compiler.GlobalEnv,
env: GlobalEnv,
typeEnv: GlobalTypeEnv,
functions: string // prelude functions
}
// NOTE(joe): This is a hack to get the CLI Repl to run. WABT registers a global
// uncaught exn handler, and this is not allowed when running the REPL
// (https://nodejs.org/api/repl.html#repl_global_uncaught_exceptions). No reason
// is given for this in the docs page, and I haven't spent time on the domain
// module to figure out what's going on here. It doesn't seem critical for WABT
// to have this support, so we patch it away.
if(typeof process !== "undefined") {
const oldProcessOn = process.on;
process.on = (...args : any) : any => {
if(args[0] === "uncaughtException") { return; }
else { return oldProcessOn.apply(process, args); }
};
}
export async function runWat(source : string, importObject : any) : Promise<any> {
const wabtInterface = await wabt();
const myModule = wabtInterface.parseWat("test.wat", source);
var asBinary = myModule.toBinary({});
var wasmModule = await WebAssembly.instantiate(asBinary.buffer, importObject);
const result = (wasmModule.instance.exports.exported_func as any)();
return [result, wasmModule];
}
export function augmentEnv(env: GlobalEnv, prog: Program<Annotation>) : GlobalEnv {
const newGlobals = new Map(env.globals);
const newClasses = new Map(env.classes);
const newClassIndices = new Map(env.classIndices);
const functionNames = new Map(env.functionNames);
var newOffset = env.offset;
prog.inits.forEach((v) => {
newGlobals.set(v.name, true);
});
prog.funs.forEach(f => {
functionNames.set(f.name, closureName(f.name, []));
const addClasses = (f: FunDef<Annotation>, ancestors: Array<FunDef<Annotation>>) => {
newClasses.set(closureName(f.name, ancestors), new Map());
f.children.forEach(c => addClasses(c, [f, ...ancestors]));
}
addClasses(f, []);
});
prog.classes.forEach(cls => {
const classFields = new Map();
cls.fields.forEach((field, i) => classFields.set(field.name, [i + 1, field.value]));
newClasses.set(cls.name, classFields);
});
return {
globals: newGlobals,
classes: newClasses,
classIndices: newClassIndices,
functionNames,
locals: env.locals,
labels: env.labels,
offset: newOffset,
vtableMethods: env.vtableMethods,
}
}
// export async function run(source : string, config: Config) : Promise<[Value, compiler.GlobalEnv, GlobalTypeEnv, string]> {
export async function run(source : string, config: Config) : Promise<[Value<Annotation>, GlobalEnv, GlobalTypeEnv, string, WebAssembly.WebAssemblyInstantiatedSource]> {
config.importObject.errors.src = source; // for error reporting
const parsed = parse(source);
const [tprogram, tenv] = tc(config.typeEnv, parsed);
const tmprogram = monomorphizeProgram(tprogram);
const globalEnv = augmentEnv(config.env, tmprogram);
const irprogram = lowerProgram(tmprogram, globalEnv);
const optIr = optimizeProgram(irprogram);
const progTyp = tmprogram.a.type;
var returnType = "";
var returnExpr = "";
// const lastExpr = parsed.stmts[parsed.stmts.length - 1]
// const lastExprTyp = lastExpr.a;
// console.log("LASTEXPR", lastExpr);
if(progTyp !== NONE) {
returnType = "(result i32)";
returnExpr = "(local.get $$last)"
}
let globalsBefore = config.env.globals;
// const compiled = compiler.compile(tprogram, config.env);
const compiled = compile(optIr, globalEnv);
const vtable = `(table ${globalEnv.vtableMethods.length} funcref)
(elem (i32.const 0) ${globalEnv.vtableMethods.map(method => `$${method[0]}`).join(" ")})`;
const typeSet = new Set<number>();
globalEnv.vtableMethods.forEach(([_, paramNum])=>typeSet.add(paramNum));
let types = "";
typeSet.forEach(paramNum => {
let paramType = "";
if (paramNum > 0) {
paramType = `(param${" i32".repeat(paramNum)})`
}
types += `(type ${makeWasmFunType(paramNum)} (func ${paramType} (result i32)))\n`;
})
const globalImports = [...globalsBefore.keys()].map(name =>
`(import "env" "${name}" (global $${name} (mut i32)))`
).join("\n");
const globalDecls = compiled.globals.map(name =>
`(global $${name} (export "${name}") (mut i32) (i32.const 0))`
).join("\n");
const importObject = config.importObject;
if(!importObject.js) {
const memory = new WebAssembly.Memory({initial:2000, maximum:2000});
importObject.js = { memory: memory };
}
const wasmSource = `(module
(import "js" "memory" (memory 1))
${wasmErrorImports}
(func $print_num (import "imports" "print_num") (param i32) (result i32))
(func $print_bool (import "imports" "print_bool") (param i32) (result i32))
(func $print_none (import "imports" "print_none") (param i32) (result i32))
(func $abs (import "imports" "abs") (param i32) (result i32))
(func $min (import "imports" "min") (param i32) (param i32) (result i32))
(func $max (import "imports" "max") (param i32) (param i32) (result i32))
(func $pow (import "imports" "pow") (param i32) (param i32) (result i32))
(func $destructure_check (import "imports" "destructure_check") (param i32) (result i32))
(func $alloc (import "libmemory" "alloc") (param i32) (result i32))
(func $load (import "libmemory" "load") (param i32) (param i32) (result i32))
(func $store (import "libmemory" "store") (param i32) (param i32) (param i32))
(func $$add (import "imports" "$add") (param i32) (param i32) (result i32))
(func $$sub (import "imports" "$sub") (param i32) (param i32) (result i32))
(func $$mul (import "imports" "$mul") (param i32) (param i32) (result i32))
(func $$div (import "imports" "$div") (param i32) (param i32) (result i32))
(func $$mod (import "imports" "$mod") (param i32) (param i32) (result i32))
(func $$eq (import "imports" "$eq") (param i32) (param i32) (result i32))
(func $$neq (import "imports" "$neq") (param i32) (param i32) (result i32))
(func $$lte (import "imports" "$lte") (param i32) (param i32) (result i32))
(func $$gte (import "imports" "$gte") (param i32) (param i32) (result i32))
(func $$lt (import "imports" "$lt") (param i32) (param i32) (result i32))
(func $$gt (import "imports" "$gt") (param i32) (param i32) (result i32))
(func $$bignum_to_i32 (import "imports" "$bignum_to_i32") (param i32) (result i32))
${types}
${globalImports}
${globalDecls}
${vtable}
${config.functions}
${compiled.functions}
(func (export "exported_func") ${returnType}
${compiled.mainSource}
${returnExpr}
)
)`;
const [result, instance] = await runWat(wasmSource, importObject);
return [PyValue(progTyp, result), compiled.newEnv, tenv, compiled.functions, instance];
}