OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Classes that describe assembly patterns as used by inline caches. | 4 // Classes that describe assembly patterns as used by inline caches. |
5 | 5 |
6 #ifndef VM_INSTRUCTIONS_IA32_H_ | 6 #ifndef VM_INSTRUCTIONS_IA32_H_ |
7 #define VM_INSTRUCTIONS_IA32_H_ | 7 #define VM_INSTRUCTIONS_IA32_H_ |
8 | 8 |
9 #ifndef VM_INSTRUCTIONS_H_ | 9 #ifndef VM_INSTRUCTIONS_H_ |
10 #error Do not include instructions_ia32.h directly; use instructions.h instead. | 10 #error Do not include instructions_ia32.h directly; use instructions.h instead. |
11 #endif | 11 #endif |
12 | 12 |
13 #include "vm/allocation.h" | 13 #include "vm/allocation.h" |
| 14 #include "vm/cpu.h" |
14 #include "vm/object.h" | 15 #include "vm/object.h" |
15 | 16 |
16 namespace dart { | 17 namespace dart { |
17 | 18 |
18 // Forward declarations. | 19 // Forward declarations. |
19 class RawClass; | 20 class RawClass; |
20 class Immediate; | 21 class Immediate; |
21 class RawObject; | 22 class RawObject; |
22 | 23 |
23 // Abstract class for all instruction pattern classes. | 24 // Template class for all instruction pattern classes. |
24 class InstructionPattern : public ValueObject { | 25 // P has to specify a static pattern and a pattern length method. |
| 26 template<class P> class InstructionPattern : public ValueObject { |
25 public: | 27 public: |
26 explicit InstructionPattern(uword pc) : start_(pc) { | 28 explicit InstructionPattern(uword pc) : start_(pc) { |
27 ASSERT(pc != 0); | 29 ASSERT(pc != 0); |
28 } | 30 } |
29 virtual ~InstructionPattern() {} | |
30 | 31 |
31 // Call to check if the instruction pattern at 'pc' match the instruction. | 32 // Call to check if the instruction pattern at 'pc' match the instruction. |
32 virtual bool IsValid() const { | 33 // 'P::pattern()' returns the expected byte pattern in form of an integer |
33 return TestBytesWith(pattern(), pattern_length_in_bytes()); | 34 // array with length of 'P::pattern_length_in_bytes()'. A '-1' element means |
| 35 // 'any byte'. |
| 36 bool IsValid() const { |
| 37 return TestBytesWith(P::pattern(), P::pattern_length_in_bytes()); |
34 } | 38 } |
35 | 39 |
36 // 'pattern' returns the expected byte pattern in form of an integer array | |
37 // with length of 'pattern_length_in_bytes'. A '-1' element means 'any byte'. | |
38 virtual const int* pattern() const = 0; | |
39 virtual int pattern_length_in_bytes() const = 0; | |
40 | |
41 protected: | 40 protected: |
42 uword start() const { return start_; } | 41 uword start() const { return start_; } |
43 | 42 |
44 private: | 43 private: |
45 // Returns true if the 'num_bytes' bytes at 'start_' correspond to | 44 // Returns true if the 'num_bytes' bytes at 'start_' correspond to |
46 // array of integers 'data'. 'data' elements are either a byte or -1, which | 45 // array of integers 'data'. 'data' elements are either a byte or -1, which |
47 // represents any byte. | 46 // represents any byte. |
48 bool TestBytesWith(const int* data, int num_bytes) const; | 47 bool TestBytesWith(const int* data, int num_bytes) const { |
| 48 ASSERT(data != NULL); |
| 49 const uint8_t* byte_array = reinterpret_cast<const uint8_t*>(start_); |
| 50 for (int i = 0; i < num_bytes; i++) { |
| 51 // Skip comparison for data[i] < 0. |
| 52 if ((data[i] >= 0) && (byte_array[i] != (0xFF & data[i]))) { |
| 53 return false; |
| 54 } |
| 55 } |
| 56 return true; |
| 57 } |
49 | 58 |
50 const uword start_; | 59 const uword start_; |
51 | 60 |
52 DISALLOW_COPY_AND_ASSIGN(InstructionPattern); | 61 DISALLOW_COPY_AND_ASSIGN(InstructionPattern); |
53 }; | 62 }; |
54 | 63 |
55 | 64 |
56 class CallOrJumpPattern : public InstructionPattern { | 65 template<class P> |
| 66 class CallOrJumpPattern : public InstructionPattern<P> { |
57 public: | 67 public: |
58 virtual int pattern_length_in_bytes() const { | 68 uword TargetAddress() const { |
59 return kLengthInBytes; | 69 ASSERT(this->IsValid()); |
| 70 return this->start() + |
| 71 P::pattern_length_in_bytes() + |
| 72 *reinterpret_cast<uword*>(this->start() + 1); |
60 } | 73 } |
61 uword TargetAddress() const; | 74 |
62 void SetTargetAddress(uword new_target) const; | 75 void SetTargetAddress(uword new_target) const { |
| 76 ASSERT(this->IsValid()); |
| 77 *reinterpret_cast<uword*>(this->start() + 1) = |
| 78 new_target - this->start() - P::pattern_length_in_bytes(); |
| 79 CPU::FlushICache(this->start() + 1, kWordSize); |
| 80 } |
63 | 81 |
64 protected: | 82 protected: |
65 explicit CallOrJumpPattern(uword pc) : InstructionPattern(pc) {} | 83 explicit CallOrJumpPattern(uword pc) : InstructionPattern<P>(pc) {} |
66 static const int kLengthInBytes = 5; | |
67 | 84 |
68 private: | 85 private: |
69 DISALLOW_COPY_AND_ASSIGN(CallOrJumpPattern); | 86 DISALLOW_COPY_AND_ASSIGN(CallOrJumpPattern); |
70 }; | 87 }; |
71 | 88 |
72 | 89 |
73 class CallPattern : public CallOrJumpPattern { | 90 class CallPattern : public CallOrJumpPattern<CallPattern> { |
74 public: | 91 public: |
75 explicit CallPattern(uword pc) : CallOrJumpPattern(pc) {} | 92 explicit CallPattern(uword pc) : CallOrJumpPattern(pc) {} |
76 static int InstructionLength() { | 93 |
77 return kLengthInBytes; | 94 static int pattern_length_in_bytes() { return kLengthInBytes; } |
| 95 static const int* pattern() { |
| 96 static const int kCallPattern[kLengthInBytes] = {0xE8, -1, -1, -1, -1}; |
| 97 return kCallPattern; |
78 } | 98 } |
79 | 99 |
80 private: | 100 private: |
81 virtual const int* pattern() const; | 101 static const int kLengthInBytes = 5; |
82 | 102 |
83 DISALLOW_COPY_AND_ASSIGN(CallPattern); | 103 DISALLOW_COPY_AND_ASSIGN(CallPattern); |
84 }; | 104 }; |
85 | 105 |
86 | 106 |
87 class JumpPattern : public CallOrJumpPattern { | 107 class JumpPattern : public CallOrJumpPattern<JumpPattern> { |
88 public: | 108 public: |
89 JumpPattern(uword pc, const Code& code) : CallOrJumpPattern(pc) {} | 109 JumpPattern(uword pc, const Code& code) : CallOrJumpPattern(pc) {} |
90 | 110 |
| 111 static int pattern_length_in_bytes() { return kLengthInBytes; } |
| 112 static const int* pattern() { |
| 113 static const int kJumpPattern[kLengthInBytes] = {0xE9, -1, -1, -1, -1}; |
| 114 return kJumpPattern; |
| 115 } |
| 116 |
91 private: | 117 private: |
92 virtual const int* pattern() const; | 118 static const int kLengthInBytes = 5; |
93 | 119 |
94 DISALLOW_COPY_AND_ASSIGN(JumpPattern); | 120 DISALLOW_COPY_AND_ASSIGN(JumpPattern); |
95 }; | 121 }; |
96 | 122 |
97 | 123 |
98 class ReturnPattern : public InstructionPattern { | 124 class ReturnPattern : public InstructionPattern<ReturnPattern> { |
99 public: | 125 public: |
100 explicit ReturnPattern(uword pc) : InstructionPattern(pc) {} | 126 explicit ReturnPattern(uword pc) : InstructionPattern(pc) {} |
101 | 127 |
102 virtual const int* pattern() const; | 128 static const int* pattern() { |
103 virtual int pattern_length_in_bytes() const { return kLengthInBytes; } | 129 static const int kReturnPattern[kLengthInBytes] = { 0xC3 }; |
| 130 return kReturnPattern; |
| 131 } |
| 132 static int pattern_length_in_bytes() { return kLengthInBytes; } |
104 | 133 |
105 private: | 134 private: |
106 static const int kLengthInBytes = 1; | 135 static const int kLengthInBytes = 1; |
107 }; | 136 }; |
108 | 137 |
109 | 138 |
110 // push ebp | 139 // push ebp |
111 // mov ebp, esp | 140 // mov ebp, esp |
112 class ProloguePattern : public InstructionPattern { | 141 class ProloguePattern : public InstructionPattern<ProloguePattern> { |
113 public: | 142 public: |
114 explicit ProloguePattern(uword pc) : InstructionPattern(pc) {} | 143 explicit ProloguePattern(uword pc) : InstructionPattern(pc) {} |
115 | 144 |
116 virtual const int* pattern() const; | 145 static const int* pattern() { |
117 virtual int pattern_length_in_bytes() const { return kLengthInBytes; } | 146 static const int kProloguePattern[kLengthInBytes] = { 0x55, 0x89, 0xe5 }; |
| 147 return kProloguePattern; |
| 148 } |
| 149 |
| 150 static int pattern_length_in_bytes() { return kLengthInBytes; } |
118 | 151 |
119 private: | 152 private: |
120 static const int kLengthInBytes = 3; | 153 static const int kLengthInBytes = 3; |
121 }; | 154 }; |
122 | 155 |
123 | 156 |
124 // mov ebp, esp | 157 // mov ebp, esp |
125 class SetFramePointerPattern : public InstructionPattern { | 158 class SetFramePointerPattern : |
| 159 public InstructionPattern<SetFramePointerPattern> { |
126 public: | 160 public: |
127 explicit SetFramePointerPattern(uword pc) : InstructionPattern(pc) {} | 161 explicit SetFramePointerPattern(uword pc) : InstructionPattern(pc) {} |
128 | 162 |
129 virtual const int* pattern() const; | 163 static const int* pattern() { |
130 virtual int pattern_length_in_bytes() const { return kLengthInBytes; } | 164 static const int kFramePointerPattern[kLengthInBytes] = { 0x89, 0xe5 }; |
| 165 return kFramePointerPattern; |
| 166 } |
| 167 |
| 168 static int pattern_length_in_bytes() { return kLengthInBytes; } |
131 | 169 |
132 private: | 170 private: |
133 static const int kLengthInBytes = 2; | 171 static const int kLengthInBytes = 2; |
134 }; | 172 }; |
135 | 173 |
136 } // namespace dart | 174 } // namespace dart |
137 | 175 |
138 #endif // VM_INSTRUCTIONS_IA32_H_ | 176 #endif // VM_INSTRUCTIONS_IA32_H_ |
OLD | NEW |