From 43c8c9ee36f661fa9e4895350f80f43be3814b74 Mon Sep 17 00:00:00 2001 From: midipix Date: Tue, 29 Dec 2015 12:43:48 -0500 Subject: driver: generate ast for each unit upon unit context creation. --- src/driver/amgc_driver_ctx.c | 5 +++++ src/driver/amgc_unit_ctx.c | 45 ++++++++++++++++++++++++++++++++++++- src/internal/apimagic_driver_impl.h | 2 ++ 3 files changed, 51 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/driver/amgc_driver_ctx.c b/src/driver/amgc_driver_ctx.c index 35d3899..7bb59a9 100644 --- a/src/driver/amgc_driver_ctx.c +++ b/src/driver/amgc_driver_ctx.c @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -110,6 +111,9 @@ static int amgc_init_cparser(void) init_ast(); init_gen_firm(); + target_set_defaults(); + target_setup(); + return 0; } @@ -163,6 +167,7 @@ int amgc_get_driver_ctx( ctx->ctx.program = program; ctx->ctx.cctx = &ctx->cctx; + ctx->cctx.ccenv = &ctx->ccenv; *pctx = &ctx->ctx; return AMGC_OK; diff --git a/src/driver/amgc_unit_ctx.c b/src/driver/amgc_unit_ctx.c index 0498a68..aed0c65 100644 --- a/src/driver/amgc_unit_ctx.c +++ b/src/driver/amgc_unit_ctx.c @@ -12,6 +12,10 @@ #include #include + +#include +#include + #include #include "apimagic_driver_impl.h" @@ -77,6 +81,29 @@ static FILE * amgc_stdin_to_tmp(const struct amgc_driver_ctx * dctx) return ftmp; } +static bool amgc_cparser_no_op( + compilation_env_t * env, + compilation_unit_t * unit) +{ + return true; +} + +static void amgc_init_cparser_unit(void) +{ + set_default_handlers(); + + /* parse only */ + set_unit_handler(COMPILATION_UNIT_AST, + build_firm_ir,true); + + /* assign no-op handler */ + set_unit_handler(COMPILATION_UNIT_PREPROCESSED_ASSEMBLER, + amgc_cparser_no_op,true); + + set_unit_handler(COMPILATION_UNIT_OBJECT, + amgc_cparser_no_op,true); +} + int amgc_get_unit_ctx( const struct amgc_driver_ctx * dctx, const char * path, @@ -86,6 +113,8 @@ int amgc_get_unit_ctx( FILE * ftmp; int fd; + amgc_init_cparser_unit(); + if (!dctx || !(ctx = calloc(sizeof(*ctx),1))) return -1; @@ -96,7 +125,7 @@ int amgc_get_unit_ctx( else if ((fd = dup(fileno(ftmp))) < 0) return amgc_free_unit_ctx_impl(ctx,-1); else - fclose(ftmp); + ctx->ccunit.input = ftmp; if (amgc_map_input(fd,path,PROT_READ,&ctx->map)) return amgc_free_unit_ctx_impl(ctx,-1); @@ -104,6 +133,19 @@ int amgc_get_unit_ctx( if (fd > 0) close(fd); + /* compilation unit */ + ctx->ccunit.name = path; + ctx->ccunit.original_name = path; + ctx->ccunit.type = COMPILATION_UNIT_C; + ctx->ccunit.standard = STANDARD_C99; + + /* parse, generate ast, generate ir */ + if ((process_unit(ctx->cctx.ccenv,&ctx->ccunit)) == false) { + if (ctx->ccunit.input) + fclose(ctx->ccunit.input); + return amgc_free_unit_ctx_impl(ctx,-1); + } + memcpy(&ctx->cctx,dctx->cctx, sizeof(ctx->cctx)); @@ -112,6 +154,7 @@ int amgc_get_unit_ctx( ctx->uctx.path = &ctx->path; ctx->uctx.map = &ctx->map; ctx->uctx.cctx = &ctx->cctx; + ctx->uctx.ccunit= &ctx->ccunit; *pctx = &ctx->uctx; return 0; diff --git a/src/internal/apimagic_driver_impl.h b/src/internal/apimagic_driver_impl.h index 9054708..ff33a4e 100644 --- a/src/internal/apimagic_driver_impl.h +++ b/src/internal/apimagic_driver_impl.h @@ -15,6 +15,7 @@ enum app_tags { struct amgc_driver_ctx_impl { struct amgc_common_ctx cctx; struct amgc_driver_ctx ctx; + struct compilation_env_t ccenv; int fdtmpin; }; @@ -23,6 +24,7 @@ struct amgc_unit_ctx_impl { struct amgc_input map; struct amgc_common_ctx cctx; struct amgc_unit_ctx uctx; + struct compilation_unit_t ccunit; }; #endif -- cgit v1.2.3