Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* Definitions for javascript analysis. */ |
michael@0 | 8 | |
michael@0 | 9 | #ifndef jsanalyze_h |
michael@0 | 10 | #define jsanalyze_h |
michael@0 | 11 | |
michael@0 | 12 | #include "jscompartment.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace js { |
michael@0 | 15 | namespace analyze { |
michael@0 | 16 | |
michael@0 | 17 | class Bytecode; |
michael@0 | 18 | struct LifetimeVariable; |
michael@0 | 19 | class SlotValue; |
michael@0 | 20 | class SSAValue; |
michael@0 | 21 | struct SSAValueInfo; |
michael@0 | 22 | class SSAUseChain; |
michael@0 | 23 | |
michael@0 | 24 | // Common representation of slots between ScriptAnalysis, TypeScript, and in the |
michael@0 | 25 | // case of TotalSlots, Ion. |
michael@0 | 26 | static inline uint32_t ThisSlot() { |
michael@0 | 27 | return 0; |
michael@0 | 28 | } |
michael@0 | 29 | static inline uint32_t ArgSlot(uint32_t arg) { |
michael@0 | 30 | return 1 + arg; |
michael@0 | 31 | } |
michael@0 | 32 | static inline uint32_t LocalSlot(JSScript *script, uint32_t local) { |
michael@0 | 33 | return 1 + local + |
michael@0 | 34 | (script->functionNonDelazifying() ? script->functionNonDelazifying()->nargs() : 0); |
michael@0 | 35 | } |
michael@0 | 36 | static inline uint32_t TotalSlots(JSScript *script) { |
michael@0 | 37 | return LocalSlot(script, 0) + script->nfixed(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | // Analysis information about a script. FIXME: At this point, the entire |
michael@0 | 41 | // purpose of this class is to compute JSScript::needsArgsObj, and to support |
michael@0 | 42 | // isReachable() in order for jsinfer.cpp:FindPreviousInnerInitializer to get |
michael@0 | 43 | // the previous opcode. For that purpose, it is completely overkill. |
michael@0 | 44 | class ScriptAnalysis |
michael@0 | 45 | { |
michael@0 | 46 | friend class Bytecode; |
michael@0 | 47 | |
michael@0 | 48 | JSScript *script_; |
michael@0 | 49 | |
michael@0 | 50 | Bytecode **codeArray; |
michael@0 | 51 | |
michael@0 | 52 | uint32_t numSlots; |
michael@0 | 53 | |
michael@0 | 54 | bool *escapedSlots; |
michael@0 | 55 | |
michael@0 | 56 | #ifdef DEBUG |
michael@0 | 57 | /* Whether the compartment was in debug mode when we performed the analysis. */ |
michael@0 | 58 | bool originalDebugMode_: 1; |
michael@0 | 59 | #endif |
michael@0 | 60 | |
michael@0 | 61 | /* --------- Bytecode analysis --------- */ |
michael@0 | 62 | |
michael@0 | 63 | bool canTrackVars:1; |
michael@0 | 64 | bool argumentsContentsObserved_:1; |
michael@0 | 65 | |
michael@0 | 66 | /* --------- Lifetime analysis --------- */ |
michael@0 | 67 | |
michael@0 | 68 | LifetimeVariable *lifetimes; |
michael@0 | 69 | |
michael@0 | 70 | public: |
michael@0 | 71 | ScriptAnalysis(JSScript *script) { |
michael@0 | 72 | mozilla::PodZero(this); |
michael@0 | 73 | this->script_ = script; |
michael@0 | 74 | #ifdef DEBUG |
michael@0 | 75 | this->originalDebugMode_ = script_->compartment()->debugMode(); |
michael@0 | 76 | #endif |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 80 | bool analyzeBytecode(JSContext *cx); |
michael@0 | 81 | |
michael@0 | 82 | bool isReachable(const jsbytecode *pc) { return maybeCode(pc); } |
michael@0 | 83 | |
michael@0 | 84 | private: |
michael@0 | 85 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 86 | bool analyzeSSA(JSContext *cx); |
michael@0 | 87 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 88 | bool analyzeLifetimes(JSContext *cx); |
michael@0 | 89 | |
michael@0 | 90 | /* Accessors for bytecode information. */ |
michael@0 | 91 | Bytecode& getCode(uint32_t offset) { |
michael@0 | 92 | JS_ASSERT(offset < script_->length()); |
michael@0 | 93 | JS_ASSERT(codeArray[offset]); |
michael@0 | 94 | return *codeArray[offset]; |
michael@0 | 95 | } |
michael@0 | 96 | Bytecode& getCode(const jsbytecode *pc) { return getCode(script_->pcToOffset(pc)); } |
michael@0 | 97 | |
michael@0 | 98 | Bytecode* maybeCode(uint32_t offset) { |
michael@0 | 99 | JS_ASSERT(offset < script_->length()); |
michael@0 | 100 | return codeArray[offset]; |
michael@0 | 101 | } |
michael@0 | 102 | Bytecode* maybeCode(const jsbytecode *pc) { return maybeCode(script_->pcToOffset(pc)); } |
michael@0 | 103 | |
michael@0 | 104 | inline bool jumpTarget(uint32_t offset); |
michael@0 | 105 | inline bool jumpTarget(const jsbytecode *pc); |
michael@0 | 106 | |
michael@0 | 107 | inline const SSAValue &poppedValue(uint32_t offset, uint32_t which); |
michael@0 | 108 | inline const SSAValue &poppedValue(const jsbytecode *pc, uint32_t which); |
michael@0 | 109 | |
michael@0 | 110 | inline const SlotValue *newValues(uint32_t offset); |
michael@0 | 111 | inline const SlotValue *newValues(const jsbytecode *pc); |
michael@0 | 112 | |
michael@0 | 113 | inline bool trackUseChain(const SSAValue &v); |
michael@0 | 114 | |
michael@0 | 115 | /* |
michael@0 | 116 | * Get the use chain for an SSA value. |
michael@0 | 117 | */ |
michael@0 | 118 | inline SSAUseChain *& useChain(const SSAValue &v); |
michael@0 | 119 | |
michael@0 | 120 | |
michael@0 | 121 | /* For a JSOP_CALL* op, get the pc of the corresponding JSOP_CALL/NEW/etc. */ |
michael@0 | 122 | inline jsbytecode *getCallPC(jsbytecode *pc); |
michael@0 | 123 | |
michael@0 | 124 | /* Accessors for local variable information. */ |
michael@0 | 125 | |
michael@0 | 126 | /* |
michael@0 | 127 | * Escaping slots include all slots that can be accessed in ways other than |
michael@0 | 128 | * through the corresponding LOCAL/ARG opcode. This includes all closed |
michael@0 | 129 | * slots in the script, all slots in scripts which use eval or are in debug |
michael@0 | 130 | * mode, and slots which are aliased by NAME or similar opcodes in the |
michael@0 | 131 | * containing script (which does not imply the variable is closed). |
michael@0 | 132 | */ |
michael@0 | 133 | inline bool slotEscapes(uint32_t slot); |
michael@0 | 134 | |
michael@0 | 135 | /* |
michael@0 | 136 | * Whether we distinguish different writes of this variable while doing |
michael@0 | 137 | * SSA analysis. Escaping locals can be written in other scripts, and the |
michael@0 | 138 | * presence of NAME opcodes which could alias local variables or arguments |
michael@0 | 139 | * keeps us from tracking variable values at each point. |
michael@0 | 140 | */ |
michael@0 | 141 | inline bool trackSlot(uint32_t slot); |
michael@0 | 142 | |
michael@0 | 143 | inline const LifetimeVariable & liveness(uint32_t slot); |
michael@0 | 144 | |
michael@0 | 145 | void printSSA(JSContext *cx); |
michael@0 | 146 | void printTypes(JSContext *cx); |
michael@0 | 147 | |
michael@0 | 148 | /* Bytecode helpers */ |
michael@0 | 149 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 150 | inline bool addJump(JSContext *cx, unsigned offset, |
michael@0 | 151 | unsigned *currentOffset, unsigned *forwardJump, unsigned *forwardLoop, |
michael@0 | 152 | unsigned stackDepth); |
michael@0 | 153 | |
michael@0 | 154 | /* Lifetime helpers */ |
michael@0 | 155 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 156 | inline bool addVariable(JSContext *cx, LifetimeVariable &var, unsigned offset, |
michael@0 | 157 | LifetimeVariable **&saved, unsigned &savedCount); |
michael@0 | 158 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 159 | inline bool killVariable(JSContext *cx, LifetimeVariable &var, unsigned offset, |
michael@0 | 160 | LifetimeVariable **&saved, unsigned &savedCount); |
michael@0 | 161 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 162 | inline bool extendVariable(JSContext *cx, LifetimeVariable &var, unsigned start, unsigned end); |
michael@0 | 163 | |
michael@0 | 164 | inline void ensureVariable(LifetimeVariable &var, unsigned until); |
michael@0 | 165 | |
michael@0 | 166 | /* SSA helpers */ |
michael@0 | 167 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 168 | bool makePhi(JSContext *cx, uint32_t slot, uint32_t offset, SSAValue *pv); |
michael@0 | 169 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 170 | bool insertPhi(JSContext *cx, SSAValue &phi, const SSAValue &v); |
michael@0 | 171 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 172 | bool mergeValue(JSContext *cx, uint32_t offset, const SSAValue &v, SlotValue *pv); |
michael@0 | 173 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 174 | bool checkPendingValue(JSContext *cx, const SSAValue &v, uint32_t slot, |
michael@0 | 175 | Vector<SlotValue> *pending); |
michael@0 | 176 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 177 | bool checkBranchTarget(JSContext *cx, uint32_t targetOffset, Vector<uint32_t> &branchTargets, |
michael@0 | 178 | SSAValueInfo *values, uint32_t stackDepth); |
michael@0 | 179 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 180 | bool checkExceptionTarget(JSContext *cx, uint32_t catchOffset, |
michael@0 | 181 | Vector<uint32_t> &exceptionTargets); |
michael@0 | 182 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 183 | bool mergeBranchTarget(JSContext *cx, SSAValueInfo &value, uint32_t slot, |
michael@0 | 184 | const Vector<uint32_t> &branchTargets, uint32_t currentOffset); |
michael@0 | 185 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 186 | bool mergeExceptionTarget(JSContext *cx, const SSAValue &value, uint32_t slot, |
michael@0 | 187 | const Vector<uint32_t> &exceptionTargets); |
michael@0 | 188 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 189 | bool mergeAllExceptionTargets(JSContext *cx, SSAValueInfo *values, |
michael@0 | 190 | const Vector<uint32_t> &exceptionTargets); |
michael@0 | 191 | MOZ_WARN_UNUSED_RESULT |
michael@0 | 192 | bool freezeNewValues(JSContext *cx, uint32_t offset); |
michael@0 | 193 | |
michael@0 | 194 | typedef Vector<SSAValue, 16> SeenVector; |
michael@0 | 195 | bool needsArgsObj(JSContext *cx, SeenVector &seen, const SSAValue &v); |
michael@0 | 196 | bool needsArgsObj(JSContext *cx, SeenVector &seen, SSAUseChain *use); |
michael@0 | 197 | bool needsArgsObj(JSContext *cx); |
michael@0 | 198 | |
michael@0 | 199 | public: |
michael@0 | 200 | #ifdef DEBUG |
michael@0 | 201 | void assertMatchingDebugMode(); |
michael@0 | 202 | void assertMatchingStackDepthAtOffset(uint32_t offset, uint32_t stackDepth); |
michael@0 | 203 | #else |
michael@0 | 204 | void assertMatchingDebugMode() { } |
michael@0 | 205 | void assertMatchingStackDepthAtOffset(uint32_t offset, uint32_t stackDepth) { } |
michael@0 | 206 | #endif |
michael@0 | 207 | }; |
michael@0 | 208 | |
michael@0 | 209 | #ifdef DEBUG |
michael@0 | 210 | void PrintBytecode(JSContext *cx, HandleScript script, jsbytecode *pc); |
michael@0 | 211 | #endif |
michael@0 | 212 | |
michael@0 | 213 | } /* namespace analyze */ |
michael@0 | 214 | } /* namespace js */ |
michael@0 | 215 | |
michael@0 | 216 | #endif /* jsanalyze_h */ |