source: driver/cc1.cc @ def9d4e

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since def9d4e 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: 15.2 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// cc1.cc --
8//
9// Author           : Peter A. Buhr
10// Created On       : Fri Aug 26 14:23:51 2005
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Sep  3 16:57:05 2018
13// Update Count     : 125
14//
15
16#include <iostream>
17using std::cerr;
18using std::endl;
19#include <string>
20using std::string;
21#include <cstdio>                                                                               // stderr, stdout, perror, fprintf
22#include <cstdlib>                                                                              // getenv, exit, mkstemp
23#include <unistd.h>                                                                             // execvp, fork, unlink
24#include <sys/wait.h>                                                                   // wait
25
26#include "config.h"                                                                             // configure info
27
28
29//#define __DEBUG_H__
30
31
32string compiler_name( CFA_BACKEND_CC );                                 // path/name of C compiler
33
34string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" );
35string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" );
36
37char tmpname[] = P_tmpdir "/CFAXXXXXX";
38int tmpfilefd = -1;
39
40
41bool prefix( string arg, string pre ) {
42        return arg.substr( 0, pre.size() ) == pre;
43} // prefix
44
45enum { NumSuffixes = 2 };
46const string suffixes[NumSuffixes] = { "cfa", "hfa", };
47
48void suffix( string arg, const char * args[], int & nargs ) {
49        //std::cerr << arg << std::endl;
50        size_t dot = arg.find_last_of( "." );
51        //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
52        if ( dot == string::npos ) return;
53        string sx = arg.substr( dot + 1 );
54        for ( int i = 0; i < NumSuffixes; i += 1 ) {
55                if ( sx == suffixes[i] ) {
56                        args[nargs] = "-x";
57                        nargs += 1;
58                        args[nargs] = "c";
59                        nargs += 1;
60                        return;
61                } // if
62        } // for
63} // suffix
64
65
66void checkEnv( const char * args[], int & nargs ) {
67        char *value;
68
69        value = getenv( "__CFA_COMPILER__" );
70        if ( value != NULL ) {
71                compiler_name = value;
72                #ifdef __DEBUG_H__
73                cerr << "env arg:\"" << compiler_name << "\"" << endl;
74                #endif // __DEBUG_H__
75        } // if
76
77        value = getenv( "__GCC_MACHINE__" );
78        if ( value != NULL ) {
79                args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
80                #ifdef __DEBUG_H__
81                cerr << "env arg:\"" << args[nargs] << "\"" << endl;
82                #endif // __DEBUG_H__
83                nargs += 1;
84        } // if
85
86        value = getenv( "__GCC_VERSION__" );
87        if ( value != NULL ) {
88                args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
89                #ifdef __DEBUG_H__
90                cerr << "env arg:\"" << args[nargs] << "\"" << endl;
91                #endif // __DEBUG_H__
92                nargs += 1;
93        } // if
94} // checkEnv
95
96
97void rmtmpfile() {
98        if ( unlink( tmpname ) == -1 ) {                                        // remove tmpname
99                perror ( "CFA Translator error: cpp failed" );
100                exit( EXIT_FAILURE );
101        } // if
102        tmpfilefd = -1;                                                                         // mark closed
103} // rmtmpfile
104
105
106void sigTermHandler( __attribute__((unused)) int signal ) {
107        if ( tmpfilefd != -1 ) {                                                        // RACE, file created ?
108                rmtmpfile();                                                                    // remove
109                exit( EXIT_FAILURE );                                                   // terminate
110        } // if
111} // sigTermHandler
112
113
114void Stage1( const int argc, const char * const argv[] ) {
115        int code;
116
117        string arg;
118        string bprefix;
119
120        const char *cpp_in = NULL;
121        const char *cpp_out = NULL;
122
123        bool CFA_flag = false;
124        bool cpp_flag = false;
125        const char *o_name = NULL;
126
127        const char *args[argc + 100];                                           // leave space for 100 additional cpp command line values
128        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
129        const char *cargs[20];                                                          // leave space for 20 additional cfa-cpp command line values
130        int ncargs = 1;                                                                         // 0 => command name
131
132        signal( SIGINT,  sigTermHandler );
133        signal( SIGTERM, sigTermHandler );
134
135        #ifdef __DEBUG_H__
136        cerr << "Stage1" << endl;
137        #endif // __DEBUG_H__
138        checkEnv( args, nargs );                                                        // arguments passed via environment variables
139        #ifdef __DEBUG_H__
140        for ( int i = 1; i < argc; i += 1 ) {
141                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
142        } // for
143        #endif // __DEBUG_H__
144
145        // process all the arguments
146
147        for ( int i = 1; i < argc; i += 1 ) {
148                arg = argv[i];
149                if ( prefix( arg, "-" ) ) {
150                        // strip g++ flags that are inappropriate or cause duplicates in subsequent passes
151
152                        if ( arg == "-quiet" ) {
153                        } else if ( arg == "-imultilib" || arg == "-imultiarch" ) {
154                                i += 1;                                                                 // and the argument
155                        } else if ( prefix( arg, "-A" ) ) {
156                        } else if ( prefix( arg, "-D__GNU" ) ) {
157                                //********
158                                // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT!
159                                //********
160                        } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) {
161                                i += 1;                                                                 // and the argument
162
163                                // strip flags controlling cpp step
164
165                        } else if ( arg == "-D__CPP__" ) {
166                                cpp_flag = true;
167                        } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) {
168                                i += 1;                                                                 // and the argument
169                                cpp_flag = true;
170                        } else if ( arg == "-D__CFA_PREPROCESS__" ) {
171                                CFA_flag = true;
172                        } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA_PREPROCESS__" ) {
173                                i += 1;                                                                 // and the argument
174                                CFA_flag = true;
175                        } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) {
176                                cargs[ncargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str();
177                                ncargs += 1;
178                        } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) {
179                                cargs[ncargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str();
180                                ncargs += 1;
181                                i += 1;                                                                 // and the argument
182                        } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) {
183                                bprefix = arg.substr( D__GCC_BPREFIX__.size() );
184                        } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) {
185                                bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 );
186                                i += 1;                                                                 // and the argument
187
188                        // all other flags
189
190                        } else if ( arg == "-o" ) {
191                                i += 1;
192                                o_name = argv[i];
193                        } else {
194                                args[nargs] = argv[i];                                  // pass the flag along
195                                nargs += 1;
196                                // CPP flags with an argument
197                                if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
198                                         arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
199                                         arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
200                                        i += 1;
201                                        args[nargs] = argv[i];                          // pass the argument along
202                                        nargs += 1;
203                                        #ifdef __DEBUG_H__
204                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
205                                        #endif // __DEBUG_H__
206                                } else if ( arg == "-MD" || arg == "-MMD" ) {
207                                        args[nargs] = "-MF";                            // insert before file
208                                        nargs += 1;
209                                        i += 1;
210                                        args[nargs] = argv[i];                          // pass the argument along
211                                        nargs += 1;
212                                        #ifdef __DEBUG_H__
213                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
214                                        #endif // __DEBUG_H__
215                                } // if
216                        } // if
217                } else {                                                                                // obtain input and possibly output files
218                        if ( cpp_in == NULL ) {
219                                cpp_in = argv[i];
220                                #ifdef __DEBUG_H__
221                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
222                                #endif // __DEBUG_H__
223                        } else if ( cpp_out == NULL ) {
224                                cpp_out = argv[i];
225                                #ifdef __DEBUG_H__
226                                cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
227                                #endif // __DEBUG_H__
228                        } else {
229                                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
230                                exit( EXIT_FAILURE );
231                        } // if
232                } // if
233        } // for
234
235        #ifdef __DEBUG_H__
236        cerr << "args:";
237        for ( int i = 1; i < nargs; i += 1 ) {
238                cerr << " " << args[i];
239        } // for
240        if ( cpp_in != NULL ) cerr << " " << cpp_in;
241        if ( cpp_out != NULL ) cerr << " " << cpp_out;
242        cerr << endl;
243        #endif // __DEBUG_H__
244
245        if ( cpp_in == NULL ) {
246                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
247                exit( EXIT_FAILURE );
248        } // if
249
250        if ( cpp_flag ) {
251                // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard
252                // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
253
254                args[0] = compiler_name.c_str();
255                suffix( cpp_in, args, nargs );                                  // check suffix
256                args[nargs] = cpp_in;
257                nargs += 1;
258                if ( o_name != NULL ) {                                                 // location for output
259                        args[nargs] = "-o";
260                        nargs += 1;
261                        args[nargs] = o_name;
262                        nargs += 1;
263                } // if
264                args[nargs] = NULL;                                                             // terminate argument list
265
266                #ifdef __DEBUG_H__
267                cerr << "nargs: " << nargs << endl;
268                for ( int i = 0; args[i] != NULL; i += 1 ) {
269                        cerr << args[i] << " ";
270                } // for
271                cerr << endl;
272                #endif // __DEBUG_H__
273
274                execvp( args[0], (char *const *)args );                 // should not return
275                perror( "CFA Translator error: cpp level, execvp" );
276                exit( EXIT_FAILURE );
277        } // if
278
279        // Create a temporary file to store output of the C preprocessor.
280
281        tmpfilefd = mkstemp( tmpname );
282        if ( tmpfilefd == -1 ) {
283                perror( "CFA Translator error: cpp level, mkstemp" );
284                exit( EXIT_FAILURE );
285        } // if
286
287        #ifdef __DEBUG_H__
288        cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
289        #endif // __DEBUG_H__
290
291        // Run the C preprocessor and save the output in tmpfile.
292
293        if ( fork() == 0 ) {                                                             // child process ?
294                // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects
295                // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
296                // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
297                if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname
298                        perror( "CFA Translator error: cpp level, freopen" );
299                        exit( EXIT_FAILURE );
300                } // if
301
302                args[0] = compiler_name.c_str();
303                suffix( cpp_in, args, nargs );                                  // check suffix
304                args[nargs] = cpp_in;                                                   // input to cpp
305                nargs += 1;
306                args[nargs] = NULL;                                                             // terminate argument list
307
308                #ifdef __DEBUG_H__
309                cerr << "cpp nargs: " << nargs << endl;
310                for ( int i = 0; args[i] != NULL; i += 1 ) {
311                        cerr << args[i] << " ";
312                } // for
313                cerr << endl;
314                #endif // __DEBUG_H__
315
316                execvp( args[0], (char *const *)args );                 // should not return
317                perror( "CFA Translator error: cpp level, execvp" );
318                exit( EXIT_FAILURE );
319        } // if
320
321        wait( &code );                                                                          // wait for child to finish
322
323        #ifdef __DEBUG_H__
324        cerr << "return code from cpp:" << WEXITSTATUS(code) << endl;
325        #endif // __DEBUG_H__
326
327        if ( WIFSIGNALED(code) != 0 ) {                                         // child failed ?
328                rmtmpfile();                                                                    // remove tmpname
329                cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl;
330                exit( EXIT_FAILURE );
331        } // if
332
333        if ( WEXITSTATUS(code) != 0 ) {                                         // child error ?
334                rmtmpfile();                                                                    // remove tmpname
335                exit( WEXITSTATUS( code ) );                                    // do not continue
336        } // if
337
338        // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
339        // output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
340
341        if ( fork() == 0 ) {                                                            // child runs CFA
342                cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
343
344                // Source file-name used to generate routine names containing global initializations for TU.
345                cargs[ncargs] = ( *new string( "-F" ) ).c_str();
346                ncargs += 1;
347                cargs[ncargs] = ( *new string( string( cpp_in ) ) ).c_str();
348                ncargs += 1;
349
350                cargs[ncargs] = tmpname;
351                ncargs += 1;
352                if ( o_name != NULL ) {
353                        cargs[ncargs] = o_name;
354                        ncargs += 1;
355                } else if ( ! CFA_flag ) {                                              // run cfa-cpp ?
356                        cargs[ncargs] = cpp_out;
357                        ncargs += 1;
358                } // if
359                cargs[ncargs] = NULL;                                                   // terminate argument list
360
361                #ifdef __DEBUG_H__
362                cerr << "cfa-cpp ncargs: " << o_name << " " << CFA_flag << " " << ncargs << endl;
363                for ( int i = 0; cargs[i] != NULL; i += 1 ) {
364                        cerr << cargs[i] << " ";
365                } // for
366                cerr << endl;
367                #endif // __DEBUG_H__
368
369                execvp( cargs[0], (char * const *)cargs );              // should not return
370                perror( "CFA Translator error: cpp level, execvp" );
371                exit( EXIT_FAILURE );
372        } // if
373
374        wait( &code );                                                                          // wait for child to finish
375
376        #ifdef __DEBUG_H__
377        cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
378        #endif // __DEBUG_H__
379
380        // Must unlink here because file must exist across execvp.
381        rmtmpfile();                                                                            // remove tmpname
382
383        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
384                cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl;
385                exit( EXIT_FAILURE );
386        } // if
387
388        exit( WEXITSTATUS(code) );
389} // Stage1
390
391
392void Stage2( const int argc, const char * const * argv ) {
393        string arg;
394
395        const char *cpp_in = NULL;
396
397        const char *args[argc + 100];                                           // leave space for 100 additional cfa command line values
398        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
399
400        #ifdef __DEBUG_H__
401        cerr << "Stage2" << endl;
402        #endif // __DEBUG_H__
403        checkEnv( args, nargs );                                                        // arguments passed via environment variables
404        #ifdef __DEBUG_H__
405        for ( int i = 1; i < argc; i += 1 ) {
406                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
407        } // for
408        #endif // __DEBUG_H__
409
410        // process all the arguments
411
412        for ( int i = 1; i < argc; i += 1 ) {
413                arg = argv[i];
414                if ( prefix( arg, "-" ) ) {
415                        // strip inappropriate flags
416
417                        if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
418                                // Currently CFA does not suppose precompiled .h files.
419                                prefix( arg, "--output-pch" ) ) {
420
421                                // strip inappropriate flags with an argument
422
423                        } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) {
424                                i += 1;
425                                #ifdef __DEBUG_H__
426                                cerr << "arg:\"" << argv[i] << "\"" << endl;
427                                #endif // __DEBUG_H__
428
429                                // all other flags
430
431                        } else {
432                                args[nargs] = argv[i];                                  // pass the flag along
433                                nargs += 1;
434                                if ( arg == "-o" ) {
435                                        i += 1;
436                                        args[nargs] = argv[i];                          // pass the argument along
437                                        nargs += 1;
438                                        #ifdef __DEBUG_H__
439                                        cerr << "arg:\"" << argv[i] << "\"" << endl;
440                                        #endif // __DEBUG_H__
441                                } // if
442                        } // if
443                } else {                                                                                // obtain input and possibly output files
444                        if ( cpp_in == NULL ) {
445                                cpp_in = argv[i];
446                                #ifdef __DEBUG_H__
447                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
448                                #endif // __DEBUG_H__
449                        } else {
450                                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
451                                exit( EXIT_FAILURE );
452                        } // if
453                } // if
454        } // for
455
456        #ifdef __DEBUG_H__
457        cerr << "args:";
458        for ( int i = 1; i < nargs; i += 1 ) {
459                cerr << " " << args[i];
460        } // for
461        cerr << endl;
462        if ( cpp_in != NULL ) cerr << " " << cpp_in;
463        #endif // __DEBUG_H__
464
465        args[0] = compiler_name.c_str();
466        args[nargs] = "-S";                                                                     // only compile and put assembler output in specified file
467        nargs += 1;
468        args[nargs] = cpp_in;
469        nargs += 1;
470        args[nargs] = NULL;                                                                     // terminate argument list
471
472        #ifdef __DEBUG_H__
473        cerr << "stage2 nargs: " << nargs << endl;
474        for ( int i = 0; args[i] != NULL; i += 1 ) {
475                cerr << args[i] << " ";
476        } // for
477        cerr << endl;
478        #endif // __DEBUG_H__
479
480        execvp( args[0], (char * const *)args );                        // should not return
481        perror( "CFA Translator error: cpp level, execvp" );
482        exit( EXIT_FAILURE );                                                           // tell gcc not to go any further
483} // Stage2
484
485
486int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
487        #ifdef __DEBUG_H__
488        for ( int int i = 0; env[i] != NULL; i += 1 ) {
489                cerr << env[i] << endl;
490        } // for
491        #endif // __DEBUG_H__
492
493        string arg = argv[1];
494
495        // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
496
497        if ( arg == "-E" ) {
498                Stage1( argc, argv );
499        } else if ( arg == "-fpreprocessed" ) {
500                Stage2( argc, argv );
501        } else {
502                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
503                exit( EXIT_FAILURE );
504        } // if
505} // main
506
507// Local Variables: //
508// tab-width: 4 //
509// mode: c++ //
510// compile-command: "make install" //
511// End: //
Note: See TracBrowser for help on using the repository browser.