summaryrefslogtreecommitdiff
path: root/src/driver/amgc_unit_ctx.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/driver/amgc_unit_ctx.c')
-rw-r--r--src/driver/amgc_unit_ctx.c45
1 files changed, 44 insertions, 1 deletions
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;