OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_ | 5 #ifndef COURGETTE_ASSEMBLY_PROGRAM_H_ |
6 #define COURGETTE_ASSEMBLY_PROGRAM_H_ | 6 #define COURGETTE_ASSEMBLY_PROGRAM_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
| 12 #include <memory> |
12 #include <set> | 13 #include <set> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
16 #include "base/memory/free_deleter.h" | 17 #include "base/memory/free_deleter.h" |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "courgette/courgette.h" | 18 #include "courgette/courgette.h" |
19 #include "courgette/image_utils.h" | 19 #include "courgette/image_utils.h" |
20 #include "courgette/label_manager.h" | 20 #include "courgette/label_manager.h" |
21 #include "courgette/memory_allocator.h" | 21 #include "courgette/memory_allocator.h" |
22 | 22 |
23 namespace courgette { | 23 namespace courgette { |
24 | 24 |
25 class EncodedProgram; | 25 class EncodedProgram; |
26 | 26 |
27 // Opcodes of simple assembly language | 27 // Opcodes of simple assembly language |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 // Looks up a label or creates a new one. Might return NULL. | 126 // Looks up a label or creates a new one. Might return NULL. |
127 Label* FindOrMakeAbs32Label(RVA rva); | 127 Label* FindOrMakeAbs32Label(RVA rva); |
128 | 128 |
129 // Looks up a label or creates a new one. Might return NULL. | 129 // Looks up a label or creates a new one. Might return NULL. |
130 Label* FindOrMakeRel32Label(RVA rva); | 130 Label* FindOrMakeRel32Label(RVA rva); |
131 | 131 |
132 void DefaultAssignIndexes(); | 132 void DefaultAssignIndexes(); |
133 void UnassignIndexes(); | 133 void UnassignIndexes(); |
134 void AssignRemainingIndexes(); | 134 void AssignRemainingIndexes(); |
135 | 135 |
136 scoped_ptr<EncodedProgram> Encode() const; | 136 std::unique_ptr<EncodedProgram> Encode() const; |
137 | 137 |
138 // Accessor for instruction list. | 138 // Accessor for instruction list. |
139 const InstructionVector& instructions() const { | 139 const InstructionVector& instructions() const { |
140 return instructions_; | 140 return instructions_; |
141 } | 141 } |
142 | 142 |
143 // Returns the label if the instruction contains an absolute 32-bit address, | 143 // Returns the label if the instruction contains an absolute 32-bit address, |
144 // otherwise returns NULL. | 144 // otherwise returns NULL. |
145 Label* InstructionAbs32Label(const Instruction* instruction) const; | 145 Label* InstructionAbs32Label(const Instruction* instruction) const; |
146 | 146 |
147 // Returns the label if the instruction contains an absolute 64-bit address, | 147 // Returns the label if the instruction contains an absolute 64-bit address, |
148 // otherwise returns NULL. | 148 // otherwise returns NULL. |
149 Label* InstructionAbs64Label(const Instruction* instruction) const; | 149 Label* InstructionAbs64Label(const Instruction* instruction) const; |
150 | 150 |
151 // Returns the label if the instruction contains a rel32 offset, | 151 // Returns the label if the instruction contains a rel32 offset, |
152 // otherwise returns NULL. | 152 // otherwise returns NULL. |
153 Label* InstructionRel32Label(const Instruction* instruction) const; | 153 Label* InstructionRel32Label(const Instruction* instruction) const; |
154 | 154 |
155 // Removes underused Labels. Thresholds used (may be 0, i.e., no trimming) is | 155 // Removes underused Labels. Thresholds used (may be 0, i.e., no trimming) is |
156 // dependent on architecture. Returns true on success, and false otherwise. | 156 // dependent on architecture. Returns true on success, and false otherwise. |
157 CheckBool TrimLabels(); | 157 CheckBool TrimLabels(); |
158 | 158 |
159 private: | 159 private: |
160 using ScopedInstruction = | 160 using ScopedInstruction = |
161 scoped_ptr<Instruction, UncheckedDeleter<Instruction>>; | 161 std::unique_ptr<Instruction, UncheckedDeleter<Instruction>>; |
162 | 162 |
163 ExecutableType kind_; | 163 ExecutableType kind_; |
164 | 164 |
165 CheckBool Emit(ScopedInstruction instruction) WARN_UNUSED_RESULT; | 165 CheckBool Emit(ScopedInstruction instruction) WARN_UNUSED_RESULT; |
166 CheckBool EmitShared(Instruction* instruction) WARN_UNUSED_RESULT; | 166 CheckBool EmitShared(Instruction* instruction) WARN_UNUSED_RESULT; |
167 | 167 |
168 static const int kLabelLowerLimit; | 168 static const int kLabelLowerLimit; |
169 | 169 |
170 // Looks up a label or creates a new one. Might return NULL. | 170 // Looks up a label or creates a new one. Might return NULL. |
171 Label* FindLabel(RVA rva, RVAToLabel* labels); | 171 Label* FindLabel(RVA rva, RVAToLabel* labels); |
172 | 172 |
173 // Helper methods for the public versions. | 173 // Helper methods for the public versions. |
174 static void UnassignIndexes(RVAToLabel* labels); | 174 static void UnassignIndexes(RVAToLabel* labels); |
175 static void DefaultAssignIndexes(RVAToLabel* labels); | 175 static void DefaultAssignIndexes(RVAToLabel* labels); |
176 static void AssignRemainingIndexes(RVAToLabel* labels); | 176 static void AssignRemainingIndexes(RVAToLabel* labels); |
177 | 177 |
178 // Sharing instructions that emit a single byte saves a lot of space. | 178 // Sharing instructions that emit a single byte saves a lot of space. |
179 Instruction* GetByteInstruction(uint8_t byte); | 179 Instruction* GetByteInstruction(uint8_t byte); |
180 scoped_ptr<Instruction* [], base::FreeDeleter> byte_instruction_cache_; | 180 std::unique_ptr<Instruction* [], base::FreeDeleter> byte_instruction_cache_; |
181 | 181 |
182 uint64_t image_base_; // Desired or mandated base address of image. | 182 uint64_t image_base_; // Desired or mandated base address of image. |
183 | 183 |
184 InstructionVector instructions_; // All the instructions in program. | 184 InstructionVector instructions_; // All the instructions in program. |
185 | 185 |
186 // These are lookup maps to find the label associated with a given address. | 186 // These are lookup maps to find the label associated with a given address. |
187 // We have separate label spaces for addresses referenced by rel32 labels and | 187 // We have separate label spaces for addresses referenced by rel32 labels and |
188 // abs32 labels. This is somewhat arbitrary. | 188 // abs32 labels. This is somewhat arbitrary. |
189 RVAToLabel rel32_labels_; | 189 RVAToLabel rel32_labels_; |
190 RVAToLabel abs32_labels_; | 190 RVAToLabel abs32_labels_; |
191 | 191 |
192 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); | 192 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); |
193 }; | 193 }; |
194 | 194 |
195 // Converts |program| into encoded form, returning it as |*output|. | 195 // Converts |program| into encoded form, returning it as |*output|. |
196 // Returns C_OK if succeeded, otherwise returns an error status and sets | 196 // Returns C_OK if succeeded, otherwise returns an error status and sets |
197 // |*output| to null. | 197 // |*output| to null. |
198 Status Encode(const AssemblyProgram& program, | 198 Status Encode(const AssemblyProgram& program, |
199 scoped_ptr<EncodedProgram>* output); | 199 std::unique_ptr<EncodedProgram>* output); |
200 | 200 |
201 } // namespace courgette | 201 } // namespace courgette |
202 | 202 |
203 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ | 203 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ |
OLD | NEW |