More const preparation: allow type casts to follow qualification.
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())
83 MapKind_ReferenceType,
91 MapKind_MetaEnvironment,
99 MapKind_EnvironmentSlotDescriptorArray
102 maps_[MapKind_Meta] = heap_.metaMap();
103 for (size_t i = 0; i < sizeof(maps) / sizeof(maps[0]); i++) {
104 if ((maps_[maps[i]] = allocateMap(maps[i], sizeof(Map))) == NULL)
108 types_ = new TypeManager(this);
109 if (!types_ || !types_->initialize())
112 // Create the cached array maps for generic arrays. This is for the API
113 // version of Invoke(), which needs to make arrays very quickly from C++.
115 // Generic "cell" array (contents do not matter, just non-gc things).
116 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
120 type = types_->newExternalArray(type);
126 externalGenericArrayMap_ = ArrayMap::Attach(this, type);
127 if (!externalGenericArrayMap_)
131 type = types_->getPrimitive(PrimitiveType_Char);
135 type = types_->newExternalArray(type);
141 externalCharArrayMap_ = ArrayMap::Attach(this, type);
142 if (!externalCharArrayMap_)
151 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
153 assert(maps_[parent]);
154 Map *map = heap_.allocateMap(kind, bytes);
157 map->init(maps_[parent]);
162 Zone::accept(VirtualObjectVisitor *visitor)
164 for (unsigned i = 0; i < MapKinds_Total; i++)
165 visitor->visit(maps_[i].address());
166 visitor->visit(externalCharArrayMap_.address());
167 visitor->visit(externalGenericArrayMap_.address());
168 types()->accept(visitor);
170 Local<Object> *local = lastLocalRoot_;
172 visitor->visit(local->address());
173 local = local->prev();
176 globalRoots_->accept(visitor);
177 rootScopes_.accept(visitor);
178 nativeTable_->accept(visitor);
180 stack_.accept(visitor);
184 Zone::makeSymbol(const char *str, size_t length)
186 return symbols_->add(str, length);
190 Zone::makeSymbol(const char *str)
192 return makeSymbol(str, strlen(str));
196 Zone::hasPendingException()
198 return hasPendingException_;
202 Zone::reportOutOfMemory()
208 Zone::reportError(Message message, ...)
211 va_start(ap, message);
212 reportErrorVA(message, ap);
216 #if defined(_MSC_VER)
217 # define VA_COPY(to, from) to = from
219 # define VA_COPY(to, from) va_copy(to, from)
223 BuildErrorMessage(const char *format, va_list ap)
228 // We over-allocate and zero terminate ahead of time, since LIBCRT's
229 // version of vsnprintf is screwy.
231 char *buffer = (char *)calloc(size + 1, 1);
236 int r = vsnprintf(buffer, size, format, use);
238 if (r > -1 && size_t(r) < size)
241 #if defined(_MSC_VER)
246 size = size_t(r) + 1;
252 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
260 Zone::reportErrorVA(Message message, va_list ap)
262 char *report = BuildErrorMessage(Messages[message].format, ap);
269 // Reporters can re-enter. When we start having actual exception
270 // objects that can be caught and inspected, this code will have to
271 // save and restore the exception state.
272 reporter_->onUncaughtException(report);
275 hasPendingException_ = true;
281 Zone::beginCompilationErrors(const char *file)
284 reporter_->beginCompilationErrors(file);
288 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
290 char *report = BuildErrorMessage(Messages[message].format, ap);
297 // Reporters can re-enter. When we start having actual exception
298 // objects that can be caught and inspected, this code will have to
299 // save and restore the exception state.
300 reporter_->reportCompilationError(report, line, col);
307 Zone::endCompilationErrors()
310 reporter_->endCompilationErrors();
314 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
316 reporter_ = reporter;
320 ZoneAllocatorPolicy::reportOutOfMemory()
322 ZONE()->reportOutOfMemory();
326 ZoneAllocatorPolicy::reportAllocationOverflow()
328 ZONE()->reportAllocationOverflow();
332 Zone::reportAllocationOverflow()
334 reportError(Message_AllocationOverflow);
338 Zone::clearPendingException()
340 hasPendingException_ = false;
344 Zone::setPublicFunctionListener(Ke::IPublicFunctionListener *listener)
346 publicFunctionListener_ = listener;
349 AutoRooted::AutoRooted(Zone *zone)
351 prev_(*zone->lastSpecialRoot())
353 *zone->lastSpecialRoot() = this;
356 AutoRooted::~AutoRooted()
358 *zone_->lastSpecialRoot() = prev_;