Opcode.h 9.8 KB

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