Get preinc working again.
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/.
21 #include "StringPool.h"
22 #include "TypeManager.h"
23 #include "GlobalRoots.h"
24 #include "AllocatorPolicies.h"
26 #include "NativeTable.h"
27 #include "Spaces-inl.h"
32 Zone *ke::SingleThreadedZone;
34 void * const NullAddress::Sentinel = NULL;
37 : stack_(thisFromCtor()),
38 heap_(thisFromCtor()),
44 hasPendingException_(false),
60 if (!stack_.initialize())
62 if (!heap_.initialize())
65 symbols_ = new StringPool(this);
66 if (!symbols_ || !symbols_->initialize())
69 globalRoots_ = new GlobalRoots(this);
70 if (!globalRoots_ || !globalRoots_->initialize())
73 nativeTable_ = new NativeTable(this);
74 if (!nativeTable_ || !nativeTable_->initialize())
89 MapKind_MetaEnvironment,
97 MapKind_EnvironmentSlotDescriptorArray
100 maps_[MapKind_Meta] = heap_.metaMap();
101 for (size_t i = 0; i < sizeof(maps) / sizeof(maps[0]); i++) {
102 if ((maps_[maps[i]] = allocateMap(maps[i], sizeof(Map))) == NULL)
106 types_ = new TypeManager(this);
107 if (!types_ || !types_->initialize())
110 // Create the cached array maps for generic arrays. This is for the API
111 // version of Invoke(), which needs to make arrays very quickly from C++.
113 // Generic "cell" array (contents do not matter, just non-gc things).
114 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
118 type = types_->newExternalArray(type);
124 externalGenericArrayMap_ = ArrayMap::Attach(this, type);
125 if (!externalGenericArrayMap_)
129 type = types_->getPrimitive(PrimitiveType_Char);
133 type = types_->newExternalArray(type);
139 externalCharArrayMap_ = ArrayMap::Attach(this, type);
140 if (!externalCharArrayMap_)
149 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
151 assert(maps_[parent]);
152 Map *map = heap_.allocateMap(kind, bytes);
155 map->init(maps_[parent]);
160 Zone::accept(VirtualObjectVisitor *visitor)
162 for (unsigned i = 0; i < MapKinds_Total; i++)
163 visitor->visit(maps_[i].address());
164 visitor->visit(externalCharArrayMap_.address());
165 visitor->visit(externalGenericArrayMap_.address());
166 types()->accept(visitor);
168 Local<Object> *local = lastLocalRoot_;
170 visitor->visit(local->address());
171 local = local->prev();
174 globalRoots_->accept(visitor);
175 rootScopes_.accept(visitor);
176 nativeTable_->accept(visitor);
178 stack_.accept(visitor);
182 Zone::makeSymbol(const char *str, size_t length)
184 return symbols_->add(str, length);
188 Zone::makeSymbol(const char *str)
190 return makeSymbol(str, strlen(str));
194 Zone::hasPendingException()
196 return hasPendingException_;
200 Zone::reportOutOfMemory()
206 Zone::reportError(Message message, ...)
209 va_start(ap, message);
210 reportErrorVA(message, ap);
214 #if defined(_MSC_VER)
215 # define VA_COPY(to, from) to = from
217 # define VA_COPY(to, from) va_copy(to, from)
221 BuildErrorMessage(const char *format, va_list ap)
226 // We over-allocate and zero terminate ahead of time, since LIBCRT's
227 // version of vsnprintf is screwy.
229 char *buffer = (char *)calloc(size + 1, 1);
234 int r = vsnprintf(buffer, size, format, use);
236 if (r > -1 && size_t(r) < size)
239 #if defined(_MSC_VER)
244 size = size_t(r) + 1;
250 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
258 Zone::reportErrorVA(Message message, va_list ap)
260 char *report = BuildErrorMessage(Messages[message].format, ap);
267 // Reporters can re-enter. When we start having actual exception
268 // objects that can be caught and inspected, this code will have to
269 // save and restore the exception state.
270 reporter_->onUncaughtException(report);
273 hasPendingException_ = true;
279 Zone::beginCompilationErrors(const char *file)
282 reporter_->beginCompilationErrors(file);
286 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
288 char *report = BuildErrorMessage(Messages[message].format, ap);
295 // Reporters can re-enter. When we start having actual exception
296 // objects that can be caught and inspected, this code will have to
297 // save and restore the exception state.
298 reporter_->reportCompilationError(report, line, col);
305 Zone::endCompilationErrors()
308 reporter_->endCompilationErrors();
312 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
314 reporter_ = reporter;
318 ZoneAllocatorPolicy::reportOutOfMemory()
320 ZONE()->reportOutOfMemory();
324 ZoneAllocatorPolicy::reportAllocationOverflow()
326 ZONE()->reportAllocationOverflow();
330 Zone::reportAllocationOverflow()
332 reportError(Message_AllocationOverflow);
336 Zone::clearPendingException()
338 hasPendingException_ = false;
342 Zone::setPublicFunctionListener(Ke::IPublicFunctionListener *listener)
344 publicFunctionListener_ = listener;
347 AutoRooted::AutoRooted(Zone *zone)
349 prev_(*zone->lastSpecialRoot())
351 *zone->lastSpecialRoot() = this;
354 AutoRooted::~AutoRooted()
356 *zone_->lastSpecialRoot() = prev_;