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, |
| 8 | std::vector<short>& short_code, |
| 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, |
| 12 | std::vector<uint32_t>& mapping_table, |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame^] | 13 | std::vector<uint16_t>& vmap_table) |
| 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 | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 18 | |
| 19 | size_t code_byte_count = short_code.size() * sizeof(short_code[0]); |
| 20 | std::vector<uint8_t> byte_code(code_byte_count); |
| 21 | memcpy(&byte_code[0], &short_code[0], code_byte_count); |
| 22 | |
| 23 | std::vector<uint32_t> length_prefixed_mapping_table; |
| 24 | length_prefixed_mapping_table.push_back(mapping_table.size()); |
| 25 | length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(), |
| 26 | mapping_table.begin(), |
| 27 | mapping_table.end()); |
| 28 | DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size()); |
| 29 | |
| 30 | std::vector<uint16_t> length_prefixed_vmap_table; |
| 31 | length_prefixed_vmap_table.push_back(vmap_table.size()); |
| 32 | length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(), |
| 33 | vmap_table.begin(), |
| 34 | vmap_table.end()); |
| 35 | DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size()); |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 36 | DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 37 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 38 | code_ = byte_code; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 39 | mapping_table_ = length_prefixed_mapping_table; |
| 40 | vmap_table_ = length_prefixed_vmap_table; |
Brian Carlstrom | 0dd7dda | 2011-10-25 15:47:53 -0700 | [diff] [blame] | 41 | |
| 42 | 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] | 43 | } |
| 44 | |
| 45 | CompiledMethod::CompiledMethod(InstructionSet instruction_set, |
| 46 | std::vector<uint8_t>& code, |
| 47 | const size_t frame_size_in_bytes, |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 48 | const uint32_t core_spill_mask, |
Ian Rogers | 169c9a7 | 2011-11-13 20:13:17 -0800 | [diff] [blame^] | 49 | const uint32_t fp_spill_mask) |
| 50 | : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes), |
| 51 | core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) { |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 52 | CHECK_NE(code.size(), 0U); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | CompiledMethod::~CompiledMethod() {} |
| 56 | |
| 57 | InstructionSet CompiledMethod::GetInstructionSet() const { |
| 58 | return instruction_set_; |
| 59 | } |
| 60 | |
| 61 | const std::vector<uint8_t>& CompiledMethod::GetCode() const { |
| 62 | return code_; |
| 63 | } |
| 64 | |
| 65 | size_t CompiledMethod::GetFrameSizeInBytes() const { |
| 66 | return frame_size_in_bytes_; |
| 67 | } |
| 68 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 69 | uint32_t CompiledMethod::GetCoreSpillMask() const { |
| 70 | return core_spill_mask_; |
| 71 | } |
| 72 | |
| 73 | uint32_t CompiledMethod::GetFpSpillMask() const { |
| 74 | return fp_spill_mask_; |
| 75 | } |
| 76 | |
| 77 | const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const { |
| 78 | return mapping_table_; |
| 79 | } |
| 80 | |
| 81 | const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const { |
| 82 | return vmap_table_; |
| 83 | } |
| 84 | |
| 85 | uint32_t CompiledMethod::AlignCode(uint32_t offset) const { |
| 86 | return AlignCode(offset, instruction_set_); |
| 87 | } |
| 88 | |
| 89 | uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) { |
| 90 | switch (instruction_set) { |
| 91 | case kArm: |
| 92 | case kThumb2: |
| 93 | return RoundUp(offset, kArmAlignment); |
| 94 | case kX86: |
| 95 | return offset; |
| 96 | default: |
| 97 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set; |
| 98 | return 0; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | size_t CompiledMethod::CodeDelta() const { |
| 103 | switch (instruction_set_) { |
| 104 | case kArm: |
| 105 | case kX86: |
| 106 | return 0; |
| 107 | case kThumb2: { |
| 108 | // +1 to set the low-order bit so a BLX will switch to Thumb mode |
| 109 | return 1; |
| 110 | } |
| 111 | default: |
| 112 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set_; |
| 113 | return NULL; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | const void* CompiledMethod::CodePointer(const void* code_pointer, |
| 118 | InstructionSet instruction_set) { |
| 119 | switch (instruction_set) { |
| 120 | case kArm: |
| 121 | case kX86: |
| 122 | return code_pointer; |
| 123 | case kThumb2: { |
| 124 | uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer); |
| 125 | // Set the low-order bit so a BLX will switch to Thumb mode |
| 126 | address |= 0x1; |
| 127 | return reinterpret_cast<const void*>(address); |
| 128 | } |
| 129 | default: |
| 130 | LOG(FATAL) << "Unknown InstructionSet " << (int) instruction_set; |
| 131 | return NULL; |
| 132 | } |
| 133 | } |
| 134 | |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 135 | CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) { |
| 136 | CHECK_NE(code.size(), 0U); |
| 137 | code_ = code; |
| 138 | } |
| 139 | |
| 140 | CompiledInvokeStub::~CompiledInvokeStub() {} |
| 141 | |
| 142 | const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const { |
| 143 | return code_; |
| 144 | } |
| 145 | |
| 146 | } // namespace art |