Add GCC/Linux 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_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_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
93 if ((maps_[MapKind_EnvironmentSlotDescriptorArray] =
94 allocateMap(MapKind_EnvironmentSlotDescriptorArray, sizeof(Map))) == NULL)
99 types_ = new TypeManager(this);
100 if (!types_ || !types_->initialize())
103 // Create the cached array maps for generic arrays. This is for the API
104 // version of Invoke(), which needs to make arrays very quickly from C++.
106 // Generic "cell" array (contents do not matter, just non-gc things).
107 Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
111 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
115 Local<ArrayMap> contained(this);
116 genericArrayMap_ = ArrayMap::New(this, type, contained);
117 if (!genericArrayMap_)
120 type = types_->getPrimitive(PrimitiveType_Char);
124 type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
128 charArrayMap_ = ArrayMap::New(this, type, contained);
137 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
139 assert(maps_[parent]);
140 Map *map = heap_.allocateMap(kind, bytes);
143 map->init(maps_[parent]);
148 Zone::accept(VirtualObjectVisitor *visitor)
150 for (unsigned i = 0; i < MapKinds_Total; i++)
151 visitor->visit(maps_[i].address());
152 visitor->visit(charArrayMap_.address());
153 visitor->visit(genericArrayMap_.address());
154 types()->accept(visitor);
156 Local<Object> *local = lastLocalRoot_;
158 visitor->visit(local->address());
159 local = local->prev();
162 globalRoots_->accept(visitor);
163 rootScopes_.accept(visitor);
165 stack_.accept(visitor);
169 Zone::makeSymbol(const char *str, size_t length)
171 return symbols_->add(str, length);
175 Zone::makeSymbol(const char *str)
177 return makeSymbol(str, strlen(str));
181 Zone::hasPendingException()
183 return hasPendingException_;
187 Zone::reportOutOfMemory()
193 Zone::reportError(Message message, ...)
196 va_start(ap, message);
197 reportErrorVA(message, ap);
201 #if defined(_MSC_VER)
202 # define VA_COPY(to, from) to = from
204 # define VA_COPY(to, from) va_copy(to, from)
208 BuildErrorMessage(const char *format, va_list ap)
213 // We over-allocate and zero terminate ahead of time, since LIBCRT's
214 // version of vsnprintf is screwy.
216 char *buffer = (char *)calloc(size + 1, 1);
221 int r = vsnprintf(buffer, size, format, use);
223 if (r > -1 && size_t(r) < size)
226 #if defined(_MSC_VER)
231 size = size_t(r) + 1;
237 if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
245 Zone::reportErrorVA(Message message, va_list ap)
247 char *report = BuildErrorMessage(Messages[message].format, ap);
254 // Reporters can re-enter. When we start having actual exception
255 // objects that can be caught and inspected, this code will have to
256 // save and restore the exception state.
257 reporter_->onUncaughtException(report);
260 hasPendingException_ = true;
266 Zone::beginCompilationErrors(const char *file)
269 reporter_->beginCompilationErrors(file);
273 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
275 char *report = BuildErrorMessage(Messages[message].format, ap);
282 // Reporters can re-enter. When we start having actual exception
283 // objects that can be caught and inspected, this code will have to
284 // save and restore the exception state.
285 reporter_->reportCompilationError(report, line, col);
292 Zone::endCompilationErrors()
295 reporter_->endCompilationErrors();
299 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
301 reporter_ = reporter;
305 ZoneAllocatorPolicy::reportOutOfMemory()
307 ZONE()->reportOutOfMemory();
311 ZoneAllocatorPolicy::reportAllocationOverflow()
313 ZONE()->reportError(Message_AllocationOverflow);