blob: 2d95851ac3be87f6b6b1dc277b78ffa8a3d59435 [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
Mathieu Chartier193bad92013-08-29 18:46:00 -070017#ifndef ART_COMPILER_COMPILED_METHOD_H_
18#define ART_COMPILER_COMPILED_METHOD_H_
Brian Carlstrom3320cf42011-10-04 14:58:28 -070019
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Brian Carlstrom265091e2013-01-30 14:08:26 -080021#include <string>
Brian Carlstrom3320cf42011-10-04 14:58:28 -070022#include <vector>
23
Elliott Hughes0f3c5532012-03-30 14:51:51 -070024#include "instruction_set.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070025#include "utils.h"
Andreas Gampe62746d82014-12-08 16:59:43 -080026#include "utils/array_ref.h"
27#include "utils/swap_space.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070028
Shih-wei Liaod1fec812012-02-13 09:51:10 -080029namespace llvm {
30 class Function;
Ian Rogersa1827042013-04-18 16:36:43 -070031} // namespace llvm
Shih-wei Liaod1fec812012-02-13 09:51:10 -080032
Brian Carlstrom3320cf42011-10-04 14:58:28 -070033namespace art {
34
Mathieu Chartier193bad92013-08-29 18:46:00 -070035class CompilerDriver;
36
Logan Chien598c5132012-04-28 22:00:44 +080037class CompiledCode {
38 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080039 // For Quick to supply an code blob
Mathieu Chartier193bad92013-08-29 18:46:00 -070040 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
Andreas Gampe62746d82014-12-08 16:59:43 -080041 const ArrayRef<const uint8_t>& quick_code);
Logan Chien598c5132012-04-28 22:00:44 +080042
Brian Carlstrom265091e2013-01-30 14:08:26 -080043 // For Portable to supply an ELF object
Mathieu Chartier193bad92013-08-29 18:46:00 -070044 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
45 const std::string& elf_object, const std::string &symbol);
Logan Chien598c5132012-04-28 22:00:44 +080046
Logan Chien598c5132012-04-28 22:00:44 +080047 InstructionSet GetInstructionSet() const {
48 return instruction_set_;
49 }
50
Andreas Gampe62746d82014-12-08 16:59:43 -080051 const SwapVector<uint8_t>* GetPortableCode() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 return portable_code_;
Logan Chien598c5132012-04-28 22:00:44 +080053 }
54
Andreas Gampe62746d82014-12-08 16:59:43 -080055 const SwapVector<uint8_t>* GetQuickCode() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -080056 return quick_code_;
Logan Chien598c5132012-04-28 22:00:44 +080057 }
58
Andreas Gampe62746d82014-12-08 16:59:43 -080059 void SetCode(const ArrayRef<const uint8_t>* quick_code,
60 const ArrayRef<const uint8_t>* portable_code);
Ian Rogersef7d42f2014-01-06 12:55:46 -080061
62 bool operator==(const CompiledCode& rhs) const;
63
Logan Chien598c5132012-04-28 22:00:44 +080064 // To align an offset from a page-aligned value to make it suitable
65 // for code storage. For example on ARM, to ensure that PC relative
66 // valu computations work out as expected.
67 uint32_t AlignCode(uint32_t offset) const;
68 static uint32_t AlignCode(uint32_t offset, InstructionSet instruction_set);
69
70 // returns the difference between the code address and a usable PC.
71 // mainly to cope with kThumb2 where the lower bit must be set.
72 size_t CodeDelta() const;
Dave Allison50abf0a2014-06-23 13:19:59 -070073 static size_t CodeDelta(InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080074
75 // Returns a pointer suitable for invoking the code at the argument
76 // code_pointer address. Mainly to cope with kThumb2 where the
77 // lower bit must be set to indicate Thumb mode.
78 static const void* CodePointer(const void* code_pointer,
79 InstructionSet instruction_set);
80
Brian Carlstrom265091e2013-01-30 14:08:26 -080081 const std::string& GetSymbol() const;
82 const std::vector<uint32_t>& GetOatdataOffsetsToCompliledCodeOffset() const;
83 void AddOatdataOffsetToCompliledCodeOffset(uint32_t offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -080084
Logan Chien598c5132012-04-28 22:00:44 +080085 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 CompilerDriver* const compiler_driver_;
Mathieu Chartier193bad92013-08-29 18:46:00 -070087
Logan Chien598c5132012-04-28 22:00:44 +080088 const InstructionSet instruction_set_;
Brian Carlstrom8227cc12013-03-06 14:26:48 -080089
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 // The ELF image for portable.
Andreas Gampe62746d82014-12-08 16:59:43 -080091 SwapVector<uint8_t>* portable_code_;
Ian Rogersef7d42f2014-01-06 12:55:46 -080092
93 // Used to store the PIC code for Quick.
Andreas Gampe62746d82014-12-08 16:59:43 -080094 SwapVector<uint8_t>* quick_code_;
Brian Carlstrom265091e2013-01-30 14:08:26 -080095
96 // Used for the Portable ELF symbol name.
Ian Rogersa1827042013-04-18 16:36:43 -070097 const std::string symbol_;
Brian Carlstrom265091e2013-01-30 14:08:26 -080098
99 // There are offsets from the oatdata symbol to where the offset to
100 // the compiled method will be found. These are computed by the
101 // OatWriter and then used by the ElfWriter to add relocations so
102 // that MCLinker can update the values to the location in the linked .so.
103 std::vector<uint32_t> oatdata_offsets_to_compiled_code_offset_;
Logan Chien598c5132012-04-28 22:00:44 +0800104};
105
106class CompiledMethod : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700107 public:
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700108 // Constructs a CompiledMethod for the non-LLVM compilers.
Ian Rogers72d32622014-05-06 16:20:11 -0700109 CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700110 InstructionSet instruction_set,
Andreas Gampe62746d82014-12-08 16:59:43 -0800111 const ArrayRef<const uint8_t>& quick_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,
Andreas Gampe62746d82014-12-08 16:59:43 -0800115 const ArrayRef<const uint8_t>& mapping_table,
116 const ArrayRef<const uint8_t>& vmap_table,
117 const ArrayRef<const uint8_t>& native_gc_map,
118 const ArrayRef<const uint8_t>& cfi_info);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700119
Ian Rogersef7d42f2014-01-06 12:55:46 -0800120 // Constructs a CompiledMethod for the QuickJniCompiler.
Ian Rogers72d32622014-05-06 16:20:11 -0700121 CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700122 InstructionSet instruction_set,
Andreas Gampe62746d82014-12-08 16:59:43 -0800123 const ArrayRef<const uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700124 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700125 const uint32_t core_spill_mask,
126 const uint32_t fp_spill_mask);
127
Brian Carlstrom265091e2013-01-30 14:08:26 -0800128 // Constructs a CompiledMethod for the Portable compiler.
Ian Rogers72d32622014-05-06 16:20:11 -0700129 CompiledMethod(CompilerDriver* driver, InstructionSet instruction_set, const std::string& code,
Andreas Gampe62746d82014-12-08 16:59:43 -0800130 const ArrayRef<const uint8_t>& gc_map, const std::string& symbol);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800131
Brian Carlstrom265091e2013-01-30 14:08:26 -0800132 // Constructs a CompiledMethod for the Portable JniCompiler.
Ian Rogers72d32622014-05-06 16:20:11 -0700133 CompiledMethod(CompilerDriver* driver, InstructionSet instruction_set, const std::string& code,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700134 const std::string& symbol);
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
Andreas Gampe62746d82014-12-08 16:59:43 -0800138 static CompiledMethod* SwapAllocCompiledMethod(CompilerDriver* driver,
139 InstructionSet instruction_set,
140 const ArrayRef<const uint8_t>& quick_code,
141 const size_t frame_size_in_bytes,
142 const uint32_t core_spill_mask,
143 const uint32_t fp_spill_mask,
144 const ArrayRef<const uint8_t>& mapping_table,
145 const ArrayRef<const uint8_t>& vmap_table,
146 const ArrayRef<const uint8_t>& native_gc_map,
147 const ArrayRef<const uint8_t>& cfi_info);
148
149 static CompiledMethod* SwapAllocCompiledMethod(CompilerDriver* driver,
150 InstructionSet instruction_set,
151 const ArrayRef<const uint8_t>& quick_code,
152 const size_t frame_size_in_bytes,
153 const uint32_t core_spill_mask,
154 const uint32_t fp_spill_mask);
155
156 static void ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m);
157
Ian Rogers0c7abda2012-09-19 13:33:42 -0700158 size_t GetFrameSizeInBytes() const {
159 return frame_size_in_bytes_;
Logan Chien110bcba2012-04-16 19:11:28 +0800160 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700161
162 uint32_t GetCoreSpillMask() const {
163 return core_spill_mask_;
164 }
165
166 uint32_t GetFpSpillMask() const {
167 return fp_spill_mask_;
168 }
169
Andreas Gampe62746d82014-12-08 16:59:43 -0800170 const SwapVector<uint8_t>& GetMappingTable() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700171 DCHECK(mapping_table_ != nullptr);
172 return *mapping_table_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700173 }
174
Andreas Gampe62746d82014-12-08 16:59:43 -0800175 const SwapVector<uint8_t>& GetVmapTable() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700176 DCHECK(vmap_table_ != nullptr);
177 return *vmap_table_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700178 }
179
Andreas Gampe62746d82014-12-08 16:59:43 -0800180 const SwapVector<uint8_t>& GetGcMap() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700181 DCHECK(gc_map_ != nullptr);
182 return *gc_map_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700183 }
Logan Chien110bcba2012-04-16 19:11:28 +0800184
Andreas Gampe62746d82014-12-08 16:59:43 -0800185 const SwapVector<uint8_t>* GetCFIInfo() const {
Mark Mendellae9fd932014-02-10 16:14:35 -0800186 return cfi_info_;
187 }
188
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700189 private:
Ian Rogersa1827042013-04-18 16:36:43 -0700190 // For quick code, the size of the activation used by the code.
Ian Rogers0c7abda2012-09-19 13:33:42 -0700191 const size_t frame_size_in_bytes_;
Ian Rogersa1827042013-04-18 16:36:43 -0700192 // For quick code, a bit mask describing spilled GPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800193 const uint32_t core_spill_mask_;
Ian Rogersa1827042013-04-18 16:36:43 -0700194 // For quick code, a bit mask describing spilled FPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800195 const uint32_t fp_spill_mask_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700196 // For quick code, a uleb128 encoded map from native PC offset to dex PC aswell as dex PC to
197 // native PC offset. Size prefixed.
Andreas Gampe62746d82014-12-08 16:59:43 -0800198 SwapVector<uint8_t>* mapping_table_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700199 // For quick code, a uleb128 encoded map from GPR/FPR register to dex register. Size prefixed.
Andreas Gampe62746d82014-12-08 16:59:43 -0800200 SwapVector<uint8_t>* vmap_table_;
Ian Rogersa1827042013-04-18 16:36:43 -0700201 // For quick code, a map keyed by native PC indices to bitmaps describing what dalvik registers
202 // are live. For portable code, the key is a dalvik PC.
Andreas Gampe62746d82014-12-08 16:59:43 -0800203 SwapVector<uint8_t>* gc_map_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800204 // For quick code, a FDE entry for the debug_frame section.
Andreas Gampe62746d82014-12-08 16:59:43 -0800205 SwapVector<uint8_t>* cfi_info_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700206};
207
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700208} // namespace art
209
Mathieu Chartier193bad92013-08-29 18:46:00 -0700210#endif // ART_COMPILER_COMPILED_METHOD_H_