source: driver/cc1.cc@ 2c60af75

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum stuck-waitfor-destruct
Last change on this file since 2c60af75 was 2c60af75, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

move cfa-cpp into stage 2 of cc1, and update cfa to facilitate the change

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