blob: 4b3cdeea6024d02bd63a8f15f8de1f86e3454775 [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
21CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Ian Rogersab058bb2012-03-11 22:19:38 -070022 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070023 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070024 const uint32_t core_spill_mask,
25 const uint32_t fp_spill_mask,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080026 const std::vector<uint32_t>& mapping_table,
27 const std::vector<uint16_t>& vmap_table)
Ian Rogers169c9a72011-11-13 20:13:17 -080028 : instruction_set_(instruction_set), frame_size_in_bytes_(frame_size_in_bytes),
Logan Chien6920bce2012-03-17 21:44:01 +080029 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
30 elf_idx_(-1)
31{
Ian Rogersab058bb2012-03-11 22:19:38 -070032 CHECK_NE(code.size(), 0U);
Ian Rogersb5d09b22012-03-06 22:14:17 -080033 DCHECK_EQ(vmap_table.size(),
34 static_cast<uint32_t>(__builtin_popcount(core_spill_mask)
35 + __builtin_popcount(fp_spill_mask)));
Brian Carlstrome7d856b2012-01-11 18:10:55 -080036 CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1
Brian Carlstrom3320cf42011-10-04 14:58:28 -070037
Ian Rogersab058bb2012-03-11 22:19:38 -070038 size_t code_byte_count = code.size() * sizeof(code[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070039 std::vector<uint8_t> byte_code(code_byte_count);
Ian Rogersab058bb2012-03-11 22:19:38 -070040 memcpy(&byte_code[0], &code[0], code_byte_count);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070041
42 std::vector<uint32_t> length_prefixed_mapping_table;
43 length_prefixed_mapping_table.push_back(mapping_table.size());
44 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
45 mapping_table.begin(),
46 mapping_table.end());
47 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
48
49 std::vector<uint16_t> length_prefixed_vmap_table;
50 length_prefixed_vmap_table.push_back(vmap_table.size());
51 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
52 vmap_table.begin(),
53 vmap_table.end());
54 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070055 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070056
Brian Carlstrom3320cf42011-10-04 14:58:28 -070057 code_ = byte_code;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070058 mapping_table_ = length_prefixed_mapping_table;
59 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070060 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 -070061}
62
Brian Carlstrome7d856b2012-01-11 18:10:55 -080063void CompiledMethod::SetGcMap(const std::vector<uint8_t>& gc_map) {
64 CHECK_NE(gc_map.size(), 0U);
65
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -070066#if !defined(ART_USE_LLVM_COMPILER) && !defined(ART_USE_GREENLAND_COMPILER)
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -070067 // Should only be used with CompiledMethods created with the non-LLVM compilers.
Brian Carlstrome7d856b2012-01-11 18:10:55 -080068 CHECK_NE(mapping_table_.size(), 0U);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080069#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -080070
Brian Carlstrom75412882012-01-18 01:26:54 -080071 gc_map_ = gc_map;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080072}
73
Brian Carlstrom3320cf42011-10-04 14:58:28 -070074CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080075 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070076 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070077 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -080078 const uint32_t fp_spill_mask)
79 : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes),
Logan Chien6920bce2012-03-17 21:44:01 +080080 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
81 elf_idx_(-1)
82{
Brian Carlstrom3320cf42011-10-04 14:58:28 -070083 CHECK_NE(code.size(), 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070084}
85
Logan Chien937105a2012-04-02 02:37:37 +080086CompiledMethod::CompiledMethod(InstructionSet instruction_set,
87 const uint16_t elf_idx,
88 const uint16_t elf_func_idx)
Logan Chien6920bce2012-03-17 21:44:01 +080089 : instruction_set_(instruction_set), frame_size_in_bytes_(0),
Logan Chien937105a2012-04-02 02:37:37 +080090 core_spill_mask_(0), fp_spill_mask_(0), elf_idx_(elf_idx),
91 elf_func_idx_(elf_func_idx) {
Logan Chien6920bce2012-03-17 21:44:01 +080092}
93
Brian Carlstrom3320cf42011-10-04 14:58:28 -070094CompiledMethod::~CompiledMethod() {}
95
96InstructionSet CompiledMethod::GetInstructionSet() const {
97 return instruction_set_;
98}
99
100const std::vector<uint8_t>& CompiledMethod::GetCode() const {
101 return code_;
102}
103
104size_t CompiledMethod::GetFrameSizeInBytes() const {
105 return frame_size_in_bytes_;
106}
107
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700108uint32_t CompiledMethod::GetCoreSpillMask() const {
109 return core_spill_mask_;
110}
111
112uint32_t CompiledMethod::GetFpSpillMask() const {
113 return fp_spill_mask_;
114}
115
116const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
117 return mapping_table_;
118}
119
120const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
121 return vmap_table_;
122}
123
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800124const std::vector<uint8_t>& CompiledMethod::GetGcMap() const {
125 return gc_map_;
126}
127
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700128uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
129 return AlignCode(offset, instruction_set_);
130}
131
132uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
133 switch (instruction_set) {
134 case kArm:
135 case kThumb2:
136 return RoundUp(offset, kArmAlignment);
Elliott Hughes742e25e2012-04-24 18:11:08 -0700137 case kMips:
138 return RoundUp(offset, kMipsAlignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700139 case kX86:
Ian Rogersb41b33b2012-03-20 14:22:54 -0700140 return RoundUp(offset, kX86Alignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700141 default:
Elliott Hughes742e25e2012-04-24 18:11:08 -0700142 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700143 return 0;
144 }
145}
146
147size_t CompiledMethod::CodeDelta() const {
148 switch (instruction_set_) {
149 case kArm:
Elliott Hughes742e25e2012-04-24 18:11:08 -0700150 case kMips:
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700151 case kX86:
152 return 0;
153 case kThumb2: {
154 // +1 to set the low-order bit so a BLX will switch to Thumb mode
155 return 1;
156 }
157 default:
Elliott Hughes742e25e2012-04-24 18:11:08 -0700158 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
Brian Carlstrom413f9e02012-01-09 22:24:30 -0800159 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700160 }
161}
162
163const void* CompiledMethod::CodePointer(const void* code_pointer,
164 InstructionSet instruction_set) {
165 switch (instruction_set) {
166 case kArm:
Elliott Hughes742e25e2012-04-24 18:11:08 -0700167 case kMips:
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700168 case kX86:
169 return code_pointer;
170 case kThumb2: {
171 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
172 // Set the low-order bit so a BLX will switch to Thumb mode
173 address |= 0x1;
174 return reinterpret_cast<const void*>(address);
175 }
176 default:
Elliott Hughes742e25e2012-04-24 18:11:08 -0700177 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700178 return NULL;
179 }
180}
181
Logan Chienf04364f2012-02-10 12:01:39 +0800182#if defined(ART_USE_LLVM_COMPILER)
Logan Chien7a2a23a2012-06-06 11:01:00 +0800183CompiledInvokeStub::CompiledInvokeStub(uint16_t elf_idx, uint16_t elf_func_idx)
184 : elf_idx_(elf_idx), elf_func_idx_(elf_func_idx) {
Logan Chienf04364f2012-02-10 12:01:39 +0800185}
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800186#endif
Logan Chien6920bce2012-03-17 21:44:01 +0800187
188CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code)
Logan Chien7a2a23a2012-06-06 11:01:00 +0800189 : elf_idx_(-1), elf_func_idx_(-1) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700190 CHECK_NE(code.size(), 0U);
191 code_ = code;
192}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700193
194CompiledInvokeStub::~CompiledInvokeStub() {}
195
196const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
197 return code_;
198}
199
200} // namespace art