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