sqvm.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* see copyright notice in squirrel.h */
  2. #ifndef _SQVM_H_
  3. #define _SQVM_H_
  4. #include "sqopcodes.h"
  5. #include "sqobject.h"
  6. #define MAX_NATIVE_CALLS 100
  7. #define MIN_STACK_OVERHEAD 15
  8. #define SQ_SUSPEND_FLAG -666
  9. #define DONT_FALL_BACK 666
  10. //base lib
  11. void sq_base_register(HSQUIRRELVM v);
  12. struct SQExceptionTrap{
  13. SQExceptionTrap() {}
  14. SQExceptionTrap(SQInteger ss, SQInteger stackbase,SQInstruction *ip, SQInteger ex_target){ _stacksize = ss; _stackbase = stackbase; _ip = ip; _extarget = ex_target;}
  15. SQExceptionTrap(const SQExceptionTrap &et) { (*this) = et; }
  16. SQInteger _stackbase;
  17. SQInteger _stacksize;
  18. SQInstruction *_ip;
  19. SQInteger _extarget;
  20. };
  21. #define _INLINE
  22. #define STK(a) _stack._vals[_stackbase+(a)]
  23. #define TARGET _stack._vals[_stackbase+arg0]
  24. typedef sqvector<SQExceptionTrap> ExceptionsTraps;
  25. struct SQVM : public CHAINABLE_OBJ
  26. {
  27. struct CallInfo{
  28. //CallInfo() { _generator = NULL;}
  29. SQInstruction *_ip;
  30. SQObjectPtr *_literals;
  31. SQObjectPtr _closure;
  32. SQGenerator *_generator;
  33. SQInt32 _etraps;
  34. SQInt32 _prevstkbase;
  35. SQInt32 _prevtop;
  36. SQInt32 _target;
  37. SQInt32 _ncalls;
  38. SQBool _root;
  39. };
  40. typedef sqvector<CallInfo> CallInfoVec;
  41. public:
  42. void DebugHookProxy(SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
  43. static void _DebugHookProxy(HSQUIRRELVM v, SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
  44. enum ExecutionType { ET_CALL, ET_RESUME_GENERATOR, ET_RESUME_VM,ET_RESUME_THROW_VM };
  45. SQVM(SQSharedState *ss);
  46. ~SQVM();
  47. bool Init(SQVM *friendvm, SQInteger stacksize);
  48. bool Execute(SQObjectPtr &func, SQInteger nargs, SQInteger stackbase, SQObjectPtr &outres, SQBool raiseerror, ExecutionType et = ET_CALL);
  49. //starts a native call return when the NATIVE closure returns
  50. bool CallNative(SQNativeClosure *nclosure, SQInteger nargs, SQInteger newbase, SQObjectPtr &retval,bool &suspend);
  51. //starts a SQUIRREL call in the same "Execution loop"
  52. bool StartCall(SQClosure *closure, SQInteger target, SQInteger nargs, SQInteger stackbase, bool tailcall);
  53. bool CreateClassInstance(SQClass *theclass, SQObjectPtr &inst, SQObjectPtr &constructor);
  54. //call a generic closure pure SQUIRREL or NATIVE
  55. bool Call(SQObjectPtr &closure, SQInteger nparams, SQInteger stackbase, SQObjectPtr &outres,SQBool raiseerror);
  56. SQRESULT Suspend();
  57. void CallDebugHook(SQInteger type,SQInteger forcedline=0);
  58. void CallErrorHandler(SQObjectPtr &e);
  59. bool Get(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &dest, bool raw, SQInteger selfidx);
  60. SQInteger FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
  61. bool InvokeDefaultDelegate(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
  62. bool Set(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val, SQInteger selfidx);
  63. SQInteger FallBackSet(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val);
  64. bool NewSlot(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val,bool bstatic);
  65. bool NewSlotA(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val,const SQObjectPtr &attrs,bool bstatic,bool raw);
  66. bool DeleteSlot(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &res);
  67. bool Clone(const SQObjectPtr &self, SQObjectPtr &target);
  68. bool ObjCmp(const SQObjectPtr &o1, const SQObjectPtr &o2,SQInteger &res);
  69. bool StringCat(const SQObjectPtr &str, const SQObjectPtr &obj, SQObjectPtr &dest);
  70. static bool IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2,bool &res);
  71. bool ToString(const SQObjectPtr &o,SQObjectPtr &res);
  72. SQString *PrintObjVal(const SQObjectPtr &o);
  73. void Raise_Error(const SQChar *s, ...);
  74. void Raise_Error(const SQObjectPtr &desc);
  75. void Raise_IdxError(const SQObjectPtr &o);
  76. void Raise_CompareError(const SQObject &o1, const SQObject &o2);
  77. void Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type);
  78. void FindOuter(SQObjectPtr &target, SQObjectPtr *stackindex);
  79. void RelocateOuters();
  80. void CloseOuters(SQObjectPtr *stackindex);
  81. bool TypeOf(const SQObjectPtr &obj1, SQObjectPtr &dest);
  82. bool CallMetaMethod(SQObjectPtr &closure, SQMetaMethod mm, SQInteger nparams, SQObjectPtr &outres);
  83. bool ArithMetaMethod(SQInteger op, const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
  84. bool Return(SQInteger _arg0, SQInteger _arg1, SQObjectPtr &retval);
  85. //new stuff
  86. _INLINE bool ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
  87. _INLINE bool BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
  88. _INLINE bool NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o1);
  89. _INLINE bool CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res);
  90. bool CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func);
  91. bool CLASS_OP(SQObjectPtr &target,SQInteger base,SQInteger attrs);
  92. //return true if the loop is finished
  93. bool FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr &o3,SQObjectPtr &o4,SQInteger arg_2,int exitpos,int &jump);
  94. //_INLINE bool LOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
  95. _INLINE bool PLOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
  96. _INLINE bool DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix,SQInteger arg0);
  97. #ifdef _DEBUG_DUMP
  98. void dumpstack(SQInteger stackbase=-1, bool dumpall = false);
  99. #endif
  100. #ifndef NO_GARBAGE_COLLECTOR
  101. void Mark(SQCollectable **chain);
  102. SQObjectType GetType() {return OT_THREAD;}
  103. #endif
  104. void Finalize();
  105. void GrowCallStack() {
  106. SQInteger newsize = _alloccallsstacksize*2;
  107. _callstackdata.resize(newsize);
  108. _callsstack = &_callstackdata[0];
  109. _alloccallsstacksize = newsize;
  110. }
  111. bool EnterFrame(SQInteger newbase, SQInteger newtop, bool tailcall);
  112. void LeaveFrame();
  113. void Release(){ sq_delete(this,SQVM); }
  114. ////////////////////////////////////////////////////////////////////////////
  115. //stack functions for the api
  116. void Remove(SQInteger n);
  117. void Replace(SQInteger n);
  118. void ReplaceAbs(SQInteger n);
  119. void Insert(SQInteger n);
  120. static bool IsFalse(SQObjectPtr &o);
  121. void Pop();
  122. void Pop(SQInteger n);
  123. void Push(const SQObjectPtr &o);
  124. void PushNull();
  125. SQObjectPtr &Top();
  126. SQObjectPtr &PopGet();
  127. SQObjectPtr &GetUp(SQInteger n);
  128. SQObjectPtr &GetAt(SQInteger n);
  129. SQObjectPtrVec _stack;
  130. SQInteger _top;
  131. SQInteger _stackbase;
  132. SQOuter *_openouters;
  133. SQObjectPtr _roottable;
  134. SQObjectPtr _lasterror;
  135. SQObjectPtr _errorhandler;
  136. bool _debughook;
  137. SQDEBUGHOOK _debughook_native;
  138. SQObjectPtr _debughook_closure;
  139. SQObjectPtr temp_reg;
  140. CallInfo* _callsstack;
  141. SQInteger _callsstacksize;
  142. SQInteger _alloccallsstacksize;
  143. sqvector<CallInfo> _callstackdata;
  144. ExceptionsTraps _etraps;
  145. CallInfo *ci;
  146. void *_foreignptr;
  147. //VMs sharing the same state
  148. SQSharedState *_sharedstate;
  149. SQInteger _nnativecalls;
  150. SQInteger _nmetamethodscall;
  151. //suspend infos
  152. SQBool _suspended;
  153. SQBool _suspended_root;
  154. SQInteger _suspended_target;
  155. SQInteger _suspended_traps;
  156. };
  157. struct AutoDec{
  158. AutoDec(SQInteger *n) { _n = n; }
  159. ~AutoDec() { (*_n)--; }
  160. SQInteger *_n;
  161. };
  162. inline SQObjectPtr &stack_get(HSQUIRRELVM v,SQInteger idx){return ((idx>=0)?(v->GetAt(idx+v->_stackbase-1)):(v->GetUp(idx)));}
  163. #define _ss(_vm_) (_vm_)->_sharedstate
  164. #ifndef NO_GARBAGE_COLLECTOR
  165. #define _opt_ss(_vm_) (_vm_)->_sharedstate
  166. #else
  167. #define _opt_ss(_vm_) NULL
  168. #endif
  169. #define PUSH_CALLINFO(v,nci){ \
  170. SQInteger css = v->_callsstacksize; \
  171. if(css == v->_alloccallsstacksize) { \
  172. v->GrowCallStack(); \
  173. } \
  174. v->ci = &v->_callsstack[css]; \
  175. *(v->ci) = nci; \
  176. v->_callsstacksize++; \
  177. }
  178. #define POP_CALLINFO(v){ \
  179. SQInteger css = --v->_callsstacksize; \
  180. v->ci->_closure.Null(); \
  181. v->ci = css?&v->_callsstack[css-1]:NULL; \
  182. }
  183. #endif //_SQVM_H_