| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 | 13 |
| 14 #include "courgette/image_info.h" | 14 #include "courgette/image_info.h" |
| 15 #include "courgette/memory_allocator.h" |
| 15 | 16 |
| 16 namespace courgette { | 17 namespace courgette { |
| 17 | 18 |
| 18 class EncodedProgram; | 19 class EncodedProgram; |
| 19 class Instruction; | 20 class Instruction; |
| 20 | 21 |
| 22 typedef std::vector<Instruction*, MemoryAllocator<Instruction*> > |
| 23 InstructionVector; |
| 24 |
| 21 // A Label is a symbolic reference to an address. Unlike a conventional | 25 // A Label is a symbolic reference to an address. Unlike a conventional |
| 22 // assembly language, we always know the address. The address will later be | 26 // assembly language, we always know the address. The address will later be |
| 23 // stored in a table and the Label will be replaced with the index into the | 27 // stored in a table and the Label will be replaced with the index into the |
| 24 // table. | 28 // table. |
| 25 // | 29 // |
| 26 // TODO(sra): Make fields private and add setters and getters. | 30 // TODO(sra): Make fields private and add setters and getters. |
| 27 class Label { | 31 class Label { |
| 28 public: | 32 public: |
| 29 static const int kNoIndex = -1; | 33 static const int kNoIndex = -1; |
| 30 Label() : rva_(0), index_(kNoIndex) {} | 34 Label() : rva_(0), index_(kNoIndex) {} |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 Label* FindOrMakeAbs32Label(RVA rva); | 86 Label* FindOrMakeAbs32Label(RVA rva); |
| 83 Label* FindOrMakeRel32Label(RVA rva); | 87 Label* FindOrMakeRel32Label(RVA rva); |
| 84 | 88 |
| 85 void DefaultAssignIndexes(); | 89 void DefaultAssignIndexes(); |
| 86 void UnassignIndexes(); | 90 void UnassignIndexes(); |
| 87 void AssignRemainingIndexes(); | 91 void AssignRemainingIndexes(); |
| 88 | 92 |
| 89 EncodedProgram* Encode() const; | 93 EncodedProgram* Encode() const; |
| 90 | 94 |
| 91 // Accessor for instruction list. | 95 // Accessor for instruction list. |
| 92 const std::vector<Instruction*>& instructions() const { | 96 const InstructionVector& instructions() const { |
| 93 return instructions_; | 97 return instructions_; |
| 94 } | 98 } |
| 95 | 99 |
| 96 // Returns the label if the instruction contains and absolute address, | 100 // Returns the label if the instruction contains and absolute address, |
| 97 // otherwise returns NULL. | 101 // otherwise returns NULL. |
| 98 Label* InstructionAbs32Label(const Instruction* instruction) const; | 102 Label* InstructionAbs32Label(const Instruction* instruction) const; |
| 99 | 103 |
| 100 // Returns the label if the instruction contains and rel32 offset, | 104 // Returns the label if the instruction contains and rel32 offset, |
| 101 // otherwise returns NULL. | 105 // otherwise returns NULL. |
| 102 Label* InstructionRel32Label(const Instruction* instruction) const; | 106 Label* InstructionRel32Label(const Instruction* instruction) const; |
| 103 | 107 |
| 104 | |
| 105 private: | 108 private: |
| 106 void Emit(Instruction* instruction) { instructions_.push_back(instruction); } | 109 void Emit(Instruction* instruction) { instructions_.push_back(instruction); } |
| 107 | 110 |
| 108 Label* FindLabel(RVA rva, RVAToLabel* labels); | 111 Label* FindLabel(RVA rva, RVAToLabel* labels); |
| 109 | 112 |
| 110 // Helper methods for the public versions. | 113 // Helper methods for the public versions. |
| 111 static void UnassignIndexes(RVAToLabel* labels); | 114 static void UnassignIndexes(RVAToLabel* labels); |
| 112 static void DefaultAssignIndexes(RVAToLabel* labels); | 115 static void DefaultAssignIndexes(RVAToLabel* labels); |
| 113 static void AssignRemainingIndexes(RVAToLabel* labels); | 116 static void AssignRemainingIndexes(RVAToLabel* labels); |
| 114 | 117 |
| 115 // Sharing instructions that emit a single byte saves a lot of space. | 118 // Sharing instructions that emit a single byte saves a lot of space. |
| 116 Instruction* GetByteInstruction(uint8 byte); | 119 Instruction* GetByteInstruction(uint8 byte); |
| 117 Instruction** byte_instruction_cache_; | 120 Instruction** byte_instruction_cache_; |
| 118 | 121 |
| 119 uint64 image_base_; // Desired or mandated base address of image. | 122 uint64 image_base_; // Desired or mandated base address of image. |
| 120 | 123 |
| 121 std::vector<Instruction*> instructions_; // All the instructions in program. | 124 InstructionVector instructions_; // All the instructions in program. |
| 122 | 125 |
| 123 // These are lookup maps to find the label associated with a given address. | 126 // These are lookup maps to find the label associated with a given address. |
| 124 // We have separate label spaces for addresses referenced by rel32 labels and | 127 // We have separate label spaces for addresses referenced by rel32 labels and |
| 125 // abs32 labels. This is somewhat arbitrary. | 128 // abs32 labels. This is somewhat arbitrary. |
| 126 RVAToLabel rel32_labels_; | 129 RVAToLabel rel32_labels_; |
| 127 RVAToLabel abs32_labels_; | 130 RVAToLabel abs32_labels_; |
| 128 | 131 |
| 129 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); | 132 DISALLOW_COPY_AND_ASSIGN(AssemblyProgram); |
| 130 }; | 133 }; |
| 131 | 134 |
| 132 } // namespace courgette | 135 } // namespace courgette |
| 133 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ | 136 #endif // COURGETTE_ASSEMBLY_PROGRAM_H_ |
| OLD | NEW |