V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
bytecode-array-writer.h
1 // Copyright 2015 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 V8_INTERPRETER_BYTECODE_ARRAY_WRITER_H_
6 #define V8_INTERPRETER_BYTECODE_ARRAY_WRITER_H_
7 
8 #include "src/base/compiler-specific.h"
9 #include "src/globals.h"
10 #include "src/interpreter/bytecodes.h"
11 #include "src/source-position-table.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class BytecodeArray;
17 class SourcePositionTableBuilder;
18 
19 namespace interpreter {
20 
21 class BytecodeLabel;
22 class BytecodeNode;
23 class BytecodeJumpTable;
24 class ConstantArrayBuilder;
25 
26 namespace bytecode_array_writer_unittest {
27 class BytecodeArrayWriterUnittest;
28 } // namespace bytecode_array_writer_unittest
29 
30 // Class for emitting bytecode as the final stage of the bytecode
31 // generation pipeline.
32 class V8_EXPORT_PRIVATE BytecodeArrayWriter final {
33  public:
35  Zone* zone, ConstantArrayBuilder* constant_array_builder,
36  SourcePositionTableBuilder::RecordingMode source_position_mode);
37 
38  void Write(BytecodeNode* node);
39  void WriteJump(BytecodeNode* node, BytecodeLabel* label);
40  void WriteSwitch(BytecodeNode* node, BytecodeJumpTable* jump_table);
41  void BindLabel(BytecodeLabel* label);
42  void BindLabel(const BytecodeLabel& target, BytecodeLabel* label);
43  void BindJumpTableEntry(BytecodeJumpTable* jump_table, int case_value);
44  Handle<BytecodeArray> ToBytecodeArray(Isolate* isolate, int register_count,
45  int parameter_count,
46  Handle<ByteArray> handler_table);
47 
48  bool RemainderOfBlockIsDead() const { return exit_seen_in_block_; }
49 
50  private:
51  // Maximum sized packed bytecode is comprised of a prefix bytecode,
52  // plus the actual bytecode, plus the maximum number of operands times
53  // the maximum operand size.
54  static const size_t kMaxSizeOfPackedBytecode =
55  2 * sizeof(Bytecode) +
56  Bytecodes::kMaxOperands * static_cast<size_t>(OperandSize::kLast);
57 
58  // Constants that act as placeholders for jump operands to be
59  // patched. These have operand sizes that match the sizes of
60  // reserved constant pool entries.
61  const uint32_t k8BitJumpPlaceholder = 0x7f;
62  const uint32_t k16BitJumpPlaceholder =
63  k8BitJumpPlaceholder | (k8BitJumpPlaceholder << 8);
64  const uint32_t k32BitJumpPlaceholder =
65  k16BitJumpPlaceholder | (k16BitJumpPlaceholder << 16);
66 
67  void PatchJump(size_t jump_target, size_t jump_location);
68  void PatchJumpWith8BitOperand(size_t jump_location, int delta);
69  void PatchJumpWith16BitOperand(size_t jump_location, int delta);
70  void PatchJumpWith32BitOperand(size_t jump_location, int delta);
71 
72  void EmitBytecode(const BytecodeNode* const node);
73  void EmitJump(BytecodeNode* node, BytecodeLabel* label);
74  void EmitSwitch(BytecodeNode* node, BytecodeJumpTable* jump_table);
75  void UpdateSourcePositionTable(const BytecodeNode* const node);
76 
77  void UpdateExitSeenInBlock(Bytecode bytecode);
78 
79  void MaybeElideLastBytecode(Bytecode next_bytecode, bool has_source_info);
80  void InvalidateLastBytecode();
81 
82  ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; }
83  SourcePositionTableBuilder* source_position_table_builder() {
84  return &source_position_table_builder_;
85  }
86  ConstantArrayBuilder* constant_array_builder() {
87  return constant_array_builder_;
88  }
89 
90  ZoneVector<uint8_t> bytecodes_;
91  int unbound_jumps_;
92  SourcePositionTableBuilder source_position_table_builder_;
93  ConstantArrayBuilder* constant_array_builder_;
94 
95  Bytecode last_bytecode_;
96  size_t last_bytecode_offset_;
97  bool last_bytecode_had_source_info_;
98  bool elide_noneffectful_bytecodes_;
99 
100  bool exit_seen_in_block_;
101 
102  friend class bytecode_array_writer_unittest::BytecodeArrayWriterUnittest;
103  DISALLOW_COPY_AND_ASSIGN(BytecodeArrayWriter);
104 };
105 
106 } // namespace interpreter
107 } // namespace internal
108 } // namespace v8
109 
110 #endif // V8_INTERPRETER_BYTECODE_ARRAY_WRITER_H_
Definition: libplatform.h:13