Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory management - Milestone 2 #100

Open
wants to merge 58 commits into
base: 2022
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
e01e404
Metadata Store First Draft
jcbib May 12, 2022
474df62
added ref addr mapping
tgujar May 12, 2022
c4e2ef3
Altered Load to account for metadata blocks
jcbib May 12, 2022
f13490a
Merge pull request #1 from jcbib/2022
jcbib May 12, 2022
c799981
added referencing to code
tgujar May 12, 2022
977d2ea
added node traversal
tgujar May 13, 2022
4c5bccf
added scoping info and node traversal
tgujar May 13, 2022
793d0d6
fixed mem array type
tgujar May 13, 2022
273cb6e
reference to addr mapping works
tgujar May 13, 2022
710868b
merge meta store
tgujar May 13, 2022
ea7b403
fixes for metadata
tgujar May 13, 2022
7888f87
fixed bitstring gen
tgujar May 13, 2022
66e5a32
basic test case passing
tgujar May 13, 2022
fdfb7d9
fixed bit string parse(yet again)
tgujar May 13, 2022
2bb50ee
tests and minor fixes
tgujar May 13, 2022
805019f
fixed mem access
tgujar May 13, 2022
bea517e
cycle failing
tgujar May 14, 2022
8b737ff
fixed cycles
tgujar May 14, 2022
3544685
added/fixed comments
tgujar May 14, 2022
4d9d680
updated design doc
tgujar May 14, 2022
34a5f76
minor fixes to doc
tgujar May 14, 2022
9423e4f
fixed test comments
tgujar May 23, 2022
ada7977
reverted lower.ts
tgujar May 23, 2022
2ac664d
moved metadata addition to compiler
tgujar May 23, 2022
ba1e4e7
initial draft for milestone 2 features
tgujar May 23, 2022
e0b4612
fixed alloc call
tgujar May 23, 2022
0bb0ee8
updates to doc
tgujar May 23, 2022
418e14e
support for extended alloc
tgujar May 23, 2022
a0eff81
Tests and proofread of design doc
Titan-Ngo May 24, 2022
0258ac7
added more tests
tgujar May 24, 2022
387b9f8
Merge branch 'dev' into milestone-2
tgujar May 24, 2022
28618a2
Merge branch '2022' into dev
tgujar May 24, 2022
63e0289
fixed merge issues
tgujar May 24, 2022
d050451
implemented compaction
tgujar May 31, 2022
889b464
Merge branch 'milestone-2' of github.com:tgujar/chocopy-wasm-compiler…
tgujar May 31, 2022
f5204bb
Merge branch 'milestone-2' of github.com:tgujar/chocopy-wasm-compiler…
tgujar May 31, 2022
662ec07
Merge branch '2022' into milestone-2
tgujar May 31, 2022
87d99ab
merge failing
tgujar May 31, 2022
fd44eb0
fix memory.wat issue
tgujar May 31, 2022
4697e28
bigint integrated
tgujar May 31, 2022
d80fd67
all tests passing
tgujar Jun 1, 2022
66c1cea
Added new tests and helper for new tests
jcbib Jun 1, 2022
f4391a2
Merge branch '2022' into m2
tgujar Jun 1, 2022
a3645ed
Debugged less simple cases except for complete deletion
jcbib Jun 1, 2022
47c8023
merge fixes
tgujar Jun 1, 2022
0b55863
Fixed reference counting bugs
jcbib Jun 1, 2022
72f2afb
removed debugging code
jcbib Jun 1, 2022
93f0faf
removed debugging code
jcbib Jun 1, 2022
c2049c0
removed debugging code
jcbib Jun 1, 2022
648b398
Merge branch 'tgujar/del-comp' into m2
tgujar Jun 1, 2022
cd667d0
ref counting fixes
tgujar Jun 1, 2022
00a9066
added removed tests
tgujar Jun 1, 2022
a91dbc0
fixes fro lists
tgujar Jun 2, 2022
bad62dd
cleanup
tgujar Jun 2, 2022
33c52fc
removed unnnecessary import
tgujar Jun 2, 2022
d33ec24
fix build-web issue
tgujar Jun 2, 2022
ab45472
build fixes
tgujar Jun 2, 2022
921f517
minor fixes
tgujar Jun 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 84 additions & 10 deletions compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Program, Stmt, Expr, Value, Class, VarInit, FunDef } from "./ir"
import { Annotation, BinOp, Type, UniOp } from "./ast"
import { APPLY, BOOL, createMethodName, makeWasmFunType, NONE, NUM } from "./utils";
import { equalType } from "./type-check";
import { getTypeInfo } from "./memory";
import exp from "constants";

