diff --git a/asm.js b/asm.js index 5a013d0..4b89ace 100644 --- a/asm.js +++ b/asm.js @@ -483,9 +483,7 @@ class Parser { const action = { type: Action.DATA, size: 1 }; const value = this.integer(token); if (value == null) { - console.error( - `ERROR: Unexpected token ${token}, expected value`); - return; + action.symbol = token; } else { if (value > 0xff) console.error(`WARNING: Value ${token} is truncated`); @@ -502,9 +500,7 @@ class Parser { const action = { type: Action.DATA, size: 2 }; const value = this.integer(token); if (value == null) { - console.error( - `ERROR: Unexpected token ${token}, expected value`); - return; + action.symbol = token; } else { if (value > 0xffff) console.error(`WARNING: Value ${token} is truncated`); @@ -723,7 +719,10 @@ export class Assembler { action_data(action) { const data = this.data.at(-1).data; - data.push(...action.value); + const value = action.value != null + ? action.value + : this.le_bytes(this.lookup_def(action.symbol), action.size); + data.push(...value); this.pos.addr += action.size; } @@ -762,6 +761,21 @@ export class Assembler { return index == -1 ? null : index; } + lookup_def(symbol) { + return this.defs[symbol]; + } + + le_bytes(value, count) { + let bytes = [] + while (value != 0 && bytes.length < count) { + bytes.push(value & 0xff); + value >>= 8; + } + while (bytes.length < count) + bytes.push(0); + return bytes; + } + wasm_section_type() { const funcs = Object.values(this.funcs); const contents = funcs.map(({ params, results }) => {