comparison gen/tollvm.cpp @ 107:3efbcc81ba45 trunk

[svn r111] Fixed most problems with complex number support and added typeinfo for them. Added typeinfo ti_C. Did some changes to the way expressions that have both lvalue and rvalue LLVM values are handled.
author lindquist
date Tue, 20 Nov 2007 00:02:35 +0100
parents 5b5194b25f33
children 288fe1029e1f
comparison
equal deleted inserted replaced
106:5b5194b25f33 107:3efbcc81ba45
686 if (DtoIsPassedByRef(realtype)) { 686 if (DtoIsPassedByRef(realtype)) {
687 if (!fnarg || !fnarg->llvmCopy) { 687 if (!fnarg || !fnarg->llvmCopy) {
688 if (DSliceValue* sv = arg->isSlice()) { 688 if (DSliceValue* sv = arg->isSlice()) {
689 retval = new llvm::AllocaInst(DtoType(realtype), "tmpparam", gIR->topallocapoint()); 689 retval = new llvm::AllocaInst(DtoType(realtype), "tmpparam", gIR->topallocapoint());
690 DtoSetArray(retval, DtoArrayLen(sv), DtoArrayPtr(sv)); 690 DtoSetArray(retval, DtoArrayLen(sv), DtoArrayPtr(sv));
691 }
692 else if (DComplexValue* cv = arg->isComplex()) {
693 retval = new llvm::AllocaInst(DtoType(realtype), "tmpparam", gIR->topallocapoint());
694 DtoComplexSet(retval, cv->re, cv->im);
691 } 695 }
692 else { 696 else {
693 retval = arg->getRVal(); 697 retval = arg->getRVal();
694 } 698 }
695 } 699 }
717 } 721 }
718 else { 722 else {
719 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint()); 723 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint());
720 } 724 }
721 } 725 }
726 else if (realtype->iscomplex()) {
727 if (arg->isComplex()) {
728 allocaInst = new llvm::AllocaInst(realtypell, "tmpparam", gIR->topallocapoint());
729 }
730 else {
731 allocaInst = new llvm::AllocaInst(pty->getElementType(), "tmpparam", gIR->topallocapoint());
732 }
733 }
722 else 734 else
723 assert(0); 735 assert(0);
724 736
725 DValue* dst = new DVarValue(realtype, allocaInst, true); 737 DValue* dst = new DVarValue(realtype, allocaInst, true);
726 DtoAssign(dst,arg); 738 DtoAssign(dst,arg);
733 Logger::println("regular arg"); 745 Logger::println("regular arg");
734 if (DSliceValue* sl = arg->isSlice()) { 746 if (DSliceValue* sl = arg->isSlice()) {
735 if (sl->ptr) Logger::cout() << "ptr = " << *sl->ptr << '\n'; 747 if (sl->ptr) Logger::cout() << "ptr = " << *sl->ptr << '\n';
736 if (sl->len) Logger::cout() << "len = " << *sl->len << '\n'; 748 if (sl->len) Logger::cout() << "len = " << *sl->len << '\n';
737 assert(0); 749 assert(0);
750 }
751 else if (DComplexValue* cl = arg->isComplex()) {
752 assert(0 && "complex in the wrong place");
738 } 753 }
739 else { 754 else {
740 retval = arg->getRVal(); 755 retval = arg->getRVal();
741 } 756 }
742 } 757 }
893 DtoStore(rhs->getRVal(), lhs->getLVal()); 908 DtoStore(rhs->getRVal(), lhs->getLVal());
894 } 909 }
895 } 910 }
896 else if (t->iscomplex()) { 911 else if (t->iscomplex()) {
897 assert(!lhs->isComplex()); 912 assert(!lhs->isComplex());
898 if (DComplexValue* cx = rhs->isComplex()) { 913
899 DtoComplexSet(lhs->getRVal(), cx->re, cx->im); 914 llvm::Value* dst;
915 if (DLRValue* lr = lhs->isLRValue()) {
916 dst = lr->getLVal();
917 rhs = DtoCastComplex(rhs, lr->getLType());
900 } 918 }
901 else { 919 else {
902 DtoComplexAssign(lhs->getRVal(), rhs->getRVal()); 920 dst = lhs->getRVal();
903 } 921 }
922
923 if (DComplexValue* cx = rhs->isComplex())
924 DtoComplexSet(dst, cx->re, cx->im);
925 else
926 DtoComplexAssign(dst, rhs->getRVal());
904 } 927 }
905 else { 928 else {
906 llvm::Value* r = rhs->getRVal(); 929 llvm::Value* r = rhs->getRVal();
907 llvm::Value* l = lhs->getLVal(); 930 llvm::Value* l = lhs->getLVal();
908 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n'; 931 Logger::cout() << "assign\nlhs: " << *l << "rhs: " << *r << '\n';
909 const llvm::Type* lit = l->getType()->getContainedType(0); 932 const llvm::Type* lit = l->getType()->getContainedType(0);
910 if (r->getType() != lit) { 933 if (r->getType() != lit) { // :(
911 r = DtoBitCast(r, lit); 934 r = DtoBitCast(r, lit);
912 Logger::cout() << "really assign\nlhs: " << *l << "rhs: " << *r << '\n'; 935 Logger::cout() << "really assign\nlhs: " << *l << "rhs: " << *r << '\n';
913 } 936 }
914 gIR->ir->CreateStore(r, l); 937 gIR->ir->CreateStore(r, l);
915 } 938 }
980 } 1003 }
981 else if (totype->isintegral()) { 1004 else if (totype->isintegral()) {
982 rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb()); 1005 rval = new llvm::PtrToIntInst(val->getRVal(), tolltype, "tmp", gIR->scopebb());
983 } 1006 }
984 else { 1007 else {
1008 Logger::println("invalid cast from '%s' to '%s'", val->getType()->toChars(), to->toChars());
985 assert(0); 1009 assert(0);
986 } 1010 }
987 1011
988 return new DImValue(to, rval); 1012 return new DImValue(to, rval);
989 } 1013 }
1038 } 1062 }
1039 1063
1040 DValue* DtoCastComplex(DValue* val, Type* _to) 1064 DValue* DtoCastComplex(DValue* val, Type* _to)
1041 { 1065 {
1042 Type* to = DtoDType(_to); 1066 Type* to = DtoDType(_to);
1043 llvm::Value* v = val->getRVal(); 1067 Type* vty = val->getType();
1044 if (to->iscomplex()) { 1068 if (to->iscomplex()) {
1045 assert(0); 1069 if (vty->size() == to->size())
1070 return val;
1071
1072 llvm::Value *re, *im;
1073 DtoGetComplexParts(val, re, im);
1074 const llvm::Type* toty = DtoComplexBaseType(to);
1075
1076 if (to->size() < vty->size()) {
1077 re = gIR->ir->CreateFPTrunc(re, toty, "tmp");
1078 im = gIR->ir->CreateFPTrunc(im, toty, "tmp");
1079 }
1080 else if (to->size() > vty->size()) {
1081 re = gIR->ir->CreateFPExt(re, toty, "tmp");
1082 im = gIR->ir->CreateFPExt(im, toty, "tmp");
1083 }
1084 else {
1085 return val;
1086 }
1087
1088 if (val->isComplex())
1089 return new DComplexValue(_to, re, im);
1090
1091 // unfortunately at this point, the cast value can show up as the lvalue for += and similar expressions.
1092 // so we need to give it storage, or fix the system that handles this stuff (DLRValue)
1093 llvm::Value* mem = new llvm::AllocaInst(DtoType(_to), "castcomplextmp", gIR->topallocapoint());
1094 DtoComplexSet(mem, re, im);
1095 return new DLRValue(val->getType(), val->getRVal(), _to, mem);
1046 } 1096 }
1047 else if (to->isimaginary()) { 1097 else if (to->isimaginary()) {
1048 DImValue* im = new DImValue(to, gIR->ir->CreateExtractElement(v, DtoConstUint(1), "im")); 1098 if (val->isComplex())
1099 return new DImValue(to, val->isComplex()->im);
1100 llvm::Value* v = val->getRVal();
1101 DImValue* im = new DImValue(to, DtoLoad(DtoGEPi(v,0,1,"tmp")));
1049 return DtoCastFloat(im, to); 1102 return DtoCastFloat(im, to);
1050 } 1103 }
1051 else if (to->isfloating()) { 1104 else if (to->isfloating()) {
1052 DImValue* re = new DImValue(to, gIR->ir->CreateExtractElement(v, DtoConstUint(0), "re")); 1105 if (val->isComplex())
1106 return new DImValue(to, val->isComplex()->re);
1107 llvm::Value* v = val->getRVal();
1108 DImValue* re = new DImValue(to, DtoLoad(DtoGEPi(v,0,0,"tmp")));
1053 return DtoCastFloat(re, to); 1109 return DtoCastFloat(re, to);
1054 } 1110 }
1055 else 1111 else
1056 assert(0); 1112 assert(0);
1057 } 1113 }