jitcraft/Scopes.cpp
changeset 12 75e385f68b7e
parent 2 46fc53b8fd2f
child 16 23627f8bc421
     1.1 --- a/jitcraft/Scopes.cpp	Fri Jun 29 19:35:17 2012 -0700
     1.2 +++ b/jitcraft/Scopes.cpp	Sat Jun 30 19:38:53 2012 -0700
     1.3 @@ -16,7 +16,9 @@
     1.4   * JITCraft. If not, see http://www.gnu.org/licenses/.
     1.5   */
     1.6  #include "Scopes.h"
     1.7 +#include "Zone.h"
     1.8  #include "Types.h"
     1.9 +#include "TypeManager.h"
    1.10  
    1.11  using namespace jc;
    1.12  
    1.13 @@ -26,7 +28,8 @@
    1.14      enclosing_(enclosing),
    1.15      locals_(0),
    1.16      maxContainedLocals_(0),
    1.17 -    numFunctions_(0)
    1.18 +    numFunctions_(0),
    1.19 +    lifoSlot_(NULL)
    1.20  {
    1.21  }
    1.22  
    1.23 @@ -148,6 +151,16 @@
    1.24  }
    1.25  
    1.26  void
    1.27 +Scope::allocate(Variable *var)
    1.28 +{
    1.29 +    if (!var->unallocated())
    1.30 +        return;
    1.31 +
    1.32 +    var->allocate(Variable::STACK, maxContainedLocals_ + locals_);
    1.33 +    locals_++;
    1.34 +}
    1.35 +
    1.36 +void
    1.37  Scope::allocate()
    1.38  {
    1.39      maxContainedLocals_ = 0;
    1.40 @@ -161,23 +174,20 @@
    1.41      for (size_t i = 0; i < parameters_.size(); i++)
    1.42          parameters_[i]->allocate(Variable::PARAMETER, i);
    1.43  
    1.44 -    locals_ = 0;
    1.45 +    if (lifoSlot_)
    1.46 +        allocate(lifoSlot_);
    1.47      for (size_t i = 0; i < names_.size(); i++) {
    1.48          if (names_[i]->kind() != BoundName::VARIABLE)
    1.49              continue;
    1.50  
    1.51 -        Variable *var = names_[i]->toVariable();
    1.52 -        if (var->unallocated()) {
    1.53 -            var->allocate(Variable::STACK, maxContainedLocals_ + locals_);
    1.54 -            locals_++;
    1.55 -        }
    1.56 +        allocate(names_[i]->toVariable());
    1.57      }
    1.58  
    1.59      maxContainedLocals_ += locals_;
    1.60  }
    1.61  
    1.62  bool
    1.63 -Scope::BindAndAllocate(ParseTree *tree)
    1.64 +Scope::Bind(ParseTree *tree)
    1.65  {
    1.66      for (size_t i = 0; i < tree->unbound()->size(); i++) {
    1.67          NameProxy *proxy = tree->unbound()->at(i);
    1.68 @@ -189,19 +199,28 @@
    1.69  
    1.70          proxy->bind(name);
    1.71      }
    1.72 -
    1.73 -    tree->scope()->allocateGlobals();
    1.74      return true;
    1.75  }
    1.76  
    1.77 -Variable *
    1.78 -Scope::firstUnsafeArray()
    1.79 +void
    1.80 +Scope::Allocate(ParseTree *tree)
    1.81 +{
    1.82 +    tree->scope()->allocateGlobals();
    1.83 +}
    1.84 +
    1.85 +void
    1.86 +Scope::setUsesLifoHeap()
    1.87  {
    1.88 -    for (size_t i = 0; i < names_.size(); i++) {
    1.89 -        if (names_[i]->kind() != BoundName::VARIABLE)
    1.90 -            continue;
    1.91 -        if (names_[i]->toVariable()->type()->isUnsafeArray())
    1.92 -            return names_[i]->toVariable();
    1.93 -    }
    1.94 -    return NULL;
    1.95 +    // Only one lifo slot is needed per scope.
    1.96 +    if (lifoSlot_)
    1.97 +        return;
    1.98 +
    1.99 +    // If the parent of this scope is the function scope, then this is the
   1.100 +    // function's block scope, and the lifo slot is part of the Frame instead.
   1.101 +    assert(kind() != FUNCTION);
   1.102 +    if (enclosing()->kind() == FUNCTION)
   1.103 +        return;
   1.104 +
   1.105 +    lifoSlot_ = new (pool_) Variable(this, NULL);
   1.106 +    lifoSlot_->setType(ZONE()->types()->getPrimitive(PrimitiveType_Native));
   1.107  }