Refactored Array, Code to be Objects. Removed Object tagging.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
21 #include "TypeManager.h"
25 Scope::Scope(PoolAllocator &pool, Kind kind, Scope *enclosing)
28 enclosing_(enclosing),
30 maxContainedLocals_(0),
40 enclosing_->children_.push_back(this);
49 assert(enclosing_->children_[enclosing_->children_.size() - 1] == this);
50 enclosing_->children_.pop_back();
52 for (size_t i = 0; i < children_.size(); i++)
53 enclosing_->children_.push_back(children_[i]);
57 Scope::unlinkIfEmpty()
67 Scope::localLookup(Name *name)
69 for (size_t i = 0; i < names_.size(); i++) {
70 if (names_[i]->name() == name)
77 Scope::lookup(Name *name)
79 for (Scope *scope = this; scope; scope = scope->enclosing()) {
80 if (BoundName *bound = scope->localLookup(name))
87 Scope::declareVariable(Name *name)
89 assert(!localLookup(name));
91 Variable *var = new (pool_) Variable(this, name);
92 names_.push_back(var);
97 Scope::declareArgument(Name *name)
99 assert(kind() == FUNCTION);
100 Variable *var = declareVariable(name);
101 names_.push_back(var);
102 parameters_.push_back(var);
107 Scope::declareFunction(Name *name)
109 assert(!localLookup(name));
110 assert(kind() == GLOBAL);
112 NamedFunction *fun = new (pool_) NamedFunction(this, name);
113 names_.push_back(fun);
118 Scope::declareEnum(Name *name)
120 NamedEnum *e = new (pool_) NamedEnum(this, name);
126 Scope::declareConstant(Name *name)
128 NamedConstant *constant = new (pool_) NamedConstant(this, name);
129 names_.push_back(constant);
134 Scope::allocateGlobals()
136 assert(kind() == GLOBAL);
138 for (size_t i = 0; i < children_.size(); i++) {
139 assert(children_[i]->kind() == FUNCTION);
140 children_[i]->allocate();
143 for (size_t i = 0; i < names_.size(); i++) {
144 BoundName *name = names_[i];
146 if (name->kind() == FUNCTION) {
147 NamedFunction *fun = name->toNamedFunction();
148 fun->allocate(numFunctions_++);
154 Scope::allocate(Variable *var)
156 if (!var->unallocated())
159 var->allocate(Variable::STACK, maxContainedLocals_ + locals_);
166 maxContainedLocals_ = 0;
167 for (size_t i = 0; i < children_.size(); i++) {
168 Scope *scope = children_[i];
170 if (scope->maxContainedLocals() > maxContainedLocals_)
171 maxContainedLocals_ = scope->maxContainedLocals();
174 for (size_t i = 0; i < parameters_.size(); i++)
175 parameters_[i]->allocate(Variable::PARAMETER, i);
179 for (size_t i = 0; i < names_.size(); i++) {
180 if (names_[i]->kind() != BoundName::VARIABLE)
183 allocate(names_[i]->toVariable());
186 maxContainedLocals_ += locals_;
190 Scope::Bind(ParseTree *tree)
192 for (size_t i = 0; i < tree->unbound()->size(); i++) {
193 NameProxy *proxy = tree->unbound()->at(i);
194 BoundName *name = tree->scope()->lookup(proxy->name());
206 Scope::Allocate(ParseTree *tree)
208 tree->scope()->allocateGlobals();
212 Scope::setUsesLifoHeap()
214 // Only one lifo slot is needed per scope.
218 // If the parent of this scope is the function scope, then this is the
219 // function's block scope, and the lifo slot is part of the Frame instead.
220 assert(kind() != FUNCTION);
221 if (enclosing()->kind() == FUNCTION)
224 lifoSlot_ = new (pool_) Variable(this, NULL);
225 lifoSlot_->setType(ZONE()->types()->getPrimitive(PrimitiveType_Native));