Refactor type word -> word_t
This commit is contained in:
16
lib/base.c
16
lib/base.c
@@ -22,7 +22,7 @@ union hword_pun
|
||||
|
||||
union word_pun
|
||||
{
|
||||
word h;
|
||||
word_t h;
|
||||
byte_t bytes[WORD_SIZE];
|
||||
};
|
||||
|
||||
@@ -52,7 +52,7 @@ hword_t hword_bctoh(hword_t w)
|
||||
#endif
|
||||
}
|
||||
|
||||
word word_htobc(word w)
|
||||
word_t word_htobc(word_t w)
|
||||
{
|
||||
#if __LITTLE_ENDIAN__
|
||||
return w;
|
||||
@@ -65,7 +65,7 @@ word word_htobc(word w)
|
||||
#endif
|
||||
}
|
||||
|
||||
word word_bctoh(word w)
|
||||
word_t word_bctoh(word_t w)
|
||||
{
|
||||
#if __LITTLE_ENDIAN__
|
||||
return w;
|
||||
@@ -92,16 +92,16 @@ void convert_hword_to_bytes(hword_t w, byte_t *bytes)
|
||||
memcpy(bytes, &be_h, HWORD_SIZE);
|
||||
}
|
||||
|
||||
void convert_word_to_bytes(word w, byte_t *bytes)
|
||||
void convert_word_to_bytes(word_t w, byte_t *bytes)
|
||||
{
|
||||
word be_w = word_htobc(w);
|
||||
word_t be_w = word_htobc(w);
|
||||
memcpy(bytes, &be_w, WORD_SIZE);
|
||||
}
|
||||
|
||||
word convert_bytes_to_word(byte_t *bytes)
|
||||
word_t convert_bytes_to_word(byte_t *bytes)
|
||||
{
|
||||
word be_w = 0;
|
||||
word_t be_w = 0;
|
||||
memcpy(&be_w, bytes, WORD_SIZE);
|
||||
word w = word_bctoh(be_w);
|
||||
word_t w = word_bctoh(be_w);
|
||||
return w;
|
||||
}
|
||||
|
||||
22
lib/base.h
22
lib/base.h
@@ -46,15 +46,13 @@ typedef double f64;
|
||||
typedef u8 byte_t;
|
||||
typedef i8 char8_t;
|
||||
typedef u32 hword_t;
|
||||
typedef i32 s_hword;
|
||||
typedef u64 word;
|
||||
typedef i32 int_t;
|
||||
typedef u64 word_t;
|
||||
typedef i64 s_word;
|
||||
|
||||
/* Macros for the sizes of common base data types. */
|
||||
#define HWORD_SIZE sizeof(hword_t)
|
||||
#define SHWORD_SIZE sizeof(s_hword)
|
||||
#define WORD_SIZE sizeof(word)
|
||||
#define SWORD_SIZE sizeof(s_word)
|
||||
#define HWORD_SIZE sizeof(hword_t)
|
||||
#define WORD_SIZE sizeof(word_t)
|
||||
|
||||
/** Union for all basic data types in the virtual machine.
|
||||
*/
|
||||
@@ -63,8 +61,8 @@ typedef union
|
||||
byte_t as_byte;
|
||||
char8_t as_char;
|
||||
hword_t as_hword;
|
||||
s_hword as_int;
|
||||
word as_word;
|
||||
int_t as_int;
|
||||
word_t as_word;
|
||||
s_word as_long;
|
||||
} data_t;
|
||||
|
||||
@@ -119,14 +117,14 @@ void convert_hword_to_bytes(hword_t h, byte_t *buffer);
|
||||
* format (big endian) and that they are at least WORD_SIZE in
|
||||
* size.
|
||||
*/
|
||||
word convert_bytes_to_word(byte_t *);
|
||||
word_t 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_t *buffer);
|
||||
void convert_word_to_bytes(word_t w, byte_t *buffer);
|
||||
|
||||
/** Convert a half word into bytecode format (little endian)
|
||||
*/
|
||||
@@ -139,10 +137,10 @@ hword_t hword_bctoh(hword_t);
|
||||
|
||||
/** Convert a word into bytecode format (little endian)
|
||||
*/
|
||||
word word_htobc(word);
|
||||
word_t word_htobc(word_t);
|
||||
|
||||
/** Convert a word in bytecode format (little endian) to host format
|
||||
*/
|
||||
word word_bctoh(word);
|
||||
word_t word_bctoh(word_t);
|
||||
|
||||
#endif
|
||||
|
||||
12
lib/inst.c
12
lib/inst.c
@@ -370,7 +370,7 @@ data_t read_type_from_darr(darr_t *darr, data_type_t type)
|
||||
if (darr->used + WORD_SIZE > darr->available)
|
||||
// TODO: Error (darr has no space left)
|
||||
return DWORD(0);
|
||||
word w = convert_bytes_to_word(darr->data + darr->used);
|
||||
word_t w = convert_bytes_to_word(darr->data + darr->used);
|
||||
darr->used += WORD_SIZE;
|
||||
return DWORD(w);
|
||||
break;
|
||||
@@ -446,9 +446,9 @@ static_assert(sizeof(prog_t) == WORD_SIZE * 2,
|
||||
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);
|
||||
word_t start = word_htobc(program->start_address);
|
||||
darr_append_bytes(buffer, (byte_t *)&start, sizeof(start));
|
||||
word count = word_htobc(program->count);
|
||||
word_t count = word_htobc(program->count);
|
||||
darr_append_bytes(buffer, (byte_t *)&count, sizeof(count));
|
||||
|
||||
// Write instructions
|
||||
@@ -466,10 +466,10 @@ prog_t *prog_read_bytecode(darr_t *buffer)
|
||||
if ((buffer->available - buffer->used) < sizeof(prog_t))
|
||||
return NULL;
|
||||
// Read program header
|
||||
word start_address = convert_bytes_to_word(buffer->data + buffer->used);
|
||||
word_t start_address = convert_bytes_to_word(buffer->data + buffer->used);
|
||||
buffer->used += sizeof(start_address);
|
||||
word count = convert_bytes_to_word(buffer->data + buffer->used);
|
||||
buffer->used += sizeof(word);
|
||||
word_t count = convert_bytes_to_word(buffer->data + buffer->used);
|
||||
buffer->used += sizeof(word_t);
|
||||
|
||||
// TODO: Error (not enough space for program instruction count)
|
||||
if ((buffer->available - buffer->used) < WORD_SIZE)
|
||||
|
||||
@@ -165,8 +165,8 @@ typedef struct
|
||||
|
||||
typedef struct
|
||||
{
|
||||
word start_address;
|
||||
word count;
|
||||
word_t start_address;
|
||||
word_t count;
|
||||
inst_t instructions[];
|
||||
} prog_t;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user