Commit b10a1141 authored by whesse@chromium.org's avatar whesse@chromium.org

Add tracing of the stack height to full code generator.

BUG=
TEST=

Review URL: http://codereview.chromium.org/7097011

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8755 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 2ff0383c
......@@ -400,6 +400,7 @@ DEFINE_bool(print_json_ast, false, "print source AST as JSON")
DEFINE_bool(print_builtin_json_ast, false,
"print source AST for builtins as JSON")
DEFINE_string(stop_at, "", "function name where to insert a breakpoint")
DEFINE_bool(verify_stack_height, false, "verify stack height tracing on ia32")
// compiler.cc
DEFINE_bool(print_builtin_scopes, false, "print scopes for builtins")
......
......@@ -437,6 +437,7 @@ void FullCodeGenerator::AccumulatorValueContext::Plug(Register reg) const {
void FullCodeGenerator::StackValueContext::Plug(Register reg) const {
__ push(reg);
codegen()->increment_stack_height();
}
......@@ -450,11 +451,13 @@ void FullCodeGenerator::TestContext::Plug(Register reg) const {
void FullCodeGenerator::EffectContext::PlugTOS() const {
__ Drop(1);
codegen()->decrement_stack_height();
}
void FullCodeGenerator::AccumulatorValueContext::PlugTOS() const {
__ pop(result_register());
codegen()->decrement_stack_height();
}
......@@ -465,6 +468,7 @@ void FullCodeGenerator::StackValueContext::PlugTOS() const {
void FullCodeGenerator::TestContext::PlugTOS() const {
// For simplicity we always test the accumulator register.
__ pop(result_register());
codegen()->decrement_stack_height();
codegen()->PrepareForBailoutBeforeSplit(TOS_REG, false, NULL, NULL);
codegen()->DoTest(this);
}
......@@ -960,6 +964,7 @@ void FullCodeGenerator::VisitEnterWithContextStatement(
VisitForStackValue(stmt->expression());
PushFunctionArgumentForContextAllocation();
__ CallRuntime(Runtime::kPushWithContext, 2);
decrement_stack_height();
StoreToFrameField(StandardFrameConstants::kContextOffset, context_register());
}
......@@ -1128,8 +1133,10 @@ void FullCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
{
TryCatch try_block(this, &catch_entry);
__ PushTryHandler(IN_JAVASCRIPT, TRY_CATCH_HANDLER);
increment_stack_height(StackHandlerConstants::kSize / kPointerSize);
Visit(stmt->try_block());
__ PopTryHandler();
decrement_stack_height(StackHandlerConstants::kSize / kPointerSize);
}
__ bind(&done);
}
......@@ -1161,6 +1168,10 @@ void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
// cooked before GC.
Label finally_entry;
Label try_handler_setup;
const int original_stack_height = stack_height();
const int finally_block_stack_height = original_stack_height + 2;
const int try_block_stack_height = original_stack_height + 4;
STATIC_ASSERT(StackHandlerConstants::kSize / kPointerSize == 4);
// Setup the try-handler chain. Use a call to
// Jump to try-handler setup and try-block code. Use call to put try-handler
......@@ -1182,6 +1193,7 @@ void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
// Finally block implementation.
Finally finally_block(this);
EnterFinallyBlock();
set_stack_height(finally_block_stack_height);
Visit(stmt->finally_block());
ExitFinallyBlock(); // Return to the calling code.
}
......@@ -1191,8 +1203,10 @@ void FullCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
// Setup try handler (stack pointer registers).
TryFinally try_block(this, &finally_entry);
__ PushTryHandler(IN_JAVASCRIPT, TRY_FINALLY_HANDLER);
set_stack_height(try_block_stack_height);
Visit(stmt->try_block());
__ PopTryHandler();
set_stack_height(original_stack_height);
}
// Execute the finally block on the way out. Clobber the unpredictable
// value in the accumulator with one that's safe for GC. The finally
......@@ -1222,6 +1236,7 @@ void FullCodeGenerator::VisitConditional(Conditional* expr) {
__ bind(&true_case);
SetExpressionPosition(expr->then_expression(),
expr->then_expression_position());
int start_stack_height = stack_height();
if (context()->IsTest()) {
const TestContext* for_test = TestContext::cast(context());
VisitForControl(expr->then_expression(),
......@@ -1235,6 +1250,7 @@ void FullCodeGenerator::VisitConditional(Conditional* expr) {
PrepareForBailoutForId(expr->ElseId(), NO_REGISTERS);
__ bind(&false_case);
set_stack_height(start_stack_height);
if (context()->IsTest()) ForwardBailoutToChild(expr);
SetExpressionPosition(expr->else_expression(),
expr->else_expression_position());
......@@ -1275,8 +1291,11 @@ void FullCodeGenerator::VisitSharedFunctionInfoLiteral(
void FullCodeGenerator::VisitThrow(Throw* expr) {
Comment cmnt(masm_, "[ Throw");
// Throw has no effect on the stack height or the current expression context.
// Usually the expression context is null, because throw is a statement.
VisitForStackValue(expr->exception());
__ CallRuntime(Runtime::kThrow, 1);
decrement_stack_height();
// Never returns here.
}
......
......@@ -83,6 +83,7 @@ class FullCodeGenerator: public AstVisitor {
scope_(NULL),
nesting_stack_(NULL),
loop_depth_(0),
stack_height_(0),
context_(NULL),
bailout_entries_(0),
stack_checks_(2), // There's always at least one.
......@@ -519,6 +520,35 @@ class FullCodeGenerator: public AstVisitor {
loop_depth_--;
}
#if defined(V8_TARGET_ARCH_IA32)
int stack_height() { return stack_height_; }
void set_stack_height(int depth) { stack_height_ = depth; }
void increment_stack_height() { stack_height_++; }
void increment_stack_height(int delta) { stack_height_ += delta; }
void decrement_stack_height() {
if (FLAG_verify_stack_height) {
ASSERT(stack_height_ > 0);
}
stack_height_--;
}
void decrement_stack_height(int delta) {
stack_height_-= delta;
if (FLAG_verify_stack_height) {
ASSERT(stack_height_ >= 0);
}
}
// Call this function only if FLAG_verify_stack_height is true.
void verify_stack_height(); // Generates a runtime check of esp - ebp.
#else
int stack_height() { return 0; }
void set_stack_height(int depth) {}
void increment_stack_height() {}
void increment_stack_height(int delta) {}
void decrement_stack_height() {}
void decrement_stack_height(int delta) {}
void verify_stack_height() {}
#endif // V8_TARGET_ARCH_IA32
MacroAssembler* masm() { return masm_; }
class ExpressionContext;
......@@ -578,6 +608,10 @@ class FullCodeGenerator: public AstVisitor {
virtual ~ExpressionContext() {
codegen_->set_new_context(old_);
if (FLAG_verify_stack_height) {
ASSERT_EQ(expected_stack_height_, codegen()->stack_height());
codegen()->verify_stack_height();
}
}
Isolate* isolate() const { return codegen_->isolate(); }
......@@ -631,6 +665,7 @@ class FullCodeGenerator: public AstVisitor {
FullCodeGenerator* codegen() const { return codegen_; }
MacroAssembler* masm() const { return masm_; }
MacroAssembler* masm_;
int expected_stack_height_; // The expected stack height esp - ebp on exit.
private:
const ExpressionContext* old_;
......@@ -640,7 +675,9 @@ class FullCodeGenerator: public AstVisitor {
class AccumulatorValueContext : public ExpressionContext {
public:
explicit AccumulatorValueContext(FullCodeGenerator* codegen)
: ExpressionContext(codegen) { }
: ExpressionContext(codegen) {
expected_stack_height_ = codegen->stack_height();
}
virtual void Plug(bool flag) const;
virtual void Plug(Register reg) const;
......@@ -661,7 +698,9 @@ class FullCodeGenerator: public AstVisitor {
class StackValueContext : public ExpressionContext {
public:
explicit StackValueContext(FullCodeGenerator* codegen)
: ExpressionContext(codegen) { }
: ExpressionContext(codegen) {
expected_stack_height_ = codegen->stack_height() + 1;
}
virtual void Plug(bool flag) const;
virtual void Plug(Register reg) const;
......@@ -690,7 +729,9 @@ class FullCodeGenerator: public AstVisitor {
condition_(condition),
true_label_(true_label),
false_label_(false_label),
fall_through_(fall_through) { }
fall_through_(fall_through) {
expected_stack_height_ = codegen->stack_height();
}
static const TestContext* cast(const ExpressionContext* context) {
ASSERT(context->IsTest());
......@@ -727,7 +768,10 @@ class FullCodeGenerator: public AstVisitor {
class EffectContext : public ExpressionContext {
public:
explicit EffectContext(FullCodeGenerator* codegen)
: ExpressionContext(codegen) { }
: ExpressionContext(codegen) {
expected_stack_height_ = codegen->stack_height();
}
virtual void Plug(bool flag) const;
virtual void Plug(Register reg) const;
......@@ -751,6 +795,7 @@ class FullCodeGenerator: public AstVisitor {
Label return_label_;
NestedStatement* nesting_stack_;
int loop_depth_;
int stack_height_;
const ExpressionContext* context_;
ZoneList<BailoutEntry> bailout_entries_;
ZoneList<BailoutEntry> stack_checks_;
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment