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