source: driver/cfa.cc @ 3271166

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resnenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 3271166 was bec4d24, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

fix debug print, better handle -x language command-line flag

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