Refactor type byte -> byte_t
Simpler to read as a type. This style will allow me to define signed versions of the base types as simpler names than what they are currently.
This commit is contained in:
12
lib/base.c
12
lib/base.c
@@ -17,13 +17,13 @@
|
||||
union hword_pun
|
||||
{
|
||||
hword h;
|
||||
byte bytes[HWORD_SIZE];
|
||||
byte_t bytes[HWORD_SIZE];
|
||||
};
|
||||
|
||||
union word_pun
|
||||
{
|
||||
word h;
|
||||
byte bytes[WORD_SIZE];
|
||||
byte_t bytes[WORD_SIZE];
|
||||
};
|
||||
|
||||
hword hword_htobc(hword w)
|
||||
@@ -78,7 +78,7 @@ word word_bctoh(word w)
|
||||
#endif
|
||||
}
|
||||
|
||||
hword convert_bytes_to_hword(byte *bytes)
|
||||
hword convert_bytes_to_hword(byte_t *bytes)
|
||||
{
|
||||
hword be_h = 0;
|
||||
memcpy(&be_h, bytes, HWORD_SIZE);
|
||||
@@ -86,19 +86,19 @@ hword convert_bytes_to_hword(byte *bytes)
|
||||
return h;
|
||||
}
|
||||
|
||||
void convert_hword_to_bytes(hword w, byte *bytes)
|
||||
void convert_hword_to_bytes(hword w, byte_t *bytes)
|
||||
{
|
||||
hword be_h = hword_htobc(w);
|
||||
memcpy(bytes, &be_h, HWORD_SIZE);
|
||||
}
|
||||
|
||||
void convert_word_to_bytes(word w, byte *bytes)
|
||||
void convert_word_to_bytes(word w, byte_t *bytes)
|
||||
{
|
||||
word be_w = word_htobc(w);
|
||||
memcpy(bytes, &be_w, WORD_SIZE);
|
||||
}
|
||||
|
||||
word convert_bytes_to_word(byte *bytes)
|
||||
word convert_bytes_to_word(byte_t *bytes)
|
||||
{
|
||||
word be_w = 0;
|
||||
memcpy(&be_w, bytes, WORD_SIZE);
|
||||
|
||||
12
lib/base.h
12
lib/base.h
@@ -43,7 +43,7 @@ typedef int64_t i64;
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
|
||||
typedef u8 byte;
|
||||
typedef u8 byte_t;
|
||||
typedef i8 s_byte;
|
||||
typedef u32 hword;
|
||||
typedef i32 s_hword;
|
||||
@@ -60,7 +60,7 @@ typedef i64 s_word;
|
||||
*/
|
||||
typedef union
|
||||
{
|
||||
byte as_byte;
|
||||
byte_t as_byte;
|
||||
s_byte as_char;
|
||||
hword as_hword;
|
||||
s_hword as_int;
|
||||
@@ -105,28 +105,28 @@ typedef enum
|
||||
* format (big endian) and that they are at least HWORD_SIZE in
|
||||
* size.
|
||||
*/
|
||||
hword convert_bytes_to_hword(byte *buffer);
|
||||
hword convert_bytes_to_hword(byte_t *buffer);
|
||||
|
||||
/** Convert a half word into a VM byte code format bytes (big endian)
|
||||
* @param h: Half word to convert
|
||||
* @param buffer: Buffer to store into. We assume the buffer has at
|
||||
* least HWORD_SIZE space.
|
||||
*/
|
||||
void convert_hword_to_bytes(hword h, byte *buffer);
|
||||
void convert_hword_to_bytes(hword h, byte_t *buffer);
|
||||
|
||||
/** Convert a buffer of bytes to a word
|
||||
* We assume the buffer of bytes are in virtual machine byte code
|
||||
* format (big endian) and that they are at least WORD_SIZE in
|
||||
* size.
|
||||
*/
|
||||
word convert_bytes_to_word(byte *);
|
||||
word convert_bytes_to_word(byte_t *);
|
||||
|
||||
/** Convert a word into a VM byte code format bytes (big endian)
|
||||
* @param w: Word to convert
|
||||
* @param buffer: Buffer to store into. We assume the buffer has at
|
||||
* least WORD_SIZE space.
|
||||
*/
|
||||
void convert_word_to_bytes(word w, byte *buffer);
|
||||
void convert_word_to_bytes(word w, byte_t *buffer);
|
||||
|
||||
/** Convert a half word into bytecode format (little endian)
|
||||
*/
|
||||
|
||||
@@ -38,20 +38,20 @@ void darr_ensure_capacity(darr_t *darr, size_t requested)
|
||||
}
|
||||
}
|
||||
|
||||
void darr_append_byte(darr_t *darr, byte byte)
|
||||
void darr_append_byte(darr_t *darr, byte_t byte_t)
|
||||
{
|
||||
darr_ensure_capacity(darr, 1);
|
||||
darr->data[darr->used++] = byte;
|
||||
darr->data[darr->used++] = byte_t;
|
||||
}
|
||||
|
||||
void darr_append_bytes(darr_t *darr, byte *bytes, size_t n)
|
||||
void darr_append_bytes(darr_t *darr, byte_t *bytes, size_t n)
|
||||
{
|
||||
darr_ensure_capacity(darr, n);
|
||||
memcpy(darr->data + darr->used, bytes, n);
|
||||
darr->used += n;
|
||||
}
|
||||
|
||||
byte darr_at(darr_t *darr, size_t index)
|
||||
byte_t darr_at(darr_t *darr, size_t index)
|
||||
{
|
||||
if (index >= darr->used)
|
||||
// TODO: Error (index is out of bounds)
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
byte *data;
|
||||
byte_t *data;
|
||||
size_t used, available;
|
||||
} darr_t;
|
||||
|
||||
@@ -58,19 +58,19 @@ void darr_ensure_capacity(darr_t *darr, size_t n);
|
||||
* If the dynamic array doesn't have enough space it will reallocate
|
||||
* to ensure it can fit it in.
|
||||
*/
|
||||
void darr_append_byte(darr_t *darr, byte b);
|
||||
void darr_append_byte(darr_t *darr, byte_t b);
|
||||
|
||||
/** Append an array of n bytes (b) to the dynamic array (darr).
|
||||
* If the dynamic array doesn't have enough space to fit all n bytes
|
||||
* it will reallocate to ensure it can fit it in.
|
||||
*/
|
||||
void darr_append_bytes(darr_t *darr, byte *b, size_t n);
|
||||
void darr_append_bytes(darr_t *darr, byte_t *b, size_t n);
|
||||
|
||||
/** Safely get the nth byte of the dynamic array (darr)
|
||||
* If the dynamic array has less than n bytes used, it will return 0
|
||||
* as a default value.
|
||||
*/
|
||||
byte darr_at(darr_t *darr, size_t n);
|
||||
byte_t darr_at(darr_t *darr, size_t n);
|
||||
|
||||
/** Write the dynamic array (darr) to the file pointer (fp) as a
|
||||
* buffer of bytes.
|
||||
|
||||
@@ -22,7 +22,7 @@ typedef struct Page
|
||||
{
|
||||
struct Page *next;
|
||||
size_t available;
|
||||
byte data[];
|
||||
byte_t data[];
|
||||
} page_t;
|
||||
|
||||
page_t *page_create(size_t, page_t *);
|
||||
|
||||
@@ -417,7 +417,8 @@ inst_t *insts_read_bytecode(darr_t *bytes, size_t *ret_size)
|
||||
while (bytes->used < bytes->available)
|
||||
{
|
||||
inst_t instruction = inst_read_bytecode(bytes);
|
||||
darr_append_bytes(&instructions, (byte *)&instruction, sizeof(instruction));
|
||||
darr_append_bytes(&instructions, (byte_t *)&instruction,
|
||||
sizeof(instruction));
|
||||
}
|
||||
*ret_size = instructions.used / sizeof(inst_t);
|
||||
return (inst_t *)instructions.data;
|
||||
@@ -446,9 +447,9 @@ void prog_write_bytecode(prog_t *program, darr_t *buffer)
|
||||
{
|
||||
// Write program header i.e. the start and count
|
||||
word start = word_htobc(program->start_address);
|
||||
darr_append_bytes(buffer, (byte *)&start, sizeof(start));
|
||||
darr_append_bytes(buffer, (byte_t *)&start, sizeof(start));
|
||||
word count = word_htobc(program->count);
|
||||
darr_append_bytes(buffer, (byte *)&count, sizeof(count));
|
||||
darr_append_bytes(buffer, (byte_t *)&count, sizeof(count));
|
||||
|
||||
// Write instructions
|
||||
insts_write_bytecode(program->instructions, program->count, buffer);
|
||||
|
||||
@@ -52,7 +52,7 @@ int main(int argc, char *argv[])
|
||||
#endif
|
||||
|
||||
size_t stack_size = 256;
|
||||
byte *stack = calloc(stack_size, 1);
|
||||
byte_t *stack = calloc(stack_size, 1);
|
||||
registers_t registers = {0};
|
||||
darr_init(®isters, 8 * WORD_SIZE);
|
||||
heap_t heap = {0};
|
||||
|
||||
18
vm/runtime.c
18
vm/runtime.c
@@ -344,11 +344,11 @@ err_t vm_push_hword(vm_t *vm, data_t f)
|
||||
{
|
||||
if (vm->stack.ptr + HWORD_SIZE >= vm->stack.max)
|
||||
return ERR_STACK_OVERFLOW;
|
||||
byte bytes[HWORD_SIZE] = {0};
|
||||
byte_t bytes[HWORD_SIZE] = {0};
|
||||
convert_hword_to_bytes(f.as_hword, bytes);
|
||||
for (size_t i = 0; i < HWORD_SIZE; ++i)
|
||||
{
|
||||
byte b = bytes[HWORD_SIZE - i - 1];
|
||||
byte_t b = bytes[HWORD_SIZE - i - 1];
|
||||
err_t err = vm_push_byte(vm, DBYTE(b));
|
||||
if (err)
|
||||
return err;
|
||||
@@ -360,11 +360,11 @@ err_t vm_push_word(vm_t *vm, data_t w)
|
||||
{
|
||||
if (vm->stack.ptr + WORD_SIZE >= vm->stack.max)
|
||||
return ERR_STACK_OVERFLOW;
|
||||
byte bytes[WORD_SIZE] = {0};
|
||||
byte_t bytes[WORD_SIZE] = {0};
|
||||
convert_word_to_bytes(w.as_word, bytes);
|
||||
for (size_t i = 0; i < WORD_SIZE; ++i)
|
||||
{
|
||||
byte b = bytes[WORD_SIZE - i - 1];
|
||||
byte_t b = bytes[WORD_SIZE - i - 1];
|
||||
err_t err = vm_push_byte(vm, DBYTE(b));
|
||||
if (err)
|
||||
return err;
|
||||
@@ -378,7 +378,7 @@ err_t vm_push_byte_register(vm_t *vm, word reg)
|
||||
return ERR_INVALID_REGISTER_BYTE;
|
||||
|
||||
// Interpret each word based register as 8 byte registers
|
||||
byte b = vm->registers.data[reg];
|
||||
byte_t b = vm->registers.data[reg];
|
||||
|
||||
return vm_push_byte(vm, DBYTE(b));
|
||||
}
|
||||
@@ -473,7 +473,7 @@ err_t vm_dup_hword(vm_t *vm, word w)
|
||||
{
|
||||
if (vm->stack.ptr < HWORD_SIZE * (w + 1))
|
||||
return ERR_STACK_UNDERFLOW;
|
||||
byte bytes[HWORD_SIZE] = {0};
|
||||
byte_t bytes[HWORD_SIZE] = {0};
|
||||
for (size_t i = 0; i < HWORD_SIZE; ++i)
|
||||
bytes[HWORD_SIZE - i - 1] =
|
||||
vm->stack.data[vm->stack.ptr - (HWORD_SIZE * (w + 1)) + i];
|
||||
@@ -484,7 +484,7 @@ err_t vm_dup_word(vm_t *vm, word w)
|
||||
{
|
||||
if (vm->stack.ptr < WORD_SIZE * (w + 1))
|
||||
return ERR_STACK_UNDERFLOW;
|
||||
byte bytes[WORD_SIZE] = {0};
|
||||
byte_t bytes[WORD_SIZE] = {0};
|
||||
for (size_t i = 0; i < WORD_SIZE; ++i)
|
||||
bytes[WORD_SIZE - i - 1] =
|
||||
vm->stack.data[vm->stack.ptr - (WORD_SIZE * (w + 1)) + i];
|
||||
@@ -621,7 +621,7 @@ err_t vm_pop_hword(vm_t *vm, data_t *ret)
|
||||
{
|
||||
if (vm->stack.ptr < HWORD_SIZE)
|
||||
return ERR_STACK_UNDERFLOW;
|
||||
byte bytes[HWORD_SIZE] = {0};
|
||||
byte_t bytes[HWORD_SIZE] = {0};
|
||||
for (size_t i = 0; i < HWORD_SIZE; ++i)
|
||||
{
|
||||
data_t b = {0};
|
||||
@@ -636,7 +636,7 @@ err_t vm_pop_word(vm_t *vm, data_t *ret)
|
||||
{
|
||||
if (vm->stack.ptr < WORD_SIZE)
|
||||
return ERR_STACK_UNDERFLOW;
|
||||
byte bytes[WORD_SIZE] = {0};
|
||||
byte_t bytes[WORD_SIZE] = {0};
|
||||
for (size_t i = 0; i < WORD_SIZE; ++i)
|
||||
{
|
||||
data_t b = {0};
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
#include "./struct.h"
|
||||
|
||||
void vm_load_stack(vm_t *vm, byte *bytes, size_t size)
|
||||
void vm_load_stack(vm_t *vm, byte_t *bytes, size_t size)
|
||||
{
|
||||
vm->stack.data = bytes;
|
||||
vm->stack.max = size;
|
||||
@@ -139,7 +139,7 @@ void vm_print_stack(vm_t *vm, FILE *fp)
|
||||
printf("\n");
|
||||
for (size_t i = stack.ptr; i > 0; --i)
|
||||
{
|
||||
byte b = stack.data[i - 1];
|
||||
byte_t b = stack.data[i - 1];
|
||||
fprintf(fp, "\t%lu: %X", stack.ptr - i, b);
|
||||
if (i != 1)
|
||||
fprintf(fp, ", ");
|
||||
|
||||
@@ -23,7 +23,7 @@ typedef darr_t registers_t;
|
||||
|
||||
struct Stack
|
||||
{
|
||||
byte *data;
|
||||
byte_t *data;
|
||||
size_t ptr, max;
|
||||
};
|
||||
|
||||
@@ -50,7 +50,7 @@ typedef struct
|
||||
} vm_t;
|
||||
|
||||
// Start and stop
|
||||
void vm_load_stack(vm_t *, byte *, size_t);
|
||||
void vm_load_stack(vm_t *, byte_t *, size_t);
|
||||
void vm_load_registers(vm_t *, registers_t);
|
||||
void vm_load_heap(vm_t *, heap_t);
|
||||
void vm_load_program(vm_t *, prog_t *);
|
||||
|
||||
Reference in New Issue
Block a user