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) {