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