compiler: example

This commit is contained in:
sfja 2025-03-26 23:30:40 +01:00
parent ad06f80f72
commit cb0406e180
3 changed files with 24 additions and 11 deletions

View File

@ -299,15 +299,15 @@ export class AsmGen {
} }
} }
private relative(offset: number): string {
return `[rbp${offset >= 0 ? `+${offset}` : `${offset}`}]`;
}
private kill(reg: lir.Reg) { private kill(reg: lir.Reg) {
this.liveRegs.delete(reg); this.liveRegs.delete(reg);
this.regSelect.delete(reg); this.regSelect.delete(reg);
} }
private relative(offset: number): string {
return `[rbp${offset >= 0 ? `+${offset}` : `${offset}`}]`;
}
private writeIns(ins: string) { private writeIns(ins: string) {
this.writer.writeln(` ${ins}`); this.writer.writeln(` ${ins}`);
} }

View File

@ -9,8 +9,8 @@ import {
} from "./lir.ts"; } from "./lir.ts";
export function optimizeLir(program: Program) { export function optimizeLir(program: Program) {
//console.log("=== BEFORE OPTIMIZATION ==="); console.log("=== BEFORE OPTIMIZATION ===");
//console.log(new ProgramStringifyer(program).stringify()); console.log(new ProgramStringifyer(program).stringify());
let sizeBefore = program.fns let sizeBefore = program.fns
.reduce((acc, fn) => acc + fn.lines.length, 0); .reduce((acc, fn) => acc + fn.lines.length, 0);
@ -34,8 +34,8 @@ export function optimizeLir(program: Program) {
sizeHistory.add(sizeBefore); sizeHistory.add(sizeBefore);
} }
//console.log("=== AFTER OPTIMIZATION ==="); console.log("=== AFTER OPTIMIZATION ===");
//console.log(new ProgramStringifyer(program).stringify()); console.log(new ProgramStringifyer(program).stringify());
} }
function eliminatePushPop(fn: Fn) { function eliminatePushPop(fn: Fn) {

View File

@ -6,15 +6,28 @@ fn notice() -> int {}
fn print_int(value: int) -> int {} fn print_int(value: int) -> int {}
#[c_function("println")] #[c_function("println")]
fn println(value: *str) -> int {} fn println(value: *str) -> int {}
fn add(a: int, b: int) -> int { return a + b; }
fn compute() -> int { return 0; }
fn consume(value: int) -> int { return 0; }
#[c_export("sbc_main")] #[c_export("sbc_main")]
fn main() -> int { fn main() -> int {
println("hello_world"); let i = 0;
loop {
if 10 < i + 1 {
break;
}
println("hello\ world");
i = i + 1;
}
return 0; return 0;
} }