blob: 3328ab08b3b57e416737708c20c8f027250d8264 [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}
Shih-wei Liaoc4c98812012-03-10 21:55:51 -080027#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Ian Rogersab058bb2012-03-11 22:19:38 -070029 const std::vector<uint8_t>& 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) {
Ian Rogersab058bb2012-03-11 22:19:38 -070037 CHECK_NE(code.size(), 0U);
Ian Rogersb5d09b22012-03-06 22:14:17 -080038 DCHECK_EQ(vmap_table.size(),
39 static_cast<uint32_t>(__builtin_popcount(core_spill_mask)
40 + __builtin_popcount(fp_spill_mask)));
Brian Carlstrome7d856b2012-01-11 18:10:55 -080041 CHECK_LE(vmap_table.size(), (1U << 16) - 1); // length must fit in 2^16-1
Brian Carlstrom3320cf42011-10-04 14:58:28 -070042
Ian Rogersab058bb2012-03-11 22:19:38 -070043 size_t code_byte_count = code.size() * sizeof(code[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070044 std::vector<uint8_t> byte_code(code_byte_count);
Ian Rogersab058bb2012-03-11 22:19:38 -070045 memcpy(&byte_code[0], &code[0], code_byte_count);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070046
47 std::vector<uint32_t> length_prefixed_mapping_table;
48 length_prefixed_mapping_table.push_back(mapping_table.size());
49 length_prefixed_mapping_table.insert(length_prefixed_mapping_table.end(),
50 mapping_table.begin(),
51 mapping_table.end());
52 DCHECK_EQ(mapping_table.size() + 1, length_prefixed_mapping_table.size());
53
54 std::vector<uint16_t> length_prefixed_vmap_table;
55 length_prefixed_vmap_table.push_back(vmap_table.size());
56 length_prefixed_vmap_table.insert(length_prefixed_vmap_table.end(),
57 vmap_table.begin(),
58 vmap_table.end());
59 DCHECK_EQ(vmap_table.size() + 1, length_prefixed_vmap_table.size());
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070060 DCHECK_EQ(vmap_table.size(), length_prefixed_vmap_table[0]);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070061
Brian Carlstrom3320cf42011-10-04 14:58:28 -070062 code_ = byte_code;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070063 mapping_table_ = length_prefixed_mapping_table;
64 vmap_table_ = length_prefixed_vmap_table;
Brian Carlstrom0dd7dda2011-10-25 15:47:53 -070065 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 -070066}
67
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)
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -070072 // Should only be used with CompiledMethods created with the non-LLVM compilers.
Brian Carlstrome7d856b2012-01-11 18:10:55 -080073 CHECK_NE(mapping_table_.size(), 0U);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080074#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -080075
Brian Carlstrom75412882012-01-18 01:26:54 -080076 gc_map_ = gc_map;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080077}
78
Brian Carlstrom3320cf42011-10-04 14:58:28 -070079CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080080 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070081 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070082 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -080083 const uint32_t fp_spill_mask)
84 : instruction_set_(instruction_set), code_(code), frame_size_in_bytes_(frame_size_in_bytes),
85 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070086 CHECK_NE(code.size(), 0U);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070087}
88
89CompiledMethod::~CompiledMethod() {}
90
91InstructionSet CompiledMethod::GetInstructionSet() const {
92 return instruction_set_;
93}
94
95const std::vector<uint8_t>& CompiledMethod::GetCode() const {
96 return code_;
97}
98
99size_t CompiledMethod::GetFrameSizeInBytes() const {
100 return frame_size_in_bytes_;
101}
102
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700103uint32_t CompiledMethod::GetCoreSpillMask() const {
104 return core_spill_mask_;
105}
106
107uint32_t CompiledMethod::GetFpSpillMask() const {
108 return fp_spill_mask_;
109}
110
111const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
112 return mapping_table_;
113}
114
115const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
116 return vmap_table_;
117}
118
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800119const std::vector<uint8_t>& CompiledMethod::GetGcMap() const {
120 return gc_map_;
121}
122
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700123uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
124 return AlignCode(offset, instruction_set_);
125}
126
127uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
128 switch (instruction_set) {
129 case kArm:
130 case kThumb2:
131 return RoundUp(offset, kArmAlignment);
132 case kX86:
Ian Rogersb41b33b2012-03-20 14:22:54 -0700133 return RoundUp(offset, kX86Alignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700134 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800135 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700136 return 0;
137 }
138}
139
140size_t CompiledMethod::CodeDelta() const {
141 switch (instruction_set_) {
142 case kArm:
143 case kX86:
144 return 0;
145 case kThumb2: {
146 // +1 to set the low-order bit so a BLX will switch to Thumb mode
147 return 1;
148 }
149 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800150 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set_);
Brian Carlstrom413f9e02012-01-09 22:24:30 -0800151 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700152 }
153}
154
155const void* CompiledMethod::CodePointer(const void* code_pointer,
156 InstructionSet instruction_set) {
157 switch (instruction_set) {
158 case kArm:
159 case kX86:
160 return code_pointer;
161 case kThumb2: {
162 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
163 // Set the low-order bit so a BLX will switch to Thumb mode
164 address |= 0x1;
165 return reinterpret_cast<const void*>(address);
166 }
167 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800168 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700169 return NULL;
170 }
171}
172
Logan Chienf04364f2012-02-10 12:01:39 +0800173#if defined(ART_USE_LLVM_COMPILER)
174CompiledInvokeStub::CompiledInvokeStub(llvm::Function* func) : func_(func) {
175 CHECK_NE(func, static_cast<llvm::Function*>(NULL));
176}
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800177#endif
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700178CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code) {
179 CHECK_NE(code.size(), 0U);
180 code_ = code;
181}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700182
183CompiledInvokeStub::~CompiledInvokeStub() {}
184
185const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
186 return code_;
187}
188
189} // namespace art