Revision: 3163
Author: [email protected]
Date: Wed Oct 28 06:25:40 2009
Log: In the toplevel compiler, shift the responsibility of assigning a
location to an Expression AST node from the node's parent to the node
itself.

This allows an inherited code generation context from a parent node to
be passed arbitrarily far down the tree (eg, the subexpression of a
unary not is in the same context as the unary expression itself, the
then and else subexpressions of the ternary operator are in the same
context as the whole expression, and so forth).

We do not yet take advantage of this in the backend (eg, the right
subexpression of short-circuited OR is still compiled by using the
parent's destination location, rather than the subexpression's
itself).

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

Modified:
  /branches/bleeding_edge/src/arm/fast-codegen-arm.cc
  /branches/bleeding_edge/src/ast.cc
  /branches/bleeding_edge/src/ast.h
  /branches/bleeding_edge/src/compiler.cc
  /branches/bleeding_edge/src/ia32/fast-codegen-ia32.cc
  /branches/bleeding_edge/src/x64/fast-codegen-x64.cc

=======================================
--- /branches/bleeding_edge/src/arm/fast-codegen-arm.cc Wed Oct 28 03:14:32  
2009
+++ /branches/bleeding_edge/src/arm/fast-codegen-arm.cc Wed Oct 28 06:25:40  
2009
@@ -275,13 +275,10 @@

    for (int i = 0; i < expr->properties()->length(); i++) {
      ObjectLiteral::Property* property = expr->properties()->at(i);
+    if (property->IsCompileTimeValue()) continue;
+
      Literal* key = property->key();
      Expression* value = property->value();
-    if (property->kind() == ObjectLiteral::Property::CONSTANT) continue;
-    if (property->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL  
&&
-        CompileTimeValue::IsCompileTimeValue(value)) {
-      continue;
-    }
      if (!result_saved) {
        __ push(r0);  // Save result on stack
        result_saved = true;
=======================================
--- /branches/bleeding_edge/src/ast.cc  Mon Oct 12 08:06:28 2009
+++ /branches/bleeding_edge/src/ast.cc  Wed Oct 28 06:25:40 2009
@@ -28,6 +28,7 @@
  #include "v8.h"

  #include "ast.h"
+#include "parser.h"
  #include "scopes.h"
  #include "string-stream.h"

@@ -136,6 +137,13 @@
    value_ = value;
    kind_ = is_getter ? GETTER : SETTER;
  }
+
+
+bool ObjectLiteral::Property::IsCompileTimeValue() {
+  return kind_ == CONSTANT ||
+      (kind_ == MATERIALIZED_LITERAL &&
+       CompileTimeValue::IsCompileTimeValue(value_));
+}


  bool ObjectLiteral::IsValidJSON() {
=======================================
--- /branches/bleeding_edge/src/ast.h   Thu Oct 15 09:42:22 2009
+++ /branches/bleeding_edge/src/ast.h   Wed Oct 28 06:25:40 2009
@@ -746,6 +746,8 @@
      Literal* key() { return key_; }
      Expression* value() { return value_; }
      Kind kind() { return kind_; }
+
+    bool IsCompileTimeValue();

     private:
      Literal* key_;
=======================================
--- /branches/bleeding_edge/src/compiler.cc     Tue Oct 27 09:11:12 2009
+++ /branches/bleeding_edge/src/compiler.cc     Wed Oct 28 06:25:40 2009
@@ -46,7 +46,10 @@
   public:
    enum CodeGenTag { NORMAL, FAST };

-  CodeGenSelector() : has_supported_syntax_(true) {}
+  CodeGenSelector()
+      : has_supported_syntax_(true),
+        location_(Location::Nowhere()) {
+  }

    CodeGenTag Select(FunctionLiteral* fun);

@@ -54,6 +57,14 @@
    void VisitDeclarations(ZoneList<Declaration*>* decls);
    void VisitStatements(ZoneList<Statement*>* stmts);

+  // Visit an expression in effect context with a desired location of
+  // nowhere.
+  void VisitAsEffect(Expression* expr);
+
+  // Visit an expression in value context with a desired location of
+  // temporary.
+  void VisitAsValue(Expression* expr);
+
    // AST node visit functions.
  #define DECLARE_VISIT(type) virtual void Visit##type(type* node);
    AST_NODE_LIST(DECLARE_VISIT)
@@ -61,6 +72,9 @@

    bool has_supported_syntax_;

+  // The desired location of the currently visited expression.
+  Location location_;
+
    DISALLOW_COPY_AND_ASSIGN(CodeGenSelector);
  };

@@ -497,6 +511,30 @@
      CHECK_BAILOUT;
    }
  }
