Moved sizeof functions to a separate package

This commit is contained in:
Eduard Urbach 2024-07-29 12:33:28 +02:00
parent d5953649d9
commit 8d4eb9935d
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
13 changed files with 83 additions and 40 deletions

View File

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

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" "encoding/binary"
"git.akyoto.dev/cli/q/src/build/cpu" "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. // 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 { 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) code = encode(code, mod, reg, rm, 8, opCode8)
return append(code, byte(number)) return append(code, byte(number))
} }

View File

@ -7,6 +7,7 @@ import (
"git.akyoto.dev/cli/q/src/build/arch/x64" "git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/cli/q/src/build/config" "git.akyoto.dev/cli/q/src/build/config"
"git.akyoto.dev/cli/q/src/build/elf" "git.akyoto.dev/cli/q/src/build/elf"
"git.akyoto.dev/cli/q/src/build/sizeof"
) )
// Finalize generates the final machine code. // Finalize generates the final machine code.
@ -242,7 +243,7 @@ restart:
for i, pointer := range pointers { for i, pointer := range pointers {
address := pointer.Resolve() address := pointer.Resolve()
if x64.SizeOf(int64(address)) > int(pointer.Size) { if sizeof.Signed(int64(address)) > int(pointer.Size) {
left := code[:pointer.Position-Address(pointer.OpSize)] left := code[:pointer.Position-Address(pointer.OpSize)]
right := code[pointer.Position+Address(pointer.Size):] right := code[pointer.Position+Address(pointer.Size):]
size := pointer.Size + pointer.OpSize size := pointer.Size + pointer.OpSize

View File

@ -21,7 +21,7 @@ func (f *Function) CompileAssignArray(node *ast.Assign) error {
defer f.UseVariable(variable) defer f.UseVariable(variable)
index := left.Children[1] index := left.Children[1]
offset, _, err := f.Number(index.Token) offset, err := f.Number(index.Token)
if err != nil { if err != nil {
return err return err

View File

@ -21,7 +21,7 @@ func (f *Function) ExecuteLeaf(operation token.Token, register cpu.Register, ope
return f.ExecuteRegisterRegister(operation, register, variable.Register) return f.ExecuteRegisterRegister(operation, register, variable.Register)
case token.Number, token.Rune: case token.Number, token.Rune:
number, _, err := f.Number(operand) number, err := f.Number(operand)
if err != nil { if err != nil {
return err return err

View File

@ -4,18 +4,21 @@ import (
"git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/asm"
"git.akyoto.dev/cli/q/src/build/errors" "git.akyoto.dev/cli/q/src/build/errors"
"git.akyoto.dev/cli/q/src/build/expression" "git.akyoto.dev/cli/q/src/build/expression"
"git.akyoto.dev/cli/q/src/build/sizeof"
"git.akyoto.dev/cli/q/src/build/token" "git.akyoto.dev/cli/q/src/build/token"
) )
// ExpressionToMemory puts the result of an expression into the specified memory address. // ExpressionToMemory puts the result of an expression into the specified memory address.
func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Memory) error { func (f *Function) ExpressionToMemory(node *expression.Expression, memory asm.Memory) error {
if node.IsLeaf() && (node.Token.Kind == token.Number || node.Token.Kind == token.Rune) { if node.IsLeaf() && (node.Token.Kind == token.Number || node.Token.Kind == token.Rune) {
number, size, err := f.Number(node.Token) number, err := f.Number(node.Token)
if err != nil { if err != nil {
return err return err
} }
size := byte(sizeof.Signed(int64(number)))
if size != memory.Length { if size != memory.Length {
return errors.New(&errors.NumberExceedsBounds{Number: number, ExpectedSize: memory.Length, Size: size}, f.File, node.Token.Position) return errors.New(&errors.NumberExceedsBounds{Number: number, ExpectedSize: memory.Length, Size: size}, f.File, node.Token.Position)
} }

View File

@ -10,45 +10,45 @@ import (
) )
// Number tries to convert the token into a numeric value. // Number tries to convert the token into a numeric value.
func (f *Function) Number(t token.Token) (int, byte, error) { func (f *Function) Number(t token.Token) (int, error) {
switch t.Kind { switch t.Kind {
case token.Number: case token.Number:
digits := t.Text(f.File.Bytes) digits := t.Text(f.File.Bytes)
if strings.HasPrefix(digits, "0x") { if strings.HasPrefix(digits, "0x") {
number, err := strconv.ParseInt(digits[2:], 16, 64) number, err := strconv.ParseInt(digits[2:], 16, 64)
return int(number), 8, err return int(number), err
} }
if strings.HasPrefix(digits, "0o") { if strings.HasPrefix(digits, "0o") {
number, err := strconv.ParseInt(digits[2:], 8, 64) number, err := strconv.ParseInt(digits[2:], 8, 64)
return int(number), 8, err return int(number), err
} }
if strings.HasPrefix(digits, "0b") { if strings.HasPrefix(digits, "0b") {
number, err := strconv.ParseInt(digits[2:], 2, 64) number, err := strconv.ParseInt(digits[2:], 2, 64)
return int(number), 8, err return int(number), err
} }
number, err := strconv.Atoi(digits) number, err := strconv.Atoi(digits)
return number, 8, err return number, err
case token.Rune: case token.Rune:
r := t.Bytes(f.File.Bytes) r := t.Bytes(f.File.Bytes)
r = r[1 : len(r)-1] r = r[1 : len(r)-1]
if len(r) == 0 { if len(r) == 0 {
return 0, 0, errors.New(errors.InvalidRune, f.File, t.Position+1) return 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
} }
number, size := utf8.DecodeRune(r) number, size := utf8.DecodeRune(r)
if len(r) > size { if len(r) > size {
return 0, 0, errors.New(errors.InvalidRune, f.File, t.Position+1) return 0, errors.New(errors.InvalidRune, f.File, t.Position+1)
} }
return int(number), byte(size), nil return int(number), nil
} }
return 0, 0, errors.New(errors.InvalidNumber, f.File, t.Position) return 0, errors.New(errors.InvalidNumber, f.File, t.Position)
} }

View File

@ -26,7 +26,7 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
return nil return nil
case token.Number, token.Rune: case token.Number, token.Rune:
number, _, err := f.Number(t) number, err := f.Number(t)
if err != nil { if err != nil {
return err return err

View File

@ -1,9 +1,9 @@
package x64 package sizeof
import "math" import "math"
// SizeOf tells you how many bytes are needed to encode this number. // Signed tells you how many bytes are needed to encode this signed number.
func SizeOf(number int64) int { func Signed(number int64) int {
switch { switch {
case number >= math.MinInt8 && number <= math.MaxInt8: case number >= math.MinInt8 && number <= math.MaxInt8:
return 1 return 1

View File

@ -0,0 +1,21 @@
package sizeof_test
import (
"math"
"testing"
"git.akyoto.dev/cli/q/src/build/sizeof"
"git.akyoto.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)
}

View File

@ -0,0 +1,20 @@
package sizeof
import "math"
// Unsigned tells you how many bytes are needed to encode this unsigned number.
func Unsigned(number uint64) int {
switch {
case number <= math.MaxUint8:
return 1
case number <= math.MaxUint16:
return 2
case number <= math.MaxUint32:
return 4
default:
return 8
}
}

View File

@ -0,0 +1,17 @@
package sizeof_test
import (
"math"
"testing"
"git.akyoto.dev/cli/q/src/build/sizeof"
"git.akyoto.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.MaxUint16), 2)
assert.Equal(t, sizeof.Unsigned(math.MaxUint32), 4)
assert.Equal(t, sizeof.Unsigned(math.MaxUint64), 8)
}