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" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 22 | #include "dex/verified_method.h" |
| 23 | #include "driver/dex_compilation_unit.h" |
| 24 | #include "gc_map_builder.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 25 | #include "leb128.h" |
| 26 | #include "mapping_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 27 | #include "utils/assembler.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 28 | #include "verifier/dex_gc_map.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 29 | #include "vmap_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 33 | void CodeGenerator::CompileBaseline(CodeAllocator* allocator) { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 34 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 35 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 36 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 37 | block_labels_.SetSize(blocks.Size()); |
| 38 | |
| 39 | DCHECK_EQ(frame_size_, kUninitializedFrameSize); |
| 40 | ComputeFrameSize(GetGraph()->GetMaximumNumberOfOutVRegs() |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame^] | 41 | + GetGraph()->GetNumberOfLocalVRegs() |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 42 | + GetGraph()->GetNumberOfTemporaries() |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 43 | + 1 /* filler */); |
Nicolas Geoffray | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 44 | GenerateFrameEntry(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 45 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 46 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 47 | HBasicBlock* block = blocks.Get(i); |
| 48 | Bind(GetLabelOf(block)); |
| 49 | HGraphVisitor* location_builder = GetLocationBuilder(); |
| 50 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
| 51 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 52 | HInstruction* current = it.Current(); |
| 53 | current->Accept(location_builder); |
| 54 | InitLocations(current); |
| 55 | current->Accept(instruction_visitor); |
| 56 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 57 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 58 | GenerateSlowPaths(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 59 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 60 | size_t code_size = GetAssembler()->CodeSize(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 61 | uint8_t* buffer = allocator->Allocate(code_size); |
| 62 | MemoryRegion code(buffer, code_size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 63 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 66 | void CodeGenerator::CompileOptimized(CodeAllocator* allocator) { |
| 67 | // The frame size has already been computed during register allocation. |
| 68 | DCHECK_NE(frame_size_, kUninitializedFrameSize); |
| 69 | const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks(); |
| 70 | DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock()); |
| 71 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1))); |
| 72 | block_labels_.SetSize(blocks.Size()); |
| 73 | |
| 74 | GenerateFrameEntry(); |
| 75 | for (size_t i = 0, e = blocks.Size(); i < e; ++i) { |
| 76 | HBasicBlock* block = blocks.Get(i); |
| 77 | Bind(GetLabelOf(block)); |
| 78 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
| 79 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 80 | HInstruction* current = it.Current(); |
| 81 | current->Accept(instruction_visitor); |
| 82 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 83 | } |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 84 | GenerateSlowPaths(); |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 85 | |
| 86 | size_t code_size = GetAssembler()->CodeSize(); |
| 87 | uint8_t* buffer = allocator->Allocate(code_size); |
| 88 | MemoryRegion code(buffer, code_size); |
| 89 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 92 | void CodeGenerator::GenerateSlowPaths() { |
| 93 | for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) { |
| 94 | slow_paths_.Get(i)->EmitNativeCode(this); |
| 95 | } |
| 96 | } |
| 97 | |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 98 | size_t CodeGenerator::AllocateFreeRegisterInternal( |
| 99 | bool* blocked_registers, size_t number_of_registers) const { |
| 100 | for (size_t regno = 0; regno < number_of_registers; regno++) { |
| 101 | if (!blocked_registers[regno]) { |
| 102 | blocked_registers[regno] = true; |
| 103 | return regno; |
| 104 | } |
| 105 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | |
Nicolas Geoffray | ab032bc | 2014-07-15 12:55:21 +0100 | [diff] [blame^] | 109 | void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots) { |
| 110 | SetFrameSize(RoundUp( |
| 111 | number_of_spill_slots * kVRegSize |
| 112 | + kVRegSize // Art method |
| 113 | + FrameEntrySpillSize(), |
| 114 | kStackAlignment)); |
| 115 | } |
| 116 | |
| 117 | Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const { |
| 118 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 119 | // Use the temporary region (right below the dex registers). |
| 120 | int32_t slot = GetFrameSize() - FrameEntrySpillSize() |
| 121 | - kVRegSize // filler |
| 122 | - (number_of_locals * kVRegSize) |
| 123 | - ((1 + temp->GetIndex()) * kVRegSize); |
| 124 | return Location::StackSlot(slot); |
| 125 | } |
| 126 | |
| 127 | int32_t CodeGenerator::GetStackSlot(HLocal* local) const { |
| 128 | uint16_t reg_number = local->GetRegNumber(); |
| 129 | uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs(); |
| 130 | if (reg_number >= number_of_locals) { |
| 131 | // Local is a parameter of the method. It is stored in the caller's frame. |
| 132 | return GetFrameSize() + kVRegSize // ART method |
| 133 | + (reg_number - number_of_locals) * kVRegSize; |
| 134 | } else { |
| 135 | // Local is a temporary in this method. It is stored in this method's frame. |
| 136 | return GetFrameSize() - FrameEntrySpillSize() |
| 137 | - kVRegSize // filler. |
| 138 | - (number_of_locals * kVRegSize) |
| 139 | + (reg_number * kVRegSize); |
| 140 | } |
| 141 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 142 | |
| 143 | void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const { |
| 144 | LocationSummary* locations = instruction->GetLocations(); |
| 145 | if (locations == nullptr) return; |
| 146 | |
| 147 | for (size_t i = 0, e = GetNumberOfRegisters(); i < e; ++i) { |
| 148 | blocked_registers_[i] = false; |
| 149 | } |
| 150 | |
| 151 | // Mark all fixed input, temp and output registers as used. |
| 152 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 153 | Location loc = locations->InAt(i); |
| 154 | if (loc.IsRegister()) { |
| 155 | // Check that a register is not specified twice in the summary. |
| 156 | DCHECK(!blocked_registers_[loc.GetEncoding()]); |
| 157 | blocked_registers_[loc.GetEncoding()] = true; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 162 | Location loc = locations->GetTemp(i); |
| 163 | if (loc.IsRegister()) { |
| 164 | // Check that a register is not specified twice in the summary. |
| 165 | DCHECK(!blocked_registers_[loc.GetEncoding()]); |
| 166 | blocked_registers_[loc.GetEncoding()] = true; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | SetupBlockedRegisters(blocked_registers_); |
| 171 | |
| 172 | // Allocate all unallocated input locations. |
| 173 | for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) { |
| 174 | Location loc = locations->InAt(i); |
| 175 | HInstruction* input = instruction->InputAt(i); |
| 176 | if (loc.IsUnallocated()) { |
| 177 | if (loc.GetPolicy() == Location::kRequiresRegister) { |
| 178 | loc = Location::RegisterLocation( |
| 179 | AllocateFreeRegister(input->GetType(), blocked_registers_)); |
| 180 | } else { |
| 181 | DCHECK_EQ(loc.GetPolicy(), Location::kAny); |
| 182 | HLoadLocal* load = input->AsLoadLocal(); |
| 183 | if (load != nullptr) { |
| 184 | loc = GetStackLocation(load); |
| 185 | } else { |
| 186 | loc = Location::RegisterLocation( |
| 187 | AllocateFreeRegister(input->GetType(), blocked_registers_)); |
| 188 | } |
| 189 | } |
| 190 | locations->SetInAt(i, loc); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Allocate all unallocated temp locations. |
| 195 | for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) { |
| 196 | Location loc = locations->GetTemp(i); |
| 197 | if (loc.IsUnallocated()) { |
| 198 | DCHECK_EQ(loc.GetPolicy(), Location::kRequiresRegister); |
| 199 | // TODO: Adjust handling of temps. We currently consider temps to use |
| 200 | // core registers. They may also use floating point registers at some point. |
| 201 | loc = Location::RegisterLocation(static_cast<ManagedRegister>( |
| 202 | AllocateFreeRegister(Primitive::kPrimInt, blocked_registers_))); |
| 203 | locations->SetTempAt(i, loc); |
| 204 | } |
| 205 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 206 | Location result_location = locations->Out(); |
| 207 | if (result_location.IsUnallocated()) { |
| 208 | switch (result_location.GetPolicy()) { |
| 209 | case Location::kAny: |
| 210 | case Location::kRequiresRegister: |
| 211 | result_location = Location::RegisterLocation( |
| 212 | AllocateFreeRegister(instruction->GetType(), blocked_registers_)); |
| 213 | break; |
| 214 | case Location::kSameAsFirstInput: |
| 215 | result_location = locations->InAt(0); |
| 216 | break; |
| 217 | } |
| 218 | locations->SetOut(result_location); |
| 219 | } |
| 220 | } |
| 221 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 222 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 223 | if (instruction->GetLocations() == nullptr) { |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 224 | if (instruction->IsTemporary()) { |
| 225 | HInstruction* previous = instruction->GetPrevious(); |
| 226 | Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); |
| 227 | Move(previous, temp_location, instruction); |
| 228 | previous->GetLocations()->SetOut(temp_location); |
| 229 | } |
Nicolas Geoffray | a7aca37 | 2014-04-28 17:47:12 +0100 | [diff] [blame] | 230 | return; |
| 231 | } |
| 232 | AllocateRegistersLocally(instruction); |
| 233 | for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 234 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 235 | if (location.IsValid()) { |
| 236 | // Move the input to the desired location. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 237 | Move(instruction->InputAt(i), location, instruction); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 243 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 244 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 248 | return block_labels_.GetRawStorage() + block->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 251 | CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator, |
| 252 | HGraph* graph, |
| 253 | InstructionSet instruction_set) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 254 | switch (instruction_set) { |
| 255 | case kArm: |
| 256 | case kThumb2: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 257 | return new (allocator) arm::CodeGeneratorARM(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 258 | } |
| 259 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 260 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 261 | case kX86: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 262 | return new (allocator) x86::CodeGeneratorX86(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 263 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 264 | case kX86_64: { |
Nicolas Geoffray | 9cf3552 | 2014-06-09 18:40:10 +0100 | [diff] [blame] | 265 | return new (allocator) x86_64::CodeGeneratorX86_64(graph); |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 266 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 267 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 268 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 272 | void CodeGenerator::BuildNativeGCMap( |
| 273 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 274 | const std::vector<uint8_t>& gc_map_raw = |
| 275 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 276 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 277 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 278 | uint32_t max_native_offset = 0; |
| 279 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 280 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 281 | if (native_offset > max_native_offset) { |
| 282 | max_native_offset = native_offset; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 287 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 288 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 289 | uint32_t native_offset = pc_info.native_pc; |
| 290 | uint32_t dex_pc = pc_info.dex_pc; |
| 291 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 292 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 293 | builder.AddEntry(native_offset, references); |
| 294 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 297 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const { |
| 298 | uint32_t pc2dex_data_size = 0u; |
| 299 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 300 | uint32_t pc2dex_offset = 0u; |
| 301 | int32_t pc2dex_dalvik_offset = 0; |
| 302 | uint32_t dex2pc_data_size = 0u; |
| 303 | uint32_t dex2pc_entries = 0u; |
| 304 | |
| 305 | // We currently only have pc2dex entries. |
| 306 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 307 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 308 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 309 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 310 | pc2dex_offset = pc_info.native_pc; |
| 311 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 312 | } |
| 313 | |
| 314 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 315 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 316 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 317 | data->resize(data_size); |
| 318 | |
| 319 | uint8_t* data_ptr = &(*data)[0]; |
| 320 | uint8_t* write_pos = data_ptr; |
| 321 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 322 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 323 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 324 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 325 | |
| 326 | pc2dex_offset = 0u; |
| 327 | pc2dex_dalvik_offset = 0u; |
| 328 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 329 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 330 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 331 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 332 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 333 | pc2dex_offset = pc_info.native_pc; |
| 334 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 335 | } |
| 336 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 337 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 338 | |
| 339 | if (kIsDebugBuild) { |
| 340 | // Verify the encoded table holds the expected data. |
| 341 | MappingTable table(data_ptr); |
| 342 | CHECK_EQ(table.TotalSize(), total_entries); |
| 343 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 344 | auto it = table.PcToDexBegin(); |
| 345 | auto it2 = table.DexToPcBegin(); |
| 346 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 347 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 348 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 349 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 350 | ++it; |
| 351 | } |
| 352 | CHECK(it == table.PcToDexEnd()); |
| 353 | CHECK(it2 == table.DexToPcEnd()); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 358 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 359 | // We currently don't use callee-saved registers. |
| 360 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 361 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 362 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 363 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 364 | |
| 365 | *data = vmap_encoder.GetData(); |
| 366 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 367 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 368 | } // namespace art |