jitcraft/Stack.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 02 01:52:57 2012 -0700 (2012-07-02)
changeset 26 a48382113da6
parent 14 0784ac5cc347
child 47 050939c6181e
permissions -rw-r--r--
Added a Function object to sit in between Module and Code.
     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 #include "Functions.h"
    25 
    26 namespace jc {
    27 
    28 class Object;
    29 class Array;
    30 class Reference;
    31     
    32 union LocalSlot
    33 {
    34     int i32;
    35     float f;
    36     Address address;
    37     Object *obj;
    38     Array *array;
    39     Reference *ref;
    40 };
    41 
    42 struct Top
    43 {
    44     LocalSlot *sp;
    45     bytecode *pc;
    46 
    47     Top(LocalSlot *sp, bytecode *pc)
    48       : sp(sp), pc(pc)
    49     {
    50     }
    51 
    52     Top(LocalSlot *sp)
    53       : sp(sp), pc(NULL)
    54     {
    55     }
    56 };
    57 
    58 class Frame
    59 {
    60     friend class Stack;
    61 
    62     Frame *prev_;
    63     bytecode *prevpc_;
    64     Function *fun_;
    65     Code *code_;
    66     Address lifoPosition_;
    67 
    68   public:
    69     Frame(Frame *prev, bytecode *prevpc, Function *fun, Code *code, Address lifoPosition)
    70       : prev_(prev),
    71         prevpc_(prevpc),
    72         fun_(fun),
    73         code_(code),
    74         lifoPosition_(lifoPosition)
    75     {
    76     }
    77 
    78     LocalSlot *locals() {
    79         return reinterpret_cast<LocalSlot *>(this + 1);
    80     }
    81     LocalSlot *stackBase() {
    82         return locals() + code_->nlocals();
    83     }
    84     Function *fun() const {
    85         assert(fun_);
    86         return fun_;
    87     }
    88     Function *maybeFun() const {
    89         return fun_;
    90     }
    91     Code *code() const {
    92         return code_;
    93     }
    94     Frame *prev() const {
    95         return prev_;
    96     }
    97     bytecode *prevpc() const {
    98         return prevpc_;
    99     }
   100     Address lifoPosition() const {
   101         return lifoPosition_;
   102     }
   103     LocalSlot &arg(unsigned slot) {
   104         assert(slot < fun()->nargs());
   105         return *(reinterpret_cast<LocalSlot *>(this) - slot - 1);
   106     }
   107 };
   108 
   109 class Stack
   110 {
   111     char *buffer_;
   112     char *end_;
   113     Top *top_;
   114 
   115   private:
   116     static const size_t STACK_SIZE = 1024 * 1024;
   117 
   118   public:
   119     Stack();
   120     ~Stack();
   121 
   122     bool initialize();
   123 
   124   public:
   125     Frame *pushFrame(Frame *prev, Function *fun);
   126     Frame *popFrame(Frame *frame);
   127 
   128     Top *top() {
   129         return top_;
   130     }
   131     void setTop(Top *top) {
   132         top_ = top;
   133     }
   134     Top begin() {
   135         return Top((LocalSlot *)buffer_);
   136     }
   137 };
   138 
   139 }
   140 
   141 #endif // _include_jc_stack_h_