Improve documentation of memory layouts in stack_map.h.

Also shorten NumberOfDexRegisterLocationCatalogEntries to
NumberOfLocationCatalogEntries.

Change-Id: I55f8ec2960ea67e2eb6871a417bd442d0e2810fb
diff --git a/runtime/stack_map.h b/runtime/stack_map.h
index 1acc442..0d3816b 100644
--- a/runtime/stack_map.h
+++ b/runtime/stack_map.h
@@ -23,6 +23,12 @@
 
 namespace art {
 
+#define ELEMENT_BYTE_OFFSET_AFTER(PreviousElement) \
+  k ## PreviousElement ## Offset + sizeof(PreviousElement ## Type)
+
+#define ELEMENT_BIT_OFFSET_AFTER(PreviousElement) \
+  k ## PreviousElement ## BitOffset + PreviousElement ## BitSize
+
 class VariableIndentationOutputStream;
 
 // Size of a frame slot, in bytes.  This constant is a signed value,
@@ -195,7 +201,9 @@
 /**
  * Store information on unique Dex register locations used in a method.
  * The information is of the form:
- * [DexRegisterLocation+].
+ *
+ *   [DexRegisterLocation+].
+ *
  * DexRegisterLocations are either 1- or 5-byte wide (see art::DexRegisterLocation::Kind).
  */
 class DexRegisterLocationCatalog {
@@ -432,7 +440,9 @@
 /* Information on Dex register locations for a specific PC, mapping a
  * stack map's Dex register to a location entry in a DexRegisterLocationCatalog.
  * The information is of the form:
- * [live_bit_mask, entries*]
+ *
+ *   [live_bit_mask, entries*]
+ *
  * where entries are concatenated unsigned integer values encoded on a number
  * of bits (fixed per DexRegisterMap instances of a CodeInfo object) depending
  * on the number of entries in the Dex register location catalog
@@ -757,8 +767,9 @@
  * - Knowing the values of dex registers.
  *
  * The information is of the form:
- * [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask,
- * stack_mask].
+ *
+ *   [dex_pc, native_pc_offset, dex_register_map_offset, inlining_info_offset, register_mask,
+ *   stack_mask].
  */
 class StackMap {
  public:
@@ -861,8 +872,6 @@
   static constexpr uint32_t kNoInlineInfo = -1;
 
  private:
-  // TODO: Instead of plain types such as "uint32_t", introduce
-  // typedefs (and document the memory layout of StackMap).
   static constexpr int kFixedSize = 0;
 
   // Loads `number_of_bytes` at the given `offset` and assemble a uint32_t. If `check_max` is true,
@@ -877,61 +886,74 @@
 
 /**
  * Inline information for a specific PC. The information is of the form:
- * [inlining_depth, [dex_pc, method_index, dex_register_map_offset]+]
+ *
+ *   [inlining_depth, entry+]
+ *
+ * where `entry` is of the form:
+ *
+ *   [dex_pc, method_index, dex_register_map_offset].
  */
 class InlineInfo {
  public:
+  // Memory layout: fixed contents.
+  typedef uint8_t DepthType;
+  // Memory layout: single entry contents.
+  typedef uint32_t MethodIndexType;
+  typedef uint32_t DexPcType;
+  typedef uint8_t InvokeTypeType;
+  typedef uint32_t DexRegisterMapType;
+
   explicit InlineInfo(MemoryRegion region) : region_(region) {}
 
-  uint8_t GetDepth() const {
-    return region_.LoadUnaligned<uint8_t>(kDepthOffset);
+  DepthType GetDepth() const {
+    return region_.LoadUnaligned<DepthType>(kDepthOffset);
   }
 
-  void SetDepth(uint8_t depth) {
-    region_.StoreUnaligned<uint8_t>(kDepthOffset, depth);
+  void SetDepth(DepthType depth) {
+    region_.StoreUnaligned<DepthType>(kDepthOffset, depth);
   }
 
-  uint32_t GetMethodIndexAtDepth(uint8_t depth) const {
-    return region_.LoadUnaligned<uint32_t>(
+  MethodIndexType GetMethodIndexAtDepth(DepthType depth) const {
+    return region_.LoadUnaligned<MethodIndexType>(
         kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset);
   }
 
-  void SetMethodIndexAtDepth(uint8_t depth, uint32_t index) {
-    region_.StoreUnaligned<uint32_t>(
+  void SetMethodIndexAtDepth(DepthType depth, MethodIndexType index) {
+    region_.StoreUnaligned<MethodIndexType>(
         kFixedSize + depth * SingleEntrySize() + kMethodIndexOffset, index);
   }
 
-  uint32_t GetDexPcAtDepth(uint8_t depth) const {
-    return region_.LoadUnaligned<uint32_t>(
+  DexPcType GetDexPcAtDepth(DepthType depth) const {
+    return region_.LoadUnaligned<DexPcType>(
         kFixedSize + depth * SingleEntrySize() + kDexPcOffset);
   }
 
-  void SetDexPcAtDepth(uint8_t depth, uint32_t dex_pc) {
-    region_.StoreUnaligned<uint32_t>(
+  void SetDexPcAtDepth(DepthType depth, DexPcType dex_pc) {
+    region_.StoreUnaligned<DexPcType>(
         kFixedSize + depth * SingleEntrySize() + kDexPcOffset, dex_pc);
   }
 
-  uint8_t GetInvokeTypeAtDepth(uint8_t depth) const {
-    return region_.LoadUnaligned<uint8_t>(
+  InvokeTypeType GetInvokeTypeAtDepth(DepthType depth) const {
+    return region_.LoadUnaligned<InvokeTypeType>(
         kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset);
   }
 
-  void SetInvokeTypeAtDepth(uint8_t depth, uint8_t invoke_type) {
-    region_.StoreUnaligned<uint8_t>(
+  void SetInvokeTypeAtDepth(DepthType depth, InvokeTypeType invoke_type) {
+    region_.StoreUnaligned<InvokeTypeType>(
         kFixedSize + depth * SingleEntrySize() + kInvokeTypeOffset, invoke_type);
   }
 
-  uint32_t GetDexRegisterMapOffsetAtDepth(uint8_t depth) const {
-    return region_.LoadUnaligned<uint32_t>(
+  DexRegisterMapType GetDexRegisterMapOffsetAtDepth(DepthType depth) const {
+    return region_.LoadUnaligned<DexRegisterMapType>(
         kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset);
   }
 
-  void SetDexRegisterMapOffsetAtDepth(uint8_t depth, uint32_t offset) {
-    region_.StoreUnaligned<uint32_t>(
+  void SetDexRegisterMapOffsetAtDepth(DepthType depth, DexRegisterMapType offset) {
+    region_.StoreUnaligned<DexRegisterMapType>(
         kFixedSize + depth * SingleEntrySize() + kDexRegisterMapOffset, offset);
   }
 
-  bool HasDexRegisterMapAtDepth(uint8_t depth) const {
+  bool HasDexRegisterMapAtDepth(DepthType depth) const {
     return GetDexRegisterMapOffsetAtDepth(depth) != StackMap::kNoDexRegisterMap;
   }
 
@@ -942,17 +964,16 @@
   void Dump(VariableIndentationOutputStream* vios,
             const CodeInfo& info, uint16_t* number_of_dex_registers) const;
 
+
  private:
-  // TODO: Instead of plain types such as "uint8_t", introduce
-  // typedefs (and document the memory layout of InlineInfo).
   static constexpr int kDepthOffset = 0;
-  static constexpr int kFixedSize = kDepthOffset + sizeof(uint8_t);
+  static constexpr int kFixedSize = ELEMENT_BYTE_OFFSET_AFTER(Depth);
 
   static constexpr int kMethodIndexOffset = 0;
-  static constexpr int kDexPcOffset = kMethodIndexOffset + sizeof(uint32_t);
-  static constexpr int kInvokeTypeOffset = kDexPcOffset + sizeof(uint32_t);
-  static constexpr int kDexRegisterMapOffset = kInvokeTypeOffset + sizeof(uint8_t);
-  static constexpr int kFixedEntrySize = kDexRegisterMapOffset + sizeof(uint32_t);
+  static constexpr int kDexPcOffset = ELEMENT_BYTE_OFFSET_AFTER(MethodIndex);
+  static constexpr int kInvokeTypeOffset = ELEMENT_BYTE_OFFSET_AFTER(DexPc);
+  static constexpr int kDexRegisterMapOffset = ELEMENT_BYTE_OFFSET_AFTER(InvokeType);
+  static constexpr int kFixedEntrySize = ELEMENT_BYTE_OFFSET_AFTER(DexRegisterMap);
 
   MemoryRegion region_;
 
@@ -964,11 +985,32 @@
 /**
  * Wrapper around all compiler information collected for a method.
  * The information is of the form:
- * [overall_size, number_of_location_catalog_entries, number_of_stack_maps, stack_mask_size,
- * DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*].
+ *
+ *   [overall_size, encoding_info, number_of_location_catalog_entries, number_of_stack_maps,
+ *   stack_mask_size, DexRegisterLocationCatalog+, StackMap+, DexRegisterMap+, InlineInfo*]
+ *
+ * where `encoding_info` is of the form:
+ *
+ *  [has_inline_info, inline_info_size_in_bytes, dex_register_map_size_in_bytes,
+ *  dex_pc_size_in_bytes, native_pc_size_in_bytes, register_mask_size_in_bytes].
  */
 class CodeInfo {
  public:
+  // Memory layout: fixed contents.
+  typedef uint32_t OverallSizeType;
+  typedef uint16_t EncodingInfoType;
+  typedef uint32_t NumberOfLocationCatalogEntriesType;
+  typedef uint32_t NumberOfStackMapsType;
+  typedef uint32_t StackMaskSizeType;
+
+  // Memory (bit) layout: encoding info.
+  static constexpr int HasInlineInfoBitSize = 1;
+  static constexpr int InlineInfoBitSize = kNumberOfBitForNumberOfBytesForEncoding;
+  static constexpr int DexRegisterMapBitSize = kNumberOfBitForNumberOfBytesForEncoding;
+  static constexpr int DexPcBitSize = kNumberOfBitForNumberOfBytesForEncoding;
+  static constexpr int NativePcBitSize = kNumberOfBitForNumberOfBytesForEncoding;
+  static constexpr int RegisterMaskBitSize = kNumberOfBitForNumberOfBytesForEncoding;
+
   explicit CodeInfo(MemoryRegion region) : region_(region) {}
 
   explicit CodeInfo(const void* data) {
@@ -1018,33 +1060,35 @@
     return StackMap(GetStackMaps(encoding).Subregion(i * stack_map_size, stack_map_size));
   }
 
-  uint32_t GetOverallSize() const {
-    return region_.LoadUnaligned<uint32_t>(kOverallSizeOffset);
+  OverallSizeType GetOverallSize() const {
+    return region_.LoadUnaligned<OverallSizeType>(kOverallSizeOffset);
   }
 
-  void SetOverallSize(uint32_t size) {
-    region_.StoreUnaligned<uint32_t>(kOverallSizeOffset, size);
+  void SetOverallSize(OverallSizeType size) {
+    region_.StoreUnaligned<OverallSizeType>(kOverallSizeOffset, size);
   }
 
-  uint32_t GetNumberOfDexRegisterLocationCatalogEntries() const {
-    return region_.LoadUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset);
+  NumberOfLocationCatalogEntriesType GetNumberOfLocationCatalogEntries() const {
+    return region_.LoadUnaligned<NumberOfLocationCatalogEntriesType>(
+        kNumberOfLocationCatalogEntriesOffset);
   }
 
-  void SetNumberOfDexRegisterLocationCatalogEntries(uint32_t num_entries) {
-    region_.StoreUnaligned<uint32_t>(kNumberOfDexRegisterLocationCatalogEntriesOffset, num_entries);
+  void SetNumberOfLocationCatalogEntries(NumberOfLocationCatalogEntriesType num_entries) {
+    region_.StoreUnaligned<NumberOfLocationCatalogEntriesType>(
+        kNumberOfLocationCatalogEntriesOffset, num_entries);
   }
 
   uint32_t GetDexRegisterLocationCatalogSize(const StackMapEncoding& encoding) const {
     return ComputeDexRegisterLocationCatalogSize(GetDexRegisterLocationCatalogOffset(encoding),
-                                                 GetNumberOfDexRegisterLocationCatalogEntries());
+                                                 GetNumberOfLocationCatalogEntries());
   }
 
-  size_t GetNumberOfStackMaps() const {
-    return region_.LoadUnaligned<uint32_t>(kNumberOfStackMapsOffset);
+  NumberOfStackMapsType GetNumberOfStackMaps() const {
+    return region_.LoadUnaligned<NumberOfStackMapsType>(kNumberOfStackMapsOffset);
   }
 
-  void SetNumberOfStackMaps(uint32_t number_of_stack_maps) {
-    region_.StoreUnaligned<uint32_t>(kNumberOfStackMapsOffset, number_of_stack_maps);
+  void SetNumberOfStackMaps(NumberOfStackMapsType number_of_stack_maps) {
+    region_.StoreUnaligned<NumberOfStackMapsType>(kNumberOfStackMapsOffset, number_of_stack_maps);
   }
 
   // Get the size all the stack maps of this CodeInfo object, in bytes.
@@ -1129,27 +1173,28 @@
             bool dump_stack_maps) const;
 
  private:
-  // TODO: Instead of plain types such as "uint32_t", introduce
-  // typedefs (and document the memory layout of CodeInfo).
   static constexpr int kOverallSizeOffset = 0;
-  static constexpr int kEncodingInfoOffset = kOverallSizeOffset + sizeof(uint32_t);
-  static constexpr int kNumberOfDexRegisterLocationCatalogEntriesOffset =
-      kEncodingInfoOffset + sizeof(uint16_t);
+  static constexpr int kEncodingInfoOffset = ELEMENT_BYTE_OFFSET_AFTER(OverallSize);
+  static constexpr int kNumberOfLocationCatalogEntriesOffset =
+      ELEMENT_BYTE_OFFSET_AFTER(EncodingInfo);
   static constexpr int kNumberOfStackMapsOffset =
-      kNumberOfDexRegisterLocationCatalogEntriesOffset + sizeof(uint32_t);
-  static constexpr int kStackMaskSizeOffset = kNumberOfStackMapsOffset + sizeof(uint32_t);
-  static constexpr int kFixedSize = kStackMaskSizeOffset + sizeof(uint32_t);
+      ELEMENT_BYTE_OFFSET_AFTER(NumberOfLocationCatalogEntries);
+  static constexpr int kStackMaskSizeOffset = ELEMENT_BYTE_OFFSET_AFTER(NumberOfStackMaps);
+  static constexpr int kFixedSize = ELEMENT_BYTE_OFFSET_AFTER(StackMaskSize);
 
-  static constexpr int kHasInlineInfoBitOffset = (kEncodingInfoOffset * kBitsPerByte);
-  static constexpr int kInlineInfoBitOffset = kHasInlineInfoBitOffset + 1;
-  static constexpr int kDexRegisterMapBitOffset =
-      kInlineInfoBitOffset + kNumberOfBitForNumberOfBytesForEncoding;
-  static constexpr int kDexPcBitOffset =
-      kDexRegisterMapBitOffset + kNumberOfBitForNumberOfBytesForEncoding;
-  static constexpr int kNativePcBitOffset =
-      kDexPcBitOffset + kNumberOfBitForNumberOfBytesForEncoding;
-  static constexpr int kRegisterMaskBitOffset =
-      kNativePcBitOffset + kNumberOfBitForNumberOfBytesForEncoding;
+  static constexpr int kHasInlineInfoBitOffset = kEncodingInfoOffset * kBitsPerByte;
+  static constexpr int kInlineInfoBitOffset = ELEMENT_BIT_OFFSET_AFTER(HasInlineInfo);
+  static constexpr int kDexRegisterMapBitOffset = ELEMENT_BIT_OFFSET_AFTER(InlineInfo);
+  static constexpr int kDexPcBitOffset = ELEMENT_BIT_OFFSET_AFTER(DexRegisterMap);
+  static constexpr int kNativePcBitOffset = ELEMENT_BIT_OFFSET_AFTER(DexPc);
+  static constexpr int kRegisterMaskBitOffset = ELEMENT_BIT_OFFSET_AFTER(NativePc);
+
+  static constexpr int kEncodingInfoPastTheEndBitOffset = ELEMENT_BIT_OFFSET_AFTER(RegisterMask);
+  static constexpr int kEncodingInfoOverallBitSize =
+      kEncodingInfoPastTheEndBitOffset - kHasInlineInfoBitOffset;
+
+  static_assert(kEncodingInfoOverallBitSize <= (sizeof(EncodingInfoType) * kBitsPerByte),
+                "art::CodeInfo::EncodingInfoType is too short to hold all encoding info elements.");
 
   MemoryRegion GetStackMaps(const StackMapEncoding& encoding) const {
     return region_.size() == 0
@@ -1172,7 +1217,7 @@
     size_t number_of_live_dex_registers =
         dex_register_map_without_locations.GetNumberOfLiveDexRegisters(number_of_dex_registers);
     size_t location_mapping_data_size_in_bits =
-        DexRegisterMap::SingleEntrySizeInBits(GetNumberOfDexRegisterLocationCatalogEntries())
+        DexRegisterMap::SingleEntrySizeInBits(GetNumberOfLocationCatalogEntries())
         * number_of_live_dex_registers;
     size_t location_mapping_data_size_in_bytes =
         RoundUp(location_mapping_data_size_in_bits, kBitsPerByte) / kBitsPerByte;
@@ -1214,6 +1259,9 @@
   friend class StackMapStream;
 };
 
+#undef ELEMENT_BYTE_OFFSET_AFTER
+#undef ELEMENT_BIT_OFFSET_AFTER
+
 }  // namespace art
 
 #endif  // ART_RUNTIME_STACK_MAP_H_