diff gen/tocall.cpp @ 1047:6bb04dbee21f

Some calling convention work for x86-64: - Implement x86-64 extern(C), hopefully correctly. - Tried to be a bit smarter about extern(D) while I was there. Interestingly, this code seems to be generating more efficient code than gcc and llvm-gcc in some edge cases, like returning a `{ [7 x i8] }` loaded from a stack slot from an extern(C) function. (gcc generates 7 1-byte loads, while this code generates a 4-byte, a 2-byte and a 1-byte load) I also added some changes to make sure structs being returned from functions or passed in as parameters are stored in memory where the rest of the backend seems to expect them to be. These should be removed when support for first-class aggregates improves.
author Frits van Bommel <fvbommel wxs.nl>
date Fri, 06 Mar 2009 16:00:47 +0100
parents 45af482e3832
children 32ead42679d1
line wrap: on
line diff
--- a/gen/tocall.cpp	Thu Mar 05 21:32:18 2009 +0100
+++ b/gen/tocall.cpp	Fri Mar 06 16:00:47 2009 +0100
@@ -217,6 +217,11 @@
 
 DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions* arguments)
 {
+    if (Logger::enabled()) {
+        Logger::println("DtoCallFunction()");
+    }
+    LOG_SCOPE
+
     // the callee D type
     Type* calleeType = fnval->getType();
 
@@ -386,6 +391,15 @@
 
             int j = tf->fty->reverseParams ? beg + n - i - 1 : beg + i;
 
+            // Hack around LDC assuming structs are in memory:
+            // If the function wants a struct, and the argument value is a
+            // pointer to a struct, load from it before passing it in.
+            if (argval->getType()->ty == Tstruct
+                    && isaPointer(arg) && !isaPointer(callableTy->getParamType(j))) {
+                Logger::println("Loading struct type for function argument");
+                arg = DtoLoad(arg);
+            }
+
             // parameter type mismatch, this is hard to get rid of
             if (arg->getType() != callableTy->getParamType(j))
             {
@@ -468,24 +482,24 @@
     // get return value
     LLValue* retllval = (retinptr) ? args[0] : call.getInstruction();
 
-    if (tf->linkage == LINKintrinsic)
-    {
-        // Ignore ABI for intrinsics
-        Type* rettype = tf->next;
-        if (rettype->ty == Tstruct) {
-            // LDC assumes structs are in memory, so put it there.
-            LLValue* mem = DtoAlloca(retllval->getType());
-            DtoStore(retllval, mem);
-            retllval = mem;
-        }
-    }
-    else if (!retinptr)
+    // Ignore ABI for intrinsics
+    if (tf->linkage != LINKintrinsic && !retinptr)
     {
         // do abi specific return value fixups
         DImValue dretval(tf->next, retllval);
         retllval = tf->fty->getRet(tf->next, &dretval);
     }
 
+    // Hack around LDC assuming structs are in memory:
+    // If the function returns a struct, and the return value is not a
+    // pointer to a struct, store it to a stack slot before continuing.
+    if (tf->next->ty == Tstruct && !isaPointer(retllval)) {
+        Logger::println("Storing return value to stack slot");
+        LLValue* mem = DtoAlloca(retllval->getType());
+        DtoStore(retllval, mem);
+        retllval = mem;
+    }
+
     // repaint the type if necessary
     if (resulttype)
     {