source: driver/cfa.cc @ ef22ad6

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ef22ad6 was 2c60af75, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

move cfa-cpp into stage 2 of cc1, and update cfa to facilitate the change

  • Property mode set to 100644
File size: 15.2 KB
RevLine 
[b87a5ed]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//
[76c7f65e]7// cfa.cc --
[b87a5ed]8//
[51b7345]9// Author           : Peter A. Buhr
10// Created On       : Tue Aug 20 13:44:49 2002
[201aeb9]11// Last Modified By : Peter A. Buhr
[2c60af75]12// Last Modified On : Thu Aug 22 22:29:50 2019
13// Update Count     : 403
[b87a5ed]14//
[51b7345]15
16#include <iostream>
[b87a5ed]17#include <cstdio>                                                                               // perror
18#include <cstdlib>                                                                              // putenv, exit
19#include <unistd.h>                                                                             // execvp
20#include <string>                                                                               // STL version
[44bca7f]21#include <string.h>                                                                             // strcmp
[8c63bb4]22#include <algorithm>                                                                    // find
[51b7345]23
[37fe352]24#include <sys/types.h>
25#include <sys/stat.h>
26
[44bca7f]27#include "Common/SemanticError.h"
[b87a5ed]28#include "config.h"                                                                             // configure info
[51b7345]29
30using std::cerr;
31using std::endl;
32using std::string;
[47a8d17]33using std::to_string;
[51b7345]34
35//#define __DEBUG_H__
36
37
[2c60af75]38void Putenv( char * argv[], string arg ) {
39        static int flags = 0;                                                           // environment variables must have unique names
40
41        if ( putenv( (char *)( *new string( string( "__CFA_FLAG" + to_string( flags++ ) + "__=" ) + arg ) ).c_str() ) != 0 ) {
42                cerr << argv[0] << " error, cannot set environment variable." << endl;
43                exit( EXIT_FAILURE );
44        } // if
45} // Putenv
46
47
48bool prefix( const string & arg, const string & pre ) { // check if string has prefix
[b87a5ed]49        return arg.substr( 0, pre.size() ) == pre;
[51b7345]50} // prefix
51
[2c60af75]52bool suffix( const string & arg ) {                                             // check if string has suffix
[8c63bb4]53        enum { NumSuffixes = 3 };
54        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
[2c60af75]55
[dffaeac]56        size_t dot = arg.find_last_of( "." );
57        if ( dot == string::npos ) return false;
[8c63bb4]58        const string * end = suffixes + NumSuffixes;
59        return std::find( suffixes, end, arg.substr( dot + 1 ) ) != end;
[dffaeac]60} // suffix
61
[51b7345]62
[2c60af75]63static inline bool dirExists( const string & path ) {   // check if directory exists
[37fe352]64    struct stat info;
[2c60af75]65    if ( stat( path.c_str(), &info ) != 0 ) return false;
66    if ( info.st_mode & S_IFDIR ) return true;
67        return false;
68} // dirExists
[37fe352]69
[51b7345]70
[2c60af75]71#define xstr(s) str(s)
[4b1afb6]72#define str(s) #s
73
[8c63bb4]74int main( int argc, char * argv[] ) {
[4b1afb6]75        string Version( CFA_VERSION_LONG );                                     // current version number from CONFIG
[2c60af75]76        string Major( xstr( CFA_VERSION_MAJOR ) ), Minor( xstr( CFA_VERSION_MINOR ) ), Patch( xstr( CFA_VERSION_PATCH ) );
[51b7345]77
[d6f4488]78        string installincdir( CFA_INCDIR );                                     // fixed location of include files
79        string installlibdir( CFA_LIBDIR );                                     // fixed location of cc1 and cfa-cpp commands when installed
80        string srcdriverdir ( TOP_BUILDDIR "driver");           // fixed location of cc1 and cfa-cpp commands when in tree
[51b7345]81
[b87a5ed]82        string heading;                                                                         // banner printed at start of cfa compilation
83        string arg;                                                                                     // current command-line argument during command-line parsing
84        string Bprefix;                                                                         // path where gcc looks for compiler command steps
85        string langstd;                                                                         // language standard
[51b7345]86
[e24f13a]87        string compiler_path( CFA_BACKEND_CC );                         // path/name of C compiler
[b87a5ed]88        string compiler_name;                                                           // name of C compiler
[51b7345]89
[4f5a8a2]90        bool x_flag = false;                                                            // -x flag
[b87a5ed]91        bool nonoptarg = false;                                                         // indicates non-option argument specified
92        bool link = true;                                                                       // linking as well as compiling
93        bool verbose = false;                                                           // -v flag
[8c63bb4]94        bool quiet = false;                                                                     // -quiet flag
95        bool debug = true;                                                                      // -debug flag
96        bool nolib = false;                                                                     // -nolib flag
97        bool help = false;                                                                      // -help flag
[b87a5ed]98        bool CFA_flag = false;                                                          // -CFA flag
99        bool cpp_flag = false;                                                          // -E or -M flag, preprocessor only
[de62360d]100        bool std_flag = false;                                                          // -std= flag
[d746bc8]101        bool noincstd_flag = false;                                                     // -no-include-stdhdr= flag
[6e4b913]102        bool debugging __attribute(( unused )) = false;         // -g flag
[8c63bb4]103        bool m32 = false;                                                                       // -m32 flag
104        bool m64 = false;                                                                       // -m64 flag
[37fe352]105        bool intree = false;
[2c60af75]106        int o_file = 0;                                                                         // -o filename position
[51b7345]107
[b87a5ed]108        const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
109        int sargs = 1;                                                                          // starting location for arguments in args list
110        int nargs = sargs;                                                                      // number of arguments in args list; 0 => command name
[51b7345]111
[b87a5ed]112        const char *libs[argc + 20];                                            // non-user libraries must come separately, plus some added libraries and flags
113        int nlibs = 0;
[51b7345]114
[dffaeac]115        #ifdef __DEBUG_H__
[b87a5ed]116        cerr << "CFA:" << endl;
[bec4d24]117        for ( int i = 1; i < argc; i += 1 ) {
118            cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
119        } // for
[dffaeac]120        #endif // __DEBUG_H__
[51b7345]121
[b87a5ed]122        // process command-line arguments
[51b7345]123
[b87a5ed]124        for ( int i = 1; i < argc; i += 1 ) {
125                arg = argv[i];                                                                  // convert to string value
126                if ( prefix( arg, "-" ) ) {
127                        // pass through arguments
128
129                        if ( arg == "-Xlinker" || arg == "-o" ) {
[2c60af75]130                                args[nargs++] = argv[i];                                // pass argument along
[b87a5ed]131                                i += 1;
132                                if ( i == argc ) continue;                              // next argument available ?
[2c60af75]133                                args[nargs++] = argv[i];                                // pass argument along
134                                if ( arg == "-o" ) o_file = i;                  // remember file
[b87a5ed]135                        } else if ( arg == "-XCFA" ) {                          // CFA pass through
136                                i += 1;
[2c60af75]137                                Putenv( argv, argv[i] );
[b87a5ed]138
139                                // CFA specific arguments
140
141                        } else if ( arg == "-CFA" ) {
142                                CFA_flag = true;                                                // strip the -CFA flag
143                                link = false;
[2c60af75]144                                args[nargs++] = "-fsyntax-only";                // stop after stage 2
[b87a5ed]145                        } else if ( arg == "-debug" ) {
146                                debug = true;                                                   // strip the debug flag
147                        } else if ( arg == "-nodebug" ) {
[2c60af75]148                                debug = false;                                                  // strip the nodebug flag
[c9e640e]149                        } else if ( arg == "-nolib" ) {
150                                nolib = true;                                                   // strip the nodebug flag
[b87a5ed]151                        } else if ( arg == "-quiet" ) {
152                                quiet = true;                                                   // strip the quiet flag
153                        } else if ( arg == "-noquiet" ) {
154                                quiet = false;                                                  // strip the noquiet flag
155                        } else if ( arg == "-help" ) {
156                                help = true;                                                    // strip the help flag
157                        } else if ( arg == "-nohelp" ) {
158                                help = false;                                                   // strip the nohelp flag
[d746bc8]159                        } else if ( arg == "-no-include-stdhdr" ) {
160                                noincstd_flag = true;                                   // strip the no-include-stdhdr flag
[37fe352]161                        } else if ( arg == "-in-tree" ) {
162                                intree = true;
[b87a5ed]163                        } else if ( arg == "-compiler" ) {
164                                // use the user specified compiler
165                                i += 1;
166                                if ( i == argc ) continue;                              // next argument available ?
167                                compiler_path = argv[i];
[2c60af75]168                                Putenv( argv, arg + "=" + argv[i] );
[b87a5ed]169
[de62360d]170                                // C specific arguments
[b87a5ed]171
172                        } else if ( arg == "-v" ) {
173                                verbose = true;                                                 // verbosity required
[2c60af75]174                                args[nargs++] = argv[i];                                // pass argument along
[b87a5ed]175                        } else if ( arg == "-g" ) {
176                                debugging = true;                                               // symbolic debugging required
[2c60af75]177                                args[nargs++] = argv[i];                                // pass argument along
[8c63bb4]178                        } else if ( prefix( arg, "-x" ) ) {                     // file suffix ?
179                                string lang;
[2c60af75]180                                args[nargs++] = argv[i];                                // pass argument along
[8c63bb4]181                                if ( arg.length() == 2 ) {                              // separate argument ?
182                                        i += 1;
183                                        if ( i == argc ) continue;                      // next argument available ?
184                                        lang = argv[i];
[2c60af75]185                                        args[nargs++] = argv[i];                        // pass argument along
[8c63bb4]186                                } else {
187                                        lang = arg.substr( 2 );
188                                } // if
[4f5a8a2]189                                x_flag = lang != "none";
190                        } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
191                                std_flag = true;                                                // -std=XX provided
[2c60af75]192                                args[nargs++] = argv[i];                                // pass argument along
[4f5a8a2]193                        } else if ( arg == "-w" ) {
[2c60af75]194                                args[nargs++] = argv[i];                                // pass argument along
195                                Putenv( argv, arg );
[44bca7f]196                        } else if ( prefix( arg, "-W" ) ) {                     // check before next tests
197                                if ( arg == "-Werror" || arg == "-Wall" ) {
[2c60af75]198                                        args[nargs++] = argv[i];                        // pass argument along
199                                        Putenv( argv, argv[i] );
[44bca7f]200                                } else {
201                                        unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
[2c60af75]202                                        args[nargs] = argv[i];                          // conditionally pass argument along
203                                        const char * warning = argv[i] + adv; // extract warning
[af39199d]204                                        if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
[2c60af75]205                                                Putenv( argv, arg );
[af39199d]206                                        } // if
[44bca7f]207                                        nargs += 1;
208                                } // if
[b87a5ed]209                        } else if ( prefix( arg, "-B" ) ) {
210                                Bprefix = arg.substr(2);                                // strip the -B flag
[2c60af75]211                                args[nargs++] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
[b87a5ed]212                        } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
[2c60af75]213                                args[nargs++] = argv[i];                                // pass argument along
[b87a5ed]214                                if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
215                                        cpp_flag = true;                                        // cpp only
216                                } // if
217                                link = false;                           // no linkage required
218                        } else if ( arg[1] == 'l' ) {
219                                // if the user specifies a library, load it after user code
[2c60af75]220                                libs[nlibs++] = argv[i];
[37fe352]221                        } else if ( arg == "-m32" ) {
222                                m32 = true;
223                                m64 = false;
[2c60af75]224                                args[nargs++] = argv[i];
[37fe352]225                        } else if ( arg == "-m64" ) {
226                                m64 = true;
227                                m32 = false;
[2c60af75]228                                args[nargs++] = argv[i];
[b87a5ed]229                        } else {
230                                // concatenate any other arguments
[2c60af75]231                                args[nargs++] = argv[i];
[b87a5ed]232                        } // if
233                } else {
[8c63bb4]234                        bool cfa = suffix( arg );                                       // check suffix
[4f5a8a2]235                        if ( ! x_flag && cfa ) {                                        // no explicit suffix and cfa suffix ?
[2c60af75]236                                args[nargs++] = "-x";
237                                args[nargs++] = "c";
[8c63bb4]238                        } // if
[2c60af75]239                        args[nargs++] = argv[i];                                        // concatenate files
[4f5a8a2]240                        if ( ! x_flag && cfa ) {                                        // no explicit suffix and cfa suffix ?
[2c60af75]241                                args[nargs++] = "-x";
242                                args[nargs++] = "none";
[dffaeac]243                        } // if
[b87a5ed]244                        nonoptarg = true;
245                } // if
246        } // for
[51b7345]247
[dffaeac]248        #ifdef __x86_64__
[2c60af75]249        args[nargs++] = "-mcx16";                                                       // allow double-wide CAA
[dffaeac]250        #endif // __x86_64__
[e3215c5]251
[dffaeac]252        #ifdef __DEBUG_H__
[b87a5ed]253        cerr << "args:";
254        for ( int i = 1; i < nargs; i += 1 ) {
255                cerr << " " << args[i];
256        } // for
257        cerr << endl;
[dffaeac]258        #endif // __DEBUG_H__
[51b7345]259
[2c60af75]260        // if ( cpp_flag && CFA_flag ) {
261        //      cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
262        //      exit( EXIT_FAILURE );
263        // } // if
[51b7345]264
[6e4b913]265        // add the CFA include-library paths, which allow direct access to header files without directory qualification
[2c60af75]266        if ( ! intree ) {
267                args[nargs++] = "-I" CFA_INCDIR;
[dfb7c96]268                if ( ! noincstd_flag ) {                                                // do not use during build
[2c60af75]269                        args[nargs++] = "-I" CFA_INCDIR "stdhdr";
[a5121bf]270                } // if
[2c60af75]271                args[nargs++] = "-I" CFA_INCDIR "concurrency";
272                args[nargs++] = "-I" CFA_INCDIR "containers";
[a5121bf]273        } else {
[2c60af75]274                args[nargs++] = "-I" TOP_SRCDIR "libcfa/src";
[dfb7c96]275                if ( ! noincstd_flag ) {                                                // do not use during build
[2c60af75]276                        args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
[a5121bf]277                } // if
[2c60af75]278                args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
279                args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
280        } // if
[76c7f65e]281
[a37133c]282        // add stdbool to get defines for bool/true/false
[2c60af75]283        args[nargs++] = "-imacros";
284        args[nargs++] = "stdbool.h";
[a37133c]285
[a5121bf]286        string libbase;
[2c60af75]287        if ( ! intree ) {
[a5121bf]288                libbase = CFA_LIBDIR;
[37fe352]289        } else {
[a5121bf]290                libbase = TOP_BUILDDIR "libcfa/";
[2c60af75]291                Putenv( argv, "-t" );
292        } // if
[37fe352]293
[dfb7c96]294        string arch = m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU);
[13a984c]295        if ( ! m32 && ! m64 ) {
296                if ( arch == "x86" ) {
[2c60af75]297                        args[nargs++] = "-m32";
[13a984c]298                } else if ( arch == "x64" ) {
[2c60af75]299                        args[nargs++] = "-m64";
[13a984c]300                }  // if
[dfb7c96]301        } // if
[a5121bf]302
[2c60af75]303        string libdir = libbase + arch + "-" + (nolib ? "nolib" : (debug ? "debug": "nodebug"));
[dfb7c96]304        if ( ! dirExists( libdir ) ) {
[2c60af75]305                cerr << argv[0] << " internal error, cannot find prelude directory " << libdir << endl;
[a5121bf]306                exit( EXIT_FAILURE );
[dfb7c96]307        } // if
[a5121bf]308
[def9d4e]309        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
[2c60af75]310                args[nargs++] = libs[i];
[def9d4e]311        } // for
312
[b87a5ed]313        if ( link ) {
[2c60af75]314                args[nargs++] = "-Xlinker";
315                args[nargs++] = "--undefined=__cfaabi_dbg_bits_write";
316                args[nargs++] = "-Xlinker";
317                args[nargs++] = "--undefined=__cfaabi_interpose_startup";
318                args[nargs++] = "-Xlinker";
319                args[nargs++] = "--undefined=__cfaabi_appready_startup";
[6bfe5cc]320
[b87a5ed]321                // include the cfa library in case it's needed
[2c60af75]322                args[nargs++] = ( *new string( string("-L" ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
323                args[nargs++] = ( *new string( string("-Wl,-rpath," ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
324                args[nargs++] = "-Wl,--push-state,--as-needed";
325                args[nargs++] = "-lcfathread";
326                args[nargs++] = "-Wl,--pop-state";
327                args[nargs++] = "-lcfa";
328                args[nargs++] = "-lpthread";
329                args[nargs++] = "-ldl";
330                args[nargs++] = "-lrt";
331                args[nargs++] = "-lm";
[51b7345]332        } // if
333
[2c60af75]334        args[nargs++] = "-fexceptions";                                         // add exception flags (unconditionally)
335
336        // add flags based on the type of compile
337
338        args[nargs++] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
339        args[nargs++] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
340        args[nargs++] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
341        args[nargs++] = "-D__CFA__";
342        args[nargs++] = "-D__CFORALL__";
343        args[nargs++] = "-D__cforall";
[51b7345]344
[b87a5ed]345        if ( cpp_flag ) {
[2c60af75]346                args[nargs++] = "-D__CPP__";
[b87a5ed]347        } // if
[51b7345]348
[b87a5ed]349        if ( CFA_flag ) {
[2c60af75]350                Putenv( argv, "-N" );
351                Putenv( argv, "-CFA" );
352                if ( o_file ) Putenv( argv, string( "-o=" ) + argv[o_file] );
[fa477f7]353        } else {
[2c60af75]354                Putenv( argv, "-L" );
[b87a5ed]355        } // if
[2c60af75]356
357        Putenv( argv, "--prelude-dir=" + libdir + (intree ? "/prelude" : "") );
[51b7345]358
[b87a5ed]359        if ( debug ) {
360                heading += " (debug)";
[2c60af75]361                args[nargs++] = "-D__CFA_DEBUG__";
[b87a5ed]362        } else {
363                heading += " (no debug)";
364        } // if
[51b7345]365
[b87a5ed]366        if ( Bprefix.length() == 0 ) {
[b740f0b]367                Bprefix = ! intree ? installlibdir : srcdriverdir;
368                if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/';
[2c60af75]369                args[nargs++] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
[b87a5ed]370        } // if
[51b7345]371
[2c60af75]372        args[nargs++] = "-Xlinker";                                                     // used by backtrace
373        args[nargs++] = "-export-dynamic";
[6bfe5cc]374
[b87a5ed]375        // execute the compilation command
[51b7345]376
[b87a5ed]377        args[0] = compiler_path.c_str();                                        // set compiler command for exec
378        // find actual name of the compiler independent of the path to it
379        int p = compiler_path.find_last_of( '/' );                      // scan r -> l for first '/'
380        if ( p == -1 ) {
381                compiler_name = compiler_path;
382        } else {
383                compiler_name = *new string( compiler_path.substr( p + 1 ) );
384        } // if
[51b7345]385
[b87a5ed]386        if ( prefix( compiler_name, "gcc" ) ) {                         // allow suffix on gcc name
[2c60af75]387                args[nargs++] = "-no-integrated-cpp";
388                args[nargs++] = "-Wno-deprecated";
389                #ifdef HAVE_CAST_FUNCTION_TYPE
390                args[nargs++] = "-Wno-cast-function-type";
391                #endif // HAVE_CAST_FUNCTION_TYPE
[157d094]392                if ( ! std_flag ) {                                                             // default c11, if none specified
[2c60af75]393                        args[nargs++] = "-std=gnu11";
[de62360d]394                } // if
[2c60af75]395                args[nargs++] = "-fgnu89-inline";
396                args[nargs++] = "-D__int8_t_defined";                   // prevent gcc type-size attributes
397                args[nargs++] = ( *new string( string("-B") + Bprefix ) ).c_str();
[b87a5ed]398        } else {
[e24f13a]399                cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
[b87a5ed]400                exit( EXIT_FAILURE );
401        } // if
[51b7345]402
[2c60af75]403        args[nargs] = NULL;                                                                     // terminate
[51b7345]404
[dffaeac]405        #ifdef __DEBUG_H__
[b87a5ed]406        cerr << "nargs: " << nargs << endl;
407        cerr << "args:" << endl;
408        for ( int i = 0; args[i] != NULL; i += 1 ) {
409                cerr << " \"" << args[i] << "\"" << endl;
410        } // for
[dffaeac]411        #endif // __DEBUG_H__
[51b7345]412
[b87a5ed]413        if ( ! quiet ) {
414                cerr << "CFA " << "Version " << Version << heading << endl;
415
416                if ( help ) {
417                        cerr <<
418                                "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
419                                "-help\t\t\t: print this help message" << endl <<
420                                "-quiet\t\t\t: print no messages from the cfa command" << endl <<
421                                "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
422                                "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
423                                "...\t\t\t: any other " << compiler_name << " flags" << endl;
424                } // if
[51b7345]425        } // if
426
[b87a5ed]427        if ( verbose ) {
428                if ( argc == 2 ) exit( EXIT_SUCCESS );                  // if only the -v flag is specified, do not invoke gcc
[51b7345]429
[b87a5ed]430                for ( int i = 0; args[i] != NULL; i += 1 ) {
431                        cerr << args[i] << " ";
432                } // for
433                cerr << endl;
434        } // if
[51b7345]435
[b87a5ed]436        if ( ! nonoptarg ) {
437                cerr << argv[0] << " error, no input files" << endl;
438                exit( EXIT_FAILURE );
439        } // if
[51b7345]440
[b87a5ed]441        // execute the command and return the result
[51b7345]442
[b87a5ed]443        execvp( args[0], (char *const *)args );                         // should not return
[2c60af75]444        perror( "CFA Translator error: execvp" );
[b87a5ed]445        exit( EXIT_FAILURE );
[51b7345]446} // main
447
448// Local Variables: //
[b87a5ed]449// tab-width: 4 //
450// mode: c++ //
[51b7345]451// compile-command: "make install" //
452// End: //
Note: See TracBrowser for help on using the repository browser.