src/BytecodeEmitter.cpp
changeset 86 32ad0523b309
parent 70 1aec996cde2d
child 92 e3974e7360e9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/BytecodeEmitter.cpp	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,114 @@
     1.4 +/* vim: set ts=4 sw=4 tw=99 et:
     1.5 + *
     1.6 + * Copyright (C) 2012 David Anderson
     1.7 + *
     1.8 + * This file is part of JITCraft.
     1.9 + *
    1.10 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.11 + * the terms of the GNU General Public License as published by the Free
    1.12 + * Software Foundation, either version 3 of the License, or (at your option)
    1.13 + * any later version.
    1.14 + * 
    1.15 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License along with
    1.20 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.21 + */
    1.22 +#include <stdlib.h>
    1.23 +#include "BytecodeEmitter.h"
    1.24 +#include "Utility.h"
    1.25 +#include "Types.h"
    1.26 +#include "Zone.h"
    1.27 +
    1.28 +using namespace ke;
    1.29 +
    1.30 +BytecodeEmitter::BytecodeEmitter(PoolAllocator &pool)
    1.31 +  : pool_(pool),
    1.32 +    buffer_(inline_buffer_),
    1.33 +    buffer_pos_(buffer_),
    1.34 +    buffer_end_(buffer_pos_ + ARRAY_LENGTH(inline_buffer_)),
    1.35 +    oom_(false),
    1.36 +    maxStackDepth_(0)
    1.37 +{
    1.38 +    lastCodePosition_ = 0;
    1.39 +    firstLine_ = 1;
    1.40 +}
    1.41 +
    1.42 +bool
    1.43 +BytecodeEmitter::growBuffer(size_t nbytes)
    1.44 +{
    1.45 +    if (oom_) {
    1.46 +        buffer_pos_ = buffer_;
    1.47 +        return false;
    1.48 +    }
    1.49 +
    1.50 +    size_t newSize = size_t((buffer_end_ - buffer_) * 2);
    1.51 +    if (newSize >= MAX_LENGTH) {
    1.52 +        oom_ = true;
    1.53 +        buffer_pos_ = buffer_;
    1.54 +        ZONE()->reportOutOfMemory();
    1.55 +        return false;
    1.56 +    }
    1.57 +
    1.58 +    bytecode *newBuffer;
    1.59 +    if (buffer_ == inline_buffer_)
    1.60 +        newBuffer = (bytecode *)malloc(nbytes);
    1.61 +    else
    1.62 +        newBuffer = (bytecode *)realloc(buffer_, newSize);
    1.63 +
    1.64 +    // If we can't allocate a buffer, reset the buffer to the start and set
    1.65 +    // a flag to indicate out-of-memory.
    1.66 +    if (!newBuffer) {
    1.67 +        oom_ = true;
    1.68 +        buffer_pos_ = buffer_;
    1.69 +        ZONE()->reportOutOfMemory();
    1.70 +        return false;
    1.71 +    }
    1.72 +
    1.73 +    buffer_pos_ = newBuffer + size_t(buffer_pos_ - buffer_);
    1.74 +    buffer_ = newBuffer;
    1.75 +    buffer_end_ = buffer_ + newSize;
    1.76 +    return true;
    1.77 +}
    1.78 +
    1.79 +void
    1.80 +BytecodeEmitter::recordOops()
    1.81 +{
    1.82 +    OopRecord *record = new (pool_) OopRecord(oopMap_.length(), position());
    1.83 +
    1.84 +    record->slots = (bool *)pool_.allocate(record->depth);
    1.85 +    if (!record->slots) {
    1.86 +        oom_ = true;
    1.87 +        return;
    1.88 +    }
    1.89 +
    1.90 +    bool hadOops = false;
    1.91 +    for (size_t i = 0; i < oopMap_.length(); i++) {
    1.92 +        bool bit = oopMap_[i];
    1.93 +        record->slots[i] = bit;
    1.94 +        hadOops |= bit;
    1.95 +    }
    1.96 +
    1.97 +    if (!hadOops)
    1.98 +        return;
    1.99 +
   1.100 +    oom_ &= oopRecords_.append(record);
   1.101 +}
   1.102 +
   1.103 +void
   1.104 +BytecodeEmitter::note_position(const SourcePosition &pos)
   1.105 +{
   1.106 +    assert(pos.line > 0);
   1.107 +    assert(unsigned(pos.line) >= firstLine_);
   1.108 +
   1.109 +    unsigned lineDelta = unsigned(pos.line) - firstLine_;
   1.110 +    unsigned pcDelta = unsigned(position()) - lastCodePosition_;
   1.111 +
   1.112 +    sourcemap_.writeUint32(pcDelta);
   1.113 +    sourcemap_.writeUint32(lineDelta);
   1.114 +    sourcemap_.writeUint32(pos.col);
   1.115 +
   1.116 +    lastCodePosition_ = unsigned(position());
   1.117 +}