source: src/driver/cfa.cc@ dfee306

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 dfee306 was de62360d, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

fix computed goto, fixed -std=, implicit typedefs for enum and aggregates, add _Noreturn _Thread_local

  • Property mode set to 100644
File size: 11.7 KB
RevLine 
[b87a5ed]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//
[51b73452]7// cfa.cc --
[b87a5ed]8//
[51b73452]9// Author : Peter A. Buhr
10// Created On : Tue Aug 20 13:44:49 2002
11// Last Modified By : Peter A. Buhr
[de62360d]12// Last Modified On : Tue Jun 23 17:47:03 2015
13// Update Count : 119
[b87a5ed]14//
[51b73452]15
16#include <iostream>
[b87a5ed]17#include <cstdio> // perror
18#include <cstdlib> // putenv, exit
19#include <unistd.h> // execvp
20#include <string> // STL version
[51b73452]21
[b87a5ed]22#include "config.h" // configure info
[51b73452]23
24using std::cerr;
25using std::endl;
26using std::string;
27
28
29//#define __DEBUG_H__
30
31
32bool prefix( string arg, string pre ) {
[b87a5ed]33 return arg.substr( 0, pre.size() ) == pre;
[51b73452]34} // prefix
35
36
37void shuffle( const char *args[], int S, int E, int N ) {
[b87a5ed]38 // S & E index 1 passed the end so adjust with -1
[51b73452]39#ifdef __DEBUG_H__
[b87a5ed]40 cerr << "shuffle:" << S << " " << E << " " << N << endl;
[51b73452]41#endif // __DEBUG_H__
[b87a5ed]42 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
[51b73452]43#ifdef __DEBUG_H__
[b87a5ed]44 cerr << "\t" << j << " " << j-N << endl;
[51b73452]45#endif // __DEBUG_H__
[b87a5ed]46 args[j] = args[j-N];
47 } // for
[51b73452]48} // shuffle
49
50
51int main( int argc, char *argv[] ) {
[b87a5ed]52 string Version( VERSION ); // current version number from CONFIG
53 string Major( "0" ), Minor( "0" ), Patch( "0" ); // default version numbers
54 int posn1 = Version.find( "." ); // find the divider between major and minor version numbers
55 if ( posn1 == -1 ) { // not there ?
56 Major = Version;
[51b73452]57 } else {
[b87a5ed]58 Major = Version.substr( 0, posn1 );
59 int posn2 = Version.find( ".", posn1 + 1 ); // find the divider between minor and patch numbers
60 if ( posn2 == -1 ) { // not there ?
61 Minor = Version.substr( posn1 );
62 } else {
63 Minor = Version.substr( posn1 + 1, posn2 - posn1 - 1 );
64 Patch = Version.substr( posn2 + 1 );
65 } // if
[51b73452]66 } // if
67
[00cc023]68 string installincdir( CFA_INCDIR ); // fixed location of include files
69 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands
[51b73452]70
[b87a5ed]71 string heading; // banner printed at start of cfa compilation
72 string arg; // current command-line argument during command-line parsing
73 string Bprefix; // path where gcc looks for compiler command steps
74 string langstd; // language standard
[51b73452]75
[b87a5ed]76 string compiler_path( GCC_PATH ); // path/name of C compiler
77 string compiler_name; // name of C compiler
[51b73452]78
[b87a5ed]79 bool nonoptarg = false; // indicates non-option argument specified
80 bool link = true; // linking as well as compiling
81 bool verbose = false; // -v flag
82 bool quiet = false; // -quiet flag
83 bool debug = true; // -debug flag
84 bool help = false; // -help flag
85 bool CFA_flag = false; // -CFA flag
86 bool cpp_flag = false; // -E or -M flag, preprocessor only
[de62360d]87 bool std_flag = false; // -std= flag
[b87a5ed]88 bool debugging = false; // -g flag
[51b73452]89
[b87a5ed]90 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
91 int sargs = 1; // starting location for arguments in args list
92 int nargs = sargs; // number of arguments in args list; 0 => command name
[51b73452]93
[b87a5ed]94 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
95 int nlibs = 0;
[51b73452]96
97#ifdef __DEBUG_H__
[b87a5ed]98 cerr << "CFA:" << endl;
[51b73452]99#endif // __DEBUG_H__
100
[b87a5ed]101 // process command-line arguments
[51b73452]102
[b87a5ed]103 for ( int i = 1; i < argc; i += 1 ) {
[51b73452]104#ifdef __DEBUG_H__
[b87a5ed]105 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b73452]106#endif // __DEBUG_H__
[b87a5ed]107 arg = argv[i]; // convert to string value
[51b73452]108#ifdef __DEBUG_H__
[b87a5ed]109 cerr << "arg:\"" << arg << "\"" << endl;
[51b73452]110#endif // __DEBUG_H__
[b87a5ed]111 if ( prefix( arg, "-" ) ) {
112 // pass through arguments
113
114 if ( arg == "-Xlinker" || arg == "-o" ) {
115 args[nargs] = argv[i]; // pass the argument along
116 nargs += 1;
117 i += 1;
118 if ( i == argc ) continue; // next argument available ?
119 args[nargs] = argv[i]; // pass the argument along
120 nargs += 1;
121 } else if ( arg == "-XCFA" ) { // CFA pass through
122 i += 1;
123 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
124 nargs += 1;
125
126 // CFA specific arguments
127
128 } else if ( arg == "-CFA" ) {
129 CFA_flag = true; // strip the -CFA flag
130 link = false;
131 args[nargs] = "-E"; // replace the argument with -E
132 nargs += 1;
133 } else if ( arg == "-debug" ) {
134 debug = true; // strip the debug flag
135 } else if ( arg == "-nodebug" ) {
136 debug = false; // strip the nodebug flag
137 } else if ( arg == "-quiet" ) {
138 quiet = true; // strip the quiet flag
139 } else if ( arg == "-noquiet" ) {
140 quiet = false; // strip the noquiet flag
141 } else if ( arg == "-help" ) {
142 help = true; // strip the help flag
143 } else if ( arg == "-nohelp" ) {
144 help = false; // strip the nohelp flag
145 } else if ( arg == "-compiler" ) {
146 // use the user specified compiler
147 i += 1;
148 if ( i == argc ) continue; // next argument available ?
149 compiler_path = argv[i];
150 if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
151 cerr << argv[0] << " error, cannot set environment variable." << endl;
152 exit( EXIT_FAILURE );
153 } // if
154
[de62360d]155 // C specific arguments
[b87a5ed]156
157 } else if ( arg == "-v" ) {
158 verbose = true; // verbosity required
159 args[nargs] = argv[i]; // pass the argument along
160 nargs += 1;
161 } else if ( arg == "-g" ) {
162 debugging = true; // symbolic debugging required
163 args[nargs] = argv[i]; // pass the argument along
164 nargs += 1;
[de62360d]165 } else if ( prefix( arg, "-std=" ) ) {
166 std_flag = true; // std=XX provided
167 args[nargs] = argv[i]; // pass the argument along
168 nargs += 1;
[b87a5ed]169 } else if ( prefix( arg, "-B" ) ) {
170 Bprefix = arg.substr(2); // strip the -B flag
171 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
172 nargs += 1;
173 } else if ( prefix( arg, "-b" ) ) {
174 if ( arg.length() == 2 ) { // separate argument ?
175 i += 1;
176 if ( i == argc ) continue; // next argument available ?
177 arg += argv[i]; // concatenate argument
178 } // if
179 // later versions of gcc require the -b option to appear at the start of the command line
180 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
181 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
182 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
183 cerr << argv[0] << " error, cannot set environment variable." << endl;
184 exit( EXIT_FAILURE );
185 } // if
186 sargs += 1;
187 nargs += 1;
188 } else if ( prefix( arg, "-V" ) ) {
189 if ( arg.length() == 2 ) { // separate argument ?
190 i += 1;
191 if ( i == argc ) continue; // next argument available ?
192 arg += argv[i]; // concatenate argument
193 } // if
194 // later versions of gcc require the -V option to appear at the start of the command line
195 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
196 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
197 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
198 cerr << argv[0] << " error, cannot set environment variable." << endl;
199 exit( EXIT_FAILURE );
200 } // if
201 sargs += 1;
202 nargs += 1;
203 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
204 args[nargs] = argv[i]; // pass the argument along
205 nargs += 1;
206 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
207 cpp_flag = true; // cpp only
208 } // if
209 link = false; // no linkage required
210 } else if ( arg[1] == 'l' ) {
211 // if the user specifies a library, load it after user code
212 libs[nlibs] = argv[i];
213 nlibs += 1;
214 } else {
215 // concatenate any other arguments
216 args[nargs] = argv[i];
217 nargs += 1;
218 } // if
219 } else {
220 // concatenate other arguments
221 args[nargs] = argv[i];
222 nargs += 1;
223 nonoptarg = true;
224 } // if
225 } // for
[51b73452]226
[b87a5ed]227#ifdef __DEBUG_H__
228 cerr << "args:";
229 for ( int i = 1; i < nargs; i += 1 ) {
230 cerr << " " << args[i];
231 } // for
232 cerr << endl;
233#endif // __DEBUG_H__
[51b73452]234
[b87a5ed]235 if ( cpp_flag && CFA_flag ) {
236 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
237 exit( EXIT_FAILURE );
238 } // if
[51b73452]239
[b87a5ed]240 string d;
241 if ( debug ) {
242 d = "-d";
243 } // if
[51b73452]244
[b87a5ed]245 args[nargs] = "-I" CFA_INCDIR;
246 nargs += 1;
[51b73452]247
[b87a5ed]248 if ( link ) {
249 // include the cfa library in case it's needed
250 args[nargs] = "-L" CFA_LIBDIR;
[51b73452]251 nargs += 1;
[b87a5ed]252 args[nargs] = "-lcfa";
[51b73452]253 nargs += 1;
254 } // if
255
[b87a5ed]256 // add the correct set of flags based on the type of compile this is
[8c17ab0]257
[b87a5ed]258 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
[51b73452]259 nargs += 1;
[b87a5ed]260 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
[51b73452]261 nargs += 1;
262
[b87a5ed]263 if ( cpp_flag ) {
264 args[nargs] = "-D__CPP__";
265 nargs += 1;
266 } // if
[51b73452]267
[b87a5ed]268 if ( CFA_flag ) {
269 args[nargs] = "-D__CFA__";
270 nargs += 1;
271 } // if
[51b73452]272
[b87a5ed]273 if ( debug ) {
274 heading += " (debug)";
275 args[nargs] = "-D__CFA_DEBUG__";
276 nargs += 1;
277 } else {
278 heading += " (no debug)";
279 } // if
[51b73452]280
[b87a5ed]281 if ( Bprefix.length() == 0 ) {
282 Bprefix = installlibdir;
283 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
284 nargs += 1;
285 } // if
[51b73452]286
[b87a5ed]287 // execute the compilation command
[51b73452]288
[b87a5ed]289 args[0] = compiler_path.c_str(); // set compiler command for exec
290 // find actual name of the compiler independent of the path to it
291 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
292 if ( p == -1 ) {
293 compiler_name = compiler_path;
294 } else {
295 compiler_name = *new string( compiler_path.substr( p + 1 ) );
296 } // if
[51b73452]297
[b87a5ed]298 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
299 args[nargs] = "-no-integrated-cpp";
300 nargs += 1;
301 args[nargs] = "-Wno-deprecated";
302 nargs += 1;
[de62360d]303 if ( ! std_flag ) { // default c99, if none specified
304 args[nargs] = "-std=c99";
305 nargs += 1;
306 } // if
[b87a5ed]307 args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
308 nargs += 1;
309 } else {
310 cerr << argv[0] << " error, compiler " << compiler_name << " not supported." << endl;
311 exit( EXIT_FAILURE );
312 } // if
[51b73452]313
[b87a5ed]314 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
315 args[nargs] = libs[i];
316 nargs += 1;
317 } // for
[51b73452]318
[b87a5ed]319 args[nargs] = NULL; // terminate with NULL
[51b73452]320
321#ifdef __DEBUG_H__
[b87a5ed]322 cerr << "nargs: " << nargs << endl;
323 cerr << "args:" << endl;
324 for ( int i = 0; args[i] != NULL; i += 1 ) {
325 cerr << " \"" << args[i] << "\"" << endl;
326 } // for
[51b73452]327#endif // __DEBUG_H__
328
[b87a5ed]329 if ( ! quiet ) {
330 cerr << "CFA " << "Version " << Version << heading << endl;
331
332 if ( help ) {
333 cerr <<
334 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
335 "-help\t\t\t: print this help message" << endl <<
336 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
337 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
338 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
339 "...\t\t\t: any other " << compiler_name << " flags" << endl;
340 } // if
[51b73452]341 } // if
342
[b87a5ed]343 if ( verbose ) {
344 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
[51b73452]345
[b87a5ed]346 for ( int i = 0; args[i] != NULL; i += 1 ) {
347 cerr << args[i] << " ";
348 } // for
349 cerr << endl;
350 } // if
[51b73452]351
[b87a5ed]352 if ( ! nonoptarg ) {
353 cerr << argv[0] << " error, no input files" << endl;
354 exit( EXIT_FAILURE );
355 } // if
[51b73452]356
[b87a5ed]357 // execute the command and return the result
[51b73452]358
[b87a5ed]359 execvp( args[0], (char *const *)args ); // should not return
360 perror( "CFA Translator error: cfa level, execvp" );
361 exit( EXIT_FAILURE );
[51b73452]362} // main
363
364// Local Variables: //
[b87a5ed]365// tab-width: 4 //
366// mode: c++ //
[51b73452]367// compile-command: "make install" //
368// End: //
Note: See TracBrowser for help on using the repository browser.