michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef jsopcode_h michael@0: #define jsopcode_h michael@0: michael@0: /* michael@0: * JS bytecode definitions. michael@0: */ michael@0: michael@0: #include "jsbytecode.h" michael@0: #include "jstypes.h" michael@0: #include "NamespaceImports.h" michael@0: michael@0: #include "frontend/SourceNotes.h" michael@0: #include "vm/Opcodes.h" michael@0: michael@0: /* michael@0: * JS operation bytecodes. michael@0: */ michael@0: typedef enum JSOp { michael@0: #define ENUMERATE_OPCODE(op, val, ...) op = val, michael@0: FOR_EACH_OPCODE(ENUMERATE_OPCODE) michael@0: #undef ENUMERATE_OPCODE michael@0: michael@0: JSOP_LIMIT, michael@0: michael@0: /* michael@0: * These pseudo-ops help js_DecompileValueGenerator decompile JSOP_SETPROP, michael@0: * JSOP_SETELEM, and comprehension-tails, respectively. They are never michael@0: * stored in bytecode, so they don't preempt valid opcodes. michael@0: */ michael@0: JSOP_GETPROP2 = JSOP_LIMIT, michael@0: JSOP_GETELEM2 = JSOP_LIMIT + 1, michael@0: JSOP_FORLOCAL = JSOP_LIMIT + 2, michael@0: JSOP_FAKE_LIMIT = JSOP_FORLOCAL michael@0: } JSOp; michael@0: michael@0: /* michael@0: * JS bytecode formats. michael@0: */ michael@0: #define JOF_BYTE 0 /* single bytecode, no immediates */ michael@0: #define JOF_JUMP 1 /* signed 16-bit jump offset immediate */ michael@0: #define JOF_ATOM 2 /* unsigned 16-bit constant index */ michael@0: #define JOF_UINT16 3 /* unsigned 16-bit immediate operand */ michael@0: #define JOF_TABLESWITCH 4 /* table switch */ michael@0: /* 5 is unused */ michael@0: #define JOF_QARG 6 /* quickened get/set function argument ops */ michael@0: #define JOF_LOCAL 7 /* var or block-local variable */ michael@0: #define JOF_DOUBLE 8 /* uint32_t index for double value */ michael@0: #define JOF_UINT24 12 /* extended unsigned 24-bit literal (index) */ michael@0: #define JOF_UINT8 13 /* uint8_t immediate, e.g. top 8 bits of 24-bit michael@0: atom index */ michael@0: #define JOF_INT32 14 /* int32_t immediate operand */ michael@0: #define JOF_OBJECT 15 /* unsigned 16-bit object index */ michael@0: /* 16 is unused */ michael@0: #define JOF_REGEXP 17 /* unsigned 32-bit regexp index */ michael@0: #define JOF_INT8 18 /* int8_t immediate operand */ michael@0: #define JOF_ATOMOBJECT 19 /* uint16_t constant index + object index */ michael@0: /* 20 is unused */ michael@0: #define JOF_SCOPECOORD 21 /* embedded ScopeCoordinate immediate */ michael@0: #define JOF_TYPEMASK 0x001f /* mask for above immediate types */ michael@0: michael@0: #define JOF_NAME (1U<<5) /* name operation */ michael@0: #define JOF_PROP (2U<<5) /* obj.prop operation */ michael@0: #define JOF_ELEM (3U<<5) /* obj[index] operation */ michael@0: #define JOF_MODEMASK (7U<<5) /* mask for above addressing modes */ michael@0: #define JOF_SET (1U<<8) /* set (i.e., assignment) operation */ michael@0: /* (1U<<9) is unused*/ michael@0: /* (1U<<10) is unused*/ michael@0: /* (1U<<11) is unused*/ michael@0: /* (1U<<12) is unused*/ michael@0: /* (1U<<13) is unused*/ michael@0: #define JOF_DETECTING (1U<<14) /* object detection for warning-quelling */ michael@0: /* (1U<<15) is unused*/ michael@0: #define JOF_LEFTASSOC (1U<<16) /* left-associative operator */ michael@0: /* (1U<<17) is unused */ michael@0: /* (1U<<18) is unused */ michael@0: /* (1U<<19) is unused*/ michael@0: /* (1U<<20) is unused*/ michael@0: #define JOF_INVOKE (1U<<21) /* JSOP_CALL, JSOP_FUNCALL, JSOP_FUNAPPLY, michael@0: JSOP_NEW, JSOP_EVAL */ michael@0: #define JOF_TMPSLOT (1U<<22) /* interpreter uses extra temporary slot michael@0: to root intermediate objects besides michael@0: the slots opcode uses */ michael@0: #define JOF_TMPSLOT2 (2U<<22) /* interpreter uses extra 2 temporary slot michael@0: besides the slots opcode uses */ michael@0: #define JOF_TMPSLOT3 (3U<<22) /* interpreter uses extra 3 temporary slot michael@0: besides the slots opcode uses */ michael@0: #define JOF_TMPSLOT_SHIFT 22 michael@0: #define JOF_TMPSLOT_MASK (JS_BITMASK(2) << JOF_TMPSLOT_SHIFT) michael@0: michael@0: /* (1U<<24) is unused */ michael@0: #define JOF_GNAME (1U<<25) /* predicted global name */ michael@0: #define JOF_TYPESET (1U<<26) /* has an entry in a script's type sets */ michael@0: #define JOF_ARITH (1U<<27) /* unary or binary arithmetic opcode */ michael@0: michael@0: /* Shorthands for type from format and type from opcode. */ michael@0: #define JOF_TYPE(fmt) ((fmt) & JOF_TYPEMASK) michael@0: #define JOF_OPTYPE(op) JOF_TYPE(js_CodeSpec[op].format) michael@0: michael@0: /* Shorthands for mode from format and mode from opcode. */ michael@0: #define JOF_MODE(fmt) ((fmt) & JOF_MODEMASK) michael@0: #define JOF_OPMODE(op) JOF_MODE(js_CodeSpec[op].format) michael@0: michael@0: /* michael@0: * Immediate operand getters, setters, and bounds. michael@0: */ michael@0: michael@0: static MOZ_ALWAYS_INLINE uint8_t michael@0: GET_UINT8(jsbytecode *pc) michael@0: { michael@0: return (uint8_t) pc[1]; michael@0: } michael@0: michael@0: static MOZ_ALWAYS_INLINE void michael@0: SET_UINT8(jsbytecode *pc, uint8_t u) michael@0: { michael@0: pc[1] = (jsbytecode) u; michael@0: } michael@0: michael@0: /* Common uint16_t immediate format helpers. */ michael@0: #define UINT16_LEN 2 michael@0: #define UINT16_HI(i) ((jsbytecode)((i) >> 8)) michael@0: #define UINT16_LO(i) ((jsbytecode)(i)) michael@0: #define GET_UINT16(pc) ((unsigned)(((pc)[1] << 8) | (pc)[2])) michael@0: #define SET_UINT16(pc,i) ((pc)[1] = UINT16_HI(i), (pc)[2] = UINT16_LO(i)) michael@0: #define UINT16_LIMIT ((unsigned)1 << 16) michael@0: michael@0: /* Helpers for accessing the offsets of jump opcodes. */ michael@0: #define JUMP_OFFSET_LEN 4 michael@0: #define JUMP_OFFSET_MIN INT32_MIN michael@0: #define JUMP_OFFSET_MAX INT32_MAX michael@0: michael@0: static MOZ_ALWAYS_INLINE int32_t michael@0: GET_JUMP_OFFSET(jsbytecode *pc) michael@0: { michael@0: return (pc[1] << 24) | (pc[2] << 16) | (pc[3] << 8) | pc[4]; michael@0: } michael@0: michael@0: static MOZ_ALWAYS_INLINE void michael@0: SET_JUMP_OFFSET(jsbytecode *pc, int32_t off) michael@0: { michael@0: pc[1] = (jsbytecode)(off >> 24); michael@0: pc[2] = (jsbytecode)(off >> 16); michael@0: pc[3] = (jsbytecode)(off >> 8); michael@0: pc[4] = (jsbytecode)off; michael@0: } michael@0: michael@0: #define UINT32_INDEX_LEN 4 michael@0: michael@0: static MOZ_ALWAYS_INLINE uint32_t michael@0: GET_UINT32_INDEX(const jsbytecode *pc) michael@0: { michael@0: return (pc[1] << 24) | (pc[2] << 16) | (pc[3] << 8) | pc[4]; michael@0: } michael@0: michael@0: static MOZ_ALWAYS_INLINE void michael@0: SET_UINT32_INDEX(jsbytecode *pc, uint32_t index) michael@0: { michael@0: pc[1] = (jsbytecode)(index >> 24); michael@0: pc[2] = (jsbytecode)(index >> 16); michael@0: pc[3] = (jsbytecode)(index >> 8); michael@0: pc[4] = (jsbytecode)index; michael@0: } michael@0: michael@0: #define UINT24_HI(i) ((jsbytecode)((i) >> 16)) michael@0: #define UINT24_MID(i) ((jsbytecode)((i) >> 8)) michael@0: #define UINT24_LO(i) ((jsbytecode)(i)) michael@0: #define GET_UINT24(pc) ((unsigned)(((pc)[1] << 16) | \ michael@0: ((pc)[2] << 8) | \ michael@0: (pc)[3])) michael@0: #define SET_UINT24(pc,i) ((pc)[1] = UINT24_HI(i), \ michael@0: (pc)[2] = UINT24_MID(i), \ michael@0: (pc)[3] = UINT24_LO(i)) michael@0: michael@0: #define GET_INT8(pc) (int8_t((pc)[1])) michael@0: michael@0: #define GET_INT32(pc) (((uint32_t((pc)[1]) << 24) | \ michael@0: (uint32_t((pc)[2]) << 16) | \ michael@0: (uint32_t((pc)[3]) << 8) | \ michael@0: uint32_t((pc)[4]))) michael@0: #define SET_INT32(pc,i) ((pc)[1] = (jsbytecode)(uint32_t(i) >> 24), \ michael@0: (pc)[2] = (jsbytecode)(uint32_t(i) >> 16), \ michael@0: (pc)[3] = (jsbytecode)(uint32_t(i) >> 8), \ michael@0: (pc)[4] = (jsbytecode)uint32_t(i)) michael@0: michael@0: /* Index limit is determined by SN_4BYTE_OFFSET_FLAG, see frontend/BytecodeEmitter.h. */ michael@0: #define INDEX_LIMIT_LOG2 31 michael@0: #define INDEX_LIMIT (uint32_t(1) << INDEX_LIMIT_LOG2) michael@0: michael@0: #define ARGC_HI(argc) UINT16_HI(argc) michael@0: #define ARGC_LO(argc) UINT16_LO(argc) michael@0: #define GET_ARGC(pc) GET_UINT16(pc) michael@0: #define ARGC_LIMIT UINT16_LIMIT michael@0: michael@0: #define GET_ARGNO(pc) GET_UINT16(pc) michael@0: #define SET_ARGNO(pc,argno) SET_UINT16(pc,argno) michael@0: #define ARGNO_LEN 2 michael@0: #define ARGNO_LIMIT UINT16_LIMIT michael@0: michael@0: #define GET_LOCALNO(pc) GET_UINT24(pc) michael@0: #define SET_LOCALNO(pc,varno) SET_UINT24(pc,varno) michael@0: #define LOCALNO_LEN 3 michael@0: #define LOCALNO_BITS 24 michael@0: #define LOCALNO_LIMIT (1 << LOCALNO_BITS) michael@0: michael@0: static inline unsigned michael@0: LoopEntryDepthHint(jsbytecode *pc) michael@0: { michael@0: JS_ASSERT(*pc == JSOP_LOOPENTRY); michael@0: return GET_UINT8(pc) & 0x7f; michael@0: } michael@0: static inline bool michael@0: LoopEntryCanIonOsr(jsbytecode *pc) michael@0: { michael@0: JS_ASSERT(*pc == JSOP_LOOPENTRY); michael@0: return GET_UINT8(pc) & 0x80; michael@0: } michael@0: static inline uint8_t michael@0: PackLoopEntryDepthHintAndFlags(unsigned loopDepth, bool canIonOsr) michael@0: { michael@0: return (loopDepth < 0x80 ? uint8_t(loopDepth) : 0x7f) | (canIonOsr ? 0x80 : 0); michael@0: } michael@0: michael@0: /* michael@0: * Describes the 'hops' component of a JOF_SCOPECOORD opcode. michael@0: * michael@0: * Note: this component is only 8 bits wide, limiting the maximum number of michael@0: * scopes between a use and def to roughly 255. This is a pretty small limit but michael@0: * note that SpiderMonkey's recursive descent parser can only parse about this michael@0: * many functions before hitting the C-stack recursion limit so this shouldn't michael@0: * be a significant limitation in practice. michael@0: */ michael@0: #define GET_SCOPECOORD_HOPS(pc) GET_UINT8(pc) michael@0: #define SET_SCOPECOORD_HOPS(pc,hops) SET_UINT8(pc,hops) michael@0: #define SCOPECOORD_HOPS_LEN 1 michael@0: #define SCOPECOORD_HOPS_BITS 8 michael@0: #define SCOPECOORD_HOPS_LIMIT (1 << SCOPECOORD_HOPS_BITS) michael@0: michael@0: /* Describes the 'slot' component of a JOF_SCOPECOORD opcode. */ michael@0: #define GET_SCOPECOORD_SLOT(pc) GET_UINT24(pc) michael@0: #define SET_SCOPECOORD_SLOT(pc,slot) SET_UINT24(pc,slot) michael@0: #define SCOPECOORD_SLOT_LEN 3 michael@0: #define SCOPECOORD_SLOT_BITS 24 michael@0: #define SCOPECOORD_SLOT_LIMIT (1 << SCOPECOORD_SLOT_BITS) michael@0: michael@0: struct JSCodeSpec { michael@0: int8_t length; /* length including opcode byte */ michael@0: int8_t nuses; /* arity, -1 if variadic */ michael@0: int8_t ndefs; /* number of stack results */ michael@0: uint32_t format; /* immediate operand format */ michael@0: michael@0: uint32_t type() const { return JOF_TYPE(format); } michael@0: }; michael@0: michael@0: extern const JSCodeSpec js_CodeSpec[]; michael@0: extern const unsigned js_NumCodeSpecs; michael@0: extern const char * const js_CodeName[]; michael@0: extern const char js_EscapeMap[]; michael@0: michael@0: /* Silence unreferenced formal parameter warnings */ michael@0: #ifdef _MSC_VER michael@0: #pragma warning(push) michael@0: #pragma warning(disable:4100) michael@0: #endif michael@0: michael@0: /* michael@0: * Return a GC'ed string containing the chars in str, with any non-printing michael@0: * chars or quotes (' or " as specified by the quote argument) escaped, and michael@0: * with the quote character at the beginning and end of the result string. michael@0: */ michael@0: extern JSString * michael@0: js_QuoteString(js::ExclusiveContext *cx, JSString *str, jschar quote); michael@0: michael@0: namespace js { michael@0: michael@0: static inline bool michael@0: IsJumpOpcode(JSOp op) michael@0: { michael@0: uint32_t type = JOF_TYPE(js_CodeSpec[op].format); michael@0: michael@0: /* michael@0: * LABEL opcodes have type JOF_JUMP but are no-ops, don't treat them as michael@0: * jumps to avoid degrading precision. michael@0: */ michael@0: return type == JOF_JUMP && op != JSOP_LABEL; michael@0: } michael@0: michael@0: static inline bool michael@0: BytecodeFallsThrough(JSOp op) michael@0: { michael@0: switch (op) { michael@0: case JSOP_GOTO: michael@0: case JSOP_DEFAULT: michael@0: case JSOP_RETURN: michael@0: case JSOP_RETRVAL: michael@0: case JSOP_THROW: michael@0: case JSOP_TABLESWITCH: michael@0: return false; michael@0: case JSOP_GOSUB: michael@0: /* These fall through indirectly, after executing a 'finally'. */ michael@0: return true; michael@0: default: michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: class SrcNoteLineScanner michael@0: { michael@0: /* offset of the current JSOp in the bytecode */ michael@0: ptrdiff_t offset; michael@0: michael@0: /* next src note to process */ michael@0: jssrcnote *sn; michael@0: michael@0: /* line number of the current JSOp */ michael@0: uint32_t lineno; michael@0: michael@0: /* michael@0: * Is the current op the first one after a line change directive? Note that michael@0: * multiple ops may be "first" if a line directive is used to return to a michael@0: * previous line (eg, with a for loop increment expression.) michael@0: */ michael@0: bool lineHeader; michael@0: michael@0: public: michael@0: SrcNoteLineScanner(jssrcnote *sn, uint32_t lineno) michael@0: : offset(0), sn(sn), lineno(lineno) michael@0: { michael@0: } michael@0: michael@0: /* michael@0: * This is called repeatedly with always-advancing relpc values. The src michael@0: * notes are tuples of . Scan michael@0: * through, updating the lineno, until the next src note is for a later michael@0: * bytecode. michael@0: * michael@0: * When looking at the desired PC offset ('relpc'), the op is first in that michael@0: * line iff there is a SRC_SETLINE or SRC_NEWLINE src note for that exact michael@0: * bytecode. michael@0: * michael@0: * Note that a single bytecode may have multiple line-modifying notes (even michael@0: * though only one should ever be needed.) michael@0: */ michael@0: void advanceTo(ptrdiff_t relpc) { michael@0: // Must always advance! If the same or an earlier PC is erroneously michael@0: // passed in, we will already be past the relevant src notes michael@0: JS_ASSERT_IF(offset > 0, relpc > offset); michael@0: michael@0: // Next src note should be for after the current offset michael@0: JS_ASSERT_IF(offset > 0, SN_IS_TERMINATOR(sn) || SN_DELTA(sn) > 0); michael@0: michael@0: // The first PC requested is always considered to be a line header michael@0: lineHeader = (offset == 0); michael@0: michael@0: if (SN_IS_TERMINATOR(sn)) michael@0: return; michael@0: michael@0: ptrdiff_t nextOffset; michael@0: while ((nextOffset = offset + SN_DELTA(sn)) <= relpc && !SN_IS_TERMINATOR(sn)) { michael@0: offset = nextOffset; michael@0: SrcNoteType type = (SrcNoteType) SN_TYPE(sn); michael@0: if (type == SRC_SETLINE || type == SRC_NEWLINE) { michael@0: if (type == SRC_SETLINE) michael@0: lineno = js_GetSrcNoteOffset(sn, 0); michael@0: else michael@0: lineno++; michael@0: michael@0: if (offset == relpc) michael@0: lineHeader = true; michael@0: } michael@0: michael@0: sn = SN_NEXT(sn); michael@0: } michael@0: } michael@0: michael@0: bool isLineHeader() const { michael@0: return lineHeader; michael@0: } michael@0: michael@0: uint32_t getLine() const { return lineno; } michael@0: }; michael@0: michael@0: extern unsigned michael@0: StackUses(JSScript *script, jsbytecode *pc); michael@0: michael@0: extern unsigned michael@0: StackDefs(JSScript *script, jsbytecode *pc); michael@0: michael@0: #ifdef DEBUG michael@0: /* michael@0: * Given bytecode address pc in script's main program code, compute the operand michael@0: * stack depth just before (JSOp) *pc executes. If *pc is not reachable, return michael@0: * false. michael@0: */ michael@0: extern bool michael@0: ReconstructStackDepth(JSContext *cx, JSScript *script, jsbytecode *pc, uint32_t *depth, bool *reachablePC); michael@0: #endif michael@0: michael@0: } /* namespace js */ michael@0: michael@0: #ifdef _MSC_VER michael@0: #pragma warning(pop) michael@0: #endif michael@0: michael@0: #define JSDVG_IGNORE_STACK 0 michael@0: #define JSDVG_SEARCH_STACK 1 michael@0: michael@0: /* michael@0: * Get the length of variable-length bytecode like JSOP_TABLESWITCH. michael@0: */ michael@0: extern size_t michael@0: js_GetVariableBytecodeLength(jsbytecode *pc); michael@0: michael@0: namespace js { michael@0: michael@0: /* michael@0: * Find the source expression that resulted in v, and return a newly allocated michael@0: * C-string containing it. Fall back on v's string conversion (fallback) if we michael@0: * can't find the bytecode that generated and pushed v on the operand stack. michael@0: * michael@0: * Search the current stack frame if spindex is JSDVG_SEARCH_STACK. Don't michael@0: * look for v on the stack if spindex is JSDVG_IGNORE_STACK. Otherwise, michael@0: * spindex is the negative index of v, measured from cx->fp->sp, or from a michael@0: * lower frame's sp if cx->fp is native. michael@0: * michael@0: * The optional argument skipStackHits can be used to skip a hit in the stack michael@0: * frame. This can be useful in self-hosted code that wants to report value michael@0: * errors containing decompiled values that are useful for the user, instead of michael@0: * values used internally by the self-hosted code. michael@0: * michael@0: * The caller must call JS_free on the result after a successful call. michael@0: */ michael@0: char * michael@0: DecompileValueGenerator(JSContext *cx, int spindex, HandleValue v, michael@0: HandleString fallback, int skipStackHits = 0); michael@0: michael@0: /* michael@0: * Decompile the formal argument at formalIndex in the nearest non-builtin michael@0: * stack frame, falling back with converting v to source. michael@0: */ michael@0: char * michael@0: DecompileArgument(JSContext *cx, int formalIndex, HandleValue v); michael@0: michael@0: /* michael@0: * Sprintf, but with unlimited and automatically allocated buffering. michael@0: */ michael@0: class Sprinter michael@0: { michael@0: public: michael@0: struct InvariantChecker michael@0: { michael@0: const Sprinter *parent; michael@0: michael@0: explicit InvariantChecker(const Sprinter *p) : parent(p) { michael@0: parent->checkInvariants(); michael@0: } michael@0: michael@0: ~InvariantChecker() { michael@0: parent->checkInvariants(); michael@0: } michael@0: }; michael@0: michael@0: ExclusiveContext *context; /* context executing the decompiler */ michael@0: michael@0: private: michael@0: static const size_t DefaultSize; michael@0: #ifdef DEBUG michael@0: bool initialized; /* true if this is initialized, use for debug builds */ michael@0: #endif michael@0: char *base; /* malloc'd buffer address */ michael@0: size_t size; /* size of buffer allocated at base */ michael@0: ptrdiff_t offset; /* offset of next free char in buffer */ michael@0: bool reportedOOM; /* this sprinter has reported OOM in string ops */ michael@0: michael@0: bool realloc_(size_t newSize); michael@0: michael@0: public: michael@0: explicit Sprinter(ExclusiveContext *cx); michael@0: ~Sprinter(); michael@0: michael@0: /* Initialize this sprinter, returns false on error */ michael@0: bool init(); michael@0: michael@0: void checkInvariants() const; michael@0: michael@0: const char *string() const; michael@0: const char *stringEnd() const; michael@0: /* Returns the string at offset |off| */ michael@0: char *stringAt(ptrdiff_t off) const; michael@0: /* Returns the char at offset |off| */ michael@0: char &operator[](size_t off); michael@0: michael@0: /* michael@0: * Attempt to reserve len + 1 space (for a trailing nullptr byte). If the michael@0: * attempt succeeds, return a pointer to the start of that space and adjust the michael@0: * internal content. The caller *must* completely fill this space on success. michael@0: */ michael@0: char *reserve(size_t len); michael@0: michael@0: /* michael@0: * Puts |len| characters from |s| at the current position and return an offset to michael@0: * the beginning of this new data michael@0: */ michael@0: ptrdiff_t put(const char *s, size_t len); michael@0: ptrdiff_t put(const char *s); michael@0: ptrdiff_t putString(JSString *str); michael@0: michael@0: /* Prints a formatted string into the buffer */ michael@0: int printf(const char *fmt, ...); michael@0: michael@0: ptrdiff_t getOffset() const; michael@0: michael@0: /* michael@0: * Report that a string operation failed to get the memory it requested. The michael@0: * first call to this function calls JS_ReportOutOfMemory, and sets this michael@0: * Sprinter's outOfMemory flag; subsequent calls do nothing. michael@0: */ michael@0: void reportOutOfMemory(); michael@0: michael@0: /* Return true if this Sprinter ran out of memory. */ michael@0: bool hadOutOfMemory() const; michael@0: }; michael@0: michael@0: extern ptrdiff_t michael@0: Sprint(Sprinter *sp, const char *format, ...); michael@0: michael@0: extern bool michael@0: CallResultEscapes(jsbytecode *pc); michael@0: michael@0: static inline unsigned michael@0: GetDecomposeLength(jsbytecode *pc, size_t len) michael@0: { michael@0: /* michael@0: * The last byte of a DECOMPOSE op stores the decomposed length. This is a michael@0: * constant: perhaps we should just hardcode values instead? michael@0: */ michael@0: JS_ASSERT(size_t(js_CodeSpec[*pc].length) == len); michael@0: return (unsigned) pc[len - 1]; michael@0: } michael@0: michael@0: static inline unsigned michael@0: GetBytecodeLength(jsbytecode *pc) michael@0: { michael@0: JSOp op = (JSOp)*pc; michael@0: JS_ASSERT(op < JSOP_LIMIT); michael@0: michael@0: if (js_CodeSpec[op].length != -1) michael@0: return js_CodeSpec[op].length; michael@0: return js_GetVariableBytecodeLength(pc); michael@0: } michael@0: michael@0: static inline bool michael@0: BytecodeIsPopped(jsbytecode *pc) michael@0: { michael@0: jsbytecode *next = pc + GetBytecodeLength(pc); michael@0: return JSOp(*next) == JSOP_POP; michael@0: } michael@0: michael@0: static inline bool michael@0: BytecodeFlowsToBitop(jsbytecode *pc) michael@0: { michael@0: // Look for simple bytecode for integer conversions like (x | 0) or (x & -1). michael@0: jsbytecode *next = pc + GetBytecodeLength(pc); michael@0: if (*next == JSOP_BITOR || *next == JSOP_BITAND) michael@0: return true; michael@0: if (*next == JSOP_INT8 && GET_INT8(next) == -1) { michael@0: next += GetBytecodeLength(next); michael@0: if (*next == JSOP_BITAND) michael@0: return true; michael@0: return false; michael@0: } michael@0: if (*next == JSOP_ONE) { michael@0: next += GetBytecodeLength(next); michael@0: if (*next == JSOP_NEG) { michael@0: next += GetBytecodeLength(next); michael@0: if (*next == JSOP_BITAND) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: if (*next == JSOP_ZERO) { michael@0: next += GetBytecodeLength(next); michael@0: if (*next == JSOP_BITOR) michael@0: return true; michael@0: return false; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: extern bool michael@0: IsValidBytecodeOffset(JSContext *cx, JSScript *script, size_t offset); michael@0: michael@0: inline bool michael@0: FlowsIntoNext(JSOp op) michael@0: { michael@0: /* JSOP_YIELD is considered to flow into the next instruction, like JSOP_CALL. */ michael@0: return op != JSOP_RETRVAL && op != JSOP_RETURN && op != JSOP_THROW && michael@0: op != JSOP_GOTO && op != JSOP_RETSUB; michael@0: } michael@0: michael@0: inline bool michael@0: IsArgOp(JSOp op) michael@0: { michael@0: return JOF_OPTYPE(op) == JOF_QARG; michael@0: } michael@0: michael@0: inline bool michael@0: IsLocalOp(JSOp op) michael@0: { michael@0: return JOF_OPTYPE(op) == JOF_LOCAL; michael@0: } michael@0: michael@0: inline bool michael@0: IsAliasedVarOp(JSOp op) michael@0: { michael@0: return JOF_OPTYPE(op) == JOF_SCOPECOORD; michael@0: } michael@0: michael@0: inline bool michael@0: IsGlobalOp(JSOp op) michael@0: { michael@0: return js_CodeSpec[op].format & JOF_GNAME; michael@0: } michael@0: michael@0: inline bool michael@0: IsEqualityOp(JSOp op) michael@0: { michael@0: return op == JSOP_EQ || op == JSOP_NE || op == JSOP_STRICTEQ || op == JSOP_STRICTNE; michael@0: } michael@0: michael@0: inline bool michael@0: IsGetPropPC(jsbytecode *pc) michael@0: { michael@0: JSOp op = JSOp(*pc); michael@0: return op == JSOP_LENGTH || op == JSOP_GETPROP || op == JSOP_CALLPROP; michael@0: } michael@0: michael@0: inline bool michael@0: IsSetPropPC(jsbytecode *pc) michael@0: { michael@0: JSOp op = JSOp(*pc); michael@0: return op == JSOP_SETPROP || op == JSOP_SETNAME || op == JSOP_SETGNAME; michael@0: } michael@0: michael@0: inline bool michael@0: IsGetElemPC(jsbytecode *pc) michael@0: { michael@0: JSOp op = JSOp(*pc); michael@0: return op == JSOP_GETELEM || op == JSOP_CALLELEM; michael@0: } michael@0: michael@0: inline bool michael@0: IsSetElemPC(jsbytecode *pc) michael@0: { michael@0: JSOp op = JSOp(*pc); michael@0: return op == JSOP_SETELEM; michael@0: } michael@0: michael@0: inline bool michael@0: IsCallPC(jsbytecode *pc) michael@0: { michael@0: return js_CodeSpec[*pc].format & JOF_INVOKE; michael@0: } michael@0: michael@0: static inline int32_t michael@0: GetBytecodeInteger(jsbytecode *pc) michael@0: { michael@0: switch (JSOp(*pc)) { michael@0: case JSOP_ZERO: return 0; michael@0: case JSOP_ONE: return 1; michael@0: case JSOP_UINT16: return GET_UINT16(pc); michael@0: case JSOP_UINT24: return GET_UINT24(pc); michael@0: case JSOP_INT8: return GET_INT8(pc); michael@0: case JSOP_INT32: return GET_INT32(pc); michael@0: default: michael@0: MOZ_ASSUME_UNREACHABLE("Bad op"); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Counts accumulated for a single opcode in a script. The counts tracked vary michael@0: * between opcodes, and this structure ensures that counts are accessed in a michael@0: * coherent fashion. michael@0: */ michael@0: class PCCounts michael@0: { michael@0: friend class ::JSScript; michael@0: double *counts; michael@0: #ifdef DEBUG michael@0: size_t capacity; michael@0: #elif JS_BITS_PER_WORD == 32 michael@0: void *padding; michael@0: #endif michael@0: michael@0: public: michael@0: michael@0: enum BaseCounts { michael@0: BASE_INTERP = 0, michael@0: michael@0: BASE_LIMIT michael@0: }; michael@0: michael@0: enum AccessCounts { michael@0: ACCESS_MONOMORPHIC = BASE_LIMIT, michael@0: ACCESS_DIMORPHIC, michael@0: ACCESS_POLYMORPHIC, michael@0: michael@0: ACCESS_BARRIER, michael@0: ACCESS_NOBARRIER, michael@0: michael@0: ACCESS_UNDEFINED, michael@0: ACCESS_NULL, michael@0: ACCESS_BOOLEAN, michael@0: ACCESS_INT32, michael@0: ACCESS_DOUBLE, michael@0: ACCESS_STRING, michael@0: ACCESS_OBJECT, michael@0: michael@0: ACCESS_LIMIT michael@0: }; michael@0: michael@0: static bool accessOp(JSOp op) { michael@0: /* michael@0: * Access ops include all name, element and property reads, as well as michael@0: * SETELEM and SETPROP (for ElementCounts/PropertyCounts alignment). michael@0: */ michael@0: if (op == JSOP_SETELEM || op == JSOP_SETPROP) michael@0: return true; michael@0: int format = js_CodeSpec[op].format; michael@0: return !!(format & (JOF_NAME | JOF_GNAME | JOF_ELEM | JOF_PROP)) michael@0: && !(format & JOF_SET); michael@0: } michael@0: michael@0: enum ElementCounts { michael@0: ELEM_ID_INT = ACCESS_LIMIT, michael@0: ELEM_ID_DOUBLE, michael@0: ELEM_ID_OTHER, michael@0: ELEM_ID_UNKNOWN, michael@0: michael@0: ELEM_OBJECT_TYPED, michael@0: ELEM_OBJECT_PACKED, michael@0: ELEM_OBJECT_DENSE, michael@0: ELEM_OBJECT_OTHER, michael@0: michael@0: ELEM_LIMIT michael@0: }; michael@0: michael@0: static bool elementOp(JSOp op) { michael@0: return accessOp(op) && (JOF_MODE(js_CodeSpec[op].format) == JOF_ELEM); michael@0: } michael@0: michael@0: enum PropertyCounts { michael@0: PROP_STATIC = ACCESS_LIMIT, michael@0: PROP_DEFINITE, michael@0: PROP_OTHER, michael@0: michael@0: PROP_LIMIT michael@0: }; michael@0: michael@0: static bool propertyOp(JSOp op) { michael@0: return accessOp(op) && (JOF_MODE(js_CodeSpec[op].format) == JOF_PROP); michael@0: } michael@0: michael@0: enum ArithCounts { michael@0: ARITH_INT = BASE_LIMIT, michael@0: ARITH_DOUBLE, michael@0: ARITH_OTHER, michael@0: ARITH_UNKNOWN, michael@0: michael@0: ARITH_LIMIT michael@0: }; michael@0: michael@0: static bool arithOp(JSOp op) { michael@0: return !!(js_CodeSpec[op].format & JOF_ARITH); michael@0: } michael@0: michael@0: static size_t numCounts(JSOp op) michael@0: { michael@0: if (accessOp(op)) { michael@0: if (elementOp(op)) michael@0: return ELEM_LIMIT; michael@0: if (propertyOp(op)) michael@0: return PROP_LIMIT; michael@0: return ACCESS_LIMIT; michael@0: } michael@0: if (arithOp(op)) michael@0: return ARITH_LIMIT; michael@0: return BASE_LIMIT; michael@0: } michael@0: michael@0: static const char *countName(JSOp op, size_t which); michael@0: michael@0: double *rawCounts() const { return counts; } michael@0: michael@0: double& get(size_t which) { michael@0: JS_ASSERT(which < capacity); michael@0: return counts[which]; michael@0: } michael@0: michael@0: /* Boolean conversion, for 'if (counters) ...' */ michael@0: operator void*() const { michael@0: return counts; michael@0: } michael@0: }; michael@0: michael@0: /* Necessary for alignment with the script. */ michael@0: JS_STATIC_ASSERT(sizeof(PCCounts) % sizeof(Value) == 0); michael@0: michael@0: static inline jsbytecode * michael@0: GetNextPc(jsbytecode *pc) michael@0: { michael@0: return pc + GetBytecodeLength(pc); michael@0: } michael@0: michael@0: } /* namespace js */ michael@0: michael@0: #if defined(DEBUG) michael@0: /* michael@0: * Disassemblers, for debugging only. michael@0: */ michael@0: bool michael@0: js_Disassemble(JSContext *cx, JS::Handle script, bool lines, js::Sprinter *sp); michael@0: michael@0: unsigned michael@0: js_Disassemble1(JSContext *cx, JS::Handle script, jsbytecode *pc, unsigned loc, michael@0: bool lines, js::Sprinter *sp); michael@0: michael@0: #endif michael@0: michael@0: void michael@0: js_DumpPCCounts(JSContext *cx, JS::Handle script, js::Sprinter *sp); michael@0: michael@0: #ifdef JS_ION michael@0: namespace js { michael@0: namespace jit { struct IonScriptCounts; } michael@0: void michael@0: DumpIonScriptCounts(js::Sprinter *sp, jit::IonScriptCounts *ionCounts); michael@0: } michael@0: #endif michael@0: michael@0: #endif /* jsopcode_h */