summaryrefslogtreecommitdiff
path: root/deps/v8/test/cctest/compiler/node-observer-tester.h
blob: 253eba230ec44a8e0241f724e2ae0f25ed0899c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_CCTEST_COMPILER_NODEOBSERVER_TESTER_H_
#define V8_CCTEST_COMPILER_NODEOBSERVER_TESTER_H_

#include "src/compiler/node-observer.h"
#include "src/compiler/simplified-operator.h"
#include "src/objects/type-hints.h"
#include "test/cctest/cctest.h"
#include "test/common/wasm/flag-utils.h"

namespace v8 {
namespace internal {
namespace compiler {

// Helpers to test TurboFan compilation using the %ObserveNode intrinsic.
struct ObserveNodeScope {
 public:
  ObserveNodeScope(Isolate* isolate, NodeObserver* node_observer)
      : isolate_(isolate) {
    DCHECK_NOT_NULL(isolate_);
    DCHECK_NULL(isolate_->node_observer());
    isolate_->set_node_observer(node_observer);
  }

  ~ObserveNodeScope() {
    DCHECK_NOT_NULL(isolate_->node_observer());

    // Checks that the code wrapped by %ObserveNode() was actually compiled in
    // the test.
    CHECK(isolate_->node_observer()->has_observed_changes());

    isolate_->set_node_observer(nullptr);
  }

 private:
  Isolate* isolate_;
};

class CreationObserver : public NodeObserver {
 public:
  explicit CreationObserver(std::function<void(const Node*)> handler)
      : handler_(handler) {
    DCHECK(handler_);
  }

  Observation OnNodeCreated(const Node* node) override {
    handler_(node);
    return Observation::kStop;
  }

 private:
  std::function<void(const Node*)> handler_;
};

class ModificationObserver : public NodeObserver {
 public:
  explicit ModificationObserver(
      std::function<void(const Node*)> on_created_handler,
      std::function<NodeObserver::Observation(
          const Node*, const ObservableNodeState& old_state)>
          on_changed_handler)
      : on_created_handler_(on_created_handler),
        on_changed_handler_(on_changed_handler) {
    DCHECK(on_created_handler_);
    DCHECK(on_changed_handler_);
  }

  Observation OnNodeCreated(const Node* node) override {
    on_created_handler_(node);
    return Observation::kContinue;
  }

  Observation OnNodeChanged(const char* reducer_name, const Node* node,
                            const ObservableNodeState& old_state) override {
    return on_changed_handler_(node, old_state);
  }

 private:
  std::function<void(const Node*)> on_created_handler_;
  std::function<NodeObserver::Observation(const Node*,
                                          const ObservableNodeState& old_state)>
      on_changed_handler_;
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_CCTEST_COMPILER_NODEOBSERVER_TESTER_H_