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 jsopcodeinlines_h michael@0: #define jsopcodeinlines_h michael@0: michael@0: #include "jsopcode.h" michael@0: michael@0: #include "jsscript.h" michael@0: michael@0: namespace js { michael@0: michael@0: static inline unsigned michael@0: GetDefCount(JSScript *script, unsigned offset) michael@0: { michael@0: jsbytecode *pc = script->offsetToPC(offset); michael@0: michael@0: /* michael@0: * Add an extra pushed value for OR/AND opcodes, so that they are included michael@0: * in the pushed array of stack values for type inference. michael@0: */ michael@0: switch (JSOp(*pc)) { michael@0: case JSOP_OR: michael@0: case JSOP_AND: michael@0: return 1; michael@0: case JSOP_PICK: michael@0: /* michael@0: * Pick pops and pushes how deep it looks in the stack + 1 michael@0: * items. i.e. if the stack were |a b[2] c[1] d[0]|, pick 2 michael@0: * would pop b, c, and d to rearrange the stack to |a c[0] michael@0: * d[1] b[2]|. michael@0: */ michael@0: return pc[1] + 1; michael@0: default: michael@0: return StackDefs(script, pc); michael@0: } michael@0: } michael@0: michael@0: static inline unsigned michael@0: GetUseCount(JSScript *script, unsigned offset) michael@0: { michael@0: jsbytecode *pc = script->offsetToPC(offset); michael@0: michael@0: if (JSOp(*pc) == JSOP_PICK) michael@0: return pc[1] + 1; michael@0: if (js_CodeSpec[*pc].nuses == -1) michael@0: return StackUses(script, pc); michael@0: return js_CodeSpec[*pc].nuses; michael@0: } michael@0: michael@0: static inline JSOp michael@0: ReverseCompareOp(JSOp op) michael@0: { michael@0: switch (op) { michael@0: case JSOP_GT: michael@0: return JSOP_LT; michael@0: case JSOP_GE: michael@0: return JSOP_LE; michael@0: case JSOP_LT: michael@0: return JSOP_GT; michael@0: case JSOP_LE: michael@0: return JSOP_GE; michael@0: case JSOP_EQ: michael@0: case JSOP_NE: michael@0: case JSOP_STRICTEQ: michael@0: case JSOP_STRICTNE: michael@0: return op; michael@0: default: michael@0: MOZ_ASSUME_UNREACHABLE("unrecognized op"); michael@0: } michael@0: } michael@0: michael@0: static inline JSOp michael@0: NegateCompareOp(JSOp op) michael@0: { michael@0: switch (op) { michael@0: case JSOP_GT: michael@0: return JSOP_LE; michael@0: case JSOP_GE: michael@0: return JSOP_LT; michael@0: case JSOP_LT: michael@0: return JSOP_GE; michael@0: case JSOP_LE: michael@0: return JSOP_GT; michael@0: case JSOP_EQ: michael@0: return JSOP_NE; michael@0: case JSOP_NE: michael@0: return JSOP_EQ; michael@0: case JSOP_STRICTNE: michael@0: return JSOP_STRICTEQ; michael@0: case JSOP_STRICTEQ: michael@0: return JSOP_STRICTNE; michael@0: default: michael@0: MOZ_ASSUME_UNREACHABLE("unrecognized op"); michael@0: } michael@0: } michael@0: michael@0: class BytecodeRange { michael@0: public: michael@0: BytecodeRange(JSContext *cx, JSScript *script) michael@0: : script(cx, script), pc(script->code()), end(pc + script->length()) michael@0: {} michael@0: bool empty() const { return pc == end; } michael@0: jsbytecode *frontPC() const { return pc; } michael@0: JSOp frontOpcode() const { return JSOp(*pc); } michael@0: size_t frontOffset() const { return script->pcToOffset(pc); } michael@0: void popFront() { pc += GetBytecodeLength(pc); } michael@0: michael@0: private: michael@0: RootedScript script; michael@0: jsbytecode *pc, *end; michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif /* jsopcodeinlines_h */