1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_DEBUG_DWARF_WRITER_H_
18 #define ART_COMPILER_DEBUG_DWARF_WRITER_H_
19 
20 #include <type_traits>
21 #include <vector>
22 #include "base/bit_utils.h"
23 #include "base/logging.h"
24 #include "leb128.h"
25 
26 namespace art {
27 namespace dwarf {
28 
29 // The base class for all DWARF writers.
30 template <typename Vector = std::vector<uint8_t>>
31 class Writer {
32   static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
33 
34  public:
PushUint8(int value)35   void PushUint8(int value) {
36     DCHECK_GE(value, 0);
37     DCHECK_LE(value, UINT8_MAX);
38     data_->push_back(value & 0xff);
39   }
40 
PushUint16(int value)41   void PushUint16(int value) {
42     DCHECK_GE(value, 0);
43     DCHECK_LE(value, UINT16_MAX);
44     data_->push_back((value >> 0) & 0xff);
45     data_->push_back((value >> 8) & 0xff);
46   }
47 
PushUint32(uint32_t value)48   void PushUint32(uint32_t value) {
49     data_->push_back((value >> 0) & 0xff);
50     data_->push_back((value >> 8) & 0xff);
51     data_->push_back((value >> 16) & 0xff);
52     data_->push_back((value >> 24) & 0xff);
53   }
54 
PushUint32(int value)55   void PushUint32(int value) {
56     DCHECK_GE(value, 0);
57     PushUint32(static_cast<uint32_t>(value));
58   }
59 
PushUint32(uint64_t value)60   void PushUint32(uint64_t value) {
61     DCHECK_LE(value, UINT32_MAX);
62     PushUint32(static_cast<uint32_t>(value));
63   }
64 
PushUint64(uint64_t value)65   void PushUint64(uint64_t value) {
66     data_->push_back((value >> 0) & 0xff);
67     data_->push_back((value >> 8) & 0xff);
68     data_->push_back((value >> 16) & 0xff);
69     data_->push_back((value >> 24) & 0xff);
70     data_->push_back((value >> 32) & 0xff);
71     data_->push_back((value >> 40) & 0xff);
72     data_->push_back((value >> 48) & 0xff);
73     data_->push_back((value >> 56) & 0xff);
74   }
75 
PushInt8(int value)76   void PushInt8(int value) {
77     DCHECK_GE(value, INT8_MIN);
78     DCHECK_LE(value, INT8_MAX);
79     PushUint8(static_cast<uint8_t>(value));
80   }
81 
PushInt16(int value)82   void PushInt16(int value) {
83     DCHECK_GE(value, INT16_MIN);
84     DCHECK_LE(value, INT16_MAX);
85     PushUint16(static_cast<uint16_t>(value));
86   }
87 
PushInt32(int value)88   void PushInt32(int value) {
89     PushUint32(static_cast<uint32_t>(value));
90   }
91 
PushInt64(int64_t value)92   void PushInt64(int64_t value) {
93     PushUint64(static_cast<uint64_t>(value));
94   }
95 
96   // Variable-length encoders.
97 
PushUleb128(uint32_t value)98   void PushUleb128(uint32_t value) {
99     EncodeUnsignedLeb128(data_, value);
100   }
101 
PushUleb128(int value)102   void PushUleb128(int value) {
103     DCHECK_GE(value, 0);
104     EncodeUnsignedLeb128(data_, value);
105   }
106 
PushSleb128(int value)107   void PushSleb128(int value) {
108     EncodeSignedLeb128(data_, value);
109   }
110 
111   // Miscellaneous functions.
112 
PushString(const char * value)113   void PushString(const char* value) {
114     data_->insert(data_->end(), value, value + strlen(value) + 1);
115   }
116 
PushData(const uint8_t * ptr,size_t num_bytes)117   void PushData(const uint8_t* ptr, size_t num_bytes) {
118     data_->insert(data_->end(), ptr, ptr + num_bytes);
119   }
120 
PushData(const char * ptr,size_t num_bytes)121   void PushData(const char* ptr, size_t num_bytes) {
122     data_->insert(data_->end(), ptr, ptr + num_bytes);
123   }
124 
PushData(const Vector * buffer)125   void PushData(const Vector* buffer) {
126     data_->insert(data_->end(), buffer->begin(), buffer->end());
127   }
128 
UpdateUint32(size_t offset,uint32_t value)129   void UpdateUint32(size_t offset, uint32_t value) {
130     DCHECK_LT(offset + 3, data_->size());
131     (*data_)[offset + 0] = (value >> 0) & 0xFF;
132     (*data_)[offset + 1] = (value >> 8) & 0xFF;
133     (*data_)[offset + 2] = (value >> 16) & 0xFF;
134     (*data_)[offset + 3] = (value >> 24) & 0xFF;
135   }
136 
UpdateUint64(size_t offset,uint64_t value)137   void UpdateUint64(size_t offset, uint64_t value) {
138     DCHECK_LT(offset + 7, data_->size());
139     (*data_)[offset + 0] = (value >> 0) & 0xFF;
140     (*data_)[offset + 1] = (value >> 8) & 0xFF;
141     (*data_)[offset + 2] = (value >> 16) & 0xFF;
142     (*data_)[offset + 3] = (value >> 24) & 0xFF;
143     (*data_)[offset + 4] = (value >> 32) & 0xFF;
144     (*data_)[offset + 5] = (value >> 40) & 0xFF;
145     (*data_)[offset + 6] = (value >> 48) & 0xFF;
146     (*data_)[offset + 7] = (value >> 56) & 0xFF;
147   }
148 
UpdateUleb128(size_t offset,uint32_t value)149   void UpdateUleb128(size_t offset, uint32_t value) {
150     DCHECK_LE(offset + UnsignedLeb128Size(value), data_->size());
151     UpdateUnsignedLeb128(data_->data() + offset, value);
152   }
153 
Pop()154   void Pop() {
155     return data_->pop_back();
156   }
157 
Pad(int alignment)158   void Pad(int alignment) {
159     DCHECK_NE(alignment, 0);
160     data_->resize(RoundUp(data_->size(), alignment), 0);
161   }
162 
data()163   const Vector* data() const {
164     return data_;
165   }
166 
size()167   size_t size() const {
168     return data_->size();
169   }
170 
Writer(Vector * buffer)171   explicit Writer(Vector* buffer) : data_(buffer) { }
172 
173  private:
174   Vector* const data_;
175 
176   DISALLOW_COPY_AND_ASSIGN(Writer);
177 };
178 
179 }  // namespace dwarf
180 }  // namespace art
181 
182 #endif  // ART_COMPILER_DEBUG_DWARF_WRITER_H_
183