Moved sizeof functions to a separate package

This commit is contained in:
2024-07-29 12:33:28 +02:00
parent d5953649d9
commit 8d4eb9935d
13 changed files with 83 additions and 40 deletions

View File

@ -4,13 +4,14 @@ import (
"encoding/binary"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/sizeof"
)
// MoveRegisterNumber moves an integer into the given register.
func MoveRegisterNumber(code []byte, destination cpu.Register, number int) []byte {
w := byte(0)
if SizeOf(int64(number)) == 8 {
if sizeof.Signed(int64(number)) == 8 {
w = 1
}

View File

@ -1,20 +0,0 @@
package x64
import "math"
// SizeOf tells you how many bytes are needed to encode this number.
func SizeOf(number int64) int {
switch {
case number >= math.MinInt8 && number <= math.MaxInt8:
return 1
case number >= math.MinInt16 && number <= math.MaxInt16:
return 2
case number >= math.MinInt32 && number <= math.MaxInt32:
return 4
default:
return 8
}
}

View File

@ -1,21 +0,0 @@
package x64_test
import (
"math"
"testing"
"git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/go/assert"
)
func TestSizeOf(t *testing.T) {
assert.Equal(t, x64.SizeOf(0), 1)
assert.Equal(t, x64.SizeOf(math.MinInt8), 1)
assert.Equal(t, x64.SizeOf(math.MaxInt8), 1)
assert.Equal(t, x64.SizeOf(math.MinInt16), 2)
assert.Equal(t, x64.SizeOf(math.MaxInt16), 2)
assert.Equal(t, x64.SizeOf(math.MinInt32), 4)
assert.Equal(t, x64.SizeOf(math.MaxInt32), 4)
assert.Equal(t, x64.SizeOf(math.MinInt64), 8)
assert.Equal(t, x64.SizeOf(math.MaxInt64), 8)
}

View File

@ -4,11 +4,12 @@ import (
"encoding/binary"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/cli/q/src/build/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(int64(number)) == 1 {
if sizeof.Signed(int64(number)) == 1 {
code = encode(code, mod, reg, rm, 8, opCode8)
return append(code, byte(number))
}