jitcraft/Stack.h
author David Anderson <dvander@alliedmods.net>
Wed Jul 04 19:39:00 2012 -0400 (2012-07-04)
changeset 47 050939c6181e
parent 26 a48382113da6
child 59 b7d61ddb3c8d
permissions -rw-r--r--
Implemented gcmaps and stack frame tracing.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jc_stack_h_
    20 #define _include_jc_stack_h_
    21 
    22 #include <stddef.h>
    23 #include "Utility.h"
    24 #include "Code.h"
    25 #include "Functions.h"
    26 #include "VirtualObjectVisitor.h"
    27 
    28 namespace jc {
    29 
    30 class Object;
    31 class Array;
    32 class Reference;
    33 class Frame;
    34     
    35 union LocalSlot
    36 {
    37     int i32;
    38     float f;
    39     Address address;
    40     Object *obj;
    41     Array *array;
    42     Reference *ref;
    43 };
    44 
    45 struct Top
    46 {
    47     LocalSlot *sp;
    48     bytecode *pc;
    49     Frame *fp;
    50 
    51     Top(LocalSlot *sp, bytecode *pc, Frame *fp)
    52       : sp(sp), pc(pc), fp(NULL)
    53     {
    54     }
    55 
    56     Top(LocalSlot *sp)
    57       : sp(sp), pc(NULL), fp(NULL)
    58     {
    59     }
    60 };
    61 
    62 class Frame
    63 {
    64     friend class Stack;
    65 
    66     Frame *prev_;
    67     bytecode *prevpc_;
    68     Function *fun_;
    69     Code *code_;
    70     Address lifoPosition_;
    71 
    72   public:
    73     Frame(Frame *prev, bytecode *prevpc, Function *fun, Code *code, Address lifoPosition)
    74       : prev_(prev),
    75         prevpc_(prevpc),
    76         fun_(fun),
    77         code_(code),
    78         lifoPosition_(lifoPosition)
    79     {
    80     }
    81 
    82     LocalSlot *locals() {
    83         return reinterpret_cast<LocalSlot *>(this + 1);
    84     }
    85     LocalSlot *stackBase() {
    86         return locals() + code_->nlocals();
    87     }
    88     Function *fun() const {
    89         assert(fun_);
    90         return fun_;
    91     }
    92     Function *maybeFun() const {
    93         return fun_;
    94     }
    95     Code *code() const {
    96         return code_;
    97     }
    98     Frame *prev() const {
    99         return prev_;
   100     }
   101     bytecode *prevpc() const {
   102         return prevpc_;
   103     }
   104     Address lifoPosition() const {
   105         return lifoPosition_;
   106     }
   107     LocalSlot &arg(unsigned slot) {
   108         assert(slot < fun()->nargs());
   109         return *(reinterpret_cast<LocalSlot *>(this) - slot - 1);
   110     }
   111     void accept(VirtualObjectVisitor *visitor);
   112 };
   113 
   114 class Stack
   115 {
   116     char *buffer_;
   117     char *end_;
   118     Top *top_;
   119 
   120   private:
   121     static const size_t STACK_SIZE = 1024 * 1024;
   122 
   123   public:
   124     Stack();
   125     ~Stack();
   126 
   127     bool initialize();
   128 
   129   public:
   130     Frame *pushFrame(Function *fun);
   131     Frame *popFrame();
   132 
   133     Top *top() {
   134         return top_;
   135     }
   136     void setTop(Top *top) {
   137         top_ = top;
   138     }
   139     Top begin() {
   140         return Top((LocalSlot *)buffer_);
   141     }
   142     void accept(VirtualObjectVisitor *visitor);
   143 };
   144 
   145 }
   146 
   147 #endif // _include_jc_stack_h_