jitcraft/BytecodeEmitter.cpp
changeset 0 c455ebb9a5dc
child 7 0478c27ba82c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/BytecodeEmitter.cpp	Mon Jun 25 02:57:28 2012 -0700
     1.3 @@ -0,0 +1,82 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#include <stdlib.h>
    1.22 +#include "BytecodeEmitter.h"
    1.23 +#include "Utility.h"
    1.24 +#include "Types.h"
    1.25 +
    1.26 +using namespace jc;
    1.27 +
    1.28 +BytecodeEmitter::BytecodeEmitter()
    1.29 +  : buffer_(inline_buffer_),
    1.30 +    buffer_pos_(buffer_),
    1.31 +    buffer_end_(buffer_pos_ + ARRAY_LENGTH(inline_buffer_)),
    1.32 +    oom_(false),
    1.33 +    stackDepth_(0),
    1.34 +    maxStackDepth_(0)
    1.35 +{
    1.36 +}
    1.37 +
    1.38 +bool
    1.39 +BytecodeEmitter::growBuffer(size_t nbytes)
    1.40 +{
    1.41 +    if (oom_) {
    1.42 +        buffer_pos_ = buffer_;
    1.43 +        return false;
    1.44 +    }
    1.45 +
    1.46 +    size_t newSize = size_t((buffer_end_ - buffer_) * 2);
    1.47 +    if (newSize >= MAX_LENGTH) {
    1.48 +        oom_ = true;
    1.49 +        buffer_pos_ = buffer_;
    1.50 +        return false;
    1.51 +    }
    1.52 +
    1.53 +    bytecode *newBuffer;
    1.54 +    if (buffer_ == inline_buffer_)
    1.55 +        newBuffer = (bytecode *)malloc(nbytes);
    1.56 +    else
    1.57 +        newBuffer = (bytecode *)realloc(buffer_, newSize);
    1.58 +
    1.59 +    // If we can't allocate a buffer, reset the buffer to the start and set
    1.60 +    // a flag to indicate out-of-memory.
    1.61 +    if (!newBuffer) {
    1.62 +        oom_ = true;
    1.63 +        buffer_pos_ = buffer_;
    1.64 +        return false;
    1.65 +    }
    1.66 +
    1.67 +    buffer_pos_ = newBuffer + size_t(buffer_pos_ - buffer_);
    1.68 +    buffer_ = newBuffer;
    1.69 +    buffer_end_ = buffer_ + newSize;
    1.70 +    return true;
    1.71 +}
    1.72 +
    1.73 +void
    1.74 +BytecodeEmitter::stackarray(Type *type)
    1.75 +{
    1.76 +    assert(type->innermost()->primitive() == PrimitiveType_Int32);
    1.77 +
    1.78 +    ensure((sizeof(Opcode) * 3) + sizeof(unsigned) * Type::MAX_UNSAFE_ARRAY_DEPTH);
    1.79 +    writeOp(OP_STACKARRAY);
    1.80 +    writeUint8(sizeof(unsigned));
    1.81 +    writeUint8(type->levels());
    1.82 +
    1.83 +    for (; type->isArray(); type = type->contained())
    1.84 +        writeUint32(type->elements());
    1.85 +}