wasm_interp.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "wasm_interp.h"
  17. #include "wasm_runtime.h"
  18. #include "wasm_thread.h"
  19. #include "wasm_opcode.h"
  20. #include "wasm_loader.h"
  21. #include "wasm_log.h"
  22. #include "wasm_memory.h"
  23. typedef int32 CellType_I32;
  24. typedef int64 CellType_I64;
  25. typedef float32 CellType_F32;
  26. typedef float64 CellType_F64;
  27. #define BR_TABLE_TMP_BUF_LEN 32
  28. /* 64-bit Memory accessors. */
  29. #if WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0
  30. #define PUT_I64_TO_ADDR(addr, value) do { \
  31. *(int64*)(addr) = (int64)(value); \
  32. } while (0)
  33. #define PUT_F64_TO_ADDR(addr, value) do { \
  34. *(float64*)(addr) = (float64)(value); \
  35. } while (0)
  36. #define GET_I64_FROM_ADDR(addr) (*(int64*)(addr))
  37. #define GET_F64_FROM_ADDR(addr) (*(float64*)(addr))
  38. #else /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
  39. #define PUT_I64_TO_ADDR(addr, value) do { \
  40. union { int64 val; uint32 parts[2]; } u; \
  41. u.val = (value); \
  42. (addr)[0] = u.parts[0]; \
  43. (addr)[1] = u.parts[1]; \
  44. } while (0)
  45. #define PUT_F64_TO_ADDR(addr, value) do { \
  46. union { float64 val; uint32 parts[2]; } u; \
  47. u.val = (value); \
  48. (addr)[0] = u.parts[0]; \
  49. (addr)[1] = u.parts[1]; \
  50. } while (0)
  51. static inline int64
  52. GET_I64_FROM_ADDR(uint32 *addr)
  53. {
  54. union { int64 val; uint32 parts[2]; } u;
  55. u.parts[0] = addr[0];
  56. u.parts[1] = addr[1];
  57. return u.val;
  58. }
  59. static inline float64
  60. GET_F64_FROM_ADDR (uint32 *addr)
  61. {
  62. union { float64 val; uint32 parts[2]; } u;
  63. u.parts[0] = addr[0];
  64. u.parts[1] = addr[1];
  65. return u.val;
  66. }
  67. #endif /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
  68. #if WASM_ENABLE_EXT_MEMORY_SPACE != 0
  69. #define CHECK_EXT_MEMORY_SPACE() \
  70. else if (module->ext_mem_data \
  71. && module->ext_mem_base_offset <= offset1 \
  72. && offset1 < module->ext_mem_base_offset \
  73. + module->ext_mem_size) { \
  74. /* If offset1 is in valid range, maddr must also be in valid range, \
  75. no need to check it again. */ \
  76. maddr = module->ext_mem_data \
  77. + (offset1 - module->ext_mem_base_offset); \
  78. if (maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] > \
  79. module->ext_mem_data_end) \
  80. goto out_of_bounds; \
  81. }
  82. #else
  83. #define CHECK_EXT_MEMORY_SPACE()
  84. #endif
  85. #define CHECK_MEMORY_OVERFLOW() do { \
  86. uint32 offset1 = offset + addr; \
  87. /* if (flags != 2) \
  88. LOG_VERBOSE("unaligned load/store in wasm interp, flag: %d.\n", flags); */\
  89. /* The WASM spec doesn't require that the dynamic address operand must be \
  90. unsigned, so we don't check whether integer overflow or not here. */ \
  91. /* if (offset1 < offset) \
  92. goto out_of_bounds; */ \
  93. if (offset1 < memory_data_size) { \
  94. /* If offset1 is in valid range, maddr must also be in valid range, \
  95. no need to check it again. */ \
  96. maddr = memory->memory_data + offset1; \
  97. if (maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] > memory->end_addr) \
  98. goto out_of_bounds; \
  99. } \
  100. else if (offset1 > heap_base_offset \
  101. && offset1 < heap_base_offset + heap_data_size) { \
  102. /* If offset1 is in valid range, maddr must also be in valid range, \
  103. no need to check it again. */ \
  104. maddr = memory->heap_data + offset1 - memory->heap_base_offset; \
  105. if (maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] > memory->heap_data_end) \
  106. goto out_of_bounds; \
  107. } \
  108. CHECK_EXT_MEMORY_SPACE() \
  109. else \
  110. goto out_of_bounds; \
  111. } while (0)
  112. static inline uint32
  113. rotl32(uint32 n, unsigned int c)
  114. {
  115. const unsigned int mask = (31);
  116. c = c % 32;
  117. c &= mask;
  118. return (n<<c) | (n>>( (-c)&mask ));
  119. }
  120. static inline uint32
  121. rotr32(uint32 n, unsigned int c)
  122. {
  123. const unsigned int mask = (31);
  124. c = c % 32;
  125. c &= mask;
  126. return (n>>c) | (n<<( (-c)&mask ));
  127. }
  128. static inline uint64
  129. rotl64(uint64 n, unsigned int c)
  130. {
  131. const unsigned int mask = (63);
  132. c = c % 64;
  133. c &= mask;
  134. return (n<<c) | (n>>( (-c)&mask ));
  135. }
  136. static inline uint64
  137. rotr64(uint64 n, unsigned int c)
  138. {
  139. const unsigned int mask = (63);
  140. c = c % 64;
  141. c &= mask;
  142. return (n>>c) | (n<<( (-c)&mask ));
  143. }
  144. static inline double
  145. wa_fmax(double a, double b)
  146. {
  147. double c = fmax(a, b);
  148. if (c==0 && a==b)
  149. return signbit(a) ? b : a;
  150. return c;
  151. }
  152. static inline double
  153. wa_fmin(double a, double b)
  154. {
  155. double c = fmin(a, b);
  156. if (c==0 && a==b)
  157. return signbit(a) ? a : b;
  158. return c;
  159. }
  160. static inline uint32
  161. clz32(uint32 type)
  162. {
  163. uint32 num = 0;
  164. if (type == 0)
  165. return 32;
  166. while (!(type & 0x80000000)) {
  167. num++;
  168. type <<= 1;
  169. }
  170. return num;
  171. }
  172. static inline uint32
  173. clz64(uint64 type)
  174. {
  175. uint32 num = 0;
  176. if (type == 0)
  177. return 64;
  178. while (!(type & 0x8000000000000000LL)) {
  179. num++;
  180. type <<= 1;
  181. }
  182. return num;
  183. }
  184. static inline uint32
  185. ctz32(uint32 type)
  186. {
  187. uint32 num = 0;
  188. if (type == 0)
  189. return 32;
  190. while (!(type & 1)) {
  191. num++;
  192. type >>= 1;
  193. }
  194. return num;
  195. }
  196. static inline uint32
  197. ctz64(uint64 type)
  198. {
  199. uint32 num = 0;
  200. if (type == 0)
  201. return 64;
  202. while (!(type & 1)) {
  203. num++;
  204. type >>= 1;
  205. }
  206. return num;
  207. }
  208. static inline uint32
  209. popcount32(uint32 u)
  210. {
  211. uint32 ret = 0;
  212. while (u) {
  213. u = (u & (u - 1));
  214. ret++;
  215. }
  216. return ret;
  217. }
  218. static inline uint32
  219. popcount64(uint64 u)
  220. {
  221. uint32 ret = 0;
  222. while (u) {
  223. u = (u & (u - 1));
  224. ret++;
  225. }
  226. return ret;
  227. }
  228. static inline WASMGlobalInstance*
  229. get_global(const WASMModuleInstance *module, uint32 global_idx)
  230. {
  231. if (global_idx >= module->global_count)
  232. return NULL;
  233. return module->globals + global_idx;
  234. }
  235. static inline uint8*
  236. get_global_addr(WASMMemoryInstance *memory, WASMGlobalInstance *global)
  237. {
  238. return memory->global_data + global->data_offset;
  239. }
  240. static uint64
  241. read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
  242. {
  243. uint64 result = 0;
  244. uint32 shift = 0;
  245. uint32 bcnt = 0;
  246. uint64 byte;
  247. while (true) {
  248. byte = buf[*p_offset];
  249. *p_offset += 1;
  250. result |= ((byte & 0x7f) << shift);
  251. shift += 7;
  252. if ((byte & 0x80) == 0) {
  253. break;
  254. }
  255. bcnt += 1;
  256. }
  257. if (sign && (shift < maxbits) && (byte & 0x40)) {
  258. /* Sign extend */
  259. result |= - ((uint64)1 << shift);
  260. }
  261. return result;
  262. }
  263. #define PUSH_I32(value) do { \
  264. *(int32*)frame_sp++ = (int32)(value); \
  265. } while (0)
  266. #define PUSH_F32(value) do { \
  267. *(float32*)frame_sp++ = (float32)(value); \
  268. } while (0)
  269. #define PUSH_I64(value) do { \
  270. PUT_I64_TO_ADDR(frame_sp, value); \
  271. frame_sp += 2; \
  272. } while (0)
  273. #define PUSH_F64(value) do { \
  274. PUT_F64_TO_ADDR(frame_sp, value); \
  275. frame_sp += 2; \
  276. } while (0)
  277. #define PUSH_CSP(type, ret_type, start, else_, end) do {\
  278. wasm_assert(frame_csp < frame->csp_boundary); \
  279. frame_csp->block_type = type; \
  280. frame_csp->return_type = ret_type; \
  281. frame_csp->start_addr = start; \
  282. frame_csp->else_addr = else_; \
  283. frame_csp->end_addr = end; \
  284. frame_csp->frame_sp = frame_sp; \
  285. frame_csp++; \
  286. } while (0)
  287. #define POP_I32() (--frame_sp, *(int32*)frame_sp)
  288. #define POP_F32() (--frame_sp, *(float32*)frame_sp)
  289. #define POP_I64() (frame_sp -= 2, GET_I64_FROM_ADDR(frame_sp))
  290. #define POP_F64() (frame_sp -= 2, GET_F64_FROM_ADDR(frame_sp))
  291. #define POP_CSP_CHECK_OVERFLOW(n) do { \
  292. wasm_assert(frame_csp - n >= frame->csp_bottom); \
  293. } while (0)
  294. #define POP_CSP() do { \
  295. POP_CSP_CHECK_OVERFLOW(1); \
  296. --frame_csp; \
  297. } while (0)
  298. #define POP_CSP_N(n) do { \
  299. uint32 *frame_sp_old = frame_sp; \
  300. POP_CSP_CHECK_OVERFLOW(n + 1); \
  301. frame_csp -= n; \
  302. if ((frame_csp - 1)->block_type != BLOCK_TYPE_LOOP) \
  303. /* block block/if/function, jump to end of block */ \
  304. frame_ip = (frame_csp - 1)->end_addr; \
  305. else /* loop block, jump to start of block */ \
  306. frame_ip = (frame_csp - 1)->start_addr; \
  307. /* copy return value of block */ \
  308. frame_sp = (frame_csp - 1)->frame_sp; \
  309. switch ((frame_csp - 1)->return_type) { \
  310. case VALUE_TYPE_I32: \
  311. PUSH_I32(*(frame_sp_old - 1)); \
  312. break; \
  313. case VALUE_TYPE_I64: \
  314. PUSH_I64(GET_I64_FROM_ADDR(frame_sp_old - 2)); \
  315. break; \
  316. case VALUE_TYPE_F32: \
  317. PUSH_F32(*(float32*)(frame_sp_old - 1)); \
  318. break; \
  319. case VALUE_TYPE_F64: \
  320. PUSH_F64(GET_F64_FROM_ADDR(frame_sp_old - 2)); \
  321. break; \
  322. } \
  323. } while (0)
  324. #define local_off(n) (frame_lp + cur_func->local_offsets[n])
  325. #define LOCAL_I32(n) (*(int32*)(local_off(n)))
  326. #define SET_LOCAL_I32(N, val) do { \
  327. int n = (N); \
  328. *(int32*)(local_off(n)) = (int32)(val); \
  329. } while (0)
  330. #define LOCAL_F32(n) (*(float32*)(local_off(n)))
  331. #define SET_LOCAL_F32(N, val) do { \
  332. int n = (N); \
  333. *(float32*)(local_off(n)) = (float32)(val); \
  334. } while (0)
  335. #define LOCAL_I64(n) (GET_I64_FROM_ADDR(local_off(n)))
  336. #define SET_LOCAL_I64(N, val) do { \
  337. int n = (N); \
  338. PUT_I64_TO_ADDR(local_off(n), val); \
  339. } while (0)
  340. #define LOCAL_F64(n) (GET_F64_FROM_ADDR(local_off(n)))
  341. #define SET_LOCAL_F64(N, val) do { \
  342. int n = (N); \
  343. PUT_F64_TO_ADDR(local_off(n), val); \
  344. } while (0)
  345. /* Pop the given number of elements from the given frame's stack. */
  346. #define POP(N) do { \
  347. int n = (N); \
  348. frame_sp -= n; \
  349. } while (0)
  350. #define SYNC_ALL_TO_FRAME() do { \
  351. frame->sp = frame_sp; \
  352. frame->ip = frame_ip; \
  353. frame->csp = frame_csp; \
  354. } while (0)
  355. #define UPDATE_ALL_FROM_FRAME() do { \
  356. frame_sp = frame->sp; \
  357. frame_ip = frame->ip; \
  358. frame_csp = frame->csp; \
  359. } while (0)
  360. #define read_leb_uint64(p, p_end, res) do { \
  361. uint32 _off = 0; \
  362. res = read_leb(p, &_off, 64, false); \
  363. p += _off; \
  364. } while (0)
  365. #define read_leb_int64(p, p_end, res) do { \
  366. uint32 _off = 0; \
  367. res = (int64)read_leb(p, &_off, 64, true); \
  368. p += _off; \
  369. } while (0)
  370. #define read_leb_uint32(p, p_end, res) do { \
  371. uint32 _off = 0; \
  372. res = (uint32)read_leb(p, &_off, 32, false); \
  373. p += _off; \
  374. } while (0)
  375. #define read_leb_int32(p, p_end, res) do { \
  376. uint32 _off = 0; \
  377. res = (int32)read_leb(p, &_off, 32, true); \
  378. p += _off; \
  379. } while (0)
  380. #define read_leb_uint8(p, p_end, res) do { \
  381. uint32 _off = 0; \
  382. res = (uint8)read_leb(p, &_off, 7, false); \
  383. p += _off; \
  384. } while (0)
  385. #define RECOVER_CONTEXT(new_frame) do { \
  386. frame = (new_frame); \
  387. cur_func = frame->function; \
  388. prev_frame = frame->prev_frame; \
  389. frame_ip = frame->ip; \
  390. frame_ip_end = wasm_runtime_get_func_code_end(cur_func); \
  391. frame_lp = frame->lp; \
  392. frame_sp = frame->sp; \
  393. frame_csp = frame->csp; \
  394. } while (0)
  395. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  396. #define GET_OPCODE() opcode = *(frame_ip - 1);
  397. #else
  398. #define GET_OPCODE() (void)0
  399. #endif
  400. #define DEF_OP_LOAD(operation) do { \
  401. uint32 offset, flags, addr; \
  402. GET_OPCODE(); \
  403. read_leb_uint32(frame_ip, frame_ip_end, flags); \
  404. read_leb_uint32(frame_ip, frame_ip_end, offset); \
  405. addr = POP_I32(); \
  406. CHECK_MEMORY_OVERFLOW(); \
  407. operation; \
  408. (void)flags; \
  409. } while (0)
  410. #define DEF_OP_STORE(sval_type, sval_op_type, operation) do { \
  411. uint32 offset, flags, addr; \
  412. sval_type sval; \
  413. GET_OPCODE(); \
  414. read_leb_uint32(frame_ip, frame_ip_end, flags); \
  415. read_leb_uint32(frame_ip, frame_ip_end, offset); \
  416. sval = POP_##sval_op_type(); \
  417. addr = POP_I32(); \
  418. CHECK_MEMORY_OVERFLOW(); \
  419. operation; \
  420. (void)flags; \
  421. } while (0)
  422. #define DEF_OP_I_CONST(ctype, src_op_type) do { \
  423. ctype cval; \
  424. read_leb_##ctype(frame_ip, frame_ip_end, cval); \
  425. PUSH_##src_op_type(cval); \
  426. } while (0)
  427. #define DEF_OP_EQZ(src_op_type) do { \
  428. uint32 val; \
  429. val = POP_##src_op_type() == 0; \
  430. PUSH_I32(val); \
  431. } while (0)
  432. #define DEF_OP_CMP(src_type, src_op_type, cond) do { \
  433. uint32 res; \
  434. src_type val1, val2; \
  435. val2 = POP_##src_op_type(); \
  436. val1 = POP_##src_op_type(); \
  437. res = val1 cond val2; \
  438. PUSH_I32(res); \
  439. } while (0)
  440. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) do { \
  441. src_type val1, val2; \
  442. val1 = POP_##src_op_type(); \
  443. val2 = operation(val1); \
  444. PUSH_##src_op_type(val2); \
  445. } while (0)
  446. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) do { \
  447. frame_sp -= sizeof(src_type2)/sizeof(uint32); \
  448. *(src_type1*)(frame_sp - sizeof(src_type1)/sizeof(uint32)) operation##= \
  449. *(src_type2*)(frame_sp); \
  450. } while (0)
  451. #define DEF_OP_MATH(src_type, src_op_type, method) do { \
  452. src_type val; \
  453. val = POP_##src_op_type(); \
  454. PUSH_##src_op_type(method(val)); \
  455. } while (0)
  456. #define DEF_OP_TRUNC(dst_type, dst_op_type, src_type, src_op_type, \
  457. min_cond, max_cond) do { \
  458. src_type value = POP_##src_op_type(); \
  459. if (isnan(value)) { \
  460. wasm_runtime_set_exception(module, \
  461. "invalid conversion to integer"); \
  462. goto got_exception; \
  463. } \
  464. else if (value min_cond || value max_cond) { \
  465. wasm_runtime_set_exception(module, "integer overflow"); \
  466. goto got_exception; \
  467. } \
  468. PUSH_##dst_op_type(((dst_type)value)); \
  469. } while (0)
  470. #define DEF_OP_CONVERT(dst_type, dst_op_type, \
  471. src_type, src_op_type) do { \
  472. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  473. PUSH_##dst_op_type(value); \
  474. } while (0)
  475. #define GET_LOCAL_INDEX_AND_TYPE() do { \
  476. param_count = cur_func->u.func->func_type->param_count; \
  477. local_count = cur_func->u.func->local_count; \
  478. read_leb_uint32(frame_ip, frame_ip_end, local_idx); \
  479. wasm_assert(local_idx < param_count + local_count); \
  480. if (local_idx < param_count) \
  481. local_type = cur_func->u.func->func_type->types[local_idx]; \
  482. else \
  483. local_type = \
  484. cur_func->u.func->local_types[local_idx - param_count]; \
  485. } while (0)
  486. static inline int32
  487. sign_ext_8_32(int8 val)
  488. {
  489. if (val & 0x80)
  490. return val | 0xffffff00;
  491. return val;
  492. }
  493. static inline int32
  494. sign_ext_16_32(int16 val)
  495. {
  496. if (val & 0x8000)
  497. return val | 0xffff0000;
  498. return val;
  499. }
  500. static inline int64
  501. sign_ext_8_64(int8 val)
  502. {
  503. if (val & 0x80)
  504. return val | 0xffffffffffffff00;
  505. return val;
  506. }
  507. static inline int64
  508. sign_ext_16_64(int16 val)
  509. {
  510. if (val & 0x8000)
  511. return val | 0xffffffffffff0000;
  512. return val;
  513. }
  514. static inline int64
  515. sign_ext_32_64(int32 val)
  516. {
  517. if (val & 0x80000000)
  518. return val | 0xffffffff00000000;
  519. return val;
  520. }
  521. static inline void
  522. word_copy(uint32 *dest, uint32 *src, unsigned num)
  523. {
  524. for (; num > 0; num--)
  525. *dest++ = *src++;
  526. }
  527. static inline WASMInterpFrame*
  528. ALLOC_FRAME(WASMThread *self, uint32 size, WASMInterpFrame *prev_frame)
  529. {
  530. WASMInterpFrame *frame = wasm_thread_alloc_wasm_frame(self, size);
  531. if (frame)
  532. frame->prev_frame = prev_frame;
  533. else {
  534. wasm_runtime_set_exception(self->module_inst,
  535. "WASM interp failed: stack overflow.");
  536. }
  537. return frame;
  538. }
  539. static inline void
  540. FREE_FRAME(WASMThread *self, WASMInterpFrame *frame)
  541. {
  542. wasm_thread_free_wasm_frame(self, frame);
  543. }
  544. static void
  545. wasm_interp_call_func_native(WASMThread *self,
  546. WASMFunctionInstance *cur_func,
  547. WASMInterpFrame *prev_frame)
  548. {
  549. unsigned local_cell_num = 2;
  550. WASMInterpFrame *frame;
  551. uint32 argv_ret[2];
  552. bool ret;
  553. if (!(frame = ALLOC_FRAME
  554. (self, wasm_interp_interp_frame_size(local_cell_num), prev_frame)))
  555. return;
  556. frame->function = cur_func;
  557. frame->ip = NULL;
  558. frame->sp = frame->lp + local_cell_num;
  559. wasm_thread_set_cur_frame (self, frame);
  560. ret = wasm_runtime_invoke_native(cur_func->u.func_import->func_ptr_linked,
  561. cur_func->u.func_import->func_type,
  562. self->module_inst,
  563. frame->lp, cur_func->param_cell_num, argv_ret);
  564. if (!ret)
  565. return;
  566. if (cur_func->ret_cell_num == 1) {
  567. prev_frame->sp[0] = argv_ret[0];
  568. prev_frame->sp++;
  569. }
  570. else if (cur_func->ret_cell_num == 2) {
  571. prev_frame->sp[0] = argv_ret[0];
  572. prev_frame->sp[1] = argv_ret[1];
  573. prev_frame->sp += 2;
  574. }
  575. FREE_FRAME(self, frame);
  576. wasm_thread_set_cur_frame(self, prev_frame);
  577. }
  578. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  579. #define HANDLE_OP(opcode) HANDLE_##opcode
  580. #define FETCH_OPCODE_AND_DISPATCH() goto *handle_table[*frame_ip++]
  581. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  582. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  583. #define HANDLE_OP(opcode) case opcode
  584. #define HANDLE_OP_END() continue
  585. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  586. static void
  587. wasm_interp_call_func_bytecode(WASMThread *self,
  588. WASMFunctionInstance *cur_func,
  589. WASMInterpFrame *prev_frame)
  590. {
  591. WASMModuleInstance *module = self->module_inst;
  592. WASMMemoryInstance *memory = module->default_memory;
  593. uint32 memory_data_size = memory
  594. ? NumBytesPerPage * memory->cur_page_count : 0;
  595. uint32 heap_base_offset = memory ? memory->heap_base_offset : 0;
  596. uint32 heap_data_size = memory
  597. ? memory->heap_data_end - memory->heap_data : 0;
  598. WASMTableInstance *table = module->default_table;
  599. uint8 opcode_IMPDEP2 = WASM_OP_IMPDEP2;
  600. WASMInterpFrame *frame = NULL;
  601. /* Points to this special opcode so as to jump to the
  602. call_method_from_entry. */
  603. register uint8 *frame_ip = &opcode_IMPDEP2; /* cache of frame->ip */
  604. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  605. register uint32 *frame_sp = NULL; /* cache of frame->sp */
  606. WASMBranchBlock *frame_csp = NULL;
  607. uint8 *frame_ip_end = frame_ip + 1;
  608. uint8 opcode, block_ret_type;
  609. uint32 *depths = NULL;
  610. uint32 depth_buf[BR_TABLE_TMP_BUF_LEN];
  611. uint32 i, depth, cond, count, fidx, tidx, frame_size = 0, all_cell_num = 0;
  612. int32 didx, val;
  613. uint8 *else_addr, *end_addr;
  614. uint8 *maddr = NULL;
  615. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  616. #define HANDLE_OPCODE(op) &&HANDLE_##op
  617. DEFINE_GOTO_TABLE (handle_table);
  618. #undef HANDLE_OPCODE
  619. #endif
  620. /* Size of memory load.
  621. This starts with the first memory load operator at opcode 0x28 */
  622. uint32 LOAD_SIZE[] = {
  623. 4, 8, 4, 8, 1, 1, 2, 2, 1, 1, 2, 2, 4, 4, /* loads */
  624. 4, 8, 4, 8, 1, 2, 1, 2, 4 }; /* stores */
  625. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  626. while (frame_ip < frame_ip_end) {
  627. opcode = *frame_ip++;
  628. switch (opcode) {
  629. #else
  630. FETCH_OPCODE_AND_DISPATCH ();
  631. #endif
  632. /* control instructions */
  633. HANDLE_OP (WASM_OP_UNREACHABLE):
  634. wasm_runtime_set_exception(module, "unreachable");
  635. goto got_exception;
  636. HANDLE_OP (WASM_OP_NOP):
  637. HANDLE_OP_END ();
  638. HANDLE_OP (WASM_OP_BLOCK):
  639. read_leb_uint32(frame_ip, frame_ip_end, block_ret_type);
  640. if (!wasm_loader_find_block_addr(module->module,
  641. frame_ip, frame_ip_end,
  642. BLOCK_TYPE_BLOCK,
  643. &else_addr, &end_addr,
  644. NULL, 0)) {
  645. wasm_runtime_set_exception(module, "find block address failed");
  646. goto got_exception;
  647. }
  648. PUSH_CSP(BLOCK_TYPE_BLOCK, block_ret_type, frame_ip, NULL, end_addr);
  649. HANDLE_OP_END ();
  650. HANDLE_OP (WASM_OP_LOOP):
  651. read_leb_uint32(frame_ip, frame_ip_end, block_ret_type);
  652. if (!wasm_loader_find_block_addr(module->module,
  653. frame_ip, frame_ip_end,
  654. BLOCK_TYPE_LOOP,
  655. &else_addr, &end_addr,
  656. NULL, 0)) {
  657. wasm_runtime_set_exception(module, "find block address failed");
  658. goto got_exception;
  659. }
  660. PUSH_CSP(BLOCK_TYPE_LOOP, block_ret_type, frame_ip, NULL, end_addr);
  661. HANDLE_OP_END ();
  662. HANDLE_OP (WASM_OP_IF):
  663. read_leb_uint32(frame_ip, frame_ip_end, block_ret_type);
  664. if (!wasm_loader_find_block_addr(module->module,
  665. frame_ip, frame_ip_end,
  666. BLOCK_TYPE_IF,
  667. &else_addr, &end_addr,
  668. NULL, 0)) {
  669. wasm_runtime_set_exception(module, "find block address failed");
  670. goto got_exception;
  671. }
  672. cond = POP_I32();
  673. PUSH_CSP(BLOCK_TYPE_IF, block_ret_type, frame_ip, else_addr, end_addr);
  674. /* condition of the if branch is false, else condition is met */
  675. if (cond == 0) {
  676. /* if there is no else branch, go to the end addr */
  677. if (else_addr == NULL) {
  678. POP_CSP();
  679. frame_ip = end_addr + 1;
  680. }
  681. /* if there is an else branch, go to the else addr */
  682. else
  683. frame_ip = else_addr + 1;
  684. }
  685. HANDLE_OP_END ();
  686. HANDLE_OP (WASM_OP_ELSE):
  687. /* comes from the if branch in WASM_OP_IF */
  688. frame_ip = (frame_csp - 1)->end_addr;
  689. HANDLE_OP_END ();
  690. HANDLE_OP (WASM_OP_END):
  691. if (frame_csp > frame->csp_bottom + 1) {
  692. POP_CSP();
  693. }
  694. else { /* end of function, treat as WASM_OP_RETURN */
  695. frame_sp -= cur_func->ret_cell_num;
  696. for (i = 0; i < cur_func->ret_cell_num; i++) {
  697. *prev_frame->sp++ = frame_sp[i];
  698. }
  699. goto return_func;
  700. }
  701. HANDLE_OP_END ();
  702. HANDLE_OP (WASM_OP_BR):
  703. read_leb_uint32(frame_ip, frame_ip_end, depth);
  704. POP_CSP_N(depth);
  705. HANDLE_OP_END ();
  706. HANDLE_OP (WASM_OP_BR_IF):
  707. read_leb_uint32(frame_ip, frame_ip_end, depth);
  708. cond = POP_I32();
  709. if (cond)
  710. POP_CSP_N(depth);
  711. HANDLE_OP_END ();
  712. HANDLE_OP (WASM_OP_BR_TABLE):
  713. read_leb_uint32(frame_ip, frame_ip_end, count);
  714. if (count <= BR_TABLE_TMP_BUF_LEN)
  715. depths = depth_buf;
  716. else {
  717. if (!(depths = wasm_malloc(sizeof(uint32) * count))) {
  718. wasm_runtime_set_exception(module,
  719. "WASM interp failed: "
  720. "allocate memory failed.");
  721. goto got_exception;
  722. }
  723. }
  724. for (i = 0; i < count; i++) {
  725. read_leb_uint32(frame_ip, frame_ip_end, depths[i]);
  726. }
  727. read_leb_uint32(frame_ip, frame_ip_end, depth);
  728. didx = POP_I32();
  729. if (didx >= 0 && (uint32)didx < count) {
  730. depth = depths[didx];
  731. }
  732. if (depths != depth_buf) {
  733. wasm_free(depths);
  734. depths = NULL;
  735. }
  736. POP_CSP_N(depth);
  737. HANDLE_OP_END ();
  738. HANDLE_OP (WASM_OP_RETURN):
  739. frame_sp -= cur_func->ret_cell_num;
  740. for (i = 0; i < cur_func->ret_cell_num; i++) {
  741. *prev_frame->sp++ = frame_sp[i];
  742. }
  743. goto return_func;
  744. HANDLE_OP (WASM_OP_CALL):
  745. read_leb_uint32(frame_ip, frame_ip_end, fidx);
  746. wasm_assert(fidx < module->function_count);
  747. cur_func = module->functions + fidx;
  748. goto call_func_from_interp;
  749. HANDLE_OP (WASM_OP_CALL_INDIRECT):
  750. {
  751. WASMType *cur_type, *cur_func_type;
  752. /* TODO: test */
  753. read_leb_uint32(frame_ip, frame_ip_end, tidx);
  754. if (tidx >= module->module->type_count) {
  755. wasm_runtime_set_exception(module, "type index is overflow");
  756. goto got_exception;
  757. }
  758. cur_type = module->module->types[tidx];
  759. /* to skip 0x00 here */
  760. frame_ip++;
  761. val = POP_I32();
  762. if (val < 0 || val >= (int32)table->cur_size) {
  763. wasm_runtime_set_exception(module, "undefined element");
  764. goto got_exception;
  765. }
  766. fidx = ((uint32*)table->base_addr)[val];
  767. /* Skip function index check, it has been checked
  768. in wasm module instantiate */
  769. cur_func = module->functions + fidx;
  770. if (cur_func->is_import_func)
  771. cur_func_type = cur_func->u.func_import->func_type;
  772. else
  773. cur_func_type = cur_func->u.func->func_type;
  774. if (!wasm_type_equal(cur_type, cur_func_type)) {
  775. wasm_runtime_set_exception(module, "indirect call type mismatch");
  776. goto got_exception;
  777. }
  778. goto call_func_from_interp;
  779. }
  780. /* parametric instructions */
  781. HANDLE_OP (WASM_OP_DROP):
  782. {
  783. wasm_runtime_set_exception(module,
  784. "WASM interp failed: unsupported opcode.");
  785. goto got_exception;
  786. }
  787. HANDLE_OP (WASM_OP_DROP_32):
  788. {
  789. frame_sp--;
  790. HANDLE_OP_END ();
  791. }
  792. HANDLE_OP (WASM_OP_DROP_64):
  793. {
  794. frame_sp -= 2;
  795. HANDLE_OP_END ();
  796. }
  797. HANDLE_OP (WASM_OP_SELECT):
  798. {
  799. wasm_runtime_set_exception(module,
  800. "WASM interp failed: unsupported opcode.");
  801. goto got_exception;
  802. }
  803. HANDLE_OP (WASM_OP_SELECT_32):
  804. {
  805. cond = POP_I32();
  806. frame_sp--;
  807. if (!cond)
  808. *(frame_sp - 1) = *frame_sp;
  809. HANDLE_OP_END ();
  810. }
  811. HANDLE_OP (WASM_OP_SELECT_64):
  812. {
  813. cond = POP_I32();
  814. frame_sp -= 2;
  815. if (!cond) {
  816. *(frame_sp - 2) = *frame_sp;
  817. *(frame_sp - 1) = *(frame_sp + 1);
  818. }
  819. HANDLE_OP_END ();
  820. }
  821. /* variable instructions */
  822. HANDLE_OP (WASM_OP_GET_LOCAL):
  823. {
  824. uint32 local_idx, param_count, local_count;
  825. uint8 local_type;
  826. GET_LOCAL_INDEX_AND_TYPE();
  827. switch (local_type) {
  828. case VALUE_TYPE_I32:
  829. PUSH_I32(LOCAL_I32(local_idx));
  830. break;
  831. case VALUE_TYPE_F32:
  832. PUSH_F32(LOCAL_F32(local_idx));
  833. break;
  834. case VALUE_TYPE_I64:
  835. PUSH_I64(LOCAL_I64(local_idx));
  836. break;
  837. case VALUE_TYPE_F64:
  838. PUSH_F64(LOCAL_F64(local_idx));
  839. break;
  840. default:
  841. wasm_runtime_set_exception(module,
  842. "invalid local type");
  843. goto got_exception;
  844. }
  845. (void)local_count;
  846. HANDLE_OP_END ();
  847. }
  848. HANDLE_OP (WASM_OP_SET_LOCAL):
  849. {
  850. uint32 local_idx, param_count, local_count;
  851. uint8 local_type;
  852. GET_LOCAL_INDEX_AND_TYPE();
  853. switch (local_type) {
  854. case VALUE_TYPE_I32:
  855. SET_LOCAL_I32(local_idx, POP_I32());
  856. break;
  857. case VALUE_TYPE_F32:
  858. SET_LOCAL_F32(local_idx, POP_F32());
  859. break;
  860. case VALUE_TYPE_I64:
  861. SET_LOCAL_I64(local_idx, POP_I64());
  862. break;
  863. case VALUE_TYPE_F64:
  864. SET_LOCAL_F64(local_idx, POP_F64());
  865. break;
  866. default:
  867. wasm_runtime_set_exception(module,
  868. "invalid local type");
  869. goto got_exception;
  870. }
  871. (void)local_count;
  872. HANDLE_OP_END ();
  873. }
  874. HANDLE_OP (WASM_OP_TEE_LOCAL):
  875. {
  876. uint32 local_idx, param_count, local_count;
  877. uint8 local_type;
  878. GET_LOCAL_INDEX_AND_TYPE();
  879. switch (local_type) {
  880. case VALUE_TYPE_I32:
  881. SET_LOCAL_I32(local_idx, *(frame_sp - 1));
  882. break;
  883. case VALUE_TYPE_F32:
  884. SET_LOCAL_F32(local_idx, *(float32*)(frame_sp - 1));
  885. break;
  886. case VALUE_TYPE_I64:
  887. SET_LOCAL_I64(local_idx, GET_I64_FROM_ADDR(frame_sp - 2));
  888. break;
  889. case VALUE_TYPE_F64:
  890. SET_LOCAL_F64(local_idx, GET_F64_FROM_ADDR(frame_sp - 2));
  891. break;
  892. default:
  893. wasm_runtime_set_exception(module, "invalid local type");
  894. goto got_exception;
  895. }
  896. (void)local_count;
  897. HANDLE_OP_END ();
  898. }
  899. HANDLE_OP (WASM_OP_GET_GLOBAL):
  900. {
  901. WASMGlobalInstance *global;
  902. uint32 global_idx;
  903. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  904. global = get_global(module, global_idx);
  905. wasm_assert(global && global_idx < module->global_count);
  906. switch (global->type) {
  907. case VALUE_TYPE_I32:
  908. PUSH_I32(*(uint32*)get_global_addr(memory, global));
  909. break;
  910. case VALUE_TYPE_F32:
  911. PUSH_F32(*(float32*)get_global_addr(memory, global));
  912. break;
  913. case VALUE_TYPE_I64:
  914. PUSH_I64(*(uint64*)get_global_addr(memory, global));
  915. break;
  916. case VALUE_TYPE_F64:
  917. PUSH_F64(*(float64*)get_global_addr(memory, global));
  918. break;
  919. default:
  920. wasm_runtime_set_exception(module, "invalid global type");
  921. goto got_exception;
  922. }
  923. HANDLE_OP_END ();
  924. }
  925. HANDLE_OP (WASM_OP_SET_GLOBAL):
  926. {
  927. WASMGlobalInstance *global;
  928. uint32 global_idx;
  929. uint8 *global_addr;
  930. read_leb_uint32(frame_ip, frame_ip_end, global_idx);
  931. global = get_global(module, global_idx);
  932. wasm_assert(global && global_idx < module->global_count);
  933. global_addr = get_global_addr(memory, global);
  934. switch (global->type) {
  935. case VALUE_TYPE_I32:
  936. *(uint32*)global_addr = POP_I32();
  937. break;
  938. case VALUE_TYPE_F32:
  939. *(float32*)global_addr = POP_F32();
  940. break;
  941. case VALUE_TYPE_I64:
  942. PUT_I64_TO_ADDR((uint32*)global_addr, POP_I64());
  943. break;
  944. case VALUE_TYPE_F64:
  945. PUT_F64_TO_ADDR((uint32*)global_addr, POP_F64());
  946. break;
  947. default:
  948. wasm_runtime_set_exception(module, "invalid global type");
  949. goto got_exception;
  950. }
  951. HANDLE_OP_END ();
  952. }
  953. /* memory load instructions */
  954. HANDLE_OP (WASM_OP_I32_LOAD):
  955. HANDLE_OP (WASM_OP_I64_LOAD):
  956. HANDLE_OP (WASM_OP_F32_LOAD):
  957. HANDLE_OP (WASM_OP_F64_LOAD):
  958. HANDLE_OP (WASM_OP_I32_LOAD8_S):
  959. HANDLE_OP (WASM_OP_I32_LOAD8_U):
  960. HANDLE_OP (WASM_OP_I32_LOAD16_S):
  961. HANDLE_OP (WASM_OP_I32_LOAD16_U):
  962. HANDLE_OP (WASM_OP_I64_LOAD8_S):
  963. HANDLE_OP (WASM_OP_I64_LOAD8_U):
  964. HANDLE_OP (WASM_OP_I64_LOAD16_S):
  965. HANDLE_OP (WASM_OP_I64_LOAD16_U):
  966. HANDLE_OP (WASM_OP_I64_LOAD32_S):
  967. HANDLE_OP (WASM_OP_I64_LOAD32_U):
  968. {
  969. uint32 offset, flags, addr;
  970. GET_OPCODE();
  971. read_leb_uint32(frame_ip, frame_ip_end, flags);
  972. read_leb_uint32(frame_ip, frame_ip_end, offset);
  973. addr = POP_I32();
  974. CHECK_MEMORY_OVERFLOW();
  975. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  976. static const void *handle_load_table[] = {
  977. &&HANDLE_LOAD_WASM_OP_I32_LOAD,
  978. &&HANDLE_LOAD_WASM_OP_I64_LOAD,
  979. &&HANDLE_LOAD_WASM_OP_F32_LOAD,
  980. &&HANDLE_LOAD_WASM_OP_F64_LOAD,
  981. &&HANDLE_LOAD_WASM_OP_I32_LOAD8_S,
  982. &&HANDLE_LOAD_WASM_OP_I32_LOAD8_U,
  983. &&HANDLE_LOAD_WASM_OP_I32_LOAD16_S,
  984. &&HANDLE_LOAD_WASM_OP_I32_LOAD16_U,
  985. &&HANDLE_LOAD_WASM_OP_I64_LOAD8_S,
  986. &&HANDLE_LOAD_WASM_OP_I64_LOAD8_U,
  987. &&HANDLE_LOAD_WASM_OP_I64_LOAD16_S,
  988. &&HANDLE_LOAD_WASM_OP_I64_LOAD16_U,
  989. &&HANDLE_LOAD_WASM_OP_I64_LOAD32_S,
  990. &&HANDLE_LOAD_WASM_OP_I64_LOAD32_U
  991. };
  992. #define HANDLE_OP_LOAD(opcode) HANDLE_LOAD_##opcode
  993. goto *handle_load_table[opcode - WASM_OP_I32_LOAD];
  994. #else
  995. #define HANDLE_OP_LOAD(opcode) case opcode
  996. switch (opcode)
  997. #endif
  998. {
  999. HANDLE_OP_LOAD(WASM_OP_I32_LOAD):
  1000. PUSH_I32(*(int32*)maddr);
  1001. HANDLE_OP_END();
  1002. HANDLE_OP_LOAD(WASM_OP_I64_LOAD):
  1003. PUSH_I64(GET_I64_FROM_ADDR((uint32*)maddr));
  1004. HANDLE_OP_END();
  1005. HANDLE_OP_LOAD(WASM_OP_F32_LOAD):
  1006. PUSH_F32(*(float32*)maddr);
  1007. HANDLE_OP_END();
  1008. HANDLE_OP_LOAD(WASM_OP_F64_LOAD):
  1009. PUSH_F64(GET_F64_FROM_ADDR((uint32*)maddr));
  1010. HANDLE_OP_END();
  1011. HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_S):
  1012. PUSH_I32(sign_ext_8_32(*(int8*)maddr));
  1013. HANDLE_OP_END();
  1014. HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_U):
  1015. PUSH_I32((uint32)(*(uint8*)maddr));
  1016. HANDLE_OP_END();
  1017. HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_S):
  1018. PUSH_I32(sign_ext_16_32(*(int16*)maddr));
  1019. HANDLE_OP_END();
  1020. HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_U):
  1021. PUSH_I32((uint32)(*(uint16*)maddr));
  1022. HANDLE_OP_END();
  1023. HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_S):
  1024. PUSH_I64(sign_ext_8_64(*(int8*)maddr));
  1025. HANDLE_OP_END();
  1026. HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_U):
  1027. PUSH_I64((uint64)(*(uint8*)maddr));
  1028. HANDLE_OP_END();
  1029. HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_S):
  1030. PUSH_I64(sign_ext_16_64(*(int16*)maddr));
  1031. HANDLE_OP_END();
  1032. HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_U):
  1033. PUSH_I64((uint64)(*(uint16*)maddr));
  1034. HANDLE_OP_END();
  1035. HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_S):
  1036. PUSH_I64(sign_ext_32_64(*(int32*)maddr));
  1037. HANDLE_OP_END();
  1038. HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_U):
  1039. PUSH_I64((uint64)(*(uint32*)maddr));
  1040. HANDLE_OP_END();
  1041. }
  1042. (void)flags;
  1043. HANDLE_OP_END ();
  1044. }
  1045. /* memory store instructions */
  1046. HANDLE_OP (WASM_OP_F32_STORE):
  1047. {
  1048. uint32 offset, flags, addr;
  1049. GET_OPCODE();
  1050. read_leb_uint32(frame_ip, frame_ip_end, flags);
  1051. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1052. frame_sp--;
  1053. addr = POP_I32();
  1054. CHECK_MEMORY_OVERFLOW();
  1055. *(uint32*)maddr = frame_sp[1];
  1056. (void)flags;
  1057. HANDLE_OP_END ();
  1058. }
  1059. HANDLE_OP (WASM_OP_F64_STORE):
  1060. {
  1061. uint32 offset, flags, addr;
  1062. GET_OPCODE();
  1063. read_leb_uint32(frame_ip, frame_ip_end, flags);
  1064. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1065. frame_sp -= 2;
  1066. addr = POP_I32();
  1067. CHECK_MEMORY_OVERFLOW();
  1068. *(uint32*)maddr = frame_sp[1];
  1069. *((uint32*)maddr + 1) = frame_sp[2];
  1070. (void)flags;
  1071. HANDLE_OP_END ();
  1072. }
  1073. HANDLE_OP (WASM_OP_I32_STORE):
  1074. HANDLE_OP (WASM_OP_I32_STORE8):
  1075. HANDLE_OP (WASM_OP_I32_STORE16):
  1076. {
  1077. uint32 offset, flags, addr;
  1078. uint32 sval;
  1079. GET_OPCODE();
  1080. read_leb_uint32(frame_ip, frame_ip_end, flags);
  1081. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1082. sval = POP_I32();
  1083. addr = POP_I32();
  1084. CHECK_MEMORY_OVERFLOW();
  1085. switch (opcode) {
  1086. case WASM_OP_I32_STORE:
  1087. *(int32*)maddr = sval;
  1088. break;
  1089. case WASM_OP_I32_STORE8:
  1090. *(uint8*)maddr = (uint8)sval;
  1091. break;
  1092. case WASM_OP_I32_STORE16:
  1093. *(uint16*)maddr = (uint16)sval;
  1094. break;
  1095. }
  1096. (void)flags;
  1097. HANDLE_OP_END ();
  1098. }
  1099. HANDLE_OP (WASM_OP_I64_STORE):
  1100. HANDLE_OP (WASM_OP_I64_STORE8):
  1101. HANDLE_OP (WASM_OP_I64_STORE16):
  1102. HANDLE_OP (WASM_OP_I64_STORE32):
  1103. {
  1104. uint32 offset, flags, addr;
  1105. uint64 sval;
  1106. GET_OPCODE();
  1107. read_leb_uint32(frame_ip, frame_ip_end, flags);
  1108. read_leb_uint32(frame_ip, frame_ip_end, offset);
  1109. sval = POP_I64();
  1110. addr = POP_I32();
  1111. CHECK_MEMORY_OVERFLOW();
  1112. switch (opcode) {
  1113. case WASM_OP_I64_STORE:
  1114. PUT_I64_TO_ADDR((uint32*)maddr, sval);
  1115. break;
  1116. case WASM_OP_I64_STORE8:
  1117. *(uint8*)maddr = (uint8)sval;
  1118. break;
  1119. case WASM_OP_I64_STORE16:
  1120. *(uint16*)maddr = (uint16)sval;
  1121. break;
  1122. case WASM_OP_I64_STORE32:
  1123. *(uint32*)maddr = (uint32)sval;
  1124. break;
  1125. }
  1126. (void)flags;
  1127. HANDLE_OP_END ();
  1128. }
  1129. /* memory size and memory grow instructions */
  1130. HANDLE_OP (WASM_OP_MEMORY_SIZE):
  1131. {
  1132. uint32 reserved;
  1133. read_leb_uint32(frame_ip, frame_ip_end, reserved);
  1134. PUSH_I32(memory->cur_page_count);
  1135. (void)reserved;
  1136. HANDLE_OP_END ();
  1137. }
  1138. HANDLE_OP (WASM_OP_MEMORY_GROW):
  1139. {
  1140. uint32 reserved, delta, prev_page_count = memory->cur_page_count;
  1141. read_leb_uint32(frame_ip, frame_ip_end, reserved);
  1142. delta = POP_I32();
  1143. if (!wasm_runtime_enlarge_memory(module, delta)) {
  1144. /* fail to memory.grow, return -1 */
  1145. PUSH_I32(-1);
  1146. if (wasm_runtime_get_exception(module)) {
  1147. printf("%s\n", wasm_runtime_get_exception(module));
  1148. wasm_runtime_set_exception(module, NULL);
  1149. }
  1150. }
  1151. else {
  1152. /* success, return previous page count */
  1153. PUSH_I32(prev_page_count);
  1154. /* update the memory instance ptr */
  1155. memory = module->default_memory;
  1156. memory_data_size = NumBytesPerPage * memory->cur_page_count;
  1157. }
  1158. (void)reserved;
  1159. HANDLE_OP_END ();
  1160. }
  1161. /* constant instructions */
  1162. HANDLE_OP (WASM_OP_I32_CONST):
  1163. DEF_OP_I_CONST(int32, I32);
  1164. HANDLE_OP_END ();
  1165. HANDLE_OP (WASM_OP_I64_CONST):
  1166. DEF_OP_I_CONST(int64, I64);
  1167. HANDLE_OP_END ();
  1168. HANDLE_OP (WASM_OP_F32_CONST):
  1169. {
  1170. uint8 *p_float = (uint8*)frame_sp++;
  1171. for (i = 0; i < sizeof(float32); i++)
  1172. *p_float++ = *frame_ip++;
  1173. HANDLE_OP_END ();
  1174. }
  1175. HANDLE_OP (WASM_OP_F64_CONST):
  1176. {
  1177. uint8 *p_float = (uint8*)frame_sp++;
  1178. frame_sp++;
  1179. for (i = 0; i < sizeof(float64); i++)
  1180. *p_float++ = *frame_ip++;
  1181. HANDLE_OP_END ();
  1182. }
  1183. /* comparison instructions of i32 */
  1184. HANDLE_OP (WASM_OP_I32_EQZ):
  1185. DEF_OP_EQZ(I32);
  1186. HANDLE_OP_END ();
  1187. HANDLE_OP (WASM_OP_I32_EQ):
  1188. DEF_OP_CMP(uint32, I32, ==);
  1189. HANDLE_OP_END ();
  1190. HANDLE_OP (WASM_OP_I32_NE):
  1191. DEF_OP_CMP(uint32, I32, !=);
  1192. HANDLE_OP_END ();
  1193. HANDLE_OP (WASM_OP_I32_LT_S):
  1194. DEF_OP_CMP(int32, I32, <);
  1195. HANDLE_OP_END ();
  1196. HANDLE_OP (WASM_OP_I32_LT_U):
  1197. DEF_OP_CMP(uint32, I32, <);
  1198. HANDLE_OP_END ();
  1199. HANDLE_OP (WASM_OP_I32_GT_S):
  1200. DEF_OP_CMP(int32, I32, >);
  1201. HANDLE_OP_END ();
  1202. HANDLE_OP (WASM_OP_I32_GT_U):
  1203. DEF_OP_CMP(uint32, I32, >);
  1204. HANDLE_OP_END ();
  1205. HANDLE_OP (WASM_OP_I32_LE_S):
  1206. DEF_OP_CMP(int32, I32, <=);
  1207. HANDLE_OP_END ();
  1208. HANDLE_OP (WASM_OP_I32_LE_U):
  1209. DEF_OP_CMP(uint32, I32, <=);
  1210. HANDLE_OP_END ();
  1211. HANDLE_OP (WASM_OP_I32_GE_S):
  1212. DEF_OP_CMP(int32, I32, >=);
  1213. HANDLE_OP_END ();
  1214. HANDLE_OP (WASM_OP_I32_GE_U):
  1215. DEF_OP_CMP(uint32, I32, >=);
  1216. HANDLE_OP_END ();
  1217. /* comparison instructions of i64 */
  1218. HANDLE_OP (WASM_OP_I64_EQZ):
  1219. DEF_OP_EQZ(I64);
  1220. HANDLE_OP_END ();
  1221. HANDLE_OP (WASM_OP_I64_EQ):
  1222. DEF_OP_CMP(uint64, I64, ==);
  1223. HANDLE_OP_END ();
  1224. HANDLE_OP (WASM_OP_I64_NE):
  1225. DEF_OP_CMP(uint64, I64, !=);
  1226. HANDLE_OP_END ();
  1227. HANDLE_OP (WASM_OP_I64_LT_S):
  1228. DEF_OP_CMP(int64, I64, <);
  1229. HANDLE_OP_END ();
  1230. HANDLE_OP (WASM_OP_I64_LT_U):
  1231. DEF_OP_CMP(uint64, I64, <);
  1232. HANDLE_OP_END ();
  1233. HANDLE_OP (WASM_OP_I64_GT_S):
  1234. DEF_OP_CMP(int64, I64, >);
  1235. HANDLE_OP_END ();
  1236. HANDLE_OP (WASM_OP_I64_GT_U):
  1237. DEF_OP_CMP(uint64, I64, >);
  1238. HANDLE_OP_END ();
  1239. HANDLE_OP (WASM_OP_I64_LE_S):
  1240. DEF_OP_CMP(int64, I64, <=);
  1241. HANDLE_OP_END ();
  1242. HANDLE_OP (WASM_OP_I64_LE_U):
  1243. DEF_OP_CMP(uint64, I64, <=);
  1244. HANDLE_OP_END ();
  1245. HANDLE_OP (WASM_OP_I64_GE_S):
  1246. DEF_OP_CMP(int64, I64, >=);
  1247. HANDLE_OP_END ();
  1248. HANDLE_OP (WASM_OP_I64_GE_U):
  1249. DEF_OP_CMP(uint64, I64, >=);
  1250. HANDLE_OP_END ();
  1251. /* comparison instructions of f32 */
  1252. HANDLE_OP (WASM_OP_F32_EQ):
  1253. DEF_OP_CMP(float32, F32, ==);
  1254. HANDLE_OP_END ();
  1255. HANDLE_OP (WASM_OP_F32_NE):
  1256. DEF_OP_CMP(float32, F32, !=);
  1257. HANDLE_OP_END ();
  1258. HANDLE_OP (WASM_OP_F32_LT):
  1259. DEF_OP_CMP(float32, F32, <);
  1260. HANDLE_OP_END ();
  1261. HANDLE_OP (WASM_OP_F32_GT):
  1262. DEF_OP_CMP(float32, F32, >);
  1263. HANDLE_OP_END ();
  1264. HANDLE_OP (WASM_OP_F32_LE):
  1265. DEF_OP_CMP(float32, F32, <=);
  1266. HANDLE_OP_END ();
  1267. HANDLE_OP (WASM_OP_F32_GE):
  1268. DEF_OP_CMP(float32, F32, >=);
  1269. HANDLE_OP_END ();
  1270. /* comparison instructions of f64 */
  1271. HANDLE_OP (WASM_OP_F64_EQ):
  1272. DEF_OP_CMP(float64, F64, ==);
  1273. HANDLE_OP_END ();
  1274. HANDLE_OP (WASM_OP_F64_NE):
  1275. DEF_OP_CMP(float64, F64, !=);
  1276. HANDLE_OP_END ();
  1277. HANDLE_OP (WASM_OP_F64_LT):
  1278. DEF_OP_CMP(float64, F64, <);
  1279. HANDLE_OP_END ();
  1280. HANDLE_OP (WASM_OP_F64_GT):
  1281. DEF_OP_CMP(float64, F64, >);
  1282. HANDLE_OP_END ();
  1283. HANDLE_OP (WASM_OP_F64_LE):
  1284. DEF_OP_CMP(float64, F64, <=);
  1285. HANDLE_OP_END ();
  1286. HANDLE_OP (WASM_OP_F64_GE):
  1287. DEF_OP_CMP(float64, F64, >=);
  1288. HANDLE_OP_END ();
  1289. /* numberic instructions of i32 */
  1290. HANDLE_OP (WASM_OP_I32_CLZ):
  1291. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  1292. HANDLE_OP_END ();
  1293. HANDLE_OP (WASM_OP_I32_CTZ):
  1294. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  1295. HANDLE_OP_END ();
  1296. HANDLE_OP (WASM_OP_I32_POPCNT):
  1297. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  1298. HANDLE_OP_END ();
  1299. HANDLE_OP (WASM_OP_I32_ADD):
  1300. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  1301. HANDLE_OP_END ();
  1302. HANDLE_OP (WASM_OP_I32_SUB):
  1303. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  1304. HANDLE_OP_END ();
  1305. HANDLE_OP (WASM_OP_I32_MUL):
  1306. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  1307. HANDLE_OP_END ();
  1308. HANDLE_OP (WASM_OP_I32_DIV_S):
  1309. {
  1310. int32 a, b;
  1311. b = POP_I32();
  1312. a = POP_I32();
  1313. if (a == (int32)0x80000000 && b == -1) {
  1314. wasm_runtime_set_exception(module, "integer overflow");
  1315. goto got_exception;
  1316. }
  1317. if (b == 0) {
  1318. wasm_runtime_set_exception(module, "integer divide by zero");
  1319. goto got_exception;
  1320. }
  1321. PUSH_I32(a / b);
  1322. HANDLE_OP_END ();
  1323. }
  1324. HANDLE_OP (WASM_OP_I32_DIV_U):
  1325. {
  1326. uint32 a, b;
  1327. b = POP_I32();
  1328. a = POP_I32();
  1329. if (b == 0) {
  1330. wasm_runtime_set_exception(module, "integer divide by zero");
  1331. goto got_exception;
  1332. }
  1333. PUSH_I32(a / b);
  1334. HANDLE_OP_END ();
  1335. }
  1336. HANDLE_OP (WASM_OP_I32_REM_S):
  1337. {
  1338. int32 a, b;
  1339. b = POP_I32();
  1340. a = POP_I32();
  1341. if (a == (int32)0x80000000 && b == -1) {
  1342. PUSH_I32(0);
  1343. HANDLE_OP_END ();
  1344. }
  1345. if (b == 0) {
  1346. wasm_runtime_set_exception(module, "integer divide by zero");
  1347. goto got_exception;
  1348. }
  1349. PUSH_I32(a % b);
  1350. HANDLE_OP_END ();
  1351. }
  1352. HANDLE_OP (WASM_OP_I32_REM_U):
  1353. {
  1354. uint32 a, b;
  1355. b = POP_I32();
  1356. a = POP_I32();
  1357. if (b == 0) {
  1358. wasm_runtime_set_exception(module, "integer divide by zero");
  1359. goto got_exception;
  1360. }
  1361. PUSH_I32(a % b);
  1362. HANDLE_OP_END ();
  1363. }
  1364. HANDLE_OP (WASM_OP_I32_AND):
  1365. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  1366. HANDLE_OP_END ();
  1367. HANDLE_OP (WASM_OP_I32_OR):
  1368. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  1369. HANDLE_OP_END ();
  1370. HANDLE_OP (WASM_OP_I32_XOR):
  1371. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  1372. HANDLE_OP_END ();
  1373. HANDLE_OP (WASM_OP_I32_SHL):
  1374. DEF_OP_NUMERIC(uint32, uint32, I32, <<);
  1375. HANDLE_OP_END ();
  1376. HANDLE_OP (WASM_OP_I32_SHR_S):
  1377. DEF_OP_NUMERIC(int32, uint32, I32, >>);
  1378. HANDLE_OP_END ();
  1379. HANDLE_OP (WASM_OP_I32_SHR_U):
  1380. DEF_OP_NUMERIC(uint32, uint32, I32, >>);
  1381. HANDLE_OP_END ();
  1382. HANDLE_OP (WASM_OP_I32_ROTL):
  1383. {
  1384. uint32 a, b;
  1385. b = POP_I32();
  1386. a = POP_I32();
  1387. PUSH_I32(rotl32(a, b));
  1388. HANDLE_OP_END ();
  1389. }
  1390. HANDLE_OP (WASM_OP_I32_ROTR):
  1391. {
  1392. uint32 a, b;
  1393. b = POP_I32();
  1394. a = POP_I32();
  1395. PUSH_I32(rotr32(a, b));
  1396. HANDLE_OP_END ();
  1397. }
  1398. /* numberic instructions of i64 */
  1399. HANDLE_OP (WASM_OP_I64_CLZ):
  1400. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  1401. HANDLE_OP_END ();
  1402. HANDLE_OP (WASM_OP_I64_CTZ):
  1403. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  1404. HANDLE_OP_END ();
  1405. HANDLE_OP (WASM_OP_I64_POPCNT):
  1406. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  1407. HANDLE_OP_END ();
  1408. HANDLE_OP (WASM_OP_I64_ADD):
  1409. DEF_OP_NUMERIC(uint64, uint64, I64, +);
  1410. HANDLE_OP_END ();
  1411. HANDLE_OP (WASM_OP_I64_SUB):
  1412. DEF_OP_NUMERIC(uint64, uint64, I64, -);
  1413. HANDLE_OP_END ();
  1414. HANDLE_OP (WASM_OP_I64_MUL):
  1415. DEF_OP_NUMERIC(uint64, uint64, I64, *);
  1416. HANDLE_OP_END ();
  1417. HANDLE_OP (WASM_OP_I64_DIV_S):
  1418. {
  1419. int64 a, b;
  1420. b = POP_I64();
  1421. a = POP_I64();
  1422. if (a == (int64)0x8000000000000000LL && b == -1) {
  1423. wasm_runtime_set_exception(module, "integer overflow");
  1424. goto got_exception;
  1425. }
  1426. if (b == 0) {
  1427. wasm_runtime_set_exception(module, "integer divide by zero");
  1428. goto got_exception;
  1429. }
  1430. PUSH_I64(a / b);
  1431. HANDLE_OP_END ();
  1432. }
  1433. HANDLE_OP (WASM_OP_I64_DIV_U):
  1434. {
  1435. uint64 a, b;
  1436. b = POP_I64();
  1437. a = POP_I64();
  1438. if (b == 0) {
  1439. wasm_runtime_set_exception(module, "integer divide by zero");
  1440. goto got_exception;
  1441. }
  1442. PUSH_I64(a / b);
  1443. HANDLE_OP_END ();
  1444. }
  1445. HANDLE_OP (WASM_OP_I64_REM_S):
  1446. {
  1447. int64 a, b;
  1448. b = POP_I64();
  1449. a = POP_I64();
  1450. if (a == (int64)0x8000000000000000LL && b == -1) {
  1451. PUSH_I64(0);
  1452. HANDLE_OP_END ();
  1453. }
  1454. if (b == 0) {
  1455. wasm_runtime_set_exception(module, "integer divide by zero");
  1456. goto got_exception;
  1457. }
  1458. PUSH_I64(a % b);
  1459. HANDLE_OP_END ();
  1460. }
  1461. HANDLE_OP (WASM_OP_I64_REM_U):
  1462. {
  1463. uint64 a, b;
  1464. b = POP_I64();
  1465. a = POP_I64();
  1466. if (b == 0) {
  1467. wasm_runtime_set_exception(module, "integer divide by zero");
  1468. goto got_exception;
  1469. }
  1470. PUSH_I64(a % b);
  1471. HANDLE_OP_END ();
  1472. }
  1473. HANDLE_OP (WASM_OP_I64_AND):
  1474. DEF_OP_NUMERIC(uint64, uint64, I64, &);
  1475. HANDLE_OP_END ();
  1476. HANDLE_OP (WASM_OP_I64_OR):
  1477. DEF_OP_NUMERIC(uint64, uint64, I64, |);
  1478. HANDLE_OP_END ();
  1479. HANDLE_OP (WASM_OP_I64_XOR):
  1480. DEF_OP_NUMERIC(uint64, uint64, I64, ^);
  1481. HANDLE_OP_END ();
  1482. HANDLE_OP (WASM_OP_I64_SHL):
  1483. DEF_OP_NUMERIC(uint64, uint64, I64, <<);
  1484. HANDLE_OP_END ();
  1485. HANDLE_OP (WASM_OP_I64_SHR_S):
  1486. DEF_OP_NUMERIC(int64, uint64, I64, >>);
  1487. HANDLE_OP_END ();
  1488. HANDLE_OP (WASM_OP_I64_SHR_U):
  1489. DEF_OP_NUMERIC(uint64, uint64, I64, >>);
  1490. HANDLE_OP_END ();
  1491. HANDLE_OP (WASM_OP_I64_ROTL):
  1492. {
  1493. uint64 a, b;
  1494. b = POP_I64();
  1495. a = POP_I64();
  1496. PUSH_I64(rotl64(a, b));
  1497. HANDLE_OP_END ();
  1498. }
  1499. HANDLE_OP (WASM_OP_I64_ROTR):
  1500. {
  1501. uint64 a, b;
  1502. b = POP_I64();
  1503. a = POP_I64();
  1504. PUSH_I64(rotr64(a, b));
  1505. HANDLE_OP_END ();
  1506. }
  1507. /* numberic instructions of f32 */
  1508. HANDLE_OP (WASM_OP_F32_ABS):
  1509. DEF_OP_MATH(float32, F32, fabs);
  1510. HANDLE_OP_END ();
  1511. HANDLE_OP (WASM_OP_F32_NEG):
  1512. DEF_OP_MATH(float32, F32, -);
  1513. HANDLE_OP_END ();
  1514. HANDLE_OP (WASM_OP_F32_CEIL):
  1515. DEF_OP_MATH(float32, F32, ceil);
  1516. HANDLE_OP_END ();
  1517. HANDLE_OP (WASM_OP_F32_FLOOR):
  1518. DEF_OP_MATH(float32, F32, floor);
  1519. HANDLE_OP_END ();
  1520. HANDLE_OP (WASM_OP_F32_TRUNC):
  1521. DEF_OP_MATH(float32, F32, trunc);
  1522. HANDLE_OP_END ();
  1523. HANDLE_OP (WASM_OP_F32_NEAREST):
  1524. DEF_OP_MATH(float32, F32, rint);
  1525. HANDLE_OP_END ();
  1526. HANDLE_OP (WASM_OP_F32_SQRT):
  1527. DEF_OP_MATH(float32, F32, sqrt);
  1528. HANDLE_OP_END ();
  1529. HANDLE_OP (WASM_OP_F32_ADD):
  1530. DEF_OP_NUMERIC(float32, float32, F32, +);
  1531. HANDLE_OP_END ();
  1532. HANDLE_OP (WASM_OP_F32_SUB):
  1533. DEF_OP_NUMERIC(float32, float32, F32, -);
  1534. HANDLE_OP_END ();
  1535. HANDLE_OP (WASM_OP_F32_MUL):
  1536. DEF_OP_NUMERIC(float32, float32, F32, *);
  1537. HANDLE_OP_END ();
  1538. HANDLE_OP (WASM_OP_F32_DIV):
  1539. DEF_OP_NUMERIC(float32, float32, F32, /);
  1540. HANDLE_OP_END ();
  1541. HANDLE_OP (WASM_OP_F32_MIN):
  1542. {
  1543. float32 a, b;
  1544. b = POP_F32();
  1545. a = POP_F32();
  1546. if (isnan(a))
  1547. PUSH_F32(a);
  1548. else if (isnan(b))
  1549. PUSH_F32(b);
  1550. else
  1551. PUSH_F32(wa_fmin(a, b));
  1552. HANDLE_OP_END ();
  1553. }
  1554. HANDLE_OP (WASM_OP_F32_MAX):
  1555. {
  1556. float32 a, b;
  1557. b = POP_F32();
  1558. a = POP_F32();
  1559. if (isnan(a))
  1560. PUSH_F32(a);
  1561. else if (isnan(b))
  1562. PUSH_F32(b);
  1563. else
  1564. PUSH_F32(wa_fmax(a, b));
  1565. HANDLE_OP_END ();
  1566. }
  1567. HANDLE_OP (WASM_OP_F32_COPYSIGN):
  1568. {
  1569. float32 a, b;
  1570. b = POP_F32();
  1571. a = POP_F32();
  1572. PUSH_F32(signbit(b) ? -fabs(a) : fabs(a));
  1573. HANDLE_OP_END ();
  1574. }
  1575. /* numberic instructions of f64 */
  1576. HANDLE_OP (WASM_OP_F64_ABS):
  1577. DEF_OP_MATH(float64, F64, fabs);
  1578. HANDLE_OP_END ();
  1579. HANDLE_OP (WASM_OP_F64_NEG):
  1580. DEF_OP_MATH(float64, F64, -);
  1581. HANDLE_OP_END ();
  1582. HANDLE_OP (WASM_OP_F64_CEIL):
  1583. DEF_OP_MATH(float64, F64, ceil);
  1584. HANDLE_OP_END ();
  1585. HANDLE_OP (WASM_OP_F64_FLOOR):
  1586. DEF_OP_MATH(float64, F64, floor);
  1587. HANDLE_OP_END ();
  1588. HANDLE_OP (WASM_OP_F64_TRUNC):
  1589. DEF_OP_MATH(float64, F64, trunc);
  1590. HANDLE_OP_END ();
  1591. HANDLE_OP (WASM_OP_F64_NEAREST):
  1592. DEF_OP_MATH(float64, F64, rint);
  1593. HANDLE_OP_END ();
  1594. HANDLE_OP (WASM_OP_F64_SQRT):
  1595. DEF_OP_MATH(float64, F64, sqrt);
  1596. HANDLE_OP_END ();
  1597. HANDLE_OP (WASM_OP_F64_ADD):
  1598. DEF_OP_NUMERIC(float64, float64, F64, +);
  1599. HANDLE_OP_END ();
  1600. HANDLE_OP (WASM_OP_F64_SUB):
  1601. DEF_OP_NUMERIC(float64, float64, F64, -);
  1602. HANDLE_OP_END ();
  1603. HANDLE_OP (WASM_OP_F64_MUL):
  1604. DEF_OP_NUMERIC(float64, float64, F64, *);
  1605. HANDLE_OP_END ();
  1606. HANDLE_OP (WASM_OP_F64_DIV):
  1607. DEF_OP_NUMERIC(float64, float64, F64, /);
  1608. HANDLE_OP_END ();
  1609. HANDLE_OP (WASM_OP_F64_MIN):
  1610. {
  1611. float64 a, b;
  1612. b = POP_F64();
  1613. a = POP_F64();
  1614. if (isnan(a))
  1615. PUSH_F64(a);
  1616. else if (isnan(b))
  1617. PUSH_F64(b);
  1618. else
  1619. PUSH_F64(wa_fmin(a, b));
  1620. HANDLE_OP_END ();
  1621. }
  1622. HANDLE_OP (WASM_OP_F64_MAX):
  1623. {
  1624. float64 a, b;
  1625. b = POP_F64();
  1626. a = POP_F64();
  1627. if (isnan(a))
  1628. PUSH_F64(a);
  1629. else if (isnan(b))
  1630. PUSH_F64(b);
  1631. else
  1632. PUSH_F64(wa_fmax(a, b));
  1633. HANDLE_OP_END ();
  1634. }
  1635. HANDLE_OP (WASM_OP_F64_COPYSIGN):
  1636. {
  1637. float64 a, b;
  1638. b = POP_F64();
  1639. a = POP_F64();
  1640. PUSH_F64(signbit(b) ? -fabs(a) : fabs(a));
  1641. HANDLE_OP_END ();
  1642. }
  1643. /* conversions of i32 */
  1644. HANDLE_OP (WASM_OP_I32_WRAP_I64):
  1645. {
  1646. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  1647. PUSH_I32(value);
  1648. HANDLE_OP_END ();
  1649. }
  1650. HANDLE_OP (WASM_OP_I32_TRUNC_S_F32):
  1651. /* Copy the float32/float64 values from WAVM, need to test more.
  1652. We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  1653. since float/double values of ieee754 cannot precisely represent
  1654. all int32/uint32/int64/uint64 values, e.g.:
  1655. UINT32_MAX is 4294967295, but (float32)4294967295 is 4294967296.0f,
  1656. but not 4294967295.0f. */
  1657. DEF_OP_TRUNC(int32, I32, float32, F32, <= -2147483904.0f,
  1658. >= 2147483648.0f);
  1659. HANDLE_OP_END ();
  1660. HANDLE_OP (WASM_OP_I32_TRUNC_U_F32):
  1661. DEF_OP_TRUNC(uint32, I32, float32, F32, <= -1.0f,
  1662. >= 4294967296.0f);
  1663. HANDLE_OP_END ();
  1664. HANDLE_OP (WASM_OP_I32_TRUNC_S_F64):
  1665. DEF_OP_TRUNC(int32, I32, float64, F64, <= -2147483649.0,
  1666. >= 2147483648.0);
  1667. HANDLE_OP_END ();
  1668. HANDLE_OP (WASM_OP_I32_TRUNC_U_F64):
  1669. DEF_OP_TRUNC(uint32, I32, float64, F64, <= -1.0 ,
  1670. >= 4294967296.0);
  1671. HANDLE_OP_END ();
  1672. /* conversions of i64 */
  1673. HANDLE_OP (WASM_OP_I64_EXTEND_S_I32):
  1674. DEF_OP_CONVERT(int64, I64, int32, I32);
  1675. HANDLE_OP_END ();
  1676. HANDLE_OP (WASM_OP_I64_EXTEND_U_I32):
  1677. DEF_OP_CONVERT(int64, I64, uint32, I32);
  1678. HANDLE_OP_END ();
  1679. HANDLE_OP (WASM_OP_I64_TRUNC_S_F32):
  1680. DEF_OP_TRUNC(int64, I64, float32, F32, <= -9223373136366403584.0f,
  1681. >= 9223372036854775808.0f);
  1682. HANDLE_OP_END ();
  1683. HANDLE_OP (WASM_OP_I64_TRUNC_U_F32):
  1684. DEF_OP_TRUNC(uint64, I64, float32, F32, <= -1.0f,
  1685. >= 18446744073709551616.0f);
  1686. HANDLE_OP_END ();
  1687. HANDLE_OP (WASM_OP_I64_TRUNC_S_F64):
  1688. DEF_OP_TRUNC(int64, I64, float64, F64, <= -9223372036854777856.0,
  1689. >= 9223372036854775808.0);
  1690. HANDLE_OP_END ();
  1691. HANDLE_OP (WASM_OP_I64_TRUNC_U_F64):
  1692. DEF_OP_TRUNC(uint64, I64, float64, F64, <= -1.0,
  1693. >= 18446744073709551616.0);
  1694. HANDLE_OP_END ();
  1695. /* conversions of f32 */
  1696. HANDLE_OP (WASM_OP_F32_CONVERT_S_I32):
  1697. DEF_OP_CONVERT(float32, F32, int32, I32);
  1698. HANDLE_OP_END ();
  1699. HANDLE_OP (WASM_OP_F32_CONVERT_U_I32):
  1700. DEF_OP_CONVERT(float32, F32, uint32, I32);
  1701. HANDLE_OP_END ();
  1702. HANDLE_OP (WASM_OP_F32_CONVERT_S_I64):
  1703. DEF_OP_CONVERT(float32, F32, int64, I64);
  1704. HANDLE_OP_END ();
  1705. HANDLE_OP (WASM_OP_F32_CONVERT_U_I64):
  1706. DEF_OP_CONVERT(float32, F32, uint64, I64);
  1707. HANDLE_OP_END ();
  1708. HANDLE_OP (WASM_OP_F32_DEMOTE_F64):
  1709. DEF_OP_CONVERT(float32, F32, float64, F64);
  1710. HANDLE_OP_END ();
  1711. /* conversions of f64 */
  1712. HANDLE_OP (WASM_OP_F64_CONVERT_S_I32):
  1713. DEF_OP_CONVERT(float64, F64, int32, I32);
  1714. HANDLE_OP_END ();
  1715. HANDLE_OP (WASM_OP_F64_CONVERT_U_I32):
  1716. DEF_OP_CONVERT(float64, F64, uint32, I32);
  1717. HANDLE_OP_END ();
  1718. HANDLE_OP (WASM_OP_F64_CONVERT_S_I64):
  1719. DEF_OP_CONVERT(float64, F64, int64, I64);
  1720. HANDLE_OP_END ();
  1721. HANDLE_OP (WASM_OP_F64_CONVERT_U_I64):
  1722. DEF_OP_CONVERT(float64, F64, uint64, I64);
  1723. HANDLE_OP_END ();
  1724. HANDLE_OP (WASM_OP_F64_PROMOTE_F32):
  1725. DEF_OP_CONVERT(float64, F64, float32, F32);
  1726. HANDLE_OP_END ();
  1727. /* reinterpretations */
  1728. HANDLE_OP (WASM_OP_I32_REINTERPRET_F32):
  1729. HANDLE_OP (WASM_OP_I64_REINTERPRET_F64):
  1730. HANDLE_OP (WASM_OP_F32_REINTERPRET_I32):
  1731. HANDLE_OP (WASM_OP_F64_REINTERPRET_I64):
  1732. HANDLE_OP_END ();
  1733. HANDLE_OP (WASM_OP_IMPDEP2):
  1734. frame = prev_frame;
  1735. frame_ip = frame->ip;
  1736. frame_sp = frame->sp;
  1737. frame_csp = frame->csp;
  1738. goto call_func_from_entry;
  1739. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1740. default:
  1741. wasm_runtime_set_exception(module,
  1742. "WASM interp failed: unsupported opcode.");
  1743. goto got_exception;
  1744. }
  1745. #endif
  1746. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1747. HANDLE_OP (WASM_OP_IMPDEP1):
  1748. HANDLE_OP (WASM_OP_UNUSED_0x06):
  1749. HANDLE_OP (WASM_OP_UNUSED_0x07):
  1750. HANDLE_OP (WASM_OP_UNUSED_0x08):
  1751. HANDLE_OP (WASM_OP_UNUSED_0x09):
  1752. HANDLE_OP (WASM_OP_UNUSED_0x0a):
  1753. HANDLE_OP (WASM_OP_UNUSED_0x12):
  1754. HANDLE_OP (WASM_OP_UNUSED_0x13):
  1755. HANDLE_OP (WASM_OP_UNUSED_0x14):
  1756. HANDLE_OP (WASM_OP_UNUSED_0x15):
  1757. HANDLE_OP (WASM_OP_UNUSED_0x16):
  1758. HANDLE_OP (WASM_OP_UNUSED_0x17):
  1759. HANDLE_OP (WASM_OP_UNUSED_0x18):
  1760. HANDLE_OP (WASM_OP_UNUSED_0x19):
  1761. HANDLE_OP (WASM_OP_UNUSED_0x1c):
  1762. HANDLE_OP (WASM_OP_UNUSED_0x1d):
  1763. HANDLE_OP (WASM_OP_UNUSED_0x1e):
  1764. HANDLE_OP (WASM_OP_UNUSED_0x1f):
  1765. HANDLE_OP (WASM_OP_UNUSED_0x25):
  1766. HANDLE_OP (WASM_OP_UNUSED_0x26):
  1767. HANDLE_OP (WASM_OP_UNUSED_0x27):
  1768. {
  1769. wasm_runtime_set_exception(module,
  1770. "WASM interp failed: unsupported opcode.");
  1771. goto got_exception;
  1772. }
  1773. #endif
  1774. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1775. continue;
  1776. #else
  1777. FETCH_OPCODE_AND_DISPATCH ();
  1778. #endif
  1779. call_func_from_interp:
  1780. /* Only do the copy when it's called from interpreter. */
  1781. {
  1782. WASMInterpFrame *outs_area = wasm_thread_wasm_stack_top(self);
  1783. POP(cur_func->param_cell_num);
  1784. SYNC_ALL_TO_FRAME();
  1785. word_copy(outs_area->lp, frame_sp, cur_func->param_cell_num);
  1786. prev_frame = frame;
  1787. }
  1788. call_func_from_entry:
  1789. {
  1790. if (cur_func->is_import_func) {
  1791. wasm_interp_call_func_native(self, cur_func, prev_frame);
  1792. prev_frame = frame->prev_frame;
  1793. cur_func = frame->function;
  1794. UPDATE_ALL_FROM_FRAME();
  1795. memory = module->default_memory;
  1796. if (wasm_runtime_get_exception(module))
  1797. goto got_exception;
  1798. }
  1799. else {
  1800. WASMType *func_type;
  1801. uint8 ret_type;
  1802. all_cell_num = cur_func->param_cell_num + cur_func->local_cell_num
  1803. + cur_func->u.func->max_stack_cell_num
  1804. + cur_func->u.func->max_block_num * sizeof(WASMBranchBlock) / 4;
  1805. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  1806. if (!(frame = ALLOC_FRAME(self, frame_size, prev_frame))) {
  1807. frame = prev_frame;
  1808. goto got_exception;
  1809. }
  1810. /* Initialize the interpreter context. */
  1811. frame->function = cur_func;
  1812. frame_ip = wasm_runtime_get_func_code(cur_func);
  1813. frame_ip_end = wasm_runtime_get_func_code_end(cur_func);
  1814. frame_lp = frame->lp;
  1815. frame_sp = frame->sp_bottom = frame_lp + cur_func->param_cell_num
  1816. + cur_func->local_cell_num;
  1817. frame->sp_boundary = frame->sp_bottom + cur_func->u.func->max_stack_cell_num;
  1818. frame_csp = frame->csp_bottom = (WASMBranchBlock*)frame->sp_boundary;
  1819. frame->csp_boundary = frame->csp_bottom + cur_func->u.func->max_block_num;
  1820. /* Initialize the local varialbes */
  1821. memset(frame_lp + cur_func->param_cell_num, 0,
  1822. cur_func->local_cell_num * 4);
  1823. /* Push function block as first block */
  1824. func_type = cur_func->u.func->func_type;
  1825. ret_type = func_type->result_count
  1826. ? func_type->types[func_type->param_count]
  1827. : VALUE_TYPE_VOID;
  1828. PUSH_CSP(BLOCK_TYPE_FUNCTION, ret_type,
  1829. frame_ip, NULL, frame_ip_end - 1);
  1830. wasm_thread_set_cur_frame(self, (WASMRuntimeFrame*)frame);
  1831. }
  1832. HANDLE_OP_END ();
  1833. }
  1834. return_func:
  1835. {
  1836. FREE_FRAME(self, frame);
  1837. wasm_thread_set_cur_frame(self, (WASMRuntimeFrame*)prev_frame);
  1838. if (!prev_frame->ip)
  1839. /* Called from native. */
  1840. return;
  1841. RECOVER_CONTEXT(prev_frame);
  1842. HANDLE_OP_END ();
  1843. }
  1844. out_of_bounds:
  1845. wasm_runtime_set_exception(module, "out of bounds memory access");
  1846. got_exception:
  1847. if (depths && depths != depth_buf) {
  1848. wasm_free(depths);
  1849. depths = NULL;
  1850. }
  1851. return;
  1852. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1853. }
  1854. #else
  1855. FETCH_OPCODE_AND_DISPATCH ();
  1856. #endif
  1857. }
  1858. void
  1859. wasm_interp_call_wasm(WASMModuleInstance *module_inst,
  1860. WASMFunctionInstance *function,
  1861. uint32 argc, uint32 argv[])
  1862. {
  1863. WASMThread *self = &module_inst->main_tlr;
  1864. WASMRuntimeFrame *prev_frame = wasm_thread_get_cur_frame(self);
  1865. WASMInterpFrame *frame, *outs_area;
  1866. /* Allocate sufficient cells for all kinds of return values. */
  1867. unsigned all_cell_num = 2, i;
  1868. /* This frame won't be used by JITed code, so only allocate interp
  1869. frame here. */
  1870. unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
  1871. if (argc != function->param_cell_num) {
  1872. char buf[128];
  1873. snprintf(buf, sizeof(buf),
  1874. "invalid argument count %d, expected %d",
  1875. argc, function->param_cell_num);
  1876. wasm_runtime_set_exception(self->module_inst, buf);
  1877. return;
  1878. }
  1879. /* TODO: check stack overflow. */
  1880. if (!(frame = ALLOC_FRAME(self, frame_size, (WASMInterpFrame*)prev_frame)))
  1881. return;
  1882. outs_area = wasm_thread_wasm_stack_top(self);
  1883. frame->function = NULL;
  1884. frame->ip = NULL;
  1885. /* There is no local variable. */
  1886. frame->sp = frame->lp + 0;
  1887. if (argc > 0)
  1888. word_copy(outs_area->lp, argv, argc);
  1889. wasm_thread_set_cur_frame(self, frame);
  1890. if (function->is_import_func)
  1891. wasm_interp_call_func_native(self, function, frame);
  1892. else
  1893. wasm_interp_call_func_bytecode(self, function, frame);
  1894. /* Output the return value to the caller */
  1895. if (!wasm_runtime_get_exception(self->module_inst)) {
  1896. for (i = 0; i < function->ret_cell_num; i++)
  1897. argv[i] = *(frame->sp + i - function->ret_cell_num);
  1898. }
  1899. wasm_thread_set_cur_frame(self, prev_frame);
  1900. FREE_FRAME(self, frame);
  1901. }