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