Refactored type hword -> hword_t
This commit is contained in:
16
lib/base.c
16
lib/base.c
@@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
union hword_pun
|
union hword_pun
|
||||||
{
|
{
|
||||||
hword h;
|
hword_t h;
|
||||||
byte_t bytes[HWORD_SIZE];
|
byte_t bytes[HWORD_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ union word_pun
|
|||||||
byte_t bytes[WORD_SIZE];
|
byte_t bytes[WORD_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
hword hword_htobc(hword w)
|
hword_t hword_htobc(hword_t w)
|
||||||
{
|
{
|
||||||
#if __LITTLE_ENDIAN__
|
#if __LITTLE_ENDIAN__
|
||||||
return w;
|
return w;
|
||||||
@@ -39,7 +39,7 @@ hword hword_htobc(hword w)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
hword hword_bctoh(hword w)
|
hword_t hword_bctoh(hword_t w)
|
||||||
{
|
{
|
||||||
#if __LITTLE_ENDIAN__
|
#if __LITTLE_ENDIAN__
|
||||||
return w;
|
return w;
|
||||||
@@ -78,17 +78,17 @@ word word_bctoh(word w)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
hword convert_bytes_to_hword(byte_t *bytes)
|
hword_t convert_bytes_to_hword(byte_t *bytes)
|
||||||
{
|
{
|
||||||
hword be_h = 0;
|
hword_t be_h = 0;
|
||||||
memcpy(&be_h, bytes, HWORD_SIZE);
|
memcpy(&be_h, bytes, HWORD_SIZE);
|
||||||
hword h = hword_bctoh(be_h);
|
hword_t h = hword_bctoh(be_h);
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
void convert_hword_to_bytes(hword w, byte_t *bytes)
|
void convert_hword_to_bytes(hword_t w, byte_t *bytes)
|
||||||
{
|
{
|
||||||
hword be_h = hword_htobc(w);
|
hword_t be_h = hword_htobc(w);
|
||||||
memcpy(bytes, &be_h, HWORD_SIZE);
|
memcpy(bytes, &be_h, HWORD_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
lib/base.h
18
lib/base.h
@@ -44,14 +44,14 @@ typedef float f32;
|
|||||||
typedef double f64;
|
typedef double f64;
|
||||||
|
|
||||||
typedef u8 byte_t;
|
typedef u8 byte_t;
|
||||||
typedef i8 s_byte;
|
typedef i8 char8_t;
|
||||||
typedef u32 hword;
|
typedef u32 hword_t;
|
||||||
typedef i32 s_hword;
|
typedef i32 s_hword;
|
||||||
typedef u64 word;
|
typedef u64 word;
|
||||||
typedef i64 s_word;
|
typedef i64 s_word;
|
||||||
|
|
||||||
/* Macros for the sizes of common base data types. */
|
/* Macros for the sizes of common base data types. */
|
||||||
#define HWORD_SIZE sizeof(hword)
|
#define HWORD_SIZE sizeof(hword_t)
|
||||||
#define SHWORD_SIZE sizeof(s_hword)
|
#define SHWORD_SIZE sizeof(s_hword)
|
||||||
#define WORD_SIZE sizeof(word)
|
#define WORD_SIZE sizeof(word)
|
||||||
#define SWORD_SIZE sizeof(s_word)
|
#define SWORD_SIZE sizeof(s_word)
|
||||||
@@ -61,8 +61,8 @@ typedef i64 s_word;
|
|||||||
typedef union
|
typedef union
|
||||||
{
|
{
|
||||||
byte_t as_byte;
|
byte_t as_byte;
|
||||||
s_byte as_char;
|
char8_t as_char;
|
||||||
hword as_hword;
|
hword_t as_hword;
|
||||||
s_hword as_int;
|
s_hword as_int;
|
||||||
word as_word;
|
word as_word;
|
||||||
s_word as_long;
|
s_word as_long;
|
||||||
@@ -105,14 +105,14 @@ typedef enum
|
|||||||
* format (big endian) and that they are at least HWORD_SIZE in
|
* format (big endian) and that they are at least HWORD_SIZE in
|
||||||
* size.
|
* size.
|
||||||
*/
|
*/
|
||||||
hword convert_bytes_to_hword(byte_t *buffer);
|
hword_t convert_bytes_to_hword(byte_t *buffer);
|
||||||
|
|
||||||
/** Convert a half word into a VM byte code format bytes (big endian)
|
/** Convert a half word into a VM byte code format bytes (big endian)
|
||||||
* @param h: Half word to convert
|
* @param h: Half word to convert
|
||||||
* @param buffer: Buffer to store into. We assume the buffer has at
|
* @param buffer: Buffer to store into. We assume the buffer has at
|
||||||
* least HWORD_SIZE space.
|
* least HWORD_SIZE space.
|
||||||
*/
|
*/
|
||||||
void convert_hword_to_bytes(hword h, byte_t *buffer);
|
void convert_hword_to_bytes(hword_t h, byte_t *buffer);
|
||||||
|
|
||||||
/** Convert a buffer of bytes to a word
|
/** Convert a buffer of bytes to a word
|
||||||
* We assume the buffer of bytes are in virtual machine byte code
|
* We assume the buffer of bytes are in virtual machine byte code
|
||||||
@@ -130,12 +130,12 @@ void convert_word_to_bytes(word w, byte_t *buffer);
|
|||||||
|
|
||||||
/** Convert a half word into bytecode format (little endian)
|
/** Convert a half word into bytecode format (little endian)
|
||||||
*/
|
*/
|
||||||
hword hword_htobc(hword);
|
hword_t hword_htobc(hword_t);
|
||||||
|
|
||||||
/** Convert a half word in bytecode format (little endian) to host
|
/** Convert a half word in bytecode format (little endian) to host
|
||||||
* format
|
* format
|
||||||
*/
|
*/
|
||||||
hword hword_bctoh(hword);
|
hword_t hword_bctoh(hword_t);
|
||||||
|
|
||||||
/** Convert a word into bytecode format (little endian)
|
/** Convert a word into bytecode format (little endian)
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ data_t read_type_from_darr(darr_t *darr, data_type_t type)
|
|||||||
if (darr->used + HWORD_SIZE > darr->available)
|
if (darr->used + HWORD_SIZE > darr->available)
|
||||||
// TODO: Error (darr has no space left)
|
// TODO: Error (darr has no space left)
|
||||||
return DWORD(0);
|
return DWORD(0);
|
||||||
hword u = convert_bytes_to_hword(darr->data + darr->used);
|
hword_t u = convert_bytes_to_hword(darr->data + darr->used);
|
||||||
darr->used += HWORD_SIZE;
|
darr->used += HWORD_SIZE;
|
||||||
return DHWORD(u);
|
return DHWORD(u);
|
||||||
break;
|
break;
|
||||||
|
|||||||
10
vm/runtime.c
10
vm/runtime.c
@@ -388,7 +388,7 @@ err_t vm_push_hword_register(vm_t *vm, word reg)
|
|||||||
if (reg > (vm->registers.used / HWORD_SIZE))
|
if (reg > (vm->registers.used / HWORD_SIZE))
|
||||||
return ERR_INVALID_REGISTER_HWORD;
|
return ERR_INVALID_REGISTER_HWORD;
|
||||||
// Interpret the bytes at point reg * HWORD_SIZE as an hword
|
// Interpret the bytes at point reg * HWORD_SIZE as an hword
|
||||||
hword hw = *(hword *)(vm->registers.data + (reg * HWORD_SIZE));
|
hword_t hw = *(hword_t *)(vm->registers.data + (reg * HWORD_SIZE));
|
||||||
return vm_push_hword(vm, DHWORD(hw));
|
return vm_push_hword(vm, DHWORD(hw));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,8 +435,8 @@ err_t vm_mov_hword(vm_t *vm, word reg)
|
|||||||
if (err)
|
if (err)
|
||||||
return err;
|
return err;
|
||||||
// Here we treat vm->registers as a set of hwords
|
// Here we treat vm->registers as a set of hwords
|
||||||
hword *hword_ptr = (hword *)(vm->registers.data + (reg * HWORD_SIZE));
|
hword_t *hword_ptr = (hword_t *)(vm->registers.data + (reg * HWORD_SIZE));
|
||||||
*hword_ptr = ret.as_hword;
|
*hword_ptr = ret.as_hword;
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -544,7 +544,7 @@ err_t vm_mset_hword(vm_t *vm, word nth)
|
|||||||
page_t *page = (page_t *)ptr.as_word;
|
page_t *page = (page_t *)ptr.as_word;
|
||||||
if (nth >= (page->available / HWORD_SIZE))
|
if (nth >= (page->available / HWORD_SIZE))
|
||||||
return ERR_OUT_OF_BOUNDS;
|
return ERR_OUT_OF_BOUNDS;
|
||||||
((hword *)page->data)[nth] = byte.as_hword;
|
((hword_t *)page->data)[nth] = byte.as_hword;
|
||||||
|
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
@@ -592,7 +592,7 @@ err_t vm_mget_hword(vm_t *vm, word n)
|
|||||||
page_t *page = (page_t *)ptr.as_word;
|
page_t *page = (page_t *)ptr.as_word;
|
||||||
if (n >= (page->available / HWORD_SIZE))
|
if (n >= (page->available / HWORD_SIZE))
|
||||||
return ERR_OUT_OF_BOUNDS;
|
return ERR_OUT_OF_BOUNDS;
|
||||||
return vm_push_hword(vm, DHWORD(((hword *)page->data)[n]));
|
return vm_push_hword(vm, DHWORD(((hword_t *)page->data)[n]));
|
||||||
}
|
}
|
||||||
|
|
||||||
err_t vm_mget_word(vm_t *vm, word n)
|
err_t vm_mget_word(vm_t *vm, word n)
|
||||||
|
|||||||
Reference in New Issue
Block a user