jitcraft/BytecodeEmitter.cpp
author David Anderson <dvander@alliedmods.net>
Tue Jul 24 23:53:34 2012 -0700 (2012-07-24)
changeset 86 32ad0523b309
parent 70 1aec996cde2d
permissions -rw-r--r--
Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
     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     lastCodePosition_ = 0;
    36     firstLine_ = 1;
    37 }
    38 
    39 bool
    40 BytecodeEmitter::growBuffer(size_t nbytes)
    41 {
    42     if (oom_) {
    43         buffer_pos_ = buffer_;
    44         return false;
    45     }
    46 
    47     size_t newSize = size_t((buffer_end_ - buffer_) * 2);
    48     if (newSize >= MAX_LENGTH) {
    49         oom_ = true;
    50         buffer_pos_ = buffer_;
    51         ZONE()->reportOutOfMemory();
    52         return false;
    53     }
    54 
    55     bytecode *newBuffer;
    56     if (buffer_ == inline_buffer_)
    57         newBuffer = (bytecode *)malloc(nbytes);
    58     else
    59         newBuffer = (bytecode *)realloc(buffer_, newSize);
    60 
    61     // If we can't allocate a buffer, reset the buffer to the start and set
    62     // a flag to indicate out-of-memory.
    63     if (!newBuffer) {
    64         oom_ = true;
    65         buffer_pos_ = buffer_;
    66         ZONE()->reportOutOfMemory();
    67         return false;
    68     }
    69 
    70     buffer_pos_ = newBuffer + size_t(buffer_pos_ - buffer_);
    71     buffer_ = newBuffer;
    72     buffer_end_ = buffer_ + newSize;
    73     return true;
    74 }
    75 
    76 void
    77 BytecodeEmitter::recordOops()
    78 {
    79     OopRecord *record = new (pool_) OopRecord(oopMap_.length(), position());
    80 
    81     record->slots = (bool *)pool_.allocate(record->depth);
    82     if (!record->slots) {
    83         oom_ = true;
    84         return;
    85     }
    86 
    87     bool hadOops = false;
    88     for (size_t i = 0; i < oopMap_.length(); i++) {
    89         bool bit = oopMap_[i];
    90         record->slots[i] = bit;
    91         hadOops |= bit;
    92     }
    93 
    94     if (!hadOops)
    95         return;
    96 
    97     oom_ &= oopRecords_.append(record);
    98 }
    99 
   100 void
   101 BytecodeEmitter::note_position(const SourcePosition &pos)
   102 {
   103     assert(pos.line > 0);
   104     assert(unsigned(pos.line) >= firstLine_);
   105 
   106     unsigned lineDelta = unsigned(pos.line) - firstLine_;
   107     unsigned pcDelta = unsigned(position()) - lastCodePosition_;
   108 
   109     sourcemap_.writeUint32(pcDelta);
   110     sourcemap_.writeUint32(lineDelta);
   111     sourcemap_.writeUint32(pos.col);
   112 
   113     lastCodePosition_ = unsigned(position());
   114 }