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