wasm_interp_fast.c 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "wasm_interp.h"
  6. #include "bh_log.h"
  7. #include "wasm_runtime.h"
  8. #include "wasm_opcode.h"
  9. #include "wasm_loader.h"
  10. #include "../common/wasm_exec_env.h"
  11. #if WASM_ENABLE_SHARED_MEMORY != 0
  12. #include "../common/wasm_shared_memory.h"
  13. #endif
  14. typedef int32 CellType_I32;
  15. typedef int64 CellType_I64;
  16. typedef float32 CellType_F32;
  17. typedef float64 CellType_F64;
  18. #define CHECK_MEMORY_OVERFLOW(bytes) \
  19. do { \
  20. uint64 offset1 = (uint64)offset + (uint64)addr; \
  21. if (offset1 + bytes <= (uint64)linear_mem_size) \
  22. /* If offset1 is in valid range, maddr must also \
  23. be in valid range, no need to check it again. */ \
  24. maddr = memory->memory_data + offset1; \
  25. else \
  26. goto out_of_bounds; \
  27. } while (0)
  28. #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
  29. do { \
  30. uint64 offset1 = (uint32)(start); \
  31. if (offset1 + bytes <= linear_mem_size) \
  32. /* App heap space is not valid space for \
  33. bulk memory operation */ \
  34. maddr = memory->memory_data + offset1; \
  35. else \
  36. goto out_of_bounds; \
  37. } while (0)
  38. #define CHECK_ATOMIC_MEMORY_ACCESS(align) \
  39. do { \
  40. if (((uintptr_t)maddr & (align - 1)) != 0) \
  41. goto unaligned_atomic; \
  42. } while (0)
  43. static inline uint32
  44. rotl32(uint32 n, uint32 c)
  45. {
  46. const uint32 mask = (31);
  47. c = c % 32;
  48. c &= mask;
  49. return (n << c) | (n >> ((0 - c) & mask));
  50. }
  51. static inline uint32
  52. rotr32(uint32 n, uint32 c)
  53. {
  54. const uint32 mask = (31);
  55. c = c % 32;
  56. c &= mask;
  57. return (n >> c) | (n << ((0 - c) & mask));
  58. }
  59. static inline uint64
  60. rotl64(uint64 n, uint64 c)
  61. {
  62. const uint64 mask = (63);
  63. c = c % 64;
  64. c &= mask;
  65. return (n << c) | (n >> ((0 - c) & mask));
  66. }
  67. static inline uint64
  68. rotr64(uint64 n, uint64 c)
  69. {
  70. const uint64 mask = (63);
  71. c = c % 64;
  72. c &= mask;
  73. return (n >> c) | (n << ((0 - c) & mask));
  74. }
  75. static inline double
  76. wa_fmax(double a, double b)
  77. {
  78. double c = fmax(a, b);
  79. if (c == 0 && a == b)
  80. return signbit(a) ? b : a;
  81. return c;
  82. }
  83. static inline double
  84. wa_fmin(double a, double b)
  85. {
  86. double c = fmin(a, b);
  87. if (c == 0 && a == b)
  88. return signbit(a) ? a : b;
  89. return c;
  90. }
  91. static inline uint32
  92. clz32(uint32 type)
  93. {
  94. uint32 num = 0;
  95. if (type == 0)
  96. return 32;
  97. while (!(type & 0x80000000)) {
  98. num++;
  99. type <<= 1;
  100. }
  101. return num;
  102. }
  103. static inline uint32
  104. clz64(uint64 type)
  105. {
  106. uint32 num = 0;
  107. if (type == 0)
  108. return 64;
  109. while (!(type & 0x8000000000000000LL)) {
  110. num++;
  111. type <<= 1;
  112. }
  113. return num;
  114. }
  115. static inline uint32
  116. ctz32(uint32 type)
  117. {
  118. uint32 num = 0;
  119. if (type == 0)
  120. return 32;
  121. while (!(type & 1)) {
  122. num++;
  123. type >>= 1;
  124. }
  125. return num;
  126. }
  127. static inline uint32
  128. ctz64(uint64 type)
  129. {
  130. uint32 num = 0;
  131. if (type == 0)
  132. return 64;
  133. while (!(type & 1)) {
  134. num++;
  135. type >>= 1;
  136. }
  137. return num;
  138. }
  139. static inline uint32
  140. popcount32(uint32 u)
  141. {
  142. uint32 ret = 0;
  143. while (u) {
  144. u = (u & (u - 1));
  145. ret++;
  146. }
  147. return ret;
  148. }
  149. static inline uint32
  150. popcount64(uint64 u)
  151. {
  152. uint32 ret = 0;
  153. while (u) {
  154. u = (u & (u - 1));
  155. ret++;
  156. }
  157. return ret;
  158. }
  159. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  160. #define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr))
  161. #define LOAD_PTR(addr) (*(void **)(addr))
  162. #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  163. static inline uint32
  164. LOAD_U32_WITH_2U16S(void *addr)
  165. {
  166. union {
  167. uint32 val;
  168. uint16 u16[2];
  169. } u;
  170. bh_assert(((uintptr_t)addr & 1) == 0);
  171. u.u16[0] = ((uint16 *)addr)[0];
  172. u.u16[1] = ((uint16 *)addr)[1];
  173. return u.val;
  174. }
  175. #if UINTPTR_MAX == UINT32_MAX
  176. #define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr))
  177. #elif UINTPTR_MAX == UINT64_MAX
  178. static inline void *
  179. LOAD_PTR(void *addr)
  180. {
  181. uintptr_t addr1 = (uintptr_t)addr;
  182. union {
  183. void *val;
  184. uint32 u32[2];
  185. uint16 u16[4];
  186. } u;
  187. bh_assert(((uintptr_t)addr & 1) == 0);
  188. if ((addr1 & (uintptr_t)7) == 0)
  189. return *(void **)addr;
  190. if ((addr1 & (uintptr_t)3) == 0) {
  191. u.u32[0] = ((uint32 *)addr)[0];
  192. u.u32[1] = ((uint32 *)addr)[1];
  193. }
  194. else {
  195. u.u16[0] = ((uint16 *)addr)[0];
  196. u.u16[1] = ((uint16 *)addr)[1];
  197. u.u16[2] = ((uint16 *)addr)[2];
  198. u.u16[3] = ((uint16 *)addr)[3];
  199. }
  200. return u.val;
  201. }
  202. #endif /* end of UINTPTR_MAX */
  203. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  204. #define read_uint32(p) \
  205. (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32)))
  206. #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
  207. do { \
  208. uint32 param_count = cur_func_type->param_count; \
  209. local_idx = read_uint32(frame_ip); \
  210. bh_assert(local_idx < param_count + cur_func->local_count); \
  211. local_offset = cur_func->local_offsets[local_idx]; \
  212. if (local_idx < param_count) \
  213. local_type = cur_func_type->types[local_idx]; \
  214. else \
  215. local_type = cur_func->local_types[local_idx - param_count]; \
  216. } while (0)
  217. #define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2))
  218. #define SET_OPERAND_I32(off, value) \
  219. do { \
  220. *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  221. } while (0)
  222. #define SET_OPERAND_F32(off, value) \
  223. do { \
  224. *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
  225. } while (0)
  226. #define SET_OPERAND_I64(off, value) \
  227. do { \
  228. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  229. PUT_I64_TO_ADDR(addr_tmp, value); \
  230. } while (0)
  231. #define SET_OPERAND_F64(off, value) \
  232. do { \
  233. uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
  234. PUT_F64_TO_ADDR(addr_tmp, value); \
  235. } while (0)
  236. #define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value)
  237. #define GET_OPERAND_I32(type, off) \
  238. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  239. #define GET_OPERAND_F32(type, off) \
  240. *(type *)(frame_lp + *(int16 *)(frame_ip + off))
  241. #define GET_OPERAND_I64(type, off) \
  242. (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  243. #define GET_OPERAND_F64(type, off) \
  244. (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
  245. #define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off)
  246. #define PUSH_I32(value) \
  247. do { \
  248. *(int32 *)(frame_lp + GET_OFFSET()) = value; \
  249. } while (0)
  250. #define PUSH_F32(value) \
  251. do { \
  252. *(float32 *)(frame_lp + GET_OFFSET()) = value; \
  253. } while (0)
  254. #define PUSH_I64(value) \
  255. do { \
  256. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  257. PUT_I64_TO_ADDR(addr_tmp, value); \
  258. } while (0)
  259. #define PUSH_F64(value) \
  260. do { \
  261. uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
  262. PUT_F64_TO_ADDR(addr_tmp, value); \
  263. } while (0)
  264. #define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET()))
  265. #define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET()))
  266. #define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()))
  267. #define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET()))
  268. #define SYNC_ALL_TO_FRAME() \
  269. do { \
  270. frame->ip = frame_ip; \
  271. } while (0)
  272. #define UPDATE_ALL_FROM_FRAME() \
  273. do { \
  274. frame_ip = frame->ip; \
  275. } while (0)
  276. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  277. #define UPDATE_FRAME_IP_END() (void)0
  278. #else
  279. #define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
  280. #endif
  281. #define RECOVER_CONTEXT(new_frame) \
  282. do { \
  283. frame = (new_frame); \
  284. cur_func = frame->function; \
  285. cur_func_type = cur_func->func_type;\
  286. prev_frame = frame->prev_frame; \
  287. frame_ip = frame->ip; \
  288. UPDATE_FRAME_IP_END(); \
  289. frame_lp = frame->lp; \
  290. } while (0)
  291. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  292. #define GET_OPCODE() opcode = *frame_ip++;
  293. #else
  294. #define GET_OPCODE() \
  295. opcode = *frame_ip; \
  296. frame_ip += 2;
  297. #endif
  298. #define DEF_OP_EQZ(ctype, src_op_type) \
  299. do { \
  300. SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \
  301. frame_ip += 4; \
  302. } while (0)
  303. #define DEF_OP_CMP(src_type, src_op_type, cond) \
  304. do { \
  305. SET_OPERAND(I32, 4, \
  306. GET_OPERAND(src_type, src_op_type, 2) \
  307. cond GET_OPERAND(src_type, src_op_type, 0)); \
  308. frame_ip += 6; \
  309. } while (0)
  310. #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
  311. do { \
  312. SET_OPERAND( \
  313. src_op_type, 2, \
  314. (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \
  315. frame_ip += 4; \
  316. } while (0)
  317. #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
  318. do { \
  319. SET_OPERAND(src_op_type, 4, \
  320. GET_OPERAND(src_type1, src_op_type, 2) \
  321. operation GET_OPERAND(src_type2, src_op_type, 0)); \
  322. frame_ip += 6; \
  323. } while (0)
  324. #define DEF_OP_REINTERPRET(src_type, src_op_type) \
  325. do { \
  326. SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \
  327. frame_ip += 4; \
  328. } while (0)
  329. #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
  330. #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
  331. do { \
  332. SET_OPERAND(src_op_type, 4, \
  333. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  334. GET_OPERAND(src_type2, src_op_type, 0) % 32)); \
  335. frame_ip += 6; \
  336. } while (0)
  337. #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
  338. do { \
  339. SET_OPERAND(src_op_type, 4, \
  340. GET_OPERAND(src_type1, src_op_type, 2) operation( \
  341. GET_OPERAND(src_type2, src_op_type, 0) % 64)); \
  342. frame_ip += 6; \
  343. } while (0)
  344. #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
  345. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
  346. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
  347. case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
  348. { \
  349. uint32 readv, sval; \
  350. \
  351. sval = POP_I32(); \
  352. addr = POP_I32(); \
  353. \
  354. if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
  355. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  356. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  357. \
  358. os_mutex_lock(&memory->mem_lock); \
  359. readv = (uint32)(*(uint8 *)maddr); \
  360. *(uint8 *)maddr = (uint8)(readv op sval); \
  361. os_mutex_unlock(&memory->mem_lock); \
  362. } \
  363. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  364. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  365. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  366. \
  367. os_mutex_lock(&memory->mem_lock); \
  368. readv = (uint32)LOAD_U16(maddr); \
  369. STORE_U16(maddr, (uint16)(readv op sval)); \
  370. os_mutex_unlock(&memory->mem_lock); \
  371. } \
  372. else { \
  373. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  374. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  375. \
  376. os_mutex_lock(&memory->mem_lock); \
  377. readv = LOAD_I32(maddr); \
  378. STORE_U32(maddr, readv op sval); \
  379. os_mutex_unlock(&memory->mem_lock); \
  380. } \
  381. PUSH_I32(readv); \
  382. break; \
  383. } \
  384. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
  385. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
  386. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
  387. case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
  388. { \
  389. uint64 readv, sval; \
  390. \
  391. sval = (uint64)POP_I64(); \
  392. addr = POP_I32(); \
  393. \
  394. if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
  395. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
  396. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  397. \
  398. os_mutex_lock(&memory->mem_lock); \
  399. readv = (uint64)(*(uint8 *)maddr); \
  400. *(uint8 *)maddr = (uint8)(readv op sval); \
  401. os_mutex_unlock(&memory->mem_lock); \
  402. } \
  403. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  404. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
  405. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  406. \
  407. os_mutex_lock(&memory->mem_lock); \
  408. readv = (uint64)LOAD_U16(maddr); \
  409. STORE_U16(maddr, (uint16)(readv op sval)); \
  410. os_mutex_unlock(&memory->mem_lock); \
  411. } \
  412. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  413. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
  414. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  415. \
  416. os_mutex_lock(&memory->mem_lock); \
  417. readv = (uint64)LOAD_U32(maddr); \
  418. STORE_U32(maddr, (uint32)(readv op sval)); \
  419. os_mutex_unlock(&memory->mem_lock); \
  420. } \
  421. else { \
  422. uint64 op_result; \
  423. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \
  424. CHECK_ATOMIC_MEMORY_ACCESS(8); \
  425. \
  426. os_mutex_lock(&memory->mem_lock); \
  427. readv = (uint64)LOAD_I64(maddr); \
  428. op_result = readv op sval; \
  429. STORE_I64(maddr, op_result); \
  430. os_mutex_unlock(&memory->mem_lock); \
  431. } \
  432. PUSH_I64(readv); \
  433. break; \
  434. }
  435. #define DEF_OP_MATH(src_type, src_op_type, method) \
  436. do { \
  437. SET_OPERAND(src_op_type, 2, \
  438. (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \
  439. frame_ip += 4; \
  440. } while (0)
  441. #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
  442. static dst_type func_name(src_type src_value, src_type src_min, \
  443. src_type src_max, dst_type dst_min, \
  444. dst_type dst_max, bool is_sign) \
  445. { \
  446. dst_type dst_value = 0; \
  447. if (!isnan(src_value)) { \
  448. if (src_value <= src_min) \
  449. dst_value = dst_min; \
  450. else if (src_value >= src_max) \
  451. dst_value = dst_max; \
  452. else { \
  453. if (is_sign) \
  454. dst_value = (dst_type)(signed_type)src_value; \
  455. else \
  456. dst_value = (dst_type)src_value; \
  457. } \
  458. } \
  459. return dst_value; \
  460. }
  461. TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
  462. TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
  463. TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
  464. TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
  465. static bool
  466. trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  467. float32 src_min, float32 src_max, bool saturating, bool is_i32,
  468. bool is_sign)
  469. {
  470. float32 src_value = GET_OPERAND(float32, F32, 0);
  471. uint64 dst_value_i64;
  472. uint32 dst_value_i32;
  473. if (!saturating) {
  474. if (isnan(src_value)) {
  475. wasm_set_exception(module, "invalid conversion to integer");
  476. return true;
  477. }
  478. else if (src_value <= src_min || src_value >= src_max) {
  479. wasm_set_exception(module, "integer overflow");
  480. return true;
  481. }
  482. }
  483. if (is_i32) {
  484. uint32 dst_min = is_sign ? INT32_MIN : 0;
  485. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  486. dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
  487. dst_max, is_sign);
  488. SET_OPERAND(I32, 2, dst_value_i32);
  489. }
  490. else {
  491. uint64 dst_min = is_sign ? INT64_MIN : 0;
  492. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  493. dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
  494. dst_max, is_sign);
  495. SET_OPERAND(I64, 2, dst_value_i64);
  496. }
  497. return false;
  498. }
  499. static bool
  500. trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
  501. float64 src_min, float64 src_max, bool saturating, bool is_i32,
  502. bool is_sign)
  503. {
  504. float64 src_value = GET_OPERAND(float64, F64, 0);
  505. uint64 dst_value_i64;
  506. uint32 dst_value_i32;
  507. if (!saturating) {
  508. if (isnan(src_value)) {
  509. wasm_set_exception(module, "invalid conversion to integer");
  510. return true;
  511. }
  512. else if (src_value <= src_min || src_value >= src_max) {
  513. wasm_set_exception(module, "integer overflow");
  514. return true;
  515. }
  516. }
  517. if (is_i32) {
  518. uint32 dst_min = is_sign ? INT32_MIN : 0;
  519. uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
  520. dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
  521. dst_max, is_sign);
  522. SET_OPERAND(I32, 2, dst_value_i32);
  523. }
  524. else {
  525. uint64 dst_min = is_sign ? INT64_MIN : 0;
  526. uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
  527. dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
  528. dst_max, is_sign);
  529. SET_OPERAND(I64, 2, dst_value_i64);
  530. }
  531. return false;
  532. }
  533. #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
  534. do { \
  535. if (trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \
  536. is_i32, is_sign)) \
  537. goto got_exception; \
  538. frame_ip += 4; \
  539. } while (0)
  540. #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
  541. do { \
  542. if (trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \
  543. is_i32, is_sign)) \
  544. goto got_exception; \
  545. frame_ip += 4; \
  546. } while (0)
  547. #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
  548. do { \
  549. (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \
  550. is_i32, is_sign); \
  551. frame_ip += 4; \
  552. } while (0)
  553. #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
  554. do { \
  555. (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \
  556. is_i32, is_sign); \
  557. frame_ip += 4; \
  558. } while (0)
  559. #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
  560. do { \
  561. dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
  562. PUSH_##dst_op_type(value); \
  563. } while (0)
  564. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  565. #define CELL_SIZE sizeof(uint8)
  566. #else
  567. #define CELL_SIZE (sizeof(uint8) * 2)
  568. #endif
  569. static bool
  570. copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity,
  571. uint32 total_cell_num, const uint8 *cells,
  572. const int16 *src_offsets, const uint16 *dst_offsets)
  573. {
  574. /* To avoid the overlap issue between src offsets and dst offset,
  575. * we use 2 steps to do the copy. First step, copy the src values
  576. * to a tmp buf. Second step, copy the values from tmp buf to dst.
  577. */
  578. uint32 buf[16] = { 0 }, i;
  579. uint32 *tmp_buf = buf;
  580. uint8 cell;
  581. int16 src, buf_index = 0;
  582. uint16 dst;
  583. /* Allocate memory if the buf is not large enough */
  584. if (total_cell_num > sizeof(buf) / sizeof(uint32)) {
  585. uint64 total_size = sizeof(uint32) * (uint64)total_cell_num;
  586. if (total_size >= UINT32_MAX
  587. || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) {
  588. wasm_set_exception(module, "allocate memory failed");
  589. return false;
  590. }
  591. }
  592. /* 1) Copy values from src to tmp buf */
  593. for (i = 0; i < arity; i++) {
  594. cell = cells[i * CELL_SIZE];
  595. src = src_offsets[i];
  596. if (cell == 1)
  597. tmp_buf[buf_index] = frame_lp[src];
  598. else {
  599. tmp_buf[buf_index] = frame_lp[src];
  600. tmp_buf[buf_index + 1] = frame_lp[src + 1];
  601. }
  602. buf_index += cell;
  603. }
  604. /* 2) Copy values from tmp buf to dest */
  605. buf_index = 0;
  606. for (i = 0; i < arity; i++) {
  607. cell = cells[i * CELL_SIZE];
  608. dst = dst_offsets[i];
  609. if (cell == 1)
  610. frame_lp[dst] = tmp_buf[buf_index];
  611. else {
  612. frame_lp[dst] = tmp_buf[buf_index];
  613. frame_lp[dst + 1] = tmp_buf[buf_index + 1];
  614. }
  615. buf_index += cell;
  616. }
  617. if (tmp_buf != buf) {
  618. wasm_runtime_free(tmp_buf);
  619. }
  620. return true;
  621. }
  622. #define RECOVER_BR_INFO() \
  623. do { \
  624. uint32 arity; \
  625. /* read arity */ \
  626. arity = read_uint32(frame_ip); \
  627. if (arity) { \
  628. uint32 total_cell; \
  629. uint16 *dst_offsets = NULL; \
  630. uint8 *cells; \
  631. int16 *src_offsets = NULL; \
  632. /* read total cell num */ \
  633. total_cell = read_uint32(frame_ip); \
  634. /* cells */ \
  635. cells = (uint8 *)frame_ip; \
  636. frame_ip += arity * CELL_SIZE; \
  637. /* src offsets */ \
  638. src_offsets = (int16 *)frame_ip; \
  639. frame_ip += arity * sizeof(int16); \
  640. /* dst offsets */ \
  641. dst_offsets = (uint16 *)frame_ip; \
  642. frame_ip += arity * sizeof(uint16); \
  643. if (arity == 1) { \
  644. if (cells[0] == 1) \
  645. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  646. else if (cells[0] == 2) { \
  647. frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
  648. frame_lp[dst_offsets[0] + 1] = \
  649. frame_lp[src_offsets[0] + 1]; \
  650. } \
  651. } \
  652. else { \
  653. if (!copy_stack_values(module, frame_lp, arity, total_cell, \
  654. cells, src_offsets, dst_offsets)) \
  655. goto got_exception; \
  656. } \
  657. } \
  658. frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
  659. } while (0)
  660. #define SKIP_BR_INFO() \
  661. do { \
  662. uint32 arity; \
  663. /* read and skip arity */ \
  664. arity = read_uint32(frame_ip); \
  665. if (arity) { \
  666. /* skip total cell num */ \
  667. frame_ip += sizeof(uint32); \
  668. /* skip cells, src offsets and dst offsets */ \
  669. frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \
  670. } \
  671. /* skip target address */ \
  672. frame_ip += sizeof(uint8 *); \
  673. } while (0)
  674. static inline int32
  675. sign_ext_8_32(int8 val)
  676. {
  677. if (val & 0x80)
  678. return (int32)val | (int32)0xffffff00;
  679. return val;
  680. }
  681. static inline int32
  682. sign_ext_16_32(int16 val)
  683. {
  684. if (val & 0x8000)
  685. return (int32)val | (int32)0xffff0000;
  686. return val;
  687. }
  688. static inline int64
  689. sign_ext_8_64(int8 val)
  690. {
  691. if (val & 0x80)
  692. return (int64)val | (int64)0xffffffffffffff00;
  693. return val;
  694. }
  695. static inline int64
  696. sign_ext_16_64(int16 val)
  697. {
  698. if (val & 0x8000)
  699. return (int64)val | (int64)0xffffffffffff0000;
  700. return val;
  701. }
  702. static inline int64
  703. sign_ext_32_64(int32 val)
  704. {
  705. if (val & (int32)0x80000000)
  706. return (int64)val | (int64)0xffffffff00000000;
  707. return val;
  708. }
  709. static inline void
  710. word_copy(uint32 *dest, uint32 *src, unsigned num)
  711. {
  712. for (; num > 0; num--)
  713. *dest++ = *src++;
  714. }
  715. static inline WASMInterpFrame *
  716. ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
  717. {
  718. WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
  719. if (frame) {
  720. frame->prev_frame = prev_frame;
  721. #if WASM_ENABLE_PERF_PROFILING != 0
  722. frame->time_started = os_time_get_boot_microsecond();
  723. #endif
  724. }
  725. else {
  726. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  727. "wasm operand stack overflow");
  728. }
  729. return frame;
  730. }
  731. static inline void
  732. FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
  733. {
  734. #if WASM_ENABLE_PERF_PROFILING != 0
  735. if (frame->function) {
  736. frame->function->total_exec_time +=
  737. os_time_get_boot_microsecond() - frame->time_started;
  738. frame->function->total_exec_cnt++;
  739. }
  740. #endif
  741. wasm_exec_env_free_wasm_frame(exec_env, frame);
  742. }
  743. static void
  744. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  745. WASMExecEnv *exec_env,
  746. WASMFunctionInstance *cur_func,
  747. WASMInterpFrame *prev_frame)
  748. {
  749. WASMFunctionImport *func_import = cur_func->u.func_import;
  750. WASMType * func_type = cur_func->func_type;
  751. unsigned local_cell_num = 2;
  752. WASMInterpFrame *frame;
  753. uint32 argv_ret[2];
  754. bool ret;
  755. if (!(frame = ALLOC_FRAME(exec_env,
  756. wasm_interp_interp_frame_size(local_cell_num),
  757. prev_frame)))
  758. return;
  759. frame->function = cur_func;
  760. frame->ip = NULL;
  761. frame->lp = frame->operand;
  762. wasm_exec_env_set_cur_frame(exec_env, frame);
  763. if (!func_import->func_ptr_linked) {
  764. char buf[128];
  765. snprintf(buf, sizeof(buf),
  766. "failed to call unlinked import function (%s, %s)",
  767. func_import->module_name->str, func_import->field_name->str);
  768. wasm_set_exception((WASMModuleInstance*)module_inst, buf);
  769. return;
  770. }
  771. if (func_import->call_conv_wasm_c_api) {
  772. ret = wasm_runtime_invoke_c_api_native(
  773. (WASMModuleInstanceCommon *)module_inst,
  774. func_import->func_ptr_linked, func_type,
  775. func_type->param_cell_num, frame->lp,
  776. func_import->wasm_c_api_with_env, func_import->attachment);
  777. if (ret) {
  778. argv_ret[0] = frame->lp[0];
  779. argv_ret[1] = frame->lp[1];
  780. }
  781. }
  782. else if (!func_import->call_conv_raw) {
  783. ret = wasm_runtime_invoke_native(
  784. exec_env, func_import->func_ptr_linked, func_type,
  785. func_import->signature, func_import->attachment, frame->lp,
  786. func_type->param_cell_num, argv_ret);
  787. }
  788. else {
  789. ret = wasm_runtime_invoke_native_raw(
  790. exec_env, func_import->func_ptr_linked, func_type,
  791. func_import->signature, func_import->attachment, frame->lp,
  792. func_type->param_cell_num, argv_ret);
  793. }
  794. if (!ret)
  795. return;
  796. if (func_type->ret_cell_num == 1) {
  797. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  798. }
  799. else if (func_type->ret_cell_num == 2) {
  800. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  801. prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
  802. }
  803. FREE_FRAME(exec_env, frame);
  804. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  805. }
  806. static void
  807. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  808. WASMExecEnv *exec_env,
  809. WASMFunctionInstance *cur_func,
  810. WASMInterpFrame *prev_frame);
  811. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  812. static void
  813. wasm_interp_call_inter_module_func(WASMModuleInstance *module_inst,
  814. WASMModuleInstance *callee_module_inst,
  815. WASMExecEnv *exec_env,
  816. WASMFunctionInstance *cur_func,
  817. WASMInterpFrame *prev_frame)
  818. {
  819. uint8 *ip = prev_frame->ip;
  820. /* set ip NULL to make call_func_bytecode return after executing
  821. this function */
  822. prev_frame->ip = NULL;
  823. /* replace exec_env's module_inst with sub_module_inst so we can
  824. call it */
  825. exec_env->module_inst = (WASMModuleInstanceCommon *)callee_module_inst;
  826. /* call function of sub-module*/
  827. wasm_interp_call_func_bytecode(callee_module_inst, exec_env,
  828. cur_func, prev_frame);
  829. /* restore ip and module_inst */
  830. prev_frame->ip = ip;
  831. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  832. /* transfer exception if it is thrown */
  833. if (wasm_get_exception(callee_module_inst)) {
  834. bh_memcpy_s(module_inst->cur_exception,
  835. sizeof(module_inst->cur_exception),
  836. callee_module_inst->cur_exception,
  837. sizeof(callee_module_inst->cur_exception));
  838. }
  839. }
  840. #endif
  841. #if WASM_ENABLE_MULTI_MODULE != 0
  842. static void
  843. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  844. WASMExecEnv *exec_env,
  845. WASMFunctionInstance *cur_func,
  846. WASMInterpFrame *prev_frame)
  847. {
  848. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  849. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  850. WASMFunctionImport *func_import = cur_func->u.func_import;
  851. uint8 *ip = prev_frame->ip;
  852. char buf[128];
  853. if (!sub_func_inst) {
  854. snprintf(buf, sizeof(buf),
  855. "failed to call unlinked import function (%s, %s)",
  856. func_import->module_name->str, func_import->field_name->str);
  857. wasm_set_exception(module_inst, buf);
  858. return;
  859. }
  860. /* set ip NULL to make call_func_bytecode return after executing
  861. this function */
  862. prev_frame->ip = NULL;
  863. /* replace exec_env's module_inst with sub_module_inst so we can
  864. call it */
  865. exec_env->module_inst = (WASMModuleInstanceCommon *)sub_module_inst;
  866. /* call function of sub-module*/
  867. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  868. prev_frame);
  869. /* restore ip and module_inst */
  870. prev_frame->ip = ip;
  871. exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
  872. /* transfer exception if it is thrown */
  873. if (wasm_get_exception(sub_module_inst)) {
  874. bh_memcpy_s(module_inst->cur_exception,
  875. sizeof(module_inst->cur_exception),
  876. sub_module_inst->cur_exception,
  877. sizeof(sub_module_inst->cur_exception));
  878. }
  879. }
  880. #endif
  881. #if WASM_ENABLE_THREAD_MGR != 0
  882. #define CHECK_SUSPEND_FLAGS() \
  883. do { \
  884. if (exec_env->suspend_flags.flags != 0) { \
  885. if (exec_env->suspend_flags.flags & 0x01) { \
  886. /* terminate current thread */ \
  887. return; \
  888. } \
  889. /* TODO: support suspend and breakpoint */ \
  890. } \
  891. } while (0)
  892. #endif
  893. #if WASM_ENABLE_OPCODE_COUNTER != 0
  894. typedef struct OpcodeInfo {
  895. char *name;
  896. uint64 count;
  897. } OpcodeInfo;
  898. /* clang-format off */
  899. #define HANDLE_OPCODE(op) \
  900. { \
  901. #op, 0 \
  902. }
  903. DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
  904. #undef HANDLE_OPCODE
  905. /* clang-format on */
  906. static void
  907. wasm_interp_dump_op_count()
  908. {
  909. uint32 i;
  910. uint64 total_count = 0;
  911. for (i = 0; i < WASM_OP_IMPDEP; i++)
  912. total_count += opcode_table[i].count;
  913. printf("total opcode count: %ld\n", total_count);
  914. for (i = 0; i < WASM_OP_IMPDEP; i++)
  915. if (opcode_table[i].count > 0)
  916. printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
  917. opcode_table[i].count,
  918. opcode_table[i].count * 100.0f / total_count);
  919. }
  920. #endif
  921. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  922. /* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
  923. #if WASM_ENABLE_OPCODE_COUNTER != 0
  924. #define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
  925. #else
  926. #define HANDLE_OP(opcode) HANDLE_##opcode:
  927. #endif
  928. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  929. #define FETCH_OPCODE_AND_DISPATCH() \
  930. do { \
  931. const void *p_label_addr = *(void **)frame_ip; \
  932. frame_ip += sizeof(void *); \
  933. goto *p_label_addr; \
  934. } while (0)
  935. #else
  936. #define FETCH_OPCODE_AND_DISPATCH() \
  937. do { \
  938. const void *p_label_addr = label_base + *(int16 *)frame_ip; \
  939. frame_ip += sizeof(int16); \
  940. goto *p_label_addr; \
  941. } while (0)
  942. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  943. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  944. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  945. #define HANDLE_OP(opcode) case opcode:
  946. #define HANDLE_OP_END() continue
  947. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  948. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  949. static void **global_handle_table;
  950. #endif
  951. static inline uint8 *
  952. get_global_addr(WASMGlobalInstance *global)
  953. {
  954. #if WASM_ENABLE_MULTI_MODULE == 0
  955. //return global_data + global->data_offset;
  956. return global->data;
  957. #else
  958. //return global->import_global_inst
  959. // ? global->import_module_inst->global_data
  960. // + global->import_global_inst->data_offset
  961. // : global_data + global->data_offset;
  962. return global->import_global_inst
  963. ? global->import_global_inst->data
  964. : global->data;
  965. #endif
  966. }
  967. static void
  968. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  969. WASMExecEnv *exec_env,
  970. WASMFunctionInstance *cur_func,
  971. WASMInterpFrame *prev_frame)
  972. {
  973. WASMMemoryInstance *memory = module->default_memory;
  974. uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0;
  975. //#if WASM_ENABLE_MULTI_MODULE == 1
  976. // uint8 *global_data = module->global_data;
  977. //#endif
  978. uint32 linear_mem_size = memory ? num_bytes_per_page * memory->cur_page_count : 0;
  979. WASMGlobalInstance *globals = module->globals, *global;
  980. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  981. WASMInterpFrame *frame = NULL;
  982. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  983. WASMProgramInstance * program = module->program;
  984. #endif
  985. WASMModuleInstance * callee_module_inst = NULL;
  986. WASMType * cur_func_type = NULL;
  987. /* Points to this special opcode so as to jump to the call_method_from_entry. */
  988. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  989. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  990. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  991. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  992. /* cache of label base addr */
  993. register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
  994. #endif
  995. #endif
  996. uint8 *frame_ip_end = frame_ip + 1;
  997. uint32 cond, count, fidx, tidx, frame_size = 0;
  998. uint64 all_cell_num = 0;
  999. int16 addr1, addr2, addr_ret = 0;
  1000. int32 didx, val;
  1001. uint8 *maddr = NULL;
  1002. uint32 local_idx, local_offset, global_idx;
  1003. uint8 opcode, local_type, *global_addr;
  1004. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1005. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1006. DEFINE_GOTO_TABLE(const void *, handle_table);
  1007. #undef HANDLE_OPCODE
  1008. if (exec_env == NULL) {
  1009. global_handle_table = (void **)handle_table;
  1010. return;
  1011. }
  1012. #endif
  1013. cur_func_type = cur_func->func_type;
  1014. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1015. while (frame_ip < frame_ip_end) {
  1016. opcode = *frame_ip++;
  1017. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1018. frame_ip++;
  1019. #endif
  1020. switch (opcode) {
  1021. #else
  1022. goto *handle_table[WASM_OP_IMPDEP];
  1023. #endif
  1024. /* control instructions */
  1025. HANDLE_OP(WASM_OP_UNREACHABLE)
  1026. {
  1027. wasm_set_exception(module, "unreachable");
  1028. goto got_exception;
  1029. }
  1030. HANDLE_OP(WASM_OP_IF)
  1031. {
  1032. cond = (uint32)POP_I32();
  1033. if (cond == 0) {
  1034. uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
  1035. if (else_addr == NULL) {
  1036. frame_ip =
  1037. (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
  1038. }
  1039. else {
  1040. frame_ip = else_addr;
  1041. }
  1042. }
  1043. else {
  1044. frame_ip += sizeof(uint8 *) * 2;
  1045. }
  1046. HANDLE_OP_END();
  1047. }
  1048. HANDLE_OP(WASM_OP_ELSE)
  1049. {
  1050. frame_ip = (uint8 *)LOAD_PTR(frame_ip);
  1051. HANDLE_OP_END();
  1052. }
  1053. HANDLE_OP(WASM_OP_BR)
  1054. {
  1055. #if WASM_ENABLE_THREAD_MGR != 0
  1056. CHECK_SUSPEND_FLAGS();
  1057. #endif
  1058. recover_br_info:
  1059. RECOVER_BR_INFO();
  1060. HANDLE_OP_END();
  1061. }
  1062. HANDLE_OP(WASM_OP_BR_IF)
  1063. {
  1064. #if WASM_ENABLE_THREAD_MGR != 0
  1065. CHECK_SUSPEND_FLAGS();
  1066. #endif
  1067. cond = frame_lp[GET_OFFSET()];
  1068. if (cond)
  1069. goto recover_br_info;
  1070. else
  1071. SKIP_BR_INFO();
  1072. HANDLE_OP_END();
  1073. }
  1074. HANDLE_OP(WASM_OP_BR_TABLE)
  1075. {
  1076. uint32 arity, br_item_size;
  1077. #if WASM_ENABLE_THREAD_MGR != 0
  1078. CHECK_SUSPEND_FLAGS();
  1079. #endif
  1080. count = read_uint32(frame_ip);
  1081. didx = GET_OPERAND(uint32, I32, 0);
  1082. frame_ip += 2;
  1083. if (!(didx >= 0 && (uint32)didx < count))
  1084. didx = count;
  1085. /* all br items must have the same arity and item size,
  1086. so we only calculate the first item size */
  1087. arity = LOAD_U32_WITH_2U16S(frame_ip);
  1088. br_item_size = sizeof(uint32); /* arity */
  1089. if (arity) {
  1090. /* total cell num */
  1091. br_item_size += sizeof(uint32);
  1092. /* cells, src offsets and dst offsets */
  1093. br_item_size +=
  1094. (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
  1095. }
  1096. /* target address */
  1097. br_item_size += sizeof(uint8 *);
  1098. frame_ip += br_item_size * didx;
  1099. goto recover_br_info;
  1100. }
  1101. HANDLE_OP(WASM_OP_RETURN)
  1102. {
  1103. uint32 ret_idx;
  1104. WASMType *func_type;
  1105. uint32 off, ret_offset;
  1106. uint8 *ret_types;
  1107. if (cur_func->is_import_func)
  1108. func_type = cur_func->u.func_import->func_type;
  1109. else
  1110. func_type = cur_func->u.func->func_type;
  1111. /* types of each return value */
  1112. ret_types = func_type->types + func_type->param_count;
  1113. ret_offset = prev_frame->ret_offset;
  1114. for (ret_idx = 0,
  1115. off = sizeof(int16) * (func_type->result_count - 1);
  1116. ret_idx < func_type->result_count;
  1117. ret_idx++, off -= sizeof(int16)) {
  1118. if (ret_types[ret_idx] == VALUE_TYPE_I64
  1119. || ret_types[ret_idx] == VALUE_TYPE_F64) {
  1120. PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
  1121. GET_OPERAND(uint64, I64, off));
  1122. ret_offset += 2;
  1123. }
  1124. else {
  1125. prev_frame->lp[ret_offset] =
  1126. GET_OPERAND(uint32, I32, off);
  1127. ret_offset++;
  1128. }
  1129. }
  1130. goto return_func;
  1131. }
  1132. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  1133. #if WASM_ENABLE_TAIL_CALL != 0
  1134. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  1135. #endif
  1136. {
  1137. WASMType *cur_type;
  1138. uint32 tbl_idx;
  1139. #if WASM_ENABLE_TAIL_CALL != 0
  1140. GET_OPCODE();
  1141. #endif
  1142. #if WASM_ENABLE_THREAD_MGR != 0
  1143. CHECK_SUSPEND_FLAGS();
  1144. #endif
  1145. tidx = read_uint32(frame_ip);
  1146. cur_type = module->module->types[tidx];
  1147. tbl_idx = read_uint32(frame_ip);
  1148. bh_assert(tbl_idx < module->table_count);
  1149. val = GET_OPERAND(uint32, I32, 0);
  1150. frame_ip += 2;
  1151. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  1152. cur_func = wasm_program_lookup_cached_resolving_func(program, val);
  1153. if (!cur_func) {
  1154. if (!wasm_program_resolve_op_call_indirect(program, module,
  1155. tbl_idx, val,
  1156. cur_type,
  1157. &callee_module_inst,
  1158. &cur_func)) {
  1159. goto got_exception;
  1160. }
  1161. bh_assert(callee_module_inst == cur_func->module_inst);
  1162. } else {
  1163. callee_module_inst = cur_func->module_inst;
  1164. }
  1165. cur_func_type = cur_func->func_type;
  1166. #else
  1167. WASMTableInstance *tbl_inst;
  1168. callee_module_inst = module;
  1169. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1170. if (val < 0 ||
  1171. (val >= (int32)tbl_inst->cur_size
  1172. )) {
  1173. wasm_set_exception(module, "undefined element");
  1174. goto got_exception;
  1175. }
  1176. fidx = ((uint32*)tbl_inst->base_addr)[val];
  1177. if (fidx == (uint32)-1) {
  1178. wasm_set_exception(module, "uninitialized element");
  1179. goto got_exception;
  1180. }
  1181. /*
  1182. * we might be using a table injected by host or
  1183. * another module. in that case, we don't validate
  1184. * the elem value while loading
  1185. */
  1186. if (fidx >= callee_module_inst->function_count) {
  1187. wasm_set_exception(module, "unknown function");
  1188. goto got_exception;
  1189. }
  1190. /* always call module own functions */
  1191. cur_func = callee_module_inst->functions + fidx;
  1192. cur_func_type = cur_func->func_type;
  1193. if (!wasm_type_equal(cur_type, cur_func_type)) {
  1194. wasm_set_exception(module, "indirect call type mismatch");
  1195. goto got_exception;
  1196. }
  1197. #endif
  1198. #if WASM_ENABLE_TAIL_CALL != 0
  1199. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  1200. goto call_func_from_return_call;
  1201. #endif
  1202. goto call_func_from_interp;
  1203. }
  1204. /* parametric instructions */
  1205. HANDLE_OP(WASM_OP_SELECT)
  1206. {
  1207. cond = frame_lp[GET_OFFSET()];
  1208. addr1 = GET_OFFSET();
  1209. addr2 = GET_OFFSET();
  1210. addr_ret = GET_OFFSET();
  1211. if (!cond) {
  1212. if (addr_ret != addr1)
  1213. frame_lp[addr_ret] = frame_lp[addr1];
  1214. }
  1215. else {
  1216. if (addr_ret != addr2)
  1217. frame_lp[addr_ret] = frame_lp[addr2];
  1218. }
  1219. HANDLE_OP_END();
  1220. }
  1221. HANDLE_OP(WASM_OP_SELECT_64)
  1222. {
  1223. cond = frame_lp[GET_OFFSET()];
  1224. addr1 = GET_OFFSET();
  1225. addr2 = GET_OFFSET();
  1226. addr_ret = GET_OFFSET();
  1227. if (!cond) {
  1228. if (addr_ret != addr1)
  1229. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1230. GET_I64_FROM_ADDR(frame_lp + addr1));
  1231. }
  1232. else {
  1233. if (addr_ret != addr2)
  1234. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1235. GET_I64_FROM_ADDR(frame_lp + addr2));
  1236. }
  1237. HANDLE_OP_END();
  1238. }
  1239. #if WASM_ENABLE_REF_TYPES != 0
  1240. HANDLE_OP(WASM_OP_TABLE_GET)
  1241. {
  1242. uint32 tbl_idx, elem_idx;
  1243. WASMTableInstance *tbl_inst;
  1244. tbl_idx = read_uint32(frame_ip);
  1245. bh_assert(tbl_idx < module->table_count);
  1246. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1247. elem_idx = POP_I32();
  1248. if (elem_idx >= tbl_inst->cur_size) {
  1249. wasm_set_exception(module, "out of bounds table access");
  1250. goto got_exception;
  1251. }
  1252. PUSH_I32(((uint32 *)tbl_inst->base_addr)[elem_idx]);
  1253. HANDLE_OP_END();
  1254. }
  1255. HANDLE_OP(WASM_OP_TABLE_SET)
  1256. {
  1257. uint32 tbl_idx, elem_idx, val;
  1258. WASMTableInstance *tbl_inst;
  1259. tbl_idx = read_uint32(frame_ip);
  1260. bh_assert(tbl_idx < module->table_count);
  1261. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1262. val = POP_I32();
  1263. elem_idx = POP_I32();
  1264. if (elem_idx >= tbl_inst->cur_size) {
  1265. wasm_set_exception(module, "out of bounds table access");
  1266. goto got_exception;
  1267. }
  1268. ((uint32 *)tbl_inst->base_addr)[elem_idx] = val;
  1269. HANDLE_OP_END();
  1270. }
  1271. HANDLE_OP(WASM_OP_REF_NULL)
  1272. {
  1273. PUSH_I32(NULL_REF);
  1274. HANDLE_OP_END();
  1275. }
  1276. HANDLE_OP(WASM_OP_REF_IS_NULL)
  1277. {
  1278. uint32 val;
  1279. val = POP_I32();
  1280. PUSH_I32(val == NULL_REF ? 1 : 0);
  1281. HANDLE_OP_END();
  1282. }
  1283. HANDLE_OP(WASM_OP_REF_FUNC)
  1284. {
  1285. uint32 func_idx = read_uint32(frame_ip);
  1286. PUSH_I32(func_idx);
  1287. HANDLE_OP_END();
  1288. }
  1289. #endif /* WASM_ENABLE_REF_TYPES */
  1290. /* variable instructions */
  1291. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  1292. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  1293. {
  1294. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1295. local_offset = *frame_ip++;
  1296. #else
  1297. /* clang-format off */
  1298. local_offset = *frame_ip;
  1299. frame_ip += 2;
  1300. /* clang-format on */
  1301. #endif
  1302. *(uint32 *)(frame_lp + local_offset) =
  1303. GET_OPERAND(uint32, I32, 0);
  1304. frame_ip += 2;
  1305. HANDLE_OP_END();
  1306. }
  1307. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  1308. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  1309. {
  1310. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1311. local_offset = *frame_ip++;
  1312. #else
  1313. /* clang-format off */
  1314. local_offset = *frame_ip;
  1315. frame_ip += 2;
  1316. /* clang-format on */
  1317. #endif
  1318. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  1319. GET_OPERAND(uint64, I64, 0));
  1320. frame_ip += 2;
  1321. HANDLE_OP_END();
  1322. }
  1323. HANDLE_OP(WASM_OP_GET_GLOBAL)
  1324. {
  1325. global_idx = read_uint32(frame_ip);
  1326. bh_assert(global_idx < module->global_count);
  1327. global = globals + global_idx;
  1328. global_addr = get_global_addr(global);
  1329. addr_ret = GET_OFFSET();
  1330. frame_lp[addr_ret] = *(uint32 *)global_addr;
  1331. HANDLE_OP_END();
  1332. }
  1333. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  1334. {
  1335. global_idx = read_uint32(frame_ip);
  1336. bh_assert(global_idx < module->global_count);
  1337. global = globals + global_idx;
  1338. global_addr = get_global_addr(global);
  1339. addr_ret = GET_OFFSET();
  1340. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1341. GET_I64_FROM_ADDR((uint32 *)global_addr));
  1342. HANDLE_OP_END();
  1343. }
  1344. HANDLE_OP(WASM_OP_SET_GLOBAL)
  1345. {
  1346. global_idx = read_uint32(frame_ip);
  1347. bh_assert(global_idx < module->global_count);
  1348. global = globals + global_idx;
  1349. global_addr = get_global_addr(global);
  1350. addr1 = GET_OFFSET();
  1351. *(int32 *)global_addr = frame_lp[addr1];
  1352. HANDLE_OP_END();
  1353. }
  1354. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  1355. {
  1356. uint32 aux_stack_top;
  1357. global_idx = read_uint32(frame_ip);
  1358. bh_assert(global_idx < module->global_count);
  1359. global = globals + global_idx;
  1360. global_addr = get_global_addr(global);
  1361. aux_stack_top = frame_lp[GET_OFFSET()];
  1362. if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
  1363. wasm_set_exception(module, "wasm auxiliary stack overflow");
  1364. goto got_exception;
  1365. }
  1366. if (aux_stack_top > exec_env->aux_stack_bottom.bottom) {
  1367. wasm_set_exception(module,
  1368. "wasm auxiliary stack underflow");
  1369. goto got_exception;
  1370. }
  1371. *(int32 *)global_addr = aux_stack_top;
  1372. #if WASM_ENABLE_MEMORY_PROFILING != 0
  1373. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  1374. uint32 aux_stack_used = module->module->aux_stack_bottom
  1375. - *(uint32 *)global_addr;
  1376. if (aux_stack_used > module->max_aux_stack_used)
  1377. module->max_aux_stack_used = aux_stack_used;
  1378. }
  1379. #endif
  1380. HANDLE_OP_END();
  1381. }
  1382. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  1383. {
  1384. global_idx = read_uint32(frame_ip);
  1385. bh_assert(global_idx < module->global_count);
  1386. global = globals + global_idx;
  1387. global_addr = get_global_addr(global);
  1388. addr1 = GET_OFFSET();
  1389. PUT_I64_TO_ADDR((uint32 *)global_addr,
  1390. GET_I64_FROM_ADDR(frame_lp + addr1));
  1391. HANDLE_OP_END();
  1392. }
  1393. /* memory load instructions */
  1394. HANDLE_OP(WASM_OP_I32_LOAD)
  1395. {
  1396. uint32 offset, addr;
  1397. offset = read_uint32(frame_ip);
  1398. addr = GET_OPERAND(uint32, I32, 0);
  1399. frame_ip += 2;
  1400. addr_ret = GET_OFFSET();
  1401. CHECK_MEMORY_OVERFLOW(4);
  1402. frame_lp[addr_ret] = LOAD_I32(maddr);
  1403. HANDLE_OP_END();
  1404. }
  1405. HANDLE_OP(WASM_OP_I64_LOAD)
  1406. {
  1407. uint32 offset, addr;
  1408. offset = read_uint32(frame_ip);
  1409. addr = GET_OPERAND(uint32, I32, 0);
  1410. frame_ip += 2;
  1411. addr_ret = GET_OFFSET();
  1412. CHECK_MEMORY_OVERFLOW(8);
  1413. PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
  1414. HANDLE_OP_END();
  1415. }
  1416. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  1417. {
  1418. uint32 offset, addr;
  1419. offset = read_uint32(frame_ip);
  1420. addr = GET_OPERAND(uint32, I32, 0);
  1421. frame_ip += 2;
  1422. addr_ret = GET_OFFSET();
  1423. CHECK_MEMORY_OVERFLOW(1);
  1424. frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
  1425. HANDLE_OP_END();
  1426. }
  1427. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  1428. {
  1429. uint32 offset, addr;
  1430. offset = read_uint32(frame_ip);
  1431. addr = GET_OPERAND(uint32, I32, 0);
  1432. frame_ip += 2;
  1433. addr_ret = GET_OFFSET();
  1434. CHECK_MEMORY_OVERFLOW(1);
  1435. frame_lp[addr_ret] = (uint32)(*(uint8 *)maddr);
  1436. HANDLE_OP_END();
  1437. }
  1438. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  1439. {
  1440. uint32 offset, addr;
  1441. offset = read_uint32(frame_ip);
  1442. addr = GET_OPERAND(uint32, I32, 0);
  1443. frame_ip += 2;
  1444. addr_ret = GET_OFFSET();
  1445. CHECK_MEMORY_OVERFLOW(2);
  1446. frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
  1447. HANDLE_OP_END();
  1448. }
  1449. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  1450. {
  1451. uint32 offset, addr;
  1452. offset = read_uint32(frame_ip);
  1453. addr = GET_OPERAND(uint32, I32, 0);
  1454. frame_ip += 2;
  1455. addr_ret = GET_OFFSET();
  1456. CHECK_MEMORY_OVERFLOW(2);
  1457. frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
  1458. HANDLE_OP_END();
  1459. }
  1460. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  1461. {
  1462. uint32 offset, addr;
  1463. offset = read_uint32(frame_ip);
  1464. addr = GET_OPERAND(uint32, I32, 0);
  1465. frame_ip += 2;
  1466. addr_ret = GET_OFFSET();
  1467. CHECK_MEMORY_OVERFLOW(1);
  1468. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1469. sign_ext_8_64(*(int8 *)maddr));
  1470. HANDLE_OP_END();
  1471. }
  1472. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  1473. {
  1474. uint32 offset, addr;
  1475. offset = read_uint32(frame_ip);
  1476. addr = GET_OPERAND(uint32, I32, 0);
  1477. frame_ip += 2;
  1478. addr_ret = GET_OFFSET();
  1479. CHECK_MEMORY_OVERFLOW(1);
  1480. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
  1481. HANDLE_OP_END();
  1482. }
  1483. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  1484. {
  1485. uint32 offset, addr;
  1486. offset = read_uint32(frame_ip);
  1487. addr = GET_OPERAND(uint32, I32, 0);
  1488. frame_ip += 2;
  1489. addr_ret = GET_OFFSET();
  1490. CHECK_MEMORY_OVERFLOW(2);
  1491. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1492. sign_ext_16_64(LOAD_I16(maddr)));
  1493. HANDLE_OP_END();
  1494. }
  1495. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  1496. {
  1497. uint32 offset, addr;
  1498. offset = read_uint32(frame_ip);
  1499. addr = GET_OPERAND(uint32, I32, 0);
  1500. frame_ip += 2;
  1501. addr_ret = GET_OFFSET();
  1502. CHECK_MEMORY_OVERFLOW(2);
  1503. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
  1504. HANDLE_OP_END();
  1505. }
  1506. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  1507. {
  1508. uint32 offset, addr;
  1509. offset = read_uint32(frame_ip);
  1510. addr = GET_OPERAND(uint32, I32, 0);
  1511. frame_ip += 2;
  1512. addr_ret = GET_OFFSET();
  1513. CHECK_MEMORY_OVERFLOW(4);
  1514. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1515. sign_ext_32_64(LOAD_I32(maddr)));
  1516. HANDLE_OP_END();
  1517. }
  1518. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  1519. {
  1520. uint32 offset, addr;
  1521. offset = read_uint32(frame_ip);
  1522. addr = GET_OPERAND(uint32, I32, 0);
  1523. frame_ip += 2;
  1524. addr_ret = GET_OFFSET();
  1525. CHECK_MEMORY_OVERFLOW(4);
  1526. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
  1527. HANDLE_OP_END();
  1528. }
  1529. HANDLE_OP(WASM_OP_I32_STORE)
  1530. {
  1531. uint32 offset, addr;
  1532. uint32 sval;
  1533. offset = read_uint32(frame_ip);
  1534. sval = GET_OPERAND(uint32, I32, 0);
  1535. addr = GET_OPERAND(uint32, I32, 2);
  1536. frame_ip += 4;
  1537. CHECK_MEMORY_OVERFLOW(4);
  1538. STORE_U32(maddr, sval);
  1539. HANDLE_OP_END();
  1540. }
  1541. HANDLE_OP(WASM_OP_I32_STORE8)
  1542. {
  1543. uint32 offset, addr;
  1544. uint32 sval;
  1545. offset = read_uint32(frame_ip);
  1546. sval = GET_OPERAND(uint32, I32, 0);
  1547. addr = GET_OPERAND(uint32, I32, 2);
  1548. frame_ip += 4;
  1549. CHECK_MEMORY_OVERFLOW(1);
  1550. *(uint8 *)maddr = (uint8)sval;
  1551. HANDLE_OP_END();
  1552. }
  1553. HANDLE_OP(WASM_OP_I32_STORE16)
  1554. {
  1555. uint32 offset, addr;
  1556. uint32 sval;
  1557. offset = read_uint32(frame_ip);
  1558. sval = GET_OPERAND(uint32, I32, 0);
  1559. addr = GET_OPERAND(uint32, I32, 2);
  1560. frame_ip += 4;
  1561. CHECK_MEMORY_OVERFLOW(2);
  1562. STORE_U16(maddr, (uint16)sval);
  1563. HANDLE_OP_END();
  1564. }
  1565. HANDLE_OP(WASM_OP_I64_STORE)
  1566. {
  1567. uint32 offset, addr;
  1568. uint64 sval;
  1569. offset = read_uint32(frame_ip);
  1570. sval = GET_OPERAND(uint64, I64, 0);
  1571. addr = GET_OPERAND(uint32, I32, 2);
  1572. frame_ip += 4;
  1573. CHECK_MEMORY_OVERFLOW(8);
  1574. STORE_I64(maddr, sval);
  1575. HANDLE_OP_END();
  1576. }
  1577. HANDLE_OP(WASM_OP_I64_STORE8)
  1578. {
  1579. uint32 offset, addr;
  1580. uint64 sval;
  1581. offset = read_uint32(frame_ip);
  1582. sval = GET_OPERAND(uint64, I64, 0);
  1583. addr = GET_OPERAND(uint32, I32, 2);
  1584. frame_ip += 4;
  1585. CHECK_MEMORY_OVERFLOW(1);
  1586. *(uint8 *)maddr = (uint8)sval;
  1587. HANDLE_OP_END();
  1588. }
  1589. HANDLE_OP(WASM_OP_I64_STORE16)
  1590. {
  1591. uint32 offset, addr;
  1592. uint64 sval;
  1593. offset = read_uint32(frame_ip);
  1594. sval = GET_OPERAND(uint64, I64, 0);
  1595. addr = GET_OPERAND(uint32, I32, 2);
  1596. frame_ip += 4;
  1597. CHECK_MEMORY_OVERFLOW(2);
  1598. STORE_U16(maddr, (uint16)sval);
  1599. HANDLE_OP_END();
  1600. }
  1601. HANDLE_OP(WASM_OP_I64_STORE32)
  1602. {
  1603. uint32 offset, addr;
  1604. uint64 sval;
  1605. offset = read_uint32(frame_ip);
  1606. sval = GET_OPERAND(uint64, I64, 0);
  1607. addr = GET_OPERAND(uint32, I32, 2);
  1608. frame_ip += 4;
  1609. CHECK_MEMORY_OVERFLOW(4);
  1610. STORE_U32(maddr, (uint32)sval);
  1611. HANDLE_OP_END();
  1612. }
  1613. /* memory size and memory grow instructions */
  1614. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  1615. {
  1616. uint32 reserved;
  1617. addr_ret = GET_OFFSET();
  1618. frame_lp[addr_ret] = memory->cur_page_count;
  1619. (void)reserved;
  1620. HANDLE_OP_END();
  1621. }
  1622. HANDLE_OP(WASM_OP_MEMORY_GROW)
  1623. {
  1624. uint32 reserved, delta,
  1625. prev_page_count = memory->cur_page_count;
  1626. addr1 = GET_OFFSET();
  1627. addr_ret = GET_OFFSET();
  1628. delta = (uint32)frame_lp[addr1];
  1629. if (!wasm_enlarge_memory(module, delta)) {
  1630. /* failed to memory.grow, return -1 */
  1631. frame_lp[addr_ret] = -1;
  1632. }
  1633. else {
  1634. /* success, return previous page count */
  1635. frame_lp[addr_ret] = prev_page_count;
  1636. /* update memory instance ptr and memory size */
  1637. memory = module->default_memory;
  1638. linear_mem_size =
  1639. num_bytes_per_page * memory->cur_page_count;
  1640. }
  1641. (void)reserved;
  1642. HANDLE_OP_END();
  1643. }
  1644. /* comparison instructions of i32 */
  1645. HANDLE_OP(WASM_OP_I32_EQZ)
  1646. {
  1647. DEF_OP_EQZ(int32, I32);
  1648. HANDLE_OP_END();
  1649. }
  1650. HANDLE_OP(WASM_OP_I32_EQ)
  1651. {
  1652. DEF_OP_CMP(uint32, I32, ==);
  1653. HANDLE_OP_END();
  1654. }
  1655. HANDLE_OP(WASM_OP_I32_NE)
  1656. {
  1657. DEF_OP_CMP(uint32, I32, !=);
  1658. HANDLE_OP_END();
  1659. }
  1660. HANDLE_OP(WASM_OP_I32_LT_S)
  1661. {
  1662. DEF_OP_CMP(int32, I32, <);
  1663. HANDLE_OP_END();
  1664. }
  1665. HANDLE_OP(WASM_OP_I32_LT_U)
  1666. {
  1667. DEF_OP_CMP(uint32, I32, <);
  1668. HANDLE_OP_END();
  1669. }
  1670. HANDLE_OP(WASM_OP_I32_GT_S)
  1671. {
  1672. DEF_OP_CMP(int32, I32, >);
  1673. HANDLE_OP_END();
  1674. }
  1675. HANDLE_OP(WASM_OP_I32_GT_U)
  1676. {
  1677. DEF_OP_CMP(uint32, I32, >);
  1678. HANDLE_OP_END();
  1679. }
  1680. HANDLE_OP(WASM_OP_I32_LE_S)
  1681. {
  1682. DEF_OP_CMP(int32, I32, <=);
  1683. HANDLE_OP_END();
  1684. }
  1685. HANDLE_OP(WASM_OP_I32_LE_U)
  1686. {
  1687. DEF_OP_CMP(uint32, I32, <=);
  1688. HANDLE_OP_END();
  1689. }
  1690. HANDLE_OP(WASM_OP_I32_GE_S)
  1691. {
  1692. DEF_OP_CMP(int32, I32, >=);
  1693. HANDLE_OP_END();
  1694. }
  1695. HANDLE_OP(WASM_OP_I32_GE_U)
  1696. {
  1697. DEF_OP_CMP(uint32, I32, >=);
  1698. HANDLE_OP_END();
  1699. }
  1700. /* comparison instructions of i64 */
  1701. HANDLE_OP(WASM_OP_I64_EQZ)
  1702. {
  1703. DEF_OP_EQZ(int64, I64);
  1704. HANDLE_OP_END();
  1705. }
  1706. HANDLE_OP(WASM_OP_I64_EQ)
  1707. {
  1708. DEF_OP_CMP(uint64, I64, ==);
  1709. HANDLE_OP_END();
  1710. }
  1711. HANDLE_OP(WASM_OP_I64_NE)
  1712. {
  1713. DEF_OP_CMP(uint64, I64, !=);
  1714. HANDLE_OP_END();
  1715. }
  1716. HANDLE_OP(WASM_OP_I64_LT_S)
  1717. {
  1718. DEF_OP_CMP(int64, I64, <);
  1719. HANDLE_OP_END();
  1720. }
  1721. HANDLE_OP(WASM_OP_I64_LT_U)
  1722. {
  1723. DEF_OP_CMP(uint64, I64, <);
  1724. HANDLE_OP_END();
  1725. }
  1726. HANDLE_OP(WASM_OP_I64_GT_S)
  1727. {
  1728. DEF_OP_CMP(int64, I64, >);
  1729. HANDLE_OP_END();
  1730. }
  1731. HANDLE_OP(WASM_OP_I64_GT_U)
  1732. {
  1733. DEF_OP_CMP(uint64, I64, >);
  1734. HANDLE_OP_END();
  1735. }
  1736. HANDLE_OP(WASM_OP_I64_LE_S)
  1737. {
  1738. DEF_OP_CMP(int64, I64, <=);
  1739. HANDLE_OP_END();
  1740. }
  1741. HANDLE_OP(WASM_OP_I64_LE_U)
  1742. {
  1743. DEF_OP_CMP(uint64, I64, <=);
  1744. HANDLE_OP_END();
  1745. }
  1746. HANDLE_OP(WASM_OP_I64_GE_S)
  1747. {
  1748. DEF_OP_CMP(int64, I64, >=);
  1749. HANDLE_OP_END();
  1750. }
  1751. HANDLE_OP(WASM_OP_I64_GE_U)
  1752. {
  1753. DEF_OP_CMP(uint64, I64, >=);
  1754. HANDLE_OP_END();
  1755. }
  1756. /* comparison instructions of f32 */
  1757. HANDLE_OP(WASM_OP_F32_EQ)
  1758. {
  1759. DEF_OP_CMP(float32, F32, ==);
  1760. HANDLE_OP_END();
  1761. }
  1762. HANDLE_OP(WASM_OP_F32_NE)
  1763. {
  1764. DEF_OP_CMP(float32, F32, !=);
  1765. HANDLE_OP_END();
  1766. }
  1767. HANDLE_OP(WASM_OP_F32_LT)
  1768. {
  1769. DEF_OP_CMP(float32, F32, <);
  1770. HANDLE_OP_END();
  1771. }
  1772. HANDLE_OP(WASM_OP_F32_GT)
  1773. {
  1774. DEF_OP_CMP(float32, F32, >);
  1775. HANDLE_OP_END();
  1776. }
  1777. HANDLE_OP(WASM_OP_F32_LE)
  1778. {
  1779. DEF_OP_CMP(float32, F32, <=);
  1780. HANDLE_OP_END();
  1781. }
  1782. HANDLE_OP(WASM_OP_F32_GE)
  1783. {
  1784. DEF_OP_CMP(float32, F32, >=);
  1785. HANDLE_OP_END();
  1786. }
  1787. /* comparison instructions of f64 */
  1788. HANDLE_OP(WASM_OP_F64_EQ)
  1789. {
  1790. DEF_OP_CMP(float64, F64, ==);
  1791. HANDLE_OP_END();
  1792. }
  1793. HANDLE_OP(WASM_OP_F64_NE)
  1794. {
  1795. DEF_OP_CMP(float64, F64, !=);
  1796. HANDLE_OP_END();
  1797. }
  1798. HANDLE_OP(WASM_OP_F64_LT)
  1799. {
  1800. DEF_OP_CMP(float64, F64, <);
  1801. HANDLE_OP_END();
  1802. }
  1803. HANDLE_OP(WASM_OP_F64_GT)
  1804. {
  1805. DEF_OP_CMP(float64, F64, >);
  1806. HANDLE_OP_END();
  1807. }
  1808. HANDLE_OP(WASM_OP_F64_LE)
  1809. {
  1810. DEF_OP_CMP(float64, F64, <=);
  1811. HANDLE_OP_END();
  1812. }
  1813. HANDLE_OP(WASM_OP_F64_GE)
  1814. {
  1815. DEF_OP_CMP(float64, F64, >=);
  1816. HANDLE_OP_END();
  1817. }
  1818. /* numberic instructions of i32 */
  1819. HANDLE_OP(WASM_OP_I32_CLZ)
  1820. {
  1821. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  1822. HANDLE_OP_END();
  1823. }
  1824. HANDLE_OP(WASM_OP_I32_CTZ)
  1825. {
  1826. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  1827. HANDLE_OP_END();
  1828. }
  1829. HANDLE_OP(WASM_OP_I32_POPCNT)
  1830. {
  1831. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  1832. HANDLE_OP_END();
  1833. }
  1834. HANDLE_OP(WASM_OP_I32_ADD)
  1835. {
  1836. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  1837. HANDLE_OP_END();
  1838. }
  1839. HANDLE_OP(WASM_OP_I32_SUB)
  1840. {
  1841. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  1842. HANDLE_OP_END();
  1843. }
  1844. HANDLE_OP(WASM_OP_I32_MUL)
  1845. {
  1846. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  1847. HANDLE_OP_END();
  1848. }
  1849. HANDLE_OP(WASM_OP_I32_DIV_S)
  1850. {
  1851. int32 a, b;
  1852. b = frame_lp[GET_OFFSET()];
  1853. a = frame_lp[GET_OFFSET()];
  1854. addr_ret = GET_OFFSET();
  1855. if (a == (int32)0x80000000 && b == -1) {
  1856. wasm_set_exception(module, "integer overflow");
  1857. goto got_exception;
  1858. }
  1859. if (b == 0) {
  1860. wasm_set_exception(module, "integer divide by zero");
  1861. goto got_exception;
  1862. }
  1863. frame_lp[addr_ret] = (a / b);
  1864. HANDLE_OP_END();
  1865. }
  1866. HANDLE_OP(WASM_OP_I32_DIV_U)
  1867. {
  1868. uint32 a, b;
  1869. addr1 = GET_OFFSET();
  1870. addr2 = GET_OFFSET();
  1871. addr_ret = GET_OFFSET();
  1872. b = (uint32)frame_lp[addr1];
  1873. a = (uint32)frame_lp[addr2];
  1874. if (b == 0) {
  1875. wasm_set_exception(module, "integer divide by zero");
  1876. goto got_exception;
  1877. }
  1878. frame_lp[addr_ret] = (a / b);
  1879. HANDLE_OP_END();
  1880. }
  1881. HANDLE_OP(WASM_OP_I32_REM_S)
  1882. {
  1883. int32 a, b;
  1884. addr1 = GET_OFFSET();
  1885. addr2 = GET_OFFSET();
  1886. addr_ret = GET_OFFSET();
  1887. b = frame_lp[addr1];
  1888. a = frame_lp[addr2];
  1889. if (a == (int32)0x80000000 && b == -1) {
  1890. frame_lp[addr_ret] = 0;
  1891. HANDLE_OP_END();
  1892. }
  1893. if (b == 0) {
  1894. wasm_set_exception(module, "integer divide by zero");
  1895. goto got_exception;
  1896. }
  1897. frame_lp[addr_ret] = (a % b);
  1898. HANDLE_OP_END();
  1899. }
  1900. HANDLE_OP(WASM_OP_I32_REM_U)
  1901. {
  1902. uint32 a, b;
  1903. addr1 = GET_OFFSET();
  1904. addr2 = GET_OFFSET();
  1905. addr_ret = GET_OFFSET();
  1906. b = (uint32)frame_lp[addr1];
  1907. a = (uint32)frame_lp[addr2];
  1908. if (b == 0) {
  1909. wasm_set_exception(module, "integer divide by zero");
  1910. goto got_exception;
  1911. }
  1912. frame_lp[addr_ret] = (a % b);
  1913. HANDLE_OP_END();
  1914. }
  1915. HANDLE_OP(WASM_OP_I32_AND)
  1916. {
  1917. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  1918. HANDLE_OP_END();
  1919. }
  1920. HANDLE_OP(WASM_OP_I32_OR)
  1921. {
  1922. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  1923. HANDLE_OP_END();
  1924. }
  1925. HANDLE_OP(WASM_OP_I32_XOR)
  1926. {
  1927. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  1928. HANDLE_OP_END();
  1929. }
  1930. HANDLE_OP(WASM_OP_I32_SHL)
  1931. {
  1932. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  1933. HANDLE_OP_END();
  1934. }
  1935. HANDLE_OP(WASM_OP_I32_SHR_S)
  1936. {
  1937. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  1938. HANDLE_OP_END();
  1939. }
  1940. HANDLE_OP(WASM_OP_I32_SHR_U)
  1941. {
  1942. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  1943. HANDLE_OP_END();
  1944. }
  1945. HANDLE_OP(WASM_OP_I32_ROTL)
  1946. {
  1947. uint32 a, b;
  1948. b = (uint32)frame_lp[GET_OFFSET()];
  1949. a = (uint32)frame_lp[GET_OFFSET()];
  1950. frame_lp[GET_OFFSET()] = rotl32(a, b);
  1951. HANDLE_OP_END();
  1952. }
  1953. HANDLE_OP(WASM_OP_I32_ROTR)
  1954. {
  1955. uint32 a, b;
  1956. b = (uint32)frame_lp[GET_OFFSET()];
  1957. a = (uint32)frame_lp[GET_OFFSET()];
  1958. frame_lp[GET_OFFSET()] = rotr32(a, b);
  1959. HANDLE_OP_END();
  1960. }
  1961. /* numberic instructions of i64 */
  1962. HANDLE_OP(WASM_OP_I64_CLZ)
  1963. {
  1964. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  1965. HANDLE_OP_END();
  1966. }
  1967. HANDLE_OP(WASM_OP_I64_CTZ)
  1968. {
  1969. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  1970. HANDLE_OP_END();
  1971. }
  1972. HANDLE_OP(WASM_OP_I64_POPCNT)
  1973. {
  1974. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  1975. HANDLE_OP_END();
  1976. }
  1977. HANDLE_OP(WASM_OP_I64_ADD)
  1978. {
  1979. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  1980. HANDLE_OP_END();
  1981. }
  1982. HANDLE_OP(WASM_OP_I64_SUB)
  1983. {
  1984. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  1985. HANDLE_OP_END();
  1986. }
  1987. HANDLE_OP(WASM_OP_I64_MUL)
  1988. {
  1989. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  1990. HANDLE_OP_END();
  1991. }
  1992. HANDLE_OP(WASM_OP_I64_DIV_S)
  1993. {
  1994. int64 a, b;
  1995. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  1996. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  1997. if (a == (int64)0x8000000000000000LL && b == -1) {
  1998. wasm_set_exception(module, "integer overflow");
  1999. goto got_exception;
  2000. }
  2001. if (b == 0) {
  2002. wasm_set_exception(module, "integer divide by zero");
  2003. goto got_exception;
  2004. }
  2005. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2006. HANDLE_OP_END();
  2007. }
  2008. HANDLE_OP(WASM_OP_I64_DIV_U)
  2009. {
  2010. uint64 a, b;
  2011. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2012. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2013. if (b == 0) {
  2014. wasm_set_exception(module, "integer divide by zero");
  2015. goto got_exception;
  2016. }
  2017. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2018. HANDLE_OP_END();
  2019. }
  2020. HANDLE_OP(WASM_OP_I64_REM_S)
  2021. {
  2022. int64 a, b;
  2023. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2024. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2025. if (a == (int64)0x8000000000000000LL && b == -1) {
  2026. *(int64 *)(frame_lp + GET_OFFSET()) = 0;
  2027. HANDLE_OP_END();
  2028. }
  2029. if (b == 0) {
  2030. wasm_set_exception(module, "integer divide by zero");
  2031. goto got_exception;
  2032. }
  2033. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2034. HANDLE_OP_END();
  2035. }
  2036. HANDLE_OP(WASM_OP_I64_REM_U)
  2037. {
  2038. uint64 a, b;
  2039. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2040. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2041. if (b == 0) {
  2042. wasm_set_exception(module, "integer divide by zero");
  2043. goto got_exception;
  2044. }
  2045. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2046. HANDLE_OP_END();
  2047. }
  2048. HANDLE_OP(WASM_OP_I64_AND)
  2049. {
  2050. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  2051. HANDLE_OP_END();
  2052. }
  2053. HANDLE_OP(WASM_OP_I64_OR)
  2054. {
  2055. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  2056. HANDLE_OP_END();
  2057. }
  2058. HANDLE_OP(WASM_OP_I64_XOR)
  2059. {
  2060. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  2061. HANDLE_OP_END();
  2062. }
  2063. HANDLE_OP(WASM_OP_I64_SHL)
  2064. {
  2065. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  2066. HANDLE_OP_END();
  2067. }
  2068. HANDLE_OP(WASM_OP_I64_SHR_S)
  2069. {
  2070. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  2071. HANDLE_OP_END();
  2072. }
  2073. HANDLE_OP(WASM_OP_I64_SHR_U)
  2074. {
  2075. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  2076. HANDLE_OP_END();
  2077. }
  2078. HANDLE_OP(WASM_OP_I64_ROTL)
  2079. {
  2080. uint64 a, b;
  2081. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2082. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2083. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
  2084. HANDLE_OP_END();
  2085. }
  2086. HANDLE_OP(WASM_OP_I64_ROTR)
  2087. {
  2088. uint64 a, b;
  2089. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2090. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2091. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
  2092. HANDLE_OP_END();
  2093. }
  2094. /* numberic instructions of f32 */
  2095. HANDLE_OP(WASM_OP_F32_ABS)
  2096. {
  2097. DEF_OP_MATH(float32, F32, fabs);
  2098. HANDLE_OP_END();
  2099. }
  2100. HANDLE_OP(WASM_OP_F32_NEG)
  2101. {
  2102. uint32 u32 = frame_lp[GET_OFFSET()];
  2103. uint32 sign_bit = u32 & ((uint32)1 << 31);
  2104. addr_ret = GET_OFFSET();
  2105. if (sign_bit)
  2106. frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
  2107. else
  2108. frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
  2109. HANDLE_OP_END();
  2110. }
  2111. HANDLE_OP(WASM_OP_F32_CEIL)
  2112. {
  2113. DEF_OP_MATH(float32, F32, ceil);
  2114. HANDLE_OP_END();
  2115. }
  2116. HANDLE_OP(WASM_OP_F32_FLOOR)
  2117. {
  2118. DEF_OP_MATH(float32, F32, floor);
  2119. HANDLE_OP_END();
  2120. }
  2121. HANDLE_OP(WASM_OP_F32_TRUNC)
  2122. {
  2123. DEF_OP_MATH(float32, F32, trunc);
  2124. HANDLE_OP_END();
  2125. }
  2126. HANDLE_OP(WASM_OP_F32_NEAREST)
  2127. {
  2128. DEF_OP_MATH(float32, F32, rint);
  2129. HANDLE_OP_END();
  2130. }
  2131. HANDLE_OP(WASM_OP_F32_SQRT)
  2132. {
  2133. DEF_OP_MATH(float32, F32, sqrt);
  2134. HANDLE_OP_END();
  2135. }
  2136. HANDLE_OP(WASM_OP_F32_ADD)
  2137. {
  2138. DEF_OP_NUMERIC(float32, float32, F32, +);
  2139. HANDLE_OP_END();
  2140. }
  2141. HANDLE_OP(WASM_OP_F32_SUB)
  2142. {
  2143. DEF_OP_NUMERIC(float32, float32, F32, -);
  2144. HANDLE_OP_END();
  2145. }
  2146. HANDLE_OP(WASM_OP_F32_MUL)
  2147. {
  2148. DEF_OP_NUMERIC(float32, float32, F32, *);
  2149. HANDLE_OP_END();
  2150. }
  2151. HANDLE_OP(WASM_OP_F32_DIV)
  2152. {
  2153. DEF_OP_NUMERIC(float32, float32, F32, /);
  2154. HANDLE_OP_END();
  2155. }
  2156. HANDLE_OP(WASM_OP_F32_MIN)
  2157. {
  2158. float32 a, b;
  2159. b = *(float32 *)(frame_lp + GET_OFFSET());
  2160. a = *(float32 *)(frame_lp + GET_OFFSET());
  2161. if (isnan(a))
  2162. *(float32 *)(frame_lp + GET_OFFSET()) = a;
  2163. else if (isnan(b))
  2164. *(float32 *)(frame_lp + GET_OFFSET()) = b;
  2165. else
  2166. *(float32 *)(frame_lp + GET_OFFSET()) =
  2167. (float32)wa_fmin(a, b);
  2168. HANDLE_OP_END();
  2169. }
  2170. HANDLE_OP(WASM_OP_F32_MAX)
  2171. {
  2172. float32 a, b;
  2173. b = *(float32 *)(frame_lp + GET_OFFSET());
  2174. a = *(float32 *)(frame_lp + GET_OFFSET());
  2175. if (isnan(a))
  2176. *(float32 *)(frame_lp + GET_OFFSET()) = a;
  2177. else if (isnan(b))
  2178. *(float32 *)(frame_lp + GET_OFFSET()) = b;
  2179. else
  2180. *(float32 *)(frame_lp + GET_OFFSET()) =
  2181. (float32)wa_fmax(a, b);
  2182. HANDLE_OP_END();
  2183. }
  2184. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  2185. {
  2186. float32 a, b;
  2187. b = *(float32 *)(frame_lp + GET_OFFSET());
  2188. a = *(float32 *)(frame_lp + GET_OFFSET());
  2189. *(float32 *)(frame_lp + GET_OFFSET()) =
  2190. (float32)(signbit(b) ? -fabs(a) : fabs(a));
  2191. HANDLE_OP_END();
  2192. }
  2193. /* numberic instructions of f64 */
  2194. HANDLE_OP(WASM_OP_F64_ABS)
  2195. {
  2196. DEF_OP_MATH(float64, F64, fabs);
  2197. HANDLE_OP_END();
  2198. }
  2199. HANDLE_OP(WASM_OP_F64_NEG)
  2200. {
  2201. uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2202. uint64 sign_bit = u64 & (((uint64)1) << 63);
  2203. if (sign_bit)
  2204. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2205. (u64 & ~(((uint64)1) << 63)));
  2206. else
  2207. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2208. (u64 | (((uint64)1) << 63)));
  2209. HANDLE_OP_END();
  2210. }
  2211. HANDLE_OP(WASM_OP_F64_CEIL)
  2212. {
  2213. DEF_OP_MATH(float64, F64, ceil);
  2214. HANDLE_OP_END();
  2215. }
  2216. HANDLE_OP(WASM_OP_F64_FLOOR)
  2217. {
  2218. DEF_OP_MATH(float64, F64, floor);
  2219. HANDLE_OP_END();
  2220. }
  2221. HANDLE_OP(WASM_OP_F64_TRUNC)
  2222. {
  2223. DEF_OP_MATH(float64, F64, trunc);
  2224. HANDLE_OP_END();
  2225. }
  2226. HANDLE_OP(WASM_OP_F64_NEAREST)
  2227. {
  2228. DEF_OP_MATH(float64, F64, rint);
  2229. HANDLE_OP_END();
  2230. }
  2231. HANDLE_OP(WASM_OP_F64_SQRT)
  2232. {
  2233. DEF_OP_MATH(float64, F64, sqrt);
  2234. HANDLE_OP_END();
  2235. }
  2236. HANDLE_OP(WASM_OP_F64_ADD)
  2237. {
  2238. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  2239. HANDLE_OP_END();
  2240. }
  2241. HANDLE_OP(WASM_OP_F64_SUB)
  2242. {
  2243. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  2244. HANDLE_OP_END();
  2245. }
  2246. HANDLE_OP(WASM_OP_F64_MUL)
  2247. {
  2248. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  2249. HANDLE_OP_END();
  2250. }
  2251. HANDLE_OP(WASM_OP_F64_DIV)
  2252. {
  2253. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  2254. HANDLE_OP_END();
  2255. }
  2256. HANDLE_OP(WASM_OP_F64_MIN)
  2257. {
  2258. float64 a, b;
  2259. b = POP_F64();
  2260. a = POP_F64();
  2261. if (isnan(a))
  2262. PUSH_F64(a);
  2263. else if (isnan(b))
  2264. PUSH_F64(b);
  2265. else
  2266. PUSH_F64(wa_fmin(a, b));
  2267. HANDLE_OP_END();
  2268. }
  2269. HANDLE_OP(WASM_OP_F64_MAX)
  2270. {
  2271. float64 a, b;
  2272. b = POP_F64();
  2273. a = POP_F64();
  2274. if (isnan(a))
  2275. PUSH_F64(a);
  2276. else if (isnan(b))
  2277. PUSH_F64(b);
  2278. else
  2279. PUSH_F64(wa_fmax(a, b));
  2280. HANDLE_OP_END();
  2281. }
  2282. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  2283. {
  2284. float64 a, b;
  2285. b = POP_F64();
  2286. a = POP_F64();
  2287. PUSH_F64(signbit(b) ? -fabs(a) : fabs(a));
  2288. HANDLE_OP_END();
  2289. }
  2290. /* conversions of i32 */
  2291. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  2292. {
  2293. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  2294. PUSH_I32(value);
  2295. HANDLE_OP_END();
  2296. }
  2297. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  2298. {
  2299. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  2300. since float/double values of ieee754 cannot precisely
  2301. represent all int32/uint32/int64/uint64 values, e.g.:
  2302. UINT32_MAX is 4294967295, but (float32)4294967295 is
  2303. 4294967296.0f, but not 4294967295.0f. */
  2304. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  2305. HANDLE_OP_END();
  2306. }
  2307. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  2308. {
  2309. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  2310. HANDLE_OP_END();
  2311. }
  2312. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  2313. {
  2314. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  2315. HANDLE_OP_END();
  2316. }
  2317. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  2318. {
  2319. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  2320. HANDLE_OP_END();
  2321. }
  2322. /* conversions of i64 */
  2323. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  2324. {
  2325. DEF_OP_CONVERT(int64, I64, int32, I32);
  2326. HANDLE_OP_END();
  2327. }
  2328. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  2329. {
  2330. DEF_OP_CONVERT(int64, I64, uint32, I32);
  2331. HANDLE_OP_END();
  2332. }
  2333. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  2334. {
  2335. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  2336. 9223372036854775808.0f, false, true);
  2337. HANDLE_OP_END();
  2338. }
  2339. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  2340. {
  2341. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  2342. HANDLE_OP_END();
  2343. }
  2344. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  2345. {
  2346. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  2347. false, true);
  2348. HANDLE_OP_END();
  2349. }
  2350. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  2351. {
  2352. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  2353. HANDLE_OP_END();
  2354. }
  2355. /* conversions of f32 */
  2356. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  2357. {
  2358. DEF_OP_CONVERT(float32, F32, int32, I32);
  2359. HANDLE_OP_END();
  2360. }
  2361. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  2362. {
  2363. DEF_OP_CONVERT(float32, F32, uint32, I32);
  2364. HANDLE_OP_END();
  2365. }
  2366. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  2367. {
  2368. DEF_OP_CONVERT(float32, F32, int64, I64);
  2369. HANDLE_OP_END();
  2370. }
  2371. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  2372. {
  2373. DEF_OP_CONVERT(float32, F32, uint64, I64);
  2374. HANDLE_OP_END();
  2375. }
  2376. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  2377. {
  2378. DEF_OP_CONVERT(float32, F32, float64, F64);
  2379. HANDLE_OP_END();
  2380. }
  2381. /* conversions of f64 */
  2382. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  2383. {
  2384. DEF_OP_CONVERT(float64, F64, int32, I32);
  2385. HANDLE_OP_END();
  2386. }
  2387. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  2388. {
  2389. DEF_OP_CONVERT(float64, F64, uint32, I32);
  2390. HANDLE_OP_END();
  2391. }
  2392. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  2393. {
  2394. DEF_OP_CONVERT(float64, F64, int64, I64);
  2395. HANDLE_OP_END();
  2396. }
  2397. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  2398. {
  2399. DEF_OP_CONVERT(float64, F64, uint64, I64);
  2400. HANDLE_OP_END();
  2401. }
  2402. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  2403. {
  2404. DEF_OP_CONVERT(float64, F64, float32, F32);
  2405. HANDLE_OP_END();
  2406. }
  2407. /* reinterpretations */
  2408. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  2409. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  2410. {
  2411. DEF_OP_REINTERPRET(uint32, I32);
  2412. HANDLE_OP_END();
  2413. }
  2414. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  2415. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
  2416. {
  2417. DEF_OP_REINTERPRET(int64, I64);
  2418. HANDLE_OP_END();
  2419. }
  2420. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  2421. {
  2422. addr1 = GET_OFFSET();
  2423. addr2 = GET_OFFSET();
  2424. frame_lp[addr2] = frame_lp[addr1];
  2425. HANDLE_OP_END();
  2426. }
  2427. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  2428. {
  2429. addr1 = GET_OFFSET();
  2430. addr2 = GET_OFFSET();
  2431. frame_lp[addr2] = frame_lp[addr1];
  2432. frame_lp[addr2 + 1] = frame_lp[addr1 + 1];
  2433. HANDLE_OP_END();
  2434. }
  2435. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  2436. {
  2437. uint32 values_count, total_cell;
  2438. uint8 *cells;
  2439. int16 *src_offsets = NULL;
  2440. uint16 *dst_offsets = NULL;
  2441. /* read values_count */
  2442. values_count = read_uint32(frame_ip);
  2443. /* read total cell num */
  2444. total_cell = read_uint32(frame_ip);
  2445. /* cells */
  2446. cells = (uint8 *)frame_ip;
  2447. frame_ip += values_count * CELL_SIZE;
  2448. /* src offsets */
  2449. src_offsets = (int16 *)frame_ip;
  2450. frame_ip += values_count * sizeof(int16);
  2451. /* dst offsets */
  2452. dst_offsets = (uint16 *)frame_ip;
  2453. frame_ip += values_count * sizeof(uint16);
  2454. if (!copy_stack_values(module, frame_lp, values_count,
  2455. total_cell, cells, src_offsets,
  2456. dst_offsets))
  2457. goto got_exception;
  2458. HANDLE_OP_END();
  2459. }
  2460. HANDLE_OP(WASM_OP_SET_LOCAL)
  2461. HANDLE_OP(WASM_OP_TEE_LOCAL)
  2462. {
  2463. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  2464. addr1 = GET_OFFSET();
  2465. if (local_type == VALUE_TYPE_I32
  2466. || local_type == VALUE_TYPE_F32) {
  2467. *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
  2468. }
  2469. else if (local_type == VALUE_TYPE_I64
  2470. || local_type == VALUE_TYPE_F64) {
  2471. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  2472. GET_I64_FROM_ADDR(frame_lp + addr1));
  2473. }
  2474. else {
  2475. wasm_set_exception(module, "invalid local type");
  2476. goto got_exception;
  2477. }
  2478. HANDLE_OP_END();
  2479. }
  2480. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  2481. {
  2482. DEF_OP_CONVERT(int32, I32, int8, I32);
  2483. HANDLE_OP_END();
  2484. }
  2485. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  2486. {
  2487. DEF_OP_CONVERT(int32, I32, int16, I32);
  2488. HANDLE_OP_END();
  2489. }
  2490. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  2491. {
  2492. DEF_OP_CONVERT(int64, I64, int8, I64);
  2493. HANDLE_OP_END();
  2494. }
  2495. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  2496. {
  2497. DEF_OP_CONVERT(int64, I64, int16, I64);
  2498. HANDLE_OP_END();
  2499. }
  2500. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  2501. {
  2502. DEF_OP_CONVERT(int64, I64, int32, I64);
  2503. HANDLE_OP_END();
  2504. }
  2505. HANDLE_OP(WASM_OP_MISC_PREFIX)
  2506. {
  2507. GET_OPCODE();
  2508. switch (opcode) {
  2509. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2510. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  2511. true, true);
  2512. break;
  2513. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2514. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  2515. break;
  2516. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2517. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  2518. true);
  2519. break;
  2520. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2521. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  2522. break;
  2523. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2524. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  2525. 9223372036854775808.0f, false,
  2526. true);
  2527. break;
  2528. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2529. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  2530. false, false);
  2531. break;
  2532. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2533. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  2534. 9223372036854775808.0, false,
  2535. true);
  2536. break;
  2537. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2538. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  2539. false, false);
  2540. break;
  2541. #if WASM_ENABLE_BULK_MEMORY != 0
  2542. case WASM_OP_MEMORY_INIT:
  2543. {
  2544. uint32 addr, segment;
  2545. uint64 bytes, offset, seg_len;
  2546. uint8 *data;
  2547. segment = read_uint32(frame_ip);
  2548. bytes = (uint64)POP_I32();
  2549. offset = (uint64)POP_I32();
  2550. addr = POP_I32();
  2551. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  2552. seg_len = (uint64)module->module->data_segments[segment].data_length;
  2553. data = module->module->data_segments[segment].data;
  2554. if (offset + bytes > seg_len)
  2555. goto out_of_bounds;
  2556. bh_memcpy_s(maddr, linear_mem_size - addr,
  2557. data + offset, (uint32)bytes);
  2558. break;
  2559. }
  2560. case WASM_OP_DATA_DROP:
  2561. {
  2562. uint32 segment;
  2563. segment = read_uint32(frame_ip);
  2564. module->module->data_segments[segment].data_length = 0;
  2565. break;
  2566. }
  2567. case WASM_OP_MEMORY_COPY:
  2568. {
  2569. uint32 dst, src, len;
  2570. uint8 *mdst, *msrc;
  2571. len = POP_I32();
  2572. src = POP_I32();
  2573. dst = POP_I32();
  2574. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  2575. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2576. /* allowing the destination and source to overlap */
  2577. bh_memmove_s(mdst, linear_mem_size - dst, msrc, len);
  2578. break;
  2579. }
  2580. case WASM_OP_MEMORY_FILL:
  2581. {
  2582. uint32 dst, len;
  2583. uint8 val, *mdst;
  2584. len = POP_I32();
  2585. val = POP_I32();
  2586. dst = POP_I32();
  2587. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2588. memset(mdst, val, len);
  2589. break;
  2590. }
  2591. #endif /* WASM_ENABLE_BULK_MEMORY */
  2592. #if WASM_ENABLE_REF_TYPES != 0
  2593. case WASM_OP_TABLE_INIT:
  2594. {
  2595. uint32 tbl_idx, elem_idx;
  2596. uint64 n, s, d;
  2597. WASMTableInstance *tbl_inst;
  2598. elem_idx = read_uint32(frame_ip);
  2599. bh_assert(elem_idx < module->module->table_seg_count);
  2600. tbl_idx = read_uint32(frame_ip);
  2601. bh_assert(tbl_idx < module->module->table_count);
  2602. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2603. n = (uint32)POP_I32();
  2604. s = (uint32)POP_I32();
  2605. d = (uint32)POP_I32();
  2606. if (!n) {
  2607. break;
  2608. }
  2609. if (n + s > module->module->table_segments[elem_idx]
  2610. .function_count
  2611. || d + n > tbl_inst->cur_size) {
  2612. wasm_set_exception(module,
  2613. "out of bounds table access");
  2614. goto got_exception;
  2615. }
  2616. if (module->module->table_segments[elem_idx]
  2617. .is_dropped) {
  2618. wasm_set_exception(module,
  2619. "out of bounds table access");
  2620. goto got_exception;
  2621. }
  2622. if (!wasm_elem_is_passive(
  2623. module->module->table_segments[elem_idx]
  2624. .mode)) {
  2625. wasm_set_exception(module,
  2626. "out of bounds table access");
  2627. goto got_exception;
  2628. }
  2629. bh_memcpy_s(
  2630. (uint8 *)tbl_inst
  2631. + offsetof(WASMTableInstance, base_addr)
  2632. + d * sizeof(uint32),
  2633. (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
  2634. module->module->table_segments[elem_idx]
  2635. .func_indexes
  2636. + s,
  2637. (uint32)(n * sizeof(uint32)));
  2638. break;
  2639. }
  2640. case WASM_OP_ELEM_DROP:
  2641. {
  2642. uint32 elem_idx = read_uint32(frame_ip);
  2643. bh_assert(elem_idx < module->module->table_seg_count);
  2644. module->module->table_segments[elem_idx].is_dropped =
  2645. true;
  2646. break;
  2647. }
  2648. case WASM_OP_TABLE_COPY:
  2649. {
  2650. uint32 src_tbl_idx, dst_tbl_idx;
  2651. uint64 n, s, d;
  2652. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  2653. dst_tbl_idx = read_uint32(frame_ip);
  2654. bh_assert(dst_tbl_idx < module->table_count);
  2655. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  2656. src_tbl_idx = read_uint32(frame_ip);
  2657. bh_assert(src_tbl_idx < module->table_count);
  2658. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  2659. n = (uint32)POP_I32();
  2660. s = (uint32)POP_I32();
  2661. d = (uint32)POP_I32();
  2662. if (s + n > dst_tbl_inst->cur_size
  2663. || d + n > src_tbl_inst->cur_size) {
  2664. wasm_set_exception(module,
  2665. "out of bounds table access");
  2666. goto got_exception;
  2667. }
  2668. /* if s >= d, copy from front to back */
  2669. /* if s < d, copy from back to front */
  2670. /* merge all together */
  2671. bh_memmove_s(
  2672. (uint8 *)dst_tbl_inst
  2673. + offsetof(WASMTableInstance, base_addr)
  2674. + d * sizeof(uint32),
  2675. (uint32)((dst_tbl_inst->cur_size - d)
  2676. * sizeof(uint32)),
  2677. (uint8 *)src_tbl_inst
  2678. + offsetof(WASMTableInstance, base_addr)
  2679. + s * sizeof(uint32),
  2680. (uint32)(n * sizeof(uint32)));
  2681. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  2682. /* invalidate all entries related to current instance, an opt could be applied which only invalidate impacted table elem id.*/
  2683. wasm_program_invalidate_cached_wasm_func(program, (WASMModuleInstanceCommon*)module);
  2684. #endif
  2685. break;
  2686. }
  2687. case WASM_OP_TABLE_GROW:
  2688. {
  2689. uint32 tbl_idx, n, init_val, orig_tbl_sz;
  2690. WASMTableInstance *tbl_inst;
  2691. tbl_idx = read_uint32(frame_ip);
  2692. bh_assert(tbl_idx < module->table_count);
  2693. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2694. orig_tbl_sz = tbl_inst->cur_size;
  2695. n = POP_I32();
  2696. init_val = POP_I32();
  2697. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  2698. PUSH_I32(-1);
  2699. }
  2700. else {
  2701. PUSH_I32(orig_tbl_sz);
  2702. }
  2703. break;
  2704. }
  2705. case WASM_OP_TABLE_SIZE:
  2706. {
  2707. uint32 tbl_idx;
  2708. WASMTableInstance *tbl_inst;
  2709. tbl_idx = read_uint32(frame_ip);
  2710. bh_assert(tbl_idx < module->table_count);
  2711. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2712. PUSH_I32(tbl_inst->cur_size);
  2713. break;
  2714. }
  2715. case WASM_OP_TABLE_FILL:
  2716. {
  2717. uint32 tbl_idx, n, val, i;
  2718. WASMTableInstance *tbl_inst;
  2719. tbl_idx = read_uint32(frame_ip);
  2720. bh_assert(tbl_idx < module->table_count);
  2721. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2722. n = POP_I32();
  2723. val = POP_I32();
  2724. i = POP_I32();
  2725. if (i + n > tbl_inst->cur_size) {
  2726. wasm_set_exception(module,
  2727. "out of bounds table access");
  2728. goto got_exception;
  2729. }
  2730. for (; n != 0; i++, n--) {
  2731. ((uint32 *)(tbl_inst->base_addr))[i] = val;
  2732. }
  2733. break;
  2734. }
  2735. #endif /* WASM_ENABLE_REF_TYPES */
  2736. default:
  2737. wasm_set_exception(module, "unsupported opcode");
  2738. goto got_exception;
  2739. }
  2740. HANDLE_OP_END();
  2741. }
  2742. #if WASM_ENABLE_SHARED_MEMORY != 0
  2743. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  2744. {
  2745. uint32 offset, addr;
  2746. GET_OPCODE();
  2747. offset = read_uint32(frame_ip);
  2748. switch (opcode) {
  2749. case WASM_OP_ATOMIC_NOTIFY:
  2750. {
  2751. uint32 count, ret;
  2752. count = POP_I32();
  2753. addr = POP_I32();
  2754. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2755. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2756. ret = wasm_runtime_atomic_notify(
  2757. (WASMModuleInstanceCommon *)module, maddr, count);
  2758. bh_assert((int32)ret >= 0);
  2759. PUSH_I32(ret);
  2760. break;
  2761. }
  2762. case WASM_OP_ATOMIC_WAIT32:
  2763. {
  2764. uint64 timeout;
  2765. uint32 expect, addr, ret;
  2766. timeout = POP_I64();
  2767. expect = POP_I32();
  2768. addr = POP_I32();
  2769. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2770. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2771. ret = wasm_runtime_atomic_wait(
  2772. (WASMModuleInstanceCommon *)module, maddr,
  2773. (uint64)expect, timeout, false);
  2774. if (ret == (uint32)-1)
  2775. goto got_exception;
  2776. PUSH_I32(ret);
  2777. break;
  2778. }
  2779. case WASM_OP_ATOMIC_WAIT64:
  2780. {
  2781. uint64 timeout, expect;
  2782. uint32 ret;
  2783. timeout = POP_I64();
  2784. expect = POP_I64();
  2785. addr = POP_I32();
  2786. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2787. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2788. ret = wasm_runtime_atomic_wait(
  2789. (WASMModuleInstanceCommon *)module, maddr, expect,
  2790. timeout, true);
  2791. if (ret == (uint32)-1)
  2792. goto got_exception;
  2793. PUSH_I32(ret);
  2794. break;
  2795. }
  2796. case WASM_OP_ATOMIC_I32_LOAD:
  2797. case WASM_OP_ATOMIC_I32_LOAD8_U:
  2798. case WASM_OP_ATOMIC_I32_LOAD16_U:
  2799. {
  2800. uint32 readv;
  2801. addr = POP_I32();
  2802. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  2803. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2804. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2805. os_mutex_lock(&memory->mem_lock);
  2806. readv = (uint32)(*(uint8 *)maddr);
  2807. os_mutex_unlock(&memory->mem_lock);
  2808. }
  2809. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  2810. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2811. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2812. os_mutex_lock(&memory->mem_lock);
  2813. readv = (uint32)LOAD_U16(maddr);
  2814. os_mutex_unlock(&memory->mem_lock);
  2815. }
  2816. else {
  2817. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2818. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2819. os_mutex_lock(&memory->mem_lock);
  2820. readv = LOAD_I32(maddr);
  2821. os_mutex_unlock(&memory->mem_lock);
  2822. }
  2823. PUSH_I32(readv);
  2824. break;
  2825. }
  2826. case WASM_OP_ATOMIC_I64_LOAD:
  2827. case WASM_OP_ATOMIC_I64_LOAD8_U:
  2828. case WASM_OP_ATOMIC_I64_LOAD16_U:
  2829. case WASM_OP_ATOMIC_I64_LOAD32_U:
  2830. {
  2831. uint64 readv;
  2832. addr = POP_I32();
  2833. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  2834. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2835. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2836. os_mutex_lock(&memory->mem_lock);
  2837. readv = (uint64)(*(uint8 *)maddr);
  2838. os_mutex_unlock(&memory->mem_lock);
  2839. }
  2840. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  2841. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2842. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2843. os_mutex_lock(&memory->mem_lock);
  2844. readv = (uint64)LOAD_U16(maddr);
  2845. os_mutex_unlock(&memory->mem_lock);
  2846. }
  2847. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  2848. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2849. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2850. os_mutex_lock(&memory->mem_lock);
  2851. readv = (uint64)LOAD_U32(maddr);
  2852. os_mutex_unlock(&memory->mem_lock);
  2853. }
  2854. else {
  2855. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2856. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2857. os_mutex_lock(&memory->mem_lock);
  2858. readv = LOAD_I64(maddr);
  2859. os_mutex_unlock(&memory->mem_lock);
  2860. }
  2861. PUSH_I64(readv);
  2862. break;
  2863. }
  2864. case WASM_OP_ATOMIC_I32_STORE:
  2865. case WASM_OP_ATOMIC_I32_STORE8:
  2866. case WASM_OP_ATOMIC_I32_STORE16:
  2867. {
  2868. uint32 sval;
  2869. sval = (uint32)POP_I32();
  2870. addr = POP_I32();
  2871. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  2872. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2873. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2874. os_mutex_lock(&memory->mem_lock);
  2875. *(uint8 *)maddr = (uint8)sval;
  2876. os_mutex_unlock(&memory->mem_lock);
  2877. }
  2878. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  2879. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2880. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2881. os_mutex_lock(&memory->mem_lock);
  2882. STORE_U16(maddr, (uint16)sval);
  2883. os_mutex_unlock(&memory->mem_lock);
  2884. }
  2885. else {
  2886. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2887. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2888. os_mutex_lock(&memory->mem_lock);
  2889. STORE_U32(maddr, sval);
  2890. os_mutex_unlock(&memory->mem_lock);
  2891. }
  2892. break;
  2893. }
  2894. case WASM_OP_ATOMIC_I64_STORE:
  2895. case WASM_OP_ATOMIC_I64_STORE8:
  2896. case WASM_OP_ATOMIC_I64_STORE16:
  2897. case WASM_OP_ATOMIC_I64_STORE32:
  2898. {
  2899. uint64 sval;
  2900. sval = (uint64)POP_I64();
  2901. addr = POP_I32();
  2902. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  2903. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2904. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2905. os_mutex_lock(&memory->mem_lock);
  2906. *(uint8 *)maddr = (uint8)sval;
  2907. os_mutex_unlock(&memory->mem_lock);
  2908. }
  2909. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  2910. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2911. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2912. os_mutex_lock(&memory->mem_lock);
  2913. STORE_U16(maddr, (uint16)sval);
  2914. os_mutex_unlock(&memory->mem_lock);
  2915. }
  2916. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  2917. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2918. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2919. os_mutex_lock(&memory->mem_lock);
  2920. STORE_U32(maddr, (uint32)sval);
  2921. os_mutex_unlock(&memory->mem_lock);
  2922. }
  2923. else {
  2924. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  2925. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2926. os_mutex_lock(&memory->mem_lock);
  2927. STORE_I64(maddr, sval);
  2928. os_mutex_unlock(&memory->mem_lock);
  2929. }
  2930. break;
  2931. }
  2932. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  2933. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  2934. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  2935. {
  2936. uint32 readv, sval, expect;
  2937. sval = POP_I32();
  2938. expect = POP_I32();
  2939. addr = POP_I32();
  2940. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  2941. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2942. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2943. os_mutex_lock(&memory->mem_lock);
  2944. readv = (uint32)(*(uint8 *)maddr);
  2945. if (readv == expect)
  2946. *(uint8 *)maddr = (uint8)(sval);
  2947. os_mutex_unlock(&memory->mem_lock);
  2948. }
  2949. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  2950. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2951. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2952. os_mutex_lock(&memory->mem_lock);
  2953. readv = (uint32)LOAD_U16(maddr);
  2954. if (readv == expect)
  2955. STORE_U16(maddr, (uint16)(sval));
  2956. os_mutex_unlock(&memory->mem_lock);
  2957. }
  2958. else {
  2959. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2960. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2961. os_mutex_lock(&memory->mem_lock);
  2962. readv = LOAD_I32(maddr);
  2963. if (readv == expect)
  2964. STORE_U32(maddr, sval);
  2965. os_mutex_unlock(&memory->mem_lock);
  2966. }
  2967. PUSH_I32(readv);
  2968. break;
  2969. }
  2970. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  2971. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  2972. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  2973. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  2974. {
  2975. uint64 readv, sval, expect;
  2976. sval = (uint64)POP_I64();
  2977. expect = (uint64)POP_I64();
  2978. addr = POP_I32();
  2979. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  2980. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
  2981. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2982. os_mutex_lock(&memory->mem_lock);
  2983. readv = (uint64)(*(uint8 *)maddr);
  2984. if (readv == expect)
  2985. *(uint8 *)maddr = (uint8)(sval);
  2986. os_mutex_unlock(&memory->mem_lock);
  2987. }
  2988. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  2989. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
  2990. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2991. os_mutex_lock(&memory->mem_lock);
  2992. readv = (uint64)LOAD_U16(maddr);
  2993. if (readv == expect)
  2994. STORE_U16(maddr, (uint16)(sval));
  2995. os_mutex_unlock(&memory->mem_lock);
  2996. }
  2997. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  2998. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
  2999. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3000. os_mutex_lock(&memory->mem_lock);
  3001. readv = (uint64)LOAD_U32(maddr);
  3002. if (readv == expect)
  3003. STORE_U32(maddr, (uint32)(sval));
  3004. os_mutex_unlock(&memory->mem_lock);
  3005. }
  3006. else {
  3007. CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
  3008. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3009. os_mutex_lock(&memory->mem_lock);
  3010. readv = (uint64)LOAD_I64(maddr);
  3011. if (readv == expect) {
  3012. STORE_I64(maddr, sval);
  3013. }
  3014. os_mutex_unlock(&memory->mem_lock);
  3015. }
  3016. PUSH_I64(readv);
  3017. break;
  3018. }
  3019. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  3020. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  3021. DEF_ATOMIC_RMW_OPCODE(AND, &);
  3022. DEF_ATOMIC_RMW_OPCODE(OR, |);
  3023. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  3024. /* xchg, ignore the read value, and store the given
  3025. value: readv * 0 + sval */
  3026. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  3027. }
  3028. HANDLE_OP_END();
  3029. }
  3030. #endif
  3031. HANDLE_OP(WASM_OP_IMPDEP)
  3032. {
  3033. frame = prev_frame;
  3034. frame_ip = frame->ip;
  3035. goto call_func_from_entry;
  3036. }
  3037. HANDLE_OP(WASM_OP_CALL)
  3038. {
  3039. #if WASM_ENABLE_THREAD_MGR != 0
  3040. CHECK_SUSPEND_FLAGS();
  3041. #endif
  3042. fidx = read_uint32(frame_ip);
  3043. #if WASM_ENABLE_MULTI_MODULE != 0
  3044. if (fidx >= module->function_count) {
  3045. wasm_set_exception(module, "unknown function");
  3046. goto got_exception;
  3047. }
  3048. #endif
  3049. cur_func = module->functions + fidx;
  3050. callee_module_inst = module;
  3051. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  3052. if (!wasm_program_resolve_op_call(program, module, &callee_module_inst, &cur_func)) {
  3053. goto got_exception;
  3054. }
  3055. #if 0
  3056. // lazy link, resolve the import function.
  3057. if (program && module->inst_id && cur_func->is_import_func &&
  3058. !cur_func->u.func_import->func_ptr_linked) {
  3059. if (!cur_func->import_module_inst &&
  3060. !cur_func->import_func_inst) {
  3061. if (program->config.binding_mode != LAZY_BINDING) {
  3062. wasm_set_exception(module, "uninitialized import function.");
  3063. goto got_exception;
  3064. }
  3065. if (!wasm_program_resolve_wasm_function(program, module, cur_func)) {
  3066. char buf[128];
  3067. snprintf(buf, sizeof(buf),
  3068. "failed to call unlinked import function (%s, %s)",
  3069. (char*)cur_func->u.func_import->module_name->str,
  3070. (char*)cur_func->u.func_import->field_name->str);
  3071. wasm_set_exception(module, buf);
  3072. goto got_exception;
  3073. }
  3074. callee_module_inst = cur_func->import_module_inst;
  3075. cur_func = cur_func->import_func_inst;
  3076. } else {
  3077. callee_module_inst = cur_func->import_module_inst;
  3078. cur_func = cur_func->import_func_inst;
  3079. }
  3080. }
  3081. #endif
  3082. #endif
  3083. cur_func_type = cur_func->func_type;
  3084. goto call_func_from_interp;
  3085. }
  3086. #if WASM_ENABLE_TAIL_CALL != 0
  3087. HANDLE_OP(WASM_OP_RETURN_CALL)
  3088. {
  3089. #if WASM_ENABLE_THREAD_MGR != 0
  3090. CHECK_SUSPEND_FLAGS();
  3091. #endif
  3092. fidx = read_uint32(frame_ip);
  3093. #if WASM_ENABLE_MULTI_MODULE != 0
  3094. if (fidx >= module->function_count) {
  3095. wasm_set_exception(module, "unknown function");
  3096. goto got_exception;
  3097. }
  3098. #endif
  3099. cur_func = module->functions + fidx;
  3100. cur_func_type = cur_func->func_type;
  3101. goto call_func_from_return_call;
  3102. }
  3103. #endif /* WASM_ENABLE_TAIL_CALL */
  3104. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3105. default:
  3106. wasm_set_exception(module, "unsupported opcode");
  3107. goto got_exception;
  3108. }
  3109. #endif
  3110. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3111. HANDLE_OP(WASM_OP_UNUSED_0x06)
  3112. HANDLE_OP(WASM_OP_UNUSED_0x07)
  3113. HANDLE_OP(WASM_OP_UNUSED_0x08)
  3114. HANDLE_OP(WASM_OP_UNUSED_0x09)
  3115. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  3116. #if WASM_ENABLE_TAIL_CALL == 0
  3117. HANDLE_OP(WASM_OP_RETURN_CALL)
  3118. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  3119. #endif
  3120. #if WASM_ENABLE_SHARED_MEMORY == 0
  3121. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  3122. #endif
  3123. #if WASM_ENABLE_REF_TYPES == 0
  3124. HANDLE_OP(WASM_OP_TABLE_GET)
  3125. HANDLE_OP(WASM_OP_TABLE_SET)
  3126. HANDLE_OP(WASM_OP_REF_NULL)
  3127. HANDLE_OP(WASM_OP_REF_IS_NULL)
  3128. HANDLE_OP(WASM_OP_REF_FUNC)
  3129. #endif
  3130. /* SELECT_T is converted to SELECT or SELECT_64 */
  3131. HANDLE_OP(WASM_OP_SELECT_T)
  3132. HANDLE_OP(WASM_OP_UNUSED_0x14)
  3133. HANDLE_OP(WASM_OP_UNUSED_0x15)
  3134. HANDLE_OP(WASM_OP_UNUSED_0x16)
  3135. HANDLE_OP(WASM_OP_UNUSED_0x17)
  3136. HANDLE_OP(WASM_OP_UNUSED_0x18)
  3137. HANDLE_OP(WASM_OP_UNUSED_0x19)
  3138. HANDLE_OP(WASM_OP_UNUSED_0x27)
  3139. /* optimized op code */
  3140. HANDLE_OP(WASM_OP_F32_STORE)
  3141. HANDLE_OP(WASM_OP_F64_STORE)
  3142. HANDLE_OP(WASM_OP_F32_LOAD)
  3143. HANDLE_OP(WASM_OP_F64_LOAD)
  3144. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  3145. HANDLE_OP(WASM_OP_GET_LOCAL)
  3146. HANDLE_OP(WASM_OP_F64_CONST)
  3147. HANDLE_OP(WASM_OP_I64_CONST)
  3148. HANDLE_OP(WASM_OP_F32_CONST)
  3149. HANDLE_OP(WASM_OP_I32_CONST)
  3150. HANDLE_OP(WASM_OP_DROP)
  3151. HANDLE_OP(WASM_OP_DROP_64)
  3152. HANDLE_OP(WASM_OP_BLOCK)
  3153. HANDLE_OP(WASM_OP_LOOP)
  3154. HANDLE_OP(WASM_OP_END)
  3155. HANDLE_OP(WASM_OP_NOP)
  3156. HANDLE_OP(EXT_OP_BLOCK)
  3157. HANDLE_OP(EXT_OP_LOOP)
  3158. HANDLE_OP(EXT_OP_IF)
  3159. {
  3160. wasm_set_exception(module, "unsupported opcode");
  3161. goto got_exception;
  3162. }
  3163. #endif
  3164. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3165. continue;
  3166. #else
  3167. FETCH_OPCODE_AND_DISPATCH();
  3168. #endif
  3169. #if WASM_ENABLE_TAIL_CALL != 0
  3170. call_func_from_return_call:
  3171. {
  3172. uint32 *lp_base;
  3173. uint32 *lp;
  3174. int i;
  3175. if (!(lp_base = lp = wasm_runtime_malloc(cur_func_type->param_cell_num
  3176. * sizeof(uint32)))) {
  3177. wasm_set_exception(module, "allocate memory failed");
  3178. goto got_exception;
  3179. }
  3180. for (i = 0; i < cur_func_type->param_count; i++) {
  3181. if (cur_func_type->types[i] == VALUE_TYPE_I64
  3182. || cur_func->types[i] == VALUE_TYPE_F64) {
  3183. PUT_I64_TO_ADDR(
  3184. lp, GET_OPERAND(uint64, I64,
  3185. 2 * (cur_func_type->param_count - i - 1)));
  3186. lp += 2;
  3187. }
  3188. else {
  3189. *lp = GET_OPERAND(uint32, I32,
  3190. (2 * (cur_func_type->param_count - i - 1)));
  3191. lp++;
  3192. }
  3193. }
  3194. frame->lp = frame->operand + cur_func->const_cell_num;
  3195. word_copy(frame->lp, lp_base, lp - lp_base);
  3196. wasm_runtime_free(lp_base);
  3197. FREE_FRAME(exec_env, frame);
  3198. frame_ip += cur_func_type->param_count * sizeof(int16);
  3199. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3200. goto call_func_from_entry;
  3201. }
  3202. #endif /* WASM_ENABLE_TAIL_CALL */
  3203. call_func_from_interp:
  3204. {
  3205. /* Only do the copy when it's called from interpreter. */
  3206. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3207. int i;
  3208. #if WASM_ENABLE_MULTI_MODULE != 0
  3209. if (cur_func->is_import_func) {
  3210. outs_area->lp = outs_area->operand
  3211. + (cur_func->import_func_inst
  3212. ? cur_func->import_func_inst->const_cell_num
  3213. : 0);
  3214. }
  3215. else
  3216. #endif
  3217. {
  3218. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  3219. }
  3220. for (i = 0; i < cur_func_type->param_count; i++) {
  3221. if (cur_func_type->types[i] == VALUE_TYPE_I64
  3222. || cur_func_type->types[i] == VALUE_TYPE_F64) {
  3223. PUT_I64_TO_ADDR(
  3224. outs_area->lp,
  3225. GET_OPERAND(uint64, I64,
  3226. 2 * (cur_func_type->param_count - i - 1)));
  3227. outs_area->lp += 2;
  3228. }
  3229. else {
  3230. *outs_area->lp = GET_OPERAND(
  3231. uint32, I32, (2 * (cur_func_type->param_count - i - 1)));
  3232. outs_area->lp++;
  3233. }
  3234. }
  3235. frame_ip += cur_func_type->param_count * sizeof(int16);
  3236. if (cur_func_type->ret_cell_num != 0) {
  3237. /* Get the first return value's offset. Since loader emit
  3238. * all return values' offset so we must skip remain return
  3239. * values' offsets.
  3240. */
  3241. WASMType *func_type;
  3242. if (cur_func->is_import_func)
  3243. func_type = cur_func->u.func_import->func_type;
  3244. else
  3245. func_type = cur_func->u.func->func_type;
  3246. frame->ret_offset = GET_OFFSET();
  3247. frame_ip += 2 * (func_type->result_count - 1);
  3248. }
  3249. SYNC_ALL_TO_FRAME();
  3250. prev_frame = frame;
  3251. }
  3252. call_func_from_entry:
  3253. {
  3254. #if WASM_ENABLE_DYNAMIC_LINKING != 0
  3255. if (callee_module_inst && module != callee_module_inst) {
  3256. wasm_interp_call_inter_module_func(module,
  3257. callee_module_inst,
  3258. exec_env,
  3259. cur_func,
  3260. prev_frame);
  3261. prev_frame = frame->prev_frame;
  3262. cur_func = frame->function;
  3263. cur_func_type = cur_func->func_type;
  3264. UPDATE_ALL_FROM_FRAME();
  3265. /* update memory instance ptr and memory size */
  3266. memory = module->default_memory;
  3267. if (memory)
  3268. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  3269. if (wasm_get_exception(callee_module_inst))
  3270. goto got_exception;
  3271. callee_module_inst = NULL;
  3272. } else
  3273. #endif
  3274. {
  3275. if (cur_func->is_import_func) {
  3276. #if WASM_ENABLE_MULTI_MODULE != 0
  3277. if (cur_func->import_func_inst) {
  3278. wasm_interp_call_func_import(module, exec_env, cur_func,
  3279. prev_frame);
  3280. }
  3281. else
  3282. #endif
  3283. {
  3284. wasm_interp_call_func_native(module, exec_env, cur_func,
  3285. prev_frame);
  3286. }
  3287. prev_frame = frame->prev_frame;
  3288. cur_func = frame->function;
  3289. cur_func_type = cur_func->func_type;
  3290. UPDATE_ALL_FROM_FRAME();
  3291. /* update memory instance ptr and memory size */
  3292. memory = module->default_memory;
  3293. if (memory)
  3294. linear_mem_size = num_bytes_per_page * memory->cur_page_count;
  3295. if (wasm_get_exception(module))
  3296. goto got_exception;
  3297. }
  3298. else {
  3299. WASMFunction *cur_wasm_func = cur_func->u.func;
  3300. all_cell_num = (uint64)cur_func_type->param_cell_num
  3301. + (uint64)cur_func->local_cell_num
  3302. + (uint64)cur_func->const_cell_num
  3303. + (uint64)cur_wasm_func->max_stack_cell_num;
  3304. if (all_cell_num >= UINT32_MAX) {
  3305. wasm_set_exception(module, "wasm operand stack overflow");
  3306. goto got_exception;
  3307. }
  3308. frame_size = wasm_interp_interp_frame_size((uint32)all_cell_num);
  3309. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  3310. frame = prev_frame;
  3311. goto got_exception;
  3312. }
  3313. /* Initialize the interpreter context. */
  3314. frame->function = cur_func;
  3315. frame_ip = wasm_get_func_code(cur_func);
  3316. frame_ip_end = wasm_get_func_code_end(cur_func);
  3317. frame_lp = frame->lp = frame->operand + cur_wasm_func->const_cell_num;
  3318. /* Initialize the consts */
  3319. word_copy(frame->operand, (uint32*)cur_wasm_func->consts,
  3320. cur_wasm_func->const_cell_num);
  3321. /* Initialize the local variables */
  3322. memset(frame_lp + cur_func_type->param_cell_num, 0,
  3323. (uint32)(cur_func->local_cell_num * 4));
  3324. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  3325. }
  3326. }
  3327. HANDLE_OP_END ();
  3328. }
  3329. return_func:
  3330. {
  3331. FREE_FRAME(exec_env, frame);
  3332. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3333. if (!prev_frame->ip)
  3334. /* Called from native. */
  3335. return;
  3336. RECOVER_CONTEXT(prev_frame);
  3337. HANDLE_OP_END();
  3338. }
  3339. (void)frame_ip_end;
  3340. #if WASM_ENABLE_SHARED_MEMORY != 0
  3341. unaligned_atomic:
  3342. wasm_set_exception(module, "unaligned atomic");
  3343. goto got_exception;
  3344. #endif
  3345. out_of_bounds:
  3346. wasm_set_exception(module, "out of bounds memory access");
  3347. got_exception:
  3348. SYNC_ALL_TO_FRAME();
  3349. return;
  3350. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3351. }
  3352. #else
  3353. FETCH_OPCODE_AND_DISPATCH();
  3354. #endif
  3355. }
  3356. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3357. void **
  3358. wasm_interp_get_handle_table()
  3359. {
  3360. WASMModuleInstance module;
  3361. memset(&module, 0, sizeof(WASMModuleInstance));
  3362. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  3363. return global_handle_table;
  3364. }
  3365. #endif
  3366. void
  3367. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  3368. WASMFunctionInstance *function, uint32 argc,
  3369. uint32 argv[])
  3370. {
  3371. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  3372. WASMInterpFrame *frame, *outs_area;
  3373. WASMType * func_type = function->func_type;
  3374. /* Allocate sufficient cells for all kinds of return values. */
  3375. unsigned all_cell_num =
  3376. func_type->ret_cell_num > 2 ? func_type->ret_cell_num : 2,
  3377. i;
  3378. /* This frame won't be used by JITed code, so only allocate interp
  3379. frame here. */
  3380. unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
  3381. if (argc != func_type->param_cell_num) {
  3382. char buf[128];
  3383. snprintf(buf, sizeof(buf), "invalid argument count %d, expected %d",
  3384. argc, func_type->param_cell_num);
  3385. wasm_set_exception(module_inst, buf);
  3386. return;
  3387. }
  3388. if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
  3389. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3390. "native stack overflow");
  3391. return;
  3392. }
  3393. if (!(frame =
  3394. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  3395. return;
  3396. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3397. frame->function = NULL;
  3398. frame->ip = NULL;
  3399. /* There is no local variable. */
  3400. frame->lp = frame->operand + 0;
  3401. frame->ret_offset = 0;
  3402. if (argc > 0)
  3403. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  3404. wasm_exec_env_set_cur_frame(exec_env, frame);
  3405. if (function->is_import_func) {
  3406. #if WASM_ENABLE_MULTI_MODULE != 0
  3407. if (function->import_module_inst) {
  3408. LOG_DEBUG("it is a function of a sub module");
  3409. wasm_interp_call_func_import(module_inst, exec_env, function,
  3410. frame);
  3411. }
  3412. else
  3413. #endif
  3414. {
  3415. LOG_DEBUG("it is an native function");
  3416. wasm_interp_call_func_native(module_inst, exec_env, function,
  3417. frame);
  3418. }
  3419. }
  3420. else {
  3421. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  3422. }
  3423. /* Output the return value to the caller */
  3424. if (!wasm_get_exception(module_inst)) {
  3425. for (i = 0; i < func_type->ret_cell_num; i++)
  3426. argv[i] = *(frame->lp + i);
  3427. }
  3428. else {
  3429. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3430. wasm_interp_dump_call_stack(exec_env);
  3431. #endif
  3432. }
  3433. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3434. FREE_FRAME(exec_env, frame);
  3435. #if WASM_ENABLE_OPCODE_COUNTER != 0
  3436. wasm_interp_dump_op_count();
  3437. #endif
  3438. }