Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "compiled_method.h" |
| 4 | |
| 5 | namespace art { |
| 6 | |
| 7 | CompiledMethod::CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 8 | const std::vector<uint16_t>& short_code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 9 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 10 | const uint32_t core_spill_mask, |
| 11 | const uint32_t fp_spill_mask, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 12 | const std::vector<uint32_t>& mapping_table, |
| 13 | const std::vector<uint16_t>& vmap_table) |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 14 | : instruction_set_(instruction_set), frame_size_in_bytes_(frame_size_in_bytes), |
| 15 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 16 | CHECK_NE(short_code.size(), 0U); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 17 | CHECK_GE(vmap_table.size(), 1U); // should always contain an entry for LR |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 18 | CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1 |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 19 | |
| 20 | size_t code_byte_count = short_code.size() * sizeof(short_code[0]); |
| 21 | std::vector<uint8_t> byte_code(code_byte_count); |
| 22 | memcpy(&byte_code[0], &short_code[0], code_byte_count); |
| 23 | |
| 24 | std::vector<uint32_t> length_prefixed_mapping_table; |
| 25 | length_prefixed_mapping_table.push_back(mapping_table.size()); |
| 26 | length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(), |
| 27 | mapping_table.begin(), |
| 28 | mapping_table.end()); |
| 29 | DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size()); |
| 30 | |
| 31 | std::vector<uint16_t> length_prefixed_vmap_table; |
| 32 | length_prefixed_vmap_table.push_back(vmap_table.size()); |
| 33 | length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(), |
| 34 | vmap_table.begin(), |
| 35 | vmap_table.end()); |
| 36 | DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size()); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 37 | DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 38 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 39 | code_ = byte_code; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 40 | mapping_table_ = length_prefixed_mapping_table; |
| 41 | vmap_table_ = length_prefixed_vmap_table; |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 42 | |
| 43 | DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask) + __builtin_popcount(fp_spill_mask))); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 46 | void CompiledMethod::SetGcMap(const std::vector<uint8_t>& gc_map) { |
| 47 | CHECK_NE(gc_map.size(), 0U); |
| 48 | |
| 49 | // Should only be used with CompiledMethods created with oatCompileMethod |
| 50 | CHECK_NE(mapping_table_.size(), 0U); |
| 51 | CHECK_NE(vmap_table_.size(), 0U); |
| 52 | |
| 53 | std::vector<uint8_t> length_prefixed_gc_map; |
| 54 | length_prefixed_gc_map.push_back((gc_map.size() & 0xff000000) >> 24); |
| 55 | length_prefixed_gc_map.push_back((gc_map.size() & 0x00ff0000) >> 16); |
| 56 | length_prefixed_gc_map.push_back((gc_map.size() & 0x0000ff00) >> 8); |
| 57 | length_prefixed_gc_map.push_back((gc_map.size() & 0x000000ff) >> 0); |
| 58 | length_prefixed_gc_map.insert(length_prefixed_gc_map.end(), |
| 59 | gc_map.begin(), |
| 60 | gc_map.end()); |
| 61 | DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map.size()); |
| 62 | DCHECK_EQ(gc_map.size(), |
| 63 | static_cast<size_t>((length_prefixed_gc_map[0] << 24) | |
| 64 | (length_prefixed_gc_map[1] << 16) | |
| 65 | (length_prefixed_gc_map[2] << 8) | |
| 66 | (length_prefixed_gc_map[3] << 0))); |
| 67 | |
| 68 | gc_map_ = length_prefixed_gc_map; |
| 69 | } |
| 70 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 71 | CompiledMethod::CompiledMethod(InstructionSet instruction_set, |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 72 | const std::vector<uint8_t>& code, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 73 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 74 | const uint32_t core_spill_mask, |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame] | 75 | const uint32_t fp_spill_mask) |
| 76 | : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes), |
| 77 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 78 | CHECK_NE(code.size(), 0U); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | CompiledMethod::~CompiledMethod() {} |
| 82 | |
| 83 | InstructionSet CompiledMethod::GetInstructionSet() const { |
| 84 | return instruction_set_; |
| 85 | } |
| 86 | |
| 87 | const std::vector<uint8_t>& CompiledMethod::GetCode() const { |
| 88 | return code_; |
| 89 | } |
| 90 | |
| 91 | size_t CompiledMethod::GetFrameSizeInBytes() const { |
| 92 | return frame_size_in_bytes_; |
| 93 | } |
| 94 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 95 | uint32_t CompiledMethod::GetCoreSpillMask() const { |
| 96 | return core_spill_mask_; |
| 97 | } |
| 98 | |
| 99 | uint32_t CompiledMethod::GetFpSpillMask() const { |
| 100 | return fp_spill_mask_; |
| 101 | } |
| 102 | |
| 103 | const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const { |
| 104 | return mapping_table_; |
| 105 | } |
| 106 | |
| 107 | const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const { |
| 108 | return vmap_table_; |
| 109 | } |
| 110 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 111 | const std::vector<uint8_t>& CompiledMethod::GetGcMap() const { |
| 112 | return gc_map_; |
| 113 | } |
| 114 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 115 | uint32_t CompiledMethod::AlignCode(uint32_t offset) const { |
| 116 | return AlignCode(offset, instruction_set_); |
| 117 | } |
| 118 | |
| 119 | uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) { |
| 120 | switch (instruction_set) { |
| 121 | case kArm: |
| 122 | case kThumb2: |
| 123 | return RoundUp(offset, kArmAlignment); |
| 124 | case kX86: |
| 125 | return offset; |
| 126 | default: |
| 127 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set; |
| 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | size_t CompiledMethod::CodeDelta() const { |
| 133 | switch (instruction_set_) { |
| 134 | case kArm: |
| 135 | case kX86: |
| 136 | return 0; |
| 137 | case kThumb2: { |
| 138 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 139 | return 1; |
| 140 | } |
| 141 | default: |
| 142 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set_; |
Brian Carlstrom | 413f9e0 | 2012-01-09 22:24:30 -0800 | [diff] [blame] | 143 | return 0; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | const void* CompiledMethod::CodePointer(const void* code_pointer, |
| 148 | InstructionSet instruction_set) { |
| 149 | switch (instruction_set) { |
| 150 | case kArm: |
| 151 | case kX86: |
| 152 | return code_pointer; |
| 153 | case kThumb2: { |
| 154 | uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer); |
| 155 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 156 | address |= 0x1; |
| 157 | return reinterpret_cast<const void*>(address); |
| 158 | } |
| 159 | default: |
| 160 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set; |
| 161 | return NULL; |
| 162 | } |
| 163 | } |
| 164 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 165 | CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) { |
| 166 | CHECK_NE(code.size(), 0U); |
| 167 | code_ = code; |
| 168 | } |
| 169 | |
| 170 | CompiledInvokeStub::~CompiledInvokeStub() {} |
| 171 | |
| 172 | const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const { |
| 173 | return code_; |
| 174 | } |
| 175 | |
| 176 | } // namespace art |