Implemented register negation on arm64

This commit is contained in:
2025-03-17 14:35:12 +01:00
parent 76356b2d38
commit eb27595593
3 changed files with 41 additions and 0 deletions

8
src/arm/Negate.go Normal file
View File

@ -0,0 +1,8 @@
package arm
import "git.urbach.dev/cli/q/src/cpu"
// NegateRegister negates the value in the source register and writes it to the destination register.
func NegateRegister(destination cpu.Register, source cpu.Register) uint32 {
return 0b11001011<<24 | reg3Imm(destination, ZR, source, 0)
}

26
src/arm/Negate_test.go Normal file
View File

@ -0,0 +1,26 @@
package arm_test
import (
"testing"
"git.urbach.dev/cli/q/src/arm"
"git.urbach.dev/cli/q/src/cpu"
"git.urbach.dev/go/assert"
)
func TestNegateRegister(t *testing.T) {
usagePatterns := []struct {
Destination cpu.Register
Source cpu.Register
Code uint32
}{
{arm.X0, arm.X0, 0xCB0003E0},
{arm.X1, arm.X1, 0xCB0103E1},
}
for _, pattern := range usagePatterns {
t.Logf("neg %s, %s", pattern.Destination, pattern.Source)
code := arm.NegateRegister(pattern.Destination, pattern.Source)
assert.DeepEqual(t, code, pattern.Code)
}
}

View File

@ -307,6 +307,13 @@ func (c *compiler) ARM(x asm.Instruction) {
panic("not implemented")
}
case asm.NEGATE:
switch x.Type {
case asm.TypeRegister:
operands := c.assembler.Param.Register[x.Index]
c.append(arm.NegateRegister(operands.Register, operands.Register))
}
default:
panic("unknown mnemonic: " + x.Mnemonic.String())
}