slige/compiler/assembler.ts

114 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-12-10 22:30:15 +00:00
import { Ops, opToString } from "./arch.ts";
2024-12-10 14:45:19 +00:00
2024-12-10 23:03:19 +00:00
export type Line = { labels?: string[]; ins: Ins };
2024-12-10 14:45:19 +00:00
2024-12-10 23:03:19 +00:00
export type Ins = Lit[];
2024-12-10 14:45:19 +00:00
2024-12-10 23:03:19 +00:00
export type Label = { label: string };
export type Lit = number | string | boolean | Label;
2024-12-10 14:45:19 +00:00
export class Assembler {
private lines: Line[] = [];
2024-12-10 23:03:19 +00:00
private addedLabels: string[] = [];
2024-12-10 14:45:19 +00:00
private labelCounter = 0;
2024-12-10 23:03:19 +00:00
public add(...ins: Ins): Assembler {
if (this.addedLabels.length > 0) {
this.lines.push({ ins, labels: this.addedLabels });
this.addedLabels = [];
2024-12-10 14:45:19 +00:00
return this;
}
2024-12-10 23:03:19 +00:00
this.lines.push({ ins });
2024-12-10 14:45:19 +00:00
return this;
}
public concat(assembler: Assembler) {
2024-12-10 23:03:19 +00:00
if (assembler.lines.length < 0) {
return;
2024-12-10 14:45:19 +00:00
}
2024-12-10 23:03:19 +00:00
if (assembler.lines[0].labels !== undefined) {
this.addedLabels.push(...assembler.lines[0].labels);
2024-12-10 14:45:19 +00:00
}
2024-12-10 23:03:19 +00:00
this.add(...assembler.lines[0].ins);
this.lines.push(...assembler.lines.slice(1));
2024-12-10 22:30:15 +00:00
}
public makeLabel(): Label {
2024-12-10 23:03:19 +00:00
return { label: `.L${(this.labelCounter++).toString()}` };
2024-12-10 22:30:15 +00:00
}
public setLabel({ label }: Label) {
this.addedLabels.push(label);
}
public assemble(): number[] {
let ip = 0;
const output: number[] = [];
2024-12-10 23:03:19 +00:00
const locs: { [key: string]: number } = {};
const refs: { [key: number]: string } = {};
2024-12-10 22:30:15 +00:00
for (const line of this.lines) {
2024-12-10 23:03:19 +00:00
for (const label of line.labels ?? []) {
locs[label] = ip;
}
2024-12-10 22:30:15 +00: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;
}
}
}
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]];
}
return output;
}
public printProgram() {
for (const line of this.lines) {
for (const label of line.labels ?? []) {
2024-12-10 23:03:19 +00:00
console.log(`${label}:`);
2024-12-10 22:30:15 +00:00
}
2024-12-10 23:03:19 +00:00
const op = opToString(line.ins[0] as number)
.padEnd(13, " ");
2024-12-10 22:30:15 +00: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-10 23:03:19 +00:00
return lit.label;
2024-12-10 22:30:15 +00:00
}
}).join(", ");
console.log(` ${op} ${args}`);
}
}
}