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