1.1 --- a/src/BytecodeEmitter.cpp Mon Sep 17 00:10:01 2012 -0700
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,122 +0,0 @@
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 <string.h>
1.24 -#include "BytecodeEmitter.h"
1.25 -#include "Utility.h"
1.26 -#include "Types.h"
1.27 -#include "Zone.h"
1.28 -#include "Spaces-inl.h"
1.29 -#include "Heap-inl.h"
1.30 -
1.31 -using namespace ke;
1.32 -
1.33 -BytecodeEmitter::BytecodeEmitter(PoolAllocator &pool)
1.34 - : pool_(pool),
1.35 - buffer_(inline_buffer_),
1.36 - buffer_pos_(buffer_),
1.37 - buffer_end_(buffer_pos_ + ARRAY_LENGTH(inline_buffer_)),
1.38 - oom_(false),
1.39 - maxStackDepth_(0)
1.40 -{
1.41 - lastCodePosition_ = 0;
1.42 - firstLine_ = 1;
1.43 -}
1.44 -
1.45 -bool
1.46 -BytecodeEmitter::growBuffer(size_t nbytes)
1.47 -{
1.48 - if (oom_) {
1.49 - buffer_pos_ = buffer_;
1.50 - return false;
1.51 - }
1.52 -
1.53 - size_t newSize = size_t((buffer_end_ - buffer_) * 2);
1.54 - if (newSize >= MAX_LENGTH) {
1.55 - oom_ = true;
1.56 - buffer_pos_ = buffer_;
1.57 - ZONE()->reportOutOfMemory();
1.58 - return false;
1.59 - }
1.60 -
1.61 - bytecode *newBuffer;
1.62 - if (buffer_ == inline_buffer_)
1.63 - newBuffer = (bytecode *)malloc(newSize);
1.64 - else
1.65 - newBuffer = (bytecode *)realloc(buffer_, newSize);
1.66 -
1.67 - // If we can't allocate a buffer, reset the buffer to the start and set
1.68 - // a flag to indicate out-of-memory.
1.69 - if (!newBuffer) {
1.70 - oom_ = true;
1.71 - buffer_pos_ = buffer_;
1.72 - ZONE()->reportOutOfMemory();
1.73 - return false;
1.74 - }
1.75 -
1.76 - if (buffer_ == inline_buffer_)
1.77 - memcpy(newBuffer, inline_buffer_, (buffer_pos_ - inline_buffer_));
1.78 -
1.79 - buffer_pos_ = newBuffer + size_t(buffer_pos_ - buffer_);
1.80 - buffer_ = newBuffer;
1.81 - buffer_end_ = buffer_ + newSize;
1.82 - assert(buffer_pos_ + nbytes < buffer_end_);
1.83 -
1.84 - return true;
1.85 -}
1.86 -
1.87 -void
1.88 -BytecodeEmitter::recordOops()
1.89 -{
1.90 - OopRecord *record = new (pool_) OopRecord(oopMap_.length(), position());
1.91 -
1.92 - record->slots = (bool *)pool_.allocate(record->depth);
1.93 - if (!record->slots) {
1.94 - oom_ = true;
1.95 - return;
1.96 - }
1.97 -
1.98 - bool hadOops = false;
1.99 - for (size_t i = 0; i < oopMap_.length(); i++) {
1.100 - bool bit = oopMap_[i];
1.101 - record->slots[i] = bit;
1.102 - hadOops |= bit;
1.103 - }
1.104 -
1.105 - if (!hadOops)
1.106 - return;
1.107 -
1.108 - oom_ &= oopRecords_.append(record);
1.109 -}
1.110 -
1.111 -void
1.112 -BytecodeEmitter::note_position(const SourcePosition &pos)
1.113 -{
1.114 - assert(pos.line > 0);
1.115 - assert(unsigned(pos.line) >= firstLine_);
1.116 -
1.117 - unsigned lineDelta = unsigned(pos.line) - firstLine_;
1.118 - unsigned pcDelta = unsigned(position()) - lastCodePosition_;
1.119 -
1.120 - sourcemap_.writeUint32(pcDelta);
1.121 - sourcemap_.writeUint32(lineDelta);
1.122 - sourcemap_.writeUint32(pos.col);
1.123 -
1.124 - lastCodePosition_ = unsigned(position());
1.125 -}