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