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