source: driver/cc1.cc @ 0b8cd722

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 0b8cd722 was d9a0e76, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

remove Parser.old, add -XCFA to driver, copy ptrdiff_t from stddef.h in preclude, remove casts from initialization constants, adjust formatting

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