stackframe.cc

Go to the documentation of this file.
00001 /*
00002  *  stackframe.cc - a usecode interpreter stack frame
00003  *
00004  *  Copyright (C) 2002  The Exult Team
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #  include <config.h>
00023 #endif
00024 
00025 #include <iostream>
00026 #include <iomanip>
00027 
00028 #include "stackframe.h"
00029 #include "useval.h"
00030 #include "ucinternal.h"
00031 #include "utils.h"
00032 #include "ucfunction.h"
00033 
00034 
00035 int Stack_frame::LastCallChainID = 0;
00036 
00037 Stack_frame::Stack_frame(Usecode_function *fun,
00038              int event,
00039              Game_object *caller,
00040              int chain, int depth)
00041   : function(fun), ip(0), data(0), externs(0), code(0), endp(0),
00042     line_number(-1), call_chain(chain), call_depth(depth), 
00043     num_externs(0), num_args(0), num_vars(0), locals(0), eventid(event),
00044     caller_item(caller), save_sp(0)
00045 {
00046   ip = function->code;
00047   endp = ip + function->len;
00048   
00049   int data_len;
00050   if (!fun->extended)
00051     data_len = Read2(ip); // Get length of (text) data.
00052   else
00053     data_len = (sint32)(Read4(ip)); // 32 bit lengths
00054 
00055   data = ip;
00056 
00057   ip += data_len;     // Point past text.
00058   num_args = Read2(ip); // # of args. this function takes.
00059 
00060   // Local variables follow args.
00061   num_vars = Read2(ip);
00062 
00063   // Allocate locals.
00064   int num_locals = num_vars + num_args;
00065   locals = new Usecode_value[num_locals];
00066 
00067   num_externs = Read2(ip); // external function references
00068   externs = ip;
00069 
00070   ip += 2 * num_externs; // now points to actual code
00071 
00072   code = ip;
00073 }
00074 
00075 Stack_frame::~Stack_frame()
00076 {
00077   delete[] locals;
00078 }
00079 
00080 std::ostream& operator<<(std::ostream& out, Stack_frame& frame)
00081 {
00082   // #depth: 0xIP in 0xfunid ( obj=..., event=..., arguments )
00083 
00084   // TODO: include any debugging info
00085   // #depth: 0xIP in functionname (obj=...,event=..., arg1name=..., ...)
00086 
00087   out << "#" << frame.call_depth << ": 0x" 
00088     << std::hex << std::setw(4) << std::setfill('0')
00089     << (int)(frame.ip - frame.code) << " in 0x"
00090     << std::setw(4) << frame.function->id
00091     << "(obj=" << std::setw(8) << (long)frame.caller_item
00092     << ",ev=" << frame.eventid
00093     << std::setfill(' ') << std::dec;
00094   
00095   for (int i=0; i < frame.num_args; i++)
00096     out << ", " << frame.locals[i];
00097 
00098   out << ")";
00099 
00100   return out;
00101 }

Generated on Mon Jul 9 14:42:52 2007 for ExultEngine by  doxygen 1.5.1