Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(635)

Unified Diff: runtime/vm/instructions_x64.h

Issue 1301963003: VM: Clean up and fix bugs in instructions patterns (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: s/Pattern::InstructionLength/Pattern::pattern_length_in_bytes/g Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: runtime/vm/instructions_x64.h
diff --git a/runtime/vm/instructions_x64.h b/runtime/vm/instructions_x64.h
index 8f5cda401c937f11b0772e480893035575398583..4c4aae833deaf66594d7c7f48adeebf5ae450fd3 100644
--- a/runtime/vm/instructions_x64.h
+++ b/runtime/vm/instructions_x64.h
@@ -20,27 +20,26 @@ class RawClass;
class Immediate;
class RawObject;
-// Abstract class for all instruction pattern classes.
-class InstructionPattern : public ValueObject {
+
+intptr_t IndexFromPPLoad(uword start);
+
+
+// Template class for all instruction pattern classes.
+// P has to specify a static pattern and a pattern length method.
+template<class P> class InstructionPattern : public ValueObject {
public:
explicit InstructionPattern(uword pc) : start_(pc) {
ASSERT(pc != 0);
}
- virtual ~InstructionPattern() {}
// Call to check if the instruction pattern at 'pc' match the instruction.
- virtual bool IsValid() const {
- return TestBytesWith(pattern(), pattern_length_in_bytes());
+ // 'P::pattern()' returns the expected byte pattern in form of an integer
+ // array with length of 'P::pattern_length_in_bytes()'. A '-1' element means
+ // 'any byte'.
+ bool IsValid() const {
+ return TestBytesWith(P::pattern(), P::pattern_length_in_bytes());
}
- // 'pattern' returns the expected byte pattern in form of an integer array
- // with length of 'pattern_length_in_bytes'. A '-1' element means 'any byte'.
- virtual const int* pattern() const = 0;
- virtual int pattern_length_in_bytes() const = 0;
-
- static intptr_t IndexFromPPLoad(uword start);
- static intptr_t OffsetFromPPIndex(intptr_t index);
-
protected:
uword start() const { return start_; }
@@ -48,7 +47,17 @@ class InstructionPattern : public ValueObject {
// Returns true if the 'num_bytes' bytes at 'start_' correspond to
// array of integers 'data'. 'data' elements are either a byte or -1, which
// represents any byte.
- bool TestBytesWith(const int* data, int num_bytes) const;
+ bool TestBytesWith(const int* data, int num_bytes) const {
+ ASSERT(data != NULL);
+ const uint8_t* byte_array = reinterpret_cast<const uint8_t*>(start_);
+ for (int i = 0; i < num_bytes; i++) {
+ // Skip comparison for data[i] < 0.
+ if ((data[i] >= 0) && (byte_array[i] != (0xFF & data[i]))) {
+ return false;
+ }
+ }
+ return true;
+ }
const uword start_;
@@ -56,23 +65,24 @@ class InstructionPattern : public ValueObject {
};
-class JumpPattern : public InstructionPattern {
+class JumpPattern : public InstructionPattern<JumpPattern> {
public:
JumpPattern(uword pc, const Code& code)
: InstructionPattern(pc),
object_pool_(ObjectPool::Handle(code.GetObjectPool())) {}
- static int InstructionLength() {
- return kLengthInBytes;
- }
+
uword TargetAddress() const;
void SetTargetAddress(uword new_target) const;
- virtual int pattern_length_in_bytes() const {
- return kLengthInBytes;
- }
static const int kLengthInBytes = 7;
+ static int pattern_length_in_bytes() { return kLengthInBytes; }
+ static const int* pattern() {
+ // 07: 41 ff a7 imm32 jmpq [reg + off]
+ static const int kJumpPattern[kLengthInBytes] =
+ {0x41, 0xFF, -1, -1, -1, -1, -1};
+ return kJumpPattern;
+ }
private:
- virtual const int* pattern() const;
const ObjectPool& object_pool_;
DISALLOW_COPY_AND_ASSIGN(JumpPattern);
@@ -80,33 +90,34 @@ class JumpPattern : public InstructionPattern {
// 5 byte call instruction.
-class ShortCallPattern : public InstructionPattern {
+class ShortCallPattern : public InstructionPattern<ShortCallPattern> {
public:
explicit ShortCallPattern(uword pc) : InstructionPattern(pc) {}
- static int InstructionLength() {
- return kLengthInBytes;
- }
-
- virtual int pattern_length_in_bytes() const {
- return kLengthInBytes;
- }
void SetTargetAddress(uword new_target) const;
+ static int pattern_length_in_bytes() { return kLengthInBytes; }
+ static const int* pattern() {
+ static const int kCallPattern[kLengthInBytes] = {0xE8, -1, -1, -1, -1};
+ return kCallPattern;
+ }
+
private:
static const int kLengthInBytes = 5;
- virtual const int* pattern() const;
-
DISALLOW_COPY_AND_ASSIGN(ShortCallPattern);
};
-class ReturnPattern : public InstructionPattern {
+class ReturnPattern : public InstructionPattern<ReturnPattern> {
public:
explicit ReturnPattern(uword pc) : InstructionPattern(pc) {}
- virtual const int* pattern() const;
- virtual int pattern_length_in_bytes() const { return kLengthInBytes; }
+ static const int* pattern() {
+ static const int kReturnPattern[kLengthInBytes] = { 0xC3 };
+ return kReturnPattern;
+ }
+
+ static int pattern_length_in_bytes() { return kLengthInBytes; }
private:
static const int kLengthInBytes = 1;
@@ -115,12 +126,17 @@ class ReturnPattern : public InstructionPattern {
// push rbp
// mov rbp, rsp
-class ProloguePattern : public InstructionPattern {
+class ProloguePattern : public InstructionPattern<ProloguePattern> {
public:
explicit ProloguePattern(uword pc) : InstructionPattern(pc) {}
- virtual const int* pattern() const;
- virtual int pattern_length_in_bytes() const { return kLengthInBytes; }
+ static const int* pattern() {
+ static const int kProloguePattern[kLengthInBytes] =
+ { 0x55, 0x48, 0x89, 0xe5 };
+ return kProloguePattern;
+ }
+
+ static int pattern_length_in_bytes() { return kLengthInBytes; }
private:
static const int kLengthInBytes = 4;
@@ -128,12 +144,18 @@ class ProloguePattern : public InstructionPattern {
// mov rbp, rsp
-class SetFramePointerPattern : public InstructionPattern {
+class SetFramePointerPattern :
+ public InstructionPattern<SetFramePointerPattern> {
public:
explicit SetFramePointerPattern(uword pc) : InstructionPattern(pc) {}
- virtual const int* pattern() const;
- virtual int pattern_length_in_bytes() const { return kLengthInBytes; }
+ static const int* pattern() {
+ static const int kFramePointerPattern[kLengthInBytes] =
+ { 0x48, 0x89, 0xe5 };
+ return kFramePointerPattern;
+ }
+
+ static int pattern_length_in_bytes() { return kLengthInBytes; }
private:
static const int kLengthInBytes = 3;

Powered by Google App Engine
This is Rietveld 408576698