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