jitcraft/BytecodeCompiler.cpp
changeset 1 27f12e2fc369
parent 0 c455ebb9a5dc
child 2 46fc53b8fd2f
     1.1 --- a/jitcraft/BytecodeCompiler.cpp	Mon Jun 25 02:57:28 2012 -0700
     1.2 +++ b/jitcraft/BytecodeCompiler.cpp	Mon Jun 25 21:20:01 2012 -0700
     1.3 @@ -27,7 +27,8 @@
     1.4  #define __ emitter_.
     1.5  
     1.6  BytecodeCompiler::BytecodeCompiler(ParseTree *tree)
     1.7 -  : tree_(tree)
     1.8 +  : tree_(tree),
     1.9 +    scope_(NULL)
    1.10  {
    1.11  }
    1.12  
    1.13 @@ -81,6 +82,16 @@
    1.14  }
    1.15  
    1.16  void
    1.17 +BytecodeCompiler::leaveScope(Scope *scope)
    1.18 +{
    1.19 +    if (!scope)
    1.20 +        return;
    1.21 +
    1.22 +    if (Variable *var = scope->firstUnsafeArray())
    1.23 +        __ unwind(var->slot());
    1.24 +}
    1.25 +
    1.26 +void
    1.27  BytecodeCompiler::visit(NameProxy *name)
    1.28  {
    1.29      Variable *var = name->binding()->toVariable();
    1.30 @@ -188,6 +199,8 @@
    1.31  void
    1.32  BytecodeCompiler::visit(ForStatement *node)
    1.33  {
    1.34 +    AutoEnterScope forScope(this, node->scope());
    1.35 +
    1.36      if (node->declaration())
    1.37          node->declaration()->accept(this);
    1.38  
    1.39 @@ -208,17 +221,25 @@
    1.40          __ jump(&header);
    1.41      }
    1.42  
    1.43 +    leaveScope(node->scope());
    1.44      assert(__ stackDepth() == 0);
    1.45  }
    1.46  
    1.47  void
    1.48  BytecodeCompiler::visit(BlockStatement *node)
    1.49  {
    1.50 +    AutoEnterScope blockScope(this, node->scope());
    1.51 +
    1.52      for (size_t i = 0; i < node->statements()->length(); i++) {
    1.53          Statement *statement = node->statements()->at(i);
    1.54          statement->accept(this);
    1.55          assert(__ stackDepth() == 0);
    1.56      }
    1.57 +
    1.58 +    // We don't bother emitting an "unwind" instruction for function scopes,
    1.59 +    // since this is taken care of by the calling convention.
    1.60 +    if (node->scope() && node->scope()->enclosing()->kind() != Scope::FUNCTION)
    1.61 +        leaveScope(node->scope());
    1.62  }
    1.63  
    1.64  void
    1.65 @@ -310,6 +331,8 @@
    1.66  Code *
    1.67  BytecodeCompiler::compileFunction(Module *module, FunctionStatement *node)
    1.68  {
    1.69 +    AutoEnterScope functionScope(this, node->scope());
    1.70 +
    1.71      locals_ = new Type *[node->scope()->maxContainedLocals()];
    1.72      memset(locals_, 0, sizeof(Type *) * node->scope()->maxContainedLocals());
    1.73