summaryrefslogtreecommitdiffhomepage
path: root/src/driver
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2015-12-29 12:43:48 -0500
committermidipix <writeonce@midipix.org>2016-01-01 22:50:24 -0500
commit43c8c9ee36f661fa9e4895350f80f43be3814b74 (patch)
tree85ce4a5f5194bee047b76e21977a552a29808dc4 /src/driver
parent389acb44d79857eb39b2afaeb8e410f6da93dcfc (diff)
downloadapimagic-43c8c9ee36f661fa9e4895350f80f43be3814b74.tar.bz2
apimagic-43c8c9ee36f661fa9e4895350f80f43be3814b74.tar.xz
driver: generate ast for each unit upon unit context creation.
Diffstat (limited to 'src/driver')
-rw-r--r--src/driver/amgc_driver_ctx.c5
-rw-r--r--src/driver/amgc_unit_ctx.c45
2 files changed, 49 insertions, 1 deletions
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 <cparser/driver/driver.h>
#include <cparser/driver/driver_t.h>
+#include <cparser/driver/target.h>
#include <cparser/driver/tempfile.h>
#include <cparser/firm/ast2firm.h>
#include <cparser/firm/firm_opt.h>
@@ -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 <errno.h>
#include <sys/mman.h>
+
+#include <cparser/driver/driver.h>
+#include <cparser/driver/c_driver.h>
+
#include <apimagic/apimagic.h>
#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;