blob: d9edc6bd556968d1f2e398cb4260ec54ce18c692 [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
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"
Dave Allisonf9487c02014-04-08 23:08:12 +000026#include "final_relocations.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070027
Shih-wei Liaod1fec812012-02-13 09:51:10 -080028namespace llvm {
29 class Function;
Ian Rogersa1827042013-04-18 16:36:43 -070030} // namespace llvm
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031
Brian Carlstrom3320cf42011-10-04 14:58:28 -070032namespace art {
33
Mathieu Chartier193bad92013-08-29 18:46:00 -070034class CompilerDriver;
Dave Allisonf9487c02014-04-08 23:08:12 +000035class OatWriter;
Mathieu Chartier193bad92013-08-29 18:46:00 -070036
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,
Dave Allisonf9487c02014-04-08 23:08:12 +000041 const std::vector<uint8_t>& quick_code,
42 const FinalRelocations* relocations);
Logan Chien598c5132012-04-28 22:00:44 +080043
Brian Carlstrom265091e2013-01-30 14:08:26 -080044 // For Portable to supply an ELF object
Mathieu Chartier193bad92013-08-29 18:46:00 -070045 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
46 const std::string& elf_object, const std::string &symbol);
Logan Chien598c5132012-04-28 22:00:44 +080047
Logan Chien598c5132012-04-28 22:00:44 +080048 InstructionSet GetInstructionSet() const {
49 return instruction_set_;
50 }
51
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 const std::vector<uint8_t>* GetPortableCode() const {
53 return portable_code_;
Logan Chien598c5132012-04-28 22:00:44 +080054 }
55
Ian Rogersef7d42f2014-01-06 12:55:46 -080056 const std::vector<uint8_t>* GetQuickCode() const {
57 return quick_code_;
Logan Chien598c5132012-04-28 22:00:44 +080058 }
59
Ian Rogersef7d42f2014-01-06 12:55:46 -080060 void SetCode(const std::vector<uint8_t>* quick_code, const std::vector<uint8_t>* portable_code);
61
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;
73
74 // Returns a pointer suitable for invoking the code at the argument
75 // code_pointer address. Mainly to cope with kThumb2 where the
76 // lower bit must be set to indicate Thumb mode.
77 static const void* CodePointer(const void* code_pointer,
78 InstructionSet instruction_set);
79
Brian Carlstrom265091e2013-01-30 14:08:26 -080080 const std::string& GetSymbol() const;
81 const std::vector<uint32_t>& GetOatdataOffsetsToCompliledCodeOffset() const;
82 void AddOatdataOffsetToCompliledCodeOffset(uint32_t offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -080083
Dave Allisonf9487c02014-04-08 23:08:12 +000084 // Apply all the final relocations to the quick code sequence.
85 void ApplyFinalRelocations(const OatWriter *writer, uint32_t address) {
86 if (final_relocations_.get() != nullptr) {
87 final_relocations_->Apply(&(*quick_code_)[0], writer, address);
88 }
89 }
90
Logan Chien598c5132012-04-28 22:00:44 +080091 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 CompilerDriver* const compiler_driver_;
Mathieu Chartier193bad92013-08-29 18:46:00 -070093
Logan Chien598c5132012-04-28 22:00:44 +080094 const InstructionSet instruction_set_;
Brian Carlstrom8227cc12013-03-06 14:26:48 -080095
Ian Rogersef7d42f2014-01-06 12:55:46 -080096 // The ELF image for portable.
97 std::vector<uint8_t>* portable_code_;
98
99 // Used to store the PIC code for Quick.
100 std::vector<uint8_t>* quick_code_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800101
102 // Used for the Portable ELF symbol name.
Ian Rogersa1827042013-04-18 16:36:43 -0700103 const std::string symbol_;
Brian Carlstrom265091e2013-01-30 14:08:26 -0800104
105 // There are offsets from the oatdata symbol to where the offset to
106 // the compiled method will be found. These are computed by the
107 // OatWriter and then used by the ElfWriter to add relocations so
108 // that MCLinker can update the values to the location in the linked .so.
109 std::vector<uint32_t> oatdata_offsets_to_compiled_code_offset_;
Dave Allisonf9487c02014-04-08 23:08:12 +0000110
111 // Set of relocations to apply as the final pass. This happens
112 // only when the the final oat file addresses are known.
113 UniquePtr<const FinalRelocations> final_relocations_;
Logan Chien598c5132012-04-28 22:00:44 +0800114};
115
116class CompiledMethod : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700117 public:
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700118 // Constructs a CompiledMethod for the non-LLVM compilers.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700119 CompiledMethod(CompilerDriver& driver,
120 InstructionSet instruction_set,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800121 const std::vector<uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700122 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700123 const uint32_t core_spill_mask,
124 const uint32_t fp_spill_mask,
Ian Rogers96faf5b2013-08-09 22:05:32 -0700125 const std::vector<uint8_t>& mapping_table,
126 const std::vector<uint8_t>& vmap_table,
Mark Mendellae9fd932014-02-10 16:14:35 -0800127 const std::vector<uint8_t>& native_gc_map,
Dave Allisonf9487c02014-04-08 23:08:12 +0000128 const std::vector<uint8_t>* cfi_info,
129 const FinalRelocations* relocations);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700130
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 // Constructs a CompiledMethod for the QuickJniCompiler.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700132 CompiledMethod(CompilerDriver& driver,
133 InstructionSet instruction_set,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 const std::vector<uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700135 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700136 const uint32_t core_spill_mask,
137 const uint32_t fp_spill_mask);
138
Brian Carlstrom265091e2013-01-30 14:08:26 -0800139 // Constructs a CompiledMethod for the Portable compiler.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700140 CompiledMethod(CompilerDriver& driver, InstructionSet instruction_set, const std::string& code,
141 const std::vector<uint8_t>& gc_map, const std::string& symbol);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800142
Brian Carlstrom265091e2013-01-30 14:08:26 -0800143 // Constructs a CompiledMethod for the Portable JniCompiler.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700144 CompiledMethod(CompilerDriver& driver, InstructionSet instruction_set, const std::string& code,
145 const std::string& symbol);
Logan Chien6920bce2012-03-17 21:44:01 +0800146
Ian Rogers0c7abda2012-09-19 13:33:42 -0700147 ~CompiledMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148
Ian Rogers0c7abda2012-09-19 13:33:42 -0700149 size_t GetFrameSizeInBytes() const {
150 return frame_size_in_bytes_;
Logan Chien110bcba2012-04-16 19:11:28 +0800151 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700152
153 uint32_t GetCoreSpillMask() const {
154 return core_spill_mask_;
155 }
156
157 uint32_t GetFpSpillMask() const {
158 return fp_spill_mask_;
159 }
160
Ian Rogers96faf5b2013-08-09 22:05:32 -0700161 const std::vector<uint8_t>& GetMappingTable() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700162 DCHECK(mapping_table_ != nullptr);
163 return *mapping_table_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700164 }
165
Ian Rogers96faf5b2013-08-09 22:05:32 -0700166 const std::vector<uint8_t>& GetVmapTable() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700167 DCHECK(vmap_table_ != nullptr);
168 return *vmap_table_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700169 }
170
Ian Rogersa1827042013-04-18 16:36:43 -0700171 const std::vector<uint8_t>& GetGcMap() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700172 DCHECK(gc_map_ != nullptr);
173 return *gc_map_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700174 }
Logan Chien110bcba2012-04-16 19:11:28 +0800175
Mark Mendellae9fd932014-02-10 16:14:35 -0800176 const std::vector<uint8_t>* GetCFIInfo() const {
177 return cfi_info_;
178 }
179
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700180 private:
Ian Rogersa1827042013-04-18 16:36:43 -0700181 // For quick code, the size of the activation used by the code.
Ian Rogers0c7abda2012-09-19 13:33:42 -0700182 const size_t frame_size_in_bytes_;
Ian Rogersa1827042013-04-18 16:36:43 -0700183 // For quick code, a bit mask describing spilled GPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800184 const uint32_t core_spill_mask_;
Ian Rogersa1827042013-04-18 16:36:43 -0700185 // For quick code, a bit mask describing spilled FPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800186 const uint32_t fp_spill_mask_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700187 // For quick code, a uleb128 encoded map from native PC offset to dex PC aswell as dex PC to
188 // native PC offset. Size prefixed.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700189 std::vector<uint8_t>* mapping_table_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700190 // For quick code, a uleb128 encoded map from GPR/FPR register to dex register. Size prefixed.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700191 std::vector<uint8_t>* vmap_table_;
Ian Rogersa1827042013-04-18 16:36:43 -0700192 // For quick code, a map keyed by native PC indices to bitmaps describing what dalvik registers
193 // are live. For portable code, the key is a dalvik PC.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700194 std::vector<uint8_t>* gc_map_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800195 // For quick code, a FDE entry for the debug_frame section.
196 std::vector<uint8_t>* cfi_info_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700197};
198
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700199} // namespace art
200
Mathieu Chartier193bad92013-08-29 18:46:00 -0700201#endif // ART_COMPILER_COMPILED_METHOD_H_