blob: 76d881ed58be4835df8372ec9e516278875d22dc [file] [log] [blame]
Ben Murdochc5610432016-08-08 18:44:38 +01001// 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"
Ben Murdoch61f157c2016-09-16 13:49:30 +01009#include "src/interpreter/source-position-table.h"
Ben Murdochc5610432016-08-08 18:44:38 +010010
11namespace v8 {
12namespace internal {
13namespace interpreter {
14
Ben Murdoch61f157c2016-09-16 13:49:30 +010015class BytecodeLabel;
Ben Murdochc5610432016-08-08 18:44:38 +010016class SourcePositionTableBuilder;
Ben Murdoch61f157c2016-09-16 13:49:30 +010017class ConstantArrayBuilder;
Ben Murdochc5610432016-08-08 18:44:38 +010018
19// Class for emitting bytecode as the final stage of the bytecode
20// generation pipeline.
21class BytecodeArrayWriter final : public BytecodePipelineStage {
22 public:
Ben Murdoch61f157c2016-09-16 13:49:30 +010023 BytecodeArrayWriter(Isolate* isolate, Zone* zone,
24 ConstantArrayBuilder* constant_array_builder);
Ben Murdochc5610432016-08-08 18:44:38 +010025 virtual ~BytecodeArrayWriter();
26
Ben Murdoch61f157c2016-09-16 13:49:30 +010027 // BytecodePipelineStage interface.
Ben Murdochc5610432016-08-08 18:44:38 +010028 void Write(BytecodeNode* node) override;
Ben Murdoch61f157c2016-09-16 13:49:30 +010029 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;
Ben Murdochc5610432016-08-08 18:44:38 +010035
36 private:
Ben Murdoch61f157c2016-09-16 13:49:30 +010037 // 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
Ben Murdochc5610432016-08-08 18:44:38 +010051 void EmitBytecode(const BytecodeNode* const node);
Ben Murdoch61f157c2016-09-16 13:49:30 +010052 void EmitJump(BytecodeNode* node, BytecodeLabel* label);
Ben Murdochc5610432016-08-08 18:44:38 +010053 void UpdateSourcePositionTable(const BytecodeNode* const node);
54
Ben Murdoch61f157c2016-09-16 13:49:30 +010055 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_;
Ben Murdochc5610432016-08-08 18:44:38 +010066 ZoneVector<uint8_t> bytecodes_;
67 int max_register_count_;
Ben Murdoch61f157c2016-09-16 13:49:30 +010068 int unbound_jumps_;
69 SourcePositionTableBuilder source_position_table_builder_;
70 ConstantArrayBuilder* constant_array_builder_;
Ben Murdochc5610432016-08-08 18:44:38 +010071
Ben Murdoch61f157c2016-09-16 13:49:30 +010072 friend class BytecodeArrayWriterUnittest;
Ben Murdochc5610432016-08-08 18:44:38 +010073 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_