Chromium Code Reviews| 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> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/bind.h" | |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/free_deleter.h" | 18 #include "base/memory/free_deleter.h" |
| 18 #include "courgette/courgette.h" | 19 #include "courgette/courgette.h" |
| 19 #include "courgette/image_utils.h" | 20 #include "courgette/image_utils.h" |
| 20 #include "courgette/label_manager.h" | 21 #include "courgette/label_manager.h" |
| 21 #include "courgette/memory_allocator.h" | 22 #include "courgette/memory_allocator.h" |
| 22 | 23 |
| 23 namespace courgette { | 24 namespace courgette { |
| 24 | 25 |
| 25 class EncodedProgram; | 26 class EncodedProgram; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 // | 73 // |
| 73 // The optional step is to modify the AssemblyProgram. One form of modification | 74 // The optional step is to modify the AssemblyProgram. One form of modification |
| 74 // is to assign indexes in such a way as to make the EncodedProgram for this | 75 // is to assign indexes in such a way as to make the EncodedProgram for this |
| 75 // AssemblyProgram look more like the EncodedProgram for some other | 76 // AssemblyProgram look more like the EncodedProgram for some other |
| 76 // AssemblyProgram. The modification process should call UnassignIndexes, do | 77 // AssemblyProgram. The modification process should call UnassignIndexes, do |
| 77 // its own assignment, and then call AssignRemainingIndexes to ensure all | 78 // its own assignment, and then call AssignRemainingIndexes to ensure all |
| 78 // indexes are assigned. | 79 // indexes are assigned. |
| 79 // | 80 // |
| 80 class AssemblyProgram { | 81 class AssemblyProgram { |
| 81 public: | 82 public: |
| 83 using LabelHandler = base::Callback<void(Label*)>; | |
| 84 using LabelDispatcherMap = std::map<OP, LabelHandler>; | |
|
chrisha
2016/10/28 20:39:14
LabelHandlerMap, for consistency?
huangs
2016/10/28 21:48:13
Done.
| |
| 85 | |
| 82 explicit AssemblyProgram(ExecutableType kind); | 86 explicit AssemblyProgram(ExecutableType kind); |
| 83 ~AssemblyProgram(); | 87 ~AssemblyProgram(); |
| 84 | 88 |
| 85 ExecutableType kind() const { return kind_; } | 89 ExecutableType kind() const { return kind_; } |
| 86 | 90 |
| 87 void set_image_base(uint64_t image_base) { image_base_ = image_base; } | 91 void set_image_base(uint64_t image_base) { image_base_ = image_base; } |
| 88 | 92 |
| 89 // Instructions will be assembled in the order they are emitted. | 93 // Instructions will be assembled in the order they are emitted. |
| 90 | 94 |
| 91 // Generates an entire base relocation table. | 95 // Generates an entire base relocation table. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 void AssignRemainingIndexes(); | 139 void AssignRemainingIndexes(); |
| 136 | 140 |
| 137 // Looks up abs32 label. Returns null if none found. | 141 // Looks up abs32 label. Returns null if none found. |
| 138 Label* FindAbs32Label(RVA rva); | 142 Label* FindAbs32Label(RVA rva); |
| 139 | 143 |
| 140 // Looks up rel32 label. Returns null if none found. | 144 // Looks up rel32 label. Returns null if none found. |
| 141 Label* FindRel32Label(RVA rva); | 145 Label* FindRel32Label(RVA rva); |
| 142 | 146 |
| 143 std::unique_ptr<EncodedProgram> Encode() const; | 147 std::unique_ptr<EncodedProgram> Encode() const; |
| 144 | 148 |
| 145 // Accessor for instruction list. | 149 // For each |instruction| in |instructions_|, looks up its opcode from |
| 146 const InstructionVector& instructions() const { | 150 // |dispatcher| for a handler. If a handler exists, invoke it by passing the |
| 147 return instructions_; | 151 // |instruction|'s label. We assume that |dispatcher| has correct keys, i.e., |
| 148 } | 152 // opcodes for an instruction that have label. |
| 149 | 153 void DispatchInstructionLabels(const LabelDispatcherMap& dispatcher) const; |
|
chrisha
2016/10/28 20:39:14
HandleInstructionLabels?
Are we "dispatching" any
huangs
2016/10/28 21:48:13
Done.
| |
| 150 // Returns the label if the instruction contains an absolute 32-bit address, | |
| 151 // otherwise returns NULL. | |
| 152 Label* InstructionAbs32Label(const Instruction* instruction) const; | |
| 153 | |
| 154 // Returns the label if the instruction contains an absolute 64-bit address, | |
| 155 // otherwise returns NULL. | |
| 156 Label* InstructionAbs64Label(const Instruction* instruction) const; | |
| 157 | |
| 158 // Returns the label if the instruction contains a rel32 offset, | |
| 159 // otherwise returns NULL. | |
| 160 Label* InstructionRel32Label(const Instruction* instruction) const; | |
| 161 | 154 |
| 162 private: | 155 private: |
| 163 using ScopedInstruction = | 156 using ScopedInstruction = |
| 164 std::unique_ptr<Instruction, UncheckedDeleter<Instruction>>; | 157 std::unique_ptr<Instruction, UncheckedDeleter<Instruction>>; |
| 165 | 158 |
| 166 ExecutableType kind_; | 159 ExecutableType kind_; |
| 167 | 160 |
| 168 CheckBool Emit(ScopedInstruction instruction) WARN_UNUSED_RESULT; | 161 CheckBool Emit(ScopedInstruction instruction) WARN_UNUSED_RESULT; |
| 169 CheckBool EmitShared(Instruction* instruction) WARN_UNUSED_RESULT; | 162 CheckBool EmitShared(Instruction* instruction) WARN_UNUSED_RESULT; |
| 170 | 163 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 196 | 189 |
| 197 // Converts |program| into encoded form, returning it as |*output|. | 190 // Converts |program| into encoded form, returning it as |*output|. |
| 198 // Returns C_OK if succeeded, otherwise returns an error status and sets | 191 // Returns C_OK if succeeded, otherwise returns an error status and sets |
| 199 // |*output| to null. | 192 // |*output| to null. |
| 200 Status Encode(const AssemblyProgram& program, | 193 Status Encode(const AssemblyProgram& program, |
| 201 std::unique_ptr<EncodedProgram>* output); | 194 std::unique_ptr<EncodedProgram>* output); |
| 202 | 195 |
| 203 } // namespace courgette | 196 } // namespace courgette |
| 204 | 197 |
| 205 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ | 198 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ |
| OLD | NEW |