159
|
1 // Compiler implementation of the D programming language
|
|
2 // Copyright (c) 1999-2008 by Digital Mars
|
|
3 // All Rights Reserved
|
|
4 // written by Walter Bright
|
|
5 // http://www.digitalmars.com
|
|
6 // License for redistribution is by either the Artistic License
|
|
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
8 // See the included readme.txt for details.
|
|
9
|
|
10 #include "llvm/Target/TargetMachineRegistry.h"
|
|
11
|
|
12 #include <stdio.h>
|
|
13 #include <stdlib.h>
|
|
14 #include <ctype.h>
|
|
15 #include <assert.h>
|
|
16 #include <limits.h>
|
|
17 #include <string>
|
|
18 #include <cstdarg>
|
|
19
|
|
20 #if _WIN32
|
|
21 #include <windows.h>
|
|
22 long __cdecl __ehfilter(LPEXCEPTION_POINTERS ep);
|
|
23 #endif
|
|
24
|
|
25 #if __DMC__
|
|
26 #include <dos.h>
|
|
27 #endif
|
|
28
|
|
29 #if linux
|
|
30 #include <errno.h>
|
|
31 #endif
|
|
32
|
|
33 #include "mem.h"
|
|
34 #include "root.h"
|
|
35
|
|
36 #include "mars.h"
|
|
37 #include "module.h"
|
|
38 #include "mtype.h"
|
|
39 #include "id.h"
|
|
40 #include "cond.h"
|
|
41 #include "expression.h"
|
|
42 #include "lexer.h"
|
|
43
|
|
44 #include "gen/logger.h"
|
|
45
|
|
46 void getenv_setargv(const char *envvar, int *pargc, char** *pargv);
|
|
47
|
|
48 Global global;
|
|
49
|
|
50 Global::Global()
|
|
51 {
|
|
52 mars_ext = "d";
|
|
53 sym_ext = "d";
|
|
54 hdr_ext = "di";
|
|
55 doc_ext = "html";
|
|
56 ddoc_ext = "ddoc";
|
|
57
|
|
58 #if IN_LLVM
|
|
59 obj_ext = "bc";
|
|
60 ll_ext = "ll";
|
|
61 bc_ext = "bc";
|
|
62 nativeobj_ext = "o";
|
|
63 #elif _WIN32
|
|
64 obj_ext = "obj";
|
|
65 #elif linux
|
|
66 obj_ext = "o";
|
|
67 #else
|
|
68 #error "fix this"
|
|
69 #endif
|
|
70
|
|
71 copyright = "Copyright (c) 1999-2008 by Digital Mars and Tomas Lindquist Olsen";
|
|
72 written = "written by Walter Bright and Tomas Lindquist Olsen";
|
|
73 version = "v1.029";
|
|
74 llvmdc_version = "0.1";
|
|
75 global.structalign = 8;
|
|
76
|
|
77 memset(¶ms, 0, sizeof(Param));
|
|
78 }
|
|
79
|
|
80 char *Loc::toChars() const
|
|
81 {
|
|
82 OutBuffer buf;
|
|
83 char *p;
|
|
84
|
|
85 if (filename)
|
|
86 {
|
|
87 buf.printf("%s", filename);
|
|
88 }
|
|
89
|
|
90 if (linnum)
|
|
91 buf.printf("(%d)", linnum);
|
|
92 buf.writeByte(0);
|
|
93 return (char *)buf.extractData();
|
|
94 }
|
|
95
|
|
96 Loc::Loc(Module *mod, unsigned linnum)
|
|
97 {
|
|
98 this->linnum = linnum;
|
|
99 this->filename = mod ? mod->srcfile->toChars() : NULL;
|
|
100 }
|
|
101
|
|
102 /**************************************
|
|
103 * Print error message and exit.
|
|
104 */
|
|
105
|
|
106 void error(Loc loc, const char *format, ...)
|
|
107 {
|
|
108 va_list ap;
|
|
109 va_start(ap, format);
|
|
110 verror(loc, format, ap);
|
|
111 va_end( ap );
|
|
112 }
|
|
113
|
|
114 void verror(Loc loc, const char *format, va_list ap)
|
|
115 {
|
|
116 if (!global.gag)
|
|
117 {
|
|
118 char *p = loc.toChars();
|
|
119
|
|
120 if (*p)
|
|
121 fprintf(stdmsg, "%s: ", p);
|
|
122 mem.free(p);
|
|
123
|
|
124 fprintf(stdmsg, "Error: ");
|
|
125 vfprintf(stdmsg, format, ap);
|
|
126 fprintf(stdmsg, "\n");
|
|
127 fflush(stdmsg);
|
|
128 }
|
|
129 global.errors++;
|
|
130 }
|
|
131
|
|
132 /***************************************
|
|
133 * Call this after printing out fatal error messages to clean up and exit
|
|
134 * the compiler.
|
|
135 */
|
|
136
|
|
137 void fatal()
|
|
138 {
|
|
139 #if 0
|
|
140 halt();
|
|
141 #endif
|
|
142 exit(EXIT_FAILURE);
|
|
143 }
|
|
144
|
|
145 /**************************************
|
|
146 * Try to stop forgetting to remove the breakpoints from
|
|
147 * release builds.
|
|
148 */
|
|
149 void halt()
|
|
150 {
|
|
151 #ifdef DEBUG
|
|
152 *(char*)0=0;
|
|
153 #endif
|
|
154 }
|
|
155
|
|
156 extern void backend_init();
|
|
157 extern void backend_term();
|
|
158
|
|
159 void usage()
|
|
160 {
|
|
161 printf("LLVM D Compiler %s (based on DMD %s and LLVM 2.2)\n%s\n%s\n",
|
|
162 global.llvmdc_version, global.version, global.copyright, global.written);
|
|
163 printf("\
|
|
164 D Language Documentation: http://www.digitalmars.com/d/1.0/index.html\n\
|
|
165 LLVMDC Homepage: http://www.dsource.org/projects/llvmdc\n\
|
|
166 Usage:\n\
|
|
167 dmd files.d ... { -switch }\n\
|
|
168 \n\
|
|
169 files.d D source files\n%s\
|
|
170 -annotate annotate the bitcode with human readable source code\n\
|
|
171 -c do not link\n\
|
|
172 -cov do code coverage analysis\n\
|
|
173 -D generate documentation\n\
|
|
174 -Dd<docdir> write documentation file to <docdir> directory\n\
|
|
175 -Df<filename> write documentation file to <filename>\n\
|
|
176 -d allow deprecated features\n\
|
|
177 -debug compile in debug code\n\
|
|
178 -debug=level compile in debug code <= level\n\
|
|
179 -debug=ident compile in debug code identified by ident\n\
|
|
180 -debuglib=name set symbolic debug library to name\n\
|
|
181 -defaultlib=name set default library to name\n\
|
|
182 -dis disassemble module after compiling\n\
|
|
183 -g add symbolic debug info\n\
|
|
184 -gc add symbolic debug info, pretend to be C\n\
|
|
185 -H generate 'header' file\n\
|
|
186 -Hd<hdrdir> write 'header' file to <hdrdir> directory\n\
|
|
187 -Hf<filename> write 'header' file to <filename>\n\
|
|
188 --help print help\n\
|
|
189 -I<path> where to look for imports\n\
|
|
190 -J<path> where to look for string imports\n\
|
|
191 -ignore ignore unsupported pragmas\n\
|
|
192 -inline do function inlining\n\
|
|
193 -Llinkerflag pass linkerflag to link\n\
|
|
194 -m<arch> emit code specific to <arch>\n\
|
|
195 x86 x86-64 ppc32 ppc64\n\
|
|
196 -nofloat do not emit reference to floating point\n\
|
|
197 -noruntime do not allow code that generates implicit runtime calls\n\
|
|
198 -noverify do not run the validation pass before writing bitcode\n\
|
|
199 -O optimize, same as -O2\n\
|
|
200 -O<n> optimize at level <n> (0-5)\n\
|
|
201 -o- do not write object file\n\
|
|
202 -od<objdir> write object files to directory <objdir>\n\
|
|
203 -of<filename> name output file to <filename>\n\
|
|
204 -op do not strip paths from source file\n\
|
|
205 -profile profile runtime performance of generated code\n\
|
|
206 -quiet suppress unnecessary messages\n\
|
|
207 -release compile release version\n\
|
|
208 -run srcfile args... run resulting program, passing args\n\
|
|
209 -R<path> provide path to the directory containing the runtime library\n\
|
|
210 -unittest compile in unit tests\n\
|
|
211 -v verbose\n\
|
|
212 -vv very verbose (does not include -v)\n\
|
|
213 -v1 D language version 1\n\
|
|
214 -version=level compile in version code >= level\n\
|
|
215 -version=ident compile in version code identified by ident\n\
|
|
216 -w enable warnings\n\
|
|
217 ",
|
|
218 #if WIN32
|
|
219 " @cmdfile read arguments from cmdfile\n"
|
|
220 #else
|
|
221 ""
|
|
222 #endif
|
|
223 );
|
|
224 }
|
|
225
|
|
226 int main(int argc, char *argv[])
|
|
227 {
|
|
228 int i;
|
|
229 Array files;
|
|
230 char *p;
|
|
231 Module *m;
|
|
232 int status = EXIT_SUCCESS;
|
|
233 int argcstart = argc;
|
|
234 char* tt_arch = 0;
|
|
235 char* tt_os = 0;
|
|
236 char* data_layout = 0;
|
|
237 bool very_verbose = false;
|
|
238
|
|
239 // Check for malformed input
|
|
240 if (argc < 1 || !argv)
|
|
241 {
|
|
242 Largs:
|
|
243 error("missing or null command line arguments");
|
|
244 fatal();
|
|
245 }
|
|
246 for (i = 0; i < argc; i++)
|
|
247 {
|
|
248 if (!argv[i])
|
|
249 goto Largs;
|
|
250 }
|
|
251
|
|
252 #if __DMC__ // DMC unique support for response files
|
|
253 if (response_expand(&argc,&argv)) // expand response files
|
|
254 error("can't open response file");
|
|
255 #endif
|
|
256
|
|
257 files.reserve(argc - 1);
|
|
258
|
|
259 // Set default values
|
|
260 global.params.argv0 = argv[0];
|
|
261 global.params.link = 1;
|
|
262 global.params.useAssert = 0;
|
|
263 global.params.useInvariants = 0;
|
|
264 global.params.useIn = 1;
|
|
265 global.params.useOut = 1;
|
|
266 global.params.useArrayBounds = 0;
|
|
267 global.params.useSwitchError = 0;
|
|
268 global.params.useInline = 0; // this one messes things up to a point where codegen breaks
|
|
269 global.params.llvmInline = 0; // use this one instead to know if inline passes should be run
|
|
270 global.params.obj = 1;
|
|
271 global.params.Dversion = 2;
|
|
272 global.params.quiet = 1;
|
|
273
|
|
274 global.params.linkswitches = new Array();
|
|
275 global.params.libfiles = new Array();
|
|
276 global.params.objfiles = new Array();
|
|
277 global.params.ddocfiles = new Array();
|
|
278
|
|
279 global.params.is64bit = sizeof(void*) == 8 ? 1 : 0;
|
|
280
|
|
281 uint16_t endiantest = 0xFF00;
|
|
282 uint8_t endianres = ((uint8_t*)&endiantest)[0];
|
|
283 if (endianres == 0x00)
|
|
284 global.params.isLE = true;
|
|
285 else if (endianres == 0xFF)
|
|
286 global.params.isLE = false;
|
|
287 else {
|
|
288 error("Endian test is broken");
|
|
289 fatal();
|
|
290 }
|
|
291
|
|
292 global.params.llvmArch = 0;
|
|
293 global.params.forceBE = 0;
|
|
294 global.params.noruntime = 0;
|
|
295 global.params.novalidate = 0;
|
|
296 global.params.optimizeLevel = -1;
|
|
297 global.params.runtimeImppath = 0;
|
|
298
|
|
299 global.params.defaultlibname = "phobos";
|
|
300 global.params.debuglibname = global.params.defaultlibname;
|
|
301
|
|
302 // Predefine version identifiers
|
|
303 #if IN_LLVM
|
|
304 VersionCondition::addPredefinedGlobalIdent("LLVM");
|
|
305 VersionCondition::addPredefinedGlobalIdent("LLVMDC");
|
|
306 VersionCondition::addPredefinedGlobalIdent("Posix");
|
|
307 #endif
|
|
308 #if _WIN32
|
|
309 VersionCondition::addPredefinedGlobalIdent("Windows");
|
|
310 VersionCondition::addPredefinedGlobalIdent("Win32");
|
|
311 global.params.isWindows = 1;
|
|
312 #elif linux
|
|
313 VersionCondition::addPredefinedGlobalIdent("linux");
|
|
314 global.params.isLinux = 1;
|
|
315 tt_os = "-unknown-linux-gnu";
|
|
316 #else
|
|
317 #error
|
|
318 #endif /* linux */
|
|
319
|
|
320 //VersionCondition::addPredefinedGlobalIdent("D_Bits");
|
|
321 //VersionCondition::addPredefinedGlobalIdent("D_InlineAsm");
|
|
322 //VersionCondition::addPredefinedGlobalIdent("D_InlineAsm_X86");
|
|
323 VersionCondition::addPredefinedGlobalIdent("all");
|
|
324
|
|
325 #if _WIN32
|
|
326 inifile(argv[0], "llvmdc.ini");
|
|
327 #endif
|
|
328 #if linux
|
|
329 inifile(argv[0], "llvmdc.conf");
|
|
330 #else
|
|
331 #error
|
|
332 #endif
|
|
333 getenv_setargv("DFLAGS", &argc, &argv);
|
|
334
|
|
335 #if 0
|
|
336 for (i = 0; i < argc; i++)
|
|
337 {
|
|
338 printf("argv[%d] = '%s'\n", i, argv[i]);
|
|
339 }
|
|
340 #endif
|
|
341
|
|
342 for (i = 1; i < argc; i++)
|
|
343 {
|
|
344 p = argv[i];
|
|
345 if (*p == '-')
|
|
346 {
|
|
347 if (strcmp(p + 1, "d") == 0)
|
|
348 global.params.useDeprecated = 1;
|
|
349 else if (strcmp(p + 1, "c") == 0)
|
|
350 global.params.link = 0;
|
|
351 else if (strcmp(p + 1, "cov") == 0)
|
|
352 global.params.cov = 1;
|
|
353 else if (strcmp(p + 1, "fPIC") == 0)
|
|
354 global.params.pic = 1;
|
|
355 else if (strcmp(p + 1, "g") == 0)
|
|
356 global.params.symdebug = 1;
|
|
357 else if (strcmp(p + 1, "gc") == 0)
|
|
358 global.params.symdebug = 2;
|
|
359 else if (strcmp(p + 1, "gt") == 0)
|
|
360 { error("use -profile instead of -gt\n");
|
|
361 global.params.trace = 1;
|
|
362 }
|
|
363 else if (strcmp(p + 1, "profile") == 0)
|
|
364 global.params.trace = 1;
|
|
365 else if (strcmp(p + 1, "v") == 0)
|
|
366 global.params.verbose = 1;
|
|
367 else if (strcmp(p + 1, "vv") == 0) {
|
|
368 Logger::enable();
|
|
369 very_verbose = true;
|
|
370 }
|
|
371 else if (strcmp(p + 1, "v1") == 0)
|
|
372 global.params.Dversion = 1;
|
|
373 else if (strcmp(p + 1, "w") == 0)
|
|
374 global.params.warnings = 1;
|
|
375 else if (p[1] == 'O')
|
|
376 {
|
|
377 global.params.optimize = 1;
|
|
378 global.params.optimizeLevel = 2;
|
|
379 if (p[2] != 0) {
|
|
380 int optlevel = atoi(p+2);
|
|
381 if (optlevel < 0 || optlevel > 5) {
|
|
382 error("Optimization level must be between 0 and 5. Using default (%d)",
|
|
383 global.params.optimizeLevel);
|
|
384 }
|
|
385 else {
|
|
386 global.params.optimizeLevel = optlevel;
|
|
387 }
|
|
388 }
|
|
389 }
|
|
390 else if (strcmp(p + 1, "forcebe") == 0)
|
|
391 global.params.forceBE = 1;
|
|
392 else if (strcmp(p + 1, "noruntime") == 0)
|
|
393 global.params.noruntime = 1;
|
|
394 else if (strcmp(p + 1, "noverify") == 0)
|
|
395 global.params.novalidate = 1;
|
|
396 else if (strcmp(p + 1, "dis") == 0)
|
|
397 global.params.disassemble = 1;
|
|
398 else if (strcmp(p + 1, "annotate") == 0)
|
|
399 global.params.llvmAnnotate = 1;
|
|
400 else if (p[1] == 'o')
|
|
401 {
|
|
402 switch (p[2])
|
|
403 {
|
|
404 case '-':
|
|
405 global.params.obj = 0;
|
|
406 break;
|
|
407
|
|
408 case 'd':
|
|
409 if (!p[3])
|
|
410 goto Lnoarg;
|
|
411 global.params.objdir = p + 3;
|
|
412 break;
|
|
413
|
|
414 case 'f':
|
|
415 if (!p[3])
|
|
416 goto Lnoarg;
|
|
417 global.params.objname = p + 3;
|
|
418 break;
|
|
419
|
|
420 case 'p':
|
|
421 if (p[3])
|
|
422 goto Lerror;
|
|
423 global.params.preservePaths = 1;
|
|
424 break;
|
|
425
|
|
426 case 0:
|
|
427 error("-o no longer supported, use -of or -od");
|
|
428 break;
|
|
429
|
|
430 default:
|
|
431 goto Lerror;
|
|
432 }
|
|
433 }
|
|
434 else if (p[1] == 'D')
|
|
435 { global.params.doDocComments = 1;
|
|
436 switch (p[2])
|
|
437 {
|
|
438 case 'd':
|
|
439 if (!p[3])
|
|
440 goto Lnoarg;
|
|
441 global.params.docdir = p + 3;
|
|
442 break;
|
|
443 case 'f':
|
|
444 if (!p[3])
|
|
445 goto Lnoarg;
|
|
446 global.params.docname = p + 3;
|
|
447 break;
|
|
448
|
|
449 case 0:
|
|
450 break;
|
|
451
|
|
452 default:
|
|
453 goto Lerror;
|
|
454 }
|
|
455 }
|
|
456 #ifdef _DH
|
|
457 else if (p[1] == 'H')
|
|
458 { global.params.doHdrGeneration = 1;
|
|
459 switch (p[2])
|
|
460 {
|
|
461 case 'd':
|
|
462 if (!p[3])
|
|
463 goto Lnoarg;
|
|
464 global.params.hdrdir = p + 3;
|
|
465 break;
|
|
466
|
|
467 case 'f':
|
|
468 if (!p[3])
|
|
469 goto Lnoarg;
|
|
470 global.params.hdrname = p + 3;
|
|
471 break;
|
|
472
|
|
473 case 0:
|
|
474 break;
|
|
475
|
|
476 default:
|
|
477 goto Lerror;
|
|
478 }
|
|
479 }
|
|
480 #endif
|
|
481 else if (strcmp(p + 1, "ignore") == 0)
|
|
482 global.params.ignoreUnsupportedPragmas = 1;
|
|
483 else if (strcmp(p + 1, "inline") == 0) {
|
|
484 // TODO
|
|
485 // the ast rewrites dmd does for inlining messes up the ast.
|
|
486 // someday maybe we can support it, for now llvm does an excellent job at inlining
|
|
487 global.params.useInline = 0; //1
|
|
488 global.params.llvmInline = 1;
|
|
489 }
|
|
490 else if (strcmp(p + 1, "nofloat") == 0)
|
|
491 global.params.nofloat = 1;
|
|
492 else if (strcmp(p + 1, "quiet") == 0)
|
|
493 global.params.quiet = 1;
|
|
494 else if (strcmp(p + 1, "release") == 0)
|
|
495 global.params.release = 1;
|
|
496 else if (strcmp(p + 1, "unittest") == 0)
|
|
497 global.params.useUnitTests = 1;
|
|
498 else if (p[1] == 'I')
|
|
499 {
|
|
500 if (!global.params.imppath)
|
|
501 global.params.imppath = new Array();
|
|
502 global.params.imppath->push(p + 2);
|
|
503 }
|
|
504 else if (p[1] == 'J')
|
|
505 {
|
|
506 if (!global.params.fileImppath)
|
|
507 global.params.fileImppath = new Array();
|
|
508 global.params.fileImppath->push(p + 2);
|
|
509 }
|
|
510 else if (p[1] == 'R')
|
|
511 {
|
|
512 global.params.runtimePath = p+2;
|
|
513 }
|
|
514 else if (memcmp(p + 1, "debug", 5) == 0 && p[6] != 'l')
|
|
515 {
|
|
516 // Parse:
|
|
517 // -debug
|
|
518 // -debug=number
|
|
519 // -debug=identifier
|
|
520 if (p[6] == '=')
|
|
521 {
|
|
522 if (isdigit(p[7]))
|
|
523 { long level;
|
|
524
|
|
525 errno = 0;
|
|
526 level = strtol(p + 7, &p, 10);
|
|
527 if (*p || errno || level > INT_MAX)
|
|
528 goto Lerror;
|
|
529 DebugCondition::setGlobalLevel((int)level);
|
|
530 }
|
|
531 else if (Lexer::isValidIdentifier(p + 7))
|
|
532 DebugCondition::addGlobalIdent(p + 7);
|
|
533 else
|
|
534 goto Lerror;
|
|
535 }
|
|
536 else if (p[6])
|
|
537 goto Lerror;
|
|
538 else
|
|
539 global.params.debuglevel = 1;
|
|
540 }
|
|
541 else if (memcmp(p + 1, "version", 5) == 0)
|
|
542 {
|
|
543 // Parse:
|
|
544 // -version=number
|
|
545 // -version=identifier
|
|
546 if (p[8] == '=')
|
|
547 {
|
|
548 if (isdigit(p[9]))
|
|
549 { long level;
|
|
550
|
|
551 errno = 0;
|
|
552 level = strtol(p + 9, &p, 10);
|
|
553 if (*p || errno || level > INT_MAX)
|
|
554 goto Lerror;
|
|
555 VersionCondition::setGlobalLevel((int)level);
|
|
556 }
|
|
557 else if (Lexer::isValidIdentifier(p + 9))
|
|
558 VersionCondition::addGlobalIdent(p + 9);
|
|
559 else
|
|
560 goto Lerror;
|
|
561 }
|
|
562 else
|
|
563 goto Lerror;
|
|
564 }
|
|
565 else if (strcmp(p + 1, "-b") == 0)
|
|
566 global.params.debugb = 1;
|
|
567 else if (strcmp(p + 1, "-c") == 0)
|
|
568 global.params.debugc = 1;
|
|
569 else if (strcmp(p + 1, "-f") == 0)
|
|
570 global.params.debugf = 1;
|
|
571 else if (strcmp(p + 1, "-help") == 0)
|
|
572 { usage();
|
|
573 exit(EXIT_SUCCESS);
|
|
574 }
|
|
575 else if (strcmp(p + 1, "-r") == 0)
|
|
576 global.params.debugr = 1;
|
|
577 else if (strcmp(p + 1, "-x") == 0)
|
|
578 global.params.debugx = 1;
|
|
579 else if (strcmp(p + 1, "-y") == 0)
|
|
580 global.params.debugy = 1;
|
|
581 else if (p[1] == 'L')
|
|
582 {
|
|
583 global.params.linkswitches->push(p + 2);
|
|
584 }
|
|
585 else if (memcmp(p + 1, "defaultlib=", 11) == 0)
|
|
586 {
|
|
587 global.params.defaultlibname = p + 1 + 11;
|
|
588 }
|
|
589 else if (memcmp(p + 1, "debuglib=", 9) == 0)
|
|
590 {
|
|
591 global.params.debuglibname = p + 1 + 9;
|
|
592 }
|
|
593 else if (strcmp(p + 1, "run") == 0)
|
|
594 { global.params.run = 1;
|
|
595 global.params.runargs_length = ((i >= argcstart) ? argc : argcstart) - i - 1;
|
|
596 if (global.params.runargs_length)
|
|
597 {
|
|
598 files.push(argv[i + 1]);
|
|
599 global.params.runargs = &argv[i + 2];
|
|
600 i += global.params.runargs_length;
|
|
601 global.params.runargs_length--;
|
|
602 }
|
|
603 else
|
|
604 { global.params.run = 0;
|
|
605 goto Lnoarg;
|
|
606 }
|
|
607 }
|
|
608 else if (p[1] == 'm')
|
|
609 {
|
|
610 global.params.llvmArch = p+2;
|
|
611 }
|
|
612 else
|
|
613 {
|
|
614 Lerror:
|
|
615 error("unrecognized switch '%s'", argv[i]);
|
|
616 continue;
|
|
617
|
|
618 Lnoarg:
|
|
619 error("argument expected for switch '%s'", argv[i]);
|
|
620 continue;
|
|
621 }
|
|
622 }
|
|
623 else
|
|
624 files.push(p);
|
|
625 }
|
|
626 if (global.errors)
|
|
627 {
|
|
628 fatal();
|
|
629 }
|
|
630 if (files.dim == 0)
|
|
631 { usage();
|
|
632 return EXIT_FAILURE;
|
|
633 }
|
|
634
|
|
635 if (global.params.release)
|
|
636 { global.params.useInvariants = 0;
|
|
637 global.params.useIn = 0;
|
|
638 global.params.useOut = 0;
|
|
639 global.params.useAssert = 0;
|
|
640 global.params.useArrayBounds = 0;
|
|
641 global.params.useSwitchError = 0;
|
|
642 }
|
|
643
|
|
644 if (global.params.run)
|
|
645 global.params.quiet = 1;
|
|
646
|
|
647 if (global.params.useUnitTests)
|
|
648 global.params.useAssert = 1;
|
|
649
|
|
650 if (!global.params.obj)
|
|
651 global.params.link = 0;
|
|
652
|
|
653 if (global.params.link)
|
|
654 {
|
|
655 global.params.exefile = global.params.objname;
|
|
656 global.params.objname = NULL;
|
|
657 }
|
|
658 else if (global.params.run)
|
|
659 {
|
|
660 error("flags conflict with -run");
|
|
661 fatal();
|
|
662 }
|
|
663 else
|
|
664 {
|
|
665 if (global.params.objname && files.dim > 1)
|
|
666 {
|
|
667 error("multiple source files, but only one .obj name");
|
|
668 fatal();
|
|
669 }
|
|
670 }
|
|
671 if (global.params.cov)
|
|
672 VersionCondition::addPredefinedGlobalIdent("D_Coverage");
|
|
673
|
|
674 bool allowForceEndianness = false;
|
|
675
|
|
676 if (global.params.llvmArch == 0) {
|
|
677 std::string err_str;
|
|
678 const llvm::TargetMachineRegistry::entry* e = llvm::TargetMachineRegistry::getClosestTargetForJIT(err_str);
|
|
679 if (e == 0) {
|
|
680 error("Failed to find a default target machine: %s", err_str.c_str());
|
|
681 fatal();
|
|
682 }
|
|
683 else {
|
|
684 global.params.llvmArch = const_cast<char*>(e->Name);
|
|
685 if (global.params.verbose || very_verbose)
|
|
686 printf("Default target found: %s\n", global.params.llvmArch);
|
|
687 if (very_verbose) {
|
|
688 int X = sizeof(va_list);
|
|
689 printf("valist.sizeof = %d\n", X);
|
|
690 }
|
|
691 }
|
|
692 }
|
|
693
|
|
694 if (strcmp(global.params.llvmArch,"x86")==0) {
|
|
695 VersionCondition::addPredefinedGlobalIdent("X86");
|
|
696 //VersionCondition::addPredefinedGlobalIdent("LLVM_InlineAsm_X86");
|
|
697 global.params.isLE = true;
|
|
698 global.params.is64bit = false;
|
|
699 tt_arch = "i686";
|
|
700 data_layout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:8";
|
|
701 }
|
|
702 else if (strcmp(global.params.llvmArch,"x86-64")==0) {
|
|
703 VersionCondition::addPredefinedGlobalIdent("X86_64");
|
|
704 //VersionCondition::addPredefinedGlobalIdent("LLVM_InlineAsm_X86_64");
|
|
705 global.params.isLE = true;
|
|
706 global.params.is64bit = true;
|
|
707 tt_arch = "x86_64";
|
|
708 data_layout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:8";
|
|
709 }
|
|
710 else if (strcmp(global.params.llvmArch,"ppc32")==0) {
|
|
711 VersionCondition::addPredefinedGlobalIdent("PPC");
|
|
712 global.params.isLE = false;
|
|
713 global.params.is64bit = false;
|
|
714 tt_arch = "powerpc";
|
|
715 data_layout = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:8";
|
|
716 }
|
|
717 else if (strcmp(global.params.llvmArch,"ppc64")==0) {
|
|
718 VersionCondition::addPredefinedGlobalIdent("PPC64");
|
|
719 global.params.isLE = false;
|
|
720 global.params.is64bit = true;
|
|
721 tt_arch = "powerpc64";
|
|
722 data_layout = "E-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:8";
|
|
723 }
|
|
724 else {
|
|
725 assert(0 && "Invalid arch");
|
|
726 }
|
|
727
|
|
728 if (allowForceEndianness && global.params.forceBE) {
|
|
729 VersionCondition::addPredefinedGlobalIdent("BigEndian");
|
|
730 global.params.isLE = false;
|
|
731 }
|
|
732 else if (global.params.isLE) {
|
|
733 VersionCondition::addPredefinedGlobalIdent("LittleEndian");
|
|
734 }
|
|
735 else {
|
|
736 VersionCondition::addPredefinedGlobalIdent("BigEndian");
|
|
737 }
|
|
738
|
|
739 if (global.params.is64bit) {
|
|
740 VersionCondition::addPredefinedGlobalIdent("LLVM64");
|
|
741 }
|
|
742
|
|
743 assert(tt_arch != 0);
|
|
744 assert(tt_os != 0);
|
|
745 assert(data_layout != 0);
|
|
746 global.params.tt_arch = tt_arch;
|
|
747 global.params.tt_os = tt_os;
|
|
748 global.params.data_layout = data_layout;
|
|
749
|
|
750 // Initialization
|
|
751 Type::init();
|
|
752 Id::initialize();
|
|
753 Module::init();
|
|
754 initPrecedence();
|
|
755
|
|
756 backend_init();
|
|
757
|
|
758 //printf("%d source files\n",files.dim);
|
|
759
|
|
760 // Build import search path
|
|
761 if (global.params.imppath)
|
|
762 {
|
|
763 for (i = 0; i < global.params.imppath->dim; i++)
|
|
764 {
|
|
765 char *path = (char *)global.params.imppath->data[i];
|
|
766 Array *a = FileName::splitPath(path);
|
|
767
|
|
768 if (a)
|
|
769 {
|
|
770 if (!global.path)
|
|
771 global.path = new Array();
|
|
772 global.path->append(a);
|
|
773 }
|
|
774 }
|
|
775 }
|
|
776
|
|
777 // Build string import search path
|
|
778 if (global.params.fileImppath)
|
|
779 {
|
|
780 for (i = 0; i < global.params.fileImppath->dim; i++)
|
|
781 {
|
|
782 char *path = (char *)global.params.fileImppath->data[i];
|
|
783 Array *a = FileName::splitPath(path);
|
|
784
|
|
785 if (a)
|
|
786 {
|
|
787 if (!global.filePath)
|
|
788 global.filePath = new Array();
|
|
789 global.filePath->append(a);
|
|
790 }
|
|
791 }
|
|
792 }
|
|
793
|
|
794 // Create Modules
|
|
795 Array modules;
|
|
796 modules.reserve(files.dim);
|
|
797 for (i = 0; i < files.dim; i++)
|
|
798 { Identifier *id;
|
|
799 char *ext;
|
|
800 char *name;
|
|
801
|
|
802 p = (char *) files.data[i];
|
|
803
|
|
804 #if _WIN32
|
|
805 // Convert / to \ so linker will work
|
|
806 for (int i = 0; p[i]; i++)
|
|
807 {
|
|
808 if (p[i] == '/')
|
|
809 p[i] = '\\';
|
|
810 }
|
|
811 #endif
|
|
812
|
|
813 p = FileName::name(p); // strip path
|
|
814 ext = FileName::ext(p);
|
|
815 if (ext)
|
|
816 {
|
|
817 #if IN_LLVM
|
|
818 if (strcmp(ext, global.nativeobj_ext) == 0 ||
|
|
819 strcmp(ext, global.obj_ext) == 0)
|
|
820 #elif TARGET_LINUX
|
|
821 if (strcmp(ext, global.obj_ext) == 0)
|
|
822 #else
|
|
823 if (stricmp(ext, global.obj_ext) == 0)
|
|
824 #endif
|
|
825 {
|
|
826 global.params.objfiles->push(files.data[i]);
|
|
827 continue;
|
|
828 }
|
|
829
|
|
830 #if TARGET_LINUX
|
|
831 if (strcmp(ext, "a") == 0)
|
|
832 #else
|
|
833 if (stricmp(ext, "lib") == 0)
|
|
834 #endif
|
|
835 {
|
|
836 global.params.libfiles->push(files.data[i]);
|
|
837 continue;
|
|
838 }
|
|
839
|
|
840 if (strcmp(ext, global.ddoc_ext) == 0)
|
|
841 {
|
|
842 global.params.ddocfiles->push(files.data[i]);
|
|
843 continue;
|
|
844 }
|
|
845
|
|
846 #if !TARGET_LINUX
|
|
847 if (stricmp(ext, "res") == 0)
|
|
848 {
|
|
849 global.params.resfile = (char *)files.data[i];
|
|
850 continue;
|
|
851 }
|
|
852
|
|
853 if (stricmp(ext, "def") == 0)
|
|
854 {
|
|
855 global.params.deffile = (char *)files.data[i];
|
|
856 continue;
|
|
857 }
|
|
858
|
|
859 if (stricmp(ext, "exe") == 0)
|
|
860 {
|
|
861 global.params.exefile = (char *)files.data[i];
|
|
862 continue;
|
|
863 }
|
|
864 #endif
|
|
865
|
|
866 if (stricmp(ext, global.mars_ext) == 0 ||
|
|
867 stricmp(ext, "htm") == 0 ||
|
|
868 stricmp(ext, "html") == 0 ||
|
|
869 stricmp(ext, "xhtml") == 0)
|
|
870 {
|
|
871 ext--; // skip onto '.'
|
|
872 assert(*ext == '.');
|
|
873 name = (char *)mem.malloc((ext - p) + 1);
|
|
874 memcpy(name, p, ext - p);
|
|
875 name[ext - p] = 0; // strip extension
|
|
876
|
|
877 if (name[0] == 0 ||
|
|
878 strcmp(name, "..") == 0 ||
|
|
879 strcmp(name, ".") == 0)
|
|
880 {
|
|
881 Linvalid:
|
|
882 error("invalid file name '%s'", (char *)files.data[i]);
|
|
883 fatal();
|
|
884 }
|
|
885 }
|
|
886 else
|
|
887 { error("unrecognized file extension %s\n", ext);
|
|
888 fatal();
|
|
889 }
|
|
890 }
|
|
891 else
|
|
892 { name = p;
|
|
893 if (!*name)
|
|
894 goto Linvalid;
|
|
895 }
|
|
896
|
|
897 id = new Identifier(name, 0);
|
|
898 m = new Module((char *) files.data[i], id, global.params.doDocComments, global.params.doHdrGeneration);
|
|
899 modules.push(m);
|
|
900
|
|
901 global.params.objfiles->push(m->objfile->name->str);
|
|
902 }
|
|
903
|
|
904 #if _WIN32
|
|
905 __try
|
|
906 {
|
|
907 #endif
|
|
908 // Read files, parse them
|
|
909 for (i = 0; i < modules.dim; i++)
|
|
910 {
|
|
911 m = (Module *)modules.data[i];
|
|
912 if (global.params.verbose)
|
|
913 printf("parse %s\n", m->toChars());
|
|
914 if (!Module::rootModule)
|
|
915 Module::rootModule = m;
|
|
916 m->importedFrom = m;
|
|
917 m->deleteObjFile();
|
|
918 m->read(0);
|
|
919 m->parse();
|
|
920 if (m->isDocFile)
|
|
921 {
|
|
922 m->gendocfile();
|
|
923
|
|
924 // Remove m from list of modules
|
|
925 modules.remove(i);
|
|
926 i--;
|
|
927
|
|
928 // Remove m's object file from list of object files
|
|
929 for (int j = 0; j < global.params.objfiles->dim; j++)
|
|
930 {
|
|
931 if (m->objfile->name->str == global.params.objfiles->data[j])
|
|
932 {
|
|
933 global.params.objfiles->remove(j);
|
|
934 break;
|
|
935 }
|
|
936 }
|
|
937
|
|
938 if (global.params.objfiles->dim == 0)
|
|
939 global.params.link = 0;
|
|
940 }
|
|
941 }
|
|
942 if (global.errors)
|
|
943 fatal();
|
|
944 #ifdef _DH
|
|
945 if (global.params.doHdrGeneration)
|
|
946 {
|
|
947 /* Generate 'header' import files.
|
|
948 * Since 'header' import files must be independent of command
|
|
949 * line switches and what else is imported, they are generated
|
|
950 * before any semantic analysis.
|
|
951 */
|
|
952 for (i = 0; i < modules.dim; i++)
|
|
953 {
|
|
954 m = (Module *)modules.data[i];
|
|
955 if (global.params.verbose)
|
|
956 printf("import %s\n", m->toChars());
|
|
957 m->genhdrfile();
|
|
958 }
|
|
959 }
|
|
960 if (global.errors)
|
|
961 fatal();
|
|
962 #endif
|
|
963
|
|
964 // Do semantic analysis
|
|
965 for (i = 0; i < modules.dim; i++)
|
|
966 {
|
|
967 m = (Module *)modules.data[i];
|
|
968 if (global.params.verbose)
|
|
969 printf("semantic %s\n", m->toChars());
|
|
970 m->semantic();
|
|
971 }
|
|
972 if (global.errors)
|
|
973 fatal();
|
|
974
|
|
975 // Do pass 2 semantic analysis
|
|
976 for (i = 0; i < modules.dim; i++)
|
|
977 {
|
|
978 m = (Module *)modules.data[i];
|
|
979 if (global.params.verbose)
|
|
980 printf("semantic2 %s\n", m->toChars());
|
|
981 m->semantic2();
|
|
982 }
|
|
983 if (global.errors)
|
|
984 fatal();
|
|
985
|
|
986 // Do pass 3 semantic analysis
|
|
987 for (i = 0; i < modules.dim; i++)
|
|
988 {
|
|
989 m = (Module *)modules.data[i];
|
|
990 if (global.params.verbose)
|
|
991 printf("semantic3 %s\n", m->toChars());
|
|
992 m->semantic3();
|
|
993 }
|
|
994 if (global.errors)
|
|
995 fatal();
|
|
996
|
|
997 #if !IN_LLVM
|
|
998 // Scan for functions to inline
|
|
999 if (global.params.useInline)
|
|
1000 {
|
|
1001 /* The problem with useArrayBounds and useAssert is that the
|
|
1002 * module being linked to may not have generated them, so if
|
|
1003 * we inline functions from those modules, the symbols for them will
|
|
1004 * not be found at link time.
|
|
1005 */
|
|
1006 if (!global.params.useArrayBounds && !global.params.useAssert)
|
|
1007 {
|
|
1008 #endif
|
|
1009 // Do pass 3 semantic analysis on all imported modules,
|
|
1010 // since otherwise functions in them cannot be inlined
|
|
1011 for (i = 0; i < Module::amodules.dim; i++)
|
|
1012 {
|
|
1013 m = (Module *)Module::amodules.data[i];
|
|
1014 if (global.params.verbose)
|
|
1015 printf("semantic3 %s\n", m->toChars());
|
|
1016 m->semantic3();
|
|
1017 }
|
|
1018 if (global.errors)
|
|
1019 fatal();
|
|
1020 #if !IN_LLVM
|
|
1021 }
|
|
1022
|
|
1023 for (i = 0; i < modules.dim; i++)
|
|
1024 {
|
|
1025 m = (Module *)modules.data[i];
|
|
1026 if (global.params.verbose)
|
|
1027 printf("inline scan %s\n", m->toChars());
|
|
1028 m->inlineScan();
|
|
1029 }
|
|
1030 }
|
|
1031 if (global.errors)
|
|
1032 fatal();
|
|
1033 #endif
|
|
1034
|
|
1035 // Generate output files
|
|
1036 for (i = 0; i < modules.dim; i++)
|
|
1037 {
|
|
1038 m = (Module *)modules.data[i];
|
|
1039 if (global.params.verbose)
|
|
1040 printf("code %s\n", m->toChars());
|
|
1041 if (global.params.obj)
|
|
1042 m->genobjfile();
|
|
1043 if (global.errors)
|
|
1044 m->deleteObjFile();
|
|
1045 else
|
|
1046 {
|
|
1047 if (global.params.doDocComments)
|
|
1048 m->gendocfile();
|
|
1049 }
|
|
1050 }
|
|
1051 #if _WIN32
|
|
1052 }
|
|
1053 __except (__ehfilter(GetExceptionInformation()))
|
|
1054 {
|
|
1055 printf("Stack overflow\n");
|
|
1056 fatal();
|
|
1057 }
|
|
1058 #endif
|
|
1059 backend_term();
|
|
1060 if (global.errors)
|
|
1061 fatal();
|
|
1062
|
|
1063 if (!global.params.objfiles->dim)
|
|
1064 {
|
|
1065 if (global.params.link)
|
|
1066 error("no object files to link");
|
|
1067 }
|
|
1068 else
|
|
1069 {
|
|
1070 if (global.params.link)
|
|
1071 status = runLINK();
|
|
1072
|
|
1073 if (global.params.run)
|
|
1074 {
|
|
1075 if (!status)
|
|
1076 {
|
|
1077 status = runProgram();
|
|
1078
|
|
1079 /* Delete .obj files and .exe file
|
|
1080 */
|
|
1081 for (i = 0; i < modules.dim; i++)
|
|
1082 {
|
|
1083 m = (Module *)modules.data[i];
|
|
1084 m->deleteObjFile();
|
|
1085 }
|
|
1086 deleteExeFile();
|
|
1087 }
|
|
1088 }
|
|
1089 }
|
|
1090
|
|
1091 return status;
|
|
1092 }
|
|
1093
|
|
1094
|
|
1095
|
|
1096 /***********************************
|
|
1097 * Parse and append contents of environment variable envvar
|
|
1098 * to argc and argv[].
|
|
1099 * The string is separated into arguments, processing \ and ".
|
|
1100 */
|
|
1101
|
|
1102 void getenv_setargv(const char *envvar, int *pargc, char** *pargv)
|
|
1103 {
|
|
1104 char *env;
|
|
1105 char *p;
|
|
1106 Array *argv;
|
|
1107 int argc;
|
|
1108
|
|
1109 int wildcard; // do wildcard expansion
|
|
1110 int instring;
|
|
1111 int slash;
|
|
1112 char c;
|
|
1113 int j;
|
|
1114
|
|
1115 env = getenv(envvar);
|
|
1116 if (!env)
|
|
1117 return;
|
|
1118
|
|
1119 env = mem.strdup(env); // create our own writable copy
|
|
1120
|
|
1121 argc = *pargc;
|
|
1122 argv = new Array();
|
|
1123 argv->setDim(argc);
|
|
1124
|
|
1125 for (int i = 0; i < argc; i++)
|
|
1126 argv->data[i] = (void *)(*pargv)[i];
|
|
1127
|
|
1128 j = 1; // leave argv[0] alone
|
|
1129 while (1)
|
|
1130 {
|
|
1131 wildcard = 1;
|
|
1132 switch (*env)
|
|
1133 {
|
|
1134 case ' ':
|
|
1135 case '\t':
|
|
1136 env++;
|
|
1137 break;
|
|
1138
|
|
1139 case 0:
|
|
1140 goto Ldone;
|
|
1141
|
|
1142 case '"':
|
|
1143 wildcard = 0;
|
|
1144 default:
|
|
1145 argv->push(env); // append
|
|
1146 //argv->insert(j, env); // insert at position j
|
|
1147 j++;
|
|
1148 argc++;
|
|
1149 p = env;
|
|
1150 slash = 0;
|
|
1151 instring = 0;
|
|
1152 c = 0;
|
|
1153
|
|
1154 while (1)
|
|
1155 {
|
|
1156 c = *env++;
|
|
1157 switch (c)
|
|
1158 {
|
|
1159 case '"':
|
|
1160 p -= (slash >> 1);
|
|
1161 if (slash & 1)
|
|
1162 { p--;
|
|
1163 goto Laddc;
|
|
1164 }
|
|
1165 instring ^= 1;
|
|
1166 slash = 0;
|
|
1167 continue;
|
|
1168
|
|
1169 case ' ':
|
|
1170 case '\t':
|
|
1171 if (instring)
|
|
1172 goto Laddc;
|
|
1173 *p = 0;
|
|
1174 //if (wildcard)
|
|
1175 //wildcardexpand(); // not implemented
|
|
1176 break;
|
|
1177
|
|
1178 case '\\':
|
|
1179 slash++;
|
|
1180 *p++ = c;
|
|
1181 continue;
|
|
1182
|
|
1183 case 0:
|
|
1184 *p = 0;
|
|
1185 //if (wildcard)
|
|
1186 //wildcardexpand(); // not implemented
|
|
1187 goto Ldone;
|
|
1188
|
|
1189 default:
|
|
1190 Laddc:
|
|
1191 slash = 0;
|
|
1192 *p++ = c;
|
|
1193 continue;
|
|
1194 }
|
|
1195 break;
|
|
1196 }
|
|
1197 }
|
|
1198 }
|
|
1199
|
|
1200 Ldone:
|
|
1201 *pargc = argc;
|
|
1202 *pargv = (char **)argv->data;
|
|
1203 }
|
|
1204
|
|
1205 #if _WIN32
|
|
1206
|
|
1207 long __cdecl __ehfilter(LPEXCEPTION_POINTERS ep)
|
|
1208 {
|
|
1209 //printf("%x\n", ep->ExceptionRecord->ExceptionCode);
|
|
1210 if (ep->ExceptionRecord->ExceptionCode == STATUS_STACK_OVERFLOW)
|
|
1211 {
|
|
1212 #ifndef DEBUG
|
|
1213 return EXCEPTION_EXECUTE_HANDLER;
|
|
1214 #endif
|
|
1215 }
|
|
1216 return EXCEPTION_CONTINUE_SEARCH;
|
|
1217 }
|
|
1218
|
|
1219 #endif
|