|
|
1.1 root 1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2: *
3: * The contents of this file are subject to the Netscape Public
4: * License Version 1.1 (the "License"); you may not use this file
5: * except in compliance with the License. You may obtain a copy of
6: * the License at http://www.mozilla.org/NPL/
7: *
8: * Software distributed under the License is distributed on an "AS
9: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10: * implied. See the License for the specific language governing
11: * rights and limitations under the License.
12: *
13: * The Original Code is Mozilla Communicator client code, released
14: * March 31, 1998.
15: *
16: * The Initial Developer of the Original Code is Netscape
17: * Communications Corporation. Portions created by Netscape are
18: * Copyright (C) 1998 Netscape Communications Corporation. All
19: * Rights Reserved.
20: *
21: * Contributor(s):
22: *
23: * Alternatively, the contents of this file may be used under the
24: * terms of the GNU Public License (the "GPL"), in which case the
25: * provisions of the GPL are applicable instead of those above.
26: * If you wish to allow use of your version of this file only
27: * under the terms of the GPL and not to allow others to use your
28: * version of this file under the NPL, indicate your decision by
29: * deleting the provisions above and replace them with the notice
30: * and other provisions required by the GPL. If you do not delete
31: * the provisions above, a recipient may use your version of this
32: * file under either the NPL or the GPL.
33: */
34:
35: #ifndef jsopcode_h___
36: #define jsopcode_h___
37: /*
38: * JS bytecode definitions.
39: */
40: #include <stddef.h>
41: #include "jsprvtd.h"
42: #include "jspubtd.h"
43:
44: JS_BEGIN_EXTERN_C
45:
46: /*
47: * JS operation bytecodes.
48: */
49: typedef enum JSOp {
50: #define OPDEF(op,val,name,token,length,nuses,ndefs,prec,format) \
51: op = val,
52: #include "jsopcode.tbl"
53: #undef OPDEF
54: JSOP_LIMIT
55: } JSOp;
56:
57: /*
58: * JS bytecode formats.
59: */
60: #define JOF_BYTE 0 /* single bytecode, no immediates */
61: #define JOF_JUMP 1 /* signed 16-bit jump offset immediate */
62: #define JOF_CONST 2 /* unsigned 16-bit constant pool index */
63: #define JOF_UINT16 3 /* unsigned 16-bit immediate operand */
64: #define JOF_TABLESWITCH 4 /* table switch */
65: #define JOF_LOOKUPSWITCH 5 /* lookup switch */
66: #define JOF_QARG 6 /* quickened get/set function argument ops */
67: #define JOF_QVAR 7 /* quickened get/set local variable ops */
68: #define JOF_DEFLOCALVAR 8 /* define local var with initial value */
69: #define JOF_JUMPX 9 /* signed 32-bit jump offset immediate */
70: #define JOF_TABLESWITCHX 10 /* extended (32-bit offset) table switch */
71: #define JOF_LOOKUPSWITCHX 11 /* extended (32-bit offset) lookup switch */
72: #define JOF_TYPEMASK 0x000f /* mask for above immediate types */
73: #define JOF_NAME 0x0010 /* name operation */
74: #define JOF_PROP 0x0020 /* obj.prop operation */
75: #define JOF_ELEM 0x0030 /* obj[index] operation */
76: #define JOF_MODEMASK 0x0030 /* mask for above addressing modes */
77: #define JOF_SET 0x0040 /* set (i.e., assignment) operation */
78: #define JOF_DEL 0x0080 /* delete operation */
79: #define JOF_DEC 0x0100 /* decrement (--, not ++) opcode */
80: #define JOF_INC 0x0200 /* increment (++, not --) opcode */
81: #define JOF_INCDEC 0x0300 /* increment or decrement opcode */
82: #define JOF_POST 0x0400 /* postorder increment or decrement */
83: #define JOF_IMPORT 0x0800 /* import property op */
84: #define JOF_FOR 0x1000 /* for-in property op */
85: #define JOF_ASSIGNING JOF_SET /* hint for JSClass.resolve, used for ops
86: that do simplex assignment */
87: #define JOF_BACKPATCH 0x4000 /* backpatch placeholder during codegen */
88: #define JOF_LEFTASSOC 0x8000 /* left-associative operator */
89:
90: #define JOF_TYPE_IS_EXTENDED_JUMP(t) \
91: ((unsigned)((t) - JOF_JUMPX) <= (unsigned)(JOF_LOOKUPSWITCHX - JOF_JUMPX))
92:
93: /*
94: * Immediate operand getters, setters, and bounds.
95: */
96:
97: /* Short (2-byte signed offset) relative jump macros. */
98: #define JUMP_OFFSET_LEN 2
99: #define JUMP_OFFSET_HI(off) ((jsbytecode)((off) >> 8))
100: #define JUMP_OFFSET_LO(off) ((jsbytecode)(off))
101: #define GET_JUMP_OFFSET(pc) ((int16)(((pc)[1] << 8) | (pc)[2]))
102: #define SET_JUMP_OFFSET(pc,off) ((pc)[1] = JUMP_OFFSET_HI(off), \
103: (pc)[2] = JUMP_OFFSET_LO(off))
104: #define JUMP_OFFSET_MIN ((int16)0x8000)
105: #define JUMP_OFFSET_MAX ((int16)0x7fff)
106:
107: /*
108: * When a short jump won't hold a relative offset, its 2-byte immediate offset
109: * operand is an unsigned index of a span-dependency record, maintained until
110: * code generation finishes -- after which some (but we hope not nearly all)
111: * span-dependent jumps must be extended (see OptimizeSpanDeps in jsemit.c).
112: *
113: * If the span-dependency record index overflows SPANDEP_INDEX_MAX, the jump
114: * offset will contain SPANDEP_INDEX_HUGE, indicating that the record must be
115: * found (via binary search) by its "before span-dependency optimization" pc
116: * offset (from script main entry point).
117: */
118: #define GET_SPANDEP_INDEX(pc) ((uint16)(((pc)[1] << 8) | (pc)[2]))
119: #define SET_SPANDEP_INDEX(pc,i) ((pc)[1] = JUMP_OFFSET_HI(i), \
120: (pc)[2] = JUMP_OFFSET_LO(i))
121: #define SPANDEP_INDEX_MAX ((uint16)0xfffe)
122: #define SPANDEP_INDEX_HUGE ((uint16)0xffff)
123:
124: /* Ultimately, if short jumps won't do, emit long (4-byte signed) offsets. */
125: #define JUMPX_OFFSET_LEN 4
126: #define JUMPX_OFFSET_B3(off) ((jsbytecode)((off) >> 24))
127: #define JUMPX_OFFSET_B2(off) ((jsbytecode)((off) >> 16))
128: #define JUMPX_OFFSET_B1(off) ((jsbytecode)((off) >> 8))
129: #define JUMPX_OFFSET_B0(off) ((jsbytecode)(off))
130: #define GET_JUMPX_OFFSET(pc) ((int32)(((pc)[1] << 24) | ((pc)[2] << 16) \
131: | ((pc)[3] << 8) | (pc)[4]))
132: #define SET_JUMPX_OFFSET(pc,off)((pc)[1] = JUMPX_OFFSET_B3(off), \
133: (pc)[2] = JUMPX_OFFSET_B2(off), \
134: (pc)[3] = JUMPX_OFFSET_B1(off), \
135: (pc)[4] = JUMPX_OFFSET_B0(off))
136: #define JUMPX_OFFSET_MIN ((int32)0x80000000)
137: #define JUMPX_OFFSET_MAX ((int32)0x7fffffff)
138:
139: /* A literal is indexed by a per-script atom map. */
140: #define ATOM_INDEX_LEN 2
141: #define ATOM_INDEX_HI(index) ((jsbytecode)((index) >> 8))
142: #define ATOM_INDEX_LO(index) ((jsbytecode)(index))
143: #define GET_ATOM_INDEX(pc) ((jsatomid)(((pc)[1] << 8) | (pc)[2]))
144: #define SET_ATOM_INDEX(pc,index)((pc)[1] = ATOM_INDEX_HI(index), \
145: (pc)[2] = ATOM_INDEX_LO(index))
146: #define GET_ATOM(cx,script,pc) js_GetAtom((cx), &(script)->atomMap, \
147: GET_ATOM_INDEX(pc))
148: #define ATOM_INDEX_LIMIT_LOG2 16
149: #define ATOM_INDEX_LIMIT ((uint32)1 << ATOM_INDEX_LIMIT_LOG2)
150:
151: /* Actual argument count operand format helpers. */
152: #define ARGC_HI(argc) ((jsbytecode)((argc) >> 8))
153: #define ARGC_LO(argc) ((jsbytecode)(argc))
154: #define GET_ARGC(pc) ((uintN)(((pc)[1] << 8) | (pc)[2]))
155: #define ARGC_LIMIT ((uint32)1 << 16)
156:
157: /* Synonyms for quick JOF_QARG and JOF_QVAR bytecodes. */
158: #define GET_ARGNO(pc) GET_ARGC(pc)
159: #define SET_ARGNO(pc,argno) SET_JUMP_OFFSET(pc,argno)
160: #define ARGNO_LEN JUMP_OFFSET_LEN
161: #define GET_VARNO(pc) GET_ARGC(pc)
162: #define SET_VARNO(pc,varno) SET_JUMP_OFFSET(pc,varno)
163: #define VARNO_LEN JUMP_OFFSET_LEN
164:
165: struct JSCodeSpec {
166: const char *name; /* JS bytecode name */
167: const char *token; /* JS source literal or null */
168: int8 length; /* length including opcode byte */
169: int8 nuses; /* arity, -1 if variadic */
170: int8 ndefs; /* number of stack results */
171: uint8 prec; /* operator precedence */
172: uint32 format; /* immediate operand format */
173: };
174:
175: extern const char js_const_str[];
176: extern const char js_var_str[];
177: extern const char js_function_str[];
178: extern const char js_in_str[];
179: extern const char js_instanceof_str[];
180: extern const char js_new_str[];
181: extern const char js_delete_str[];
182: extern const char js_typeof_str[];
183: extern const char js_void_str[];
184: extern const char js_null_str[];
185: extern const char js_this_str[];
186: extern const char js_false_str[];
187: extern const char js_true_str[];
188: extern const JSCodeSpec js_CodeSpec[];
189: extern uintN js_NumCodeSpecs;
190: extern const jschar js_EscapeMap[];
191:
192: /*
193: * Return a GC'ed string containing the chars in str, with any non-printing
194: * chars or quotes (' or " as specified by the quote argument) escaped, and
195: * with the quote character at the beginning and end of the result string.
196: */
197: extern JSString *
198: js_QuoteString(JSContext *cx, JSString *str, jschar quote);
199:
200: /*
201: * JSPrinter operations, for printf style message formatting. The return
202: * value from js_GetPrinterOutput() is the printer's cumulative output, in
203: * a GC'ed string.
204: */
205: extern JSPrinter *
206: js_NewPrinter(JSContext *cx, const char *name, uintN indent, JSBool pretty);
207:
208: extern void
209: js_DestroyPrinter(JSPrinter *jp);
210:
211: extern JSString *
212: js_GetPrinterOutput(JSPrinter *jp);
213:
214: extern int
215: js_printf(JSPrinter *jp, const char *format, ...);
216:
217: extern JSBool
218: js_puts(JSPrinter *jp, const char *s);
219:
220: #ifdef DEBUG
221: /*
222: * Disassemblers, for debugging only.
223: */
224: #include <stdio.h>
225:
226: extern JS_FRIEND_API(void)
227: js_Disassemble(JSContext *cx, JSScript *script, JSBool lines, FILE *fp);
228:
229: extern JS_FRIEND_API(uintN)
230: js_Disassemble1(JSContext *cx, JSScript *script, jsbytecode *pc, uintN loc,
231: JSBool lines, FILE *fp);
232: #endif /* DEBUG */
233:
234: /*
235: * Decompilers, for script, function, and expression pretty-printing.
236: */
237: extern JSBool
238: js_DecompileCode(JSPrinter *jp, JSScript *script, jsbytecode *pc, uintN len);
239:
240: extern JSBool
241: js_DecompileScript(JSPrinter *jp, JSScript *script);
242:
243: extern JSBool
244: js_DecompileFunctionBody(JSPrinter *jp, JSFunction *fun);
245:
246: extern JSBool
247: js_DecompileFunction(JSPrinter *jp, JSFunction *fun);
248:
249: /*
250: * Find the source expression that resulted in v, and return a new string
251: * containing it. Fall back on v's string conversion (fallback) if we can't
252: * find the bytecode that generated and pushed v on the operand stack.
253: *
254: * Search the current stack frame if spindex is JSDVG_SEARCH_STACK. Don't
255: * look for v on the stack if spindex is JSDVG_IGNORE_STACK. Otherwise,
256: * spindex is the negative index of v, measured from cx->fp->sp, or from a
257: * lower frame's sp if cx->fp is native.
258: */
259: extern JSString *
260: js_DecompileValueGenerator(JSContext *cx, intN spindex, jsval v,
261: JSString *fallback);
262:
263: #define JSDVG_IGNORE_STACK 0
264: #define JSDVG_SEARCH_STACK 1
265:
266: JS_END_EXTERN_C
267:
268: #endif /* jsopcode_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.