Basic support for parsing and creating types from structure syntax.
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_MetaEnvironment] = allocateMap(MapKind_MetaEnvironment, sizeof(Map))) == NULL)
83 if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
85 if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
87 if ((maps_[MapKind_String] = allocateMap(MapKind_String, sizeof(Map))) == NULL)
89 if ((maps_[MapKind_Native] = allocateMap(MapKind_Native, sizeof(Map))) == NULL)
91 if ((maps_[MapKind_Descriptor] = allocateMap(MapKind_Descriptor, sizeof(Map))) == NULL)
93 if ((maps_[MapKind_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
95 if ((maps_[MapKind_EnvironmentSlotDescriptorArray] =
96 allocateMap(MapKind_EnvironmentSlotDescriptorArray, sizeof(Map))) == NULL)
101 types_ = new TypeManager(this);
102 if (!types_ || !types_->initialize())
105 // Create the cached array maps for generic arrays. This is for the API
106 // version of Invoke(), which needs to make arrays very quickly from C++.
108 // Generic "cell" array (contents do not matter, just non-gc things).
109 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
113 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
117 Local<ArrayMap> contained(this);
118 genericArrayMap_ = ArrayMap::New(this, type, contained);
119 if (!genericArrayMap_)
122 type = types_->getPrimitive(PrimitiveType_Char);
126 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
130 charArrayMap_ = ArrayMap::New(this, type, contained);
139 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
141 assert(maps_[parent]);
142 Map *map = heap_.allocateMap(kind, bytes);
145 map->init(maps_[parent]);
150 Zone::accept(VirtualObjectVisitor *visitor)
152 for (unsigned i = 0; i < MapKinds_Total; i++)
153 visitor->visit(maps_[i].address());
154 visitor->visit(charArrayMap_.address());
155 visitor->visit(genericArrayMap_.address());
156 types()->accept(visitor);
158 Local<Object> *local = lastLocalRoot_;
160 visitor->visit(local->address());
161 local = local->prev();
164 globalRoots_->accept(visitor);
165 rootScopes_.accept(visitor);
167 stack_.accept(visitor);
171 Zone::makeSymbol(const char *str, size_t length)
173 return symbols_->add(str, length);
177 Zone::makeSymbol(const char *str)
179 return makeSymbol(str, strlen(str));
183 Zone::hasPendingException()
185 return hasPendingException_;
189 Zone::reportOutOfMemory()
195 Zone::reportError(Message message, ...)
198 va_start(ap, message);
199 reportErrorVA(message, ap);
203 #if defined(_MSC_VER)
204 # define VA_COPY(to, from) to = from
206 # define VA_COPY(to, from) va_copy(to, from)
210 BuildErrorMessage(const char *format, va_list ap)
215 // We over-allocate and zero terminate ahead of time, since LIBCRT's
216 // version of vsnprintf is screwy.
218 char *buffer = (char *)calloc(size + 1, 1);
223 int r = vsnprintf(buffer, size, format, use);
225 if (r > -1 && size_t(r) < size)
228 #if defined(_MSC_VER)
233 size = size_t(r) + 1;
239 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
247 Zone::reportErrorVA(Message message, va_list ap)
249 char *report = BuildErrorMessage(Messages[message].format, ap);
256 // Reporters can re-enter. When we start having actual exception
257 // objects that can be caught and inspected, this code will have to
258 // save and restore the exception state.
259 reporter_->onUncaughtException(report);
262 hasPendingException_ = true;
268 Zone::beginCompilationErrors(const char *file)
271 reporter_->beginCompilationErrors(file);
275 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
277 char *report = BuildErrorMessage(Messages[message].format, ap);
284 // Reporters can re-enter. When we start having actual exception
285 // objects that can be caught and inspected, this code will have to
286 // save and restore the exception state.
287 reporter_->reportCompilationError(report, line, col);
294 Zone::endCompilationErrors()
297 reporter_->endCompilationErrors();
301 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
303 reporter_ = reporter;
307 ZoneAllocatorPolicy::reportOutOfMemory()
309 ZONE()->reportOutOfMemory();
313 ZoneAllocatorPolicy::reportAllocationOverflow()
315 ZONE()->reportError(Message_AllocationOverflow);
319 Zone::clearPendingException()
321 hasPendingException_ = false;