Opcode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/DistinctNumeric.h>
  8. namespace Wasm {
  9. AK_TYPEDEF_DISTINCT_ORDERED_ID(u32, OpCode);
  10. namespace Instructions {
  11. #define ENUMERATE_SINGLE_BYTE_WASM_OPCODES(M) \
  12. M(unreachable, 0x00) \
  13. M(nop, 0x01) \
  14. M(block, 0x02) \
  15. M(loop, 0x03) \
  16. M(if_, 0x04) \
  17. M(br, 0x0c) \
  18. M(br_if, 0x0d) \
  19. M(br_table, 0x0e) \
  20. M(return_, 0x0f) \
  21. M(call, 0x10) \
  22. M(call_indirect, 0x11) \
  23. M(drop, 0x1a) \
  24. M(select, 0x1b) \
  25. M(select_typed, 0x1c) \
  26. M(local_get, 0x20) \
  27. M(local_set, 0x21) \
  28. M(local_tee, 0x22) \
  29. M(global_get, 0x23) \
  30. M(global_set, 0x24) \
  31. M(table_get, 0x25) \
  32. M(table_set, 0x26) \
  33. M(i32_load, 0x28) \
  34. M(i64_load, 0x29) \
  35. M(f32_load, 0x2a) \
  36. M(f64_load, 0x2b) \
  37. M(i32_load8_s, 0x2c) \
  38. M(i32_load8_u, 0x2d) \
  39. M(i32_load16_s, 0x2e) \
  40. M(i32_load16_u, 0x2f) \
  41. M(i64_load8_s, 0x30) \
  42. M(i64_load8_u, 0x31) \
  43. M(i64_load16_s, 0x32) \
  44. M(i64_load16_u, 0x33) \
  45. M(i64_load32_s, 0x34) \
  46. M(i64_load32_u, 0x35) \
  47. M(i32_store, 0x36) \
  48. M(i64_store, 0x37) \
  49. M(f32_store, 0x38) \
  50. M(f64_store, 0x39) \
  51. M(i32_store8, 0x3a) \
  52. M(i32_store16, 0x3b) \
  53. M(i64_store8, 0x3c) \
  54. M(i64_store16, 0x3d) \
  55. M(i64_store32, 0x3e) \
  56. M(memory_size, 0x3f) \
  57. M(memory_grow, 0x40) \
  58. M(i32_const, 0x41) \
  59. M(i64_const, 0x42) \
  60. M(f32_const, 0x43) \
  61. M(f64_const, 0x44) \
  62. M(i32_eqz, 0x45) \
  63. M(i32_eq, 0x46) \
  64. M(i32_ne, 0x47) \
  65. M(i32_lts, 0x48) \
  66. M(i32_ltu, 0x49) \
  67. M(i32_gts, 0x4a) \
  68. M(i32_gtu, 0x4b) \
  69. M(i32_les, 0x4c) \
  70. M(i32_leu, 0x4d) \
  71. M(i32_ges, 0x4e) \
  72. M(i32_geu, 0x4f) \
  73. M(i64_eqz, 0x50) \
  74. M(i64_eq, 0x51) \
  75. M(i64_ne, 0x52) \
  76. M(i64_lts, 0x53) \
  77. M(i64_ltu, 0x54) \
  78. M(i64_gts, 0x55) \
  79. M(i64_gtu, 0x56) \
  80. M(i64_les, 0x57) \
  81. M(i64_leu, 0x58) \
  82. M(i64_ges, 0x59) \
  83. M(i64_geu, 0x5a) \
  84. M(f32_eq, 0x5b) \
  85. M(f32_ne, 0x5c) \
  86. M(f32_lt, 0x5d) \
  87. M(f32_gt, 0x5e) \
  88. M(f32_le, 0x5f) \
  89. M(f32_ge, 0x60) \
  90. M(f64_eq, 0x61) \
  91. M(f64_ne, 0x62) \
  92. M(f64_lt, 0x63) \
  93. M(f64_gt, 0x64) \
  94. M(f64_le, 0x65) \
  95. M(f64_ge, 0x66) \
  96. M(i32_clz, 0x67) \
  97. M(i32_ctz, 0x68) \
  98. M(i32_popcnt, 0x69) \
  99. M(i32_add, 0x6a) \
  100. M(i32_sub, 0x6b) \
  101. M(i32_mul, 0x6c) \
  102. M(i32_divs, 0x6d) \
  103. M(i32_divu, 0x6e) \
  104. M(i32_rems, 0x6f) \
  105. M(i32_remu, 0x70) \
  106. M(i32_and, 0x71) \
  107. M(i32_or, 0x72) \
  108. M(i32_xor, 0x73) \
  109. M(i32_shl, 0x74) \
  110. M(i32_shrs, 0x75) \
  111. M(i32_shru, 0x76) \
  112. M(i32_rotl, 0x77) \
  113. M(i32_rotr, 0x78) \
  114. M(i64_clz, 0x79) \
  115. M(i64_ctz, 0x7a) \
  116. M(i64_popcnt, 0x7b) \
  117. M(i64_add, 0x7c) \
  118. M(i64_sub, 0x7d) \
  119. M(i64_mul, 0x7e) \
  120. M(i64_divs, 0x7f) \
  121. M(i64_divu, 0x80) \
  122. M(i64_rems, 0x81) \
  123. M(i64_remu, 0x82) \
  124. M(i64_and, 0x83) \
  125. M(i64_or, 0x84) \
  126. M(i64_xor, 0x85) \
  127. M(i64_shl, 0x86) \
  128. M(i64_shrs, 0x87) \
  129. M(i64_shru, 0x88) \
  130. M(i64_rotl, 0x89) \
  131. M(i64_rotr, 0x8a) \
  132. M(f32_abs, 0x8b) \
  133. M(f32_neg, 0x8c) \
  134. M(f32_ceil, 0x8d) \
  135. M(f32_floor, 0x8e) \
  136. M(f32_trunc, 0x8f) \
  137. M(f32_nearest, 0x90) \
  138. M(f32_sqrt, 0x91) \
  139. M(f32_add, 0x92) \
  140. M(f32_sub, 0x93) \
  141. M(f32_mul, 0x94) \
  142. M(f32_div, 0x95) \
  143. M(f32_min, 0x96) \
  144. M(f32_max, 0x97) \
  145. M(f32_copysign, 0x98) \
  146. M(f64_abs, 0x99) \
  147. M(f64_neg, 0x9a) \
  148. M(f64_ceil, 0x9b) \
  149. M(f64_floor, 0x9c) \
  150. M(f64_trunc, 0x9d) \
  151. M(f64_nearest, 0x9e) \
  152. M(f64_sqrt, 0x9f) \
  153. M(f64_add, 0xa0) \
  154. M(f64_sub, 0xa1) \
  155. M(f64_mul, 0xa2) \
  156. M(f64_div, 0xa3) \
  157. M(f64_min, 0xa4) \
  158. M(f64_max, 0xa5) \
  159. M(f64_copysign, 0xa6) \
  160. M(i32_wrap_i64, 0xa7) \
  161. M(i32_trunc_sf32, 0xa8) \
  162. M(i32_trunc_uf32, 0xa9) \
  163. M(i32_trunc_sf64, 0xaa) \
  164. M(i32_trunc_uf64, 0xab) \
  165. M(i64_extend_si32, 0xac) \
  166. M(i64_extend_ui32, 0xad) \
  167. M(i64_trunc_sf32, 0xae) \
  168. M(i64_trunc_uf32, 0xaf) \
  169. M(i64_trunc_sf64, 0xb0) \
  170. M(i64_trunc_uf64, 0xb1) \
  171. M(f32_convert_si32, 0xb2) \
  172. M(f32_convert_ui32, 0xb3) \
  173. M(f32_convert_si64, 0xb4) \
  174. M(f32_convert_ui64, 0xb5) \
  175. M(f32_demote_f64, 0xb6) \
  176. M(f64_convert_si32, 0xb7) \
  177. M(f64_convert_ui32, 0xb8) \
  178. M(f64_convert_si64, 0xb9) \
  179. M(f64_convert_ui64, 0xba) \
  180. M(f64_promote_f32, 0xbb) \
  181. M(i32_reinterpret_f32, 0xbc) \
  182. M(i64_reinterpret_f64, 0xbd) \
  183. M(f32_reinterpret_i32, 0xbe) \
  184. M(f64_reinterpret_i64, 0xbf) \
  185. M(i32_extend8_s, 0xc0) \
  186. M(i32_extend16_s, 0xc1) \
  187. M(i64_extend8_s, 0xc2) \
  188. M(i64_extend16_s, 0xc3) \
  189. M(i64_extend32_s, 0xc4) \
  190. M(ref_null, 0xd0) \
  191. M(ref_is_null, 0xd1) \
  192. M(ref_func, 0xd2)
  193. // These are synthetic opcodes, they are _not_ seen in wasm with these values.
  194. #define ENUMERATE_MULTI_BYTE_WASM_OPCODES(M) \
  195. M(i32_trunc_sat_f32_s, 0xfc00) \
  196. M(i32_trunc_sat_f32_u, 0xfc01) \
  197. M(i32_trunc_sat_f64_s, 0xfc02) \
  198. M(i32_trunc_sat_f64_u, 0xfc03) \
  199. M(i64_trunc_sat_f32_s, 0xfc04) \
  200. M(i64_trunc_sat_f32_u, 0xfc05) \
  201. M(i64_trunc_sat_f64_s, 0xfc06) \
  202. M(i64_trunc_sat_f64_u, 0xfc07) \
  203. M(memory_init, 0xfc08) \
  204. M(data_drop, 0xfc09) \
  205. M(memory_copy, 0xfc0a) \
  206. M(memory_fill, 0x0fc0b) \
  207. M(table_init, 0xfc0c) \
  208. M(elem_drop, 0xfc0d) \
  209. M(table_copy, 0xfc0e) \
  210. M(table_grow, 0xfc0f) \
  211. M(table_size, 0xfc10) \
  212. M(table_fill, 0xfc11) \
  213. M(structured_else, 0xff00) \
  214. M(structured_end, 0xff01)
  215. #define ENUMERATE_WASM_OPCODES(M) \
  216. ENUMERATE_SINGLE_BYTE_WASM_OPCODES(M) \
  217. ENUMERATE_MULTI_BYTE_WASM_OPCODES(M)
  218. #define M(name, value) static constexpr OpCode name = value;
  219. ENUMERATE_WASM_OPCODES(M)
  220. #undef M
  221. static constexpr u32 i32_trunc_sat_f32_s_second = 0,
  222. i32_trunc_sat_f32_u_second = 1,
  223. i32_trunc_sat_f64_s_second = 2,
  224. i32_trunc_sat_f64_u_second = 3,
  225. i64_trunc_sat_f32_s_second = 4,
  226. i64_trunc_sat_f32_u_second = 5,
  227. i64_trunc_sat_f64_s_second = 6,
  228. i64_trunc_sat_f64_u_second = 7,
  229. memory_init_second = 8,
  230. data_drop_second = 9,
  231. memory_copy_second = 10,
  232. memory_fill_second = 11,
  233. table_init_second = 12,
  234. elem_drop_second = 13,
  235. table_copy_second = 14,
  236. table_grow_second = 15,
  237. table_size_second = 16,
  238. table_fill_second = 17;
  239. }
  240. }