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