diff gen/complex.cpp @ 244:a95056b3c996 trunk

[svn r261] Fixed debug info for integer and floating local variables, can now be inspected in GDB. Did a lot of smaller cleans up here and there. Replaced more llvm::Foo with LLFoo for common stuff. Split up tollvm.cpp.
author lindquist
date Mon, 09 Jun 2008 09:37:08 +0200
parents 0806379a5eca
children 4aa2b6753059
line wrap: on
line diff
--- a/gen/complex.cpp	Mon Jun 09 03:02:14 2008 +0200
+++ b/gen/complex.cpp	Mon Jun 09 09:37:08 2008 +0200
@@ -5,6 +5,7 @@
 
 #include "gen/complex.h"
 #include "gen/tollvm.h"
+#include "gen/llvmhelpers.h"
 #include "gen/irstate.h"
 #include "gen/dvalue.h"
 
@@ -307,3 +308,56 @@
     LLValue* b2 = new llvm::FCmpInst(cmpop, b, d, "tmp", gIR->scopebb());
     return gIR->ir->CreateAnd(b1,b2,"tmp");
 }
+
+//////////////////////////////////////////////////////////////////////////////////////////
+
+DValue* DtoCastComplex(DValue* val, Type* _to)
+{
+    Type* to = DtoDType(_to);
+    Type* vty = val->getType();
+    if (to->iscomplex()) {
+        if (vty->size() == to->size())
+            return val;
+
+        llvm::Value *re, *im;
+        DtoGetComplexParts(val, re, im);
+        const LLType* toty = DtoComplexBaseType(to);
+
+        if (to->size() < vty->size()) {
+            re = gIR->ir->CreateFPTrunc(re, toty, "tmp");
+            im = gIR->ir->CreateFPTrunc(im, toty, "tmp");
+        }
+        else if (to->size() > vty->size()) {
+            re = gIR->ir->CreateFPExt(re, toty, "tmp");
+            im = gIR->ir->CreateFPExt(im, toty, "tmp");
+        }
+        else {
+            return val;
+        }
+
+        if (val->isComplex())
+            return new DComplexValue(_to, re, im);
+
+        // unfortunately at this point, the cast value can show up as the lvalue for += and similar expressions.
+        // so we need to give it storage, or fix the system that handles this stuff (DLRValue)
+        LLValue* mem = new llvm::AllocaInst(DtoType(_to), "castcomplextmp", gIR->topallocapoint());
+        DtoComplexSet(mem, re, im);
+        return new DLRValue(val->getType(), val->getRVal(), _to, mem);
+    }
+    else if (to->isimaginary()) {
+        if (val->isComplex())
+            return new DImValue(to, val->isComplex()->im);
+        LLValue* v = val->getRVal();
+        DImValue* im = new DImValue(to, DtoLoad(DtoGEPi(v,0,1,"tmp")));
+        return DtoCastFloat(im, to);
+    }
+    else if (to->isfloating()) {
+        if (val->isComplex())
+            return new DImValue(to, val->isComplex()->re);
+        LLValue* v = val->getRVal();
+        DImValue* re = new DImValue(to, DtoLoad(DtoGEPi(v,0,0,"tmp")));
+        return DtoCastFloat(re, to);
+    }
+    else
+    assert(0);
+}
\ No newline at end of file