source: driver/cfa.cc@ ef46abb

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since ef46abb was ef46abb, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Merge branch 'master' into distcc

  • Property mode set to 100644
File size: 19.1 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cfa.cc --
8//
9// Author : Peter A. Buhr
10// Created On : Tue Aug 20 13:44:49 2002
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Aug 10 08:44:15 2019
13// Update Count : 311
14//
15
16#include <iostream>
17#include <cstdio> // perror
18#include <cstdlib> // putenv, exit
19#include <unistd.h> // execvp
20#include <string> // STL version
21#include <string.h> // strcmp
22#include <algorithm> // find
23
24#include <sys/types.h>
25#include <sys/stat.h>
26
27#include "Common/SemanticError.h"
28#include "config.h" // configure info
29
30
31using std::cerr;
32using std::endl;
33using std::string;
34using std::to_string;
35
36
37//#define __DEBUG_H__
38
39
40bool prefix( string arg, string pre ) {
41 return arg.substr( 0, pre.size() ) == pre;
42} // prefix
43
44bool suffix( string arg ) {
45 enum { NumSuffixes = 3 };
46 static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
47 //std::cerr << arg << std::endl;
48 size_t dot = arg.find_last_of( "." );
49 //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
50 if ( dot == string::npos ) return false;
51 const string * end = suffixes + NumSuffixes;
52 return std::find( suffixes, end, arg.substr( dot + 1 ) ) != end;
53} // suffix
54
55
56void shuffle( const char * args[], int S, int E, int N ) {
57 // S & E index 1 passed the end so adjust with -1
58 #ifdef __DEBUG_H__
59 cerr << "shuffle:" << S << " " << E << " " << N << endl;
60 #endif // __DEBUG_H__
61 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
62 #ifdef __DEBUG_H__
63 cerr << "\t" << j << " " << j-N << endl;
64 #endif // __DEBUG_H__
65 args[j] = args[j-N];
66 } // for
67} // shuffle
68
69static inline bool dirExists( const string & path ) {
70 struct stat info;
71 if(stat( path.c_str(), &info ) != 0)
72 return false;
73 else if(info.st_mode & S_IFDIR)
74 return true;
75 else
76 return false;
77} //dirExists
78
79static inline string dir(const string & path) {
80 return path.substr(0, path.find_last_of('/'));
81}
82
83
84#define str(s) #s
85
86int main( int argc, char * argv[] ) {
87 string Version( CFA_VERSION_LONG ); // current version number from CONFIG
88 string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
89
90 string installincdir( CFA_INCDIR ); // fixed location of include files
91 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands when installed
92 string srcdriverdir ( TOP_BUILDDIR "driver"); // fixed location of cc1 and cfa-cpp commands when in tree
93
94 string heading; // banner printed at start of cfa compilation
95 string arg; // current command-line argument during command-line parsing
96 string Bprefix; // path where gcc looks for compiler command steps
97 string langstd; // language standard
98
99 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
100 string compiler_name; // name of C compiler
101
102 bool x_flag = false; // -x flag
103 bool nonoptarg = false; // indicates non-option argument specified
104 bool link = true; // linking as well as compiling
105 bool verbose = false; // -v flag
106 bool quiet = false; // -quiet flag
107 bool debug = true; // -debug flag
108 bool nolib = false; // -nolib flag
109 bool help = false; // -help flag
110 bool CFA_flag = false; // -CFA flag
111 bool cpp_flag = false; // -E or -M flag, preprocessor only
112 bool std_flag = false; // -std= flag
113 bool noincstd_flag = false; // -no-include-stdhdr= flag
114 bool debugging __attribute(( unused )) = false; // -g flag
115 bool m32 = false; // -m32 flag
116 bool m64 = false; // -m64 flag
117 bool intree = false;
118 bool disttree = false;
119
120 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
121 int sargs = 1; // starting location for arguments in args list
122 int nargs = sargs; // number of arguments in args list; 0 => command name
123
124 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
125 int nlibs = 0;
126
127 #ifdef __DEBUG_H__
128 cerr << "CFA:" << endl;
129 for ( int i = 1; i < argc; i += 1 ) {
130 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
131 } // for
132 #endif // __DEBUG_H__
133
134 // process command-line arguments
135
136 args[nargs] = "-x"; // turn off language
137 nargs += 1;
138 args[nargs] = "c";
139 nargs += 1;
140
141 for ( int i = 1; i < argc; i += 1 ) {
142 arg = argv[i]; // convert to string value
143 if ( prefix( arg, "-" ) ) {
144 // pass through arguments
145
146 if ( arg == "-Xlinker" || arg == "-o" ) {
147 args[nargs] = argv[i]; // pass the argument along
148 nargs += 1;
149 i += 1;
150 if ( i == argc ) continue; // next argument available ?
151 args[nargs] = argv[i]; // pass the argument along
152 nargs += 1;
153 } else if ( arg == "-XCFA" ) { // CFA pass through
154 i += 1;
155 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
156 nargs += 1;
157
158 // CFA specific arguments
159
160 } else if ( arg == "-CFA" ) {
161 CFA_flag = true; // strip the -CFA flag
162 link = false;
163 args[nargs] = "-E"; // replace the argument with -E
164 nargs += 1;
165 } else if ( arg == "-debug" ) {
166 debug = true; // strip the debug flag
167 } else if ( arg == "-nodebug" ) {
168 debug = false; // strip the debug flag
169 } else if ( arg == "-nolib" ) {
170 nolib = true; // strip the nodebug flag
171 } else if ( arg == "-quiet" ) {
172 quiet = true; // strip the quiet flag
173 } else if ( arg == "-noquiet" ) {
174 quiet = false; // strip the noquiet flag
175 } else if ( arg == "-help" ) {
176 help = true; // strip the help flag
177 } else if ( arg == "-nohelp" ) {
178 help = false; // strip the nohelp flag
179 } else if ( arg == "-no-include-stdhdr" ) {
180 noincstd_flag = true; // strip the no-include-stdhdr flag
181 } else if ( arg == "-in-tree" ) {
182 intree = true;
183 } else if ( arg == "-dist-tree" ) {
184 disttree = true;
185 } else if ( arg == "-compiler" ) {
186 // use the user specified compiler
187 i += 1;
188 if ( i == argc ) continue; // next argument available ?
189 compiler_path = argv[i];
190 if ( putenv( (char *)( *new string( string( "__CFA_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
191 cerr << argv[0] << " error, cannot set environment variable." << endl;
192 exit( EXIT_FAILURE );
193 } // if
194
195 // C specific arguments
196
197 } else if ( arg == "-v" ) {
198 verbose = true; // verbosity required
199 args[nargs] = argv[i]; // pass the argument along
200 nargs += 1;
201 } else if ( arg == "-g" ) {
202 debugging = true; // symbolic debugging required
203 args[nargs] = argv[i]; // pass the argument along
204 nargs += 1;
205 } else if ( prefix( arg, "-x" ) ) { // file suffix ?
206 string lang;
207 args[nargs] = argv[i]; // pass the argument along
208 nargs += 1;
209 if ( arg.length() == 2 ) { // separate argument ?
210 i += 1;
211 if ( i == argc ) continue; // next argument available ?
212 lang = argv[i];
213 args[nargs] = argv[i]; // pass the argument along
214 nargs += 1;
215 } else {
216 lang = arg.substr( 2 );
217 } // if
218 x_flag = lang != "none";
219 } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
220 std_flag = true; // -std=XX provided
221 args[nargs] = argv[i]; // pass the argument along
222 nargs += 1;
223 } else if ( arg == "-w" ) {
224 args[nargs] = argv[i]; // pass the argument along
225 nargs += 1;
226 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
227 nargs += 1;
228 } else if ( prefix( arg, "-W" ) ) { // check before next tests
229 if ( arg == "-Werror" || arg == "-Wall" ) {
230 args[nargs] = argv[i]; // pass the argument along
231 nargs += 1;
232 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
233 nargs += 1;
234 } else {
235 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
236 args[nargs] = argv[i]; // conditionally pass the argument along
237 const char * warning = argv[i] + adv; // extract warning
238 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
239 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
240 } // if
241 nargs += 1;
242 } // if
243 } else if ( prefix( arg, "-B" ) ) {
244 Bprefix = arg.substr(2); // strip the -B flag
245 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
246 nargs += 1;
247 } else if ( prefix( arg, "-b" ) ) {
248 if ( arg.length() == 2 ) { // separate argument ?
249 i += 1;
250 if ( i == argc ) continue; // next argument available ?
251 arg += argv[i]; // concatenate argument
252 } // if
253 // later versions of gcc require the -b option to appear at the start of the command line
254 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
255 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
256 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
257 cerr << argv[0] << " error, cannot set environment variable." << endl;
258 exit( EXIT_FAILURE );
259 } // if
260 sargs += 1;
261 nargs += 1;
262 } else if ( prefix( arg, "-V" ) ) {
263 if ( arg.length() == 2 ) { // separate argument ?
264 i += 1;
265 if ( i == argc ) continue; // next argument available ?
266 arg += argv[i]; // concatenate argument
267 } // if
268 // later versions of gcc require the -V option to appear at the start of the command line
269 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
270 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
271 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
272 cerr << argv[0] << " error, cannot set environment variable." << endl;
273 exit( EXIT_FAILURE );
274 } // if
275 sargs += 1;
276 nargs += 1;
277 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
278 args[nargs] = argv[i]; // pass the argument along
279 nargs += 1;
280 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
281 cpp_flag = true; // cpp only
282 } // if
283 link = false; // no linkage required
284 } else if ( arg[1] == 'l' ) {
285 // if the user specifies a library, load it after user code
286 libs[nlibs] = argv[i];
287 nlibs += 1;
288 } else if ( arg == "-m32" ) {
289 m32 = true;
290 m64 = false;
291 args[nargs] = argv[i];
292 nargs += 1;
293 } else if ( arg == "-m64" ) {
294 m64 = true;
295 m32 = false;
296 args[nargs] = argv[i];
297 nargs += 1;
298 } else {
299 // concatenate any other arguments
300 args[nargs] = argv[i];
301 nargs += 1;
302 } // if
303 } else {
304 bool cfa = suffix( arg ); // check suffix
305 if ( ! x_flag && cfa ) { // no explicit suffix and cfa suffix ?
306 args[nargs] = "-x";
307 nargs += 1;
308 args[nargs] = "c";
309 nargs += 1;
310 } // if
311 args[nargs] = argv[i]; // concatenate file
312 nargs += 1;
313 if ( ! x_flag && cfa ) { // no explicit suffix and cfa suffix ?
314 args[nargs] = "-x";
315 nargs += 1;
316 args[nargs] = "none";
317 nargs += 1;
318 } // if
319 nonoptarg = true;
320 } // if
321 } // for
322
323 #ifdef __x86_64__
324 args[nargs] = "-mcx16"; // allow double-wide CAA
325 nargs += 1;
326 #endif // __x86_64__
327
328 #ifdef __DEBUG_H__
329 cerr << "args:";
330 for ( int i = 1; i < nargs; i += 1 ) {
331 cerr << " " << args[i];
332 } // for
333 cerr << endl;
334 #endif // __DEBUG_H__
335
336 if ( cpp_flag && CFA_flag ) {
337 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
338 exit( EXIT_FAILURE );
339 } // if
340
341 // add the CFA include-library paths, which allow direct access to header files without directory qualification
342 if( !intree ) {
343 args[nargs] = "-I" CFA_INCDIR;
344 nargs += 1;
345 if ( ! noincstd_flag ) { // do not use during build
346 args[nargs] = "-I" CFA_INCDIR "stdhdr";
347 nargs += 1;
348 } // if
349 args[nargs] = "-I" CFA_INCDIR "concurrency";
350 nargs += 1;
351 args[nargs] = "-I" CFA_INCDIR "containers";
352 nargs += 1;
353 } else {
354 args[nargs] = "-I" TOP_SRCDIR "libcfa/src";
355 nargs += 1;
356 if ( ! noincstd_flag ) { // do not use during build
357 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
358 nargs += 1;
359 } // if
360 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
361 nargs += 1;
362 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
363 nargs += 1;
364 }
365
366 // add stdbool to get defines for bool/true/false
367 args[nargs] = "-imacros";
368 nargs += 1;
369 args[nargs] = "stdbool.h";
370 nargs += 1;
371
372 string libbase;
373 if( !intree ) {
374 libbase = CFA_LIBDIR;
375 } else {
376 libbase = TOP_BUILDDIR "libcfa/";
377 args[nargs] = "-D__CFA_FLAG__=-t";
378 nargs += 1;
379 }
380
381 string arch = m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU);
382 if ( ! m32 && ! m64 ) {
383 if ( arch == "x86" ) {
384 args[nargs] = "-m32";
385 nargs += 1;
386 } else if ( arch == "x64" ) {
387 args[nargs] = "-m64";
388 nargs += 1;
389 } // if
390 } // if
391 const char * config = nolib ? "nolib" : (debug ? "debug": "nodebug");
392 string libdir = libbase + arch + "-" + config;
393
394 if (!disttree) {
395 if ( ! nolib && ! dirExists( libdir ) ) {
396 cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
397 cerr << "Was looking for " << libdir << endl;
398 for(int i = 1; i < argc; i++) {
399 cerr << argv[i] << " ";
400 }
401 cerr << endl;
402 libdir = libbase + arch + "-" + "nolib";
403 } // if
404
405 if ( ! dirExists( libdir ) ) {
406 cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
407 cerr << "Was looking for " << libdir << endl;
408 exit( EXIT_FAILURE );
409 } // if
410 } // if
411
412 if(disttree) {
413 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + dir(argv[0])) ).c_str();
414 } else if(intree) {
415 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir + "/prelude") ).c_str();
416 } else {
417 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir) ).c_str();
418 }
419 nargs += 1;
420
421 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
422 args[nargs] = libs[i];
423 nargs += 1;
424 } // for
425
426 if ( link ) {
427 args[nargs] = "-Xlinker";
428 nargs += 1;
429 args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
430 nargs += 1;
431 args[nargs] = "-Xlinker";
432 nargs += 1;
433 args[nargs] = "--undefined=__cfaabi_interpose_startup";
434 nargs += 1;
435 args[nargs] = "-Xlinker";
436 nargs += 1;
437 args[nargs] = "--undefined=__cfaabi_appready_startup";
438 nargs += 1;
439
440 // include the cfa library in case it's needed
441 args[nargs] = ( *new string( string("-L" ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
442 nargs += 1;
443 args[nargs] = ( *new string( string("-Wl,-rpath," ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
444 nargs += 1;
445 args[nargs] = "-Wl,--push-state,--as-needed";
446 nargs += 1;
447 args[nargs] = "-lcfathread";
448 nargs += 1;
449 args[nargs] = "-Wl,--pop-state";
450 nargs += 1;
451 args[nargs] = "-lcfa";
452 nargs += 1;
453 args[nargs] = "-lpthread";
454 nargs += 1;
455 args[nargs] = "-ldl";
456 nargs += 1;
457 args[nargs] = "-lrt";
458 nargs += 1;
459 args[nargs] = "-lm";
460 nargs += 1;
461 } // if
462
463 // Add exception flags (unconditionally)
464 args[nargs] = "-fexceptions";
465 nargs += 1;
466
467 // add the correct set of flags based on the type of compile this is
468
469 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
470 nargs += 1;
471 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
472 nargs += 1;
473 args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
474 nargs += 1;
475 args[nargs] = "-D__CFA__";
476 nargs += 1;
477 args[nargs] = "-D__CFORALL__";
478 nargs += 1;
479 args[nargs] = "-D__cforall";
480 nargs += 1;
481
482 if ( cpp_flag ) {
483 args[nargs] = "-D__CPP__";
484 nargs += 1;
485 } // if
486
487 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
488 nargs += 1;
489 if ( CFA_flag ) {
490 args[sargs] = "-D__CFA_FLAG__=-N";
491 args[nargs] = "-D__CFA_PREPROCESS_";
492 nargs += 1;
493 } else {
494 args[sargs] = "-D__CFA_FLAG__=-L";
495 } // if
496 sargs += 1;
497
498 if ( debug ) {
499 heading += " (debug)";
500 args[nargs] = "-D__CFA_DEBUG__";
501 nargs += 1;
502 } else {
503 heading += " (no debug)";
504 } // if
505
506 if ( Bprefix.length() == 0 ) {
507 if(disttree) {
508 Bprefix = dir(argv[0]);
509 } else if(intree) {
510 Bprefix = srcdriverdir;
511 } else {
512 Bprefix = installlibdir;
513 }
514
515 if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/';
516 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
517 nargs += 1;
518 } // if
519
520 args[nargs] = "-Xlinker"; // used by backtrace
521 nargs += 1;
522 args[nargs] = "-export-dynamic";
523 nargs += 1;
524
525 // execute the compilation command
526
527 args[0] = compiler_path.c_str(); // set compiler command for exec
528 // find actual name of the compiler independent of the path to it
529 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
530 if ( p == -1 ) {
531 compiler_name = compiler_path;
532 } else {
533 compiler_name = *new string( compiler_path.substr( p + 1 ) );
534 } // if
535
536 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
537 args[nargs] = "-no-integrated-cpp";
538 nargs += 1;
539 args[nargs] = "-Wno-deprecated";
540 nargs += 1;
541#ifdef HAVE_CAST_FUNCTION_TYPE
542 args[nargs] = "-Wno-cast-function-type";
543 nargs += 1;
544#endif // HAVE_CAST_FUNCTION_TYPE
545 if ( ! std_flag ) { // default c11, if none specified
546 args[nargs] = "-std=gnu11";
547 nargs += 1;
548 } // if
549 args[nargs] = "-fgnu89-inline";
550 nargs += 1;
551 args[nargs] = "-D__int8_t_defined"; // prevent gcc type-size attributes
552 nargs += 1;
553 args[nargs] = ( *new string( string("-B") + Bprefix ) ).c_str();
554 nargs += 1;
555 } else {
556 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
557 exit( EXIT_FAILURE );
558 } // if
559
560 args[nargs] = NULL; // terminate with NULL
561
562 #ifdef __DEBUG_H__
563 cerr << "nargs: " << nargs << endl;
564 cerr << "args:" << endl;
565 for ( int i = 0; args[i] != NULL; i += 1 ) {
566 cerr << " \"" << args[i] << "\"" << endl;
567 } // for
568 #endif // __DEBUG_H__
569
570 if ( ! quiet ) {
571 cerr << "CFA " << "Version " << Version << heading << endl;
572 if ( help ) {
573 cerr <<
574 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
575 "-help\t\t\t: print this help message" << endl <<
576 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
577 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
578 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
579 "...\t\t\t: any other " << compiler_name << " flags" << endl;
580 } // if
581 } // if
582
583 if ( verbose ) {
584 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
585
586 for ( int i = 0; args[i] != NULL; i += 1 ) {
587 cerr << args[i] << " ";
588 } // for
589 cerr << endl;
590 } // if
591
592 if ( ! nonoptarg ) {
593 cerr << argv[0] << " error, no input files" << endl;
594 exit( EXIT_FAILURE );
595 } // if
596
597 // execute the command and return the result
598
599 execvp( args[0], (char *const *)args ); // should not return
600 perror( "CFA Translator error: cfa level, execvp" );
601 exit( EXIT_FAILURE );
602} // main
603
604// Local Variables: //
605// tab-width: 4 //
606// mode: c++ //
607// compile-command: "make install" //
608// End: //
Note: See TracBrowser for help on using the repository browser.