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