source/clast.c: insert_modulo_guard_constraint: remove unused variable
[cloog/bastoul.git] / source / input.c
blob39c611c9ab288ce84319139056a3ab6dd418a1d3
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "../include/cloog/cloog.h"
6 #ifdef OSL_SUPPORT
7 #include <osl/scop.h>
8 #endif
10 #define ALLOC(type) (type*)malloc(sizeof(type))
12 static char *next_line(FILE *input, char *line, unsigned len)
14 char *p;
16 do {
17 if (!(p = fgets(line, len, input)))
18 return NULL;
19 while (isspace(*p) && *p != '\n')
20 ++p;
21 } while (*p == '#' || *p == '\n');
23 return p;
26 #ifdef OSL_SUPPORT
27 /**
28 * This function translates an OpenScop scop to a CLooG input.
29 * \param[in,out] state CLooG state.
30 * \param[in] scop Scop to translate.
31 * \return A CloogInput corresponding to the scop input.
33 CloogInput *cloog_input_from_osl_scop(CloogState *state, osl_scop_p scop) {
34 CloogInput *input = NULL;
35 CloogDomain *context = NULL;
36 CloogUnionDomain *ud = NULL;
38 if (scop) {
39 /* Extract the context. */
40 context = cloog_domain_from_osl_relation(state, scop->context);
42 /* Extract the union of domains. */
43 ud = cloog_union_domain_from_osl_scop(state, scop);
45 /* Build and return the input. */
46 input = cloog_input_alloc(context, ud);
49 return input;
51 #endif
53 /**
54 * Read input from a .cloog file, putting most of the information
55 * in the returned CloogInput. The chosen language is put in
56 * options->language.
58 CloogInput *cloog_input_read(FILE *file, CloogOptions *options)
60 char line[MAX_STRING];
61 char language;
62 CloogDomain *context;
63 CloogUnionDomain *ud;
64 int nb_par;
66 #ifdef OSL_SUPPORT
67 if (options->openscop) {
68 osl_scop_p scop = osl_scop_read(file);
69 CloogInput * input = cloog_input_from_osl_scop(options->state,
70 scop);
71 cloog_options_copy_from_osl_scop(scop, options);
72 osl_scop_free(scop);
73 return input;
75 #endif
77 /* First of all, we read the language to use. */
78 if (!next_line(file, line, sizeof(line)))
79 cloog_die("Input error.\n");
80 if (sscanf(line, "%c", &language) != 1)
81 cloog_die("Input error.\n");
83 if (language == 'f')
84 options->language = CLOOG_LANGUAGE_FORTRAN;
85 else
86 options->language = CLOOG_LANGUAGE_C;
88 /* We then read the context data. */
89 context = cloog_domain_read_context(options->state, file);
90 nb_par = cloog_domain_parameter_dimension(context);
92 ud = cloog_union_domain_read(file, nb_par, options);
94 return cloog_input_alloc(context, ud);
97 /**
98 * Create a CloogInput from a CloogDomain context and a CloogUnionDomain.
100 CloogInput *cloog_input_alloc(CloogDomain *context, CloogUnionDomain *ud)
102 CloogInput *input;
104 input = ALLOC(CloogInput);
105 if (!input)
106 cloog_die("memory overflow.\n");
108 input->context = context;
109 input->ud = ud;
111 return input;
114 void cloog_input_free(CloogInput *input)
116 cloog_domain_free(input->context);
117 cloog_union_domain_free(input->ud);
118 free(input);
121 static void print_names(FILE *file, CloogUnionDomain *ud,
122 enum cloog_dim_type type, const char *name)
124 int i;
126 fprintf(file, "\n%d # %s name(s)\n", ud->name[type] ? 1 : 0, name);
127 if (!ud->name[type])
128 return;
130 for (i = 0; i < ud->n_name[type]; i++)
131 fprintf(file, "%s ", ud->name[type][i]);
133 fprintf(file, "\n");
137 * Dump the .cloog description of a CloogInput and a CloogOptions data structure
138 * into a file. The generated .cloog file will contain the same information as
139 * the data structures. The file can be used to run the cloog program on the
140 * example.
142 void cloog_input_dump_cloog(FILE *file, CloogInput *input, CloogOptions *opt)
144 int i, num_statements;
145 CloogUnionDomain *ud = input->ud;
146 CloogNamedDomainList *ndl = ud->domain;
148 fprintf(file,
149 "# CLooG -> CLooG\n"
150 "# This is an automatic dump of a CLooG input file from a "
151 "CloogInput data\n"
152 "# structure.\n\n");
154 /* Language. */
155 if (opt->language == CLOOG_LANGUAGE_FORTRAN) {
156 fprintf(file, "# Language: FORTRAN\n");
157 fprintf(file, "f\n\n");
158 } else {
159 fprintf(file, "# Language: C\n");
160 fprintf(file, "c\n\n");
163 /* Context. */
164 fprintf(file, "# Context:\n");
165 cloog_domain_print_constraints(file, input->context, 1);
167 print_names(file, ud, CLOOG_PARAM, "Parameter");
169 /* Statement number. */
170 i = 0;
171 while (ndl != NULL) {
172 i++;
173 ndl = ndl->next;
175 num_statements = i;
176 fprintf(file, "\n# Statement number:\n%d\n\n", num_statements);
178 /* Iteration domains. */
179 i = 1;
180 ndl = ud->domain;
181 while (ndl != NULL) {
182 fprintf(file, "# Iteration domain of statement %d (%s).\n", i,
183 ndl->name);
185 cloog_domain_print_constraints(file, ndl->domain, 1);
186 fprintf(file,"\n0 0 0 # For future options.\n\n");
188 i++;
189 ndl = ndl->next;
192 print_names(file, ud, CLOOG_ITER, "Iterator");
194 /* Exit, if no scattering is supplied. */
195 if (!ud->domain || !ud->domain->scattering) {
196 fprintf(file, "# No scattering functions.\n0\n\n");
197 return;
200 /* Scattering relations. */
201 fprintf(file,
202 "# --------------------- SCATTERING --------------------\n");
204 fprintf(file, "%d # Scattering functions\n", num_statements);
206 i = 1;
207 ndl = ud->domain;
208 while (ndl != NULL) {
209 fprintf(file, "\n# Scattering of statement %d (%s).\n", i,
210 ndl->name);
212 cloog_scattering_print_constraints(file, ndl->scattering);
214 i++;
215 ndl = ndl->next;
218 print_names(file, ud, CLOOG_SCAT, "Scattering dimension");