Refactor MethodDebugInfo (input of DWARF writer).

Do not pass CompiledMethod pointer through since it is only available
during AOT compile but not during JIT compile or at runtime. Creating
mock CompiledMethod just pass data is proving increasingly tricky, so
copy the fields that we need to MethodDebugInfo instead.

Change-Id: I820297b41e769fcac488c0ff2d2ea0492bb13ed8
diff --git a/compiler/debug/elf_debug_loc_writer.h b/compiler/debug/elf_debug_loc_writer.h
index c321b4b..2d4fff4 100644
--- a/compiler/debug/elf_debug_loc_writer.h
+++ b/compiler/debug/elf_debug_loc_writer.h
@@ -74,8 +74,8 @@
 }
 
 struct VariableLocation {
-  uint32_t low_pc;
-  uint32_t high_pc;
+  uint32_t low_pc;  // Relative to compilation unit.
+  uint32_t high_pc;  // Relative to compilation unit.
   DexRegisterLocation reg_lo;  // May be None if the location is unknown.
   DexRegisterLocation reg_hi;  // Most significant bits of 64-bit value.
 };
@@ -90,19 +90,23 @@
     const std::vector<DexRegisterMap>& dex_register_maps,
     uint16_t vreg,
     bool is64bitValue,
+    uint64_t compilation_unit_code_address,
     uint32_t dex_pc_low,
     uint32_t dex_pc_high) {
   std::vector<VariableLocation> variable_locations;
 
   // Get stack maps sorted by pc (they might not be sorted internally).
-  const CodeInfo code_info(method_info->compiled_method->GetVmapTable().data());
+  const CodeInfo code_info(method_info->code_info);
   const StackMapEncoding encoding = code_info.ExtractEncoding();
   std::map<uint32_t, uint32_t> stack_maps;  // low_pc -> stack_map_index.
   for (uint32_t s = 0; s < code_info.GetNumberOfStackMaps(); s++) {
     StackMap stack_map = code_info.GetStackMapAt(s, encoding);
     DCHECK(stack_map.IsValid());
-    const uint32_t low_pc = method_info->low_pc + stack_map.GetNativePcOffset(encoding);
-    DCHECK_LE(low_pc, method_info->high_pc);
+    const uint32_t pc_offset = stack_map.GetNativePcOffset(encoding);
+    DCHECK_LE(pc_offset, method_info->code_size);
+    DCHECK_LE(compilation_unit_code_address, method_info->code_address);
+    const uint32_t low_pc = dchecked_integral_cast<uint32_t>(
+        method_info->code_address + pc_offset - compilation_unit_code_address);
     stack_maps.emplace(low_pc, s);
   }
 
@@ -113,8 +117,9 @@
     const StackMap& stack_map = code_info.GetStackMapAt(stack_map_index, encoding);
     auto next_it = it;
     next_it++;
-    const uint32_t high_pc = next_it != stack_maps.end() ? next_it->first
-                                                         : method_info->high_pc;
+    const uint32_t high_pc = next_it != stack_maps.end()
+      ? next_it->first
+      : method_info->code_address + method_info->code_size - compilation_unit_code_address;
     DCHECK_LE(low_pc, high_pc);
     if (low_pc == high_pc) {
       continue;  // Ignore if the address range is empty.
@@ -165,7 +170,7 @@
                                const std::vector<DexRegisterMap>& dex_register_maps,
                                uint16_t vreg,
                                bool is64bitValue,
-                               uint32_t compilation_unit_low_pc,
+                               uint64_t compilation_unit_code_address,
                                uint32_t dex_pc_low,
                                uint32_t dex_pc_high,
                                InstructionSet isa,
@@ -173,7 +178,7 @@
                                std::vector<uint8_t>* debug_loc_buffer,
                                std::vector<uint8_t>* debug_ranges_buffer) {
   using Kind = DexRegisterLocation::Kind;
-  if (!method_info->IsFromOptimizingCompiler()) {
+  if (method_info->code_info == nullptr || dex_register_maps.empty()) {
     return;
   }
 
@@ -182,6 +187,7 @@
       dex_register_maps,
       vreg,
       is64bitValue,
+      compilation_unit_code_address,
       dex_pc_low,
       dex_pc_high);
 
@@ -202,9 +208,8 @@
       const Kind kind = reg_loc.GetKind();
       const int32_t value = reg_loc.GetValue();
       if (kind == Kind::kInStack) {
-        const size_t frame_size = method_info->compiled_method->GetFrameSizeInBytes();
         // The stack offset is relative to SP. Make it relative to CFA.
-        expr.WriteOpFbreg(value - frame_size);
+        expr.WriteOpFbreg(value - method_info->frame_size_in_bytes);
         if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
             reg_hi.GetValue() == value + 4) {
           break;  // the high word is correctly implied by the low word.
@@ -249,11 +254,11 @@
 
     if (expr.size() > 0) {
       if (is64bit) {
-        debug_loc.PushUint64(variable_location.low_pc - compilation_unit_low_pc);
-        debug_loc.PushUint64(variable_location.high_pc - compilation_unit_low_pc);
+        debug_loc.PushUint64(variable_location.low_pc);
+        debug_loc.PushUint64(variable_location.high_pc);
       } else {
-        debug_loc.PushUint32(variable_location.low_pc - compilation_unit_low_pc);
-        debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
+        debug_loc.PushUint32(variable_location.low_pc);
+        debug_loc.PushUint32(variable_location.high_pc);
       }
       // Write the expression.
       debug_loc.PushUint16(expr.size());
@@ -283,11 +288,11 @@
       high_pc = variable_locations[++i].high_pc;
     }
     if (is64bit) {
-      debug_ranges.PushUint64(low_pc - compilation_unit_low_pc);
-      debug_ranges.PushUint64(high_pc - compilation_unit_low_pc);
+      debug_ranges.PushUint64(low_pc);
+      debug_ranges.PushUint64(high_pc);
     } else {
-      debug_ranges.PushUint32(low_pc - compilation_unit_low_pc);
-      debug_ranges.PushUint32(high_pc - compilation_unit_low_pc);
+      debug_ranges.PushUint32(low_pc);
+      debug_ranges.PushUint32(high_pc);
     }
   }
   // Write end-of-list entry.