blob: 3c3f6ef5b42f3e3dfae9abf94c673c735015cf96 [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
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#if defined(ART_USE_LLVM_COMPILER)
22CompiledMethod::CompiledMethod(art::InstructionSet instruction_set,
23 llvm::Function *func)
24 : instruction_set_(instruction_set), func_(func), frame_size_in_bytes_(0),
25 core_spill_mask_(0), fp_spill_mask_(0) {
26}
27#else
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080029 const std::vector<uint16_t>& short_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070030 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031 const uint32_t core_spill_mask,
32 const uint32_t fp_spill_mask,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080033 const std::vector<uint32_t>& mapping_table,
34 const std::vector<uint16_t>& vmap_table)
Ian Rogers169c9a72011-11-13 20:13:17 -080035 : instruction_set_(instruction_set), frame_size_in_bytes_(frame_size_in_bytes),
36 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070037 CHECK_NE(short_code.size(), 0U);
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070038 CHECK_GE(vmap_table.size(), 1U); // should always contain an entry for LR
Brian Carlstrome7d856b2012-01-11 18:10:55 -080039 CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1
Brian Carlstrom3320cf42011-10-04 14:58:28 -070040
41 size_t code_byte_count = short_code.size() * sizeof(short_code[0]);
42 std::vector<uint8_t> byte_code(code_byte_count);
43 memcpy(&byte_code[0], &short_code[0], code_byte_count);
44
45 std::vector<uint32_t> length_prefixed_mapping_table;
46 length_prefixed_mapping_table.push_back(mapping_table.size());
47 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
48 mapping_table.begin(),
49 mapping_table.end());
50 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
51
52 std::vector<uint16_t> length_prefixed_vmap_table;
53 length_prefixed_vmap_table.push_back(vmap_table.size());
54 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
55 vmap_table.begin(),
56 vmap_table.end());
57 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070058 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070059
Brian Carlstrom3320cf42011-10-04 14:58:28 -070060 code_ = byte_code;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061 mapping_table_ = length_prefixed_mapping_table;
62 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070063
64 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 -070065}
Shih-wei Liaod1fec812012-02-13 09:51:10 -080066#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -070067
Brian Carlstrome7d856b2012-01-11 18:10:55 -080068void CompiledMethod::SetGcMap(const std::vector<uint8_t>& gc_map) {
69 CHECK_NE(gc_map.size(), 0U);
70
Shih-wei Liaod1fec812012-02-13 09:51:10 -080071#if !defined(ART_USE_LLVM_COMPILER)
Brian Carlstrome7d856b2012-01-11 18:10:55 -080072 // Should only be used with CompiledMethods created with oatCompileMethod
73 CHECK_NE(mapping_table_.size(), 0U);
74 CHECK_NE(vmap_table_.size(), 0U);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -080076
Brian Carlstrom75412882012-01-18 01:26:54 -080077 gc_map_ = gc_map;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080078}
79
Brian Carlstrom3320cf42011-10-04 14:58:28 -070080CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080081 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070082 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070083 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -080084 const uint32_t fp_spill_mask)
85 : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes),
86 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070087 CHECK_NE(code.size(), 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070088}
89
90CompiledMethod::~CompiledMethod() {}
91
92InstructionSet CompiledMethod::GetInstructionSet() const {
93 return instruction_set_;
94}
95
96const std::vector<uint8_t>& CompiledMethod::GetCode() const {
97 return code_;
98}
99
100size_t CompiledMethod::GetFrameSizeInBytes() const {
101 return frame_size_in_bytes_;
102}
103
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700104uint32_t CompiledMethod::GetCoreSpillMask() const {
105 return core_spill_mask_;
106}
107
108uint32_t CompiledMethod::GetFpSpillMask() const {
109 return fp_spill_mask_;
110}
111
112const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
113 return mapping_table_;
114}
115
116const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
117 return vmap_table_;
118}
119
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800120const std::vector<uint8_t>& CompiledMethod::GetGcMap() const {
121 return gc_map_;
122}
123
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700124uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
125 return AlignCode(offset, instruction_set_);
126}
127
128uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
129 switch (instruction_set) {
130 case kArm:
131 case kThumb2:
132 return RoundUp(offset, kArmAlignment);
133 case kX86:
134 return offset;
135 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800136 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700137 return 0;
138 }
139}
140
141size_t CompiledMethod::CodeDelta() const {
142 switch (instruction_set_) {
143 case kArm:
144 case kX86:
145 return 0;
146 case kThumb2: {
147 // +1 to set the low-order bit so a BLX will switch to Thumb mode
148 return 1;
149 }
150 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800151 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set_);
Brian Carlstrom413f9e02012-01-09 22:24:30 -0800152 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700153 }
154}
155
156const void* CompiledMethod::CodePointer(const void* code_pointer,
157 InstructionSet instruction_set) {
158 switch (instruction_set) {
159 case kArm:
160 case kX86:
161 return code_pointer;
162 case kThumb2: {
163 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
164 // Set the low-order bit so a BLX will switch to Thumb mode
165 address |= 0x1;
166 return reinterpret_cast<const void*>(address);
167 }
168 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800169 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700170 return NULL;
171 }
172}
173
Logan Chienf04364f2012-02-10 12:01:39 +0800174#if defined(ART_USE_LLVM_COMPILER)
175CompiledInvokeStub::CompiledInvokeStub(llvm::Function* func) : func_(func) {
176 CHECK_NE(func, static_cast<llvm::Function*>(NULL));
177}
178#else
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700179CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
180 CHECK_NE(code.size(), 0U);
181 code_ = code;
182}
Logan Chienf04364f2012-02-10 12:01:39 +0800183#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700184
185CompiledInvokeStub::~CompiledInvokeStub() {}
186
187const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
188 return code_;
189}
190
191} // namespace art