jitcraft/Stack.cpp
changeset 0 c455ebb9a5dc
child 1 27f12e2fc369
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/Stack.cpp	Mon Jun 25 02:57:28 2012 -0700
     1.3 @@ -0,0 +1,107 @@
     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 <new>
    1.22 +#include <stdlib.h>
    1.23 +#include "Stack.h"
    1.24 +
    1.25 +using namespace jc;
    1.26 +
    1.27 +Stack::Stack()
    1.28 +  : buffer_(NULL),
    1.29 +    top_(NULL),
    1.30 +    end_(NULL)
    1.31 +{
    1.32 +}
    1.33 +
    1.34 +Stack::~Stack()
    1.35 +{
    1.36 +    free(buffer_);
    1.37 +}
    1.38 +
    1.39 +bool
    1.40 +Stack::initialize()
    1.41 +{
    1.42 +    buffer_ = (char *)malloc(STACK_SIZE);
    1.43 +    if (!buffer_)
    1.44 +        return false;
    1.45 +
    1.46 +    top_ = NULL;
    1.47 +    end_ = buffer_ + STACK_SIZE;
    1.48 +    return true;
    1.49 +}
    1.50 +
    1.51 +static inline size_t
    1.52 +SizeForCodeFrame(Code *code)
    1.53 +{
    1.54 +    return sizeof(Frame) + sizeof(LocalSlot) * (code->nlocals() + code->maxStackDepth());
    1.55 +}
    1.56 +
    1.57 +Frame *
    1.58 +Stack::pushFrame(Frame *prev, Code *code)
    1.59 +{
    1.60 +    size_t nbytes = SizeForCodeFrame(code);
    1.61 +    if (reinterpret_cast<char *>(top_->sp) + nbytes >= end_)
    1.62 +        return NULL;
    1.63 +
    1.64 +    Frame *frame = reinterpret_cast<Frame *>(top_->sp);
    1.65 +    new (frame) Frame(prev, top_->pc, code);
    1.66 +    memset(frame->locals(), 0, sizeof(LocalSlot) * code->nlocals());
    1.67 +
    1.68 +    top_->pc = frame->code()->start();
    1.69 +    top_->sp = frame->stackBase();
    1.70 +
    1.71 +    return frame;
    1.72 +}
    1.73 +
    1.74 +
    1.75 +Frame *
    1.76 +Stack::popFrame(Frame *frame)
    1.77 +{
    1.78 +    Frame *prev = frame->prev();
    1.79 +    Code *code = frame->code();
    1.80 +    size_t nbytes = sizeof(LocalSlot) * code->nlocals() +
    1.81 +                    sizeof(Frame) +
    1.82 +                    sizeof(LocalSlot) * code->nargs();
    1.83 +    top_->pc = frame->prevpc();
    1.84 +    top_->sp = reinterpret_cast<LocalSlot *>(reinterpret_cast<char *>(top_->sp) - nbytes);
    1.85 +    return prev;
    1.86 +}
    1.87 +
    1.88 +UnsafeStack::UnsafeStack()
    1.89 +  : buffer_(NULL),
    1.90 +    pos_(NULL),
    1.91 +    end_(NULL)
    1.92 +{
    1.93 +}
    1.94 +
    1.95 +UnsafeStack::~UnsafeStack()
    1.96 +{
    1.97 +    free(buffer_);
    1.98 +}
    1.99 +
   1.100 +bool
   1.101 +UnsafeStack::initialize()
   1.102 +{
   1.103 +    buffer_ = (char *)malloc(STACK_SIZE);
   1.104 +    if (!buffer_)
   1.105 +        return false;
   1.106 +
   1.107 +    pos_ = NULL;
   1.108 +    end_ = buffer_ + STACK_SIZE;
   1.109 +    return true;
   1.110 +}
   1.111 \ No newline at end of file