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 #include "courgette/assembly_program.h" | 5 #include "courgette/assembly_program.h" |
| 6 | 6 |
| 7 #include <memory.h> | 7 #include <memory.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 uint16_t op_size() const { return op_size_; } | 100 uint16_t op_size() const { return op_size_; } |
| 101 | 101 |
| 102 private: | 102 private: |
| 103 uint16_t compressed_op_; | 103 uint16_t compressed_op_; |
| 104 const uint8_t* arm_op_; | 104 const uint8_t* arm_op_; |
| 105 uint16_t op_size_; | 105 uint16_t op_size_; |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 } // namespace | 108 } // namespace |
| 109 | 109 |
| 110 /******** InstructionReceptor ********/ | |
| 111 | |
| 112 InstructionReceptor::InstructionReceptor() {} | |
| 113 | |
| 114 InstructionReceptor::~InstructionReceptor() {} | |
| 115 | |
| 116 /******** InstructionCountReceptor ********/ | |
| 117 | |
| 118 InstructionCountReceptor::InstructionCountReceptor() : size_(0) {} | |
| 119 | |
| 120 InstructionCountReceptor::~InstructionCountReceptor() {} | |
| 121 | |
| 122 // TODO(huangs): Populate these with size_ += ... | |
|
grt (UTC plus 2)
2016/11/04 09:38:53
TODO when?
huangs
2016/11/04 18:20:39
The follow-up CL, which focuses on AssemblyProgram
| |
| 123 CheckBool InstructionCountReceptor::EmitPeRelocs() { | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 CheckBool InstructionCountReceptor::EmitElfRelocation() { | |
| 128 return true; | |
| 129 } | |
| 130 | |
| 131 CheckBool InstructionCountReceptor::EmitElfARMRelocation() { | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 CheckBool InstructionCountReceptor::EmitOrigin(RVA rva) { | |
| 136 return true; | |
| 137 } | |
| 138 | |
| 139 CheckBool InstructionCountReceptor::EmitSingleByte(uint8_t byte) { | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 CheckBool InstructionCountReceptor::EmitMultipleBytes(const uint8_t* value, | |
| 144 size_t len) { | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 CheckBool InstructionCountReceptor::EmitRel32(Label* label) { | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 CheckBool InstructionCountReceptor::EmitRel32ARM(uint16_t op, | |
| 153 Label* label, | |
| 154 const uint8_t* arm_op, | |
| 155 uint16_t op_size) { | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 159 CheckBool InstructionCountReceptor::EmitAbs32(Label* label) { | |
| 160 return true; | |
| 161 } | |
| 162 | |
| 163 CheckBool InstructionCountReceptor::EmitAbs64(Label* label) { | |
| 164 return true; | |
| 165 } | |
| 166 | |
| 167 /******** InstructionStoreReceptor ********/ | |
| 168 | |
| 169 // TODO(huangs): Populate these. | |
|
grt (UTC plus 2)
2016/11/04 09:38:53
when?
huangs
2016/11/04 18:20:39
Same.
| |
| 170 InstructionStoreReceptor::InstructionStoreReceptor(size_t init_size, | |
| 171 AssemblyProgram* program) | |
| 172 : program_(program) { | |
| 173 CHECK(program_ != nullptr); | |
|
grt (UTC plus 2)
2016/11/04 09:38:53
CHECK_NE(program_, nullptr);
huangs
2016/11/04 18:20:39
Should be consistent with later uses (DCHECK). Com
grt (UTC plus 2)
2016/11/07 09:36:17
I like the brief approach as well.
huangs
2016/11/07 19:46:52
Acknowledged.
| |
| 174 } | |
| 175 | |
| 176 InstructionStoreReceptor::~InstructionStoreReceptor() {} | |
| 177 | |
| 178 CheckBool InstructionStoreReceptor::EmitPeRelocs() { | |
| 179 return program_->EmitPeRelocs(); | |
| 180 } | |
| 181 | |
| 182 CheckBool InstructionStoreReceptor::EmitElfRelocation() { | |
| 183 return program_->EmitElfRelocation(); | |
| 184 } | |
| 185 | |
| 186 CheckBool InstructionStoreReceptor::EmitElfARMRelocation() { | |
| 187 return program_->EmitElfARMRelocation(); | |
| 188 } | |
| 189 | |
| 190 CheckBool InstructionStoreReceptor::EmitOrigin(RVA rva) { | |
| 191 return program_->EmitOrigin(rva); | |
| 192 } | |
| 193 | |
| 194 CheckBool InstructionStoreReceptor::EmitSingleByte(uint8_t byte) { | |
| 195 return program_->EmitSingleByte(byte); | |
| 196 } | |
| 197 | |
| 198 CheckBool InstructionStoreReceptor::EmitMultipleBytes(const uint8_t* value, | |
| 199 size_t len) { | |
| 200 return program_->EmitMultipleBytes(value, len); | |
| 201 } | |
| 202 | |
| 203 CheckBool InstructionStoreReceptor::EmitRel32(Label* label) { | |
| 204 return program_->EmitRel32(label); | |
| 205 } | |
| 206 | |
| 207 CheckBool InstructionStoreReceptor::EmitRel32ARM(uint16_t op, | |
| 208 Label* label, | |
| 209 const uint8_t* arm_op, | |
| 210 uint16_t op_size) { | |
| 211 return program_->EmitRel32ARM(op, label, arm_op, op_size); | |
| 212 } | |
| 213 | |
| 214 CheckBool InstructionStoreReceptor::EmitAbs32(Label* label) { | |
| 215 return program_->EmitAbs32(label); | |
| 216 } | |
| 217 | |
| 218 CheckBool InstructionStoreReceptor::EmitAbs64(Label* label) { | |
| 219 return program_->EmitAbs64(label); | |
| 220 } | |
| 221 | |
| 222 /******** AssemblyProgram ********/ | |
| 223 | |
| 110 AssemblyProgram::AssemblyProgram(ExecutableType kind) | 224 AssemblyProgram::AssemblyProgram(ExecutableType kind) |
| 111 : kind_(kind), image_base_(0) { | 225 : kind_(kind), image_base_(0) { |
| 112 } | 226 } |
| 113 | 227 |
| 114 AssemblyProgram::~AssemblyProgram() { | 228 AssemblyProgram::~AssemblyProgram() { |
| 115 for (size_t i = 0; i < instructions_.size(); ++i) { | 229 for (size_t i = 0; i < instructions_.size(); ++i) { |
| 116 Instruction* instruction = instructions_[i]; | 230 Instruction* instruction = instructions_[i]; |
| 117 if (instruction->op() != DEFBYTE) // Owned by byte_instruction_cache_. | 231 if (instruction->op() != DEFBYTE) // Owned by byte_instruction_cache_. |
| 118 UncheckedDelete(instruction); | 232 UncheckedDelete(instruction); |
| 119 } | 233 } |
| 120 if (byte_instruction_cache_.get()) { | 234 if (byte_instruction_cache_.get()) { |
| 121 for (size_t i = 0; i < 256; ++i) | 235 for (size_t i = 0; i < 256; ++i) |
| 122 UncheckedDelete(byte_instruction_cache_[i]); | 236 UncheckedDelete(byte_instruction_cache_[i]); |
| 123 } | 237 } |
| 124 } | 238 } |
| 125 | 239 |
| 126 CheckBool AssemblyProgram::EmitPeRelocsInstruction() { | 240 CheckBool AssemblyProgram::EmitPeRelocs() { |
| 127 return Emit(ScopedInstruction(UncheckedNew<PeRelocsInstruction>())); | 241 return Emit(ScopedInstruction(UncheckedNew<PeRelocsInstruction>())); |
| 128 } | 242 } |
| 129 | 243 |
| 130 CheckBool AssemblyProgram::EmitElfRelocationInstruction() { | 244 CheckBool AssemblyProgram::EmitElfRelocation() { |
| 131 return Emit(ScopedInstruction(UncheckedNew<ElfRelocsInstruction>())); | 245 return Emit(ScopedInstruction(UncheckedNew<ElfRelocsInstruction>())); |
| 132 } | 246 } |
| 133 | 247 |
| 134 CheckBool AssemblyProgram::EmitElfARMRelocationInstruction() { | 248 CheckBool AssemblyProgram::EmitElfARMRelocation() { |
| 135 return Emit(ScopedInstruction(UncheckedNew<ElfARMRelocsInstruction>())); | 249 return Emit(ScopedInstruction(UncheckedNew<ElfARMRelocsInstruction>())); |
| 136 } | 250 } |
| 137 | 251 |
| 138 CheckBool AssemblyProgram::EmitOriginInstruction(RVA rva) { | 252 CheckBool AssemblyProgram::EmitOrigin(RVA rva) { |
| 139 return Emit(ScopedInstruction(UncheckedNew<OriginInstruction>(rva))); | 253 return Emit(ScopedInstruction(UncheckedNew<OriginInstruction>(rva))); |
| 140 } | 254 } |
| 141 | 255 |
| 142 CheckBool AssemblyProgram::EmitByteInstruction(uint8_t byte) { | 256 CheckBool AssemblyProgram::EmitSingleByte(uint8_t byte) { |
| 143 return EmitShared(GetByteInstruction(byte)); | 257 return EmitShared(GetByteInstruction(byte)); |
| 144 } | 258 } |
| 145 | 259 |
| 146 CheckBool AssemblyProgram::EmitBytesInstruction(const uint8_t* values, | 260 CheckBool AssemblyProgram::EmitMultipleBytes(const uint8_t* values, |
| 147 size_t len) { | 261 size_t len) { |
| 148 return Emit(ScopedInstruction(UncheckedNew<BytesInstruction>(values, len))); | 262 return Emit(ScopedInstruction(UncheckedNew<BytesInstruction>(values, len))); |
| 149 } | 263 } |
| 150 | 264 |
| 151 CheckBool AssemblyProgram::EmitRel32(Label* label) { | 265 CheckBool AssemblyProgram::EmitRel32(Label* label) { |
| 152 return Emit( | 266 return Emit( |
| 153 ScopedInstruction(UncheckedNew<InstructionWithLabel>(REL32, label))); | 267 ScopedInstruction(UncheckedNew<InstructionWithLabel>(REL32, label))); |
| 154 } | 268 } |
| 155 | 269 |
| 156 CheckBool AssemblyProgram::EmitRel32ARM(uint16_t op, | 270 CheckBool AssemblyProgram::EmitRel32ARM(uint16_t op, |
| 157 Label* label, | 271 Label* label, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 const AssemblyProgram::LabelHandlerMap& handler_map) const { | 335 const AssemblyProgram::LabelHandlerMap& handler_map) const { |
| 222 for (const Instruction* instruction : instructions_) { | 336 for (const Instruction* instruction : instructions_) { |
| 223 LabelHandlerMap::const_iterator it = handler_map.find(instruction->op()); | 337 LabelHandlerMap::const_iterator it = handler_map.find(instruction->op()); |
| 224 if (it != handler_map.end()) { | 338 if (it != handler_map.end()) { |
| 225 it->second.Run( | 339 it->second.Run( |
| 226 static_cast<const InstructionWithLabel*>(instruction)->label()); | 340 static_cast<const InstructionWithLabel*>(instruction)->label()); |
| 227 } | 341 } |
| 228 } | 342 } |
| 229 } | 343 } |
| 230 | 344 |
| 345 CheckBool AssemblyProgram::CreateInstructionCountReceptor( | |
|
grt (UTC plus 2)
2016/11/04 09:38:53
why CheckBool rather than returning a pointer sinc
huangs
2016/11/04 18:20:39
For symmetry with CreateInstructionStoreReceptor()
grt (UTC plus 2)
2016/11/07 09:36:17
Ah. May fail in the future in a different CL. I'm
huangs
2016/11/07 19:46:52
Okay, thanks!
| |
| 346 InstructionCountReceptor** ret) { | |
| 347 DCHECK(ret); | |
| 348 DCHECK(!count_receptor_); | |
| 349 count_receptor_.reset(new InstructionCountReceptor); | |
| 350 *ret = count_receptor_.get(); | |
| 351 return true; | |
| 352 } | |
| 353 | |
| 354 CheckBool AssemblyProgram::CreateInstructionStoreReceptor( | |
| 355 InstructionStoreReceptor** ret) { | |
| 356 DCHECK(ret); | |
| 357 DCHECK(count_receptor_); | |
| 358 DCHECK(!store_receptor_); | |
| 359 store_receptor_.reset( | |
| 360 new InstructionStoreReceptor(count_receptor_->size(), this)); | |
| 361 *ret = store_receptor_.get(); | |
| 362 return true; | |
| 363 } | |
| 364 | |
| 231 CheckBool AssemblyProgram::Emit(ScopedInstruction instruction) { | 365 CheckBool AssemblyProgram::Emit(ScopedInstruction instruction) { |
| 232 if (!instruction || !instructions_.push_back(instruction.get())) | 366 if (!instruction || !instructions_.push_back(instruction.get())) |
| 233 return false; | 367 return false; |
| 234 // Ownership successfully passed to instructions_. | 368 // Ownership successfully passed to instructions_. |
| 235 ignore_result(instruction.release()); | 369 ignore_result(instruction.release()); |
| 236 return true; | 370 return true; |
| 237 } | 371 } |
| 238 | 372 |
| 239 CheckBool AssemblyProgram::EmitShared(Instruction* instruction) { | 373 CheckBool AssemblyProgram::EmitShared(Instruction* instruction) { |
| 240 DCHECK(!instruction || instruction->op() == DEFBYTE); | 374 DCHECK(!instruction || instruction->op() == DEFBYTE); |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 467 Status Encode(const AssemblyProgram& program, | 601 Status Encode(const AssemblyProgram& program, |
| 468 std::unique_ptr<EncodedProgram>* output) { | 602 std::unique_ptr<EncodedProgram>* output) { |
| 469 // Explicitly release any memory associated with the output before encoding. | 603 // Explicitly release any memory associated with the output before encoding. |
| 470 output->reset(); | 604 output->reset(); |
| 471 | 605 |
| 472 *output = program.Encode(); | 606 *output = program.Encode(); |
| 473 return (*output) ? C_OK : C_GENERAL_ERROR; | 607 return (*output) ? C_OK : C_GENERAL_ERROR; |
| 474 } | 608 } |
| 475 | 609 |
| 476 } // namespace courgette | 610 } // namespace courgette |
| OLD | NEW |