blob: 95912ad6c96bf9644ce97420be817c3dd03c7361 [file] [log] [blame]
David Srbecky15c19752015-03-31 14:53:55 +00001/*
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
David Srbecky4fda4eb2016-02-05 13:34:46 +000017#ifndef ART_COMPILER_DEBUG_DWARF_WRITER_H_
18#define ART_COMPILER_DEBUG_DWARF_WRITER_H_
David Srbecky15c19752015-03-31 14:53:55 +000019
Vladimir Marko35831e82015-09-11 11:59:18 +010020#include <type_traits>
David Srbecky15c19752015-03-31 14:53:55 +000021#include <vector>
Vladimir Marko80afd022015-05-19 18:08:00 +010022#include "base/bit_utils.h"
David Srbecky15c19752015-03-31 14:53:55 +000023#include "base/logging.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "leb128.h"
David Srbecky15c19752015-03-31 14:53:55 +000025
26namespace art {
27namespace dwarf {
28
29// The base class for all DWARF writers.
Vladimir Markoec7802a2015-10-01 20:57:57 +010030template <typename Vector = std::vector<uint8_t>>
David Srbecky15c19752015-03-31 14:53:55 +000031class Writer {
Vladimir Markoec7802a2015-10-01 20:57:57 +010032 static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type");
33
David Srbecky15c19752015-03-31 14:53:55 +000034 public:
35 void PushUint8(int value) {
36 DCHECK_GE(value, 0);
37 DCHECK_LE(value, UINT8_MAX);
38 data_->push_back(value & 0xff);
39 }
40
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
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
55 void PushUint32(int value) {
56 DCHECK_GE(value, 0);
57 PushUint32(static_cast<uint32_t>(value));
58 }
59
60 void PushUint32(uint64_t value) {
61 DCHECK_LE(value, UINT32_MAX);
62 PushUint32(static_cast<uint32_t>(value));
63 }
64
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
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
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
88 void PushInt32(int value) {
89 PushUint32(static_cast<uint32_t>(value));
90 }
91
92 void PushInt64(int64_t value) {
93 PushUint64(static_cast<uint64_t>(value));
94 }
95
96 // Variable-length encoders.
97
98 void PushUleb128(uint32_t value) {
99 EncodeUnsignedLeb128(data_, value);
100 }
101
102 void PushUleb128(int value) {
103 DCHECK_GE(value, 0);
104 EncodeUnsignedLeb128(data_, value);
105 }
106
107 void PushSleb128(int value) {
108 EncodeSignedLeb128(data_, value);
109 }
110
111 // Miscellaneous functions.
112
113 void PushString(const char* value) {
114 data_->insert(data_->end(), value, value + strlen(value) + 1);
115 }
116
David Srbecky91cb54e2016-01-15 13:47:59 +0000117 void PushData(const uint8_t* ptr, size_t num_bytes) {
118 data_->insert(data_->end(), ptr, ptr + num_bytes);
David Srbecky15c19752015-03-31 14:53:55 +0000119 }
120
David Srbecky91cb54e2016-01-15 13:47:59 +0000121 void PushData(const char* ptr, size_t num_bytes) {
122 data_->insert(data_->end(), ptr, ptr + num_bytes);
123 }
124
125 void PushData(const Vector* buffer) {
126 data_->insert(data_->end(), buffer->begin(), buffer->end());
David Srbeckyb5362472015-04-08 19:37:39 +0100127 }
128
David Srbecky15c19752015-03-31 14:53:55 +0000129 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
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
David Srbeckyb5362472015-04-08 19:37:39 +0100149 void UpdateUleb128(size_t offset, uint32_t value) {
150 DCHECK_LE(offset + UnsignedLeb128Size(value), data_->size());
151 UpdateUnsignedLeb128(data_->data() + offset, value);
152 }
153
154 void Pop() {
155 return data_->pop_back();
156 }
157
David Srbecky15c19752015-03-31 14:53:55 +0000158 void Pad(int alignment) {
159 DCHECK_NE(alignment, 0);
160 data_->resize(RoundUp(data_->size(), alignment), 0);
161 }
162
Vladimir Markoec7802a2015-10-01 20:57:57 +0100163 const Vector* data() const {
David Srbecky15c19752015-03-31 14:53:55 +0000164 return data_;
165 }
166
David Srbecky04b05262015-11-09 18:05:48 +0000167 size_t size() const {
168 return data_->size();
169 }
170
Vladimir Markoec7802a2015-10-01 20:57:57 +0100171 explicit Writer(Vector* buffer) : data_(buffer) { }
David Srbecky15c19752015-03-31 14:53:55 +0000172
173 private:
Vladimir Markoec7802a2015-10-01 20:57:57 +0100174 Vector* const data_;
David Srbecky15c19752015-03-31 14:53:55 +0000175
176 DISALLOW_COPY_AND_ASSIGN(Writer);
177};
178
179} // namespace dwarf
180} // namespace art
181
David Srbecky4fda4eb2016-02-05 13:34:46 +0000182#endif // ART_COMPILER_DEBUG_DWARF_WRITER_H_