call-tester.h 1.8 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 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_CALL_TESTER_H_
#define V8_CCTEST_COMPILER_CALL_TESTER_H_

8
#include "src/execution/simulator.h"
9
#include "src/handles/handles.h"
10
#include "src/objects/code.h"
11 12
#include "test/cctest/compiler/c-signature.h"

13 14 15 16
namespace v8 {
namespace internal {
namespace compiler {

17
template <typename R>
18 19
class CallHelper {
 public:
20
  explicit CallHelper(Isolate* isolate, MachineSignature* csig)
21
      : csig_(csig), isolate_(isolate) {
22 23
    USE(isolate_);
  }
24
  virtual ~CallHelper() = default;
25

26 27 28
  template <typename... Params>
  R Call(Params... args) {
    CSignature::VerifyParams<Params...>(csig_);
29
    Address entry = Generate();
30 31
    auto fn = GeneratedCode<R, Params...>::FromAddress(isolate_, entry);
    return fn.Call(args...);
32 33
  }

34
 protected:
35
  MachineSignature* csig_;
36

37
  virtual Address Generate() = 0;
38 39 40 41 42

 private:
  Isolate* isolate_;
};

43 44 45 46 47 48 49 50 51
template <>
template <typename... Params>
Object CallHelper<Object>::Call(Params... args) {
  CSignature::VerifyParams<Params...>(csig_);
  Address entry = Generate();
  auto fn = GeneratedCode<Address, Params...>::FromAddress(isolate_, entry);
  return Object(fn.Call(args...));
}

52 53 54 55
// A call helper that calls the given code object assuming C calling convention.
template <typename T>
class CodeRunner : public CallHelper<T> {
 public:
56
  CodeRunner(Isolate* isolate, Handle<Code> code, MachineSignature* csig)
57
      : CallHelper<T>(isolate, csig), code_(code) {}
58
  ~CodeRunner() override = default;
59

60
  Address Generate() override { return code_->entry(); }
61 62 63 64 65 66

 private:
  Handle<Code> code_;
};


67 68 69 70 71
}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_CCTEST_COMPILER_CALL_TESTER_H_