From f04e9d7a60eb4e5a452fc844ed96714664aec8be Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 14 Jun 2024 10:46:45 +0200 Subject: [PATCH] Implemented return keyword --- src/build/Function.go | 7 +++++++ src/build/asm/Assembler.go | 6 ++++++ src/build/asm/Instructions.go | 5 +++++ src/build/asm/Mnemonic.go | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/src/build/Function.go b/src/build/Function.go index 11c84ed..729c5c5 100644 --- a/src/build/Function.go +++ b/src/build/Function.go @@ -34,6 +34,13 @@ func (f *Function) Compile() { continue } + if line[0].Kind == token.Keyword { + switch line[0].Text() { + case "return": + f.Assembler.Return() + } + } + if line[0].Kind == token.Identifier && line[0].Text() == "syscall" { paramTokens := line[2 : len(line)-1] start := 0 diff --git a/src/build/asm/Assembler.go b/src/build/asm/Assembler.go index 94a4ce7..01379f9 100644 --- a/src/build/asm/Assembler.go +++ b/src/build/asm/Assembler.go @@ -38,8 +38,14 @@ func (a *Assembler) Finalize() ([]byte, []byte) { }) } + case RETURN: + code = x64.Return(code) + case SYSCALL: code = x64.Syscall(code) + + default: + panic("Unknown mnemonic: " + x.Mnemonic.String()) } } diff --git a/src/build/asm/Instructions.go b/src/build/asm/Instructions.go index 0cd9014..f201af9 100644 --- a/src/build/asm/Instructions.go +++ b/src/build/asm/Instructions.go @@ -26,6 +26,11 @@ func (a *Assembler) MoveRegisterAddress(reg cpu.Register, address Address) { }) } +// Return returns back to the caller. +func (a *Assembler) Return() { + a.Instructions = append(a.Instructions, Instruction{Mnemonic: RETURN}) +} + // Syscall executes a kernel function. func (a *Assembler) Syscall() { a.Instructions = append(a.Instructions, Instruction{Mnemonic: SYSCALL}) diff --git a/src/build/asm/Mnemonic.go b/src/build/asm/Mnemonic.go index cd7f033..7fd1a06 100644 --- a/src/build/asm/Mnemonic.go +++ b/src/build/asm/Mnemonic.go @@ -5,6 +5,7 @@ type Mnemonic uint8 const ( NONE Mnemonic = iota MOVE + RETURN SYSCALL ) @@ -14,6 +15,9 @@ func (m Mnemonic) String() string { case MOVE: return "move" + case RETURN: + return "return" + case SYSCALL: return "syscall" }