Added support for ISourcePawnEngine natives.
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 if (enclosing_ && !enclosing_->children_.append(this))
52 assert(enclosing_->children_[enclosing_->children_.length() - 1] == this);
53 enclosing_->children_.pop();
55 // OOM here is swallowed and checked after compilation.
56 // :TODO: make the above true.
57 for (size_t i = 0; i < children_.length(); i++)
58 enclosing_->children_.append(children_[i]);
62 Scope::unlinkIfEmpty()
72 Scope::localLookup(Handle<String> name)
74 for (size_t i = 0; i < names_.length(); i++) {
75 if (names_[i]->name() == name)
82 Scope::lookup(Handle<String> name)
84 for (Scope *scope = this; scope; scope = scope->enclosing()) {
85 if (BoundName *bound = scope->localLookup(name))
92 Scope::declareVariable(Handle<String> name, const SourcePosition &pos)
94 assert(!localLookup(name));
96 Variable *var = new (pool_) Variable(this, name, pos);
97 if (!names_.append(var))
103 Scope::declareArgument(Handle<String> name, const SourcePosition &pos)
105 assert(kind() == FUNCTION);
106 Variable *var = declareVariable(name, pos);
107 if (!names_.append(var))
109 if (!parameters_.append(var))
115 Scope::declareFunction(Handle<String> name, const SourcePosition &pos, TokenKind token)
117 assert(!localLookup(name));
118 assert(kind() == GLOBAL);
120 NamedFunction *fun = new (pool_) NamedFunction(this, name, pos, token);
121 if (!names_.append(fun))
127 Scope::declareEnum(Handle<String> name, const SourcePosition &pos)
129 NamedEnum *e = new (pool_) NamedEnum(this, name, pos);
130 if (!names_.append(e))
136 Scope::declareConstant(Handle<String> name, const SourcePosition &pos)
138 NamedConstant *constant = new (pool_) NamedConstant(this, name, pos);
139 if (!names_.append(constant))
145 Scope::allocateGlobals()
147 assert(kind() == GLOBAL);
149 for (size_t i = 0; i < children_.length(); i++) {
150 assert(children_[i]->kind() == FUNCTION);
151 children_[i]->allocate();
154 for (size_t i = 0; i < names_.length(); i++) {
155 BoundName *name = names_[i];
157 if (name->kind() == BoundName::FUNCTION) {
158 NamedFunction *fun = name->toNamedFunction();
160 fun->allocate(numNatives_++);
162 fun->allocate(numFunctions_++);
168 Scope::allocate(Variable *var)
170 if (!var->unallocated())
173 var->allocate(Variable::STACK, maxContainedLocals_ + locals_);
180 maxContainedLocals_ = 0;
181 for (size_t i = 0; i < children_.length(); i++) {
182 Scope *scope = children_[i];
184 if (scope->maxContainedLocals() > maxContainedLocals_)
185 maxContainedLocals_ = scope->maxContainedLocals();
188 for (size_t i = 0; i < parameters_.length(); i++)
189 parameters_[i]->allocate(Variable::PARAMETER, i);
193 for (size_t i = 0; i < names_.length(); i++) {
194 if (names_[i]->kind() != BoundName::VARIABLE)
197 allocate(names_[i]->toVariable());
200 maxContainedLocals_ += locals_;
204 Scope::Bind(ParseTree *tree)
206 Local<String> name(ZONE());
207 for (size_t i = 0; i < tree->unbound()->length(); i++) {
208 NameProxy *proxy = tree->unbound()->at(i);
209 name = proxy->name();
210 BoundName *binding = tree->scope()->lookup(name);
216 proxy->bind(binding);
222 Scope::Allocate(ParseTree *tree)
224 tree->scope()->allocateGlobals();
228 Scope::setUsesLifoHeap()
230 // Only one lifo slot is needed per scope.
234 // If the parent of this scope is the function scope, then this is the
235 // function's block scope, and the lifo slot is part of the Frame instead.
236 // Note we can get a function scope here if we're making a temporary in a
237 // function with no local variables.
238 if (kind() == FUNCTION || enclosing()->kind() == FUNCTION)
241 Local<Type> type(ZONE(), ZONE()->types()->getPrimitive(PrimitiveType_Native));
245 Local<String> name(ZONE());
246 lifoSlot_ = new (pool_) Variable(this, name, SourcePosition());
247 lifoSlot_->setType(type);