+
+
+void CodeGenSelector::VisitAsEffect(Expression* expr) {
+  if (location_.is_nowhere()) {
+    Visit(expr);
+  } else {
+    Location saved = location_;
+    location_ = Location::Nowhere();
+    Visit(expr);
+    location_ = saved;
+  }
+}
+
+
+void CodeGenSelector::VisitAsValue(Expression* expr) {
+  if (location_.is_temporary()) {
+    Visit(expr);
+  } else {
+    Location saved = location_;
+    location_ = Location::Temporary();
+    Visit(expr);
+    location_ = saved;
+  }
+}


  void CodeGenSelector::VisitDeclaration(Declaration* decl) {
@@ -513,10 +551,7 @@


  void CodeGenSelector::VisitExpressionStatement(ExpressionStatement* stmt) {
-  Expression* expr = stmt->expression();
-  Visit(expr);
-  CHECK_BAILOUT;
-  expr->set_location(Location::Nowhere());
+  VisitAsEffect(stmt->expression());
  }


@@ -541,7 +576,7 @@


  void CodeGenSelector::VisitReturnStatement(ReturnStatement* stmt) {
-  Visit(stmt->expression());
+  VisitAsValue(stmt->expression());
  }


@@ -599,6 +634,7 @@
    if (!expr->AllowsLazyCompilation()) {
      BAILOUT("FunctionLiteral does not allow lazy compilation");
    }
+  expr->set_location(location_);
  }


@@ -614,37 +650,75 @@


  void CodeGenSelector::VisitSlot(Slot* expr) {
-  Slot::Type type = expr->type();
-  if (type != Slot::PARAMETER && type != Slot::LOCAL) {
-    BAILOUT("non-parameter/non-local slot reference");
-  }
+  UNREACHABLE();
  }


  void CodeGenSelector::VisitVariableProxy(VariableProxy* expr) {
    Expression* rewrite = expr->var()->rewrite();
-  if (rewrite != NULL) Visit(rewrite);
+  // A rewrite of NULL indicates a global variable.
+  if (rewrite != NULL) {
+    // Non-global.
+    Slot* slot = rewrite->AsSlot();
+    if (slot == NULL) {
+      // This is a variable rewritten to an explicit property access
+      // on the arguments object.
+      BAILOUT("non-global/non-slot variable reference");
+    }
+
+    Slot::Type type = slot->type();
+    if (type != Slot::PARAMETER && type != Slot::LOCAL) {
+      BAILOUT("non-parameter/non-local slot reference");
+    }
+  }
+  expr->set_location(location_);
  }


  void CodeGenSelector::VisitLiteral(Literal* expr) {
-  // Literals are supported.
+  expr->set_location(location_);
  }


  void CodeGenSelector::VisitRegExpLiteral(RegExpLiteral* expr) {
-  // RegexpLiterals are supported.
+  expr->set_location(location_);
  }


  void CodeGenSelector::VisitObjectLiteral(ObjectLiteral* expr) {
-  for (int i = 0; i < expr->properties()->length(); i++) {
-    ObjectLiteral::Property* property = expr->properties()->at(i);
-    Visit(property->key());
+  ZoneList<ObjectLiteral::Property*>* properties = expr->properties();
+
+  for (int i = 0, len = properties->length(); i < len; i++) {
+    ObjectLiteral::Property* property = properties->at(i);
+    if (property->IsCompileTimeValue()) continue;
+
+    switch (property->kind()) {
+      case ObjectLiteral::Property::CONSTANT:
+        UNREACHABLE();
+
+      // For (non-compile-time) materialized literals and computed
+      // properties with symbolic keys we will use an IC and therefore not
+      // generate code for the key.
+      case ObjectLiteral::Property::COMPUTED:  // Fall through.
+      case ObjectLiteral::Property::MATERIALIZED_LITERAL:
+        if (property->key()->handle()->IsSymbol()) {
+          break;
+        }
+        // Fall through.
+
+      // In all other cases we need the key's value on the stack
+      // for a runtime call.  (Relies on TEMP meaning STACK.)
+      case ObjectLiteral::Property::GETTER:  // Fall through.
+      case ObjectLiteral::Property::SETTER:  // Fall through.
+      case ObjectLiteral::Property::PROTOTYPE:
+        VisitAsValue(property->key());
+        CHECK_BAILOUT;
+        break;
+    }
+    VisitAsValue(property->value());
      CHECK_BAILOUT;
-    Visit(property->value());
-    CHECK_BAILOUT;
-  }
+  }
+  expr->set_location(location_);
  }


@@ -654,9 +728,10 @@
      Expression* subexpr = subexprs->at(i);
      if (subexpr->AsLiteral() != NULL) continue;
      if (CompileTimeValue::IsCompileTimeValue(subexpr)) continue;
-    Visit(subexpr);
+    VisitAsValue(subexpr);
      CHECK_BAILOUT;
    }
+  expr->set_location(location_);
  }


