Add OpenScop support
[cloog.git] / source / options.c
blob7b42cda40a5ed0ed8ec3c45483bdb1a787524e5b
2 /**-------------------------------------------------------------------**
3 ** CLooG **
4 **-------------------------------------------------------------------**
5 ** options.c **
6 **-------------------------------------------------------------------**
7 ** First version: april 19th 2003 **
8 **-------------------------------------------------------------------**/
11 /******************************************************************************
12 * CLooG : the Chunky Loop Generator (experimental) *
13 ******************************************************************************
14 * *
15 * Copyright (C) 2001-2005 Cedric Bastoul *
16 * *
17 * This library is free software; you can redistribute it and/or *
18 * modify it under the terms of the GNU Lesser General Public *
19 * License as published by the Free Software Foundation; either *
20 * version 2.1 of the License, or (at your option) any later version. *
21 * *
22 * This library is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
25 * Lesser General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU Lesser General Public *
28 * License along with this library; if not, write to the Free Software *
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
30 * Boston, MA 02110-1301 USA *
31 * *
32 * CLooG, the Chunky Loop Generator *
33 * Written by Cedric Bastoul, Cedric.Bastoul@inria.fr *
34 * *
35 ******************************************************************************/
38 #include <stdarg.h>
39 # include <stdlib.h>
40 # include <stdio.h>
41 # include <string.h>
42 # include "../include/cloog/cloog.h"
44 #ifdef OSL_SUPPORT
45 #include <osl/scop.h>
46 #endif
49 /******************************************************************************
50 * Error reporting functions *
51 ******************************************************************************/
53 void cloog_vmsg(CloogOptions *options, enum cloog_msg_type type,
54 const char *msg, va_list ap)
56 const char *type_msg;
58 if (options && options->quiet &&
59 (type == CLOOG_WARNING || type == CLOOG_INFO))
60 return;
62 switch(type) {
63 case CLOOG_WARNING:
64 type_msg = "WARNING";
65 break;
66 case CLOOG_INFO:
67 type_msg = "INFO";
68 break;
69 case CLOOG_ERROR:
70 default:
71 type_msg = "ERROR";
72 break;
74 fprintf(stderr, "[CLooG] %s: ", type_msg);
75 vfprintf(stderr, msg, ap);
78 /**
79 * Print message to stderr.
80 * @param msg printf format string
82 void cloog_msg(CloogOptions *options, enum cloog_msg_type type,
83 const char *msg, ...)
85 va_list args;
87 va_start(args, msg);
88 cloog_vmsg(options, type, msg, args);
89 va_end(args);
92 /**
93 * Print error message to stderr and exit.
94 * @param msg printf format string
96 void cloog_die(const char *msg, ...)
98 va_list args;
100 va_start(args, msg);
101 cloog_vmsg(NULL, CLOOG_ERROR, msg, args);
102 va_end(args);
103 exit(1);
106 /******************************************************************************
107 * Structure display function *
108 ******************************************************************************/
112 * cloog_option_print function:
113 * This function prints the content of a CloogOptions structure (program) into
114 * a file (foo, possibly stdout).
115 * - April 19th 2003: first version.
117 void cloog_options_print(FILE * foo, CloogOptions * options)
118 { fprintf(foo,"Options:\n") ;
119 fprintf(foo,"OPTIONS FOR LOOP GENERATION\n") ;
120 fprintf(foo,"l = %3d,\n",options->l) ;
121 fprintf(foo,"f = %3d,\n",options->f) ;
122 fprintf(foo,"stop = %3d,\n",options->stop) ;
123 fprintf(foo,"strides = %3d,\n",options->strides) ;
124 fprintf(foo,"sh = %3d,\n",options->sh);
125 fprintf(foo,"OPTIONS FOR PRETTY PRINTING\n") ;
126 fprintf(foo,"esp = %3d,\n",options->esp) ;
127 fprintf(foo,"fsp = %3d,\n",options->fsp) ;
128 fprintf(foo,"otl = %3d.\n",options->otl) ;
129 fprintf(foo,"block = %3d.\n",options->block) ;
130 fprintf(foo,"compilable = %3d.\n",options->compilable) ;
131 fprintf(foo,"callable = %3d.\n",options->callable) ;
132 fprintf(foo,"MISC OPTIONS\n") ;
133 fprintf(foo,"name = %3s.\n", options->name);
134 fprintf(foo,"openscop = %3d.\n", options->openscop);
135 fprintf(foo,"UNDOCUMENTED OPTIONS FOR THE AUTHOR ONLY\n") ;
136 fprintf(foo,"leaks = %3d.\n",options->leaks) ;
137 fprintf(foo,"backtrack = %3d.\n",options->backtrack);
138 fprintf(foo,"override = %3d.\n",options->override) ;
139 fprintf(foo,"structure = %3d.\n",options->structure) ;
140 fprintf(foo,"noscalars = %3d.\n",options->noscalars) ;
141 fprintf(foo,"noblocks = %3d.\n",options->noblocks) ;
142 fprintf(foo,"nosimplify = %3d.\n",options->nosimplify) ;
146 /******************************************************************************
147 * Memory deallocation function *
148 ******************************************************************************/
152 * cloog_options_free function:
153 * This function frees the allocated memory for a CloogOptions structure.
154 * - April 19th 2003: first version.
156 void cloog_options_free(CloogOptions *options)
158 free(options);
162 /******************************************************************************
163 * Processing functions *
164 ******************************************************************************/
168 * cloog_options_help function:
169 * This function displays the quick help when the user set the option -help
170 * while calling cloog. Prints are cutted to respect the 509 characters
171 * limitation of the ISO C 89 compilers.
172 * - August 5th 2002: first version.
174 void cloog_options_help()
175 { printf(
176 "Usage: cloog [ options | file ] ...\n"
177 "Options for code generation:\n"
178 " -l <depth> Last loop depth to optimize (-1: infinity)\n"
179 " (default setting: -1).\n"
180 " -f <depth> First loop depth to start loop separation (-1: "
181 "infinity)\n (default setting: 1).\n") ;
182 printf(
183 " -stop <depth> Loop depth to stop code generation (-1: infinity)"
184 "\n (default setting: -1).\n"
185 " -strides <boolean> Handle non-unit strides (1) or not (0)\n"
186 " (default setting: 0).\n"
187 " -first-unroll <depth> First loop dimension to unroll (-1: no unrolling)\n");
188 printf(
189 "\nOptions for pretty printing:\n"
190 " -otl <boolean> Simplify loops running one time (1) or not (0)\n"
191 " (default setting: 1).\n") ;
192 printf(
193 " -esp <boolean> Allow complex equalities spreading (1) or not (0)\n"
194 " (default setting: 0).\n");
195 printf(
196 " -fsp <level> First level to begin the spreading\n"
197 " (default setting: 1).\n"
198 " -block <boolean> Make a new statement block per iterator in C\n"
199 " programs (1) or not (0) (default setting: 0).\n") ;
200 printf(
201 " -compilable <number> Compilable code by using preprocessor (not 0) or"
202 "\n not (0), number being the value of the parameters"
203 "\n (default setting: 0).\n"
204 " -callable <boolean> Testable code by using preprocessor (not 0) or"
205 "\n not (0) (default setting: 0).\n");
206 printf(
207 "\nGeneral options:\n"
208 " -o <output> Name of the output file; 'stdout' is a special\n"
209 " value: when used, output is standard output\n"
210 " (default setting: stdout).\n"
211 #ifdef OSL_SUPPORT
212 " -openscop Input file has OpenScop format.\n"
213 #endif
214 " -v, --version Display the version information (and more).\n"
215 " -q, --quiet Don't print any informational messages.\n"
216 " -h, --help Display this information.\n\n") ;
217 printf(
218 "The special value 'stdin' for 'file' makes CLooG to read data on\n"
219 "standard input.\n\n"
220 "For bug reporting or any suggestions, please send an email to the author\n"
221 "<cedric.bastoul@inria.fr>.\n") ;
226 * cloog_options_version function:
227 * This function displays some version informations when the user set the
228 * option -version while calling cloog. Prints are cutted to respect the 509
229 * characters limitation of the ISO C 89 compilers.
230 * - August 5th 2002: first version.
232 void cloog_options_version()
233 { printf("%s The Chunky Loop Generator\n", cloog_version());
234 printf(
235 "-----\n"
236 "This is a loop generator for scanning Z-polyhedra. It is based on the "
237 "work of\nF. Quillere and C. Bastoul on high level code generation and of "
238 "the PolyLib Team\non polyhedral computation. This program is distributed "
239 "under the terms of the\nGNU Lesser General Public License "
240 "(details at http://www.gnu.org/licenses/lgpl-2.1.html).\n"
241 "-----\n") ;
242 printf(
243 "It would be fair to refer the following paper in any publication "
244 "resulting from\nthe use of this software or its library:\n"
245 "@InProceedings{Bas04,\n"
246 "author = {Cedric Bastoul},\n"
247 "title = {Code Generation in the Polyhedral Model Is Easier Than You "
248 "Think},\n"
249 "booktitle = {PACT'13 IEEE International Conference on Parallel "
250 "Architecture\n and Compilation Techniques},\n"
251 "pages = {7--16},\n"
252 "month = {september},\n"
253 "year = 2004,\n"
254 "address = {Juan-les-Pins}\n"
255 "}\n"
256 "-----\n"
257 "For any information, please ask the author at "
258 "<cedric.bastoul@inria.fr>.\n") ;
263 * cloog_options_set function:
264 * This function sets the value of an option thanks to the user's calling line.
265 * - option is the value to set,
266 * - argc are the elements of the user's calling line,
267 * - number is the number of the element corresponding to the considered option,
268 * this function adds 1 to number to pass away the option value.
270 * - August 5th 2002: first version.
271 * - June 29th 2003: (debug) lack of argument now detected.
273 void cloog_options_set(int * option, int argv, char ** argc, int * number)
274 { char ** endptr ;
276 if (*number+1 >= argv)
277 cloog_die("an option lacks of argument.\n");
279 endptr = NULL ;
280 *option = strtol(argc[*number+1],endptr,10) ;
281 if (endptr != NULL)
282 cloog_die("value '%s' for option '%s' is not valid.\n",
283 argc[*number+1], argc[*number]);
284 *number = *number + 1 ;
289 * cloog_options_malloc function:
290 * This functions allocate the memory space for a CLoogOptions structure and
291 * fill its fields with the defaults values. It returns a pointer to the
292 * allocated CloogOptions structure.
293 * - April 19th 2003: first version.
294 * - November 21th 2005: name changed (before it was cloog_options_init).
296 CloogOptions *cloog_options_malloc(CloogState *state)
297 { CloogOptions * options ;
299 /* Memory allocation for the CloogOptions structure. */
300 options = (CloogOptions *)malloc(sizeof(CloogOptions)) ;
301 if (options == NULL)
302 cloog_die("memory overflow.\n");
304 options->state = state;
306 /* We set the various fields with default values. */
307 /* OPTIONS FOR LOOP GENERATION */
308 options->l = -1 ; /* Last level to optimize: infinity. */
309 options->f = 1 ; /* First level to optimize: the first. */
310 options->stop = -1 ; /* Generate all the code. */
311 options->strides = 0 ; /* Generate a code with unit strides. */
312 options->sh = 0; /* Compute actual convex hull. */
313 options->first_unroll = -1; /* First level to unroll: none. */
314 options->name = "";
315 /* OPTIONS FOR PRETTY PRINTING */
316 options->esp = 1 ; /* We want Equality SPreading.*/
317 options->fsp = 1 ; /* The First level to SPread is the first. */
318 options->otl = 1 ; /* We want to fire One Time Loops. */
319 options->block = 0 ; /* We don't want to force statement blocks. */
320 options->compilable = 0 ; /* No compilable code. */
321 options->callable = 0 ; /* No callable code. */
322 options->quiet = 0; /* Do print informational messages. */
323 options->save_domains = 0; /* Don't save domains. */
324 /* MISC OPTIONS */
325 options->language = CLOOG_LANGUAGE_C; /* The default output language is C. */
326 options->openscop = 0 ; /* The input file has not the OpenScop format.*/
327 /* UNDOCUMENTED OPTIONS FOR THE AUTHOR ONLY */
328 options->leaks = 0 ; /* I don't want to print allocation statistics.*/
329 options->backtrack = 0; /* Perform backtrack in Quillere's algorithm.*/
330 options->override = 0 ; /* I don't want to override CLooG decisions.*/
331 options->structure = 0 ; /* I don't want to print internal structure.*/
332 options->noblocks = 0 ; /* I do want to make statement blocks.*/
333 options->noscalars = 0 ; /* I do want to use scalar dimensions.*/
334 options->nosimplify = 0 ; /* I do want to simplify polyhedra.*/
336 return options ;
342 * cloog_options_read function:
343 * This functions reads all the options and the input/output files thanks
344 * the the user's calling line elements (in argc). It fills a CloogOptions
345 * structure and the FILE structure corresponding to input and output files.
346 * - August 5th 2002: first version.
347 * - April 19th 2003: now in options.c and support of the CloogOptions structure.
349 void cloog_options_read(CloogState *state, int argc, char **argv,
350 FILE **input, FILE **output, CloogOptions **options)
351 { int i, infos=0, input_is_set=0 ;
353 /* CloogOptions structure allocation and initialization. */
354 *options = cloog_options_malloc(state);
356 /* The default output is the standard output. */
357 *output = stdout ;
359 for (i=1;i<argc;i++)
360 if (argv[i][0] == '-')
361 { if (strcmp(argv[i],"-l") == 0)
362 cloog_options_set(&(*options)->l,argc,argv,&i) ;
363 else
364 if (strcmp(argv[i],"-f") == 0)
365 cloog_options_set(&(*options)->f,argc,argv,&i) ;
366 else
367 if (strcmp(argv[i],"-stop") == 0)
368 cloog_options_set(&(*options)->stop,argc,argv,&i) ;
369 else
370 if (strcmp(argv[i],"-strides") == 0)
371 cloog_options_set(&(*options)->strides,argc,argv,&i) ;
372 else if (strcmp(argv[i],"-sh") == 0)
373 cloog_options_set(&(*options)->sh,argc,argv,&i) ;
374 else if (!strcmp(argv[i], "-first-unroll"))
375 cloog_options_set(&(*options)->first_unroll, argc, argv, &i);
376 else
377 if (strcmp(argv[i],"-otl") == 0)
378 cloog_options_set(&(*options)->otl,argc,argv,&i) ;
379 else
380 if (strcmp(argv[i],"-openscop") == 0) {
381 #ifdef OSL_SUPPORT
382 (*options)->openscop = 1 ;
383 #else
384 cloog_die("CLooG has not been compiled with OpenScop support.\n");
385 #endif
387 else
388 if (strcmp(argv[i],"-esp") == 0)
389 cloog_options_set(&(*options)->esp,argc,argv,&i) ;
390 else
391 if (strcmp(argv[i],"-fsp") == 0)
392 cloog_options_set(&(*options)->fsp,argc,argv,&i) ;
393 else
394 if (strcmp(argv[i],"-block") == 0)
395 cloog_options_set(&(*options)->block,argc,argv,&i) ;
396 else
397 if (strcmp(argv[i],"-compilable") == 0)
398 cloog_options_set(&(*options)->compilable, argc, argv, &i);
399 else if (strcmp(argv[i], "-callable") == 0)
400 cloog_options_set(&(*options)->callable, argc, argv, &i);
401 else
402 if (strcmp(argv[i],"-loopo") == 0) /* Special option for the LooPo team ! */
403 { (*options)->esp = 0 ;
404 (*options)->block = 1 ;
406 else
407 if (strcmp(argv[i],"-bipbip") == 0)/* Special option for the author only !*/
408 (*options)->backtrack = 0;
409 else
410 if (strcmp(argv[i],"-leaks") == 0)
411 (*options)->leaks = 1 ;
412 else
413 if (strcmp(argv[i],"-nobacktrack") == 0)
414 (*options)->backtrack = 0;
415 else if (strcmp(argv[i], "-backtrack") == 0)
416 (*options)->backtrack = 1;
417 else
418 if (strcmp(argv[i],"-override") == 0)
419 (*options)->override = 1 ;
420 else
421 if (strcmp(argv[i],"-noblocks") == 0)
422 (*options)->noblocks = 1 ;
423 else
424 if (strcmp(argv[i],"-noscalars") == 0)
425 (*options)->noscalars = 1 ;
426 else
427 if (strcmp(argv[i],"-nosimplify") == 0)
428 (*options)->nosimplify = 1 ;
429 else
430 if ((strcmp(argv[i],"-struct") == 0) || (strcmp(argv[i],"-structure") == 0))
431 (*options)->structure = 1 ;
432 else
433 if ((strcmp(argv[i],"--help") == 0) || (strcmp(argv[i],"-h") == 0))
434 { cloog_options_help() ;
435 infos = 1 ;
437 else
438 if ((strcmp(argv[i],"--version") == 0) || (strcmp(argv[i],"-v") == 0))
439 { cloog_options_version() ;
440 infos = 1 ;
441 } else if ((strcmp(argv[i],"--quiet") == 0) || (strcmp(argv[i],"-q") == 0))
442 (*options)->quiet = 1;
443 else
444 if (strcmp(argv[i],"-o") == 0)
445 { if (i+1 >= argc)
446 cloog_die("no output name for -o option.\n");
448 /* stdout is a special value, when used, we set output to standard
449 * output.
451 if (strcmp(argv[i+1],"stdout") == 0)
452 *output = stdout ;
453 else
454 { *output = fopen(argv[i+1],"w") ;
455 if (*output == NULL)
456 cloog_die("can't create output file %s.\n", argv[i+1]);
458 i ++ ;
460 else
461 cloog_msg(*options, CLOOG_WARNING, "unknown %s option.\n", argv[i]);
463 else
464 { if (!input_is_set)
465 { input_is_set = 1 ;
466 (*options)->name = argv[i] ;
467 /* stdin is a special value, when used, we set input to standard input. */
468 if (strcmp(argv[i],"stdin") == 0)
469 *input = stdin ;
470 else
471 { *input = fopen(argv[i],"r") ;
472 if (*input == NULL)
473 cloog_die("%s file does not exist.\n", argv[i]);
476 else
477 cloog_die("multiple input files.\n");
479 if (!input_is_set)
480 { if (!infos)
481 cloog_die("no input file (-h for help).\n");
482 exit(1) ;
486 #ifdef OSL_SUPPORT
488 * This function extracts CLooG option values from an OpenScop scop and
489 * updates an existing CloogOption structure with those values. If the
490 * options were already set, they are updated without warning.
491 * \param[in] scop Input Scop.
492 * \param[in,out] options CLooG options to be updated.
494 void cloog_options_copy_from_osl_scop(osl_scop_p scop,
495 CloogOptions *options) {
496 if (!options)
497 cloog_die("Options must be provided.\n");
499 if (scop) {
500 /* Extract the language. */
501 if (!strcmp(scop->language, "FORTRAN"))
502 options->language = CLOOG_LANGUAGE_FORTRAN;
503 else
504 options->language = CLOOG_LANGUAGE_C;
507 #endif