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 <memory> |
13 #include <set> | 13 #include <set> |
grt (UTC plus 2)
2017/01/12 11:56:41
unused
huangs
2017/01/12 19:54:41
Done.
| |
14 #include <vector> | 14 #include <vector> |
grt (UTC plus 2)
2017/01/12 11:56:41
unused
huangs
2017/01/12 19:54:42
Done.
| |
15 | 15 |
16 #include "base/bind.h" | 16 #include "base/bind.h" |
grt (UTC plus 2)
2017/01/12 11:56:41
unused
huangs
2017/01/12 19:54:42
Need bind.h for base::Callback.
grt (UTC plus 2)
2017/01/13 08:27:32
It looks like all you need here is the forward-dec
huangs
2017/01/13 16:44:54
Ah nice! Going with callback_forward.h (include ca
| |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/free_deleter.h" | 18 #include "base/memory/free_deleter.h" |
19 #include "courgette/courgette.h" | 19 #include "courgette/courgette.h" |
20 #include "courgette/image_utils.h" | 20 #include "courgette/image_utils.h" |
21 #include "courgette/label_manager.h" | 21 #include "courgette/label_manager.h" |
22 #include "courgette/memory_allocator.h" | 22 #include "courgette/memory_allocator.h" |
23 | 23 |
24 namespace courgette { | 24 namespace courgette { |
25 | 25 |
26 class EncodedProgram; | 26 class EncodedProgram; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 using LabelHandlerMap = std::map<OP, LabelHandler>; | 125 using LabelHandlerMap = std::map<OP, LabelHandler>; |
126 | 126 |
127 // A callback for GenerateInstructions() to emit instructions. The first | 127 // A callback for GenerateInstructions() to emit instructions. The first |
128 // argument (AssemblyProgram*) is provided for Label-related feature access. | 128 // argument (AssemblyProgram*) is provided for Label-related feature access. |
129 // The second argument (InstructionReceptor*) is a receptor for instructions. | 129 // The second argument (InstructionReceptor*) is a receptor for instructions. |
130 // The callback (which gets called in 2 passes) should return true on success, | 130 // The callback (which gets called in 2 passes) should return true on success, |
131 // and false otherwise. | 131 // and false otherwise. |
132 using InstructionGenerator = | 132 using InstructionGenerator = |
133 base::Callback<CheckBool(AssemblyProgram*, InstructionReceptor*)>; | 133 base::Callback<CheckBool(AssemblyProgram*, InstructionReceptor*)>; |
134 | 134 |
135 explicit AssemblyProgram(ExecutableType kind); | 135 AssemblyProgram(ExecutableType kind, uint64_t image_base); |
136 ~AssemblyProgram(); | 136 ~AssemblyProgram(); |
137 | 137 |
138 ExecutableType kind() const { return kind_; } | 138 ExecutableType kind() const { return kind_; } |
139 | 139 |
140 void set_image_base(uint64_t image_base) { image_base_ = image_base; } | |
141 | |
142 // Traverses RVAs in |abs32_visitor| and |rel32_visitor| to precompute Labels. | 140 // Traverses RVAs in |abs32_visitor| and |rel32_visitor| to precompute Labels. |
143 void PrecomputeLabels(RvaVisitor* abs32_visitor, RvaVisitor* rel32_visitor); | 141 void PrecomputeLabels(RvaVisitor* abs32_visitor, RvaVisitor* rel32_visitor); |
144 | 142 |
145 // Removes underused Labels. Thresholds used (0 = no trimming) is | 143 // Removes underused Labels. Thresholds used (0 = no trimming) is |
146 // architecture-dependent. | 144 // architecture-dependent. |
147 void TrimLabels(); | 145 void TrimLabels(); |
148 | 146 |
149 void UnassignIndexes(); | 147 void UnassignIndexes(); |
150 void DefaultAssignIndexes(); | 148 void DefaultAssignIndexes(); |
151 void AssignRemainingIndexes(); | 149 void AssignRemainingIndexes(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 | 203 |
206 // Generates an 8-byte absolute reference to address of 'label'. | 204 // Generates an 8-byte absolute reference to address of 'label'. |
207 CheckBool EmitAbs64(Label* label) WARN_UNUSED_RESULT; | 205 CheckBool EmitAbs64(Label* label) WARN_UNUSED_RESULT; |
208 | 206 |
209 private: | 207 private: |
210 using InstructionVector = NoThrowBuffer<Instruction*>; | 208 using InstructionVector = NoThrowBuffer<Instruction*>; |
211 | 209 |
212 using ScopedInstruction = | 210 using ScopedInstruction = |
213 std::unique_ptr<Instruction, UncheckedDeleter<Instruction>>; | 211 std::unique_ptr<Instruction, UncheckedDeleter<Instruction>>; |
214 | 212 |
215 ExecutableType kind_; | 213 const ExecutableType kind_; |
grt (UTC plus 2)
2017/01/12 11:56:41
nit: these two data members (although const) belon
huangs
2017/01/12 19:54:41
Done.
| |
214 const uint64_t image_base_; // Desired or mandated base address of image. | |
216 | 215 |
217 CheckBool Emit(ScopedInstruction instruction) WARN_UNUSED_RESULT; | 216 CheckBool Emit(ScopedInstruction instruction) WARN_UNUSED_RESULT; |
218 CheckBool EmitShared(Instruction* instruction) WARN_UNUSED_RESULT; | 217 CheckBool EmitShared(Instruction* instruction) WARN_UNUSED_RESULT; |
219 | 218 |
220 static const int kLabelLowerLimit; | 219 static const int kLabelLowerLimit; |
221 | 220 |
222 // Looks up a label or creates a new one. Might return NULL. | 221 // Looks up a label or creates a new one. Might return NULL. |
223 Label* FindLabel(RVA rva, RVAToLabel* labels); | 222 Label* FindLabel(RVA rva, RVAToLabel* labels); |
224 | 223 |
225 // Helper methods for the public versions. | |
226 static void UnassignIndexes(RVAToLabel* labels); | |
227 static void DefaultAssignIndexes(RVAToLabel* labels); | |
228 static void AssignRemainingIndexes(RVAToLabel* labels); | |
229 | |
230 // Sharing instructions that emit a single byte saves a lot of space. | 224 // Sharing instructions that emit a single byte saves a lot of space. |
231 Instruction* GetByteInstruction(uint8_t byte); | 225 Instruction* GetByteInstruction(uint8_t byte); |
232 std::unique_ptr<Instruction* [], base::FreeDeleter> byte_instruction_cache_; | 226 std::unique_ptr<Instruction* [], base::FreeDeleter> byte_instruction_cache_; |
233 | 227 |
234 uint64_t image_base_; // Desired or mandated base address of image. | |
235 | |
236 InstructionVector instructions_; // All the instructions in program. | 228 InstructionVector instructions_; // All the instructions in program. |
237 | 229 |
238 // Storage and lookup of Labels associated with target addresses. We use | 230 // Storage and lookup of Labels associated with target addresses. We use |
239 // separate abs32 and rel32 labels. | 231 // separate abs32 and rel32 labels. |
240 LabelManager abs32_label_manager_; | 232 LabelManager abs32_label_manager_; |
241 LabelManager rel32_label_manager_; | 233 LabelManager rel32_label_manager_; |
242 | 234 |
243 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); | 235 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); |
244 }; | 236 }; |
245 | 237 |
246 // Converts |program| into encoded form, returning it as |*output|. | 238 // Converts |program| into encoded form, returning it as |*output|. |
247 // Returns C_OK if succeeded, otherwise returns an error status and sets | 239 // Returns C_OK if succeeded, otherwise returns an error status and sets |
248 // |*output| to null. | 240 // |*output| to null. |
249 Status Encode(const AssemblyProgram& program, | 241 Status Encode(const AssemblyProgram& program, |
250 std::unique_ptr<EncodedProgram>* output); | 242 std::unique_ptr<EncodedProgram>* output); |
251 | 243 |
252 } // namespace courgette | 244 } // namespace courgette |
253 | 245 |
254 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ | 246 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ |
OLD | NEW |