blob: e92777ff123a6fed11a697aeb861ab92f20ab29b [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;
Vladimir Marko35831e82015-09-11 11:59:18 +010031class CompiledMethodStorage;
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032template<typename T> class LengthPrefixedArray;
33
34namespace linker {
35class LinkerPatch;
36} // namespace linker
Mathieu Chartier193bad92013-08-29 18:46:00 -070037
Logan Chien598c5132012-04-28 22:00:44 +080038class CompiledCode {
39 public:
Brian Carlstrom265091e2013-01-30 14:08:26 -080040 // For Quick to supply an code blob
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000041 CompiledCode(CompiledMethodStorage* storage,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010042 InstructionSet instruction_set,
Vladimir Marko35831e82015-09-11 11:59:18 +010043 const ArrayRef<const uint8_t>& quick_code);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080044
45 virtual ~CompiledCode();
Logan Chien598c5132012-04-28 22:00:44 +080046
Logan Chien598c5132012-04-28 22:00:44 +080047 InstructionSet GetInstructionSet() const {
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000048 return GetPackedField<InstructionSetField>();
Logan Chien598c5132012-04-28 22:00:44 +080049 }
50
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010051 ArrayRef<const uint8_t> GetQuickCode() const;
Logan Chien598c5132012-04-28 22:00:44 +080052
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 bool operator==(const CompiledCode& rhs) const;
54
Logan Chien598c5132012-04-28 22:00:44 +080055 // To align an offset from a page-aligned value to make it suitable
56 // for code storage. For example on ARM, to ensure that PC relative
57 // valu computations work out as expected.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080058 size_t AlignCode(size_t offset) const;
59 static size_t AlignCode(size_t offset, InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080060
61 // returns the difference between the code address and a usable PC.
62 // mainly to cope with kThumb2 where the lower bit must be set.
63 size_t CodeDelta() const;
Dave Allison50abf0a2014-06-23 13:19:59 -070064 static size_t CodeDelta(InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080065
66 // Returns a pointer suitable for invoking the code at the argument
67 // code_pointer address. Mainly to cope with kThumb2 where the
68 // lower bit must be set to indicate Thumb mode.
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010069 static const void* CodePointer(const void* code_pointer, InstructionSet instruction_set);
Logan Chien598c5132012-04-28 22:00:44 +080070
Vladimir Marko35831e82015-09-11 11:59:18 +010071 protected:
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000072 static constexpr size_t kInstructionSetFieldSize =
73 MinimumBitsToStore(static_cast<size_t>(InstructionSet::kLast));
74 static constexpr size_t kNumberOfCompiledCodePackedBits = kInstructionSetFieldSize;
75 static constexpr size_t kMaxNumberOfPackedBits = sizeof(uint32_t) * kBitsPerByte;
76
Vladimir Marko35831e82015-09-11 11:59:18 +010077 template <typename T>
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010078 static ArrayRef<const T> GetArray(const LengthPrefixedArray<T>* array);
Vladimir Marko35831e82015-09-11 11:59:18 +010079
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000080 CompiledMethodStorage* GetStorage() {
81 return storage_;
Vladimir Marko35831e82015-09-11 11:59:18 +010082 }
Brian Carlstrom265091e2013-01-30 14:08:26 -080083
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000084 template <typename BitFieldType>
85 typename BitFieldType::value_type GetPackedField() const {
86 return BitFieldType::Decode(packed_fields_);
87 }
88
89 template <typename BitFieldType>
90 void SetPackedField(typename BitFieldType::value_type value) {
91 DCHECK(IsUint<BitFieldType::size>(static_cast<uintptr_t>(value)));
92 packed_fields_ = BitFieldType::Update(value, packed_fields_);
93 }
94
Logan Chien598c5132012-04-28 22:00:44 +080095 private:
Vladimir Marko92f7f3c2017-10-31 11:38:30 +000096 using InstructionSetField = BitField<InstructionSet, 0u, kInstructionSetFieldSize>;
97
Vladimir Marko33f7c8a2018-11-19 10:22:01 +000098 CompiledMethodStorage* const storage_;
Mathieu Chartier193bad92013-08-29 18:46:00 -070099
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000100 // Used to store the compiled code.
Vladimir Marko35831e82015-09-11 11:59:18 +0100101 const LengthPrefixedArray<uint8_t>* const quick_code_;
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000102
103 uint32_t packed_fields_;
Logan Chien598c5132012-04-28 22:00:44 +0800104};
105
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100106class CompiledMethod final : public CompiledCode {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700107 public:
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800108 // Constructs a CompiledMethod.
109 // Note: Consider using the static allocation methods below that will allocate the CompiledMethod
110 // in the swap space.
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000111 CompiledMethod(CompiledMethodStorage* storage,
Mathieu Chartier193bad92013-08-29 18:46:00 -0700112 InstructionSet instruction_set,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800113 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800114 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800115 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100116 const ArrayRef<const linker::LinkerPatch>& patches);
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700117
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800118 virtual ~CompiledMethod();
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700119
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800120 static CompiledMethod* SwapAllocCompiledMethod(
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000121 CompiledMethodStorage* storage,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800122 InstructionSet instruction_set,
123 const ArrayRef<const uint8_t>& quick_code,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800124 const ArrayRef<const uint8_t>& vmap_table,
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800125 const ArrayRef<const uint8_t>& cfi_info,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100126 const ArrayRef<const linker::LinkerPatch>& patches);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800127
Vladimir Marko33f7c8a2018-11-19 10:22:01 +0000128 static void ReleaseSwapAllocatedCompiledMethod(CompiledMethodStorage* storage, CompiledMethod* m);
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800129
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000130 bool IsIntrinsic() const {
131 return GetPackedField<IsIntrinsicField>();
132 }
133
134 // Marks the compiled method as being generated using an intrinsic codegen.
135 // Such methods have no relationships to their code items.
136 // This affects debug information generated at link time.
137 void MarkAsIntrinsic() {
138 DCHECK(!IsIntrinsic());
Andreas Gampe3db70682018-12-26 15:12:03 -0800139 SetPackedField<IsIntrinsicField>(/* value= */ true);
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000140 }
141
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100142 ArrayRef<const uint8_t> GetVmapTable() const;
Ian Rogers0c7abda2012-09-19 13:33:42 -0700143
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100144 ArrayRef<const uint8_t> GetCFIInfo() const;
Mark Mendellae9fd932014-02-10 16:14:35 -0800145
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100146 ArrayRef<const linker::LinkerPatch> GetPatches() const;
Vladimir Markof4da6752014-08-01 19:04:18 +0100147
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700148 private:
Vladimir Marko92f7f3c2017-10-31 11:38:30 +0000149 static constexpr size_t kIsIntrinsicLsb = kNumberOfCompiledCodePackedBits;
150 static constexpr size_t kIsIntrinsicSize = 1u;
151 static constexpr size_t kNumberOfCompiledMethodPackedBits = kIsIntrinsicLsb + kIsIntrinsicSize;
152 static_assert(kNumberOfCompiledMethodPackedBits <= CompiledCode::kMaxNumberOfPackedBits,
153 "Too many packed fields.");
154
155 using IsIntrinsicField = BitField<bool, kIsIntrinsicLsb, kIsIntrinsicSize>;
156
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700157 // For quick code, holds code infos which contain stack maps, inline information, and etc.
Mathieu Chartier279e3a32018-01-24 18:17:55 -0800158 const LengthPrefixedArray<uint8_t>* const vmap_table_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800159 // For quick code, a FDE entry for the debug_frame section.
Vladimir Marko35831e82015-09-11 11:59:18 +0100160 const LengthPrefixedArray<uint8_t>* const cfi_info_;
Vladimir Markof4da6752014-08-01 19:04:18 +0100161 // For quick code, linker patches needed by the method.
Vladimir Markod8dbc8d2017-09-20 13:37:47 +0100162 const LengthPrefixedArray<linker::LinkerPatch>* const patches_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700163};
164
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700165} // namespace art
166
Mathieu Chartier193bad92013-08-29 18:46:00 -0700167#endif // ART_COMPILER_COMPILED_METHOD_H_