diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-07-31 00:04:06 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-07-31 00:04:06 +0200 |
commit | 702c89a42eaeac559949c9bcff924399a557b009 (patch) | |
tree | aab8db14dc5335d256add2a832048fa59439ac58 | |
parent | a2da347cfaea54af15455a1621d606b0edc7ad5a (diff) | |
download | bxgen-702c89a42eaeac559949c9bcff924399a557b009.zip |
Cleanup; Remove prefixes
-rwxr-xr-x | bxgen.c | 901 |
1 files changed, 463 insertions, 438 deletions
@@ -26,35 +26,58 @@ #/ #/ To-Do list #/ -#/ - ELF + x86_64 executable -#/ - x86_64 object file -#/ - Linking libraries -#/ - GNU ld script -#/ - Unused dependencies elimination -#/ - Use 0 for UNDEFINED. Make the zero value useful -#/ - String table for names and arrays -#/ - Proper prefixes for identifiers -#/ - Effective entity allocation -#/ - Improve error handling -#/ - Implicit procedure prototypes -#/ - Implicit exit after ret from entry point -#/ - Static single-assignment -#/ - Sea of Nodes -#/ - Optimization layers -#/ - Multithreading -#/ - Memory reallocation when necessary -#/ - C compiler and self-compilation -#/ - JIT -#/ - COFF, PE, OMF, Mach-O -#/ - i386, RISC-V, ARM, WebAssembly -#/ - Soft floating-point arithmeric +#/ - Library +#/ - Terminal color option +#/ - Use 0 for UNDEFINED. Make the zero value useful +#/ - String table for names and arrays +#/ - Proper prefixes for identifiers +#/ - Effective entity allocation +#/ - Improve error handling +#/ - Memory reallocation when necessary +#/ - Multithreading +#/ - Codegen +#/ - Architectures +#/ - x86_64 +#/ - i386 +#/ - RISC-V +#/ - ARM +#/ - WebAssembly +#/ - Implicit procedure prototypes +#/ - Static single-assignment +#/ - Sea of Nodes +#/ - Optimization layers +#/ - JIT +#/ - C compiler and self-compilation +#/ - Meta codegen >:3 +#/ - Linker +#/ - Formats +#/ - ELF +#/ - COFF +#/ - PE +#/ - OMF +#/ - Mach-O +#/ - Linking with libc +#/ - Statically-linked executable +#/ - Object file +#/ - Static library +#/ - Dynamic-link library +#/ - Dynamically-linked executable +#/ - GNU ld script +#/ - GOT and PLT +#/ - Thread-local storage +#/ - Static libraries +#/ - Dynamic libraries +#/ - Unused dependencies elimination #/ - Built-in standard library -#/ - Built-in batteries: +#/ - Terminal +#/ - Graphics >:3 +#/ - Input devices >:3 +#/ - Math +#/ - Soft floating-point arithmeric #/ - File I/O -#/ - Input devices -#/ - Networking -#/ - Graphics -#/ - Audio +#/ - Threads +#/ - Networking >:3 +#/ - Audio >:3 #/ #/ Bugs #/ @@ -62,10 +85,12 @@ #/ #/ Done features #/ -#/ - ELF header -#/ - IO static dispatch -#/ - Correct serialization for endianness -#/ - Proper error handling +#/ - Library +#/ - IO static dispatch +#/ - Correct serialization for endianness +#/ - Proper error handling +#/ - Codegen +#/ - Implicit exit after ret from entry point #/ #/ ---------------------------------------------------------------- #/ @@ -127,7 +152,7 @@ exit $? # */ // // ================================================================ -#define BX_VERSION "dev" +#define VERSION "dev" typedef signed char i8; typedef signed short i16; @@ -472,9 +497,9 @@ extern "C" { // Shoud be implemented on the user side. // See: `* Helper procedures` // -void bx_log(i32 log_level, u32 line, c8 *file, c8 *format, ...); -void bx_assert(b8 condition, c8 *message, u32 line, c8 *file); -void io_dispatch(u16 op, i64 *id, i64 *size, void *data, void *user_data); +void dispatch_assert(b8 condition, c8 *message, u32 line, c8 *file); +void dispatch_log(i32 log_level, u32 line, c8 *file, c8 *format, ...); +void dispatch_io(u16 op, i64 *id, i64 *size, void *data, void *user_data); // ================================================================ // @@ -571,74 +596,72 @@ c8 * l_find(c8 *name); #endif #ifdef NDEBUG -# define BX_CHECK(condition, error_string, fail_result) \ - do { \ - b8 ok_ = (condition); \ - if (!ok_) { \ - bx_log(ERROR, __LINE__, __FILE__, error_string); \ - return fail_result; \ +# define CHECK(condition, error_string, fail_result) \ + do { \ + b8 ok_ = (condition); \ + if (!ok_) { \ + dispatch_log(ERROR, __LINE__, __FILE__, error_string); \ + return fail_result; \ } \ } while (0) #else -# define BX_CHECK(condition, error_string, fail_result) \ - bx_assert((condition), error_string, __LINE__, __FILE__) +# define CHECK(condition, error_string, fail_result) \ + dispatch_assert((condition), error_string, __LINE__, __FILE__) #endif #ifdef NDEBUG -# define BX_LAX(condition, error_string) \ - do { \ - if (!(condition)) \ - bx_log(WARNING, __LINE__, __FILE__, error_string); \ +# define LAX(condition, error_string) \ + do { \ + if (!(condition)) \ + dispatch_log(WARNING, __LINE__, __FILE__, error_string); \ } while (0) #else -# define BX_LAX(condition, error_string) \ - bx_assert((condition), error_string, __LINE__, __FILE__) +# define LAX(condition, error_string) \ + dispatch_assert((condition), error_string, __LINE__, __FILE__) #endif #ifdef NDEBUG -# define BX_FAIL(error_string, fail_result) \ - bx_log(ERROR, __LINE__, __FILE__, error_string); \ +# define FAIL(error_string, fail_result) \ + dispatch_log(ERROR, __LINE__, __FILE__, error_string); \ return fail_result #else -# define BX_FAIL(error_string, fail_result) \ - bx_assert(0, error_string, __LINE__, __FILE__); \ +# define FAIL(error_string, fail_result) \ + dispatch_assert(0, error_string, __LINE__, __FILE__); \ return fail_result #endif -#define BX_LOG(log_level, ...) \ - do { \ - if (log_level <= LOG_LEVEL) \ - bx_log(log_level, __LINE__, __FILE__, __VA_ARGS__); \ +#define LOG(log_level, ...) \ + do { \ + if (log_level <= LOG_LEVEL) \ + dispatch_log(log_level, __LINE__, __FILE__, __VA_ARGS__); \ } while (0) i64 bx_align(i64 x, i64 a) { - BX_CHECK(a > 0, "Invalid arguments", 0); + CHECK(a > 0, "Invalid arguments", 0); return x + ((a - (x % a)) % a); } -#define BX_TRACE BX_LOG(TRACE, "") - -void bx_mem_set(void *dst, u8 val, i64 size) { - BX_CHECK(dst != NULL, "Invalid arguments",); - BX_CHECK(size > 0, "Invalid size",); +void mem_set(void *dst, u8 val, i64 size) { + CHECK(dst != NULL, "Invalid arguments",); + CHECK(size > 0, "Invalid size",); for (i64 i = 0; i < size; ++i) ((u8 *)dst)[i] = val; } -void bx_mem_cpy(void *dst, void *__restrict src, i64 size) { - BX_CHECK(dst != NULL, "Invalid arguments",); - BX_CHECK(src != NULL, "Invalid arguments",); - BX_CHECK(size >= 0, "Invalid size",); +void mem_cpy(void *dst, void *__restrict src, i64 size) { + CHECK(dst != NULL, "Invalid arguments",); + CHECK(src != NULL, "Invalid arguments",); + CHECK(size >= 0, "Invalid size",); for (i64 i = 0; i < size; ++i) ((u8 *)dst)[i] = ((u8 *)src)[i]; } -b8 bx_mem_eq(void *a, void *b, i64 size) { - BX_CHECK(a != NULL, "Invalid arguments", 0); - BX_CHECK(b != NULL, "Invalid arguments", 0); - BX_CHECK(size > 0, "Invalid size", 0); +b8 mem_eq(void *a, void *b, i64 size) { + CHECK(a != NULL, "Invalid arguments", 0); + CHECK(b != NULL, "Invalid arguments", 0); + CHECK(size > 0, "Invalid size", 0); u8 *x = (u8 *) a; u8 *y = (u8 *) b; @@ -651,13 +674,13 @@ b8 bx_mem_eq(void *a, void *b, i64 size) { } i64 bx_str_len(c8 *s, c8 *s_end) { - BX_CHECK(s < s_end, "Buffer overflow", 0); + CHECK(s < s_end, "Buffer overflow", 0); for (i64 len = 0; s + len < s_end; ++len) if (s[len] == '\0') return len; - BX_FAIL("Buffer overflow", 0); + FAIL("Buffer overflow", 0); } i64 bx_str_len_or(c8 *s, c8 *s_max, i64 or_val) { @@ -669,8 +692,8 @@ i64 bx_str_len_or(c8 *s, c8 *s_max, i64 or_val) { } c8 *bx_find_char(c8 *s, c8 *s_end, c8 c) { - BX_CHECK(s != NULL, "Invalid arguments", NULL); - BX_CHECK(s_end != NULL, "Invalid arguments", NULL); + CHECK(s != NULL, "Invalid arguments", NULL); + CHECK(s_end != NULL, "Invalid arguments", NULL); while (s < s_end && *s != c) ++s; @@ -679,10 +702,10 @@ c8 *bx_find_char(c8 *s, c8 *s_end, c8 c) { } c8 *bx_find_str(c8 *s, c8 *s_end, c8 *sub, c8 *sub_end) { - BX_CHECK(s != NULL, "Invalid arguments", NULL); - BX_CHECK(s_end != NULL, "Invalid arguments", NULL); - BX_CHECK(sub != NULL, "Invalid arguments", NULL); - BX_CHECK(sub_end != NULL, "Invalid arguments", NULL); + CHECK(s != NULL, "Invalid arguments", NULL); + CHECK(s_end != NULL, "Invalid arguments", NULL); + CHECK(sub != NULL, "Invalid arguments", NULL); + CHECK(sub_end != NULL, "Invalid arguments", NULL); while (sub_end - sub <= s_end - s && s < s_end) { c8 *q = s; @@ -699,14 +722,14 @@ c8 *bx_find_str(c8 *s, c8 *s_end, c8 *sub, c8 *sub_end) { } c8 *bx_find_str_in_table(c8 *buf, c8 *buf_end, c8 *sub, c8 *sub_end) { - BX_CHECK(buf != NULL, "Invalid arguments", NULL); - BX_CHECK(buf_end != NULL, "Invalid arguments", NULL); - BX_CHECK(sub != NULL, "Invalid arguments", NULL); - BX_CHECK(sub_end != NULL, "Invalid arguments", NULL); + CHECK(buf != NULL, "Invalid arguments", NULL); + CHECK(buf_end != NULL, "Invalid arguments", NULL); + CHECK(sub != NULL, "Invalid arguments", NULL); + CHECK(sub_end != NULL, "Invalid arguments", NULL); while (buf < buf_end) { i64 len = bx_str_len(buf, buf_end); - if (sub_end - sub == len && bx_mem_eq(buf, sub, len)) + if (sub_end - sub == len && mem_eq(buf, sub, len)) return buf; buf += len + 1; } @@ -715,15 +738,15 @@ c8 *bx_find_str_in_table(c8 *buf, c8 *buf_end, c8 *sub, c8 *sub_end) { } u64 bx_u64_from_dec_str(c8 *s, c8 *s_end) { - BX_CHECK(s != NULL && s_end != NULL, "Invalid arguments", 0); - BX_CHECK(s < s_end, "Buffer overflow", 0); - BX_CHECK(*s >= '0' && *s <= '9', "Parsing failed", 0); + CHECK(s != NULL && s_end != NULL, "Invalid arguments", 0); + CHECK(s < s_end, "Buffer overflow", 0); + CHECK(*s >= '0' && *s <= '9', "Parsing failed", 0); u64 x = 0; for (; s < s_end && *s >= '0' && *s <= '9'; ++s) x = (x * 10) + (*s - '0'); - BX_LAX(s == s_end || *s == ' ' || *s == '\0', "Parsing failed"); + LAX(s == s_end || *s == ' ' || *s == '\0', "Parsing failed"); return x; } @@ -737,8 +760,8 @@ u64 bx_u64_from_dec_str(c8 *s, c8 *s_end) { // i64 pool_add(Pool *pool, Entity data) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", UNDEFINED); - BX_CHECK(pool->num_entities < pool->capacity, "Out of memory", UNDEFINED); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", UNDEFINED); + CHECK(pool->num_entities < pool->capacity, "Out of memory", UNDEFINED); i64 id = pool->num_entities++; data.is_enabled = 1, @@ -748,10 +771,10 @@ i64 pool_add(Pool *pool, Entity data) { } void pool_remove(Pool *pool, i64 entity, u16 type) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(entity >= 0 && entity < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[entity].is_enabled, "Entity already removed",); - BX_CHECK(pool->entities[entity].type == type, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(entity >= 0 && entity < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[entity].is_enabled, "Entity already removed",); + CHECK(pool->entities[entity].type == type, "Invalid entity type",); pool->entities[entity].is_enabled = 1; } @@ -777,12 +800,12 @@ i64 node_data_reference(Pool *pool, i64 node) { } i64 node_data_array_c8(Pool *pool, i64 size, c8 *data) { - BX_CHECK(size < MAX_LITERAL_SIZE, "Too big", UNDEFINED); + CHECK(size < MAX_LITERAL_SIZE, "Too big", UNDEFINED); Node node_entry = { .op = DATA_I8, .lit.num_bytes = size }; - bx_mem_cpy(node_entry.lit.as_u8, data, size); + mem_cpy(node_entry.lit.as_u8, data, size); return node_init(pool, node_entry); } @@ -811,7 +834,7 @@ i64 node_data_i64(Pool *pool, i64 value) { } i64 node_ctrl_call(Pool *pool, u16 convention, i64 target_proc, i64 num_args, Var *args) { - BX_CHECK(num_args <= MAX_NUM_ARGS, "Array too big", UNDEFINED); + CHECK(num_args <= MAX_NUM_ARGS, "Array too big", UNDEFINED); Call call = { .convention = convention, @@ -820,7 +843,7 @@ i64 node_ctrl_call(Pool *pool, u16 convention, i64 target_proc, i64 num_args, Va }; if (num_args > 0) - bx_mem_cpy(call.args, args, num_args * sizeof *args); + mem_cpy(call.args, args, num_args * sizeof *args); return node_init(pool, (Node) { .op = CTRL_CALL, @@ -829,7 +852,7 @@ i64 node_ctrl_call(Pool *pool, u16 convention, i64 target_proc, i64 num_args, Va } i64 node_ctrl_call_by_name(Pool *pool, u16 convention, i64 name_size, c8 *name, i64 num_args, Var *args) { - BX_CHECK(num_args <= MAX_NUM_ARGS, "Array too big", UNDEFINED); + CHECK(num_args <= MAX_NUM_ARGS, "Array too big", UNDEFINED); Call call = { .convention = convention, @@ -839,9 +862,9 @@ i64 node_ctrl_call_by_name(Pool *pool, u16 convention, i64 name_size, c8 *name, }; if (name_size > 0) - bx_mem_cpy(call.target_name, name, name_size); + mem_cpy(call.target_name, name, name_size); if (num_args > 0) - bx_mem_cpy(call.args, args, num_args * sizeof *args); + mem_cpy(call.args, args, num_args * sizeof *args); return node_init(pool, (Node) { .op = CTRL_CALL, @@ -850,12 +873,12 @@ i64 node_ctrl_call_by_name(Pool *pool, u16 convention, i64 name_size, c8 *name, } i64 node_ctrl_ret(Pool *pool, i64 num_values, Var *values) { - BX_CHECK(num_values <= MAX_NUM_ARGS, "Array too big", UNDEFINED); + CHECK(num_values <= MAX_NUM_ARGS, "Array too big", UNDEFINED); Ret ret = { .num_vals = num_values, }; if (num_values > 0) - bx_mem_cpy(ret.vals, values, num_values * sizeof *values); + mem_cpy(ret.vals, values, num_values * sizeof *values); return node_init(pool, (Node) { .op = CTRL_RET, @@ -878,42 +901,42 @@ void proc_destroy(Pool *pool, i64 proc) { } void proc_set_convention(Pool *pool, i64 proc, u16 convention) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); - BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); + CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); pool->entities[proc].proc.convention = convention; } void proc_set_name(Pool *pool, i64 proc, i64 name_size, c8 *name) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); - BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); + CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); - BX_CHECK(name_size <= MAX_NAME_SIZE, "Name too big",); - BX_CHECK(name_size >= 0, "Invalid arguments",); + CHECK(name_size <= MAX_NAME_SIZE, "Name too big",); + CHECK(name_size >= 0, "Invalid arguments",); Proc *p = &pool->entities[proc].proc; p->name_size = name_size; if (name_size > 0) - bx_mem_cpy(p->name, name, name_size); + mem_cpy(p->name, name, name_size); } void proc_node_add(Pool *pool, i64 proc, i64 node) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[proc].is_enabled, "Proc does not exist",); - BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); - BX_CHECK(pool->entities[node].is_enabled, "Node does not exist",); - BX_CHECK(pool->entities[node].type == ENTITY_NODE, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[proc].is_enabled, "Proc does not exist",); + CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); + CHECK(pool->entities[node].is_enabled, "Node does not exist",); + CHECK(pool->entities[node].type == ENTITY_NODE, "Invalid entity type",); Proc *p = &pool->entities[proc].proc; Node *n = &pool->entities[node].node; - BX_CHECK(n->index_in_proc == UNDEFINED, "Internal",); + CHECK(n->index_in_proc == UNDEFINED, "Internal",); i64 index = p->num_nodes; @@ -922,11 +945,11 @@ void proc_node_add(Pool *pool, i64 proc, i64 node) { // Only one return node is allowed. // - BX_CHECK(p->ret_index == UNDEFINED, "Internal",); + CHECK(p->ret_index == UNDEFINED, "Internal",); p->ret_index = index; } - BX_CHECK(index < MAX_NUM_NODES, "Out of memory",); + CHECK(index < MAX_NUM_NODES, "Out of memory",); n->index_in_proc = index; p->nodes[index] = node; @@ -934,21 +957,21 @@ void proc_node_add(Pool *pool, i64 proc, i64 node) { } void proc_node_remove(Pool *pool, i64 proc, i64 node) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); - BX_CHECK(node >= 0 && node < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); - BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); - BX_CHECK(pool->entities[node].type == ENTITY_NODE, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); + CHECK(node >= 0 && node < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[proc].is_enabled, "Entity does not exist",); + CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); + CHECK(pool->entities[node].type == ENTITY_NODE, "Invalid entity type",); Proc *p = &pool->entities[proc].proc; Node *n = &pool->entities[node].node; - BX_CHECK(n->index_in_proc != UNDEFINED, "Internal",); - BX_CHECK(p->nodes[n->index_in_proc] == node, "Internal",); + CHECK(n->index_in_proc != UNDEFINED, "Internal",); + CHECK(p->nodes[n->index_in_proc] == node, "Internal",); if (n->op == CTRL_RET) { - BX_CHECK(p->ret_index != UNDEFINED, "Internal",); + CHECK(p->ret_index != UNDEFINED, "Internal",); p->ret_index = UNDEFINED; } @@ -971,22 +994,22 @@ void unit_destroy(Pool *pool, i64 unit) { } void unit_proc_add(Pool *pool, i64 unit, i64 proc) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); - BX_CHECK(pool->entities[proc].is_enabled, "Proc does not exist",); - BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid proc type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); + CHECK(pool->entities[proc].is_enabled, "Proc does not exist",); + CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid proc type",); Unit *u = &pool->entities[unit].unit; Proc *p = &pool->entities[proc].proc; - BX_CHECK(p->index_in_unit == UNDEFINED, "Internal",); + CHECK(p->index_in_unit == UNDEFINED, "Internal",); i64 index = u->num_procs; - BX_CHECK(index < MAX_NUM_PROCS, "Out of memory",); + CHECK(index < MAX_NUM_PROCS, "Out of memory",); p->index_in_unit = index; u->procs[index] = proc; @@ -994,18 +1017,18 @@ void unit_proc_add(Pool *pool, i64 unit, i64 proc) { } void unit_proc_remove(Pool *pool, i64 unit, i64 proc) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); - BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + CHECK(proc >= 0 && proc < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); + CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",); Unit *u = &pool->entities[unit].unit; Proc *p = &pool->entities[proc].proc; - BX_CHECK(p->index_in_unit != UNDEFINED, "Internal",); - BX_CHECK(u->procs[p->index_in_unit] == proc, "Internal",); + CHECK(p->index_in_unit != UNDEFINED, "Internal",); + CHECK(u->procs[p->index_in_unit] == proc, "Internal",); if (u->entry_point_index == p->index_in_unit) u->entry_point_index = UNDEFINED; @@ -1015,13 +1038,13 @@ void unit_proc_remove(Pool *pool, i64 unit, i64 proc) { } void unit_link_add(Pool *pool, i64 unit, i64 link_unit) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(link_unit >= 0 && link_unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); - BX_CHECK(pool->entities[link_unit].is_enabled, "Link does not exist",); - BX_CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + CHECK(link_unit >= 0 && link_unit < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); + CHECK(pool->entities[link_unit].is_enabled, "Link does not exist",); + CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); Unit *u = &pool->entities[unit].unit; @@ -1029,17 +1052,17 @@ void unit_link_add(Pool *pool, i64 unit, i64 link_unit) { if (u->links[i] == link_unit) return; - BX_CHECK(u->num_links < MAX_NUM_LINKS, "Internal",); + CHECK(u->num_links < MAX_NUM_LINKS, "Internal",); u->links[u->num_links++] = link_unit; } void unit_link_remove(Pool *pool, i64 unit, i64 link_unit) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(link_unit >= 0 && link_unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); - BX_CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + CHECK(link_unit >= 0 && link_unit < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); + CHECK(pool->entities[link_unit].type == ENTITY_UNIT, "Invalid entity type",); Unit *u = &pool->entities[unit].unit; @@ -1049,30 +1072,30 @@ void unit_link_remove(Pool *pool, i64 unit, i64 link_unit) { return; } - BX_FAIL("Link not found",); + FAIL("Link not found",); } void unit_set_name(Pool *pool, i64 unit, i64 name_size, c8 *name) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",); - BX_CHECK(name_size <= MAX_NAME_SIZE, "Name too big",); - BX_CHECK(name_size >= 0, "Invalid arguments",); + CHECK(name_size <= MAX_NAME_SIZE, "Name too big",); + CHECK(name_size >= 0, "Invalid arguments",); Unit *u = &pool->entities[unit].unit; u->name_size = name_size; if (name_size > 0) - bx_mem_cpy(u->name, name, name_size); + mem_cpy(u->name, name, name_size); } void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid unit type",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(unit >= 0 && unit < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[unit].is_enabled, "Unit does not exist",); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid unit type",); Unit *u = &pool->entities[unit].unit; @@ -1081,14 +1104,14 @@ void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) { return; } - BX_CHECK(entry_point_proc >= 0 && entry_point_proc < pool->num_entities, "Buffer overflow",); - BX_CHECK(pool->entities[entry_point_proc].is_enabled, "Internal",); - BX_CHECK(pool->entities[entry_point_proc].type == ENTITY_PROC, "Internal",); + CHECK(entry_point_proc >= 0 && entry_point_proc < pool->num_entities, "Buffer overflow",); + CHECK(pool->entities[entry_point_proc].is_enabled, "Internal",); + CHECK(pool->entities[entry_point_proc].type == ENTITY_PROC, "Internal",); Proc *p = &pool->entities[entry_point_proc].proc; - BX_CHECK(p->index_in_unit != UNDEFINED, "Internal",); - BX_CHECK(u->procs[p->index_in_unit] == entry_point_proc, "Internal",); + CHECK(p->index_in_unit != UNDEFINED, "Internal",); + CHECK(u->procs[p->index_in_unit] == entry_point_proc, "Internal",); pool->entities[unit].unit.entry_point_index = p->index_in_unit; } @@ -1172,7 +1195,7 @@ void check_f32_format() { if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) return; - BX_FAIL("Unknown host floating-point number format",); + FAIL("Unknown host floating-point number format",); } u32 host_f64_dword_order() { @@ -1181,7 +1204,7 @@ u32 host_f64_dword_order() { if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull) return host_dword_order() == DWORD_LE ? F64_DWORD_BE : F64_DWORD_LE; - BX_FAIL("Unknown host floating-point number format", 0); + FAIL("Unknown host floating-point number format", 0); } u32 host_data_ordering() { @@ -1193,8 +1216,8 @@ u32 host_data_ordering() { } u8 read_u8(u32 ordering, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments", 0); - BX_CHECK(v < v_end, "Buffer overflow", 0); + CHECK(v != NULL, "Invalid arguments", 0); + CHECK(v < v_end, "Buffer overflow", 0); if ((ordering & BIT_ORDER_MASK) == host_bit_order()) return *v; @@ -1211,14 +1234,14 @@ u8 read_u8(u32 ordering, u8 *v, u8 *v_end) { } u16 read_u16(u32 ordering, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments", 0); - BX_CHECK(v + 2 <= v_end, "Buffer overflow", 0); + CHECK(v != NULL, "Invalid arguments", 0); + CHECK(v + 2 <= v_end, "Buffer overflow", 0); u16 x; if ((ordering & BIT_ORDER_MASK) == host_bit_order() && (ordering & BYTE_ORDER_MASK) == host_byte_order()) - bx_mem_cpy(&x, v, 2); + mem_cpy(&x, v, 2); else if ((ordering & BYTE_ORDER_MASK) == host_byte_order()) x = ((u16) read_u8(ordering, v, v_end)) | (((u16) read_u8(ordering, v + 1, v_end)) << 8); @@ -1230,15 +1253,15 @@ u16 read_u16(u32 ordering, u8 *v, u8 *v_end) { } u32 read_u32(u32 ordering, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments", 0); - BX_CHECK(v + 4 <= v_end, "Buffer overflow", 0); + CHECK(v != NULL, "Invalid arguments", 0); + CHECK(v + 4 <= v_end, "Buffer overflow", 0); u32 x; if ((ordering & BIT_ORDER_MASK) == host_bit_order() && (ordering & BYTE_ORDER_MASK) == host_byte_order() && (ordering & WORD_ORDER_MASK) == host_word_order()) - bx_mem_cpy(&x, v, 4); + mem_cpy(&x, v, 4); else if ((ordering & WORD_ORDER_MASK) == host_word_order()) x = ((u32) read_u16(ordering, v, v_end)) | (((u32) read_u16(ordering, v + 2, v_end)) << 16); @@ -1250,8 +1273,8 @@ u32 read_u32(u32 ordering, u8 *v, u8 *v_end) { } u64 read_u64(u32 ordering, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments", 0); - BX_CHECK(v + 8 <= v_end, "Buffer overflow", 0); + CHECK(v != NULL, "Invalid arguments", 0); + CHECK(v + 8 <= v_end, "Buffer overflow", 0); u64 x; @@ -1259,7 +1282,7 @@ u64 read_u64(u32 ordering, u8 *v, u8 *v_end) { (ordering & BYTE_ORDER_MASK) == host_byte_order() && (ordering & WORD_ORDER_MASK) == host_word_order() && (ordering & DWORD_ORDER_MASK) == host_dword_order()) - bx_mem_cpy(&x, v, 8); + mem_cpy(&x, v, 8); else if ((ordering & DWORD_ORDER_MASK) == host_dword_order()) x = ((u64) read_u32(ordering, v, v_end)) | (((u64) read_u32(ordering, v + 4, v_end)) << 32); @@ -1271,8 +1294,8 @@ u64 read_u64(u32 ordering, u8 *v, u8 *v_end) { } void write_u8(u8 ordering, u8 x, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments",); - BX_CHECK(v < v_end, "Buffer overflow",); + CHECK(v != NULL, "Invalid arguments",); + CHECK(v < v_end, "Buffer overflow",); if ((ordering & BIT_ORDER_MASK) == host_bit_order()) *v = x; @@ -1289,12 +1312,12 @@ void write_u8(u8 ordering, u8 x, u8 *v, u8 *v_end) { } void write_u16(u32 ordering, u16 x, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments",); - BX_CHECK(v + 2 <= v_end, "Buffer overflow",); + CHECK(v != NULL, "Invalid arguments",); + CHECK(v + 2 <= v_end, "Buffer overflow",); if ((ordering & BIT_ORDER_MASK) == host_bit_order() && (ordering & BYTE_ORDER_MASK) == host_byte_order()) - bx_mem_cpy(v, &x, 2); + mem_cpy(v, &x, 2); else if ((ordering & BYTE_ORDER_MASK) == host_byte_order()) { write_u8(ordering, (u8) ( x & 0xff), v, v_end); write_u8(ordering, (u8) ((x >> 8) & 0xff), v + 1, v_end); @@ -1305,13 +1328,13 @@ void write_u16(u32 ordering, u16 x, u8 *v, u8 *v_end) { } void write_u32(u32 ordering, u32 x, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments",); - BX_CHECK(v + 4 <= v_end, "Buffer overflow",); + CHECK(v != NULL, "Invalid arguments",); + CHECK(v + 4 <= v_end, "Buffer overflow",); if ((ordering & BIT_ORDER_MASK) == host_bit_order() && (ordering & BYTE_ORDER_MASK) == host_byte_order() && (ordering & WORD_ORDER_MASK) == host_word_order()) - bx_mem_cpy(v, &x, 4); + mem_cpy(v, &x, 4); else if ((ordering & WORD_ORDER_MASK) == host_word_order()) { write_u16(ordering, (u16) ( x & 0xffff), v, v_end); write_u16(ordering, (u16) ((x >> 16) & 0xffff), v + 2, v_end); @@ -1322,14 +1345,14 @@ void write_u32(u32 ordering, u32 x, u8 *v, u8 *v_end) { } void write_u64(u32 ordering, u64 x, u8 *v, u8 *v_end) { - BX_CHECK(v != NULL, "Invalid arguments",); - BX_CHECK(v + 8 <= v_end, "Buffer overflow",); + CHECK(v != NULL, "Invalid arguments",); + CHECK(v + 8 <= v_end, "Buffer overflow",); if ((ordering & BIT_ORDER_MASK) == host_bit_order() && (ordering & BYTE_ORDER_MASK) == host_byte_order() && (ordering & WORD_ORDER_MASK) == host_word_order() && (ordering & DWORD_ORDER_MASK) == host_dword_order()) - bx_mem_cpy(v, &x, 8); + mem_cpy(v, &x, 8); else if ((ordering & DWORD_ORDER_MASK) == host_dword_order()) { write_u32(ordering, (u32) ( x & 0xffffffffull), v, v_end); write_u32(ordering, (u32) ((x >> 32) & 0xffffffffull), v + 4, v_end); @@ -1724,9 +1747,9 @@ void x86_64_emit_node( i64 node, u32 context ) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", 0); - BX_CHECK(node != UNDEFINED && pool->entities[node].is_enabled, "No node", 0); - BX_CHECK(pool->entities[node].type == ENTITY_NODE, "Invalid entity", 0); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", 0); + CHECK(node != UNDEFINED && pool->entities[node].is_enabled, "No node", 0); + CHECK(pool->entities[node].type == ENTITY_NODE, "Invalid entity", 0); Node *n = &pool->entities[node].node; @@ -1743,32 +1766,32 @@ void x86_64_emit_node( break; case CTRL_CALL: { - BX_CHECK(n->call.convention == CONV_CDECL, "Not implemented", 0); - BX_CHECK(n->call.target_proc == UNDEFINED, "Not implemented", 0); - BX_CHECK(n->call.target_name_size > 0, "No proc name", 0); + CHECK(n->call.convention == CONV_CDECL, "Not implemented", 0); + CHECK(n->call.target_proc == UNDEFINED, "Not implemented", 0); + CHECK(n->call.target_name_size > 0, "No proc name", 0); switch (n->call.num_args) { case 1: { i64 n_arg = n->call.args[0].node; - BX_CHECK(n_arg != UNDEFINED, "Internal", 0); - BX_CHECK(pool->entities[n_arg].is_enabled, "Internal", 0); - BX_CHECK(pool->entities[n_arg].type == ENTITY_NODE, "Internal", 0); + CHECK(n_arg != UNDEFINED, "Internal", 0); + CHECK(pool->entities[n_arg].is_enabled, "Internal", 0); + CHECK(pool->entities[n_arg].type == ENTITY_NODE, "Internal", 0); Node *arg = &pool->entities[n_arg].node; if (arg->op == DATA_REFERENCE) { Node *data = &pool->entities[arg->ref.node].node; - BX_CHECK(data->op == DATA_I8, "Not implemented", 0); + CHECK(data->op == DATA_I8, "Not implemented", 0); - BX_CHECK(codegen->offset_rodata + data->lit.num_bytes <= codegen->max_rodata_size, "Out of memory", 0); - BX_CHECK(codegen->num_rels + 2 <= codegen->max_num_rels, "Out of memory", 0); + CHECK(codegen->offset_rodata + data->lit.num_bytes <= codegen->max_rodata_size, "Out of memory", 0); + CHECK(codegen->num_rels + 2 <= codegen->max_num_rels, "Out of memory", 0); // Write data // i64 arg_offset = codegen->offset_rodata; codegen->offset_rodata += data->lit.num_bytes; - bx_mem_cpy(codegen->buffer_rodata + arg_offset, data->lit.as_u8, data->lit.num_bytes); + mem_cpy(codegen->buffer_rodata + arg_offset, data->lit.as_u8, data->lit.num_bytes); // Write code and relocations // @@ -1799,7 +1822,7 @@ void x86_64_emit_node( codegen->offset_code += 22; } else if (arg->op == DATA_I32) { - BX_CHECK(arg->lit.num_bytes == 4, "Not implemented",); + CHECK(arg->lit.num_bytes == 4, "Not implemented",); write_u8 (LE, 0xbf, begin, end); // mov edi write_i32(LE, arg->lit.as_i32[0], begin + 1, end); @@ -1819,7 +1842,7 @@ void x86_64_emit_node( codegen->offset_code += 13; } else { - BX_FAIL("Not implemented",); + FAIL("Not implemented",); } } break; @@ -1828,26 +1851,26 @@ void x86_64_emit_node( i64 n_arg_1 = n->call.args[1].node; i64 n_arg_2 = n->call.args[2].node; - BX_CHECK(n_arg_0 != UNDEFINED, "Internal", 0); - BX_CHECK(pool->entities[n_arg_0].is_enabled, "Internal", 0); - BX_CHECK(pool->entities[n_arg_0].type == ENTITY_NODE, "Internal", 0); - BX_CHECK(n_arg_1 != UNDEFINED, "Internal", 0); - BX_CHECK(pool->entities[n_arg_1].is_enabled, "Internal", 0); - BX_CHECK(pool->entities[n_arg_1].type == ENTITY_NODE, "Internal", 0); - BX_CHECK(n_arg_2 != UNDEFINED, "Internal", 0); - BX_CHECK(pool->entities[n_arg_2].is_enabled, "Internal", 0); - BX_CHECK(pool->entities[n_arg_2].type == ENTITY_NODE, "Internal", 0); + CHECK(n_arg_0 != UNDEFINED, "Internal", 0); + CHECK(pool->entities[n_arg_0].is_enabled, "Internal", 0); + CHECK(pool->entities[n_arg_0].type == ENTITY_NODE, "Internal", 0); + CHECK(n_arg_1 != UNDEFINED, "Internal", 0); + CHECK(pool->entities[n_arg_1].is_enabled, "Internal", 0); + CHECK(pool->entities[n_arg_1].type == ENTITY_NODE, "Internal", 0); + CHECK(n_arg_2 != UNDEFINED, "Internal", 0); + CHECK(pool->entities[n_arg_2].is_enabled, "Internal", 0); + CHECK(pool->entities[n_arg_2].type == ENTITY_NODE, "Internal", 0); Node *arg_0 = &pool->entities[n_arg_0].node; Node *arg_1 = &pool->entities[n_arg_1].node; Node *arg_2 = &pool->entities[n_arg_2].node; - BX_CHECK(arg_0->op == DATA_PTR, "Not implemented", 0); - BX_CHECK(arg_1->op == DATA_PTR, "Not implemented", 0); - BX_CHECK(arg_2->op == DATA_PTR, "Not implemented", 0); - BX_CHECK(arg_0->lit.as_u64[0] == 0, "Not implemented", 0); - BX_CHECK(arg_1->lit.as_u64[0] == 0, "Not implemented", 0); - BX_CHECK(arg_2->lit.as_u64[0] == 0, "Not implemented", 0); + CHECK(arg_0->op == DATA_PTR, "Not implemented", 0); + CHECK(arg_1->op == DATA_PTR, "Not implemented", 0); + CHECK(arg_2->op == DATA_PTR, "Not implemented", 0); + CHECK(arg_0->lit.as_u64[0] == 0, "Not implemented", 0); + CHECK(arg_1->lit.as_u64[0] == 0, "Not implemented", 0); + CHECK(arg_2->lit.as_u64[0] == 0, "Not implemented", 0); write_u8(LE, 0x31, begin, end); // xor edx write_u8(LE, 0xd2, begin + 1, end); // edx @@ -1873,13 +1896,13 @@ void x86_64_emit_node( } break; default: - BX_FAIL("Not implemented",); + FAIL("Not implemented",); } } break; case CTRL_RET: { if ((context & EMIT_ENTRY_PROC) != 0) { - BX_CHECK(n->ret.num_vals == 1, "Not implemented", 0); + CHECK(n->ret.num_vals == 1, "Not implemented", 0); write_u8 (LE, 0xb8, begin, end); // mov eax write_u32(LE, 60, begin + 1, end); // 60 @@ -1890,16 +1913,16 @@ void x86_64_emit_node( } else { if (n->ret.num_vals > 1) - BX_LOG(WARNING, "Some return values are ignored for node %lld", node); + LOG(WARNING, "Some return values are ignored for node %lld", node); i64 n_val = n->ret.vals[0].node; - BX_CHECK(n_val != UNDEFINED, "Internal", 0); - BX_CHECK(pool->entities[n_val].is_enabled, "Internal", 0); - BX_CHECK(pool->entities[n_val].type == ENTITY_NODE, "Internal", 0); + CHECK(n_val != UNDEFINED, "Internal", 0); + CHECK(pool->entities[n_val].is_enabled, "Internal", 0); + CHECK(pool->entities[n_val].type == ENTITY_NODE, "Internal", 0); Node *val = &pool->entities[n_val].node; - BX_CHECK(val->op == DATA_I64, "Not implemented", 0); - BX_CHECK(val->lit.num_bytes == 8, "Not implemented", 0); + CHECK(val->op == DATA_I64, "Not implemented", 0); + CHECK(val->lit.num_bytes == 8, "Not implemented", 0); write_u8 (LE, 0xbf, begin + 5, end); // mov edi write_u32(LE, *val->lit.as_u32, begin + 6, end); // <- literal @@ -1910,12 +1933,12 @@ void x86_64_emit_node( codegen->offset_code += 12; } else { - BX_FAIL("Not implemented",); + FAIL("Not implemented",); } } break; default: - BX_FAIL("Unknown operation",); + FAIL("Unknown operation",); } } @@ -1926,10 +1949,10 @@ void emit_proc( u16 arch, u32 context ) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", 0); - BX_CHECK(proc != UNDEFINED && pool->entities[proc].is_enabled, "No proc", 0); - BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity", 0); - BX_CHECK(arch == ARCH_X86_64, "Target not supported", 0); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", 0); + CHECK(proc != UNDEFINED && pool->entities[proc].is_enabled, "No proc", 0); + CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity", 0); + CHECK(arch == ARCH_X86_64, "Target not supported", 0); Proc *p = &pool->entities[proc].proc; @@ -1943,9 +1966,9 @@ void emit_proc( } void emit_unit(Pool *pool, Codegen_Context *codegen, i64 unit, u16 arch) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", 0); - BX_CHECK(unit != UNDEFINED && pool->entities[unit].is_enabled, "No unit", 0); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity", 0); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", 0); + CHECK(unit != UNDEFINED && pool->entities[unit].is_enabled, "No unit", 0); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity", 0); for (i64 i = 0; i < pool->entities[unit].unit.num_procs; ++i) { u32 context = 0; @@ -1970,9 +1993,9 @@ i64 ar_find_symbol_offset_by_name( c8 *name, c8 *name_end ) { - BX_CHECK(ar_symbol_table != NULL, "Invalid arguments", -1); - BX_CHECK(name != NULL, "Invalid arguments", -1); - BX_CHECK(name_end > name, "Invalid arguments", -1); + CHECK(ar_symbol_table != NULL, "Invalid arguments", -1); + CHECK(name != NULL, "Invalid arguments", -1); + CHECK(name_end > name, "Invalid arguments", -1); i64 num = (i64) read_u32((LE & ~BYTE_ORDER_MASK) | BYTE_BE, ar_symbol_table, ar_end); i64 len = name_end - name; @@ -1981,19 +2004,19 @@ i64 ar_find_symbol_offset_by_name( i64 index = 0; for (; index < num; ++index) { - BX_CHECK(s + len <= (c8 *) ar_end, "Buffer overflow", -1); + CHECK(s + len <= (c8 *) ar_end, "Buffer overflow", -1); - if (s[len] == '\0' && bx_mem_eq(s, name, len)) + if (s[len] == '\0' && mem_eq(s, name, len)) return (i64) read_u32((LE & ~BYTE_ORDER_MASK) | BYTE_BE, ar_symbol_table + 4 * (index + 1), ar_end); while (*s != '\0' && s < (c8 *) ar_end) ++s; - BX_CHECK(s < (c8 *) ar_end, "Buffer overflow", -1); - BX_CHECK(*s == '\0', "Buffer overflow", -1); + CHECK(s < (c8 *) ar_end, "Buffer overflow", -1); + CHECK(*s == '\0', "Buffer overflow", -1); ++s; } - BX_FAIL("Symbol not found", 0); + FAIL("Symbol not found", 0); } Buffer_Context elf_buffer_context( @@ -2018,7 +2041,7 @@ Offset_Num elf_section_headers( u8 *begin = b.begin + b.elf.offset; u8 *end = begin + b.elf.size; - BX_CHECK(end <= b.end, "Buffer overflow", (Offset_Num) {0}); + CHECK(end <= b.end, "Buffer overflow", (Offset_Num) {0}); return (Offset_Num) { .offset = b.elf.offset + read_i64(LE, begin + 40, end), @@ -2039,7 +2062,7 @@ Offset_Size elf_section_names_data( u8 *elf_begin = b.begin + b.elf.offset; u8 *elf_end = elf_begin + b.elf.size; - BX_CHECK(elf_end <= b.end, "Buffer overflow", (Offset_Size) {0}); + CHECK(elf_end <= b.end, "Buffer overflow", (Offset_Size) {0}); i64 string_table_index = (i64) read_u16(LE, elf_begin + 62, elf_end); @@ -2076,7 +2099,7 @@ i64 elf_find_section_index_by_name( c8 * name, i64 name_size ) { - BX_CHECK(name != NULL, "Invalid arguments", 0); + CHECK(name != NULL, "Invalid arguments", 0); if (name_size == 0) return 0; @@ -2090,7 +2113,7 @@ i64 elf_find_section_index_by_name( Offset_Size s = elf_name_in_string_table(b, names, name_index); if (s.size == name_size && - bx_mem_eq(b.begin + s.offset, name, name_size)) + mem_eq(b.begin + s.offset, name, name_size)) return i; } @@ -2104,7 +2127,7 @@ Elf_Section_Header elf_section( Offset_Size names = elf_section_names_data(b); u8 * begin = b.begin + elf_section_header_offset(b, index); u8 * end = b.begin + b.elf.offset + b.elf.size; - BX_CHECK(end <= b.end, "Buffer overflow", (Elf_Section_Header) {0}); + CHECK(end <= b.end, "Buffer overflow", (Elf_Section_Header) {0}); i64 name_index = (i64) read_u32(LE, begin, end); i64 size = read_i64(LE, begin + 32, end); @@ -2114,7 +2137,7 @@ Elf_Section_Header elf_section( u64 flags = read_u64(LE, begin + 8, end); if (type > SEC_SYMTAB_SHNDX || type == 12 || type == 13) { - BX_LOG(WARNING, "Unknown section type: %d", type); + LOG(WARNING, "Unknown section type: %d", type); type = SEC_NONE; } @@ -2153,7 +2176,7 @@ c8 *elf_name_from_offset( c8 *begin = (c8 *) (b.begin + name.offset); i64 len = bx_str_len(begin, (c8 *) b.end); - BX_CHECK((i64) name.size == len, "Buffer overflow", ""); + CHECK((i64) name.size == len, "Buffer overflow", ""); return begin; } @@ -2165,7 +2188,7 @@ i64 elf_find_related_section_index( Elf_Section_Header dst = elf_section(b, section_index - 1); if (src_name.size > dst.name.size && - bx_mem_eq( + mem_eq( elf_name_from_offset(b, src_name) + (src_name.size - dst.name.size), elf_name_from_offset(b, dst.name), dst.name.size)) @@ -2178,16 +2201,16 @@ i64 elf_find_related_section_index( continue; dst = elf_section(b, i); if (src_name.size > dst.name.size && - bx_mem_eq( + mem_eq( elf_name_from_offset(b, src_name) + (src_name.size - dst.name.size), elf_name_from_offset(b, dst.name), dst.name.size)) { - BX_LOG(WARNING, "Unexpected section order"); + LOG(WARNING, "Unexpected section order"); return i; } } - BX_FAIL("Not found", 0); + FAIL("Not found", 0); } Offset_Size elf_find_related_data( @@ -2206,8 +2229,8 @@ Elf_Symbol_Entry elf_symbol( u8 *begin = b.begin + symbol_table.offset + symbol_index * ELF_SYMBOL_ENTRY_SIZE; u8 *end = b.begin + symbol_table.offset + symbol_table.size; - BX_CHECK(end <= b.end, "Buffer overflow", (Elf_Symbol_Entry) {0}); - BX_CHECK(end <= b.begin + b.elf.offset + b.elf.size, "Buffer overflow", (Elf_Symbol_Entry) {0}); + CHECK(end <= b.end, "Buffer overflow", (Elf_Symbol_Entry) {0}); + CHECK(end <= b.begin + b.elf.offset + b.elf.size, "Buffer overflow", (Elf_Symbol_Entry) {0}); i64 sym_name = (i64) read_u32(LE, begin, end); u8 sym_info = read_u8 (LE, begin + 4, end); @@ -2250,9 +2273,9 @@ Elf_Relx_Entry elf_relx( u8 *begin = b.begin + relocations.offset + relx_index * (is_rela ? ELF_RELA_ENTRY_SIZE : ELF_REL_ENTRY_SIZE); u8 *end = begin + ELF_RELA_ENTRY_SIZE; - BX_CHECK(end <= b.end, "Buffer overflow", (Elf_Relx_Entry) {0}); - BX_CHECK(end <= b.begin + b.elf.offset + b.elf.size, "Buffer overflow", (Elf_Relx_Entry) {0}); - BX_CHECK(end <= b.begin + relocations.offset + relocations.size, "Buffer overflow", (Elf_Relx_Entry) {0}); + CHECK(end <= b.end, "Buffer overflow", (Elf_Relx_Entry) {0}); + CHECK(end <= b.begin + b.elf.offset + b.elf.size, "Buffer overflow", (Elf_Relx_Entry) {0}); + CHECK(end <= b.begin + relocations.offset + relocations.size, "Buffer overflow", (Elf_Relx_Entry) {0}); i64 relx_offset = read_i64(LE, begin, end); u32 relx_type = read_u32(LE, begin + 8, end); @@ -2279,14 +2302,14 @@ Elf_Symbol_Entry elf_find_symbol_by_name( for (i64 i = 0; i < symbol_table.num_entries; ++i) { Elf_Symbol_Entry sym = elf_symbol(b, symbol_table.data, string_table, i); - BX_CHECK(b.begin + sym.name.offset + name_size <= b.end, "Buffer overflow", (Elf_Symbol_Entry) {0}); - BX_CHECK(sym.name.offset + name_size <= b.elf.size, "Buffer overflow", (Elf_Symbol_Entry) {0}); + CHECK(b.begin + sym.name.offset + name_size <= b.end, "Buffer overflow", (Elf_Symbol_Entry) {0}); + CHECK(sym.name.offset + name_size <= b.elf.size, "Buffer overflow", (Elf_Symbol_Entry) {0}); - if (name_size == sym.name.size && bx_mem_eq(name, b.begin + sym.name.offset, name_size)) + if (name_size == sym.name.size && mem_eq(name, b.begin + sym.name.offset, name_size)) return sym; } - BX_FAIL("Not found", (Elf_Symbol_Entry) {0}); + FAIL("Not found", (Elf_Symbol_Entry) {0}); } void elf_checks(Buffer_Context b) { @@ -2295,27 +2318,27 @@ void elf_checks(Buffer_Context b) { u8 osabi = read_u8(LE, begin + 7, end); - BX_CHECK( read_u8 (LE, begin, end) == ELF_MAGIC[0], "Invalid ELF file",); - BX_CHECK( read_u8 (LE, begin + 1, end) == ELF_MAGIC[1], "Invalid ELF file",); - BX_CHECK( read_u8 (LE, begin + 2, end) == ELF_MAGIC[2], "Invalid ELF file",); - BX_CHECK( read_u8 (LE, begin + 3, end) == ELF_MAGIC[3], "Invalid ELF file",); - - BX_CHECK( read_u8 (LE, begin + 4, end) == ELF_64, "Unsupported ELF file",); - BX_CHECK( read_u8 (LE, begin + 5, end) == ELF_2_LE, "Unsupported ELF file",); - BX_CHECK( read_u8 (LE, begin + 6, end) == ELF_VERSION, "Unsupported ELF file",); - BX_CHECK( osabi == ELF_SYS_V || osabi == ELF_LINUX, "Unsupported ELF file",); - BX_CHECK( read_u8 (LE, begin + 8, end) == ELF_ABI_VERSION, "Unsupported ELF file",); - BX_CHECK( read_u16(LE, begin + 16, end) == ELF_RELOCATABLE, "Unsupported ELF file",); - BX_CHECK( read_u16(LE, begin + 18, end) == ELF_X86_64, "Unsupported ELF file",); - BX_CHECK( read_u32(LE, begin + 20, end) == ELF_VERSION, "Unsupported ELF file",); - - BX_LAX( read_u64(LE, begin + 24, end) == 0, "Invalid entry point"); - BX_LAX( read_u64(LE, begin + 32, end) == 0, "Invalid program header offset"); - BX_LAX( read_u32(LE, begin + 48, end) == 0, "Invalid flags"); - BX_LAX( read_u16(LE, begin + 52, end) == ELF_HEADER_SIZE, "Invalid ELF header size"); - BX_LAX( read_u16(LE, begin + 54, end) == 0, "Invalid program header size"); - BX_LAX( read_u16(LE, begin + 56, end) == 0, "Invalid num program headers"); - BX_LAX( read_u16(LE, begin + 58, end) == ELF_SECTION_HEADER_SIZE, "Invalid section header size"); + CHECK( read_u8 (LE, begin, end) == ELF_MAGIC[0], "Invalid ELF file",); + CHECK( read_u8 (LE, begin + 1, end) == ELF_MAGIC[1], "Invalid ELF file",); + CHECK( read_u8 (LE, begin + 2, end) == ELF_MAGIC[2], "Invalid ELF file",); + CHECK( read_u8 (LE, begin + 3, end) == ELF_MAGIC[3], "Invalid ELF file",); + + CHECK( read_u8 (LE, begin + 4, end) == ELF_64, "Unsupported ELF file",); + CHECK( read_u8 (LE, begin + 5, end) == ELF_2_LE, "Unsupported ELF file",); + CHECK( read_u8 (LE, begin + 6, end) == ELF_VERSION, "Unsupported ELF file",); + CHECK( osabi == ELF_SYS_V || osabi == ELF_LINUX, "Unsupported ELF file",); + CHECK( read_u8 (LE, begin + 8, end) == ELF_ABI_VERSION, "Unsupported ELF file",); + CHECK( read_u16(LE, begin + 16, end) == ELF_RELOCATABLE, "Unsupported ELF file",); + CHECK( read_u16(LE, begin + 18, end) == ELF_X86_64, "Unsupported ELF file",); + CHECK( read_u32(LE, begin + 20, end) == ELF_VERSION, "Unsupported ELF file",); + + LAX( read_u64(LE, begin + 24, end) == 0, "Invalid entry point"); + LAX( read_u64(LE, begin + 32, end) == 0, "Invalid program header offset"); + LAX( read_u32(LE, begin + 48, end) == 0, "Invalid flags"); + LAX( read_u16(LE, begin + 52, end) == ELF_HEADER_SIZE, "Invalid ELF header size"); + LAX( read_u16(LE, begin + 54, end) == 0, "Invalid program header size"); + LAX( read_u16(LE, begin + 56, end) == 0, "Invalid num program headers"); + LAX( read_u16(LE, begin + 58, end) == ELF_SECTION_HEADER_SIZE, "Invalid section header size"); } void elf_dump(u32 log_level, Buffer_Context b, b8 term_color) { @@ -2328,7 +2351,7 @@ void elf_dump(u32 log_level, Buffer_Context b, b8 term_color) { c8 *name = elf_name_from_offset(b, section.name); - BX_LOG( + LOG( log_level, "\"%s%s%s\"%*s%-14s%s%s%s%s%lld%s", !term_color ? "" : @@ -2353,7 +2376,7 @@ void elf_dump(u32 log_level, Buffer_Context b, b8 term_color) { switch (section.type) { case SEC_SYMTAB: - BX_LOG(log_level, " - -"); + LOG(log_level, " - -"); for (i64 sym_index = 1; sym_index < section.num_entries; ++sym_index) { Elf_Symbol_Entry sym = elf_symbol(b, section.data, strtab, (u16) sym_index); @@ -2362,7 +2385,7 @@ void elf_dump(u32 log_level, Buffer_Context b, b8 term_color) { i32 len = (sym.name.size == 0) ? 4 : (i32) sym.name.size; - BX_LOG( + LOG( log_level, " %08llx %-04llx %s%s%s%s%s %.*s %s%-7s %s%s", section.data.offset + sym.value.offset, @@ -2394,17 +2417,17 @@ void elf_dump(u32 log_level, Buffer_Context b, b8 term_color) { ); } - BX_LOG(log_level, " - -"); + LOG(log_level, " - -"); break; case SEC_REL: case SEC_RELA: { - BX_LOG(log_level, " - -"); + LOG(log_level, " - -"); for (i64 relx_index = 0; relx_index < section.num_entries; ++relx_index) { Elf_Relx_Entry relx = elf_relx(b, symtab, strtab, section.data, relx_index, section.type == SEC_RELA); - BX_LOG( + LOG( log_level, " %-16s %08llx %-+5lld <= %s%08llx%s%s%s \"%s\"", REL_NAMES[relx.type], @@ -2427,14 +2450,14 @@ void elf_dump(u32 log_level, Buffer_Context b, b8 term_color) { ); } - BX_LOG(log_level, " - -"); + LOG(log_level, " - -"); } break; default:; } } - BX_LOG(log_level, ""); + LOG(log_level, ""); } i64 unit_write_in_memory( @@ -2445,14 +2468,14 @@ i64 unit_write_in_memory( u16 format, u16 arch ) { - BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); - BX_CHECK(unit != UNDEFINED && pool->entities[unit].is_enabled, "No unit",); - BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity", 0); - BX_CHECK(pool->entities[unit].unit.entry_point_index != UNDEFINED, "No entry point",); - BX_CHECK(format == FORMAT_ELF && arch == ARCH_X86_64, "Target not supported",); - BX_CHECK(linker->obj_file_buffer != NULL, "No object file buffer",); - BX_CHECK(linker->dependencies_buffer != NULL, "No dependencies buffer",); - BX_CHECK(linker->obj_file_offsets != NULL, "No object file offsets buffer",); + CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",); + CHECK(unit != UNDEFINED && pool->entities[unit].is_enabled, "No unit",); + CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity", 0); + CHECK(pool->entities[unit].unit.entry_point_index != UNDEFINED, "No entry point",); + CHECK(format == FORMAT_ELF && arch == ARCH_X86_64, "Target not supported",); + CHECK(linker->obj_file_buffer != NULL, "No object file buffer",); + CHECK(linker->dependencies_buffer != NULL, "No dependencies buffer",); + CHECK(linker->obj_file_offsets != NULL, "No object file offsets buffer",); emit_unit(pool, codegen, unit, arch); @@ -2463,7 +2486,7 @@ i64 unit_write_in_memory( i64 rotext_address = base_address + program_offset; i64 entry = rotext_address + codegen->entry_point; - BX_LOG(VERBOSE, "Entry point: 0x%llx (%lld)", entry, entry); + LOG(VERBOSE, "Entry point: 0x%llx (%lld)", entry, entry); i64 rotext_size = codegen->offset_code; i64 rwzval_size = 0; @@ -2487,7 +2510,7 @@ i64 unit_write_in_memory( Offset_Num headers = elf_section_headers(buf); for (i64 sec_index = 1; sec_index < headers.num; ++sec_index, ++num_sections_total) { - BX_CHECK(num_sections_total < linker->max_num_sections, "Too many sections",); + CHECK(num_sections_total < linker->max_num_sections, "Too many sections",); Elf_Section_Header section = elf_section(buf, sec_index); @@ -2510,7 +2533,7 @@ i64 unit_write_in_memory( linker->section_addresses[num_sections_total] = rodata_size; rodata_size += bx_align(section.data.size, X86_64_ALIGNMENT); } else { - BX_LAX(0, "Unsupported section type"); + LAX(0, "Unsupported section type"); continue; } } @@ -2534,7 +2557,7 @@ i64 unit_write_in_memory( Offset_Num headers = elf_section_headers(buf); for (i64 sec_index = 1; sec_index < headers.num; ++sec_index, ++sec_index_global) { - BX_CHECK(sec_index_global < num_sections_total, "Buffer overflow",); + CHECK(sec_index_global < num_sections_total, "Buffer overflow",); Elf_Section_Header section = elf_section(buf, sec_index); @@ -2585,12 +2608,12 @@ i64 unit_write_in_memory( if (sym.section != 65521 && elf_section(buf, sym.section).alloc) { sym_section = sec_index_global + sym.section - 1; - BX_CHECK(sym_section < num_sections_total, "Buffer overflow",); - BX_CHECK(linker->section_addresses[sym_section] != 0, "Sanity",); + CHECK(sym_section < num_sections_total, "Buffer overflow",); + CHECK(linker->section_addresses[sym_section] != 0, "Sanity",); sym_address = linker->section_addresses[sym_section] + sym.value.offset; } - BX_CHECK(num_symbols < linker->max_num_symbols, "Too many symbols",); + CHECK(num_symbols < linker->max_num_symbols, "Too many symbols",); linker->symbols[num_symbols++] = (Symbol_Entry) { .name_size = sym.name.size, @@ -2676,7 +2699,7 @@ i64 unit_write_in_memory( i64 dst_index = elf_find_related_section_index(buf, sec_index); i64 dst_index_global = sec_index_global + dst_index - 1; - BX_CHECK(dst_index_global >= 0 && dst_index_global < linker->max_num_sections, "Buffer overflow",); + CHECK(dst_index_global >= 0 && dst_index_global < linker->max_num_sections, "Buffer overflow",); for (i64 entry_index = 0; entry_index < src_sec.num_entries; ++entry_index) { Elf_Relx_Entry relx = elf_relx(buf, symtab, strtab, src_sec.data, entry_index, src_sec.type == SEC_RELA); @@ -2688,7 +2711,7 @@ i64 unit_write_in_memory( if (relx.symbol.section == 0) { for (i64 i = 0; i < num_symbols; ++i) if (linker->symbols[i].name_size == relx.symbol.name.size && - bx_mem_eq( + mem_eq( linker->symbols[i].name, sym_name, relx.symbol.name.size @@ -2704,9 +2727,9 @@ i64 unit_write_in_memory( sym_name + relx.symbol.name.size ) == NULL) { // FIXME - // BX_LOG(WARNING, "Undefined symbol: %s", sym_name); - BX_CHECK(not_found_size + relx.symbol.name.size + 1 <= linker->max_not_found_size, "Out of memory",); - bx_mem_cpy(linker->not_found_buffer + not_found_size, sym_name, relx.symbol.name.size); + // LOG(WARNING, "Undefined symbol: %s", sym_name); + CHECK(not_found_size + relx.symbol.name.size + 1 <= linker->max_not_found_size, "Out of memory",); + mem_cpy(linker->not_found_buffer + not_found_size, sym_name, relx.symbol.name.size); not_found_size += relx.symbol.name.size + 1; } } else { @@ -2753,7 +2776,7 @@ i64 unit_write_in_memory( write_i##BITS(LE, (i##BITS) x_, dst, buf.end); \ } while (0) - #define TODO_ BX_FAIL("Not implemented", 0) + #define TODO_ FAIL("Not implemented", 0) case R_X86_64_NONE: /* Do nothing */ break; case R_X86_64_64: ADD_(64, S + A); break; // 64, S + A @@ -2797,7 +2820,7 @@ i64 unit_write_in_memory( case R_X86_64_GOTPCRELX: TODO_; break; case R_X86_64_REX_GOTPCRELX: ADD_(32, GOT - P + G - 4); break; // 32, GOT - P + G - 4 - default: BX_FAIL("Unknown relocation type", 0); + default: FAIL("Unknown relocation type", 0); #undef ADD_ #undef TODO_ @@ -2820,36 +2843,36 @@ i64 unit_write_in_memory( switch (rel.type) { case REL_ADD_INSTRUCTION_ADDRESS: { - BX_CHECK(rel.size == 8, "Not implemented", 0); + CHECK(rel.size == 8, "Not implemented", 0); i64 value = rel.value + rotext_address + rel.offset; write_i64(LE, value, begin, end); } break; case REL_ADD_RODATA_ADDRESS: { - BX_CHECK(rel.size == 8, "Not implemented", 0); + CHECK(rel.size == 8, "Not implemented", 0); i64 value = rel.value + rodata_address; write_i64(LE, value, begin, end); } break; case REL_ADD_PROC_ADDRESS: { - BX_CHECK(rel.size == 8, "Not implemented", 0); - BX_CHECK(rel.name_size > 0 && rel.name != NULL, "No proc name", 0); + CHECK(rel.size == 8, "Not implemented", 0); + CHECK(rel.name_size > 0 && rel.name != NULL, "No proc name", 0); b8 found = 0; for (i64 i = 0; i < num_symbols; ++i) if (linker->symbols[i].address != 0 && linker->symbols[i].name_size == rel.name_size && - bx_mem_eq(linker->symbols[i].name, rel.name, rel.name_size)) { + mem_eq(linker->symbols[i].name, rel.name, rel.name_size)) { i64 value = rel.value + linker->symbols[i].address; write_i64(LE, value, begin, end); found = 1; - BX_LOG(VERBOSE, "Found %.*s: 0x%llx", rel.name_size, rel.name, value); + LOG(VERBOSE, "Found %.*s: 0x%llx", rel.name_size, rel.name, value); break; } if (!found) { - BX_LOG(ERROR, "Undefined symbol: %.*s", rel.name_size, rel.name); - BX_FAIL("Link failed", 0); + LOG(ERROR, "Undefined symbol: %.*s", rel.name_size, rel.name); + FAIL("Link failed", 0); } } break; } @@ -2862,18 +2885,18 @@ i64 unit_write_in_memory( i64 output_size = bx_align(program_offset + rotext_size + rwzval_size + rwdata_size + rodata_size, X86_64_ALIGNMENT); - BX_CHECK(output_size <= linker->max_output_size, "Out of memory",); + CHECK(output_size <= linker->max_output_size, "Out of memory",); - BX_LOG(VERBOSE, "Total %lld sections", num_sections_total); - BX_LOG(VERBOSE, "Total %lld symbols", num_symbols); + LOG(VERBOSE, "Total %lld sections", num_sections_total); + LOG(VERBOSE, "Total %lld symbols", num_symbols); - BX_LOG(VERBOSE, "Total size"); - BX_LOG(VERBOSE, ".rotext - %7lld bytes", rotext_size); - BX_LOG(VERBOSE, ".rwzval - %7lld bytes", rwzval_size); - BX_LOG(VERBOSE, ".rwdata - %7lld bytes", rwdata_size); - BX_LOG(VERBOSE, ".rodata - %7lld bytes", rodata_size); + LOG(VERBOSE, "Total size"); + LOG(VERBOSE, ".rotext - %7lld bytes", rotext_size); + LOG(VERBOSE, ".rwzval - %7lld bytes", rwzval_size); + LOG(VERBOSE, ".rwdata - %7lld bytes", rwdata_size); + LOG(VERBOSE, ".rodata - %7lld bytes", rodata_size); - BX_LOG(VERBOSE, "Writing ELF x86_64 executable"); + LOG(VERBOSE, "Writing ELF x86_64 executable"); u8 *o = linker->output_buffer; u8 *o_end = o + linker->max_output_size; @@ -2881,7 +2904,7 @@ i64 unit_write_in_memory( // ELF header // - bx_mem_cpy(o, ELF_MAGIC, 4); + mem_cpy(o, ELF_MAGIC, 4); write_u8 (LE, ELF_64, o + 4, o_end); write_u8 (LE, ELF_2_LE, o + 5, o_end); @@ -2907,9 +2930,9 @@ i64 unit_write_in_memory( // Program headers // - BX_CHECK(rotext_offset % X86_64_PAGE_SIZE == rotext_address % X86_64_PAGE_SIZE, "Invalid alignment",); - BX_CHECK(rwdata_offset % X86_64_PAGE_SIZE == rwdata_address % X86_64_PAGE_SIZE, "Invalid alignment",); - BX_CHECK(rodata_offset % X86_64_PAGE_SIZE == rodata_address % X86_64_PAGE_SIZE, "Invalid alignment",); + CHECK(rotext_offset % X86_64_PAGE_SIZE == rotext_address % X86_64_PAGE_SIZE, "Invalid alignment",); + CHECK(rwdata_offset % X86_64_PAGE_SIZE == rwdata_address % X86_64_PAGE_SIZE, "Invalid alignment",); + CHECK(rodata_offset % X86_64_PAGE_SIZE == rodata_address % X86_64_PAGE_SIZE, "Invalid alignment",); // .rotext write_u32(LE, 1, o + 64, o_end); // type (PT_LOAD) @@ -2951,13 +2974,13 @@ i64 unit_write_in_memory( write_i64(LE, rodata_size, o + 272, o_end); // size in memory write_i64(LE, X86_64_ALIGNMENT, o + 280, o_end); - BX_CHECK(rotext_offset >= 288, "Sanity",); + CHECK(rotext_offset >= 288, "Sanity",); // Code // - bx_mem_cpy(o + rotext_offset, codegen->buffer_code, codegen->offset_code); - bx_mem_cpy(o + rodata_offset, codegen->buffer_rodata, codegen->offset_rodata); + mem_cpy(o + rotext_offset, codegen->buffer_code, codegen->offset_code); + mem_cpy(o + rodata_offset, codegen->buffer_rodata, codegen->offset_rodata); // ============================================================== // @@ -2975,9 +2998,9 @@ i64 unit_write_in_memory( section.data.size == 0) continue; u8 *p = o + offset; - BX_CHECK(p >= o + program_offset + codegen->offset_code, "Buffer overflow",); - BX_CHECK(p + section.data.size <= o + output_size, "Buffer overflow",); - bx_mem_cpy(p, buf.begin + section.data.offset, section.data.size); + CHECK(p >= o + program_offset + codegen->offset_code, "Buffer overflow",); + CHECK(p + section.data.size <= o + output_size, "Buffer overflow",); + mem_cpy(p, buf.begin + section.data.offset, section.data.size); } } @@ -3009,20 +3032,20 @@ void unit_write( if (id == UNDEFINED) continue; Unit *l = &pool->entities[id].unit; - BX_CHECK(pool->entities[id].is_enabled, "Internal",); - BX_CHECK(l->type == UNIT_LIBRARY_STATIC, "Link type not supported",); - BX_CHECK(l->name_size > 0, "No link name",); - BX_CHECK(l->name_size <= MAX_NAME_SIZE, "Link name too big",); + CHECK(pool->entities[id].is_enabled, "Internal",); + CHECK(l->type == UNIT_LIBRARY_STATIC, "Link type not supported",); + CHECK(l->name_size > 0, "No link name",); + CHECK(l->name_size <= MAX_NAME_SIZE, "Link name too big",); i64 f = io_open_read(l->name_size, l->name, io_user_data); io_seek(f, 0, IO_SEEK_END, io_user_data); i64 in_size = io_tell(f, io_user_data); - BX_CHECK(in_size <= linker->max_obj_file_size, "AR file too big",); + CHECK(in_size <= linker->max_obj_file_size, "AR file too big",); io_seek(f, 0, IO_SEEK_BEGIN, io_user_data); i64 n = io_read(f, in_size, linker->obj_file_buffer, io_user_data); - BX_CHECK(n == in_size, "Read failed",); + CHECK(n == in_size, "Read failed",); io_close(f, io_user_data); @@ -3033,7 +3056,7 @@ void unit_write( u8 *ar_begin = linker->obj_file_buffer; u8 *ar_end = linker->obj_file_buffer + in_size; - BX_CHECK(bx_mem_eq(ar_begin, AR_MAGIC, 8), "Invalid AR file",); + CHECK(mem_eq(ar_begin, AR_MAGIC, 8), "Invalid AR file",); u8 *f_begin = ar_begin + 8; @@ -3047,19 +3070,19 @@ void unit_write( size = bx_align(size, 2); - BX_CHECK(bx_mem_eq(f_end, "\x60\x0a", 2), "Invalid AR file",); - BX_CHECK(f_begin + size <= ar_end, "Buffer overflow",); + CHECK(mem_eq(f_end, "\x60\x0a", 2), "Invalid AR file",); + CHECK(f_begin + size <= ar_end, "Buffer overflow",); - if (!bx_mem_eq(f_id, AR_SYMBOL_TABLE, 16) && - !bx_mem_eq(f_id, AR_STRING_TABLE, 16)) { + if (!mem_eq(f_id, AR_SYMBOL_TABLE, 16) && + !mem_eq(f_id, AR_STRING_TABLE, 16)) { // Read ELF object file i64 delta_size = bx_align(size, X86_64_ALIGNMENT); - BX_CHECK(obj_files_size + delta_size < linker->max_dependencies_size, "Out of memory",); - BX_CHECK(linker->num_obj_files + 1 < linker->max_num_obj_files, "Out of memory",); + CHECK(obj_files_size + delta_size < linker->max_dependencies_size, "Out of memory",); + CHECK(linker->num_obj_files + 1 < linker->max_num_obj_files, "Out of memory",); - bx_mem_cpy(linker->dependencies_buffer + obj_files_size, f_data, size); + mem_cpy(linker->dependencies_buffer + obj_files_size, f_data, size); linker->obj_file_offsets[linker->num_obj_files] = obj_files_size; obj_files_size += delta_size; @@ -3088,58 +3111,58 @@ void unit_write( codegen->entry_point = 0; linker->num_obj_files = 0; - bx_mem_set(codegen->rels, 0, codegen->max_num_rels * sizeof *codegen->rels); - bx_mem_set(codegen->buffer_code, 0, codegen->max_code_size); - bx_mem_set(codegen->buffer_rodata, 0, codegen->max_rodata_size); - bx_mem_set(linker->obj_file_buffer, 0, linker->max_obj_file_size); - bx_mem_set(linker->dependencies_buffer, 0, linker->max_dependencies_size); - bx_mem_set(linker->obj_file_offsets, 0, linker->max_num_obj_files * sizeof *linker->obj_file_offsets); - bx_mem_set(linker->section_offsets, 0, linker->max_num_sections * sizeof *linker->section_offsets); - bx_mem_set(linker->section_addresses, 0, linker->max_num_sections * sizeof *linker->section_addresses); - bx_mem_set(linker->symbols, 0, linker->max_num_symbols * sizeof *linker->symbols); - bx_mem_set(linker->not_found_buffer, 0, linker->max_not_found_size); - bx_mem_set(linker->output_buffer, 0, linker->max_output_size); + mem_set(codegen->rels, 0, codegen->max_num_rels * sizeof *codegen->rels); + mem_set(codegen->buffer_code, 0, codegen->max_code_size); + mem_set(codegen->buffer_rodata, 0, codegen->max_rodata_size); + mem_set(linker->obj_file_buffer, 0, linker->max_obj_file_size); + mem_set(linker->dependencies_buffer, 0, linker->max_dependencies_size); + mem_set(linker->obj_file_offsets, 0, linker->max_num_obj_files * sizeof *linker->obj_file_offsets); + mem_set(linker->section_offsets, 0, linker->max_num_sections * sizeof *linker->section_offsets); + mem_set(linker->section_addresses, 0, linker->max_num_sections * sizeof *linker->section_addresses); + mem_set(linker->symbols, 0, linker->max_num_symbols * sizeof *linker->symbols); + mem_set(linker->not_found_buffer, 0, linker->max_not_found_size); + mem_set(linker->output_buffer, 0, linker->max_output_size); } i64 io_open_read(i64 name_size, c8 *name, void *user_data) { i64 f; - io_dispatch(IO_OPEN_READ, &f, &name_size, name, user_data); + dispatch_io(IO_OPEN_READ, &f, &name_size, name, user_data); return f; } i64 io_open_write(i64 name_size, c8 *name, void *user_data) { i64 f; - io_dispatch(IO_OPEN_WRITE, &f, &name_size, name, user_data); + dispatch_io(IO_OPEN_WRITE, &f, &name_size, name, user_data); return f; } void io_close(i64 f, void *user_data) { - io_dispatch(IO_CLOSE, &f, NULL, NULL, user_data); + dispatch_io(IO_CLOSE, &f, NULL, NULL, user_data); } b8 io_seek(i64 f, i64 offset, u16 origin, void *user_data) { - io_dispatch(IO_SEEK, &f, &offset, &origin, user_data); + dispatch_io(IO_SEEK, &f, &offset, &origin, user_data); return 1; } i64 io_tell(i64 f, void *user_data) { i64 offset; - io_dispatch(IO_TELL, &f, &offset, NULL, user_data); + dispatch_io(IO_TELL, &f, &offset, NULL, user_data); return offset; } i64 io_read(i64 f, i64 size, void *data, void *user_data) { - io_dispatch(IO_READ, &f, &size, data, user_data); + dispatch_io(IO_READ, &f, &size, data, user_data); return size; } i64 io_write(i64 f, i64 size, void *data, void *user_data) { - io_dispatch(IO_WRITE, &f, &size, data, user_data); + dispatch_io(IO_WRITE, &f, &size, data, user_data); return size; } void io_chmod_exe(i64 f, void *user_data) { - io_dispatch(IO_CHMOD_EXE, &f, NULL, NULL, user_data); + dispatch_io(IO_CHMOD_EXE, &f, NULL, NULL, user_data); } // ================================================================ @@ -3165,7 +3188,15 @@ void wait_any_input(void) { fflush(stdin); } -void bx_log(i32 log_level, u32 line, c8 *file, c8 *format, ...) { +void dispatch_assert(b8 condition, c8 *message, u32 line, c8 *file) { + if (condition) + return; + + dispatch_log(ERROR, line, file, message); + exit(-1); +} + +void dispatch_log(i32 log_level, u32 line, c8 *file, c8 *format, ...) { if (file == NULL || format == NULL) return; @@ -3233,19 +3264,11 @@ void bx_log(i32 log_level, u32 line, c8 *file, c8 *format, ...) { } } -void bx_assert(b8 condition, c8 *message, u32 line, c8 *file) { - if (condition) - return; - - bx_log(ERROR, line, file, message); - exit(-1); -} - // IO dispatch procedure // -void io_dispatch(u16 op, i64 *id, i64 *size, void *data, void *user_data) { - BX_CHECK(id != NULL, "Invalid arguments",); +void dispatch_io(u16 op, i64 *id, i64 *size, void *data, void *user_data) { + CHECK(id != NULL, "Invalid arguments",); (void) user_data; @@ -3255,74 +3278,74 @@ void io_dispatch(u16 op, i64 *id, i64 *size, void *data, void *user_data) { switch (op) { case IO_OPEN_READ: case IO_OPEN_WRITE: - BX_CHECK(size != NULL, "Invalid arguments",); - BX_CHECK(*size > 0 && *size < MAX_NAME_SIZE, "Invalid arguments",); - BX_CHECK(data != NULL, "Invalid arguments",); + CHECK(size != NULL, "Invalid arguments",); + CHECK(*size > 0 && *size < MAX_NAME_SIZE, "Invalid arguments",); + CHECK(data != NULL, "Invalid arguments",); - bx_mem_cpy(buf, data, *size); + mem_cpy(buf, data, *size); *f = fopen(buf, op == IO_OPEN_READ ? "rb" : "wb"); - BX_CHECK(*f != NULL, "File open failed",); + CHECK(*f != NULL, "File open failed",); break; case IO_CLOSE: - BX_CHECK(*f != NULL, "Invalid arguments",); - BX_CHECK(size == NULL, "Invalid arguments",); - BX_CHECK(data == NULL, "Invalid arguments",); + CHECK(*f != NULL, "Invalid arguments",); + CHECK(size == NULL, "Invalid arguments",); + CHECK(data == NULL, "Invalid arguments",); fclose(*f); break; case IO_SEEK: { - BX_CHECK(*f != NULL, "Invalid arguments",); - BX_CHECK(size != NULL, "Invalid arguments",); - BX_CHECK(data != NULL, "Invalid arguments",); + CHECK(*f != NULL, "Invalid arguments",); + CHECK(size != NULL, "Invalid arguments",); + CHECK(data != NULL, "Invalid arguments",); u16 *origin = (u16 *) data; if (!(*origin == IO_SEEK_CURSOR && *size == 0)) { - BX_CHECK(*origin == IO_SEEK_CURSOR || + CHECK(*origin == IO_SEEK_CURSOR || *origin == IO_SEEK_BEGIN || *origin == IO_SEEK_END, "Invalid arguments",); i32 s = fseek(*f, *size, *origin == IO_SEEK_CURSOR ? SEEK_CUR : *origin == IO_SEEK_BEGIN ? SEEK_SET : SEEK_END); - BX_CHECK(s == 0, "File seek failed",); + CHECK(s == 0, "File seek failed",); } } break; case IO_TELL: { - BX_CHECK(*f != NULL, "Invalid arguments",); - BX_CHECK(size != NULL, "Invalid arguments",); - BX_CHECK(data == NULL, "Invalid arguments",); + CHECK(*f != NULL, "Invalid arguments",); + CHECK(size != NULL, "Invalid arguments",); + CHECK(data == NULL, "Invalid arguments",); i64 n = (i64) ftell(*f); - BX_CHECK(n >= 0, "File tell failed",); + CHECK(n >= 0, "File tell failed",); *size = n; } break; case IO_READ: - BX_CHECK(*f != NULL, "Invalid arguments",); - BX_CHECK(size != NULL, "Invalid arguments",); - BX_CHECK(data != NULL, "Invalid arguments",); - BX_CHECK(*size > 0, "Invalid arguments",); + CHECK(*f != NULL, "Invalid arguments",); + CHECK(size != NULL, "Invalid arguments",); + CHECK(data != NULL, "Invalid arguments",); + CHECK(*size > 0, "Invalid arguments",); *size = fread(data, 1, *size, *f); break; case IO_WRITE: - BX_CHECK(*f != NULL, "Invalid arguments",); - BX_CHECK(size != NULL, "Invalid arguments",); - BX_CHECK(data != NULL, "Invalid arguments",); - BX_CHECK(*size > 0, "Invalid arguments",); + CHECK(*f != NULL, "Invalid arguments",); + CHECK(size != NULL, "Invalid arguments",); + CHECK(data != NULL, "Invalid arguments",); + CHECK(*size > 0, "Invalid arguments",); *size = fwrite(data, 1, *size, *f); break; case IO_CHMOD_EXE: - BX_CHECK(*f != NULL, "Invalid arguments",); - BX_CHECK(size == NULL, "Invalid arguments",); + CHECK(*f != NULL, "Invalid arguments",); + CHECK(size == NULL, "Invalid arguments",); #ifdef __unix__ fchmod(fileno(*f), 0775); @@ -3330,7 +3353,7 @@ void io_dispatch(u16 op, i64 *id, i64 *size, void *data, void *user_data) { break; default: - BX_FAIL("Invalid arguments",); + FAIL("Invalid arguments",); } } @@ -3491,8 +3514,8 @@ void l_static(i64 unit, c8 *static_library) { c8 *l_find(c8 *name) { // Find the full path to a library - BX_CHECK(name != NULL, "Invalid argument", ""); - BX_CHECK(bx_str_len(name, name + MAX_PATH_SIZE) < MAX_PATH_SIZE, "Invalid argument", ""); + CHECK(name != NULL, "Invalid argument", ""); + CHECK(bx_str_len(name, name + MAX_PATH_SIZE) < MAX_PATH_SIZE, "Invalid argument", ""); static c8 buf[MAX_PATH_SIZE]; // FIXME @@ -3502,7 +3525,7 @@ c8 *l_find(c8 *name) { FILE *f = fopen(buf, "rb"); \ if (f == NULL) break; \ fclose(f); \ - BX_LOG(VERBOSE, "Found library: %s", buf); \ + LOG(VERBOSE, "Found library: %s", buf); \ return buf; \ } while (0) @@ -3511,7 +3534,9 @@ c8 *l_find(c8 *name) { TRY_("/lib/x86_64-linux-gnu/%s"); TRY_("/lib/x86_64-linux-gnu/lib%s.a"); - BX_FAIL("Library not found", ""); + #undef TRY_ + + FAIL("Library not found", ""); } #endif @@ -3528,7 +3553,7 @@ int main(int argc, char **argv) { (void) argc; (void) argv; - BX_LOG(INFO, "bxgen " BX_VERSION); + LOG(INFO, "bxgen " VERSION); // ============================================================ // @@ -3594,17 +3619,17 @@ int main(int argc, char **argv) { // ============================================================ if (HOST != HOST_Linux) { - BX_LOG(INFO, "Skip running the executable. Host system is not compatible."); + LOG(INFO, "Skip running the executable. Host system is not compatible."); } else if (HO != LE) { - BX_LOG(INFO, "Skip running the executable. Host data ordering is not compatible."); + LOG(INFO, "Skip running the executable. Host data ordering is not compatible."); } else { // Run the created executable file. i32 ret = system("./test_foo"); - BX_CHECK(WEXITSTATUS(ret) == 42, "Failure", -1); + CHECK(WEXITSTATUS(ret) == 42, "Failure", -1); } - BX_LOG(INFO, "Bye!"); + LOG(INFO, "Bye!"); return 0; } |