Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / js / src / jsopcode.h
blob68cf5140d8baba7df0146ed880915c8b1d76a2da
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
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.
24 * Contributor(s):
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.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef jsopcode_h___
41 #define jsopcode_h___
43 * JS bytecode definitions.
45 #include <stddef.h>
46 #include "jsprvtd.h"
47 #include "jspubtd.h"
48 #include "jsutil.h"
50 #ifdef __cplusplus
51 # include "jsvalue.h"
52 #endif
54 JS_BEGIN_EXTERN_C
57 * JS operation bytecodes.
59 typedef enum JSOp {
60 #define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
61 op = val,
62 #include "jsopcode.tbl"
63 #undef OPDEF
64 JSOP_LIMIT,
67 * These pseudo-ops help js_DecompileValueGenerator decompile JSOP_SETNAME,
68 * JSOP_SETPROP, and JSOP_SETELEM, respectively. They are never stored in
69 * bytecode, so they don't preempt valid opcodes.
71 JSOP_GETPROP2 = JSOP_LIMIT,
72 JSOP_GETELEM2 = JSOP_LIMIT + 1,
73 JSOP_FAKE_LIMIT = JSOP_GETELEM2
74 } JSOp;
77 * JS bytecode formats.
79 #define JOF_BYTE 0 /* single bytecode, no immediates */
80 #define JOF_JUMP 1 /* signed 16-bit jump offset immediate */
81 #define JOF_ATOM 2 /* unsigned 16-bit constant index */
82 #define JOF_UINT16 3 /* unsigned 16-bit immediate operand */
83 #define JOF_TABLESWITCH 4 /* table switch */
84 #define JOF_LOOKUPSWITCH 5 /* lookup switch */
85 #define JOF_QARG 6 /* quickened get/set function argument ops */
86 #define JOF_LOCAL 7 /* var or block-local variable */
87 #define JOF_SLOTATOM 8 /* uint16 slot + constant index */
88 #define JOF_JUMPX 9 /* signed 32-bit jump offset immediate */
89 #define JOF_TABLESWITCHX 10 /* extended (32-bit offset) table switch */
90 #define JOF_LOOKUPSWITCHX 11 /* extended (32-bit offset) lookup switch */
91 #define JOF_UINT24 12 /* extended unsigned 24-bit literal (index) */
92 #define JOF_UINT8 13 /* uint8 immediate, e.g. top 8 bits of 24-bit
93 atom index */
94 #define JOF_INT32 14 /* int32 immediate operand */
95 #define JOF_OBJECT 15 /* unsigned 16-bit object index */
96 #define JOF_SLOTOBJECT 16 /* uint16 slot index + object index */
97 #define JOF_REGEXP 17 /* unsigned 16-bit regexp index */
98 #define JOF_INT8 18 /* int8 immediate operand */
99 #define JOF_ATOMOBJECT 19 /* uint16 constant index + object index */
100 #define JOF_UINT16PAIR 20 /* pair of uint16 immediates */
101 #define JOF_GLOBAL 21 /* uint16 global array index */
102 #define JOF_TYPEMASK 0x001f /* mask for above immediate types */
104 #define JOF_NAME (1U<<5) /* name operation */
105 #define JOF_PROP (2U<<5) /* obj.prop operation */
106 #define JOF_ELEM (3U<<5) /* obj[index] operation */
107 #define JOF_XMLNAME (4U<<5) /* XML name: *, a::b, @a, @a::b, etc. */
108 #define JOF_VARPROP (5U<<5) /* x.prop for this, arg, var, or local x */
109 #define JOF_MODEMASK (7U<<5) /* mask for above addressing modes */
110 #define JOF_SET (1U<<8) /* set (i.e., assignment) operation */
111 #define JOF_DEL (1U<<9) /* delete operation */
112 #define JOF_DEC (1U<<10) /* decrement (--, not ++) opcode */
113 #define JOF_INC (2U<<10) /* increment (++, not --) opcode */
114 #define JOF_INCDEC (3U<<10) /* increment or decrement opcode */
115 #define JOF_POST (1U<<12) /* postorder increment or decrement */
116 #define JOF_FOR (1U<<13) /* for-in property op (akin to JOF_SET) */
117 #define JOF_ASSIGNING JOF_SET /* hint for Class.resolve, used for ops
118 that do simplex assignment */
119 #define JOF_DETECTING (1U<<14) /* object detection for JSNewResolveOp */
120 #define JOF_BACKPATCH (1U<<15) /* backpatch placeholder during codegen */
121 #define JOF_LEFTASSOC (1U<<16) /* left-associative operator */
122 #define JOF_DECLARING (1U<<17) /* var, const, or function declaration op */
123 #define JOF_INDEXBASE (1U<<18) /* atom segment base setting prefix op */
124 #define JOF_CALLOP (1U<<19) /* call operation that pushes function and
125 this */
126 #define JOF_PARENHEAD (1U<<20) /* opcode consumes value of expression in
127 parenthesized statement head */
128 #define JOF_INVOKE (1U<<21) /* JSOP_CALL, JSOP_NEW, JSOP_EVAL */
129 #define JOF_TMPSLOT (1U<<22) /* interpreter uses extra temporary slot
130 to root intermediate objects besides
131 the slots opcode uses */
132 #define JOF_TMPSLOT2 (2U<<22) /* interpreter uses extra 2 temporary slot
133 besides the slots opcode uses */
134 #define JOF_TMPSLOT3 (3U<<22) /* interpreter uses extra 3 temporary slot
135 besides the slots opcode uses */
136 #define JOF_TMPSLOT_SHIFT 22
137 #define JOF_TMPSLOT_MASK (JS_BITMASK(2) << JOF_TMPSLOT_SHIFT)
139 #define JOF_SHARPSLOT (1U<<24) /* first immediate is uint16 stack slot no.
140 that needs fixup when in global code (see
141 Compiler::compileScript) */
142 #define JOF_GNAME (1U<<25) /* predicted global name */
144 /* Shorthands for type from format and type from opcode. */
145 #define JOF_TYPE(fmt) ((fmt) & JOF_TYPEMASK)
146 #define JOF_OPTYPE(op) JOF_TYPE(js_CodeSpec[op].format)
148 /* Shorthands for mode from format and mode from opcode. */
149 #define JOF_MODE(fmt) ((fmt) & JOF_MODEMASK)
150 #define JOF_OPMODE(op) JOF_MODE(js_CodeSpec[op].format)
152 #define JOF_TYPE_IS_EXTENDED_JUMP(t) \
153 ((unsigned)((t) - JOF_JUMPX) <= (unsigned)(JOF_LOOKUPSWITCHX - JOF_JUMPX))
156 * Immediate operand getters, setters, and bounds.
159 /* Common uint16 immediate format helpers. */
160 #define UINT16_LEN 2
161 #define UINT16_HI(i) ((jsbytecode)((i) >> 8))
162 #define UINT16_LO(i) ((jsbytecode)(i))
163 #define GET_UINT16(pc) ((uintN)(((pc)[1] << 8) | (pc)[2]))
164 #define SET_UINT16(pc,i) ((pc)[1] = UINT16_HI(i), (pc)[2] = UINT16_LO(i))
165 #define UINT16_LIMIT ((uintN)1 << 16)
167 /* Short (2-byte signed offset) relative jump macros. */
168 #define JUMP_OFFSET_LEN 2
169 #define JUMP_OFFSET_HI(off) ((jsbytecode)((off) >> 8))
170 #define JUMP_OFFSET_LO(off) ((jsbytecode)(off))
171 #define GET_JUMP_OFFSET(pc) ((int16)GET_UINT16(pc))
172 #define SET_JUMP_OFFSET(pc,off) ((pc)[1] = JUMP_OFFSET_HI(off), \
173 (pc)[2] = JUMP_OFFSET_LO(off))
174 #define JUMP_OFFSET_MIN ((int16)0x8000)
175 #define JUMP_OFFSET_MAX ((int16)0x7fff)
178 * When a short jump won't hold a relative offset, its 2-byte immediate offset
179 * operand is an unsigned index of a span-dependency record, maintained until
180 * code generation finishes -- after which some (but we hope not nearly all)
181 * span-dependent jumps must be extended (see OptimizeSpanDeps in jsemit.c).
183 * If the span-dependency record index overflows SPANDEP_INDEX_MAX, the jump
184 * offset will contain SPANDEP_INDEX_HUGE, indicating that the record must be
185 * found (via binary search) by its "before span-dependency optimization" pc
186 * offset (from script main entry point).
188 #define GET_SPANDEP_INDEX(pc) ((uint16)GET_UINT16(pc))
189 #define SET_SPANDEP_INDEX(pc,i) ((pc)[1] = JUMP_OFFSET_HI(i), \
190 (pc)[2] = JUMP_OFFSET_LO(i))
191 #define SPANDEP_INDEX_MAX ((uint16)0xfffe)
192 #define SPANDEP_INDEX_HUGE ((uint16)0xffff)
194 /* Ultimately, if short jumps won't do, emit long (4-byte signed) offsets. */
195 #define JUMPX_OFFSET_LEN 4
196 #define JUMPX_OFFSET_B3(off) ((jsbytecode)((off) >> 24))
197 #define JUMPX_OFFSET_B2(off) ((jsbytecode)((off) >> 16))
198 #define JUMPX_OFFSET_B1(off) ((jsbytecode)((off) >> 8))
199 #define JUMPX_OFFSET_B0(off) ((jsbytecode)(off))
200 #define GET_JUMPX_OFFSET(pc) ((int32)(((pc)[1] << 24) | ((pc)[2] << 16) \
201 | ((pc)[3] << 8) | (pc)[4]))
202 #define SET_JUMPX_OFFSET(pc,off)((pc)[1] = JUMPX_OFFSET_B3(off), \
203 (pc)[2] = JUMPX_OFFSET_B2(off), \
204 (pc)[3] = JUMPX_OFFSET_B1(off), \
205 (pc)[4] = JUMPX_OFFSET_B0(off))
206 #define JUMPX_OFFSET_MIN ((int32)0x80000000)
207 #define JUMPX_OFFSET_MAX ((int32)0x7fffffff)
210 * A literal is indexed by a per-script atom or object maps. Most scripts
211 * have relatively few literals, so the standard JOF_ATOM, JOF_OBJECT and
212 * JOF_REGEXP formats specifies a fixed 16 bits of immediate operand index.
213 * A script with more than 64K literals must wrap the bytecode into
214 * JSOP_INDEXBASE and JSOP_RESETBASE pair.
216 #define INDEX_LEN 2
217 #define INDEX_HI(i) ((jsbytecode)((i) >> 8))
218 #define INDEX_LO(i) ((jsbytecode)(i))
219 #define GET_INDEX(pc) GET_UINT16(pc)
220 #define SET_INDEX(pc,i) ((pc)[1] = INDEX_HI(i), (pc)[2] = INDEX_LO(i))
222 #define GET_INDEXBASE(pc) (JS_ASSERT(*(pc) == JSOP_INDEXBASE), \
223 ((uintN)((pc)[1])) << 16)
224 #define INDEXBASE_LEN 1
226 #define UINT24_HI(i) ((jsbytecode)((i) >> 16))
227 #define UINT24_MID(i) ((jsbytecode)((i) >> 8))
228 #define UINT24_LO(i) ((jsbytecode)(i))
229 #define GET_UINT24(pc) ((jsatomid)(((pc)[1] << 16) | \
230 ((pc)[2] << 8) | \
231 (pc)[3]))
232 #define SET_UINT24(pc,i) ((pc)[1] = UINT24_HI(i), \
233 (pc)[2] = UINT24_MID(i), \
234 (pc)[3] = UINT24_LO(i))
236 #define GET_INT8(pc) ((jsint)(int8)(pc)[1])
238 #define GET_INT32(pc) ((jsint)(((uint32)((pc)[1]) << 24) | \
239 ((uint32)((pc)[2]) << 16) | \
240 ((uint32)((pc)[3]) << 8) | \
241 (uint32)(pc)[4]))
242 #define SET_INT32(pc,i) ((pc)[1] = (jsbytecode)((uint32)(i) >> 24), \
243 (pc)[2] = (jsbytecode)((uint32)(i) >> 16), \
244 (pc)[3] = (jsbytecode)((uint32)(i) >> 8), \
245 (pc)[4] = (jsbytecode)(uint32)(i))
247 /* Index limit is determined by SN_3BYTE_OFFSET_FLAG, see jsemit.h. */
248 #define INDEX_LIMIT_LOG2 23
249 #define INDEX_LIMIT ((uint32)1 << INDEX_LIMIT_LOG2)
251 /* Actual argument count operand format helpers. */
252 #define ARGC_HI(argc) UINT16_HI(argc)
253 #define ARGC_LO(argc) UINT16_LO(argc)
254 #define GET_ARGC(pc) GET_UINT16(pc)
255 #define ARGC_LIMIT UINT16_LIMIT
257 /* Synonyms for quick JOF_QARG and JOF_LOCAL bytecodes. */
258 #define GET_ARGNO(pc) GET_UINT16(pc)
259 #define SET_ARGNO(pc,argno) SET_UINT16(pc,argno)
260 #define ARGNO_LEN 2
261 #define ARGNO_LIMIT UINT16_LIMIT
263 #define GET_SLOTNO(pc) GET_UINT16(pc)
264 #define SET_SLOTNO(pc,varno) SET_UINT16(pc,varno)
265 #define SLOTNO_LEN 2
266 #define SLOTNO_LIMIT UINT16_LIMIT
268 struct JSCodeSpec {
269 int8 length; /* length including opcode byte */
270 int8 nuses; /* arity, -1 if variadic */
271 int8 ndefs; /* number of stack results */
272 uint8 prec; /* operator precedence */
273 uint32 format; /* immediate operand format */
275 #ifdef __cplusplus
276 uint32 type() const { return JOF_TYPE(format); }
277 #endif
280 extern const JSCodeSpec js_CodeSpec[];
281 extern uintN js_NumCodeSpecs;
282 extern const char *js_CodeName[];
283 extern const char js_EscapeMap[];
285 /* Silence unreferenced formal parameter warnings */
286 #ifdef _MSC_VER
287 #pragma warning(push)
288 #pragma warning(disable:4100)
289 #endif
292 * Return a GC'ed string containing the chars in str, with any non-printing
293 * chars or quotes (' or " as specified by the quote argument) escaped, and
294 * with the quote character at the beginning and end of the result string.
296 extern JSString *
297 js_QuoteString(JSContext *cx, JSString *str, jschar quote);
300 * JSPrinter operations, for printf style message formatting. The return
301 * value from js_GetPrinterOutput() is the printer's cumulative output, in
302 * a GC'ed string.
304 * strict is true if the context in which the output will appear has
305 * already been marked as strict, thus indicating that nested
306 * functions need not be re-marked with a strict directive. It should
307 * be false in the outermost printer.
310 extern JSPrinter *
311 js_NewPrinter(JSContext *cx, const char *name, JSFunction *fun,
312 uintN indent, JSBool pretty, JSBool grouped, JSBool strict);
314 extern void
315 js_DestroyPrinter(JSPrinter *jp);
317 extern JSString *
318 js_GetPrinterOutput(JSPrinter *jp);
320 extern int
321 js_printf(JSPrinter *jp, const char *format, ...);
323 extern JSBool
324 js_puts(JSPrinter *jp, const char *s);
327 * Get index operand from the bytecode using a bytecode analysis to deduce the
328 * the index register. This function is infallible, in spite of taking cx as
329 * its first parameter; it uses only cx->runtime when calling JS_GetTrapOpcode.
330 * The GET_*_FROM_BYTECODE macros that call it pick up cx from their caller's
331 * lexical environments.
333 uintN
334 js_GetIndexFromBytecode(JSContext *cx, JSScript *script, jsbytecode *pc,
335 ptrdiff_t pcoff);
338 * A slower version of GET_ATOM when the caller does not want to maintain
339 * the index segment register itself.
341 #define GET_ATOM_FROM_BYTECODE(script, pc, pcoff, atom) \
342 JS_BEGIN_MACRO \
343 JS_ASSERT(*(pc) != JSOP_DOUBLE); \
344 uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
345 JS_GET_SCRIPT_ATOM(script, pc, index_, atom); \
346 JS_END_MACRO
349 * Variant for getting a double atom when we might be in an imacro. Bytecodes
350 * with literals that are only ever doubles must use this macro, and never use
351 * GET_ATOM_FROM_BYTECODE or JS_GET_SCRIPT_ATOM.
353 * Unfortunately some bytecodes such as JSOP_LOOKUPSWITCH have immediates that
354 * might be string or double atoms. Those opcodes cannot be used from imacros.
355 * See the assertions in the JSOP_DOUBLE and JSOP_LOOKUPSWTICH* opcode cases in
356 * jsinterp.cpp.
358 #define GET_DOUBLE_FROM_BYTECODE(script, pc, pcoff, dbl) \
359 JS_BEGIN_MACRO \
360 uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
361 JS_ASSERT(index_ < (script)->consts()->length); \
362 (dbl) = (script)->getConst(index_).toDouble(); \
363 JS_END_MACRO
365 #define GET_OBJECT_FROM_BYTECODE(script, pc, pcoff, obj) \
366 JS_BEGIN_MACRO \
367 uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
368 obj = (script)->getObject(index_); \
369 JS_END_MACRO
371 #define GET_FUNCTION_FROM_BYTECODE(script, pc, pcoff, fun) \
372 JS_BEGIN_MACRO \
373 uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
374 fun = (script)->getFunction(index_); \
375 JS_END_MACRO
377 #define GET_REGEXP_FROM_BYTECODE(script, pc, pcoff, obj) \
378 JS_BEGIN_MACRO \
379 uintN index_ = js_GetIndexFromBytecode(cx, (script), (pc), (pcoff)); \
380 obj = (script)->getRegExp(index_); \
381 JS_END_MACRO
384 * Get the length of variable-length bytecode like JSOP_TABLESWITCH.
386 extern uintN
387 js_GetVariableBytecodeLength(jsbytecode *pc);
390 * Find the number of stack slots used by a variadic opcode such as JSOP_CALL
391 * (for such ops, JSCodeSpec.nuses is -1).
393 extern uintN
394 js_GetVariableStackUses(JSOp op, jsbytecode *pc);
397 * Find the number of stack slots defined by JSOP_ENTERBLOCK (for this op,
398 * JSCodeSpec.ndefs is -1).
400 extern uintN
401 js_GetEnterBlockStackDefs(JSContext *cx, JSScript *script, jsbytecode *pc);
403 #ifdef __cplusplus /* Aargh, libgjs, bug 492720. */
404 static JS_INLINE uintN
405 js_GetStackUses(const JSCodeSpec *cs, JSOp op, jsbytecode *pc)
407 JS_ASSERT(cs == &js_CodeSpec[op]);
408 if (cs->nuses >= 0)
409 return cs->nuses;
410 return js_GetVariableStackUses(op, pc);
413 static JS_INLINE uintN
414 js_GetStackDefs(JSContext *cx, const JSCodeSpec *cs, JSOp op, JSScript *script,
415 jsbytecode *pc)
417 JS_ASSERT(cs == &js_CodeSpec[op]);
418 if (cs->ndefs >= 0)
419 return cs->ndefs;
421 /* Only JSOP_ENTERBLOCK has a variable number of stack defs. */
422 JS_ASSERT(op == JSOP_ENTERBLOCK);
423 return js_GetEnterBlockStackDefs(cx, script, pc);
425 #endif
427 #ifdef DEBUG
429 * Disassemblers, for debugging only.
431 #include <stdio.h>
433 extern JS_FRIEND_API(JSBool)
434 js_Disassemble(JSContext *cx, JSScript *script, JSBool lines, FILE *fp);
436 extern JS_FRIEND_API(uintN)
437 js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc, uintN loc,
438 JSBool lines, FILE *fp);
439 #endif /* DEBUG */
442 * Decompilers, for script, function, and expression pretty-printing.
444 extern JSBool
445 js_DecompileScript(JSPrinter *jp, JSScript *script);
447 extern JSBool
448 js_DecompileFunctionBody(JSPrinter *jp);
450 extern JSBool
451 js_DecompileFunction(JSPrinter *jp);
454 * Some C++ compilers treat the language linkage (extern "C" vs.
455 * extern "C++") as part of function (and thus pointer-to-function)
456 * types. The use of this typedef (defined in "C") ensures that
457 * js_DecompileToString's definition (in "C++") gets matched up with
458 * this declaration.
460 typedef JSBool (* JSDecompilerPtr)(JSPrinter *);
462 extern JSString *
463 js_DecompileToString(JSContext *cx, const char *name, JSFunction *fun,
464 uintN indent, JSBool pretty, JSBool grouped, JSBool strict,
465 JSDecompilerPtr decompiler);
468 * Find the source expression that resulted in v, and return a newly allocated
469 * C-string containing it. Fall back on v's string conversion (fallback) if we
470 * can't find the bytecode that generated and pushed v on the operand stack.
472 * Search the current stack frame if spindex is JSDVG_SEARCH_STACK. Don't
473 * look for v on the stack if spindex is JSDVG_IGNORE_STACK. Otherwise,
474 * spindex is the negative index of v, measured from cx->fp->sp, or from a
475 * lower frame's sp if cx->fp is native.
477 * The caller must call JS_free on the result after a succsesful call.
479 extern char *
480 js_DecompileValueGenerator(JSContext *cx, intN spindex, jsval v,
481 JSString *fallback);
483 #define JSDVG_IGNORE_STACK 0
484 #define JSDVG_SEARCH_STACK 1
486 #ifdef __cplusplus
487 namespace js {
489 static inline char *
490 DecompileValueGenerator(JSContext *cx, intN spindex, const Value &v,
491 JSString *fallback)
493 return js_DecompileValueGenerator(cx, spindex, Jsvalify(v), fallback);
497 #endif
500 * Given bytecode address pc in script's main program code, return the operand
501 * stack depth just before (JSOp) *pc executes.
503 extern uintN
504 js_ReconstructStackDepth(JSContext *cx, JSScript *script, jsbytecode *pc);
506 #ifdef _MSC_VER
507 #pragma warning(pop)
508 #endif
510 JS_END_EXTERN_C
512 #endif /* jsopcode_h___ */