jitcraft/BytecodeEmitter.cpp
author David Anderson <dvander@alliedmods.net>
Wed Jul 11 01:12:54 2012 -0700 (2012-07-11)
changeset 69 dfce0539a77d
parent 59 b7d61ddb3c8d
child 70 1aec996cde2d
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 "BytecodeEmitter.h"
    21 #include "Utility.h"
    22 #include "Types.h"
    23 #include "Zone.h"
    24 
    25 using namespace ke;
    26 
    27 BytecodeEmitter::BytecodeEmitter(PoolAllocator &pool)
    28   : pool_(pool),
    29     buffer_(inline_buffer_),
    30     buffer_pos_(buffer_),
    31     buffer_end_(buffer_pos_ + ARRAY_LENGTH(inline_buffer_)),
    32     oom_(false),
    33     maxStackDepth_(0)
    34 {
    35 }
    36 
    37 bool
    38 BytecodeEmitter::growBuffer(size_t nbytes)
    39 {
    40     if (oom_) {
    41         buffer_pos_ = buffer_;
    42         return false;
    43     }
    44 
    45     size_t newSize = size_t((buffer_end_ - buffer_) * 2);
    46     if (newSize >= MAX_LENGTH) {
    47         oom_ = true;
    48         buffer_pos_ = buffer_;
    49         ZONE()->reportOutOfMemory();
    50         return false;
    51     }
    52 
    53     bytecode *newBuffer;
    54     if (buffer_ == inline_buffer_)
    55         newBuffer = (bytecode *)malloc(nbytes);
    56     else
    57         newBuffer = (bytecode *)realloc(buffer_, newSize);
    58 
    59     // If we can't allocate a buffer, reset the buffer to the start and set
    60     // a flag to indicate out-of-memory.
    61     if (!newBuffer) {
    62         oom_ = true;
    63         buffer_pos_ = buffer_;
    64         ZONE()->reportOutOfMemory();
    65         return false;
    66     }
    67 
    68     buffer_pos_ = newBuffer + size_t(buffer_pos_ - buffer_);
    69     buffer_ = newBuffer;
    70     buffer_end_ = buffer_ + newSize;
    71     return true;
    72 }
    73 
    74 void
    75 BytecodeEmitter::recordOops()
    76 {
    77     OopRecord *record = new (pool_) OopRecord(oopMap_.length(), position());
    78 
    79     record->slots = (bool *)pool_.allocate(record->depth);
    80     if (!record->slots) {
    81         oom_ = true;
    82         return;
    83     }
    84 
    85     bool hadOops = false;
    86     for (size_t i = 0; i < oopMap_.length(); i++) {
    87         bool bit = oopMap_[i];
    88         record->slots[i] = bit;
    89         hadOops |= bit;
    90     }
    91 
    92     if (!hadOops)
    93         return;
    94 
    95     oom_ &= oopRecords_.append(record);
    96 }
    97