Create helper class for DWARF expressions.

This ensures we generate valid DWARF opcodes and
it slightly simplifies the debug info writer.

Change-Id: I2ef8a9222c9a129c58f500741acd209b67e2ecff
diff --git a/compiler/dwarf/debug_frame_opcode_writer.h b/compiler/dwarf/debug_frame_opcode_writer.h
index 5a99641..f74f37c 100644
--- a/compiler/dwarf/debug_frame_opcode_writer.h
+++ b/compiler/dwarf/debug_frame_opcode_writer.h
@@ -248,7 +248,7 @@
     }
   }
 
-  void ALWAYS_INLINE DefCFAExpression(void * expr, int expr_size) {
+  void ALWAYS_INLINE DefCFAExpression(uint8_t* expr, int expr_size) {
     if (UNLIKELY(enabled_)) {
       ImplicitlyAdvancePC();
       uses_dwarf3_features_ = true;
@@ -258,7 +258,7 @@
     }
   }
 
-  void ALWAYS_INLINE Expression(Reg reg, void * expr, int expr_size) {
+  void ALWAYS_INLINE Expression(Reg reg, uint8_t* expr, int expr_size) {
     if (UNLIKELY(enabled_)) {
       ImplicitlyAdvancePC();
       uses_dwarf3_features_ = true;
@@ -269,7 +269,7 @@
     }
   }
 
-  void ALWAYS_INLINE ValExpression(Reg reg, void * expr, int expr_size) {
+  void ALWAYS_INLINE ValExpression(Reg reg, uint8_t* expr, int expr_size) {
     if (UNLIKELY(enabled_)) {
       ImplicitlyAdvancePC();
       uses_dwarf3_features_ = true;
diff --git a/compiler/dwarf/debug_info_entry_writer.h b/compiler/dwarf/debug_info_entry_writer.h
index a551e4b..e5bbed3 100644
--- a/compiler/dwarf/debug_info_entry_writer.h
+++ b/compiler/dwarf/debug_info_entry_writer.h
@@ -22,6 +22,7 @@
 
 #include "base/casts.h"
 #include "dwarf/dwarf_constants.h"
+#include "dwarf/expression.h"
 #include "dwarf/writer.h"
 #include "leb128.h"
 
@@ -106,16 +107,16 @@
     }
   }
 
-  void WriteBlock(Attribute attrib, const void* ptr, size_t num_bytes) {
+  void WriteBlock(Attribute attrib, const uint8_t* ptr, size_t num_bytes) {
     AddAbbrevAttribute(attrib, DW_FORM_block);
     this->PushUleb128(num_bytes);
     this->PushData(ptr, num_bytes);
   }
 
-  void WriteExprLoc(Attribute attrib, const void* ptr, size_t num_bytes) {
+  void WriteExprLoc(Attribute attrib, const Expression& expr) {
     AddAbbrevAttribute(attrib, DW_FORM_exprloc);
-    this->PushUleb128(dchecked_integral_cast<uint32_t>(num_bytes));
-    this->PushData(ptr, num_bytes);
+    this->PushUleb128(dchecked_integral_cast<uint32_t>(expr.size()));
+    this->PushData(expr.data());
   }
 
   void WriteData1(Attribute attrib, uint8_t value) {
diff --git a/compiler/dwarf/expression.h b/compiler/dwarf/expression.h
new file mode 100644
index 0000000..1503d03
--- /dev/null
+++ b/compiler/dwarf/expression.h
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_COMPILER_DWARF_EXPRESSION_H_
+#define ART_COMPILER_DWARF_EXPRESSION_H_
+
+#include <cstddef>
+#include <cstdint>
+
+#include "dwarf/dwarf_constants.h"
+#include "dwarf/writer.h"
+
+namespace art {
+namespace dwarf {
+
+// Writer for DWARF expressions which are used in .debug_info and .debug_loc sections.
+// See the DWARF specification for the precise meaning of the opcodes.
+// If multiple equivalent encodings are possible, it will choose the most compact one.
+// The writer is not exhaustive - it only implements opcodes we have needed so far.
+class Expression : private Writer<> {
+ public:
+  using Writer<>::data;
+  using Writer<>::size;
+
+  // Push signed integer on the stack.
+  void WriteOpConsts(int32_t value) {
+    if (0 <= value && value < 32) {
+      PushUint8(DW_OP_lit0 + value);
+    } else {
+      PushUint8(DW_OP_consts);
+      PushSleb128(value);
+    }
+  }
+
+  // Push unsigned integer on the stack.
+  void WriteOpConstu(uint32_t value) {
+    if (value < 32) {
+      PushUint8(DW_OP_lit0 + value);
+    } else {
+      PushUint8(DW_OP_constu);
+      PushUleb128(value);
+    }
+  }
+
+  // Variable is stored in given register.
+  void WriteOpReg(uint32_t dwarf_reg_num) {
+    if (dwarf_reg_num < 32) {
+      PushUint8(DW_OP_reg0 + dwarf_reg_num);
+    } else {
+      PushUint8(DW_OP_regx);
+      PushUleb128(dwarf_reg_num);
+    }
+  }
+
+  // Variable is stored on stack.  Also see DW_AT_frame_base.
+  void WriteOpFbreg(int32_t stack_offset) {
+    PushUint8(DW_OP_fbreg);
+    PushSleb128(stack_offset);
+  }
+
+  // The variable is stored in multiple locations (pieces).
+  void WriteOpPiece(uint32_t num_bytes) {
+    PushUint8(DW_OP_piece);
+    PushUleb128(num_bytes);
+  }
+
+  // Loads 32-bit or 64-bit value depending on architecture.
+  void WriteOpDeref() { PushUint8(DW_OP_deref); }
+
+  // Loads value of given byte size.
+  void WriteOpDerefSize(uint8_t num_bytes) {
+    PushUint8(DW_OP_deref_size);
+    PushUint8(num_bytes);
+  }
+
+  // Pop two values and push their sum.
+  void WriteOpPlus() { PushUint8(DW_OP_plus); }
+
+  // Add constant value to value on top of stack.
+  void WriteOpPlusUconst(uint32_t offset) {
+    PushUint8(DW_OP_plus_uconst);
+    PushUleb128(offset);
+  }
+
+  // Negate top of stack.
+  void WriteOpNeg() { PushUint8(DW_OP_neg); }
+
+  // Pop two values and push their bitwise-AND.
+  void WriteOpAnd() { PushUint8(DW_OP_and); }
+
+  // Push stack base pointer as determined from .debug_frame.
+  void WriteOpCallFrameCfa() { PushUint8(DW_OP_call_frame_cfa); }
+
+  // Push address of the variable we are working with.
+  void WriteOpPushObjectAddress() { PushUint8(DW_OP_push_object_address); }
+
+  // Return the top stack as the value of the variable.
+  // Otherwise, the top of stack is the variable's location.
+  void WriteOpStackValue() { PushUint8(DW_OP_stack_value); }
+
+  explicit Expression(std::vector<uint8_t>* buffer) : Writer<>(buffer) {
+    buffer->clear();
+  }
+};
+}  // namespace dwarf
+}  // namespace art
+
+#endif  // ART_COMPILER_DWARF_EXPRESSION_H_
diff --git a/compiler/dwarf/headers.h b/compiler/dwarf/headers.h
index f76f76f..137c566 100644
--- a/compiler/dwarf/headers.h
+++ b/compiler/dwarf/headers.h
@@ -70,7 +70,7 @@
       writer.PushUint8(DW_EH_PE_absptr | DW_EH_PE_udata4);  // R: Pointer encoding.
     }
   }
-  writer.PushData(*opcodes.data());
+  writer.PushData(opcodes.data());
   writer.Pad(is64bit ? 8 : 4);
   writer.UpdateUint32(cie_header_start_, writer.data()->size() - cie_header_start_ - 4);
 }
@@ -117,7 +117,7 @@
     writer.PushUint32(code_size);
   }
   writer.PushUleb128(0);  // Augmentation data size.
-  writer.PushData(opcodes);
+  writer.PushData(opcodes.data(), opcodes.size());
   writer.Pad(is64bit ? 8 : 4);
   writer.UpdateUint32(fde_header_start, writer.data()->size() - fde_header_start - 4);
 }
@@ -139,7 +139,7 @@
   writer.PushUint8(entries.Is64bit() ? 8 : 4);
   size_t entries_offset = writer.data()->size();
   DCHECK_EQ(entries_offset, DebugInfoEntryWriter<Vector>::kCompilationUnitHeaderSize);
-  writer.PushData(*entries.data());
+  writer.PushData(entries.data());
   writer.UpdateUint32(start, writer.data()->size() - start - 4);
   // Copy patch locations and make them relative to .debug_info section.
   for (uintptr_t patch_location : entries.GetPatchLocations()) {
@@ -193,7 +193,7 @@
   writer.PushUint8(0);  // Terminate file list.
   writer.UpdateUint32(header_length_pos, writer.data()->size() - header_length_pos - 4);
   size_t opcodes_offset = writer.data()->size();
-  writer.PushData(*opcodes.data());
+  writer.PushData(opcodes.data());
   writer.UpdateUint32(header_start, writer.data()->size() - header_start - 4);
   // Copy patch locations and make them relative to .debug_line section.
   for (uintptr_t patch_location : opcodes.GetPatchLocations()) {
diff --git a/compiler/dwarf/writer.h b/compiler/dwarf/writer.h
index d2add7f..74acf07 100644
--- a/compiler/dwarf/writer.h
+++ b/compiler/dwarf/writer.h
@@ -114,16 +114,16 @@
     data_->insert(data_->end(), value, value + strlen(value) + 1);
   }
 
-  void PushData(const void* ptr, size_t num_bytes) {
-    const char* p = reinterpret_cast<const char*>(ptr);
-    data_->insert(data_->end(), p, p + num_bytes);
+  void PushData(const uint8_t* ptr, size_t num_bytes) {
+    data_->insert(data_->end(), ptr, ptr + num_bytes);
   }
 
-  template<typename Vector2>
-  void PushData(const Vector2& buffer) {
-    static_assert(std::is_same<typename std::add_const<typename Vector::value_type>::type,
-                               const uint8_t>::value, "Invalid value type");
-    data_->insert(data_->end(), buffer.begin(), buffer.end());
+  void PushData(const char* ptr, size_t num_bytes) {
+    data_->insert(data_->end(), ptr, ptr + num_bytes);
+  }
+
+  void PushData(const Vector* buffer) {
+    data_->insert(data_->end(), buffer->begin(), buffer->end());
   }
 
   void UpdateUint32(size_t offset, uint32_t value) {
diff --git a/compiler/elf_writer_debug.cc b/compiler/elf_writer_debug.cc
index 3e64762..99d2b84 100644
--- a/compiler/elf_writer_debug.cc
+++ b/compiler/elf_writer_debug.cc
@@ -25,6 +25,7 @@
 #include "dex_file-inl.h"
 #include "driver/compiler_driver.h"
 #include "dwarf/dedup_vector.h"
+#include "dwarf/expression.h"
 #include "dwarf/headers.h"
 #include "dwarf/method_debug_info.h"
 #include "dwarf/register.h"
@@ -498,8 +499,10 @@
         WriteName(dex->GetMethodName(dex_method));
         info_.WriteAddr(DW_AT_low_pc, text_address + mi->low_pc_);
         info_.WriteUdata(DW_AT_high_pc, dchecked_integral_cast<uint32_t>(mi->high_pc_-mi->low_pc_));
-        uint8_t frame_base[] = { DW_OP_call_frame_cfa };
-        info_.WriteExprLoc(DW_AT_frame_base, &frame_base, sizeof(frame_base));
+        std::vector<uint8_t> expr_buffer;
+        Expression expr(&expr_buffer);
+        expr.WriteOpCallFrameCfa();
+        info_.WriteExprLoc(DW_AT_frame_base, expr);
         WriteLazyType(dex->GetReturnTypeDescriptor(dex_proto));
 
         // Write parameters. DecodeDebugLocalInfo returns them as well, but it does not
@@ -588,6 +591,7 @@
       info_.WriteStrp(DW_AT_producer, owner_->WriteString("Android dex2oat"));
       info_.WriteData1(DW_AT_language, DW_LANG_Java);
 
+      std::vector<uint8_t> count_expr_buffer;
       for (mirror::Class* type : types) {
         if (type->IsPrimitive()) {
           // For primitive types the definition and the declaration is the same.
@@ -605,15 +609,11 @@
           WriteLazyType(element_type->GetDescriptor(&descriptor_string));
           info_.WriteUdata(DW_AT_data_member_location, data_offset);
           info_.StartTag(DW_TAG_subrange_type);
-          DCHECK_LT(length_offset, 32u);
-          uint8_t count[] = {
-            DW_OP_push_object_address,
-            static_cast<uint8_t>(DW_OP_lit0 + length_offset),
-            DW_OP_plus,
-            DW_OP_deref_size,
-            4  // Array length is always 32-bit wide.
-          };
-          info_.WriteExprLoc(DW_AT_count, &count, sizeof(count));
+          Expression count_expr(&count_expr_buffer);
+          count_expr.WriteOpPushObjectAddress();
+          count_expr.WriteOpPlusUconst(length_offset);
+          count_expr.WriteOpDerefSize(4);  // Array length is always 32-bit wide.
+          info_.WriteExprLoc(DW_AT_count, count_expr);
           info_.EndTag();  // DW_TAG_subrange_type.
           info_.EndTag();  // DW_TAG_array_type.
         } else {
@@ -713,12 +713,12 @@
       // Write .debug_loc entries.
       const InstructionSet isa = owner_->builder_->GetIsa();
       const bool is64bit = Is64BitInstructionSet(isa);
+      std::vector<uint8_t> expr_buffer;
       for (const VariableLocation& variable_location : variable_locations) {
         // Translate dex register location to DWARF expression.
         // Note that 64-bit value might be split to two distinct locations.
         // (for example, two 32-bit machine registers, or even stack and register)
-        uint8_t buffer[64];
-        uint8_t* pos = buffer;
+        Expression expr(&expr_buffer);
         DexRegisterLocation reg_lo = variable_location.reg_lo;
         DexRegisterLocation reg_hi = variable_location.reg_hi;
         for (int piece = 0; piece < (is64bitValue ? 2 : 1); piece++) {
@@ -727,15 +727,14 @@
           const int32_t value = reg_loc.GetValue();
           if (kind == Kind::kInStack) {
             const size_t frame_size = method_info->compiled_method_->GetFrameSizeInBytes();
-            *(pos++) = DW_OP_fbreg;
             // The stack offset is relative to SP. Make it relative to CFA.
-            pos = EncodeSignedLeb128(pos, value - frame_size);
+            expr.WriteOpFbreg(value - frame_size);
             if (piece == 0 && reg_hi.GetKind() == Kind::kInStack &&
                 reg_hi.GetValue() == value + 4) {
               break;  // the high word is correctly implied by the low word.
             }
           } else if (kind == Kind::kInRegister) {
-            pos = WriteOpReg(pos, GetDwarfCoreReg(isa, value).num());
+            expr.WriteOpReg(GetDwarfCoreReg(isa, value).num());
             if (piece == 0 && reg_hi.GetKind() == Kind::kInRegisterHigh &&
                 reg_hi.GetValue() == value) {
               break;  // the high word is correctly implied by the low word.
@@ -745,22 +744,21 @@
                 piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegister &&
                 reg_hi.GetValue() == value + 1 && value % 2 == 0) {
               // Translate S register pair to D register (e.g. S4+S5 to D2).
-              pos = WriteOpReg(pos, Reg::ArmDp(value / 2).num());
+              expr.WriteOpReg(Reg::ArmDp(value / 2).num());
               break;
             }
             if (isa == kMips || isa == kMips64) {
               // TODO: Find what the DWARF floating point register numbers are on MIPS.
               break;
             }
-            pos = WriteOpReg(pos, GetDwarfFpReg(isa, value).num());
+            expr.WriteOpReg(GetDwarfFpReg(isa, value).num());
             if (piece == 0 && reg_hi.GetKind() == Kind::kInFpuRegisterHigh &&
                 reg_hi.GetValue() == reg_lo.GetValue()) {
               break;  // the high word is correctly implied by the low word.
             }
           } else if (kind == Kind::kConstant) {
-            *(pos++) = DW_OP_consts;
-            pos = EncodeSignedLeb128(pos, value);
-            *(pos++) = DW_OP_stack_value;
+            expr.WriteOpConsts(value);
+            expr.WriteOpStackValue();
           } else if (kind == Kind::kNone) {
             break;
           } else {
@@ -774,14 +772,11 @@
           if (is64bitValue) {
             // Write the marker which is needed by split 64-bit values.
             // This code is skipped by the special cases.
-            *(pos++) = DW_OP_piece;
-            pos = EncodeUnsignedLeb128(pos, 4);
+            expr.WriteOpPiece(4);
           }
         }
 
-        // Check that the buffer is large enough; keep half of it empty for safety.
-        DCHECK_LE(static_cast<size_t>(pos - buffer), sizeof(buffer) / 2);
-        if (pos > buffer) {
+        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);
@@ -790,8 +785,8 @@
             debug_loc.PushUint32(variable_location.high_pc - compilation_unit_low_pc);
           }
           // Write the expression.
-          debug_loc.PushUint16(pos - buffer);
-          debug_loc.PushData(buffer, pos - buffer);
+          debug_loc.PushUint16(expr.size());
+          debug_loc.PushData(expr.data());
         } else {
           // Do not generate .debug_loc if the location is not known.
         }
@@ -856,17 +851,6 @@
       }
     }
 
-    // Helper which writes DWARF expression referencing a register.
-    static uint8_t* WriteOpReg(uint8_t* buffer, uint32_t dwarf_reg_num) {
-      if (dwarf_reg_num < 32) {
-        *(buffer++) = DW_OP_reg0 + dwarf_reg_num;
-      } else {
-        *(buffer++) = DW_OP_regx;
-        buffer = EncodeUnsignedLeb128(buffer, dwarf_reg_num);
-      }
-      return buffer;
-    }
-
     // Convert dex type descriptor to DWARF.
     // Returns offset in the compilation unit.
     size_t WriteTypeDeclaration(const std::string& desc) {