1/*
2 * Copyright (C) 2008, 2009, 2013 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 "LLIntOpcode.h"
34
35#include <algorithm>
36#include <string.h>
37
38#include <wtf/Assertions.h>
39
40namespace JSC {
41
42#define FOR_EACH_CORE_OPCODE_ID_WITH_EXTENSION(macro, extension__) \
43    macro(op_enter, 1) \
44    macro(op_create_activation, 2) \
45    macro(op_init_lazy_reg, 2) \
46    macro(op_create_arguments, 2) \
47    macro(op_create_this, 4) \
48    macro(op_get_callee, 3) \
49    macro(op_convert_this, 3) \
50    \
51    macro(op_new_object, 4) \
52    macro(op_new_array, 5) \
53    macro(op_new_array_with_size, 4) \
54    macro(op_new_array_buffer, 5) \
55    macro(op_new_regexp, 3) \
56    macro(op_mov, 3) \
57    \
58    macro(op_not, 3) \
59    macro(op_eq, 4) \
60    macro(op_eq_null, 3) \
61    macro(op_neq, 4) \
62    macro(op_neq_null, 3) \
63    macro(op_stricteq, 4) \
64    macro(op_nstricteq, 4) \
65    macro(op_less, 4) \
66    macro(op_lesseq, 4) \
67    macro(op_greater, 4) \
68    macro(op_greatereq, 4) \
69    \
70    macro(op_inc, 2) \
71    macro(op_dec, 2) \
72    macro(op_to_number, 3) \
73    macro(op_negate, 3) \
74    macro(op_add, 5) \
75    macro(op_mul, 5) \
76    macro(op_div, 5) \
77    macro(op_mod, 4) \
78    macro(op_sub, 5) \
79    \
80    macro(op_lshift, 4) \
81    macro(op_rshift, 4) \
82    macro(op_urshift, 4) \
83    macro(op_bitand, 5) \
84    macro(op_bitxor, 5) \
85    macro(op_bitor, 5) \
86    \
87    macro(op_check_has_instance, 5) \
88    macro(op_instanceof, 4) \
89    macro(op_typeof, 3) \
90    macro(op_is_undefined, 3) \
91    macro(op_is_boolean, 3) \
92    macro(op_is_number, 3) \
93    macro(op_is_string, 3) \
94    macro(op_is_object, 3) \
95    macro(op_is_function, 3) \
96    macro(op_in, 4) \
97    \
98    macro(op_get_scoped_var, 5) /* has value profiling */ \
99    macro(op_put_scoped_var, 4) \
100    \
101    macro(op_resolve, 5) /* has value profiling */  \
102    macro(op_resolve_global_property, 5) /* has value profiling */  \
103    macro(op_resolve_global_var, 5) /* has value profiling */  \
104    macro(op_resolve_scoped_var, 5) /* has value profiling */  \
105    macro(op_resolve_scoped_var_on_top_scope, 5) /* has value profiling */  \
106    macro(op_resolve_scoped_var_with_top_scope_check, 5) /* has value profiling */  \
107    \
108    macro(op_resolve_base_to_global, 7) /* has value profiling */ \
109    macro(op_resolve_base_to_global_dynamic, 7) /* has value profiling */ \
110    macro(op_resolve_base_to_scope, 7) /* has value profiling */ \
111    macro(op_resolve_base_to_scope_with_top_scope_check, 7) /* has value profiling */ \
112    macro(op_resolve_base, 7) /* has value profiling */ \
113    \
114    macro(op_resolve_with_base, 7) /* has value profiling */ \
115    \
116    macro(op_resolve_with_this, 6) /* has value profiling */ \
117    \
118    macro(op_put_to_base, 5) \
119    macro(op_put_to_base_variable, 5) \
120    \
121    macro(op_init_global_const_nop, 5) \
122    macro(op_init_global_const, 5) \
123    macro(op_init_global_const_check, 5) \
124    macro(op_get_by_id, 9) /* has value profiling */ \
125    macro(op_get_by_id_out_of_line, 9) /* has value profiling */ \
126    macro(op_get_by_id_self, 9) /* has value profiling */ \
127    macro(op_get_by_id_proto, 9) /* has value profiling */ \
128    macro(op_get_by_id_chain, 9) /* has value profiling */ \
129    macro(op_get_by_id_getter_self, 9) /* has value profiling */ \
130    macro(op_get_by_id_getter_proto, 9) /* has value profiling */ \
131    macro(op_get_by_id_getter_chain, 9) /* has value profiling */ \
132    macro(op_get_by_id_custom_self, 9) /* has value profiling */ \
133    macro(op_get_by_id_custom_proto, 9) /* has value profiling */ \
134    macro(op_get_by_id_custom_chain, 9) /* has value profiling */ \
135    macro(op_get_by_id_generic, 9) /* has value profiling */ \
136    macro(op_get_array_length, 9) /* has value profiling */ \
137    macro(op_get_string_length, 9) /* has value profiling */ \
138    macro(op_get_arguments_length, 4) \
139    macro(op_put_by_id, 9) \
140    macro(op_put_by_id_out_of_line, 9) \
141    macro(op_put_by_id_transition, 9) \
142    macro(op_put_by_id_transition_direct, 9) \
143    macro(op_put_by_id_transition_direct_out_of_line, 9) \
144    macro(op_put_by_id_transition_normal, 9) \
145    macro(op_put_by_id_transition_normal_out_of_line, 9) \
146    macro(op_put_by_id_replace, 9) \
147    macro(op_put_by_id_generic, 9) \
148    macro(op_del_by_id, 4) \
149    macro(op_get_by_val, 6) /* has value profiling */ \
150    macro(op_get_argument_by_val, 6) /* must be the same size as op_get_by_val */ \
151    macro(op_get_by_pname, 7) \
152    macro(op_put_by_val, 5) \
153    macro(op_del_by_val, 4) \
154    macro(op_put_by_index, 4) \
155    macro(op_put_getter_setter, 5) \
156    \
157    macro(op_jmp, 2) \
158    macro(op_jtrue, 3) \
159    macro(op_jfalse, 3) \
160    macro(op_jeq_null, 3) \
161    macro(op_jneq_null, 3) \
162    macro(op_jneq_ptr, 4) \
163    macro(op_jless, 4) \
164    macro(op_jlesseq, 4) \
165    macro(op_jgreater, 4) \
166    macro(op_jgreatereq, 4) \
167    macro(op_jnless, 4) \
168    macro(op_jnlesseq, 4) \
169    macro(op_jngreater, 4) \
170    macro(op_jngreatereq, 4) \
171    \
172    macro(op_loop_hint, 1) \
173    \
174    macro(op_switch_imm, 4) \
175    macro(op_switch_char, 4) \
176    macro(op_switch_string, 4) \
177    \
178    macro(op_new_func, 4) \
179    macro(op_new_func_exp, 3) \
180    macro(op_call, 6) \
181    macro(op_call_eval, 6) \
182    macro(op_call_varargs, 5) \
183    macro(op_tear_off_activation, 2) \
184    macro(op_tear_off_arguments, 3) \
185    macro(op_ret, 2) \
186    macro(op_call_put_result, 3) /* has value profiling */ \
187    macro(op_ret_object_or_this, 3) \
188    \
189    macro(op_construct, 6) \
190    macro(op_strcat, 4) \
191    macro(op_to_primitive, 3) \
192    \
193    macro(op_get_pnames, 6) \
194    macro(op_next_pname, 7) \
195    \
196    macro(op_push_with_scope, 2) \
197    macro(op_pop_scope, 1) \
198    macro(op_push_name_scope, 4) \
199    \
200    macro(op_catch, 2) \
201    macro(op_throw, 2) \
202    macro(op_throw_static_error, 3) \
203    \
204    macro(op_debug, 5) \
205    macro(op_profile_will_call, 2) \
206    macro(op_profile_did_call, 2) \
207    \
208    extension__ \
209    \
210    macro(op_end, 2) // end must be the last opcode in the list
211
212#define FOR_EACH_CORE_OPCODE_ID(macro) \
213    FOR_EACH_CORE_OPCODE_ID_WITH_EXTENSION(macro, /* No extension */ )
214
215#define FOR_EACH_OPCODE_ID(macro) \
216    FOR_EACH_CORE_OPCODE_ID_WITH_EXTENSION( \
217        macro, \
218        FOR_EACH_LLINT_OPCODE_EXTENSION(macro) \
219    )
220
221
222#define OPCODE_ID_ENUM(opcode, length) opcode,
223    typedef enum { FOR_EACH_OPCODE_ID(OPCODE_ID_ENUM) } OpcodeID;
224#undef OPCODE_ID_ENUM
225
226const int maxOpcodeLength = 9;
227const int numOpcodeIDs = op_end + 1;
228
229#define OPCODE_ID_LENGTHS(id, length) const int id##_length = length;
230    FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS);
231#undef OPCODE_ID_LENGTHS
232
233#define OPCODE_LENGTH(opcode) opcode##_length
234
235#define OPCODE_ID_LENGTH_MAP(opcode, length) length,
236    const int opcodeLengths[numOpcodeIDs] = { FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTH_MAP) };
237#undef OPCODE_ID_LENGTH_MAP
238
239#define VERIFY_OPCODE_ID(id, size) COMPILE_ASSERT(id <= op_end, ASSERT_THAT_JS_OPCODE_IDS_ARE_VALID);
240    FOR_EACH_OPCODE_ID(VERIFY_OPCODE_ID);
241#undef VERIFY_OPCODE_ID
242
243#if ENABLE(COMPUTED_GOTO_OPCODES)
244typedef void* Opcode;
245#else
246typedef OpcodeID Opcode;
247#endif
248
249#define PADDING_STRING "                                "
250#define PADDING_STRING_LENGTH static_cast<unsigned>(strlen(PADDING_STRING))
251
252extern const char* const opcodeNames[];
253
254inline const char* padOpcodeName(OpcodeID op, unsigned width)
255{
256    unsigned pad = width - strlen(opcodeNames[op]);
257    pad = std::min(pad, PADDING_STRING_LENGTH);
258    return PADDING_STRING + PADDING_STRING_LENGTH - pad;
259}
260
261#undef PADDING_STRING_LENGTH
262#undef PADDING_STRING
263
264#if ENABLE(OPCODE_STATS)
265
266struct OpcodeStats {
267    OpcodeStats();
268    ~OpcodeStats();
269    static long long opcodeCounts[numOpcodeIDs];
270    static long long opcodePairCounts[numOpcodeIDs][numOpcodeIDs];
271    static int lastOpcode;
272
273    static void recordInstruction(int opcode);
274    static void resetLastInstruction();
275};
276
277#endif
278
279inline size_t opcodeLength(OpcodeID opcode)
280{
281    switch (opcode) {
282#define OPCODE_ID_LENGTHS(id, length) case id: return OPCODE_LENGTH(id);
283         FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS)
284#undef OPCODE_ID_LENGTHS
285    }
286    RELEASE_ASSERT_NOT_REACHED();
287    return 0;
288}
289
290} // namespace JSC
291
292#endif // Opcode_h
293