Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
instructions_x64.h
Go to the documentation of this file.
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 <limits>
14
15#include "platform/unaligned.h"
16#include "vm/allocation.h"
17
18namespace dart {
19
22
23// Template class for all instruction pattern classes.
24// P has to specify a static pattern and a pattern length method.
25template <class P>
26class InstructionPattern : public ValueObject {
27 public:
28 explicit InstructionPattern(uword pc) : start_(pc) { ASSERT(pc != 0); }
29
30 // Call to check if the instruction pattern at 'pc' match the instruction.
31 // 'P::pattern()' returns the expected byte pattern in form of an integer
32 // array with length of 'P::pattern_length_in_bytes()'. A '-1' element means
33 // 'any byte'.
34 bool IsValid() const {
35 return TestBytesWith(P::pattern(), P::pattern_length_in_bytes());
36 }
37
38 protected:
39 uword start() const { return start_; }
40
41 private:
42 // Returns true if the 'num_bytes' bytes at 'start_' correspond to
43 // array of integers 'data'. 'data' elements are either a byte or -1, which
44 // represents any byte.
45 bool TestBytesWith(const int* data, int num_bytes) const {
46 ASSERT(data != nullptr);
47 const uint8_t* byte_array = reinterpret_cast<const uint8_t*>(start_);
48 for (int i = 0; i < num_bytes; i++) {
49 // Skip comparison for data[i] < 0.
50 if ((data[i] >= 0) && (byte_array[i] != (0xFF & data[i]))) {
51 return false;
52 }
53 }
54 return true;
55 }
56
57 const uword start_;
58
59 DISALLOW_COPY_AND_ASSIGN(InstructionPattern);
60};
61
62class ReturnPattern : public InstructionPattern<ReturnPattern> {
63 public:
65
66 static const int* pattern() {
67 static const int kReturnPattern[kLengthInBytes] = {0xC3};
68 return kReturnPattern;
69 }
70
71 static int pattern_length_in_bytes() { return kLengthInBytes; }
72
73 private:
74 static constexpr int kLengthInBytes = 1;
75};
76
77// push rbp
78// mov rbp, rsp
79class ProloguePattern : public InstructionPattern<ProloguePattern> {
80 public:
82
83 static const int* pattern() {
84 static const int kProloguePattern[kLengthInBytes] = {0x55, 0x48, 0x89,
85 0xe5};
86 return kProloguePattern;
87 }
88
89 static int pattern_length_in_bytes() { return kLengthInBytes; }
90
91 private:
92 static constexpr int kLengthInBytes = 4;
93};
94
95// mov rbp, rsp
96class SetFramePointerPattern
97 : public InstructionPattern<SetFramePointerPattern> {
98 public:
100
101 static const int* pattern() {
102 static const int kFramePointerPattern[kLengthInBytes] = {0x48, 0x89, 0xe5};
103 return kFramePointerPattern;
104 }
105
106 static int pattern_length_in_bytes() { return kLengthInBytes; }
107
108 private:
109 static constexpr int kLengthInBytes = 3;
110};
111
112// callq *[rip+offset]
113class PcRelativeCallPattern : public InstructionPattern<PcRelativeCallPattern> {
114 public:
115 static constexpr int kLengthInBytes = 5;
116
117 // Theoretically we can encode offsets 5 bytes more than INT_MAX since the
118 // instruction encoding uses the PC after current instruction.
119 // Though in order to use int32_t as type on all architectures for distances,
120 // we make the upper limit not have `+ kLengthInBytes`.
121 static constexpr int32_t kLowerCallingRange =
122 std::numeric_limits<int32_t>::min() + kLengthInBytes;
123 static constexpr int32_t kUpperCallingRange =
124 std::numeric_limits<int32_t>::max();
125
127
128 int32_t distance() {
129 return LoadUnaligned(reinterpret_cast<int32_t*>(start() + 1)) +
131 }
132
133 void set_distance(int32_t distance) {
134 // [distance] is relative to the start of the instruction, x64 considers the
135 // offset relative to next PC.
136 StoreUnaligned(reinterpret_cast<int32_t*>(start() + 1),
138 }
139
140 static const int* pattern() {
141 static const int kPattern[kLengthInBytes] = {0xe8, -1, -1, -1, -1};
142 return kPattern;
143 }
144
146};
147
148// Instruction pattern for a tail call to a signed 32-bit PC-relative offset
149//
150// The AOT compiler can emit PC-relative calls. If the destination of such a
151// call is not in range for the "bl.<cond> <offset>" instruction, the AOT
152// compiler will emit a trampoline which is in range. That trampoline will
153// then tail-call to the final destination (also via PC-relative offset, but it
154// supports a full signed 32-bit offset).
155//
156// The pattern of the trampoline looks like:
157//
158// jmp $rip + <offset>
159//
160// (Strictly speaking the pc-relative call distance on X64 is big enough, but
161// for making AOT relocation code (i.e. relocation.cc) platform independent and
162// allow testing of trampolines on X64 we have it nonetheless)
163class PcRelativeTrampolineJumpPattern : public ValueObject {
164 public:
165 static constexpr int kLengthInBytes = 5;
166
168 : pattern_start_(pattern_start) {}
169
170 void Initialize() {
171 uint8_t* pattern = reinterpret_cast<uint8_t*>(pattern_start_);
172 pattern[0] = 0xe9;
173 }
174
175 int32_t distance() {
176 return LoadUnaligned(reinterpret_cast<int32_t*>(pattern_start_ + 1)) +
178 }
179
180 void set_distance(intptr_t distance) {
181 // [distance] is relative to the start of the instruction, x64 considers the
182 // offset relative to next PC.
183 StoreUnaligned(reinterpret_cast<int32_t*>(pattern_start_ + 1),
184 static_cast<int32_t>(distance - kLengthInBytes));
185 }
186
187 bool IsValid() const {
188 uint8_t* pattern = reinterpret_cast<uint8_t*>(pattern_start_);
189 return pattern[0] == 0xe9;
190 }
191
192 private:
193 uword pattern_start_;
194};
195
196class PcRelativeTailCallPattern : public PcRelativeTrampolineJumpPattern {
197 public:
198 static constexpr int32_t kLowerCallingRange =
200 static constexpr int32_t kUpperCallingRange =
202
205};
206
207} // namespace dart
208
209#endif // RUNTIME_VM_INSTRUCTIONS_X64_H_
static const int * pattern()
void set_distance(int32_t distance)
static constexpr int32_t kLowerCallingRange
static constexpr int kLengthInBytes
static constexpr int32_t kUpperCallingRange
static constexpr int32_t kUpperCallingRange
static constexpr int32_t kLowerCallingRange
PcRelativeTrampolineJumpPattern(uword pattern_start)
static const int * pattern()
static int pattern_length_in_bytes()
static constexpr int kLengthInBytes
static const int * pattern()
static int pattern_length_in_bytes()
#define ASSERT(E)
uintptr_t uword
Definition globals.h:501
static T LoadUnaligned(const T *ptr)
Definition unaligned.h:14
static void StoreUnaligned(T *ptr, T value)
Definition unaligned.h:22
intptr_t IndexFromPPLoadDisp32(uword start)
static int8_t data[kExtLength]
intptr_t IndexFromPPLoadDisp8(uword start)
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581