Add a source-2-source compiler example to the documentation
[cloog.git] / examples / polyhedral_compiler / poc.c
blobf6f67580157c43c278cd2722b2d1dd45ff0ed84e
1 /* poc.c A complete C to polyhedra to C compiler */
3 #include <stdlib.h>
4 #include <osl/osl.h>
5 #include <clan/clan.h>
6 #include <cloog/cloog.h>
8 /* Use the Clan library to convert a SCoP from C to OpenScop */
9 osl_scop_p read_scop_from_c(FILE* input, char* input_name) {
10 clan_options_p clanoptions;
11 osl_scop_p scop;
13 clanoptions = clan_options_malloc();
14 clanoptions->precision = OSL_PRECISION_MP;
15 CLAN_strdup(clanoptions->name, input_name);
16 scop = clan_scop_extract(input, clanoptions);
17 clan_options_free(clanoptions);
18 return scop;
21 /* Use the CLooG library to output a SCoP from OpenScop to C */
22 void print_scop_to_c(FILE* output, osl_scop_p scop) {
23 CloogState* state;
24 CloogOptions* options;
25 CloogInput* input;
26 struct clast_stmt* clast;
28 state = cloog_state_malloc();
29 options = cloog_options_malloc(state);
30 options->openscop = 1;
31 cloog_options_copy_from_osl_scop(scop, options);
32 input = cloog_input_from_osl_scop(options->state, scop);
33 clast = cloog_clast_create_from_input(input, options);
34 clast_pprint(output, clast, 0, options);
36 cloog_clast_free(clast);
37 options->scop = NULL; // don't free the scop
38 cloog_options_free(options);
39 cloog_state_free(state); // the input is freed inside
42 int main(int argc, char* argv[]) {
43 osl_scop_p scop;
44 FILE* input;
46 if ((argc < 2) || (argc > 2)) {
47 fprintf(stderr, "usage: %s file.c\n", argv[0]);
48 exit(0);
51 if (argc == 1)
52 input = stdin;
53 else
54 input = fopen(argv[1], "r");
56 if (input == NULL) {
57 fprintf(stderr, "cannot open input file\n");
58 exit(0);
61 scop = read_scop_from_c(input, argv[1]);
62 osl_scop_print(stdout, scop);
64 // UPDATE THE SCOP IN A SMART WAY HERE
66 print_scop_to_c(stdout, scop);
67 osl_scop_free(scop);
69 fclose(input);
70 return 0;