diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2024-07-25 12:10:04 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2024-07-25 12:10:04 +0200 |
commit | afa9010d342c1a2d4036e60a5ce22d9d07ec99d6 (patch) | |
tree | b3414a783f0933f1aaacfa3886d38734c591b9a5 | |
parent | 46189dd08f21ee1c26b656bfc212d442cb8ad469 (diff) | |
download | bxgen-afa9010d342c1a2d4036e60a5ce22d9d07ec99d6.zip |
Simplify helpers API
-rwxr-xr-x | bxgen.c | 131 |
1 files changed, 72 insertions, 59 deletions
@@ -484,12 +484,13 @@ void io_chmod_exe(i64 f, void *user_data); // Helpers API // #ifndef DISABLE_HELPERS -i64 n_str(c8 *value); -i64 n_i64(i64 value); -i64 n_call(i16 convention, i64 target_proc, i64 num_args, Var *args); -i64 n_call_by_name(i16 convention, c8 *name, i64 num_args, Var *args); -i64 n_ret(i64 num_vals, Var *vals); -i64 p_new(c8 *name); +i64 n_str(i64 proc, c8 *value); +i64 n_i64(i64 proc, i64 value); +i64 n_call(i64 proc, i16 convention, i64 target_proc, i64 num_args, Var *args); +i64 n_call_by_name(i64 proc, i16 convention, c8 *name, i64 num_args, Var *args); +i64 n_ret(i64 proc, i64 num_vals, Var *vals); +i64 p_new(i64 unit, c8 *name); +i64 p_new_entry(i64 unit, c8 *name); void p_add(i64 proc, i64 node); i64 u_new(); void u_add(i64 unit, i64 proc); @@ -3112,30 +3113,47 @@ Linker_Context g_linker = { // Handy procedures // -i64 n_str(c8 *value) { +i64 n_str(i64 proc, c8 *value) { i64 len = bx_str_len(value, value + MAX_LITERAL_SIZE); - return node_data_array_c8(&g_pool, len, value); + i64 n = node_data_array_c8(&g_pool, len, value); + p_add(proc, n); + return n; } -i64 n_i64(i64 value) { - return node_data_i64(&g_pool, value); +i64 n_i64(i64 proc, i64 value) { + i64 n = node_data_i64(&g_pool, value); + p_add(proc, n); + return n; } -i64 n_call(i16 convention, i64 target_proc, i64 num_args, Var *args) { - return node_ctrl_call(&g_pool, convention, target_proc, num_args, args); +i64 n_call(i64 proc, i16 convention, i64 target_proc, i64 num_args, Var *args) { + i64 n = node_ctrl_call(&g_pool, convention, target_proc, num_args, args); + p_add(proc, n); + return n; } -i64 n_call_by_name(i16 convention, c8 *name, i64 num_args, Var *args) { - return node_ctrl_call_by_name(&g_pool, convention, bx_str_len(name, name + MAX_NAME_SIZE), name, num_args, args); +i64 n_call_by_name(i64 proc, i16 convention, c8 *name, i64 num_args, Var *args) { + i64 n = node_ctrl_call_by_name(&g_pool, convention, bx_str_len(name, name + MAX_NAME_SIZE), name, num_args, args); + p_add(proc, n); + return n; } -i64 n_ret(i64 num_vals, Var *vals) { - return node_ctrl_ret(&g_pool, num_vals, vals); +i64 n_ret(i64 proc, i64 num_vals, Var *vals) { + i64 n = node_ctrl_ret(&g_pool, num_vals, vals); + p_add(proc, n); + return n; } -i64 p_new(c8 *name) { +i64 p_new(i64 unit, c8 *name) { i64 p = proc_init(&g_pool); proc_set_name(&g_pool, p, bx_str_len(name, name + MAX_NAME_SIZE), name); + u_add(unit, p); + return p; +} + +i64 p_new_entry(i64 unit, c8 *name) { + i64 p = p_new(unit, name); + u_entry_point(unit, p); return p; } @@ -3197,60 +3215,55 @@ int main(int argc, char **argv) { BX_LOG(INFO, "bxgen " BX_VERSION); - // Add the `main` procedure. - i64 mainproc = p_new("main"); - - // Create string literal - i64 n0 = n_str("hello sailor"); - p_add(mainproc, n0); - - // Call printf - p_add(mainproc, - n_call_by_name( - CONV_CDECL, - "printf", - 1, - (Var[]) {{ - .num = 1, - .type = TYPE_PTR, - .node = n0, - }} - )); - - // Create `42` i64 data node and add it into the `main`. - i64 n1 = n_i64(42); - p_add(mainproc, n1); - - // Add a return statement into the `main`. - // The return statement will point at the previously - // created node `42`. - p_add(mainproc, - // Create a ret node. - n_ret( 1, // number of returned values - (Var[]) {{ - // the return value - .num = 1, // number of elements - .type = TYPE_I32, // type - .node = n1, // source to get the value from - }} - )); + // ============================================================ + // + // Create the program // Add a compilation unit. i64 u = u_new(); - // Add the `main` procedure to the compilation unit. - u_add(u, mainproc); + // Add the `main` procedure. + i64 mainproc = p_new_entry(u, "main"); - // Set the `main` as the entry point of the compilation unit. - u_entry_point(u, mainproc); + // Call printf + n_call_by_name( + mainproc, + CONV_CDECL, // calling convention + "printf", // proc name + 1, // number of argument + (Var[]) {{ + // the first argument + .num = 1, + .type = TYPE_PTR, + .node = n_str(mainproc, "hello sailor"), + }} + ); + + // Return 42 + n_ret( + mainproc, + 1, // number of returned values + (Var[]) {{ + // the return value + .num = 1, // number of elements + .type = TYPE_I32, // type + .node = n_i64(mainproc, 42), // source to get the value from + }} + ); + + // ============================================================ + // + // Compile and link - // Link a static library. + // Add a static library. l_static(u, "/lib/x86_64-linux-gnu/libc.a"); // l_static(u, "libtest.a"); // Write the compilation unit into an executable file. u_elf_x86_64(u, "test_foo"); + // ============================================================ + BX_CHECK(HOST == HOST_Linux, "Host system is not compatible", -1); BX_CHECK(HO == LE, "Host data ordering is not compatible", -1); |