Revision: 3738
Author: [email protected]
Date: Fri Jan 29 01:42:13 2010
Log: Add a pass for the fast compiler to label expression nodes.

This change adds a post-order numbering to AST nodes that
are relevant for the fast code generator. It is only invoked
together with the fast compiler.

Also changed the ast printer to print the  numbering for
testing purposes if it is present.

Review URL: http://codereview.chromium.org/553134
http://code.google.com/p/v8/source/detail?r=3738

Added:
 /branches/bleeding_edge/src/data-flow.cc
 /branches/bleeding_edge/src/data-flow.h
Modified:
 /branches/bleeding_edge/src/SConscript
 /branches/bleeding_edge/src/ast.h
 /branches/bleeding_edge/src/compiler.cc
 /branches/bleeding_edge/src/prettyprinter.cc
 /branches/bleeding_edge/src/prettyprinter.h
 /branches/bleeding_edge/tools/gyp/v8.gyp
 /branches/bleeding_edge/tools/visual_studio/v8_base.vcproj
 /branches/bleeding_edge/tools/visual_studio/v8_base_arm.vcproj
 /branches/bleeding_edge/tools/visual_studio/v8_base_x64.vcproj

=======================================
--- /dev/null
+++ /branches/bleeding_edge/src/data-flow.cc    Fri Jan 29 01:42:13 2010
@@ -0,0 +1,264 @@
+// Copyright 2010 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "v8.h"
+
+#include "data-flow.h"
+
+namespace v8 {
+namespace internal {
+
+
+void AstLabeler::Label(FunctionLiteral* fun) {
+  VisitStatements(fun->body());
+}
+
+
+void AstLabeler::VisitStatements(ZoneList<Statement*>* stmts) {
+  for (int i = 0, len = stmts->length(); i < len; i++) {
+    Visit(stmts->at(i));
+  }
+}
+
+
+void AstLabeler::VisitDeclarations(ZoneList<Declaration*>* decls) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitBlock(Block* stmt) {
+  VisitStatements(stmt->statements());
+}
+
+
+void AstLabeler::VisitExpressionStatement(
+    ExpressionStatement* stmt) {
+  Visit(stmt->expression());
+}
+
+
+void AstLabeler::VisitEmptyStatement(EmptyStatement* stmt) {
+  // Do nothing.
+}
+
+
+void AstLabeler::VisitIfStatement(IfStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitContinueStatement(ContinueStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitBreakStatement(BreakStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitReturnStatement(ReturnStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitWithEnterStatement(
+    WithEnterStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitWithExitStatement(WithExitStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitSwitchStatement(SwitchStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitDoWhileStatement(DoWhileStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitWhileStatement(WhileStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitForStatement(ForStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitForInStatement(ForInStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitTryCatchStatement(TryCatchStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitTryFinallyStatement(
+    TryFinallyStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitDebuggerStatement(
+    DebuggerStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitFunctionLiteral(FunctionLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitFunctionBoilerplateLiteral(
+    FunctionBoilerplateLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitConditional(Conditional* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitSlot(Slot* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitVariableProxy(VariableProxy* expr) {
+  expr->set_num(next_number_++);
+}
+
+
+void AstLabeler::VisitLiteral(Literal* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitRegExpLiteral(RegExpLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitObjectLiteral(ObjectLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitArrayLiteral(ArrayLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitCatchExtensionObject(
+    CatchExtensionObject* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitAssignment(Assignment* expr) {
+  Property* prop = expr->target()->AsProperty();
+  ASSERT(prop != NULL);
+  if (prop != NULL) {
+    ASSERT(prop->key()->IsPropertyName());
+    if (prop->obj()->AsVariableProxy() == NULL ||
+        !prop->obj()->AsVariableProxy()->var()->is_this())
+      Visit(prop->obj());
+  }
+  Visit(expr->value());
+  expr->set_num(next_number_++);
+}
+
+
+void AstLabeler::VisitThrow(Throw* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitProperty(Property* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitCall(Call* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitCallNew(CallNew* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitCallRuntime(CallRuntime* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitUnaryOperation(UnaryOperation* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitCountOperation(CountOperation* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitBinaryOperation(BinaryOperation* expr) {
+  Visit(expr->left());
+  Visit(expr->right());
+  expr->set_num(next_number_++);
+}
+
+
+void AstLabeler::VisitCompareOperation(CompareOperation* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitThisFunction(ThisFunction* expr) {
+  UNREACHABLE();
+}
+
+
+void AstLabeler::VisitDeclaration(Declaration* decl) {
+  UNREACHABLE();
+}
+
+} }  // namespace v8::internal
=======================================
--- /dev/null
+++ /branches/bleeding_edge/src/data-flow.h     Fri Jan 29 01:42:13 2010
@@ -0,0 +1,63 @@
+// Copyright 2010 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+//       notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+//       copyright notice, this list of conditions and the following
+//       disclaimer in the documentation and/or other materials provided
+//       with the distribution.
+//     * Neither the name of Google Inc. nor the names of its
+//       contributors may be used to endorse or promote products derived
+//       from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef V8_DATAFLOW_H_
+#define V8_DATAFLOW_H_
+
+#include "ast.h"
+#include "scopes.h"
+
+namespace v8 {
+namespace internal {
+
+// This class is used to number all expressions in the AST according to
+// their evaluation order (post-order left-to-right traversal).
+class AstLabeler: public AstVisitor {
+ public:
+  AstLabeler() : next_number_(0) {}
+
+  void Label(FunctionLiteral* fun);
+
+ private:
+  void VisitDeclarations(ZoneList<Declaration*>* decls);
+  void VisitStatements(ZoneList<Statement*>* stmts);
+
+  // AST node visit functions.
+#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
+  AST_NODE_LIST(DECLARE_VISIT)
+#undef DECLARE_VISIT
+
+  // Traversal number for labelling AST nodes.
+  int next_number_;
+
+  DISALLOW_COPY_AND_ASSIGN(AstLabeler);
+};
+
+
+} }  // namespace v8::internal
+
+#endif  // V8_DATAFLOW_H_
=======================================
--- /branches/bleeding_edge/src/SConscript      Wed Jan 27 07:23:48 2010
+++ /branches/bleeding_edge/src/SConscript      Fri Jan 29 01:42:13 2010
@@ -50,6 +50,7 @@
     contexts.cc
     conversions.cc
     counters.cc
+    data-flow.cc
     dateparser.cc
     debug-agent.cc
     debug.cc
=======================================
--- /branches/bleeding_edge/src/ast.h   Wed Jan 27 07:23:48 2010
+++ /branches/bleeding_edge/src/ast.h   Fri Jan 29 01:42:13 2010
@@ -179,6 +179,10 @@
     // needed if false.
     kTestValue
   };
+
+  static const int kNoLabel = -1;
+
+  Expression() : num_(kNoLabel) {}

   virtual Expression* AsExpression()  { return this; }

@@ -197,9 +201,15 @@

   // Static type information for this expression.
   StaticType* type() { return &type_; }
+
+  int num() { return num_; }
+
+  // AST node numbering ordered by evaluation order.
+  void set_num(int n) { num_ = n; }

  private:
   StaticType type_;
+  int num_;
 };


=======================================
--- /branches/bleeding_edge/src/compiler.cc     Wed Jan 27 07:23:48 2010
+++ /branches/bleeding_edge/src/compiler.cc     Fri Jan 29 01:42:13 2010
@@ -31,6 +31,7 @@
 #include "codegen-inl.h"
 #include "compilation-cache.h"
 #include "compiler.h"
+#include "data-flow.h"
 #include "debug.h"
 #include "fast-codegen.h"
 #include "full-codegen.h"
@@ -110,6 +111,10 @@
              (FLAG_fast_compiler && !is_run_once)) {
     FastCodeGenSyntaxChecker checker;
     checker.Check(literal);
+    if (checker.has_supported_syntax()) {
+      AstLabeler labeler;
+      labeler.Label(literal);
+    }
     // Does not yet generate code.
   }

@@ -498,6 +503,10 @@
                (FLAG_fast_compiler && !is_run_once)) {
       FastCodeGenSyntaxChecker checker;
       checker.Check(literal);
+      if (checker.has_supported_syntax()) {
+        AstLabeler label_nodes;
+        label_nodes.Label(literal);
+      }
       // Generate no code.
     }

=======================================
--- /branches/bleeding_edge/src/prettyprinter.cc        Wed Dec 16 22:21:50 2009
+++ /branches/bleeding_edge/src/prettyprinter.cc        Fri Jan 29 01:42:13 2010
@@ -594,12 +594,22 @@
     ast_printer_->inc_indent();
   }

-  explicit IndentedScope(const char* txt, StaticType* type = NULL) {
+  explicit IndentedScope(const char* txt, AstNode* node = NULL) {
     ast_printer_->PrintIndented(txt);
-    if ((type != NULL) && (type->IsKnown())) {
-      ast_printer_->Print(" (type = ");
-      ast_printer_->Print(StaticType::Type2String(type));
-      ast_printer_->Print(")");
+    if (node != NULL && node->AsExpression() != NULL) {
+      Expression* expr = node->AsExpression();
+      bool printed_first = false;
+      if ((expr->type() != NULL) && (expr->type()->IsKnown())) {
+        ast_printer_->Print(" (type = ");
+        ast_printer_->Print(StaticType::Type2String(expr->type()));
+        printed_first = true;
+      }
+      if (expr->num() != Expression::kNoLabel) {
+        ast_printer_->Print(printed_first ? ", num = " : " (num = ");
+        ast_printer_->Print("%d", expr->num());
+        printed_first = true;
+      }
+      if (printed_first) ast_printer_->Print(")");
     }
     ast_printer_->Print("\n");
     ast_printer_->inc_indent();
@@ -657,19 +667,22 @@
 void AstPrinter::PrintLiteralWithModeIndented(const char* info,
                                               Variable* var,
                                               Handle<Object> value,
-                                              StaticType* type) {
+                                              StaticType* type,
+                                              int num) {
   if (var == NULL) {
     PrintLiteralIndented(info, value, true);
   } else {
     EmbeddedVector<char, 256> buf;
+    int pos = OS::SNPrintF(buf, "%s (mode = %s", info,
+                           Variable::Mode2String(var->mode()));
     if (type->IsKnown()) {
-      OS::SNPrintF(buf, "%s (mode = %s, type = %s)", info,
-                   Variable::Mode2String(var->mode()),
-                   StaticType::Type2String(type));
-    } else {
-      OS::SNPrintF(buf, "%s (mode = %s)", info,
-                   Variable::Mode2String(var->mode()));
-    }
+      pos += OS::SNPrintF(buf + pos, ", type = %s",
+                          StaticType::Type2String(type));
+    }
+    if (num != Expression::kNoLabel) {
+      pos += OS::SNPrintF(buf + pos, ", num = %d", num);
+    }
+    OS::SNPrintF(buf + pos, ")");
     PrintLiteralIndented(buf.start(), value, true);
   }
 }
@@ -692,7 +705,7 @@


 void AstPrinter::PrintIndentedVisit(const char* s, AstNode* node) {
-  IndentedScope indent(s);
+  IndentedScope indent(s, node);
   Visit(node);
 }

@@ -726,7 +739,8 @@
     for (int i = 0; i < scope->num_parameters(); i++) {
       PrintLiteralWithModeIndented("VAR", scope->parameter(i),
                                    scope->parameter(i)->name(),
-                                   scope->parameter(i)->type());
+                                   scope->parameter(i)->type(),
+                                   Expression::kNoLabel);
     }
   }
 }
@@ -771,7 +785,8 @@
     PrintLiteralWithModeIndented(Variable::Mode2String(node->mode()),
                                  node->proxy()->AsVariable(),
                                  node->proxy()->name(),
-                                 node->proxy()->AsVariable()->type());
+                                 node->proxy()->AsVariable()->type(),
+                                 Expression::kNoLabel);
   } else {
     // function declarations
     PrintIndented("FUNCTION ");
@@ -1007,7 +1022,7 @@

 void AstPrinter::VisitVariableProxy(VariableProxy* node) {
PrintLiteralWithModeIndented("VAR PROXY", node->AsVariable(), node->name(),
-                               node->type());
+                               node->type(), node->num());
   Variable* var = node->var();
   if (var != NULL && var->rewrite() != NULL) {
     IndentedScope indent;
@@ -1017,7 +1032,7 @@


 void AstPrinter::VisitAssignment(Assignment* node) {
-  IndentedScope indent(Token::Name(node->op()), node->type());
+  IndentedScope indent(Token::Name(node->op()), node);
   Visit(node->target());
   Visit(node->value());
 }
@@ -1029,7 +1044,7 @@


 void AstPrinter::VisitProperty(Property* node) {
-  IndentedScope indent("PROPERTY");
+  IndentedScope indent("PROPERTY", node);
   Visit(node->obj());
   Literal* literal = node->key()->AsLiteral();
   if (literal != NULL && literal->handle()->IsSymbol()) {
@@ -1082,14 +1097,14 @@


 void AstPrinter::VisitBinaryOperation(BinaryOperation* node) {
-  IndentedScope indent(Token::Name(node->op()), node->type());
+  IndentedScope indent(Token::Name(node->op()), node);
   Visit(node->left());
   Visit(node->right());
 }


 void AstPrinter::VisitCompareOperation(CompareOperation* node) {
-  IndentedScope indent(Token::Name(node->op()), node->type());
+  IndentedScope indent(Token::Name(node->op()), node);
   Visit(node->left());
   Visit(node->right());
 }
=======================================
--- /branches/bleeding_edge/src/prettyprinter.h Wed Dec 16 22:21:50 2009
+++ /branches/bleeding_edge/src/prettyprinter.h Fri Jan 29 01:42:13 2010
@@ -102,7 +102,8 @@
   void PrintLiteralWithModeIndented(const char* info,
                                     Variable* var,
                                     Handle<Object> value,
-                                    StaticType* type);
+                                    StaticType* type,
+                                    int num);
   void PrintLabelsIndented(const char* info, ZoneStringList* labels);

   void inc_indent() { indent_++; }
=======================================
--- /branches/bleeding_edge/tools/gyp/v8.gyp    Wed Jan 27 07:23:48 2010
+++ /branches/bleeding_edge/tools/gyp/v8.gyp    Fri Jan 29 01:42:13 2010
@@ -252,6 +252,8 @@
         '../../src/counters.cc',
         '../../src/counters.h',
         '../../src/cpu.h',
+       '../../src/data-flow.cc',
+       '../../src/data-flow.h',
         '../../src/dateparser.cc',
         '../../src/dateparser.h',
         '../../src/dateparser-inl.h',
=======================================
--- /branches/bleeding_edge/tools/visual_studio/v8_base.vcproj Thu Jan 28 00:41:31 2010 +++ /branches/bleeding_edge/tools/visual_studio/v8_base.vcproj Fri Jan 29 01:42:13 2010
@@ -335,6 +335,14 @@
                        <File
                                RelativePath="..\..\src\cpu.h"
                                >
+                       </File>
+                       <File
+                               RelativePath="..\..\src\data-flow.cc"
+                               >
+                       </File>
+                       <File
+                               RelativePath="..\..\src\data-flow.h"
+                               >
                        </File>
                        <File
                                RelativePath="..\..\src\dateparser.cc"
=======================================
--- /branches/bleeding_edge/tools/visual_studio/v8_base_arm.vcproj Thu Jan 28 00:41:31 2010 +++ /branches/bleeding_edge/tools/visual_studio/v8_base_arm.vcproj Fri Jan 29 01:42:13 2010
@@ -343,6 +343,14 @@
                        <File
                                RelativePath="..\..\src\cpu.h"
                                >
+                       </File>
+                       <File
+                               RelativePath="..\..\src\data-flow.cc"
+                               >
+                       </File>
+                       <File
+                               RelativePath="..\..\src\data-flow.h"
+                               >
                        </File>
                        <File
                                RelativePath="..\..\src\dateparser.cc"
=======================================
--- /branches/bleeding_edge/tools/visual_studio/v8_base_x64.vcproj Thu Jan 28 00:41:31 2010 +++ /branches/bleeding_edge/tools/visual_studio/v8_base_x64.vcproj Fri Jan 29 01:42:13 2010
@@ -335,6 +335,14 @@
                        <File
                                RelativePath="..\..\src\cpu.h"
                                >
+                       </File>
+                       <File
+                               RelativePath="..\..\src\data-flow.cc"
+                               >
+                       </File>
+                       <File
+                               RelativePath="..\..\src\data-flow.h"
+                               >
                        </File>
                        <File
                                RelativePath="..\..\src\dateparser.cc"

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to