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