aboutsummaryrefslogtreecommitdiff
path: root/lib/inst.h
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-03 19:07:10 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-03 19:07:10 +0000
commitfe7f26256b5da63e46d3e36a8ddfe119dc9095b4 (patch)
tree98aae34fea26776f08654e94bcc03756ff765dfc /lib/inst.h
parentb5a1582976c721dbb9ece622226be0e5ea575fd8 (diff)
downloadovm-fe7f26256b5da63e46d3e36a8ddfe119dc9095b4.tar.gz
ovm-fe7f26256b5da63e46d3e36a8ddfe119dc9095b4.tar.bz2
ovm-fe7f26256b5da63e46d3e36a8ddfe119dc9095b4.zip
Defined a `program` structure
Essentially a "program header", followed by a count, followed by instructions. Provides a stronger format for bytecode files and allows for better bounds checking on instructions.
Diffstat (limited to 'lib/inst.h')
-rw-r--r--lib/inst.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/inst.h b/lib/inst.h
index 3877a2b..e6225a6 100644
--- a/lib/inst.h
+++ b/lib/inst.h
@@ -188,6 +188,28 @@ inst_t *insts_read_bytecode(darr_t *, size_t *);
void insts_write_bytecode_file(inst_t *, size_t, FILE *);
inst_t *insts_read_bytecode_file(FILE *, size_t *);
+typedef struct
+{
+ word start_address;
+} prog_header_t;
+
+typedef struct
+{
+ prog_header_t header;
+ word count;
+ inst_t instructions[];
+} prog_t;
+
+// Write the entire program as bytecode
+void prog_write_bytecode(prog_t *, darr_t *);
+// Only append the instructions as bytecode
+void prog_append_bytecode(prog_t *, darr_t *);
+// Read an entire program as bytecode
+prog_t *prog_read_bytecode(darr_t *);
+
+void prog_write_file(prog_t *, FILE *);
+prog_t *prog_read_file(FILE *);
+
#define INST_NOOP ((inst_t){0})
#define INST_HALT ((inst_t){.opcode = OP_HALT})