assembler - Change to better catch lines that are missing operands, and provide a useful error message

This commit is contained in:
n loewen 2023-08-24 15:00:33 +01:00
parent 9268b2c59a
commit 0fca21dfab
1 changed files with 34 additions and 26 deletions

View File

@ -76,13 +76,15 @@ function preparseSourceCode(source) {
} }
return lines.map((line, index) => { return lines.map((line, index) => {
dbg(1, ` ${line}`); dbg(1, ` in: ${line}`);
let info = { let info = {
number: index + 1, number: index + 1,
source: line, source: line,
sanitized: stripWhitespaceFromEnds(stripComments(line)), sanitized: stripWhitespaceFromEnds(stripComments(line)),
type: getLineType(line), type: getLineType(line),
}; };
dbg(1, `${info.number} - ${info.type}: ${info.sanitized}`);
dbg(1, ``);
if (info.type === 'code') { if (info.type === 'code') {
const op_arg_array = info.sanitized.split(/\s+/); // split line into an array of [op, arg] const op_arg_array = info.sanitized.split(/\s+/); // split line into an array of [op, arg]
@ -147,6 +149,7 @@ function handleConstantDefinitions(op, arg, IP, constants) {
constantValue = IP.toString(); constantValue = IP.toString();
} }
constants[constantName] = constantValue; constants[constantName] = constantValue;
dbg(1, '');
dbg(1, `Constants:`); dbg(1, `Constants:`);
dbg(1, constants); dbg(1, constants);
dbg(1, ''); dbg(1, '');
@ -208,9 +211,24 @@ function decodeInstructions(source) {
// dbg(2, line); // dbg(2, line);
if (line.type === 'code') { if (line.type === 'code') {
const op = line.operation; const op = line.operation;
let arg = null;
let sourceArgument; // TODO remove null?
if (typeof line.argument != 'undefined') { if (typeof line.argument != 'undefined') {
arg = line.argument; sourceArgument = line.argument;
} else {
// If this isn't a label definition,
// or one of the ops with optional arguments,
// then it's an error
if (!line.operation.startsWith('@')) {
if (mnemonicsWithOptionalArgs.indexOf(line.operation) < 0) {
console.error(`Missing opcode for line ${line.number}: ${line.source}`);
throw new Error("Missing opcode");
} else {
// It *is* one of the special optional-arg ops
// So let's fill in the implicit operand with $00
sourceArgument = '0';
}
}
} }
@ -224,13 +242,13 @@ function decodeInstructions(source) {
// Opcodes - Handle constant definitions // Opcodes - Handle constant definitions
if (op.startsWith(ASM_CONSTANT_PREFIX)) { if (op.startsWith(ASM_CONSTANT_PREFIX)) {
constants = handleConstantDefinitions(op, arg, IP, constants); constants = handleConstantDefinitions(op, sourceArgument, IP, constants);
continue; continue;
} }
// Opcodes - Handle setting value of IP // Opcodes - Handle setting value of IP
if (op.startsWith(ASM_IP_LABEL)) { if (op.startsWith(ASM_IP_LABEL)) {
IP = parseInt(arg); IP = parseInt(sourceArgument);
continue; continue;
} }
@ -248,17 +266,8 @@ function decodeInstructions(source) {
// Now that it can't be a label or a constant, normalize the opcode // Now that it can't be a label or a constant, normalize the opcode
line.operation = line.operation.toLowerCase(); line.operation = line.operation.toLowerCase();
// Opcodes - Handle mnemonics without operands (eg END)
if (arg === null) {
if (mnemonicsWithOptionalArgs.indexOf(line.operation) < 0) {
console.error(`Missing opcode for line ${line.number}: ${line.source}`);
throw new Error("Missing opcode");
}
decodedArg = 0;
}
// Operands - Handle references to labels // Operands - Handle references to labels
if (arg !== null && arg.startsWith(ASM_LABEL_PREFIX)) { if (sourceArgument.startsWith(ASM_LABEL_PREFIX)) {
let label = line.argument.substring(1); // strip label prefix let label = line.argument.substring(1); // strip label prefix
if (label in labels) { if (label in labels) {
dbg(1, `'${label}' already in labels object`); dbg(1, `'${label}' already in labels object`);
@ -276,30 +285,30 @@ function decodeInstructions(source) {
} }
// Operands - Handle references to the Instruction Pointer // Operands - Handle references to the Instruction Pointer
if (arg !== null && arg === ASM_IP_LABEL) { if (sourceArgument === ASM_IP_LABEL) {
dbg(1, ` References current IP - ${IP}`); dbg(1, ` References current IP - ${IP}`);
decodedArg = IP; decodedArg = IP;
} }
// Operands - Handle references to constants // Operands - Handle references to constants
if (arg !== null && arg.startsWith(ASM_CONSTANT_PREFIX)) { if (sourceArgument.startsWith(ASM_CONSTANT_PREFIX)) {
dbg(1, `References '${arg}'`); dbg(1, `References '${sourceArgument}'`);
decodedArg = decodeNumericOp(constants[arg.substring(1)]); // substring(1) strips '>' decodedArg = decodeNumericOp(constants[sourceArgument.substring(1)]); // substring(1) strips '>'
} }
// Operands - Handle references to constants in indirect mode // Operands - Handle references to constants in indirect mode
if (arg !== null && arg.startsWith(`(${ASM_CONSTANT_PREFIX}`)) { if (sourceArgument.startsWith(`(${ASM_CONSTANT_PREFIX}`)) {
addressingMode = "indirect"; addressingMode = "indirect";
dbg(1, `(Indirectly) References '${arg}'`); dbg(1, `(Indirectly) References '${sourceArgument}'`);
let constName = arg.replace(`(${ASM_CONSTANT_PREFIX}`, ""); let constName = sourceArgument.replace(`(${ASM_CONSTANT_PREFIX}`, "");
constName = constName.replace(")", ""); constName = constName.replace(")", "");
decodedArg = decodeNumericOp(constants[constName]); decodedArg = decodeNumericOp(constants[constName]);
} }
// Operands - Handle indirect expressions // Operands - Handle indirect expressions
if (arg !== null && decodedArg === null && arg.startsWith("(")) { if (decodedArg === null && sourceArgument.startsWith("(")) {
addressingMode = "indirect"; addressingMode = "indirect";
let indyTemp = arg.replace("(", "").replace(")", ""); let indyTemp = sourceArgument.replace("(", "").replace(")", "");
decodedArg = decodeNumericOp(indyTemp); decodedArg = decodeNumericOp(indyTemp);
} }
@ -308,13 +317,11 @@ function decodeInstructions(source) {
decodedOp = mnemonics2opcodes[line.operation][addressingMode]; decodedOp = mnemonics2opcodes[line.operation][addressingMode];
} }
// Decode regular operands // Decode regular operands
if (decodedArg === null) { if (decodedArg === null) {
decodedArg = decodeNumericOp(line.argument); decodedArg = decodeNumericOp(line.argument);
} }
machineCode[IP] = decodedOp; machineCode[IP] = decodedOp;
machineCode[IP + 1] = decodedArg; machineCode[IP + 1] = decodedArg;
@ -340,6 +347,7 @@ function decodeInstructions(source) {
}; };
} }
dbg(1, '');
dbgGroup(1, 'Memory before filling in label constants'); dbgGroup(1, 'Memory before filling in label constants');
dbgExec(1, () => logMemory(new Uint8Array(machineCode))); dbgExec(1, () => logMemory(new Uint8Array(machineCode)));
dbgGroupEnd(1); dbgGroupEnd(1);