Done TODO: Comment coverage > lib > base.h

Pretty simple
This commit is contained in:
2024-04-09 15:15:00 +06:30
parent 9d4e56c441
commit 84028dab79
2 changed files with 59 additions and 18 deletions

View File

@@ -15,6 +15,7 @@
#include <stdint.h> #include <stdint.h>
/* Basic macros for a variety of uses. Quite self explanatory. */
#define ARR_SIZE(xs) (sizeof(xs) / sizeof(xs[0])) #define ARR_SIZE(xs) (sizeof(xs) / sizeof(xs[0]))
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) > (b) ? (b) : (a)) #define MIN(a, b) ((a) > (b) ? (b) : (a))
@@ -23,7 +24,7 @@
#define TERM_RED "\e[0;31m" #define TERM_RED "\e[0;31m"
#define TERM_RESET "\e[0;0m" #define TERM_RESET "\e[0;0m"
// Flags // Flags for program behaviour (usually related to printing)
#ifndef VERBOSE #ifndef VERBOSE
#define VERBOSE 0 #define VERBOSE 0
#endif #endif
@@ -31,6 +32,7 @@
#define PRINT_HEX 0 #define PRINT_HEX 0
#endif #endif
/* Ease of use aliases for numeric types */
typedef uint8_t u8; typedef uint8_t u8;
typedef int8_t i8; typedef int8_t i8;
typedef uint32_t u32; typedef uint32_t u32;
@@ -48,6 +50,14 @@ 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. */
#define HWORD_SIZE sizeof(hword)
#define SHWORD_SIZE sizeof(s_hword)
#define WORD_SIZE sizeof(word)
#define SWORD_SIZE sizeof(s_word)
/** Union for all basic data types in the virtual machine.
*/
typedef union typedef union
{ {
byte as_byte; byte as_byte;
@@ -58,6 +68,8 @@ typedef union
s_word as_long; s_word as_long;
} data_t; } data_t;
/** Enum of type tags for the data_t structure to provide context.
*/
typedef enum typedef enum
{ {
DATA_TYPE_NIL = 0, DATA_TYPE_NIL = 0,
@@ -66,25 +78,54 @@ typedef enum
DATA_TYPE_WORD, DATA_TYPE_WORD,
} data_type_t; } data_type_t;
#define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)}) /* Some macros for constructing data_t instances quickly. */
#define DHWORD(HWORD) ((data_t){.as_hword = (HWORD)}) #define DBYTE(BYTE) ((data_t){.as_byte = (BYTE)})
#define DWORD(WORD) ((data_t){.as_word = (WORD)}) #define DHWORD(HWORD) ((data_t){.as_hword = (HWORD)})
#define DWORD(WORD) ((data_t){.as_word = (WORD)})
/** Safely subtract SUB from W, where both are words (64 bit integers).
*
* In case of underflow (i.e. where W - SUB < 0) returns 0 instead of
* the underflowed result.
*/
#define WORD_SAFE_SUB(W, SUB) ((W) > (SUB) ? ((W) - (SUB)) : 0) #define WORD_SAFE_SUB(W, SUB) ((W) > (SUB) ? ((W) - (SUB)) : 0)
#define HWORD_SIZE sizeof(hword) /** Return the Nth byte of WORD
#define SHWORD_SIZE sizeof(s_hword) * N should range from 0 to 7 as there are 8 bytes in a word.
#define WORD_SIZE sizeof(word) */
#define SWORD_SIZE sizeof(s_word) #define WORD_NTH_BYTE(WORD, N) (((WORD) >> ((N) * 8)) & 0xFF)
// Macros to extract the nth byte or nth hword from a word /** Return the Nth half word of WORD
#define WORD_NTH_BYTE(WORD, N) (((WORD) >> ((N)*8)) & 0xFF) * N should range from 0 to 1 as there are 2 half words in a word
#define WORD_NTH_HWORD(WORD, N) (((WORD) >> ((N)*2)) & 0xFFFFFFFF) */
#define WORD_NTH_HWORD(WORD, N) (((WORD) >> ((N) * 2)) & 0xFFFFFFFF)
// Assume array contains 4 bytes. /** Convert a buffer of bytes to a half word
hword convert_bytes_to_hword(byte *); * We assume the buffer of bytes are in virtual machine byte code
void convert_hword_to_bytes(hword, byte *); * format (big endian) and that they are at least HWORD_SIZE in
// Assume array contains 8 bytes. * size.
*/
hword convert_bytes_to_hword(byte *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);
/** 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 *);
void convert_word_to_bytes(word, byte *);
/** 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);
#endif #endif

View File

@@ -4,8 +4,8 @@
* TODO Better documentation [0%] :DOC: * TODO Better documentation [0%] :DOC:
** TODO Comment coverage [0%] ** TODO Comment coverage [0%]
*** WIP Lib [0%] *** WIP Lib [25%]
**** WIP lib/base.h **** DONE lib/base.h
**** TODO lib/darr.h **** TODO lib/darr.h
**** TODO lib/heap.h **** TODO lib/heap.h
**** TODO lib/inst.h **** TODO lib/inst.h