move all stride information into separate CloogStride structure
[cloog.git] / source / input.c
blob2e175b5fe91183c03d6c11cc23258a4fc74ea768
1 #include <ctype.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "../include/cloog/cloog.h"
6 #define ALLOC(type) (type*)malloc(sizeof(type))
8 static char *next_line(FILE *input, char *line, unsigned len)
10 char *p;
12 do {
13 if (!(p = fgets(line, len, input)))
14 return NULL;
15 while (isspace(*p) && *p != '\n')
16 ++p;
17 } while (*p == '#' || *p == '\n');
19 return p;
22 /**
23 * Read input from a .cloog file, putting most of the information
24 * in the returned CloogInput. The chosen language is put in
25 * options->language.
27 CloogInput *cloog_input_read(FILE *file, CloogOptions *options)
29 char line[MAX_STRING];
30 char language;
31 CloogDomain *context;
32 CloogUnionDomain *ud;
33 int nb_par;
35 /* First of all, we read the language to use. */
36 if (!next_line(file, line, sizeof(line)))
37 cloog_die("Input error.\n");
38 if (sscanf(line, "%c", &language) != 1)
39 cloog_die("Input error.\n");
41 if (language == 'f')
42 options->language = LANGUAGE_FORTRAN;
43 else
44 options->language = LANGUAGE_C;
46 /* We then read the context data. */
47 context = cloog_domain_read_context(options->state, file);
48 nb_par = cloog_domain_parameter_dimension(context);
50 ud = cloog_union_domain_read(file, nb_par, options);
52 return cloog_input_alloc(context, ud);
55 /**
56 * Create a CloogInput from a CloogDomain context and a CloogUnionDomain.
58 CloogInput *cloog_input_alloc(CloogDomain *context, CloogUnionDomain *ud)
60 CloogInput *input;
62 input = ALLOC(CloogInput);
63 if (!input)
64 cloog_die("memory overflow.\n");
66 input->context = context;
67 input->ud = ud;
69 return input;
72 void cloog_input_free(CloogInput *input)
74 cloog_domain_free(input->context);
75 cloog_union_domain_free(input->ud);
76 free(input);
79 /**
80 * Dump the .cloog description of a CloogInput and a CloogOptions data structure
81 * into a file. The generated .cloog file will contain the same information as
82 * the data structures. The file can be used to run the cloog program on the
83 * example.
85 void cloog_input_dump_cloog(FILE *file, CloogInput *input, CloogOptions *opt)
87 int i, num_statements;
88 CloogUnionDomain *ud = input->ud;
89 CloogNamedDomainList *ndl = ud->domain;
91 fprintf(file,
92 "# CLooG -> CLooG\n"
93 "# This is an automatic dump of a CLooG input file from a "
94 "CloogInput data\n"
95 "# structure.\n\n");
97 /* Language. */
98 if (opt->language == LANGUAGE_FORTRAN) {
99 fprintf(file, "# Language: FORTRAN\n");
100 fprintf(file, "f\n\n");
101 } else {
102 fprintf(file, "# Language: C\n");
103 fprintf(file, "c\n\n");
106 /* Context. */
107 fprintf(file, "# Context:\n");
108 cloog_domain_print_constraints(file, input->context, 0);
110 fprintf(file, "\n%d # Parameter name(s)\n",
111 ud->name[CLOOG_PARAM] ? 1 : 0);
113 if (ud->name[CLOOG_PARAM])
114 for (i = 0; i < ud->n_name[CLOOG_PARAM]; i++)
115 fprintf(file, "%s ", ud->name[CLOOG_PARAM][i]);
117 /* Statement number. */
118 i = 0;
119 while (ndl != NULL) {
120 i++;
121 ndl = ndl->next;
123 num_statements = i;
124 fprintf(file, "\n# Statement number:\n%d\n\n", num_statements);
126 /* Iteration domains. */
127 i = 1;
128 ndl = ud->domain;
129 while (ndl != NULL) {
130 fprintf(file, "# Iteration domain of statement %d (%s).\n", i,
131 ndl->name);
133 cloog_domain_print_constraints(file, ndl->domain, 1);
134 fprintf(file,"\n0 0 0 # For future options.\n\n");
136 i++;
137 ndl = ndl->next;
140 fprintf(file, "\n%d # Iterator name(s)\n\n",
141 ud->name[CLOOG_ITER] ? 1 : 0);
143 if (ud->name[CLOOG_ITER])
144 for (i = 0; i < ud->n_name[CLOOG_ITER]; i++)
145 fprintf(file, "%s ", ud->name[CLOOG_ITER][i]);
147 /* Exit, if no scattering is supplied. */
148 if (!ud->domain || !ud->domain->scattering) {
149 fprintf(file, "# No scattering functions.\n0\n\n");
150 return;
153 /* Scattering relations. */
154 fprintf(file,
155 "# --------------------- SCATTERING --------------------\n");
157 fprintf(file, "%d # Scattering functions\n", num_statements);
159 i = 1;
160 ndl = ud->domain;
161 while (ndl != NULL) {
162 fprintf(file, "\n# Scattering of statement %d (%s).\n", i,
163 ndl->name);
165 cloog_scattering_print_constraints(file, ndl->scattering, 1);
167 i++;
168 ndl = ndl->next;
171 fprintf(file, "\n%d # Scattering dimension name(s)\n",
172 ud->name[CLOOG_SCAT] ? 1 : 0);
174 if (ud->name[CLOOG_SCAT])
175 for (i = 0; i < ud->n_name[CLOOG_SCAT]; i++)
176 fprintf(file, "%s ", ud->name[CLOOG_SCAT][i]);