diff --git a/examples/array/array.q b/examples/array/array.q index c81a7b0..69c3e45 100644 --- a/examples/array/array.q +++ b/examples/array/array.q @@ -8,6 +8,6 @@ main() { buffer[2] = 'l' buffer[3] = 'l' buffer[4] = 'o' - io.write(1, buffer) + io.out(buffer) mem.free(buffer) } \ No newline at end of file diff --git a/src/arm/Add.go b/src/arm/Add.go index c97afa8..c05e16e 100644 --- a/src/arm/Add.go +++ b/src/arm/Add.go @@ -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) } diff --git a/src/arm/Jump.go b/src/arm/Jump.go new file mode 100644 index 0000000..78a75db --- /dev/null +++ b/src/arm/Jump.go @@ -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 +} diff --git a/src/arm/Load.go b/src/arm/Load.go index e1d09e1..8b82b98 100644 --- a/src/arm/Load.go +++ b/src/arm/Load.go @@ -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") } diff --git a/src/arm/LoadPair.go b/src/arm/LoadPair.go index 12cc72c..8ca16d7 100644 --- a/src/arm/LoadPair.go +++ b/src/arm/LoadPair.go @@ -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) } diff --git a/src/arm/Load_test.go b/src/arm/Load_test.go index c04f60a..43eaf28 100644 --- a/src/arm/Load_test.go +++ b/src/arm/Load_test.go @@ -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}, diff --git a/src/arm/Store.go b/src/arm/Store.go new file mode 100644 index 0000000..d81f6a1 --- /dev/null +++ b/src/arm/Store.go @@ -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") +} diff --git a/src/arm/StorePair.go b/src/arm/StorePair.go index f31cce9..006457d 100644 --- a/src/arm/StorePair.go +++ b/src/arm/StorePair.go @@ -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) } diff --git a/src/arm/Store_test.go b/src/arm/Store_test.go new file mode 100644 index 0000000..9f4150d --- /dev/null +++ b/src/arm/Store_test.go @@ -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) + } +} diff --git a/src/arm/Sub.go b/src/arm/Sub.go new file mode 100644 index 0000000..6d222e7 --- /dev/null +++ b/src/arm/Sub.go @@ -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) +} diff --git a/src/asm/Memory.go b/src/asm/Memory.go index 9ffd5d3..ddc5ddb 100644 --- a/src/asm/Memory.go +++ b/src/asm/Memory.go @@ -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)) } diff --git a/src/asmc/compileARM.go b/src/asmc/compileARM.go index fa4b99f..9de618b 100644 --- a/src/asmc/compileARM.go +++ b/src/asmc/compileARM.go @@ -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: diff --git a/src/asmc/compileX86.go b/src/asmc/compileX86.go index f873cdd..2b59340 100644 --- a/src/asmc/compileX86.go +++ b/src/asmc/compileX86.go @@ -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] diff --git a/src/asmc/jumpARM.go b/src/asmc/jumpARM.go new file mode 100644 index 0000000..6819262 --- /dev/null +++ b/src/asmc/jumpARM.go @@ -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) +} diff --git a/src/asmc/jump.go b/src/asmc/jumpX86.go similarity index 95% rename from src/asmc/jump.go rename to src/asmc/jumpX86.go index 2b2f159..e9ace20 100644 --- a/src/asmc/jump.go +++ b/src/asmc/jumpX86.go @@ -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) diff --git a/src/asmc/load.go b/src/asmc/load.go index d6691f7..845092b 100644 --- a/src/asmc/load.go +++ b/src/asmc/load.go @@ -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) diff --git a/src/asmc/store.go b/src/asmc/store.go index f67b9b0..b5d338e 100644 --- a/src/asmc/store.go +++ b/src/asmc/store.go @@ -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) diff --git a/src/core/CompileLen.go b/src/core/CompileLen.go index db20d71..8fe23ef 100644 --- a/src/core/CompileLen.go +++ b/src/core/CompileLen.go @@ -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, } diff --git a/src/core/CompileMemoryStore.go b/src/core/CompileMemoryStore.go index ff6d59f..75e6da7 100644 --- a/src/core/CompileMemoryStore.go +++ b/src/core/CompileMemoryStore.go @@ -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), } diff --git a/src/core/EvaluateArray.go b/src/core/EvaluateArray.go index 39cb01a..5e840a5 100644 --- a/src/core/EvaluateArray.go +++ b/src/core/EvaluateArray.go @@ -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), } diff --git a/src/core/EvaluateDot.go b/src/core/EvaluateDot.go index 52738b3..f968839 100644 --- a/src/core/EvaluateDot.go +++ b/src/core/EvaluateDot.go @@ -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()), }, } diff --git a/src/cpu/Register.go b/src/cpu/Register.go index 006fc68..8c75cb6 100644 --- a/src/cpu/Register.go +++ b/src/cpu/Register.go @@ -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 { diff --git a/src/x86/Jump.go b/src/x86/Jump.go index 27b1ef4..feed711 100644 --- a/src/x86/Jump.go +++ b/src/x86/Jump.go @@ -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)) }