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 | b55f835 | 2014-04-07 15:26:35 +0100 | [diff] [blame] | 36 | GenerateFrameEntry(); |
| 37 | for (size_t i = 0; 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 | |
| 46 | void CodeGenerator::CompileBlock(HBasicBlock* block) { |
| 47 | Bind(GetLabelOf(block)); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 48 | HGraphVisitor* location_builder = GetLocationBuilder(); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 49 | HGraphVisitor* instruction_visitor = GetInstructionVisitor(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 50 | for (HInstructionIterator it(*block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 51 | HInstruction* current = it.Current(); |
| 52 | current->Accept(location_builder); |
| 53 | InitLocations(current); |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 54 | current->Accept(instruction_visitor); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 58 | void CodeGenerator::InitLocations(HInstruction* instruction) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 59 | if (instruction->GetLocations() == nullptr) return; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame^] | 60 | for (size_t i = 0; i < instruction->InputCount(); i++) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 61 | Location location = instruction->GetLocations()->InAt(i); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 62 | if (location.IsValid()) { |
| 63 | // Move the input to the desired location. |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 64 | Move(instruction->InputAt(i), location, instruction); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 70 | // We currently iterate over the block in insertion order. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 71 | return current->GetBlockId() + 1 == next->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | Label* CodeGenerator::GetLabelOf(HBasicBlock* block) const { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 75 | return block_labels_.GetRawStorage() + block->GetBlockId(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 78 | CodeGenerator* CodeGenerator::Create(ArenaAllocator* allocator, |
| 79 | HGraph* graph, |
| 80 | InstructionSet instruction_set) { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 81 | switch (instruction_set) { |
| 82 | case kArm: |
| 83 | case kThumb2: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 84 | return new (allocator) arm::CodeGeneratorARM(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 85 | } |
| 86 | case kMips: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 87 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 88 | case kX86: { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 89 | return new (allocator) x86::CodeGeneratorX86(graph); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 90 | } |
Dmitry Petrochenko | 6a58cb1 | 2014-04-02 17:27:59 +0700 | [diff] [blame] | 91 | case kX86_64: { |
| 92 | return new (allocator) x86::CodeGeneratorX86(graph); |
| 93 | } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 94 | default: |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 95 | return nullptr; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 99 | void CodeGenerator::BuildNativeGCMap( |
| 100 | std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const { |
| 101 | const std::vector<uint8_t>& gc_map_raw = |
| 102 | dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap(); |
| 103 | verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]); |
| 104 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 105 | uint32_t max_native_offset = 0; |
| 106 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 107 | uint32_t native_offset = pc_infos_.Get(i).native_pc; |
| 108 | if (native_offset > max_native_offset) { |
| 109 | max_native_offset = native_offset; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth()); |
| 114 | for (size_t i = 0; i < pc_infos_.Size(); i++) { |
| 115 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 116 | uint32_t native_offset = pc_info.native_pc; |
| 117 | uint32_t dex_pc = pc_info.dex_pc; |
| 118 | const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false); |
| 119 | CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc; |
| 120 | builder.AddEntry(native_offset, references); |
| 121 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 124 | void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const { |
| 125 | uint32_t pc2dex_data_size = 0u; |
| 126 | uint32_t pc2dex_entries = pc_infos_.Size(); |
| 127 | uint32_t pc2dex_offset = 0u; |
| 128 | int32_t pc2dex_dalvik_offset = 0; |
| 129 | uint32_t dex2pc_data_size = 0u; |
| 130 | uint32_t dex2pc_entries = 0u; |
| 131 | |
| 132 | // We currently only have pc2dex entries. |
| 133 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 134 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 135 | pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset); |
| 136 | pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset); |
| 137 | pc2dex_offset = pc_info.native_pc; |
| 138 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 139 | } |
| 140 | |
| 141 | uint32_t total_entries = pc2dex_entries + dex2pc_entries; |
| 142 | uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries); |
| 143 | uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size; |
| 144 | data->resize(data_size); |
| 145 | |
| 146 | uint8_t* data_ptr = &(*data)[0]; |
| 147 | uint8_t* write_pos = data_ptr; |
| 148 | write_pos = EncodeUnsignedLeb128(write_pos, total_entries); |
| 149 | write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries); |
| 150 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size); |
| 151 | uint8_t* write_pos2 = write_pos + pc2dex_data_size; |
| 152 | |
| 153 | pc2dex_offset = 0u; |
| 154 | pc2dex_dalvik_offset = 0u; |
| 155 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 156 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 157 | DCHECK(pc2dex_offset <= pc_info.native_pc); |
| 158 | write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset); |
| 159 | write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset); |
| 160 | pc2dex_offset = pc_info.native_pc; |
| 161 | pc2dex_dalvik_offset = pc_info.dex_pc; |
| 162 | } |
| 163 | DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size); |
| 164 | DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size); |
| 165 | |
| 166 | if (kIsDebugBuild) { |
| 167 | // Verify the encoded table holds the expected data. |
| 168 | MappingTable table(data_ptr); |
| 169 | CHECK_EQ(table.TotalSize(), total_entries); |
| 170 | CHECK_EQ(table.PcToDexSize(), pc2dex_entries); |
| 171 | auto it = table.PcToDexBegin(); |
| 172 | auto it2 = table.DexToPcBegin(); |
| 173 | for (size_t i = 0; i < pc2dex_entries; i++) { |
| 174 | struct PcInfo pc_info = pc_infos_.Get(i); |
| 175 | CHECK_EQ(pc_info.native_pc, it.NativePcOffset()); |
| 176 | CHECK_EQ(pc_info.dex_pc, it.DexPc()); |
| 177 | ++it; |
| 178 | } |
| 179 | CHECK(it == table.PcToDexEnd()); |
| 180 | CHECK(it2 == table.DexToPcEnd()); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const { |
| 185 | Leb128EncodingVector vmap_encoder; |
Nicolas Geoffray | 4a34a42 | 2014-04-03 10:38:37 +0100 | [diff] [blame] | 186 | // We currently don't use callee-saved registers. |
| 187 | size_t size = 0 + 1 /* marker */ + 0; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 188 | vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128). |
| 189 | vmap_encoder.PushBackUnsigned(size); |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 190 | vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker); |
| 191 | |
| 192 | *data = vmap_encoder.GetData(); |
| 193 | } |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 194 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 195 | } // namespace art |