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