1 : /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is the Mozilla SpiderMonkey JaegerMonkey implementation
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Foundation
20 : * Portions created by the Initial Developer are Copyright (C) 2002-2010
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "jsautooplen.h"
40 :
41 : #include "frontend/BytecodeEmitter.h"
42 :
43 : namespace js {
44 :
45 : static inline PropertyName *
46 23861499 : GetNameFromBytecode(JSContext *cx, jsbytecode *pc, JSOp op, const JSCodeSpec &cs)
47 : {
48 23861499 : if (op == JSOP_LENGTH)
49 14326 : return cx->runtime->atomState.lengthAtom;
50 :
51 : // The method JIT's implementation of instanceof contains an internal lookup
52 : // of the prototype property.
53 23847173 : if (op == JSOP_INSTANCEOF)
54 437 : return cx->runtime->atomState.classPrototypeAtom;
55 :
56 23846736 : JSScript *script = cx->stack.currentScript();
57 : PropertyName *name;
58 23846736 : GET_NAME_FROM_BYTECODE(script, pc, 0, name);
59 23846736 : return name;
60 : }
61 :
62 : class BytecodeRange {
63 : public:
64 4823 : BytecodeRange(JSScript *script)
65 4823 : : script(script), pc(script->code), end(pc + script->length) {}
66 17817628 : bool empty() const { return pc == end; }
67 6231563 : jsbytecode *frontPC() const { return pc; }
68 2969977 : JSOp frontOpcode() const { return JSOp(*pc); }
69 11875800 : size_t frontOffset() const { return pc - script->code; }
70 11872851 : void popFront() { pc += GetBytecodeLength(pc); }
71 :
72 : private:
73 : JSScript *script;
74 : jsbytecode *pc, *end;
75 : };
76 :
77 : class SrcNoteLineScanner
78 : {
79 : /* offset of the current JSOp in the bytecode */
80 : ptrdiff_t offset;
81 :
82 : /* next src note to process */
83 : jssrcnote *sn;
84 :
85 : /* line number of the current JSOp */
86 : uint32_t lineno;
87 :
88 : /*
89 : * Is the current op the first one after a line change directive? Note that
90 : * multiple ops may be "first" if a line directive is used to return to a
91 : * previous line (eg, with a for loop increment expression.)
92 : */
93 : bool lineHeader;
94 :
95 : public:
96 131876 : SrcNoteLineScanner(jssrcnote *sn, uint32_t lineno)
97 131876 : : offset(0), sn(sn), lineno(lineno)
98 : {
99 131876 : }
100 :
101 : /*
102 : * This is called repeatedly with always-advancing relpc values. The src
103 : * notes are tuples of <PC offset from prev src note, type, args>. Scan
104 : * through, updating the lineno, until the next src note is for a later
105 : * bytecode.
106 : *
107 : * When looking at the desired PC offset ('relpc'), the op is first in that
108 : * line iff there is a SRC_SETLINE or SRC_NEWLINE src note for that exact
109 : * bytecode.
110 : *
111 : * Note that a single bytecode may have multiple line-modifying notes (even
112 : * though only one should ever be needed.)
113 : */
114 8820573 : void advanceTo(ptrdiff_t relpc) {
115 : // Must always advance! If the same or an earlier PC is erroneously
116 : // passed in, we will already be past the relevant src notes
117 8820573 : JS_ASSERT_IF(offset > 0, relpc > offset);
118 :
119 : // Next src note should be for after the current offset
120 8820573 : JS_ASSERT_IF(offset > 0, SN_IS_TERMINATOR(sn) || SN_DELTA(sn) > 0);
121 :
122 : // The first PC requested is always considered to be a line header
123 8820573 : lineHeader = (offset == 0);
124 :
125 8820573 : if (SN_IS_TERMINATOR(sn))
126 259819 : return;
127 :
128 : ptrdiff_t nextOffset;
129 21993940 : while ((nextOffset = offset + SN_DELTA(sn)) <= relpc && !SN_IS_TERMINATOR(sn)) {
130 4872432 : offset = nextOffset;
131 4872432 : SrcNoteType type = (SrcNoteType) SN_TYPE(sn);
132 4872432 : if (type == SRC_SETLINE || type == SRC_NEWLINE) {
133 1707127 : if (type == SRC_SETLINE)
134 329754 : lineno = js_GetSrcNoteOffset(sn, 0);
135 : else
136 1377373 : lineno++;
137 :
138 1707127 : if (offset == relpc)
139 1147343 : lineHeader = true;
140 : }
141 :
142 4872432 : sn = SN_NEXT(sn);
143 : }
144 : }
145 :
146 141 : bool isLineHeader() const {
147 141 : return lineHeader;
148 : }
149 :
150 0 : uint32_t getLine() const { return lineno; }
151 : };
152 :
153 : }
|