blob: 7441daccfecb9b760f966b305e9ff90250329fc8 [file] [log] [blame]
Mathieu Chartier193bad92013-08-29 18:46:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "compiled_method.h"
18#include "driver/compiler_driver.h"
19
20namespace art {
21
22CompiledCode::CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
Dave Allisond6ed6422014-04-09 23:36:15 +000023 const std::vector<uint8_t>& quick_code)
Ian Rogersef7d42f2014-01-06 12:55:46 -080024 : compiler_driver_(compiler_driver), instruction_set_(instruction_set),
Dave Allisond6ed6422014-04-09 23:36:15 +000025 portable_code_(nullptr), quick_code_(nullptr) {
Ian Rogersef7d42f2014-01-06 12:55:46 -080026 SetCode(&quick_code, nullptr);
Mathieu Chartier193bad92013-08-29 18:46:00 -070027}
28
29CompiledCode::CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
30 const std::string& elf_object, const std::string& symbol)
Ian Rogersef7d42f2014-01-06 12:55:46 -080031 : compiler_driver_(compiler_driver), instruction_set_(instruction_set),
Dave Allisond6ed6422014-04-09 23:36:15 +000032 portable_code_(nullptr), quick_code_(nullptr), symbol_(symbol) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070033 CHECK_NE(elf_object.size(), 0U);
34 CHECK_NE(symbol.size(), 0U);
35 std::vector<uint8_t> temp_code(elf_object.size());
36 for (size_t i = 0; i < elf_object.size(); ++i) {
37 temp_code[i] = elf_object[i];
38 }
39 // TODO: we shouldn't just shove ELF objects in as "code" but
40 // change to have different kinds of compiled methods. This is
41 // being deferred until we work on hybrid execution or at least
42 // until we work on batch compilation.
Ian Rogersef7d42f2014-01-06 12:55:46 -080043 SetCode(nullptr, &temp_code);
Mathieu Chartier193bad92013-08-29 18:46:00 -070044}
45
Ian Rogersef7d42f2014-01-06 12:55:46 -080046void CompiledCode::SetCode(const std::vector<uint8_t>* quick_code,
47 const std::vector<uint8_t>* portable_code) {
48 if (portable_code != nullptr) {
49 CHECK(!portable_code->empty());
50 portable_code_ = compiler_driver_->DeduplicateCode(*portable_code);
51 }
52 if (quick_code != nullptr) {
53 CHECK(!quick_code->empty());
54 quick_code_ = compiler_driver_->DeduplicateCode(*quick_code);
55 }
56}
57
58bool CompiledCode::operator==(const CompiledCode& rhs) const {
59 if (quick_code_ != nullptr) {
60 if (rhs.quick_code_ == nullptr) {
61 return false;
62 } else if (quick_code_->size() != rhs.quick_code_->size()) {
63 return false;
64 } else {
65 return std::equal(quick_code_->begin(), quick_code_->end(), rhs.quick_code_->begin());
66 }
67 } else if (portable_code_ != nullptr) {
68 if (rhs.portable_code_ == nullptr) {
69 return false;
70 } else if (portable_code_->size() != rhs.portable_code_->size()) {
71 return false;
72 } else {
73 return std::equal(portable_code_->begin(), portable_code_->end(),
74 rhs.portable_code_->begin());
75 }
76 }
77 return (rhs.quick_code_ == nullptr) && (rhs.portable_code_ == nullptr);
Mathieu Chartier193bad92013-08-29 18:46:00 -070078}
79
80uint32_t CompiledCode::AlignCode(uint32_t offset) const {
81 return AlignCode(offset, instruction_set_);
82}
83
84uint32_t CompiledCode::AlignCode(uint32_t offset, InstructionSet instruction_set) {
Andreas Gampeaf13ad92014-04-11 12:07:48 -070085 return RoundUp(offset, GetInstructionSetAlignment(instruction_set));
Mathieu Chartier193bad92013-08-29 18:46:00 -070086}
87
88size_t CompiledCode::CodeDelta() const {
89 switch (instruction_set_) {
90 case kArm:
Stuart Monteithb95a5342014-03-12 13:32:32 +000091 case kArm64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070092 case kMips:
93 case kX86:
Dmitry Petrochenkofca82202014-03-21 11:21:37 +070094 case kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -070095 return 0;
96 case kThumb2: {
97 // +1 to set the low-order bit so a BLX will switch to Thumb mode
98 return 1;
99 }
100 default:
101 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
102 return 0;
103 }
104}
105
106const void* CompiledCode::CodePointer(const void* code_pointer,
107 InstructionSet instruction_set) {
108 switch (instruction_set) {
109 case kArm:
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 case kArm64:
Mathieu Chartier193bad92013-08-29 18:46:00 -0700111 case kMips:
112 case kX86:
Dmitry Petrochenkofca82202014-03-21 11:21:37 +0700113 case kX86_64:
Mathieu Chartier193bad92013-08-29 18:46:00 -0700114 return code_pointer;
115 case kThumb2: {
116 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
117 // Set the low-order bit so a BLX will switch to Thumb mode
118 address |= 0x1;
119 return reinterpret_cast<const void*>(address);
120 }
121 default:
122 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
123 return NULL;
124 }
125}
126
Mathieu Chartier193bad92013-08-29 18:46:00 -0700127const std::string& CompiledCode::GetSymbol() const {
128 CHECK_NE(0U, symbol_.size());
129 return symbol_;
130}
131
132const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const {
133 CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size()) << symbol_;
134 return oatdata_offsets_to_compiled_code_offset_;
135}
136
137void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) {
138 oatdata_offsets_to_compiled_code_offset_.push_back(offset);
139}
Mathieu Chartier193bad92013-08-29 18:46:00 -0700140
Ian Rogers72d32622014-05-06 16:20:11 -0700141CompiledMethod::CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700142 InstructionSet instruction_set,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800143 const std::vector<uint8_t>& quick_code,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700144 const size_t frame_size_in_bytes,
145 const uint32_t core_spill_mask,
146 const uint32_t fp_spill_mask,
147 const std::vector<uint8_t>& mapping_table,
148 const std::vector<uint8_t>& vmap_table,
Mark Mendellae9fd932014-02-10 16:14:35 -0800149 const std::vector<uint8_t>& native_gc_map,
Dave Allisond6ed6422014-04-09 23:36:15 +0000150 const std::vector<uint8_t>* cfi_info)
Ian Rogers72d32622014-05-06 16:20:11 -0700151 : CompiledCode(driver, instruction_set, quick_code), frame_size_in_bytes_(frame_size_in_bytes),
Mathieu Chartier193bad92013-08-29 18:46:00 -0700152 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
Ian Rogers72d32622014-05-06 16:20:11 -0700153 mapping_table_(driver->DeduplicateMappingTable(mapping_table)),
154 vmap_table_(driver->DeduplicateVMapTable(vmap_table)),
155 gc_map_(driver->DeduplicateGCMap(native_gc_map)),
156 cfi_info_(driver->DeduplicateCFIInfo(cfi_info)) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700157}
158
Ian Rogers72d32622014-05-06 16:20:11 -0700159CompiledMethod::CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700160 InstructionSet instruction_set,
161 const std::vector<uint8_t>& code,
162 const size_t frame_size_in_bytes,
163 const uint32_t core_spill_mask,
164 const uint32_t fp_spill_mask)
Ian Rogers72d32622014-05-06 16:20:11 -0700165 : CompiledCode(driver, instruction_set, code),
Mathieu Chartier193bad92013-08-29 18:46:00 -0700166 frame_size_in_bytes_(frame_size_in_bytes),
Mark Mendellae9fd932014-02-10 16:14:35 -0800167 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
Ian Rogers72d32622014-05-06 16:20:11 -0700168 mapping_table_(driver->DeduplicateMappingTable(std::vector<uint8_t>())),
169 vmap_table_(driver->DeduplicateVMapTable(std::vector<uint8_t>())),
170 gc_map_(driver->DeduplicateGCMap(std::vector<uint8_t>())),
Mark Mendellae9fd932014-02-10 16:14:35 -0800171 cfi_info_(nullptr) {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700172}
173
174// Constructs a CompiledMethod for the Portable compiler.
Ian Rogers72d32622014-05-06 16:20:11 -0700175CompiledMethod::CompiledMethod(CompilerDriver* driver, InstructionSet instruction_set,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700176 const std::string& code, const std::vector<uint8_t>& gc_map,
177 const std::string& symbol)
Ian Rogers72d32622014-05-06 16:20:11 -0700178 : CompiledCode(driver, instruction_set, code, symbol),
Mathieu Chartier193bad92013-08-29 18:46:00 -0700179 frame_size_in_bytes_(kStackAlignment), core_spill_mask_(0),
Ian Rogers72d32622014-05-06 16:20:11 -0700180 fp_spill_mask_(0), gc_map_(driver->DeduplicateGCMap(gc_map)) {
181 mapping_table_ = driver->DeduplicateMappingTable(std::vector<uint8_t>());
182 vmap_table_ = driver->DeduplicateVMapTable(std::vector<uint8_t>());
Mathieu Chartier193bad92013-08-29 18:46:00 -0700183}
184
Ian Rogers72d32622014-05-06 16:20:11 -0700185CompiledMethod::CompiledMethod(CompilerDriver* driver, InstructionSet instruction_set,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700186 const std::string& code, const std::string& symbol)
Ian Rogers72d32622014-05-06 16:20:11 -0700187 : CompiledCode(driver, instruction_set, code, symbol),
Mathieu Chartier193bad92013-08-29 18:46:00 -0700188 frame_size_in_bytes_(kStackAlignment), core_spill_mask_(0),
189 fp_spill_mask_(0) {
Ian Rogers72d32622014-05-06 16:20:11 -0700190 mapping_table_ = driver->DeduplicateMappingTable(std::vector<uint8_t>());
191 vmap_table_ = driver->DeduplicateVMapTable(std::vector<uint8_t>());
192 gc_map_ = driver->DeduplicateGCMap(std::vector<uint8_t>());
Mathieu Chartier193bad92013-08-29 18:46:00 -0700193}
Dave Allisond6ed6422014-04-09 23:36:15 +0000194
Mathieu Chartier193bad92013-08-29 18:46:00 -0700195} // namespace art