Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
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"
29 Zone *ke::SingleThreadedZone;
32 : stack_(thisFromCtor()),
33 heap_(thisFromCtor()),
38 hasPendingException_(false),
53 if (!stack_.initialize())
55 if (!heap_.initialize())
58 symbols_ = new StringPool(this);
59 if (!symbols_ || !symbols_->initialize())
62 globalRoots_ = new GlobalRoots(this);
63 if (!globalRoots_ || !globalRoots_->initialize())
66 maps_[MapKind_Meta] = heap_.metaMap();
67 if ((maps_[MapKind_Type] = allocateMap(MapKind_Type, sizeof(Map))) == NULL)
69 if ((maps_[MapKind_FixedArray] = allocateMap(MapKind_FixedArray, sizeof(Map))) == NULL)
71 if ((maps_[MapKind_ByteArray] = allocateMap(MapKind_ByteArray, sizeof(Map))) == NULL)
73 if ((maps_[MapKind_Code] = allocateMap(MapKind_Code, sizeof(Map))) == NULL)
75 if ((maps_[MapKind_Reference] = allocateMap(MapKind_Reference, sizeof(Map))) == NULL)
77 if ((maps_[MapKind_MetaArray] = allocateMap(MapKind_MetaArray, sizeof(Map))) == NULL)
79 if ((maps_[MapKind_MetaEnvironment] = allocateMap(MapKind_MetaEnvironment, sizeof(Map))) == NULL)
81 if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
83 if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
85 if ((maps_[MapKind_String] = allocateMap(MapKind_String, sizeof(Map))) == NULL)
87 if ((maps_[MapKind_Native] = allocateMap(MapKind_Native, sizeof(Map))) == NULL)
89 if ((maps_[MapKind_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
91 if ((maps_[MapKind_EnvironmentSlotDescriptorArray] =
92 allocateMap(MapKind_EnvironmentSlotDescriptorArray, sizeof(Map))) == NULL)
97 types_ = new TypeManager(this);
98 if (!types_ || !types_->initialize())
101 // Create the cached array maps for generic arrays. This is for the API
102 // version of Invoke(), which needs to make arrays very quickly from C++.
104 // Generic "cell" array (contents do not matter, just non-gc things).
105 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
109 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
113 Local<ArrayMap> contained(this);
114 genericArrayMap_ = ArrayMap::New(this, type, contained);
115 if (!genericArrayMap_)
118 type = types_->getPrimitive(PrimitiveType_Char);
122 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
126 charArrayMap_ = ArrayMap::New(this, type, contained);
135 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
137 assert(maps_[parent]);
138 Map *map = heap_.allocateMap(kind, bytes);
141 map->init(maps_[parent]);
146 Zone::accept(VirtualObjectVisitor *visitor)
148 for (unsigned i = 0; i < MapKinds_Total; i++)
149 visitor->visit(maps_[i].address());
150 visitor->visit(charArrayMap_.address());
151 visitor->visit(genericArrayMap_.address());
152 types()->accept(visitor);
154 Local<Object> *local = lastLocalRoot_;
156 visitor->visit(local->address());
157 local = local->prev();
160 globalRoots_->accept(visitor);
161 rootScopes_.accept(visitor);
163 stack_.accept(visitor);
167 Zone::makeSymbol(const char *str, size_t length)
169 return symbols_->add(str, length);
173 Zone::makeSymbol(const char *str)
175 return makeSymbol(str, strlen(str));
179 Zone::hasPendingException()
181 return hasPendingException_;
185 Zone::reportOutOfMemory()
191 Zone::reportError(Message message, ...)
194 va_start(ap, message);
195 reportErrorVA(message, ap);
199 #if defined(_MSC_VER)
200 # define VA_COPY(to, from) to = from
202 # define VA_COPY(to, from) va_copy(to, from)
206 BuildErrorMessage(const char *format, va_list ap)
211 // We over-allocate and zero terminate ahead of time, since LIBCRT's
212 // version of vsnprintf is screwy.
214 char *buffer = (char *)calloc(size + 1, 1);
219 int r = vsnprintf(buffer, size, format, use);
221 if (r > -1 && size_t(r) < size)
224 #if defined(_MSC_VER)
229 size = size_t(r) + 1;
235 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
243 Zone::reportErrorVA(Message message, va_list ap)
245 char *report = BuildErrorMessage(Messages[message].format, ap);
252 // Reporters can re-enter. When we start having actual exception
253 // objects that can be caught and inspected, this code will have to
254 // save and restore the exception state.
255 reporter_->onUncaughtException(report);
258 hasPendingException_ = true;
264 Zone::beginCompilationErrors(const char *file)
267 reporter_->beginCompilationErrors(file);
271 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
273 char *report = BuildErrorMessage(Messages[message].format, ap);
280 // Reporters can re-enter. When we start having actual exception
281 // objects that can be caught and inspected, this code will have to
282 // save and restore the exception state.
283 reporter_->reportCompilationError(report, line, col);
290 Zone::endCompilationErrors()
293 reporter_->endCompilationErrors();
297 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
299 reporter_ = reporter;
303 ZoneAllocatorPolicy::reportOutOfMemory()
305 ZONE()->reportOutOfMemory();
309 ZoneAllocatorPolicy::reportAllocationOverflow()
311 ZONE()->reportError(Message_AllocationOverflow);