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