4 #include "../include/cloog/cloog.h"
6 #define ALLOC(type) (type*)malloc(sizeof(type))
7 #define ALLOCN(type,n) (type*)malloc((n)*sizeof(type))
9 void cloog_named_domain_list_free(CloogNamedDomainList
*list
)
11 while (list
!= NULL
) {
12 CloogNamedDomainList
*temp
= list
->next
;
13 cloog_domain_free(list
->domain
);
14 cloog_scattering_free(list
->scattering
);
21 CloogUnionDomain
*cloog_union_domain_alloc(int nb_par
)
25 ud
= ALLOC(CloogUnionDomain
);
27 cloog_die("memory overflow.\n");
30 ud
->next_domain
= &ud
->domain
;
32 ud
->n_name
[CLOOG_PARAM
] = nb_par
;
33 ud
->n_name
[CLOOG_ITER
] = 0;
34 ud
->n_name
[CLOOG_SCAT
] = 0;
36 ud
->name
[CLOOG_PARAM
] = NULL
;
37 ud
->name
[CLOOG_ITER
] = NULL
;
38 ud
->name
[CLOOG_SCAT
] = NULL
;
43 void cloog_union_domain_free(CloogUnionDomain
*ud
)
51 for (i
= 0; i
< 3; ++i
) {
54 for (j
= 0; j
< ud
->n_name
[i
]; ++i
)
59 cloog_named_domain_list_free(ud
->domain
);
65 * Add a domain with scattering function to the union of domains.
66 * name may be NULL and is duplicated if it is not.
67 * domain and scattering are taken over by the CloogUnionDomain.
68 * scattering may be NULL.
70 CloogUnionDomain
*cloog_union_domain_add_domain(CloogUnionDomain
*ud
,
71 const char *name
, CloogDomain
*domain
, CloogScattering
*scattering
,
74 CloogNamedDomainList
*named
;
80 named
= ALLOC(CloogNamedDomainList
);
82 cloog_die("memory overflow.\n");
84 if (ud
->name
[CLOOG_ITER
])
85 cloog_die("iterator names must be set after adding domains.\n");
86 if (ud
->name
[CLOOG_SCAT
])
87 cloog_die("scattering names must be set after adding domains.\n");
89 n
= cloog_domain_dimension(domain
);
90 if (n
> ud
->n_name
[CLOOG_ITER
])
91 ud
->n_name
[CLOOG_ITER
] = n
;
94 n
= cloog_scattering_dimension(scattering
, domain
);
95 if (n
> ud
->n_name
[CLOOG_SCAT
])
96 ud
->n_name
[CLOOG_SCAT
] = n
;
99 named
->domain
= domain
;
100 named
->scattering
= scattering
;
101 named
->name
= name
? strdup(name
) : NULL
;
105 *ud
->next_domain
= named
;
106 ud
->next_domain
= &named
->next
;
112 * Set the name of parameter, iterator or scattering dimension
113 * at the specified position. The name is duplicated.
115 CloogUnionDomain
*cloog_union_domain_set_name(CloogUnionDomain
*ud
,
116 enum cloog_dim_type type
, int index
, const char *name
)
123 if (type
!= CLOOG_PARAM
&&
124 type
!= CLOOG_ITER
&&
126 cloog_die("invalid dim type\n");
128 if (index
< 0 || index
>= ud
->n_name
[type
])
129 cloog_die("index out of range\n");
131 if (!ud
->name
[type
]) {
132 ud
->name
[type
] = ALLOCN(char *, ud
->n_name
[type
]);
134 cloog_die("memory overflow.\n");
135 for (i
= 0; i
< ud
->n_name
[type
]; ++i
)
136 ud
->name
[type
][i
] = NULL
;
139 ud
->name
[type
][index
] = strdup(name
);
140 if (!ud
->name
[type
][index
])
141 cloog_die("memory overflow.\n");
146 static char *next_line(FILE *input
, char *line
, unsigned len
)
151 if (!(p
= fgets(line
, len
, input
)))
153 while (isspace(*p
) && *p
!= '\n')
155 } while (*p
== '#' || *p
== '\n');
161 * cloog_scattering_list_read
162 * Read in a list of scattering functions for the nb_statements
165 static CloogScatteringList
*cloog_scattering_list_read(FILE * foo
,
166 CloogDomain
**domain
, int nb_statements
, int nb_parameters
)
170 CloogScatteringList
*list
= NULL
, **next
= &list
;
172 /* We read first the number of scattering functions in the list. */
174 if (!fgets(s
, MAX_STRING
, foo
))
176 } while ((*s
=='#' || *s
=='\n') || (sscanf(s
, " %d", &nb_scat
) < 1));
181 if (nb_scat
!= nb_statements
)
182 cloog_die("wrong number of scattering functions.\n");
185 *next
= (CloogScatteringList
*)malloc(sizeof(CloogScatteringList
));
186 (*next
)->scatt
= cloog_domain_read_scattering(*domain
, foo
);
187 (*next
)->next
= NULL
;
189 next
= &(*next
)->next
;
195 static CloogUnionDomain
*set_names_from_list(CloogUnionDomain
*ud
,
196 enum cloog_dim_type type
, int n
, char **names
)
203 for (i
= 0; i
< n
; ++i
) {
204 ud
= cloog_union_domain_set_name(ud
, type
, i
, names
[i
]);
213 * Fill up a CloogUnionDomain from information in a CLooG input file.
214 * The language and the context are assumed to have been read from
215 * the input file already.
217 CloogUnionDomain
*cloog_union_domain_read(FILE *file
, int nb_par
,
218 CloogOptions
*options
)
221 char line
[MAX_STRING
];
222 CloogDomain
**domain
;
223 CloogUnionDomain
*ud
;
224 CloogScatteringList
*scatteringl
;
230 ud
= cloog_union_domain_alloc(nb_par
);
232 names
= cloog_names_read_strings(file
, nb_par
);
233 ud
= set_names_from_list(ud
, CLOOG_PARAM
, nb_par
, names
);
235 /* We read the number of statements. */
236 if (!next_line(file
, line
, sizeof(line
)))
237 cloog_die("Input error.\n");
238 if (sscanf(line
, "%d", &n_dom
) != 1)
239 cloog_die("Input error.\n");
241 domain
= ALLOCN(CloogDomain
*, n_dom
);
243 cloog_die("memory overflow.\n");
245 for (i
= 0; i
< n_dom
; ++i
) {
248 domain
[i
] = cloog_domain_union_read(options
->state
, file
,
250 dim
= cloog_domain_dimension(domain
[i
]);
254 /* To read that stupid "0 0 0" line. */
255 if (!next_line(file
, line
, sizeof(line
)))
256 cloog_die("Input error.\n");
257 if (sscanf(line
, " %d %d %d", &op1
, &op2
, &op3
) != 3)
258 cloog_die("Input error.\n");
261 /* Reading of the iterator names. */
262 names
= cloog_names_read_strings(file
, n_iter
);
264 /* Reading and putting the scattering data in program structure. */
265 scatteringl
= cloog_scattering_list_read(file
, domain
, n_dom
, nb_par
);
268 CloogScatteringList
*is
, *next
;
270 if (cloog_scattering_list_lazy_same(scatteringl
))
271 cloog_msg(options
, CLOOG_WARNING
,
272 "some scattering functions are similar.\n");
274 for (i
= 0, is
= scatteringl
; i
< n_dom
; ++i
, is
= next
) {
276 ud
= cloog_union_domain_add_domain(ud
, NULL
, domain
[i
],
281 for (i
= 0; i
< n_dom
; ++i
)
282 ud
= cloog_union_domain_add_domain(ud
, NULL
, domain
[i
],
286 ud
= set_names_from_list(ud
, CLOOG_ITER
, n_iter
, names
);
289 int n_scat
= ud
->n_name
[CLOOG_SCAT
];
290 names
= cloog_names_read_strings(file
, n_scat
);
291 ud
= set_names_from_list(ud
, CLOOG_SCAT
, n_scat
, names
);