diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-06-09 21:53:37 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-06-09 21:53:37 +0200 |
commit | 9bc6afd129f4730855a12de7ad01222a2614da26 (patch) | |
tree | 60c2240eb9c4146afad0c956894eeaa1a8e52558 | |
parent | ed040c9704c69272b79a1a8ae3368f3e1fd36e35 (diff) | |
download | bxgen-9bc6afd129f4730855a12de7ad01222a2614da26.zip |
Refactor & cleanup
-rwxr-xr-x | bxgen.c | 114 |
1 files changed, 58 insertions, 56 deletions
@@ -146,7 +146,7 @@ enum { typedef struct { i16 op; - i8 flow; + i8 flow; // `FLOW_DATA` or `FLOW_CONTROL` union { u8 lit_bytes[MAX_LITERAL_SIZE]; // byte array literal i64 lit_int; // integer literal @@ -206,9 +206,9 @@ typedef struct { typedef struct { i64 entity_count; - i64 entities_capacity; + i64 capacity; entity_t *entities; -} entity_pool_t; +} pool_t; // ================================================================ // @@ -220,26 +220,26 @@ typedef struct { extern "C" { #endif -i64 pool_entity_add(entity_pool_t *pool, entity_t data); -void pool_entity_remove(entity_pool_t *pool, i64 entity, i16 type); +i64 pool_add(pool_t *pool, entity_t data); +void pool_remove(pool_t *pool, i64 entity, i16 type); -i64 node_init(entity_pool_t *pool, node_t data); -void node_destroy(entity_pool_t *pool, i64 node); -i64 node_op_i64(entity_pool_t *pool, i64 value); -i64 node_op_ret(entity_pool_t *pool, i64 node_return_value); +i64 node_init(pool_t *pool, node_t data); +void node_destroy(pool_t *pool, i64 node); +i64 node_op_i64(pool_t *pool, i64 value); +i64 node_op_ret(pool_t *pool, i64 node_return_value); -i64 proc_init(entity_pool_t *pool); -void proc_destroy(entity_pool_t *pool, i64 proc); -void proc_set_name(entity_pool_t *pool, i64 proc, i64 name_size, c8 const *name); -void proc_node_add(entity_pool_t *pool, i64 proc, i64 node); -void proc_node_remove(entity_pool_t *pool, i64 proc, i64 node); +i64 proc_init(pool_t *pool); +void proc_destroy(pool_t *pool, i64 proc); +void proc_set_name(pool_t *pool, i64 proc, i64 name_size, c8 const *name); +void proc_node_add(pool_t *pool, i64 proc, i64 node); +void proc_node_remove(pool_t *pool, i64 proc, i64 node); -i64 unit_init(entity_pool_t *pool); -void unit_destroy(entity_pool_t *pool, i64 unit); -void unit_proc_add(entity_pool_t *pool, i64 unit, i64 proc); -void unit_proc_remove(entity_pool_t *pool, i64 unit, i64 proc); -void unit_set_entry_point(entity_pool_t *pool, i64 unit, i64 entry_point_proc); -void unit_write(entity_pool_t *pool, i64 unit, u16 target, FILE *out); +i64 unit_init(pool_t *pool); +void unit_destroy(pool_t *pool, i64 unit); +void unit_proc_add(pool_t *pool, i64 unit, i64 proc); +void unit_proc_remove(pool_t *pool, i64 unit, i64 proc); +void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc); +void unit_write(pool_t *pool, i64 unit, u16 target, FILE *out); #ifndef DISABLE_HELPERS i64 n_i64(i64 value); @@ -268,21 +268,15 @@ void u_elf_x86_64(i64 unit, c8 const *output_file_name); #error Implementation code should be compiled with a C compiler! #endif -#include <stdlib.h> -#include <string.h> -#include <assert.h> - -#ifdef __unix__ -#include <sys/types.h> -#include <sys/stat.h> -#endif +#include <string.h> // memcpy +#include <assert.h> // assert // IR building procs // -i64 pool_entity_add(entity_pool_t *pool, entity_t data) { +i64 pool_add(pool_t *pool, entity_t data) { assert(pool != NULL); - assert(pool->entity_count < pool->entities_capacity); + assert(pool->entity_count < pool->capacity); i64 id = pool->entity_count++; data.is_enabled = 1, @@ -291,7 +285,7 @@ i64 pool_entity_add(entity_pool_t *pool, entity_t data) { return id; } -void pool_entity_remove(entity_pool_t *pool, i64 entity, i16 type) { +void pool_remove(pool_t *pool, i64 entity, i16 type) { assert(pool != NULL); assert(pool->entities[entity].is_enabled); assert(pool->entities[entity].type == type); @@ -299,19 +293,19 @@ void pool_entity_remove(entity_pool_t *pool, i64 entity, i16 type) { pool->entities[entity].is_enabled = 1; } -i64 node_init(entity_pool_t *pool, node_t data) { - return pool_entity_add(pool, (entity_t) { +i64 node_init(pool_t *pool, node_t data) { + return pool_add(pool, (entity_t) { .type = TYPE_NODE, .tail = UNDEFINED, .node = data, }); } -void node_destroy(entity_pool_t *pool, i64 node) { - pool_entity_remove(pool, node, TYPE_NODE); +void node_destroy(pool_t *pool, i64 node) { + pool_remove(pool, node, TYPE_NODE); } -i64 node_op_i64(entity_pool_t *pool, i64 value) { +i64 node_op_i64(pool_t *pool, i64 value) { return node_init(pool, (node_t) { .op = OP_I64, .flow = FLOW_DATA, @@ -319,26 +313,26 @@ i64 node_op_i64(entity_pool_t *pool, i64 value) { }); } -i64 node_op_ret(entity_pool_t *pool, i64 node_return_value) { +i64 node_op_ret(pool_t *pool, i64 node_return_value) { return node_init(pool, (node_t) { .op = OP_RET, .flow = FLOW_CONTROL, - .ref_node = node_return_value, + .ref_node = { node_return_value, 0, }, }); } -i64 proc_init(entity_pool_t *pool) { - return pool_entity_add(pool, (entity_t) { +i64 proc_init(pool_t *pool) { + return pool_add(pool, (entity_t) { .type = TYPE_PROC, .tail = UNDEFINED, }); } -void proc_destroy(entity_pool_t *pool, i64 proc) { - pool_entity_remove(pool, proc, TYPE_PROC); +void proc_destroy(pool_t *pool, i64 proc) { + pool_remove(pool, proc, TYPE_PROC); } -void proc_set_name(entity_pool_t *pool, i64 proc, i64 name_size, c8 const *name) { +void proc_set_name(pool_t *pool, i64 proc, i64 name_size, c8 const *name) { assert(pool != NULL); assert(pool->entities[proc].is_enabled); assert(pool->entities[proc].type == TYPE_PROC); @@ -353,7 +347,7 @@ void proc_set_name(entity_pool_t *pool, i64 proc, i64 name_size, c8 const *name) memcpy(p->name, name, name_size); } -void proc_node_add(entity_pool_t *pool, i64 proc, i64 node) { +void proc_node_add(pool_t *pool, i64 proc, i64 node) { assert(pool != NULL); assert(pool->entities[proc].is_enabled); assert(pool->entities[proc].type == TYPE_PROC); @@ -369,7 +363,7 @@ void proc_node_add(entity_pool_t *pool, i64 proc, i64 node) { p->nodes[p->node_count++] = node; } -void proc_node_remove(entity_pool_t *pool, i64 proc, i64 node) { +void proc_node_remove(pool_t *pool, i64 proc, i64 node) { assert(pool != NULL); assert(pool->entities[proc].is_enabled); assert(pool->entities[proc].type == TYPE_PROC); @@ -378,8 +372,8 @@ void proc_node_remove(entity_pool_t *pool, i64 proc, i64 node) { pool->entities[proc].proc.nodes[node] = UNDEFINED; } -i64 unit_init(entity_pool_t *pool) { - return pool_entity_add(pool, (entity_t) { +i64 unit_init(pool_t *pool) { + return pool_add(pool, (entity_t) { .type = TYPE_UNIT, .tail = UNDEFINED, .unit = (unit_t) { @@ -388,11 +382,11 @@ i64 unit_init(entity_pool_t *pool) { }); } -void unit_destroy(entity_pool_t *pool, i64 unit) { - pool_entity_remove(pool, unit, TYPE_UNIT); +void unit_destroy(pool_t *pool, i64 unit) { + pool_remove(pool, unit, TYPE_UNIT); } -void unit_proc_add(entity_pool_t *pool, i64 unit, i64 proc) { +void unit_proc_add(pool_t *pool, i64 unit, i64 proc) { assert(pool != NULL); assert(pool->entities[unit].is_enabled); assert(pool->entities[unit].type == TYPE_UNIT); @@ -408,7 +402,7 @@ void unit_proc_add(entity_pool_t *pool, i64 unit, i64 proc) { u->procs[u->proc_count++] = proc; } -void unit_proc_remove(entity_pool_t *pool, i64 unit, i64 proc) { +void unit_proc_remove(pool_t *pool, i64 unit, i64 proc) { assert(pool != NULL); assert(pool->entities[unit].is_enabled); assert(pool->entities[unit].type == TYPE_UNIT); @@ -417,7 +411,7 @@ void unit_proc_remove(entity_pool_t *pool, i64 unit, i64 proc) { pool->entities[unit].unit.procs[proc] = UNDEFINED; } -void unit_set_entry_point(entity_pool_t *pool, i64 unit, i64 entry_point_proc) { +void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc) { assert(pool != NULL); assert(pool->entities[unit].is_enabled); assert(pool->entities[unit].type == TYPE_UNIT); @@ -429,7 +423,10 @@ void unit_set_entry_point(entity_pool_t *pool, i64 unit, i64 entry_point_proc) { // Code generation proc // -void unit_write(entity_pool_t *pool, i64 unit, u16 target, FILE *out) { +void unit_write(pool_t *pool, i64 unit, u16 target, FILE *out) { + // TODO + // Use callback instead of `FILE` to not depend on `stdio.h` here. + assert(pool != NULL); assert(pool->entities[unit].is_enabled); assert(pool->entities[unit].unit.entry_point != UNDEFINED); @@ -525,17 +522,22 @@ void unit_write(entity_pool_t *pool, i64 unit, u16 target, FILE *out) { #ifndef DISABLE_HELPERS +#ifdef __unix__ +#include <sys/types.h> +#include <sys/stat.h> +#endif + // Global state // -static entity_pool_t g_pool = { +static pool_t g_pool = { // Statically allocate a large memory block. // // TODO // Reallocate the memory block when necessary. // - .entities_capacity = MAX_ENTITY_COUNT, - .entities = (entity_t[MAX_ENTITY_COUNT]) { 0 }, + .capacity = MAX_ENTITY_COUNT, + .entities = (entity_t[MAX_ENTITY_COUNT]) { 0 }, }; // Handy procedures |