Skip to content

Commit

Permalink
1-byte prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 13, 2024
1 parent 9037095 commit 77507c8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
21 changes: 13 additions & 8 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { w } from "../../wazum";
import { w } from "../../wazum/src/index";
import { FunctionDeclaration } from '../ast/nodes/Function';
import { getNameOf, getTypeOf, toDataType, toNumericType } from './util';
import { BinaryExpression, Operator } from '../ast/nodes/BinaryExpression';
Expand All @@ -11,7 +11,11 @@ import { Program } from "../ast/Program";
import { StringLiteral } from "../ast/nodes/StringLiteral";
import { VariableDeclaration } from "../ast/nodes/VariableDeclaration";
import { writeLength } from "./util";
import { Instr } from "../../wazum/src/nodes";
import { NoInfer } from "../../wazum/dist/utils";
import { Node } from "../ast/nodes/Node";

let offset: number = 0;
export class Generator {
public module: w.Module = new w.Module();
public segments: w.MemorySegment[] = [];
Expand All @@ -27,6 +31,7 @@ export class Generator {
}
}
}

toWat(): string {
return this.module.compile();
}
Expand Down Expand Up @@ -56,22 +61,21 @@ export class Generator {
params.push([type, name]);
}

/*for (const stmt of node.block.statements) {
let body: Instr[] = [];
for (const stmt of node.block.statements) {
if (stmt instanceof CallExpression) body.push(this.parseCall(stmt));
else if (stmt instanceof ReturnStatement) body.push(this.parseReturnStatement(stmt));
else throw new Error("Could not parse body of FunctionDeclaration to wasm equivalent!");
}*/
}

const callExpression = this.parseCall(node.block.statements[0] as CallExpression);
//const ret = this.parseReturnStatement(node.block.statements[1] as ReturnStatement);
const fn = w.func(
name,
{
params: params,
returnType: returnType,
locals: []
},
callExpression
{ __nodeType: "block", name: null, body, returnType } as w.Block
);

this.module.addFunc(fn, node.exported);
Expand Down Expand Up @@ -114,9 +118,10 @@ export class Generator {
}
parseStringLiteral(node: StringLiteral): w.MemorySegment {
const seg: w.MemorySegment = {
data: writeLength(node.data.length) + node.data,
offset: w.constant("i32", 0)
data: "\\0" + node.data.length.toString(16) + node.data,
offset: w.constant("i32", offset)
}
offset += 1 + node.data.length;
this.segments.push(seg);
return seg;
}
Expand Down
10 changes: 4 additions & 6 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const tokenizer = new Tokenizer(`
#[extern]: env.print
fn print(start: i32) -> void
str text = "hello world"
str word1 = "Hello, Zep!"
export fn main() -> void {
print(0)
Expand Down Expand Up @@ -52,15 +52,13 @@ execSync("wat2wasm test.wat -o test.wasm");
const wasm = readFileSync("./test.wasm");
const module = new WebAssembly.Module(wasm);
let mem: WebAssembly.Memory;
let dv: DataView;
const instance = new WebAssembly.Instance(module, {
env: {
print: (ptr: number) => {
dv = new DataView(mem.buffer);
const length = dv.getUint16(ptr, true);
console.log("Length: ", length.toString(16));
const length = new Uint8Array(mem.buffer, ptr, 1)[0]
console.log("Length: ", length);
console.log("Mem: ", mem);
console.log("Print: " + String.fromCharCode(...[...(new Uint8Array(mem.buffer, ptr+2, length))]))
console.log("Print: " + String.fromCharCode(...[...(new Uint8Array(mem.buffer, ptr + 1, length))]))
}
}
});
Expand Down
Binary file modified test.wasm
Binary file not shown.
8 changes: 5 additions & 3 deletions test.wat
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
(export "memory" (memory $memory))
(data
(i32.const 0)
"\11\00hello world"
"\0bHello, Zep!"
)
(export "main" (func $main))
(func $main
(call $print
(i32.const 0)
(block
(call $print
(i32.const 0)
)
)
)
)
2 changes: 1 addition & 1 deletion wazum
Submodule wazum updated 3 files
+ bun.lockb
+1 −4 src/compiler.ts
+1 −1 src/module.ts

0 comments on commit 77507c8

Please sign in to comment.