From 9596c2d168eaf32718584ffc3f493cbffbd5df60 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Thu, 25 Jul 2013 14:46:30 +0200 Subject: [PATCH] add pet_scop_print_original Signed-off-by: Sven Verdoolaege --- include/pet.h | 5 +++++ pet.cc | 5 +++++ scop.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- scop.h | 1 + 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/include/pet.h b/include/pet.h index 1ae3a49..72c0128 100644 --- a/include/pet.h +++ b/include/pet.h @@ -274,6 +274,11 @@ struct pet_scop *pet_scop_extract_from_C_source(isl_ctx *ctx, int pet_transform_C_source(isl_ctx *ctx, const char *input, FILE *output, __isl_give isl_printer *(*transform)(__isl_take isl_printer *p, struct pet_scop *scop, void *user), void *user); +/* Given a scop and a printer passed to a pet_transform_C_source callback, + * print the original corresponding code to the printer. + */ +__isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop, + __isl_take isl_printer *p); /* Update all isl_sets and isl_maps such that they all have the same * parameters in the same order. diff --git a/pet.cc b/pet.cc index 6925804..825cb12 100644 --- a/pet.cc +++ b/pet.cc @@ -923,6 +923,10 @@ struct pet_transform_data { * a call to data->transform. * Finally, we keep track of the end of "scop" so that we can * continue copying when we find the next scop. + * + * Before calling data->transform, we store a pointer to the original + * input file in the extended scop in case the user wants to call + * pet_scop_print_original from the callback. */ static int pet_transform(struct pet_scop *scop, void *user) { @@ -931,6 +935,7 @@ static int pet_transform(struct pet_scop *scop, void *user) if (copy(data->in, data->out, data->end, scop->start) < 0) goto error; data->end = scop->end; + scop = pet_scop_set_input_file(scop, data->in); data->p = data->transform(data->p, scop, data->user); if (!data->p) return -1; diff --git a/scop.c b/scop.c index 5468d2d..b559363 100644 --- a/scop.c +++ b/scop.c @@ -37,6 +37,7 @@ #include #include "scop.h" +#include "print.h" #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array)) @@ -74,7 +75,7 @@ static char *op_str[] = { [pet_op_kill] = "kill" }; -/* pet_scop with extra information that is only used during parsing. +/* pet_scop with extra information that is used during parsing and printing. * * In particular, we keep track of conditions under which we want * to skip the rest of the current loop iteration (skip[pet_skip_now]) @@ -90,11 +91,15 @@ static char *op_str[] = { * * A missing condition (skip[type] == NULL) means that we don't want * to skip anything. + * + * Additionally, we keep track of the original input file + * inside pet_transform_C_source. */ struct pet_scop_ext { struct pet_scop scop; isl_multi_pw_aff *skip[2]; + FILE *input; }; const char *pet_op_str(enum pet_op_type op) @@ -4027,3 +4032,48 @@ int pet_scop_has_data_dependent_conditions(struct pet_scop *scop) return 0; } + +/* Keep track of the "input" file inside the (extended) "scop". + */ +struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input) +{ + struct pet_scop_ext *ext = (struct pet_scop_ext *) scop; + + if (!scop) + return NULL; + + ext->input = input; + + return scop; +} + +/* Print the original code corresponding to "scop" to printer "p". + * + * pet_scop_print_original can only be called from + * a pet_transform_C_source callback. This means that the input + * file is stored in the extended scop and that the printer prints + * to a file. + */ +__isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop, + __isl_take isl_printer *p) +{ + struct pet_scop_ext *ext = (struct pet_scop_ext *) scop; + FILE *output; + + if (!scop || !p) + return isl_printer_free(p); + + if (!ext->input) + isl_die(isl_printer_get_ctx(p), isl_error_invalid, + "no input file stored in scop", + return isl_printer_free(p)); + + output = isl_printer_get_file(p); + if (!output) + return isl_printer_free(p); + + if (copy(ext->input, output, scop->start, scop->end) < 0) + return isl_printer_free(p); + + return p; +} diff --git a/scop.h b/scop.h index 196c2c4..f343df3 100644 --- a/scop.h +++ b/scop.h @@ -125,6 +125,7 @@ struct pet_scop *pet_scop_add_array(struct pet_scop *scop, struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop, unsigned start, unsigned end); +struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input); #if defined(__cplusplus) } -- 2.11.4.GIT