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