• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
34   BytecodeArrayWriter(
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  private:
49   // Maximum sized packed bytecode is comprised of a prefix bytecode,
50   // plus the actual bytecode, plus the maximum number of operands times
51   // the maximum operand size.
52   static const size_t kMaxSizeOfPackedBytecode =
53       2 * sizeof(Bytecode) +
54       Bytecodes::kMaxOperands * static_cast<size_t>(OperandSize::kLast);
55 
56   // Constants that act as placeholders for jump operands to be
57   // patched. These have operand sizes that match the sizes of
58   // reserved constant pool entries.
59   const uint32_t k8BitJumpPlaceholder = 0x7f;
60   const uint32_t k16BitJumpPlaceholder =
61       k8BitJumpPlaceholder | (k8BitJumpPlaceholder << 8);
62   const uint32_t k32BitJumpPlaceholder =
63       k16BitJumpPlaceholder | (k16BitJumpPlaceholder << 16);
64 
65   void PatchJump(size_t jump_target, size_t jump_location);
66   void PatchJumpWith8BitOperand(size_t jump_location, int delta);
67   void PatchJumpWith16BitOperand(size_t jump_location, int delta);
68   void PatchJumpWith32BitOperand(size_t jump_location, int delta);
69 
70   void EmitBytecode(const BytecodeNode* const node);
71   void EmitJump(BytecodeNode* node, BytecodeLabel* label);
72   void EmitSwitch(BytecodeNode* node, BytecodeJumpTable* jump_table);
73   void UpdateSourcePositionTable(const BytecodeNode* const node);
74 
75   void UpdateExitSeenInBlock(Bytecode bytecode);
76 
77   void MaybeElideLastBytecode(Bytecode next_bytecode, bool has_source_info);
78   void InvalidateLastBytecode();
79 
bytecodes()80   ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; }
source_position_table_builder()81   SourcePositionTableBuilder* source_position_table_builder() {
82     return &source_position_table_builder_;
83   }
constant_array_builder()84   ConstantArrayBuilder* constant_array_builder() {
85     return constant_array_builder_;
86   }
87 
88   ZoneVector<uint8_t> bytecodes_;
89   int unbound_jumps_;
90   SourcePositionTableBuilder source_position_table_builder_;
91   ConstantArrayBuilder* constant_array_builder_;
92 
93   Bytecode last_bytecode_;
94   size_t last_bytecode_offset_;
95   bool last_bytecode_had_source_info_;
96   bool elide_noneffectful_bytecodes_;
97 
98   bool exit_seen_in_block_;
99 
100   friend class bytecode_array_writer_unittest::BytecodeArrayWriterUnittest;
101   DISALLOW_COPY_AND_ASSIGN(BytecodeArrayWriter);
102 };
103 
104 }  // namespace interpreter
105 }  // namespace internal
106 }  // namespace v8
107 
108 #endif  // V8_INTERPRETER_BYTECODE_ARRAY_WRITER_H_
109