src/Scopes.cpp
author David Anderson <danderson@mozilla.com>
Wed Jul 25 01:32:49 2012 -0700 (2012-07-25)
changeset 93 23189d937087
parent 92 e3974e7360e9
child 137 be919b22085b
permissions -rw-r--r--
Fix opt clang build.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include "Scopes.h"
    20 #include "Zone.h"
    21 #include "Types.h"
    22 #include "TypeManager.h"
    23 #include "Strings.h"
    24 #include "CompileContext.h"
    25 #include "Environments.h"
    26 #include "Spaces-inl.h"
    27 #include "Heap-inl.h"
    28 
    29 using namespace ke;
    30 
    31 Scope::Scope(PoolAllocator &pool, Kind kind, Scope *enclosing)
    32   : pool_(pool),
    33     locals_(0),
    34     maxContainedLocals_(0),
    35     numFunctions_(0),
    36     numNatives_(0),
    37     lifoSlot_(NULL),
    38     kind_(kind),
    39     enclosing_(enclosing)
    40 {
    41 }
    42 
    43 bool
    44 Scope::initialize()
    45 {
    46     if (enclosing_ && !enclosing_->children_.append(this))
    47         return false;
    48     return true;
    49 }
    50 
    51 void
    52 Scope::unlink()
    53 {
    54     assert(empty());
    55 
    56     assert(enclosing_->children_[enclosing_->children_.length() - 1] == this);
    57     enclosing_->children_.pop();
    58 
    59     // OOM here is swallowed and checked after compilation.
    60     // :TODO: make the above true.
    61     for (size_t i = 0; i < children_.length(); i++)
    62         enclosing_->children_.append(children_[i]);
    63 }
    64 
    65 Scope *
    66 Scope::unlinkIfEmpty()
    67 {
    68     if (empty()) {
    69         unlink();
    70         return NULL;
    71     }
    72     return this;
    73 }
    74 
    75 BoundName *
    76 Scope::localLookup(Handle<String> name)
    77 {
    78     for (size_t i = 0; i < names_.length(); i++) {
    79         if (names_[i]->name() == name)
    80             return names_[i];
    81     }
    82     return NULL;
    83 }
    84 
    85 BoundName *
    86 Scope::lookup(Handle<String> name)
    87 {
    88     for (Scope *scope = this; scope; scope = scope->enclosing()) {
    89         if (BoundName *bound = scope->localLookup(name))
    90             return bound;
    91     }
    92     return NULL;
    93 }
    94 
    95 Variable *
    96 Scope::declareVariable(Handle<String> name, const SourcePosition &pos)
    97 {
    98     assert(!localLookup(name));
    99     
   100     Variable *var = new (pool_) Variable(this, name, pos);
   101     if (!names_.append(var))
   102         return NULL;
   103     return var;
   104 }
   105 
   106 Variable *
   107 Scope::declareArgument(Handle<String> name, const SourcePosition &pos)
   108 {
   109     assert(kind() == FUNCTION);
   110     Variable *var = declareVariable(name, pos);
   111     if (!names_.append(var))
   112         return NULL;
   113     if (!parameters_.append(var))
   114         return NULL;
   115     return var;
   116 }
   117 
   118 NamedFunction *
   119 Scope::declareFunction(Handle<String> name, const SourcePosition &pos, TokenKind token)
   120 {
   121     assert(!localLookup(name));
   122     assert(kind() == GLOBAL);
   123 
   124     NamedFunction *fun = new (pool_) NamedFunction(this, name, pos, token);
   125     if (!names_.append(fun))
   126         return NULL;
   127     return fun;
   128 }
   129 
   130 NamedEnum *
   131 Scope::declareEnum(Handle<String> name, const SourcePosition &pos)
   132 {
   133     NamedEnum *e = new (pool_) NamedEnum(this, name, pos);
   134     if (!names_.append(e))
   135         return NULL;
   136     return e;
   137 }
   138 
   139 NamedConstant *
   140 Scope::declareConstant(Handle<String> name, const SourcePosition &pos)
   141 {
   142     NamedConstant *constant = new (pool_) NamedConstant(this, name, pos);
   143     if (!names_.append(constant))
   144         return NULL;
   145     return constant;
   146 }
   147 
   148 void
   149 Scope::allocateGlobals()
   150 {
   151     assert(kind() == GLOBAL);
   152 
   153     for (size_t i = 0; i < children_.length(); i++) {
   154         assert(children_[i]->kind() == FUNCTION);
   155         children_[i]->allocate();
   156     }
   157 
   158     Local<Type> type(ZONE());
   159     for (size_t i = 0; i < names_.length(); i++) {
   160         BoundName *name = names_[i];
   161         
   162         if (name->kind() == BoundName::FUNCTION) {
   163             NamedFunction *fun = name->toNamedFunction();
   164             if (fun->isNative())
   165                 fun->allocate(numNatives_++);
   166             else
   167                 fun->allocate(numFunctions_++);
   168         } else if (name->kind() == BoundName::VARIABLE) {
   169             allocate(name->toVariable());
   170         }
   171     }
   172 }
   173 
   174 void
   175 Scope::allocate(Variable *var)
   176 {
   177     if (!var->unallocated())
   178         return;
   179 
   180     if (kind() == GLOBAL) {
   181         var->allocate(Variable::ENVIRONMENT, envVars_.length());
   182         envVars_.append(var);
   183     } else {
   184         var->allocate(Variable::STACK, maxContainedLocals_ + locals_);
   185         locals_++;
   186     }
   187 }
   188 
   189 void
   190 Scope::allocate()
   191 {
   192     maxContainedLocals_ = 0;
   193     for (size_t i = 0; i < children_.length(); i++) {
   194         Scope *scope = children_[i];
   195         scope->allocate();
   196         if (scope->maxContainedLocals() > maxContainedLocals_)
   197             maxContainedLocals_ = scope->maxContainedLocals();
   198     }
   199 
   200     for (size_t i = 0; i < parameters_.length(); i++)
   201         parameters_[i]->allocate(Variable::PARAMETER, i);
   202 
   203     if (lifoSlot_)
   204         allocate(lifoSlot_);
   205     for (size_t i = 0; i < names_.length(); i++) {
   206         if (names_[i]->kind() != BoundName::VARIABLE)
   207             continue;
   208 
   209         allocate(names_[i]->toVariable());
   210     }
   211 
   212     maxContainedLocals_ += locals_;
   213 }
   214 
   215 bool
   216 Scope::Bind(CompileContext &cc, ParseTree *tree)
   217 {
   218     Local<String> name(ZONE());
   219     for (size_t i = 0; i < tree->unbound()->length(); i++) {
   220         NameProxy *proxy = tree->unbound()->at(i);
   221         name = proxy->name();
   222         BoundName *binding = tree->scope()->lookup(name);
   223 
   224         if (!binding) {
   225             cc.reportError(proxy->pos(), Message_IdentifierNotFound, name->chars());
   226             return false;
   227         }
   228 
   229         // We only allow global functions to be late-bound.
   230         if (binding->kind() != BoundName::FUNCTION ||
   231             binding->scope()->kind() != Scope::GLOBAL)
   232         {
   233             cc.reportError(proxy->pos(), Message_IdentifierNotFound, name->chars());
   234             return false;
   235         }
   236 
   237         proxy->bind(binding);
   238     }
   239     return true;
   240 }
   241 
   242 void
   243 Scope::Allocate(ParseTree *tree)
   244 {
   245     tree->scope()->allocateGlobals();
   246 }
   247 
   248 void
   249 Scope::setUsesLifoHeap()
   250 {
   251     // Only one lifo slot is needed per scope.
   252     if (lifoSlot_)
   253         return;
   254 
   255     // If the parent of this scope is the function scope, then this is the
   256     // function's block scope, and the lifo slot is part of the Frame instead.
   257     // Note we can get a function scope here if we're making a temporary in a
   258     // function with no local variables.
   259     if (kind() == FUNCTION || enclosing()->kind() == FUNCTION)
   260         return;
   261 
   262     Local<Type> type(ZONE(), ZONE()->types()->getPrimitive(PrimitiveType_Native));
   263     if (!type)
   264         return;
   265 
   266     Local<String> name(ZONE());
   267     lifoSlot_ = new (pool_) Variable(this, name, SourcePosition());
   268     lifoSlot_->setType(type);
   269 }
   270 
   271 EnvironmentMap *
   272 Scope::createEnvironmentMap(Zone *zone)
   273 {
   274     return EnvironmentMap::New(zone, envVars_.length(), envVars_.buffer());
   275 }