Added support for arrays of Floats.
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/.
23 Scope::Scope(PoolAllocator &pool, Kind kind, Scope *enclosing)
26 enclosing_(enclosing),
28 maxContainedLocals_(0),
37 enclosing_->children_.push_back(this);
46 assert(enclosing_->children_[enclosing_->children_.size() - 1] == this);
47 enclosing_->children_.pop_back();
49 for (size_t i = 0; i < children_.size(); i++)
50 enclosing_->children_.push_back(children_[i]);
54 Scope::unlinkIfEmpty()
64 Scope::localLookup(Name *name)
66 for (size_t i = 0; i < names_.size(); i++) {
67 if (names_[i]->name() == name)
74 Scope::lookup(Name *name)
76 for (Scope *scope = this; scope; scope = scope->enclosing()) {
77 if (BoundName *bound = scope->localLookup(name))
84 Scope::declareVariable(Name *name)
86 assert(!localLookup(name));
88 Variable *var = new (pool_) Variable(this, name);
89 names_.push_back(var);
94 Scope::declareArgument(Name *name)
96 assert(kind() == FUNCTION);
97 Variable *var = declareVariable(name);
98 names_.push_back(var);
99 parameters_.push_back(var);
104 Scope::declareFunction(Name *name)
106 assert(!localLookup(name));
107 assert(kind() == GLOBAL);
109 NamedFunction *fun = new (pool_) NamedFunction(this, name);
110 names_.push_back(fun);
115 Scope::declareEnum(Name *name)
117 NamedEnum *e = new (pool_) NamedEnum(this, name);
123 Scope::declareConstant(Name *name)
125 NamedConstant *constant = new (pool_) NamedConstant(this, name);
126 names_.push_back(constant);
131 Scope::allocateGlobals()
133 assert(kind() == GLOBAL);
135 for (size_t i = 0; i < children_.size(); i++) {
136 assert(children_[i]->kind() == FUNCTION);
137 children_[i]->allocate();
140 for (size_t i = 0; i < names_.size(); i++) {
141 BoundName *name = names_[i];
143 if (name->kind() == FUNCTION) {
144 NamedFunction *fun = name->toNamedFunction();
145 fun->allocate(numFunctions_++);
153 maxContainedLocals_ = 0;
154 for (size_t i = 0; i < children_.size(); i++) {
155 Scope *scope = children_[i];
157 if (scope->maxContainedLocals() > maxContainedLocals_)
158 maxContainedLocals_ = scope->maxContainedLocals();
161 for (size_t i = 0; i < parameters_.size(); i++)
162 parameters_[i]->allocate(Variable::PARAMETER, i);
165 for (size_t i = 0; i < names_.size(); i++) {
166 if (names_[i]->kind() != BoundName::VARIABLE)
169 Variable *var = names_[i]->toVariable();
170 if (var->unallocated()) {
171 var->allocate(Variable::STACK, maxContainedLocals_ + locals_);
176 maxContainedLocals_ += locals_;
180 Scope::BindAndAllocate(ParseTree *tree)
182 for (size_t i = 0; i < tree->unbound()->size(); i++) {
183 NameProxy *proxy = tree->unbound()->at(i);
184 BoundName *name = tree->scope()->lookup(proxy->name());
193 tree->scope()->allocateGlobals();
198 Scope::firstUnsafeArray()
200 for (size_t i = 0; i < names_.size(); i++) {
201 if (names_[i]->kind() != BoundName::VARIABLE)
203 if (names_[i]->toVariable()->type()->isUnsafeArray())
204 return names_[i]->toVariable();