From 735057ac7473fd7bb8b77ab30095475078e2b6d5 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 27 Jun 2024 12:59:41 +0200 Subject: [PATCH] Added push and pop instructions --- src/build/asm/Assembler.go | 12 ++++++++++++ src/build/asm/Instructions.go | 10 ++++++++++ src/build/asm/Mnemonic.go | 8 ++++++++ src/build/asm/Register.go | 15 +++++++++++++++ 4 files changed, 45 insertions(+) create mode 100644 src/build/asm/Register.go diff --git a/src/build/asm/Assembler.go b/src/build/asm/Assembler.go index 9fe9229..e3341a3 100644 --- a/src/build/asm/Assembler.go +++ b/src/build/asm/Assembler.go @@ -91,6 +91,18 @@ func (a *Assembler) Finalize() ([]byte, []byte) { code = x64.MoveRegisterRegister64(code, operands.Destination, operands.Source) } + case POP: + switch operands := x.Data.(type) { + case *Register: + code = x64.PopRegister(code, operands.Register) + } + + case PUSH: + switch operands := x.Data.(type) { + case *Register: + code = x64.PushRegister(code, operands.Register) + } + case RETURN: code = x64.Return(code) diff --git a/src/build/asm/Instructions.go b/src/build/asm/Instructions.go index 04de2ea..4856d1e 100644 --- a/src/build/asm/Instructions.go +++ b/src/build/asm/Instructions.go @@ -24,6 +24,16 @@ func (a *Assembler) RegisterRegister(mnemonic Mnemonic, left cpu.Register, right }) } +// Register adds an instruction using a single register. +func (a *Assembler) Register(mnemonic Mnemonic, register cpu.Register) { + a.Instructions = append(a.Instructions, Instruction{ + Mnemonic: mnemonic, + Data: &Register{ + Register: register, + }, + }) +} + // Label adds a label at the current position. func (a *Assembler) Label(name string) { a.Instructions = append(a.Instructions, Instruction{ diff --git a/src/build/asm/Mnemonic.go b/src/build/asm/Mnemonic.go index 95fc2a2..77dbf81 100644 --- a/src/build/asm/Mnemonic.go +++ b/src/build/asm/Mnemonic.go @@ -11,6 +11,8 @@ const ( MUL LABEL MOVE + POP + PUSH RETURN SUB SYSCALL @@ -40,6 +42,12 @@ func (m Mnemonic) String() string { case MUL: return "mul" + case POP: + return "pop" + + case PUSH: + return "push" + case RETURN: return "return" diff --git a/src/build/asm/Register.go b/src/build/asm/Register.go new file mode 100644 index 0000000..a618370 --- /dev/null +++ b/src/build/asm/Register.go @@ -0,0 +1,15 @@ +package asm + +import ( + "git.akyoto.dev/cli/q/src/build/cpu" +) + +// Register operates with a single register. +type Register struct { + Register cpu.Register +} + +// String returns a human readable version. +func (data *Register) String() string { + return data.Register.String() +}