jitcraft/Stack.cpp
author David Anderson <dvander@alliedmods.net>
Mon Jul 02 01:24:57 2012 -0700 (2012-07-02)
changeset 25 23e8b3d8e4e6
parent 12 75e385f68b7e
child 26 a48382113da6
permissions -rw-r--r--
Scanning, parsing, and static analysis now report errors properly.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include <new>
    19 #include <stdlib.h>
    20 #include "Stack.h"
    21 #include "Zone.h"
    22 #include "Heap-inl.h"
    23 
    24 using namespace jc;
    25 
    26 Stack::Stack()
    27   : buffer_(NULL),
    28     top_(NULL),
    29     end_(NULL)
    30 {
    31 }
    32 
    33 Stack::~Stack()
    34 {
    35     free(buffer_);
    36 }
    37 
    38 bool
    39 Stack::initialize()
    40 {
    41     buffer_ = (char *)malloc(STACK_SIZE);
    42     if (!buffer_)
    43         return false;
    44 
    45     top_ = NULL;
    46     end_ = buffer_ + STACK_SIZE;
    47     return true;
    48 }
    49 
    50 static inline size_t
    51 SizeForCodeFrame(Code *code)
    52 {
    53     return sizeof(Frame) + sizeof(LocalSlot) * (code->nlocals() + code->maxStackDepth());
    54 }
    55 
    56 Frame *
    57 Stack::pushFrame(Frame *prev, Code *code)
    58 {
    59     size_t nbytes = SizeForCodeFrame(code);
    60     if (reinterpret_cast<char *>(top_->sp) + nbytes >= end_)
    61         return NULL;
    62 
    63     Frame *frame = reinterpret_cast<Frame *>(top_->sp);
    64     new (frame) Frame(prev, top_->pc, code, ZONE()->heap().lifoPosition());
    65     memset(frame->locals(), 0, sizeof(LocalSlot) * code->nlocals());
    66 
    67     top_->pc = frame->code()->start();
    68     top_->sp = frame->stackBase();
    69 
    70     return frame;
    71 }
    72 
    73 
    74 Frame *
    75 Stack::popFrame(Frame *frame)
    76 {
    77     Frame *prev = frame->prev();
    78     Code *code = frame->code();
    79     size_t nbytes = sizeof(LocalSlot) * code->nlocals() +
    80                     sizeof(Frame) +
    81                     sizeof(LocalSlot) * code->nargs();
    82     top_->pc = frame->prevpc();
    83     top_->sp = reinterpret_cast<LocalSlot *>(reinterpret_cast<char *>(top_->sp) - nbytes);
    84     ZONE()->heap().unwindLifo(frame->lifoPosition());
    85     return prev;
    86 }