Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: courgette/disassembler.cc

Issue 2771753004: [Courgette] Refactor: Unify Disassembler::Disassemble() and instantiate AssemblyProgram there. (Closed)
Patch Set: Fix signed/unsigned comparison issue in test. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « courgette/disassembler.h ('k') | courgette/disassembler_elf_32.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/disassembler.h" 5 #include "courgette/disassembler.h"
6 6
7 #include <memory>
8
9 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h"
10 #include "courgette/assembly_program.h" 9 #include "courgette/assembly_program.h"
11 10
12 namespace courgette { 11 namespace courgette {
13 12
14 Disassembler::RvaVisitor_Abs32::RvaVisitor_Abs32( 13 Disassembler::RvaVisitor_Abs32::RvaVisitor_Abs32(
15 const std::vector<RVA>& rva_locations, 14 const std::vector<RVA>& rva_locations,
16 const AddressTranslator& translator) 15 const AddressTranslator& translator)
17 : VectorRvaVisitor<RVA>(rva_locations), translator_(translator) { 16 : VectorRvaVisitor<RVA>(rva_locations), translator_(translator) {
18 } 17 }
19 18
(...skipping 11 matching lines...) Expand all
31 RVA Disassembler::RvaVisitor_Rel32::Get() const { 30 RVA Disassembler::RvaVisitor_Rel32::Get() const {
32 // For Rel32 targets, only handle 32-bit offsets. 31 // For Rel32 targets, only handle 32-bit offsets.
33 return *it_ + 4 + Read32LittleEndian(translator_.RVAToPointer(*it_)); 32 return *it_ + 4 + Read32LittleEndian(translator_.RVAToPointer(*it_));
34 } 33 }
35 34
36 Disassembler::Disassembler(const uint8_t* start, size_t length) 35 Disassembler::Disassembler(const uint8_t* start, size_t length)
37 : failure_reason_("uninitialized") { 36 : failure_reason_("uninitialized") {
38 start_ = start; 37 start_ = start;
39 length_ = length; 38 length_ = length;
40 end_ = start_ + length_; 39 end_ = start_ + length_;
41 }; 40 }
42 41
43 Disassembler::~Disassembler() {}; 42 Disassembler::~Disassembler() {}
44 43
45 const uint8_t* Disassembler::FileOffsetToPointer(FileOffset file_offset) const { 44 const uint8_t* Disassembler::FileOffsetToPointer(FileOffset file_offset) const {
46 CHECK_LE(file_offset, static_cast<FileOffset>(end_ - start_)); 45 CHECK_LE(file_offset, static_cast<FileOffset>(end_ - start_));
47 return start_ + file_offset; 46 return start_ + file_offset;
48 } 47 }
49 48
50 const uint8_t* Disassembler::RVAToPointer(RVA rva) const { 49 const uint8_t* Disassembler::RVAToPointer(RVA rva) const {
51 FileOffset file_offset = RVAToFileOffset(rva); 50 FileOffset file_offset = RVAToFileOffset(rva);
52 if (file_offset == kNoFileOffset) 51 if (file_offset == kNoFileOffset)
53 return nullptr; 52 return nullptr;
54 53
55 return FileOffsetToPointer(file_offset); 54 return FileOffsetToPointer(file_offset);
56 } 55 }
57 56
57 std::unique_ptr<AssemblyProgram> Disassembler::Disassemble() {
58 if (!ok() || !ExtractAbs32Locations() || !ExtractRel32Locations())
59 return nullptr;
60
61 std::unique_ptr<AssemblyProgram> program =
62 base::MakeUnique<AssemblyProgram>(kind(), image_base());
63
64 PrecomputeLabels(program.get());
65 RemoveUnusedRel32Locations(program.get());
66
67 if (!program->GenerateInstructions(GetInstructionGenerator(program.get())))
68 return nullptr;
69
70 program->DefaultAssignIndexes();
71 return program;
72 }
73
58 bool Disassembler::Good() { 74 bool Disassembler::Good() {
59 failure_reason_ = nullptr; 75 failure_reason_ = nullptr;
60 return true; 76 return true;
61 } 77 }
62 78
63 bool Disassembler::Bad(const char* reason) { 79 bool Disassembler::Bad(const char* reason) {
64 failure_reason_ = reason; 80 failure_reason_ = reason;
65 return false; 81 return false;
66 } 82 }
67 83
68 void Disassembler::PrecomputeLabels(AssemblyProgram* program) { 84 void Disassembler::PrecomputeLabels(AssemblyProgram* program) {
69 std::unique_ptr<RvaVisitor> abs32_visitor(CreateAbs32TargetRvaVisitor()); 85 std::unique_ptr<RvaVisitor> abs32_visitor(CreateAbs32TargetRvaVisitor());
70 std::unique_ptr<RvaVisitor> rel32_visitor(CreateRel32TargetRvaVisitor()); 86 std::unique_ptr<RvaVisitor> rel32_visitor(CreateRel32TargetRvaVisitor());
71 program->PrecomputeLabels(abs32_visitor.get(), rel32_visitor.get()); 87 program->PrecomputeLabels(abs32_visitor.get(), rel32_visitor.get());
72 } 88 }
73 89
74 void Disassembler::ReduceLength(size_t reduced_length) { 90 void Disassembler::ReduceLength(size_t reduced_length) {
75 CHECK_LE(reduced_length, length_); 91 CHECK_LE(reduced_length, length_);
76 length_ = reduced_length; 92 length_ = reduced_length;
77 end_ = start_ + length_; 93 end_ = start_ + length_;
78 } 94 }
79 95
80 } // namespace courgette 96 } // namespace courgette
OLDNEW
« no previous file with comments | « courgette/disassembler.h ('k') | courgette/disassembler_elf_32.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698