Convert AST std::vector to PoolList.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
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)
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.
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/.
22 #include "TypeManager.h"
27 Scope::Scope(PoolAllocator &pool, Kind kind, Scope *enclosing)
30 maxContainedLocals_(0),
42 enclosing_->children_.push_back(this);
51 assert(enclosing_->children_[enclosing_->children_.size() - 1] == this);
52 enclosing_->children_.pop_back();
54 for (size_t i = 0; i < children_.size(); i++)
55 enclosing_->children_.push_back(children_[i]);
59 Scope::unlinkIfEmpty()
69 Scope::localLookup(Handle<String> name)
71 for (size_t i = 0; i < names_.size(); i++) {
72 if (names_[i]->name() == name)
79 Scope::lookup(Handle<String> name)
81 for (Scope *scope = this; scope; scope = scope->enclosing()) {
82 if (BoundName *bound = scope->localLookup(name))
89 Scope::declareVariable(Handle<String> name, const SourcePosition &pos)
91 assert(!localLookup(name));
93 Variable *var = new (pool_) Variable(this, name, pos);
94 names_.push_back(var);
99 Scope::declareArgument(Handle<String> name, const SourcePosition &pos)
101 assert(kind() == FUNCTION);
102 Variable *var = declareVariable(name, pos);
103 names_.push_back(var);
104 parameters_.push_back(var);
109 Scope::declareFunction(Handle<String> name, const SourcePosition &pos)
111 assert(!localLookup(name));
112 assert(kind() == GLOBAL);
114 NamedFunction *fun = new (pool_) NamedFunction(this, name, pos);
115 names_.push_back(fun);
120 Scope::declareEnum(Handle<String> name, const SourcePosition &pos)
122 NamedEnum *e = new (pool_) NamedEnum(this, name, pos);
128 Scope::declareConstant(Handle<String> name, const SourcePosition &pos)
130 NamedConstant *constant = new (pool_) NamedConstant(this, name, pos);
131 names_.push_back(constant);
136 Scope::allocateGlobals()
138 assert(kind() == GLOBAL);
140 for (size_t i = 0; i < children_.size(); i++) {
141 assert(children_[i]->kind() == FUNCTION);
142 children_[i]->allocate();
145 for (size_t i = 0; i < names_.size(); i++) {
146 BoundName *name = names_[i];
148 if (name->kind() == BoundName::FUNCTION) {
149 NamedFunction *fun = name->toNamedFunction();
150 fun->allocate(numFunctions_++);
156 Scope::allocate(Variable *var)
158 if (!var->unallocated())
161 var->allocate(Variable::STACK, maxContainedLocals_ + locals_);
168 maxContainedLocals_ = 0;
169 for (size_t i = 0; i < children_.size(); i++) {
170 Scope *scope = children_[i];
172 if (scope->maxContainedLocals() > maxContainedLocals_)
173 maxContainedLocals_ = scope->maxContainedLocals();
176 for (size_t i = 0; i < parameters_.size(); i++)
177 parameters_[i]->allocate(Variable::PARAMETER, i);
181 for (size_t i = 0; i < names_.size(); i++) {
182 if (names_[i]->kind() != BoundName::VARIABLE)
185 allocate(names_[i]->toVariable());
188 maxContainedLocals_ += locals_;
192 Scope::Bind(ParseTree *tree)
194 Local<String> name(ZONE());
195 for (size_t i = 0; i < tree->unbound()->length(); i++) {
196 NameProxy *proxy = tree->unbound()->at(i);
197 name = proxy->name();
198 BoundName *binding = tree->scope()->lookup(name);
204 proxy->bind(binding);
210 Scope::Allocate(ParseTree *tree)
212 tree->scope()->allocateGlobals();
216 Scope::setUsesLifoHeap()
218 // Only one lifo slot is needed per scope.
222 // If the parent of this scope is the function scope, then this is the
223 // function's block scope, and the lifo slot is part of the Frame instead.
224 // Note we can get a function scope here if we're making a temporary in a
225 // function with no local variables.
226 if (kind() == FUNCTION || enclosing()->kind() == FUNCTION)
229 Local<Type> type(ZONE(), ZONE()->types()->getPrimitive(PrimitiveType_Native));
233 Local<String> name(ZONE());
234 lifoSlot_ = new (pool_) Variable(this, name, SourcePosition());
235 lifoSlot_->setType(type);