source: driver/cc1.cc @ 8c17ab0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 8c17ab0 was 8c17ab0, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add quoted identifiers, add compilation include directory, reformatted some files

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