Opcode.h 9.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

30 31
#ifndef Opcode_h
#define Opcode_h
32

33 34 35
#include <algorithm>
#include <string.h>

36 37
#include <wtf/Assertions.h>

38
namespace JSC {
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    #define FOR_EACH_OPCODE_ID(macro) \
        macro(op_enter, 1) \
        macro(op_create_activation, 2) \
        macro(op_init_lazy_reg, 2) \
        macro(op_create_arguments, 2) \
        macro(op_create_this, 2) \
        macro(op_convert_this, 3) \
        \
        macro(op_new_object, 2) \
        macro(op_new_array, 4) \
        macro(op_new_array_buffer, 4) \
        macro(op_new_regexp, 3) \
        macro(op_mov, 3) \
        \
        macro(op_not, 3) \
        macro(op_eq, 4) \
        macro(op_eq_null, 3) \
        macro(op_neq, 4) \
        macro(op_neq_null, 3) \
        macro(op_stricteq, 4) \
        macro(op_nstricteq, 4) \
        macro(op_less, 4) \
        macro(op_lesseq, 4) \
        macro(op_greater, 4) \
        macro(op_greatereq, 4) \
        \
        macro(op_pre_inc, 2) \
        macro(op_pre_dec, 2) \
        macro(op_post_inc, 3) \
        macro(op_post_dec, 3) \
        macro(op_to_jsnumber, 3) \
        macro(op_negate, 3) \
        macro(op_add, 5) \
        macro(op_mul, 5) \
        macro(op_div, 5) \
        macro(op_mod, 4) \
        macro(op_sub, 5) \
        \
        macro(op_lshift, 4) \
        macro(op_rshift, 4) \
        macro(op_urshift, 4) \
        macro(op_bitand, 5) \
        macro(op_bitxor, 5) \
        macro(op_bitor, 5) \
        \
        macro(op_check_has_instance, 2) \
        macro(op_instanceof, 5) \
        macro(op_typeof, 3) \
        macro(op_is_undefined, 3) \
        macro(op_is_boolean, 3) \
        macro(op_is_number, 3) \
        macro(op_is_string, 3) \
        macro(op_is_object, 3) \
        macro(op_is_function, 3) \
        macro(op_in, 4) \
        \
        macro(op_resolve, 4) /* has value profiling */  \
        macro(op_resolve_skip, 5) /* has value profiling */ \
        macro(op_resolve_global, 6) /* has value profiling */ \
        macro(op_resolve_global_dynamic, 7) /* has value profiling */ \
        macro(op_get_scoped_var, 5) /* has value profiling */ \
        macro(op_put_scoped_var, 4) \
        macro(op_get_global_var, 4) /* has value profiling */ \
        macro(op_get_global_var_watchable, 5) /* has value profiling */ \
        macro(op_put_global_var, 3) \
        macro(op_put_global_var_check, 5) \
        macro(op_resolve_base, 5) /* has value profiling */ \
        macro(op_ensure_property_exists, 3) \
        macro(op_resolve_with_base, 5) /* has value profiling */ \
        macro(op_resolve_with_this, 5) /* has value profiling */ \
        macro(op_get_by_id, 9) /* has value profiling */ \
        macro(op_get_by_id_out_of_line, 9) /* has value profiling */ \
        macro(op_get_by_id_self, 9) /* has value profiling */ \
        macro(op_get_by_id_proto, 9) /* has value profiling */ \
        macro(op_get_by_id_chain, 9) /* has value profiling */ \
        macro(op_get_by_id_getter_self, 9) /* has value profiling */ \
        macro(op_get_by_id_getter_proto, 9) /* has value profiling */ \
        macro(op_get_by_id_getter_chain, 9) /* has value profiling */ \
        macro(op_get_by_id_custom_self, 9) /* has value profiling */ \
        macro(op_get_by_id_custom_proto, 9) /* has value profiling */ \
        macro(op_get_by_id_custom_chain, 9) /* has value profiling */ \
        macro(op_get_by_id_generic, 9) /* has value profiling */ \
        macro(op_get_array_length, 9) /* has value profiling */ \
        macro(op_get_string_length, 9) /* has value profiling */ \
        macro(op_get_arguments_length, 4) \
        macro(op_put_by_id, 9) \
        macro(op_put_by_id_out_of_line, 9) \
        macro(op_put_by_id_transition, 9) \
        macro(op_put_by_id_transition_direct, 9) \
        macro(op_put_by_id_transition_direct_out_of_line, 9) \
        macro(op_put_by_id_transition_normal, 9) \
        macro(op_put_by_id_transition_normal_out_of_line, 9) \
        macro(op_put_by_id_replace, 9) \
        macro(op_put_by_id_generic, 9) \
        macro(op_del_by_id, 4) \
        macro(op_get_by_val, 6) /* has value profiling */ \
        macro(op_get_argument_by_val, 6) /* must be the same size as op_get_by_val */ \
        macro(op_get_by_pname, 7) \
        macro(op_put_by_val, 5) \
        macro(op_del_by_val, 4) \
        macro(op_put_by_index, 4) \
        macro(op_put_getter_setter, 5) \
        \
        macro(op_jmp, 2) \
        macro(op_jtrue, 3) \
        macro(op_jfalse, 3) \
        macro(op_jeq_null, 3) \
        macro(op_jneq_null, 3) \
        macro(op_jneq_ptr, 4) \
        macro(op_jless, 4) \
        macro(op_jlesseq, 4) \
        macro(op_jgreater, 4) \
        macro(op_jgreatereq, 4) \
        macro(op_jnless, 4) \
        macro(op_jnlesseq, 4) \
        macro(op_jngreater, 4) \
        macro(op_jngreatereq, 4) \
        macro(op_jmp_scopes, 3) \
        macro(op_loop, 2) \
        macro(op_loop_if_true, 3) \
        macro(op_loop_if_false, 3) \
        macro(op_loop_if_less, 4) \
        macro(op_loop_if_lesseq, 4) \
        macro(op_loop_if_greater, 4) \
        macro(op_loop_if_greatereq, 4) \
        macro(op_loop_hint, 1) \
        macro(op_switch_imm, 4) \
        macro(op_switch_char, 4) \
        macro(op_switch_string, 4) \
        \
        macro(op_new_func, 4) \
        macro(op_new_func_exp, 3) \
        macro(op_call, 6) \
        macro(op_call_eval, 6) \
        macro(op_call_varargs, 5) \
        macro(op_tear_off_activation, 3) \
        macro(op_tear_off_arguments, 2) \
        macro(op_ret, 2) \
        macro(op_call_put_result, 3) /* has value profiling */ \
        macro(op_ret_object_or_this, 3) \
        macro(op_method_check, 1) \
        \
        macro(op_construct, 6) \
        macro(op_strcat, 4) \
        macro(op_to_primitive, 3) \
        \
        macro(op_get_pnames, 6) \
        macro(op_next_pname, 7) \
        \
        macro(op_push_scope, 2) \
        macro(op_pop_scope, 1) \
        macro(op_push_new_scope, 4) \
        \
        macro(op_catch, 2) \
        macro(op_throw, 2) \
        macro(op_throw_reference_error, 2) \
        \
        macro(op_debug, 5) \
        macro(op_profile_will_call, 2) \
        macro(op_profile_did_call, 2) \
        \
        macro(op_end, 2) // end must be the last opcode in the list

203
    #define OPCODE_ID_ENUM(opcode, length) opcode,
204 205
        typedef enum { FOR_EACH_OPCODE_ID(OPCODE_ID_ENUM) } OpcodeID;
    #undef OPCODE_ID_ENUM
206

207
    const int maxOpcodeLength = 9;
208
    const int numOpcodeIDs = op_end + 1;
209

210 211
    #define OPCODE_ID_LENGTHS(id, length) const int id##_length = length;
         FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS);
