blob: acdce260e511f992d39b673d176d103be82c05c0 [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <memory>
Brian Carlstrom265091e2013-01-30 14:08:26 -080021#include <string>
Brian Carlstrom3320cf42011-10-04 14:58:28 -070022#include <vector>
23
Ian Rogersd582fa42014-11-05 23:46:43 -080024#include "arch/instruction_set.h"
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000025#include "base/bit_field.h"
26#include "base/bit_utils.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070027
28namespace art {
29
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010030template <typename T> class ArrayRef;
Mathieu Chartier193bad92013-08-29 18:46:00 -070031class CompilerDriver;
Vladimir Marko35831e82015-09-11 11:59:18 +010032class CompiledMethodStorage;
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033template<typename T> class LengthPrefixedArray;
34
35namespace linker {
36class LinkerPatch;
37} // namespace linker
Mathieu Chartier193bad92013-08-29 18:46:00 -070038
Logan Chien598c5132012-04-28 22:00:44 +080039class CompiledCode {
40 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080041 // For Quick to supply an code blob
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010042 CompiledCode(CompilerDriver* compiler_driver,
43 InstructionSet instruction_set,
Vladimir Marko35831e82015-09-11 11:59:18 +010044 const ArrayRef<const uint8_t>& quick_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080045
46 virtual ~CompiledCode();
Logan Chien598c5132012-04-28 22:00:44 +080047
Logan Chien598c5132012-04-28 22:00:44 +080048 InstructionSet GetInstructionSet() const {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000049 return GetPackedField<InstructionSetField>();
Logan Chien598c5132012-04-28 22:00:44 +080050 }
51
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010052 ArrayRef<const uint8_t> GetQuickCode() const;
Logan Chien598c5132012-04-28 22:00:44 +080053
Ian Rogersef7d42f2014-01-06 12:55:46 -080054 bool operator==(const CompiledCode& rhs) const;
55
Logan Chien598c5132012-04-28 22:00:44 +080056 // To align an offset from a page-aligned value to make it suitable
57 // for code storage. For example on ARM, to ensure that PC relative
58 // valu computations work out as expected.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080059 size_t AlignCode(size_t offset) const;
60 static size_t AlignCode(size_t offset, InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080061
62 // returns the difference between the code address and a usable PC.
63 // mainly to cope with kThumb2 where the lower bit must be set.
64 size_t CodeDelta() const;
Dave Allison50abf0a2014-06-23 13:19:59 -070065 static size_t CodeDelta(InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080066
67 // Returns a pointer suitable for invoking the code at the argument
68 // code_pointer address. Mainly to cope with kThumb2 where the
69 // lower bit must be set to indicate Thumb mode.
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010070 static const void* CodePointer(const void* code_pointer, InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080071
Vladimir Marko35831e82015-09-11 11:59:18 +010072 protected:
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000073 static constexpr size_t kInstructionSetFieldSize =
74 MinimumBitsToStore(static_cast<size_t>(InstructionSet::kLast));
75 static constexpr size_t kNumberOfCompiledCodePackedBits = kInstructionSetFieldSize;
76 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
77
Vladimir Marko35831e82015-09-11 11:59:18 +010078 template <typename T>
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010079 static ArrayRef<const T> GetArray(const LengthPrefixedArray<T>* array);
Vladimir Marko35831e82015-09-11 11:59:18 +010080
81 CompilerDriver* GetCompilerDriver() {
82 return compiler_driver_;
83 }
Brian Carlstrom265091e2013-01-30 14:08:26 -080084
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000085 template <typename BitFieldType>
86 typename BitFieldType::value_type GetPackedField() const {
87 return BitFieldType::Decode(packed_fields_);
88 }
89
90 template <typename BitFieldType>
91 void SetPackedField(typename BitFieldType::value_type value) {
92 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
93 packed_fields_ = BitFieldType::Update(value, packed_fields_);
94 }
95
Logan Chien598c5132012-04-28 22:00:44 +080096 private:
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000097 using InstructionSetField = BitField<InstructionSet, 0u, kInstructionSetFieldSize>;
98
Ian Rogersef7d42f2014-01-06 12:55:46 -080099 CompilerDriver* const compiler_driver_;
Mathieu Chartier193bad92013-08-29 18:46:00 -0700100
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000101 // Used to store the compiled code.
Vladimir Marko35831e82015-09-11 11:59:18 +0100102 const LengthPrefixedArray<uint8_t>* const quick_code_;
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000103
104 uint32_t packed_fields_;
Logan Chien598c5132012-04-28 22:00:44 +0800105};
106
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700107class CompiledMethod FINAL : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700108 public:
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800109 // Constructs a CompiledMethod.
110 // Note: Consider using the static allocation methods below that will allocate the CompiledMethod
111 // in the swap space.
Ian Rogers72d32622014-05-06 16:20:11 -0700112 CompiledMethod(CompilerDriver* driver,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700113 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800114 const ArrayRef<const uint8_t>& quick_code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700115 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700116 const uint32_t core_spill_mask,
117 const uint32_t fp_spill_mask,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700118 const ArrayRef<const uint8_t>& method_info,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800119 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800120 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100121 const ArrayRef<const linker::LinkerPatch>& patches);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700122
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800123 virtual ~CompiledMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700124
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800125 static CompiledMethod* SwapAllocCompiledMethod(
126 CompilerDriver* driver,
127 InstructionSet instruction_set,
128 const ArrayRef<const uint8_t>& quick_code,
129 const size_t frame_size_in_bytes,
130 const uint32_t core_spill_mask,
131 const uint32_t fp_spill_mask,
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700132 const ArrayRef<const uint8_t>& method_info,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800133 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800134 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100135 const ArrayRef<const linker::LinkerPatch>& patches);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800136
137 static void ReleaseSwapAllocatedCompiledMethod(CompilerDriver* driver, CompiledMethod* m);
138
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000139 bool IsIntrinsic() const {
140 return GetPackedField<IsIntrinsicField>();
141 }
142
143 // Marks the compiled method as being generated using an intrinsic codegen.
144 // Such methods have no relationships to their code items.
145 // This affects debug information generated at link time.
146 void MarkAsIntrinsic() {
147 DCHECK(!IsIntrinsic());
148 SetPackedField<IsIntrinsicField>(/* value */ true);
149 }
150
Ian Rogers0c7abda2012-09-19 13:33:42 -0700151 size_t GetFrameSizeInBytes() const {
152 return frame_size_in_bytes_;
Logan Chien110bcba2012-04-16 19:11:28 +0800153 }
Ian Rogers0c7abda2012-09-19 13:33:42 -0700154
155 uint32_t GetCoreSpillMask() const {
156 return core_spill_mask_;
157 }
158
159 uint32_t GetFpSpillMask() const {
160 return fp_spill_mask_;
161 }
162
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100163 ArrayRef<const uint8_t> GetMethodInfo() const;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700164
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100165 ArrayRef<const uint8_t> GetVmapTable() const;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700166
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100167 ArrayRef<const uint8_t> GetCFIInfo() const;
Mark Mendellae9fd932014-02-10 16:14:35 -0800168
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100169 ArrayRef<const linker::LinkerPatch> GetPatches() const;
Vladimir Markof4da6752014-08-01 19:04:18 +0100170
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700171 private:
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000172 static constexpr size_t kIsIntrinsicLsb = kNumberOfCompiledCodePackedBits;
173 static constexpr size_t kIsIntrinsicSize = 1u;
174 static constexpr size_t kNumberOfCompiledMethodPackedBits = kIsIntrinsicLsb + kIsIntrinsicSize;
175 static_assert(kNumberOfCompiledMethodPackedBits <= CompiledCode::kMaxNumberOfPackedBits,
176 "Too many packed fields.");
177
178 using IsIntrinsicField = BitField<bool, kIsIntrinsicLsb, kIsIntrinsicSize>;
179
Ian Rogersa1827042013-04-18 16:36:43 -0700180 // For quick code, the size of the activation used by the code.
Ian Rogers0c7abda2012-09-19 13:33:42 -0700181 const size_t frame_size_in_bytes_;
Ian Rogersa1827042013-04-18 16:36:43 -0700182 // For quick code, a bit mask describing spilled GPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800183 const uint32_t core_spill_mask_;
Ian Rogersa1827042013-04-18 16:36:43 -0700184 // For quick code, a bit mask describing spilled FPR callee-save registers.
Ian Rogers169c9a72011-11-13 20:13:17 -0800185 const uint32_t fp_spill_mask_;
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700186 // For quick code, method specific information that is not very dedupe friendly (method indices).
187 const LengthPrefixedArray<uint8_t>* const method_info_;
188 // For quick code, holds code infos which contain stack maps, inline information, and etc.
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800189 const LengthPrefixedArray<uint8_t>* const vmap_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800190 // For quick code, a FDE entry for the debug_frame section.
Vladimir Marko35831e82015-09-11 11:59:18 +0100191 const LengthPrefixedArray<uint8_t>* const cfi_info_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100192 // For quick code, linker patches needed by the method.
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100193 const LengthPrefixedArray<linker::LinkerPatch>* const patches_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700194};
195
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700196} // namespace art
197
Mathieu Chartier193bad92013-08-29 18:46:00 -0700198#endif // ART_COMPILER_COMPILED_METHOD_H_