blob: f4362149536930ef307e038683099fbbb6159dac [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
17#ifndef ART_SRC_COMPILED_METHOD_H_
18#define ART_SRC_COMPILED_METHOD_H_
19
20#include <vector>
21
Elliott Hughes0f3c5532012-03-30 14:51:51 -070022#include "instruction_set.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070023#include "utils.h"
TDYa127eead4ac2012-06-03 07:15:25 -070024#include "UniquePtr.h"
Brian Carlstrom3320cf42011-10-04 14:58:28 -070025
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026namespace llvm {
27 class Function;
28}
29
Brian Carlstrom3320cf42011-10-04 14:58:28 -070030namespace art {
31
32class CompiledMethod {
33 public:
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -070034 // Constructs a CompiledMethod for the non-LLVM compilers.
Brian Carlstrom3320cf42011-10-04 14:58:28 -070035 CompiledMethod(InstructionSet instruction_set,
Ian Rogersab058bb2012-03-11 22:19:38 -070036 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070037 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070038 const uint32_t core_spill_mask,
39 const uint32_t fp_spill_mask,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080040 const std::vector<uint32_t>& mapping_table,
41 const std::vector<uint16_t>& vmap_table);
42
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -070043 // Sets the GC map for a CompiledMethod.
Brian Carlstrome7d856b2012-01-11 18:10:55 -080044 void SetGcMap(const std::vector<uint8_t>& gc_map);
Brian Carlstrom3320cf42011-10-04 14:58:28 -070045
Elliott Hughes3fa1b7e2012-03-13 17:06:22 -070046 // Constructs a CompiledMethod for the JniCompiler.
Brian Carlstrom3320cf42011-10-04 14:58:28 -070047 CompiledMethod(InstructionSet instruction_set,
Brian Carlstrome7d856b2012-01-11 18:10:55 -080048 const std::vector<uint8_t>& code,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070049 const size_t frame_size_in_bytes,
Brian Carlstrom3320cf42011-10-04 14:58:28 -070050 const uint32_t core_spill_mask,
51 const uint32_t fp_spill_mask);
52
Logan Chien6920bce2012-03-17 21:44:01 +080053 // Constructs a CompiledMethod for the LLVM compiler.
Logan Chien937105a2012-04-02 02:37:37 +080054 CompiledMethod(InstructionSet instruction_set,
55 const uint16_t elf_idx,
56 const uint16_t elf_func_idx);
Logan Chien6920bce2012-03-17 21:44:01 +080057
Brian Carlstrom3320cf42011-10-04 14:58:28 -070058 ~CompiledMethod();
59
60 InstructionSet GetInstructionSet() const;
61 const std::vector<uint8_t>& GetCode() const;
62 size_t GetFrameSizeInBytes() const;
Brian Carlstrom3320cf42011-10-04 14:58:28 -070063 uint32_t GetCoreSpillMask() const;
64 uint32_t GetFpSpillMask() const;
65 const std::vector<uint32_t>& GetMappingTable() const;
66 const std::vector<uint16_t>& GetVmapTable() const;
Brian Carlstrome7d856b2012-01-11 18:10:55 -080067 const std::vector<uint8_t>& GetGcMap() const;
68
Logan Chien110bcba2012-04-16 19:11:28 +080069#if defined(ART_USE_LLVM_COMPILER)
70 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
71 frame_size_in_bytes_ = new_frame_size_in_bytes;
72 }
73#endif
74
Brian Carlstrom3320cf42011-10-04 14:58:28 -070075 // Aligns an offset from a page aligned value to make it suitable
76 // for code storage. important to ensure that PC relative value
77 // computations work out as expected on ARM.
78 uint32_t AlignCode(uint32_t offset) const;
79 static uint32_t AlignCode(uint32_t offset, InstructionSet instruction_set);
80
81 // returns the difference between the code address and a usable PC.
82 // mainly to cope with kThumb2 where the lower bit must be set.
83 size_t CodeDelta() const;
84
85 // Returns a pointer suitable for invoking the code at the argument
86 // code_pointer address. Mainly to cope with kThumb2 where the
87 // lower bit must be set to indicate Thumb mode.
88 static const void* CodePointer(const void* code_pointer,
89 InstructionSet instruction_set);
90
Logan Chien937105a2012-04-02 02:37:37 +080091 uint16_t GetElfIndex() const {
92 DCHECK(IsExecutableInElf());
Logan Chien6920bce2012-03-17 21:44:01 +080093 return elf_idx_;
94 }
95
Logan Chien937105a2012-04-02 02:37:37 +080096 uint16_t GetElfFuncIndex() const {
97 DCHECK(IsExecutableInElf());
98 return elf_func_idx_;
99 }
100
Logan Chien6920bce2012-03-17 21:44:01 +0800101 bool IsExecutableInElf() const {
Logan Chien937105a2012-04-02 02:37:37 +0800102 return (elf_idx_ != static_cast<uint16_t>(-1u));
Logan Chien6920bce2012-03-17 21:44:01 +0800103 }
104
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700105 private:
Logan Chien6920bce2012-03-17 21:44:01 +0800106 // For non-LLVM
Ian Rogers169c9a72011-11-13 20:13:17 -0800107 const InstructionSet instruction_set_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700108 std::vector<uint8_t> code_;
Logan Chien110bcba2012-04-16 19:11:28 +0800109 size_t frame_size_in_bytes_;
Ian Rogers169c9a72011-11-13 20:13:17 -0800110 const uint32_t core_spill_mask_;
111 const uint32_t fp_spill_mask_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700112 std::vector<uint32_t> mapping_table_;
113 std::vector<uint16_t> vmap_table_;
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800114 std::vector<uint8_t> gc_map_;
Logan Chien6920bce2012-03-17 21:44:01 +0800115 // For LLVM
Logan Chien937105a2012-04-02 02:37:37 +0800116 uint16_t elf_idx_;
117 uint16_t elf_func_idx_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700118};
119
120class CompiledInvokeStub {
121 public:
Shih-wei Liaoc4c98812012-03-10 21:55:51 -0800122 explicit CompiledInvokeStub(std::vector<uint8_t>& code);
Logan Chienf04364f2012-02-10 12:01:39 +0800123#if defined(ART_USE_LLVM_COMPILER)
TDYa127eead4ac2012-06-03 07:15:25 -0700124 explicit CompiledInvokeStub(uint16_t elf_idx);
Logan Chienf04364f2012-02-10 12:01:39 +0800125#endif
Logan Chien6920bce2012-03-17 21:44:01 +0800126 ~CompiledInvokeStub();
127
128 const std::vector<uint8_t>& GetCode() const;
129
TDYa127eead4ac2012-06-03 07:15:25 -0700130 uint16_t GetStubElfIndex() const {
Logan Chien937105a2012-04-02 02:37:37 +0800131 DCHECK(IsExecutableInElf());
Logan Chien6920bce2012-03-17 21:44:01 +0800132 return elf_idx_;
133 }
134
TDYa127eead4ac2012-06-03 07:15:25 -0700135 uint16_t GetInvokeStubElfFuncIndex() const {
Logan Chien937105a2012-04-02 02:37:37 +0800136 DCHECK(IsExecutableInElf());
TDYa127eead4ac2012-06-03 07:15:25 -0700137 return invoke_stub_elf_func_idx_;
138 }
139
140 uint16_t GetProxyStubElfFuncIndex() const {
141 DCHECK(IsExecutableInElf());
142 return proxy_stub_elf_func_idx_;
143 }
144
145 void SetInvokeStub(uint16_t invoke_stub_elf_func_idx) {
146 invoke_stub_elf_func_idx_ = invoke_stub_elf_func_idx;
147 }
148
149 void SetProxyStub(uint16_t proxy_stub_elf_func_idx) {
150 proxy_stub_elf_func_idx_ = proxy_stub_elf_func_idx;
Logan Chien937105a2012-04-02 02:37:37 +0800151 }
152
Logan Chien6920bce2012-03-17 21:44:01 +0800153 bool IsExecutableInElf() const {
Logan Chien937105a2012-04-02 02:37:37 +0800154 return (elf_idx_ != static_cast<uint16_t>(-1u));
Logan Chien6920bce2012-03-17 21:44:01 +0800155 }
156
157 private:
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700158 std::vector<uint8_t> code_;
Logan Chien937105a2012-04-02 02:37:37 +0800159 uint16_t elf_idx_;
TDYa127eead4ac2012-06-03 07:15:25 -0700160 uint16_t invoke_stub_elf_func_idx_;
161 uint16_t proxy_stub_elf_func_idx_;
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700162};
163
164} // namespace art
165
166#endif // ART_SRC_COMPILED_METHOD_H_