Implemented more arm64 instructions

This commit is contained in:
2025-03-13 13:17:58 +01:00
parent dc664dbf5a
commit 5a5061c5d7
23 changed files with 290 additions and 53 deletions

View File

@ -8,6 +8,6 @@ main() {
buffer[2] = 'l'
buffer[3] = 'l'
buffer[4] = 'o'
io.write(1, buffer)
io.out(buffer)
mem.free(buffer)
}

View File

@ -2,8 +2,8 @@ package arm
import "git.urbach.dev/cli/q/src/cpu"
// AddRegisterNumber adds a number to a register
// AddRegisterNumber adds a number to a register.
func AddRegisterNumber(destination cpu.Register, source cpu.Register, number int) uint32 {
number &= 0xFFF
number &= 0b1111_1111_1111
return 0b100100010<<23 | (uint32(number) << 10) | (uint32(source) << 5) | uint32(destination)
}

43
src/arm/Jump.go Normal file
View File

@ -0,0 +1,43 @@
package arm
// Jump continues program flow at the new offset.
func Jump(offset int) uint32 {
offset &= 0b11_1111_1111_1111_1111_1111_1111
return 0b000101<<26 | uint32(offset)
}
// JumpIfLess jumps if the result was less.
func JumpIfLess(offset int) uint32 {
return branchCond(0b1001, offset)
}
// JumpIfLessOrEqual jumps if the result was less or equal.
func JumpIfLessOrEqual(offset int) uint32 {
return branchCond(0b1101, offset)
}
// JumpIfGreater jumps if the result was greater.
func JumpIfGreater(offset int) uint32 {
return branchCond(0b1100, offset)
}
// JumpIfGreaterOrEqual jumps if the result was greater or equal.
func JumpIfGreaterOrEqual(offset int) uint32 {
return branchCond(0b1010, offset)
}
// JumpIfEqual jumps if the result was equal.
func JumpIfEqual(offset int) uint32 {
return branchCond(0b0000, offset)
}
// JumpIfNotEqual jumps if the result was not equal.
func JumpIfNotEqual(offset int) uint32 {
return branchCond(0b0001, offset)
}
// branchCond performs a conditional branch to a PC-relative offset.
func branchCond(cond uint32, offset int) uint32 {
offset &= 0b111_1111_1111_1111_1111
return 0b01010100<<24 | uint32(offset)<<5 | cond
}

View File

