Home | History | Annotate | Download | only in interpreter
      1 // Copyright 2016 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef TEST_CCTEST_INTERPRETER_BYTECODE_EXPECTATIONS_PRINTER_H_
      6 #define TEST_CCTEST_INTERPRETER_BYTECODE_EXPECTATIONS_PRINTER_H_
      7 
      8 #include <iostream>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "src/interpreter/bytecodes.h"
     13 #include "src/objects.h"
     14 
     15 namespace v8 {
     16 
     17 class Isolate;
     18 
     19 namespace internal {
     20 namespace interpreter {
     21 
     22 class BytecodeArrayIterator;
     23 class SourcePositionTableIterator;
     24 
     25 class BytecodeExpectationsPrinter final {
     26  public:
     27   enum class ConstantPoolType {
     28     kUnknown,
     29     kString,
     30     kNumber,
     31     kMixed,
     32   };
     33 
     34   BytecodeExpectationsPrinter(v8::Isolate* i,
     35                               ConstantPoolType t = ConstantPoolType::kMixed)
     36       : isolate_(i),
     37         const_pool_type_(t),
     38         execute_(true),
     39         wrap_(true),
     40         top_level_(false),
     41         test_function_name_(kDefaultTopFunctionName) {}
     42 
     43   void PrintExpectation(std::ostream& stream,  // NOLINT
     44                         const std::string& snippet) const;
     45 
     46   void set_constant_pool_type(ConstantPoolType const_pool_type) {
     47     const_pool_type_ = const_pool_type;
     48   }
     49   ConstantPoolType const_pool_type() const { return const_pool_type_; }
     50 
     51   void set_execute(bool execute) { execute_ = execute; }
     52   bool execute() const { return execute_; }
     53 
     54   void set_wrap(bool wrap) { wrap_ = wrap; }
     55   bool wrap() const { return wrap_; }
     56 
     57   void set_top_level(bool top_level) { top_level_ = top_level; }
     58   bool top_level() const { return top_level_; }
     59 
     60   void set_test_function_name(const std::string& test_function_name) {
     61     test_function_name_ = test_function_name;
     62   }
     63   std::string test_function_name() const { return test_function_name_; }
     64 
     65  private:
     66   void PrintEscapedString(std::ostream& stream,  // NOLINT
     67                           const std::string& string) const;
     68   void PrintBytecodeOperand(std::ostream& stream,  // NOLINT
     69                             const BytecodeArrayIterator& bytecode_iterator,
     70                             const Bytecode& bytecode, int op_index,
     71                             int parameter_count) const;
     72   void PrintBytecode(std::ostream& stream,  // NOLINT
     73                      const BytecodeArrayIterator& bytecode_iterator,
     74                      int parameter_count) const;
     75   void PrintSourcePosition(std::ostream& stream,  // NOLINT
     76                            SourcePositionTableIterator& source_iterator,
     77                            int bytecode_offset) const;
     78   void PrintV8String(std::ostream& stream,  // NOLINT
     79                      i::String* string) const;
     80   void PrintConstant(std::ostream& stream,  // NOLINT
     81                      i::Handle<i::Object> constant) const;
     82   void PrintFrameSize(std::ostream& stream,  // NOLINT
     83                       i::Handle<i::BytecodeArray> bytecode_array) const;
     84   void PrintBytecodeSequence(std::ostream& stream,  // NOLINT
     85                              i::Handle<i::BytecodeArray> bytecode_array) const;
     86   void PrintConstantPool(std::ostream& stream,  // NOLINT
     87                          i::FixedArray* constant_pool) const;
     88   void PrintCodeSnippet(std::ostream& stream,  // NOLINT
     89                         const std::string& body) const;
     90   void PrintBytecodeArray(std::ostream& stream,  // NOLINT
     91                           i::Handle<i::BytecodeArray> bytecode_array) const;
     92   void PrintHandlers(std::ostream& stream,  // NOLINT
     93                      i::Handle<i::BytecodeArray> bytecode_array) const;
     94 
     95   v8::Local<v8::String> V8StringFromUTF8(const char* data) const;
     96   std::string WrapCodeInFunction(const char* function_name,
     97                                  const std::string& function_body) const;
     98 
     99   v8::Local<v8::Script> Compile(const char* program) const;
    100   void Run(v8::Local<v8::Script> script) const;
    101   i::Handle<i::BytecodeArray> GetBytecodeArrayForGlobal(
    102       const char* global_name) const;
    103   i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForScript(
    104       v8::Local<v8::Script> script) const;
    105 
    106   i::Isolate* i_isolate() const {
    107     return reinterpret_cast<i::Isolate*>(isolate_);
    108   }
    109 
    110   v8::Isolate* isolate_;
    111   ConstantPoolType const_pool_type_;
    112   bool execute_;
    113   bool wrap_;
    114   bool top_level_;
    115   std::string test_function_name_;
    116 
    117   static const char* const kDefaultTopFunctionName;
    118   static const char* const kIndent;
    119 };
    120 
    121 }  // namespace interpreter
    122 }  // namespace internal
    123 }  // namespace v8
    124 
    125 #endif  // TEST_CCTEST_INTERPRETER_BYTECODE_EXPECTATIONS_PRINTER_H_
    126