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/interpreter/bytecode-pipeline.h" 9 #include "src/interpreter/source-position-table.h" 10 11 namespace v8 { 12 namespace internal { 13 namespace interpreter { 14 15 class BytecodeLabel; 16 class SourcePositionTableBuilder; 17 class ConstantArrayBuilder; 18 19 // Class for emitting bytecode as the final stage of the bytecode 20 // generation pipeline. 21 class BytecodeArrayWriter final : public BytecodePipelineStage { 22 public: 23 BytecodeArrayWriter(Isolate* isolate, Zone* zone, 24 ConstantArrayBuilder* constant_array_builder); 25 virtual ~BytecodeArrayWriter(); 26 27 // BytecodePipelineStage interface. 28 void Write(BytecodeNode* node) override; 29 void WriteJump(BytecodeNode* node, BytecodeLabel* label) override; 30 void BindLabel(BytecodeLabel* label) override; 31 void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override; 32 Handle<BytecodeArray> ToBytecodeArray( 33 int fixed_register_count, int parameter_count, 34 Handle<FixedArray> handler_table) override; 35 36 private: 37 // Constants that act as placeholders for jump operands to be 38 // patched. These have operand sizes that match the sizes of 39 // reserved constant pool entries. 40 const uint32_t k8BitJumpPlaceholder = 0x7f; 41 const uint32_t k16BitJumpPlaceholder = 42 k8BitJumpPlaceholder | (k8BitJumpPlaceholder << 8); 43 const uint32_t k32BitJumpPlaceholder = 44 k16BitJumpPlaceholder | (k16BitJumpPlaceholder << 16); 45 46 void PatchJump(size_t jump_target, size_t jump_location); 47 void PatchJumpWith8BitOperand(size_t jump_location, int delta); 48 void PatchJumpWith16BitOperand(size_t jump_location, int delta); 49 void PatchJumpWith32BitOperand(size_t jump_location, int delta); 50 51 void EmitBytecode(const BytecodeNode* const node); 52 void EmitJump(BytecodeNode* node, BytecodeLabel* label); 53 void UpdateSourcePositionTable(const BytecodeNode* const node); 54 55 Isolate* isolate() { return isolate_; } 56 ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; } 57 SourcePositionTableBuilder* source_position_table_builder() { 58 return &source_position_table_builder_; 59 } 60 ConstantArrayBuilder* constant_array_builder() { 61 return constant_array_builder_; 62 } 63 int max_register_count() { return max_register_count_; } 64 65 Isolate* isolate_; 66 ZoneVector<uint8_t> bytecodes_; 67 int max_register_count_; 68 int unbound_jumps_; 69 SourcePositionTableBuilder source_position_table_builder_; 70 ConstantArrayBuilder* constant_array_builder_; 71 72 friend class BytecodeArrayWriterUnittest; 73 DISALLOW_COPY_AND_ASSIGN(BytecodeArrayWriter); 74 }; 75 76 } // namespace interpreter 77 } // namespace internal 78 } // namespace v8 79 80 #endif // V8_INTERPRETER_BYTECODE_ARRAY_WRITER_H_ 81