comparison dmd/mars.c @ 1:c53b6e3fe49a trunk

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