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