212
    #undef OPCODE_ID_LENGTHS
213 214 215
    
    #define OPCODE_LENGTH(opcode) opcode##_length

216 217 218 219
    #define OPCODE_ID_LENGTH_MAP(opcode, length) length,
        const int opcodeLengths[numOpcodeIDs] = { FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTH_MAP) };
    #undef OPCODE_ID_LENGTH_MAP

220
    #define VERIFY_OPCODE_ID(id, size) COMPILE_ASSERT(id <= op_end, ASSERT_THAT_JS_OPCODE_IDS_ARE_VALID);
221 222
        FOR_EACH_OPCODE_ID(VERIFY_OPCODE_ID);
    #undef VERIFY_OPCODE_ID
223

224
#if ENABLE(COMPUTED_GOTO_CLASSIC_INTERPRETER) || ENABLE(LLINT)
225
#if COMPILER(RVCT) || COMPILER(INTEL)
226 227
    typedef void* Opcode;
#else
228
    typedef const void* Opcode;
229
#endif
230
#else
231
    typedef OpcodeID Opcode;
232 233
#endif

234 235
#define PADDING_STRING "                                "
#define PADDING_STRING_LENGTH static_cast<unsigned>(strlen(PADDING_STRING))
236

237
    extern const char* const opcodeNames[];
238

239
    inline const char* padOpcodeName(OpcodeID op, unsigned width)
240
    {
241
        unsigned pad = width - strlen(opcodeNames[op]);
242 243 244
        pad = std::min(pad, PADDING_STRING_LENGTH);
        return PADDING_STRING + PADDING_STRING_LENGTH - pad;
    }
245

246 247
#undef PADDING_STRING_LENGTH
#undef PADDING_STRING
248

249
#if ENABLE(OPCODE_STATS)
250

251 252 253 254 255 256
    struct OpcodeStats {
        OpcodeStats();
        ~OpcodeStats();
        static long long opcodeCounts[numOpcodeIDs];
        static long long opcodePairCounts[numOpcodeIDs][numOpcodeIDs];
        static int lastOpcode;
257

258
        static void recordInstruction(int opcode);
259
        static void resetLastInstruction();
260 261 262 263
    };

#endif

264 265 266 267 268 269 270 271 272 273 274
    inline size_t opcodeLength(OpcodeID opcode)
    {
        switch (opcode) {
#define OPCODE_ID_LENGTHS(id, length) case id: return OPCODE_LENGTH(id);
             FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS)
#undef OPCODE_ID_LENGTHS
        }
        ASSERT_NOT_REACHED();
        return 0;
    }

275
} // namespace JSC
276

277
#endif // Opcode_h