]> git.saurik.com Git - apple/javascriptcore.git/blame_incremental - bytecode/Opcode.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / bytecode / Opcode.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef Opcode_h
31#define Opcode_h
32
33#include <algorithm>
34#include <string.h>
35
36#include <wtf/Assertions.h>
37
38namespace JSC {
39
40 #define FOR_EACH_OPCODE_ID(macro) \
41 macro(op_enter, 1) \
42 macro(op_create_activation, 2) \
43 macro(op_init_lazy_reg, 2) \
44 macro(op_create_arguments, 2) \
45 macro(op_create_this, 3) \
46 macro(op_get_callee, 2) \
47 macro(op_convert_this, 2) \
48 \
49 macro(op_new_object, 2) \
50 macro(op_new_array, 4) \
51 macro(op_new_array_buffer, 4) \
52 macro(op_new_regexp, 3) \
53 macro(op_mov, 3) \
54 \
55 macro(op_not, 3) \
56 macro(op_eq, 4) \
57 macro(op_eq_null, 3) \
58 macro(op_neq, 4) \
59 macro(op_neq_null, 3) \
60 macro(op_stricteq, 4) \
61 macro(op_nstricteq, 4) \
62 macro(op_less, 4) \
63 macro(op_lesseq, 4) \
64 macro(op_greater, 4) \
65 macro(op_greatereq, 4) \
66 \
67 macro(op_pre_inc, 2) \
68 macro(op_pre_dec, 2) \
69 macro(op_post_inc, 3) \
70 macro(op_post_dec, 3) \
71 macro(op_to_jsnumber, 3) \
72 macro(op_negate, 3) \
73 macro(op_add, 5) \
74 macro(op_mul, 5) \
75 macro(op_div, 5) \
76 macro(op_mod, 4) \
77 macro(op_sub, 5) \
78 \
79 macro(op_lshift, 4) \
80 macro(op_rshift, 4) \
81 macro(op_urshift, 4) \
82 macro(op_bitand, 5) \
83 macro(op_bitxor, 5) \
84 macro(op_bitor, 5) \
85 \
86 macro(op_check_has_instance, 2) \
87 macro(op_instanceof, 5) \
88 macro(op_typeof, 3) \
89 macro(op_is_undefined, 3) \
90 macro(op_is_boolean, 3) \
91 macro(op_is_number, 3) \
92 macro(op_is_string, 3) \
93 macro(op_is_object, 3) \
94 macro(op_is_function, 3) \
95 macro(op_in, 4) \
96 \
97 macro(op_resolve, 4) /* has value profiling */ \
98 macro(op_resolve_skip, 5) /* has value profiling */ \
99 macro(op_resolve_global, 6) /* has value profiling */ \
100 macro(op_resolve_global_dynamic, 7) /* has value profiling */ \
101 macro(op_get_scoped_var, 5) /* has value profiling */ \
102 macro(op_put_scoped_var, 4) \
103 macro(op_get_global_var, 4) /* has value profiling */ \
104 macro(op_put_global_var, 3) \
105 macro(op_resolve_base, 5) /* has value profiling */ \
106 macro(op_ensure_property_exists, 3) \
107 macro(op_resolve_with_base, 5) /* has value profiling */ \
108 macro(op_resolve_with_this, 5) /* has value profiling */ \
109 macro(op_get_by_id, 9) /* has value profiling */ \
110 macro(op_get_by_id_self, 9) /* has value profiling */ \
111 macro(op_get_by_id_proto, 9) /* has value profiling */ \
112 macro(op_get_by_id_chain, 9) /* has value profiling */ \
113 macro(op_get_by_id_getter_self, 9) /* has value profiling */ \
114 macro(op_get_by_id_getter_proto, 9) /* has value profiling */ \
115 macro(op_get_by_id_getter_chain, 9) /* has value profiling */ \
116 macro(op_get_by_id_custom_self, 9) /* has value profiling */ \
117 macro(op_get_by_id_custom_proto, 9) /* has value profiling */ \
118 macro(op_get_by_id_custom_chain, 9) /* has value profiling */ \
119 macro(op_get_by_id_generic, 9) /* has value profiling */ \
120 macro(op_get_array_length, 9) /* has value profiling */ \
121 macro(op_get_string_length, 9) /* has value profiling */ \
122 macro(op_get_arguments_length, 4) \
123 macro(op_put_by_id, 9) \
124 macro(op_put_by_id_transition, 9) \
125 macro(op_put_by_id_transition_direct, 9) \
126 macro(op_put_by_id_transition_normal, 9) \
127 macro(op_put_by_id_replace, 9) \
128 macro(op_put_by_id_generic, 9) \
129 macro(op_del_by_id, 4) \
130 macro(op_get_by_val, 5) /* has value profiling */ \
131 macro(op_get_argument_by_val, 5) /* must be the same size as op_get_by_val */ \
132 macro(op_get_by_pname, 7) \
133 macro(op_put_by_val, 4) \
134 macro(op_del_by_val, 4) \
135 macro(op_put_by_index, 4) \
136 macro(op_put_getter_setter, 5) \
137 \
138 macro(op_jmp, 2) \
139 macro(op_jtrue, 3) \
140 macro(op_jfalse, 3) \
141 macro(op_jeq_null, 3) \
142 macro(op_jneq_null, 3) \
143 macro(op_jneq_ptr, 4) \
144 macro(op_jless, 4) \
145 macro(op_jlesseq, 4) \
146 macro(op_jgreater, 4) \
147 macro(op_jgreatereq, 4) \
148 macro(op_jnless, 4) \
149 macro(op_jnlesseq, 4) \
150 macro(op_jngreater, 4) \
151 macro(op_jngreatereq, 4) \
152 macro(op_jmp_scopes, 3) \
153 macro(op_loop, 2) \
154 macro(op_loop_if_true, 3) \
155 macro(op_loop_if_false, 3) \
156 macro(op_loop_if_less, 4) \
157 macro(op_loop_if_lesseq, 4) \
158 macro(op_loop_if_greater, 4) \
159 macro(op_loop_if_greatereq, 4) \
160 macro(op_loop_hint, 1) \
161 macro(op_switch_imm, 4) \
162 macro(op_switch_char, 4) \
163 macro(op_switch_string, 4) \
164 \
165 macro(op_new_func, 4) \
166 macro(op_new_func_exp, 3) \
167 macro(op_call, 6) \
168 macro(op_call_eval, 6) \
169 macro(op_call_varargs, 5) \
170 macro(op_tear_off_activation, 3) \
171 macro(op_tear_off_arguments, 2) \
172 macro(op_ret, 2) \
173 macro(op_call_put_result, 3) /* has value profiling */ \
174 macro(op_ret_object_or_this, 3) \
175 macro(op_method_check, 1) \
176 \
177 macro(op_construct, 6) \
178 macro(op_strcat, 4) \
179 macro(op_to_primitive, 3) \
180 \
181 macro(op_get_pnames, 6) \
182 macro(op_next_pname, 7) \
183 \
184 macro(op_push_scope, 2) \
185 macro(op_pop_scope, 1) \
186 macro(op_push_new_scope, 4) \
187 \
188 macro(op_catch, 2) \
189 macro(op_throw, 2) \
190 macro(op_throw_reference_error, 2) \
191 \
192 macro(op_debug, 4) \
193 macro(op_profile_will_call, 2) \
194 macro(op_profile_did_call, 2) \
195 \
196 macro(op_end, 2) // end must be the last opcode in the list
197
198 #define OPCODE_ID_ENUM(opcode, length) opcode,
199 typedef enum { FOR_EACH_OPCODE_ID(OPCODE_ID_ENUM) } OpcodeID;
200 #undef OPCODE_ID_ENUM
201
202 const int maxOpcodeLength = 9;
203 const int numOpcodeIDs = op_end + 1;
204
205 #define OPCODE_ID_LENGTHS(id, length) const int id##_length = length;
206 FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS);
207 #undef OPCODE_ID_LENGTHS
208
209 #define OPCODE_LENGTH(opcode) opcode##_length
210
211 #define OPCODE_ID_LENGTH_MAP(opcode, length) length,
212 const int opcodeLengths[numOpcodeIDs] = { FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTH_MAP) };
213 #undef OPCODE_ID_LENGTH_MAP
214
215 #define VERIFY_OPCODE_ID(id, size) COMPILE_ASSERT(id <= op_end, ASSERT_THAT_JS_OPCODE_IDS_ARE_VALID);
216 FOR_EACH_OPCODE_ID(VERIFY_OPCODE_ID);
217 #undef VERIFY_OPCODE_ID
218
219#if ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER) || ENABLE(LLINT)
220#if COMPILER(RVCT) || COMPILER(INTEL)
221 typedef void* Opcode;
222#else
223 typedef const void* Opcode;
224#endif
225#else
226 typedef OpcodeID Opcode;
227#endif
228
229#define PADDING_STRING " "
230#define PADDING_STRING_LENGTH static_cast<unsigned>(strlen(PADDING_STRING))
231
232 extern const char* const opcodeNames[];
233
234 inline const char* padOpcodeName(OpcodeID op, unsigned width)
235 {
236 unsigned pad = width - strlen(opcodeNames[op]);
237 pad = std::min(pad, PADDING_STRING_LENGTH);
238 return PADDING_STRING + PADDING_STRING_LENGTH - pad;
239 }
240
241#undef PADDING_STRING_LENGTH
242#undef PADDING_STRING
243
244#if ENABLE(OPCODE_STATS)
245
246 struct OpcodeStats {
247 OpcodeStats();
248 ~OpcodeStats();
249 static long long opcodeCounts[numOpcodeIDs];
250 static long long opcodePairCounts[numOpcodeIDs][numOpcodeIDs];
251 static int lastOpcode;
252
253 static void recordInstruction(int opcode);
254 static void resetLastInstruction();
255 };
256
257#endif
258
259 inline size_t opcodeLength(OpcodeID opcode)
260 {
261 switch (opcode) {
262#define OPCODE_ID_LENGTHS(id, length) case id: return OPCODE_LENGTH(id);
263 FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS)
264#undef OPCODE_ID_LENGTHS
265 }
266 ASSERT_NOT_REACHED();
267 return 0;
268 }
269
270} // namespace JSC
271
272#endif // Opcode_h