2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
36 #include <wtf/Assertions.h>
40 #define DUMP_OPCODE_STATS 0
42 #define FOR_EACH_OPCODE_ID(macro) \
43 macro(op_unexpected_load) \
44 macro(op_new_object) \
46 macro(op_new_regexp) \
63 macro(op_to_jsnumber) \
79 macro(op_instanceof) \
81 macro(op_is_undefined) \
82 macro(op_is_boolean) \
86 macro(op_is_function) \
90 macro(op_resolve_skip) \
91 macro(op_resolve_global) \
92 macro(op_get_scoped_var) \
93 macro(op_put_scoped_var) \
94 macro(op_get_global_var) \
95 macro(op_put_global_var) \
96 macro(op_resolve_base) \
97 macro(op_resolve_with_base) \
98 macro(op_resolve_func) \
100 macro(op_get_by_id_self) \
101 macro(op_get_by_id_proto) \
102 macro(op_get_by_id_chain) \
103 macro(op_get_by_id_generic) \
104 macro(op_get_array_length) \
105 macro(op_get_string_length) \
106 macro(op_put_by_id) \
107 macro(op_put_by_id_transition) \
108 macro(op_put_by_id_replace) \
109 macro(op_put_by_id_generic) \
110 macro(op_del_by_id) \
111 macro(op_get_by_val) \
112 macro(op_put_by_val) \
113 macro(op_del_by_val) \
114 macro(op_put_by_index) \
115 macro(op_put_getter) \
116 macro(op_put_setter) \
122 macro(op_jmp_scopes) \
124 macro(op_loop_if_true) \
125 macro(op_loop_if_less) \
126 macro(op_loop_if_lesseq) \
127 macro(op_switch_imm) \
128 macro(op_switch_char) \
129 macro(op_switch_string) \
132 macro(op_new_func_exp) \
134 macro(op_call_eval) \
137 macro(op_construct) \
138 macro(op_construct_verify) \
140 macro(op_get_pnames) \
141 macro(op_next_pname) \
143 macro(op_push_scope) \
144 macro(op_pop_scope) \
145 macro(op_push_new_scope) \
149 macro(op_new_error) \
156 macro(op_end) // end must be the last opcode in the list
158 #define OPCODE_ID_ENUM(opcode) opcode,
159 typedef enum { FOR_EACH_OPCODE_ID(OPCODE_ID_ENUM) } OpcodeID;
160 #undef OPCODE_ID_ENUM
162 const int numOpcodeIDs = op_end + 1;
164 #define VERIFY_OPCODE_ID(id) COMPILE_ASSERT(id <= op_end, ASSERT_THAT_JS_OPCODE_IDS_ARE_VALID);
165 FOR_EACH_OPCODE_ID(VERIFY_OPCODE_ID);
166 #undef VERIFY_OPCODE_ID
168 #if HAVE(COMPUTED_GOTO)
169 typedef void* Opcode;
171 typedef OpcodeID Opcode;
174 #if ENABLE(SAMPLING_TOOL) || DUMP_OPCODE_STATS
176 #define PADDING_STRING " "
177 #define PADDING_STRING_LENGTH static_cast<unsigned>(strlen(PADDING_STRING))
179 extern const char* const opcodeNames[];
181 inline const char* padOpcodeName(OpcodeID op, unsigned width)
183 unsigned pad = width - strlen(opcodeNames[op]);
184 pad = std::min(pad, PADDING_STRING_LENGTH);
185 return PADDING_STRING + PADDING_STRING_LENGTH - pad;
188 #undef PADDING_STRING_LENGTH
189 #undef PADDING_STRING
193 #if DUMP_OPCODE_STATS
198 static long long opcodeCounts[numOpcodeIDs];
199 static long long opcodePairCounts[numOpcodeIDs][numOpcodeIDs];
200 static int lastOpcode;
202 static void recordInstruction(int opcode);
203 static void resetLastInstruction();