From ab48259a5a79121996e462c4b632a68de763cf15 Mon Sep 17 00:00:00 2001 From: sfja Date: Wed, 29 Apr 2026 15:24:13 +0200 Subject: [PATCH] fix tests --- src/codegen/x86_64/isel.ts | 60 ++++++++++++++++++++++++-------------- src/main.ts | 9 +++--- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/codegen/x86_64/isel.ts b/src/codegen/x86_64/isel.ts index c56ee3a..c40d6a5 100644 --- a/src/codegen/x86_64/isel.ts +++ b/src/codegen/x86_64/isel.ts @@ -1,4 +1,5 @@ import * as mir from "../../mir.ts"; +import { Ty } from "../../ty.ts"; import { Block, Fn, Inst, Reg, Regs } from "./module.ts"; export function selectFnInstructions(fn: mir.Fn): Fn { @@ -74,30 +75,11 @@ class InstructionSelector { }; this.fn.visit({ - visitInst(inst) { + visitInst: (inst) => { if (inst.kind.tag === "Alloca") { const ty = inst.ty.as("PtrMut").kind.ty; - switch (ty.kind.tag) { - case "Int": { - switch (ty.kind.intTy) { - case "i32": - pushLocal(inst, 4, 4); - break; - case "i64": - pushLocal(inst, 8, 8); - break; - default: - throw new Error( - `not implemented (${ty.kind.intTy})`, - ); - } - break; - } - default: - throw new Error( - `not implemented (${ty.kind.tag})`, - ); - } + const [size, align] = this.tySizeAlign(ty); + pushLocal(inst, size, align); } }, }); @@ -109,6 +91,40 @@ class InstructionSelector { this.localsSize = -offset; } + private tySizeAlign(ty: Ty): [number, number] { + switch (ty.kind.tag) { + case "Int": { + switch (ty.kind.intTy) { + case "i32": + return [4, 4]; + case "i64": + return [8, 8]; + case "u8": + return [1, 1]; + default: + throw new Error( + `not implemented (${ty.kind.intTy})`, + ); + } + } + case "Bool": { + return [1, 1]; + } + case "Ptr": + case "PtrMut": { + return [8, 8]; + } + case "Array": { + const [innerSize, align] = this.tySizeAlign(ty.kind.ty); + return [innerSize * ty.kind.length, align]; + } + default: + throw new Error( + `not implemented (${ty.kind.tag})`, + ); + } + } + private generateBasicBlock(bb: mir.BasicBlock): Block { const regs = new Map(); diff --git a/src/main.ts b/src/main.ts index 5ea0385..74fdc31 100644 --- a/src/main.ts +++ b/src/main.ts @@ -51,10 +51,11 @@ if (Deno.args.includes("--print-mir")) { } const result = selectFnInstructions(mainMiddleFn); -stringify.printWithConsoleColors( - stringify.x86_64FnPretty(result, stringify.consoleColors), -); -// console.log(JSON.stringify(result, null, 4)); +if (Deno.args.includes("--print-isel")) { + stringify.printWithConsoleColors( + stringify.x86_64FnPretty(result, stringify.consoleColors), + ); +} if (!reporter.ok()) { reporter.printAll();