Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "code_generator.h" |
| 18 | |
| 19 | #include "code_generator_arm.h" |
| 20 | #include "code_generator_x86.h" |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 21 | #include "code_generator_x86_64.h" |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 22 | #include "compiled_method.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 23 | #include "dex/verified_method.h" |
| 24 | #include "driver/dex_compilation_unit.h" |
| 25 | #include "gc_map_builder.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 26 | #include "leb128.h" |
| 27 | #include "mapping_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 28 | #include "utils/assembler.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 29 | #include "verifier/dex_gc_map.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 30 | #include "vmap_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 34 | void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 35 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 36 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 37 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 38 | block_labels_.SetSize(blocks.Size()); |
| 39 | |
| 40 | DCHECK_EQ(frame_size_, kUninitializedFrameSize); |
Nicolas Geoffray | 73e80c3 | 2014-07-22 17:47:56 +0100 | [diff] [blame] | 41 | if (!is_leaf) { |
| 42 | MarkNotLeaf(); |
| 43 | } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 44 | ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs() |
| 45 | + GetGraph()->GetNumberOfTemporaries() |
| 46 | + 1 /* filler */, |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 47 | 0, /* the baseline compiler does not have live registers at slow path */ |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 48 | GetGraph()->GetMaximumNumberOfOutVRegs() |
| 49 | + 1 /* current method */); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 50 | GenerateFrameEntry(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 51 | |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 52 | HGraphVisitor* location_builder = GetLocationBuilder(); |
| 53 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 54 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 55 | HBasicBlock* block = blocks.Get(i); |
| 56 | Bind(GetLabelOf(block)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 57 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 58 | HInstruction* current = it.Current(); |
| 59 | current->Accept(location_builder); |
| 60 | InitLocations(current); |
| 61 | current->Accept(instruction_visitor); |
| 62 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 63 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 64 | GenerateSlowPaths(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 65 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 66 | size_t code_size = GetAssembler()->CodeSize(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 67 | uint8_t* buffer = allocator->Allocate(code_size); |
| 68 | MemoryRegion code(buffer, code_size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 69 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 72 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
| 73 | // The frame size has already been computed during register allocation. |
| 74 | DCHECK_NE(frame_size_, kUninitializedFrameSize); |
| 75 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 76 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 77 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
| 78 | block_labels_.SetSize(blocks.Size()); |
| 79 | |
| 80 | GenerateFrameEntry(); |
Nicolas Geoffray | 8a16d97 | 2014-09-11 10:30:02 +0100 | [diff] [blame] | 81 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 82 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
| 83 | HBasicBlock* block = blocks.Get(i); |
| 84 | Bind(GetLabelOf(block)); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 85 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 86 | HInstruction* current = it.Current(); |
| 87 | current->Accept(instruction_visitor); |
| 88 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 89 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 90 | GenerateSlowPaths(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 91 | |
| 92 | size_t code_size = GetAssembler()->CodeSize(); |
| 93 | uint8_t* buffer = allocator->Allocate(code_size); |
| 94 | MemoryRegion code(buffer, code_size); |
| 95 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 98 | void CodeGenerator::GenerateSlowPaths() { |
| 99 | for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) { |
| 100 | slow_paths_.Get(i)->EmitNativeCode(this); |
| 101 | } |
| 102 | } |
| 103 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 104 | size_t CodeGenerator::AllocateFreeRegisterInternal( |
| 105 | bool* blocked_registers, size_t number_of_registers) const { |
| 106 | for (size_t regno = 0; regno < number_of_registers; regno++) { |
| 107 | if (!blocked_registers[regno]) { |
| 108 | blocked_registers[regno] = true; |
| 109 | return regno; |
| 110 | } |
| 111 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 115 | void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots, |
| 116 | size_t maximum_number_of_live_registers, |
| 117 | size_t number_of_out_slots) { |
| 118 | first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize; |
| 119 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 120 | SetFrameSize(RoundUp( |
| 121 | number_of_spill_slots * kVRegSize |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 122 | + number_of_out_slots * kVRegSize |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 123 | + maximum_number_of_live_registers * GetWordSize() |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame] | 124 | + FrameEntrySpillSize(), |
| 125 | kStackAlignment)); |
| 126 | } |
| 127 | |
| 128 | Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const { |
| 129 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 130 | // Use the temporary region (right below the dex registers). |
| 131 | int32_t slot = GetFrameSize() - FrameEntrySpillSize() |
| 132 | - kVRegSize // filler |
| 133 | - (number_of_locals * kVRegSize) |
| 134 | - ((1 + temp->GetIndex()) * kVRegSize); |
| 135 | return Location::StackSlot(slot); |
| 136 | } |
| 137 | |
| 138 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 139 | uint16_t reg_number = local->GetRegNumber(); |
| 140 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 141 | if (reg_number >= number_of_locals) { |
| 142 | // Local is a parameter of the method. It is stored in the caller's frame. |
| 143 | return GetFrameSize() + kVRegSize // ART method |
| 144 | + (reg_number - number_of_locals) * kVRegSize; |
| 145 | } else { |
| 146 | // Local is a temporary in this method. It is stored in this method's frame. |
| 147 | return GetFrameSize() - FrameEntrySpillSize() |
| 148 | - kVRegSize // filler. |
| 149 | - (number_of_locals * kVRegSize) |
| 150 | + (reg_number * kVRegSize); |
| 151 | } |
| 152 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 153 | |
| 154 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 155 | LocationSummary* locations = instruction->GetLocations(); |
| 156 | if (locations == nullptr) return; |
| 157 | |
| 158 | for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) { |
| 159 | blocked_registers_[i] = false; |
| 160 | } |
| 161 | |
| 162 | // Mark all fixed input, temp and output registers as used. |
| 163 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 164 | Location loc = locations->InAt(i); |
| 165 | if (loc.IsRegister()) { |
| 166 | // Check that a register is not specified twice in the summary. |
| 167 | DCHECK(!blocked_registers_[loc.GetEncoding()]); |
| 168 | blocked_registers_[loc.GetEncoding()] = true; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 173 | Location loc = locations->GetTemp(i); |
| 174 | if (loc.IsRegister()) { |
| 175 | // Check that a register is not specified twice in the summary. |
| 176 | DCHECK(!blocked_registers_[loc.GetEncoding()]); |
| 177 | blocked_registers_[loc.GetEncoding()] = true; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | SetupBlockedRegisters(blocked_registers_); |
| 182 | |
| 183 | // Allocate all unallocated input locations. |
| 184 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 185 | Location loc = locations->InAt(i); |
| 186 | HInstruction* input = instruction->InputAt(i); |
| 187 | if (loc.IsUnallocated()) { |
| 188 | if (loc.GetPolicy() == Location::kRequiresRegister) { |
| 189 | loc = Location::RegisterLocation( |
| 190 | AllocateFreeRegister(input->GetType(), blocked_registers_)); |
| 191 | } else { |
| 192 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 193 | HLoadLocal* load = input->AsLoadLocal(); |
| 194 | if (load != nullptr) { |
| 195 | loc = GetStackLocation(load); |
| 196 | } else { |
| 197 | loc = Location::RegisterLocation( |
| 198 | AllocateFreeRegister(input->GetType(), blocked_registers_)); |
| 199 | } |
| 200 | } |
| 201 | locations->SetInAt(i, loc); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Allocate all unallocated temp locations. |
| 206 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 207 | Location loc = locations->GetTemp(i); |
| 208 | if (loc.IsUnallocated()) { |
| 209 | DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister); |
| 210 | // TODO: Adjust handling of temps. We currently consider temps to use |
| 211 | // core registers. They may also use floating point registers at some point. |
| 212 | loc = Location::RegisterLocation(static_cast<ManagedRegister>( |
| 213 | AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_))); |
| 214 | locations->SetTempAt(i, loc); |
| 215 | } |
| 216 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 217 | Location result_location = locations->Out(); |
| 218 | if (result_location.IsUnallocated()) { |
| 219 | switch (result_location.GetPolicy()) { |
| 220 | case Location::kAny: |
| 221 | case Location::kRequiresRegister: |
| 222 | result_location = Location::RegisterLocation( |
| 223 | AllocateFreeRegister(instruction->GetType(), blocked_registers_)); |
| 224 | break; |
| 225 | case Location::kSameAsFirstInput: |
| 226 | result_location = locations->InAt(0); |
| 227 | break; |
| 228 | } |
| 229 | locations->SetOut(result_location); |
| 230 | } |
| 231 | } |
| 232 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 233 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 234 | if (instruction->GetLocations() == nullptr) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 235 | if (instruction->IsTemporary()) { |
| 236 | HInstruction* previous = instruction->GetPrevious(); |
| 237 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 238 | Move(previous, temp_location, instruction); |
| 239 | previous->GetLocations()->SetOut(temp_location); |
| 240 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | AllocateRegistersLocally(instruction); |
| 244 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 245 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 246 | if (location.IsValid()) { |
| 247 | // Move the input to the desired location. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 248 | Move(instruction->InputAt(i), location, instruction); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 254 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 255 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 259 | return block_labels_.GetRawStorage() + block->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 262 | CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator, |
| 263 | HGraph* graph, |
| 264 | InstructionSet instruction_set) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 265 | switch (instruction_set) { |
| 266 | case kArm: |
| 267 | case kThumb2: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 268 | return new (allocator) arm::CodeGeneratorARM(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 269 | } |
| 270 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 271 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 272 | case kX86: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 273 | return new (allocator) x86::CodeGeneratorX86(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 274 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 275 | case kX86_64: { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 276 | return new (allocator) x86_64::CodeGeneratorX86_64(graph); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 277 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 278 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 279 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 283 | void CodeGenerator::BuildNativeGCMap( |
| 284 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 285 | const std::vector<uint8_t>& gc_map_raw = |
| 286 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 287 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 288 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 289 | uint32_t max_native_offset = 0; |
| 290 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 291 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 292 | if (native_offset > max_native_offset) { |
| 293 | max_native_offset = native_offset; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 298 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 299 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 300 | uint32_t native_offset = pc_info.native_pc; |
| 301 | uint32_t dex_pc = pc_info.dex_pc; |
| 302 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 303 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 304 | builder.AddEntry(native_offset, references); |
| 305 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 308 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, SrcMap* src_map) const { |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 309 | uint32_t pc2dex_data_size = 0u; |
| 310 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 311 | uint32_t pc2dex_offset = 0u; |
| 312 | int32_t pc2dex_dalvik_offset = 0; |
| 313 | uint32_t dex2pc_data_size = 0u; |
| 314 | uint32_t dex2pc_entries = 0u; |
| 315 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 316 | if (src_map != nullptr) { |
| 317 | src_map->reserve(pc2dex_entries); |
| 318 | } |
| 319 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 320 | // We currently only have pc2dex entries. |
| 321 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 322 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 323 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 324 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 325 | pc2dex_offset = pc_info.native_pc; |
| 326 | pc2dex_dalvik_offset = pc_info.dex_pc; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 327 | if (src_map != nullptr) { |
| 328 | src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset})); |
| 329 | } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 333 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 334 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 335 | data->resize(data_size); |
| 336 | |
| 337 | uint8_t* data_ptr = &(*data)[0]; |
| 338 | uint8_t* write_pos = data_ptr; |
| 339 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 340 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 341 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 342 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 343 | |
| 344 | pc2dex_offset = 0u; |
| 345 | pc2dex_dalvik_offset = 0u; |
| 346 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 347 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 348 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 349 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 350 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 351 | pc2dex_offset = pc_info.native_pc; |
| 352 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 353 | } |
| 354 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 355 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 356 | |
| 357 | if (kIsDebugBuild) { |
| 358 | // Verify the encoded table holds the expected data. |
| 359 | MappingTable table(data_ptr); |
| 360 | CHECK_EQ(table.TotalSize(), total_entries); |
| 361 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 362 | auto it = table.PcToDexBegin(); |
| 363 | auto it2 = table.DexToPcBegin(); |
| 364 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 365 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 366 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 367 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 368 | ++it; |
| 369 | } |
| 370 | CHECK(it == table.PcToDexEnd()); |
| 371 | CHECK(it2 == table.DexToPcEnd()); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 376 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 377 | // We currently don't use callee-saved registers. |
| 378 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 379 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 380 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 381 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 382 | |
| 383 | *data = vmap_encoder.GetData(); |
| 384 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 385 | |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 386 | void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) { |
| 387 | uint32_t size = stack_map_stream_.ComputeNeededSize(); |
| 388 | data->resize(size); |
| 389 | MemoryRegion region(data->data(), size); |
| 390 | stack_map_stream_.FillIn(region); |
| 391 | } |
| 392 | |
| 393 | void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) { |
| 394 | // Collect PC infos for the mapping table. |
| 395 | struct PcInfo pc_info; |
| 396 | pc_info.dex_pc = dex_pc; |
| 397 | pc_info.native_pc = GetAssembler()->CodeSize(); |
| 398 | pc_infos_.Add(pc_info); |
| 399 | |
| 400 | // Populate stack map information. |
| 401 | |
| 402 | if (instruction == nullptr) { |
| 403 | // For stack overflow checks. |
| 404 | stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | LocationSummary* locations = instruction->GetLocations(); |
| 409 | HEnvironment* environment = instruction->GetEnvironment(); |
| 410 | |
| 411 | size_t environment_size = instruction->EnvironmentSize(); |
| 412 | |
| 413 | size_t register_mask = 0; |
| 414 | size_t inlining_depth = 0; |
| 415 | stack_map_stream_.AddStackMapEntry( |
| 416 | dex_pc, pc_info.native_pc, register_mask, |
| 417 | locations->GetStackMask(), environment_size, inlining_depth); |
| 418 | |
| 419 | // Walk over the environment, and record the location of dex registers. |
| 420 | for (size_t i = 0; i < environment_size; ++i) { |
| 421 | HInstruction* current = environment->GetInstructionAt(i); |
| 422 | if (current == nullptr) { |
| 423 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0); |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | Location location = locations->GetEnvironmentAt(i); |
| 428 | switch (location.GetKind()) { |
| 429 | case Location::kConstant: { |
| 430 | DCHECK(current == location.GetConstant()); |
| 431 | if (current->IsLongConstant()) { |
| 432 | int64_t value = current->AsLongConstant()->GetValue(); |
| 433 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value)); |
| 434 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value)); |
| 435 | ++i; |
| 436 | DCHECK_LT(i, environment_size); |
| 437 | } else { |
| 438 | DCHECK(current->IsIntConstant()); |
| 439 | int32_t value = current->AsIntConstant()->GetValue(); |
| 440 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value); |
| 441 | } |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | case Location::kStackSlot: { |
| 446 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 447 | break; |
| 448 | } |
| 449 | |
| 450 | case Location::kDoubleStackSlot: { |
| 451 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex()); |
| 452 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, |
| 453 | location.GetHighStackIndex(kVRegSize)); |
| 454 | ++i; |
| 455 | DCHECK_LT(i, environment_size); |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | case Location::kRegister : { |
| 460 | int id = location.reg().RegId(); |
| 461 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
| 462 | if (current->GetType() == Primitive::kPrimDouble |
| 463 | || current->GetType() == Primitive::kPrimLong) { |
| 464 | stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id); |
| 465 | ++i; |
| 466 | DCHECK_LT(i, environment_size); |
| 467 | } |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | default: |
| 472 | LOG(FATAL) << "Unexpected kind " << location.GetKind(); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 477 | size_t CodeGenerator::GetStackOffsetOfSavedRegister(size_t index) { |
| 478 | return first_register_slot_in_slow_path_ + index * GetWordSize(); |
| 479 | } |
| 480 | |
| 481 | void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) { |
| 482 | RegisterSet* register_set = locations->GetLiveRegisters(); |
| 483 | uint32_t count = 0; |
| 484 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 485 | if (register_set->ContainsCoreRegister(i)) { |
| 486 | size_t stack_offset = GetStackOffsetOfSavedRegister(count); |
| 487 | ++count; |
| 488 | SaveCoreRegister(Location::StackSlot(stack_offset), i); |
| 489 | // If the register holds an object, update the stack mask. |
| 490 | if (locations->RegisterContainsObject(i)) { |
| 491 | locations->SetStackBit(stack_offset / kVRegSize); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 497 | if (register_set->ContainsFloatingPointRegister(i)) { |
| 498 | LOG(FATAL) << "Unimplemented"; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) { |
| 504 | RegisterSet* register_set = locations->GetLiveRegisters(); |
| 505 | uint32_t count = 0; |
| 506 | for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) { |
| 507 | if (register_set->ContainsCoreRegister(i)) { |
| 508 | size_t stack_offset = GetStackOffsetOfSavedRegister(count); |
| 509 | ++count; |
| 510 | RestoreCoreRegister(Location::StackSlot(stack_offset), i); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) { |
| 515 | if (register_set->ContainsFloatingPointRegister(i)) { |
| 516 | LOG(FATAL) << "Unimplemented"; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 521 | } // namespace art |