Compare commits
5 Commits
7828b0f112
...
6ee4adfea5
| Author | SHA1 | Date | |
|---|---|---|---|
|
6ee4adfea5
|
|||
|
5dc0a7a601
|
|||
|
896a1ca563
|
|||
|
37d56988ef
|
|||
|
6c643f8402
|
@@ -280,15 +280,14 @@ class Parser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
integer(token) {
|
integer(token) {
|
||||||
let base;
|
let base, regex;
|
||||||
switch (token.slice(-1)) {
|
switch (token.slice(-1)) {
|
||||||
case "b": base = 2; break;
|
case "b": base = 2; regex = /^-?[01]+b$/; break;
|
||||||
case "o": base = 8; break;
|
case "o": base = 8; regex = /^-?[0-7]+o$/; break;
|
||||||
case "h": base = 16; break;
|
case "h": base = 16; regex = /^-?[0-9A-F]+h$/; break;
|
||||||
default: base = 10; break;
|
default: base = 10; regex = /^-?[0-9]+d?$/; break;
|
||||||
}
|
}
|
||||||
const x = parseInt(token, base);
|
return regex.test(token) ? parseInt(token, base) : null;
|
||||||
return Number.isNaN(x) ? null : x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
translate_code(token) {
|
translate_code(token) {
|
||||||
@@ -932,6 +931,7 @@ export class Assembler {
|
|||||||
this.types = [];
|
this.types = [];
|
||||||
this.type_bindings = {};
|
this.type_bindings = {};
|
||||||
this.tables = {};
|
this.tables = {};
|
||||||
|
this.unresolved = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
action_append(action) {
|
action_append(action) {
|
||||||
@@ -1037,9 +1037,14 @@ export class Assembler {
|
|||||||
}
|
}
|
||||||
value = this.lookup_def(action.symbol);
|
value = this.lookup_def(action.symbol);
|
||||||
if (value == undefined) {
|
if (value == undefined) {
|
||||||
console.error(
|
this.unresolved.push({
|
||||||
`ERROR: Unable to resolve symbol ${action.symbol}`);
|
type: "data",
|
||||||
return;
|
size: action.size,
|
||||||
|
symbol: action.symbol,
|
||||||
|
target: data,
|
||||||
|
offset: data.length,
|
||||||
|
});
|
||||||
|
value = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bytes = this.le(value, action.size);
|
bytes = this.le(value, action.size);
|
||||||
@@ -1327,7 +1332,7 @@ export class Assembler {
|
|||||||
opcodes["i32.const"],
|
opcodes["i32.const"],
|
||||||
...this.leb128(loc.addr),
|
...this.leb128(loc.addr),
|
||||||
opcodes["end"],
|
opcodes["end"],
|
||||||
data.length,
|
...this.uleb128(data.length),
|
||||||
...data,
|
...data,
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
@@ -1335,6 +1340,7 @@ export class Assembler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wasm() {
|
wasm() {
|
||||||
|
this.resolve_refs();
|
||||||
this.resolve_func_types();
|
this.resolve_func_types();
|
||||||
|
|
||||||
const template = [
|
const template = [
|
||||||
@@ -1403,4 +1409,32 @@ export class Assembler {
|
|||||||
return acc;
|
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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
-2023
File diff suppressed because it is too large
Load Diff
+2088
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user