wasm_interp_fast.c 141 KB

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