bytecode-expectations-printer.h 4.42 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2016 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 TEST_CCTEST_INTERPRETER_BYTECODE_EXPECTATIONS_PRINTER_H_
#define TEST_CCTEST_INTERPRETER_BYTECODE_EXPECTATIONS_PRINTER_H_

#include <iostream>
#include <string>
10
#include <vector>
11 12

#include "src/interpreter/bytecodes.h"
13
#include "src/objects/objects.h"
14 15 16 17 18 19

namespace v8 {

class Isolate;

namespace internal {
20

21
class BytecodeArray;
22 23
class SourcePositionTableIterator;

24 25 26 27 28 29
namespace interpreter {

class BytecodeArrayIterator;

class BytecodeExpectationsPrinter final {
 public:
30
  explicit BytecodeExpectationsPrinter(v8::Isolate* i)
31
      : isolate_(i),
32
        module_(false),
33
        wrap_(true),
34
        top_level_(false),
35
        print_callee_(false),
36
        oneshot_opt_(false),
37
        test_function_name_(kDefaultTopFunctionName) {}
38

39
  void PrintExpectation(std::ostream* stream, const std::string& snippet) const;
40

41 42 43
  void set_module(bool module) { module_ = module; }
  bool module() const { return module_; }

44 45 46 47 48 49
  void set_wrap(bool wrap) { wrap_ = wrap; }
  bool wrap() const { return wrap_; }

  void set_top_level(bool top_level) { top_level_ = top_level; }
  bool top_level() const { return top_level_; }

50 51 52 53 54 55
  void set_print_callee(bool print_callee) { print_callee_ = print_callee; }
  bool print_callee() { return print_callee_; }

  void set_oneshot_opt(bool oneshot_opt) { oneshot_opt_ = oneshot_opt; }
  bool oneshot_opt() { return oneshot_opt_; }

56 57 58 59
  void set_test_function_name(const std::string& test_function_name) {
    test_function_name_ = test_function_name;
  }
  std::string test_function_name() const { return test_function_name_; }
60 61

 private:
62
  void PrintEscapedString(std::ostream* stream,
63
                          const std::string& string) const;
64
  void PrintBytecodeOperand(std::ostream* stream,
65
                            const BytecodeArrayIterator& bytecode_iterator,
66 67
                            const Bytecode& bytecode, int op_index,
                            int parameter_count) const;
68
  void PrintBytecode(std::ostream* stream,
69
                     const BytecodeArrayIterator& bytecode_iterator,
70
                     int parameter_count) const;
71 72
  void PrintSourcePosition(std::ostream* stream,
                           SourcePositionTableIterator* source_iterator,
73
                           int bytecode_offset) const;
74 75 76
  void PrintV8String(std::ostream* stream, i::String string) const;
  void PrintConstant(std::ostream* stream, i::Handle<i::Object> constant) const;
  void PrintFrameSize(std::ostream* stream,
77
                      i::Handle<i::BytecodeArray> bytecode_array) const;
78
  void PrintBytecodeSequence(std::ostream* stream,
79
                             i::Handle<i::BytecodeArray> bytecode_array) const;
80
  void PrintConstantPool(std::ostream* stream,
81
                         i::FixedArray constant_pool) const;
82 83
  void PrintCodeSnippet(std::ostream* stream, const std::string& body) const;
  void PrintBytecodeArray(std::ostream* stream,
84
                          i::Handle<i::BytecodeArray> bytecode_array) const;
85
  void PrintHandlers(std::ostream* stream,
86
                     i::Handle<i::BytecodeArray> bytecode_array) const;
87 88 89 90 91

  v8::Local<v8::String> V8StringFromUTF8(const char* data) const;
  std::string WrapCodeInFunction(const char* function_name,
                                 const std::string& function_body) const;

92 93
  v8::Local<v8::Script> CompileScript(const char* program) const;
  v8::Local<v8::Module> CompileModule(const char* program) const;
94
  void Run(v8::Local<v8::Script> script) const;
95 96
  i::Handle<i::BytecodeArray> GetBytecodeArrayForGlobal(
      const char* global_name) const;
97 98
  i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForModule(
      v8::Local<v8::Module> module) const;
99 100
  i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForScript(
      v8::Local<v8::Script> script) const;
101 102
  i::Handle<i::BytecodeArray> GetBytecodeArrayOfCallee(
      const char* source_code) const;
103 104 105 106 107 108

  i::Isolate* i_isolate() const {
    return reinterpret_cast<i::Isolate*>(isolate_);
  }

  v8::Isolate* isolate_;
109
  bool module_;
110 111
  bool wrap_;
  bool top_level_;
112 113
  bool print_callee_;
  bool oneshot_opt_;
114 115 116
  std::string test_function_name_;

  static const char* const kDefaultTopFunctionName;
117
  static const char* const kIndent;
118 119 120 121 122 123 124
};

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

#endif  // TEST_CCTEST_INTERPRETER_BYTECODE_EXPECTATIONS_PRINTER_H_