Add tables to map between callee saves and vregs

Also added function to return sp-relative offset for Dalvik
virtual registers using Method*

[Note: must be matched with corresponding libcore change to
reflect new field in Method]

Change-Id: Id739908c6232ce60763d8199bc05111e960da46e
diff --git a/src/compiler/CompilerIR.h b/src/compiler/CompilerIR.h
index 0965c14..5a38c47 100644
--- a/src/compiler/CompilerIR.h
+++ b/src/compiler/CompilerIR.h
@@ -198,6 +198,8 @@
     int assemblerRetries;
     std::vector<short> codeBuffer;
     std::vector<uint32_t> mappingTable;
+    std::vector<uint32_t> coreVmapTable;
+    std::vector<short> fpVmapTable;
     bool printMe;
     bool printMeVerbose;
     bool dumpCFG;
diff --git a/src/compiler/Frontend.cc b/src/compiler/Frontend.cc
index 6a01e36..9659903 100644
--- a/src/compiler/Frontend.cc
+++ b/src/compiler/Frontend.cc
@@ -889,7 +889,21 @@
     memcpy(mapping_table->GetData(),
            reinterpret_cast<const int32_t*>(&cUnit.mappingTable[0]),
            mapping_table->GetLength() * sizeof(cUnit.mappingTable[0]));
-    method->SetCode(managed_code, art::kThumb2, mapping_table);
+    // Add a marker to take place of lr
+    cUnit.coreVmapTable.push_back(-1);
+    // Combine vmap tables - core regs, then fp regs
+    for (uint32_t i = 0; i < cUnit.fpVmapTable.size(); i++) {
+        cUnit.coreVmapTable.push_back(cUnit.fpVmapTable[i]);
+    }
+    DCHECK(cUnit.coreVmapTable.size() == (uint32_t)
+        (__builtin_popcount(cUnit.coreSpillMask) +
+         __builtin_popcount(cUnit.fpSpillMask)));
+    art::ShortArray* vmap_table =
+        art::ShortArray::Alloc(cUnit.coreVmapTable.size());
+    memcpy(vmap_table->GetData(),
+           reinterpret_cast<const int16_t*>(&cUnit.coreVmapTable[0]),
+           vmap_table->GetLength() * sizeof(cUnit.coreVmapTable[0]));
+    method->SetCode(managed_code, art::kThumb2, mapping_table, vmap_table);
     method->SetFrameSizeInBytes(cUnit.frameSize);
     method->SetReturnPcOffsetInBytes(cUnit.frameSize - sizeof(intptr_t));
     method->SetCoreSpillMask(cUnit.coreSpillMask);
diff --git a/src/compiler/codegen/RallocUtil.cc b/src/compiler/codegen/RallocUtil.cc
index 8b84500..8b9ae13 100644
--- a/src/compiler/codegen/RallocUtil.cc
+++ b/src/compiler/codegen/RallocUtil.cc
@@ -220,6 +220,7 @@
             res = coreRegs[i].reg;
             coreRegs[i].inUse = true;
             cUnit->coreSpillMask |= (1 << res);
+            cUnit->coreVmapTable.push_back(sReg);
             cUnit->numSpills++;
             cUnit->regLocation[sReg].location = kLocPhysReg;
             cUnit->regLocation[sReg].lowReg = res;
@@ -245,6 +246,7 @@
             res = FPRegs[i].reg;
             FPRegs[i].inUse = true;
             cUnit->fpSpillMask |= (1 << (res & FP_REG_MASK));
+            cUnit->fpVmapTable.push_back(sReg);
             cUnit->numSpills++;
             cUnit->numFPSpills++;
             cUnit->regLocation[sReg].fpLocation = kLocPhysReg;
diff --git a/src/compiler/codegen/arm/ArchUtility.cc b/src/compiler/codegen/arm/ArchUtility.cc
index 45e1b19..350f38c 100644
--- a/src/compiler/codegen/arm/ArchUtility.cc
+++ b/src/compiler/codegen/arm/ArchUtility.cc
@@ -450,13 +450,15 @@
 
     }
 
-    int linebreak = 0;
     std::string signature = method->GetSignature()->ToModifiedUtf8();
     std::string name = method->GetName()->ToModifiedUtf8();
     std::string descriptor = method->GetDeclaringClass()->GetDescriptor()->
         ToModifiedUtf8();
 
     char buf[256];
+#if 0
+    int linebreak = 0;
+    //TODO: delete when we're sure it's no longer necessary
     LOG(INFO) << "*/";
     sprintf(buf,"\n    u1 %s%s_%s_code[] = {", descriptor.c_str(),
             name.c_str(), signature.c_str());
@@ -477,6 +479,7 @@
         LOG(INFO) << buf;
     }
     LOG(INFO) << "    };\n\n";
+#endif
 
     // Dump mapping table
     if (cUnit->mappingTable.size() > 0) {
@@ -495,4 +498,6 @@
         }
         LOG(INFO) <<"    };\n\n";
     }
+
+    // Dump vmap table
 }
diff --git a/src/compiler/codegen/arm/ArmRallocUtil.cc b/src/compiler/codegen/arm/ArmRallocUtil.cc
index 030131f..0908c6d 100644
--- a/src/compiler/codegen/arm/ArmRallocUtil.cc
+++ b/src/compiler/codegen/arm/ArmRallocUtil.cc
@@ -260,6 +260,21 @@
             cUnit->insOffset + ((reg - cUnit->numRegs) << 2);
 }
 
+/* Return sp-relative offset in bytes using Method* */
+extern int oatVRegOffsetFromMethod(Method* method, int reg)
+{
+    int numIns = method->NumIns();
+    int numRegs = method->NumRegisters() - numIns;
+    int numOuts = method->NumOuts();
+    int numSpills = __builtin_popcount(method->GetCoreSpillMask()) +
+                    __builtin_popcount(method->GetFpSpillMask());
+    int numPadding = (STACK_ALIGN_WORDS -
+        (numSpills + numRegs + numOuts + 2)) & (STACK_ALIGN_WORDS-1);
+    int regsOffset = (numOuts + numPadding + 1) * 4;
+    int insOffset = method->GetFrameSizeInBytes() + 4;
+    return (reg < numRegs) ? regsOffset + (reg << 2) :
+           insOffset + ((reg - numRegs) << 2);
+}
 
 /* Clobber all regs that might be used by an external C call */
 extern void oatClobberCallRegs(CompilationUnit *cUnit)