forked from ucsd-cse231-s22/chocopy-wasm-compiler-A
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.ts
128 lines (115 loc) · 4.96 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
// 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 { Program, Type, Value } from './ast';
import { PyValue, NONE, BOOL, NUM, CLASS } from "./utils";
import { lowerProgram } from './lower';
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<Type>) : GlobalEnv {
const newGlobals = new Map(env.globals);
const newClasses = new Map(env.classes);
var newOffset = env.offset;
prog.inits.forEach((v) => {
newGlobals.set(v.name, true);
});
prog.classes.forEach(cls => {
const classFields = new Map();
cls.fields.forEach((field, i) => classFields.set(field.name, [i, field.value]));
newClasses.set(cls.name, classFields);
});
return {
globals: newGlobals,
classes: newClasses,
locals: env.locals,
labels: env.labels,
offset: newOffset
}
}
// export async function run(source : string, config: Config) : Promise<[Value, compiler.GlobalEnv, GlobalTypeEnv, string]> {
export async function run(source : string, config: Config) : Promise<[Value, GlobalEnv, GlobalTypeEnv, string, WebAssembly.WebAssemblyInstantiatedSource]> {
const parsed = parse(source);
const [tprogram, tenv] = tc(config.typeEnv, parsed);
const globalEnv = augmentEnv(config.env, tprogram);
const irprogram = lowerProgram(tprogram, globalEnv);
const progTyp = tprogram.a;
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(irprogram, globalEnv);
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))
(func $assert_not_none (import "imports" "assert_not_none") (param i32) (result i32))
(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 $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))
${globalImports}
${globalDecls}
${config.functions}
${compiled.functions}
(func (export "exported_func") ${returnType}
${compiled.mainSource}
${returnExpr}
)
)`;
console.log(wasmSource);
const [result, instance] = await runWat(wasmSource, importObject);
return [PyValue(progTyp, result), compiled.newEnv, tenv, compiled.functions, instance];
}