2015-09-10 16:21:34 +00:00
|
|
|
// 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_COMPILER_BYTECODE_GRAPH_BUILDER_H_
|
|
|
|
#define V8_COMPILER_BYTECODE_GRAPH_BUILDER_H_
|
|
|
|
|
|
|
|
#include "src/compiler.h"
|
2015-12-16 16:29:09 +00:00
|
|
|
#include "src/compiler/bytecode-branch-analysis.h"
|
2015-09-10 16:21:34 +00:00
|
|
|
#include "src/compiler/js-graph.h"
|
|
|
|
#include "src/interpreter/bytecode-array-iterator.h"
|
|
|
|
#include "src/interpreter/bytecodes.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
// The BytecodeGraphBuilder produces a high-level IR graph based on
|
|
|
|
// interpreter bytecodes.
|
|
|
|
class BytecodeGraphBuilder {
|
|
|
|
public:
|
|
|
|
BytecodeGraphBuilder(Zone* local_zone, CompilationInfo* info,
|
|
|
|
JSGraph* jsgraph);
|
|
|
|
|
|
|
|
// Creates a graph by visiting bytecodes.
|
|
|
|
bool CreateGraph(bool stack_check = true);
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Environment;
|
2015-12-18 08:41:10 +00:00
|
|
|
class FrameStateBeforeAndAfter;
|
2015-09-10 16:21:34 +00:00
|
|
|
|
|
|
|
void CreateGraphBody(bool stack_check);
|
|
|
|
void VisitBytecodes();
|
|
|
|
|
2015-11-17 09:05:37 +00:00
|
|
|
// Get or create the node that represents the outer function closure.
|
|
|
|
Node* GetFunctionClosure();
|
|
|
|
|
|
|
|
// Get or create the node that represents the outer function context.
|
2015-09-10 16:21:34 +00:00
|
|
|
Node* GetFunctionContext();
|
|
|
|
|
2015-12-01 17:28:43 +00:00
|
|
|
// Get or create the node that represents the incoming new target value.
|
|
|
|
Node* GetNewTarget();
|
|
|
|
|
2015-11-20 09:25:13 +00:00
|
|
|
// Builder for accessing a (potentially immutable) object field.
|
|
|
|
Node* BuildLoadObjectField(Node* object, int offset);
|
2015-11-17 09:05:37 +00:00
|
|
|
Node* BuildLoadImmutableObjectField(Node* object, int offset);
|
|
|
|
|
|
|
|
// Builder for accessing type feedback vector.
|
|
|
|
Node* BuildLoadFeedbackVector();
|
|
|
|
|
2015-11-20 09:25:13 +00:00
|
|
|
// Builder for loading the a native context field.
|
|
|
|
Node* BuildLoadNativeContextField(int index);
|
|
|
|
|
2015-11-17 09:05:37 +00:00
|
|
|
// Helper function for creating a pair containing type feedback vector and
|
|
|
|
// a feedback slot.
|
|
|
|
VectorSlotPair CreateVectorSlotPair(int slot_id);
|
|
|
|
|
2015-09-10 16:21:34 +00:00
|
|
|
void set_environment(Environment* env) { environment_ = env; }
|
|
|
|
const Environment* environment() const { return environment_; }
|
|
|
|
Environment* environment() { return environment_; }
|
|
|
|
|
|
|
|
// Node creation helpers
|
|
|
|
Node* NewNode(const Operator* op, bool incomplete = false) {
|
2016-01-11 12:56:50 +00:00
|
|
|
return MakeNode(op, 0, static_cast<Node**>(nullptr), incomplete);
|
2015-09-10 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* NewNode(const Operator* op, Node* n1) {
|
|
|
|
Node* buffer[] = {n1};
|
|
|
|
return MakeNode(op, arraysize(buffer), buffer, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* NewNode(const Operator* op, Node* n1, Node* n2) {
|
|
|
|
Node* buffer[] = {n1, n2};
|
|
|
|
return MakeNode(op, arraysize(buffer), buffer, false);
|
|
|
|
}
|
|
|
|
|
2015-11-17 09:05:37 +00:00
|
|
|
Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3) {
|
|
|
|
Node* buffer[] = {n1, n2, n3};
|
|
|
|
return MakeNode(op, arraysize(buffer), buffer, false);
|
|
|
|
}
|
|
|
|
|
2015-11-19 14:16:36 +00:00
|
|
|
Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4) {
|
|
|
|
Node* buffer[] = {n1, n2, n3, n4};
|
|
|
|
return MakeNode(op, arraysize(buffer), buffer, false);
|
|
|
|
}
|
|
|
|
|
2015-12-16 16:29:09 +00:00
|
|
|
// Helpers to create new control nodes.
|
|
|
|
Node* NewIfTrue() { return NewNode(common()->IfTrue()); }
|
|
|
|
Node* NewIfFalse() { return NewNode(common()->IfFalse()); }
|
|
|
|
Node* NewMerge() { return NewNode(common()->Merge(1), true); }
|
|
|
|
Node* NewLoop() { return NewNode(common()->Loop(1), true); }
|
|
|
|
Node* NewBranch(Node* condition, BranchHint hint = BranchHint::kNone) {
|
|
|
|
return NewNode(common()->Branch(hint), condition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new Phi node having {count} input values.
|
|
|
|
Node* NewPhi(int count, Node* input, Node* control);
|
|
|
|
Node* NewEffectPhi(int count, Node* input, Node* control);
|
2015-09-10 16:21:34 +00:00
|
|
|
|
2015-12-16 16:29:09 +00:00
|
|
|
// Helpers for merging control, effect or value dependencies.
|
2015-09-10 16:21:34 +00:00
|
|
|
Node* MergeControl(Node* control, Node* other);
|
2015-12-16 16:29:09 +00:00
|
|
|
Node* MergeEffect(Node* effect, Node* other_effect, Node* control);
|
|
|
|
Node* MergeValue(Node* value, Node* other_value, Node* control);
|
2015-09-10 16:21:34 +00:00
|
|
|
|
2015-12-16 16:29:09 +00:00
|
|
|
// The main node creation chokepoint. Adds context, frame state, effect,
|
|
|
|
// and control dependencies depending on the operator.
|
|
|
|
Node* MakeNode(const Operator* op, int value_input_count, Node** value_inputs,
|
|
|
|
bool incomplete);
|
2015-09-10 16:21:34 +00:00
|
|
|
|
2015-12-16 16:29:09 +00:00
|
|
|
Node** EnsureInputBufferSize(int size);
|
|
|
|
|
2015-11-20 09:25:13 +00:00
|
|
|
Node* ProcessCallArguments(const Operator* call_op, Node* callee,
|
2015-11-17 12:18:25 +00:00
|
|
|
interpreter::Register receiver, size_t arity);
|
2015-11-20 09:25:13 +00:00
|
|
|
Node* ProcessCallNewArguments(const Operator* call_new_op,
|
|
|
|
interpreter::Register callee,
|
|
|
|
interpreter::Register first_arg, size_t arity);
|
|
|
|
Node* ProcessCallRuntimeArguments(const Operator* call_runtime_op,
|
|
|
|
interpreter::Register first_arg,
|
|
|
|
size_t arity);
|
2015-11-17 12:18:25 +00:00
|
|
|
|
2016-01-29 10:15:26 +00:00
|
|
|
void BuildCreateLiteral(const Operator* op);
|
|
|
|
void BuildCreateRegExpLiteral();
|
|
|
|
void BuildCreateArrayLiteral();
|
|
|
|
void BuildCreateObjectLiteral();
|
|
|
|
void BuildCreateArguments(CreateArgumentsParameters::Type type);
|
|
|
|
void BuildLoadGlobal(TypeofMode typeof_mode);
|
|
|
|
void BuildStoreGlobal();
|
|
|
|
void BuildNamedLoad();
|
|
|
|
void BuildKeyedLoad();
|
|
|
|
void BuildNamedStore();
|
|
|
|
void BuildKeyedStore();
|
|
|
|
void BuildLdaLookupSlot(TypeofMode typeof_mode);
|
|
|
|
void BuildStaLookupSlot(LanguageMode language_mode);
|
|
|
|
void BuildCall();
|
|
|
|
void BuildCallJSRuntime();
|
|
|
|
void BuildCallRuntime();
|
|
|
|
void BuildCallRuntimeForPair();
|
|
|
|
void BuildCallConstruct();
|
2016-02-01 14:01:22 +00:00
|
|
|
void BuildThrow();
|
2016-01-29 10:15:26 +00:00
|
|
|
void BuildBinaryOp(const Operator* op);
|
|
|
|
void BuildCompareOp(const Operator* op);
|
|
|
|
void BuildDelete();
|
2016-02-01 12:17:53 +00:00
|
|
|
void BuildCastOperator(const Operator* op);
|
2016-01-29 10:15:26 +00:00
|
|
|
void BuildForInPrepare();
|
|
|
|
void BuildForInNext();
|
2015-11-17 12:18:25 +00:00
|
|
|
|
2015-12-16 16:29:09 +00:00
|
|
|
// Control flow plumbing.
|
|
|
|
void BuildJump();
|
|
|
|
void BuildConditionalJump(Node* condition);
|
2016-01-05 19:08:11 +00:00
|
|
|
void BuildJumpIfEqual(Node* comperand);
|
|
|
|
void BuildJumpIfToBooleanEqual(Node* boolean_comperand);
|
2015-12-16 16:29:09 +00:00
|
|
|
|
2016-02-01 09:47:19 +00:00
|
|
|
// Simulates control flow by forward-propagating environments.
|
|
|
|
void MergeIntoSuccessorEnvironment(int target_offset);
|
|
|
|
void BuildLoopHeaderEnvironment(int current_offset);
|
|
|
|
void SwitchToMergeEnvironment(int current_offset);
|
2015-12-16 16:29:09 +00:00
|
|
|
|
2016-02-01 12:17:53 +00:00
|
|
|
// Simulates control flow that exits the function body.
|
|
|
|
void MergeControlToLeaveFunction(Node* exit);
|
|
|
|
|
2016-01-28 12:18:14 +00:00
|
|
|
// Simulates entry and exit of exception handlers.
|
|
|
|
void EnterAndExitExceptionHandlers(int current_offset);
|
|
|
|
|
2016-01-12 17:08:03 +00:00
|
|
|
// Attaches a frame state to |node| for the entry to the function.
|
|
|
|
void PrepareEntryFrameState(Node* node);
|
|
|
|
|
2015-09-10 16:21:34 +00:00
|
|
|
// Growth increment for the temporary buffer used to construct input lists to
|
|
|
|
// new nodes.
|
|
|
|
static const int kInputBufferSizeIncrement = 64;
|
|
|
|
|
2016-01-28 12:18:14 +00:00
|
|
|
// An abstract representation for an exception handler that is being
|
|
|
|
// entered and exited while the graph builder is iterating over the
|
|
|
|
// underlying bytecode. The exception handlers within the bytecode are
|
|
|
|
// well scoped, hence will form a stack during iteration.
|
|
|
|
struct ExceptionHandler {
|
|
|
|
int start_offset_; // Start offset of the handled area in the bytecode.
|
|
|
|
int end_offset_; // End offset of the handled area in the bytecode.
|
|
|
|
int handler_offset_; // Handler entry offset within the bytecode.
|
|
|
|
};
|
|
|
|
|
2015-09-10 16:21:34 +00:00
|
|
|
// Field accessors
|
2016-01-29 10:15:26 +00:00
|
|
|
Graph* graph() const { return jsgraph_->graph(); }
|
2015-09-10 16:21:34 +00:00
|
|
|
CommonOperatorBuilder* common() const { return jsgraph_->common(); }
|
|
|
|
Zone* graph_zone() const { return graph()->zone(); }
|
|
|
|
CompilationInfo* info() const { return info_; }
|
|
|
|
JSGraph* jsgraph() const { return jsgraph_; }
|
|
|
|
JSOperatorBuilder* javascript() const { return jsgraph_->javascript(); }
|
|
|
|
Zone* local_zone() const { return local_zone_; }
|
|
|
|
const Handle<BytecodeArray>& bytecode_array() const {
|
|
|
|
return bytecode_array_;
|
|
|
|
}
|
2016-01-28 12:18:14 +00:00
|
|
|
const Handle<HandlerTable>& exception_handler_table() const {
|
|
|
|
return exception_handler_table_;
|
|
|
|
}
|
2015-12-18 08:41:10 +00:00
|
|
|
const FrameStateFunctionInfo* frame_state_function_info() const {
|
|
|
|
return frame_state_function_info_;
|
|
|
|
}
|
2015-09-10 16:21:34 +00:00
|
|
|
|
|
|
|
LanguageMode language_mode() const {
|
2015-11-17 09:05:37 +00:00
|
|
|
// TODO(mythria): Don't rely on parse information to get language mode.
|
|
|
|
return info()->language_mode();
|
2015-09-10 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 10:15:26 +00:00
|
|
|
const interpreter::BytecodeArrayIterator& bytecode_iterator() const {
|
|
|
|
return *bytecode_iterator_;
|
2015-12-16 16:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_bytecode_iterator(
|
|
|
|
const interpreter::BytecodeArrayIterator* bytecode_iterator) {
|
|
|
|
bytecode_iterator_ = bytecode_iterator;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BytecodeBranchAnalysis* branch_analysis() const {
|
|
|
|
return branch_analysis_;
|
|
|
|
}
|
|
|
|
|
2016-01-28 12:18:14 +00:00
|
|
|
void set_branch_analysis(BytecodeBranchAnalysis* branch_analysis) {
|
2015-12-16 16:29:09 +00:00
|
|
|
branch_analysis_ = branch_analysis;
|
|
|
|
}
|
|
|
|
|
2016-01-29 10:15:26 +00:00
|
|
|
#define DECLARE_VISIT_BYTECODE(name, ...) void Visit##name();
|
2015-09-10 16:21:34 +00:00
|
|
|
BYTECODE_LIST(DECLARE_VISIT_BYTECODE)
|
|
|
|
#undef DECLARE_VISIT_BYTECODE
|
|
|
|
|
|
|
|
Zone* local_zone_;
|
|
|
|
CompilationInfo* info_;
|
|
|
|
JSGraph* jsgraph_;
|
|
|
|
Handle<BytecodeArray> bytecode_array_;
|
2016-01-28 12:18:14 +00:00
|
|
|
Handle<HandlerTable> exception_handler_table_;
|
2015-12-18 08:41:10 +00:00
|
|
|
const FrameStateFunctionInfo* frame_state_function_info_;
|
2015-12-16 16:29:09 +00:00
|
|
|
const interpreter::BytecodeArrayIterator* bytecode_iterator_;
|
2016-01-28 12:18:14 +00:00
|
|
|
BytecodeBranchAnalysis* branch_analysis_; // TODO(mstarzinger): Make const.
|
2015-09-10 16:21:34 +00:00
|
|
|
Environment* environment_;
|
|
|
|
|
2016-02-01 09:47:19 +00:00
|
|
|
// Merge environments are snapshots of the environment at points where the
|
|
|
|
// control flow merges. This models a forward data flow propagation of all
|
|
|
|
// values from all predecessors of the merge in question.
|
2015-12-16 16:29:09 +00:00
|
|
|
ZoneMap<int, Environment*> merge_environments_;
|
|
|
|
|
2016-01-28 12:18:14 +00:00
|
|
|
// Exception handlers currently entered by the iteration.
|
|
|
|
ZoneStack<ExceptionHandler> exception_handlers_;
|
|
|
|
int current_exception_handler_;
|
|
|
|
|
2015-09-10 16:21:34 +00:00
|
|
|
// Temporary storage for building node input lists.
|
|
|
|
int input_buffer_size_;
|
|
|
|
Node** input_buffer_;
|
|
|
|
|
|
|
|
// Nodes representing values in the activation record.
|
|
|
|
SetOncePointer<Node> function_context_;
|
2015-11-17 09:05:37 +00:00
|
|
|
SetOncePointer<Node> function_closure_;
|
2015-12-01 17:28:43 +00:00
|
|
|
SetOncePointer<Node> new_target_;
|
2015-11-17 09:05:37 +00:00
|
|
|
|
|
|
|
// Optimization to cache loaded feedback vector.
|
|
|
|
SetOncePointer<Node> feedback_vector_;
|
2015-09-10 16:21:34 +00:00
|
|
|
|
|
|
|
// Control nodes that exit the function body.
|
|
|
|
ZoneVector<Node*> exit_controls_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BytecodeGraphBuilder);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_COMPILER_BYTECODE_GRAPH_BUILDER_H_
|