Structure declaration support.
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 "Spaces-inl.h"
31 Zone *ke::SingleThreadedZone;
34 : stack_(thisFromCtor()),
35 heap_(thisFromCtor()),
40 hasPendingException_(false),
55 if (!stack_.initialize())
57 if (!heap_.initialize())
60 symbols_ = new StringPool(this);
61 if (!symbols_ || !symbols_->initialize())
64 globalRoots_ = new GlobalRoots(this);
65 if (!globalRoots_ || !globalRoots_->initialize())
68 maps_[MapKind_Meta] = heap_.metaMap();
69 if ((maps_[MapKind_Type] = allocateMap(MapKind_Type, sizeof(Map))) == NULL)
71 if ((maps_[MapKind_FixedArray] = allocateMap(MapKind_FixedArray, sizeof(Map))) == NULL)
73 if ((maps_[MapKind_ByteArray] = allocateMap(MapKind_ByteArray, sizeof(Map))) == NULL)
75 if ((maps_[MapKind_Code] = allocateMap(MapKind_Code, sizeof(Map))) == NULL)
77 if ((maps_[MapKind_Reference] = allocateMap(MapKind_Reference, sizeof(Map))) == NULL)
79 if ((maps_[MapKind_MetaArray] = allocateMap(MapKind_MetaArray, sizeof(Map))) == NULL)
81 if ((maps_[MapKind_MetaStruct] = allocateMap(MapKind_MetaStruct, sizeof(Map))) == NULL)
83 if ((maps_[MapKind_MetaEnvironment] = allocateMap(MapKind_MetaEnvironment, sizeof(Map))) == NULL)
85 if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
87 if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
89 if ((maps_[MapKind_String] = allocateMap(MapKind_String, sizeof(Map))) == NULL)
91 if ((maps_[MapKind_Native] = allocateMap(MapKind_Native, sizeof(Map))) == NULL)
93 if ((maps_[MapKind_Descriptor] = allocateMap(MapKind_Descriptor, sizeof(Map))) == NULL)
95 if ((maps_[MapKind_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
97 if ((maps_[MapKind_EnvironmentSlotDescriptorArray] =
98 allocateMap(MapKind_EnvironmentSlotDescriptorArray, sizeof(Map))) == NULL)
103 types_ = new TypeManager(this);
104 if (!types_ || !types_->initialize())
107 // Create the cached array maps for generic arrays. This is for the API
108 // version of Invoke(), which needs to make arrays very quickly from C++.
110 // Generic "cell" array (contents do not matter, just non-gc things).
111 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
115 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
119 Local<ArrayMap> contained(this);
120 genericArrayMap_ = ArrayMap::New(this, type, contained);
121 if (!genericArrayMap_)
124 type = types_->getPrimitive(PrimitiveType_Char);
128 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
132 charArrayMap_ = ArrayMap::New(this, type, contained);
141 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
143 assert(maps_[parent]);
144 Map *map = heap_.allocateMap(kind, bytes);
147 map->init(maps_[parent]);
152 Zone::accept(VirtualObjectVisitor *visitor)
154 for (unsigned i = 0; i < MapKinds_Total; i++)
155 visitor->visit(maps_[i].address());
156 visitor->visit(charArrayMap_.address());
157 visitor->visit(genericArrayMap_.address());
158 types()->accept(visitor);
160 Local<Object> *local = lastLocalRoot_;
162 visitor->visit(local->address());
163 local = local->prev();
166 globalRoots_->accept(visitor);
167 rootScopes_.accept(visitor);
169 stack_.accept(visitor);
173 Zone::makeSymbol(const char *str, size_t length)
175 return symbols_->add(str, length);
179 Zone::makeSymbol(const char *str)
181 return makeSymbol(str, strlen(str));
185 Zone::hasPendingException()
187 return hasPendingException_;
191 Zone::reportOutOfMemory()
197 Zone::reportError(Message message, ...)
200 va_start(ap, message);
201 reportErrorVA(message, ap);
205 #if defined(_MSC_VER)
206 # define VA_COPY(to, from) to = from
208 # define VA_COPY(to, from) va_copy(to, from)
212 BuildErrorMessage(const char *format, va_list ap)
217 // We over-allocate and zero terminate ahead of time, since LIBCRT's
218 // version of vsnprintf is screwy.
220 char *buffer = (char *)calloc(size + 1, 1);
225 int r = vsnprintf(buffer, size, format, use);
227 if (r > -1 && size_t(r) < size)
230 #if defined(_MSC_VER)
235 size = size_t(r) + 1;
241 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
249 Zone::reportErrorVA(Message message, va_list ap)
251 char *report = BuildErrorMessage(Messages[message].format, ap);
258 // Reporters can re-enter. When we start having actual exception
259 // objects that can be caught and inspected, this code will have to
260 // save and restore the exception state.
261 reporter_->onUncaughtException(report);
264 hasPendingException_ = true;
270 Zone::beginCompilationErrors(const char *file)
273 reporter_->beginCompilationErrors(file);
277 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
279 char *report = BuildErrorMessage(Messages[message].format, ap);
286 // Reporters can re-enter. When we start having actual exception
287 // objects that can be caught and inspected, this code will have to
288 // save and restore the exception state.
289 reporter_->reportCompilationError(report, line, col);
296 Zone::endCompilationErrors()
299 reporter_->endCompilationErrors();
303 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
305 reporter_ = reporter;
309 ZoneAllocatorPolicy::reportOutOfMemory()
311 ZONE()->reportOutOfMemory();
315 ZoneAllocatorPolicy::reportAllocationOverflow()
317 ZONE()->reportError(Message_AllocationOverflow);
321 Zone::clearPendingException()
323 hasPendingException_ = false;