| Index: src/interpreter/bytecode-writer.h
|
| diff --git a/src/interpreter/bytecode-writer.h b/src/interpreter/bytecode-writer.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bcbb76f522d4117d7098028d21b44b64251f2527
|
| --- /dev/null
|
| +++ b/src/interpreter/bytecode-writer.h
|
| @@ -0,0 +1,188 @@
|
| +// Copyright 2015 the V8 project authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef V8_INTERPRETER_BYTECODE_WRITER_H_
|
| +#define V8_INTERPRETER_BYTECODE_WRITER_H_
|
| +
|
| +#include "src/interpreter/bytecode-register-allocator.h"
|
| +#include "src/interpreter/bytecodes.h"
|
| +#include "src/zone-containers.h"
|
| +
|
| +namespace v8 {
|
| +namespace internal {
|
| +namespace interpreter {
|
| +
|
| +class BytecodeArrayBuilder;
|
| +class SourcePositionTableBuilder;
|
| +class TemporaryRegisterAllocator;
|
| +
|
| +class BytecodeNodeAllocator;
|
| +
|
| +// Source code position information.
|
| +class BytecodeSourceInfo final {
|
| + public:
|
| + static const int kUninitializedPosition = -1;
|
| +
|
| + BytecodeSourceInfo(int position = kUninitializedPosition,
|
| + bool is_statement = false)
|
| + : source_position_(position), is_statement_(is_statement) {}
|
| +
|
| + // Combine later source info with current.
|
| + void Update(const BytecodeSourceInfo& entry);
|
| +
|
| + int source_position() const {
|
| + DCHECK(is_valid());
|
| + return source_position_;
|
| + }
|
| +
|
| + int is_statement() const { return is_valid() && is_statement_; }
|
| +
|
| + bool is_valid() const { return source_position_ != kUninitializedPosition; }
|
| + void set_invalid() { source_position_ = kUninitializedPosition; }
|
| +
|
| + bool operator==(const BytecodeSourceInfo& other) const {
|
| + return source_position_ == other.source_position_ &&
|
| + is_statement_ == other.is_statement_;
|
| + }
|
| + bool operator!=(const BytecodeSourceInfo& other) const {
|
| + return source_position_ != other.source_position_ ||
|
| + is_statement_ != other.is_statement_;
|
| + }
|
| +
|
| + private:
|
| + int source_position_;
|
| + bool is_statement_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(BytecodeSourceInfo);
|
| +};
|
| +
|
| +// A container for a generated bytecode, it's operands, and source information.
|
| +// These must be allocated by a BytecodeNodeAllocator instance.
|
| +class BytecodeNode final : ZoneObject {
|
| + public:
|
| + // Print to stream.
|
| + void Print(std::ostream& os);
|
| +
|
| + // Return the size when this node is serialized to a bytecode array.
|
| + size_t Size() const;
|
| +
|
| + // Return to node to allocator for re-use.
|
| + void Release();
|
| +
|
| + Bytecode bytecode() const { return bytecode_; }
|
| + void set_bytecode(Bytecode bytecode);
|
| + void set_bytecode(Bytecode bytecode, uint32_t operand0,
|
| + OperandScale operand_scale);
|
| + void set_bytecode(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
|
| + OperandScale operand_scale);
|
| + void set_bytecode(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
|
| + uint32_t operand2, OperandScale operand_scale);
|
| + void set_bytecode(Bytecode bytecode, uint32_t operand0, uint32_t operand1,
|
| + uint32_t operand2, uint32_t operand3,
|
| + OperandScale operand_scale);
|
| +
|
| + uint32_t* operands() { return operands_; }
|
| + const uint32_t* operands() const { return operands_; }
|
| +
|
| + int operand_count() const { return Bytecodes::NumberOfOperands(bytecode_); }
|
| +
|
| + OperandScale operand_scale() const { return operand_scale_; }
|
| +
|
| + const BytecodeSourceInfo& source_info() const { return source_info_; }
|
| + BytecodeSourceInfo& source_info() { return source_info_; }
|
| +
|
| + private:
|
| + static const int kInvalidPosition = kMinInt;
|
| + static const size_t kMaxOperands = 4;
|
| +
|
| + friend class BytecodeNodeAllocator;
|
| + explicit BytecodeNode(BytecodeNodeAllocator* allocator);
|
| +
|
| + Bytecode bytecode_;
|
| + uint32_t operands_[kMaxOperands];
|
| + OperandScale operand_scale_;
|
| + BytecodeSourceInfo source_info_;
|
| + BytecodeNodeAllocator* allocator_;
|
| + BytecodeNode* next_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(BytecodeNode);
|
| +};
|
| +
|
| +// Allocator for BytecodeNodes that zone allocates them when needed
|
| +// and maintains a freelist to reduce number of allocations and limit
|
| +// memory use.
|
| +class BytecodeNodeAllocator final {
|
| + public:
|
| + explicit BytecodeNodeAllocator(Zone* zone)
|
| + : allocation_count_(0), free_list_(nullptr), zone_(zone) {}
|
| +
|
| + BytecodeNode* Allocate();
|
| +
|
| + private:
|
| + friend class BytecodeNode;
|
| + static const int kMaxDebugAllocations = 8;
|
| +
|
| + void Release(BytecodeNode* node);
|
| +
|
| + Zone* zone() const { return zone_; }
|
| +
|
| + int allocation_count_;
|
| + BytecodeNode* free_list_;
|
| + Zone* zone_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(BytecodeNodeAllocator);
|
| +};
|
| +
|
| +// Interface for bytecode writers.
|
| +class BytecodeWriter {
|
| + public:
|
| + virtual ~BytecodeWriter() {}
|
| +
|
| + // Flush state for bytecode array offset calculation. Returns the
|
| + // current size of bytecode array.
|
| + virtual size_t FlushForOffset() = 0;
|
| +
|
| + // Signal end of basic block.
|
| + virtual void LeaveBasicBlock() = 0;
|
| +
|
| + // Write bytecode into pipeline. The callee owns node from here and
|
| + // the caller should not hold any references to it.
|
| + virtual void Write(BytecodeNode* node) = 0;
|
| +};
|
| +
|
| +// Class for emitting bytecode as the final stage of a bytecode writer
|
| +// chain.
|
| +class FinalStageBytecodeWriter : public BytecodeWriter {
|
| + public:
|
| + FinalStageBytecodeWriter(
|
| + Zone* zone, SourcePositionTableBuilder* source_position_table_builder);
|
| + virtual ~FinalStageBytecodeWriter();
|
| +
|
| + size_t FlushForOffset() override;
|
| + void LeaveBasicBlock() override;
|
| + void Write(BytecodeNode* node) override;
|
| +
|
| + // Get the bytecode vector.
|
| + ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; }
|
| +
|
| + // Returns the size in bytes of the frame associated with the
|
| + // bytecode written.
|
| + int GetMeasuredFrameSize();
|
| +
|
| + private:
|
| + void EmitBytecode(const BytecodeNode* const node);
|
| + void UpdateSourcePositionTable(const BytecodeNode* const node);
|
| +
|
| + ZoneVector<uint8_t> bytecodes_;
|
| + int frame_register_count_;
|
| + SourcePositionTableBuilder* source_position_table_builder_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(FinalStageBytecodeWriter);
|
| +};
|
| +
|
| +} // namespace interpreter
|
| +} // namespace internal
|
| +} // namespace v8
|
| +
|
| +#endif // V8_INTERPRETER_BYTECODE_WRITER_H_
|
|
|