blob: 4631cb5db41b9d8eccbd9208bc8278a24804c4fb [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)
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070022 : instruction_set_(instruction_set), code_(code) {
Brian Carlstrom265091e2013-01-30 14:08:26 -080023 CHECK_NE(code.size(), 0U);
24}
25
26CompiledCode::CompiledCode(InstructionSet instruction_set,
27 const std::string& elf_object,
28 const std::string& symbol)
29 : instruction_set_(instruction_set), symbol_(symbol) {
30 CHECK_NE(elf_object.size(), 0U);
31 CHECK_NE(symbol.size(), 0U);
32 // TODO: we shouldn't just shove ELF objects in as "code" but
33 // change to have different kinds of compiled methods. This is
34 // being deferred until we work on hybrid execution or at least
35 // until we work on batch compilation.
36 code_.resize(elf_object.size());
37 memcpy(&code_[0], &elf_object[0], elf_object.size());
38}
39
Logan Chien598c5132012-04-28 22:00:44 +080040uint32_t CompiledCode::AlignCode(uint32_t offset) const {
41 return AlignCode(offset, instruction_set_);
42}
43
44uint32_t CompiledCode::AlignCode(uint32_t offset, InstructionSet instruction_set) {
45 switch (instruction_set) {
46 case kArm:
47 case kThumb2:
48 return RoundUp(offset, kArmAlignment);
49 case kMips:
50 return RoundUp(offset, kMipsAlignment);
51 case kX86:
52 return RoundUp(offset, kX86Alignment);
53 default:
54 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
55 return 0;
56 }
57}
58
59size_t CompiledCode::CodeDelta() const {
60 switch (instruction_set_) {
61 case kArm:
62 case kMips:
63 case kX86:
64 return 0;
65 case kThumb2: {
66 // +1 to set the low-order bit so a BLX will switch to Thumb mode
67 return 1;
68 }
69 default:
70 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set_;
71 return 0;
72 }
73}
74
75const void* CompiledCode::CodePointer(const void* code_pointer,
76 InstructionSet instruction_set) {
77 switch (instruction_set) {
78 case kArm:
79 case kMips:
80 case kX86:
81 return code_pointer;
82 case kThumb2: {
83 uintptr_t address = reinterpret_cast<uintptr_t>(code_pointer);
84 // Set the low-order bit so a BLX will switch to Thumb mode
85 address |= 0x1;
86 return reinterpret_cast<const void*>(address);
87 }
88 default:
89 LOG(FATAL) << "Unknown InstructionSet: " << instruction_set;
90 return NULL;
91 }
92}
93
Brian Carlstrom265091e2013-01-30 14:08:26 -080094#if defined(ART_USE_PORTABLE_COMPILER)
95const std::string& CompiledCode::GetSymbol() const {
96 CHECK_NE(0U, symbol_.size());
97 return symbol_;
98}
99
100const std::vector<uint32_t>& CompiledCode::GetOatdataOffsetsToCompliledCodeOffset() const {
101 CHECK_NE(0U, oatdata_offsets_to_compiled_code_offset_.size()) << symbol_;
102 return oatdata_offsets_to_compiled_code_offset_;
103}
104
105void CompiledCode::AddOatdataOffsetToCompliledCodeOffset(uint32_t offset) {
106 oatdata_offsets_to_compiled_code_offset_.push_back(offset);
107}
108#endif
109
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700110CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Ian Rogersab058bb2012-03-11 22:19:38 -0700111 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700112 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113 const uint32_t core_spill_mask,
114 const uint32_t fp_spill_mask,
Ian Rogers96faf5b2013-08-09 22:05:32 -0700115 const std::vector<uint8_t>& mapping_table,
116 const std::vector<uint8_t>& vmap_table,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700117 const std::vector<uint8_t>& native_gc_map)
Brian Carlstrom265091e2013-01-30 14:08:26 -0800118 : CompiledCode(instruction_set, code), frame_size_in_bytes_(frame_size_in_bytes),
Ian Rogers0c7abda2012-09-19 13:33:42 -0700119 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask),
Ian Rogers96faf5b2013-08-09 22:05:32 -0700120 mapping_table_(mapping_table), vmap_table_(vmap_table),
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700121 gc_map_(native_gc_map) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700122}
123
124CompiledMethod::CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800125 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700126 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700127 const uint32_t core_spill_mask,
Ian Rogers169c9a72011-11-13 20:13:17 -0800128 const uint32_t fp_spill_mask)
Logan Chien598c5132012-04-28 22:00:44 +0800129 : CompiledCode(instruction_set, code),
130 frame_size_in_bytes_(frame_size_in_bytes),
Brian Carlstrom265091e2013-01-30 14:08:26 -0800131 core_spill_mask_(core_spill_mask), fp_spill_mask_(fp_spill_mask) {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700132
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700133} // namespace art