From cdbfa744b7ff6a0716a30c76cf372bb2d8728f4f Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 31 Jul 2024 15:36:44 +0200 Subject: [PATCH] Removed unnecessary instructions --- src/build/register/RegisterNumber.go | 29 +++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/src/build/register/RegisterNumber.go b/src/build/register/RegisterNumber.go index 298c4d7..a6b5aa6 100644 --- a/src/build/register/RegisterNumber.go +++ b/src/build/register/RegisterNumber.go @@ -11,20 +11,27 @@ func (f *Machine) RegisterNumber(mnemonic asm.Mnemonic, a cpu.Register, b int) { f.SaveRegister(a) } - if sizeof.Signed(int64(b)) == 8 { - tmp := f.NewRegister() - f.Assembler.RegisterNumber(asm.MOVE, tmp, b) - f.UseRegister(tmp) - f.postInstruction() - f.Assembler.RegisterRegister(mnemonic, a, tmp) - f.postInstruction() - f.FreeRegister(tmp) - } else { + // The `MOVE` operation is very flexible and works with any type of immediate number. + if mnemonic == asm.MOVE { f.Assembler.RegisterNumber(mnemonic, a, b) + f.UseRegister(a) f.postInstruction() + return } - if mnemonic == asm.MOVE { - f.UseRegister(a) + // If the number only needs 32 bits, we can encode the instruction. + if sizeof.Signed(int64(b)) <= 4 { + f.Assembler.RegisterNumber(mnemonic, a, b) + f.postInstruction() + return } + + // If the number needs 64 bits, we need to use a temporary register. + tmp := f.NewRegister() + f.Assembler.RegisterNumber(asm.MOVE, tmp, b) + f.UseRegister(tmp) + f.postInstruction() + f.Assembler.RegisterRegister(mnemonic, a, tmp) + f.postInstruction() + f.FreeRegister(tmp) }