Mercurial > projects > ldc
annotate dmd/mars.c @ 16:860524276608 trunk
[svn r20] * Added a simple compiletime std.stdio by downs
* Added a simple lphobos of std.stdio (so far)
* 'tester.sh run' now links to lphobos
author | lindquist |
---|---|
date | Wed, 03 Oct 2007 05:41:15 +0200 |
parents | d3ee9efe20e2 |
children | 788401029ecf |
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"; | |
71 version = "v1.020"; | |
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 | |
285 // Predefine version identifiers | |
286 #if IN_LLVM | |
287 VersionCondition::addPredefinedGlobalIdent("LLVM"); | |
288 #endif | |
289 #if _WIN32 | |
290 VersionCondition::addPredefinedGlobalIdent("Windows"); | |
291 VersionCondition::addPredefinedGlobalIdent("Win32"); | |
292 global.params.isWindows = 1; | |
293 #elif linux | |
294 VersionCondition::addPredefinedGlobalIdent("linux"); | |
295 global.params.isLinux = 1; | |
296 tt_os = "-unknown-linux-gnu"; | |
297 #else | |
298 #error | |
299 #endif /* linux */ | |
300 | |
301 //VersionCondition::addPredefinedGlobalIdent("D_Bits"); | |
302 //VersionCondition::addPredefinedGlobalIdent("D_InlineAsm"); | |
303 //VersionCondition::addPredefinedGlobalIdent("D_InlineAsm_X86"); | |
304 VersionCondition::addPredefinedGlobalIdent("all"); | |
305 | |
306 #if _WIN32 | |
307 inifile(argv[0], "llvmdc.ini"); | |
308 #endif | |
309 #if linux | |
310 inifile(argv[0], "llvmdc.conf"); | |
311 #else | |
312 #error | |
313 #endif | |
314 getenv_setargv("DFLAGS", &argc, &argv); | |
315 | |
316 #if 0 | |
317 for (i = 0; i < argc; i++) | |
318 { | |
319 printf("argv[%d] = '%s'\n", i, argv[i]); | |
320 } | |
321 #endif | |
322 | |
323 for (i = 1; i < argc; i++) | |
324 { | |
325 p = argv[i]; | |
326 if (*p == '-') | |
327 { | |
328 if (strcmp(p + 1, "d") == 0) | |
329 global.params.useDeprecated = 1; | |
330 else if (strcmp(p + 1, "c") == 0) | |
331 global.params.link = 0; | |
332 else if (strcmp(p + 1, "cov") == 0) | |
333 global.params.cov = 1; | |
334 else if (strcmp(p + 1, "fPIC") == 0) | |
335 global.params.pic = 1; | |
336 else if (strcmp(p + 1, "g") == 0) | |
337 global.params.symdebug = 1; | |
338 else if (strcmp(p + 1, "gc") == 0) | |
339 global.params.symdebug = 2; | |
340 else if (strcmp(p + 1, "gt") == 0) | |
341 { error("use -profile instead of -gt\n"); | |
342 global.params.trace = 1; | |
343 } | |
344 else if (strcmp(p + 1, "profile") == 0) | |
345 global.params.trace = 1; | |
346 else if (strcmp(p + 1, "v") == 0) | |
347 global.params.verbose = 1; | |
348 else if (strcmp(p + 1, "v1") == 0) | |
349 global.params.Dversion = 1; | |
350 else if (strcmp(p + 1, "w") == 0) | |
351 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
|
352 else if (p[1] == 'O') |
1 | 353 { |
354 global.params.optimize = 1; | |
355 if (p[2] != 0) { | |
356 int optlevel = atoi(p+2); | |
357 if (optlevel < 0 || optlevel > 5) { | |
358 error("Optimization level must be between 0 and 5. Using default (%d)", | |
359 global.params.optimizeLevel); | |
360 } | |
361 else { | |
362 global.params.optimizeLevel = optlevel; | |
363 } | |
364 } | |
365 } | |
366 else if (strcmp(p + 1, "forcebe") == 0) | |
367 global.params.forceBE = 1; | |
368 else if (strcmp(p + 1, "noruntime") == 0) | |
369 global.params.noruntime = 1; | |
370 else if (p[1] == 'o') | |
371 { | |
372 switch (p[2]) | |
373 { | |
374 case '-': | |
375 global.params.obj = 0; | |
376 break; | |
377 | |
378 case 'd': | |
379 if (!p[3]) | |
380 goto Lnoarg; | |
381 global.params.objdir = p + 3; | |
382 break; | |
383 | |
384 case 'f': | |
385 if (!p[3]) | |
386 goto Lnoarg; | |
387 global.params.objname = p + 3; | |
388 break; | |
389 | |
390 case 'p': | |
391 if (p[3]) | |
392 goto Lerror; | |
393 global.params.preservePaths = 1; | |
394 break; | |
395 | |
396 case 0: | |
397 error("-o no longer supported, use -of or -od"); | |
398 break; | |
399 | |
400 default: | |
401 goto Lerror; | |
402 } | |
403 } | |
404 else if (p[1] == 'D') | |
405 { global.params.doDocComments = 1; | |
406 switch (p[2]) | |
407 { | |
408 case 'd': | |
409 if (!p[3]) | |
410 goto Lnoarg; | |
411 global.params.docdir = p + 3; | |
412 break; | |
413 case 'f': | |
414 if (!p[3]) | |
415 goto Lnoarg; | |
416 global.params.docname = p + 3; | |
417 break; | |
418 | |
419 case 0: | |
420 break; | |
421 | |
422 default: | |
423 goto Lerror; | |
424 } | |
425 } | |
426 #ifdef _DH | |
427 else if (p[1] == 'H') | |
428 { global.params.doHdrGeneration = 1; | |
429 switch (p[2]) | |
430 { | |
431 case 'd': | |
432 if (!p[3]) | |
433 goto Lnoarg; | |
434 global.params.hdrdir = p + 3; | |
435 break; | |
436 | |
437 case 'f': | |
438 if (!p[3]) | |
439 goto Lnoarg; | |
440 global.params.hdrname = p + 3; | |
441 break; | |
442 | |
443 case 0: | |
444 break; | |
445 | |
446 default: | |
447 goto Lerror; | |
448 } | |
449 } | |
450 #endif | |
451 else if (strcmp(p + 1, "inline") == 0) | |
452 global.params.useInline = 1; | |
453 else if (strcmp(p + 1, "nofloat") == 0) | |
454 global.params.nofloat = 1; | |
455 else if (strcmp(p + 1, "quiet") == 0) | |
456 global.params.quiet = 1; | |
457 else if (strcmp(p + 1, "release") == 0) | |
458 global.params.release = 1; | |
459 else if (strcmp(p + 1, "unittest") == 0) | |
460 global.params.useUnitTests = 1; | |
461 else if (p[1] == 'I') | |
462 { | |
463 if (!global.params.imppath) | |
464 global.params.imppath = new Array(); | |
465 global.params.imppath->push(p + 2); | |
466 } | |
467 else if (p[1] == 'J') | |
468 { | |
469 if (!global.params.fileImppath) | |
470 global.params.fileImppath = new Array(); | |
471 global.params.fileImppath->push(p + 2); | |
472 } | |
473 else if (p[1] == 'E') | |
474 { | |
475 global.params.runtimeImppath = p+2; | |
476 } | |
477 else if (memcmp(p + 1, "debug", 5) == 0) | |
478 { | |
479 // Parse: | |
480 // -debug | |
481 // -debug=number | |
482 // -debug=identifier | |
483 if (p[6] == '=') | |
484 { | |
485 if (isdigit(p[7])) | |
486 { long level; | |
487 | |
488 errno = 0; | |
489 level = strtol(p + 7, &p, 10); | |
490 if (*p || errno || level > INT_MAX) | |
491 goto Lerror; | |
492 DebugCondition::setGlobalLevel((int)level); | |
493 } | |
494 else if (Lexer::isValidIdentifier(p + 7)) | |
495 DebugCondition::addGlobalIdent(p + 7); | |
496 else | |
497 goto Lerror; | |
498 } | |
499 else if (p[6]) | |
500 goto Lerror; | |
501 else | |
502 global.params.debuglevel = 1; | |
503 } | |
504 else if (memcmp(p + 1, "version", 5) == 0) | |
505 { | |
506 // Parse: | |
507 // -version=number | |
508 // -version=identifier | |
509 if (p[8] == '=') | |
510 { | |
511 if (isdigit(p[9])) | |
512 { long level; | |
513 | |
514 errno = 0; | |
515 level = strtol(p + 9, &p, 10); | |
516 if (*p || errno || level > INT_MAX) | |
517 goto Lerror; | |
518 VersionCondition::setGlobalLevel((int)level); | |
519 } | |
520 else if (Lexer::isValidIdentifier(p + 9)) | |
521 VersionCondition::addGlobalIdent(p + 9); | |
522 else | |
523 goto Lerror; | |
524 } | |
525 else | |
526 goto Lerror; | |
527 } | |
528 else if (strcmp(p + 1, "-b") == 0) | |
529 global.params.debugb = 1; | |
530 else if (strcmp(p + 1, "-c") == 0) | |
531 global.params.debugc = 1; | |
532 else if (strcmp(p + 1, "-f") == 0) | |
533 global.params.debugf = 1; | |
534 else if (strcmp(p + 1, "-help") == 0) | |
535 { usage(); | |
536 exit(EXIT_SUCCESS); | |
537 } | |
538 else if (strcmp(p + 1, "-r") == 0) | |
539 global.params.debugr = 1; | |
540 else if (strcmp(p + 1, "-x") == 0) | |
541 global.params.debugx = 1; | |
542 else if (strcmp(p + 1, "-y") == 0) | |
543 global.params.debugy = 1; | |
544 else if (p[1] == 'L') | |
545 { | |
546 global.params.linkswitches->push(p + 2); | |
547 } | |
548 else if (strcmp(p + 1, "run") == 0) | |
549 { global.params.run = 1; | |
550 global.params.runargs_length = ((i >= argcstart) ? argc : argcstart) - i - 1; | |
551 if (global.params.runargs_length) | |
552 { | |
553 files.push(argv[i + 1]); | |
554 global.params.runargs = &argv[i + 2]; | |
555 i += global.params.runargs_length; | |
556 global.params.runargs_length--; | |
557 } | |
558 else | |
559 { global.params.run = 0; | |
560 goto Lnoarg; | |
561 } | |
562 } | |
563 else if (p[1] == 'm') | |
564 { | |
565 global.params.llvmArch = p+2; | |
566 } | |
567 else | |
568 { | |
569 Lerror: | |
570 error("unrecognized switch '%s'", argv[i]); | |
571 continue; | |
572 | |
573 Lnoarg: | |
574 error("argument expected for switch '%s'", argv[i]); | |
575 continue; | |
576 } | |
577 } | |
578 else | |
579 files.push(p); | |
580 } | |
581 if (global.errors) | |
582 { | |
583 fatal(); | |
584 } | |
585 if (files.dim == 0) | |
586 { usage(); | |
587 return EXIT_FAILURE; | |
588 } | |
589 | |
590 if (global.params.release) | |
591 { global.params.useInvariants = 0; | |
592 global.params.useIn = 0; | |
593 global.params.useOut = 0; | |
594 global.params.useAssert = 0; | |
595 global.params.useArrayBounds = 0; | |
596 global.params.useSwitchError = 0; | |
597 } | |
598 | |
599 if (global.params.run) | |
600 global.params.quiet = 1; | |
601 | |
602 if (global.params.useUnitTests) | |
603 global.params.useAssert = 1; | |
604 | |
605 if (!global.params.obj) | |
606 global.params.link = 0; | |
607 | |
608 if (global.params.link) | |
609 { | |
610 global.params.exefile = global.params.objname; | |
611 global.params.objname = NULL; | |
612 } | |
613 else if (global.params.run) | |
614 { | |
615 error("flags conflict with -run"); | |
616 fatal(); | |
617 } | |
618 else | |
619 { | |
620 if (global.params.objname && files.dim > 1) | |
621 { | |
622 error("multiple source files, but only one .obj name"); | |
623 fatal(); | |
624 } | |
625 } | |
626 if (global.params.cov) | |
627 VersionCondition::addPredefinedGlobalIdent("D_Coverage"); | |
628 | |
629 bool allowForceEndianness = false; | |
630 | |
631 if (global.params.llvmArch == 0) { | |
632 std::string err_str; | |
633 const llvm::TargetMachineRegistry::Entry* e = llvm::TargetMachineRegistry::getClosestTargetForJIT(err_str); | |
634 if (e == 0) { | |
635 error("Failed to find a default target machine: %s", err_str.c_str()); | |
636 fatal(); | |
637 } | |
638 else { | |
639 global.params.llvmArch = const_cast<char*>(e->Name); | |
640 printf("Default target found: %s\n", global.params.llvmArch); | |
641 } | |
642 } | |
643 | |
644 if (strcmp(global.params.llvmArch,"x86")==0) { | |
645 VersionCondition::addPredefinedGlobalIdent("X86"); | |
646 global.params.isLE = true; | |
647 global.params.is64bit = false; | |
648 tt_arch = "i686"; | |
649 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"; | |
650 } | |
651 else if (strcmp(global.params.llvmArch,"x86-64")==0) { | |
652 VersionCondition::addPredefinedGlobalIdent("X86_64"); | |
653 global.params.isLE = true; | |
654 global.params.is64bit = true; | |
655 tt_arch = "x86_64"; | |
656 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"; | |
657 } | |
658 else if (strcmp(global.params.llvmArch,"ppc32")==0) { | |
659 VersionCondition::addPredefinedGlobalIdent("PPC"); | |
660 global.params.isLE = false; | |
661 global.params.is64bit = false; | |
662 tt_arch = "powerpc"; | |
663 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"; | |
664 } | |
665 else if (strcmp(global.params.llvmArch,"ppc64")==0) { | |
666 VersionCondition::addPredefinedGlobalIdent("PPC64"); | |
667 global.params.isLE = false; | |
668 global.params.is64bit = true; | |
669 tt_arch = "powerpc64"; | |
670 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"; | |
671 } | |
672 else { | |
673 assert(0 && "Invalid arch"); | |
674 } | |
675 | |
676 if (allowForceEndianness && global.params.forceBE) { | |
677 VersionCondition::addPredefinedGlobalIdent("BigEndian"); | |
678 global.params.isLE = false; | |
679 } | |
680 else if (global.params.isLE) { | |
681 VersionCondition::addPredefinedGlobalIdent("LittleEndian"); | |
682 } | |
683 else { | |
684 VersionCondition::addPredefinedGlobalIdent("BigEndian"); | |
685 } | |
686 | |
687 if (global.params.is64bit) { | |
688 VersionCondition::addPredefinedGlobalIdent("LLVM64"); | |
689 } | |
690 | |
691 assert(tt_arch != 0); | |
692 assert(tt_os != 0); | |
693 assert(data_layout != 0); | |
694 global.params.tt_arch = tt_arch; | |
695 global.params.tt_os = tt_os; | |
696 global.params.data_layout = data_layout; | |
697 | |
698 // Initialization | |
699 Type::init(); | |
700 Id::initialize(); | |
701 Module::init(); | |
702 initPrecedence(); | |
703 | |
704 backend_init(); | |
705 | |
706 //printf("%d source files\n",files.dim); | |
707 | |
708 // Build import search path | |
709 if (global.params.imppath) | |
710 { | |
711 for (i = 0; i < global.params.imppath->dim; i++) | |
712 { | |
713 char *path = (char *)global.params.imppath->data[i]; | |
714 Array *a = FileName::splitPath(path); | |
715 | |
716 if (a) | |
717 { | |
718 if (!global.path) | |
719 global.path = new Array(); | |
720 global.path->append(a); | |
721 } | |
722 } | |
723 } | |
724 | |
725 // Build string import search path | |
726 if (global.params.fileImppath) | |
727 { | |
728 for (i = 0; i < global.params.fileImppath->dim; i++) | |
729 { | |
730 char *path = (char *)global.params.fileImppath->data[i]; | |
731 Array *a = FileName::splitPath(path); | |
732 | |
733 if (a) | |
734 { | |
735 if (!global.filePath) | |
736 global.filePath = new Array(); | |
737 global.filePath->append(a); | |
738 } | |
739 } | |
740 } | |
741 | |
742 // Create Modules | |
743 Array modules; | |
744 modules.reserve(files.dim); | |
745 for (i = 0; i < files.dim; i++) | |
746 { Identifier *id; | |
747 char *ext; | |
748 char *name; | |
749 | |
750 p = (char *) files.data[i]; | |
751 | |
752 #if _WIN32 | |
753 // Convert / to \ so linker will work | |
754 for (int i = 0; p[i]; i++) | |
755 { | |
756 if (p[i] == '/') | |
757 p[i] = '\\'; | |
758 } | |
759 #endif | |
760 | |
761 p = FileName::name(p); // strip path | |
762 ext = FileName::ext(p); | |
763 if (ext) | |
764 { | |
765 #if IN_LLVM | |
766 if (strcmp(ext, global.nativeobj_ext) == 0 || | |
767 strcmp(ext, global.obj_ext) == 0) | |
768 #elif TARGET_LINUX | |
769 if (strcmp(ext, global.obj_ext) == 0) | |
770 #else | |
771 if (stricmp(ext, global.obj_ext) == 0) | |
772 #endif | |
773 { | |
774 global.params.objfiles->push(files.data[i]); | |
775 continue; | |
776 } | |
777 | |
778 #if !IN_LLVM | |
779 #if TARGET_LINUX | |
780 if (strcmp(ext, "a") == 0) | |
781 #else | |
782 if (stricmp(ext, "lib") == 0) | |
783 #endif | |
784 { | |
785 global.params.libfiles->push(files.data[i]); | |
786 continue; | |
787 } | |
788 #endif | |
789 | |
790 if (strcmp(ext, global.ddoc_ext) == 0) | |
791 { | |
792 global.params.ddocfiles->push(files.data[i]); | |
793 continue; | |
794 } | |
795 | |
796 #if !TARGET_LINUX | |
797 if (stricmp(ext, "res") == 0) | |
798 { | |
799 global.params.resfile = (char *)files.data[i]; | |
800 continue; | |
801 } | |
802 | |
803 if (stricmp(ext, "def") == 0) | |
804 { | |
805 global.params.deffile = (char *)files.data[i]; | |
806 continue; | |
807 } | |
808 | |
809 if (stricmp(ext, "exe") == 0) | |
810 { | |
811 global.params.exefile = (char *)files.data[i]; | |
812 continue; | |
813 } | |
814 #endif | |
815 | |
816 if (stricmp(ext, global.mars_ext) == 0 || | |
817 stricmp(ext, "htm") == 0 || | |
818 stricmp(ext, "html") == 0 || | |
819 stricmp(ext, "xhtml") == 0) | |
820 { | |
821 ext--; // skip onto '.' | |
822 assert(*ext == '.'); | |
823 name = (char *)mem.malloc((ext - p) + 1); | |
824 memcpy(name, p, ext - p); | |
825 name[ext - p] = 0; // strip extension | |
826 | |
827 if (name[0] == 0 || | |
828 strcmp(name, "..") == 0 || | |
829 strcmp(name, ".") == 0) | |
830 { | |
831 Linvalid: | |
832 error("invalid file name '%s'", (char *)files.data[i]); | |
833 fatal(); | |
834 } | |
835 } | |
836 else | |
837 { error("unrecognized file extension %s\n", ext); | |
838 fatal(); | |
839 } | |
840 } | |
841 else | |
842 { name = p; | |
843 if (!*name) | |
844 goto Linvalid; | |
845 } | |
846 | |
847 id = new Identifier(name, 0); | |
848 m = new Module((char *) files.data[i], id, global.params.doDocComments, global.params.doHdrGeneration); | |
849 modules.push(m); | |
850 | |
851 global.params.objfiles->push(m->objfile->name->str); | |
852 } | |
853 | |
854 #if _WIN32 | |
855 __try | |
856 { | |
857 #endif | |
858 // Read files, parse them | |
859 for (i = 0; i < modules.dim; i++) | |
860 { | |
861 m = (Module *)modules.data[i]; | |
862 if (global.params.verbose) | |
863 printf("parse %s\n", m->toChars()); | |
864 if (!Module::rootModule) | |
865 Module::rootModule = m; | |
866 m->importedFrom = m; | |
867 m->deleteObjFile(); | |
868 m->read(0); | |
869 m->parse(); | |
870 if (m->isDocFile) | |
871 { | |
872 m->gendocfile(); | |
873 | |
874 // Remove m from list of modules | |
875 modules.remove(i); | |
876 i--; | |
877 | |
878 // Remove m's object file from list of object files | |
879 for (int j = 0; j < global.params.objfiles->dim; j++) | |
880 { | |
881 if (m->objfile->name->str == global.params.objfiles->data[j]) | |
882 { | |
883 global.params.objfiles->remove(j); | |
884 break; | |
885 } | |
886 } | |
887 | |
888 if (global.params.objfiles->dim == 0) | |
889 global.params.link = 0; | |
890 } | |
891 } | |
892 if (global.errors) | |
893 fatal(); | |
894 #ifdef _DH | |
895 if (global.params.doHdrGeneration) | |
896 { | |
897 /* Generate 'header' import files. | |
898 * Since 'header' import files must be independent of command | |
899 * line switches and what else is imported, they are generated | |
900 * before any semantic analysis. | |
901 */ | |
902 for (i = 0; i < modules.dim; i++) | |
903 { | |
904 m = (Module *)modules.data[i]; | |
905 if (global.params.verbose) | |
906 printf("import %s\n", m->toChars()); | |
907 m->genhdrfile(); | |
908 } | |
909 } | |
910 if (global.errors) | |
911 fatal(); | |
912 #endif | |
913 | |
914 // Do semantic analysis | |
915 for (i = 0; i < modules.dim; i++) | |
916 { | |
917 m = (Module *)modules.data[i]; | |
918 if (global.params.verbose) | |
919 printf("semantic %s\n", m->toChars()); | |
920 m->semantic(); | |
921 } | |
922 if (global.errors) | |
923 fatal(); | |
924 | |
925 // Do pass 2 semantic analysis | |
926 for (i = 0; i < modules.dim; i++) | |
927 { | |
928 m = (Module *)modules.data[i]; | |
929 if (global.params.verbose) | |
930 printf("semantic2 %s\n", m->toChars()); | |
931 m->semantic2(); | |
932 } | |
933 if (global.errors) | |
934 fatal(); | |
935 | |
936 // Do pass 3 semantic analysis | |
937 for (i = 0; i < modules.dim; i++) | |
938 { | |
939 m = (Module *)modules.data[i]; | |
940 if (global.params.verbose) | |
941 printf("semantic3 %s\n", m->toChars()); | |
942 m->semantic3(); | |
943 } | |
944 if (global.errors) | |
945 fatal(); | |
946 | |
947 // Scan for functions to inline | |
948 if (global.params.useInline) | |
949 { | |
950 /* The problem with useArrayBounds and useAssert is that the | |
951 * module being linked to may not have generated them, so if | |
952 * we inline functions from those modules, the symbols for them will | |
953 * not be found at link time. | |
954 */ | |
955 if (!global.params.useArrayBounds && !global.params.useAssert) | |
956 { | |
957 // Do pass 3 semantic analysis on all imported modules, | |
958 // since otherwise functions in them cannot be inlined | |
959 for (i = 0; i < Module::amodules.dim; i++) | |
960 { | |
961 m = (Module *)Module::amodules.data[i]; | |
962 if (global.params.verbose) | |
963 printf("semantic3 %s\n", m->toChars()); | |
964 m->semantic3(); | |
965 } | |
966 if (global.errors) | |
967 fatal(); | |
968 } | |
969 | |
970 for (i = 0; i < modules.dim; i++) | |
971 { | |
972 m = (Module *)modules.data[i]; | |
973 if (global.params.verbose) | |
974 printf("inline scan %s\n", m->toChars()); | |
975 m->inlineScan(); | |
976 } | |
977 } | |
978 if (global.errors) | |
979 fatal(); | |
980 | |
981 // Generate output files | |
982 for (i = 0; i < modules.dim; i++) | |
983 { | |
984 m = (Module *)modules.data[i]; | |
985 if (global.params.verbose) | |
986 printf("code %s\n", m->toChars()); | |
987 if (global.params.obj) | |
988 m->genobjfile(); | |
989 if (global.errors) | |
990 m->deleteObjFile(); | |
991 else | |
992 { | |
993 if (global.params.doDocComments) | |
994 m->gendocfile(); | |
995 } | |
996 } | |
997 #if _WIN32 | |
998 } | |
999 __except (__ehfilter(GetExceptionInformation())) | |
1000 { | |
1001 printf("Stack overflow\n"); | |
1002 fatal(); | |
1003 } | |
1004 #endif | |
1005 backend_term(); | |
1006 if (global.errors) | |
1007 fatal(); | |
1008 | |
1009 if (!global.params.objfiles->dim) | |
1010 { | |
1011 if (global.params.link) | |
1012 error("no object files to link"); | |
1013 } | |
1014 else | |
1015 { | |
1016 if (global.params.link) | |
1017 status = runLINK(); | |
1018 | |
1019 if (global.params.run) | |
1020 { | |
1021 if (!status) | |
1022 { | |
1023 status = runProgram(); | |
1024 | |
1025 /* Delete .obj files and .exe file | |
1026 */ | |
1027 for (i = 0; i < modules.dim; i++) | |
1028 { | |
1029 m = (Module *)modules.data[i]; | |
1030 m->deleteObjFile(); | |
1031 } | |
1032 deleteExeFile(); | |
1033 } | |
1034 } | |
1035 } | |
1036 | |
1037 return status; | |
1038 } | |
1039 | |
1040 | |
1041 | |
1042 /*********************************** | |
1043 * Parse and append contents of environment variable envvar | |
1044 * to argc and argv[]. | |
1045 * The string is separated into arguments, processing \ and ". | |
1046 */ | |
1047 | |
1048 void getenv_setargv(const char *envvar, int *pargc, char** *pargv) | |
1049 { | |
1050 char *env; | |
1051 char *p; | |
1052 Array *argv; | |
1053 int argc; | |
1054 | |
1055 int wildcard; // do wildcard expansion | |
1056 int instring; | |
1057 int slash; | |
1058 char c; | |
1059 int j; | |
1060 | |
1061 env = getenv(envvar); | |
1062 if (!env) | |
1063 return; | |
1064 | |
1065 env = mem.strdup(env); // create our own writable copy | |
1066 | |
1067 argc = *pargc; | |
1068 argv = new Array(); | |
1069 argv->setDim(argc); | |
1070 | |
1071 for (int i = 0; i < argc; i++) | |
1072 argv->data[i] = (void *)(*pargv)[i]; | |
1073 | |
1074 j = 1; // leave argv[0] alone | |
1075 while (1) | |
1076 { | |
1077 wildcard = 1; | |
1078 switch (*env) | |
1079 { | |
1080 case ' ': | |
1081 case '\t': | |
1082 env++; | |
1083 break; | |
1084 | |
1085 case 0: | |
1086 goto Ldone; | |
1087 | |
1088 case '"': | |
1089 wildcard = 0; | |
1090 default: | |
1091 argv->push(env); // append | |
1092 //argv->insert(j, env); // insert at position j | |
1093 j++; | |
1094 argc++; | |
1095 p = env; | |
1096 slash = 0; | |
1097 instring = 0; | |
1098 c = 0; | |
1099 | |
1100 while (1) | |
1101 { | |
1102 c = *env++; | |
1103 switch (c) | |
1104 { | |
1105 case '"': | |
1106 p -= (slash >> 1); | |
1107 if (slash & 1) | |
1108 { p--; | |
1109 goto Laddc; | |
1110 } | |
1111 instring ^= 1; | |
1112 slash = 0; | |
1113 continue; | |
1114 | |
1115 case ' ': | |
1116 case '\t': | |
1117 if (instring) | |
1118 goto Laddc; | |
1119 *p = 0; | |
1120 //if (wildcard) | |
1121 //wildcardexpand(); // not implemented | |
1122 break; | |
1123 | |
1124 case '\\': | |
1125 slash++; | |
1126 *p++ = c; | |
1127 continue; | |
1128 | |
1129 case 0: | |
1130 *p = 0; | |
1131 //if (wildcard) | |
1132 //wildcardexpand(); // not implemented | |
1133 goto Ldone; | |
1134 | |
1135 default: | |
1136 Laddc: | |
1137 slash = 0; | |
1138 *p++ = c; | |
1139 continue; | |
1140 } | |
1141 break; | |
1142 } | |
1143 } | |
1144 } | |
1145 | |
1146 Ldone: | |
1147 *pargc = argc; | |
1148 *pargv = (char **)argv->data; | |
1149 } | |
1150 | |
1151 #if _WIN32 | |
1152 | |
1153 long __cdecl __ehfilter(LPEXCEPTION_POINTERS ep) | |
1154 { | |
1155 //printf("%x\n", ep->ExceptionRecord->ExceptionCode); | |
1156 if (ep->ExceptionRecord->ExceptionCode == STATUS_STACK_OVERFLOW) | |
1157 { | |
1158 #ifndef DEBUG | |
1159 return EXCEPTION_EXECUTE_HANDLER; | |
1160 #endif | |
1161 } | |
1162 return EXCEPTION_CONTINUE_SEARCH; | |
1163 } | |
1164 | |
1165 #endif |