summaryrefslogtreecommitdiff
path: root/bxgen.c
diff options
context:
space:
mode:
Diffstat (limited to 'bxgen.c')
-rwxr-xr-xbxgen.c603
1 files changed, 328 insertions, 275 deletions
diff --git a/bxgen.c b/bxgen.c
index a95573e..2f393f7 100755
--- a/bxgen.c
+++ b/bxgen.c
@@ -146,6 +146,9 @@ enum {
HOST = HOST_Unknown,
#endif
+ // Log level
+ ERROR = 0,
+
// For indices
UNDEFINED = -1,
@@ -401,6 +404,7 @@ i64 io_read(i64 f, i64 size, void *data, void *user_data);
i64 io_write(i64 f, i64 size, void *data, void *user_data);
void io_chmod_exe(i64 f, void *user_data);
+void bx_log(i32 log_level, c8 *message, u32 line, c8 *file);
void bx_assert(b8 condition, c8 *message, u32 line, c8 *file);
void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data);
@@ -444,27 +448,53 @@ void l_static(i64 unit, c8 *static_library);
#define NULL ((void *) 0)
#endif
-#define BX_ASSERT(condition) \
- bx_assert((condition), #condition, __LINE__, __FILE__)
+#ifdef NDEBUG
+# define BX_CHECK(condition, error_string, fail_result) \
+ do { \
+ b8 ok_ = (condition); \
+ if (!ok_) { \
+ bx_log(ERROR, error_string, __LINE__, __FILE__); \
+ return fail_result; \
+ } \
+ } while (0)
+#else
+# define BX_CHECK(condition, error_string, fail_result) \
+ do { \
+ b8 ok_ = (condition); \
+ bx_assert(ok_, error_string, __LINE__, __FILE__); \
+ if (!ok_) \
+ return fail_result; \
+ } while (0)
+#endif
+
+#ifdef NDEBUG
+# define BX_FAIL(error_string, fail_result) \
+ bx_log(ERROR, error_string, __LINE__, __FILE__); \
+ return fail_result
+#else
+# define BX_FAIL(error_string, fail_result) \
+ bx_assert(0, error_string, __LINE__, __FILE__); \
+ return fail_result
+#endif
i64 bx_align(i64 x, i64 a) {
- BX_ASSERT(a > 0);
+ BX_CHECK(a > 0, "Invalid arguments", 0);
return x + ((a - (x % a)) % a);
}
void bx_mem_cpy(void *dst, void *src, i64 size) {
- BX_ASSERT(dst != NULL);
- BX_ASSERT(src != NULL);
- BX_ASSERT(size > 0);
+ BX_CHECK(dst != NULL, "Invalid arguments",);
+ BX_CHECK(src != NULL, "Invalid arguments",);
+ BX_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_ASSERT(a != NULL);
- BX_ASSERT(b != NULL);
- BX_ASSERT(size > 0);
+ BX_CHECK(a != NULL, "Invalid arguments", 0);
+ BX_CHECK(b != NULL, "Invalid arguments", 0);
+ BX_CHECK(size > 0, "Invalid size", 0);
u8 *x = (u8 *) a;
u8 *y = (u8 *) b;
@@ -480,16 +510,16 @@ i64 bx_str_len(c8 *s, c8 *s_max) {
for (i64 len = 0; s + len < s_max; ++len)
if (s[len] == '\0')
return len;
- BX_ASSERT(0);
- return 0;
+
+ BX_FAIL("Buffer overflow", 0);
}
b8 bx_str_eq(
c8 *a, c8 *a_end,
c8 *b, c8 *b_end
) {
- BX_ASSERT(a != NULL && a_end != NULL);
- BX_ASSERT(b != NULL && b_end != NULL);
+ BX_CHECK(a != NULL && a_end != NULL, "Invalid arguments", 0);
+ BX_CHECK(b != NULL && b_end != NULL, "Invalid arguments", 0);
while (*a == *b && a != a_end && b != b_end) {
++a;
@@ -500,8 +530,8 @@ b8 bx_str_eq(
}
c8 *bx_find_char(c8 *s, c8 *s_end, c8 c) {
- BX_ASSERT(s != NULL);
- BX_ASSERT(s_end != NULL);
+ BX_CHECK(s != NULL, "Invalid arguments", NULL);
+ BX_CHECK(s_end != NULL, "Invalid arguments", NULL);
while (s != s_end && *s != c)
++s;
@@ -519,8 +549,8 @@ c8 *bx_find_char(c8 *s, c8 *s_end, c8 c) {
//
i64 pool_add(Pool *pool, Entity data) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entity_count < pool->capacity);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments", UNDEFINED);
+ BX_CHECK(pool->entity_count < pool->capacity, "Out of memory", UNDEFINED);
i64 id = pool->entity_count++;
data.is_enabled = 1,
@@ -530,9 +560,9 @@ i64 pool_add(Pool *pool, Entity data) {
}
void pool_remove(Pool *pool, i64 entity, i16 type) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[entity].is_enabled);
- BX_ASSERT(pool->entities[entity].type == type);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ BX_CHECK(pool->entities[entity].is_enabled, "Entity already removed",);
+ BX_CHECK(pool->entities[entity].type == type, "Invalid entity type",);
pool->entities[entity].is_enabled = 1;
}
@@ -558,7 +588,7 @@ i64 node_data_i64(Pool *pool, i64 value) {
}
i64 node_ctrl_call(Pool *pool, i16 convention, i64 target_proc, i64 arg_count, Var *args) {
- BX_ASSERT(arg_count <= MAX_ARG_COUNT);
+ BX_CHECK(arg_count <= MAX_ARG_COUNT, "Array too big", UNDEFINED);
Call call = {
.convention = convention,
@@ -576,7 +606,7 @@ i64 node_ctrl_call(Pool *pool, i16 convention, i64 target_proc, i64 arg_count, V
}
i64 node_ctrl_call_by_name(Pool *pool, i16 convention, i64 name_size, c8 *name, i64 arg_count, Var *args) {
- BX_ASSERT(arg_count <= MAX_ARG_COUNT);
+ BX_CHECK(arg_count <= MAX_ARG_COUNT, "Array too big", UNDEFINED);
Call call = {
.convention = convention,
@@ -596,7 +626,7 @@ i64 node_ctrl_call_by_name(Pool *pool, i16 convention, i64 name_size, c8 *name,
}
i64 node_ctrl_ret(Pool *pool, i64 value_count, Var *values) {
- BX_ASSERT(value_count <= MAX_ARG_COUNT);
+ BX_CHECK(value_count <= MAX_ARG_COUNT, "Array too big", UNDEFINED);
Ret ret = { .val_count = value_count, };
@@ -624,20 +654,20 @@ void proc_destroy(Pool *pool, i64 proc) {
}
void proc_set_convention(Pool *pool, i64 proc, i16 convention) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[proc].is_enabled);
- BX_ASSERT(pool->entities[proc].type == ENTITY_PROC);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",);
+ BX_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_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[proc].is_enabled);
- BX_ASSERT(pool->entities[proc].type == ENTITY_PROC);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ BX_CHECK(pool->entities[proc].is_enabled, "Entity does not exist",);
+ BX_CHECK(pool->entities[proc].type == ENTITY_PROC, "Invalid entity type",);
- BX_ASSERT(name_size <= MAX_NAME_SIZE);
- BX_ASSERT(name_size >= 0);
+ BX_CHECK(name_size <= MAX_NAME_SIZE, "Name too big",);
+ BX_CHECK(name_size >= 0, "Invalid arguments",);
Proc *p = &pool->entities[proc].proc;
p->name_size = name_size;
@@ -647,16 +677,16 @@ void proc_set_name(Pool *pool, i64 proc, i64 name_size, c8 *name) {
}
void proc_node_add(Pool *pool, i64 proc, i64 node) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[proc].is_enabled);
- BX_ASSERT(pool->entities[proc].type == ENTITY_PROC);
- BX_ASSERT(pool->entities[node].is_enabled);
- BX_ASSERT(pool->entities[node].type == ENTITY_NODE);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ 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",);
Proc *p = &pool->entities[proc].proc;
Node *n = &pool->entities[node].node;
- BX_ASSERT(n->index_in_proc == UNDEFINED);
+ BX_CHECK(n->index_in_proc == UNDEFINED, "Internal",);
i64 index = p->node_count;
@@ -665,11 +695,11 @@ void proc_node_add(Pool *pool, i64 proc, i64 node) {
// Only one return node is allowed.
//
- BX_ASSERT(p->ret_index == UNDEFINED);
+ BX_CHECK(p->ret_index == UNDEFINED, "Internal",);
p->ret_index = index;
}
- BX_ASSERT(index < MAX_NODE_COUNT);
+ BX_CHECK(index < MAX_NODE_COUNT, "Out of memory",);
n->index_in_proc = index;
p->nodes[index] = node;
@@ -677,19 +707,19 @@ void proc_node_add(Pool *pool, i64 proc, i64 node) {
}
void proc_node_remove(Pool *pool, i64 proc, i64 node) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[proc].is_enabled);
- BX_ASSERT(pool->entities[proc].type == ENTITY_PROC);
- BX_ASSERT(pool->entities[node].type == ENTITY_NODE);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ 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",);
Proc *p = &pool->entities[proc].proc;
Node *n = &pool->entities[node].node;
- BX_ASSERT(n->index_in_proc != UNDEFINED);
- BX_ASSERT(p->nodes[n->index_in_proc] == node);
+ BX_CHECK(n->index_in_proc != UNDEFINED, "Internal",);
+ BX_CHECK(p->nodes[n->index_in_proc] == node, "Internal",);
if (n->op == CTRL_RET) {
- BX_ASSERT(p->ret_index != UNDEFINED);
+ BX_CHECK(p->ret_index != UNDEFINED, "Internal",);
p->ret_index = UNDEFINED;
}
@@ -712,20 +742,20 @@ void unit_destroy(Pool *pool, i64 unit) {
}
void unit_proc_add(Pool *pool, i64 unit, i64 proc) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[unit].is_enabled);
- BX_ASSERT(pool->entities[unit].type == ENTITY_UNIT);
- BX_ASSERT(pool->entities[proc].is_enabled);
- BX_ASSERT(pool->entities[proc].type == ENTITY_PROC);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ 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",);
Unit *u = &pool->entities[unit].unit;
Proc *p = &pool->entities[proc].proc;
- BX_ASSERT(p->index_in_unit == UNDEFINED);
+ BX_CHECK(p->index_in_unit == UNDEFINED, "Internal",);
i64 index = u->proc_count;
- BX_ASSERT(index < MAX_PROC_COUNT);
+ BX_CHECK(index < MAX_PROC_COUNT, "Out of memory",);
p->index_in_unit = index;
u->procs[index] = proc;
@@ -733,16 +763,16 @@ void unit_proc_add(Pool *pool, i64 unit, i64 proc) {
}
void unit_proc_remove(Pool *pool, i64 unit, i64 proc) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[unit].is_enabled);
- BX_ASSERT(pool->entities[unit].type == ENTITY_UNIT);
- BX_ASSERT(pool->entities[proc].type == ENTITY_PROC);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ 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",);
Unit *u = &pool->entities[unit].unit;
Proc *p = &pool->entities[proc].proc;
- BX_ASSERT(p->index_in_unit != UNDEFINED);
- BX_ASSERT(u->procs[p->index_in_unit] == proc);
+ BX_CHECK(p->index_in_unit != UNDEFINED, "Internal",);
+ BX_CHECK(u->procs[p->index_in_unit] == proc, "Internal",);
if (u->entry_point_index == p->index_in_unit)
u->entry_point_index = UNDEFINED;
@@ -752,11 +782,11 @@ void unit_proc_remove(Pool *pool, i64 unit, i64 proc) {
}
void unit_link_add(Pool *pool, i64 unit, i64 link_unit) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[unit].is_enabled);
- BX_ASSERT(pool->entities[unit].type == ENTITY_UNIT);
- BX_ASSERT(pool->entities[link_unit].is_enabled);
- BX_ASSERT(pool->entities[link_unit].type == ENTITY_UNIT);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ 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",);
Unit *u = &pool->entities[unit].unit;
@@ -764,15 +794,15 @@ void unit_link_add(Pool *pool, i64 unit, i64 link_unit) {
if (u->links[i] == link_unit)
return;
- BX_ASSERT(u->link_count < MAX_LINK_COUNT);
+ BX_CHECK(u->link_count < MAX_LINK_COUNT, "Internal",);
u->links[u->link_count++] = link_unit;
}
void unit_link_remove(Pool *pool, i64 unit, i64 link_unit) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[unit].is_enabled);
- BX_ASSERT(pool->entities[unit].type == ENTITY_UNIT);
- BX_ASSERT(pool->entities[link_unit].type == ENTITY_UNIT);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ 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",);
Unit *u = &pool->entities[unit].unit;
@@ -782,18 +812,18 @@ void unit_link_remove(Pool *pool, i64 unit, i64 link_unit) {
return;
}
- BX_ASSERT(0);
+ BX_FAIL("Link not found",);
}
void unit_set_name(Pool *pool, i64 unit, i64 name_size, c8 *name) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[unit].is_enabled);
- BX_ASSERT(pool->entities[unit].type == ENTITY_UNIT);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",);
+ BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid entity type",);
- BX_ASSERT(name_size <= MAX_NAME_SIZE);
- BX_ASSERT(name_size >= 0);
+ BX_CHECK(name_size <= MAX_NAME_SIZE, "Name too big",);
+ BX_CHECK(name_size >= 0, "Invalid arguments",);
- Unit *u = &pool->entities[unit].unit;
+ Unit *u = &pool->entities[unit].unit;
u->name_size = name_size;
if (name_size > 0)
@@ -801,9 +831,9 @@ void unit_set_name(Pool *pool, i64 unit, i64 name_size, c8 *name) {
}
void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[unit].is_enabled);
- BX_ASSERT(pool->entities[unit].type == ENTITY_UNIT);
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",);
+ BX_CHECK(pool->entities[unit].type == ENTITY_UNIT, "Invalid unit type",);
Unit *u = &pool->entities[unit].unit;
@@ -812,13 +842,13 @@ void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) {
return;
}
- BX_ASSERT(pool->entities[entry_point_proc].is_enabled);
- BX_ASSERT(pool->entities[entry_point_proc].type == ENTITY_PROC);
+ BX_CHECK(pool->entities[entry_point_proc].is_enabled, "Internal",);
+ BX_CHECK(pool->entities[entry_point_proc].type == ENTITY_PROC, "Internal",);
Proc *p = &pool->entities[entry_point_proc].proc;
- BX_ASSERT(p->index_in_unit != UNDEFINED);
- BX_ASSERT(u->procs[p->index_in_unit] == entry_point_proc);
+ BX_CHECK(p->index_in_unit != UNDEFINED, "Internal",);
+ BX_CHECK(u->procs[p->index_in_unit] == entry_point_proc, "Internal",);
pool->entities[unit].unit.entry_point_index = p->index_in_unit;
}
@@ -851,8 +881,7 @@ i32 host_f64_dword_order() {
if ((*(u64 *) &(f64) { -1.4575323640233e-306 } & 0xffffffff00000000ull) == 0x40301fcb00000000ull)
return host_byte_order() == LE ? BE : LE;
- BX_ASSERT(0 && "Unknown native floating-point number format");
- return 0;
+ BX_FAIL("Unknown native floating-point number format", 0);
}
u8 read_u8(i32 bit_order, u8 *v) {
@@ -871,59 +900,77 @@ u8 read_u8(i32 bit_order, u8 *v) {
}
u16 read_u16(i32 bit_order, i32 byte_order, u8 *v, u8 *v_end) {
- BX_ASSERT(v != NULL);
- BX_ASSERT(v + 2 <= v_end);
-
- if (byte_order == host_byte_order())
- return ((u16) read_u8(bit_order, v)) |
- (((u16) read_u8(bit_order, v + 1)) << 8);
+ BX_CHECK(v != NULL, "Invalid arguments", 0);
+ BX_CHECK(v + 2 <= v_end, "Invalid arguments", 0);
- return ((u16) read_u8(bit_order, v + 1)) |
- (((u16) read_u8(bit_order, v)) << 8);
-}
+ u16 x;
-u32 read_u32(i32 bit_order, i32 byte_order, u8 *v, u8 *v_end) {
- BX_ASSERT(v != NULL);
- BX_ASSERT(v + 4 <= v_end);
+ if (bit_order == host_bit_order() && byte_order == host_byte_order())
+ bx_mem_cpy(&x, v, 2);
+ else if (byte_order == host_byte_order())
+ x = ((u16) read_u8(bit_order, v)) |
+ (((u16) read_u8(bit_order, v + 1)) << 8);
+ else
+ x = ((u16) read_u8(bit_order, v + 1)) |
+ (((u16) read_u8(bit_order, v)) << 8);
- if (byte_order == host_byte_order())
- return ((u32) read_u8(bit_order, v)) |
- (((u32) read_u8(bit_order, v + 1)) << 8) |
- (((u32) read_u8(bit_order, v + 2)) << 16) |
- (((u32) read_u8(bit_order, v + 3)) << 24);
+ return x;
+}
- return ((u32) read_u8(bit_order, v + 3)) |
+u32 read_u32(i32 bit_order, i32 byte_order, u8 *v, u8 *v_end) {
+ BX_CHECK(v != NULL, "Invalid arguments", 0);
+ BX_CHECK(v + 4 <= v_end, "Invalid arguments", 0);
+
+ u32 x;
+
+ if (bit_order == host_bit_order() && byte_order == host_byte_order())
+ bx_mem_cpy(&x, v, 4);
+ else if (byte_order == host_byte_order())
+ x = ((u32) read_u8(bit_order, v)) |
+ (((u32) read_u8(bit_order, v + 1)) << 8) |
+ (((u32) read_u8(bit_order, v + 2)) << 16) |
+ (((u32) read_u8(bit_order, v + 3)) << 24);
+ else
+ x = ((u32) read_u8(bit_order, v + 3)) |
(((u32) read_u8(bit_order, v + 2)) << 8) |
(((u32) read_u8(bit_order, v + 1)) << 16) |
(((u32) read_u8(bit_order, v)) << 24);
+
+ return x;
}
u64 read_u64(i32 bit_order, i32 byte_order, u8 *v, u8 *v_end) {
- BX_ASSERT(v != NULL);
- BX_ASSERT(v + 8 <= v_end);
-
- if (byte_order == host_byte_order())
- return ((u64) read_u8(bit_order, v)) |
- (((u64) read_u8(bit_order, v + 1)) << 8) |
- (((u64) read_u8(bit_order, v + 2)) << 16) |
- (((u64) read_u8(bit_order, v + 3)) << 24) |
- (((u64) read_u8(bit_order, v + 4)) << 32) |
- (((u64) read_u8(bit_order, v + 5)) << 40) |
- (((u64) read_u8(bit_order, v + 6)) << 48) |
- (((u64) read_u8(bit_order, v + 7)) << 56);
-
- return ((u64) read_u8(bit_order, v + 7)) |
- (((u64) read_u8(bit_order, v + 6)) << 8) |
- (((u64) read_u8(bit_order, v + 5)) << 16) |
- (((u64) read_u8(bit_order, v + 4)) << 24) |
- (((u64) read_u8(bit_order, v + 3)) << 32) |
- (((u64) read_u8(bit_order, v + 2)) << 40) |
- (((u64) read_u8(bit_order, v + 1)) << 48) |
- (((u64) read_u8(bit_order, v)) << 56);
+ BX_CHECK(v != NULL, "Invalid arguments", 0);
+ BX_CHECK(v + 8 <= v_end, "Invalid arguments", 0);
+
+ u64 x;
+
+ if (bit_order == host_bit_order() && byte_order == host_byte_order())
+ bx_mem_cpy(&x, v, 8);
+ else if (byte_order == host_byte_order())
+ x = ((u64) read_u8(bit_order, v)) |
+ (((u64) read_u8(bit_order, v + 1)) << 8) |
+ (((u64) read_u8(bit_order, v + 2)) << 16) |
+ (((u64) read_u8(bit_order, v + 3)) << 24) |
+ (((u64) read_u8(bit_order, v + 4)) << 32) |
+ (((u64) read_u8(bit_order, v + 5)) << 40) |
+ (((u64) read_u8(bit_order, v + 6)) << 48) |
+ (((u64) read_u8(bit_order, v + 7)) << 56);
+ else
+ x = ((u64) read_u8(bit_order, v + 7)) |
+ (((u64) read_u8(bit_order, v + 6)) << 8) |
+ (((u64) read_u8(bit_order, v + 5)) << 16) |
+ (((u64) read_u8(bit_order, v + 4)) << 24) |
+ (((u64) read_u8(bit_order, v + 3)) << 32) |
+ (((u64) read_u8(bit_order, v + 2)) << 40) |
+ (((u64) read_u8(bit_order, v + 1)) << 48) |
+ (((u64) read_u8(bit_order, v)) << 56);
+
+ return x;
}
void write_u8(i32 bit_order, u8 x, u8 *v) {
- BX_ASSERT(v != NULL);
+ BX_CHECK(v != NULL, "Invalid arguments",);
if (bit_order == host_bit_order())
*v = x;
@@ -940,10 +987,12 @@ void write_u8(i32 bit_order, u8 x, u8 *v) {
}
void write_u16(i32 bit_order, i32 byte_order, u16 x, u8 *v, u8 *v_end) {
- BX_ASSERT(v != NULL);
- BX_ASSERT(v + 2 <= v_end);
+ BX_CHECK(v != NULL, "Invalid arguments",);
+ BX_CHECK(v + 2 <= v_end, "Invalid arguments",);
- if (byte_order == host_byte_order()) {
+ if (bit_order == host_bit_order() && byte_order == host_byte_order())
+ bx_mem_cpy(v, &x, 2);
+ else if (byte_order == host_byte_order()) {
write_u8(bit_order, (u8) ( x & 0xff), v);
write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 1);
} else {
@@ -953,10 +1002,12 @@ void write_u16(i32 bit_order, i32 byte_order, u16 x, u8 *v, u8 *v_end) {
}
void write_u32(i32 bit_order, i32 byte_order, u32 x, u8 *v, u8 *v_end) {
- BX_ASSERT(v != NULL);
- BX_ASSERT(v + 4 <= v_end);
+ BX_CHECK(v != NULL, "Invalid arguments",);
+ BX_CHECK(v + 4 <= v_end, "Invalid arguments",);
- if (byte_order == host_byte_order()) {
+ if (bit_order == host_bit_order() && byte_order == host_byte_order())
+ bx_mem_cpy(v, &x, 4);
+ else if (byte_order == host_byte_order()) {
write_u8(bit_order, (u8) ( x & 0xff), v);
write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 1);
write_u8(bit_order, (u8) ((x >> 16) & 0xff), v + 2);
@@ -970,10 +1021,12 @@ void write_u32(i32 bit_order, i32 byte_order, u32 x, u8 *v, u8 *v_end) {
}
void write_u64(i32 bit_order, i32 byte_order, u64 x, u8 *v, u8 *v_end) {
- BX_ASSERT(v != NULL);
- BX_ASSERT(v + 8 <= v_end);
+ BX_CHECK(v != NULL, "Invalid arguments",);
+ BX_CHECK(v + 8 <= v_end, "Invalid arguments",);
- if (byte_order == host_byte_order()) {
+ if (bit_order == host_bit_order() && byte_order == host_byte_order())
+ bx_mem_cpy(v, &x, 8);
+ else if (byte_order == host_byte_order()) {
write_u8(bit_order, (u8) ( x & 0xff), v);
write_u8(bit_order, (u8) ((x >> 8) & 0xff), v + 1);
write_u8(bit_order, (u8) ((x >> 16) & 0xff), v + 2);
@@ -1011,7 +1064,7 @@ f32 read_f32(i32 bit_order, i32 byte_order, void *v, void *v_end) {
return *(f32 *) &(u32) { read_u32(bit_order, byte_order, v, v_end) };
}
-f32 read_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, void *v, void *v_end) {
+f64 read_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, void *v, void *v_end) {
u64 x = read_u64(bit_order, byte_order, v, v_end);
if (f64_dword_order != host_f64_dword_order())
@@ -1051,7 +1104,7 @@ void write_f64(i32 bit_order, i32 byte_order, i32 f64_dword_order, f64 x, void *
#define HO_u32 host_bit_order(), host_byte_order()
#define HO_u64 host_bit_order(), host_byte_order()
#define HO_f32 host_bit_order(), host_byte_order()
-#define HO_f64 host_bit_order(), host_byte_order(), host_f64_dword_order()
+#define HO_f64 host_bit_order(), host_byte_order(), host_f64_dword_order()
// ================================================================
//
@@ -1210,9 +1263,9 @@ c8 STRTAB[] = ".strtab";
// ================================================================
u32 ar_find_symbol_offset_by_name(u8 *ar_symbol_table, u8 *ar_end, c8 *name, c8 *name_end) {
- BX_ASSERT(ar_symbol_table != NULL);
- BX_ASSERT(name != NULL);
- BX_ASSERT(name_end > name);
+ BX_CHECK(ar_symbol_table != NULL, "Invalid arguments", -1);
+ BX_CHECK(name != NULL, "Invalid arguments", -1);
+ BX_CHECK(name_end > name, "Invalid arguments", -1);
u32 count = read_u32(host_bit_order(), BE, ar_symbol_table, ar_end);
@@ -1220,20 +1273,19 @@ u32 ar_find_symbol_offset_by_name(u8 *ar_symbol_table, u8 *ar_end, c8 *name, c8
c8 *s = (c8 *) (ar_symbol_table + 4 * (count + 1));
u32 index = 0;
-
+
for (; index < count; ++index) {
- BX_ASSERT(s + len <= (c8 *) ar_end);
+ BX_CHECK(s + len <= (c8 *) ar_end, "Buffer overflow", -1);
if (s[len] == '\0' && bx_mem_eq(s, name, len))
return read_u32(host_bit_order(), BE, ar_symbol_table + 4 * (index + 1), ar_end);
while (*s != '\0' && s < (c8 *) ar_end)
++s;
- BX_ASSERT(s < (c8 *) ar_end);
- BX_ASSERT(*s == '\0');
+ BX_CHECK(s < (c8 *) ar_end, "Buffer overflow", -1);
+ BX_CHECK(*s == '\0', "Buffer overflow", -1);
++s;
}
- BX_ASSERT(0);
- return 0;
+ BX_FAIL("Symbol not found", -1);
}
u16 elf_section_names_table_index(
@@ -1285,7 +1337,7 @@ Offset_Size elf_name_in_string_table(
c8 *begin = (c8 *) (elf_begin + offset);
c8 *end = begin + string_table.size;
- BX_ASSERT(end <= (c8 *) elf_end);
+ BX_CHECK(end <= (c8 *) elf_end, "Buffer overflow", (Offset_Size) {0});
return (Offset_Size) {
.offset = offset,
@@ -1311,8 +1363,7 @@ u64 elf_find_section_header_by_name(
return headers.offset + i * ELF_SECTION_HEADER_SIZE;
}
- BX_ASSERT(0);
- return 0;
+ BX_FAIL("Not found", -1);
}
Section_Header elf_section_data_by_offset(
@@ -1320,9 +1371,9 @@ Section_Header elf_section_data_by_offset(
u8 *elf_end,
u64 offset
) {
- Offset_Size names = elf_section_names_data(elf_begin, elf_end);
+ Offset_Size names = elf_section_names_data(elf_begin, elf_end);
u8 * begin = elf_begin + offset;
- u32 name_index = read_u32(HO_u32, begin, elf_end);
+ u32 name_index = read_u32(HO_u32, begin, elf_end);
return (Section_Header) {
.name = elf_name_in_string_table(
@@ -1341,10 +1392,10 @@ Section_Header elf_section_data_by_offset(
}
void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data) {
- BX_ASSERT(pool != NULL && pool->entities != NULL);
- BX_ASSERT(pool->entities[unit].is_enabled);
- BX_ASSERT(pool->entities[unit].unit.entry_point_index != UNDEFINED);
- BX_ASSERT(target == (FORMAT_ELF | ARCH_X86_64));
+ BX_CHECK(pool != NULL && pool->entities != NULL, "Invalid arguments",);
+ BX_CHECK(pool->entities[unit].is_enabled, "Unit does not exist",);
+ BX_CHECK(pool->entities[unit].unit.entry_point_index != UNDEFINED, "No entry point",);
+ BX_CHECK(target == (FORMAT_ELF | ARCH_X86_64), "Target not supported",);
// ==============================================================
@@ -1369,8 +1420,8 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u64 code_address = base_address + code_offset;
u64 entry = code_address + entry_offset;
- BX_ASSERT((code_offset % X86_64_ALIGNMENT) == 0);
- BX_ASSERT((code_size % X86_64_ALIGNMENT) == 0);
+ BX_CHECK((code_offset % X86_64_ALIGNMENT) == 0, "Invalid alignment",);
+ BX_CHECK((code_size % X86_64_ALIGNMENT) == 0, "Invalid alignment",);
// ELF header
//
@@ -1427,7 +1478,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
#undef WRITE
// ==============================================================
-
+
// Intermediate buffer
//
if (0) {
@@ -1444,12 +1495,12 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
i64 id = u->links[link_index];
if (id == UNDEFINED)
continue;
- BX_ASSERT(ar_count + 1 < (i64) (sizeof ar_offsets / sizeof *ar_offsets));
-
+ BX_CHECK(ar_count + 1 < (i64) (sizeof ar_offsets / sizeof *ar_offsets), "Buffer overflow",);
+
Unit *l = &pool->entities[id].unit;
- BX_ASSERT(pool->entities[id].is_enabled);
- BX_ASSERT(l->type == UNIT_LIBRARY_STATIC);
- BX_ASSERT(l->name_size > 0 && l->name_size <= MAX_NAME_SIZE);
+ 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 && 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);
@@ -1459,7 +1510,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
ar_offsets[ar_count] = 0;
ar_offsets[ar_count + 1] = ar_offsets[ar_count] + bx_align(size, 8);
i64 n = io_read(f, size, in_buffer + ar_offsets[ar_count], io_user_data);
- BX_ASSERT(n == size);
+ BX_CHECK(n == size, "Read failed",);
++ar_count;
io_close(f, io_user_data);
}
@@ -1476,7 +1527,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u8 *ar_symbol_table = NULL;
u8 *ar_string_table = NULL;
- BX_ASSERT(bx_mem_eq(ar_begin, AR_MAGIC, 8));
+ BX_CHECK(bx_mem_eq(ar_begin, AR_MAGIC, 8), "Invalid AR file",);
u8 *f_begin = ar_begin + 8;
@@ -1495,9 +1546,9 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
fn_size = bx_align(fn_size, 2);
- BX_ASSERT(bx_mem_eq(f_end, "\x60\x0a", 2));
- BX_ASSERT(f_begin + fn_size <= ar_end);
-
+ BX_CHECK(bx_mem_eq(f_end, "\x60\x0a", 2), "Invalid AR file",);
+ BX_CHECK(f_begin + fn_size <= ar_end, "Buffer overflow",);
+
if (bx_mem_eq(f_id, AR_SYMBOL_TABLE, 16)) {
// AR symbol table
//
@@ -1526,30 +1577,30 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u16 string_table_index;
u16 string_table_offset;
- BX_ASSERT(read_u8(HO_u8, elf_begin) == ELF_MAGIC[0]);
- BX_ASSERT(read_u8(HO_u8, elf_begin + 1) == ELF_MAGIC[1]);
- BX_ASSERT(read_u8(HO_u8, elf_begin + 2) == ELF_MAGIC[2]);
- BX_ASSERT(read_u8(HO_u8, elf_begin + 3) == ELF_MAGIC[3]);
- BX_ASSERT(read_u8(HO_u8, elf_begin + 4) == ELF_64);
- BX_ASSERT(read_u8(HO_u8, elf_begin + 5) == ELF_2_LE);
- BX_ASSERT(read_u8(HO_u8, elf_begin + 6) == ELF_VERSION);
+ BX_CHECK(read_u8(HO_u8, elf_begin) == ELF_MAGIC[0], "Invalid ELF file",);
+ BX_CHECK(read_u8(HO_u8, elf_begin + 1) == ELF_MAGIC[1], "Invalid ELF file",);
+ BX_CHECK(read_u8(HO_u8, elf_begin + 2) == ELF_MAGIC[2], "Invalid ELF file",);
+ BX_CHECK(read_u8(HO_u8, elf_begin + 3) == ELF_MAGIC[3], "Invalid ELF file",);
+ BX_CHECK(read_u8(HO_u8, elf_begin + 4) == ELF_64, "Invalid ELF file",);
+ BX_CHECK(read_u8(HO_u8, elf_begin + 5) == ELF_2_LE, "Invalid ELF file",);
+ BX_CHECK(read_u8(HO_u8, elf_begin + 6) == ELF_VERSION, "Invalid ELF file",);
u8 osabi = read_u8(HO_u8, elf_begin + 7);
- BX_ASSERT(osabi == ELF_SYS_V || osabi == ELF_LINUX);
- BX_ASSERT(read_u8(HO_u8, elf_begin + 8) == ELF_ABI_VERSION);
+ BX_CHECK(osabi == ELF_SYS_V || osabi == ELF_LINUX, "Invalid ELF file",);
+ BX_CHECK(read_u8(HO_u8, elf_begin + 8) == ELF_ABI_VERSION, "Invalid ELF file",);
- BX_ASSERT(read_u16(HO_u16, elf_begin + 16, elf_end) == ELF_RELOCATABLE);
- BX_ASSERT(read_u16(HO_u16, elf_begin + 18, elf_end) == ELF_X86_64);
- BX_ASSERT(read_u32(HO_u32, elf_begin + 20, elf_end) == ELF_VERSION);
- BX_ASSERT(read_u64(HO_u64, elf_begin + 24, elf_end) == 0); // entry
- BX_ASSERT(read_u64(HO_u64, elf_begin + 32, elf_end) == 0); // program header offset
+ BX_CHECK(read_u16(HO_u16, elf_begin + 16, elf_end) == ELF_RELOCATABLE, "Invalid ELF file",);
+ BX_CHECK(read_u16(HO_u16, elf_begin + 18, elf_end) == ELF_X86_64, "Invalid ELF file",);
+ BX_CHECK(read_u32(HO_u32, elf_begin + 20, elf_end) == ELF_VERSION, "Invalid ELF file",);
+ BX_CHECK(read_u64(HO_u64, elf_begin + 24, elf_end) == 0, "Invalid ELF file",); // entry
+ BX_CHECK(read_u64(HO_u64, elf_begin + 32, elf_end) == 0, "Invalid ELF file",); // program header offset
section_header_offset = read_u64(HO_u64, elf_begin + 40, elf_end);
- BX_ASSERT(read_u32(HO_u32, elf_begin + 48, elf_end) == 0); // flags
- BX_ASSERT(read_u16(HO_u16, elf_begin + 52, elf_end) == ELF_HEADER_SIZE);
- BX_ASSERT(read_u16(HO_u16, elf_begin + 54, elf_end) == 0); // program header size
- BX_ASSERT(read_u16(HO_u16, elf_begin + 56, elf_end) == 0); // program header count
- BX_ASSERT(read_u16(HO_u16, elf_begin + 58, elf_end) == ELF_SECTION_HEADER_SIZE);
+ BX_CHECK(read_u32(HO_u32, elf_begin + 48, elf_end) == 0, "Invalid ELF file",); // flags
+ BX_CHECK(read_u16(HO_u16, elf_begin + 52, elf_end) == ELF_HEADER_SIZE, "Invalid ELF file",);
+ BX_CHECK(read_u16(HO_u16, elf_begin + 54, elf_end) == 0, "Invalid ELF file",); // program header size
+ BX_CHECK(read_u16(HO_u16, elf_begin + 56, elf_end) == 0, "Invalid ELF file",); // program header count
+ BX_CHECK(read_u16(HO_u16, elf_begin + 58, elf_end) == ELF_SECTION_HEADER_SIZE, "Invalid ELF file",);
section_count = read_u16(HO_u16, elf_begin + 60, elf_end);
string_table_index = read_u16(HO_u16, elf_begin + 62, elf_end);
@@ -1573,7 +1624,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
return;
}
-
+
// ==============================================================
// Read dependencies
@@ -1588,9 +1639,9 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
i64 index = u->links[i];
Unit *l = &pool->entities[index].unit;
- BX_ASSERT(pool->entities[index].is_enabled);
- BX_ASSERT(l->type == UNIT_LIBRARY_STATIC);
- BX_ASSERT(l->name_size > 0 && l->name_size <= MAX_NAME_SIZE);
+ BX_CHECK(pool->entities[index].is_enabled, "",);
+ BX_CHECK(l->type == UNIT_LIBRARY_STATIC, "",);
+ BX_CHECK(l->name_size > 0 && l->name_size <= MAX_NAME_SIZE, "",);
i64 f = io_open_read(l->name_size, l->name, io_user_data);
@@ -1609,14 +1660,14 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
n = io_read(f, sizeof magic, magic, io_user_data); if (n == 0) continue;
current_offset += n;
- BX_ASSERT(magic[0] == '!');
- BX_ASSERT(magic[1] == '<');
- BX_ASSERT(magic[2] == 'a');
- BX_ASSERT(magic[3] == 'r');
- BX_ASSERT(magic[4] == 'c');
- BX_ASSERT(magic[5] == 'h');
- BX_ASSERT(magic[6] == '>');
- BX_ASSERT(magic[7] == '\n');
+ BX_CHECK(magic[0] == '!', "",);
+ BX_CHECK(magic[1] == '<', "",);
+ BX_CHECK(magic[2] == 'a', "",);
+ BX_CHECK(magic[3] == 'r', "",);
+ BX_CHECK(magic[4] == 'c', "",);
+ BX_CHECK(magic[5] == 'h', "",);
+ BX_CHECK(magic[6] == '>', "",);
+ BX_CHECK(magic[7] == '\n', "",);
u32 static offsets[10000] = { 0 };
c8 static symbols[10000][256] = { 0 };
@@ -1642,8 +1693,8 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
n = io_read(f, (sizeof size) - 1, size, io_user_data); if (n == 0) break; current_offset += n;
n = io_read(f, sizeof end, end, io_user_data); if (n == 0) break; current_offset += n;
- BX_ASSERT(end[0] == '\x60');
- BX_ASSERT(end[1] == '\x0a');
+ BX_CHECK(end[0] == '\x60', "",);
+ BX_CHECK(end[1] == '\x0a', "",);
if (bx_str_eq(id, id + 16, AR_SYMBOL_TABLE, AR_SYMBOL_TABLE + 16)) {
// AR symbol table
@@ -1660,7 +1711,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
((count_be >> 24) & 0xffu);
printf("Symbol table - %lld symbols.\n\n", num_symbols);
- BX_ASSERT(num_symbols <= (i64) (sizeof offsets / sizeof *offsets));
+ BX_CHECK(num_symbols <= (i64) (sizeof offsets / sizeof *offsets), "",);
for (u32 j = 0; j < num_symbols; ++j) {
u32 offset_be;
@@ -1686,7 +1737,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
++byte_count;
if (c == '\0')
break;
- BX_ASSERT(symbol_size < 256);
+ BX_CHECK(symbol_size < 256, "",);
if (symbol_size < 256)
symbols[j][symbol_size] = c;
}
@@ -1769,16 +1820,16 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
current_offset += n;
byte_count -= n;
- BX_ASSERT(buf[0] == 0x7f);
- BX_ASSERT(buf[1] == 'E');
- BX_ASSERT(buf[2] == 'L');
- BX_ASSERT(buf[3] == 'F');
+ BX_CHECK(buf[0] == 0x7f, "",);
+ BX_CHECK(buf[1] == 'E', "",);
+ BX_CHECK(buf[2] == 'L', "",);
+ BX_CHECK(buf[3] == 'F', "",);
- BX_ASSERT(buf[4] == ELF_64);
- BX_ASSERT(buf[5] == ELF_2_LE);
- BX_ASSERT(buf[6] == ELF_VERSION);
- BX_ASSERT(buf[7] == ELF_SYS_V || buf[7] == ELF_LINUX); // SysV or Linux
- BX_ASSERT(buf[8] == ELF_ABI_VERSION);
+ BX_CHECK(buf[4] == ELF_64, "",);
+ BX_CHECK(buf[5] == ELF_2_LE, "",);
+ BX_CHECK(buf[6] == ELF_VERSION, "",);
+ BX_CHECK(buf[7] == ELF_SYS_V || buf[7] == ELF_LINUX, "",);
+ BX_CHECK(buf[8] == ELF_ABI_VERSION, "",);
#define READ(x) do { \
n = io_read(f, sizeof (x), \
@@ -1825,17 +1876,17 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
READ(section_count);
READ(strings_index);
- BX_ASSERT(type == ELF_RELOCATABLE);
- BX_ASSERT(machine == ELF_X86_64);
- BX_ASSERT(ver == ELF_VERSION);
+ BX_CHECK(type == ELF_RELOCATABLE, "",);
+ BX_CHECK(machine == ELF_X86_64, "",);
+ BX_CHECK(ver == ELF_VERSION, "",);
- BX_ASSERT(entry == 0);
- BX_ASSERT(program_header_offset == 0);
- BX_ASSERT(flags == 0);
- BX_ASSERT(elf_header_size == 64);
- BX_ASSERT(program_header_size == 0);
- BX_ASSERT(program_header_count == 0);
- BX_ASSERT(section_header_size == 64);
+ BX_CHECK(entry == 0, "",);
+ BX_CHECK(program_header_offset == 0, "",);
+ BX_CHECK(flags == 0, "",);
+ BX_CHECK(elf_header_size == 64, "",);
+ BX_CHECK(program_header_size == 0, "",);
+ BX_CHECK(program_header_count == 0, "",);
+ BX_CHECK(section_header_size == 64, "",);
u64 section_offset = section_header_offset - (current_offset - begin_offset);
@@ -2016,7 +2067,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
// Find symbol addresses
//
- BX_ASSERT(entsize == 24);
+ BX_CHECK(entsize == 24, "",);
i64 prev_offset = current_offset;
i64 prev_byte_count = byte_count;
@@ -2028,7 +2079,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
printf("\n");
for (byte_count = size; byte_count > 0;) {
- BX_ASSERT(symbol_names_found);
+ BX_CHECK(symbol_names_found, "",);
u32 sym_name;
u8 sym_info;
@@ -2037,12 +2088,12 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u64 sym_value;
u64 sym_size;
- READ(sym_name); BX_ASSERT(n != 0);
- READ(sym_info); BX_ASSERT(n != 0);
- READ(sym_other); BX_ASSERT(n != 0);
- READ(sym_shndx); BX_ASSERT(n != 0);
- READ(sym_value); BX_ASSERT(n != 0);
- READ(sym_size); BX_ASSERT(n != 0);
+ READ(sym_name); BX_CHECK(n != 0, "",);
+ READ(sym_info); BX_CHECK(n != 0, "",);
+ READ(sym_other); BX_CHECK(n != 0, "",);
+ READ(sym_shndx); BX_CHECK(n != 0, "",);
+ READ(sym_value); BX_CHECK(n != 0, "",);
+ READ(sym_size); BX_CHECK(n != 0, "",);
printf(" ");
@@ -2117,7 +2168,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
// Relocarions without addends
case 9: {
- BX_ASSERT(entsize == 16);
+ BX_CHECK(entsize == 16, "",);
i64 prev_offset = current_offset;
i64 prev_byte_count = byte_count;
@@ -2130,8 +2181,8 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u64 rel_offset;
u64 rel_info;
- READ(rel_offset); BX_ASSERT(n != 0);
- READ(rel_info); BX_ASSERT(n != 0);
+ READ(rel_offset); BX_CHECK(n != 0, "",);
+ READ(rel_info); BX_CHECK(n != 0, "",);
u32 rel_sym = (u32) (rel_info >> 32);
u32 rel_type = (u32) (rel_info & 0xffffffff);
@@ -2164,7 +2215,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
// )
case 4: {
- BX_ASSERT(entsize == 24);
+ BX_CHECK(entsize == 24, "",);
i64 prev_offset = current_offset;
i64 prev_byte_count = byte_count;
@@ -2178,9 +2229,9 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
u64 rela_info;
i64 rela_addent;
- READ(rela_offset); BX_ASSERT(n != 0);
- READ(rela_info); BX_ASSERT(n != 0);
- READ(rela_addent); BX_ASSERT(n != 0);
+ READ(rela_offset); BX_CHECK(n != 0, "",);
+ READ(rela_info); BX_CHECK(n != 0, "",);
+ READ(rela_addent); BX_CHECK(n != 0, "",);
u32 rela_sym = (u32) (rela_info >> 32);
u32 rela_type = (u32) (rela_info & 0xffffffff);
@@ -2233,7 +2284,7 @@ void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data
io_read(f, sym_size, buf, io_user_data);
for (u32 k = 0; k < sym_size; ++k)
- BX_ASSERT(buf[k] == 0);
+ BX_CHECK(buf[k] == 0, "",);
}
}
@@ -2340,18 +2391,20 @@ void io_chmod_exe(i64 f, void *user_data) {
#include <sys/stat.h>
#endif
-// Assert
-//
+void bx_log(i32 log_level, c8 *message, u32 line, c8 *file) {
+ fflush(stdout);
+ if (log_level == ERROR)
+ fprintf(stderr,
+ "\r\x1b[41;1m\x1b[30mERROR:\x1b[40m\x1b[37m %s"
+ " \x1b[36m%s\x1b[34m:%d\x1b[37m\n",
+ message, file, line);
+}
void bx_assert(b8 condition, c8 *message, u32 line, c8 *file) {
if (condition)
return;
- fflush(stdout);
- fprintf(stderr,
- "\r\x1b[31mASSERTION:\x1b[37m `\x1b[33m%s\x1b[37m`"
- " is false in \x1b[36m%s:%d\x1b[37m\n",
- message, file, line);
+ bx_log(ERROR, message, line, file);
exit(-1);
}
@@ -2359,7 +2412,7 @@ void bx_assert(b8 condition, c8 *message, u32 line, c8 *file) {
//
void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data) {
- BX_ASSERT(id != NULL);
+ BX_CHECK(id != NULL, "Invalid arguments",);
(void) user_data;
@@ -2369,74 +2422,74 @@ void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data) {
switch (op) {
case IO_OPEN_READ:
case IO_OPEN_WRITE:
- BX_ASSERT(size != NULL);
- BX_ASSERT(*size > 0 && *size < MAX_NAME_SIZE);
- BX_ASSERT(data != NULL);
+ BX_CHECK(size != NULL, "Invalid arguments",);
+ BX_CHECK(*size > 0 && *size < MAX_NAME_SIZE, "Invalid arguments",);
+ BX_CHECK(data != NULL, "Invalid arguments",);
bx_mem_cpy(buf, data, *size);
*f = fopen(buf, op == IO_OPEN_READ ? "rb" : "wb");
- BX_ASSERT(*f != NULL);
+ BX_CHECK(*f != NULL, "File open failed",);
break;
case IO_CLOSE:
- BX_ASSERT(*f != NULL);
- BX_ASSERT(size == NULL);
- BX_ASSERT(data == NULL);
+ BX_CHECK(*f != NULL, "Invalid arguments",);
+ BX_CHECK(size == NULL, "Invalid arguments",);
+ BX_CHECK(data == NULL, "Invalid arguments",);
fclose(*f);
break;
case IO_SEEK: {
- BX_ASSERT(*f != NULL);
- BX_ASSERT(size != NULL);
- BX_ASSERT(data != NULL);
+ BX_CHECK(*f != NULL, "Invalid arguments",);
+ BX_CHECK(size != NULL, "Invalid arguments",);
+ BX_CHECK(data != NULL, "Invalid arguments",);
u16 *origin = (u16 *) data;
if (!(*origin == IO_SEEK_CURSOR && *size == 0)) {
- BX_ASSERT(*origin == IO_SEEK_CURSOR ||
- *origin == IO_SEEK_BEGIN ||
- *origin == IO_SEEK_END);
+ BX_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_ASSERT(s == 0);
+ BX_CHECK(s == 0, "File seek failed",);
}
} break;
case IO_TELL: {
- BX_ASSERT(*f != NULL);
- BX_ASSERT(size != NULL);
- BX_ASSERT(data == NULL);
+ BX_CHECK(*f != NULL, "Invalid arguments",);
+ BX_CHECK(size != NULL, "Invalid arguments",);
+ BX_CHECK(data == NULL, "Invalid arguments",);
i64 n = (i64) ftell(*f);
- BX_ASSERT(n >= 0);
+ BX_CHECK(n >= 0, "File tell failed",);
*size = n;
} break;
case IO_READ:
- BX_ASSERT(*f != NULL);
- BX_ASSERT(size != NULL);
- BX_ASSERT(data != NULL);
- BX_ASSERT(*size > 0);
+ BX_CHECK(*f != NULL, "Invalid arguments",);
+ BX_CHECK(size != NULL, "Invalid arguments",);
+ BX_CHECK(data != NULL, "Invalid arguments",);
+ BX_CHECK(*size > 0, "Invalid arguments",);
*size = fread(data, 1, *size, *f);
break;
case IO_WRITE:
- BX_ASSERT(*f != NULL);
- BX_ASSERT(size != NULL);
- BX_ASSERT(data != NULL);
- BX_ASSERT(*size > 0);
+ BX_CHECK(*f != NULL, "Invalid arguments",);
+ BX_CHECK(size != NULL, "Invalid arguments",);
+ BX_CHECK(data != NULL, "Invalid arguments",);
+ BX_CHECK(*size > 0, "Invalid arguments",);
*size = fwrite(data, 1, *size, *f);
break;
case IO_CHMOD_EXE:
- BX_ASSERT(*f != NULL);
- BX_ASSERT(size == NULL);
+ BX_CHECK(*f != NULL, "Invalid arguments",);
+ BX_CHECK(size == NULL, "Invalid arguments",);
#ifdef __unix__
fchmod(fileno(*f), 0775);
@@ -2444,7 +2497,7 @@ void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data) {
break;
default:
- BX_ASSERT(0);
+ BX_FAIL("Invalid arguments",);
}
}
@@ -2563,7 +2616,7 @@ int main(int argc, char **argv) {
i32 ret = system("./test_foo");
- BX_ASSERT(WEXITSTATUS(ret) == 42);
+ BX_CHECK(WEXITSTATUS(ret) == 42, "Failure", -1);
printf("\nBye!\n");
return 0;