jitcraft/Zone.cpp
author David Anderson <dvander@alliedmods.net>
Wed Jul 11 01:12:54 2012 -0700 (2012-07-11)
changeset 69 dfce0539a77d
parent 68 b40b0e18a918
child 85 06a0015b6a5c
permissions -rw-r--r--
Added error reporting API and fixed all known error paths.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     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)
    10  * any later version.
    11  * 
    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.
    15  *
    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/.
    18  */
    19 #include <stdlib.h>
    20 #include "Zone.h"
    21 #include "StringPool.h"
    22 #include "TypeManager.h"
    23 #include "GlobalRoots.h"
    24 #include "AllocatorPolicies.h"
    25 #include "Array.h"
    26 
    27 using namespace ke;
    28 
    29 Zone *ke::SingleThreadedZone;
    30 
    31 Zone::Zone()
    32   : stack_(thisFromCtor()),
    33     heap_(thisFromCtor()),
    34     types_(NULL),
    35     symbols_(NULL),
    36     globalRoots_(NULL),
    37     lastLocalRoot_(NULL),
    38     hasPendingException_(false),
    39     reporter_(NULL)
    40 {
    41 }
    42 
    43 Zone::~Zone()
    44 {
    45     delete types_;
    46     delete symbols_;
    47     delete globalRoots_;
    48 }
    49 
    50 bool
    51 Zone::initialize()
    52 {
    53     if (!stack_.initialize())
    54         return false;
    55     if (!heap_.initialize())
    56         return false;
    57 
    58     symbols_ = new StringPool(this);
    59     if (!symbols_ || !symbols_->initialize())
    60         return false;
    61 
    62     globalRoots_ = new GlobalRoots(this);
    63     if (!globalRoots_ || !globalRoots_->initialize())
    64         return false;
    65 
    66     maps_[MapKind_Meta] = heap_.metaMap();
    67     if ((maps_[MapKind_Type] = allocateMap(MapKind_Type, sizeof(Map))) == NULL)
    68         return false;
    69     if ((maps_[MapKind_FixedArray] = allocateMap(MapKind_FixedArray, sizeof(Map))) == NULL)
    70         return false;
    71     if ((maps_[MapKind_ByteArray] = allocateMap(MapKind_ByteArray, sizeof(Map))) == NULL)
    72         return false;
    73     if ((maps_[MapKind_Code] = allocateMap(MapKind_Code, sizeof(Map))) == NULL)
    74         return false;
    75     if ((maps_[MapKind_Reference] = allocateMap(MapKind_Reference, sizeof(Map))) == NULL)
    76         return false;
    77     if ((maps_[MapKind_MetaArray] = allocateMap(MapKind_MetaArray, sizeof(Map))) == NULL)
    78         return false;
    79     if ((maps_[MapKind_Module] = allocateMap(MapKind_Module, sizeof(Map))) == NULL)
    80         return false;
    81     if ((maps_[MapKind_Function] = allocateMap(MapKind_Function, sizeof(Map))) == NULL)
    82         return false;
    83     if ((maps_[MapKind_String] = allocateMap(MapKind_String, sizeof(Map))) == NULL)
    84         return false;
    85     if ((maps_[MapKind_Native] = allocateMap(MapKind_Native, sizeof(Map))) == NULL)
    86         return false;
    87     if ((maps_[MapKind_FreeRegion] = allocateMap(MapKind_FreeRegion, sizeof(Map))) == NULL)
    88         return false;
    89 
    90     types_ = new TypeManager(this);
    91     if (!types_ || !types_->initialize())
    92         return false;
    93 
    94     // Create the cached array maps for generic arrays. This is for the API
    95     // version of Invoke(), which needs to make arrays very quickly from C++.
    96     {
    97         // Generic "cell" array (contents do not matter, just non-gc things).
    98         Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
    99         if (!type)
   100             return false;
   101 
   102         type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
   103         if (!type)
   104             return false;
   105 
   106         Local<ArrayMap> contained(this);
   107         genericArrayMap_ = ArrayMap::New(this, type, contained);
   108         if (!genericArrayMap_)
   109             return false;
   110 
   111         type = types_->getPrimitive(PrimitiveType_Char);
   112         if (!type)
   113             return false;
   114 
   115         type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
   116         if (!type)
   117             return false;
   118 
   119         charArrayMap_ = ArrayMap::New(this, type, contained);
   120         if (!charArrayMap_)
   121             return false;
   122     }
   123 
   124     return true;
   125 }
   126 
   127 Map *
   128 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
   129 {
   130     assert(maps_[parent]);
   131     Map *map = heap_.allocateMap(kind, bytes);
   132     if (!map)
   133         return NULL;
   134     map->init(maps_[parent]);
   135     return map;
   136 }
   137 
   138 void
   139 Zone::accept(VirtualObjectVisitor *visitor)
   140 {
   141     for (unsigned i = 0; i < MapKinds_Total; i++)
   142         visitor->visit(maps_[i].address());
   143     visitor->visit(charArrayMap_.address());
   144     visitor->visit(genericArrayMap_.address());
   145     types()->accept(visitor);
   146 
   147     Local<Object> *local = lastLocalRoot_;
   148     while (local) {
   149         visitor->visit(local->address());
   150         local = local->prev();
   151     }
   152 
   153     globalRoots_->accept(visitor);
   154     rootScopes_.accept(visitor);
   155 
   156     stack_.accept(visitor);
   157 }
   158 
   159 String *
   160 Zone::makeSymbol(const char *str, size_t length)
   161 {
   162     return symbols_->add(str, length);
   163 }
   164 
   165 String *
   166 Zone::makeSymbol(const char *str)
   167 {
   168     return makeSymbol(str, strlen(str));
   169 }
   170 
   171 bool
   172 Zone::hasPendingException()
   173 {
   174     return hasPendingException_;
   175 }
   176 
   177 void
   178 Zone::reportOutOfMemory()
   179 {
   180     
   181 }
   182 
   183 void
   184 Zone::reportError(Message message, ...)
   185 {
   186     va_list ap;
   187     va_start(ap, message);
   188     reportErrorVA(message, ap);
   189     va_end(ap);
   190 }
   191 
   192 #if defined(_MSC_VER)
   193 # define VA_COPY(to, from)  to = from
   194 #else
   195 # define VA_COPY(to, from)  va_copy(to, from)
   196 #endif
   197 
   198 static char *
   199 BuildErrorMessage(const char *format, va_list ap)
   200 {
   201     va_list use;
   202     VA_COPY(use, ap);
   203 
   204     // We over-allocate and zero terminate ahead of time, since LIBCRT's
   205     // version of vsnprintf is screwy.
   206     size_t size = 255;
   207     char *buffer = (char *)calloc(size + 1, 1);
   208     if (!buffer)
   209         return NULL;
   210 
   211     for (;;) {
   212         int r = vsnprintf(buffer, size, format, use);
   213 
   214         if (r > -1 && size_t(r) < size)
   215             return buffer;
   216        
   217 #if defined(_MSC_VER)
   218         if (r < 0)
   219             size *= 2;
   220 #else
   221         if (r > -1)
   222             size = size_t(r) + 1;
   223         else
   224             size *= 2;
   225 #endif
   226 
   227         free(buffer);
   228         if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
   229             return NULL;
   230 
   231         VA_COPY(use, ap);
   232     }
   233 }
   234 
   235 void
   236 Zone::reportErrorVA(Message message, va_list ap)
   237 {
   238     char *report = BuildErrorMessage(Messages[message].format, ap);
   239     if (!report) {
   240         reportOutOfMemory();
   241         return;
   242     }
   243 
   244     if (reporter_) {
   245         // Reporters can re-enter. When we start having actual exception
   246         // objects that can be caught and inspected, this code will have to
   247         // save and restore the exception state.
   248         reporter_->onUncaughtException(report);
   249     }
   250 
   251     hasPendingException_ = true;
   252 
   253     free(report);
   254 }
   255 
   256 void
   257 Zone::beginCompilationErrors(const char *file)
   258 {
   259     if (reporter_)
   260         reporter_->beginCompilationErrors(file);
   261 }
   262 
   263 void
   264 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
   265 {
   266     char *report = BuildErrorMessage(Messages[message].format, ap);
   267     if (!report) {
   268         reportOutOfMemory();
   269         return;
   270     }
   271 
   272     if (reporter_) {
   273         // Reporters can re-enter. When we start having actual exception
   274         // objects that can be caught and inspected, this code will have to
   275         // save and restore the exception state.
   276         reporter_->reportCompilationError(report, line, col);
   277     }
   278 
   279     free(report);
   280 }
   281 
   282 void
   283 Zone::endCompilationErrors()
   284 {
   285     if (reporter_)
   286         reporter_->endCompilationErrors();
   287 }
   288 
   289 void
   290 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
   291 {
   292     reporter_ = reporter;
   293 }
   294 
   295 void
   296 ZoneAllocatorPolicy::reportOutOfMemory()
   297 {
   298     ZONE()->reportOutOfMemory();
   299 }
   300 
   301 void
   302 ZoneAllocatorPolicy::reportAllocationOverflow()
   303 {
   304     ZONE()->reportError(Message_AllocationOverflow);
   305 }