codeInterpreter.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CODEINTERPRETER_H_
  23. #define _CODEINTERPRETER_H_
  24. #include "console/codeBlock.h"
  25. #include "console/console.h"
  26. #include "console/consoleInternal.h"
  27. /// Frame data for a foreach/foreach$ loop.
  28. struct IterStackRecord
  29. {
  30. /// If true, this is a foreach$ loop; if not, it's a foreach loop.
  31. bool mIsStringIter;
  32. /// The iterator variable.
  33. Dictionary::Entry* mVariable;
  34. /// Information for an object iterator loop.
  35. struct ObjectPos
  36. {
  37. /// The set being iterated over.
  38. SimSet* mSet;
  39. /// Current index in the set.
  40. U32 mIndex;
  41. };
  42. /// Information for a string iterator loop.
  43. struct StringPos
  44. {
  45. /// The raw string data on the string stack.
  46. StringStackPtr mString;
  47. /// Current parsing position.
  48. U32 mIndex;
  49. };
  50. union
  51. {
  52. ObjectPos mObj;
  53. StringPos mStr;
  54. } mData;
  55. };
  56. enum OPCodeReturn
  57. {
  58. exitCode = -1,
  59. success = 0,
  60. breakContinue = 1
  61. };
  62. class CodeInterpreter
  63. {
  64. public:
  65. CodeInterpreter(CodeBlock *cb);
  66. ~CodeInterpreter();
  67. ConsoleValueRef exec(U32 ip,
  68. StringTableEntry functionName,
  69. Namespace *thisNamespace,
  70. U32 argc,
  71. ConsoleValueRef *argv,
  72. bool noCalls,
  73. StringTableEntry packageName,
  74. S32 setFrame);
  75. static void init();
  76. // Methods
  77. private:
  78. void parseArgs(U32 &ip);
  79. /// Group op codes
  80. /// @{
  81. OPCodeReturn op_func_decl(U32 &ip);
  82. OPCodeReturn op_create_object(U32 &ip);
  83. OPCodeReturn op_add_object(U32 &ip);
  84. OPCodeReturn op_end_object(U32 &ip);
  85. OPCodeReturn op_finish_object(U32 &ip);
  86. OPCodeReturn op_jmpiffnot(U32 &ip);
  87. OPCodeReturn op_jmpifnot(U32 &ip);
  88. OPCodeReturn op_jmpiff(U32 &ip);
  89. OPCodeReturn op_jmpif(U32 &ip);
  90. OPCodeReturn op_jmpifnot_np(U32 &ip);
  91. OPCodeReturn op_jmpif_np(U32 &ip);
  92. OPCodeReturn op_jmp(U32 &ip);
  93. OPCodeReturn op_return_void(U32 &ip);
  94. OPCodeReturn op_return(U32 &ip);
  95. OPCodeReturn op_return_flt(U32 &ip);
  96. OPCodeReturn op_return_uint(U32 &ip);
  97. OPCodeReturn op_cmpeq(U32 &ip);
  98. OPCodeReturn op_cmpgr(U32 &ip);
  99. OPCodeReturn op_cmpge(U32 &ip);
  100. OPCodeReturn op_cmplt(U32 &ip);
  101. OPCodeReturn op_cmple(U32 &ip);
  102. OPCodeReturn op_cmpne(U32 &ip);
  103. OPCodeReturn op_xor(U32 &ip);
  104. OPCodeReturn op_mod(U32 &ip);
  105. OPCodeReturn op_bitand(U32 &ip);
  106. OPCodeReturn op_bitor(U32 &ip);
  107. OPCodeReturn op_not(U32 &ip);
  108. OPCodeReturn op_notf(U32 &ip);
  109. OPCodeReturn op_onescomplement(U32 &ip);
  110. OPCodeReturn op_shr(U32 &ip);
  111. OPCodeReturn op_shl(U32 &ip);
  112. OPCodeReturn op_and(U32 &ip);
  113. OPCodeReturn op_or(U32 &ip);
  114. OPCodeReturn op_add(U32 &ip);
  115. OPCodeReturn op_sub(U32 &ip);
  116. OPCodeReturn op_mul(U32 &ip);
  117. OPCodeReturn op_div(U32 &ip);
  118. OPCodeReturn op_neg(U32 &ip);
  119. OPCodeReturn op_inc(U32 &ip);
  120. OPCodeReturn op_dec(U32 &ip);
  121. OPCodeReturn op_setcurvar(U32 &ip);
  122. OPCodeReturn op_setcurvar_create(U32 &ip);
  123. OPCodeReturn op_setcurvar_array(U32 &ip);
  124. OPCodeReturn op_setcurvar_array_varlookup(U32 &ip);
  125. OPCodeReturn op_setcurvar_array_create(U32 &ip);
  126. OPCodeReturn op_setcurvar_array_create_varlookup(U32 &ip);
  127. OPCodeReturn op_loadvar_uint(U32 &ip);
  128. OPCodeReturn op_loadvar_flt(U32 &ip);
  129. OPCodeReturn op_loadvar_str(U32 &ip);
  130. OPCodeReturn op_loadvar_var(U32 &ip);
  131. OPCodeReturn op_savevar_uint(U32 &ip);
  132. OPCodeReturn op_savevar_flt(U32 &ip);
  133. OPCodeReturn op_savevar_str(U32 &ip);
  134. OPCodeReturn op_savevar_var(U32 &ip);
  135. OPCodeReturn op_setcurobject(U32 &ip);
  136. OPCodeReturn op_setcurobject_internal(U32 &ip);
  137. OPCodeReturn op_setcurobject_new(U32 &ip);
  138. OPCodeReturn op_setcurfield(U32 &ip);
  139. OPCodeReturn op_setcurfield_array(U32 &ip);
  140. OPCodeReturn op_setcurfield_type(U32 &ip);
  141. OPCodeReturn op_setcurfield_this(U32 &ip);
  142. OPCodeReturn op_setcurfield_array_var(U32 &ip);
  143. OPCodeReturn op_loadfield_uint(U32 &ip);
  144. OPCodeReturn op_loadfield_flt(U32 &ip);
  145. OPCodeReturn op_loadfield_str(U32 &ip);
  146. OPCodeReturn op_savefield_uint(U32 &ip);
  147. OPCodeReturn op_savefield_flt(U32 &ip);
  148. OPCodeReturn op_savefield_str(U32 &ip);
  149. OPCodeReturn op_str_to_uint(U32 &ip);
  150. OPCodeReturn op_str_to_flt(U32 &ip);
  151. OPCodeReturn op_str_to_none(U32 &ip);
  152. OPCodeReturn op_flt_to_uint(U32 &ip);
  153. OPCodeReturn op_flt_to_str(U32 &ip);
  154. OPCodeReturn op_flt_to_none(U32 &ip);
  155. OPCodeReturn op_uint_to_flt(U32 &ip);
  156. OPCodeReturn op_uint_to_str(U32 &ip);
  157. OPCodeReturn op_uint_to_none(U32 &ip);
  158. OPCodeReturn op_copyvar_to_none(U32 &ip);
  159. OPCodeReturn op_loadimmed_uint(U32 &ip);
  160. OPCodeReturn op_loadimmed_flt(U32 &ip);
  161. OPCodeReturn op_tag_to_str(U32 &ip);
  162. OPCodeReturn op_loadimmed_str(U32 &ip);
  163. OPCodeReturn op_docblock_str(U32 &ip);
  164. OPCodeReturn op_loadimmed_ident(U32 &ip);
  165. OPCodeReturn op_callfunc_resolve(U32 &ip);
  166. OPCodeReturn op_callfunc(U32 &ip);
  167. OPCodeReturn op_callfunc_pointer(U32 &ip);
  168. OPCodeReturn op_callfunc_this(U32 &ip);
  169. OPCodeReturn op_advance_str(U32 &ip);
  170. OPCodeReturn op_advance_str_appendchar(U32 &ip);
  171. OPCodeReturn op_advance_str_comma(U32 &ip);
  172. OPCodeReturn op_advance_str_nul(U32 &ip);
  173. OPCodeReturn op_rewind_str(U32 &ip);
  174. OPCodeReturn op_terminate_rewind_str(U32 &ip);
  175. OPCodeReturn op_compare_str(U32 &ip);
  176. OPCodeReturn op_push(U32 &ip);
  177. OPCodeReturn op_push_uint(U32 &ip);
  178. OPCodeReturn op_push_flt(U32 &ip);
  179. OPCodeReturn op_push_var(U32 &ip);
  180. OPCodeReturn op_push_this(U32 &ip);
  181. OPCodeReturn op_push_frame(U32 &ip);
  182. OPCodeReturn op_assert(U32 &ip);
  183. OPCodeReturn op_break(U32 &ip);
  184. OPCodeReturn op_iter_begin_str(U32 &ip);
  185. OPCodeReturn op_iter_begin(U32 &ip);
  186. OPCodeReturn op_iter(U32 &ip);
  187. OPCodeReturn op_iter_end(U32 &ip);
  188. OPCodeReturn op_invalid(U32 &ip);
  189. /// @}
  190. private:
  191. CodeBlock *mCodeBlock;
  192. /// Group exec arguments.
  193. struct
  194. {
  195. StringTableEntry functionName;
  196. Namespace *thisNamespace;
  197. U32 argc;
  198. ConsoleValueRef *argv;
  199. bool noCalls;
  200. StringTableEntry packageName;
  201. S32 setFrame;
  202. } mExec;
  203. U32 mIterDepth;
  204. F64 *mCurFloatTable;
  205. char *mCurStringTable;
  206. StringTableEntry mThisFunctionName;
  207. bool mPopFrame;
  208. // Add local object creation stack [7/9/2007 Black]
  209. static const U32 objectCreationStackSize = 32;
  210. U32 mObjectCreationStackIndex;
  211. struct
  212. {
  213. SimObject *newObject;
  214. U32 failJump;
  215. } mObjectCreationStack[objectCreationStackSize];
  216. SimObject *mCurrentNewObject;
  217. U32 mFailJump;
  218. StringTableEntry mPrevField;
  219. StringTableEntry mCurField;
  220. SimObject *mPrevObject;
  221. SimObject *mCurObject;
  222. SimObject *mSaveObject;
  223. SimObject *mThisObject;
  224. Namespace::Entry *mNSEntry;
  225. StringTableEntry mCurFNDocBlock;
  226. StringTableEntry mCurNSDocBlock;
  227. U32 mCallArgc;
  228. ConsoleValueRef *mCallArgv;
  229. CodeBlock *mSaveCodeBlock;
  230. // note: anything returned is pushed to CSTK and will be invalidated on the next exec()
  231. ConsoleValueRef mReturnValue;
  232. U32 mCurrentInstruction;
  233. static const S32 nsDocLength = 128;
  234. char mNSDocBlockClass[nsDocLength];
  235. };
  236. #endif