changeset 979:523bf4f166bc

Fix some assembler issues: The assembler was miscompiling "add" (specifically, the "add reg/mem, imm" variations). The change that caused this seems to have been made because without it, some "add"s didn't compile at all. This patch reverts the previous change, and makes sure assembler operands are remapped correctly even though the input operands auto-generated due to updating operations aren't explicitly used.
author Frits van Bommel <fvbommel wxs.nl>
date Wed, 18 Feb 2009 21:46:14 +0100
parents 6a32d2e18175
children ae710cba0884
files gen/asm-x86-64.h gen/asmstmt.cpp tests/mini/asm9.d
diffstat 3 files changed, 42 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/gen/asm-x86-64.h	Wed Feb 18 03:38:12 2009 +0100
+++ b/gen/asm-x86-64.h	Wed Feb 18 21:46:14 2009 +0100
@@ -630,8 +630,7 @@
     static AsmOpEnt opData[] =
     {
         { "adc",    Op_UpdSrcF },
-
-        { "add",    Op_DstSrcNT }, //Op_UpdSrcF },
+        { "add",    Op_UpdSrcF },
         { "addpd",  Op_DstSrcSSE },
         { "addps",  Op_DstSrcSSE },
         { "addq",    Op_DstSrcSSE },
--- a/gen/asmstmt.cpp	Wed Feb 18 03:38:12 2009 +0100
+++ b/gen/asmstmt.cpp	Wed Feb 18 21:46:14 2009 +0100
@@ -396,8 +396,9 @@
             // Add input operand with same value, with original as "matching output".
             std::ostringstream ss;
             ss << '*' << (n + asmblock->outputcount);
-            input_constraints.push_front(ss.str());
-            input_values.push_front(output_values[n]);
+            // Must be at the back; unused operands before used ones screw up numbering.
+            input_constraints.push_back(ss.str());
+            input_values.push_back(output_values[n]);
         }
         llvmOutConstraints += *i;
         llvmOutConstraints += ",";
@@ -440,7 +441,7 @@
 }
 
 // rewrite argument indices to the block scope indices
-static void remap_outargs(std::string& insnt, size_t nargs, size_t& idx)
+static void remap_outargs(std::string& insnt, size_t nargs, size_t idx)
 {
     static const std::string digits[10] =
     {
@@ -465,7 +466,7 @@
 }
 
 // rewrite argument indices to the block scope indices
-static void remap_inargs(std::string& insnt, size_t nargs, size_t& idx)
+static void remap_inargs(std::string& insnt, size_t nargs, size_t idx)
 {
     static const std::string digits[10] =
     {
@@ -644,6 +645,7 @@
             out_c += a->out_c;
         }
         remap_outargs(a->code, onn+a->in.size(), asmIdx);
+        asmIdx += onn;
     }
 
     Logger::println("do inputs");
@@ -662,6 +664,7 @@
             in_c += a->in_c;
         }
         remap_inargs(a->code, inn+a->out.size(), asmIdx);
+        asmIdx += inn;
         if (!code.empty())
             code += "\n\t";
         code += a->code;
@@ -707,8 +710,12 @@
     if (Logger::enabled()) {
         Logger::cout() << "Arguments:" << '\n';
         Logger::indent();
-        for (std::vector<LLValue*>::iterator b = args.begin(), i = b, e = args.end(); i != e; ++i)
-            Logger::cout() << '$' << (i - b) << " ==> " << **i;
+        for (std::vector<LLValue*>::iterator b = args.begin(), i = b, e = args.end(); i != e; ++i) {
+            std::ostream& cout = Logger::cout();
+            cout << '$' << (i - b) << " ==> " << **i;
+            if (llvm::isa<LLConstant>(*i))
+                cout << '\n';
+        }
         Logger::undent();
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/asm9.d	Wed Feb 18 21:46:14 2009 +0100
@@ -0,0 +1,28 @@
+module asm9;
+
+version(X86)            version = DoSome;
+else version(X86_64)    version = DoSome;
+
+T add(T, T t)(T a) {
+    asm {
+        add a, t;
+    }
+    return a;
+}
+
+void main() {
+    version (DoSome) {
+        assert(add!(ubyte, 20)(10) == 30);
+        assert(add!(ushort, 20_000)(10_000) == 30_000);
+        assert(add!(uint, 2_000_000)(1_000_000) == 3_000_000);
+    }
+    version(X86_64) {
+        // 64-bit immediates aren't allowed on "ADD", nor are
+        // unsigned 32-bit ones, so make the template parameter
+        // fit in a 32-bit signed int.
+        // These values were chosen so that the lower 32-bits overflow
+        // and we can see the upper half of the 64-bit input increment.
+        auto result = add!(long, 2_000_000_000)(21_000_000_000);
+        assert(result == 23_000_000_000);
+    }
+}