?? engine.cpp.svn-base
字號:
/* ENGINE.CPP
* The stackcode engine
* UnderC C++ interpreter
* Steve Donovan, 2001
* This is GPL'd software, and the usual disclaimers apply.
* See LICENCE
*/
#include "classlib.h"
#include <list>
#include <memory.h>
#include "stack.h"
#include "opcodes.h"
#include "directcall.h"
#include "hard_except.h"
#include "breakpoints.h"
#include "common.h"
#include "errors.h"
#include "program.h"
#include "loaded_module_list.h"
int lo_byte(int data)
{
return data & 0xFF;
}
int hi_byte(int data)
{
return (data & 0xFF00) >> 8;
}
// *change 1.2.2 Switch off ODS debugging by default in debug mode
#ifdef _DEBUG
//#define DEBUG_ODS
//#define TRACK_ODS
extern int gDebugBreak;
extern FBlock* gFunBlock;
void __break(int);
void check_fun_block(FBlock* fb)
{
if (gFunBlock == fb)
__break(3);
}
#else
#define check_fun_block(x)
#endif
// *change 1.2.3 We now by default put all global objects requiring destruction
// onto a static ODL, which is cleared at the end of the session.
// Uncomment the next line if you don't want this!
// #define NO_STATIC_ODS
// from common.cpp
void unpack_bitfield(int data, int& offs, int& bit_offs, int& bit_size);
CEXPORT char *sh_fun(); // debug function
// later in this file....
int current_ip();
void exec_message(char *msg, bool was_error, bool dont_unwind = false);
bool get_curent_exec_pos(int& ip_offs, string& fname, LineInfo& li);
int get_current_exec_line();
namespace Builtin {
PClass imported_class_from_function(void *pfn);
}
// useful macro for tracing...
#define OUT(v) cout << #v " = " << v << endl
const int ODS_STACKSIZE = 200, ODS_TEMP_BUFFSIZE = 400;
const int MAX_CODE_LEVELS = 2;
const int MAX_FUNCTION_DEPTH = 8000;
const int EXEC_STACKSIZE = 100000, OBJECT_STACKSIZE = 9000;
#define START_FB (FBlock *)0x0a
void
CodeGenerator::out(Instruction *is)
{
*pcode = *is;
pcode++;
NC++;
mTotal++;
}
void
CodeGenerator::emitc(int opc, RMode rm, int data)
//------------------------------------------
{
pcode->opcode = opc;
pcode->rmode = rm;
pcode->data = data;
pcode++;
NC++;
mTotal++;
}
int
CodeGenerator::total_instructions()
{ return mTotal; }
int
CodeGenerator::ip_offset()
{ return NC; } // *because it counts instructions!*
Instruction *
CodeGenerator::current_pi()
{ return pcode; }
Instruction *
CodeGenerator::last_pi()
{ return pcode-1; }
void
CodeGenerator::begin_code()
{
NC = 0;
}
void
CodeGenerator::backspace()
{
pcode--;
NC--;
mTotal--;
}
Instruction *
CodeGenerator::end_code()
//---------------------
{
if (NC) {
pcode->opcode = 0; NC++; // flag end-of-code!
Instruction *pi = new Instruction[NC];
memcpy(pi,_code_buff_,sizeof(Instruction)*NC);
pcode = _code_buff_;
NC = 0;
mTotal = 0; // *fix 1.1.2 Was not reset
return pi;
}
else return NULL;
}
// *add 1.2.3a Returns non-NULL ptr to instruction if this function
// has exactly one instruction
Instruction*
CodeGenerator::has_one_instruction(Function* pf)
{
Instruction* ip = pf->fun_block()->pstart;
// look past the RETx instruction to see the end-of-code marker
if (ip && (ip+2)->opcode == 0) return ip;
else return NULL;
}
Instruction *
CodeGenerator::instruction_with_pointer(int opcode, void *ptr)
{
typedef void *pvoid;
Instruction *pi = new Instruction();
pi->opcode = opcode;
pi->rmode = DIRECT;
pi->data = Parser::global().alloc(sizeof(pvoid),NULL);
*(pvoid *)Parser::global().addr(pi->data) = ptr;
return pi;
}
Instruction *
x_copy_code(Instruction *iptr)
{
Instruction *pi = iptr;
while (pi->opcode != 0) pi++;
int sz = ((int)pi - (int)iptr)/sizeof(Instruction);
pi = new Instruction[sz];
memcpy(pi,iptr,sizeof(Instruction)*sz);
return pi;
}
const int FUNCTION_STACK_DEPTH = 3*MAX_FUNCTION_DEPTH;
static Stack<void *,FUNCTION_STACK_DEPTH> fs;
char *mDataSeg;
static char *mDataPtr = mDataSeg;
void Engine::set_data_seg(void *ptr)
{ mDataSeg = (char *)ptr; }
// for reasons of efficiency, the execution stack
// is statically allocated. Fine if thread-safe isn't
// a piority.
// (note: I know this should be in an anonymous namespace,
// but the Visual C++ debugger can't see them then...until
// then!)
static bool ptr_check;
static int _stack_[EXEC_STACKSIZE];
static int *mSP = _stack_ + EXEC_STACKSIZE - 1;
static int *mEndStack = _stack_;
void reset_execution_stack()
{ // currently only called after stack overflow....
mSP = _stack_ + EXEC_STACKSIZE - 1;
}
// double-word stack operations
inline void push(int val) {
*(--mSP) = val;
}
inline int pop() {
return *(mSP++);
}
int popp() { // specialized pointer-checking pop!
int p = pop();
if (ptr_check) Builtin::bad_ptr_check((void *)p);
return p;
}
inline int& tos() {
return *mSP;
}
inline void drop() {
++mSP;
}
inline void pushf(float v) {
*((float *)--mSP) = v;
}
inline float popf() {
return *((float *)mSP++);
}
// quadword stack operations
inline void push2(double fval) {
mSP -= 2; *((double *)mSP) = fval;
}
inline double pop2() {
double tmp = *(double *)mSP; mSP += 2; return tmp;
}
inline double& tos2() {
return *(double *)mSP;
}
inline void drop2() {
mSP += 2;
}
int stack_depth() { return (int)_stack_ - (int)mSP; }
void stack_report(ostream& os,int *base)
{
os << "STACK: ";
if (base == NULL) base = _stack_;
if (base == mSP) { os << "(Empty)" << endl; return; }
while (base != mSP-1) {
os << *base++ << ' ';
}
os << '(' << *base << ')' << endl;
}
int Engine::retval()
{ return tos(); }
//----------------------- object-stack ------------------------------------
static Stack<char *,OBJECT_STACKSIZE> ostack;
static char *mOP;
// these functions define the Virtual Method Table structure of UC
inline PFBlock virtual_method_lookup(int slot)
{
return PFBlock( (*VMT(mOP))[slot] );
}
PFBlock virtual_method_imported_lookup(int slot)
{
// *add 1.2.0 Imported objects may have associated VMTs!
return PFBlock( (Class::find_VMT(mOP))[slot] );
}
PClass get_class_object(void *p)
{
// *change 1.2.0 In general, dynamic casts need to look in the VMT map
// because objects may have been imported without a VMT in the usual position
PPClass pc = Class::find_VMT(p);
if (pc) return pc[0];
else return (*VMT(p))[0];
}
PPClass& get_object_VMT(void* p)
{
return *VMT(p);
}
inline void opop()
{
mOP = ostack.pop();
}
void chk_ods()
{
if (ostack.depth() > OBJECT_STACKSIZE)
ostack.clear();
}
inline void opush(void *data)
{
if (ptr_check) Builtin::bad_ptr_check(data);
ostack.push(mOP);
mOP = (char *)data;
}
inline char *otos()
{
return ostack.TOS();
}
void Engine::object_ptr(void *data)
{
mOP = (char *)data;
}
void *Engine::object_ptr()
{
return mOP;
}
// potentially quite dubious - at the mo., only used so we can use JINC to move mOP
// for object array construction.
char **Engine::object_ptr_addr()
{
return &mOP;
}
//----------------------- THE ACTUAL ENGINE ---------------------------------
Instruction *ppcode;
FBlock *fb;
int *baseSP;
struct ExecutionState {
Instruction *m_ppcode;
FBlock* m_fb;
int *m_baseSP;
bool m_cntxt_pushed;
ArgBlock *m_xargs;
void save(Instruction *_ppcode, bool cpushed)
{
m_ppcode = _ppcode;
m_fb = fb;
m_baseSP = baseSP;
m_cntxt_pushed = cpushed;
}
void restore(bool& cpushed, ArgBlock *&args)
{
ppcode = m_ppcode;
fb = m_fb;
baseSP = m_baseSP;
cpushed = m_cntxt_pushed;
args = m_xargs;
}
};
ExecutionState resume_state;
const Instruction *end_of_code = (Instruction *)4;
//----------------------- object destructor stack ---------------------------
// *change 1.0.0 The ODS is now a stack of <class,object> records
// *change 1.2.3 The stack is now backed by a vector, so we don't get
// unpleasant suprises with large numbers of objects.
template <class T, int N>
class VStack {
private:
std::vector<T> m_vec;
public:
VStack()
{ m_vec.reserve(N); m_vec.resize(0);}
void push(const T& val)
{
m_vec.push_back(val);
}
T pop()
{
T val = m_vec.back();
m_vec.pop_back();
return val;
}
bool empty()
{
return m_vec.size() = 0;
}
void clear()
{
m_vec.resize(0);
}
int depth()
{
return m_vec.size();
}
T& get(int i)
{ return m_vec[i]; }
};
struct ODS_Record {
PClass m_class;
void *m_obj;
#ifdef DEBUG_ODS
Function *m_fn; //DEBUG fields
int m_offs;
#endif
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -