src/Zone.cpp
author David Anderson <dvander@alliedmods.net>
Sat Dec 22 23:18:57 2012 -0800 (2012-12-22)
changeset 233 bbb498ccb4ac
parent 222 0cde1379b29f
child 240 e3c851a14f16
permissions -rw-r--r--
Re-add enum support.
     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 #include "NativeTable.h"
    27 #include "Spaces-inl.h"
    28 #include "Heap-inl.h"
    29 
    30 using namespace ke;
    31 
    32 Zone *ke::SingleThreadedZone;
    33 
    34 void * const NullAddress::Sentinel = NULL;
    35 
    36 Zone::Zone()
    37   : stack_(thisFromCtor()),
    38     heap_(thisFromCtor()),
    39     types_(NULL),
    40     symbols_(NULL),
    41     globalRoots_(NULL),
    42     lastLocalRoot_(NULL),
    43     specialRoots_(NULL),
    44     hasPendingException_(false),
    45     reporter_(NULL)
    46 {
    47 }
    48 
    49 Zone::~Zone()
    50 {
    51     delete types_;
    52     delete nativeTable_;
    53     delete globalRoots_;
    54     delete symbols_;
    55 }
    56 
    57 bool
    58 Zone::initialize()
    59 {
    60     if (!stack_.initialize())
    61         return false;
    62     if (!heap_.initialize())
    63         return false;
    64 
    65     symbols_ = new StringPool(this);
    66     if (!symbols_ || !symbols_->initialize())
    67         return false;
    68 
    69     globalRoots_ = new GlobalRoots(this);
    70     if (!globalRoots_ || !globalRoots_->initialize())
    71         return false;
    72 
    73     nativeTable_ = new NativeTable(this);
    74     if (!nativeTable_ || !nativeTable_->initialize())
    75         return false;
    76 
    77     MapKind maps[] =
    78     {
    79         MapKind_Type,
    80         MapKind_ArrayType,
    81         MapKind_FunctionType,
    82         MapKind_StructType,
    83         MapKind_ReferenceType,
    84         MapKind_EnumType,
    85         MapKind_FixedArray,
    86         MapKind_ByteArray,
    87         MapKind_Code,
    88         MapKind_Reference,
    89         MapKind_MetaArray,
    90         MapKind_MetaStruct,
    91         MapKind_MetaEnvironment,
    92         MapKind_Module,
    93         MapKind_Function,
    94         MapKind_String,
    95         MapKind_Native,
    96         MapKind_Descriptor,
    97         MapKind_Box,
    98         MapKind_FreeRegion,
    99         MapKind_EnvironmentSlotDescriptorArray
   100     };
   101 
   102     maps_[MapKind_Meta] = heap_.metaMap();
   103     for (size_t i = 0; i < sizeof(maps) / sizeof(maps[0]); i++) {
   104         if ((maps_[maps[i]] = allocateMap(maps[i], sizeof(Map))) == NULL)
   105             return false;
   106     }
   107 
   108     types_ = new TypeManager(this);
   109     if (!types_ || !types_->initialize())
   110         return false;
   111 
   112     // Create the cached array maps for generic arrays. This is for the API
   113     // version of Invoke(), which needs to make arrays very quickly from C++.
   114     {
   115         // Generic "cell" array (contents do not matter, just non-gc things).
   116         Local<Type> type(this, types_->getPrimitive(PrimitiveType_Int32));
   117         if (!type)
   118             return false;
   119 
   120         type = types_->newExternalArray(type);
   121         if (!type)
   122             return false;
   123 
   124         // :TODO:
   125 #if 0
   126         externalGenericArrayMap_ = ArrayMap::Attach(this, type);
   127         if (!externalGenericArrayMap_)
   128             return false;
   129 #endif
   130 
   131         type = types_->getPrimitive(PrimitiveType_Char);
   132         if (!type)
   133             return false;
   134 
   135         type = types_->newExternalArray(type);
   136         if (!type)
   137             return false;
   138 
   139         // :TODO:
   140 #if 0
   141         externalCharArrayMap_ = ArrayMap::Attach(this, type);
   142         if (!externalCharArrayMap_)
   143             return false;
   144 #endif
   145     }
   146 
   147     return true;
   148 }
   149 
   150 Map *
   151 Zone::allocateMap(MapKind kind, size_t bytes, MapKind parent)
   152 {
   153     assert(maps_[parent]);
   154     Map *map = heap_.allocateMap(kind, bytes);
   155     if (!map)
   156         return NULL;
   157     map->init(maps_[parent]);
   158     return map;
   159 }
   160 
   161 void
   162 Zone::accept(VirtualObjectVisitor *visitor)
   163 {
   164     for (unsigned i = 0; i < MapKinds_Total; i++)
   165         visitor->visit(maps_[i].address());
   166     visitor->visit(externalCharArrayMap_.address());
   167     visitor->visit(externalGenericArrayMap_.address());
   168     types()->accept(visitor);
   169 
   170     Local<Object> *local = lastLocalRoot_;
   171     while (local) {
   172         visitor->visit(local->address());
   173         local = local->prev();
   174     }
   175 
   176     globalRoots_->accept(visitor);
   177     rootScopes_.accept(visitor);
   178     nativeTable_->accept(visitor);
   179 
   180     stack_.accept(visitor);
   181 }
   182 
   183 String *
   184 Zone::makeSymbol(const char *str, size_t length)
   185 {
   186     return symbols_->add(str, length);
   187 }
   188 
   189 String *
   190 Zone::makeSymbol(const char *str)
   191 {
   192     return makeSymbol(str, strlen(str));
   193 }
   194 
   195 bool
   196 Zone::hasPendingException()
   197 {
   198     return hasPendingException_;
   199 }
   200 
   201 void
   202 Zone::reportOutOfMemory()
   203 {
   204     
   205 }
   206 
   207 void
   208 Zone::reportError(Message message, ...)
   209 {
   210     va_list ap;
   211     va_start(ap, message);
   212     reportErrorVA(message, ap);
   213     va_end(ap);
   214 }
   215 
   216 #if defined(_MSC_VER)
   217 # define VA_COPY(to, from)  to = from
   218 #else
   219 # define VA_COPY(to, from)  va_copy(to, from)
   220 #endif
   221 
   222 static char *
   223 BuildErrorMessage(const char *format, va_list ap)
   224 {
   225     va_list use;
   226     VA_COPY(use, ap);
   227 
   228     // We over-allocate and zero terminate ahead of time, since LIBCRT's
   229     // version of vsnprintf is screwy.
   230     size_t size = 255;
   231     char *buffer = (char *)calloc(size + 1, 1);
   232     if (!buffer)
   233         return NULL;
   234 
   235     for (;;) {
   236         int r = vsnprintf(buffer, size, format, use);
   237 
   238         if (r > -1 && size_t(r) < size)
   239             return buffer;
   240        
   241 #if defined(_MSC_VER)
   242         if (r < 0)
   243             size *= 2;
   244 #else
   245         if (r > -1)
   246             size = size_t(r) + 1;
   247         else
   248             size *= 2;
   249 #endif
   250 
   251         free(buffer);
   252         if ((buffer = (char *)calloc(size + 1, 1)) == NULL)
   253             return NULL;
   254 
   255         VA_COPY(use, ap);
   256     }
   257 }
   258 
   259 void
   260 Zone::reportErrorVA(Message message, va_list ap)
   261 {
   262     char *report = BuildErrorMessage(Messages[message].format, ap);
   263     if (!report) {
   264         reportOutOfMemory();
   265         return;
   266     }
   267 
   268     if (reporter_) {
   269         // Reporters can re-enter. When we start having actual exception
   270         // objects that can be caught and inspected, this code will have to
   271         // save and restore the exception state.
   272         reporter_->onUncaughtException(report);
   273     }
   274 
   275     hasPendingException_ = true;
   276 
   277     free(report);
   278 }
   279 
   280 void
   281 Zone::beginCompilationErrors(const char *file)
   282 {
   283     if (reporter_)
   284         reporter_->beginCompilationErrors(file);
   285 }
   286 
   287 void
   288 Zone::reportCompilationError(Message message, unsigned line, unsigned col, va_list ap)
   289 {
   290     char *report = BuildErrorMessage(Messages[message].format, ap);
   291     if (!report) {
   292         reportOutOfMemory();
   293         return;
   294     }
   295 
   296     if (reporter_) {
   297         // Reporters can re-enter. When we start having actual exception
   298         // objects that can be caught and inspected, this code will have to
   299         // save and restore the exception state.
   300         reporter_->reportCompilationError(report, line, col);
   301     }
   302 
   303     free(report);
   304 }
   305 
   306 void
   307 Zone::endCompilationErrors()
   308 {
   309     if (reporter_)
   310         reporter_->endCompilationErrors();
   311 }
   312 
   313 void
   314 Zone::installErrorReporter(Ke::IMessageReporter *reporter)
   315 {
   316     reporter_ = reporter;
   317 }
   318 
   319 void
   320 ZoneAllocatorPolicy::reportOutOfMemory()
   321 {
   322     ZONE()->reportOutOfMemory();
   323 }
   324 
   325 void
   326 ZoneAllocatorPolicy::reportAllocationOverflow()
   327 {
   328     ZONE()->reportAllocationOverflow();
   329 }
   330 
   331 void
   332 Zone::reportAllocationOverflow()
   333 {
   334     reportError(Message_AllocationOverflow);
   335 }
   336 
   337 void
   338 Zone::clearPendingException()
   339 {
   340     hasPendingException_ = false;
   341 }
   342 
   343 void
   344 Zone::setPublicFunctionListener(Ke::IPublicFunctionListener *listener)
   345 {
   346     publicFunctionListener_ = listener;
   347 }
   348 
   349 AutoRooted::AutoRooted(Zone *zone)
   350   : zone_(zone),
   351     prev_(*zone->lastSpecialRoot())
   352 {
   353     *zone->lastSpecialRoot() = this;
   354 }
   355 
   356 AutoRooted::~AutoRooted()
   357 {
   358     *zone_->lastSpecialRoot() = prev_;
   359 }