/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsopcode.h

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · C++ Header · 404 lines · 220 code · 51 blank · 133 comment · 1 complexity · 14b7bd836bf6873003a73a62c2cf0d55 MD5 · raw file

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Mozilla Communicator client code, released
  17. * March 31, 1998.
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Netscape Communications Corporation.
  21. * Portions created by the Initial Developer are Copyright (C) 1998
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either of the GNU General Public License Version 2 or later (the "GPL"),
  28. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. #ifndef jsopcode_h___
  40. #define jsopcode_h___
  41. /*
  42. * JS bytecode definitions.
  43. */
  44. #include <stddef.h>
  45. #include "jsprvtd.h"
  46. #include "jspubtd.h"
  47. #include "jsutil.h"
  48. JS_BEGIN_EXTERN_C
  49. /*
  50. * JS operation bytecodes.
  51. */
  52. typedef enum JSOp {
  53. #define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
  54. op = val,
  55. #include "jsopcode.tbl"
  56. #undef OPDEF
  57. JSOP_LIMIT
  58. } JSOp;
  59. /*
  60. * JS bytecode formats.
  61. */
  62. #define JOF_BYTE 0 /* single bytecode, no immediates */
  63. #define JOF_JUMP 1 /* signed 16-bit jump offset immediate */
  64. #define JOF_ATOM 2 /* unsigned 16-bit constant pool index */
  65. #define JOF_UINT16 3 /* unsigned 16-bit immediate operand */
  66. #define JOF_TABLESWITCH 4 /* table switch */
  67. #define JOF_LOOKUPSWITCH 5 /* lookup switch */
  68. #define JOF_QARG 6 /* quickened get/set function argument ops */
  69. #define JOF_LOCAL 7 /* var or block-local variable */
  70. #define JOF_SLOTATOM 8 /* uint16 slot index + constant pool index */
  71. #define JOF_JUMPX 9 /* signed 32-bit jump offset immediate */
  72. #define JOF_TABLESWITCHX 10 /* extended (32-bit offset) table switch */
  73. #define JOF_LOOKUPSWITCHX 11 /* extended (32-bit offset) lookup switch */
  74. #define JOF_UINT24 12 /* extended unsigned 24-bit literal (index) */
  75. #define JOF_UINT8 13 /* uint8 immediate, e.g. top 8 bits of 24-bit
  76. atom index */
  77. #define JOF_INT32 14 /* int32 immediate operand */
  78. #define JOF_OBJECT 15 /* unsigned 16-bit object pool index */
  79. #define JOF_SLOTOBJECT 16 /* uint16 slot index + object pool index */
  80. #define JOF_REGEXP 17 /* unsigned 16-bit regexp pool index */
  81. #define JOF_INT8 18 /* int8 immediate operand */
  82. #define JOF_TYPEMASK 0x001f /* mask for above immediate types */
  83. #define JOF_NAME (1U<<5) /* name operation */
  84. #define JOF_PROP (2U<<5) /* obj.prop operation */
  85. #define JOF_ELEM (3U<<5) /* obj[index] operation */
  86. #define JOF_XMLNAME (4U<<5) /* XML name: *, a::b, @a, @a::b, etc. */
  87. #define JOF_VARPROP (5U<<5) /* x.prop for this, arg, var, or local x */
  88. #define JOF_MODEMASK (7U<<5) /* mask for above addressing modes */
  89. #define JOF_SET (1U<<8) /* set (i.e., assignment) operation */
  90. #define JOF_DEL (1U<<9) /* delete operation */
  91. #define JOF_DEC (1U<<10) /* decrement (--, not ++) opcode */
  92. #define JOF_INC (2U<<10) /* increment (++, not --) opcode */
  93. #define JOF_INCDEC (3U<<10) /* increment or decrement opcode */
  94. #define JOF_POST (1U<<12) /* postorder increment or decrement */
  95. #define JOF_FOR (1U<<13) /* for-in property op (akin to JOF_SET) */
  96. #define JOF_ASSIGNING JOF_SET /* hint for JSClass.resolve, used for ops
  97. that do simplex assignment */
  98. #define JOF_DETECTING (1U<<14) /* object detection for JSNewResolveOp */
  99. #define JOF_BACKPATCH (1U<<15) /* backpatch placeholder during codegen */
  100. #define JOF_LEFTASSOC (1U<<16) /* left-associative operator */
  101. #define JOF_DECLARING (1U<<17) /* var, const, or function declaration op */
  102. #define JOF_INDEXBASE (1U<<18) /* atom segment base setting prefix op */
  103. #define JOF_CALLOP (1U<<19) /* call operation that pushes function and
  104. this */
  105. #define JOF_PARENHEAD (1U<<20) /* opcode consumes value of expression in
  106. parenthesized statement head */
  107. #define JOF_INVOKE (1U<<21) /* JSOP_CALL, JSOP_NEW, JSOP_EVAL */
  108. #define JOF_TMPSLOT (1U<<22) /* interpreter uses extra temporary slot
  109. to root intermediate objects besides
  110. the slots opcode uses */
  111. #define JOF_TMPSLOT2 (2U<<22) /* interpreter uses extra 2 temporary slot
  112. besides the slots opcode uses */
  113. #define JOF_TMPSLOT_SHIFT 22
  114. #define JOF_TMPSLOT_MASK (JS_BITMASK(2) << JOF_TMPSLOT_SHIFT)
  115. /* Shorthands for type from format and type from opcode. */
  116. #define JOF_TYPE(fmt) ((fmt) & JOF_TYPEMASK)
  117. #define JOF_OPTYPE(op) JOF_TYPE(js_CodeSpec[op].format)
  118. /* Shorthands for mode from format and mode from opcode. */
  119. #define JOF_MODE(fmt) ((fmt) & JOF_MODEMASK)
  120. #define JOF_OPMODE(op) JOF_MODE(js_CodeSpec[op].format)
  121. #define JOF_TYPE_IS_EXTENDED_JUMP(t) \
  122. ((unsigned)((t) - JOF_JUMPX) <= (unsigned)(JOF_LOOKUPSWITCHX - JOF_JUMPX))
  123. /*
  124. * Immediate operand getters, setters, and bounds.
  125. */
  126. /* Common uint16 immediate format helpers. */
  127. #define UINT16_LEN 2
  128. #define UINT16_HI(i) ((jsbytecode)((i) >> 8))
  129. #define UINT16_LO(i) ((jsbytecode)(i))
  130. #define GET_UINT16(pc) ((uintN)(((pc)[1] << 8) | (pc)[2]))
  131. #define SET_UINT16(pc,i) ((pc)[1] = UINT16_HI(i), (pc)[2] = UINT16_LO(i))
  132. #define UINT16_LIMIT ((uintN)1 << 16)
  133. /* Short (2-byte signed offset) relative jump macros. */
  134. #define JUMP_OFFSET_LEN 2
  135. #define JUMP_OFFSET_HI(off) ((jsbytecode)((off) >> 8))
  136. #define JUMP_OFFSET_LO(off) ((jsbytecode)(off))
  137. #define GET_JUMP_OFFSET(pc) ((int16)GET_UINT16(pc))
  138. #define SET_JUMP_OFFSET(pc,off) ((pc)[1] = JUMP_OFFSET_HI(off), \
  139. (pc)[2] = JUMP_OFFSET_LO(off))
  140. #define JUMP_OFFSET_MIN ((int16)0x8000)
  141. #define JUMP_OFFSET_MAX ((int16)0x7fff)
  142. /*
  143. * When a short jump won't hold a relative offset, its 2-byte immediate offset
  144. * operand is an unsigned index of a span-dependency record, maintained until
  145. * code generation finishes -- after which some (but we hope not nearly all)
  146. * span-dependent jumps must be extended (see OptimizeSpanDeps in jsemit.c).
  147. *
  148. * If the span-dependency record index overflows SPANDEP_INDEX_MAX, the jump
  149. * offset will contain SPANDEP_INDEX_HUGE, indicating that the record must be
  150. * found (via binary search) by its "before span-dependency optimization" pc
  151. * offset (from script main entry point).
  152. */
  153. #define GET_SPANDEP_INDEX(pc) ((uint16)GET_UINT16(pc))
  154. #define SET_SPANDEP_INDEX(pc,i) ((pc)[1] = JUMP_OFFSET_HI(i), \
  155. (pc)[2] = JUMP_OFFSET_LO(i))
  156. #define SPANDEP_INDEX_MAX ((uint16)0xfffe)
  157. #define SPANDEP_INDEX_HUGE ((uint16)0xffff)
  158. /* Ultimately, if short jumps won't do, emit long (4-byte signed) offsets. */
  159. #define JUMPX_OFFSET_LEN 4
  160. #define JUMPX_OFFSET_B3(off) ((jsbytecode)((off) >> 24))
  161. #define JUMPX_OFFSET_B2(off) ((jsbytecode)((off) >> 16))
  162. #define JUMPX_OFFSET_B1(off) ((jsbytecode)((off) >> 8))
  163. #define JUMPX_OFFSET_B0(off) ((jsbytecode)(off))
  164. #define GET_JUMPX_OFFSET(pc) ((int32)(((pc)[1] << 24) | ((pc)[2] << 16) \
  165. | ((pc)[3] << 8) | (pc)[4]))
  166. #define SET_JUMPX_OFFSET(pc,off)((pc)[1] = JUMPX_OFFSET_B3(off), \
  167. (pc)[2] = JUMPX_OFFSET_B2(off), \
  168. (pc)[3] = JUMPX_OFFSET_B1(off), \
  169. (pc)[4] = JUMPX_OFFSET_B0(off))
  170. #define JUMPX_OFFSET_MIN ((int32)0x80000000)
  171. #define JUMPX_OFFSET_MAX ((int32)0x7fffffff)
  172. /*
  173. * A literal is indexed by a per-script atom or object maps. Most scripts
  174. * have relatively few literals, so the standard JOF_ATOM, JOF_OBJECT and
  175. * JOF_REGEXP formats specifies a fixed 16 bits of immediate operand index.
  176. * A script with more than 64K literals must wrap the bytecode into
  177. * JSOP_INDEXBASE and JSOP_RESETBASE pair.
  178. */
  179. #define INDEX_LEN 2
  180. #define INDEX_HI(i) ((jsbytecode)((i) >> 8))
  181. #define INDEX_LO(i) ((jsbytecode)(i))
  182. #define GET_INDEX(pc) GET_UINT16(pc)
  183. #define SET_INDEX(pc,i) ((pc)[1] = INDEX_HI(i), (pc)[2] = INDEX_LO(i))
  184. #define GET_INDEXBASE(pc) (JS_ASSERT(*(pc) == JSOP_INDEXBASE), \
  185. ((uintN)((pc)[1])) << 16)
  186. #define INDEXBASE_LEN 1
  187. #define UINT24_HI(i) ((jsbytecode)((i) >> 16))
  188. #define UINT24_MID(i) ((jsbytecode)((i) >> 8))
  189. #define UINT24_LO(i) ((jsbytecode)(i))
  190. #define GET_UINT24(pc) ((jsatomid)(((pc)[1] << 16) | \
  191. ((pc)[2] << 8) | \
  192. (pc)[3]))
  193. #define SET_UINT24(pc,i) ((pc)[1] = UINT24_HI(i), \
  194. (pc)[2] = UINT24_MID(i), \
  195. (pc)[3] = UINT24_LO(i))
  196. #define GET_INT8(pc) ((jsint)(int8)(pc)[1])
  197. #define GET_INT32(pc) ((jsint)(((uint32)((pc)[1]) << 24) | \
  198. ((uint32)((pc)[2]) << 16) | \
  199. ((uint32)((pc)[3]) << 8) | \
  200. (uint32)(pc)[4]))
  201. #define SET_INT32(pc,i) ((pc)[1] = (jsbytecode)((uint32)(i) >> 24), \
  202. (pc)[2] = (jsbytecode)((uint32)(i) >> 16), \
  203. (pc)[3] = (jsbytecode)((uint32)(i) >> 8), \
  204. (pc)[4] = (jsbytecode)(uint32)(i))
  205. /* Index limit is determined by SN_3BYTE_OFFSET_FLAG, see jsemit.h. */
  206. #define INDEX_LIMIT_LOG2 23
  207. #define INDEX_LIMIT ((uint32)1 << INDEX_LIMIT_LOG2)
  208. JS_STATIC_ASSERT(sizeof(uint32) * JS_BITS_PER_BYTE >= INDEX_LIMIT_LOG2 + 1);
  209. /* Actual argument count operand format helpers. */
  210. #define ARGC_HI(argc) UINT16_HI(argc)
  211. #define ARGC_LO(argc) UINT16_LO(argc)
  212. #define GET_ARGC(pc) GET_UINT16(pc)
  213. #define ARGC_LIMIT UINT16_LIMIT
  214. /* Synonyms for quick JOF_QARG and JOF_LOCAL bytecodes. */
  215. #define GET_ARGNO(pc) GET_UINT16(pc)
  216. #define SET_ARGNO(pc,argno) SET_UINT16(pc,argno)
  217. #define ARGNO_LEN 2
  218. #define ARGNO_LIMIT UINT16_LIMIT
  219. #define GET_SLOTNO(pc) GET_UINT16(pc)
  220. #define SET_SLOTNO(pc,varno) SET_UINT16(pc,varno)
  221. #define SLOTNO_LEN 2
  222. #define SLOTNO_LIMIT UINT16_LIMIT
  223. struct JSCodeSpec {
  224. int8 length; /* length including opcode byte */
  225. int8 nuses; /* arity, -1 if variadic */
  226. int8 ndefs; /* number of stack results */
  227. uint8 prec; /* operator precedence */
  228. uint32 format; /* immediate operand format */
  229. };
  230. extern const JSCodeSpec js_CodeSpec[];
  231. extern uintN js_NumCodeSpecs;
  232. extern const char *js_CodeName[];
  233. extern const char js_EscapeMap[];
  234. /*
  235. * Return a GC'ed string containing the chars in str, with any non-printing
  236. * chars or quotes (' or " as specified by the quote argument) escaped, and
  237. * with the quote character at the beginning and end of the result string.
  238. */
  239. extern JSString *
  240. js_QuoteString(JSContext *cx, JSString *str, jschar quote);
  241. /*
  242. * JSPrinter operations, for printf style message formatting. The return
  243. * value from js_GetPrinterOutput() is the printer's cumulative output, in
  244. * a GC'ed string.
  245. */
  246. #ifdef JS_ARENAMETER
  247. # define JS_NEW_PRINTER(cx, name, fun, indent, pretty) \
  248. js_NewPrinter(cx, name, fun, indent, pretty)
  249. #else
  250. # define JS_NEW_PRINTER(cx, name, fun, indent, pretty) \
  251. js_NewPrinter(cx, fun, indent, pretty)
  252. #endif
  253. extern JSPrinter *
  254. JS_NEW_PRINTER(JSContext *cx, const char *name, JSFunction *fun,
  255. uintN indent, JSBool pretty);
  256. extern void
  257. js_DestroyPrinter(JSPrinter *jp);
  258. extern JSString *
  259. js_GetPrinterOutput(JSPrinter *jp);
  260. extern int
  261. js_printf(JSPrinter *jp, const char *format, ...);
  262. extern JSBool
  263. js_puts(JSPrinter *jp, const char *s);
  264. /*
  265. * Get index operand from the bytecode using a bytecode analysis to deduce the
  266. * the index register. This function is infallible, in spite of taking cx as
  267. * its first parameter; it uses only cx->runtime when calling JS_GetTrapOpcode.
  268. * The GET_*_FROM_BYTECODE macros that call it pick up cx from their caller's
  269. * lexical environments.
  270. */
  271. uintN
  272. js_GetIndexFromBytecode(JSContext *cx, JSScript *script, jsbytecode *pc,
  273. ptrdiff_t pcoff);
  274. /*
  275. * A slower version of GET_ATOM when the caller does not want to maintain
  276. * the index segment register itself.
  277. */
  278. #define GET_ATOM_FROM_BYTECODE(script, pc, pcoff, atom) \
  279. JS_BEGIN_MACRO \
  280. uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
  281. JS_GET_SCRIPT_ATOM((script), index_, atom); \
  282. JS_END_MACRO
  283. #define GET_OBJECT_FROM_BYTECODE(script, pc, pcoff, obj) \
  284. JS_BEGIN_MACRO \
  285. uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
  286. JS_GET_SCRIPT_OBJECT((script), index_, obj); \
  287. JS_END_MACRO
  288. #define GET_FUNCTION_FROM_BYTECODE(script, pc, pcoff, fun) \
  289. JS_BEGIN_MACRO \
  290. uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
  291. JS_GET_SCRIPT_FUNCTION((script), index_, fun); \
  292. JS_END_MACRO
  293. #define GET_REGEXP_FROM_BYTECODE(script, pc, pcoff, obj) \
  294. JS_BEGIN_MACRO \
  295. uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
  296. JS_GET_SCRIPT_REGEXP((script), index_, obj); \
  297. JS_END_MACRO
  298. /*
  299. * Get the length of variable-length bytecode like JSOP_TABLESWITCH.
  300. */
  301. extern uintN
  302. js_GetVariableBytecodeLength(jsbytecode *pc);
  303. /*
  304. * Find the number of stack slots used by a variadic opcode such as JSOP_CALL
  305. * or JSOP_NEWARRAY (for such ops, JSCodeSpec.nuses is -1).
  306. */
  307. extern uintN
  308. js_GetVariableStackUseLength(JSOp op, jsbytecode *pc);
  309. #ifdef DEBUG
  310. /*
  311. * Disassemblers, for debugging only.
  312. */
  313. #include <stdio.h>
  314. extern JS_FRIEND_API(JSBool)
  315. js_Disassemble(JSContext *cx, JSScript *script, JSBool lines, FILE *fp);
  316. extern JS_FRIEND_API(uintN)
  317. js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc, uintN loc,
  318. JSBool lines, FILE *fp);
  319. #endif /* DEBUG */
  320. /*
  321. * Decompilers, for script, function, and expression pretty-printing.
  322. */
  323. extern JSBool
  324. js_DecompileScript(JSPrinter *jp, JSScript *script);
  325. extern JSBool
  326. js_DecompileFunctionBody(JSPrinter *jp);
  327. extern JSBool
  328. js_DecompileFunction(JSPrinter *jp);
  329. /*
  330. * Find the source expression that resulted in v, and return a newly allocated
  331. * C-string containing it. Fall back on v's string conversion (fallback) if we
  332. * can't find the bytecode that generated and pushed v on the operand stack.
  333. *
  334. * Search the current stack frame if spindex is JSDVG_SEARCH_STACK. Don't
  335. * look for v on the stack if spindex is JSDVG_IGNORE_STACK. Otherwise,
  336. * spindex is the negative index of v, measured from cx->fp->sp, or from a
  337. * lower frame's sp if cx->fp is native.
  338. *
  339. * The caller must call JS_free on the result after a succsesful call.
  340. */
  341. extern char *
  342. js_DecompileValueGenerator(JSContext *cx, intN spindex, jsval v,
  343. JSString *fallback);
  344. #define JSDVG_IGNORE_STACK 0
  345. #define JSDVG_SEARCH_STACK 1
  346. /*
  347. * Given bytecode address pc in script's main program code, return the operand
  348. * stack depth just before (JSOp) *pc executes.
  349. */
  350. extern uintN
  351. js_ReconstructStackDepth(JSContext *cx, JSScript *script, jsbytecode *pc);
  352. JS_END_EXTERN_C
  353. #endif /* jsopcode_h___ */