jitcraft/Stack.h
author David Anderson <dvander@alliedmods.net>
Sat Jun 30 19:38:53 2012 -0700 (2012-06-30)
changeset 12 75e385f68b7e
parent 10 0c3bcd50e361
child 14 0784ac5cc347
permissions -rw-r--r--
Refactored Array, Code to be Objects. Removed Object tagging.
     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 #ifndef _include_jc_stack_h_
    19 #define _include_jc_stack_h_
    20 
    21 #include <stddef.h>
    22 #include "Utility.h"
    23 #include "Code.h"
    24 
    25 namespace jc {
    26 
    27 class Object;
    28 class Array;
    29     
    30 union LocalSlot
    31 {
    32     int i32;
    33     float f;
    34     Address address;
    35     Object *obj;
    36     Array *array;
    37 };
    38 
    39 struct Top
    40 {
    41     LocalSlot *sp;
    42     bytecode *pc;
    43 
    44     Top(LocalSlot *sp, bytecode *pc)
    45       : sp(sp), pc(pc)
    46     {
    47     }
    48 
    49     Top(LocalSlot *sp)
    50       : sp(sp), pc(NULL)
    51     {
    52     }
    53 };
    54 
    55 class Frame
    56 {
    57     friend class Stack;
    58 
    59     Frame *prev_;
    60     bytecode *prevpc_;
    61     Code *code_;
    62     Address lifoPosition_;
    63 
    64   public:
    65     Frame(Frame *prev, bytecode *prevpc, Code *code, Address lifoPosition)
    66       : prev_(prev),
    67         prevpc_(prevpc),
    68         code_(code),
    69         lifoPosition_(lifoPosition)
    70     {
    71     }
    72 
    73     LocalSlot *locals() {
    74         return reinterpret_cast<LocalSlot *>(this + 1);
    75     }
    76     LocalSlot *stackBase() {
    77         return locals() + code_->nlocals();
    78     }
    79     Code *code() const {
    80         return code_;
    81     }
    82     Frame *prev() const {
    83         return prev_;
    84     }
    85     bytecode *prevpc() const {
    86         return prevpc_;
    87     }
    88     Address lifoPosition() const {
    89         return lifoPosition_;
    90     }
    91     LocalSlot &arg(unsigned slot) {
    92         assert(slot < code()->nargs());
    93         return *(reinterpret_cast<LocalSlot *>(this) - slot - 1);
    94     }
    95 };
    96 
    97 class Stack
    98 {
    99     char *buffer_;
   100     char *end_;
   101     Top *top_;
   102 
   103   private:
   104     static const size_t STACK_SIZE = 1024 * 1024;
   105 
   106   public:
   107     Stack();
   108     ~Stack();
   109 
   110     bool initialize();
   111 
   112   public:
   113     Frame *pushFrame(Frame *prev, Code *code);
   114     Frame *popFrame(Frame *frame);
   115 
   116     Top *top() {
   117         return top_;
   118     }
   119     void setTop(Top *top) {
   120         top_ = top;
   121     }
   122     Top begin() {
   123         return Top((LocalSlot *)buffer_);
   124     }
   125 };
   126 
   127 }
   128 
   129 #endif // _include_jc_stack_h_