blob: 611230509a8c47a8f08c9e7ee2236f740009c8be [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"
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
Mathieu Chartier193bad92013-08-29 18:46:00 -070033class CompilerDriver;
34
Logan Chien598c5132012-04-28 22:00:44 +080035class CompiledCode {
36 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080037 // For Quick to supply an code blob
Mathieu Chartier193bad92013-08-29 18:46:00 -070038 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
Ian Rogersef7d42f2014-01-06 12:55:46 -080039 const std::vector<uint8_t>& quick_code);
Logan Chien598c5132012-04-28 22:00:44 +080040
Brian Carlstrom265091e2013-01-30 14:08:26 -080041 // For Portable to supply an ELF object
Mathieu Chartier193bad92013-08-29 18:46:00 -070042 CompiledCode(CompilerDriver* compiler_driver, InstructionSet instruction_set,
43 const std::string& elf_object, const std::string &symbol);
Logan Chien598c5132012-04-28 22:00:44 +080044
Logan Chien598c5132012-04-28 22:00:44 +080045 InstructionSet GetInstructionSet() const {
46 return instruction_set_;
47 }
48
Ian Rogersef7d42f2014-01-06 12:55:46 -080049 const std::vector<uint8_t>* GetPortableCode() const {
50 return portable_code_;
Logan Chien598c5132012-04-28 22:00:44 +080051 }
52
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 const std::vector<uint8_t>* GetQuickCode() const {
54 return quick_code_;
Logan Chien598c5132012-04-28 22:00:44 +080055 }
56
Ian Rogersef7d42f2014-01-06 12:55:46 -080057 void SetCode(const std::vector<uint8_t>* quick_code, const std::vector<uint8_t>* portable_code);
58
59 bool operator==(const CompiledCode& rhs) const;
60
Logan Chien598c5132012-04-28 22:00:44 +080061 // To align an offset from a page-aligned value to make it suitable
62 // for code storage. For example on ARM, to ensure that PC relative
63 // valu computations work out as expected.
64 uint32_t AlignCode(uint32_t offset) const;
65 static uint32_t AlignCode(uint32_t offset, InstructionSet instruction_set);
66
67 // returns the difference between the code address and a usable PC.
68 // mainly to cope with kThumb2 where the lower bit must be set.
69 size_t CodeDelta() const;
70
71 // Returns a pointer suitable for invoking the code at the argument
72 // code_pointer address. Mainly to cope with kThumb2 where the
73 // lower bit must be set to indicate Thumb mode.
74 static const void* CodePointer(const void* code_pointer,
75 InstructionSet instruction_set);
76
Brian Carlstrom265091e2013-01-30 14:08:26 -080077 const std::string& GetSymbol() const;
78 const std::vector<uint32_t>& GetOatdataOffsetsToCompliledCodeOffset() const;
79 void AddOatdataOffsetToCompliledCodeOffset(uint32_t offset);
Brian Carlstrom265091e2013-01-30 14:08:26 -080080
Logan Chien598c5132012-04-28 22:00:44 +080081 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 CompilerDriver* const compiler_driver_;
Mathieu Chartier193bad92013-08-29 18:46:00 -070083
Logan Chien598c5132012-04-28 22:00:44 +080084 const InstructionSet instruction_set_;
Brian Carlstrom8227cc12013-03-06 14:26:48 -080085
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 // The ELF image for portable.
87 std::vector<uint8_t>* portable_code_;
88
89 // Used to store the PIC code for Quick.
90 std::vector<uint8_t>* quick_code_;
Brian Carlstrom265091e2013-01-30 14:08:26 -080091
92 // Used for the Portable ELF symbol name.
Ian Rogersa1827042013-04-18 16:36:43 -070093 const std::string symbol_;
Brian Carlstrom265091e2013-01-30 14:08:26 -080094
95 // There are offsets from the oatdata symbol to where the offset to
96 // the compiled method will be found. These are computed by the
97 // OatWriter and then used by the ElfWriter to add relocations so
98 // that MCLinker can update the values to the location in the linked .so.
99 std::vector<uint32_t> oatdata_offsets_to_compiled_code_offset_;
Logan Chien598c5132012-04-28 22:00:44 +0800100};
101
102class CompiledMethod : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700103 public:
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -0700104 // Constructs a CompiledMethod for the non-LLVM compilers.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700105 CompiledMethod(CompilerDriver& driver,
106 InstructionSet instruction_set,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107 const std::vector<uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700108 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700109 const uint32_t core_spill_mask,
110 const uint32_t fp_spill_mask,
Ian Rogers96faf5b2013-08-09 22:05:32 -0700111 const std::vector<uint8_t>& mapping_table,
112 const std::vector<uint8_t>& vmap_table,
Ian Rogers0c7abda2012-09-19 13:33:42 -0700113 const std::vector<uint8_t>& native_gc_map);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700114
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115 // Constructs a CompiledMethod for the QuickJniCompiler.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700116 CompiledMethod(CompilerDriver& driver,
117 InstructionSet instruction_set,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800118 const std::vector<uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700119 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700120 const uint32_t core_spill_mask,
121 const uint32_t fp_spill_mask);
122
Brian Carlstrom265091e2013-01-30 14:08:26 -0800123 // Constructs a CompiledMethod for the Portable compiler.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700124 CompiledMethod(CompilerDriver& driver, InstructionSet instruction_set, const std::string& code,
125 const std::vector<uint8_t>& gc_map, const std::string& symbol);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800126
Brian Carlstrom265091e2013-01-30 14:08:26 -0800127 // Constructs a CompiledMethod for the Portable JniCompiler.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700128 CompiledMethod(CompilerDriver& driver, InstructionSet instruction_set, const std::string& code,
129 const std::string& symbol);
Logan Chien6920bce2012-03-17 21:44:01 +0800130
Ian Rogers0c7abda2012-09-19 13:33:42 -0700131 ~CompiledMethod() {}
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700132
Ian Rogers0c7abda2012-09-19 13:33:42 -0700133 size_t GetFrameSizeInBytes() const {
134 return frame_size_in_bytes_;
Logan Chien110bcba2012-04-16 19:11:28 +0800135 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700136
137 uint32_t GetCoreSpillMask() const {
138 return core_spill_mask_;
139 }
140
141 uint32_t GetFpSpillMask() const {
142 return fp_spill_mask_;
143 }
144
Ian Rogers96faf5b2013-08-09 22:05:32 -0700145 const std::vector<uint8_t>& GetMappingTable() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700146 DCHECK(mapping_table_ != nullptr);
147 return *mapping_table_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700148 }
149
Ian Rogers96faf5b2013-08-09 22:05:32 -0700150 const std::vector<uint8_t>& GetVmapTable() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700151 DCHECK(vmap_table_ != nullptr);
152 return *vmap_table_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700153 }
154
Ian Rogersa1827042013-04-18 16:36:43 -0700155 const std::vector<uint8_t>& GetGcMap() const {
Mathieu Chartier193bad92013-08-29 18:46:00 -0700156 DCHECK(gc_map_ != nullptr);
157 return *gc_map_;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700158 }
Logan Chien110bcba2012-04-16 19:11:28 +0800159
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700160 private:
Ian Rogersa1827042013-04-18 16:36:43 -0700161 // For quick code, the size of the activation used by the code.
Ian Rogers0c7abda2012-09-19 13:33:42 -0700162 const size_t frame_size_in_bytes_;
Ian Rogersa1827042013-04-18 16:36:43 -0700163 // For quick code, a bit mask describing spilled GPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800164 const uint32_t core_spill_mask_;
Ian Rogersa1827042013-04-18 16:36:43 -0700165 // For quick code, a bit mask describing spilled FPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800166 const uint32_t fp_spill_mask_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700167 // For quick code, a uleb128 encoded map from native PC offset to dex PC aswell as dex PC to
168 // native PC offset. Size prefixed.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700169 std::vector<uint8_t>* mapping_table_;
Ian Rogers96faf5b2013-08-09 22:05:32 -0700170 // For quick code, a uleb128 encoded map from GPR/FPR register to dex register. Size prefixed.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700171 std::vector<uint8_t>* vmap_table_;
Ian Rogersa1827042013-04-18 16:36:43 -0700172 // For quick code, a map keyed by native PC indices to bitmaps describing what dalvik registers
173 // are live. For portable code, the key is a dalvik PC.
Mathieu Chartier193bad92013-08-29 18:46:00 -0700174 std::vector<uint8_t>* gc_map_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700175};
176
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700177} // namespace art
178
Mathieu Chartier193bad92013-08-29 18:46:00 -0700179#endif // ART_COMPILER_COMPILED_METHOD_H_