many things

This commit is contained in:
nora 2023-07-31 22:39:17 +02:00
parent f582a5b4c3
commit 924236532c
13 changed files with 264 additions and 85 deletions

View file

@ -4,7 +4,7 @@
// Base types.
export type u32 = number;
export type u64 = number;
export type u64 = bigint;
export type f32 = number;
export type f64 = number;
export type VecByte = Uint8Array;
@ -73,7 +73,7 @@ export type BitWidth = "32" | "64";
export type Sign = "u" | "s";
export type NumericInstr =
| { kind: "i32.const"; imm: u32 }
| { kind: "i32.const"; imm: bigint }
| { kind: "i64.const"; imm: u64 }
| { kind: "f32.const"; imm: f32 }
| { kind: "f64.const"; imm: f64 }

View file

@ -29,7 +29,7 @@ const EXAMPLE_MODULE: Module = {
type: { kind: "typeidx", idx: 1 },
instrs: [{ kind: "local.get", imm: 0 }],
},
{ kind: "i32.const", imm: 1 },
{ kind: "i32.const", imm: 1n },
{ kind: "i32.add" },
],
},
@ -49,7 +49,7 @@ const EXAMPLE_MODULE: Module = {
globals: [
{
type: { mut: "const", type: "i32" },
init: [{ kind: "i32.const", imm: 0 }],
init: [{ kind: "i32.const", imm: 0n }],
_name: "globalling",
},
],
@ -70,7 +70,7 @@ const EXAMPLE_MODULE: Module = {
mode: {
kind: "active",
memory: 0,
offset: [{ kind: "i32.const", imm: 0 }],
offset: [{ kind: "i32.const", imm: 0n }],
},
init: new Uint8Array(),
_name: "very-active-data",
@ -86,7 +86,7 @@ it("should print a Wasm module with the correct formatting", () => {
(type (func (param i32) (result i32)))
(type (func (param) (result i32)))
(import "left-pad" "padLeft" (func (type 0)))
(func $addOne (type 0)
(func $addOne (;1;) (type 0)
(local i32 i32)
local.set 0
block (type 1)

View file

@ -72,7 +72,7 @@ class FmtCtx {
this.word(word, chalk.blue);
}
type(word: string | number) {
type(word: string | number | bigint) {
this.word(word, chalk.green);
}
@ -90,7 +90,10 @@ class FmtCtx {
}
}
word(word: string | number, color: (s: string) => string = identity) {
word(
word: string | number | bigint,
color: (s: string) => string = identity
) {
const last = this.wordsInSexpr.length - 1;
if (this.wordsInSexpr[last] > 0 && !this.freshLinebreak) {
// The first word hugs the left parenthesis.
@ -436,9 +439,16 @@ function printInstr(instr: Instr, f: FmtCtx) {
case "call": {
f.controlFlow(instr.kind);
f.word(instr.func);
const name = f.mod.funcs[instr.func]?._name;
if (name !== undefined) {
f.comment(name);
if (instr.func < f.mod.imports.length) {
const name = f.mod.imports[instr.func]?.name;
if (name !== undefined) {
f.comment(name);
}
} else {
const name = f.mod.funcs[instr.func - f.mod.imports.length]?._name;
if (name !== undefined) {
f.comment(name);
}
}
break;
}