From 265ab988d952285b82783f455e52114f6bad573e Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 30 Jul 2024 12:20:33 +0200 Subject: [PATCH] Simplified code --- src/build/core/AddBytes.go | 13 +++++++++++++ src/build/core/ExecuteRegisterRegister.go | 22 +++++++++++----------- src/build/core/TokenToRegister.go | 8 ++------ 3 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 src/build/core/AddBytes.go diff --git a/src/build/core/AddBytes.go b/src/build/core/AddBytes.go new file mode 100644 index 0000000..2e793ea --- /dev/null +++ b/src/build/core/AddBytes.go @@ -0,0 +1,13 @@ +package core + +import ( + "fmt" +) + +// AddBytes adds a sequence of bytes and returns its address as a label. +func (f *Function) AddBytes(value []byte) string { + f.count.data++ + label := fmt.Sprintf("%s_data_%d", f.Name, f.count.data) + f.Assembler.SetData(label, value) + return label +} diff --git a/src/build/core/ExecuteRegisterRegister.go b/src/build/core/ExecuteRegisterRegister.go index bea03e9..aa8006f 100644 --- a/src/build/core/ExecuteRegisterRegister.go +++ b/src/build/core/ExecuteRegisterRegister.go @@ -8,37 +8,37 @@ import ( ) // ExecuteRegisterRegister performs an operation on two registers. -func (f *Function) ExecuteRegisterRegister(operation token.Token, destination cpu.Register, source cpu.Register) error { +func (f *Function) ExecuteRegisterRegister(operation token.Token, register cpu.Register, operand cpu.Register) error { switch operation.Kind { case token.Add, token.AddAssign: - f.RegisterRegister(asm.ADD, destination, source) + f.RegisterRegister(asm.ADD, register, operand) case token.Sub, token.SubAssign: - f.RegisterRegister(asm.SUB, destination, source) + f.RegisterRegister(asm.SUB, register, operand) case token.Mul, token.MulAssign: - f.RegisterRegister(asm.MUL, destination, source) + f.RegisterRegister(asm.MUL, register, operand) case token.Div, token.DivAssign: - f.RegisterRegister(asm.DIV, destination, source) + f.RegisterRegister(asm.DIV, register, operand) case token.Mod, token.ModAssign: - f.RegisterRegister(asm.MODULO, destination, source) + f.RegisterRegister(asm.MODULO, register, operand) case token.And, token.AndAssign: - f.RegisterRegister(asm.AND, destination, source) + f.RegisterRegister(asm.AND, register, operand) case token.Or, token.OrAssign: - f.RegisterRegister(asm.OR, destination, source) + f.RegisterRegister(asm.OR, register, operand) case token.Xor, token.XorAssign: - f.RegisterRegister(asm.XOR, destination, source) + f.RegisterRegister(asm.XOR, register, operand) case token.Equal, token.NotEqual, token.Less, token.LessEqual, token.Greater, token.GreaterEqual: - f.RegisterRegister(asm.COMPARE, destination, source) + f.RegisterRegister(asm.COMPARE, register, operand) case token.Assign: - f.RegisterRegister(asm.MOVE, destination, source) + f.RegisterRegister(asm.MOVE, register, operand) default: return errors.New(&errors.InvalidOperator{Operator: operation.Text(f.File.Bytes)}, f.File, operation.Position) diff --git a/src/build/core/TokenToRegister.go b/src/build/core/TokenToRegister.go index e95d87d..0ed4e0a 100644 --- a/src/build/core/TokenToRegister.go +++ b/src/build/core/TokenToRegister.go @@ -1,8 +1,6 @@ package core import ( - "fmt" - "git.akyoto.dev/cli/q/src/build/asm" "git.akyoto.dev/cli/q/src/build/cpu" "git.akyoto.dev/cli/q/src/build/errors" @@ -36,10 +34,8 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error { return nil case token.String: - f.count.data++ - label := fmt.Sprintf("%s_data_%d", f.Name, f.count.data) - value := t.Bytes(f.File.Bytes)[1 : t.Length-1] - f.Assembler.SetData(label, value) + data := t.Bytes(f.File.Bytes)[1 : t.Length-1] + label := f.AddBytes(data) f.RegisterLabel(asm.MOVE, register, label) return nil