export type GlobalEnv = {
globals: Map<string, boolean>;
Expand Down Expand Up @@ -89,18 +91,66 @@ export function compile(ast: Program<Annotation>, env: GlobalEnv) : CompileResul
function codeGenStmt(stmt: Stmt<Annotation>, env: GlobalEnv): Array<string> {
switch (stmt.tag) {
case "store":
return [
let post = [
...codeGenValue(stmt.start, env),
`(i32.add)`,
`call $ref_lookup`,
...codeGenValue(stmt.offset, env),
...codeGenValue(stmt.value, env),
`(local.get $$scratch)`,
`call $store`

]
let pre = [...codeGenValue(stmt.value, env), `(local.set $$scratch)` ,`(i32.const 0)`]
if (stmt.value.a?.type?.tag === "list" ||stmt.value.a?.type?.tag === "class" || stmt.value?.tag === "none" || stmt.value?.tag === "num") {
pre = [
...codeGenValue(stmt.value, env),
`(local.set $$scratch)`,
...codeGenValue(stmt.start, env),
`call $ref_lookup`,
...codeGenValue(stmt.offset, env),
`(call $load)`, // load the ref number referred to by argument ref no. and the offset
`(i32.const 0)`,
`(i32.const -1)`,
`(i32.const 0)`,
`(call $traverse_update)`,
`(i32.mul (i32.const 0))`, // hack to take top value of stack
`(local.get $$scratch)`,
`(i32.add)`, // hack to take top value of stack
...codeGenValue(stmt.start, env),
`(i32.const 1)`,
`(i32.const 0)`,
`(call $traverse_update)`,
`(i32.mul (i32.const 0))`
]
}
return pre.concat(post);

case "assign":
var valStmts = codeGenExpr(stmt.value, env);
if (env.locals.has(stmt.name)) {
return valStmts.concat([`(local.set $${stmt.name})`]);
} else {
return valStmts.concat([`(global.set $${stmt.name})`]);
if (((stmt.value.a?.type?.tag === "list" || stmt.value.a?.type?.tag === "class" || (stmt.value.tag === "value" && stmt.value.value.tag === "none")) || (stmt.value?.tag === "value" && stmt.value.value.tag === "num")) && (stmt.value.tag !== "alloc")) { // if the assignment is object assignment
valStmts.push(`(i32.const 0)`, `(i32.const 1)`, `(i32.const 1)` , `(call $traverse_update)`) // update the count of the object on the RHS
if (env.locals.has(stmt.name)) {
return [`(local.get $${stmt.name})`, // update the count of the object on the LHS
`(i32.const 0)`,
`(i32.const -1)`,
`(i32.const 1)`,
`(call $traverse_update)`,
`(local.set $${stmt.name})`].concat(valStmts).concat([`(local.set $${stmt.name})`]);
} else {
return [`(global.get $${stmt.name})`,
`(i32.const 0)`,
`(i32.const -1)`,
`(i32.const 1)`,
`(call $traverse_update)`,
`(global.set $${stmt.name})`].concat(valStmts).concat([`(global.set $${stmt.name})`]);
}
}
else {
if (env.locals.has(stmt.name)) {
return valStmts.concat([`(local.set $${stmt.name})`]);
} else {
return valStmts.concat([`(global.set $${stmt.name})`]);
}
}

case "return":
Expand Down Expand Up @@ -157,9 +207,11 @@ function codeGenExpr(expr: Expr<Annotation>, env: GlobalEnv): Array<string> {
...exprStmts,
`(local.set $$scratch)`, // bignum addr
`(local.get $$scratch)`, // store addr
`(call $ref_lookup)`,
`(i32.const 0)`, // store offset
`(i32.const 0)`, // 0 - len
`(local.get $$scratch)`, // load addr
`(call $ref_lookup)`,
`(i32.const 0)`, // load offset
`(call $load)`, // load bignum len
`(i32.sub)`, // store val
Expand All @@ -181,7 +233,7 @@ function codeGenExpr(expr: Expr<Annotation>, env: GlobalEnv): Array<string> {
} else if (expr.name === "print" && equalType(argTyp, NONE)) {
callName = "print_none";
} else if (expr.name === "len") {
return [...argStmts, "(i32.const 0)", "call $load"];
return [...argStmts,"(call $ref_lookup)", "(i32.const 0)", "call $load"];
}

return argStmts.concat([`(call $${callName})`]);
Expand All @@ -194,24 +246,42 @@ function codeGenExpr(expr: Expr<Annotation>, env: GlobalEnv): Array<string> {
case "call":
var valStmts = expr.arguments.map((arg) => codeGenValue(arg, env)).flat();
if(expr.name === "len"){
return [...valStmts, "(i32.const 0)", "call $load"];
return [...valStmts, "(call $ref_lookup)", "(i32.const 0)", "call $load"];
}
valStmts.push(`(call $${expr.name})`);
return valStmts;
// Not sure if plugging in the scope calls here is the best way to do this
return [
`(call $add_scope)`,
...valStmts,
`(call $remove_scope)`
];

case "call_indirect":
var valStmts = codeGenExpr(expr.fn, env);
var fnStmts = expr.arguments.map((arg) => codeGenValue(arg, env)).flat();
return [...fnStmts, ...valStmts, `(call_indirect (type ${makeWasmFunType(expr.arguments.length)}))`];
return [`(call $add_scope)`, ...fnStmts, ...valStmts, `(call_indirect (type ${makeWasmFunType(expr.arguments.length)}))`, `(call $remove_scope)`];

case "alloc":
if (expr.fixed) {
let r = [
...codeGenValue(expr.amount, env),
`(i32.const ${parseInt(expr.fixed.map(b => b ? 1: 0).reverse().join(""), 2)})`, //parseInt(binArr.reverse().join(""), 2)
`(i32.const ${expr.fixed.length})`,
`call $alloc`
]
return(r);
}
let fields = [...env.classes.get(expr?.a?.type?.tag === "class" && expr.a.type.name).values()];
return [
...codeGenValue(expr.amount, env),
`(i32.const ${getTypeInfo(fields.map(f => f[1]))})`,
`(i32.const ${fields.length})`,
`call $alloc`
];
case "load":
return [
...codeGenValue(expr.start, env),
`call $ref_lookup`,
...codeGenValue(expr.offset, env),
`call $load`
]
Expand Down Expand Up @@ -240,12 +310,15 @@ function codeGenValue(val: Value<Annotation>, env: GlobalEnv): Array<string> {
var return_val : string[] = []

return_val.push(`(i32.const ${n})`);
return_val.push(`(i32.const 0)`)
return_val.push(`(i32.const 1)`);
return_val.push(`(call $alloc)`);
return_val.push(`(local.set $$scratch)`);

// store the bignum in (n+1) blocks
// store number of blocks in the first block
return_val.push(`(local.get $$scratch)`);
return_val.push(`(call $ref_lookup)`);
return_val.push(`(i32.const ${i})`);
if (neg)
return_val.push(`(i32.const -${n-1})`);
Expand All @@ -257,6 +330,7 @@ function codeGenValue(val: Value<Annotation>, env: GlobalEnv): Array<string> {
// store the digits in the rest of blocks
for (i; i < n; i++) {
return_val.push(`(local.get $$scratch)`);
return_val.push(`(call $ref_lookup)`);
return_val.push(`(i32.const ${i})`);
return_val.push(`(i32.const ${digits[i-1]})`);
return_val.push(`call $store`);
Expand Down
Binary file added designs/images/metadata.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading