source: driver/cc1.cc @ 9aa9126

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 9aa9126 was bf71cfd, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Moved up many directories in source

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