sqvm.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. void CallAtExitHandler();
  60. bool Get(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &dest, bool raw, SQInteger selfidx);
  61. SQInteger FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
  62. bool InvokeDefaultDelegate(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
  63. bool Set(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val, SQInteger selfidx);
  64. SQInteger FallBackSet(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val);
  65. bool NewSlot(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val,bool bstatic);
  66. bool NewSlotA(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val,const SQObjectPtr &attrs,bool bstatic,bool raw);
  67. bool DeleteSlot(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &res);
  68. bool Clone(const SQObjectPtr &self, SQObjectPtr &target);
  69. bool ObjCmp(const SQObjectPtr &o1, const SQObjectPtr &o2,SQInteger &res);
  70. bool StringCat(const SQObjectPtr &str, const SQObjectPtr &obj, SQObjectPtr &dest);
  71. static bool IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2,bool &res);
  72. bool ToString(const SQObjectPtr &o,SQObjectPtr &res);
  73. SQString *PrintObjVal(const SQObjectPtr &o);
  74. void Raise_Error(const SQChar *s, ...);
  75. void Raise_Error(const SQObjectPtr &desc);
  76. void Raise_IdxError(const SQObjectPtr &o);
  77. void Raise_CompareError(const SQObject &o1, const SQObject &o2);
  78. void Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type);
  79. void FindOuter(SQObjectPtr &target, SQObjectPtr *stackindex);
  80. void RelocateOuters();
  81. void CloseOuters(SQObjectPtr *stackindex);
  82. bool TypeOf(const SQObjectPtr &obj1, SQObjectPtr &dest);
  83. bool CallMetaMethod(SQObjectPtr &closure, SQMetaMethod mm, SQInteger nparams, SQObjectPtr &outres);
  84. bool ArithMetaMethod(SQInteger op, const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
  85. bool Return(SQInteger _arg0, SQInteger _arg1, SQObjectPtr &retval);
  86. //new stuff
  87. _INLINE bool ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
  88. _INLINE bool BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
  89. _INLINE bool NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o1);
  90. _INLINE bool CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res);
  91. bool CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func);
  92. bool CLASS_OP(SQObjectPtr &target,SQInteger base,SQInteger attrs);
  93. //return true if the loop is finished
  94. bool FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr &o3,SQObjectPtr &o4,SQInteger arg_2,int exitpos,int &jump);
  95. //_INLINE bool LOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
  96. _INLINE bool PLOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
  97. _INLINE bool DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix,SQInteger arg0);
  98. #ifdef _DEBUG_DUMP
  99. void dumpstack(SQInteger stackbase=-1, bool dumpall = false);
  100. #endif
  101. #ifndef NO_GARBAGE_COLLECTOR
  102. void Mark(SQCollectable **chain);
  103. SQObjectType GetType() {return OT_THREAD;}
  104. #endif
  105. void Finalize();
  106. void GrowCallStack() {
  107. SQInteger newsize = _alloccallsstacksize*2;
  108. _callstackdata.resize(newsize);
  109. _callsstack = &_callstackdata[0];
  110. _alloccallsstacksize = newsize;
  111. }
  112. bool EnterFrame(SQInteger newbase, SQInteger newtop, bool tailcall);
  113. void LeaveFrame();
  114. void Release(){ sq_delete(this,SQVM); }
  115. ////////////////////////////////////////////////////////////////////////////
  116. //stack functions for the api
  117. void Remove(SQInteger n);
  118. void Replace(SQInteger n);
  119. void ReplaceAbs(SQInteger n);
  120. void Insert(SQInteger n);
  121. static bool IsFalse(SQObjectPtr &o);
  122. void Pop();
  123. void Pop(SQInteger n);
  124. void Push(const SQObjectPtr &o);
  125. void PushNull();
  126. SQObjectPtr &Top();
  127. SQObjectPtr &PopGet();
  128. SQObjectPtr &GetUp(SQInteger n);
  129. SQObjectPtr &GetAt(SQInteger n);
  130. SQObjectPtrVec _stack;
  131. SQInteger _top;
  132. SQInteger _stackbase;
  133. SQOuter *_openouters;
  134. SQObjectPtr _roottable;
  135. SQObjectPtr _lasterror;
  136. SQStackInfos _lasterror_stackinfo;
  137. SQObjectPtr _errorhandler;
  138. SQObjectPtr _atexithandler;
  139. bool _debughook;
  140. SQDEBUGHOOK _debughook_native;
  141. SQObjectPtr _debughook_closure;
  142. SQObjectPtr temp_reg;
  143. CallInfo* _callsstack;
  144. SQInteger _callsstacksize;
  145. SQInteger _alloccallsstacksize;
  146. sqvector<CallInfo> _callstackdata;
  147. ExceptionsTraps _etraps;
  148. CallInfo *ci;
  149. void *_foreignptr;
  150. //VMs sharing the same state
  151. SQSharedState *_sharedstate;
  152. SQInteger _nnativecalls;
  153. SQInteger _nmetamethodscall;
  154. //suspend infos
  155. SQBool _suspended;
  156. SQBool _suspended_root;
  157. SQInteger _suspended_target;
  158. SQInteger _suspended_traps;
  159. };
  160. struct AutoDec{
  161. AutoDec(SQInteger *n) { _n = n; }
  162. ~AutoDec() { (*_n)--; }
  163. SQInteger *_n;
  164. };
  165. inline SQObjectPtr &stack_get(HSQUIRRELVM v,SQInteger idx){return ((idx>=0)?(v->GetAt(idx+v->_stackbase-1)):(v->GetUp(idx)));}
  166. #define _ss(_vm_) (_vm_)->_sharedstate
  167. #ifndef NO_GARBAGE_COLLECTOR
  168. #define _opt_ss(_vm_) (_vm_)->_sharedstate
  169. #else
  170. #define _opt_ss(_vm_) NULL
  171. #endif
  172. #define PUSH_CALLINFO(v,nci){ \
  173. SQInteger css = v->_callsstacksize; \
  174. if(css == v->_alloccallsstacksize) { \
  175. v->GrowCallStack(); \
  176. } \
  177. v->ci = &v->_callsstack[css]; \
  178. *(v->ci) = nci; \
  179. v->_callsstacksize++; \
  180. }
  181. #define POP_CALLINFO(v){ \
  182. SQInteger css = --v->_callsstacksize; \
  183. v->ci->_closure.Null(); \
  184. v->ci = css?&v->_callsstack[css-1]:NULL; \
  185. }
  186. #endif //_SQVM_H_