From f0e48a186d76623017758a96e60174412de78933 Mon Sep 17 00:00:00 2001
From: Aryadev Chavali <aryadev@aryadevchavali.com>
Date: Sun, 15 Oct 2023 21:14:56 +0100
Subject: Added a register `ret`, a word, which holds the result of any
 instruction

Allows inspection at runtime of any routines result.
---
 src/main.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

(limited to 'src')

diff --git a/src/main.c b/src/main.c
index f431dd7..c0a2e00 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,6 +23,7 @@ typedef struct
 {
   struct Registers
   {
+    word ret;
     byte b[VM_BYTE_REGISTERS];
     word w[VM_WORD_REGISTERS];
     f64 f[VM_FLOAT_REGISTERS];
@@ -177,18 +178,20 @@ void vm_execute(vm_t *vm)
   if (OPCODE_IS_PUSH(instruction.opcode))
   {
     PUSH_ROUTINES[instruction.opcode](vm, instruction.operand);
+    vm->registers.ret = instruction.operand.as_word;
     prog->ptr++;
   }
   else if (OPCODE_IS_MOV(instruction.opcode))
   {
     MOV_ROUTINES[instruction.opcode](vm, instruction.operand, instruction.reg);
+    vm->registers.ret = instruction.operand.as_word;
     prog->ptr++;
   }
   else if (OPCODE_IS_POP(instruction.opcode))
   {
-    // TODO: Figure out what should happen to the result of this pop
-    // routine
-    POP_ROUTINES[instruction.opcode](vm);
+    // NOTE: We use the `ret` register for the result of this pop
+    data_t d          = POP_ROUTINES[instruction.opcode](vm);
+    vm->registers.ret = d.as_word;
     prog->ptr++;
   }
   else
-- 
cgit v1.2.3-13-gbd6f