Mercurial > projects > ddmd
annotate main.d @ 164:8bf07a566fdf
GC is working now
author | korDen |
---|---|
date | Thu, 30 Sep 2010 07:53:23 +0400 |
parents | 60bb0fe4563e |
children | 25ede4f66bda |
rev | line source |
---|---|
0 | 1 module main; |
2 | |
99 | 3 import dmd.Macro; |
0 | 4 import dmd.Array; |
5 import dmd.Module; | |
6 import dmd.Global; | |
7 import dmd.VersionCondition; | |
8 import dmd.DebugCondition; | |
9 import dmd.Loc; | |
10 import dmd.Lexer; | |
11 import dmd.OutBuffer; | |
12 import dmd.FileName; | |
13 import dmd.Type; | |
14 import dmd.File; | |
15 import dmd.Id; | |
16 import dmd.Identifier; | |
79 | 17 import dmd.Json; |
0 | 18 import dmd.Library; |
19 import dmd.TOK; | |
20 import dmd.String; | |
21 import dmd.backend.glue; | |
22 | |
23 import std.stdarg; | |
24 import std.string : toStringz; | |
34 | 25 import std.exception; |
0 | 26 |
27 import core.stdc.string; | |
28 import core.stdc.stdio; | |
29 import core.stdc.ctype; | |
30 import core.stdc.errno; | |
31 import core.stdc.stdlib; | |
32 import core.stdc.limits; | |
33 | |
34 import core.memory; | |
35 | |
70 | 36 import dbg.CallStackInfo; |
0 | 37 |
38 import dmd.Util; | |
39 | |
40 enum ExitCode | |
41 { | |
42 EXIT_SUCCESS = 0, | |
43 } | |
44 | |
164 | 45 version = CrashHandler; |
46 | |
47 version (CrashHandler) { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
48 version(Linux) |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
49 { |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
50 extern (C) extern __gshared bool rt_trapExceptions; |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
51 |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
52 static this() { |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
53 rt_trapExceptions = false; |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
54 } |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
55 |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
56 } |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
57 |
164 | 58 |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
59 version(Windows) |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
60 { |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
61 |
70 | 62 version (Windows) |
63 { | |
64 private import core.stdc.wchar_; | |
65 | |
66 extern (Windows) alias int function() FARPROC; | |
67 extern (Windows) FARPROC GetProcAddress(void*, in char*); | |
68 extern (Windows) void* LoadLibraryA(in char*); | |
69 extern (Windows) int FreeLibrary(void*); | |
70 extern (Windows) void* LocalFree(void*); | |
71 extern (Windows) wchar_t* GetCommandLineW(); | |
72 extern (Windows) wchar_t** CommandLineToArgvW(wchar_t*, int*); | |
73 extern (Windows) export int WideCharToMultiByte(uint, uint, wchar_t*, int, char*, int, char*, int); | |
74 pragma(lib, "shell32.lib"); // needed for CommandLineToArgvW | |
75 } | |
76 | |
77 shared bool _d_isHalting = false; | |
78 __gshared string[] _d_args = null; | |
79 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
80 version(Posix) |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
81 { |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
82 extern (C) void _STI_monitor_staticctor(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
83 extern (C) void _STD_monitor_staticdtor(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
84 extern (C) void _STI_critical_init(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
85 extern (C) void _STD_critical_term(); |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
86 } |
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
87 |
70 | 88 extern (C) void gc_init(); |
89 extern (C) void gc_term(); | |
90 extern (C) void _minit(); | |
91 extern (C) void _moduleCtor(); | |
92 extern (C) void _moduleDtor(); | |
93 extern (C) bool runModuleUnitTests(); | |
94 extern (C) void thread_joinAll(); | |
95 | |
96 import rt.memory; | |
97 | |
98 extern (C) int main(int argc, char** argv) | |
99 { | |
100 char[][] args; | |
101 int result; | |
102 | |
103 version (OSX) | |
104 { /* OSX does not provide a way to get at the top of the | |
105 * stack, except for the magic value 0xC0000000. | |
106 * But as far as the gc is concerned, argv is at the top | |
107 * of the main thread's stack, so save the address of that. | |
108 */ | |
109 __osx_stack_end = cast(void*)&argv; | |
110 } | |
111 | |
112 version (FreeBSD) version (D_InlineAsm_X86) | |
113 { | |
114 /* | |
115 * FreeBSD/i386 sets the FPU precision mode to 53 bit double. | |
116 * Make it 64 bit extended. | |
117 */ | |
118 ushort fpucw; | |
119 asm | |
120 { | |
121 fstsw fpucw; | |
122 or fpucw, 0b11_00_111111; // 11: use 64 bit extended-precision | |
123 // 111111: mask all FP exceptions | |
124 fldcw fpucw; | |
125 } | |
126 } | |
127 | |
128 version (Posix) | |
129 { | |
130 _STI_monitor_staticctor(); | |
131 _STI_critical_init(); | |
132 } | |
0 | 133 |
70 | 134 version (Windows) |
135 { | |
136 wchar_t* wcbuf = GetCommandLineW(); | |
137 size_t wclen = wcslen(wcbuf); | |
138 int wargc = 0; | |
139 wchar_t** wargs = CommandLineToArgvW(wcbuf, &wargc); | |
140 assert(wargc == argc); | |
141 | |
142 char* cargp = null; | |
143 size_t cargl = WideCharToMultiByte(65001, 0, wcbuf, wclen, null, 0, null, 0); | |
144 | |
145 cargp = cast(char*) alloca(cargl); | |
146 args = ((cast(char[]*) alloca(wargc * (char[]).sizeof)))[0 .. wargc]; | |
147 | |
148 for (size_t i = 0, p = 0; i < wargc; i++) | |
149 { | |
150 int wlen = wcslen(wargs[i]); | |
151 int clen = WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, null, 0, null, 0); | |
152 args[i] = cargp[p .. p+clen]; | |
153 p += clen; assert(p <= cargl); | |
154 WideCharToMultiByte(65001, 0, &wargs[i][0], wlen, &args[i][0], clen, null, 0); | |
155 } | |
156 LocalFree(wargs); | |
157 wargs = null; | |
158 wargc = 0; | |
159 } | |
160 else version (Posix) | |
161 { | |
162 char[]* am = cast(char[]*) malloc(argc * (char[]).sizeof); | |
163 scope(exit) free(am); | |
164 | |
165 for (size_t i = 0; i < argc; i++) | |
166 { | |
167 auto len = strlen(argv[i]); | |
168 am[i] = argv[i][0 .. len]; | |
169 } | |
170 args = am[0 .. argc]; | |
171 } | |
172 _d_args = cast(string[]) args; | |
173 | |
174 void runMain() | |
175 { | |
164 | 176 CrashHandlerInit(); |
70 | 177 result = main(_d_args); |
178 } | |
179 | |
180 void runAll() | |
181 { | |
182 gc_init(); | |
183 initStaticDataGC(); | |
184 version (Windows) | |
185 _minit(); | |
186 _moduleCtor(); | |
187 _moduleTlsCtor(); | |
188 if (runModuleUnitTests()) | |
189 runMain(); | |
190 else | |
191 result = EXIT_FAILURE; | |
192 _moduleTlsDtor(); | |
193 thread_joinAll(); | |
194 _d_isHalting = true; | |
195 _moduleDtor(); | |
196 gc_term(); | |
197 } | |
198 | |
199 runAll(); | |
200 | |
201 version (Posix) | |
202 { | |
203 _STD_critical_term(); | |
204 _STD_monitor_staticdtor(); | |
205 } | |
206 return result; | |
0 | 207 } |
25 | 208 |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
209 } |
164 | 210 } |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
70
diff
changeset
|
211 |
0 | 212 int main(string[] args) |
213 { | |
214 Array files = new Array(); | |
215 Array libmodules = new Array(); | |
216 Module m; | |
217 int status = ExitCode.EXIT_SUCCESS; | |
218 int argcstart = args.length; | |
219 int setdebuglib = 0; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
220 byte noboundscheck = 0; |
0 | 221 |
222 global = new Global(); | |
223 | |
224 /// if (response_expand(&argc,&argv)) // expand response files | |
225 /// error("can't open response file"); | |
226 | |
227 files.reserve(args.length - 1); | |
228 | |
229 // Set default values | |
230 global.params.argv0 = args[0]; | |
231 global.params.link = 1; | |
232 global.params.useAssert = 1; | |
233 global.params.useInvariants = 1; | |
234 global.params.useIn = 1; | |
235 global.params.useOut = 1; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
236 global.params.useArrayBounds = 2; // default to all functions |
0 | 237 global.params.useSwitchError = 1; |
238 global.params.useInline = 0; | |
239 global.params.obj = 1; | |
240 global.params.Dversion = 2; | |
241 global.params.quiet = 1; | |
242 | |
243 global.params.linkswitches = new Array(); | |
244 global.params.libfiles = new Array(); | |
245 global.params.objfiles = new Array(); | |
246 global.params.ddocfiles = new Array(); | |
247 | |
248 version (TARGET_WINDOS) { | |
249 global.params.defaultlibname = "phobos"; | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
250 } else version (POSIX) { //#elif TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS |
0 | 251 global.params.defaultlibname = "phobos2"; |
252 } else version (TARGET_NET) { | |
253 } else { | |
254 static assert(false, "fix this"); | |
255 } | |
256 | |
257 // Predefine version identifiers | |
258 VersionCondition.addPredefinedGlobalIdent("DigitalMars"); | |
259 | |
260 version (TARGET_WINDOS) { | |
261 VersionCondition.addPredefinedGlobalIdent("Windows"); | |
262 global.params.isWindows = 1; | |
263 version (TARGET_NET) { | |
264 // TARGET_NET macro is NOT mutually-exclusive with TARGET_WINDOS | |
265 VersionCondition.addPredefinedGlobalIdent("D_NET"); | |
266 } | |
267 } else version (TARGET_LINUX) { | |
268 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
269 VersionCondition.addPredefinedGlobalIdent("linux"); | |
270 global.params.isLinux = 1; | |
271 } else version (TARGET_OSX) { | |
272 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
273 VersionCondition.addPredefinedGlobalIdent("OSX"); | |
274 global.params.isOSX = 1; | |
275 | |
276 // For legacy compatibility | |
277 VersionCondition.addPredefinedGlobalIdent("darwin"); | |
278 } else version (TARGET_FREEBSD) { | |
279 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
280 VersionCondition.addPredefinedGlobalIdent("FreeBSD"); | |
281 global.params.isFreeBSD = 1; | |
282 } else version (TARGET_SOLARIS) { | |
283 VersionCondition.addPredefinedGlobalIdent("Posix"); | |
284 VersionCondition.addPredefinedGlobalIdent("Solaris"); | |
285 global.params.isSolaris = 1; | |
286 } else { | |
287 static assert (false, "fix this"); | |
288 } | |
289 | |
290 VersionCondition.addPredefinedGlobalIdent("LittleEndian"); | |
291 //VersionCondition.addPredefinedGlobalIdent("D_Bits"); | |
292 version (DMDV2) { | |
293 VersionCondition.addPredefinedGlobalIdent("D_Version2"); | |
294 } | |
295 VersionCondition.addPredefinedGlobalIdent("all"); | |
296 | |
114 | 297 version (Windows) |
298 { | |
299 inifile(args[0], "sc.ini"); | |
300 } | |
301 else version (Posix) ///linux || __APPLE__ || __FreeBSD__ || __sun&&__SVR4 | |
302 { | |
303 inifile(args[0], "dmd.conf"); | |
304 } | |
305 else | |
306 { | |
307 static assert (false, "fix this"); | |
0 | 308 } |
309 args = getenv_setargv("DFLAGS", args); | |
310 | |
311 version (disabled) { | |
312 for (i = 0; i < argc; i++) | |
313 { | |
314 writef("argv[%d] = '%s'\n", i, argv[i]); | |
315 } | |
316 } | |
317 | |
318 foreach(i; 1..args.length) | |
319 { | |
320 auto arg = args[i]; | |
321 auto p = arg.ptr; | |
322 if (*p == '-') | |
323 { | |
324 arg = arg[1..$]; | |
325 if (arg == "d") | |
326 global.params.useDeprecated = 1; | |
327 else if (arg == "c") | |
328 global.params.link = 0; | |
329 else if (arg == "cov") | |
330 global.params.cov = 1; | |
331 ///version (XXX) {// TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_SOLARIS | |
332 /// else if (arg == "fPIC") | |
333 /// global.params.pic = 1; | |
334 ///} | |
335 else if (arg == "multiobj") | |
336 global.params.multiobj = 1; | |
337 else if (arg == "g") | |
338 global.params.symdebug = 1; | |
339 else if (arg == "gc") | |
340 global.params.symdebug = 2; | |
341 else if (arg == "gt") | |
342 { error("use -profile instead of -gt\n"); | |
343 global.params.trace = 1; | |
344 } | |
345 else if (arg == "m64") | |
346 global.params.isX86_64 = 1; | |
347 else if (arg == "profile") | |
348 global.params.trace = 1; | |
349 else if (arg == "v") | |
350 global.params.verbose = 1; | |
351 ///version (DMDV2) { | |
352 else if (arg == "vtls") | |
353 global.params.vtls = 1; | |
354 ///} | |
355 else if (arg == "v1") | |
356 { | |
357 version (DMDV1) { | |
358 global.params.Dversion = 1; | |
359 } else { | |
360 error("use DMD 1.0 series compilers for -v1 switch"); | |
361 break; | |
362 } | |
363 } | |
364 else if (arg == "w") | |
365 global.params.warnings = 1; | |
366 else if (arg == "O") | |
367 global.params.optimize = 1; | |
368 else if (p[1] == 'o') | |
369 { | |
370 switch (p[2]) | |
371 { | |
372 case '-': | |
373 global.params.obj = 0; | |
374 break; | |
375 | |
376 case 'd': | |
377 if (!p[3]) | |
378 goto Lnoarg; | |
379 global.params.objdir = arg[(p - arg.ptr) + 3..$]; | |
380 break; | |
381 | |
382 case 'f': | |
383 { | |
384 if (!p[3]) | |
385 goto Lnoarg; | |
386 | |
387 global.params.objname = arg[(p - arg.ptr) + 3..$]; | |
388 break; | |
389 } | |
390 | |
391 case 'p': | |
392 if (p[3]) | |
393 goto Lerror; | |
394 global.params.preservePaths = 1; | |
395 break; | |
396 | |
397 case 0: | |
398 error("-o no longer supported, use -of or -od"); | |
399 break; | |
400 | |
401 default: | |
402 goto Lerror; | |
403 } | |
404 } | |
405 else if (p[1] == 'D') | |
406 { global.params.doDocComments = 1; | |
407 switch (p[2]) | |
408 { | |
409 case 'd': | |
410 if (!p[3]) | |
411 goto Lnoarg; | |
412 global.params.docdir = arg[(p - arg.ptr) + 3..$]; | |
413 break; | |
414 case 'f': | |
415 if (!p[3]) | |
416 goto Lnoarg; | |
417 global.params.docname = arg[(p - arg.ptr) + 3..$]; | |
418 break; | |
419 | |
420 case 0: | |
421 break; | |
422 | |
423 default: | |
424 goto Lerror; | |
425 } | |
426 } | |
427 ///version (_DH) { | |
428 else if (p[1] == 'H') | |
429 { global.params.doHdrGeneration = 1; | |
430 switch (p[2]) | |
431 { | |
432 case 'd': | |
433 if (!p[3]) | |
434 goto Lnoarg; | |
435 global.params.hdrdir = arg[(p - arg.ptr) + 3..$]; | |
436 break; | |
437 | |
438 case 'f': | |
439 if (!p[3]) | |
440 goto Lnoarg; | |
441 global.params.hdrname = arg[(p - arg.ptr) + 3..$]; | |
442 break; | |
443 | |
444 case 0: | |
445 break; | |
446 | |
447 default: | |
448 goto Lerror; | |
449 } | |
450 } | |
451 ///} | |
79 | 452 else if (p[1] == 'X') |
453 { | |
454 global.params.doXGeneration = 1; | |
455 switch (p[2]) | |
456 { | |
457 case 'f': | |
458 if (!p[3]) | |
459 goto Lnoarg; | |
460 global.params.xfilename = arg[(p - arg.ptr) + 3..$]; | |
461 break; | |
462 | |
463 case 0: | |
464 break; | |
465 | |
466 default: | |
467 goto Lerror; | |
468 } | |
469 } | |
470 | |
0 | 471 else if (arg == "ignore") |
472 global.params.ignoreUnsupportedPragmas = 1; | |
473 else if (arg == "inline") | |
474 global.params.useInline = 1; | |
475 else if (arg == "lib") | |
476 global.params.lib = 1; | |
477 else if (arg == "nofloat") | |
478 global.params.nofloat = 1; | |
479 else if (arg == "quiet") | |
480 global.params.quiet = 1; | |
481 else if (arg == "release") | |
482 global.params.release = 1; | |
483 ///version (DMDV2) { | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
484 else if (arg == "noboundscheck") |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
485 noboundscheck = 1; |
0 | 486 ///} |
487 else if (arg == "unittest") | |
488 global.params.useUnitTests = 1; | |
489 else if (p[1] == 'I') | |
490 { | |
491 if (!global.params.imppath) | |
492 global.params.imppath = new Array(); | |
493 global.params.imppath.push(cast(void*)new String(arg[(p - arg.ptr) + 2..$])); /// | |
494 } | |
495 else if (p[1] == 'J') | |
496 { | |
497 if (!global.params.fileImppath) | |
498 global.params.fileImppath = new Array(); | |
499 global.params.fileImppath.push(cast(void*)new String(arg[(p - arg.ptr) + 2..$])); | |
500 } | |
501 else if (memcmp(p + 1, "debug".ptr, 5) == 0 && p[6] != 'l') | |
502 { | |
503 // Parse: | |
504 // -debug | |
505 // -debug=number | |
506 // -debug=identifier | |
507 if (p[6] == '=') | |
508 { | |
509 if (isdigit(p[7])) | |
510 { long level; | |
511 | |
512 errno = 0; | |
513 level = strtol(p + 7, cast(char**)&p, 10); | |
514 if (*p || errno || level > INT_MAX) | |
515 goto Lerror; | |
516 DebugCondition.setGlobalLevel(cast(int)level); | |
517 } | |
518 else if (Lexer.isValidIdentifier(arg[(p - arg.ptr) + 7..$])) /// | |
519 DebugCondition.addGlobalIdent(p + 7); | |
520 else | |
521 goto Lerror; | |
522 } | |
523 else if (p[6]) | |
524 goto Lerror; | |
525 else | |
526 global.params.debuglevel = 1; | |
527 } | |
528 else if (memcmp(p + 1, "version".ptr, 5) == 0) | |
529 { | |
530 // Parse: | |
531 // -version=number | |
532 // -version=identifier | |
533 if (p[8] == '=') | |
534 { | |
535 if (isdigit(p[9])) | |
536 { long level; | |
537 | |
538 errno = 0; | |
539 level = strtol(p + 9, cast(char**)&p, 10); /// | |
540 if (*p || errno || level > INT_MAX) | |
541 goto Lerror; | |
542 VersionCondition.setGlobalLevel(cast(int)level); | |
543 } | |
544 else if (Lexer.isValidIdentifier(arg[(p - arg.ptr) + 9..$])) /// | |
545 VersionCondition.addGlobalIdent(arg[(p - arg.ptr) + 9..$]); | |
546 else | |
547 goto Lerror; | |
548 } | |
549 else | |
550 goto Lerror; | |
551 } | |
552 else if (arg == "-b") | |
553 global.params.debugb = 1; | |
554 else if (arg == "-c") | |
555 global.params.debugc = 1; | |
556 else if (arg == "-f") | |
557 global.params.debugf = 1; | |
558 else if (arg == "-help") | |
559 { usage(); | |
560 exit(EXIT_SUCCESS); | |
561 } | |
562 else if (arg == "-r") | |
563 global.params.debugr = 1; | |
564 else if (arg == "-x") | |
565 global.params.debugx = 1; | |
566 else if (arg == "-y") | |
567 global.params.debugy = 1; | |
568 else if (p[1] == 'L') | |
569 { | |
570 global.params.linkswitches.push(cast(void*)p + 2); | |
571 } | |
572 else if (memcmp(p + 1, "defaultlib=".ptr, 11) == 0) | |
573 { | |
574 global.params.defaultlibname = p + 1 + 11; | |
575 } | |
576 else if (memcmp(p + 1, "debuglib=".ptr, 9) == 0) | |
577 { | |
578 setdebuglib = 1; | |
579 global.params.debuglibname = p + 1 + 9; | |
580 } | |
581 else if (memcmp(p + 1, "deps=".ptr, 5) == 0) | |
582 { | |
583 global.params.moduleDepsFile = arg[(p - arg.ptr) + 1 + 5..$]; | |
584 if (!global.params.moduleDepsFile[0]) | |
585 goto Lnoarg; | |
586 global.params.moduleDeps = new OutBuffer; | |
587 } | |
588 else if (memcmp(p + 1, "man".ptr, 3) == 0) | |
589 { | |
114 | 590 version (Windows) { |
0 | 591 version (DMDV1) { |
592 browse("http://www.digitalmars.com/d/1.0/dmd-windows.html"); | |
593 } else { | |
594 browse("http://www.digitalmars.com/d/2.0/dmd-windows.html"); | |
595 } | |
596 } | |
597 version (linux) { | |
598 version (DMDV1) { | |
599 browse("http://www.digitalmars.com/d/1.0/dmd-linux.html"); | |
600 } else { | |
601 browse("http://www.digitalmars.com/d/2.0/dmd-linux.html"); | |
602 } | |
603 } | |
604 version (__APPLE__) { | |
605 version (DMDV1) { | |
606 browse("http://www.digitalmars.com/d/1.0/dmd-osx.html"); | |
607 } else { | |
608 browse("http://www.digitalmars.com/d/2.0/dmd-osx.html"); | |
609 } | |
610 } | |
611 version (__FreeBSD__) { | |
612 version (DMDV1) { | |
613 browse("http://www.digitalmars.com/d/1.0/dmd-freebsd.html"); | |
614 } else { | |
615 browse("http://www.digitalmars.com/d/2.0/dmd-freebsd.html"); | |
616 } | |
617 } | |
618 exit(EXIT_SUCCESS); | |
619 } | |
620 else if (arg == "run") | |
621 { global.params.run = 1; | |
622 global.params.runargs_length = ((i >= argcstart) ? args.length : argcstart) - i - 1; | |
623 if (global.params.runargs_length) | |
624 { | |
625 files.push(cast(void*)args[i + 1].ptr); | |
626 global.params.runargs = args[i + 2..$]; | |
627 i += global.params.runargs_length; | |
628 global.params.runargs_length--; | |
629 } | |
630 else | |
631 { global.params.run = 0; | |
632 goto Lnoarg; | |
633 } | |
634 } | |
635 else | |
636 { | |
637 Lerror: | |
638 error("unrecognized switch '%s'", args[i]); | |
639 continue; | |
640 | |
641 Lnoarg: | |
642 error("argument expected for switch '%s'", args[i]); | |
643 continue; | |
644 } | |
645 } | |
646 else | |
647 { | |
648 version (TARGET_WINDOS) { | |
649 string ext = FileName.ext(p[0..arg.length]); | |
650 if (ext != null && FileName.compare(ext, "exe") == 0) | |
651 { | |
652 global.params.objname = arg[(p - arg.ptr)..$]; | |
653 continue; | |
654 } | |
655 } | |
656 files.push(cast(void*)new String(arg[(p - arg.ptr)..$])); | |
657 } | |
658 } | |
659 if (global.errors) | |
660 { | |
661 fatal(); | |
662 } | |
663 if (files.dim == 0) | |
664 { usage(); | |
665 return EXIT_FAILURE; | |
666 } | |
667 | |
668 if (!setdebuglib) | |
669 global.params.debuglibname = global.params.defaultlibname; | |
670 | |
671 version (TARGET_OSX) { | |
672 global.params.pic = 1; | |
673 } | |
674 | |
675 if (global.params.release) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
676 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
677 global.params.useInvariants = 0; |
0 | 678 global.params.useIn = 0; |
679 global.params.useOut = 0; | |
680 global.params.useAssert = 0; | |
681 global.params.useArrayBounds = 0; | |
682 global.params.useSwitchError = 0; | |
683 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
684 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
685 if (noboundscheck) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
686 global.params.useArrayBounds = 0; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
687 |
0 | 688 if (global.params.run) |
689 global.params.quiet = 1; | |
690 | |
691 if (global.params.useUnitTests) | |
692 global.params.useAssert = 1; | |
693 | |
694 if (!global.params.obj || global.params.lib) | |
695 global.params.link = 0; | |
696 | |
697 if (global.params.link) | |
698 { | |
699 global.params.exefile = global.params.objname; | |
700 global.params.oneobj = 1; | |
701 if (global.params.objname) | |
702 { | |
703 /* Use this to name the one object file with the same | |
704 * name as the exe file. | |
705 */ | |
706 global.params.objname = FileName.forceExt(global.params.objname, global.obj_ext).toChars(); | |
707 | |
708 /* If output directory is given, use that path rather than | |
709 * the exe file path. | |
710 */ | |
711 if (global.params.objdir) | |
712 { | |
713 string name = FileName.name(global.params.objname); | |
714 global.params.objname = FileName.combine(global.params.objdir, name); | |
715 } | |
716 } | |
717 } | |
718 else if (global.params.lib) | |
719 { | |
720 global.params.libname = global.params.objname; | |
721 global.params.objname = null; | |
722 | |
723 // Haven't investigated handling these options with multiobj | |
724 if (!global.params.cov && !global.params.trace) | |
725 global.params.multiobj = 1; | |
726 } | |
727 else if (global.params.run) | |
728 { | |
729 error("flags conflict with -run"); | |
730 fatal(); | |
731 } | |
732 else | |
733 { | |
734 if (global.params.objname && files.dim > 1) | |
735 { | |
736 global.params.oneobj = 1; | |
737 //error("multiple source files, but only one .obj name"); | |
738 //fatal(); | |
739 } | |
740 } | |
741 if (global.params.isX86_64) | |
742 { | |
743 VersionCondition.addPredefinedGlobalIdent("D_InlineAsm_X86_64"); | |
744 VersionCondition.addPredefinedGlobalIdent("X86_64"); | |
745 VersionCondition.addPredefinedGlobalIdent("D_LP64"); | |
746 version (TARGET_WINDOS) { | |
747 VersionCondition.addPredefinedGlobalIdent("Win64"); | |
748 } | |
749 } | |
750 else | |
751 { | |
752 VersionCondition.addPredefinedGlobalIdent("D_InlineAsm"); | |
753 VersionCondition.addPredefinedGlobalIdent("D_InlineAsm_X86"); | |
754 VersionCondition.addPredefinedGlobalIdent("X86"); | |
755 version (TARGET_WINDOS) { | |
756 VersionCondition.addPredefinedGlobalIdent("Win32"); | |
757 } | |
758 } | |
759 if (global.params.doDocComments) | |
760 VersionCondition.addPredefinedGlobalIdent("D_Ddoc"); | |
761 if (global.params.cov) | |
762 VersionCondition.addPredefinedGlobalIdent("D_Coverage"); | |
763 if (global.params.pic) | |
764 VersionCondition.addPredefinedGlobalIdent("D_PIC"); | |
765 version (DMDV2) { | |
766 if (global.params.useUnitTests) | |
767 VersionCondition.addPredefinedGlobalIdent("unittest"); | |
768 } | |
769 | |
770 // Initialization | |
771 Type.init(); | |
772 Id.initialize(); | |
773 Module.init(); | |
774 initPrecedence(); | |
775 | |
776 backend_init(); | |
777 | |
778 //printf("%d source files\n",files.dim); | |
779 | |
780 // Build import search path | |
781 if (global.params.imppath) | |
782 { | |
783 for (int i = 0; i < global.params.imppath.dim; i++) | |
784 { | |
785 string path = (cast(String)global.params.imppath.data[i]).str; | |
786 string[] a = FileName.splitPath(path); | |
787 | |
788 global.path ~= a; | |
789 } | |
790 } | |
791 | |
792 // Build string import search path | |
793 if (global.params.fileImppath) | |
794 { | |
795 for (int i = 0; i < global.params.fileImppath.dim; i++) | |
796 { | |
797 string path = (cast(String)global.params.fileImppath.data[i]).str; | |
798 string[] a = FileName.splitPath(path); | |
799 | |
800 global.filePath ~= a; | |
801 } | |
802 } | |
803 | |
804 // Create Modules | |
805 Array modules = new Array(); | |
806 modules.reserve(files.dim); | |
807 int firstmodule = 1; | |
808 for (int i = 0; i < files.dim; i++) | |
809 { | |
810 string ext; | |
811 string name; | |
812 | |
813 String s = cast(String) files.data[i]; | |
814 string mp = s.str; | |
815 | |
114 | 816 version (Windows) { |
0 | 817 char[] copy = null; |
818 // Convert / to \ so linker will work | |
819 foreach (j, c; mp) | |
820 { | |
821 if (c == '/') { | |
822 if (copy is null) copy = mp.dup; | |
823 copy[j] = '\\'; | |
824 } | |
825 } | |
826 | |
827 if (copy !is null) mp = assumeUnique(copy); | |
828 } | |
829 string p = mp; | |
830 | |
831 p = FileName.name(p); // strip path | |
832 ext = FileName.ext(p); | |
833 | |
834 if (ext.length != 0) | |
835 { /* Deduce what to do with a file based on its extension | |
836 */ | |
837 if (FileName.equals(ext, global.obj_ext)) | |
838 { | |
839 global.params.objfiles.push(files.data[i]); | |
840 libmodules.push(files.data[i]); | |
841 continue; | |
842 } | |
843 | |
844 if (FileName.equals(ext, global.lib_ext)) | |
845 { | |
846 global.params.libfiles.push(files.data[i]); | |
847 libmodules.push(files.data[i]); | |
848 continue; | |
849 } | |
850 | |
851 if (ext == global.ddoc_ext) | |
852 { | |
853 global.params.ddocfiles.push(files.data[i]); | |
854 continue; | |
855 } | |
79 | 856 |
857 if (FileName.equals(ext, global.json_ext)) | |
858 { | |
859 global.params.doXGeneration = 1; | |
860 global.params.xfilename = (cast(immutable(char)*)files.data[i])[0..0]; | |
861 continue; | |
862 } | |
0 | 863 |
79 | 864 version (TARGET_WINDOS) |
865 { | |
0 | 866 if (FileName.equals(ext, "res")) |
867 { | |
868 global.params.resfile = (cast(immutable(char)*)files.data[i])[0..0]; /// !!! | |
869 continue; | |
870 } | |
871 | |
872 if (FileName.equals(ext, "def")) | |
873 { | |
874 global.params.deffile = (cast(immutable(char)*)files.data[i])[0..0]; | |
875 continue; | |
876 } | |
877 | |
878 if (FileName.equals(ext, "exe")) | |
879 { | |
880 assert(0); // should have already been handled | |
881 } | |
882 } | |
883 | |
884 /* Examine extension to see if it is a valid | |
885 * D source file extension | |
886 */ | |
887 if (FileName.equals(ext, global.mars_ext) || | |
888 FileName.equals(ext, global.hdr_ext) || | |
889 FileName.equals(ext, "dd") || | |
890 FileName.equals(ext, "htm") || | |
891 FileName.equals(ext, "html") || | |
892 FileName.equals(ext, "xhtml")) | |
893 { | |
894 immutable(char)* e = ext.ptr; | |
895 e--; // skip onto '.' | |
896 assert(*e == '.'); | |
897 | |
898 immutable(char)* n = p.ptr; | |
899 | |
900 name = n[0..(e-n)]; // strip extension | |
901 | |
902 if (name.length == 0 || name == ".." || name == ".") | |
903 { | |
904 Linvalid: | |
905 error("invalid file name '%s'", (cast(String)files.data[i]).str); | |
906 fatal(); | |
907 } | |
908 } | |
909 else | |
910 { error("unrecognized file extension %s\n", ext); | |
911 fatal(); | |
912 } | |
913 } | |
914 else | |
915 { | |
916 name = p; | |
917 if (!*name) | |
918 goto Linvalid; | |
919 } | |
920 | |
921 /* At this point, name is the D source file name stripped of | |
922 * its path and extension. | |
923 */ | |
924 | |
925 Identifier id = new Identifier(name, TOK.TOKreserved); | |
926 m = new Module((cast(String) files.data[i]).str, id, global.params.doDocComments, global.params.doHdrGeneration); | |
927 modules.push(cast(void*)m); | |
928 | |
929 if (firstmodule) | |
930 { | |
931 global.params.objfiles.push(cast(void*)m.objfile.name); | |
932 firstmodule = 0; | |
933 } | |
934 } | |
935 | |
936 // Read files | |
937 //version = ASYNCREAD; | |
938 version (ASYNCREAD) { | |
939 // Multi threaded | |
940 AsyncRead *aw = AsyncRead.create(modules.dim); | |
941 for (i = 0; i < modules.dim; i++) | |
942 { | |
943 m = cast(Module *)modules.data[i]; | |
944 aw.addFile(m.srcfile); | |
945 } | |
946 aw.start(); | |
947 } else { | |
948 // Single threaded | |
949 for (int i = 0; i < modules.dim; i++) | |
950 { | |
951 m = cast(Module)modules.data[i]; | |
952 m.read(Loc(0)); | |
953 } | |
954 } | |
955 | |
956 // Parse files | |
957 int anydocfiles = 0; | |
958 for (int i = 0; i < modules.dim; i++) | |
959 { | |
960 m = cast(Module)modules.data[i]; | |
961 if (global.params.verbose) | |
962 writef("parse %s\n", m.toChars()); | |
963 if (!Module.rootModule) | |
964 Module.rootModule = m; | |
965 m.importedFrom = m; | |
966 if (!global.params.oneobj || i == 0 || m.isDocFile) | |
967 m.deleteObjFile(); | |
968 version (ASYNCREAD) { | |
969 if (aw.read(i)) | |
970 { | |
971 error("cannot read file %s", m.srcfile.name.toChars()); | |
972 } | |
973 } | |
974 | |
975 //while (loop) {} | |
976 m.parse(); | |
977 if (m.isDocFile) | |
978 { | |
979 anydocfiles = 1; | |
980 m.gendocfile(); | |
981 | |
982 // Remove m from list of modules | |
983 modules.remove(i); | |
984 i--; | |
985 | |
986 // Remove m's object file from list of object files | |
987 for (int j = 0; j < global.params.objfiles.dim; j++) | |
988 { | |
989 if (m.objfile.name.str == (cast(FileName)global.params.objfiles.data[j]).str) | |
990 { | |
991 global.params.objfiles.remove(j); | |
992 break; | |
993 } | |
994 } | |
995 | |
996 if (global.params.objfiles.dim == 0) | |
997 global.params.link = 0; | |
998 } | |
999 } | |
1000 version (ASYNCREAD) { | |
1001 AsyncRead.dispose(aw); | |
1002 } | |
1003 | |
1004 if (anydocfiles && modules.dim && | |
1005 (global.params.oneobj || global.params.objname)) | |
1006 { | |
1007 error("conflicting Ddoc and obj generation options"); | |
1008 fatal(); | |
1009 } | |
1010 if (global.errors) | |
1011 fatal(); | |
79 | 1012 version (_DH) |
1013 { | |
0 | 1014 if (global.params.doHdrGeneration) |
1015 { | |
1016 /* Generate 'header' import files. | |
1017 * Since 'header' import files must be independent of command | |
1018 * line switches and what else is imported, they are generated | |
1019 * before any semantic analysis. | |
1020 */ | |
1021 for (i = 0; i < modules.dim; i++) | |
1022 { | |
79 | 1023 m = cast(Module)modules.data[i]; |
0 | 1024 if (global.params.verbose) |
1025 writef("import %s\n", m.toChars()); | |
1026 m.genhdrfile(); | |
1027 } | |
1028 } | |
1029 if (global.errors) | |
1030 fatal(); | |
1031 } | |
79 | 1032 //load all unconditional imports for better symbol resolving |
1033 for (int i = 0; i < modules.dim; i++) | |
1034 { | |
1035 m = cast(Module)modules.data[i]; | |
1036 if (global.params.verbose) | |
1037 writef("importall %s\n", m.toChars()); | |
1038 m.importAll(null); | |
1039 } | |
1040 if (global.errors) | |
1041 fatal(); | |
164 | 1042 |
0 | 1043 // Do semantic analysis |
1044 for (int i = 0; i < modules.dim; i++) | |
1045 { | |
1046 m = cast(Module)modules.data[i]; | |
1047 if (global.params.verbose) | |
1048 writef("semantic %s\n", m.toChars()); | |
1049 m.semantic(); | |
1050 } | |
1051 if (global.errors) | |
1052 fatal(); | |
164 | 1053 |
0 | 1054 // Do pass 2 semantic analysis |
1055 for (int i = 0; i < modules.dim; i++) | |
1056 { | |
1057 m = cast(Module)modules.data[i]; | |
1058 if (global.params.verbose) | |
1059 writef("semantic2 %s\n", m.toChars()); | |
1060 m.semantic2(); | |
1061 } | |
1062 if (global.errors) | |
1063 fatal(); | |
1064 | |
1065 // Do pass 3 semantic analysis | |
1066 for (int i = 0; i < modules.dim; i++) | |
1067 { | |
1068 m = cast(Module)modules.data[i]; | |
1069 if (global.params.verbose) | |
1070 writef("semantic3 %s\n", m.toChars()); | |
1071 m.semantic3(); | |
1072 } | |
1073 if (global.errors) | |
1074 fatal(); | |
164 | 1075 |
0 | 1076 if (global.params.moduleDeps !is null) |
1077 { | |
1078 assert(global.params.moduleDepsFile !is null); | |
1079 | |
1080 File deps = new File(global.params.moduleDepsFile); | |
1081 OutBuffer ob = global.params.moduleDeps; | |
1082 deps.setbuffer(cast(void*)ob.data, ob.offset); | |
1083 deps.writev(); | |
1084 } | |
1085 | |
1086 | |
1087 // Scan for functions to inline | |
1088 if (global.params.useInline) | |
1089 { | |
1090 /* The problem with useArrayBounds and useAssert is that the | |
1091 * module being linked to may not have generated them, so if | |
1092 * we inline functions from those modules, the symbols for them will | |
1093 * not be found at link time. | |
1094 */ | |
1095 if (!global.params.useArrayBounds && !global.params.useAssert) | |
1096 { | |
1097 // Do pass 3 semantic analysis on all imported modules, | |
1098 // since otherwise functions in them cannot be inlined | |
1099 for (int i = 0; i < Module.amodules.dim; i++) | |
1100 { | |
1101 m = cast(Module)Module.amodules.data[i]; | |
1102 if (global.params.verbose) | |
1103 writef("semantic3 %s\n", m.toChars()); | |
1104 m.semantic3(); | |
1105 } | |
1106 if (global.errors) | |
1107 fatal(); | |
1108 } | |
1109 | |
1110 for (int i = 0; i < modules.dim; i++) | |
1111 { | |
1112 m = cast(Module)modules.data[i]; | |
1113 if (global.params.verbose) | |
1114 writef("inline scan %s\n", m.toChars()); | |
1115 | |
1116 m.inlineScan(); | |
1117 } | |
1118 } | |
1119 if (global.errors) | |
1120 fatal(); | |
1121 | |
1122 Library library = null; | |
1123 if (global.params.lib) | |
1124 { | |
1125 library = new Library(); | |
1126 library.setFilename(global.params.objdir, global.params.libname); | |
1127 | |
1128 // Add input object and input library files to output library | |
1129 for (int i = 0; i < libmodules.dim; i++) | |
1130 { | |
1131 string p = (cast(String)libmodules.data[i]).str; | |
1132 library.addObject(p, null, 0); | |
1133 } | |
1134 } | |
1135 | |
1136 // Generate output files | |
79 | 1137 if (global.params.doXGeneration) |
1138 json_generate(modules); | |
1139 | |
0 | 1140 if (global.params.oneobj) |
1141 { | |
1142 for (int i = 0; i < modules.dim; i++) | |
1143 { | |
1144 m = cast(Module)modules.data[i]; | |
1145 if (global.params.verbose) | |
1146 writef("code %s\n", m.toChars()); | |
1147 if (i == 0) | |
1148 obj_start(cast(char*)toStringz(m.srcfile.toChars())); | |
1149 m.genobjfile(0); | |
1150 if (!global.errors && global.params.doDocComments) | |
1151 m.gendocfile(); | |
1152 } | |
1153 if (!global.errors && modules.dim) | |
1154 { | |
1155 obj_end(library, (cast(Module)modules.data[0]).objfile); | |
1156 } | |
1157 } | |
1158 else | |
1159 { | |
1160 for (int i = 0; i < modules.dim; i++) | |
1161 { | |
1162 m = cast(Module)modules.data[i]; | |
1163 if (global.params.verbose) | |
1164 writef("code %s\n", m.toChars()); | |
1165 if (global.params.obj) | |
1166 { | |
1167 obj_start(cast(char*)toStringz(m.srcfile.toChars())); | |
1168 m.genobjfile(global.params.multiobj); | |
1169 obj_end(library, m.objfile); | |
1170 obj_write_deferred(library); | |
1171 } | |
1172 if (global.errors) | |
1173 { | |
1174 if (!global.params.lib) | |
1175 m.deleteObjFile(); | |
1176 } | |
1177 else | |
1178 { | |
1179 if (global.params.doDocComments) | |
1180 m.gendocfile(); | |
1181 } | |
1182 } | |
164 | 1183 } |
0 | 1184 |
1185 if (global.params.lib && !global.errors) | |
1186 library.write(); | |
1187 | |
1188 backend_term(); | |
1189 if (global.errors) | |
1190 fatal(); | |
1191 | |
1192 if (!global.params.objfiles.dim) | |
1193 { | |
1194 if (global.params.link) | |
1195 error("no object files to link"); | |
1196 } | |
1197 else | |
1198 { | |
1199 if (global.params.link) | |
1200 status = runLINK(); | |
1201 | |
1202 if (global.params.run) | |
1203 { | |
1204 if (!status) | |
1205 { | |
1206 status = runProgram(); | |
1207 | |
1208 /* Delete .obj files and .exe file | |
1209 */ | |
1210 for (int i = 0; i < modules.dim; i++) | |
1211 { | |
1212 m = cast(Module)modules.data[i]; | |
1213 m.deleteObjFile(); | |
1214 if (global.params.oneobj) | |
1215 break; | |
1216 } | |
1217 deleteExeFile(); | |
1218 } | |
1219 } | |
1220 } | |
1221 | |
1222 return status; | |
14
2cc604139636
Implemented Linux support for ddmd. Some parts are a bit hacky to just "get it working", that said, druntime and phobos compile, and unittests pass.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
1223 } |