@@ -668,7 +743,10 @@
  void CodeGenSelector::VisitAssignment(Assignment* expr) {
    // We support plain non-compound assignments to parameters and
    // non-context (stack-allocated) locals.
-  if (expr->starts_initialization_block()) BAILOUT("initialization block");
+  if (expr->starts_initialization_block() ||
+      expr->ends_initialization_block()) {
+    BAILOUT("initialization block start");
+  }

    Token::Value op = expr->op();
    if (op == Token::INIT_CONST) BAILOUT("initialize constant");
@@ -687,7 +765,8 @@
      }
    }

-  Visit(expr->value());
+  VisitAsValue(expr->value());
+  expr->set_location(location_);
  }


@@ -697,9 +776,10 @@


  void CodeGenSelector::VisitProperty(Property* expr) {
-  Visit(expr->obj());
+  VisitAsValue(expr->obj());
    CHECK_BAILOUT;
-  Visit(expr->key());
+  VisitAsValue(expr->key());
+  expr->set_location(location_);
  }


@@ -718,38 +798,42 @@
    } else {
      BAILOUT("Call to a non-global function");
    }
-  // Check all arguments to the call
+  // Check all arguments to the call.  (Relies on TEMP meaning STACK.)
    for (int i = 0; i < args->length(); i++) {
-    Visit(args->at(i));
+    VisitAsValue(args->at(i));
      CHECK_BAILOUT;
    }
+  expr->set_location(location_);
  }


  void CodeGenSelector::VisitCallNew(CallNew* expr) {
-  Visit(expr->expression());
+  VisitAsValue(expr->expression());
    CHECK_BAILOUT;
    ZoneList<Expression*>* args = expr->arguments();
    // Check all arguments to the call
    for (int i = 0; i < args->length(); i++) {
-    Visit(args->at(i));
+    VisitAsValue(args->at(i));
      CHECK_BAILOUT;
    }
+  expr->set_location(location_);
  }


  void CodeGenSelector::VisitCallRuntime(CallRuntime* expr) {
    // In case of JS runtime function bail out.
-  if (expr->function() == NULL) BAILOUT("CallRuntime");
+  if (expr->function() == NULL) BAILOUT("call JS runtime function");
    // Check for inline runtime call
    if (expr->name()->Get(0) == '_' &&
        CodeGenerator::FindInlineRuntimeLUT(expr->name()) != NULL) {
-    BAILOUT("InlineRuntimeCall");
-  }
+    BAILOUT("inlined runtime call");
+  }
+  // Check all arguments to the call.  (Relies on TEMP meaning STACK.)
    for (int i = 0; i < expr->arguments()->length(); i++) {
-    Visit(expr->arguments()->at(i));
+    VisitAsValue(expr->arguments()->at(i));
      CHECK_BAILOUT;
    }
+  expr->set_location(location_);
  }


@@ -766,14 +850,17 @@
  void CodeGenSelector::VisitBinaryOperation(BinaryOperation* expr) {
    switch (expr->op()) {
      case Token::OR:
-      Visit(expr->left());
+      VisitAsValue(expr->left());
        CHECK_BAILOUT;
+      // The location for the right subexpression is the same as for the
+      // whole expression so we call Visit directly.
        Visit(expr->right());
        break;

      default:
        BAILOUT("Unsupported binary operation");
    }
+  expr->set_location(location_);
  }


=======================================
--- /branches/bleeding_edge/src/ia32/fast-codegen-ia32.cc       Wed Oct 28  
03:14:32 2009
+++ /branches/bleeding_edge/src/ia32/fast-codegen-ia32.cc       Wed Oct 28  
06:25:40 2009
@@ -270,13 +270,10 @@

    for (int i = 0; i < expr->properties()->length(); i++) {
      ObjectLiteral::Property* property = expr->properties()->at(i);
+    if (property->IsCompileTimeValue()) continue;
+
      Literal* key = property->key();
      Expression* value = property->value();
-    if (property->kind() == ObjectLiteral::Property::CONSTANT) continue;
-    if (property->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL  
&&
-        CompileTimeValue::IsCompileTimeValue(value)) {
-      continue;
-    }
      if (!result_saved) {
        __ push(eax);  // Save result on the stack
        result_saved = true;
=======================================
--- /branches/bleeding_edge/src/x64/fast-codegen-x64.cc Wed Oct 28 03:14:32  
2009
+++ /branches/bleeding_edge/src/x64/fast-codegen-x64.cc Wed Oct 28 06:25:40  
2009
@@ -281,13 +281,10 @@

    for (int i = 0; i < expr->properties()->length(); i++) {
      ObjectLiteral::Property* property = expr->properties()->at(i);
+    if (property->IsCompileTimeValue()) continue;
+
      Literal* key = property->key();
      Expression* value = property->value();
-    if (property->kind() == ObjectLiteral::Property::CONSTANT) continue;
-    if (property->kind() == ObjectLiteral::Property::MATERIALIZED_LITERAL  
&&
-        CompileTimeValue::IsCompileTimeValue(value)) {
-      continue;
-    }
      if (!result_saved) {
        __ push(rax);  // Save result on the stack
        result_saved = true;

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

Reply via email to