From 5c5d036086eaecfed1d4b2f0eecfbe2b88466efd Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Wed, 26 May 2021 16:41:20 -0400 Subject: Added the abillity to have more than one active state. I also figured out how to get libtcc working properly. --- libtcc-test.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/libtcc-test.c b/libtcc-test.c index eae8e97..fddd143 100644 --- a/libtcc-test.c +++ b/libtcc-test.c @@ -1,6 +1,17 @@ #include #include #include +#include +#include +#include + +typedef struct tcc_state tcc_state; + +struct tcc_state { + TCCState *s; + tcc_state *prev; + tcc_state *next; +}; const char *files[] = { "test.c", @@ -8,22 +19,80 @@ const char *files[] = { NULL }; +tcc_state *states = NULL; +tcc_state *last_state = NULL; + +int is_ready = 0; + +void print_sym(void *ctx, const char *name, const void *val) { + printf("symbol: %s, address: %p\n", name, val); + is_ready = 1; +} + +tcc_state *make_state(TCCState *s) { + tcc_state *new_state = malloc(sizeof(tcc_state)); + memset(new_state, 0, sizeof(tcc_state)); + (last_state) ? (last_state->next = new_state) : (states = new_state); + + new_state->s = s; + new_state->prev = (last_state) ? last_state : NULL; + new_state->next = NULL; + + last_state = new_state; + + return new_state; +} + +void free_states(tcc_state *st) { + tcc_state *state; + TCCState *s; + + if (st != NULL) { + state = st; + s = st->s; + st = st->next; + memset(state, 0, sizeof(tcc_state)); + tcc_delete(s); + free(state); + state = NULL; + free_states(st); + } +} + +void cleanup() { + if (states) { + free_states(states); + } +} + int main(int argc, char **argv) { - TCCState *s = tcc_new(); int (*start)(void); - tcc_set_output_type(s, TCC_OUTPUT_MEMORY); + tcc_state *st = make_state(tcc_new()); + tcc_set_output_type(st->s, TCC_OUTPUT_MEMORY); for (int i = 0; files[i]; i++) { - if (tcc_add_file(s, files[i])) { + /*printf("files[%i]: %s\n", i, files[i]);*/ + if (tcc_add_file(st->s, files[i]) < 0) { printf("oof, press f, failed to compile %s.\n", files[i]); + cleanup(); return 1; } } - start = tcc_get_symbol(s, "start"); + tcc_relocate(st->s, TCC_RELOCATE_AUTO); + + /*while (!is_ready) { + tcc_list_symbols(st->s, NULL, &print_sym); + } + is_ready = 0;*/ + + start = tcc_get_symbol(st->s, "start"); + /*printf("start: %p\n", start);*/ int ret = start(); + cleanup(); + return 0; } -- cgit v1.2.3-13-gbd6f