From fd1244107dc1627b1f7b42785e951b833b88f75d Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 28 Apr 2013 14:29:46 +0000 Subject: [PATCH] More librarization --- Makefile | 6 +-- base.h | 13 ------- ftepp.c | 61 +++++++++++++++-------------- gmqcc.h | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 24 ++++++------ 5 files changed, 158 insertions(+), 60 deletions(-) diff --git a/Makefile b/Makefile index 821867f..976305c 100644 --- a/Makefile +++ b/Makefile @@ -40,10 +40,6 @@ else endif endif -ifeq ($(track), no) - CFLAGS += -DNOTRACK -endif - OBJ_D = util.o code.o ast.o ir.o conout.o ftepp.o opts.o fs.o utf8.o correct.o OBJ_P = util.o fs.o conout.o opts.o pak.o OBJ_T = test.o util.o conout.o fs.o @@ -250,6 +246,6 @@ utf8.o: base.h opts.def correct.o: base.h opts.def pak.o: base.h opts.def test.o: base.h opts.def -main.o: base.h opts.def lexer.h +main.o: base.h opts.def lexer.h gmqcc.h lexer.o: base.h opts.def lexer.h parser.o: base.h opts.def lexer.h ast.h ir.h intrin.h diff --git a/base.h b/base.h index 34f994e..88f6591 100644 --- a/base.h +++ b/base.h @@ -995,19 +995,6 @@ bool parser_compile_string(struct parser_s *parser, const char *, co bool parser_finish (struct parser_s *parser, const char *); void parser_cleanup (struct parser_s *parser); -/*===================================================================*/ -/*====================== ftepp.c commandline ========================*/ -/*===================================================================*/ -struct ftepp_s; -struct ftepp_s *ftepp_create (); -bool ftepp_preprocess_file (struct ftepp_s *ftepp, const char *filename); -bool ftepp_preprocess_string(struct ftepp_s *ftepp, const char *name, const char *str); -void ftepp_finish (struct ftepp_s *ftepp); -const char *ftepp_get (struct ftepp_s *ftepp); -void ftepp_flush (struct ftepp_s *ftepp); -void ftepp_add_define (struct ftepp_s *ftepp, const char *source, const char *name); -void ftepp_add_macro (struct ftepp_s *ftepp, const char *name, const char *value); - /*===================================================================*/ /*======================= main.c commandline ========================*/ /*===================================================================*/ diff --git a/ftepp.c b/ftepp.c index 0b51715..cada764 100644 --- a/ftepp.c +++ b/ftepp.c @@ -57,7 +57,7 @@ typedef struct { pptoken **output; } ppmacro; -typedef struct ftepp_s { +typedef struct gmqcc_preprocess_s { lex_file *lex; int token; unsigned int errors; @@ -81,7 +81,7 @@ static uint32_t ftepp_predef_countval = 0; static uint32_t ftepp_predef_randval = 0; /* __DATE__ */ -char *ftepp_predef_date(lex_file *context) { +static char *ftepp_predef_date(lex_file *context) { struct tm *itime = NULL; time_t rtime; char *value = (char*)mem_a(82); @@ -104,7 +104,7 @@ char *ftepp_predef_date(lex_file *context) { } /* __TIME__ */ -char *ftepp_predef_time(lex_file *context) { +static char *ftepp_predef_time(lex_file *context) { struct tm *itime = NULL; time_t rtime; char *value = (char*)mem_a(82); @@ -127,13 +127,13 @@ char *ftepp_predef_time(lex_file *context) { } /* __LINE__ */ -char *ftepp_predef_line(lex_file *context) { +static char *ftepp_predef_line(lex_file *context) { char *value; util_asprintf(&value, "%d", (int)context->line); return value; } /* __FILE__ */ -char *ftepp_predef_file(lex_file *context) { +static char *ftepp_predef_file(lex_file *context) { size_t length = strlen(context->name) + 3; /* two quotes and a terminator */ char *value = (char*)mem_a(length); util_snprintf(value, length, "\"%s\"", context->name); @@ -149,7 +149,7 @@ char *ftepp_predef_counterlast(lex_file *context) { return value; } /* __COUNTER__ */ -char *ftepp_predef_counter(lex_file *context) { +static char *ftepp_predef_counter(lex_file *context) { char *value; ftepp_predef_countval ++; util_asprintf(&value, "%u", ftepp_predef_countval); @@ -158,7 +158,7 @@ char *ftepp_predef_counter(lex_file *context) { return value; } /* __RANDOM__ */ -char *ftepp_predef_random(lex_file *context) { +static char *ftepp_predef_random(lex_file *context) { char *value; ftepp_predef_randval = (util_rand() % 0xFF) + 1; util_asprintf(&value, "%u", ftepp_predef_randval); @@ -167,7 +167,7 @@ char *ftepp_predef_random(lex_file *context) { return value; } /* __RANDOM_LAST__ */ -char *ftepp_predef_randomlast(lex_file *context) { +static char *ftepp_predef_randomlast(lex_file *context) { char *value; util_asprintf(&value, "%u", ftepp_predef_randval); @@ -175,7 +175,7 @@ char *ftepp_predef_randomlast(lex_file *context) { return value; } /* __TIMESTAMP__ */ -char *ftepp_predef_timestamp(lex_file *context) { +static char *ftepp_predef_timestamp(lex_file *context) { struct stat finfo; char *find; char *value; @@ -1779,7 +1779,9 @@ static bool ftepp_preprocess_done(ftepp_t *ftepp) return retval; } -bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename) +void gmqcc_preprocess_adddefine(ftepp_t *ftepp, const char *source, const char *name); + +bool gmqcc_preprocess_file(ftepp_t *ftepp, const char *filename) { ftepp->lex = lex_open(filename); ftepp->itemname = util_strdup(filename); @@ -1792,9 +1794,9 @@ bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename) return ftepp_preprocess_done(ftepp); } -bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str) +bool gmqcc_preprocess_string(ftepp_t *ftepp, const char *name, const char *str) { - ftepp->lex = lex_open_string(str, strlen(str), name); + ftepp->lex = lex_open_string(str, strlen(str), name); ftepp->itemname = util_strdup(name); if (!ftepp->lex) { con_out("failed to create lexer for string \"%s\"\n", name); @@ -1806,12 +1808,12 @@ bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str) } -void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) { +void gmqcc_preprocess_addmacro(ftepp_t *ftepp, const char *name, const char *value) { char *create = NULL; /* use saner path for empty macros */ if (!value) { - ftepp_add_define(ftepp, "__builtin__", name); + gmqcc_preprocess_adddefine(ftepp, "__builtin__", name); return; } @@ -1821,11 +1823,11 @@ void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) { vec_upload(create, value, strlen(value)); vec_push (create, 0); - ftepp_preprocess_string(ftepp, "__builtin__", create); + gmqcc_preprocess_string(ftepp, "__builtin__", create); vec_free (create); } -ftepp_t *ftepp_create() +ftepp_t *gmqcc_preprocess_create(void) { ftepp_t *ftepp; char minor[32]; @@ -1839,9 +1841,9 @@ ftepp_t *ftepp_create() memset(major, 0, sizeof(major)); /* set the right macro based on the selected standard */ - ftepp_add_define(ftepp, NULL, "GMQCC"); + gmqcc_preprocess_adddefine(ftepp, NULL, "GMQCC"); if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) { - ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__"); + gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_FTEQCC__"); /* 1.00 */ major[0] = '"'; major[1] = '1'; @@ -1851,15 +1853,15 @@ ftepp_t *ftepp_create() minor[1] = '0'; minor[2] = '"'; } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) { - ftepp_add_define(ftepp, NULL, "__STD_GMQCC__"); + gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_GMQCC__"); util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR); util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR); } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) { - ftepp_add_define(ftepp, NULL, "__STD_QCCX__"); + gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_QCCX__"); util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR); util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR); } else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) { - ftepp_add_define(ftepp, NULL, "__STD_QCC__"); + gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_QCC__"); /* 1.0 */ major[0] = '"'; major[1] = '1'; @@ -1870,19 +1872,19 @@ ftepp_t *ftepp_create() minor[2] = '"'; } - ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor); - ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major); + gmqcc_preprocess_addmacro(ftepp, "__STD_VERSION_MINOR__", minor); + gmqcc_preprocess_addmacro(ftepp, "__STD_VERSION_MAJOR__", major); /* * We're going to just make __NULL__ nil, which works for 60% of the * cases of __NULL_ for fteqcc. */ - ftepp_add_macro(ftepp, "__NULL__", "nil"); + gmqcc_preprocess_addmacro(ftepp, "__NULL__", "nil"); return ftepp; } -void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name) +void gmqcc_preprocess_adddefine(ftepp_t *ftepp, const char *source, const char *name) { ppmacro *macro; lex_ctx ctx = { "__builtin__", 0 }; @@ -1892,18 +1894,15 @@ void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name) util_htset(ftepp->macros, name, macro); } -const char *ftepp_get(ftepp_t *ftepp) -{ +const char *gmqcc_preprocess_get(ftepp_t *ftepp) { return ftepp->output_string; } -void ftepp_flush(ftepp_t *ftepp) -{ +void gmqcc_preprocess_flush(ftepp_t *ftepp) { ftepp_flush_do(ftepp); } -void ftepp_finish(ftepp_t *ftepp) -{ +void gmqcc_preprocess_destroy(ftepp_t *ftepp) { if (!ftepp) return; ftepp_delete(ftepp); diff --git a/gmqcc.h b/gmqcc.h index 1ac06f0..2a62778 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -27,6 +27,12 @@ extern "C" { #endif /*! __cplusplus */ +/* + * Forward declare everything since these are `private` implementations + * that the user shouldn't have information of. + */ +struct gmqcc_preprocess_s; + /* * Function: gmqcc_global_setmemory * Set implementations for dynamic memory manipulation. @@ -50,6 +56,114 @@ bool gmqcc_global_setmemory ( void (*free_impl) (void *) ); +/* + * Function: gmqcc_preprocess_create + * Creates a preprocessor context + * + * Returns: + * Preprocessor context on success, NULL otherwise + */ +struct gmqcc_preprocess_s *gmqcc_preprocess_create(void); + +/* + * Function: gmqcc_preprocess_file + * Preprocesses a file for a given preprocessor context + * + * Parameters: + * pp - Pointer to a preprocessor context + * filename - Filename as string to be opened and preprocessed + * + * Returns: + * true on success, false otherwise. + */ +bool gmqcc_preprocess_file( + struct gmqcc_preprocess_s *pp, + const char *filename +); + +/* + * Function: gmqcc_preprocess_string + * Preprocesses a string for a given preprocessor context + * + * Parameters: + * pp - Pointer to a preprocessor context + * name - Name for the given string (used in error reporting) + * str - String to be preprocessed + * + * Returns: + * true on success, false otherwise. + */ +bool gmqcc_preprocess_string ( + struct gmqcc_preprocess_s *pp, + const char *name, + const char *str +); + +/* + * Function: gmqcc_preprocess_adddefine + * Defines a macro for a given preprocessor context + * + * Parameters: + * pp - Pointer to a preprocessor context + * ident - Identifier for the given definition (used in error reporting) + * name - Name of macro to define + */ +void gmqcc_preprocess_adddefine( + struct gmqcc_preprocess_s *pp, + const char *ident, + const char *name +); + +/* + * Function: gmqcc_preprocess_addmacro + * Defines a macro with a value for a given preprocessor context + * + * Parameters: + * pp - Pointer to a preprocessor context + * name - Name of the macro to define + * value - Value of the macro + */ +void gmqcc_preprocess_addmacro( + struct gmqcc_preprocess_s *pp, + const char *name, + const char *value +); + +/* + * Function: gmqcc_preprocess_get + * Get preprocessed data as string + * + * Parameters: + * pp - Pointer to preprocessor context + * + * Returns: + * const string of the preprocessed contents. + */ +const char *gmqcc_preprocess_get( + struct gmqcc_preprocess_s *pp +); + +/* + * Function: gmqcc_preprocess_flush + * Flush contents to be preprocessed + * + * Parameters: + * pp - Pointer to preprocessor context + */ +void gmqcc_preprocess_flush( + struct gmqcc_preprocess_s *pp +); + +/* + * Function: gmqcc_preprocess_destroy + * Destroy preprocessor context + * + * Parameters: + * pp - Pointer to preprocessor context + */ +void gmqcc_preprocess_destroy( + struct gmqcc_preprocess_s *pp +); #ifdef __cplusplus } diff --git a/main.c b/main.c index 93caf93..9ea723a 100644 --- a/main.c +++ b/main.c @@ -23,6 +23,7 @@ */ #include "base.h" #include "lexer.h" +#include "gmqcc.h" #include /* TODO: cleanup this whole file .. it's a fuckign mess */ @@ -549,7 +550,8 @@ int main(int argc, char **argv) { bool progs_src = false; FILE *outfile = NULL; struct parser_s *parser = NULL; - struct ftepp_s *ftepp = NULL; + + struct gmqcc_preprocess_s *ftepp = NULL; app_name = argv[0]; con_init (); @@ -632,7 +634,7 @@ int main(int argc, char **argv) { } if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) { - if (!(ftepp = ftepp_create())) { + if (!(ftepp = gmqcc_preprocess_create())) { con_err("failed to initialize parser\n"); retval = 1; goto cleanup; @@ -642,7 +644,7 @@ int main(int argc, char **argv) { /* add macros */ if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) { for (itr = 0; itr < vec_size(ppems); itr++) { - ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value); + gmqcc_preprocess_addmacro(ftepp, ppems[itr].name, ppems[itr].value); mem_d(ppems[itr].name); /* can be null */ @@ -716,30 +718,30 @@ srcdone: if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) { const char *out; - if (!ftepp_preprocess_file(ftepp, items[itr].filename)) { + if (!gmqcc_preprocess_file(ftepp, items[itr].filename)) { retval = 1; goto cleanup; } - out = ftepp_get(ftepp); + out = gmqcc_preprocess_get(ftepp); if (out) fs_file_printf(outfile, "%s", out); - ftepp_flush(ftepp); + gmqcc_preprocess_flush(ftepp); } else { if (OPTS_FLAG(FTEPP)) { const char *data; - if (!ftepp_preprocess_file(ftepp, items[itr].filename)) { + if (!gmqcc_preprocess_file(ftepp, items[itr].filename)) { retval = 1; goto cleanup; } - data = ftepp_get(ftepp); + data = gmqcc_preprocess_get(ftepp); if (vec_size(data)) { if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) { retval = 1; goto cleanup; } } - ftepp_flush(ftepp); + gmqcc_preprocess_flush(ftepp); } else { if (!parser_compile_file(parser, items[itr].filename)) { @@ -755,7 +757,7 @@ srcdone: } } - ftepp_finish(ftepp); + gmqcc_preprocess_destroy(ftepp); ftepp = NULL; if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) { if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) { @@ -778,7 +780,7 @@ srcdone: cleanup: if (ftepp) - ftepp_finish(ftepp); + gmqcc_preprocess_destroy(ftepp); con_close(); vec_free(items); vec_free(ppems); -- 2.39.2