blob: 36c4eeac576ff82cf1cfd14e5fca4c9685459391 [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)
22 : instruction_set_(instruction_set), code_(code)
23{
24 CHECK_NE(code.size(), 0U);
25}
26
27CompiledCode::CompiledCode(InstructionSet instruction_set,
28 const std::string& elf_object,
29 const std::string& symbol)
30 : instruction_set_(instruction_set), symbol_(symbol) {
31 CHECK_NE(elf_object.size(), 0U);
32 CHECK_NE(symbol.size(), 0U);
33 // TODO: we shouldn't just shove ELF objects in as "code" but
34 // change to have different kinds of compiled methods. This is
35 // being deferred until we work on hybrid execution or at least
36 // until we work on batch compilation.
37 code_.resize(elf_object.size());
38 memcpy(&code_[0], &elf_object[0], elf_object.size());
39}
40
Logan Chien598c5132012-04-28 22:00:44 +080041uint32_t CompiledCode::AlignCode(uint32_t offset) const {
42 return AlignCode(offset, instruction_set_);
43}
44
45uint32_t CompiledCode::AlignCode(uint32_t offset, InstructionSet instruction_set) {
46 switch (instruction_set) {
47 case kArm:
48 case kThumb2:
49 return RoundUp(offset, kArmAlignment);
50 case kMips:
51 return RoundUp(offset, kMipsAlignment);
52 case kX86:
53 return RoundUp(offset, kX86Alignment);
54 default:
55 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
56 return 0;
57 }
58}
59
60size_t CompiledCode::CodeDelta() const {
61 switch (instruction_set_) {
62 case kArm:
63 case kMips:
64 case kX86:
65 return 0;
66 case kThumb2: {
67 // +1 to set the low-order bit so a BLX will switch to Thumb mode
68 return 1;
69 }
70 default:
71 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
72 return 0;
73 }
74}
75
76const void* CompiledCode::CodePointer(const void* code_pointer,
77 InstructionSet instruction_set) {
78 switch (instruction_set) {
79 case kArm:
80 case kMips:
81 case kX86:
82 return code_pointer;
83 case kThumb2: {
84 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
85 // Set the low-order bit so a BLX will switch to Thumb mode
86 address |= 0x1;
87 return reinterpret_cast<const void*>(address);
88 }
89 default:
90 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
91 return NULL;
92 }
93}
94
Brian Carlstrom265091e2013-01-30 14:08:26 -080095#if defined(ART_USE_PORTABLE_COMPILER)
96const std::string& CompiledCode::GetSymbol() const {
97 CHECK_NE(0U, symbol_.size());
98 return symbol_;
99}
100
101const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const {
102 CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size()) << symbol_;
103 return oatdata_offsets_to_compiled_code_offset_;
104}
105
106void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) {
107 oatdata_offsets_to_compiled_code_offset_.push_back(offset);
108}
109#endif
110
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700111CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Ian Rogersab058bb2012-03-11 22:19:38 -0700112 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700114 const uint32_t core_spill_mask,
115 const uint32_t fp_spill_mask,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800116 const std::vector<uint32_t>& mapping_table,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700117 const std::vector<uint16_t>& vmap_table,
118 const std::vector<uint8_t>& native_gc_map)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800119 : CompiledCode(instruction_set, code), frame_size_in_bytes_(frame_size_in_bytes),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700120 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
121 native_gc_map_(native_gc_map)
Logan Chien6920bce2012-03-17 21:44:01 +0800122{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800123 DCHECK_EQ(vmap_table.size(),
124 static_cast<uint32_t>(__builtin_popcount(core_spill_mask)
125 + __builtin_popcount(fp_spill_mask)));
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800126 CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700128 std::vector<uint32_t> length_prefixed_mapping_table;
129 length_prefixed_mapping_table.push_back(mapping_table.size());
130 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
131 mapping_table.begin(),
132 mapping_table.end());
133 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
134
135 std::vector<uint16_t> length_prefixed_vmap_table;
136 length_prefixed_vmap_table.push_back(vmap_table.size());
137 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
138 vmap_table.begin(),
139 vmap_table.end());
140 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700141 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700142
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700143 mapping_table_ = length_prefixed_mapping_table;
144 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -0700145 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 -0700146}
147
148CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800149 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700150 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700151 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -0800152 const uint32_t fp_spill_mask)
Logan Chien598c5132012-04-28 22:00:44 +0800153 : CompiledCode(instruction_set, code),
154 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800155 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700156
Logan Chien598c5132012-04-28 22:00:44 +0800157CompiledInvokeStub::CompiledInvokeStub(InstructionSet instruction_set,
158 const std::vector<uint8_t>& code)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800159 : CompiledCode(instruction_set, code) {}
160
161CompiledInvokeStub::CompiledInvokeStub(InstructionSet instruction_set,
162 const std::string& elf_object,
163 const std::string& symbol)
164 : CompiledCode(instruction_set, elf_object, symbol) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700165
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700166} // namespace art