@ -3,11 +3,20 @@ package arm
import "git.urbach.dev/cli/q/src/cpu"
// LoadRegister loads from memory into a register.
func LoadRegister(destination cpu.Register, base cpu.Register, offset int16, length byte) uint32 {
if offset < 0 {
offset &= 0xFF
offset |= 1 << 8
func LoadRegister(destination cpu.Register, base cpu.Register, offset int, length byte) uint32 {
offset &= 0b1_1111_1111
common := 1<<22 | uint32(offset)<<12 | uint32(base)<<5 | uint32(destination)
switch length {
case 1:
return 0b00111<<27 | common
case 2:
return 0b01111<<27 | common
case 4:
return 0b10111<<27 | common
case 8:
return 0b11111<<27 | common
}
return 0b11111000010<<21 | uint32(offset)<<12 | uint32(base)<<5 | uint32(destination)
panic("invalid length")
}

View File

@ -7,6 +7,6 @@ import "git.urbach.dev/cli/q/src/cpu"
// This is the post-index version of the instruction so the offset is applied to the base register after the memory access.
func LoadPair(reg1 cpu.Register, reg2 cpu.Register, base cpu.Register, offset int) uint32 {
offset /= 8
offset &= 0b1111111
offset &= 0b111_1111
return 0b1010100011<<22 | (uint32(offset) << 15) | (uint32(reg2) << 10) | (uint32(base) << 5) | uint32(reg1)
}

View File

@ -12,10 +12,18 @@ func TestLoadRegister(t *testing.T) {
usagePatterns := []struct {
Destination cpu.Register
Base cpu.Register
Offset int16
Offset int
Length byte
Code uint32
}{
{arm.X0, arm.X1, -8, 1, 0x385F8020},
{arm.X1, arm.X0, -8, 1, 0x385F8001},
{arm.X0, arm.X1, -8, 2, 0x785F8020},
{arm.X1, arm.X0, -8, 2, 0x785F8001},
{arm.X0, arm.X1, -8, 4, 0xB85F8020},
{arm.X1, arm.X0, -8, 4, 0xB85F8001},
{arm.X0, arm.X1, -8, 8, 0xF85F8020},
{arm.X1, arm.X0, -8, 8, 0xF85F8001},
{arm.X2, arm.X1, -8, 8, 0xF85F8022},
{arm.X2, arm.X1, 0, 8, 0xF8400022},
{arm.X2, arm.X1, 8, 8, 0xF8408022},

24
src/arm/Store.go Normal file
View File

@ -0,0 +1,24 @@
package arm
import (
"git.urbach.dev/cli/q/src/cpu"
)
// StoreRegister writes the contents of the register to a memory address.
func StoreRegister(source cpu.Register, base cpu.Register, offset int, length byte) uint32 {
offset &= 0b1_1111_1111
common := uint32(offset)<<12 | uint32(base)<<5 | uint32(source)
switch length {
case 1:
return 0b00111<<27 | common
case 2:
return 0b01111<<27 | common
case 4:
return 0b10111<<27 | common
case 8:
return 0b11111<<27 | common
}
panic("invalid length")
}

View File

@ -9,6 +9,6 @@ import (
// This is the pre-index version of the instruction so the offset is applied to the base register before the memory access.
func StorePair(reg1 cpu.Register, reg2 cpu.Register, base cpu.Register, offset int) uint32 {
offset /= 8
offset &= 0b1111111
offset &= 0b111_1111
return 0b1010100110<<22 | uint32(offset)<<15 | uint32(reg2)<<10 | uint32(base)<<5 | uint32(reg1)
}

34
src/arm/Store_test.go Normal file
View File

@ -0,0 +1,34 @@
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 TestStoreRegister(t *testing.T) {
usagePatterns := []struct {
Source cpu.Register
Base cpu.Register
Offset int
Length byte
Code uint32
}{
{arm.X0, arm.X1, -8, 1, 0x381F8020},
{arm.X1, arm.X0, -8, 1, 0x381F8001},
{arm.X0, arm.X1, -8, 2, 0x781F8020},
{arm.X1, arm.X0, -8, 2, 0x781F8001},
{arm.X0, arm.X1, -8, 4, 0xB81F8020},
{arm.X1, arm.X0, -8, 4, 0xB81F8001},
{arm.X0, arm.X1, -8, 8, 0xF81F8020},
{arm.X1, arm.X0, -8, 8, 0xF81F8001},
}
for _, pattern := range usagePatterns {
t.Logf("stur %s, [%s, #%d] %db", pattern.Source, pattern.Base, pattern.Offset, pattern.Length)
code := arm.StoreRegister(pattern.Source, pattern.Base, pattern.Offset, pattern.Length)
assert.DeepEqual(t, code, pattern.Code)
}
}

9
src/arm/Sub.go Normal file
View File

@ -0,0 +1,9 @@
package arm
import "git.urbach.dev/cli/q/src/cpu"
// SubRegisterNumber subtracts a number from the given register.
func SubRegisterNumber(destination cpu.Register, source cpu.Register, number int) uint32 {
number &= 0b1111_1111_1111
return 0b111100010<<23 | (uint32(number) << 10) | (uint32(source) << 5) | uint32(destination)
}

View File

@ -2,7 +2,6 @@ package asm
import (
"fmt"
"math"
"strconv"
"strings"
@ -22,7 +21,7 @@ func (mem *Memory) Format(custom string) string {
tmp.WriteString("[")
tmp.WriteString(fmt.Sprint(mem.Base))
if mem.OffsetRegister != math.MaxUint8 {
if mem.OffsetRegister >= 0 {
tmp.WriteString("+")
tmp.WriteString(fmt.Sprint(mem.OffsetRegister))
}

View File

@ -3,7 +3,6 @@ package asmc
import (
"encoding/binary"
"fmt"
"math"
"strings"
"git.urbach.dev/cli/q/src/arm"
@ -37,6 +36,9 @@ func (c *compiler) compileARM(x asm.Instruction) {
}
c.codePointers = append(c.codePointers, pointer)
default:
panic("not implemented")
}
case asm.LABEL:
@ -50,14 +52,79 @@ func (c *compiler) compileARM(x asm.Instruction) {
case asm.TypeMemoryRegister:
operands := c.assembler.Param.MemoryRegister[x.Index]
if operands.Address.OffsetRegister == math.MaxUint8 {
c.append(arm.LoadRegister(operands.Register, operands.Address.Base, int16(operands.Address.Offset), operands.Address.Length))
if operands.Address.OffsetRegister < 0 {
c.append(arm.LoadRegister(operands.Register, operands.Address.Base, int(operands.Address.Offset), operands.Address.Length))
} else {
// TODO: LoadDynamicRegister
panic("not implemented")
}
}
case asm.STORE:
switch x.Type {
case asm.TypeMemoryRegister:
operands := c.assembler.Param.MemoryRegister[x.Index]
if operands.Address.OffsetRegister < 0 {
c.append(arm.StoreRegister(operands.Register, operands.Address.Base, int(operands.Address.Offset), operands.Address.Length))
} else {
panic("not implemented")
}
case asm.TypeMemoryNumber:
operands := c.assembler.Param.MemoryNumber[x.Index]
if operands.Address.OffsetRegister < 0 {
c.append(arm.MoveRegisterNumber(arm.X0, operands.Number))
c.append(arm.StoreRegister(arm.X0, operands.Address.Base, int(operands.Address.Offset), operands.Address.Length))
} else {
panic("not implemented")
}
}
case asm.PUSH:
switch x.Type {
case asm.TypeRegister:
operand := c.assembler.Param.Register[x.Index]
c.append(arm.StorePair(operand.Register, operand.Register, arm.SP, -16))
}
case asm.POP:
switch x.Type {
case asm.TypeRegister:
operand := c.assembler.Param.Register[x.Index]
c.append(arm.LoadPair(operand.Register, operand.Register, arm.SP, 16))
}
case asm.ADD:
switch x.Type {
case asm.TypeRegisterNumber:
operand := c.assembler.Param.RegisterNumber[x.Index]
c.append(arm.AddRegisterNumber(operand.Register, operand.Register, operand.Number))
case asm.TypeRegisterRegister:
panic("not implemented")
}
case asm.SUB:
switch x.Type {
case asm.TypeRegisterNumber:
operand := c.assembler.Param.RegisterNumber[x.Index]
c.append(arm.SubRegisterNumber(operand.Register, operand.Register, operand.Number))
case asm.TypeRegisterRegister:
panic("not implemented")
}
case asm.COMPARE:
switch x.Type {
case asm.TypeRegisterNumber:
operand := c.assembler.Param.RegisterNumber[x.Index]
c.append(arm.SubRegisterNumber(0b11111, operand.Register, operand.Number))
case asm.TypeRegisterRegister:
panic("not implemented")
}
case asm.JE, asm.JNE, asm.JG, asm.JGE, asm.JL, asm.JLE, asm.JUMP:
c.jumpARM(x)
case asm.MOVE:
switch x.Type {
case asm.TypeRegisterRegister:

View File

@ -99,7 +99,7 @@ func (c *compiler) compileX86(x asm.Instruction) {
c.dllCall(x)
case asm.JE, asm.JNE, asm.JG, asm.JGE, asm.JL, asm.JLE, asm.JUMP:
c.jump(x)
c.jumpX86(x)
case asm.LABEL:
label := c.assembler.Param.Label[x.Index]

53
src/asmc/jumpARM.go Normal file
View File

@ -0,0 +1,53 @@
package asmc
import (
"fmt"
"git.urbach.dev/cli/q/src/arm"
"git.urbach.dev/cli/q/src/asm"
)
func (c *compiler) jumpARM(x asm.Instruction) {
mnemonic := x.Mnemonic
position := Address(len(c.code))
label := c.assembler.Param.Label[x.Index]
pointer := &pointer{
Position: position,
OpSize: 0,
Size: 4,
}
c.append(arm.Nop())
pointer.Resolve = func() Address {
destination, exists := c.codeLabels[label.Name]
if !exists {
panic(fmt.Sprintf("unknown jump label %s", label.Name))
}
distance := (int(destination) - int(position)) / 4
switch mnemonic {
case asm.JE:
return arm.JumpIfEqual(distance)
case asm.JNE:
return arm.JumpIfNotEqual(distance)
case asm.JG:
return arm.JumpIfGreater(distance)
case asm.JGE:
return arm.JumpIfGreaterOrEqual(distance)
case asm.JL:
return arm.JumpIfLess(distance)
case asm.JLE:
return arm.JumpIfLessOrEqual(distance)
case asm.JUMP:
return arm.Jump(distance)
default:
panic("not implemented")
}
}
c.codePointers = append(c.codePointers, pointer)
}

View File

@ -7,7 +7,7 @@ import (
"git.urbach.dev/cli/q/src/x86"
)
func (c *compiler) jump(x asm.Instruction) {
func (c *compiler) jumpX86(x asm.Instruction) {
switch x.Mnemonic {
case asm.JE:
c.code = x86.Jump8IfEqual(c.code, 0x00)

View File

@ -1,8 +1,6 @@
package asmc
import (
"math"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/x86"
)
@ -12,7 +10,7 @@ func (c *compiler) load(x asm.Instruction) {
case asm.TypeMemoryRegister:
operands := c.assembler.Param.MemoryRegister[x.Index]
if operands.Address.OffsetRegister == math.MaxUint8 {
if operands.Address.OffsetRegister < 0 {
c.code = x86.LoadRegister(c.code, operands.Register, operands.Address.Base, operands.Address.Offset, operands.Address.Length)
} else {
c.code = x86.LoadDynamicRegister(c.code, operands.Register, operands.Address.Base, operands.Address.OffsetRegister, operands.Address.Length)

View File

@ -1,8 +1,6 @@
package asmc
import (
"math"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/config"
"git.urbach.dev/cli/q/src/x86"
@ -13,7 +11,7 @@ func (c *compiler) store(x asm.Instruction) {
case asm.TypeMemoryNumber:
operands := c.assembler.Param.MemoryNumber[x.Index]
if operands.Address.OffsetRegister == math.MaxUint8 {
if operands.Address.OffsetRegister < 0 {
c.code = x86.StoreNumber(c.code, operands.Address.Base, operands.Address.Offset, operands.Address.Length, operands.Number)
} else {
c.code = x86.StoreDynamicNumber(c.code, operands.Address.Base, operands.Address.OffsetRegister, operands.Address.Length, operands.Number)
@ -22,7 +20,7 @@ func (c *compiler) store(x asm.Instruction) {
operands := c.assembler.Param.MemoryLabel[x.Index]
start := len(c.code)
if operands.Address.OffsetRegister == math.MaxUint8 {
if operands.Address.OffsetRegister < 0 {
c.code = x86.StoreNumber(c.code, operands.Address.Base, operands.Address.Offset, operands.Address.Length, 0b00_00_00_00)
} else {
c.code = x86.StoreDynamicNumber(c.code, operands.Address.Base, operands.Address.OffsetRegister, operands.Address.Length, 0b00_00_00_00)
@ -48,7 +46,7 @@ func (c *compiler) store(x asm.Instruction) {
case asm.TypeMemoryRegister:
operands := c.assembler.Param.MemoryRegister[x.Index]
if operands.Address.OffsetRegister == math.MaxUint8 {
if operands.Address.OffsetRegister < 0 {
c.code = x86.StoreRegister(c.code, operands.Address.Base, operands.Address.Offset, operands.Address.Length, operands.Register)
} else {
c.code = x86.StoreDynamicRegister(c.code, operands.Address.Base, operands.Address.OffsetRegister, operands.Address.Length, operands.Register)

View File

@ -2,7 +2,6 @@ package core
import (
"fmt"
"math"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/errors"
@ -25,7 +24,7 @@ func (f *Function) CompileLen(root *expression.Expression) error {
memory := asm.Memory{
Offset: -8,
OffsetRegister: math.MaxUint8,
OffsetRegister: -1,
Length: 8,
}

View File

@ -1,8 +1,6 @@
package core
import (
"math"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/errors"
"git.urbach.dev/cli/q/src/expression"
@ -29,7 +27,7 @@ func (f *Function) CompileMemoryStore(root *expression.Expression) error {
memory := asm.Memory{
Base: variable.Value.Register,
OffsetRegister: math.MaxUint8,
OffsetRegister: -1,
Length: byte(numBytes),
}

View File

@ -2,7 +2,6 @@ package core
import (
"fmt"
"math"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/errors"
@ -25,7 +24,7 @@ func (f *Function) EvaluateArray(expr *expression.Expression) (*eval.Memory, err
memory := asm.Memory{
Base: base.Value.Register,
Offset: 0,
OffsetRegister: math.MaxUint8,
OffsetRegister: -1,
Length: byte(1),
}

View File

@ -2,7 +2,6 @@ package core
import (
"fmt"
"math"
"git.urbach.dev/cli/q/src/asm"
"git.urbach.dev/cli/q/src/errors"
@ -34,7 +33,7 @@ func (f *Function) EvaluateDot(expr *expression.Expression) (eval.Value, error)
Memory: asm.Memory{
Base: variable.Value.Register,
Offset: int8(field.Offset),
OffsetRegister: math.MaxUint8,
OffsetRegister: -1,
Length: byte(field.Type.Size()),
},
}

View File

@ -3,7 +3,7 @@ package cpu
import "fmt"
// Register represents the number of the register.
type Register uint8
type Register int8
// String returns the human readable name of the register.
func (r Register) String() string {

View File

@ -1,37 +1,37 @@
package x86
// Jump continues program flow at the new address.
// The address is relative to the next instruction.
func Jump8(code []byte, address int8) []byte {
return append(code, 0xEB, byte(address))
// Jump continues program flow at the new offset.
// The offset is relative to the next instruction.
func Jump8(code []byte, offset int8) []byte {
return append(code, 0xEB, byte(offset))
}
// JumpIfLess jumps if the result was less.
func Jump8IfLess(code []byte, address int8) []byte {
return append(code, 0x7C, byte(address))
func Jump8IfLess(code []byte, offset int8) []byte {
return append(code, 0x7C, byte(offset))
}
// JumpIfLessOrEqual jumps if the result was less or equal.
func Jump8IfLessOrEqual(code []byte, address int8) []byte {
return append(code, 0x7E, byte(address))
func Jump8IfLessOrEqual(code []byte, offset int8) []byte {
return append(code, 0x7E, byte(offset))
}
// JumpIfGreater jumps if the result was greater.
func Jump8IfGreater(code []byte, address int8) []byte {
return append(code, 0x7F, byte(address))
func Jump8IfGreater(code []byte, offset int8) []byte {
return append(code, 0x7F, byte(offset))
}
// JumpIfGreaterOrEqual jumps if the result was greater or equal.
func Jump8IfGreaterOrEqual(code []byte, address int8) []byte {
return append(code, 0x7D, byte(address))
func Jump8IfGreaterOrEqual(code []byte, offset int8) []byte {
return append(code, 0x7D, byte(offset))
}
// JumpIfEqual jumps if the result was equal.
func Jump8IfEqual(code []byte, address int8) []byte {
return append(code, 0x74, byte(address))
func Jump8IfEqual(code []byte, offset int8) []byte {
return append(code, 0x74, byte(offset))
}
// JumpIfNotEqual jumps if the result was not equal.
func Jump8IfNotEqual(code []byte, address int8) []byte {
return append(code, 0x75, byte(address))
func Jump8IfNotEqual(code []byte, offset int8) []byte {
return append(code, 0x75, byte(offset))
}