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