mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 1 | /* |
mark.lam@apple.com | 31fc87e2 | 2014-01-18 21:11:56 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008, 2009, 2013, 2014 Apple Inc. All rights reserved. |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 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. |
mjs@apple.com | 9204733 | 2014-03-15 04:08:27 +0000 | [diff] [blame] | 14 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 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 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 30 | #pragma once |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 31 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 32 | #include "Bytecodes.h" |
commit-queue@webkit.org | 61b1cdb | 2012-08-29 03:57:52 +0000 | [diff] [blame] | 33 | #include "LLIntOpcode.h" |
| 34 | |
barraclough@apple.com | eb51bd9 | 2008-07-30 17:38:35 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | #include <string.h> |
| 37 | |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 38 | #include <wtf/Assertions.h> |
| 39 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 40 | namespace JSC { |
cwzwarich@webkit.org | b7a9610 | 2008-06-05 04:39:09 +0000 | [diff] [blame] | 41 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 42 | #define FOR_EACH_CORE_OPCODE_ID_WITH_EXTENSION(macro, extension__) \ |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 43 | FOR_EACH_BYTECODE_ID(macro) \ |
| 44 | extension__ |
oliver@apple.com | 3fc2985 | 2012-08-27 23:03:03 +0000 | [diff] [blame] | 45 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 46 | #define FOR_EACH_CORE_OPCODE_ID(macro) \ |
| 47 | FOR_EACH_CORE_OPCODE_ID_WITH_EXTENSION(macro, /* No extension */ ) |
commit-queue@webkit.org | 0206200 | 2012-09-12 22:20:34 +0000 | [diff] [blame] | 48 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 49 | #define FOR_EACH_OPCODE_ID(macro) \ |
| 50 | FOR_EACH_CORE_OPCODE_ID_WITH_EXTENSION( \ |
| 51 | macro, \ |
| 52 | FOR_EACH_LLINT_OPCODE_EXTENSION(macro) \ |
| 53 | ) |
commit-queue@webkit.org | 0206200 | 2012-09-12 22:20:34 +0000 | [diff] [blame] | 54 | |
| 55 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 56 | #define OPCODE_ID_ENUM(opcode, length) opcode, |
fpizlo@apple.com | bc16ddb | 2016-09-06 01:02:22 +0000 | [diff] [blame] | 57 | enum OpcodeID : unsigned { FOR_EACH_OPCODE_ID(OPCODE_ID_ENUM) }; |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 58 | #undef OPCODE_ID_ENUM |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 59 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 60 | const int maxOpcodeLength = 9; |
dbatyai.u-szeged@partner.samsung.com | 46f07e5 | 2014-06-19 16:32:31 +0000 | [diff] [blame] | 61 | #if !ENABLE(JIT) |
msaboff@apple.com | 8f8907e | 2014-04-10 22:33:59 +0000 | [diff] [blame] | 62 | const int numOpcodeIDs = NUMBER_OF_BYTECODE_IDS + NUMBER_OF_CLOOP_BYTECODE_HELPER_IDS + NUMBER_OF_BYTECODE_HELPER_IDS; |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 63 | #else |
msaboff@apple.com | 8f8907e | 2014-04-10 22:33:59 +0000 | [diff] [blame] | 64 | const int numOpcodeIDs = NUMBER_OF_BYTECODE_IDS + NUMBER_OF_BYTECODE_HELPER_IDS; |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 65 | #endif |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 66 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 67 | #define OPCODE_ID_LENGTHS(id, length) const int id##_length = length; |
| 68 | FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS); |
| 69 | #undef OPCODE_ID_LENGTHS |
cwzwarich@webkit.org | c8af3ef | 2008-12-09 19:19:04 +0000 | [diff] [blame] | 70 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 71 | #define OPCODE_LENGTH(opcode) opcode##_length |
weinig@apple.com | 0aaf82a | 2008-12-12 08:02:09 +0000 | [diff] [blame] | 72 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 73 | #define OPCODE_ID_LENGTH_MAP(opcode, length) length, |
| 74 | const int opcodeLengths[numOpcodeIDs] = { FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTH_MAP) }; |
| 75 | #undef OPCODE_ID_LENGTH_MAP |
| 76 | |
mcatanzaro@igalia.com | ce78d37 | 2016-09-06 14:29:43 +0000 | [diff] [blame] | 77 | #if COMPILER(GCC) |
| 78 | #pragma GCC diagnostic push |
| 79 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 80 | #endif |
| 81 | |
msaboff@apple.com | 5148607 | 2014-02-27 18:48:37 +0000 | [diff] [blame] | 82 | #define VERIFY_OPCODE_ID(id, size) COMPILE_ASSERT(id <= numOpcodeIDs, ASSERT_THAT_JS_OPCODE_IDS_ARE_VALID); |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 83 | FOR_EACH_OPCODE_ID(VERIFY_OPCODE_ID); |
| 84 | #undef VERIFY_OPCODE_ID |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 85 | |
mcatanzaro@igalia.com | ce78d37 | 2016-09-06 14:29:43 +0000 | [diff] [blame] | 86 | #if COMPILER(GCC) |
| 87 | #pragma GCC diagnostic pop |
| 88 | #endif |
| 89 | |
commit-queue@webkit.org | b841948 | 2012-08-30 22:21:48 +0000 | [diff] [blame] | 90 | #if ENABLE(COMPUTED_GOTO_OPCODES) |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 91 | typedef void* Opcode; |
eric@webkit.org | f8c8730 | 2010-01-27 04:20:33 +0000 | [diff] [blame] | 92 | #else |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 93 | typedef OpcodeID Opcode; |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
barraclough@apple.com | eb51bd9 | 2008-07-30 17:38:35 +0000 | [diff] [blame] | 96 | #define PADDING_STRING " " |
| 97 | #define PADDING_STRING_LENGTH static_cast<unsigned>(strlen(PADDING_STRING)) |
aroben@apple.com | 42e34ef | 2008-07-24 01:00:35 +0000 | [diff] [blame] | 98 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 99 | extern const char* const opcodeNames[]; |
barraclough@apple.com | eb51bd9 | 2008-07-30 17:38:35 +0000 | [diff] [blame] | 100 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 101 | inline const char* padOpcodeName(OpcodeID op, unsigned width) |
| 102 | { |
| 103 | unsigned pad = width - strlen(opcodeNames[op]); |
| 104 | pad = std::min(pad, PADDING_STRING_LENGTH); |
| 105 | return PADDING_STRING + PADDING_STRING_LENGTH - pad; |
| 106 | } |
barraclough@apple.com | f2bacea | 2008-07-23 22:36:39 +0000 | [diff] [blame] | 107 | |
barraclough@apple.com | eb51bd9 | 2008-07-30 17:38:35 +0000 | [diff] [blame] | 108 | #undef PADDING_STRING_LENGTH |
| 109 | #undef PADDING_STRING |
barraclough@apple.com | f2bacea | 2008-07-23 22:36:39 +0000 | [diff] [blame] | 110 | |
ggaren@apple.com | 47d3f05 | 2008-11-15 21:37:49 +0000 | [diff] [blame] | 111 | #if ENABLE(OPCODE_STATS) |
cwzwarich@webkit.org | b7a9610 | 2008-06-05 04:39:09 +0000 | [diff] [blame] | 112 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 113 | struct OpcodeStats { |
| 114 | OpcodeStats(); |
| 115 | ~OpcodeStats(); |
| 116 | static long long opcodeCounts[numOpcodeIDs]; |
| 117 | static long long opcodePairCounts[numOpcodeIDs][numOpcodeIDs]; |
| 118 | static int lastOpcode; |
| 119 | |
| 120 | static void recordInstruction(int opcode); |
| 121 | static void resetLastInstruction(); |
| 122 | }; |
cwzwarich@webkit.org | b7a9610 | 2008-06-05 04:39:09 +0000 | [diff] [blame] | 123 | |
| 124 | #endif |
| 125 | |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 126 | inline size_t opcodeLength(OpcodeID opcode) |
| 127 | { |
| 128 | switch (opcode) { |
commit-queue@webkit.org | 6ef841b | 2010-08-05 22:22:49 +0000 | [diff] [blame] | 129 | #define OPCODE_ID_LENGTHS(id, length) case id: return OPCODE_LENGTH(id); |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 130 | FOR_EACH_OPCODE_ID(OPCODE_ID_LENGTHS) |
commit-queue@webkit.org | 6ef841b | 2010-08-05 22:22:49 +0000 | [diff] [blame] | 131 | #undef OPCODE_ID_LENGTHS |
commit-queue@webkit.org | 6ef841b | 2010-08-05 22:22:49 +0000 | [diff] [blame] | 132 | } |
fpizlo@apple.com | 3f65c49 | 2013-02-19 23:21:06 +0000 | [diff] [blame] | 133 | RELEASE_ASSERT_NOT_REACHED(); |
| 134 | return 0; |
| 135 | } |
commit-queue@webkit.org | 6ef841b | 2010-08-05 22:22:49 +0000 | [diff] [blame] | 136 | |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 137 | inline bool isBranch(OpcodeID opcodeID) |
| 138 | { |
| 139 | switch (opcodeID) { |
| 140 | case op_jmp: |
| 141 | case op_jtrue: |
| 142 | case op_jfalse: |
| 143 | case op_jeq_null: |
| 144 | case op_jneq_null: |
| 145 | case op_jneq_ptr: |
| 146 | case op_jless: |
| 147 | case op_jlesseq: |
| 148 | case op_jgreater: |
| 149 | case op_jgreatereq: |
| 150 | case op_jnless: |
| 151 | case op_jnlesseq: |
| 152 | case op_jngreater: |
| 153 | case op_jngreatereq: |
| 154 | case op_switch_imm: |
| 155 | case op_switch_char: |
| 156 | case op_switch_string: |
| 157 | return true; |
| 158 | default: |
| 159 | return false; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | inline bool isUnconditionalBranch(OpcodeID opcodeID) |
| 164 | { |
| 165 | switch (opcodeID) { |
| 166 | case op_jmp: |
| 167 | return true; |
| 168 | default: |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | inline bool isTerminal(OpcodeID opcodeID) |
| 174 | { |
| 175 | switch (opcodeID) { |
| 176 | case op_ret: |
| 177 | case op_end: |
fpizlo@apple.com | f502684 | 2017-06-06 18:08:17 +0000 | [diff] [blame] | 178 | case op_unreachable: |
utatane.tea@gmail.com | c258519 | 2016-08-25 22:55:10 +0000 | [diff] [blame] | 179 | return true; |
| 180 | default: |
| 181 | return false; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | inline bool isThrow(OpcodeID opcodeID) |
| 186 | { |
| 187 | switch (opcodeID) { |
| 188 | case op_throw: |
| 189 | case op_throw_static_error: |
| 190 | return true; |
| 191 | default: |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 196 | } // namespace JSC |
mrowe@apple.com | 2f6dfdf | 2008-05-22 01:20:45 +0000 | [diff] [blame] | 197 | |
mark.lam@apple.com | df88484 | 2015-11-11 22:31:18 +0000 | [diff] [blame] | 198 | namespace WTF { |
| 199 | |
| 200 | class PrintStream; |
| 201 | |
| 202 | void printInternal(PrintStream&, JSC::OpcodeID); |
| 203 | |
| 204 | } // namespace WTF |