Add fast code generator visitor.
authorkmillikin@chromium.org <kmillikin@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Fri, 29 Jan 2010 15:29:33 +0000 (15:29 +0000)
committerkmillikin@chromium.org <kmillikin@chromium.org@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>
Fri, 29 Jan 2010 15:29:33 +0000 (15:29 +0000)
It does not yet emit code so there is a flag --print-ir to print the
AST as seen by the code generator.

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@3748 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

src/compiler.cc
src/compiler.h
src/fast-codegen.cc
src/fast-codegen.h
src/flag-definitions.h

index 604af2c..98a26f8 100644 (file)
@@ -31,7 +31,6 @@
 #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"
@@ -113,10 +112,9 @@ static Handle<Code> MakeCode(FunctionLiteral* literal,
     FastCodeGenSyntaxChecker checker;
     checker.Check(literal, info);
     if (checker.has_supported_syntax()) {
-      AstLabeler labeler;
-      labeler.Label(literal);
+      // Does not yet generate code.
+      FastCodeGenerator::MakeCode(literal, script, is_eval, info);
     }
-    // Does not yet generate code.
   }
 
   return CodeGenerator::MakeCode(literal, script, is_eval, info);
@@ -513,10 +511,9 @@ Handle<JSFunction> Compiler::BuildBoilerplate(FunctionLiteral* literal,
       FastCodeGenSyntaxChecker checker;
       checker.Check(literal, &info);
       if (checker.has_supported_syntax()) {
-        AstLabeler label_nodes;
-        label_nodes.Label(literal);
+        // Does not yet generate code.
+        FastCodeGenerator::MakeCode(literal, script, false, &info);
       }
-      // Generate no code.
     }
 
     if (!is_compiled) {
index 0661f34..d12423d 100644 (file)
@@ -42,7 +42,7 @@ class CompilationInfo BASE_EMBEDDED {
                   Handle<Object> receiver,
                   int loop_nesting)
       : shared_info_(shared_info),
-        receiver_(receiver_),
+        receiver_(receiver),
         loop_nesting_(loop_nesting) {
   }
 
index 5a8c09e..db0205c 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "v8.h"
 
+#include "data-flow.h"
 #include "fast-codegen.h"
 #include "scopes.h"
 
@@ -51,6 +52,8 @@ namespace internal {
 
 void FastCodeGenSyntaxChecker::Check(FunctionLiteral* fun,
                                      CompilationInfo* info) {
+  info_ = info;
+
   // We do not specialize if we do not have a receiver.
   if (!info->has_receiver()) BAILOUT("No receiver");
 
@@ -322,4 +325,239 @@ void FastCodeGenSyntaxChecker::VisitThisFunction(ThisFunction* expr) {
 #undef CHECK_BAILOUT
 
 
+void FastCodeGenerator::MakeCode(FunctionLiteral* fun,
+                                 Handle<Script> script,
+                                 bool is_eval,
+                                 CompilationInfo* info) {
+  AstLabeler labeler;
+  FastCodeGenerator cgen(script, is_eval);
+  labeler.Label(fun);
+  cgen.Generate(fun, info);
+}
+
+
+void FastCodeGenerator::Generate(FunctionLiteral* fun, CompilationInfo* info) {
+  ASSERT(function_ == NULL);
+  ASSERT(info_ == NULL);
+  function_ = fun;
+  info_ = info;
+  VisitStatements(fun->body());
+  function_ = NULL;
+  info_ = NULL;
+}
+
+
+void FastCodeGenerator::VisitDeclaration(Declaration* decl) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitBlock(Block* stmt) {
+  VisitStatements(stmt->statements());
+}
+
+
+void FastCodeGenerator::VisitExpressionStatement(ExpressionStatement* stmt) {
+  Visit(stmt->expression());
+}
+
+
+void FastCodeGenerator::VisitEmptyStatement(EmptyStatement* stmt) {
+  // Nothing to do.
+}
+
+
+void FastCodeGenerator::VisitIfStatement(IfStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitContinueStatement(ContinueStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitBreakStatement(BreakStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitReturnStatement(ReturnStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitWithEnterStatement(WithEnterStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitWithExitStatement(WithExitStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitSwitchStatement(SwitchStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitDoWhileStatement(DoWhileStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitWhileStatement(WhileStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitForStatement(ForStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitForInStatement(ForInStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitTryCatchStatement(TryCatchStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitDebuggerStatement(DebuggerStatement* stmt) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitFunctionLiteral(FunctionLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitFunctionBoilerplateLiteral(
+    FunctionBoilerplateLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitConditional(Conditional* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitSlot(Slot* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitVariableProxy(VariableProxy* expr) {
+  if (FLAG_print_ir) {
+    ASSERT(expr->var()->is_global() && !expr->var()->is_this());
+    SmartPointer<char> name = expr->name()->ToCString();
+    PrintF("%d: t%d = Global(%s)\n", expr->num(), expr->num(), *name);
+  }
+}
+
+
+void FastCodeGenerator::VisitLiteral(Literal* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitRegExpLiteral(RegExpLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitArrayLiteral(ArrayLiteral* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitCatchExtensionObject(CatchExtensionObject* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitAssignment(Assignment* expr) {
+  // Known to be a simple this property assignment.
+  Visit(expr->value());
+
+  if (FLAG_print_ir) {
+    Property* prop = expr->target()->AsProperty();
+    ASSERT_NOT_NULL(prop);
+    ASSERT_NOT_NULL(prop->obj()->AsVariableProxy());
+    ASSERT(prop->obj()->AsVariableProxy()->var()->is_this());
+    ASSERT(prop->key()->IsPropertyName());
+    Handle<String> key =
+        Handle<String>::cast(prop->key()->AsLiteral()->handle());
+    SmartPointer<char> name = key->ToCString();
+    PrintF("%d: t%d = Store(this, \"%s\", t%d)\n",
+           expr->num(), expr->num(), *name, expr->value()->num());
+  }
+}
+
+
+void FastCodeGenerator::VisitThrow(Throw* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitProperty(Property* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitCall(Call* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitCallNew(CallNew* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitCallRuntime(CallRuntime* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitUnaryOperation(UnaryOperation* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitCountOperation(CountOperation* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitBinaryOperation(BinaryOperation* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitCompareOperation(CompareOperation* expr) {
+  UNREACHABLE();
+}
+
+
+void FastCodeGenerator::VisitThisFunction(ThisFunction* expr) {
+  UNREACHABLE();
+}
+
 } }  // namespace v8::internal
index 4f2628d..f28a0ea 100644 (file)
@@ -63,6 +63,40 @@ class FastCodeGenSyntaxChecker: public AstVisitor {
 };
 
 
+class FastCodeGenerator: public AstVisitor {
+ public:
+  FastCodeGenerator(Handle<Script> script, bool is_eval)
+      : masm_(NULL),
+        script_(script),
+        is_eval_(is_eval),
+        function_(NULL),
+        info_(NULL) {
+  }
+
+  static void MakeCode(FunctionLiteral* fun,
+                       Handle<Script> script,
+                       bool is_eval,
+                       CompilationInfo* info);
+
+  void Generate(FunctionLiteral* fun, CompilationInfo* info);
+
+ private:
+  // AST node visit functions.
+#define DECLARE_VISIT(type) virtual void Visit##type(type* node);
+  AST_NODE_LIST(DECLARE_VISIT)
+#undef DECLARE_VISIT
+
+  MacroAssembler* masm_;
+  Handle<Script> script_;
+  bool is_eval_;
+
+  FunctionLiteral* function_;
+  CompilationInfo* info_;
+
+  DISALLOW_COPY_AND_ASSIGN(FastCodeGenerator);
+};
+
+
 } }  // namespace v8::internal
 
 #endif  // V8_FAST_CODEGEN_H_
index 0208813..b57f2cb 100644 (file)
@@ -301,6 +301,7 @@ DEFINE_string(stop_at, "", "function name where to insert a breakpoint")
 // compiler.cc
 DEFINE_bool(print_builtin_scopes, false, "print scopes for builtins")
 DEFINE_bool(print_scopes, false, "print scopes")
+DEFINE_bool(print_ir, false, "print the AST as seen by the backend")
 
 // contexts.cc
 DEFINE_bool(trace_contexts, false, "trace contexts operations")