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 | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 21 | #include "dex/verified_method.h" |
| 22 | #include "driver/dex_compilation_unit.h" |
| 23 | #include "gc_map_builder.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 24 | #include "leb128.h" |
| 25 | #include "mapping_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 26 | #include "utils/assembler.h" |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 27 | #include "verifier/dex_gc_map.h" |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 28 | #include "vmap_table.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
| 32 | void CodeGenerator::Compile(CodeAllocator* allocator) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 33 | const GrowableArray<HBasicBlock*>* blocks = GetGraph()->GetBlocks(); |
| 34 | DCHECK(blocks->Get(0) == GetGraph()->GetEntryBlock()); |
| 35 | DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks->Get(1))); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 36 | CompileEntryBlock(); |
| 37 | for (size_t i = 1; i < blocks->Size(); i++) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 38 | CompileBlock(blocks->Get(i)); |
| 39 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 40 | size_t code_size = GetAssembler()->CodeSize(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 41 | uint8_t* buffer = allocator->Allocate(code_size); |
| 42 | MemoryRegion code(buffer, code_size); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 43 | GetAssembler()->FinalizeInstructions(code); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 46 | void CodeGenerator::CompileEntryBlock() { |
| 47 | HGraphVisitor* location_builder = GetLocationBuilder(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 48 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 49 | // The entry block contains all locals for this method. By visiting the entry block, |
| 50 | // we're computing the required frame size. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 51 | for (HInstructionIterator it(GetGraph()->GetEntryBlock()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 52 | HInstruction* current = it.Current(); |
| 53 | // Instructions in the entry block should not generate code. |
| 54 | if (kIsDebugBuild) { |
| 55 | current->Accept(location_builder); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 56 | DCHECK(current->GetLocations() == nullptr); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 57 | } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 58 | current->Accept(instruction_visitor); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 59 | } |
| 60 | GenerateFrameEntry(); |
| 61 | } |
| 62 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 63 | void CodeGenerator::CompileBlock(HBasicBlock* block) { |
| 64 | Bind(GetLabelOf(block)); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 65 | HGraphVisitor* location_builder = GetLocationBuilder(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 66 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 67 | for (HInstructionIterator it(block); !it.Done(); it.Advance()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 68 | // For each instruction, we emulate a stack-based machine, where the inputs are popped from |
| 69 | // the runtime stack, and the result is pushed on the stack. We currently can do this because |
| 70 | // we do not perform any code motion, and the Dex format does not reference individual |
| 71 | // instructions but uses registers instead (our equivalent of HLocal). |
| 72 | HInstruction* current = it.Current(); |
| 73 | current->Accept(location_builder); |
| 74 | InitLocations(current); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 75 | current->Accept(instruction_visitor); |
| 76 | if (current->GetLocations() != nullptr && current->GetLocations()->Out().IsValid()) { |
| 77 | Push(current, current->GetLocations()->Out()); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 78 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 82 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 83 | if (instruction->GetLocations() == nullptr) return; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 84 | for (int i = 0; i < instruction->InputCount(); i++) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 85 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 86 | if (location.IsValid()) { |
| 87 | // Move the input to the desired location. |
| 88 | Move(instruction->InputAt(i), location); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 94 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 95 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 99 | return block_labels_.GetRawStorage() + block->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 102 | CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator, |
| 103 | HGraph* graph, |
| 104 | InstructionSet instruction_set) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 105 | switch (instruction_set) { |
| 106 | case kArm: |
| 107 | case kThumb2: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 108 | return new (allocator) arm::CodeGeneratorARM(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 109 | } |
| 110 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 111 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 112 | case kX86: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 113 | return new (allocator) x86::CodeGeneratorX86(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 114 | } |
| 115 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 116 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 120 | void CodeGenerator::BuildNativeGCMap( |
| 121 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 122 | const std::vector<uint8_t>& gc_map_raw = |
| 123 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 124 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 125 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 126 | uint32_t max_native_offset = 0; |
| 127 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 128 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 129 | if (native_offset > max_native_offset) { |
| 130 | max_native_offset = native_offset; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 135 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 136 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 137 | uint32_t native_offset = pc_info.native_pc; |
| 138 | uint32_t dex_pc = pc_info.dex_pc; |
| 139 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 140 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 141 | builder.AddEntry(native_offset, references); |
| 142 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 145 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const { |
| 146 | uint32_t pc2dex_data_size = 0u; |
| 147 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 148 | uint32_t pc2dex_offset = 0u; |
| 149 | int32_t pc2dex_dalvik_offset = 0; |
| 150 | uint32_t dex2pc_data_size = 0u; |
| 151 | uint32_t dex2pc_entries = 0u; |
| 152 | |
| 153 | // We currently only have pc2dex entries. |
| 154 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 155 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 156 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 157 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 158 | pc2dex_offset = pc_info.native_pc; |
| 159 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 160 | } |
| 161 | |
| 162 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 163 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 164 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 165 | data->resize(data_size); |
| 166 | |
| 167 | uint8_t* data_ptr = &(*data)[0]; |
| 168 | uint8_t* write_pos = data_ptr; |
| 169 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 170 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 171 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 172 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 173 | |
| 174 | pc2dex_offset = 0u; |
| 175 | pc2dex_dalvik_offset = 0u; |
| 176 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 177 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 178 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 179 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 180 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 181 | pc2dex_offset = pc_info.native_pc; |
| 182 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 183 | } |
| 184 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 185 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 186 | |
| 187 | if (kIsDebugBuild) { |
| 188 | // Verify the encoded table holds the expected data. |
| 189 | MappingTable table(data_ptr); |
| 190 | CHECK_EQ(table.TotalSize(), total_entries); |
| 191 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 192 | auto it = table.PcToDexBegin(); |
| 193 | auto it2 = table.DexToPcBegin(); |
| 194 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 195 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 196 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 197 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 198 | ++it; |
| 199 | } |
| 200 | CHECK(it == table.PcToDexEnd()); |
| 201 | CHECK(it2 == table.DexToPcEnd()); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 206 | Leb128EncodingVector vmap_encoder; |
| 207 | size_t size = 1 + 1 /* marker */ + 0; |
| 208 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 209 | vmap_encoder.PushBackUnsigned(size); |
| 210 | // We're currently always saving the frame pointer, so set it in the table as a temporary. |
| 211 | vmap_encoder.PushBackUnsigned(kVRegTempBaseReg + VmapTable::kEntryAdjustment); |
| 212 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 213 | |
| 214 | *data = vmap_encoder.GetData(); |
| 215 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 216 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 217 | } // namespace art |