Enable using defs in .byte and .word directives

This commit is contained in:
2026-03-14 13:51:04 +00:00
parent 2972030d0a
commit e2429b2b03

28
asm.js
View File

@@ -483,9 +483,7 @@ class Parser {
const action = { type: Action.DATA, size: 1 }; const action = { type: Action.DATA, size: 1 };
const value = this.integer(token); const value = this.integer(token);
if (value == null) { if (value == null) {
console.error( action.symbol = token;
`ERROR: Unexpected token ${token}, expected value`);
return;
} else { } else {
if (value > 0xff) if (value > 0xff)
console.error(`WARNING: Value ${token} is truncated`); console.error(`WARNING: Value ${token} is truncated`);
@@ -502,9 +500,7 @@ class Parser {
const action = { type: Action.DATA, size: 2 }; const action = { type: Action.DATA, size: 2 };
const value = this.integer(token); const value = this.integer(token);
if (value == null) { if (value == null) {
console.error( action.symbol = token;
`ERROR: Unexpected token ${token}, expected value`);
return;
} else { } else {
if (value > 0xffff) if (value > 0xffff)
console.error(`WARNING: Value ${token} is truncated`); console.error(`WARNING: Value ${token} is truncated`);
@@ -723,7 +719,10 @@ export class Assembler {
action_data(action) { action_data(action) {
const data = this.data.at(-1).data; 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; this.pos.addr += action.size;
} }
@@ -762,6 +761,21 @@ export class Assembler {
return index == -1 ? null : index; 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() { wasm_section_type() {
const funcs = Object.values(this.funcs); const funcs = Object.values(this.funcs);
const contents = funcs.map(({ params, results }) => { const contents = funcs.map(({ params, results }) => {