2024-12-10 23:30:15 +01:00
|
|
|
import { Ops, opToString } from "./arch.ts";
|
2024-12-10 15:45:19 +01:00
|
|
|
|
2024-12-11 00:03:19 +01:00
|
|
|
export type Line = { labels?: string[]; ins: Ins };
|
2024-12-10 15:45:19 +01:00
|
|
|
|
2024-12-11 00:03:19 +01:00
|
|
|
export type Ins = Lit[];
|
2024-12-10 15:45:19 +01:00
|
|
|
|
2024-12-11 00:03:19 +01:00
|
|
|
export type Label = { label: string };
|
|
|
|
|
|
|
|
export type Lit = number | string | boolean | Label;
|
2024-12-10 15:45:19 +01:00
|
|
|
|
|
|
|
export class Assembler {
|
|
|
|
private lines: Line[] = [];
|
2024-12-11 00:03:19 +01:00
|
|
|
private addedLabels: string[] = [];
|
2024-12-10 15:45:19 +01:00
|
|
|
private labelCounter = 0;
|
|
|
|
|
2024-12-11 00:03:19 +01:00
|
|
|
public add(...ins: Ins): Assembler {
|
|
|
|
if (this.addedLabels.length > 0) {
|
|
|
|
this.lines.push({ ins, labels: this.addedLabels });
|
|
|
|
this.addedLabels = [];
|
2024-12-10 15:45:19 +01:00
|
|
|
return this;
|
|
|
|
}
|
2024-12-11 00:03:19 +01:00
|
|
|
this.lines.push({ ins });
|
2024-12-10 15:45:19 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public concat(assembler: Assembler) {
|
2024-12-11 00:03:19 +01:00
|
|
|
if (assembler.lines.length < 0) {
|
|
|
|
return;
|
2024-12-10 15:45:19 +01:00
|
|
|
}
|
2024-12-11 00:03:19 +01:00
|
|
|
if (assembler.lines[0].labels !== undefined) {
|
|
|
|
this.addedLabels.push(...assembler.lines[0].labels);
|
2024-12-10 15:45:19 +01:00
|
|
|
}
|
2024-12-11 00:03:19 +01:00
|
|
|
this.add(...assembler.lines[0].ins);
|
|
|
|
this.lines.push(...assembler.lines.slice(1));
|
2024-12-10 23:30:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public makeLabel(): Label {
|
2024-12-11 00:03:19 +01:00
|
|
|
return { label: `.L${(this.labelCounter++).toString()}` };
|
2024-12-10 23:30:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public setLabel({ label }: Label) {
|
|
|
|
this.addedLabels.push(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
public assemble(): number[] {
|
2024-12-11 03:11:00 +01:00
|
|
|
console.log("Assembling...");
|
2024-12-10 23:30:15 +01:00
|
|
|
let ip = 0;
|
|
|
|
const output: number[] = [];
|
2024-12-11 00:03:19 +01:00
|
|
|
const locs: { [key: string]: number } = {};
|
|
|
|
const refs: { [key: number]: string } = {};
|
2024-12-11 03:11:00 +01:00
|
|
|
|
|
|
|
const debugLines: {
|
|
|
|
startIp: number;
|
|
|
|
endIp: number;
|
|
|
|
insString: string;
|
|
|
|
}[] = [];
|
|
|
|
|
2024-12-10 23:30:15 +01:00
|
|
|
for (const line of this.lines) {
|
2024-12-11 00:03:19 +01:00
|
|
|
for (const label of line.labels ?? []) {
|
|
|
|
locs[label] = ip;
|
|
|
|
}
|
2024-12-11 03:11:00 +01:00
|
|
|
const startIp = ip;
|
2024-12-10 23:30:15 +01:00
|
|
|
for (const lit of line.ins as Lit[]) {
|
|
|
|
if (typeof lit === "number") {
|
|
|
|
output.push(lit);
|
|
|
|
ip += 1;
|
|
|
|
} else if (typeof lit === "boolean") {
|
|
|
|
output.push(lit ? 1 : 0);
|
|
|
|
ip += 1;
|
|
|
|
} else if (typeof lit === "string") {
|
|
|
|
for (let i = 0; i < lit.length; ++i) {
|
|
|
|
output.push(lit.charCodeAt(i));
|
|
|
|
ip += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output.push(0);
|
|
|
|
refs[ip] = lit.label;
|
|
|
|
ip += 1;
|
|
|
|
}
|
|
|
|
}
|
2024-12-11 03:11:00 +01:00
|
|
|
debugLines.push({
|
|
|
|
startIp,
|
|
|
|
endIp: ip,
|
|
|
|
insString: this.insToString(line.ins),
|
|
|
|
});
|
2024-12-10 23:30:15 +01:00
|
|
|
}
|
|
|
|
for (let i = 0; i < output.length; ++i) {
|
|
|
|
if (!(i in refs)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(refs[i] in locs)) {
|
|
|
|
console.error(
|
|
|
|
`Assembler: label '${refs[i]}' used at ${i} not defined`,
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
output[i] = locs[refs[i]];
|
|
|
|
}
|
2024-12-11 03:11:00 +01:00
|
|
|
|
|
|
|
for (const line of debugLines) {
|
|
|
|
console.log(
|
|
|
|
line.startIp.toString().padStart(3, " ") + " " +
|
|
|
|
output.slice(line.startIp, line.endIp).join(", ") + "\n" +
|
|
|
|
line.insString + "\n",
|
|
|
|
);
|
|
|
|
}
|
2024-12-10 23:30:15 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
public printProgram() {
|
|
|
|
for (const line of this.lines) {
|
|
|
|
for (const label of line.labels ?? []) {
|
2024-12-11 00:03:19 +01:00
|
|
|
console.log(`${label}:`);
|
2024-12-10 23:30:15 +01:00
|
|
|
}
|
2024-12-11 00:03:19 +01:00
|
|
|
const op = opToString(line.ins[0] as number)
|
|
|
|
.padEnd(13, " ");
|
2024-12-10 23:30:15 +01:00
|
|
|
const args = (line.ins.slice(1) as Lit[]).map((lit) => {
|
|
|
|
if (typeof lit === "number") {
|
|
|
|
return lit;
|
|
|
|
} else if (typeof lit === "boolean") {
|
|
|
|
return lit.toString();
|
|
|
|
} else if (typeof lit === "string") {
|
|
|
|
return '"' +
|
|
|
|
lit.replaceAll("\\", "\\\\").replaceAll("\0", "\\0")
|
|
|
|
.replaceAll("\n", "\\n").replaceAll("\t", "\\t")
|
|
|
|
.replaceAll("\r", "\\r") +
|
|
|
|
'"';
|
|
|
|
} else {
|
2024-12-11 00:03:19 +01:00
|
|
|
return lit.label;
|
2024-12-10 23:30:15 +01:00
|
|
|
}
|
|
|
|
}).join(", ");
|
|
|
|
console.log(` ${op} ${args}`);
|
|
|
|
}
|
|
|
|
}
|
2024-12-11 03:11:00 +01:00
|
|
|
|
|
|
|
private insToString(ins: Ins): string {
|
|
|
|
const op = opToString(ins[0] as number)
|
|
|
|
.padEnd(13, " ");
|
|
|
|
const args = (ins.slice(1) as Lit[]).map((lit) => {
|
|
|
|
if (typeof lit === "number") {
|
|
|
|
return lit;
|
|
|
|
} else if (typeof lit === "boolean") {
|
|
|
|
return lit.toString();
|
|
|
|
} else if (typeof lit === "string") {
|
|
|
|
return '"' +
|
|
|
|
lit.replaceAll("\\", "\\\\").replaceAll("\0", "\\0")
|
|
|
|
.replaceAll("\n", "\\n").replaceAll("\t", "\\t")
|
|
|
|
.replaceAll("\r", "\\r") +
|
|
|
|
'"';
|
|
|
|
} else {
|
|
|
|
return lit.label;
|
|
|
|
}
|
|
|
|
}).join(", ");
|
|
|
|
return ` ${op} ${args}`;
|
|
|
|
}
|
2024-12-10 23:30:15 +01:00
|
|
|
}
|