blob: c64c71ed3fe185a0f5c532d99e09d0b305d81c3f [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstrom3320cf42011-10-04 14:58:28 -070016
17#include "compiled_method.h"
18
19namespace art {
20
Brian Carlstrom265091e2013-01-30 14:08:26 -080021CompiledCode::CompiledCode(InstructionSet instruction_set, const std::vector<uint8_t>& code)
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070022 : instruction_set_(instruction_set), code_(code) {
Brian Carlstrom265091e2013-01-30 14:08:26 -080023 CHECK_NE(code.size(), 0U);
24}
25
26CompiledCode::CompiledCode(InstructionSet instruction_set,
27 const std::string& elf_object,
28 const std::string& symbol)
29 : instruction_set_(instruction_set), symbol_(symbol) {
30 CHECK_NE(elf_object.size(), 0U);
31 CHECK_NE(symbol.size(), 0U);
32 // TODO: we shouldn't just shove ELF objects in as "code" but
33 // change to have different kinds of compiled methods. This is
34 // being deferred until we work on hybrid execution or at least
35 // until we work on batch compilation.
36 code_.resize(elf_object.size());
37 memcpy(&code_[0], &elf_object[0], elf_object.size());
38}
39
Logan Chien598c5132012-04-28 22:00:44 +080040uint32_t CompiledCode::AlignCode(uint32_t offset) const {
41 return AlignCode(offset, instruction_set_);
42}
43
44uint32_t CompiledCode::AlignCode(uint32_t offset, InstructionSet instruction_set) {
45 switch (instruction_set) {
46 case kArm:
47 case kThumb2:
48 return RoundUp(offset, kArmAlignment);
49 case kMips:
50 return RoundUp(offset, kMipsAlignment);
51 case kX86:
52 return RoundUp(offset, kX86Alignment);
53 default:
54 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
55 return 0;
56 }
57}
58
59size_t CompiledCode::CodeDelta() const {
60 switch (instruction_set_) {
61 case kArm:
62 case kMips:
63 case kX86:
64 return 0;
65 case kThumb2: {
66 // +1 to set the low-order bit so a BLX will switch to Thumb mode
67 return 1;
68 }
69 default:
70 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
71 return 0;
72 }
73}
74
75const void* CompiledCode::CodePointer(const void* code_pointer,
76 InstructionSet instruction_set) {
77 switch (instruction_set) {
78 case kArm:
79 case kMips:
80 case kX86:
81 return code_pointer;
82 case kThumb2: {
83 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
84 // Set the low-order bit so a BLX will switch to Thumb mode
85 address |= 0x1;
86 return reinterpret_cast<const void*>(address);
87 }
88 default:
89 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
90 return NULL;
91 }
92}
93
Brian Carlstrom265091e2013-01-30 14:08:26 -080094#if defined(ART_USE_PORTABLE_COMPILER)
95const std::string& CompiledCode::GetSymbol() const {
96 CHECK_NE(0U, symbol_.size());
97 return symbol_;
98}
99
100const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const {
101 CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size()) << symbol_;
102 return oatdata_offsets_to_compiled_code_offset_;
103}
104
105void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) {
106 oatdata_offsets_to_compiled_code_offset_.push_back(offset);
107}
108#endif
109
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700110CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Ian Rogersab058bb2012-03-11 22:19:38 -0700111 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700112 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113 const uint32_t core_spill_mask,
114 const uint32_t fp_spill_mask,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800115 const std::vector<uint32_t>& mapping_table,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700116 const std::vector<uint16_t>& vmap_table,
117 const std::vector<uint8_t>& native_gc_map)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800118 : CompiledCode(instruction_set, code), frame_size_in_bytes_(frame_size_in_bytes),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700119 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700120 gc_map_(native_gc_map) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800121 DCHECK_EQ(vmap_table.size(),
122 static_cast<uint32_t>(__builtin_popcount(core_spill_mask)
123 + __builtin_popcount(fp_spill_mask)));
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700124 CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700125
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700126 std::vector<uint32_t> length_prefixed_mapping_table;
127 length_prefixed_mapping_table.push_back(mapping_table.size());
128 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
129 mapping_table.begin(),
130 mapping_table.end());
131 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
132
133 std::vector<uint16_t> length_prefixed_vmap_table;
134 length_prefixed_vmap_table.push_back(vmap_table.size());
135 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
136 vmap_table.begin(),
137 vmap_table.end());
138 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700139 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700140
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 mapping_table_ = length_prefixed_mapping_table;
142 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700143 DCHECK_EQ(vmap_table_[0], static_cast<uint32_t>(__builtin_popcount(core_spill_mask) + __builtin_popcount(fp_spill_mask)));
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700144}
145
146CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800147 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700149 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -0800150 const uint32_t fp_spill_mask)
Logan Chien598c5132012-04-28 22:00:44 +0800151 : CompiledCode(instruction_set, code),
152 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800153 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700154
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700155} // namespace art