aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:27:09 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:28:10 +0100
commitaa3d1cb85f7940738468db37211c6841d90e6d4b (patch)
tree23d450fe342e43be24f6b6a25c45d67a929892cc /src
parentb20ad511a0a375da9dd92542e58a4a2cd0721d9f (diff)
downloadovm-aa3d1cb85f7940738468db37211c6841d90e6d4b.tar.gz
ovm-aa3d1cb85f7940738468db37211c6841d90e6d4b.tar.bz2
ovm-aa3d1cb85f7940738468db37211c6841d90e6d4b.zip
Added NUMBER_OF_OPCODES which aids in compilation errors
If I add a new operand I want the build system to be more helpful in finding the places I need to change to make it work.
Diffstat (limited to 'src')
-rw-r--r--src/inst.c9
-rw-r--r--src/inst.h2
-rw-r--r--src/runtime.c2
3 files changed, 12 insertions, 1 deletions
diff --git a/src/inst.c b/src/inst.c
index 8a5ce2c..9ca20bd 100644
--- a/src/inst.c
+++ b/src/inst.c
@@ -10,6 +10,7 @@
* Description: Implementation of bytecode for instructions
*/
+#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
@@ -116,6 +117,9 @@ const char *opcode_as_cstr(opcode_t code)
case OP_HALT:
return "HALT";
break;
+ case NUMBER_OF_OPCODES:
+ return "";
+ break;
}
return "";
}
@@ -201,6 +205,7 @@ void inst_print(inst_t instruction, FILE *fp)
size_t inst_bytecode_size(inst_t inst)
{
+ static_assert(NUMBER_OF_OPCODES == 31, "inst_bytecode_size: Out of date");
size_t size = 1; // for opcode
if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH))
{
@@ -223,6 +228,7 @@ size_t inst_bytecode_size(inst_t inst)
void inst_write_bytecode(inst_t inst, darr_t *darr)
{
+ static_assert(NUMBER_OF_OPCODES == 31, "inst_write_bytecode: Out of date");
// Append opcode
darr_append_byte(darr, inst.opcode);
// Then append 0 or more operands
@@ -294,11 +300,12 @@ data_t read_type_from_darr(darr_t *darr, data_type_t type)
inst_t inst_read_bytecode(darr_t *darr)
{
+ static_assert(NUMBER_OF_OPCODES == 31, "inst_read_bytecode: Out of date");
if (darr->used >= darr->available)
return (inst_t){0};
inst_t inst = {0};
opcode_t opcode = darr->data[darr->used++];
- if (opcode > OP_HALT)
+ if (opcode > OP_HALT || opcode == NUMBER_OF_OPCODES)
// Translate to NOOP
return inst;
// Read operands
diff --git a/src/inst.h b/src/inst.h
index 16374ee..61d7ede 100644
--- a/src/inst.h
+++ b/src/inst.h
@@ -65,6 +65,8 @@ typedef enum
OP_EQ_HWORD,
OP_EQ_WORD,
+ // Should not be an opcode
+ NUMBER_OF_OPCODES,
OP_HALT = 0b11111111, // top of the byte is a HALT
} opcode_t;
diff --git a/src/runtime.c b/src/runtime.c
index 67ea779..5c9b985 100644
--- a/src/runtime.c
+++ b/src/runtime.c
@@ -10,6 +10,7 @@
* Description: Virtual machine implementation
*/
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -18,6 +19,7 @@
void vm_execute(vm_t *vm)
{
+ static_assert(NUMBER_OF_OPCODES == 31, "vm_execute: Out of date");
struct Program *prog = &vm->program;
if (prog->ptr >= prog->max)
// TODO: Error (Went past end of program)