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