source: driver/cc1.cc@ 7fdae38

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

formatting

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