source: driver/cc1.cc@ 643a2e1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since 643a2e1 was 6c3744e, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

add list initializer, formatting changes

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