1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | // Classes that describe assembly patterns as used by inline caches. |
5 | |
6 | #ifndef RUNTIME_VM_INSTRUCTIONS_X64_H_ |
7 | #define RUNTIME_VM_INSTRUCTIONS_X64_H_ |
8 | |
9 | #ifndef RUNTIME_VM_INSTRUCTIONS_H_ |
10 | #error "Do not include instructions_x64.h directly; use instructions.h instead." |
11 | #endif |
12 | |
13 | #include "platform/unaligned.h" |
14 | #include "vm/allocation.h" |
15 | |
16 | namespace dart { |
17 | |
18 | intptr_t IndexFromPPLoadDisp8(uword start); |
19 | intptr_t IndexFromPPLoadDisp32(uword start); |
20 | |
21 | // Template class for all instruction pattern classes. |
22 | // P has to specify a static pattern and a pattern length method. |
23 | template <class P> |
24 | class InstructionPattern : public ValueObject { |
25 | public: |
26 | explicit InstructionPattern(uword pc) : start_(pc) { ASSERT(pc != 0); } |
27 | |
28 | // Call to check if the instruction pattern at 'pc' match the instruction. |
29 | // 'P::pattern()' returns the expected byte pattern in form of an integer |
30 | // array with length of 'P::pattern_length_in_bytes()'. A '-1' element means |
31 | // 'any byte'. |
32 | bool IsValid() const { |
33 | return TestBytesWith(P::pattern(), P::pattern_length_in_bytes()); |
34 | } |
35 | |
36 | protected: |
37 | uword start() const { return start_; } |
38 | |
39 | private: |
40 | // Returns true if the 'num_bytes' bytes at 'start_' correspond to |
41 | // array of integers 'data'. 'data' elements are either a byte or -1, which |
42 | // represents any byte. |
43 | bool TestBytesWith(const int* data, int num_bytes) const { |
44 | ASSERT(data != NULL); |
45 | const uint8_t* byte_array = reinterpret_cast<const uint8_t*>(start_); |
46 | for (int i = 0; i < num_bytes; i++) { |
47 | // Skip comparison for data[i] < 0. |
48 | if ((data[i] >= 0) && (byte_array[i] != (0xFF & data[i]))) { |
49 | return false; |
50 | } |
51 | } |
52 | return true; |
53 | } |
54 | |
55 | const uword start_; |
56 | |
57 | DISALLOW_COPY_AND_ASSIGN(InstructionPattern); |
58 | }; |
59 | |
60 | class ReturnPattern : public InstructionPattern<ReturnPattern> { |
61 | public: |
62 | explicit ReturnPattern(uword pc) : InstructionPattern(pc) {} |
63 | |
64 | static const int* pattern() { |
65 | static const int kReturnPattern[kLengthInBytes] = {0xC3}; |
66 | return kReturnPattern; |
67 | } |
68 | |
69 | static int pattern_length_in_bytes() { return kLengthInBytes; } |
70 | |
71 | private: |
72 | static const int kLengthInBytes = 1; |
73 | }; |
74 | |
75 | // push rbp |
76 | // mov rbp, rsp |
77 | class ProloguePattern : public InstructionPattern<ProloguePattern> { |
78 | public: |
79 | explicit ProloguePattern(uword pc) : InstructionPattern(pc) {} |
80 | |
81 | static const int* pattern() { |
82 | static const int kProloguePattern[kLengthInBytes] = {0x55, 0x48, 0x89, |
83 | 0xe5}; |
84 | return kProloguePattern; |
85 | } |
86 | |
87 | static int pattern_length_in_bytes() { return kLengthInBytes; } |
88 | |
89 | private: |
90 | static const int kLengthInBytes = 4; |
91 | }; |
92 | |
93 | // mov rbp, rsp |
94 | class SetFramePointerPattern |
95 | : public InstructionPattern<SetFramePointerPattern> { |
96 | public: |
97 | explicit SetFramePointerPattern(uword pc) : InstructionPattern(pc) {} |
98 | |
99 | static const int* pattern() { |
100 | static const int kFramePointerPattern[kLengthInBytes] = {0x48, 0x89, 0xe5}; |
101 | return kFramePointerPattern; |
102 | } |
103 | |
104 | static int pattern_length_in_bytes() { return kLengthInBytes; } |
105 | |
106 | private: |
107 | static const int kLengthInBytes = 3; |
108 | }; |
109 | |
110 | // callq *[rip+offset] |
111 | class PcRelativeCallPattern : public InstructionPattern<PcRelativeCallPattern> { |
112 | public: |
113 | static const intptr_t kLowerCallingRange = -(DART_UINT64_C(1) << 31); |
114 | static const intptr_t kUpperCallingRange = (DART_UINT64_C(1) << 31) - 1; |
115 | |
116 | explicit PcRelativeCallPattern(uword pc) : InstructionPattern(pc) {} |
117 | |
118 | int32_t distance() { |
119 | return LoadUnaligned(reinterpret_cast<int32_t*>(start() + 1)) + |
120 | kLengthInBytes; |
121 | } |
122 | |
123 | void set_distance(int32_t distance) { |
124 | // [distance] is relative to the start of the instruction, x64 considers the |
125 | // offset relative to next PC. |
126 | StoreUnaligned(reinterpret_cast<int32_t*>(start() + 1), |
127 | distance - kLengthInBytes); |
128 | } |
129 | |
130 | static const int* pattern() { |
131 | static const int kPattern[kLengthInBytes] = {0xe8, -1, -1, -1, -1}; |
132 | return kPattern; |
133 | } |
134 | |
135 | static int pattern_length_in_bytes() { return kLengthInBytes; } |
136 | |
137 | static const int kLengthInBytes = 5; |
138 | }; |
139 | |
140 | // Instruction pattern for a tail call to a signed 32-bit PC-relative offset |
141 | // |
142 | // The AOT compiler can emit PC-relative calls. If the destination of such a |
143 | // call is not in range for the "bl.<cond> <offset>" instruction, the AOT |
144 | // compiler will emit a trampoline which is in range. That trampoline will |
145 | // then tail-call to the final destination (also via PC-relative offset, but it |
146 | // supports a full signed 32-bit offset). |
147 | // |
148 | // The pattern of the trampoline looks like: |
149 | // |
150 | // jmp $rip + <offset> |
151 | // |
152 | // (Strictly speaking the pc-relative call distance on X64 is big enough, but |
153 | // for making AOT relocation code (i.e. relocation.cc) platform independent and |
154 | // allow testing of trampolines on X64 we have it nonetheless) |
155 | class PcRelativeTrampolineJumpPattern : public ValueObject { |
156 | public: |
157 | static const int kLengthInBytes = 5; |
158 | |
159 | explicit PcRelativeTrampolineJumpPattern(uword pattern_start) |
160 | : pattern_start_(pattern_start) {} |
161 | |
162 | void Initialize() { |
163 | uint8_t* pattern = reinterpret_cast<uint8_t*>(pattern_start_); |
164 | pattern[0] = 0xe9; |
165 | } |
166 | |
167 | int32_t distance() { |
168 | return LoadUnaligned(reinterpret_cast<int32_t*>(pattern_start_ + 1)) + |
169 | kLengthInBytes; |
170 | } |
171 | |
172 | void set_distance(int32_t distance) { |
173 | // [distance] is relative to the start of the instruction, x64 considers the |
174 | // offset relative to next PC. |
175 | StoreUnaligned(reinterpret_cast<int32_t*>(pattern_start_ + 1), |
176 | distance - kLengthInBytes); |
177 | } |
178 | |
179 | bool IsValid() const { |
180 | uint8_t* pattern = reinterpret_cast<uint8_t*>(pattern_start_); |
181 | return pattern[0] == 0xe9; |
182 | } |
183 | |
184 | private: |
185 | uword pattern_start_; |
186 | }; |
187 | |
188 | class PcRelativeTailCallPattern : public PcRelativeTrampolineJumpPattern { |
189 | public: |
190 | static const intptr_t kLowerCallingRange = -(1ul << 31) + kLengthInBytes; |
191 | static const intptr_t kUpperCallingRange = (1ul << 31) - 1; |
192 | |
193 | explicit PcRelativeTailCallPattern(uword pc) |
194 | : PcRelativeTrampolineJumpPattern(pc) {} |
195 | }; |
196 | |
197 | } // namespace dart |
198 | |
199 | #endif // RUNTIME_VM_INSTRUCTIONS_X64_H_ |
200 | |