1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsopcode.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,846 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef jsopcode_h 1.11 +#define jsopcode_h 1.12 + 1.13 +/* 1.14 + * JS bytecode definitions. 1.15 + */ 1.16 + 1.17 +#include "jsbytecode.h" 1.18 +#include "jstypes.h" 1.19 +#include "NamespaceImports.h" 1.20 + 1.21 +#include "frontend/SourceNotes.h" 1.22 +#include "vm/Opcodes.h" 1.23 + 1.24 +/* 1.25 + * JS operation bytecodes. 1.26 + */ 1.27 +typedef enum JSOp { 1.28 +#define ENUMERATE_OPCODE(op, val, ...) op = val, 1.29 +FOR_EACH_OPCODE(ENUMERATE_OPCODE) 1.30 +#undef ENUMERATE_OPCODE 1.31 + 1.32 + JSOP_LIMIT, 1.33 + 1.34 + /* 1.35 + * These pseudo-ops help js_DecompileValueGenerator decompile JSOP_SETPROP, 1.36 + * JSOP_SETELEM, and comprehension-tails, respectively. They are never 1.37 + * stored in bytecode, so they don't preempt valid opcodes. 1.38 + */ 1.39 + JSOP_GETPROP2 = JSOP_LIMIT, 1.40 + JSOP_GETELEM2 = JSOP_LIMIT + 1, 1.41 + JSOP_FORLOCAL = JSOP_LIMIT + 2, 1.42 + JSOP_FAKE_LIMIT = JSOP_FORLOCAL 1.43 +} JSOp; 1.44 + 1.45 +/* 1.46 + * JS bytecode formats. 1.47 + */ 1.48 +#define JOF_BYTE 0 /* single bytecode, no immediates */ 1.49 +#define JOF_JUMP 1 /* signed 16-bit jump offset immediate */ 1.50 +#define JOF_ATOM 2 /* unsigned 16-bit constant index */ 1.51 +#define JOF_UINT16 3 /* unsigned 16-bit immediate operand */ 1.52 +#define JOF_TABLESWITCH 4 /* table switch */ 1.53 +/* 5 is unused */ 1.54 +#define JOF_QARG 6 /* quickened get/set function argument ops */ 1.55 +#define JOF_LOCAL 7 /* var or block-local variable */ 1.56 +#define JOF_DOUBLE 8 /* uint32_t index for double value */ 1.57 +#define JOF_UINT24 12 /* extended unsigned 24-bit literal (index) */ 1.58 +#define JOF_UINT8 13 /* uint8_t immediate, e.g. top 8 bits of 24-bit 1.59 + atom index */ 1.60 +#define JOF_INT32 14 /* int32_t immediate operand */ 1.61 +#define JOF_OBJECT 15 /* unsigned 16-bit object index */ 1.62 +/* 16 is unused */ 1.63 +#define JOF_REGEXP 17 /* unsigned 32-bit regexp index */ 1.64 +#define JOF_INT8 18 /* int8_t immediate operand */ 1.65 +#define JOF_ATOMOBJECT 19 /* uint16_t constant index + object index */ 1.66 +/* 20 is unused */ 1.67 +#define JOF_SCOPECOORD 21 /* embedded ScopeCoordinate immediate */ 1.68 +#define JOF_TYPEMASK 0x001f /* mask for above immediate types */ 1.69 + 1.70 +#define JOF_NAME (1U<<5) /* name operation */ 1.71 +#define JOF_PROP (2U<<5) /* obj.prop operation */ 1.72 +#define JOF_ELEM (3U<<5) /* obj[index] operation */ 1.73 +#define JOF_MODEMASK (7U<<5) /* mask for above addressing modes */ 1.74 +#define JOF_SET (1U<<8) /* set (i.e., assignment) operation */ 1.75 +/* (1U<<9) is unused*/ 1.76 +/* (1U<<10) is unused*/ 1.77 +/* (1U<<11) is unused*/ 1.78 +/* (1U<<12) is unused*/ 1.79 +/* (1U<<13) is unused*/ 1.80 +#define JOF_DETECTING (1U<<14) /* object detection for warning-quelling */ 1.81 +/* (1U<<15) is unused*/ 1.82 +#define JOF_LEFTASSOC (1U<<16) /* left-associative operator */ 1.83 +/* (1U<<17) is unused */ 1.84 +/* (1U<<18) is unused */ 1.85 +/* (1U<<19) is unused*/ 1.86 +/* (1U<<20) is unused*/ 1.87 +#define JOF_INVOKE (1U<<21) /* JSOP_CALL, JSOP_FUNCALL, JSOP_FUNAPPLY, 1.88 + JSOP_NEW, JSOP_EVAL */ 1.89 +#define JOF_TMPSLOT (1U<<22) /* interpreter uses extra temporary slot 1.90 + to root intermediate objects besides 1.91 + the slots opcode uses */ 1.92 +#define JOF_TMPSLOT2 (2U<<22) /* interpreter uses extra 2 temporary slot 1.93 + besides the slots opcode uses */ 1.94 +#define JOF_TMPSLOT3 (3U<<22) /* interpreter uses extra 3 temporary slot 1.95 + besides the slots opcode uses */ 1.96 +#define JOF_TMPSLOT_SHIFT 22 1.97 +#define JOF_TMPSLOT_MASK (JS_BITMASK(2) << JOF_TMPSLOT_SHIFT) 1.98 + 1.99 +/* (1U<<24) is unused */ 1.100 +#define JOF_GNAME (1U<<25) /* predicted global name */ 1.101 +#define JOF_TYPESET (1U<<26) /* has an entry in a script's type sets */ 1.102 +#define JOF_ARITH (1U<<27) /* unary or binary arithmetic opcode */ 1.103 + 1.104 +/* Shorthands for type from format and type from opcode. */ 1.105 +#define JOF_TYPE(fmt) ((fmt) & JOF_TYPEMASK) 1.106 +#define JOF_OPTYPE(op) JOF_TYPE(js_CodeSpec[op].format) 1.107 + 1.108 +/* Shorthands for mode from format and mode from opcode. */ 1.109 +#define JOF_MODE(fmt) ((fmt) & JOF_MODEMASK) 1.110 +#define JOF_OPMODE(op) JOF_MODE(js_CodeSpec[op].format) 1.111 + 1.112 +/* 1.113 + * Immediate operand getters, setters, and bounds. 1.114 + */ 1.115 + 1.116 +static MOZ_ALWAYS_INLINE uint8_t 1.117 +GET_UINT8(jsbytecode *pc) 1.118 +{ 1.119 + return (uint8_t) pc[1]; 1.120 +} 1.121 + 1.122 +static MOZ_ALWAYS_INLINE void 1.123 +SET_UINT8(jsbytecode *pc, uint8_t u) 1.124 +{ 1.125 + pc[1] = (jsbytecode) u; 1.126 +} 1.127 + 1.128 +/* Common uint16_t immediate format helpers. */ 1.129 +#define UINT16_LEN 2 1.130 +#define UINT16_HI(i) ((jsbytecode)((i) >> 8)) 1.131 +#define UINT16_LO(i) ((jsbytecode)(i)) 1.132 +#define GET_UINT16(pc) ((unsigned)(((pc)[1] << 8) | (pc)[2])) 1.133 +#define SET_UINT16(pc,i) ((pc)[1] = UINT16_HI(i), (pc)[2] = UINT16_LO(i)) 1.134 +#define UINT16_LIMIT ((unsigned)1 << 16) 1.135 + 1.136 +/* Helpers for accessing the offsets of jump opcodes. */ 1.137 +#define JUMP_OFFSET_LEN 4 1.138 +#define JUMP_OFFSET_MIN INT32_MIN 1.139 +#define JUMP_OFFSET_MAX INT32_MAX 1.140 + 1.141 +static MOZ_ALWAYS_INLINE int32_t 1.142 +GET_JUMP_OFFSET(jsbytecode *pc) 1.143 +{ 1.144 + return (pc[1] << 24) | (pc[2] << 16) | (pc[3] << 8) | pc[4]; 1.145 +} 1.146 + 1.147 +static MOZ_ALWAYS_INLINE void 1.148 +SET_JUMP_OFFSET(jsbytecode *pc, int32_t off) 1.149 +{ 1.150 + pc[1] = (jsbytecode)(off >> 24); 1.151 + pc[2] = (jsbytecode)(off >> 16); 1.152 + pc[3] = (jsbytecode)(off >> 8); 1.153 + pc[4] = (jsbytecode)off; 1.154 +} 1.155 + 1.156 +#define UINT32_INDEX_LEN 4 1.157 + 1.158 +static MOZ_ALWAYS_INLINE uint32_t 1.159 +GET_UINT32_INDEX(const jsbytecode *pc) 1.160 +{ 1.161 + return (pc[1] << 24) | (pc[2] << 16) | (pc[3] << 8) | pc[4]; 1.162 +} 1.163 + 1.164 +static MOZ_ALWAYS_INLINE void 1.165 +SET_UINT32_INDEX(jsbytecode *pc, uint32_t index) 1.166 +{ 1.167 + pc[1] = (jsbytecode)(index >> 24); 1.168 + pc[2] = (jsbytecode)(index >> 16); 1.169 + pc[3] = (jsbytecode)(index >> 8); 1.170 + pc[4] = (jsbytecode)index; 1.171 +} 1.172 + 1.173 +#define UINT24_HI(i) ((jsbytecode)((i) >> 16)) 1.174 +#define UINT24_MID(i) ((jsbytecode)((i) >> 8)) 1.175 +#define UINT24_LO(i) ((jsbytecode)(i)) 1.176 +#define GET_UINT24(pc) ((unsigned)(((pc)[1] << 16) | \ 1.177 + ((pc)[2] << 8) | \ 1.178 + (pc)[3])) 1.179 +#define SET_UINT24(pc,i) ((pc)[1] = UINT24_HI(i), \ 1.180 + (pc)[2] = UINT24_MID(i), \ 1.181 + (pc)[3] = UINT24_LO(i)) 1.182 + 1.183 +#define GET_INT8(pc) (int8_t((pc)[1])) 1.184 + 1.185 +#define GET_INT32(pc) (((uint32_t((pc)[1]) << 24) | \ 1.186 + (uint32_t((pc)[2]) << 16) | \ 1.187 + (uint32_t((pc)[3]) << 8) | \ 1.188 + uint32_t((pc)[4]))) 1.189 +#define SET_INT32(pc,i) ((pc)[1] = (jsbytecode)(uint32_t(i) >> 24), \ 1.190 + (pc)[2] = (jsbytecode)(uint32_t(i) >> 16), \ 1.191 + (pc)[3] = (jsbytecode)(uint32_t(i) >> 8), \ 1.192 + (pc)[4] = (jsbytecode)uint32_t(i)) 1.193 + 1.194 +/* Index limit is determined by SN_4BYTE_OFFSET_FLAG, see frontend/BytecodeEmitter.h. */ 1.195 +#define INDEX_LIMIT_LOG2 31 1.196 +#define INDEX_LIMIT (uint32_t(1) << INDEX_LIMIT_LOG2) 1.197 + 1.198 +#define ARGC_HI(argc) UINT16_HI(argc) 1.199 +#define ARGC_LO(argc) UINT16_LO(argc) 1.200 +#define GET_ARGC(pc) GET_UINT16(pc) 1.201 +#define ARGC_LIMIT UINT16_LIMIT 1.202 + 1.203 +#define GET_ARGNO(pc) GET_UINT16(pc) 1.204 +#define SET_ARGNO(pc,argno) SET_UINT16(pc,argno) 1.205 +#define ARGNO_LEN 2 1.206 +#define ARGNO_LIMIT UINT16_LIMIT 1.207 + 1.208 +#define GET_LOCALNO(pc) GET_UINT24(pc) 1.209 +#define SET_LOCALNO(pc,varno) SET_UINT24(pc,varno) 1.210 +#define LOCALNO_LEN 3 1.211 +#define LOCALNO_BITS 24 1.212 +#define LOCALNO_LIMIT (1 << LOCALNO_BITS) 1.213 + 1.214 +static inline unsigned 1.215 +LoopEntryDepthHint(jsbytecode *pc) 1.216 +{ 1.217 + JS_ASSERT(*pc == JSOP_LOOPENTRY); 1.218 + return GET_UINT8(pc) & 0x7f; 1.219 +} 1.220 +static inline bool 1.221 +LoopEntryCanIonOsr(jsbytecode *pc) 1.222 +{ 1.223 + JS_ASSERT(*pc == JSOP_LOOPENTRY); 1.224 + return GET_UINT8(pc) & 0x80; 1.225 +} 1.226 +static inline uint8_t 1.227 +PackLoopEntryDepthHintAndFlags(unsigned loopDepth, bool canIonOsr) 1.228 +{ 1.229 + return (loopDepth < 0x80 ? uint8_t(loopDepth) : 0x7f) | (canIonOsr ? 0x80 : 0); 1.230 +} 1.231 + 1.232 +/* 1.233 + * Describes the 'hops' component of a JOF_SCOPECOORD opcode. 1.234 + * 1.235 + * Note: this component is only 8 bits wide, limiting the maximum number of 1.236 + * scopes between a use and def to roughly 255. This is a pretty small limit but 1.237 + * note that SpiderMonkey's recursive descent parser can only parse about this 1.238 + * many functions before hitting the C-stack recursion limit so this shouldn't 1.239 + * be a significant limitation in practice. 1.240 + */ 1.241 +#define GET_SCOPECOORD_HOPS(pc) GET_UINT8(pc) 1.242 +#define SET_SCOPECOORD_HOPS(pc,hops) SET_UINT8(pc,hops) 1.243 +#define SCOPECOORD_HOPS_LEN 1 1.244 +#define SCOPECOORD_HOPS_BITS 8 1.245 +#define SCOPECOORD_HOPS_LIMIT (1 << SCOPECOORD_HOPS_BITS) 1.246 + 1.247 +/* Describes the 'slot' component of a JOF_SCOPECOORD opcode. */ 1.248 +#define GET_SCOPECOORD_SLOT(pc) GET_UINT24(pc) 1.249 +#define SET_SCOPECOORD_SLOT(pc,slot) SET_UINT24(pc,slot) 1.250 +#define SCOPECOORD_SLOT_LEN 3 1.251 +#define SCOPECOORD_SLOT_BITS 24 1.252 +#define SCOPECOORD_SLOT_LIMIT (1 << SCOPECOORD_SLOT_BITS) 1.253 + 1.254 +struct JSCodeSpec { 1.255 + int8_t length; /* length including opcode byte */ 1.256 + int8_t nuses; /* arity, -1 if variadic */ 1.257 + int8_t ndefs; /* number of stack results */ 1.258 + uint32_t format; /* immediate operand format */ 1.259 + 1.260 + uint32_t type() const { return JOF_TYPE(format); } 1.261 +}; 1.262 + 1.263 +extern const JSCodeSpec js_CodeSpec[]; 1.264 +extern const unsigned js_NumCodeSpecs; 1.265 +extern const char * const js_CodeName[]; 1.266 +extern const char js_EscapeMap[]; 1.267 + 1.268 +/* Silence unreferenced formal parameter warnings */ 1.269 +#ifdef _MSC_VER 1.270 +#pragma warning(push) 1.271 +#pragma warning(disable:4100) 1.272 +#endif 1.273 + 1.274 +/* 1.275 + * Return a GC'ed string containing the chars in str, with any non-printing 1.276 + * chars or quotes (' or " as specified by the quote argument) escaped, and 1.277 + * with the quote character at the beginning and end of the result string. 1.278 + */ 1.279 +extern JSString * 1.280 +js_QuoteString(js::ExclusiveContext *cx, JSString *str, jschar quote); 1.281 + 1.282 +namespace js { 1.283 + 1.284 +static inline bool 1.285 +IsJumpOpcode(JSOp op) 1.286 +{ 1.287 + uint32_t type = JOF_TYPE(js_CodeSpec[op].format); 1.288 + 1.289 + /* 1.290 + * LABEL opcodes have type JOF_JUMP but are no-ops, don't treat them as 1.291 + * jumps to avoid degrading precision. 1.292 + */ 1.293 + return type == JOF_JUMP && op != JSOP_LABEL; 1.294 +} 1.295 + 1.296 +static inline bool 1.297 +BytecodeFallsThrough(JSOp op) 1.298 +{ 1.299 + switch (op) { 1.300 + case JSOP_GOTO: 1.301 + case JSOP_DEFAULT: 1.302 + case JSOP_RETURN: 1.303 + case JSOP_RETRVAL: 1.304 + case JSOP_THROW: 1.305 + case JSOP_TABLESWITCH: 1.306 + return false; 1.307 + case JSOP_GOSUB: 1.308 + /* These fall through indirectly, after executing a 'finally'. */ 1.309 + return true; 1.310 + default: 1.311 + return true; 1.312 + } 1.313 +} 1.314 + 1.315 +class SrcNoteLineScanner 1.316 +{ 1.317 + /* offset of the current JSOp in the bytecode */ 1.318 + ptrdiff_t offset; 1.319 + 1.320 + /* next src note to process */ 1.321 + jssrcnote *sn; 1.322 + 1.323 + /* line number of the current JSOp */ 1.324 + uint32_t lineno; 1.325 + 1.326 + /* 1.327 + * Is the current op the first one after a line change directive? Note that 1.328 + * multiple ops may be "first" if a line directive is used to return to a 1.329 + * previous line (eg, with a for loop increment expression.) 1.330 + */ 1.331 + bool lineHeader; 1.332 + 1.333 +public: 1.334 + SrcNoteLineScanner(jssrcnote *sn, uint32_t lineno) 1.335 + : offset(0), sn(sn), lineno(lineno) 1.336 + { 1.337 + } 1.338 + 1.339 + /* 1.340 + * This is called repeatedly with always-advancing relpc values. The src 1.341 + * notes are tuples of <PC offset from prev src note, type, args>. Scan 1.342 + * through, updating the lineno, until the next src note is for a later 1.343 + * bytecode. 1.344 + * 1.345 + * When looking at the desired PC offset ('relpc'), the op is first in that 1.346 + * line iff there is a SRC_SETLINE or SRC_NEWLINE src note for that exact 1.347 + * bytecode. 1.348 + * 1.349 + * Note that a single bytecode may have multiple line-modifying notes (even 1.350 + * though only one should ever be needed.) 1.351 + */ 1.352 + void advanceTo(ptrdiff_t relpc) { 1.353 + // Must always advance! If the same or an earlier PC is erroneously 1.354 + // passed in, we will already be past the relevant src notes 1.355 + JS_ASSERT_IF(offset > 0, relpc > offset); 1.356 + 1.357 + // Next src note should be for after the current offset 1.358 + JS_ASSERT_IF(offset > 0, SN_IS_TERMINATOR(sn) || SN_DELTA(sn) > 0); 1.359 + 1.360 + // The first PC requested is always considered to be a line header 1.361 + lineHeader = (offset == 0); 1.362 + 1.363 + if (SN_IS_TERMINATOR(sn)) 1.364 + return; 1.365 + 1.366 + ptrdiff_t nextOffset; 1.367 + while ((nextOffset = offset + SN_DELTA(sn)) <= relpc && !SN_IS_TERMINATOR(sn)) { 1.368 + offset = nextOffset; 1.369 + SrcNoteType type = (SrcNoteType) SN_TYPE(sn); 1.370 + if (type == SRC_SETLINE || type == SRC_NEWLINE) { 1.371 + if (type == SRC_SETLINE) 1.372 + lineno = js_GetSrcNoteOffset(sn, 0); 1.373 + else 1.374 + lineno++; 1.375 + 1.376 + if (offset == relpc) 1.377 + lineHeader = true; 1.378 + } 1.379 + 1.380 + sn = SN_NEXT(sn); 1.381 + } 1.382 + } 1.383 + 1.384 + bool isLineHeader() const { 1.385 + return lineHeader; 1.386 + } 1.387 + 1.388 + uint32_t getLine() const { return lineno; } 1.389 +}; 1.390 + 1.391 +extern unsigned 1.392 +StackUses(JSScript *script, jsbytecode *pc); 1.393 + 1.394 +extern unsigned 1.395 +StackDefs(JSScript *script, jsbytecode *pc); 1.396 + 1.397 +#ifdef DEBUG 1.398 +/* 1.399 + * Given bytecode address pc in script's main program code, compute the operand 1.400 + * stack depth just before (JSOp) *pc executes. If *pc is not reachable, return 1.401 + * false. 1.402 + */ 1.403 +extern bool 1.404 +ReconstructStackDepth(JSContext *cx, JSScript *script, jsbytecode *pc, uint32_t *depth, bool *reachablePC); 1.405 +#endif 1.406 + 1.407 +} /* namespace js */ 1.408 + 1.409 +#ifdef _MSC_VER 1.410 +#pragma warning(pop) 1.411 +#endif 1.412 + 1.413 +#define JSDVG_IGNORE_STACK 0 1.414 +#define JSDVG_SEARCH_STACK 1 1.415 + 1.416 +/* 1.417 + * Get the length of variable-length bytecode like JSOP_TABLESWITCH. 1.418 + */ 1.419 +extern size_t 1.420 +js_GetVariableBytecodeLength(jsbytecode *pc); 1.421 + 1.422 +namespace js { 1.423 + 1.424 +/* 1.425 + * Find the source expression that resulted in v, and return a newly allocated 1.426 + * C-string containing it. Fall back on v's string conversion (fallback) if we 1.427 + * can't find the bytecode that generated and pushed v on the operand stack. 1.428 + * 1.429 + * Search the current stack frame if spindex is JSDVG_SEARCH_STACK. Don't 1.430 + * look for v on the stack if spindex is JSDVG_IGNORE_STACK. Otherwise, 1.431 + * spindex is the negative index of v, measured from cx->fp->sp, or from a 1.432 + * lower frame's sp if cx->fp is native. 1.433 + * 1.434 + * The optional argument skipStackHits can be used to skip a hit in the stack 1.435 + * frame. This can be useful in self-hosted code that wants to report value 1.436 + * errors containing decompiled values that are useful for the user, instead of 1.437 + * values used internally by the self-hosted code. 1.438 + * 1.439 + * The caller must call JS_free on the result after a successful call. 1.440 + */ 1.441 +char * 1.442 +DecompileValueGenerator(JSContext *cx, int spindex, HandleValue v, 1.443 + HandleString fallback, int skipStackHits = 0); 1.444 + 1.445 +/* 1.446 + * Decompile the formal argument at formalIndex in the nearest non-builtin 1.447 + * stack frame, falling back with converting v to source. 1.448 + */ 1.449 +char * 1.450 +DecompileArgument(JSContext *cx, int formalIndex, HandleValue v); 1.451 + 1.452 +/* 1.453 + * Sprintf, but with unlimited and automatically allocated buffering. 1.454 + */ 1.455 +class Sprinter 1.456 +{ 1.457 + public: 1.458 + struct InvariantChecker 1.459 + { 1.460 + const Sprinter *parent; 1.461 + 1.462 + explicit InvariantChecker(const Sprinter *p) : parent(p) { 1.463 + parent->checkInvariants(); 1.464 + } 1.465 + 1.466 + ~InvariantChecker() { 1.467 + parent->checkInvariants(); 1.468 + } 1.469 + }; 1.470 + 1.471 + ExclusiveContext *context; /* context executing the decompiler */ 1.472 + 1.473 + private: 1.474 + static const size_t DefaultSize; 1.475 +#ifdef DEBUG 1.476 + bool initialized; /* true if this is initialized, use for debug builds */ 1.477 +#endif 1.478 + char *base; /* malloc'd buffer address */ 1.479 + size_t size; /* size of buffer allocated at base */ 1.480 + ptrdiff_t offset; /* offset of next free char in buffer */ 1.481 + bool reportedOOM; /* this sprinter has reported OOM in string ops */ 1.482 + 1.483 + bool realloc_(size_t newSize); 1.484 + 1.485 + public: 1.486 + explicit Sprinter(ExclusiveContext *cx); 1.487 + ~Sprinter(); 1.488 + 1.489 + /* Initialize this sprinter, returns false on error */ 1.490 + bool init(); 1.491 + 1.492 + void checkInvariants() const; 1.493 + 1.494 + const char *string() const; 1.495 + const char *stringEnd() const; 1.496 + /* Returns the string at offset |off| */ 1.497 + char *stringAt(ptrdiff_t off) const; 1.498 + /* Returns the char at offset |off| */ 1.499 + char &operator[](size_t off); 1.500 + 1.501 + /* 1.502 + * Attempt to reserve len + 1 space (for a trailing nullptr byte). If the 1.503 + * attempt succeeds, return a pointer to the start of that space and adjust the 1.504 + * internal content. The caller *must* completely fill this space on success. 1.505 + */ 1.506 + char *reserve(size_t len); 1.507 + 1.508 + /* 1.509 + * Puts |len| characters from |s| at the current position and return an offset to 1.510 + * the beginning of this new data 1.511 + */ 1.512 + ptrdiff_t put(const char *s, size_t len); 1.513 + ptrdiff_t put(const char *s); 1.514 + ptrdiff_t putString(JSString *str); 1.515 + 1.516 + /* Prints a formatted string into the buffer */ 1.517 + int printf(const char *fmt, ...); 1.518 + 1.519 + ptrdiff_t getOffset() const; 1.520 + 1.521 + /* 1.522 + * Report that a string operation failed to get the memory it requested. The 1.523 + * first call to this function calls JS_ReportOutOfMemory, and sets this 1.524 + * Sprinter's outOfMemory flag; subsequent calls do nothing. 1.525 + */ 1.526 + void reportOutOfMemory(); 1.527 + 1.528 + /* Return true if this Sprinter ran out of memory. */ 1.529 + bool hadOutOfMemory() const; 1.530 +}; 1.531 + 1.532 +extern ptrdiff_t 1.533 +Sprint(Sprinter *sp, const char *format, ...); 1.534 + 1.535 +extern bool 1.536 +CallResultEscapes(jsbytecode *pc); 1.537 + 1.538 +static inline unsigned 1.539 +GetDecomposeLength(jsbytecode *pc, size_t len) 1.540 +{ 1.541 + /* 1.542 + * The last byte of a DECOMPOSE op stores the decomposed length. This is a 1.543 + * constant: perhaps we should just hardcode values instead? 1.544 + */ 1.545 + JS_ASSERT(size_t(js_CodeSpec[*pc].length) == len); 1.546 + return (unsigned) pc[len - 1]; 1.547 +} 1.548 + 1.549 +static inline unsigned 1.550 +GetBytecodeLength(jsbytecode *pc) 1.551 +{ 1.552 + JSOp op = (JSOp)*pc; 1.553 + JS_ASSERT(op < JSOP_LIMIT); 1.554 + 1.555 + if (js_CodeSpec[op].length != -1) 1.556 + return js_CodeSpec[op].length; 1.557 + return js_GetVariableBytecodeLength(pc); 1.558 +} 1.559 + 1.560 +static inline bool 1.561 +BytecodeIsPopped(jsbytecode *pc) 1.562 +{ 1.563 + jsbytecode *next = pc + GetBytecodeLength(pc); 1.564 + return JSOp(*next) == JSOP_POP; 1.565 +} 1.566 + 1.567 +static inline bool 1.568 +BytecodeFlowsToBitop(jsbytecode *pc) 1.569 +{ 1.570 + // Look for simple bytecode for integer conversions like (x | 0) or (x & -1). 1.571 + jsbytecode *next = pc + GetBytecodeLength(pc); 1.572 + if (*next == JSOP_BITOR || *next == JSOP_BITAND) 1.573 + return true; 1.574 + if (*next == JSOP_INT8 && GET_INT8(next) == -1) { 1.575 + next += GetBytecodeLength(next); 1.576 + if (*next == JSOP_BITAND) 1.577 + return true; 1.578 + return false; 1.579 + } 1.580 + if (*next == JSOP_ONE) { 1.581 + next += GetBytecodeLength(next); 1.582 + if (*next == JSOP_NEG) { 1.583 + next += GetBytecodeLength(next); 1.584 + if (*next == JSOP_BITAND) 1.585 + return true; 1.586 + } 1.587 + return false; 1.588 + } 1.589 + if (*next == JSOP_ZERO) { 1.590 + next += GetBytecodeLength(next); 1.591 + if (*next == JSOP_BITOR) 1.592 + return true; 1.593 + return false; 1.594 + } 1.595 + return false; 1.596 +} 1.597 + 1.598 +extern bool 1.599 +IsValidBytecodeOffset(JSContext *cx, JSScript *script, size_t offset); 1.600 + 1.601 +inline bool 1.602 +FlowsIntoNext(JSOp op) 1.603 +{ 1.604 + /* JSOP_YIELD is considered to flow into the next instruction, like JSOP_CALL. */ 1.605 + return op != JSOP_RETRVAL && op != JSOP_RETURN && op != JSOP_THROW && 1.606 + op != JSOP_GOTO && op != JSOP_RETSUB; 1.607 +} 1.608 + 1.609 +inline bool 1.610 +IsArgOp(JSOp op) 1.611 +{ 1.612 + return JOF_OPTYPE(op) == JOF_QARG; 1.613 +} 1.614 + 1.615 +inline bool 1.616 +IsLocalOp(JSOp op) 1.617 +{ 1.618 + return JOF_OPTYPE(op) == JOF_LOCAL; 1.619 +} 1.620 + 1.621 +inline bool 1.622 +IsAliasedVarOp(JSOp op) 1.623 +{ 1.624 + return JOF_OPTYPE(op) == JOF_SCOPECOORD; 1.625 +} 1.626 + 1.627 +inline bool 1.628 +IsGlobalOp(JSOp op) 1.629 +{ 1.630 + return js_CodeSpec[op].format & JOF_GNAME; 1.631 +} 1.632 + 1.633 +inline bool 1.634 +IsEqualityOp(JSOp op) 1.635 +{ 1.636 + return op == JSOP_EQ || op == JSOP_NE || op == JSOP_STRICTEQ || op == JSOP_STRICTNE; 1.637 +} 1.638 + 1.639 +inline bool 1.640 +IsGetPropPC(jsbytecode *pc) 1.641 +{ 1.642 + JSOp op = JSOp(*pc); 1.643 + return op == JSOP_LENGTH || op == JSOP_GETPROP || op == JSOP_CALLPROP; 1.644 +} 1.645 + 1.646 +inline bool 1.647 +IsSetPropPC(jsbytecode *pc) 1.648 +{ 1.649 + JSOp op = JSOp(*pc); 1.650 + return op == JSOP_SETPROP || op == JSOP_SETNAME || op == JSOP_SETGNAME; 1.651 +} 1.652 + 1.653 +inline bool 1.654 +IsGetElemPC(jsbytecode *pc) 1.655 +{ 1.656 + JSOp op = JSOp(*pc); 1.657 + return op == JSOP_GETELEM || op == JSOP_CALLELEM; 1.658 +} 1.659 + 1.660 +inline bool 1.661 +IsSetElemPC(jsbytecode *pc) 1.662 +{ 1.663 + JSOp op = JSOp(*pc); 1.664 + return op == JSOP_SETELEM; 1.665 +} 1.666 + 1.667 +inline bool 1.668 +IsCallPC(jsbytecode *pc) 1.669 +{ 1.670 + return js_CodeSpec[*pc].format & JOF_INVOKE; 1.671 +} 1.672 + 1.673 +static inline int32_t 1.674 +GetBytecodeInteger(jsbytecode *pc) 1.675 +{ 1.676 + switch (JSOp(*pc)) { 1.677 + case JSOP_ZERO: return 0; 1.678 + case JSOP_ONE: return 1; 1.679 + case JSOP_UINT16: return GET_UINT16(pc); 1.680 + case JSOP_UINT24: return GET_UINT24(pc); 1.681 + case JSOP_INT8: return GET_INT8(pc); 1.682 + case JSOP_INT32: return GET_INT32(pc); 1.683 + default: 1.684 + MOZ_ASSUME_UNREACHABLE("Bad op"); 1.685 + } 1.686 +} 1.687 + 1.688 +/* 1.689 + * Counts accumulated for a single opcode in a script. The counts tracked vary 1.690 + * between opcodes, and this structure ensures that counts are accessed in a 1.691 + * coherent fashion. 1.692 + */ 1.693 +class PCCounts 1.694 +{ 1.695 + friend class ::JSScript; 1.696 + double *counts; 1.697 +#ifdef DEBUG 1.698 + size_t capacity; 1.699 +#elif JS_BITS_PER_WORD == 32 1.700 + void *padding; 1.701 +#endif 1.702 + 1.703 + public: 1.704 + 1.705 + enum BaseCounts { 1.706 + BASE_INTERP = 0, 1.707 + 1.708 + BASE_LIMIT 1.709 + }; 1.710 + 1.711 + enum AccessCounts { 1.712 + ACCESS_MONOMORPHIC = BASE_LIMIT, 1.713 + ACCESS_DIMORPHIC, 1.714 + ACCESS_POLYMORPHIC, 1.715 + 1.716 + ACCESS_BARRIER, 1.717 + ACCESS_NOBARRIER, 1.718 + 1.719 + ACCESS_UNDEFINED, 1.720 + ACCESS_NULL, 1.721 + ACCESS_BOOLEAN, 1.722 + ACCESS_INT32, 1.723 + ACCESS_DOUBLE, 1.724 + ACCESS_STRING, 1.725 + ACCESS_OBJECT, 1.726 + 1.727 + ACCESS_LIMIT 1.728 + }; 1.729 + 1.730 + static bool accessOp(JSOp op) { 1.731 + /* 1.732 + * Access ops include all name, element and property reads, as well as 1.733 + * SETELEM and SETPROP (for ElementCounts/PropertyCounts alignment). 1.734 + */ 1.735 + if (op == JSOP_SETELEM || op == JSOP_SETPROP) 1.736 + return true; 1.737 + int format = js_CodeSpec[op].format; 1.738 + return !!(format & (JOF_NAME | JOF_GNAME | JOF_ELEM | JOF_PROP)) 1.739 + && !(format & JOF_SET); 1.740 + } 1.741 + 1.742 + enum ElementCounts { 1.743 + ELEM_ID_INT = ACCESS_LIMIT, 1.744 + ELEM_ID_DOUBLE, 1.745 + ELEM_ID_OTHER, 1.746 + ELEM_ID_UNKNOWN, 1.747 + 1.748 + ELEM_OBJECT_TYPED, 1.749 + ELEM_OBJECT_PACKED, 1.750 + ELEM_OBJECT_DENSE, 1.751 + ELEM_OBJECT_OTHER, 1.752 + 1.753 + ELEM_LIMIT 1.754 + }; 1.755 + 1.756 + static bool elementOp(JSOp op) { 1.757 + return accessOp(op) && (JOF_MODE(js_CodeSpec[op].format) == JOF_ELEM); 1.758 + } 1.759 + 1.760 + enum PropertyCounts { 1.761 + PROP_STATIC = ACCESS_LIMIT, 1.762 + PROP_DEFINITE, 1.763 + PROP_OTHER, 1.764 + 1.765 + PROP_LIMIT 1.766 + }; 1.767 + 1.768 + static bool propertyOp(JSOp op) { 1.769 + return accessOp(op) && (JOF_MODE(js_CodeSpec[op].format) == JOF_PROP); 1.770 + } 1.771 + 1.772 + enum ArithCounts { 1.773 + ARITH_INT = BASE_LIMIT, 1.774 + ARITH_DOUBLE, 1.775 + ARITH_OTHER, 1.776 + ARITH_UNKNOWN, 1.777 + 1.778 + ARITH_LIMIT 1.779 + }; 1.780 + 1.781 + static bool arithOp(JSOp op) { 1.782 + return !!(js_CodeSpec[op].format & JOF_ARITH); 1.783 + } 1.784 + 1.785 + static size_t numCounts(JSOp op) 1.786 + { 1.787 + if (accessOp(op)) { 1.788 + if (elementOp(op)) 1.789 + return ELEM_LIMIT; 1.790 + if (propertyOp(op)) 1.791 + return PROP_LIMIT; 1.792 + return ACCESS_LIMIT; 1.793 + } 1.794 + if (arithOp(op)) 1.795 + return ARITH_LIMIT; 1.796 + return BASE_LIMIT; 1.797 + } 1.798 + 1.799 + static const char *countName(JSOp op, size_t which); 1.800 + 1.801 + double *rawCounts() const { return counts; } 1.802 + 1.803 + double& get(size_t which) { 1.804 + JS_ASSERT(which < capacity); 1.805 + return counts[which]; 1.806 + } 1.807 + 1.808 + /* Boolean conversion, for 'if (counters) ...' */ 1.809 + operator void*() const { 1.810 + return counts; 1.811 + } 1.812 +}; 1.813 + 1.814 +/* Necessary for alignment with the script. */ 1.815 +JS_STATIC_ASSERT(sizeof(PCCounts) % sizeof(Value) == 0); 1.816 + 1.817 +static inline jsbytecode * 1.818 +GetNextPc(jsbytecode *pc) 1.819 +{ 1.820 + return pc + GetBytecodeLength(pc); 1.821 +} 1.822 + 1.823 +} /* namespace js */ 1.824 + 1.825 +#if defined(DEBUG) 1.826 +/* 1.827 + * Disassemblers, for debugging only. 1.828 + */ 1.829 +bool 1.830 +js_Disassemble(JSContext *cx, JS::Handle<JSScript*> script, bool lines, js::Sprinter *sp); 1.831 + 1.832 +unsigned 1.833 +js_Disassemble1(JSContext *cx, JS::Handle<JSScript*> script, jsbytecode *pc, unsigned loc, 1.834 + bool lines, js::Sprinter *sp); 1.835 + 1.836 +#endif 1.837 + 1.838 +void 1.839 +js_DumpPCCounts(JSContext *cx, JS::Handle<JSScript*> script, js::Sprinter *sp); 1.840 + 1.841 +#ifdef JS_ION 1.842 +namespace js { 1.843 +namespace jit { struct IonScriptCounts; } 1.844 +void 1.845 +DumpIonScriptCounts(js::Sprinter *sp, jit::IonScriptCounts *ionCounts); 1.846 +} 1.847 +#endif 1.848 + 1.849 +#endif /* jsopcode_h */