tt/asm/amd64/codegen.go
2025-01-21 20:57:51 +01:00

60 lines
981 B
Go

package amd64
import (
"fmt"
"robaertschi.xyz/robaertschi/tt/ttir"
)
func toAsmOperand(op ttir.Operand) Operand {
switch op := op.(type) {
case *ttir.Constant:
return Imm(op.Value)
default:
panic(fmt.Sprintf("unkown operand %T", op))
}
}
func CgProgram(prog *ttir.Program) Program {
funcs := make([]Function, 0)
for _, f := range prog.Functions {
funcs = append(funcs, cgFunction(f))
}
return Program{
Functions: funcs,
}
}
func cgFunction(f ttir.Function) Function {
newInstructions := []Instruction{}
for _, inst := range f.Instructions {
newInstructions = append(newInstructions, cgInstruction(inst)...)
}
return Function{
Name: f.Name,
Instructions: newInstructions,
}
}
func cgInstruction(i ttir.Instruction) []Instruction {
switch i := i.(type) {
case *ttir.Ret:
return []Instruction{
{
Opcode: Mov,
Lhs: AX,
Rhs: toAsmOperand(i.Op),
},
{
Opcode: Ret,
},
}
}
return []Instruction{}
}