From 77507c835206dcc74a2468080cbd788f5737ab0c Mon Sep 17 00:00:00 2001 From: JairusSW Date: Fri, 12 Jan 2024 23:07:35 -0800 Subject: [PATCH] 1-byte prefix --- src/generator/index.ts | 21 +++++++++++++-------- src/test.ts | 10 ++++------ test.wasm | Bin 93 -> 95 bytes test.wat | 8 +++++--- wazum | 2 +- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/generator/index.ts b/src/generator/index.ts index 0a7c900..dab84bf 100644 --- a/src/generator/index.ts +++ b/src/generator/index.ts @@ -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'; @@ -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[] = []; @@ -27,6 +31,7 @@ export class Generator { } } } + toWat(): string { return this.module.compile(); } @@ -56,14 +61,13 @@ 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, { @@ -71,7 +75,7 @@ export class Generator { returnType: returnType, locals: [] }, - callExpression + { __nodeType: "block", name: null, body, returnType } as w.Block ); this.module.addFunc(fn, node.exported); @@ -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; } diff --git a/src/test.ts b/src/test.ts index ef5c7d2..733ec42 100644 --- a/src/test.ts +++ b/src/test.ts @@ -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) @@ -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))])) } } }); diff --git a/test.wasm b/test.wasm index 3d22437552d332fa56c0072eec6ef58bc5e883ea..2d9620d55445bab09ee8cdcd9223ff905a992eb8 100644 GIT binary patch delta 37 scma!zpJ1=R&B)2X