From b3dd6b686ae68407150dd829a76929aa588b59db Mon Sep 17 00:00:00 2001 From: "sgjesse@chromium.org" Date: Wed, 17 Dec 2008 08:45:42 +0000 Subject: [PATCH] Refactored the recording of source position in the generated code. The code generator now has two methods void CodeForStatement(Node* node) void CodeForSourcePosition(int pos) The first is used to indicate that code is about to be generated for the given statement and the second is used to indicate that code is about to be generated for the given source position. Added position information for some statements which was missing whem. Updated the code generator for ARM to emit source position the same way as for IA-32. Added an assert to ensure that deferred code stubs will always have a source source position as if it has not it will take whatever source position before which makes no sense. The passing test on ARM has only been tested using the simulator. Review URL: http://codereview.chromium.org/14170 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@985 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/assembler-arm.cc | 42 +++++++++++++++------- src/assembler-arm.h | 16 ++++----- src/assembler-ia32.cc | 33 +++++++++++------- src/assembler-ia32.h | 10 +++--- src/codegen-arm.cc | 65 +++++++++++++++++----------------- src/codegen-arm.h | 9 ++--- src/codegen-ia32.cc | 69 ++++++++++++++++--------------------- src/codegen-ia32.h | 10 +++--- src/codegen.cc | 30 +++++++++++++--- src/parser.cc | 3 ++ test/mjsunit/mjsunit.status | 1 - 11 files changed, 162 insertions(+), 126 deletions(-) diff --git a/src/assembler-arm.cc b/src/assembler-arm.cc index 21cd22527f..4e3ae1afc0 100644 --- a/src/assembler-arm.cc +++ b/src/assembler-arm.cc @@ -320,8 +320,10 @@ Assembler::Assembler(void* buffer, int buffer_size) { no_const_pool_before_ = 0; last_const_pool_end_ = 0; last_bound_pos_ = 0; - last_position_ = RelocInfo::kNoPosition; - last_position_is_statement_ = false; + current_statement_position_ = RelocInfo::kNoPosition; + current_position_ = RelocInfo::kNoPosition; + written_statement_position_ = current_statement_position_; + written_position_ = current_position_; } @@ -1306,20 +1308,36 @@ void Assembler::RecordComment(const char* msg) { void Assembler::RecordPosition(int pos) { if (pos == RelocInfo::kNoPosition) return; ASSERT(pos >= 0); - if (pos == last_position_) return; - CheckBuffer(); - RecordRelocInfo(RelocInfo::POSITION, pos); - last_position_ = pos; - last_position_is_statement_ = false; + current_position_ = pos; + WriteRecordedPositions(); } void Assembler::RecordStatementPosition(int pos) { - if (pos == last_position_) return; - CheckBuffer(); - RecordRelocInfo(RelocInfo::STATEMENT_POSITION, pos); - last_position_ = pos; - last_position_is_statement_ = true; + if (pos == RelocInfo::kNoPosition) return; + ASSERT(pos >= 0); + current_statement_position_ = pos; + WriteRecordedPositions(); +} + + +void Assembler::WriteRecordedPositions() { + // Write the statement position if it is different from what was written last + // time. + if (current_statement_position_ != written_statement_position_) { + CheckBuffer(); + RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_); + written_statement_position_ = current_statement_position_; + } + + // Write the position if it is different from what was written last time and + // also diferent from the written statement position. + if (current_position_ != written_position_ && + current_position_ != written_statement_position_) { + CheckBuffer(); + RecordRelocInfo(RelocInfo::POSITION, current_position_); + written_position_ = current_position_; + } } diff --git a/src/assembler-arm.h b/src/assembler-arm.h index bd7006608a..faf402a7f9 100644 --- a/src/assembler-arm.h +++ b/src/assembler-arm.h @@ -643,15 +643,11 @@ class Assembler : public Malloced { void RecordPosition(int pos); void RecordStatementPosition(int pos); + void WriteRecordedPositions(); int pc_offset() const { return pc_ - buffer_; } - int last_position() const { return last_position_; } - bool last_position_is_statement() const { - return last_position_is_statement_; - } - - // Temporary helper function. Used by codegen.cc. - int last_statement_position() const { return last_position_; } + int current_position() const { return current_position_; } + int current_statement_position() const { return current_position_; } protected: int buffer_space() const { return reloc_info_writer.pos() - pc_; } @@ -754,8 +750,10 @@ class Assembler : public Malloced { int last_bound_pos_; // source position information - int last_position_; - bool last_position_is_statement_; + int current_position_; + int current_statement_position_; + int written_position_; + int written_statement_position_; // Code emission inline void CheckBuffer(); diff --git a/src/assembler-ia32.cc b/src/assembler-ia32.cc index 051624bb60..c04fd5758b 100644 --- a/src/assembler-ia32.cc +++ b/src/assembler-ia32.cc @@ -316,8 +316,10 @@ Assembler::Assembler(void* buffer, int buffer_size) { reloc_info_writer.Reposition(buffer_ + buffer_size, pc_); last_pc_ = NULL; - last_position_ = RelocInfo::kNoPosition; - last_statement_position_ = RelocInfo::kNoPosition; + current_statement_position_ = RelocInfo::kNoPosition; + current_position_ = RelocInfo::kNoPosition; + written_statement_position_ = current_statement_position_; + written_position_ = current_position_; } @@ -1964,31 +1966,36 @@ void Assembler::RecordComment(const char* msg) { void Assembler::RecordPosition(int pos) { - if (pos == RelocInfo::kNoPosition) return; + ASSERT(pos != RelocInfo::kNoPosition); ASSERT(pos >= 0); - last_position_ = pos; + current_position_ = pos; } void Assembler::RecordStatementPosition(int pos) { - if (pos == RelocInfo::kNoPosition) return; + ASSERT(pos != RelocInfo::kNoPosition); ASSERT(pos >= 0); - last_statement_position_ = pos; + current_statement_position_ = pos; } void Assembler::WriteRecordedPositions() { - if (last_statement_position_ != RelocInfo::kNoPosition) { + // Write the statement position if it is different from what was written last + // time. + if (current_statement_position_ != written_statement_position_) { EnsureSpace ensure_space(this); - RecordRelocInfo(RelocInfo::STATEMENT_POSITION, last_statement_position_); + RecordRelocInfo(RelocInfo::STATEMENT_POSITION, current_statement_position_); + written_statement_position_ = current_statement_position_; } - if ((last_position_ != RelocInfo::kNoPosition) && - (last_position_ != last_statement_position_)) { + + // Write the position if it is different from what was written last time and + // also diferent from the written statement position. + if (current_position_ != written_position_ && + current_position_ != written_statement_position_) { EnsureSpace ensure_space(this); - RecordRelocInfo(RelocInfo::POSITION, last_position_); + RecordRelocInfo(RelocInfo::POSITION, current_position_); + written_position_ = current_position_; } - last_statement_position_ = RelocInfo::kNoPosition; - last_position_ = RelocInfo::kNoPosition; } diff --git a/src/assembler-ia32.h b/src/assembler-ia32.h index 1e2f45dbf6..7db218ef41 100644 --- a/src/assembler-ia32.h +++ b/src/assembler-ia32.h @@ -712,8 +712,8 @@ class Assembler : public Malloced { void WriteInternalReference(int position, const Label& bound_label); int pc_offset() const { return pc_ - buffer_; } - int last_statement_position() const { return last_statement_position_; } - int last_position() const { return last_position_; } + int current_statement_position() const { return current_statement_position_; } + int current_position() const { return current_position_; } // Check if there is less than kGap bytes available in the buffer. // If this is the case, we need to grow the buffer before emitting @@ -800,8 +800,10 @@ class Assembler : public Malloced { byte* last_pc_; // source position information - int last_position_; - int last_statement_position_; + int current_statement_position_; + int current_position_; + int written_statement_position_; + int written_position_; }; diff --git a/src/codegen-arm.cc b/src/codegen-arm.cc index 712a98925c..fa2541a686 100644 --- a/src/codegen-arm.cc +++ b/src/codegen-arm.cc @@ -1043,7 +1043,7 @@ void CodeGenerator::CallWithArguments(ZoneList* args, } // Record the position for debugging purposes. - __ RecordPosition(position); + CodeForSourcePosition(position); // Use the shared code stub to call the function. CallFunctionStub call_function(args->length()); @@ -1074,7 +1074,7 @@ void CodeGenerator::CheckStack() { void CodeGenerator::VisitBlock(Block* node) { Comment cmnt(masm_, "[ Block"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); node->set_break_stack_height(break_stack_height_); VisitStatements(node->statements()); __ bind(node->break_target()); @@ -1094,6 +1094,7 @@ void CodeGenerator::DeclareGlobals(Handle pairs) { void CodeGenerator::VisitDeclaration(Declaration* node) { Comment cmnt(masm_, "[ Declaration"); + CodeForStatement(node); Variable* var = node->proxy()->var(); ASSERT(var != NULL); // must have been resolved Slot* slot = var->slot(); @@ -1159,7 +1160,7 @@ void CodeGenerator::VisitDeclaration(Declaration* node) { void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { Comment cmnt(masm_, "[ ExpressionStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); Expression* expression = node->expression(); expression->MarkAsStatement(); Load(expression); @@ -1169,6 +1170,7 @@ void CodeGenerator::VisitExpressionStatement(ExpressionStatement* node) { void CodeGenerator::VisitEmptyStatement(EmptyStatement* node) { Comment cmnt(masm_, "// EmptyStatement"); + CodeForStatement(node); // nothing to do } @@ -1180,7 +1182,7 @@ void CodeGenerator::VisitIfStatement(IfStatement* node) { bool has_then_stm = node->HasThenStatement(); bool has_else_stm = node->HasElseStatement(); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); Label exit; if (has_then_stm && has_else_stm) { @@ -1245,7 +1247,7 @@ void CodeGenerator::CleanStack(int num_bytes) { void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { Comment cmnt(masm_, "[ ContinueStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); CleanStack(break_stack_height_ - node->target()->break_stack_height()); __ b(node->target()->continue_target()); } @@ -1253,7 +1255,7 @@ void CodeGenerator::VisitContinueStatement(ContinueStatement* node) { void CodeGenerator::VisitBreakStatement(BreakStatement* node) { Comment cmnt(masm_, "[ BreakStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); CleanStack(break_stack_height_ - node->target()->break_stack_height()); __ b(node->target()->break_target()); } @@ -1261,7 +1263,7 @@ void CodeGenerator::VisitBreakStatement(BreakStatement* node) { void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { Comment cmnt(masm_, "[ ReturnStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); Load(node->expression()); // Move the function result into r0. frame_->Pop(r0); @@ -1272,7 +1274,7 @@ void CodeGenerator::VisitReturnStatement(ReturnStatement* node) { void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { Comment cmnt(masm_, "[ WithEnterStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); Load(node->expression()); __ CallRuntime(Runtime::kPushContext, 1); if (kDebug) { @@ -1289,6 +1291,7 @@ void CodeGenerator::VisitWithEnterStatement(WithEnterStatement* node) { void CodeGenerator::VisitWithExitStatement(WithExitStatement* node) { Comment cmnt(masm_, "[ WithExitStatement"); + CodeForStatement(node); // Pop context. __ ldr(cp, ContextOperand(cp, Context::PREVIOUS_INDEX)); // Update context local. @@ -1355,7 +1358,7 @@ void CodeGenerator::GenerateFastCaseSwitchJumpTable( void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { Comment cmnt(masm_, "[ SwitchStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); node->set_break_stack_height(break_stack_height_); Load(node->tag()); @@ -1423,7 +1426,7 @@ void CodeGenerator::VisitSwitchStatement(SwitchStatement* node) { void CodeGenerator::VisitLoopStatement(LoopStatement* node) { Comment cmnt(masm_, "[ LoopStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); node->set_break_stack_height(break_stack_height_); // simple condition analysis @@ -1463,7 +1466,7 @@ void CodeGenerator::VisitLoopStatement(LoopStatement* node) { // Record source position of the statement as this code which is after the // code for the body actually belongs to the loop statement and not the // body. - if (FLAG_debug_info) __ RecordPosition(node->statement_pos()); + CodeForStatement(node); ASSERT(node->type() == LoopStatement::FOR_LOOP); Visit(node->next()); } @@ -1495,7 +1498,7 @@ void CodeGenerator::VisitLoopStatement(LoopStatement* node) { void CodeGenerator::VisitForInStatement(ForInStatement* node) { Comment cmnt(masm_, "[ ForInStatement"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); // We keep stuff on the stack while the body is executing. // Record it, so that a break/continue crossing this statement @@ -1683,6 +1686,7 @@ void CodeGenerator::VisitForInStatement(ForInStatement* node) { void CodeGenerator::VisitTryCatch(TryCatch* node) { Comment cmnt(masm_, "[ TryCatch"); + CodeForStatement(node); Label try_block, exit; @@ -1779,6 +1783,7 @@ void CodeGenerator::VisitTryCatch(TryCatch* node) { void CodeGenerator::VisitTryFinally(TryFinally* node) { Comment cmnt(masm_, "[ TryFinally"); + CodeForStatement(node); // State: Used to keep track of reason for entering the finally // block. Should probably be extended to hold information for @@ -1920,7 +1925,7 @@ void CodeGenerator::VisitTryFinally(TryFinally* node) { void CodeGenerator::VisitDebuggerStatement(DebuggerStatement* node) { Comment cmnt(masm_, "[ DebuggerStatament"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); __ CallRuntime(Runtime::kDebugBreak, 0); // Ignore the return value. } @@ -2228,7 +2233,7 @@ void CodeGenerator::VisitArrayLiteral(ArrayLiteral* node) { void CodeGenerator::VisitAssignment(Assignment* node) { Comment cmnt(masm_, "[ Assignment"); - if (FLAG_debug_info) RecordStatementPosition(node); + CodeForStatement(node); Reference target(this, node->target()); if (target.is_illegal()) return; @@ -2259,7 +2264,7 @@ void CodeGenerator::VisitAssignment(Assignment* node) { // Assignment ignored - leave the value on the stack. } else { - __ RecordPosition(node->position()); + CodeForSourcePosition(node->position()); if (node->op() == Token::INIT_CONST) { // Dynamic constant initializations must use the function context // and initialize the actual constant declared. Dynamic variable @@ -2276,7 +2281,7 @@ void CodeGenerator::VisitThrow(Throw* node) { Comment cmnt(masm_, "[ Throw"); Load(node->exception()); - __ RecordPosition(node->position()); + CodeForSourcePosition(node->position()); __ CallRuntime(Runtime::kThrow, 1); frame_->Push(r0); } @@ -2295,7 +2300,7 @@ void CodeGenerator::VisitCall(Call* node) { ZoneList* args = node->arguments(); - RecordStatementPosition(node); + CodeForStatement(node); // Standard function call. // Check if the function is a variable or a property. @@ -2331,7 +2336,7 @@ void CodeGenerator::VisitCall(Call* node) { // Setup the receiver register and call the IC initialization code. Handle stub = ComputeCallInitialize(args->length()); - __ RecordPosition(node->position()); + CodeForSourcePosition(node->position()); __ Call(stub, RelocInfo::CODE_TARGET_CONTEXT); __ ldr(cp, frame_->Context()); // Remove the function from the stack. @@ -2378,7 +2383,7 @@ void CodeGenerator::VisitCall(Call* node) { // Set the receiver register and call the IC initialization code. Handle stub = ComputeCallInitialize(args->length()); - __ RecordPosition(node->position()); + CodeForSourcePosition(node->position()); __ Call(stub, RelocInfo::CODE_TARGET); __ ldr(cp, frame_->Context()); @@ -2432,7 +2437,7 @@ void CodeGenerator::VisitCallEval(CallEval* node) { ZoneList* args = node->arguments(); Expression* function = node->expression(); - RecordStatementPosition(node); + CodeForStatement(node); // Prepare stack for call to resolved function. Load(function); @@ -2462,7 +2467,7 @@ void CodeGenerator::VisitCallEval(CallEval* node) { __ str(r1, MemOperand(sp, args->length() * kPointerSize)); // Call the function. - __ RecordPosition(node->position()); + CodeForSourcePosition(node->position()); CallFunctionStub call_function(args->length()); __ CallStub(&call_function); @@ -2476,6 +2481,7 @@ void CodeGenerator::VisitCallEval(CallEval* node) { void CodeGenerator::VisitCallNew(CallNew* node) { Comment cmnt(masm_, "[ CallNew"); + CodeForStatement(node); // According to ECMA-262, section 11.2.2, page 44, the function // expression in new calls must be evaluated before the @@ -2501,7 +2507,7 @@ void CodeGenerator::VisitCallNew(CallNew* node) { // Call the construct call builtin that handles allocation and // constructor invocation. - __ RecordPosition(RelocInfo::POSITION); + CodeForSourcePosition(node->position()); __ Call(Handle(Builtins::builtin(Builtins::JSConstructCall)), RelocInfo::CONSTRUCT_CALL); @@ -3207,15 +3213,6 @@ void CodeGenerator::VisitCompareOperation(CompareOperation* node) { } -void CodeGenerator::RecordStatementPosition(Node* node) { - if (FLAG_debug_info) { - int statement_pos = node->statement_pos(); - if (statement_pos == RelocInfo::kNoPosition) return; - __ RecordStatementPosition(statement_pos); - } -} - - #undef __ #define __ masm-> @@ -3243,7 +3240,7 @@ void Reference::GetValue(TypeofState typeof_state) { VirtualFrame* frame = cgen_->frame(); Property* property = expression_->AsProperty(); if (property != NULL) { - __ RecordPosition(property->position()); + cgen_->CodeForSourcePosition(property->position()); } switch (type_) { @@ -3309,7 +3306,7 @@ void Reference::SetValue(InitState init_state) { VirtualFrame* frame = cgen_->frame(); Property* property = expression_->AsProperty(); if (property != NULL) { - __ RecordPosition(property->position()); + cgen_->CodeForSourcePosition(property->position()); } switch (type_) { @@ -3412,7 +3409,7 @@ void Reference::SetValue(InitState init_state) { Comment cmnt(masm, "[ Store to keyed Property"); Property* property = expression_->AsProperty(); ASSERT(property != NULL); - __ RecordPosition(property->position()); + cgen_->CodeForSourcePosition(property->position()); // Call IC code. Handle ic(Builtins::builtin(Builtins::KeyedStoreIC_Initialize)); diff --git a/src/codegen-arm.h b/src/codegen-arm.h index 904a3b71e1..42a05eca6f 100644 --- a/src/codegen-arm.h +++ b/src/codegen-arm.h @@ -376,10 +376,11 @@ class CodeGenerator: public AstVisitor { bool TryGenerateFastCaseSwitchStatement(SwitchStatement* node); - // Bottle-neck interface to call the Assembler to generate the statement - // position. This allows us to easily control whether statement positions - // should be generated or not. - void RecordStatementPosition(Node* node); + // Methods used to indicate which source code is generated for. Source + // positions are collected by the assembler and emitted with the relocation + // information. + void CodeForStatement(Node* node); + void CodeForSourcePosition(int pos); bool is_eval_; // Tells whether code is generated for eval. Handle