blob: b3bb20fac4a3bf468efd51b0a6d0a2a6fdb17d38 [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_COMPILED_METHOD_H_
18#define ART_RUNTIME_COMPILED_METHOD_H_
Brian Carlstrom3320cf42011-10-04 14:58:28 -070019
Brian Carlstrom265091e2013-01-30 14:08:26 -080020#include <string>
Brian Carlstrom3320cf42011-10-04 14:58:28 -070021#include <vector>
22
Elliott Hughes0f3c5532012-03-30 14:51:51 -070023#include "instruction_set.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070024#include "utils.h"
TDYa127eead4ac2012-06-03 07:15:25 -070025#include "UniquePtr.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070026
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027namespace llvm {
28 class Function;
Ian Rogersa1827042013-04-18 16:36:43 -070029} // namespace llvm
Shih-wei Liaod1fec812012-02-13 09:51:10 -080030
Brian Carlstrom3320cf42011-10-04 14:58:28 -070031namespace art {
32
Logan Chien598c5132012-04-28 22:00:44 +080033class CompiledCode {
34 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080035 // For Quick to supply an code blob
36 CompiledCode(InstructionSet instruction_set, const std::vector<uint8_t>& code);
Logan Chien598c5132012-04-28 22:00:44 +080037
Brian Carlstrom265091e2013-01-30 14:08:26 -080038 // For Portable to supply an ELF object
39 CompiledCode(InstructionSet instruction_set,
40 const std::string& elf_object,
41 const std::string &symbol);
Logan Chien598c5132012-04-28 22:00:44 +080042
Logan Chien598c5132012-04-28 22:00:44 +080043 InstructionSet GetInstructionSet() const {
44 return instruction_set_;
45 }
46
47 const std::vector<uint8_t>& GetCode() const {
48 return code_;
49 }
50
51 void SetCode(const std::vector<uint8_t>& code) {
Logan Chien971bf3f2012-05-01 15:47:55 +080052 CHECK_NE(code.size(), 0U);
Logan Chien598c5132012-04-28 22:00:44 +080053 code_ = code;
54 }
55
56 bool operator==(const CompiledCode& rhs) const {
57 return (code_ == rhs.code_);
58 }
59
Logan Chien598c5132012-04-28 22:00:44 +080060 // To align an offset from a page-aligned value to make it suitable
61 // for code storage. For example on ARM, to ensure that PC relative
62 // valu computations work out as expected.
63 uint32_t AlignCode(uint32_t offset) const;
64 static uint32_t AlignCode(uint32_t offset, InstructionSet instruction_set);
65
66 // returns the difference between the code address and a usable PC.
67 // mainly to cope with kThumb2 where the lower bit must be set.
68 size_t CodeDelta() const;
69
70 // Returns a pointer suitable for invoking the code at the argument
71 // code_pointer address. Mainly to cope with kThumb2 where the
72 // lower bit must be set to indicate Thumb mode.
73 static const void* CodePointer(const void* code_pointer,
74 InstructionSet instruction_set);
75
Brian Carlstrom265091e2013-01-30 14:08:26 -080076#if defined(ART_USE_PORTABLE_COMPILER)
77 const std::string& GetSymbol() const;
78 const std::vector<uint32_t>& GetOatdataOffsetsToCompliledCodeOffset() const;
79 void AddOatdataOffsetToCompliledCodeOffset(uint32_t offset);
80#endif
81
Logan Chien598c5132012-04-28 22:00:44 +080082 private:
83 const InstructionSet instruction_set_;
Brian Carlstrom8227cc12013-03-06 14:26:48 -080084
Brian Carlstrom265091e2013-01-30 14:08:26 -080085 // Used to store the PIC code for Quick and an ELF image for portable.
Logan Chien598c5132012-04-28 22:00:44 +080086 std::vector<uint8_t> code_;
Brian Carlstrom265091e2013-01-30 14:08:26 -080087
88 // Used for the Portable ELF symbol name.
Ian Rogersa1827042013-04-18 16:36:43 -070089 const std::string symbol_;
Brian Carlstrom265091e2013-01-30 14:08:26 -080090
91 // There are offsets from the oatdata symbol to where the offset to
92 // the compiled method will be found. These are computed by the
93 // OatWriter and then used by the ElfWriter to add relocations so
94 // that MCLinker can update the values to the location in the linked .so.
95 std::vector<uint32_t> oatdata_offsets_to_compiled_code_offset_;
Logan Chien598c5132012-04-28 22:00:44 +080096};
97
98class CompiledMethod : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -070099 public:
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700100 // Constructs a CompiledMethod for the non-LLVM compilers.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700101 CompiledMethod(InstructionSet instruction_set,
Ian Rogersab058bb2012-03-11 22:19:38 -0700102 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700103 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700104 const uint32_t core_spill_mask,
105 const uint32_t fp_spill_mask,
Ian Rogers96faf5b2013-08-09 22:05:32 -0700106 const std::vector<uint8_t>& mapping_table,
107 const std::vector<uint8_t>& vmap_table,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700108 const std::vector<uint8_t>& native_gc_map);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700109
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700110 // Constructs a CompiledMethod for the JniCompiler.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700111 CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800112 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700113 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700114 const uint32_t core_spill_mask,
115 const uint32_t fp_spill_mask);
116
Brian Carlstrom265091e2013-01-30 14:08:26 -0800117 // Constructs a CompiledMethod for the Portable compiler.
Logan Chien937105a2012-04-02 02:37:37 +0800118 CompiledMethod(InstructionSet instruction_set,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800119 const std::string& code,
120 const std::vector<uint8_t>& gc_map,
121 const std::string& symbol)
122 : CompiledCode(instruction_set, code, symbol),
TDYa127ce4cc0d2012-11-18 16:59:53 -0800123 frame_size_in_bytes_(kStackAlignment), core_spill_mask_(0),
Ian Rogersa1827042013-04-18 16:36:43 -0700124 fp_spill_mask_(0), gc_map_(gc_map) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800125 }
126
Brian Carlstrom265091e2013-01-30 14:08:26 -0800127 // Constructs a CompiledMethod for the Portable JniCompiler.
TDYa127ce4cc0d2012-11-18 16:59:53 -0800128 CompiledMethod(InstructionSet instruction_set,
Brian Carlstrom265091e2013-01-30 14:08:26 -0800129 const std::string& code,
130 const std::string& symbol)
131 : CompiledCode(instruction_set, code, symbol),
Logan Chien598c5132012-04-28 22:00:44 +0800132 frame_size_in_bytes_(kStackAlignment), core_spill_mask_(0),
133 fp_spill_mask_(0) {
134 }
Logan Chien6920bce2012-03-17 21:44:01 +0800135
Ian Rogers0c7abda2012-09-19 13:33:42 -0700136 ~CompiledMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700137
Ian Rogers0c7abda2012-09-19 13:33:42 -0700138 size_t GetFrameSizeInBytes() const {
139 return frame_size_in_bytes_;
Logan Chien110bcba2012-04-16 19:11:28 +0800140 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700141
142 uint32_t GetCoreSpillMask() const {
143 return core_spill_mask_;
144 }
145
146 uint32_t GetFpSpillMask() const {
147 return fp_spill_mask_;
148 }
149
Ian Rogers96faf5b2013-08-09 22:05:32 -0700150 const std::vector<uint8_t>& GetMappingTable() const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700151 return mapping_table_;
152 }
153
Ian Rogers96faf5b2013-08-09 22:05:32 -0700154 const std::vector<uint8_t>& GetVmapTable() const {
Ian Rogers0c7abda2012-09-19 13:33:42 -0700155 return vmap_table_;
156 }
157
Ian Rogersa1827042013-04-18 16:36:43 -0700158 const std::vector<uint8_t>& GetGcMap() const {
159 return gc_map_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700160 }
Logan Chien110bcba2012-04-16 19:11:28 +0800161
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700162 private:
Ian Rogersa1827042013-04-18 16:36:43 -0700163 // For quick code, the size of the activation used by the code.
Ian Rogers0c7abda2012-09-19 13:33:42 -0700164 const size_t frame_size_in_bytes_;
Ian Rogersa1827042013-04-18 16:36:43 -0700165 // For quick code, a bit mask describing spilled GPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800166 const uint32_t core_spill_mask_;
Ian Rogersa1827042013-04-18 16:36:43 -0700167 // For quick code, a bit mask describing spilled FPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800168 const uint32_t fp_spill_mask_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700169 // For quick code, a uleb128 encoded map from native PC offset to dex PC aswell as dex PC to
170 // native PC offset. Size prefixed.
171 std::vector<uint8_t> mapping_table_;
172 // For quick code, a uleb128 encoded map from GPR/FPR register to dex register. Size prefixed.
173 std::vector<uint8_t> vmap_table_;
Ian Rogersa1827042013-04-18 16:36:43 -0700174 // For quick code, a map keyed by native PC indices to bitmaps describing what dalvik registers
175 // are live. For portable code, the key is a dalvik PC.
176 std::vector<uint8_t> gc_map_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700177};
178
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700179} // namespace art
180
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700181#endif // ART_RUNTIME_COMPILED_METHOD_H_