diff options
| -rw-r--r-- | libtcc-test.c | 77 | 
1 files 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 <libtcc.h>  #include <stdint.h>  #include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +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;  } | 
