Clan  0.8.1
options.c
Go to the documentation of this file.
1 
2  /*+------- <| --------------------------------------------------------**
3  ** A Clan **
4  **--- /.\ -----------------------------------------------------**
5  ** <| [""M# options.c **
6  **- A | # -----------------------------------------------------**
7  ** /.\ [""M# First version: 24/05/2008 **
8  **- [""M# | # U"U#U -----------------------------------------------**
9  | # | # \ .:/
10  | # | #___| #
11  ****** | "--' .-" ******************************************************
12  * |"-"-"-"-"-#-#-## Clan : the Chunky Loop Analyzer (experimental) *
13  **** | # ## ###### *****************************************************
14  * \ .::::'/ *
15  * \ ::::'/ Copyright (C) 2008 University Paris-Sud 11 *
16  * :8a| # # ## *
17  * ::88a ### This is free software; you can redistribute it *
18  * ::::888a 8a ##::. and/or modify it under the terms of the GNU Lesser *
19  * ::::::::888a88a[]::: General Public License as published by the Free *
20  *::8:::::::::SUNDOGa8a::. Software Foundation, either version 2.1 of the *
21  *::::::::8::::888:Y8888:: License, or (at your option) any later version. *
22  *::::':::88::::888::Y88a::::::::::::... *
23  *::'::.. . ..... .. ... . *
24  * This software is distributed in the hope that it will be useful, but *
25  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
26  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
27  * for more details. *
28  * *
29  * You should have received a copy of the GNU Lesser General Public License *
30  * along with software; if not, write to the Free Software Foundation, Inc., *
31  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
32  * *
33  * Clan, the Chunky Loop Analyzer *
34  * Written by Cedric Bastoul, Cedric.Bastoul@u-psud.fr *
35  * *
36  ******************************************************************************/
37 
38 
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <osl/macros.h>
45 #include <clan/macros.h>
46 #include <clan/options.h>
47 
48 
49 /*+****************************************************************************
50  * Structure display function *
51  ******************************************************************************/
52 
53 
61 void clan_options_print(FILE* foo, clan_options_p options) {
62  fprintf(foo, "Options:\n");
63 
64  if (options->name != NULL)
65  fprintf(foo, "name = %s,\n", options->name);
66  else
67  fprintf(foo, "name = NULL,\n");
68 
69  fprintf(foo, "castle = %3d,\n", options->castle);
70  fprintf(foo, "structure = %3d.\n", options->structure);
71  fprintf(foo, "autoscop = %3d.\n", options->autoscop);
72  fprintf(foo, "autopragma = %3d.\n", options->autopragma);
73  fprintf(foo, "autoinsert = %3d.\n", options->autoinsert);
74  fprintf(foo, "inputscop = %3d.\n", options->inputscop);
75  fprintf(foo, "bounded_context = %3d.\n", options->bounded_context);
76  fprintf(foo, "noloopcontext = %3d.\n", options->noloopcontext);
77  fprintf(foo, "nosimplify = %3d.\n", options->nosimplify);
78  fprintf(foo, "extbody = %3d.\n", options->extbody);
79 }
80 
81 
82 /*+****************************************************************************
83  * Memory deallocation function *
84  ******************************************************************************/
85 
86 
93  free(options->name);
94  free(options);
95 }
96 
97 
98 /*+****************************************************************************
99  * Processing functions *
100  ******************************************************************************/
101 
102 
110  printf(
111  "Usage: clan [ options | file ] ...\n");
112  printf(
113  "\nGeneral options:\n"
114  " -o <output> Name of the output file; 'stdout' is a special\n"
115  " value: when used, output is standard output\n"
116  " (default setting: stdout).\n"
117  " -autoscop Automatic SCoP extraction.\n"
118  " -autopragma Automatic insertion of SCoP pragmas in input code.\n"
119  " -autoinsert Automatic insertion of SCoP pragmas in input file.\n"
120  " -inputscop Read a .scop as the input.\n"
121  " -precision <value> 32 to work in 32 bits, 64 for 64 bits, 0 for GMP.\n"
122  " -boundedctxt Bound all global parameters to be >= -1.\n"
123  " -noloopctxt Do not include loop context (simplifies domains).\n"
124  " -nosimplify Do not simplify iteration domains.\n"
125  " -outscoplib Print to the SCoPLib format.\n"
126  " -extbody Will generate the extbody.\n"
127  " -v, --version Display the release information (and more).\n"
128  " -h, --help Display this information.\n\n");
129  printf(
130  "The special value 'stdin' for 'file' or the special option '-' makes clan\n"
131  "to read data on standard input.\n\n"
132  "For bug reporting or any suggestions, please send an email to the author\n"
133  "Cedric Bastoul <cedric.bastoul@inria.fr>.\n");
134 }
135 
136 
144  printf("clan %s ", CLAN_VERSION);
145  printf(" The Chunky Loop Analyzer\n");
146 
147  printf(
148  "-----\n"
149  "This is a polyhedral representation extractor for imperative programs using "
150  "a C\ngrammar for control flow and array accesses (this includes C, C++,"
151  " Java, C#\nand probably few toy languages too). This program is distributed "
152  "under the\nterms of the GNU Lesser General Public License, see details of "
153  "the licence at\nhttp://www.gnu.org/copyleft/lgpl.html\n"
154  "-----\n");
155  printf(
156  "It would be fair to refer the following paper in any publication "
157  "resulting from\nthe use of this software or its library (it defines SCoPs):\n"
158  "@InProceedings{Bas03,\n"
159  "author = {Cedric Bastoul and Albert Cohen and Sylvain Girbal and\n"
160  " Saurabh Sharma and Olivier Temam},\n"
161  "title = {Putting Polyhedral Loop Transformations to Work},\n"
162  "booktitle = {LCPC'16 International Workshop on Languages and\n"
163  " Compilers for Parallel Computers, LNCS 2958},\n"
164  "pages = {209--225},\n"
165  "month = {october},\n"
166  "year = 2003,\n"
167  "address = {College Station, Texas}\n"
168  "}\n"
169  "-----\n");
170  printf(
171  "For any information, please send an email to the author\n"
172  "Cedric Bastoul <cedric.bastoul@inria.fr>.\n");
173 }
174 
175 
185 void clan_options_set(int* option, int argv, char** argc, int* number) {
186  char** endptr;
187 
188  if (*number+1 >= argv)
189  CLAN_error("an option lacks of argument");
190 
191  endptr = NULL;
192  *option = strtol(argc[*number+1], endptr, 10);
193  if (endptr != NULL) {
194  fprintf(stderr, "[Clan] Error: %s value for %s option is not valid.\n",
195  argc[*number + 1], argc[*number]);
196  exit(1);
197  }
198  *number = *number + 1;
199 }
200 
201 
209  clan_options_p options;
210 
211  // Memory allocation for the clan_options_t structure.
212  CLAN_malloc(options, clan_options_p, sizeof(clan_options_t));
213 
214  // We set the various fields with default values.
215  options->name = NULL; // Name of the input file is not set.
216  options->castle = 1; // Do print the Clan McCloog castle in output.
217  options->structure = 0; // Don't print internal structure.
218  options->autoscop = 0; // Do not extract SCoPs automatically.
219  options->autopragma = 0; // Do not insert SCoP pragmas in the input code.
220  options->autoinsert = 0; // Do not insert SCoP pragmas in the input file.
221  options->inputscop = 0; // Default input is a source file, not a .scop.
222  options->precision = 64; // Work in 64 bits by default.
223  options->bounded_context = 0;// Don't bound the global parameters.
224  options->noloopcontext = 0;// Do include loop context in domains.
225  options->nosimplify = 0;// Do simplify iteration domains.
226  options->outscoplib = 0;// Default OpenScop format
227  options->extbody = 0;// Don't generate the extbody
228  return options;
229 }
230 
231 
243 clan_options_p clan_options_read(int argv, char** argc,
244  char*** input_files, FILE** output) {
245  int i, infos=0, input_is_stdin=0;
246  clan_options_p options;
247  int nb_input_files = 0;
248 
249  // clan_options_t structure allocation and initialization.
250  options = clan_options_malloc();
251 
252  // The default output is the standard output.
253  *output = stdout;
254  *input_files = NULL;
255 
256  // Prepare an empty array of input file names.
257  CLAN_malloc(*input_files, char**, sizeof(char*));
258  (*input_files)[0] = NULL;
259 
260  for (i=1; i < argv; i++) {
261  if (argc[i][0] == '-') {
262  if (argc[i][1] == '\0') {
263  // "-" alone is a special option to set input to standard input.
264  nb_input_files++;
265  input_is_stdin = 1;
266  CLAN_realloc(*input_files, char**, sizeof(char*) * (nb_input_files+1));
267  CLAN_strdup((*input_files)[nb_input_files-1], "stdin");
268  (*input_files)[nb_input_files] = NULL;
269  nb_input_files++;
270  } else if (strcmp(argc[i], "-castle") == 0) {
271  clan_options_set(&(options)->castle, argv, argc, &i);
272  } else if (strcmp(argc[i], "-structure") == 0) {
273  options->structure = 1;
274  } else if (strcmp(argc[i], "-autoscop") == 0) {
275  options->autoscop = 1;
276  } else if (strcmp(argc[i], "-autopragma") == 0) {
277  options->autoscop = 1;
278  options->autopragma = 1;
279  } else if (strcmp(argc[i], "-autoinsert") == 0) {
280  options->autoscop = 1;
281  options->autoinsert = 1;
282  } else if (strcmp(argc[i], "-inputscop") == 0) {
283  options->inputscop = 1;
284  } else if (strcmp(argc[i], "-boundedctxt") == 0) {
285  options->bounded_context = 1;
286  } else if (strcmp(argc[i], "-noloopctxt") == 0) {
287  options->noloopcontext = 1;
288  } else if (strcmp(argc[i], "-nosimplify") == 0) {
289  options->nosimplify = 1;
290  } else if (strcmp(argc[i], "-outscoplib") == 0) {
291  options->outscoplib = 1;
292  } else if (strcmp(argc[i], "-extbody") == 0) {
293  options->extbody = 1;
294  } else if (strcmp(argc[i], "-precision") == 0) {
295  clan_options_set(&(options)->precision, argv, argc, &i);
296  } else if ((strcmp(argc[i], "--help") == 0) ||
297  (strcmp(argc[i], "-h") == 0)) {
299  infos = 1;
300  } else if ((strcmp(argc[i],"--version") == 0) ||
301  (strcmp(argc[i],"-v") == 0)) {
303  infos = 1;
304  } else if (strcmp(argc[i], "-o") == 0) {
305  if (i+1 >= argv)
306  CLAN_error("no output name for -o option");
307 
308  // stdout is a special value to set output to standard output.
309  if (strcmp(argc[i+1], "stdout") == 0) {
310  *output = stdout;
311  } else {
312  *output = fopen(argc[i+1], "w");
313  if (*output == NULL)
314  CLAN_error("cannot open the output file");
315  }
316  i++;
317  } else {
318  fprintf(stderr, "[Clan] Warning: unknown %s option.\n", argc[i]);
319  }
320  } else {
321  if (!input_is_stdin) {
322  nb_input_files++;
323  CLAN_realloc(*input_files, char**, sizeof(char*) * (nb_input_files+1));
324  CLAN_strdup((*input_files)[nb_input_files-1], argc[i]);
325  (*input_files)[nb_input_files] = NULL;
326  // stdin is a special value to set input to standard input.
327  if (strcmp(argc[i], "stdin") == 0) {
328  input_is_stdin = 1;
329  }
330  }
331  else {
332  CLAN_error("Cannot have multiple input files with stdin");
333  }
334  }
335  }
336 
337  if ((options->precision != OSL_PRECISION_MP) &&
338  (options->precision != OSL_PRECISION_SP) &&
339  (options->precision != OSL_PRECISION_DP))
340  CLAN_error("invalid precision (use 32, 64 or 0 for GMP)");
341 
342  if ((options->autoscop || options->autopragma || options->autoinsert) &&
343  !nb_input_files )
344  CLAN_error("autoscop/autopragma/autoinsert options need an input file");
345 
346  if (!input_is_stdin && !nb_input_files && !infos)
347  CLAN_error("no input file (-h for help)");
348 
349  return options;
350 }
351 
352 const char* clan_options_autopragma_file(void) {
353  /*
354  * This function aims to "improve" the macro CLAN_AUTOPRAGMA_FILE...
355  * The function returns a pointer to a static char array! This is dirty but
356  * the previous implementation of the macro expanded to a literal string.
357  * At least now, two processes of CLAN can run at the same time.
358  */
359  static char clan_autopragma_filename[128] = { 0 };
360  if (!clan_autopragma_filename[0])
361  {
362  strcpy(clan_autopragma_filename, "/tmp/clan_autopragmaXXXXXX");
363  int fd = mkstemp(clan_autopragma_filename);
364  if (fd == -1)
365  CLAN_error("mkstemp");
366  if (unlink(clan_autopragma_filename) == -1)
367  CLAN_error("unlink");
368  if (close(fd) == -1)
369  CLAN_error("close");
370  }
371 
372  return clan_autopragma_filename;
373 }
int castle
Definition: options.h:57
int autopragma
Definition: options.h:60
int bounded_context
Definition: options.h:66
int nosimplify
Definition: options.h:68
int outscoplib
Definition: options.h:70
void clan_options_version()
Definition: options.c:143
char * name
Definition: options.h:56
clan_options_p clan_options_read(int argv, char **argc, char ***input_files, FILE **output)
Definition: options.c:243
int autoinsert
Definition: options.h:62
int autoscop
Definition: options.h:59
void clan_options_free(clan_options_p options)
Definition: options.c:92
int extbody
Definition: options.h:72
int noloopcontext
Definition: options.h:67
const char * clan_options_autopragma_file(void)
Definition: options.c:352
clan_options_p clan_options_malloc(void)
Definition: options.c:208
void clan_options_set(int *option, int argv, char **argc, int *number)
Definition: options.c:185
void clan_options_print(FILE *foo, clan_options_p options)
Definition: options.c:61
int inputscop
Definition: options.h:63
void clan_options_help()
Definition: options.c:109
int precision
Definition: options.h:65
int structure
Definition: options.h:58