Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
instructions_ia32.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_IA32_H_
7#define RUNTIME_VM_INSTRUCTIONS_IA32_H_
8
9#ifndef RUNTIME_VM_INSTRUCTIONS_H_
10#error Do not include instructions_ia32.h directly; use instructions.h instead.
11#endif
12
13#include "vm/allocation.h"
14#include "vm/cpu.h"
15
16namespace dart {
17
18// Template class for all instruction pattern classes.
19// P has to specify a static pattern and a pattern length method.
20template <class P>
21class InstructionPattern : public ValueObject {
22 public:
23 explicit InstructionPattern(uword pc) : start_(pc) { ASSERT(pc != 0); }
24
25 // Call to check if the instruction pattern at 'pc' match the instruction.
26 // 'P::pattern()' returns the expected byte pattern in form of an integer
27 // array with length of 'P::pattern_length_in_bytes()'. A '-1' element means
28 // 'any byte'.
29 bool IsValid() const {
30 return TestBytesWith(P::pattern(), P::pattern_length_in_bytes());
31 }
32
33 protected:
34 uword start() const { return start_; }
35
36 private:
37 // Returns true if the 'num_bytes' bytes at 'start_' correspond to
38 // array of integers 'data'. 'data' elements are either a byte or -1, which
39 // represents any byte.
40 bool TestBytesWith(const int* data, int num_bytes) const {
41 ASSERT(data != nullptr);
42 const uint8_t* byte_array = reinterpret_cast<const uint8_t*>(start_);
43 for (int i = 0; i < num_bytes; i++) {
44 // Skip comparison for data[i] < 0.
45 if ((data[i] >= 0) && (byte_array[i] != (0xFF & data[i]))) {
46 return false;
47 }
48 }
49 return true;
50 }
51
52 const uword start_;
53
54 DISALLOW_COPY_AND_ASSIGN(InstructionPattern);
55};
56
57class CallPattern : public InstructionPattern<CallPattern> {
58 public:
59 explicit CallPattern(uword pc) : InstructionPattern(pc) {}
61 ASSERT(this->IsValid());
63 *reinterpret_cast<uword*>(this->start() + 1);
64 }
65
66 void SetTargetAddress(uword new_target) const {
67 ASSERT(this->IsValid());
68 *reinterpret_cast<uword*>(this->start() + 1) =
69 new_target - this->start() - CallPattern::pattern_length_in_bytes();
70 CPU::FlushICache(this->start() + 1, kWordSize);
71 }
72
73 static int pattern_length_in_bytes() { return kLengthInBytes; }
74 static const int* pattern() {
75 static const int kCallPattern[kLengthInBytes] = {0xE8, -1, -1, -1, -1};
76 return kCallPattern;
77 }
78
79 private:
80 static constexpr int kLengthInBytes = 5;
82};
83
84class ReturnPattern : public InstructionPattern<ReturnPattern> {
85 public:
87
88 static const int* pattern() {
89 static const int kReturnPattern[kLengthInBytes] = {0xC3};
90 return kReturnPattern;
91 }
92 static int pattern_length_in_bytes() { return kLengthInBytes; }
93
94 private:
95 static constexpr int kLengthInBytes = 1;
96};
97
98// push ebp
99// mov ebp, esp
100class ProloguePattern : public InstructionPattern<ProloguePattern> {
101 public:
103
104 static const int* pattern() {
105 static const int kProloguePattern[kLengthInBytes] = {0x55, 0x89, 0xe5};
106 return kProloguePattern;
107 }
108
109 static int pattern_length_in_bytes() { return kLengthInBytes; }
110
111 private:
112 static constexpr int kLengthInBytes = 3;
113};
114
115// mov ebp, esp
117 : public InstructionPattern<SetFramePointerPattern> {
118 public:
120
121 static const int* pattern() {
122 static const int kFramePointerPattern[kLengthInBytes] = {0x89, 0xe5};
123 return kFramePointerPattern;
124 }
125
126 static int pattern_length_in_bytes() { return kLengthInBytes; }
127
128 private:
129 static constexpr int kLengthInBytes = 2;
130};
131
132} // namespace dart
133
134#endif // RUNTIME_VM_INSTRUCTIONS_IA32_H_
static void FlushICache(uword start, uword size)
void SetTargetAddress(uword new_target) const
static int pattern_length_in_bytes()
static const int * pattern()
uword TargetAddress() const
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
constexpr intptr_t kWordSize
Definition globals.h:509
static int8_t data[kExtLength]
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition globals.h:581