bytecode-array-writer.h 4.91 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2015 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_INTERPRETER_BYTECODE_ARRAY_WRITER_H_
#define V8_INTERPRETER_BYTECODE_ARRAY_WRITER_H_

#include "src/base/compiler-specific.h"
9
#include "src/codegen/source-position-table.h"
10
#include "src/common/globals.h"
11 12 13 14 15
#include "src/interpreter/bytecodes.h"

namespace v8 {
namespace internal {

16
class BytecodeArray;
17 18 19 20 21
class SourcePositionTableBuilder;

namespace interpreter {

class BytecodeLabel;
22
class BytecodeLoopHeader;
23 24 25
class BytecodeNode;
class BytecodeJumpTable;
class ConstantArrayBuilder;
26
class HandlerTableBuilder;
27

28 29 30 31
namespace bytecode_array_writer_unittest {
class BytecodeArrayWriterUnittest;
}  // namespace bytecode_array_writer_unittest

32 33 34 35 36 37 38
// Class for emitting bytecode as the final stage of the bytecode
// generation pipeline.
class V8_EXPORT_PRIVATE BytecodeArrayWriter final {
 public:
  BytecodeArrayWriter(
      Zone* zone, ConstantArrayBuilder* constant_array_builder,
      SourcePositionTableBuilder::RecordingMode source_position_mode);
39 40
  BytecodeArrayWriter(const BytecodeArrayWriter&) = delete;
  BytecodeArrayWriter& operator=(const BytecodeArrayWriter&) = delete;
41 42 43

  void Write(BytecodeNode* node);
  void WriteJump(BytecodeNode* node, BytecodeLabel* label);
44
  void WriteJumpLoop(BytecodeNode* node, BytecodeLoopHeader* loop_header);
45 46
  void WriteSwitch(BytecodeNode* node, BytecodeJumpTable* jump_table);
  void BindLabel(BytecodeLabel* label);
47
  void BindLoopHeader(BytecodeLoopHeader* loop_header);
48
  void BindJumpTableEntry(BytecodeJumpTable* jump_table, int case_value);
49 50 51 52 53 54 55
  void BindHandlerTarget(HandlerTableBuilder* handler_table_builder,
                         int handler_id);
  void BindTryRegionStart(HandlerTableBuilder* handler_table_builder,
                          int handler_id);
  void BindTryRegionEnd(HandlerTableBuilder* handler_table_builder,
                        int handler_id);

56 57
  void SetFunctionEntrySourcePosition(int position);

58
  template <typename IsolateT>
59
  EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
60 61
  Handle<BytecodeArray> ToBytecodeArray(IsolateT* isolate, int register_count,
                                        int parameter_count,
62
                                        Handle<ByteArray> handler_table);
63

64
  template <typename IsolateT>
65
  EXPORT_TEMPLATE_DECLARE(V8_EXPORT_PRIVATE)
66
  Handle<ByteArray> ToSourcePositionTable(IsolateT* isolate);
67 68

#ifdef DEBUG
69
  // Returns -1 if they match or the offset of the first mismatching byte.
70
  int CheckBytecodeMatches(BytecodeArray bytecode);
71 72
#endif

73 74
  bool RemainderOfBlockIsDead() const { return exit_seen_in_block_; }

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
 private:
  // Maximum sized packed bytecode is comprised of a prefix bytecode,
  // plus the actual bytecode, plus the maximum number of operands times
  // the maximum operand size.
  static const size_t kMaxSizeOfPackedBytecode =
      2 * sizeof(Bytecode) +
      Bytecodes::kMaxOperands * static_cast<size_t>(OperandSize::kLast);

  // Constants that act as placeholders for jump operands to be
  // patched. These have operand sizes that match the sizes of
  // reserved constant pool entries.
  const uint32_t k8BitJumpPlaceholder = 0x7f;
  const uint32_t k16BitJumpPlaceholder =
      k8BitJumpPlaceholder | (k8BitJumpPlaceholder << 8);
  const uint32_t k32BitJumpPlaceholder =
      k16BitJumpPlaceholder | (k16BitJumpPlaceholder << 16);

  void PatchJump(size_t jump_target, size_t jump_location);
  void PatchJumpWith8BitOperand(size_t jump_location, int delta);
  void PatchJumpWith16BitOperand(size_t jump_location, int delta);
  void PatchJumpWith32BitOperand(size_t jump_location, int delta);

  void EmitBytecode(const BytecodeNode* const node);
  void EmitJump(BytecodeNode* node, BytecodeLabel* label);
99
  void EmitJumpLoop(BytecodeNode* node, BytecodeLoopHeader* loop_header);
100 101 102 103 104 105 106 107
  void EmitSwitch(BytecodeNode* node, BytecodeJumpTable* jump_table);
  void UpdateSourcePositionTable(const BytecodeNode* const node);

  void UpdateExitSeenInBlock(Bytecode bytecode);

  void MaybeElideLastBytecode(Bytecode next_bytecode, bool has_source_info);
  void InvalidateLastBytecode();

108 109
  void StartBasicBlock();

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; }
  SourcePositionTableBuilder* source_position_table_builder() {
    return &source_position_table_builder_;
  }
  ConstantArrayBuilder* constant_array_builder() {
    return constant_array_builder_;
  }

  ZoneVector<uint8_t> bytecodes_;
  int unbound_jumps_;
  SourcePositionTableBuilder source_position_table_builder_;
  ConstantArrayBuilder* constant_array_builder_;

  Bytecode last_bytecode_;
  size_t last_bytecode_offset_;
  bool last_bytecode_had_source_info_;
  bool elide_noneffectful_bytecodes_;

  bool exit_seen_in_block_;

130
  friend class bytecode_array_writer_unittest::BytecodeArrayWriterUnittest;
131 132 133 134 135 136 137
};

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

#endif  // V8_INTERPRETER_BYTECODE_ARRAY_WRITER_H_