diff --git a/src/arm/Move.go b/src/arm/Move.go index b442118..d6454ec 100644 --- a/src/arm/Move.go +++ b/src/arm/Move.go @@ -4,7 +4,6 @@ import ( "encoding/binary" "git.urbach.dev/cli/q/src/cpu" - "git.urbach.dev/cli/q/src/sizeof" ) // MoveRegisterNumber moves a number into the given register. @@ -40,7 +39,7 @@ func MoveRegisterNumberMI(code []byte, destination cpu.Register, number int) []b // MoveRegisterNumberSI moves a number into the given register using a single instruction. func MoveRegisterNumberSI(destination cpu.Register, number int) (uint32, bool) { - if sizeof.Signed(number) <= 2 { + if cpu.SizeInt(number) <= 2 { if number < 0 { return MoveInvertedNumber(destination, uint16(^number), 0), true } @@ -48,7 +47,7 @@ func MoveRegisterNumberSI(destination cpu.Register, number int) (uint32, bool) { return MoveZero(destination, 0, uint16(number)), true } - if (number&0xFFFFFFFFFFFF == 0xFFFFFFFFFFFF) && sizeof.Signed(number>>48) <= 2 { + if (number&0xFFFFFFFFFFFF == 0xFFFFFFFFFFFF) && cpu.SizeInt(number>>48) <= 2 { return MoveInvertedNumber(destination, uint16((^number)>>48), 3), true } @@ -58,11 +57,11 @@ func MoveRegisterNumberSI(destination cpu.Register, number int) (uint32, bool) { return code, true } - if (number&0xFFFFFFFF == 0xFFFFFFFF) && sizeof.Signed(number>>32) <= 2 { + if (number&0xFFFFFFFF == 0xFFFFFFFF) && cpu.SizeInt(number>>32) <= 2 { return MoveInvertedNumber(destination, uint16((^number)>>32), 2), true } - if (number&0xFFFF == 0xFFFF) && sizeof.Signed(number>>16) <= 2 { + if (number&0xFFFF == 0xFFFF) && cpu.SizeInt(number>>16) <= 2 { return MoveInvertedNumber(destination, uint16((^number)>>16), 1), true } diff --git a/src/asm/compilerX86.go b/src/asm/compilerX86.go index 8540f05..fd2493f 100644 --- a/src/asm/compilerX86.go +++ b/src/asm/compilerX86.go @@ -5,8 +5,8 @@ import ( "fmt" "slices" + "git.urbach.dev/cli/q/src/cpu" "git.urbach.dev/cli/q/src/exe" - "git.urbach.dev/cli/q/src/sizeof" "git.urbach.dev/cli/q/src/token" "git.urbach.dev/cli/q/src/x86" ) @@ -135,7 +135,7 @@ func (c *compilerX86) Compile(instr Instruction) { switch code[0] { case 0x74, 0x75, 0x7C, 0x7D, 0x7E, 0x7F, 0xEB: - if sizeof.Signed(offset) == 1 { + if cpu.SizeInt(offset) == 1 { code[1] = byte(offset) return code } diff --git a/src/codegen/canEncodeNumber.go b/src/codegen/canEncodeNumber.go index 5f99887..e2527ac 100644 --- a/src/codegen/canEncodeNumber.go +++ b/src/codegen/canEncodeNumber.go @@ -3,7 +3,7 @@ package codegen import ( "git.urbach.dev/cli/q/src/arm" "git.urbach.dev/cli/q/src/config" - "git.urbach.dev/cli/q/src/sizeof" + "git.urbach.dev/cli/q/src/cpu" "git.urbach.dev/cli/q/src/ssa" "git.urbach.dev/cli/q/src/token" ) @@ -50,12 +50,12 @@ func (f *Function) canEncodeNumber(instr ssa.Value, number *ssa.Int) bool { case config.X86: if instr.Op.IsComparison() { - return sizeof.Signed(number.Int) <= 4 + return cpu.SizeInt(number.Int) <= 4 } switch instr.Op { case token.Add, token.And, token.Or, token.Sub, token.Xor: - return sizeof.Signed(number.Int) <= 4 + return cpu.SizeInt(number.Int) <= 4 case token.Shl, token.Shr: return number.Int >= 0 && number.Int <= 63 @@ -67,7 +67,7 @@ func (f *Function) canEncodeNumber(instr ssa.Value, number *ssa.Int) bool { case config.ARM: return false case config.X86: - return instr.Value == number && sizeof.Signed(number.Int) <= 4 + return instr.Value == number && cpu.SizeInt(number.Int) <= 4 } } diff --git a/src/sizeof/Signed.go b/src/cpu/SizeInt.go similarity index 61% rename from src/sizeof/Signed.go rename to src/cpu/SizeInt.go index 96de668..86ede24 100644 --- a/src/sizeof/Signed.go +++ b/src/cpu/SizeInt.go @@ -1,9 +1,9 @@ -package sizeof +package cpu import "math" -// Signed tells you how many bytes are needed to encode this signed number. -func Signed[T int | int8 | int16 | int32 | int64](number T) int { +// SizeInt tells you how many bytes are needed to encode this signed number. +func SizeInt[T int | int8 | int16 | int32 | int64](number T) int { x := int64(number) switch { diff --git a/src/cpu/SizeInt_test.go b/src/cpu/SizeInt_test.go new file mode 100644 index 0000000..d37cbad --- /dev/null +++ b/src/cpu/SizeInt_test.go @@ -0,0 +1,21 @@ +package cpu_test + +import ( + "math" + "testing" + + "git.urbach.dev/cli/q/src/cpu" + "git.urbach.dev/go/assert" +) + +func TestSizeInt(t *testing.T) { + assert.Equal(t, cpu.SizeInt(0), 1) + assert.Equal(t, cpu.SizeInt(math.MinInt8), 1) + assert.Equal(t, cpu.SizeInt(math.MaxInt8), 1) + assert.Equal(t, cpu.SizeInt(math.MinInt16), 2) + assert.Equal(t, cpu.SizeInt(math.MaxInt16), 2) + assert.Equal(t, cpu.SizeInt(math.MinInt32), 4) + assert.Equal(t, cpu.SizeInt(math.MaxInt32), 4) + assert.Equal(t, cpu.SizeInt(math.MinInt64), 8) + assert.Equal(t, cpu.SizeInt(math.MaxInt64), 8) +} \ No newline at end of file diff --git a/src/sizeof/Unsigned.go b/src/cpu/SizeUint.go similarity index 63% rename from src/sizeof/Unsigned.go rename to src/cpu/SizeUint.go index 4609f51..d0d68d3 100644 --- a/src/sizeof/Unsigned.go +++ b/src/cpu/SizeUint.go @@ -1,9 +1,9 @@ -package sizeof +package cpu import "math" -// Unsigned tells you how many bytes are needed to encode this unsigned number. -func Unsigned[T uint | uint8 | uint16 | uint32 | uint64 | int | int8 | int16 | int32 | int64](number T) int { +// SizeUint tells you how many bytes are needed to encode this unsigned number. +func SizeUint[T uint | uint8 | uint16 | uint32 | uint64 | int | int8 | int16 | int32 | int64](number T) int { x := uint64(number) switch { diff --git a/src/cpu/SizeUint_test.go b/src/cpu/SizeUint_test.go new file mode 100644 index 0000000..5f0e1dd --- /dev/null +++ b/src/cpu/SizeUint_test.go @@ -0,0 +1,19 @@ +package cpu_test + +import ( + "math" + "testing" + + "git.urbach.dev/cli/q/src/cpu" + "git.urbach.dev/go/assert" +) + +func TestSizeUint(t *testing.T) { + assert.Equal(t, cpu.SizeUint(0), 1) + assert.Equal(t, cpu.SizeUint(math.MaxUint8), 1) + assert.Equal(t, cpu.SizeUint(math.MaxUint8+1), 2) + assert.Equal(t, cpu.SizeUint(math.MaxUint16), 2) + assert.Equal(t, cpu.SizeUint(math.MaxUint16+1), 4) + assert.Equal(t, cpu.SizeUint(math.MaxUint32), 4) + assert.Equal(t, cpu.SizeUint(math.MaxUint32+1), 8) +} \ No newline at end of file diff --git a/src/sizeof/Signed_test.go b/src/sizeof/Signed_test.go deleted file mode 100644 index f2e1d24..0000000 --- a/src/sizeof/Signed_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package sizeof_test - -import ( - "math" - "testing" - - "git.urbach.dev/cli/q/src/sizeof" - "git.urbach.dev/go/assert" -) - -func TestSigned(t *testing.T) { - assert.Equal(t, sizeof.Signed(0), 1) - assert.Equal(t, sizeof.Signed(math.MinInt8), 1) - assert.Equal(t, sizeof.Signed(math.MaxInt8), 1) - assert.Equal(t, sizeof.Signed(math.MinInt16), 2) - assert.Equal(t, sizeof.Signed(math.MaxInt16), 2) - assert.Equal(t, sizeof.Signed(math.MinInt32), 4) - assert.Equal(t, sizeof.Signed(math.MaxInt32), 4) - assert.Equal(t, sizeof.Signed(math.MinInt64), 8) - assert.Equal(t, sizeof.Signed(math.MaxInt64), 8) -} \ No newline at end of file diff --git a/src/sizeof/Unsigned_test.go b/src/sizeof/Unsigned_test.go deleted file mode 100644 index a8d048b..0000000 --- a/src/sizeof/Unsigned_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package sizeof_test - -import ( - "math" - "testing" - - "git.urbach.dev/cli/q/src/sizeof" - "git.urbach.dev/go/assert" -) - -func TestUnsigned(t *testing.T) { - assert.Equal(t, sizeof.Unsigned(0), 1) - assert.Equal(t, sizeof.Unsigned(math.MaxUint8), 1) - assert.Equal(t, sizeof.Unsigned(math.MaxUint8+1), 2) - assert.Equal(t, sizeof.Unsigned(math.MaxUint16), 2) - assert.Equal(t, sizeof.Unsigned(math.MaxUint16+1), 4) - assert.Equal(t, sizeof.Unsigned(math.MaxUint32), 4) - assert.Equal(t, sizeof.Unsigned(math.MaxUint32+1), 8) -} \ No newline at end of file diff --git a/src/x86/Move.go b/src/x86/Move.go index 43bceae..0fd8c6b 100644 --- a/src/x86/Move.go +++ b/src/x86/Move.go @@ -4,14 +4,13 @@ import ( "encoding/binary" "git.urbach.dev/cli/q/src/cpu" - "git.urbach.dev/cli/q/src/sizeof" ) // MoveRegisterNumber moves an integer into the given register. func MoveRegisterNumber(code []byte, destination cpu.Register, number int) []byte { w := byte(0) - if sizeof.Signed(int64(number)) == 8 { + if cpu.SizeInt(int64(number)) == 8 { w = 1 } diff --git a/src/x86/Push.go b/src/x86/Push.go index 8459a23..e036153 100644 --- a/src/x86/Push.go +++ b/src/x86/Push.go @@ -2,12 +2,11 @@ package x86 import ( "git.urbach.dev/cli/q/src/cpu" - "git.urbach.dev/cli/q/src/sizeof" ) // PushNumber pushes a number onto the stack. func PushNumber(code []byte, number int32) []byte { - length := sizeof.Signed(number) + length := cpu.SizeInt(number) if length >= 2 { return append( diff --git a/src/x86/encodeNum.go b/src/x86/encodeNum.go index e1c2073..7050c77 100644 --- a/src/x86/encodeNum.go +++ b/src/x86/encodeNum.go @@ -4,12 +4,11 @@ import ( "encoding/binary" "git.urbach.dev/cli/q/src/cpu" - "git.urbach.dev/cli/q/src/sizeof" ) // encodeNum encodes an instruction with up to two registers and a number parameter. func encodeNum(code []byte, mod AddressMode, reg cpu.Register, rm cpu.Register, number int, opCode8 byte, opCode32 byte) []byte { - if sizeof.Signed(int64(number)) == 1 { + if cpu.SizeInt(int64(number)) == 1 { code = encode(code, mod, reg, rm, 8, opCode8) return append(code, byte(number)) }