CLooG 0.18.4
[cloog.git] / source / input.c
blob49106cc3d6cd21604f631e95b7a386dfd2e3c3c8
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 return input;
74 #else
75 if (options->openscop) {
76 cloog_die("CLooG has not been compiled with OpenScop support.\n");
78 #endif
80 /* First of all, we read the language to use. */
81 if (!next_line(file, line, sizeof(line)))
82 cloog_die("Input error.\n");
83 if (sscanf(line, "%c", &language) != 1)
84 cloog_die("Input error.\n");
86 if (language == 'f')
87 options->language = CLOOG_LANGUAGE_FORTRAN;
88 else
89 options->language = CLOOG_LANGUAGE_C;
91 /* We then read the context data. */
92 context = cloog_domain_read_context(options->state, file);
93 nb_par = cloog_domain_parameter_dimension(context);
95 ud = cloog_union_domain_read(file, nb_par, options);
97 return cloog_input_alloc(context, ud);
101 * Create a CloogInput from a CloogDomain context and a CloogUnionDomain.
103 CloogInput *cloog_input_alloc(CloogDomain *context, CloogUnionDomain *ud)
105 CloogInput *input;
107 input = ALLOC(CloogInput);
108 if (!input)
109 cloog_die("memory overflow.\n");
111 input->context = context;
112 input->ud = ud;
114 return input;
117 void cloog_input_free(CloogInput *input)
119 cloog_domain_free(input->context);
120 cloog_union_domain_free(input->ud);
121 free(input);
124 static void print_names(FILE *file, CloogUnionDomain *ud,
125 enum cloog_dim_type type, const char *name)
127 int i;
129 fprintf(file, "\n%d # %s name(s)\n", ud->name[type] ? 1 : 0, name);
130 if (!ud->name[type])
131 return;
133 for (i = 0; i < ud->n_name[type]; i++)
134 fprintf(file, "%s ", ud->name[type][i]);
136 fprintf(file, "\n");
140 * Dump the .cloog description of a CloogInput and a CloogOptions data structure
141 * into a file. The generated .cloog file will contain the same information as
142 * the data structures. The file can be used to run the cloog program on the
143 * example.
145 void cloog_input_dump_cloog(FILE *file, CloogInput *input, CloogOptions *opt)
147 int i, num_statements;
148 CloogUnionDomain *ud = input->ud;
149 CloogNamedDomainList *ndl = ud->domain;
151 fprintf(file,
152 "# CLooG -> CLooG\n"
153 "# This is an automatic dump of a CLooG input file from a "
154 "CloogInput data\n"
155 "# structure.\n\n");
157 /* Language. */
158 if (opt->language == CLOOG_LANGUAGE_FORTRAN) {
159 fprintf(file, "# Language: FORTRAN\n");
160 fprintf(file, "f\n\n");
161 } else {
162 fprintf(file, "# Language: C\n");
163 fprintf(file, "c\n\n");
166 /* Context. */
167 fprintf(file, "# Context:\n");
168 cloog_domain_print_constraints(file, input->context, 1);
170 print_names(file, ud, CLOOG_PARAM, "Parameter");
172 /* Statement number. */
173 i = 0;
174 while (ndl != NULL) {
175 i++;
176 ndl = ndl->next;
178 num_statements = i;
179 fprintf(file, "\n# Statement number:\n%d\n\n", num_statements);
181 /* Iteration domains. */
182 i = 1;
183 ndl = ud->domain;
184 while (ndl != NULL) {
185 fprintf(file, "# Iteration domain of statement %d (%s).\n", i,
186 ndl->name);
188 cloog_domain_print_constraints(file, ndl->domain, 1);
189 fprintf(file,"\n0 0 0 # For future options.\n\n");
191 i++;
192 ndl = ndl->next;
195 print_names(file, ud, CLOOG_ITER, "Iterator");
197 /* Exit, if no scattering is supplied. */
198 if (!ud->domain || !ud->domain->scattering) {
199 fprintf(file, "# No scattering functions.\n0\n\n");
200 return;
203 /* Scattering relations. */
204 fprintf(file,
205 "# --------------------- SCATTERING --------------------\n");
207 fprintf(file, "%d # Scattering functions\n", num_statements);
209 i = 1;
210 ndl = ud->domain;
211 while (ndl != NULL) {
212 fprintf(file, "\n# Scattering of statement %d (%s).\n", i,
213 ndl->name);
215 cloog_scattering_print_constraints(file, ndl->scattering);
217 i++;
218 ndl = ndl->next;
221 print_names(file, ud, CLOOG_SCAT, "Scattering dimension");