source: driver/cfa.cc@ eb8d791

ADT ast-experimental
Last change on this file since eb8d791 was 372b6d3, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

add -invariant/--invariant flag to enable invariant checking during AST passes

  • Property mode set to 100644
File size: 19.7 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 : Mon Apr 10 21:16:00 2023
13// Update Count : 476
14//
15
16#include <iostream>
17#include <cstdio> // perror
18#include <cstdlib> // putenv, exit
19#include <climits> // PATH_MAX
20#include <string> // STL version
21#include <algorithm> // find
22
23#include <unistd.h> // execvp
24#include <sys/types.h>
25#include <sys/stat.h>
26
27#include "Common/SemanticError.h"
28#include "config.h" // configure info
29
30using std::cerr;
31using std::endl;
32using std::string;
33using std::to_string;
34
35//#define __DEBUG_H__
36
37#define xstr(s) str(s)
38#define str(s) #s
39
40static string __CFA_FLAGPREFIX__( "__CFA_FLAG" ); // "__CFA_FLAG__=" suffix
41
42static void Putenv( char * argv[], string arg ) {
43 // environment variables must have unique names
44 static int flags = 0;
45
46 if ( putenv( (char *)( *new string( string( __CFA_FLAGPREFIX__ + to_string( flags++ ) + "__=" ) + arg ) ).c_str() ) ) {
47 cerr << argv[0] << " error, cannot set environment variable." << endl;
48 exit( EXIT_FAILURE );
49 } // if
50} // Putenv
51
52static bool prefix( const string & arg, const string & pre ) { // check if string has prefix
53 return arg.substr( 0, pre.size() ) == pre;
54} // prefix
55
56// check if string has suffix
57static bool suffix( const string & arg ) {
58 enum { NumSuffixes = 3 };
59 static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
60
61 size_t dot = arg.find_last_of( "." );
62 if ( dot == string::npos ) return false;
63 const string * end = suffixes + NumSuffixes;
64 return std::find( suffixes, end, arg.substr( dot + 1 ) ) != end;
65} // suffix
66
67static inline bool dirExists( const string & path ) { // check if directory exists
68 struct stat info;
69 if ( stat( path.c_str(), &info ) != 0 ) return false;
70 return (info.st_mode & S_IFDIR) != 0;
71} // dirExists
72
73static inline string dir(const string & path) {
74 return path.substr(0, path.find_last_of('/'));
75} // dir
76
77// Different path modes
78enum PathMode {
79 Installed, // cfa is installed, use prefix
80 BuildTree, // cfa is in the tree, use source and build tree
81 Distributed // cfa is distributed, use build tree for includes and executable directory for .cfs
82};
83
84// Get path mode from /proc
85PathMode FromProc() {
86 std::string abspath;
87 abspath.resize(PATH_MAX);
88
89 // get executable path from /proc/self/exe
90 ssize_t size = readlink("/proc/self/exe", const_cast<char*>(abspath.c_str()), abspath.size());
91 if ( size <= 0 ) {
92 std::cerr << "Error could not evaluate absolute path from /proc/self/exe" << std::endl;
93 std::cerr << "Failed with " << std::strerror(errno) << std::endl;
94 std::exit(1);
95 } // if
96
97 // Trim extra characters
98 abspath.resize(size);
99
100 // Are we installed
101 if ( abspath.rfind(CFA_BINDIR, 0) == 0 ) { return Installed; }
102
103 // Is this the build tree
104 if ( abspath.rfind(TOP_BUILDDIR, 0 ) == 0 ) { return BuildTree; }
105
106 // Does this look like distcc
107 if ( abspath.find( "/.cfadistcc/" ) != std::string::npos ) { return Distributed; }
108
109 // None of the above? Give up since we don't know where the prelude or include directories are
110 std::cerr << "Cannot find required files from excutable path " << abspath << std::endl;
111 std::exit(1);
112}
113
114
115int main( int argc, char * argv[] ) {
116 string Version( CFA_VERSION_LONG ); // current version number from CONFIG
117 string Major( xstr( CFA_VERSION_MAJOR ) ), Minor( xstr( CFA_VERSION_MINOR ) ), Patch( xstr( CFA_VERSION_PATCH ) );
118
119 string installincdir( CFA_INCDIR ); // fixed location of include files
120 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands when installed
121 string srcdriverdir ( TOP_BUILDDIR "driver"); // fixed location of cc1 and cfa-cpp commands when in tree
122
123 string heading; // banner printed at start of cfa compilation
124 string arg; // current command-line argument during command-line parsing
125 string bprefix; // path where gcc looks for compiler steps
126 string langstd; // language standard
127
128 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
129 string compiler_name; // name of C compiler
130
131 bool x_flag = false; // -x flag
132 bool nonoptarg = false; // no non-option arguments specified, i.e., no file names
133 bool link = true; // link stage occurring
134 bool verbose = false; // -v flag
135 bool quiet = false; // -quiet flag
136 bool debug = true; // -debug flag
137 bool nolib = false; // -nolib flag
138 bool help = false; // -help flag
139 bool CFA_flag = false; // -CFA flag
140 bool cpp_flag = false; // -E or -M flag, preprocessor only
141 bool std_flag = false; // -std= flag
142 bool noincstd_flag = false; // -no-include-stdhdr= flag
143 bool debugging __attribute(( unused )) = false; // -g flag
144 bool m32 = false; // -m32 flag
145 bool m64 = false; // -m64 flag
146 bool compiling_libs = false;
147 int o_file = 0; // -o filename position
148
149 PathMode path = FromProc();
150
151 const char * args[argc + 100]; // cfa command line values, plus some space for additional flags
152 int sargs = 1; // starting location for arguments in args list
153 int nargs = sargs; // number of arguments in args list; 0 => command name
154
155 const char * libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
156 int nlibs = 0;
157
158 #ifdef __DEBUG_H__
159 cerr << "CFA:" << endl;
160 for ( int i = 1; i < argc; i += 1 ) {
161 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
162 } // for
163 #endif // __DEBUG_H__
164
165 // process command-line arguments
166
167 for ( int i = 1; i < argc; i += 1 ) {
168 arg = argv[i]; // convert to string value
169 if ( prefix( arg, "-" ) ) {
170 // pass through arguments
171
172 if ( arg == "-Xlinker" || arg == "-o" ) {
173 args[nargs++] = argv[i]; // pass flag along
174 i += 1;
175 if ( i == argc ) continue; // next argument available ?
176 args[nargs++] = argv[i]; // pass argument along
177 if ( arg == "-o" ) o_file = i; // remember file
178
179 // CFA specific arguments
180
181 } else if ( strncmp(arg.c_str(), "-XCFA", 5) == 0 ) { // CFA pass through
182 if ( arg.size() == 5 ) {
183 i += 1;
184 if ( i == argc ) continue; // next argument available ?
185 Putenv( argv, argv[i] ); // make available for cc1
186 } else if ( arg[5] == ',' ) { // CFA specific arguments
187 string xcfargs = arg.substr( 6 ) + ","; // add sentinel
188 for ( ;; ) {
189 size_t posn = xcfargs.find_first_of( "," ); // find separator
190 if ( posn == string::npos ) break; // any characters left ?
191 string xcfarg = xcfargs.substr( 0, posn ); // remove XCFA argument
192 Putenv( argv, xcfarg ); // make available for cc1
193 xcfargs.erase( 0, posn + 1 ); // remove first argument and comma
194 } // for
195 } else { // CFA specific arguments
196 assert( false ); // this does not make sense
197 args[nargs++] = argv[i];
198 } // if
199 } else if ( arg == "-CFA" ) {
200 CFA_flag = true; // strip -CFA flag
201 link = false;
202 args[nargs++] = "-fsyntax-only"; // stop after stage 2
203 } else if ( arg == "-debug" ) {
204 debug = true; // strip debug flag
205 } else if ( arg == "-nodebug" ) {
206 debug = false; // strip nodebug flag
207 } else if ( arg == "-quiet" ) {
208 quiet = true; // strip quiet flag
209 } else if ( arg == "-noquiet" ) {
210 quiet = false; // strip noquiet flag
211 } else if ( arg == "-invariant" ) {
212 Putenv( argv, "-" + arg );
213 } else if ( arg == "--invariant" ) {
214 Putenv( argv, arg );
215 } else if ( arg == "-no-include-stdhdr" ) {
216 noincstd_flag = true; // strip no-include-stdhdr flag
217 } else if ( arg == "-nolib" ) {
218 nolib = true; // strip nolib flag
219 } else if ( arg == "-help" ) {
220 help = true; // strip help flag
221 } else if ( arg == "-nohelp" ) {
222 help = false; // strip nohelp flag
223 } else if ( arg == "-cfalib") {
224 compiling_libs = true;
225 } else if ( arg == "-compiler" ) {
226 // use the user specified compiler
227 i += 1;
228 if ( i == argc ) continue; // next argument available ?
229 compiler_path = argv[i];
230 Putenv( argv, arg + "=" + argv[i] );
231
232 // C specific arguments
233
234 } else if ( arg == "-v" ) {
235 verbose = true; // verbosity required
236 args[nargs++] = argv[i]; // pass flag along
237 } else if ( arg == "-g" ) {
238 debugging = true; // symbolic debugging required
239 args[nargs++] = argv[i]; // pass flag along
240 } else if ( arg == "-save-temps" || arg == "--save-temps" ) {
241 args[nargs++] = argv[i]; // pass flag along
242 Putenv( argv, arg ); // save cfa-cpp output
243 } else if ( prefix( arg, "-x" ) ) { // file suffix ?
244 string lang;
245 args[nargs++] = argv[i]; // pass flag along
246 if ( arg.length() == 2 ) { // separate argument ?
247 i += 1;
248 if ( i == argc ) continue; // next argument available ?
249 lang = argv[i];
250 args[nargs++] = argv[i]; // pass argument along
251 } else {
252 lang = arg.substr( 2 );
253 } // if
254 if ( x_flag ) {
255 cerr << argv[0] << " warning, only one -x flag per compile, ignoring subsequent flag." << endl;
256 } else {
257 x_flag = true;
258 Putenv( argv, string( "-x=" ) + lang );
259 } // if
260 } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
261 std_flag = true; // -std=XX provided
262 args[nargs++] = argv[i]; // pass flag along
263 } else if ( arg == "-w" ) {
264 args[nargs++] = argv[i]; // pass flag along
265 Putenv( argv, arg );
266 } else if ( prefix( arg, "-W" ) ) { // check before next tests
267 if ( arg == "-Werror" || arg == "-Wall" ) {
268 args[nargs++] = argv[i]; // pass flag along
269 Putenv( argv, argv[i] );
270 } else {
271 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
272 args[nargs] = argv[i]; // conditionally pass argument along
273 const char * warning = argv[i] + adv; // extract warning
274 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
275 Putenv( argv, arg );
276 } // if
277 nargs += 1;
278 } // if
279 } else if ( prefix( arg, "-B" ) ) {
280 bprefix = arg.substr(2); // strip -B flag
281 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
282 args[nargs++] = argv[i]; // pass flag along
283 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
284 cpp_flag = true; // cpp only
285 } // if
286 link = false; // no linkage required
287 } else if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
288 arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
289 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
290 args[nargs++] = argv[i]; // pass flag along
291 i += 1;
292 args[nargs++] = argv[i]; // pass argument along
293 } else if ( arg[1] == 'l' ) {
294 // if the user specifies a library, load it after user code
295 libs[nlibs++] = argv[i];
296 } else if ( arg == "-m32" ) {
297 m32 = true;
298 m64 = false;
299 args[nargs++] = argv[i];
300 } else if ( arg == "-m64" ) {
301 m64 = true;
302 m32 = false;
303 args[nargs++] = argv[i];
304 } else {
305 // concatenate any other arguments
306 args[nargs++] = argv[i];
307 } // if
308 } else {
309 bool cfa = suffix( arg ); // check suffix
310 if ( ! x_flag && cfa ) { // no explicit suffix and cfa suffix ?
311 args[nargs++] = "-x";
312 args[nargs++] = "c";
313 } // if
314 args[nargs++] = argv[i]; // concatenate files
315 if ( ! x_flag && cfa ) { // no explicit suffix and cfa suffix ?
316 args[nargs++] = "-x";
317 args[nargs++] = "none";
318 } // if
319 nonoptarg = true;
320 } // if
321 } // for
322
323 #ifdef __x86_64__
324 args[nargs++] = "-mcx16"; // allow double-wide CAS
325 #endif // __x86_64__
326
327 #ifdef __DEBUG_H__
328 cerr << "args:";
329 for ( int i = 1; i < nargs; i += 1 ) {
330 cerr << " " << args[i];
331 } // for
332 cerr << endl;
333 #endif // __DEBUG_H__
334
335 // -E flag stops at cc1 stage 1, so cfa-cpp in cc1 stage 2 is never executed.
336 if ( cpp_flag && CFA_flag ) {
337 CFA_flag = false;
338 cerr << argv[0] << " warning, both -E and -CFA flags specified, using -E and ignoring -CFA." << endl;
339 } // if
340
341 // add the CFA include-library paths, which allow direct access to header files without directory qualification
342 string libbase;
343 switch(path) {
344 case Installed:
345 args[nargs++] = "-I" CFA_INCDIR;
346 // do not use during build
347 if ( ! noincstd_flag ) {
348 args[nargs++] = "-I" CFA_INCDIR "stdhdr";
349 } // if
350 args[nargs++] = "-I" CFA_INCDIR "concurrency";
351 args[nargs++] = "-I" CFA_INCDIR "containers";
352 libbase = CFA_LIBDIR;
353 break;
354 case BuildTree:
355 case Distributed:
356 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src";
357 // do not use during build
358 if ( ! noincstd_flag ) {
359 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
360 } // if
361 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
362 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
363
364 libbase = TOP_BUILDDIR "libcfa/";
365
366 break;
367 } // if
368
369 // add stdbool to get defines for bool/true/false
370 args[nargs++] = "-imacros";
371 args[nargs++] = "stdbool.h";
372
373 if ( compiling_libs ) {
374 Putenv( argv, "-t" );
375 } // if
376
377 string arch( m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU) );
378 if ( ! m32 && ! m64 ) {
379 if ( arch == "x86" ) {
380 args[nargs++] = "-m32";
381 } else if ( arch == "x64" ) {
382 args[nargs++] = "-m64";
383 } // if
384 } // if
385
386 const char * config = nolib ? "nolib" : (debug ? "debug": "nodebug");
387 string libdir = libbase + arch + "-" + config;
388
389 if ( path != Distributed ) {
390 if ( ! nolib && ! dirExists( libdir ) ) {
391 cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
392 cerr << "Was looking for " << libdir << endl;
393 for(int i = 1; i < argc; i++) {
394 cerr << argv[i] << " ";
395 }
396 cerr << endl;
397 libdir = libbase + arch + "-" + "nolib";
398 } // if
399
400 if ( ! dirExists( libdir ) ) {
401 cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
402 cerr << "Was looking for " << libdir << endl;
403 exit( EXIT_FAILURE );
404 } // if
405 } // if
406
407 string preludedir;
408 switch(path) {
409 case Installed : preludedir = libdir; break;
410 case BuildTree : preludedir = libdir + "/prelude"; break;
411 case Distributed : preludedir = dir(argv[0]); break;
412 } // switch
413
414 Putenv( argv, "--prelude-dir=" + preludedir );
415 args[nargs++] = "-include";
416 args[nargs++] = (*new string(preludedir + "/defines.hfa")).c_str();
417
418 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
419 args[nargs++] = libs[i];
420 } // for
421
422 if ( link ) {
423 args[nargs++] = "-Xlinker";
424 args[nargs++] = "--undefined=__cfaabi_dbg_bits_write";
425 args[nargs++] = "-Xlinker";
426 args[nargs++] = "--undefined=__cfaabi_interpose_startup";
427 args[nargs++] = "-Xlinker";
428 args[nargs++] = "--undefined=__cfaabi_appready_startup";
429 args[nargs++] = "-z";
430 args[nargs++] = "execstack";
431
432 // include the cfa library in case it is needed
433 args[nargs++] = ( *new string( string("-L" ) + libdir + (path != Installed ? "/src/.libs" : "")) ).c_str();
434 args[nargs++] = ( *new string( string("-Wl,-rpath," ) + libdir + (path != Installed ? "/src/.libs" : "")) ).c_str();
435 args[nargs++] = "-Wl,--push-state,--as-needed";
436 args[nargs++] = "-lcfathread";
437 args[nargs++] = "-Wl,--pop-state";
438 args[nargs++] = "-Wl,--push-state,--no-as-needed";
439 args[nargs++] = "-lcfa";
440 args[nargs++] = "-Wl,--pop-state";
441 args[nargs++] = "-pthread";
442 #if defined( __x86_64__ ) || defined( __ARM_ARCH )
443 args[nargs++] = "-latomic"; // allow double-wide CAS
444 #endif // __x86_64__
445 args[nargs++] = "-ldl";
446 args[nargs++] = "-lm";
447 } // if
448
449 args[nargs++] = "-fexceptions"; // add exception flags (unconditionally)
450 args[nargs++] = "-D_GNU_SOURCE"; // force gnu libraries
451
452 // add flags based on the type of compile
453
454 args[nargs++] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
455 args[nargs++] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
456 args[nargs++] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
457 args[nargs++] = "-D__CFA__";
458 args[nargs++] = "-D__CFORALL__";
459 args[nargs++] = "-D__cforall";
460
461 if ( cpp_flag ) {
462 args[nargs++] = "-D__CPP__";
463 } // if
464
465 if ( CFA_flag ) {
466 Putenv( argv, "-N" );
467 Putenv( argv, "-CFA" );
468 // -CFA implies cc1 stage 2, but gcc does not pass the -o file to this stage because it believe the file is for
469 // the linker. Hence, the -o file is explicit passed to cc1 stage 2 and used as cfa-cpp's output file.
470 if ( o_file ) Putenv( argv, string( "-o=" ) + argv[o_file] );
471 } else {
472 Putenv( argv, "-L" );
473 } // if
474
475 if ( debug ) {
476 heading += " (debug)";
477 args[nargs++] = "-D__CFA_DEBUG__";
478 } else {
479 heading += " (no debug)";
480 } // if
481
482 if ( bprefix.length() == 0 ) {
483 switch(path) {
484 case Installed : bprefix = installlibdir; break;
485 case BuildTree : bprefix = srcdriverdir ; break;
486 case Distributed : bprefix = dir(argv[0]) ; break;
487 } // switch
488 } // if
489 if ( bprefix[bprefix.length() - 1] != '/' ) bprefix += '/';
490 Putenv( argv, string("-B=") + bprefix );
491
492 args[nargs++] = "-Xlinker"; // used by backtrace
493 args[nargs++] = "-export-dynamic";
494
495 // execute the compilation command
496
497 args[0] = compiler_path.c_str(); // set compiler command for exec
498 // find actual name of the compiler independent of the path to it
499 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
500 if ( p == -1 ) {
501 compiler_name = compiler_path;
502 } else {
503 compiler_name = *new string( compiler_path.substr( p + 1 ) );
504 } // if
505
506 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
507 args[nargs++] = "-no-integrated-cpp";
508 args[nargs++] = "-Wno-deprecated";
509 args[nargs++] = "-Wno-strict-aliasing"; // casting from one type to another
510 #ifdef HAVE_CAST_FUNCTION_TYPE
511 args[nargs++] = "-Wno-cast-function-type";
512 #endif // HAVE_CAST_FUNCTION_TYPE
513 if ( ! std_flag && ! x_flag ) {
514 args[nargs++] = "-std=gnu11"; // default c11, if none specified
515 } // if
516 args[nargs++] = "-fgnu89-inline";
517 args[nargs++] = "-D__int8_t_defined"; // prevent gcc type-size attributes
518 args[nargs++] = ( *new string( string("-B") + bprefix ) ).c_str();
519 } else {
520 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
521 exit( EXIT_FAILURE );
522 } // if
523
524 args[nargs] = nullptr; // terminate
525
526 #ifdef __DEBUG_H__
527 cerr << "nargs: " << nargs << endl;
528 cerr << "args:" << endl;
529 for ( int i = 0; args[i] != nullptr; i += 1 ) {
530 cerr << " \"" << args[i] << "\"" << endl;
531 } // for
532 cerr << endl;
533 #endif // __DEBUG_H__
534
535 if ( ! quiet ) {
536 cerr << "CFA " << "Version " << Version << heading << endl;
537 if ( help ) {
538 cerr <<
539 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
540 "-help\t\t\t: print this help message" << endl <<
541 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
542 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
543 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
544 "...\t\t\t: any other " << compiler_name << " flags" << endl;
545 } // if
546 } // if
547
548 if ( verbose ) {
549 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
550
551 for ( int i = 0; args[i] != nullptr; i += 1 ) {
552 cerr << args[i] << " ";
553 } // for
554 cerr << endl;
555 } // if
556
557 if ( ! nonoptarg ) {
558 cerr << argv[0] << " error, no input files" << endl;
559 exit( EXIT_FAILURE );
560 } // if
561
562 // execute the command and return the result
563
564 execvp( args[0], (char * const *)args ); // should not return
565 perror( "CFA Translator error: execvp" );
566 exit( EXIT_FAILURE );
567} // main
568
569// Local Variables: //
570// tab-width: 4 //
571// mode: c++ //
572// compile-command: "make install" //
573// End: //
Note: See TracBrowser for help on using the repository browser.