summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbxgen.c250
1 files changed, 125 insertions, 125 deletions
diff --git a/bxgen.c b/bxgen.c
index 436e3e4..db35ff8 100755
--- a/bxgen.c
+++ b/bxgen.c
@@ -70,7 +70,7 @@ exit $?
//#define DISABLE_IMPLEMENTATION
//#define DISABLE_HELPERS
-//#define DISABLE_EXAMPLE
+//#define DISABLE_TESTING
// ================================================================
//
@@ -166,7 +166,7 @@ enum {
// NOTE
//
// All limits can be exceeded using the linked list of entities
- // (see `entity_t::tail`), except for `MAX_ENTITY_COUNT`.
+ // (see `Entity::tail`), except for `MAX_ENTITY_COUNT`.
//
MAX_LITERAL_SIZE = 400,
@@ -201,36 +201,36 @@ typedef struct {
i16 size;
i16 type;
i64 node;
-} var_t;
+} Var;
typedef struct {
- i16 val_count;
- var_t vals[MAX_ARG_COUNT];
-} ret_t;
+ i16 val_count;
+ Var vals[MAX_ARG_COUNT];
+} Ret;
typedef struct {
// NOTE
// We may call a local procedure by it's id,
// or a global procedure by name.
- i16 convention; // can be implicitly retrieved from the procedure
- i64 target_proc;
- i64 target_name_size;
- c8 target_name[MAX_NAME_SIZE];
- i64 arg_count;
- var_t args[MAX_ARG_COUNT];
-} call_t;
+ i16 convention; // can be implicitly retrieved from the procedure
+ i64 target_proc;
+ i64 target_name_size;
+ c8 target_name[MAX_NAME_SIZE];
+ i64 arg_count;
+ Var args[MAX_ARG_COUNT];
+} Call;
typedef struct {
i16 op;
i64 index_in_proc;
union {
- u8 lit_bytes[MAX_LITERAL_SIZE]; // byte array literal
- i64 lit_int; // integer literal
- ret_t ret;
- call_t call;
+ u8 lit_bytes[MAX_LITERAL_SIZE]; // byte array literal
+ i64 lit_int; // integer literal
+ Ret ret;
+ Call call;
};
-} node_t;
+} Node;
// A procedure is a collection of semantic nodes
// and has a string name.
@@ -245,7 +245,7 @@ typedef struct {
i64 ret_index;
i64 unit;
i64 index_in_unit;
-} proc_t;
+} Proc;
// A compilation unit is a collection of procedures.
//
@@ -259,12 +259,12 @@ typedef struct {
i64 procs[MAX_PROC_COUNT];
i64 link_count;
i64 links[MAX_LINK_COUNT];
-} unit_t;
+} Unit;
// An entity can be any of:
-// - `node_t`
-// - `proc_t`
-// - `unit_t`
+// - `Node`
+// - `Proc`
+// - `Unit`
//
// Every entity can be referenced by it's unique index
// in the entity pool.
@@ -279,14 +279,14 @@ typedef struct {
i16 type;
i64 tail;
union {
- node_t node;
- proc_t proc;
- unit_t unit;
- c8 tail_chars[1];
- i64 tail_ids[1];
- var_t tail_vars[1];
+ Node node;
+ Proc proc;
+ Unit unit;
+ c8 tail_chars[1];
+ i64 tail_ids[1];
+ Var tail_vars[1];
};
-} entity_t;
+} Entity;
// Pool, a collection of all entities.
//
@@ -295,10 +295,10 @@ typedef struct {
//
typedef struct {
- i64 entity_count;
- i64 capacity;
- entity_t *entities;
-} pool_t;
+ i64 entity_count;
+ i64 capacity;
+ Entity *entities;
+} Pool;
// ================================================================
//
@@ -310,32 +310,32 @@ typedef struct {
extern "C" {
#endif
-i64 pool_add(pool_t *pool, entity_t data);
-void pool_remove(pool_t *pool, i64 entity, i16 type);
-
-i64 node_init(pool_t *pool, node_t data);
-void node_destroy(pool_t *pool, i64 node);
-i64 node_data_i64(pool_t *pool, i64 value);
-i64 node_ctrl_call(pool_t *pool, i16 convention, i64 target_proc, i64 arg_count, var_t *args);
-i64 node_ctrl_call_by_name(pool_t *pool, i16 convention, i64 name_size, c8 *name, i64 arg_count, var_t *args);
-i64 node_ctrl_ret(pool_t *pool, i64 value_count, var_t *values);
-
-i64 proc_init(pool_t *pool);
-void proc_destroy(pool_t *pool, i64 proc);
-void proc_set_convention(pool_t *pool, i64 proc, i16 convention);
-void proc_set_name(pool_t *pool, i64 proc, i64 name_size, c8 *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(pool_t *pool, i16 type);
-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_link_add(pool_t *pool, i64 unit, i64 link_unit);
-void unit_link_remove(pool_t *pool, i64 unit, i64 link_unit);
-void unit_set_name(pool_t *pool, i64 unit, i64 name_size, c8 *name);
-void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc);
-void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_id, void *io_user_data);
+i64 pool_add(Pool *pool, Entity data);
+void pool_remove(Pool *pool, i64 entity, i16 type);
+
+i64 node_init(Pool *pool, Node data);
+void node_destroy(Pool *pool, i64 node);
+i64 node_data_i64(Pool *pool, i64 value);
+i64 node_ctrl_call(Pool *pool, i16 convention, i64 target_proc, i64 arg_count, Var *args);
+i64 node_ctrl_call_by_name(Pool *pool, i16 convention, i64 name_size, c8 *name, i64 arg_count, Var *args);
+i64 node_ctrl_ret(Pool *pool, i64 value_count, Var *values);
+
+i64 proc_init(Pool *pool);
+void proc_destroy(Pool *pool, i64 proc);
+void proc_set_convention(Pool *pool, i64 proc, i16 convention);
+void proc_set_name(Pool *pool, i64 proc, i64 name_size, c8 *name);
+void proc_node_add(Pool *pool, i64 proc, i64 node);
+void proc_node_remove(Pool *pool, i64 proc, i64 node);
+
+i64 unit_init(Pool *pool, i16 type);
+void unit_destroy(Pool *pool, i64 unit);
+void unit_proc_add(Pool *pool, i64 unit, i64 proc);
+void unit_proc_remove(Pool *pool, i64 unit, i64 proc);
+void unit_link_add(Pool *pool, i64 unit, i64 link_unit);
+void unit_link_remove(Pool *pool, i64 unit, i64 link_unit);
+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);
+void unit_write(Pool *pool, i64 unit, u16 target, i64 io_id, void *io_user_data);
i64 io_open_read(i64 name_size, c8 *name, void *user_data);
i64 io_open_write(i64 name_size, c8 *name, void *user_data);
@@ -349,9 +349,9 @@ void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data);
#ifndef DISABLE_HELPERS
i64 n_i64(i64 value);
-i64 n_call(i16 convention, i64 target_proc, i64 arg_count, var_t *args);
-i64 n_call_by_name(i16 convention, c8 *name, i64 arg_count, var_t *args);
-i64 n_ret(i64 val_count, var_t *vals);
+i64 n_call(i16 convention, i64 target_proc, i64 arg_count, Var *args);
+i64 n_call_by_name(i16 convention, c8 *name, i64 arg_count, Var *args);
+i64 n_ret(i64 val_count, Var *vals);
i64 p_new(c8 *name);
void p_add(i64 proc, i64 node);
i64 u_new();
@@ -385,7 +385,7 @@ void l_static(i64 unit, c8 *static_library);
// IR building procs
//
-i64 pool_add(pool_t *pool, entity_t data) {
+i64 pool_add(Pool *pool, Entity data) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entity_count < pool->capacity);
@@ -396,7 +396,7 @@ i64 pool_add(pool_t *pool, entity_t data) {
return id;
}
-void pool_remove(pool_t *pool, i64 entity, i16 type) {
+void pool_remove(Pool *pool, i64 entity, i16 type) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[entity].is_enabled);
assert(pool->entities[entity].type == type);
@@ -404,31 +404,31 @@ void pool_remove(pool_t *pool, i64 entity, i16 type) {
pool->entities[entity].is_enabled = 1;
}
-i64 node_init(pool_t *pool, node_t data) {
+i64 node_init(Pool *pool, Node data) {
data.index_in_proc = UNDEFINED;
- return pool_add(pool, (entity_t) {
+ return pool_add(pool, (Entity) {
.type = ENTITY_NODE,
.tail = UNDEFINED,
.node = data,
});
}
-void node_destroy(pool_t *pool, i64 node) {
+void node_destroy(Pool *pool, i64 node) {
pool_remove(pool, node, ENTITY_NODE);
}
-i64 node_data_i64(pool_t *pool, i64 value) {
- return node_init(pool, (node_t) {
+i64 node_data_i64(Pool *pool, i64 value) {
+ return node_init(pool, (Node) {
.op = DATA_I64,
.lit_int = value,
});
}
-i64 node_ctrl_call(pool_t *pool, i16 convention, i64 target_proc, i64 arg_count, var_t *args) {
+i64 node_ctrl_call(Pool *pool, i16 convention, i64 target_proc, i64 arg_count, Var *args) {
assert(arg_count <= MAX_ARG_COUNT);
- call_t call = {
+ Call call = {
.convention = convention,
.target_proc = target_proc,
.arg_count = arg_count,
@@ -437,16 +437,16 @@ i64 node_ctrl_call(pool_t *pool, i16 convention, i64 target_proc, i64 arg_count,
if (arg_count > 0)
memcpy(call.args, args, arg_count * sizeof *args);
- return node_init(pool, (node_t) {
+ return node_init(pool, (Node) {
.op = CTRL_CALL,
.call = call,
});
}
-i64 node_ctrl_call_by_name(pool_t *pool, i16 convention, i64 name_size, c8 *name, i64 arg_count, var_t *args) {
+i64 node_ctrl_call_by_name(Pool *pool, i16 convention, i64 name_size, c8 *name, i64 arg_count, Var *args) {
assert(arg_count <= MAX_ARG_COUNT);
- call_t call = {
+ Call call = {
.convention = convention,
.target_name_size = name_size,
.arg_count = arg_count,
@@ -457,42 +457,42 @@ i64 node_ctrl_call_by_name(pool_t *pool, i16 convention, i64 name_size, c8 *name
if (arg_count > 0)
memcpy(call.args, args, arg_count * sizeof *args);
- return node_init(pool, (node_t) {
+ return node_init(pool, (Node) {
.op = CTRL_CALL,
.call = call,
});
}
-i64 node_ctrl_ret(pool_t *pool, i64 value_count, var_t *values) {
+i64 node_ctrl_ret(Pool *pool, i64 value_count, Var *values) {
assert(value_count <= MAX_ARG_COUNT);
- ret_t ret = { .val_count = value_count, };
+ Ret ret = { .val_count = value_count, };
if (value_count > 0)
memcpy(ret.vals, values, value_count * sizeof *values);
- return node_init(pool, (node_t) {
+ return node_init(pool, (Node) {
.op = CTRL_RET,
.ret = ret,
});
}
-i64 proc_init(pool_t *pool) {
- return pool_add(pool, (entity_t) {
+i64 proc_init(Pool *pool) {
+ return pool_add(pool, (Entity) {
.type = ENTITY_PROC,
.tail = UNDEFINED,
- .proc = (proc_t) {
+ .proc = (Proc) {
.ret_index = UNDEFINED,
.index_in_unit = UNDEFINED,
},
});
}
-void proc_destroy(pool_t *pool, i64 proc) {
+void proc_destroy(Pool *pool, i64 proc) {
pool_remove(pool, proc, ENTITY_PROC);
}
-void proc_set_convention(pool_t *pool, i64 proc, i16 convention) {
+void proc_set_convention(Pool *pool, i64 proc, i16 convention) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[proc].is_enabled);
assert(pool->entities[proc].type == ENTITY_PROC);
@@ -500,7 +500,7 @@ void proc_set_convention(pool_t *pool, i64 proc, i16 convention) {
pool->entities[proc].proc.convention = convention;
}
-void proc_set_name(pool_t *pool, i64 proc, i64 name_size, c8 *name) {
+void proc_set_name(Pool *pool, i64 proc, i64 name_size, c8 *name) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[proc].is_enabled);
assert(pool->entities[proc].type == ENTITY_PROC);
@@ -511,22 +511,22 @@ void proc_set_name(pool_t *pool, i64 proc, i64 name_size, c8 *name) {
assert(name_size <= MAX_NAME_SIZE);
assert(name_size >= 0);
- proc_t *p = &pool->entities[proc].proc;
+ Proc *p = &pool->entities[proc].proc;
p->name_size = name_size;
if (name_size > 0)
memcpy(p->name, name, name_size);
}
-void proc_node_add(pool_t *pool, i64 proc, i64 node) {
+void proc_node_add(Pool *pool, i64 proc, i64 node) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[proc].is_enabled);
assert(pool->entities[proc].type == ENTITY_PROC);
assert(pool->entities[node].is_enabled);
assert(pool->entities[node].type == ENTITY_NODE);
- proc_t *p = &pool->entities[proc].proc;
- node_t *n = &pool->entities[node].node;
+ Proc *p = &pool->entities[proc].proc;
+ Node *n = &pool->entities[node].node;
assert(n->index_in_proc == UNDEFINED);
@@ -551,14 +551,14 @@ void proc_node_add(pool_t *pool, i64 proc, i64 node) {
++p->node_count;
}
-void proc_node_remove(pool_t *pool, i64 proc, i64 node) {
+void proc_node_remove(Pool *pool, i64 proc, i64 node) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[proc].is_enabled);
assert(pool->entities[proc].type == ENTITY_PROC);
assert(pool->entities[node].type == ENTITY_NODE);
- proc_t *p = &pool->entities[proc].proc;
- node_t *n = &pool->entities[node].node;
+ Proc *p = &pool->entities[proc].proc;
+ Node *n = &pool->entities[node].node;
// TODO
// Implement large entities.
@@ -575,30 +575,30 @@ void proc_node_remove(pool_t *pool, i64 proc, i64 node) {
n->index_in_proc = UNDEFINED;
}
-i64 unit_init(pool_t *pool, i16 type) {
- return pool_add(pool, (entity_t) {
+i64 unit_init(Pool *pool, i16 type) {
+ return pool_add(pool, (Entity) {
.type = ENTITY_UNIT,
.tail = UNDEFINED,
- .unit = (unit_t) {
+ .unit = (Unit) {
.type = type,
.entry_point_index = UNDEFINED,
}
});
}
-void unit_destroy(pool_t *pool, i64 unit) {
+void unit_destroy(Pool *pool, i64 unit) {
pool_remove(pool, unit, ENTITY_UNIT);
}
-void unit_proc_add(pool_t *pool, i64 unit, i64 proc) {
+void unit_proc_add(Pool *pool, i64 unit, i64 proc) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[unit].is_enabled);
assert(pool->entities[unit].type == ENTITY_UNIT);
assert(pool->entities[proc].is_enabled);
assert(pool->entities[proc].type == ENTITY_PROC);
- unit_t *u = &pool->entities[unit].unit;
- proc_t *p = &pool->entities[proc].proc;
+ Unit *u = &pool->entities[unit].unit;
+ Proc *p = &pool->entities[proc].proc;
assert(p->index_in_unit == UNDEFINED);
@@ -614,14 +614,14 @@ void unit_proc_add(pool_t *pool, i64 unit, i64 proc) {
++u->proc_count;
}
-void unit_proc_remove(pool_t *pool, i64 unit, i64 proc) {
+void unit_proc_remove(Pool *pool, i64 unit, i64 proc) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[unit].is_enabled);
assert(pool->entities[unit].type == ENTITY_UNIT);
assert(pool->entities[proc].type == ENTITY_PROC);
- unit_t *u = &pool->entities[unit].unit;
- proc_t *p = &pool->entities[proc].proc;
+ Unit *u = &pool->entities[unit].unit;
+ Proc *p = &pool->entities[proc].proc;
// TODO
// Implement large entities.
@@ -636,14 +636,14 @@ void unit_proc_remove(pool_t *pool, i64 unit, i64 proc) {
p->index_in_unit = UNDEFINED;
}
-void unit_link_add(pool_t *pool, i64 unit, i64 link_unit) {
+void unit_link_add(Pool *pool, i64 unit, i64 link_unit) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[unit].is_enabled);
assert(pool->entities[unit].type == ENTITY_UNIT);
assert(pool->entities[link_unit].is_enabled);
assert(pool->entities[link_unit].type == ENTITY_UNIT);
- unit_t *u = &pool->entities[unit].unit;
+ Unit *u = &pool->entities[unit].unit;
for (i64 i = 0; i < u->link_count; ++i)
if (u->links[i] == link_unit)
@@ -653,13 +653,13 @@ void unit_link_add(pool_t *pool, i64 unit, i64 link_unit) {
u->links[u->link_count++] = link_unit;
}
-void unit_link_remove(pool_t *pool, i64 unit, i64 link_unit) {
+void unit_link_remove(Pool *pool, i64 unit, i64 link_unit) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[unit].is_enabled);
assert(pool->entities[unit].type == ENTITY_UNIT);
assert(pool->entities[link_unit].type == ENTITY_UNIT);
- unit_t *u = &pool->entities[unit].unit;
+ Unit *u = &pool->entities[unit].unit;
for (i64 i = 0; i < u->link_count; ++i)
if (u->links[i] == link_unit) {
@@ -670,7 +670,7 @@ void unit_link_remove(pool_t *pool, i64 unit, i64 link_unit) {
assert(0);
}
-void unit_set_name(pool_t *pool, i64 unit, i64 name_size, c8 *name) {
+void unit_set_name(Pool *pool, i64 unit, i64 name_size, c8 *name) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[unit].is_enabled);
assert(pool->entities[unit].type == ENTITY_UNIT);
@@ -681,19 +681,19 @@ void unit_set_name(pool_t *pool, i64 unit, i64 name_size, c8 *name) {
assert(name_size <= MAX_NAME_SIZE);
assert(name_size >= 0);
- unit_t *u = &pool->entities[unit].unit;
+ Unit *u = &pool->entities[unit].unit;
u->name_size = name_size;
if (name_size > 0)
memcpy(u->name, name, name_size);
}
-void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc) {
+void unit_set_entry_point(Pool *pool, i64 unit, i64 entry_point_proc) {
assert(pool != NULL && pool->entities != NULL);
assert(pool->entities[unit].is_enabled);
assert(pool->entities[unit].type == ENTITY_UNIT);
- unit_t *u = &pool->entities[unit].unit;
+ Unit *u = &pool->entities[unit].unit;
if (entry_point_proc == UNDEFINED) {
u->entry_point_index = UNDEFINED;
@@ -703,7 +703,7 @@ void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc) {
assert(pool->entities[entry_point_proc].is_enabled);
assert(pool->entities[entry_point_proc].type == ENTITY_PROC);
- proc_t *p = &pool->entities[entry_point_proc].proc;
+ Proc *p = &pool->entities[entry_point_proc].proc;
assert(p->index_in_unit != UNDEFINED);
assert(u->procs[p->index_in_unit] == entry_point_proc);
@@ -717,7 +717,7 @@ void unit_set_entry_point(pool_t *pool, i64 unit, i64 entry_point_proc) {
#include <stdio.h> // TEMP
#include <stdlib.h> // TEMP
-void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_data) {
+void unit_write(Pool *pool, i64 unit, u16 target, i64 io_out, void *io_user_data) {
// Docs
//
// AR https://man.freebsd.org/cgi/man.cgi?query=ar&sektion=5
@@ -733,14 +733,14 @@ void unit_write(pool_t *pool, i64 unit, u16 target, i64 io_out, void *io_user_da
// Read dependencies
//
{
- unit_t *u = &pool->entities[unit].unit;
+ Unit *u = &pool->entities[unit].unit;
for (i64 i = 0; i < u->link_count; ++i) {
if (u->links[i] == UNDEFINED)
continue;
i64 index = u->links[i];
- unit_t *l = &pool->entities[index].unit;
+ Unit *l = &pool->entities[index].unit;
assert(pool->entities[index].is_enabled);
assert(l->type == UNIT_LIBRARY_STATIC);
@@ -1093,14 +1093,14 @@ void io_dispatch(i16 op, i64 *id, i64 *size, void *data, void *user_data) {
// Global state
//
-static pool_t g_pool = {
+static Pool g_pool = {
// Statically allocate a large memory block.
//
// TODO
// Reallocate the memory block when necessary.
//
.capacity = MAX_ENTITY_COUNT,
- .entities = (entity_t[MAX_ENTITY_COUNT]) { 0 },
+ .entities = (Entity[MAX_ENTITY_COUNT]) { 0 },
};
// Handy procedures
@@ -1110,15 +1110,15 @@ i64 n_i64(i64 value) {
return node_data_i64(&g_pool, value);
}
-i64 n_call(i16 convention, i64 target_proc, i64 arg_count, var_t *args) {
+i64 n_call(i16 convention, i64 target_proc, i64 arg_count, Var *args) {
return node_ctrl_call(&g_pool, convention, target_proc, arg_count, args);
}
-i64 n_call_by_name(i16 convention, c8 *name, i64 arg_count, var_t *args) {
+i64 n_call_by_name(i16 convention, c8 *name, i64 arg_count, Var *args) {
return node_ctrl_call_by_name(&g_pool, convention, strlen(name), name, arg_count, args);
}
-i64 n_ret(i64 val_count, var_t *vals) {
+i64 n_ret(i64 val_count, Var *vals) {
return node_ctrl_ret(&g_pool, val_count, vals);
}
@@ -1179,21 +1179,21 @@ void l_static(i64 unit, c8 *static_library) {
//
// ================================================================
-#if !defined(DISABLE_HELPERS) && !defined(DISABLE_EXAMPLE)
+#if !defined(DISABLE_HELPERS) && !defined(DISABLE_TESTING)
int main(int argc, char **argv) {
(void) argc;
(void) argv;
- printf("node - %d bytes\n", (i32) sizeof(node_t));
- printf("proc - %d bytes\n", (i32) sizeof(proc_t));
- printf("unit - %d bytes\n", (i32) sizeof(unit_t));
- printf("entity - %d bytes\n\n", (i32) sizeof(entity_t));
+ printf("node - %d bytes\n", (i32) sizeof(Node));
+ printf("proc - %d bytes\n", (i32) sizeof(Proc));
+ printf("unit - %d bytes\n", (i32) sizeof(Unit));
+ printf("entity - %d bytes\n\n", (i32) sizeof(Entity));
i64 main = p_new("main");
i64 n0 = n_i64(42);
p_add(main, n0);
- p_add(main, n_ret(1, (var_t[]) { {.size = 4, .type = TYPE_I32, .node = n0, } }));
+ p_add(main, n_ret(1, (Var[]) { {.size = 4, .type = TYPE_I32, .node = n0, } }));
i64 u = u_new();
u_add(u, main);