diff --git a/asm/amd64/codegen.go b/asm/amd64/codegen.go index f0474cc..0ced5e4 100644 --- a/asm/amd64/codegen.go +++ b/asm/amd64/codegen.go @@ -104,9 +104,10 @@ func cgInstruction(i ttir.Instruction) []Instruction { return []Instruction{JmpInstruction(i)} case *ttir.Copy: return []Instruction{&SimpleInstruction{Opcode: Mov, Lhs: toAsmOperand(i.Dst), Rhs: toAsmOperand(i.Src)}} + default: + panic(fmt.Sprintf("unexpected ttir.Instruction: %#v", i)) } - return []Instruction{} } func cgBinary(b *ttir.Binary) []Instruction { diff --git a/test.tt b/test.tt index e065faf..8bce7bd 100644 --- a/test.tt +++ b/test.tt @@ -1,8 +1,9 @@ fn main() = { hi := 4; - if hi == 2 in hi = 3 - else hi = 2; + if hi == 4 in + hi = 0 + else hi = 1; hi }; diff --git a/ttir/emit.go b/ttir/emit.go index 551059b..e08a653 100644 --- a/ttir/emit.go +++ b/ttir/emit.go @@ -105,8 +105,11 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) { instructions = append(instructions, &JumpIfZero{Value: condDst, Label: elseLabel}) thenDst, thenInstructions := emitExpression(expr.Then) instructions = append(instructions, thenInstructions...) - if !expr.ReturnType.IsSameType(types.Unit) { - instructions = append(instructions, &Copy{Src: thenDst, Dst: dst}, Jump(endOfIfLabel)) + if expr.Else != nil { + if !expr.ReturnType.IsSameType(types.Unit) { + instructions = append(instructions, &Copy{Src: thenDst, Dst: dst}) + } + instructions = append(instructions, Jump(endOfIfLabel)) } else { dst = nil } @@ -122,8 +125,21 @@ func emitExpression(expr tast.Expression) (Operand, []Instruction) { instructions = append(instructions, Label(endOfIfLabel)) return dst, instructions case *tast.AssignmentExpression: + ident := expr.Lhs.(*tast.VariableReference) + + rhsDst, instructions := emitExpression(expr.Rhs) + + instructions = append(instructions, &Copy{Src: rhsDst, Dst: &Var{Value: ident.Identifier}}) + + return nil, instructions case *tast.VariableDeclaration: + rhsDst, instructions := emitExpression(expr.InitializingExpression) + + instructions = append(instructions, &Copy{Src: rhsDst, Dst: &Var{Value: expr.Identifier}}) + + return nil, instructions case *tast.VariableReference: + return &Var{Value: expr.Identifier}, []Instruction{} default: panic(fmt.Sprintf("unexpected tast.Expression: %#v", expr)) }