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_initialise_locals) \
44 macro(op_unexpected_load) \
45 macro(op_new_object) \
47 macro(op_new_regexp) \
64 macro(op_to_jsnumber) \
80 macro(op_instanceof) \
82 macro(op_is_undefined) \
83 macro(op_is_boolean) \
87 macro(op_is_function) \
91 macro(op_resolve_skip) \
92 macro(op_resolve_global) \
93 macro(op_get_scoped_var) \
94 macro(op_put_scoped_var) \
95 macro(op_get_global_var) \
96 macro(op_put_global_var) \
97 macro(op_resolve_base) \
98 macro(op_resolve_with_base) \
99 macro(op_resolve_func) \
100 macro(op_get_by_id) \
101 macro(op_get_by_id_self) \
102 macro(op_get_by_id_proto) \
103 macro(op_get_by_id_chain) \
104 macro(op_get_by_id_generic) \
105 macro(op_get_array_length) \
106 macro(op_get_string_length) \
107 macro(op_put_by_id) \
108 macro(op_put_by_id_transition) \
109 macro(op_put_by_id_replace) \
110 macro(op_put_by_id_generic) \
111 macro(op_del_by_id) \
112 macro(op_get_by_val) \
113 macro(op_put_by_val) \
114 macro(op_del_by_val) \
115 macro(op_put_by_index) \
116 macro(op_put_getter) \
117 macro(op_put_setter) \
123 macro(op_jmp_scopes) \
125 macro(op_loop_if_true) \
126 macro(op_loop_if_less) \
127 macro(op_loop_if_lesseq) \
128 macro(op_switch_imm) \
129 macro(op_switch_char) \
130 macro(op_switch_string) \
133 macro(op_new_func_exp) \
135 macro(op_call_eval) \
138 macro(op_construct) \
139 macro(op_construct_verify) \
141 macro(op_get_pnames) \
142 macro(op_next_pname) \
144 macro(op_push_scope) \
145 macro(op_pop_scope) \
146 macro(op_push_new_scope) \
150 macro(op_new_error) \
157 macro(op_end) // end must be the last opcode in the list
159 #define OPCODE_ID_ENUM(opcode) opcode,
160 typedef enum { FOR_EACH_OPCODE_ID(OPCODE_ID_ENUM) } OpcodeID;
161 #undef OPCODE_ID_ENUM
163 const int numOpcodeIDs = op_end + 1;
165 #define VERIFY_OPCODE_ID(id) COMPILE_ASSERT(id <= op_end, ASSERT_THAT_JS_OPCODE_IDS_ARE_VALID);
166 FOR_EACH_OPCODE_ID(VERIFY_OPCODE_ID);
167 #undef VERIFY_OPCODE_ID
169 #if HAVE(COMPUTED_GOTO)
170 typedef void* Opcode;
172 typedef OpcodeID Opcode;
175 #if ENABLE(SAMPLING_TOOL) || DUMP_OPCODE_STATS
177 #define PADDING_STRING " "
178 #define PADDING_STRING_LENGTH static_cast<unsigned>(strlen(PADDING_STRING))
180 extern const char* const opcodeNames[];
182 inline const char* padOpcodeName(OpcodeID op, unsigned width)
184 unsigned pad = width - strlen(opcodeNames[op]);
185 pad = std::min(pad, PADDING_STRING_LENGTH);
186 return PADDING_STRING + PADDING_STRING_LENGTH - pad;
189 #undef PADDING_STRING_LENGTH
190 #undef PADDING_STRING
194 #if DUMP_OPCODE_STATS
199 static long long opcodeCounts[numOpcodeIDs];
200 static long long opcodePairCounts[numOpcodeIDs][numOpcodeIDs];
201 static int lastOpcode;
203 static void recordInstruction(int opcode);
204 static void resetLastInstruction();