aboutsummaryrefslogtreecommitdiff
path: root/src/inst.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:25:17 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:25:17 +0100
commit137a6d3b759d30665ef0e375226c928956f119f1 (patch)
treeef4adff8227744d82658ed433c4999f5b3cef489 /src/inst.c
parentd5d37f1264174b0ae4178a23bced4c497452c0f4 (diff)
downloadovm-137a6d3b759d30665ef0e375226c928956f119f1.tar.gz
ovm-137a6d3b759d30665ef0e375226c928956f119f1.tar.bz2
ovm-137a6d3b759d30665ef0e375226c928956f119f1.zip
Cleaned up inst.c
Use (H)WORD_SIZE more, added some notes, etc
Diffstat (limited to 'src/inst.c')
-rw-r--r--src/inst.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/src/inst.c b/src/inst.c
index 36156d4..fc02b66 100644
--- a/src/inst.c
+++ b/src/inst.c
@@ -193,9 +193,9 @@ size_t inst_bytecode_size(inst_t inst)
if (inst.opcode == OP_PUSH_BYTE)
++size;
else if (inst.opcode == OP_PUSH_HWORD)
- size += sizeof(i32);
+ size += HWORD_SIZE;
else if (inst.opcode == OP_PUSH_WORD)
- size += sizeof(word);
+ size += WORD_SIZE;
}
else if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) ||
OPCODE_IS_TYPE(inst.opcode, OP_MOV))
@@ -227,12 +227,10 @@ void inst_write_bytecode(inst_t inst, darr_t *darr)
darr_append_byte(darr, inst.operand.as_byte);
break;
case DATA_TYPE_HWORD:
- darr_append_bytes(darr, (byte *)&inst.operand.as_hword,
- sizeof(inst.operand.as_hword));
+ darr_append_bytes(darr, (byte *)&inst.operand.as_hword, HWORD_SIZE);
break;
case DATA_TYPE_WORD:
- darr_append_bytes(darr, (byte *)&inst.operand.as_word,
- sizeof(inst.operand.as_word));
+ darr_append_bytes(darr, (byte *)&inst.operand.as_word, WORD_SIZE);
break;
}
}
@@ -260,17 +258,17 @@ data_t read_type_from_darr(darr_t *darr, data_type_t type)
// TODO: Error (darr has no space left)
return DWORD(0);
hword u = 0;
- memcpy(&u, darr->data + darr->used, sizeof(u));
- darr->used += sizeof(u);
+ memcpy(&u, darr->data + darr->used, HWORD_SIZE);
+ darr->used += HWORD_SIZE;
return DHWORD(u);
break;
case DATA_TYPE_WORD:
- if (darr->used + sizeof(word) >= darr->available)
+ if (darr->used + WORD_SIZE >= darr->available)
// TODO: Error (darr has no space left)
return DWORD(0);
word w = 0;
- memcpy(&w, darr->data + darr->used, sizeof(w));
- darr->used += sizeof(w);
+ memcpy(&w, darr->data + darr->used, WORD_SIZE);
+ darr->used += WORD_SIZE;
return DWORD(w);
break;
}
@@ -303,9 +301,10 @@ inst_t inst_read_bytecode(darr_t *darr)
inst_t *insts_read_bytecode(darr_t *bytes, size_t *ret_size)
{
- *ret_size = 0;
+ *ret_size = 0;
+ // NOTE: Here we use the darr as a dynamic array of inst_t.
darr_t instructions = {0};
- darr_init(&instructions, 0);
+ darr_init(&instructions, sizeof(inst_t));
while (bytes->used < bytes->available)
{
inst_t instruction = inst_read_bytecode(bytes);