Compare commits

..

5 Commits

4 changed files with 2142 additions and 2034 deletions

56
asm.js
View File

@@ -280,15 +280,14 @@ class Parser {
}
integer(token) {
let base;
let base, regex;
switch (token.slice(-1)) {
case "b": base = 2; break;
case "o": base = 8; break;
case "h": base = 16; break;
default: base = 10; break;
case "b": base = 2; regex = /^-?[01]+b$/; break;
case "o": base = 8; regex = /^-?[0-7]+o$/; break;
case "h": base = 16; regex = /^-?[0-9A-F]+h$/; break;
default: base = 10; regex = /^-?[0-9]+d?$/; break;
}
const x = parseInt(token, base);
return Number.isNaN(x) ? null : x;
return regex.test(token) ? parseInt(token, base) : null;
}
translate_code(token) {
@@ -932,6 +931,7 @@ export class Assembler {
this.types = [];
this.type_bindings = {};
this.tables = {};
this.unresolved = [];
}
action_append(action) {
@@ -1037,9 +1037,14 @@ export class Assembler {
}
value = this.lookup_def(action.symbol);
if (value == undefined) {
console.error(
`ERROR: Unable to resolve symbol ${action.symbol}`);
return;
this.unresolved.push({
type: "data",
size: action.size,
symbol: action.symbol,
target: data,
offset: data.length,
});
value = 0;
}
}
bytes = this.le(value, action.size);
@@ -1327,7 +1332,7 @@ export class Assembler {
opcodes["i32.const"],
...this.leb128(loc.addr),
opcodes["end"],
data.length,
...this.uleb128(data.length),
...data,
]
});
@@ -1335,6 +1340,7 @@ export class Assembler {
}
wasm() {
this.resolve_refs();
this.resolve_func_types();
const template = [
@@ -1403,4 +1409,32 @@ export class Assembler {
return acc;
}, []);
}
resolve_refs() {
const failed = [];
for (const ref of this.unresolved) {
if (ref.type != "data") {
console.error(
`ERROR: Unsupported ref type ${ref.type} for `
+ `symbol ${ref.symbol}`
);
failed.push(ref.symbol);
continue;
}
const value = this.defs[ref.symbol];
if (value == undefined) {
failed.push(ref.symbol);
continue;
}
const bytes = this.le(value, ref.size);
ref.target.splice(ref.offset, ref.size, ...bytes);
}
if (failed.length != 0) {
const failed_str = failed.join(" ");
console.error(`ERROR: Unable to resolve refs: ${failed_str}`);
}
}
}

9
driver.js Normal file
View File

@@ -0,0 +1,9 @@
import { Assembler } from "./asm.js";
import { writeAll } from "jsr:@std/io/write-all";
const asm = new Assembler();
for await (const chunk of Deno.stdin.readable) {
asm.push(chunk);
}
const wasm = asm.wasm();
await writeAll(Deno.stdout, wasm);

File diff suppressed because it is too large Load Diff

2088
wipforth.ws Normal file

File diff suppressed because it is too large Load Diff