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