Skip to content

Instantly share code, notes, and snippets.

@rexim
Created January 21, 2025 18:49
Show Gist options
  • Save rexim/2a6c45f981bfa5e6041f7946e6a70be3 to your computer and use it in GitHub Desktop.
Save rexim/2a6c45f981bfa5e6041f7946e6a70be3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "tb.h"
int main()
{
TB_Arena a = {0};
tb_arena_create(&a, "ABOBA");
TB_Module* m = tb_module_create(TB_ARCH_X86_64, TB_SYSTEM_LINUX, false);
printf("m = %p\n", m);
TB_ModuleSectionHandle text_section = tb_module_get_text(m);
const char *start_name = "_start";
size_t start_name_len = strlen(start_name);
TB_Function* start_f = tb_function_create(m, start_name_len, start_name, TB_LINKAGE_PUBLIC);
printf("start_f = %p\n", start_f);
TB_FunctionPrototype* start_prototype = tb_prototype_create(m, TB_STDCALL, 0, NULL, 0, NULL, false);
tb_function_set_prototype(start_f, text_section, start_prototype);
TB_Node *SYS_exit = tb_inst_sint(start_f, TB_TYPE_I64, 60);
TB_Node *exit_code = tb_inst_sint(start_f, TB_TYPE_I64, 69);
tb_inst_syscall(start_f, TB_TYPE_VOID, SYS_exit, 1, &exit_code);
tb_inst_ret(start_f, 0, NULL);
TB_FeatureSet features = { 0 };
TB_Worklist* ws = tb_worklist_alloc();
TB_FunctionOutput* start_output = tb_codegen(start_f, ws, &a, &features, true);
// TB_Symbol* s = tb_extern_create(m, start_name_len, start_name, TB_EXTERNAL_SO_EXPORT);
// printf("s = %p\n", s);
TB_GraphBuilder* g = tb_builder_enter(start_f, text_section, start_prototype, NULL);
tb_builder_exit(g);
TB_ExportBuffer buffer = tb_module_object_export(m, &a, TB_DEBUGFMT_NONE);
const char *output_path = "b.out";
if (!tb_export_buffer_to_file(buffer, output_path)) {
printf("ERROR: could not export file %s\n", output_path);
return 1;
}
printf("Generated %s\n", output_path);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment