blob: e85809c7e5d389ec1d98ed93ce633cfb5884e4c8 [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 Liaod1fec812012-02-13 09:51:10 -080066#if !defined(ART_USE_LLVM_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 Chien6920bce2012-03-17 21:44:01 +080086CompiledMethod::CompiledMethod(InstructionSet instruction_set, size_t elf_idx)
87 : instruction_set_(instruction_set), frame_size_in_bytes_(0),
88 core_spill_mask_(0), fp_spill_mask_(0), elf_idx_(elf_idx) {
89}
90
Brian Carlstrom3320cf42011-10-04 14:58:28 -070091CompiledMethod::~CompiledMethod() {}
92
93InstructionSet CompiledMethod::GetInstructionSet() const {
94 return instruction_set_;
95}
96
97const std::vector<uint8_t>& CompiledMethod::GetCode() const {
98 return code_;
99}
100
101size_t CompiledMethod::GetFrameSizeInBytes() const {
102 return frame_size_in_bytes_;
103}
104
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700105uint32_t CompiledMethod::GetCoreSpillMask() const {
106 return core_spill_mask_;
107}
108
109uint32_t CompiledMethod::GetFpSpillMask() const {
110 return fp_spill_mask_;
111}
112
113const std::vector<uint32_t>& CompiledMethod::GetMappingTable() const {
114 return mapping_table_;
115}
116
117const std::vector<uint16_t>& CompiledMethod::GetVmapTable() const {
118 return vmap_table_;
119}
120
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800121const std::vector<uint8_t>& CompiledMethod::GetGcMap() const {
122 return gc_map_;
123}
124
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700125uint32_t CompiledMethod::AlignCode(uint32_t offset) const {
126 return AlignCode(offset, instruction_set_);
127}
128
129uint32_t CompiledMethod::AlignCode(uint32_t offset, InstructionSet instruction_set) {
130 switch (instruction_set) {
131 case kArm:
132 case kThumb2:
133 return RoundUp(offset, kArmAlignment);
134 case kX86:
Ian Rogersb41b33b2012-03-20 14:22:54 -0700135 return RoundUp(offset, kX86Alignment);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700136 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800137 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700138 return 0;
139 }
140}
141
142size_t CompiledMethod::CodeDelta() const {
143 switch (instruction_set_) {
144 case kArm:
145 case kX86:
146 return 0;
147 case kThumb2: {
148 // +1 to set the low-order bit so a BLX will switch to Thumb mode
149 return 1;
150 }
151 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800152 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set_);
Brian Carlstrom413f9e02012-01-09 22:24:30 -0800153 return 0;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700154 }
155}
156
157const void* CompiledMethod::CodePointer(const void* code_pointer,
158 InstructionSet instruction_set) {
159 switch (instruction_set) {
160 case kArm:
161 case kX86:
162 return code_pointer;
163 case kThumb2: {
164 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
165 // Set the low-order bit so a BLX will switch to Thumb mode
166 address |= 0x1;
167 return reinterpret_cast<const void*>(address);
168 }
169 default:
Elliott Hughesba8eee12012-01-24 20:25:24 -0800170 LOG(FATAL) << "Unknown InstructionSet: " << static_cast<int>(instruction_set);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700171 return NULL;
172 }
173}
174
Logan Chienf04364f2012-02-10 12:01:39 +0800175#if defined(ART_USE_LLVM_COMPILER)
Logan Chien6920bce2012-03-17 21:44:01 +0800176CompiledInvokeStub::CompiledInvokeStub(size_t elf_idx) : elf_idx_(elf_idx) {
Logan Chienf04364f2012-02-10 12:01:39 +0800177}
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800178#endif
Logan Chien6920bce2012-03-17 21:44:01 +0800179
180CompiledInvokeStub::CompiledInvokeStub(std::vector<uint8_t>& code)
181 : elf_idx_(-1) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700182 CHECK_NE(code.size(), 0U);
183 code_ = code;
184}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700185
186CompiledInvokeStub::~CompiledInvokeStub() {}
187
188const std::vector<uint8_t>& CompiledInvokeStub::GetCode() const {
189 return code_;
190}
191
192} // namespace art