comparison gen/main.cpp @ 1007:55051098d9fc

Readded indentation that got lost in the dmd/mars.c -> gen/main.cpp move.
author Christian Kamm <kamm incasoftware de>
date Sat, 28 Feb 2009 15:06:09 +0100
parents 2b6c2e961f25
children d1ec9ff0e9ba
comparison
equal deleted inserted replaced
1006:132bece5ae62 1007:55051098d9fc
226 if (!I->empty()) 226 if (!I->empty())
227 files.push(mem.strdup(I->c_str())); 227 files.push(mem.strdup(I->c_str()));
228 228
229 if (global.errors) 229 if (global.errors)
230 { 230 {
231 fatal(); 231 fatal();
232 } 232 }
233 if (files.dim == 0) 233 if (files.dim == 0)
234 { 234 {
235 cl::PrintHelpMessage(); 235 cl::PrintHelpMessage();
236 return EXIT_FAILURE; 236 return EXIT_FAILURE;
237 } 237 }
238 238
239 Array* libs; 239 Array* libs;
240 if (global.params.symdebug) 240 if (global.params.symdebug)
241 libs = global.params.debuglibnames; 241 libs = global.params.debuglibnames;
242 else 242 else
243 libs = global.params.defaultlibnames; 243 libs = global.params.defaultlibnames;
244 244
245 if (libs) 245 if (libs)
246 { 246 {
247 for (int i = 0; i < libs->dim; i++) 247 for (int i = 0; i < libs->dim; i++)
248 { 248 {
249 char *arg = (char *)mem.malloc(64); 249 char *arg = (char *)mem.malloc(64);
250 strcpy(arg, "-l"); 250 strcpy(arg, "-l");
251 strncat(arg, (char *)libs->data[i], 64); 251 strncat(arg, (char *)libs->data[i], 64);
252 global.params.linkswitches->push(arg);
253 }
254 }
255 else if (!noDefaultLib)
256 {
257 char *arg;
258 arg = (char *)mem.malloc(64);
259 strcpy(arg, "-lldc-runtime");
252 global.params.linkswitches->push(arg); 260 global.params.linkswitches->push(arg);
253 } 261 arg = (char *)mem.malloc(64);
254 } 262 strcpy(arg, "-ltango-cc-tango");
255 else if (!noDefaultLib) 263 global.params.linkswitches->push(arg);
256 { 264 arg = (char *)mem.malloc(64);
257 char *arg; 265 strcpy(arg, "-ltango-gc-basic");
258 arg = (char *)mem.malloc(64); 266 global.params.linkswitches->push(arg);
259 strcpy(arg, "-lldc-runtime"); 267 // pass the runtime again to resolve issues
260 global.params.linkswitches->push(arg); 268 // with linking order
261 arg = (char *)mem.malloc(64); 269 arg = (char *)mem.malloc(64);
262 strcpy(arg, "-ltango-cc-tango"); 270 strcpy(arg, "-lldc-runtime");
263 global.params.linkswitches->push(arg); 271 global.params.linkswitches->push(arg);
264 arg = (char *)mem.malloc(64);
265 strcpy(arg, "-ltango-gc-basic");
266 global.params.linkswitches->push(arg);
267 // pass the runtime again to resolve issues
268 // with linking order
269 arg = (char *)mem.malloc(64);
270 strcpy(arg, "-lldc-runtime");
271 global.params.linkswitches->push(arg);
272 } 272 }
273 273
274 if (global.params.run) 274 if (global.params.run)
275 quiet = 1; 275 quiet = 1;
276 276
277 if (global.params.useUnitTests) 277 if (global.params.useUnitTests)
278 global.params.useAssert = 1; 278 global.params.useAssert = 1;
279 279
280 // LDC output determination 280 // LDC output determination
281 281
282 // if we don't link, autodetect target from extension 282 // if we don't link, autodetect target from extension
283 if(!global.params.link && global.params.objname) { 283 if(!global.params.link && global.params.objname) {
284 ext = FileName::ext(global.params.objname); 284 ext = FileName::ext(global.params.objname);
285 bool autofound = false; 285 bool autofound = false;
286 if (!ext) { 286 if (!ext) {
287 // keep things as they are 287 // keep things as they are
288 } else if (strcmp(ext, global.ll_ext) == 0) { 288 } else if (strcmp(ext, global.ll_ext) == 0) {
289 global.params.output_ll = OUTPUTFLAGset; 289 global.params.output_ll = OUTPUTFLAGset;
290 autofound = true; 290 autofound = true;
291 } else if (strcmp(ext, global.bc_ext) == 0) { 291 } else if (strcmp(ext, global.bc_ext) == 0) {
292 global.params.output_bc = OUTPUTFLAGset; 292 global.params.output_bc = OUTPUTFLAGset;
293 autofound = true; 293 autofound = true;
294 } else if (strcmp(ext, global.s_ext) == 0) { 294 } else if (strcmp(ext, global.s_ext) == 0) {
295 global.params.output_s = OUTPUTFLAGset; 295 global.params.output_s = OUTPUTFLAGset;
296 autofound = true; 296 autofound = true;
297 } else if (strcmp(ext, global.obj_ext) == 0) { 297 } else if (strcmp(ext, global.obj_ext) == 0) {
298 global.params.output_o = OUTPUTFLAGset; 298 global.params.output_o = OUTPUTFLAGset;
299 autofound = true; 299 autofound = true;
300 } else { 300 } else {
301 // append dot, so forceExt won't change existing name even if it contains dots 301 // append dot, so forceExt won't change existing name even if it contains dots
302 size_t len = strlen(global.params.objname); 302 size_t len = strlen(global.params.objname);
303 size_t extlen = strlen("."); 303 size_t extlen = strlen(".");
304 char* s = (char *)mem.malloc(len + 1 + extlen + 1); 304 char* s = (char *)mem.malloc(len + 1 + extlen + 1);
305 memcpy(s, global.params.objname, len); 305 memcpy(s, global.params.objname, len);
306 s[len] = '.'; 306 s[len] = '.';
307 s[len+1+extlen] = 0; 307 s[len+1+extlen] = 0;
308 global.params.objname = s; 308 global.params.objname = s;
309 309
310 } 310 }
311 if(autofound && global.params.output_o == OUTPUTFLAGdefault) 311 if(autofound && global.params.output_o == OUTPUTFLAGdefault)
312 global.params.output_o = OUTPUTFLAGno; 312 global.params.output_o = OUTPUTFLAGno;
313 } 313 }
314 314
315 // only link if possible 315 // only link if possible
316 if (!global.params.obj || !global.params.output_o) 316 if (!global.params.obj || !global.params.output_o)
317 global.params.link = 0; 317 global.params.link = 0;
318 318
319 if (global.params.link) 319 if (global.params.link)
320 { 320 {
321 global.params.exefile = global.params.objname; 321 global.params.exefile = global.params.objname;
322 if (files.dim > 1) 322 if (files.dim > 1)
323 global.params.objname = NULL; 323 global.params.objname = NULL;
324 } 324 }
325 else if (global.params.run) 325 else if (global.params.run)
326 { 326 {
327 error("flags conflict with -run"); 327 error("flags conflict with -run");
328 fatal(); 328 fatal();
329 } 329 }
330 else 330 else
331 { 331 {
332 if (global.params.objname && files.dim > 1) 332 if (global.params.objname && files.dim > 1)
333 { 333 {
334 error("multiple source files, but only one .obj name"); 334 error("multiple source files, but only one .obj name");
335 fatal(); 335 fatal();
336 } 336 }
337 } 337 }
338 338
339 // create a proper target 339 // create a proper target
340 llvm::Module mod("dummy"); 340 llvm::Module mod("dummy");
341 341
531 //printf("%d source files\n",files.dim); 531 //printf("%d source files\n",files.dim);
532 532
533 // Build import search path 533 // Build import search path
534 if (global.params.imppath) 534 if (global.params.imppath)
535 { 535 {
536 for (int i = 0; i < global.params.imppath->dim; i++) 536 for (int i = 0; i < global.params.imppath->dim; i++)
537 { 537 {
538 char *path = (char *)global.params.imppath->data[i]; 538 char *path = (char *)global.params.imppath->data[i];
539 Array *a = FileName::splitPath(path); 539 Array *a = FileName::splitPath(path);
540 540
541 if (a) 541 if (a)
542 { 542 {
543 if (!global.path) 543 if (!global.path)
544 global.path = new Array(); 544 global.path = new Array();
545 global.path->append(a); 545 global.path->append(a);
546 } 546 }
547 } 547 }
548 } 548 }
549 549
550 // Build string import search path 550 // Build string import search path
551 if (global.params.fileImppath) 551 if (global.params.fileImppath)
552 { 552 {
553 for (int i = 0; i < global.params.fileImppath->dim; i++) 553 for (int i = 0; i < global.params.fileImppath->dim; i++)
554 { 554 {
555 char *path = (char *)global.params.fileImppath->data[i]; 555 char *path = (char *)global.params.fileImppath->data[i];
556 Array *a = FileName::splitPath(path); 556 Array *a = FileName::splitPath(path);
557 557
558 if (a) 558 if (a)
559 { 559 {
560 if (!global.filePath) 560 if (!global.filePath)
561 global.filePath = new Array(); 561 global.filePath = new Array();
562 global.filePath->append(a); 562 global.filePath->append(a);
563 } 563 }
564 } 564 }
565 } 565 }
566 566
567 // Create Modules 567 // Create Modules
568 Array modules; 568 Array modules;
569 modules.reserve(files.dim); 569 modules.reserve(files.dim);
570 for (int i = 0; i < files.dim; i++) 570 for (int i = 0; i < files.dim; i++)
571 { Identifier *id; 571 { Identifier *id;
572 char *ext; 572 char *ext;
573 char *name; 573 char *name;
574 574
575 p = (char *) files.data[i]; 575 p = (char *) files.data[i];
576 576
577 p = FileName::name(p); // strip path 577 p = FileName::name(p); // strip path
578 ext = FileName::ext(p); 578 ext = FileName::ext(p);
579 if (ext) 579 if (ext)
580 { 580 {
581 #if POSIX 581 #if POSIX
582 if (strcmp(ext, global.obj_ext) == 0 || 582 if (strcmp(ext, global.obj_ext) == 0 ||
583 strcmp(ext, global.bc_ext) == 0) 583 strcmp(ext, global.bc_ext) == 0)
584 #else 584 #else
585 if (stricmp(ext, global.obj_ext) == 0 || 585 if (stricmp(ext, global.obj_ext) == 0 ||
586 stricmp(ext, global.bc_ext) == 0) 586 stricmp(ext, global.bc_ext) == 0)
587 #endif 587 #endif
588 { 588 {
589 global.params.objfiles->push(files.data[i]); 589 global.params.objfiles->push(files.data[i]);
590 continue; 590 continue;
591 } 591 }
592 592
593 #if POSIX 593 #if POSIX
594 if (strcmp(ext, "a") == 0) 594 if (strcmp(ext, "a") == 0)
595 #elif __MINGW32__ 595 #elif __MINGW32__
596 if (stricmp(ext, "a") == 0) 596 if (stricmp(ext, "a") == 0)
597 #else 597 #else
598 if (stricmp(ext, "lib") == 0) 598 if (stricmp(ext, "lib") == 0)
599 #endif 599 #endif
600 { 600 {
601 global.params.libfiles->push(files.data[i]); 601 global.params.libfiles->push(files.data[i]);
602 continue; 602 continue;
603 } 603 }
604 604
605 if (strcmp(ext, global.ddoc_ext) == 0) 605 if (strcmp(ext, global.ddoc_ext) == 0)
606 { 606 {
607 global.params.ddocfiles->push(files.data[i]); 607 global.params.ddocfiles->push(files.data[i]);
608 continue; 608 continue;
609 } 609 }
610 610
611 #if !POSIX 611 #if !POSIX
612 if (stricmp(ext, "res") == 0) 612 if (stricmp(ext, "res") == 0)
613 { 613 {
614 global.params.resfile = (char *)files.data[i]; 614 global.params.resfile = (char *)files.data[i];
615 continue; 615 continue;
616 } 616 }
617 617
618 if (stricmp(ext, "def") == 0) 618 if (stricmp(ext, "def") == 0)
619 { 619 {
620 global.params.deffile = (char *)files.data[i]; 620 global.params.deffile = (char *)files.data[i];
621 continue; 621 continue;
622 } 622 }
623 623
624 if (stricmp(ext, "exe") == 0) 624 if (stricmp(ext, "exe") == 0)
625 { 625 {
626 global.params.exefile = (char *)files.data[i]; 626 global.params.exefile = (char *)files.data[i];
627 continue; 627 continue;
628 } 628 }
629 #endif 629 #endif
630 630
631 if (stricmp(ext, global.mars_ext) == 0 || 631 if (stricmp(ext, global.mars_ext) == 0 ||
632 stricmp(ext, global.hdr_ext) == 0 || 632 stricmp(ext, global.hdr_ext) == 0 ||
633 stricmp(ext, "htm") == 0 || 633 stricmp(ext, "htm") == 0 ||
634 stricmp(ext, "html") == 0 || 634 stricmp(ext, "html") == 0 ||
635 stricmp(ext, "xhtml") == 0) 635 stricmp(ext, "xhtml") == 0)
636 { 636 {
637 ext--; // skip onto '.' 637 ext--; // skip onto '.'
638 assert(*ext == '.'); 638 assert(*ext == '.');
639 name = (char *)mem.malloc((ext - p) + 1); 639 name = (char *)mem.malloc((ext - p) + 1);
640 memcpy(name, p, ext - p); 640 memcpy(name, p, ext - p);
641 name[ext - p] = 0; // strip extension 641 name[ext - p] = 0; // strip extension
642 642
643 if (name[0] == 0 || 643 if (name[0] == 0 ||
644 strcmp(name, "..") == 0 || 644 strcmp(name, "..") == 0 ||
645 strcmp(name, ".") == 0) 645 strcmp(name, ".") == 0)
646 { 646 {
647 Linvalid: 647 Linvalid:
648 error("invalid file name '%s'", (char *)files.data[i]); 648 error("invalid file name '%s'", (char *)files.data[i]);
649 fatal(); 649 fatal();
650 } 650 }
651 }
652 else
653 { error("unrecognized file extension %s\n", ext);
654 fatal();
655 }
651 } 656 }
652 else 657 else
653 { error("unrecognized file extension %s\n", ext); 658 { name = p;
654 fatal(); 659 if (!*name)
655 } 660 goto Linvalid;
656 } 661 }
657 else 662
658 { name = p; 663 id = new Identifier(name, 0);
659 if (!*name) 664 m = new Module((char *) files.data[i], id, global.params.doDocComments, global.params.doHdrGeneration);
660 goto Linvalid; 665 modules.push(m);
661 }
662
663 id = new Identifier(name, 0);
664 m = new Module((char *) files.data[i], id, global.params.doDocComments, global.params.doHdrGeneration);
665 modules.push(m);
666 } 666 }
667 667
668 // Read files, parse them 668 // Read files, parse them
669 for (int i = 0; i < modules.dim; i++) 669 for (int i = 0; i < modules.dim; i++)
670 { 670 {
671 m = (Module *)modules.data[i]; 671 m = (Module *)modules.data[i];
672 if (global.params.verbose) 672 if (global.params.verbose)
673 printf("parse %s\n", m->toChars()); 673 printf("parse %s\n", m->toChars());
674 if (!Module::rootModule) 674 if (!Module::rootModule)
675 Module::rootModule = m; 675 Module::rootModule = m;
676 m->importedFrom = m; 676 m->importedFrom = m;
677 m->read(0); 677 m->read(0);
678 m->parse(); 678 m->parse();
679 m->buildTargetFiles(); 679 m->buildTargetFiles();
680 m->deleteObjFile(); 680 m->deleteObjFile();
681 if (m->isDocFile) 681 if (m->isDocFile)
682 { 682 {
683 m->gendocfile(); 683 m->gendocfile();
684 684
685 // Remove m from list of modules 685 // Remove m from list of modules
686 modules.remove(i); 686 modules.remove(i);
687 i--; 687 i--;
688 } 688 }
689 } 689 }
690 if (global.errors) 690 if (global.errors)
691 fatal(); 691 fatal();
692 #ifdef _DH 692 #ifdef _DH
693 if (global.params.doHdrGeneration) 693 if (global.params.doHdrGeneration)
694 { 694 {
695 /* Generate 'header' import files. 695 /* Generate 'header' import files.
696 * Since 'header' import files must be independent of command 696 * Since 'header' import files must be independent of command
697 * line switches and what else is imported, they are generated 697 * line switches and what else is imported, they are generated
698 * before any semantic analysis. 698 * before any semantic analysis.
699 */ 699 */
700 for (int i = 0; i < modules.dim; i++)
701 {
702 m = (Module *)modules.data[i];
703 if (global.params.verbose)
704 printf("import %s\n", m->toChars());
705 m->genhdrfile();
706 }
707 }
708 if (global.errors)
709 fatal();
710 #endif
711
712 // Do semantic analysis
700 for (int i = 0; i < modules.dim; i++) 713 for (int i = 0; i < modules.dim; i++)
701 { 714 {
702 m = (Module *)modules.data[i]; 715 m = (Module *)modules.data[i];
703 if (global.params.verbose) 716 if (global.params.verbose)
704 printf("import %s\n", m->toChars()); 717 printf("semantic %s\n", m->toChars());
705 m->genhdrfile(); 718 m->semantic();
706 }
707 } 719 }
708 if (global.errors) 720 if (global.errors)
709 fatal(); 721 fatal();
710 #endif
711
712 // Do semantic analysis
713 for (int i = 0; i < modules.dim; i++)
714 {
715 m = (Module *)modules.data[i];
716 if (global.params.verbose)
717 printf("semantic %s\n", m->toChars());
718 m->semantic();
719 }
720 if (global.errors)
721 fatal();
722 722
723 // Do pass 2 semantic analysis 723 // Do pass 2 semantic analysis
724 for (int i = 0; i < modules.dim; i++) 724 for (int i = 0; i < modules.dim; i++)
725 { 725 {
726 m = (Module *)modules.data[i]; 726 m = (Module *)modules.data[i];
727 if (global.params.verbose) 727 if (global.params.verbose)
728 printf("semantic2 %s\n", m->toChars()); 728 printf("semantic2 %s\n", m->toChars());
729 m->semantic2(); 729 m->semantic2();
730 } 730 }
731 if (global.errors) 731 if (global.errors)
732 fatal(); 732 fatal();
733 733
734 // Do pass 3 semantic analysis 734 // Do pass 3 semantic analysis
735 for (int i = 0; i < modules.dim; i++) 735 for (int i = 0; i < modules.dim; i++)
736 { 736 {
737 m = (Module *)modules.data[i]; 737 m = (Module *)modules.data[i];
738 if (global.params.verbose) 738 if (global.params.verbose)
739 printf("semantic3 %s\n", m->toChars()); 739 printf("semantic3 %s\n", m->toChars());
740 m->semantic3(); 740 m->semantic3();
741 } 741 }
742 if (global.errors) 742 if (global.errors)
743 fatal(); 743 fatal();
744 744
745 #if !IN_LLVM 745 #if !IN_LLVM
746 // Scan for functions to inline 746 // Scan for functions to inline
747 if (global.params.useInline) 747 if (global.params.useInline)
748 { 748 {
749 /* The problem with useArrayBounds and useAssert is that the 749 /* The problem with useArrayBounds and useAssert is that the
750 * module being linked to may not have generated them, so if 750 * module being linked to may not have generated them, so if
751 * we inline functions from those modules, the symbols for them will 751 * we inline functions from those modules, the symbols for them will
752 * not be found at link time. 752 * not be found at link time.
753 */ 753 */
754 if (!global.params.useArrayBounds && !global.params.useAssert) 754 if (!global.params.useArrayBounds && !global.params.useAssert)
755 { 755 {
756 #endif 756 #endif
757 // Do pass 3 semantic analysis on all imported modules, 757 // Do pass 3 semantic analysis on all imported modules,
758 // since otherwise functions in them cannot be inlined 758 // since otherwise functions in them cannot be inlined
759 for (int i = 0; i < Module::amodules.dim; i++) 759 for (int i = 0; i < Module::amodules.dim; i++)
760 { 760 {
761 m = (Module *)Module::amodules.data[i]; 761 m = (Module *)Module::amodules.data[i];
762 if (global.params.verbose) 762 if (global.params.verbose)
763 printf("semantic3 %s\n", m->toChars()); 763 printf("semantic3 %s\n", m->toChars());
764 m->semantic3(); 764 m->semantic3();
765 } 765 }
766 if (global.errors) 766 if (global.errors)
767 fatal(); 767 fatal();
768 #if !IN_LLVM 768 #if !IN_LLVM
769 } 769 }
770 770
771 for (int i = 0; i < modules.dim; i++)
772 {
773 m = (Module *)modules.data[i];
774 if (global.params.verbose)
775 printf("inline scan %s\n", m->toChars());
776 m->inlineScan();
777 }
778 }
779 #endif
780 if (global.errors)
781 fatal();
782
783 // Generate output files
771 for (int i = 0; i < modules.dim; i++) 784 for (int i = 0; i < modules.dim; i++)
772 { 785 {
773 m = (Module *)modules.data[i]; 786 m = (Module *)modules.data[i];
774 if (global.params.verbose) 787 if (global.params.verbose)
775 printf("inline scan %s\n", m->toChars()); 788 printf("code %s\n", m->toChars());
776 m->inlineScan(); 789 if (global.params.obj)
777 } 790 {
778 } 791 m->genobjfile(0);
779 #endif 792 global.params.objfiles->push(m->objfile->name->str);
780 if (global.errors) 793 }
781 fatal(); 794 if (global.errors)
782 795 m->deleteObjFile();
783 // Generate output files 796 else
784 for (int i = 0; i < modules.dim; i++) 797 {
785 { 798 if (global.params.doDocComments)
786 m = (Module *)modules.data[i]; 799 m->gendocfile();
787 if (global.params.verbose) 800 }
788 printf("code %s\n", m->toChars());
789 if (global.params.obj)
790 {
791 m->genobjfile(0);
792 global.params.objfiles->push(m->objfile->name->str);
793 }
794 if (global.errors)
795 m->deleteObjFile();
796 else
797 {
798 if (global.params.doDocComments)
799 m->gendocfile();
800 }
801 } 801 }
802 802
803 backend_term(); 803 backend_term();
804 if (global.errors) 804 if (global.errors)
805 fatal(); 805 fatal();
806 806
807 if (!global.params.objfiles->dim) 807 if (!global.params.objfiles->dim)
808 { 808 {
809 if (global.params.link) 809 if (global.params.link)
810 error("no object files to link"); 810 error("no object files to link");
811 } 811 }
812 else 812 else
813 { 813 {
814 if (global.params.link) 814 if (global.params.link)
815 //status = runLINK(); 815 //status = runLINK();
816 linkObjToExecutable(global.params.argv0); 816 linkObjToExecutable(global.params.argv0);
817 817
818 if (global.params.run) 818 if (global.params.run)
819 { 819 {
820 if (!status) 820 if (!status)
821 { 821 {
822 status = runExectuable(); 822 status = runExectuable();
823 823
824 /* Delete .obj files and .exe file 824 /* Delete .obj files and .exe file
825 */ 825 */
826 for (int i = 0; i < modules.dim; i++) 826 for (int i = 0; i < modules.dim; i++)
827 { 827 {
828 m = (Module *)modules.data[i]; 828 m = (Module *)modules.data[i];
829 m->deleteObjFile(); 829 m->deleteObjFile();
830 } 830 }
831 deleteExecutable(); 831 deleteExecutable();
832 } 832 }
833 } 833 }
834 } 834 }
835 835
836 return status; 836 return status;
837 } 837 }