wasm_interp_fast.c 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073
  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() GET_LINEAR_MEMORY_SIZE(memory)
  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_MEMORY_OVERFLOW(1); \
  427. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  428. \
  429. shared_memory_lock(memory); \
  430. readv = (uint32)(*(uint8 *)maddr); \
  431. *(uint8 *)maddr = (uint8)(readv op sval); \
  432. shared_memory_unlock(memory); \
  433. } \
  434. else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
  435. CHECK_MEMORY_OVERFLOW(2); \
  436. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  437. \
  438. shared_memory_lock(memory); \
  439. readv = (uint32)LOAD_U16(maddr); \
  440. STORE_U16(maddr, (uint16)(readv op sval)); \
  441. shared_memory_unlock(memory); \
  442. } \
  443. else { \
  444. CHECK_MEMORY_OVERFLOW(4); \
  445. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  446. \
  447. shared_memory_lock(memory); \
  448. readv = LOAD_I32(maddr); \
  449. STORE_U32(maddr, readv op sval); \
  450. shared_memory_unlock(memory); \
  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_MEMORY_OVERFLOW(1); \
  467. CHECK_ATOMIC_MEMORY_ACCESS(1); \
  468. \
  469. shared_memory_lock(memory); \
  470. readv = (uint64)(*(uint8 *)maddr); \
  471. *(uint8 *)maddr = (uint8)(readv op sval); \
  472. shared_memory_unlock(memory); \
  473. } \
  474. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
  475. CHECK_MEMORY_OVERFLOW(2); \
  476. CHECK_ATOMIC_MEMORY_ACCESS(2); \
  477. \
  478. shared_memory_lock(memory); \
  479. readv = (uint64)LOAD_U16(maddr); \
  480. STORE_U16(maddr, (uint16)(readv op sval)); \
  481. shared_memory_unlock(memory); \
  482. } \
  483. else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
  484. CHECK_MEMORY_OVERFLOW(4); \
  485. CHECK_ATOMIC_MEMORY_ACCESS(4); \
  486. \
  487. shared_memory_lock(memory); \
  488. readv = (uint64)LOAD_U32(maddr); \
  489. STORE_U32(maddr, (uint32)(readv op sval)); \
  490. shared_memory_unlock(memory); \
  491. } \
  492. else { \
  493. uint64 op_result; \
  494. CHECK_MEMORY_OVERFLOW(8); \
  495. CHECK_ATOMIC_MEMORY_ACCESS(8); \
  496. \
  497. shared_memory_lock(memory); \
  498. readv = (uint64)LOAD_I64(maddr); \
  499. op_result = readv op sval; \
  500. STORE_I64(maddr, op_result); \
  501. shared_memory_unlock(memory); \
  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_thread_cputime_us();
  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. WASMInterpFrame *prev_frame = frame->prev_frame;
  815. uint64 elapsed = os_time_thread_cputime_us() - frame->time_started;
  816. frame->function->total_exec_time += elapsed;
  817. frame->function->total_exec_cnt++;
  818. if (prev_frame && prev_frame->function)
  819. prev_frame->function->children_exec_time += elapsed;
  820. }
  821. #endif
  822. wasm_exec_env_free_wasm_frame(exec_env, frame);
  823. }
  824. static void
  825. wasm_interp_call_func_native(WASMModuleInstance *module_inst,
  826. WASMExecEnv *exec_env,
  827. WASMFunctionInstance *cur_func,
  828. WASMInterpFrame *prev_frame)
  829. {
  830. WASMFunctionImport *func_import = cur_func->u.func_import;
  831. CApiFuncImport *c_api_func_import = NULL;
  832. unsigned local_cell_num = 2;
  833. WASMInterpFrame *frame;
  834. uint32 argv_ret[2], cur_func_index;
  835. void *native_func_pointer = NULL;
  836. bool ret;
  837. if (!(frame = ALLOC_FRAME(exec_env,
  838. wasm_interp_interp_frame_size(local_cell_num),
  839. prev_frame)))
  840. return;
  841. frame->function = cur_func;
  842. frame->ip = NULL;
  843. frame->lp = frame->operand;
  844. wasm_exec_env_set_cur_frame(exec_env, frame);
  845. cur_func_index = (uint32)(cur_func - module_inst->e->functions);
  846. bh_assert(cur_func_index < module_inst->module->import_function_count);
  847. if (!func_import->call_conv_wasm_c_api) {
  848. native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
  849. }
  850. else if (module_inst->e->common.c_api_func_imports) {
  851. c_api_func_import =
  852. module_inst->e->common.c_api_func_imports + cur_func_index;
  853. native_func_pointer = c_api_func_import->func_ptr_linked;
  854. }
  855. if (!native_func_pointer) {
  856. char buf[128];
  857. snprintf(buf, sizeof(buf),
  858. "failed to call unlinked import function (%s, %s)",
  859. func_import->module_name, func_import->field_name);
  860. wasm_set_exception((WASMModuleInstance *)module_inst, buf);
  861. return;
  862. }
  863. if (func_import->call_conv_wasm_c_api) {
  864. ret = wasm_runtime_invoke_c_api_native(
  865. (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
  866. func_import->func_type, cur_func->param_cell_num, frame->lp,
  867. c_api_func_import->with_env_arg, c_api_func_import->env_arg);
  868. if (ret) {
  869. argv_ret[0] = frame->lp[0];
  870. argv_ret[1] = frame->lp[1];
  871. }
  872. }
  873. else if (!func_import->call_conv_raw) {
  874. ret = wasm_runtime_invoke_native(
  875. exec_env, native_func_pointer, func_import->func_type,
  876. func_import->signature, func_import->attachment, frame->lp,
  877. cur_func->param_cell_num, argv_ret);
  878. }
  879. else {
  880. ret = wasm_runtime_invoke_native_raw(
  881. exec_env, native_func_pointer, func_import->func_type,
  882. func_import->signature, func_import->attachment, frame->lp,
  883. cur_func->param_cell_num, argv_ret);
  884. }
  885. if (!ret)
  886. return;
  887. if (cur_func->ret_cell_num == 1) {
  888. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  889. }
  890. else if (cur_func->ret_cell_num == 2) {
  891. prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
  892. prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
  893. }
  894. FREE_FRAME(exec_env, frame);
  895. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  896. }
  897. #if WASM_ENABLE_MULTI_MODULE != 0
  898. static void
  899. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  900. WASMExecEnv *exec_env,
  901. WASMFunctionInstance *cur_func,
  902. WASMInterpFrame *prev_frame);
  903. static void
  904. wasm_interp_call_func_import(WASMModuleInstance *module_inst,
  905. WASMExecEnv *exec_env,
  906. WASMFunctionInstance *cur_func,
  907. WASMInterpFrame *prev_frame)
  908. {
  909. WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
  910. WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
  911. WASMFunctionImport *func_import = cur_func->u.func_import;
  912. uint8 *ip = prev_frame->ip;
  913. char buf[128];
  914. WASMExecEnv *sub_module_exec_env = NULL;
  915. uint32 aux_stack_origin_boundary = 0;
  916. uint32 aux_stack_origin_bottom = 0;
  917. if (!sub_func_inst) {
  918. snprintf(buf, sizeof(buf),
  919. "failed to call unlinked import function (%s, %s)",
  920. func_import->module_name, func_import->field_name);
  921. wasm_set_exception(module_inst, buf);
  922. return;
  923. }
  924. /* Switch exec_env but keep using the same one by replacing necessary
  925. * variables */
  926. sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
  927. (WASMModuleInstanceCommon *)sub_module_inst);
  928. if (!sub_module_exec_env) {
  929. wasm_set_exception(module_inst, "create singleton exec_env failed");
  930. return;
  931. }
  932. /* - module_inst */
  933. wasm_exec_env_set_module_inst(exec_env,
  934. (WASMModuleInstanceCommon *)sub_module_inst);
  935. /* - aux_stack_boundary */
  936. aux_stack_origin_boundary = exec_env->aux_stack_boundary.boundary;
  937. exec_env->aux_stack_boundary.boundary =
  938. sub_module_exec_env->aux_stack_boundary.boundary;
  939. /* - aux_stack_bottom */
  940. aux_stack_origin_bottom = exec_env->aux_stack_bottom.bottom;
  941. exec_env->aux_stack_bottom.bottom =
  942. sub_module_exec_env->aux_stack_bottom.bottom;
  943. /* set ip NULL to make call_func_bytecode return after executing
  944. this function */
  945. prev_frame->ip = NULL;
  946. /* call function of sub-module*/
  947. wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
  948. prev_frame);
  949. /* restore ip and other replaced */
  950. prev_frame->ip = ip;
  951. exec_env->aux_stack_boundary.boundary = aux_stack_origin_boundary;
  952. exec_env->aux_stack_bottom.bottom = aux_stack_origin_bottom;
  953. wasm_exec_env_restore_module_inst(exec_env,
  954. (WASMModuleInstanceCommon *)module_inst);
  955. }
  956. #endif
  957. #if WASM_ENABLE_THREAD_MGR != 0
  958. #define CHECK_SUSPEND_FLAGS() \
  959. do { \
  960. WASM_SUSPEND_FLAGS_LOCK(exec_env->wait_lock); \
  961. if (WASM_SUSPEND_FLAGS_GET(exec_env->suspend_flags) \
  962. & WASM_SUSPEND_FLAG_TERMINATE) { \
  963. /* terminate current thread */ \
  964. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  965. return; \
  966. } \
  967. /* TODO: support suspend and breakpoint */ \
  968. WASM_SUSPEND_FLAGS_UNLOCK(exec_env->wait_lock); \
  969. } while (0)
  970. #endif
  971. #if WASM_ENABLE_OPCODE_COUNTER != 0
  972. typedef struct OpcodeInfo {
  973. char *name;
  974. uint64 count;
  975. } OpcodeInfo;
  976. /* clang-format off */
  977. #define HANDLE_OPCODE(op) \
  978. { \
  979. #op, 0 \
  980. }
  981. DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
  982. #undef HANDLE_OPCODE
  983. /* clang-format on */
  984. static void
  985. wasm_interp_dump_op_count()
  986. {
  987. uint32 i;
  988. uint64 total_count = 0;
  989. for (i = 0; i < WASM_OP_IMPDEP; i++)
  990. total_count += opcode_table[i].count;
  991. printf("total opcode count: %ld\n", total_count);
  992. for (i = 0; i < WASM_OP_IMPDEP; i++)
  993. if (opcode_table[i].count > 0)
  994. printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
  995. opcode_table[i].count,
  996. opcode_table[i].count * 100.0f / total_count);
  997. }
  998. #endif
  999. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1000. /* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
  1001. #if WASM_ENABLE_OPCODE_COUNTER != 0
  1002. #define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
  1003. #else
  1004. #define HANDLE_OP(opcode) HANDLE_##opcode:
  1005. #endif
  1006. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1007. #define FETCH_OPCODE_AND_DISPATCH() \
  1008. do { \
  1009. const void *p_label_addr = *(void **)frame_ip; \
  1010. frame_ip += sizeof(void *); \
  1011. goto *p_label_addr; \
  1012. } while (0)
  1013. #else
  1014. #if UINTPTR_MAX == UINT64_MAX
  1015. #define FETCH_OPCODE_AND_DISPATCH() \
  1016. do { \
  1017. const void *p_label_addr; \
  1018. bh_assert(((uintptr_t)frame_ip & 1) == 0); \
  1019. /* int32 relative offset was emitted in 64-bit target */ \
  1020. p_label_addr = label_base + (int32)LOAD_U32_WITH_2U16S(frame_ip); \
  1021. frame_ip += sizeof(int32); \
  1022. goto *p_label_addr; \
  1023. } while (0)
  1024. #else
  1025. #define FETCH_OPCODE_AND_DISPATCH() \
  1026. do { \
  1027. const void *p_label_addr; \
  1028. bh_assert(((uintptr_t)frame_ip & 1) == 0); \
  1029. /* uint32 label address was emitted in 32-bit target */ \
  1030. p_label_addr = (void *)(uintptr_t)LOAD_U32_WITH_2U16S(frame_ip); \
  1031. frame_ip += sizeof(int32); \
  1032. goto *p_label_addr; \
  1033. } while (0)
  1034. #endif
  1035. #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
  1036. #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
  1037. #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
  1038. #define HANDLE_OP(opcode) case opcode:
  1039. #define HANDLE_OP_END() continue
  1040. #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
  1041. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1042. static void **global_handle_table;
  1043. #endif
  1044. static inline uint8 *
  1045. get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
  1046. {
  1047. #if WASM_ENABLE_MULTI_MODULE == 0
  1048. return global_data + global->data_offset;
  1049. #else
  1050. return global->import_global_inst
  1051. ? global->import_module_inst->global_data
  1052. + global->import_global_inst->data_offset
  1053. : global_data + global->data_offset;
  1054. #endif
  1055. }
  1056. static void
  1057. wasm_interp_call_func_bytecode(WASMModuleInstance *module,
  1058. WASMExecEnv *exec_env,
  1059. WASMFunctionInstance *cur_func,
  1060. WASMInterpFrame *prev_frame)
  1061. {
  1062. WASMMemoryInstance *memory = wasm_get_default_memory(module);
  1063. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1064. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1065. || WASM_ENABLE_BULK_MEMORY != 0
  1066. uint32 linear_mem_size = 0;
  1067. if (memory)
  1068. #if WASM_ENABLE_THREAD_MGR == 0
  1069. linear_mem_size = memory->memory_data_size;
  1070. #else
  1071. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  1072. #endif
  1073. #endif
  1074. WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
  1075. WASMGlobalInstance *global;
  1076. uint8 *global_data = module->global_data;
  1077. uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
  1078. WASMInterpFrame *frame = NULL;
  1079. /* Points to this special opcode so as to jump to the
  1080. * call_method_from_entry. */
  1081. register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
  1082. register uint32 *frame_lp = NULL; /* cache of frame->lp */
  1083. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1084. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 && UINTPTR_MAX == UINT64_MAX
  1085. /* cache of label base addr */
  1086. register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
  1087. #endif
  1088. #endif
  1089. uint8 *frame_ip_end = frame_ip + 1;
  1090. uint32 cond, count, fidx, tidx, frame_size = 0;
  1091. uint32 all_cell_num = 0;
  1092. int16 addr1, addr2, addr_ret = 0;
  1093. int32 didx, val;
  1094. uint8 *maddr = NULL;
  1095. uint32 local_idx, local_offset, global_idx;
  1096. uint8 opcode, local_type, *global_addr;
  1097. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1098. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1099. #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
  1100. bool disable_bounds_checks = !wasm_runtime_is_bounds_checks_enabled(
  1101. (WASMModuleInstanceCommon *)module);
  1102. #else
  1103. bool disable_bounds_checks = false;
  1104. #endif
  1105. #endif
  1106. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  1107. #define HANDLE_OPCODE(op) &&HANDLE_##op
  1108. DEFINE_GOTO_TABLE(const void *, handle_table);
  1109. #undef HANDLE_OPCODE
  1110. if (exec_env == NULL) {
  1111. global_handle_table = (void **)handle_table;
  1112. return;
  1113. }
  1114. #endif
  1115. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  1116. while (frame_ip < frame_ip_end) {
  1117. opcode = *frame_ip++;
  1118. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
  1119. frame_ip++;
  1120. #endif
  1121. switch (opcode) {
  1122. #else
  1123. goto *handle_table[WASM_OP_IMPDEP];
  1124. #endif
  1125. /* control instructions */
  1126. HANDLE_OP(WASM_OP_UNREACHABLE)
  1127. {
  1128. wasm_set_exception(module, "unreachable");
  1129. goto got_exception;
  1130. }
  1131. HANDLE_OP(WASM_OP_IF)
  1132. {
  1133. cond = (uint32)POP_I32();
  1134. if (cond == 0) {
  1135. uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
  1136. if (else_addr == NULL) {
  1137. frame_ip =
  1138. (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
  1139. }
  1140. else {
  1141. frame_ip = else_addr;
  1142. }
  1143. }
  1144. else {
  1145. frame_ip += sizeof(uint8 *) * 2;
  1146. }
  1147. HANDLE_OP_END();
  1148. }
  1149. HANDLE_OP(WASM_OP_ELSE)
  1150. {
  1151. frame_ip = (uint8 *)LOAD_PTR(frame_ip);
  1152. HANDLE_OP_END();
  1153. }
  1154. HANDLE_OP(WASM_OP_BR)
  1155. {
  1156. #if WASM_ENABLE_THREAD_MGR != 0
  1157. CHECK_SUSPEND_FLAGS();
  1158. #endif
  1159. recover_br_info:
  1160. RECOVER_BR_INFO();
  1161. HANDLE_OP_END();
  1162. }
  1163. HANDLE_OP(WASM_OP_BR_IF)
  1164. {
  1165. #if WASM_ENABLE_THREAD_MGR != 0
  1166. CHECK_SUSPEND_FLAGS();
  1167. #endif
  1168. cond = frame_lp[GET_OFFSET()];
  1169. if (cond)
  1170. goto recover_br_info;
  1171. else
  1172. SKIP_BR_INFO();
  1173. HANDLE_OP_END();
  1174. }
  1175. HANDLE_OP(WASM_OP_BR_TABLE)
  1176. {
  1177. uint32 arity, br_item_size;
  1178. #if WASM_ENABLE_THREAD_MGR != 0
  1179. CHECK_SUSPEND_FLAGS();
  1180. #endif
  1181. count = read_uint32(frame_ip);
  1182. didx = GET_OPERAND(uint32, I32, 0);
  1183. frame_ip += 2;
  1184. if (!(didx >= 0 && (uint32)didx < count))
  1185. didx = count;
  1186. /* all br items must have the same arity and item size,
  1187. so we only calculate the first item size */
  1188. arity = LOAD_U32_WITH_2U16S(frame_ip);
  1189. br_item_size = sizeof(uint32); /* arity */
  1190. if (arity) {
  1191. /* total cell num */
  1192. br_item_size += sizeof(uint32);
  1193. /* cells, src offsets and dst offsets */
  1194. br_item_size +=
  1195. (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
  1196. }
  1197. /* target address */
  1198. br_item_size += sizeof(uint8 *);
  1199. frame_ip += br_item_size * didx;
  1200. goto recover_br_info;
  1201. }
  1202. HANDLE_OP(WASM_OP_RETURN)
  1203. {
  1204. uint32 ret_idx;
  1205. WASMType *func_type;
  1206. uint32 off, ret_offset;
  1207. uint8 *ret_types;
  1208. if (cur_func->is_import_func)
  1209. func_type = cur_func->u.func_import->func_type;
  1210. else
  1211. func_type = cur_func->u.func->func_type;
  1212. /* types of each return value */
  1213. ret_types = func_type->types + func_type->param_count;
  1214. ret_offset = prev_frame->ret_offset;
  1215. for (ret_idx = 0,
  1216. off = sizeof(int16) * (func_type->result_count - 1);
  1217. ret_idx < func_type->result_count;
  1218. ret_idx++, off -= sizeof(int16)) {
  1219. if (ret_types[ret_idx] == VALUE_TYPE_I64
  1220. || ret_types[ret_idx] == VALUE_TYPE_F64) {
  1221. PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
  1222. GET_OPERAND(uint64, I64, off));
  1223. ret_offset += 2;
  1224. }
  1225. else {
  1226. prev_frame->lp[ret_offset] =
  1227. GET_OPERAND(uint32, I32, off);
  1228. ret_offset++;
  1229. }
  1230. }
  1231. goto return_func;
  1232. }
  1233. HANDLE_OP(WASM_OP_CALL_INDIRECT)
  1234. #if WASM_ENABLE_TAIL_CALL != 0
  1235. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  1236. #endif
  1237. {
  1238. WASMType *cur_type, *cur_func_type;
  1239. WASMTableInstance *tbl_inst;
  1240. uint32 tbl_idx;
  1241. #if WASM_ENABLE_TAIL_CALL != 0
  1242. GET_OPCODE();
  1243. #endif
  1244. #if WASM_ENABLE_THREAD_MGR != 0
  1245. CHECK_SUSPEND_FLAGS();
  1246. #endif
  1247. tidx = read_uint32(frame_ip);
  1248. cur_type = module->module->types[tidx];
  1249. tbl_idx = read_uint32(frame_ip);
  1250. bh_assert(tbl_idx < module->table_count);
  1251. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1252. val = GET_OPERAND(uint32, I32, 0);
  1253. frame_ip += 2;
  1254. if ((uint32)val >= tbl_inst->cur_size) {
  1255. wasm_set_exception(module, "undefined element");
  1256. goto got_exception;
  1257. }
  1258. fidx = tbl_inst->elems[val];
  1259. if (fidx == NULL_REF) {
  1260. wasm_set_exception(module, "uninitialized element");
  1261. goto got_exception;
  1262. }
  1263. /*
  1264. * we might be using a table injected by host or
  1265. * another module. in that case, we don't validate
  1266. * the elem value while loading
  1267. */
  1268. if (fidx >= module->e->function_count) {
  1269. wasm_set_exception(module, "unknown function");
  1270. goto got_exception;
  1271. }
  1272. /* always call module own functions */
  1273. cur_func = module->e->functions + fidx;
  1274. if (cur_func->is_import_func)
  1275. cur_func_type = cur_func->u.func_import->func_type;
  1276. else
  1277. cur_func_type = cur_func->u.func->func_type;
  1278. if (cur_type != cur_func_type) {
  1279. wasm_set_exception(module, "indirect call type mismatch");
  1280. goto got_exception;
  1281. }
  1282. #if WASM_ENABLE_TAIL_CALL != 0
  1283. if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
  1284. goto call_func_from_return_call;
  1285. #endif
  1286. goto call_func_from_interp;
  1287. }
  1288. #if WASM_ENABLE_EXCE_HANDLING != 0
  1289. HANDLE_OP(WASM_OP_TRY)
  1290. HANDLE_OP(WASM_OP_CATCH)
  1291. HANDLE_OP(WASM_OP_THROW)
  1292. HANDLE_OP(WASM_OP_RETHROW)
  1293. HANDLE_OP(WASM_OP_DELEGATE)
  1294. HANDLE_OP(WASM_OP_CATCH_ALL)
  1295. HANDLE_OP(EXT_OP_TRY)
  1296. {
  1297. wasm_set_exception(module, "unsupported opcode");
  1298. goto got_exception;
  1299. }
  1300. #endif
  1301. /* parametric instructions */
  1302. HANDLE_OP(WASM_OP_SELECT)
  1303. {
  1304. cond = frame_lp[GET_OFFSET()];
  1305. addr1 = GET_OFFSET();
  1306. addr2 = GET_OFFSET();
  1307. addr_ret = GET_OFFSET();
  1308. if (!cond) {
  1309. if (addr_ret != addr1)
  1310. frame_lp[addr_ret] = frame_lp[addr1];
  1311. }
  1312. else {
  1313. if (addr_ret != addr2)
  1314. frame_lp[addr_ret] = frame_lp[addr2];
  1315. }
  1316. HANDLE_OP_END();
  1317. }
  1318. HANDLE_OP(WASM_OP_SELECT_64)
  1319. {
  1320. cond = frame_lp[GET_OFFSET()];
  1321. addr1 = GET_OFFSET();
  1322. addr2 = GET_OFFSET();
  1323. addr_ret = GET_OFFSET();
  1324. if (!cond) {
  1325. if (addr_ret != addr1)
  1326. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1327. GET_I64_FROM_ADDR(frame_lp + addr1));
  1328. }
  1329. else {
  1330. if (addr_ret != addr2)
  1331. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1332. GET_I64_FROM_ADDR(frame_lp + addr2));
  1333. }
  1334. HANDLE_OP_END();
  1335. }
  1336. #if WASM_ENABLE_REF_TYPES != 0
  1337. HANDLE_OP(WASM_OP_TABLE_GET)
  1338. {
  1339. uint32 tbl_idx, elem_idx;
  1340. WASMTableInstance *tbl_inst;
  1341. tbl_idx = read_uint32(frame_ip);
  1342. bh_assert(tbl_idx < module->table_count);
  1343. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1344. elem_idx = POP_I32();
  1345. if (elem_idx >= tbl_inst->cur_size) {
  1346. wasm_set_exception(module, "out of bounds table access");
  1347. goto got_exception;
  1348. }
  1349. PUSH_I32(tbl_inst->elems[elem_idx]);
  1350. HANDLE_OP_END();
  1351. }
  1352. HANDLE_OP(WASM_OP_TABLE_SET)
  1353. {
  1354. uint32 tbl_idx, elem_idx, elem_val;
  1355. WASMTableInstance *tbl_inst;
  1356. tbl_idx = read_uint32(frame_ip);
  1357. bh_assert(tbl_idx < module->table_count);
  1358. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  1359. elem_val = POP_I32();
  1360. elem_idx = POP_I32();
  1361. if (elem_idx >= tbl_inst->cur_size) {
  1362. wasm_set_exception(module, "out of bounds table access");
  1363. goto got_exception;
  1364. }
  1365. tbl_inst->elems[elem_idx] = elem_val;
  1366. HANDLE_OP_END();
  1367. }
  1368. HANDLE_OP(WASM_OP_REF_NULL)
  1369. {
  1370. PUSH_I32(NULL_REF);
  1371. HANDLE_OP_END();
  1372. }
  1373. HANDLE_OP(WASM_OP_REF_IS_NULL)
  1374. {
  1375. uint32 ref_val;
  1376. ref_val = POP_I32();
  1377. PUSH_I32(ref_val == NULL_REF ? 1 : 0);
  1378. HANDLE_OP_END();
  1379. }
  1380. HANDLE_OP(WASM_OP_REF_FUNC)
  1381. {
  1382. uint32 func_idx = read_uint32(frame_ip);
  1383. PUSH_I32(func_idx);
  1384. HANDLE_OP_END();
  1385. }
  1386. #endif /* WASM_ENABLE_REF_TYPES */
  1387. /* variable instructions */
  1388. HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
  1389. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
  1390. {
  1391. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1392. local_offset = *frame_ip++;
  1393. #else
  1394. /* clang-format off */
  1395. local_offset = *frame_ip;
  1396. frame_ip += 2;
  1397. /* clang-format on */
  1398. #endif
  1399. *(uint32 *)(frame_lp + local_offset) =
  1400. GET_OPERAND(uint32, I32, 0);
  1401. frame_ip += 2;
  1402. HANDLE_OP_END();
  1403. }
  1404. HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
  1405. HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
  1406. {
  1407. #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
  1408. local_offset = *frame_ip++;
  1409. #else
  1410. /* clang-format off */
  1411. local_offset = *frame_ip;
  1412. frame_ip += 2;
  1413. /* clang-format on */
  1414. #endif
  1415. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  1416. GET_OPERAND(uint64, I64, 0));
  1417. frame_ip += 2;
  1418. HANDLE_OP_END();
  1419. }
  1420. HANDLE_OP(WASM_OP_GET_GLOBAL)
  1421. {
  1422. global_idx = read_uint32(frame_ip);
  1423. bh_assert(global_idx < module->e->global_count);
  1424. global = globals + global_idx;
  1425. global_addr = get_global_addr(global_data, global);
  1426. addr_ret = GET_OFFSET();
  1427. frame_lp[addr_ret] = *(uint32 *)global_addr;
  1428. HANDLE_OP_END();
  1429. }
  1430. HANDLE_OP(WASM_OP_GET_GLOBAL_64)
  1431. {
  1432. global_idx = read_uint32(frame_ip);
  1433. bh_assert(global_idx < module->e->global_count);
  1434. global = globals + global_idx;
  1435. global_addr = get_global_addr(global_data, global);
  1436. addr_ret = GET_OFFSET();
  1437. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1438. GET_I64_FROM_ADDR((uint32 *)global_addr));
  1439. HANDLE_OP_END();
  1440. }
  1441. HANDLE_OP(WASM_OP_SET_GLOBAL)
  1442. {
  1443. global_idx = read_uint32(frame_ip);
  1444. bh_assert(global_idx < module->e->global_count);
  1445. global = globals + global_idx;
  1446. global_addr = get_global_addr(global_data, global);
  1447. addr1 = GET_OFFSET();
  1448. *(int32 *)global_addr = frame_lp[addr1];
  1449. HANDLE_OP_END();
  1450. }
  1451. HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
  1452. {
  1453. uint32 aux_stack_top;
  1454. global_idx = read_uint32(frame_ip);
  1455. bh_assert(global_idx < module->e->global_count);
  1456. global = globals + global_idx;
  1457. global_addr = get_global_addr(global_data, global);
  1458. aux_stack_top = frame_lp[GET_OFFSET()];
  1459. if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
  1460. wasm_set_exception(module, "wasm auxiliary stack overflow");
  1461. goto got_exception;
  1462. }
  1463. if (aux_stack_top > exec_env->aux_stack_bottom.bottom) {
  1464. wasm_set_exception(module,
  1465. "wasm auxiliary stack underflow");
  1466. goto got_exception;
  1467. }
  1468. *(int32 *)global_addr = aux_stack_top;
  1469. #if WASM_ENABLE_MEMORY_PROFILING != 0
  1470. if (module->module->aux_stack_top_global_index != (uint32)-1) {
  1471. uint32 aux_stack_used = module->module->aux_stack_bottom
  1472. - *(uint32 *)global_addr;
  1473. if (aux_stack_used > module->e->max_aux_stack_used)
  1474. module->e->max_aux_stack_used = aux_stack_used;
  1475. }
  1476. #endif
  1477. HANDLE_OP_END();
  1478. }
  1479. HANDLE_OP(WASM_OP_SET_GLOBAL_64)
  1480. {
  1481. global_idx = read_uint32(frame_ip);
  1482. bh_assert(global_idx < module->e->global_count);
  1483. global = globals + global_idx;
  1484. global_addr = get_global_addr(global_data, global);
  1485. addr1 = GET_OFFSET();
  1486. PUT_I64_TO_ADDR((uint32 *)global_addr,
  1487. GET_I64_FROM_ADDR(frame_lp + addr1));
  1488. HANDLE_OP_END();
  1489. }
  1490. /* memory load instructions */
  1491. HANDLE_OP(WASM_OP_I32_LOAD)
  1492. {
  1493. uint32 offset, addr;
  1494. offset = read_uint32(frame_ip);
  1495. addr = GET_OPERAND(uint32, I32, 0);
  1496. frame_ip += 2;
  1497. addr_ret = GET_OFFSET();
  1498. CHECK_MEMORY_OVERFLOW(4);
  1499. frame_lp[addr_ret] = LOAD_I32(maddr);
  1500. HANDLE_OP_END();
  1501. }
  1502. HANDLE_OP(WASM_OP_I64_LOAD)
  1503. {
  1504. uint32 offset, addr;
  1505. offset = read_uint32(frame_ip);
  1506. addr = GET_OPERAND(uint32, I32, 0);
  1507. frame_ip += 2;
  1508. addr_ret = GET_OFFSET();
  1509. CHECK_MEMORY_OVERFLOW(8);
  1510. PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
  1511. HANDLE_OP_END();
  1512. }
  1513. HANDLE_OP(WASM_OP_I32_LOAD8_S)
  1514. {
  1515. uint32 offset, addr;
  1516. offset = read_uint32(frame_ip);
  1517. addr = GET_OPERAND(uint32, I32, 0);
  1518. frame_ip += 2;
  1519. addr_ret = GET_OFFSET();
  1520. CHECK_MEMORY_OVERFLOW(1);
  1521. frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
  1522. HANDLE_OP_END();
  1523. }
  1524. HANDLE_OP(WASM_OP_I32_LOAD8_U)
  1525. {
  1526. uint32 offset, addr;
  1527. offset = read_uint32(frame_ip);
  1528. addr = GET_OPERAND(uint32, I32, 0);
  1529. frame_ip += 2;
  1530. addr_ret = GET_OFFSET();
  1531. CHECK_MEMORY_OVERFLOW(1);
  1532. frame_lp[addr_ret] = (uint32)(*(uint8 *)(maddr));
  1533. HANDLE_OP_END();
  1534. }
  1535. HANDLE_OP(WASM_OP_I32_LOAD16_S)
  1536. {
  1537. uint32 offset, addr;
  1538. offset = read_uint32(frame_ip);
  1539. addr = GET_OPERAND(uint32, I32, 0);
  1540. frame_ip += 2;
  1541. addr_ret = GET_OFFSET();
  1542. CHECK_MEMORY_OVERFLOW(2);
  1543. frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
  1544. HANDLE_OP_END();
  1545. }
  1546. HANDLE_OP(WASM_OP_I32_LOAD16_U)
  1547. {
  1548. uint32 offset, addr;
  1549. offset = read_uint32(frame_ip);
  1550. addr = GET_OPERAND(uint32, I32, 0);
  1551. frame_ip += 2;
  1552. addr_ret = GET_OFFSET();
  1553. CHECK_MEMORY_OVERFLOW(2);
  1554. frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
  1555. HANDLE_OP_END();
  1556. }
  1557. HANDLE_OP(WASM_OP_I64_LOAD8_S)
  1558. {
  1559. uint32 offset, addr;
  1560. offset = read_uint32(frame_ip);
  1561. addr = GET_OPERAND(uint32, I32, 0);
  1562. frame_ip += 2;
  1563. addr_ret = GET_OFFSET();
  1564. CHECK_MEMORY_OVERFLOW(1);
  1565. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1566. sign_ext_8_64(*(int8 *)maddr));
  1567. HANDLE_OP_END();
  1568. }
  1569. HANDLE_OP(WASM_OP_I64_LOAD8_U)
  1570. {
  1571. uint32 offset, addr;
  1572. offset = read_uint32(frame_ip);
  1573. addr = GET_OPERAND(uint32, I32, 0);
  1574. frame_ip += 2;
  1575. addr_ret = GET_OFFSET();
  1576. CHECK_MEMORY_OVERFLOW(1);
  1577. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
  1578. HANDLE_OP_END();
  1579. }
  1580. HANDLE_OP(WASM_OP_I64_LOAD16_S)
  1581. {
  1582. uint32 offset, addr;
  1583. offset = read_uint32(frame_ip);
  1584. addr = GET_OPERAND(uint32, I32, 0);
  1585. frame_ip += 2;
  1586. addr_ret = GET_OFFSET();
  1587. CHECK_MEMORY_OVERFLOW(2);
  1588. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1589. sign_ext_16_64(LOAD_I16(maddr)));
  1590. HANDLE_OP_END();
  1591. }
  1592. HANDLE_OP(WASM_OP_I64_LOAD16_U)
  1593. {
  1594. uint32 offset, addr;
  1595. offset = read_uint32(frame_ip);
  1596. addr = GET_OPERAND(uint32, I32, 0);
  1597. frame_ip += 2;
  1598. addr_ret = GET_OFFSET();
  1599. CHECK_MEMORY_OVERFLOW(2);
  1600. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
  1601. HANDLE_OP_END();
  1602. }
  1603. HANDLE_OP(WASM_OP_I64_LOAD32_S)
  1604. {
  1605. uint32 offset, addr;
  1606. offset = read_uint32(frame_ip);
  1607. addr = GET_OPERAND(uint32, I32, 0);
  1608. frame_ip += 2;
  1609. addr_ret = GET_OFFSET();
  1610. CHECK_MEMORY_OVERFLOW(4);
  1611. PUT_I64_TO_ADDR(frame_lp + addr_ret,
  1612. sign_ext_32_64(LOAD_I32(maddr)));
  1613. HANDLE_OP_END();
  1614. }
  1615. HANDLE_OP(WASM_OP_I64_LOAD32_U)
  1616. {
  1617. uint32 offset, addr;
  1618. offset = read_uint32(frame_ip);
  1619. addr = GET_OPERAND(uint32, I32, 0);
  1620. frame_ip += 2;
  1621. addr_ret = GET_OFFSET();
  1622. CHECK_MEMORY_OVERFLOW(4);
  1623. PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
  1624. HANDLE_OP_END();
  1625. }
  1626. HANDLE_OP(WASM_OP_I32_STORE)
  1627. {
  1628. uint32 offset, addr;
  1629. uint32 sval;
  1630. offset = read_uint32(frame_ip);
  1631. sval = GET_OPERAND(uint32, I32, 0);
  1632. addr = GET_OPERAND(uint32, I32, 2);
  1633. frame_ip += 4;
  1634. CHECK_MEMORY_OVERFLOW(4);
  1635. STORE_U32(maddr, sval);
  1636. HANDLE_OP_END();
  1637. }
  1638. HANDLE_OP(WASM_OP_I32_STORE8)
  1639. {
  1640. uint32 offset, addr;
  1641. uint32 sval;
  1642. offset = read_uint32(frame_ip);
  1643. sval = GET_OPERAND(uint32, I32, 0);
  1644. addr = GET_OPERAND(uint32, I32, 2);
  1645. frame_ip += 4;
  1646. CHECK_MEMORY_OVERFLOW(1);
  1647. STORE_U8(maddr, (uint8_t)sval);
  1648. HANDLE_OP_END();
  1649. }
  1650. HANDLE_OP(WASM_OP_I32_STORE16)
  1651. {
  1652. uint32 offset, addr;
  1653. uint32 sval;
  1654. offset = read_uint32(frame_ip);
  1655. sval = GET_OPERAND(uint32, I32, 0);
  1656. addr = GET_OPERAND(uint32, I32, 2);
  1657. frame_ip += 4;
  1658. CHECK_MEMORY_OVERFLOW(2);
  1659. STORE_U16(maddr, (uint16)sval);
  1660. HANDLE_OP_END();
  1661. }
  1662. HANDLE_OP(WASM_OP_I64_STORE)
  1663. {
  1664. uint32 offset, addr;
  1665. uint64 sval;
  1666. offset = read_uint32(frame_ip);
  1667. sval = GET_OPERAND(uint64, I64, 0);
  1668. addr = GET_OPERAND(uint32, I32, 2);
  1669. frame_ip += 4;
  1670. CHECK_MEMORY_OVERFLOW(8);
  1671. STORE_I64(maddr, sval);
  1672. HANDLE_OP_END();
  1673. }
  1674. HANDLE_OP(WASM_OP_I64_STORE8)
  1675. {
  1676. uint32 offset, addr;
  1677. uint64 sval;
  1678. offset = read_uint32(frame_ip);
  1679. sval = GET_OPERAND(uint64, I64, 0);
  1680. addr = GET_OPERAND(uint32, I32, 2);
  1681. frame_ip += 4;
  1682. CHECK_MEMORY_OVERFLOW(1);
  1683. *(uint8 *)maddr = (uint8)sval;
  1684. HANDLE_OP_END();
  1685. }
  1686. HANDLE_OP(WASM_OP_I64_STORE16)
  1687. {
  1688. uint32 offset, addr;
  1689. uint64 sval;
  1690. offset = read_uint32(frame_ip);
  1691. sval = GET_OPERAND(uint64, I64, 0);
  1692. addr = GET_OPERAND(uint32, I32, 2);
  1693. frame_ip += 4;
  1694. CHECK_MEMORY_OVERFLOW(2);
  1695. STORE_U16(maddr, (uint16)sval);
  1696. HANDLE_OP_END();
  1697. }
  1698. HANDLE_OP(WASM_OP_I64_STORE32)
  1699. {
  1700. uint32 offset, addr;
  1701. uint64 sval;
  1702. offset = read_uint32(frame_ip);
  1703. sval = GET_OPERAND(uint64, I64, 0);
  1704. addr = GET_OPERAND(uint32, I32, 2);
  1705. frame_ip += 4;
  1706. CHECK_MEMORY_OVERFLOW(4);
  1707. STORE_U32(maddr, (uint32)sval);
  1708. HANDLE_OP_END();
  1709. }
  1710. /* memory size and memory grow instructions */
  1711. HANDLE_OP(WASM_OP_MEMORY_SIZE)
  1712. {
  1713. uint32 reserved;
  1714. addr_ret = GET_OFFSET();
  1715. frame_lp[addr_ret] = memory->cur_page_count;
  1716. (void)reserved;
  1717. HANDLE_OP_END();
  1718. }
  1719. HANDLE_OP(WASM_OP_MEMORY_GROW)
  1720. {
  1721. uint32 reserved, delta,
  1722. prev_page_count = memory->cur_page_count;
  1723. addr1 = GET_OFFSET();
  1724. addr_ret = GET_OFFSET();
  1725. delta = (uint32)frame_lp[addr1];
  1726. if (!wasm_enlarge_memory(module, delta)) {
  1727. /* failed to memory.grow, return -1 */
  1728. frame_lp[addr_ret] = -1;
  1729. }
  1730. else {
  1731. /* success, return previous page count */
  1732. frame_lp[addr_ret] = prev_page_count;
  1733. /* update memory size, no need to update memory ptr as
  1734. it isn't changed in wasm_enlarge_memory */
  1735. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  1736. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  1737. || WASM_ENABLE_BULK_MEMORY != 0
  1738. linear_mem_size = GET_LINEAR_MEMORY_SIZE(memory);
  1739. #endif
  1740. }
  1741. (void)reserved;
  1742. HANDLE_OP_END();
  1743. }
  1744. /* constant instructions */
  1745. HANDLE_OP(WASM_OP_F64_CONST)
  1746. HANDLE_OP(WASM_OP_I64_CONST)
  1747. {
  1748. uint8 *orig_ip = frame_ip;
  1749. frame_ip += sizeof(uint64);
  1750. addr_ret = GET_OFFSET();
  1751. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
  1752. sizeof(uint64));
  1753. HANDLE_OP_END();
  1754. }
  1755. HANDLE_OP(WASM_OP_F32_CONST)
  1756. HANDLE_OP(WASM_OP_I32_CONST)
  1757. {
  1758. uint8 *orig_ip = frame_ip;
  1759. frame_ip += sizeof(uint32);
  1760. addr_ret = GET_OFFSET();
  1761. bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
  1762. sizeof(uint32));
  1763. HANDLE_OP_END();
  1764. }
  1765. /* comparison instructions of i32 */
  1766. HANDLE_OP(WASM_OP_I32_EQZ)
  1767. {
  1768. DEF_OP_EQZ(int32, I32);
  1769. HANDLE_OP_END();
  1770. }
  1771. HANDLE_OP(WASM_OP_I32_EQ)
  1772. {
  1773. DEF_OP_CMP(uint32, I32, ==);
  1774. HANDLE_OP_END();
  1775. }
  1776. HANDLE_OP(WASM_OP_I32_NE)
  1777. {
  1778. DEF_OP_CMP(uint32, I32, !=);
  1779. HANDLE_OP_END();
  1780. }
  1781. HANDLE_OP(WASM_OP_I32_LT_S)
  1782. {
  1783. DEF_OP_CMP(int32, I32, <);
  1784. HANDLE_OP_END();
  1785. }
  1786. HANDLE_OP(WASM_OP_I32_LT_U)
  1787. {
  1788. DEF_OP_CMP(uint32, I32, <);
  1789. HANDLE_OP_END();
  1790. }
  1791. HANDLE_OP(WASM_OP_I32_GT_S)
  1792. {
  1793. DEF_OP_CMP(int32, I32, >);
  1794. HANDLE_OP_END();
  1795. }
  1796. HANDLE_OP(WASM_OP_I32_GT_U)
  1797. {
  1798. DEF_OP_CMP(uint32, I32, >);
  1799. HANDLE_OP_END();
  1800. }
  1801. HANDLE_OP(WASM_OP_I32_LE_S)
  1802. {
  1803. DEF_OP_CMP(int32, I32, <=);
  1804. HANDLE_OP_END();
  1805. }
  1806. HANDLE_OP(WASM_OP_I32_LE_U)
  1807. {
  1808. DEF_OP_CMP(uint32, I32, <=);
  1809. HANDLE_OP_END();
  1810. }
  1811. HANDLE_OP(WASM_OP_I32_GE_S)
  1812. {
  1813. DEF_OP_CMP(int32, I32, >=);
  1814. HANDLE_OP_END();
  1815. }
  1816. HANDLE_OP(WASM_OP_I32_GE_U)
  1817. {
  1818. DEF_OP_CMP(uint32, I32, >=);
  1819. HANDLE_OP_END();
  1820. }
  1821. /* comparison instructions of i64 */
  1822. HANDLE_OP(WASM_OP_I64_EQZ)
  1823. {
  1824. DEF_OP_EQZ(int64, I64);
  1825. HANDLE_OP_END();
  1826. }
  1827. HANDLE_OP(WASM_OP_I64_EQ)
  1828. {
  1829. DEF_OP_CMP(uint64, I64, ==);
  1830. HANDLE_OP_END();
  1831. }
  1832. HANDLE_OP(WASM_OP_I64_NE)
  1833. {
  1834. DEF_OP_CMP(uint64, I64, !=);
  1835. HANDLE_OP_END();
  1836. }
  1837. HANDLE_OP(WASM_OP_I64_LT_S)
  1838. {
  1839. DEF_OP_CMP(int64, I64, <);
  1840. HANDLE_OP_END();
  1841. }
  1842. HANDLE_OP(WASM_OP_I64_LT_U)
  1843. {
  1844. DEF_OP_CMP(uint64, I64, <);
  1845. HANDLE_OP_END();
  1846. }
  1847. HANDLE_OP(WASM_OP_I64_GT_S)
  1848. {
  1849. DEF_OP_CMP(int64, I64, >);
  1850. HANDLE_OP_END();
  1851. }
  1852. HANDLE_OP(WASM_OP_I64_GT_U)
  1853. {
  1854. DEF_OP_CMP(uint64, I64, >);
  1855. HANDLE_OP_END();
  1856. }
  1857. HANDLE_OP(WASM_OP_I64_LE_S)
  1858. {
  1859. DEF_OP_CMP(int64, I64, <=);
  1860. HANDLE_OP_END();
  1861. }
  1862. HANDLE_OP(WASM_OP_I64_LE_U)
  1863. {
  1864. DEF_OP_CMP(uint64, I64, <=);
  1865. HANDLE_OP_END();
  1866. }
  1867. HANDLE_OP(WASM_OP_I64_GE_S)
  1868. {
  1869. DEF_OP_CMP(int64, I64, >=);
  1870. HANDLE_OP_END();
  1871. }
  1872. HANDLE_OP(WASM_OP_I64_GE_U)
  1873. {
  1874. DEF_OP_CMP(uint64, I64, >=);
  1875. HANDLE_OP_END();
  1876. }
  1877. /* comparison instructions of f32 */
  1878. HANDLE_OP(WASM_OP_F32_EQ)
  1879. {
  1880. DEF_OP_CMP(float32, F32, ==);
  1881. HANDLE_OP_END();
  1882. }
  1883. HANDLE_OP(WASM_OP_F32_NE)
  1884. {
  1885. DEF_OP_CMP(float32, F32, !=);
  1886. HANDLE_OP_END();
  1887. }
  1888. HANDLE_OP(WASM_OP_F32_LT)
  1889. {
  1890. DEF_OP_CMP(float32, F32, <);
  1891. HANDLE_OP_END();
  1892. }
  1893. HANDLE_OP(WASM_OP_F32_GT)
  1894. {
  1895. DEF_OP_CMP(float32, F32, >);
  1896. HANDLE_OP_END();
  1897. }
  1898. HANDLE_OP(WASM_OP_F32_LE)
  1899. {
  1900. DEF_OP_CMP(float32, F32, <=);
  1901. HANDLE_OP_END();
  1902. }
  1903. HANDLE_OP(WASM_OP_F32_GE)
  1904. {
  1905. DEF_OP_CMP(float32, F32, >=);
  1906. HANDLE_OP_END();
  1907. }
  1908. /* comparison instructions of f64 */
  1909. HANDLE_OP(WASM_OP_F64_EQ)
  1910. {
  1911. DEF_OP_CMP(float64, F64, ==);
  1912. HANDLE_OP_END();
  1913. }
  1914. HANDLE_OP(WASM_OP_F64_NE)
  1915. {
  1916. DEF_OP_CMP(float64, F64, !=);
  1917. HANDLE_OP_END();
  1918. }
  1919. HANDLE_OP(WASM_OP_F64_LT)
  1920. {
  1921. DEF_OP_CMP(float64, F64, <);
  1922. HANDLE_OP_END();
  1923. }
  1924. HANDLE_OP(WASM_OP_F64_GT)
  1925. {
  1926. DEF_OP_CMP(float64, F64, >);
  1927. HANDLE_OP_END();
  1928. }
  1929. HANDLE_OP(WASM_OP_F64_LE)
  1930. {
  1931. DEF_OP_CMP(float64, F64, <=);
  1932. HANDLE_OP_END();
  1933. }
  1934. HANDLE_OP(WASM_OP_F64_GE)
  1935. {
  1936. DEF_OP_CMP(float64, F64, >=);
  1937. HANDLE_OP_END();
  1938. }
  1939. /* numberic instructions of i32 */
  1940. HANDLE_OP(WASM_OP_I32_CLZ)
  1941. {
  1942. DEF_OP_BIT_COUNT(uint32, I32, clz32);
  1943. HANDLE_OP_END();
  1944. }
  1945. HANDLE_OP(WASM_OP_I32_CTZ)
  1946. {
  1947. DEF_OP_BIT_COUNT(uint32, I32, ctz32);
  1948. HANDLE_OP_END();
  1949. }
  1950. HANDLE_OP(WASM_OP_I32_POPCNT)
  1951. {
  1952. DEF_OP_BIT_COUNT(uint32, I32, popcount32);
  1953. HANDLE_OP_END();
  1954. }
  1955. HANDLE_OP(WASM_OP_I32_ADD)
  1956. {
  1957. DEF_OP_NUMERIC(uint32, uint32, I32, +);
  1958. HANDLE_OP_END();
  1959. }
  1960. HANDLE_OP(WASM_OP_I32_SUB)
  1961. {
  1962. DEF_OP_NUMERIC(uint32, uint32, I32, -);
  1963. HANDLE_OP_END();
  1964. }
  1965. HANDLE_OP(WASM_OP_I32_MUL)
  1966. {
  1967. DEF_OP_NUMERIC(uint32, uint32, I32, *);
  1968. HANDLE_OP_END();
  1969. }
  1970. HANDLE_OP(WASM_OP_I32_DIV_S)
  1971. {
  1972. int32 a, b;
  1973. b = frame_lp[GET_OFFSET()];
  1974. a = frame_lp[GET_OFFSET()];
  1975. addr_ret = GET_OFFSET();
  1976. if (a == (int32)0x80000000 && b == -1) {
  1977. wasm_set_exception(module, "integer overflow");
  1978. goto got_exception;
  1979. }
  1980. if (b == 0) {
  1981. wasm_set_exception(module, "integer divide by zero");
  1982. goto got_exception;
  1983. }
  1984. frame_lp[addr_ret] = (a / b);
  1985. HANDLE_OP_END();
  1986. }
  1987. HANDLE_OP(WASM_OP_I32_DIV_U)
  1988. {
  1989. uint32 a, b;
  1990. addr1 = GET_OFFSET();
  1991. addr2 = GET_OFFSET();
  1992. addr_ret = GET_OFFSET();
  1993. b = (uint32)frame_lp[addr1];
  1994. a = (uint32)frame_lp[addr2];
  1995. if (b == 0) {
  1996. wasm_set_exception(module, "integer divide by zero");
  1997. goto got_exception;
  1998. }
  1999. frame_lp[addr_ret] = (a / b);
  2000. HANDLE_OP_END();
  2001. }
  2002. HANDLE_OP(WASM_OP_I32_REM_S)
  2003. {
  2004. int32 a, b;
  2005. addr1 = GET_OFFSET();
  2006. addr2 = GET_OFFSET();
  2007. addr_ret = GET_OFFSET();
  2008. b = frame_lp[addr1];
  2009. a = frame_lp[addr2];
  2010. if (a == (int32)0x80000000 && b == -1) {
  2011. frame_lp[addr_ret] = 0;
  2012. HANDLE_OP_END();
  2013. }
  2014. if (b == 0) {
  2015. wasm_set_exception(module, "integer divide by zero");
  2016. goto got_exception;
  2017. }
  2018. frame_lp[addr_ret] = (a % b);
  2019. HANDLE_OP_END();
  2020. }
  2021. HANDLE_OP(WASM_OP_I32_REM_U)
  2022. {
  2023. uint32 a, b;
  2024. addr1 = GET_OFFSET();
  2025. addr2 = GET_OFFSET();
  2026. addr_ret = GET_OFFSET();
  2027. b = (uint32)frame_lp[addr1];
  2028. a = (uint32)frame_lp[addr2];
  2029. if (b == 0) {
  2030. wasm_set_exception(module, "integer divide by zero");
  2031. goto got_exception;
  2032. }
  2033. frame_lp[addr_ret] = (a % b);
  2034. HANDLE_OP_END();
  2035. }
  2036. HANDLE_OP(WASM_OP_I32_AND)
  2037. {
  2038. DEF_OP_NUMERIC(uint32, uint32, I32, &);
  2039. HANDLE_OP_END();
  2040. }
  2041. HANDLE_OP(WASM_OP_I32_OR)
  2042. {
  2043. DEF_OP_NUMERIC(uint32, uint32, I32, |);
  2044. HANDLE_OP_END();
  2045. }
  2046. HANDLE_OP(WASM_OP_I32_XOR)
  2047. {
  2048. DEF_OP_NUMERIC(uint32, uint32, I32, ^);
  2049. HANDLE_OP_END();
  2050. }
  2051. HANDLE_OP(WASM_OP_I32_SHL)
  2052. {
  2053. DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
  2054. HANDLE_OP_END();
  2055. }
  2056. HANDLE_OP(WASM_OP_I32_SHR_S)
  2057. {
  2058. DEF_OP_NUMERIC2(int32, uint32, I32, >>);
  2059. HANDLE_OP_END();
  2060. }
  2061. HANDLE_OP(WASM_OP_I32_SHR_U)
  2062. {
  2063. DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
  2064. HANDLE_OP_END();
  2065. }
  2066. HANDLE_OP(WASM_OP_I32_ROTL)
  2067. {
  2068. uint32 a, b;
  2069. b = (uint32)frame_lp[GET_OFFSET()];
  2070. a = (uint32)frame_lp[GET_OFFSET()];
  2071. frame_lp[GET_OFFSET()] = rotl32(a, b);
  2072. HANDLE_OP_END();
  2073. }
  2074. HANDLE_OP(WASM_OP_I32_ROTR)
  2075. {
  2076. uint32 a, b;
  2077. b = (uint32)frame_lp[GET_OFFSET()];
  2078. a = (uint32)frame_lp[GET_OFFSET()];
  2079. frame_lp[GET_OFFSET()] = rotr32(a, b);
  2080. HANDLE_OP_END();
  2081. }
  2082. /* numberic instructions of i64 */
  2083. HANDLE_OP(WASM_OP_I64_CLZ)
  2084. {
  2085. DEF_OP_BIT_COUNT(uint64, I64, clz64);
  2086. HANDLE_OP_END();
  2087. }
  2088. HANDLE_OP(WASM_OP_I64_CTZ)
  2089. {
  2090. DEF_OP_BIT_COUNT(uint64, I64, ctz64);
  2091. HANDLE_OP_END();
  2092. }
  2093. HANDLE_OP(WASM_OP_I64_POPCNT)
  2094. {
  2095. DEF_OP_BIT_COUNT(uint64, I64, popcount64);
  2096. HANDLE_OP_END();
  2097. }
  2098. HANDLE_OP(WASM_OP_I64_ADD)
  2099. {
  2100. DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
  2101. HANDLE_OP_END();
  2102. }
  2103. HANDLE_OP(WASM_OP_I64_SUB)
  2104. {
  2105. DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
  2106. HANDLE_OP_END();
  2107. }
  2108. HANDLE_OP(WASM_OP_I64_MUL)
  2109. {
  2110. DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
  2111. HANDLE_OP_END();
  2112. }
  2113. HANDLE_OP(WASM_OP_I64_DIV_S)
  2114. {
  2115. int64 a, b;
  2116. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2117. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2118. if (a == (int64)0x8000000000000000LL && b == -1) {
  2119. wasm_set_exception(module, "integer overflow");
  2120. goto got_exception;
  2121. }
  2122. if (b == 0) {
  2123. wasm_set_exception(module, "integer divide by zero");
  2124. goto got_exception;
  2125. }
  2126. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2127. HANDLE_OP_END();
  2128. }
  2129. HANDLE_OP(WASM_OP_I64_DIV_U)
  2130. {
  2131. uint64 a, b;
  2132. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2133. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2134. if (b == 0) {
  2135. wasm_set_exception(module, "integer divide by zero");
  2136. goto got_exception;
  2137. }
  2138. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
  2139. HANDLE_OP_END();
  2140. }
  2141. HANDLE_OP(WASM_OP_I64_REM_S)
  2142. {
  2143. int64 a, b;
  2144. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2145. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2146. if (a == (int64)0x8000000000000000LL && b == -1) {
  2147. *(int64 *)(frame_lp + GET_OFFSET()) = 0;
  2148. HANDLE_OP_END();
  2149. }
  2150. if (b == 0) {
  2151. wasm_set_exception(module, "integer divide by zero");
  2152. goto got_exception;
  2153. }
  2154. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2155. HANDLE_OP_END();
  2156. }
  2157. HANDLE_OP(WASM_OP_I64_REM_U)
  2158. {
  2159. uint64 a, b;
  2160. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2161. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2162. if (b == 0) {
  2163. wasm_set_exception(module, "integer divide by zero");
  2164. goto got_exception;
  2165. }
  2166. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
  2167. HANDLE_OP_END();
  2168. }
  2169. HANDLE_OP(WASM_OP_I64_AND)
  2170. {
  2171. DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
  2172. HANDLE_OP_END();
  2173. }
  2174. HANDLE_OP(WASM_OP_I64_OR)
  2175. {
  2176. DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
  2177. HANDLE_OP_END();
  2178. }
  2179. HANDLE_OP(WASM_OP_I64_XOR)
  2180. {
  2181. DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
  2182. HANDLE_OP_END();
  2183. }
  2184. HANDLE_OP(WASM_OP_I64_SHL)
  2185. {
  2186. DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
  2187. HANDLE_OP_END();
  2188. }
  2189. HANDLE_OP(WASM_OP_I64_SHR_S)
  2190. {
  2191. DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
  2192. HANDLE_OP_END();
  2193. }
  2194. HANDLE_OP(WASM_OP_I64_SHR_U)
  2195. {
  2196. DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
  2197. HANDLE_OP_END();
  2198. }
  2199. HANDLE_OP(WASM_OP_I64_ROTL)
  2200. {
  2201. uint64 a, b;
  2202. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2203. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2204. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
  2205. HANDLE_OP_END();
  2206. }
  2207. HANDLE_OP(WASM_OP_I64_ROTR)
  2208. {
  2209. uint64 a, b;
  2210. b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2211. a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2212. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
  2213. HANDLE_OP_END();
  2214. }
  2215. /* numberic instructions of f32 */
  2216. HANDLE_OP(WASM_OP_F32_ABS)
  2217. {
  2218. DEF_OP_MATH(float32, F32, fabsf);
  2219. HANDLE_OP_END();
  2220. }
  2221. HANDLE_OP(WASM_OP_F32_NEG)
  2222. {
  2223. uint32 u32 = frame_lp[GET_OFFSET()];
  2224. uint32 sign_bit = u32 & ((uint32)1 << 31);
  2225. addr_ret = GET_OFFSET();
  2226. if (sign_bit)
  2227. frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
  2228. else
  2229. frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
  2230. HANDLE_OP_END();
  2231. }
  2232. HANDLE_OP(WASM_OP_F32_CEIL)
  2233. {
  2234. DEF_OP_MATH(float32, F32, ceilf);
  2235. HANDLE_OP_END();
  2236. }
  2237. HANDLE_OP(WASM_OP_F32_FLOOR)
  2238. {
  2239. DEF_OP_MATH(float32, F32, floorf);
  2240. HANDLE_OP_END();
  2241. }
  2242. HANDLE_OP(WASM_OP_F32_TRUNC)
  2243. {
  2244. DEF_OP_MATH(float32, F32, truncf);
  2245. HANDLE_OP_END();
  2246. }
  2247. HANDLE_OP(WASM_OP_F32_NEAREST)
  2248. {
  2249. DEF_OP_MATH(float32, F32, rintf);
  2250. HANDLE_OP_END();
  2251. }
  2252. HANDLE_OP(WASM_OP_F32_SQRT)
  2253. {
  2254. DEF_OP_MATH(float32, F32, sqrtf);
  2255. HANDLE_OP_END();
  2256. }
  2257. HANDLE_OP(WASM_OP_F32_ADD)
  2258. {
  2259. DEF_OP_NUMERIC(float32, float32, F32, +);
  2260. HANDLE_OP_END();
  2261. }
  2262. HANDLE_OP(WASM_OP_F32_SUB)
  2263. {
  2264. DEF_OP_NUMERIC(float32, float32, F32, -);
  2265. HANDLE_OP_END();
  2266. }
  2267. HANDLE_OP(WASM_OP_F32_MUL)
  2268. {
  2269. DEF_OP_NUMERIC(float32, float32, F32, *);
  2270. HANDLE_OP_END();
  2271. }
  2272. HANDLE_OP(WASM_OP_F32_DIV)
  2273. {
  2274. DEF_OP_NUMERIC(float32, float32, F32, /);
  2275. HANDLE_OP_END();
  2276. }
  2277. HANDLE_OP(WASM_OP_F32_MIN)
  2278. {
  2279. float32 a, b;
  2280. b = *(float32 *)(frame_lp + GET_OFFSET());
  2281. a = *(float32 *)(frame_lp + GET_OFFSET());
  2282. *(float32 *)(frame_lp + GET_OFFSET()) = f32_min(a, b);
  2283. HANDLE_OP_END();
  2284. }
  2285. HANDLE_OP(WASM_OP_F32_MAX)
  2286. {
  2287. float32 a, b;
  2288. b = *(float32 *)(frame_lp + GET_OFFSET());
  2289. a = *(float32 *)(frame_lp + GET_OFFSET());
  2290. *(float32 *)(frame_lp + GET_OFFSET()) = f32_max(a, b);
  2291. HANDLE_OP_END();
  2292. }
  2293. HANDLE_OP(WASM_OP_F32_COPYSIGN)
  2294. {
  2295. float32 a, b;
  2296. b = *(float32 *)(frame_lp + GET_OFFSET());
  2297. a = *(float32 *)(frame_lp + GET_OFFSET());
  2298. *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
  2299. HANDLE_OP_END();
  2300. }
  2301. /* numberic instructions of f64 */
  2302. HANDLE_OP(WASM_OP_F64_ABS)
  2303. {
  2304. DEF_OP_MATH(float64, F64, fabs);
  2305. HANDLE_OP_END();
  2306. }
  2307. HANDLE_OP(WASM_OP_F64_NEG)
  2308. {
  2309. uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
  2310. uint64 sign_bit = u64 & (((uint64)1) << 63);
  2311. if (sign_bit)
  2312. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2313. (u64 & ~(((uint64)1) << 63)));
  2314. else
  2315. PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
  2316. (u64 | (((uint64)1) << 63)));
  2317. HANDLE_OP_END();
  2318. }
  2319. HANDLE_OP(WASM_OP_F64_CEIL)
  2320. {
  2321. DEF_OP_MATH(float64, F64, ceil);
  2322. HANDLE_OP_END();
  2323. }
  2324. HANDLE_OP(WASM_OP_F64_FLOOR)
  2325. {
  2326. DEF_OP_MATH(float64, F64, floor);
  2327. HANDLE_OP_END();
  2328. }
  2329. HANDLE_OP(WASM_OP_F64_TRUNC)
  2330. {
  2331. DEF_OP_MATH(float64, F64, trunc);
  2332. HANDLE_OP_END();
  2333. }
  2334. HANDLE_OP(WASM_OP_F64_NEAREST)
  2335. {
  2336. DEF_OP_MATH(float64, F64, rint);
  2337. HANDLE_OP_END();
  2338. }
  2339. HANDLE_OP(WASM_OP_F64_SQRT)
  2340. {
  2341. DEF_OP_MATH(float64, F64, sqrt);
  2342. HANDLE_OP_END();
  2343. }
  2344. HANDLE_OP(WASM_OP_F64_ADD)
  2345. {
  2346. DEF_OP_NUMERIC_64(float64, float64, F64, +);
  2347. HANDLE_OP_END();
  2348. }
  2349. HANDLE_OP(WASM_OP_F64_SUB)
  2350. {
  2351. DEF_OP_NUMERIC_64(float64, float64, F64, -);
  2352. HANDLE_OP_END();
  2353. }
  2354. HANDLE_OP(WASM_OP_F64_MUL)
  2355. {
  2356. DEF_OP_NUMERIC_64(float64, float64, F64, *);
  2357. HANDLE_OP_END();
  2358. }
  2359. HANDLE_OP(WASM_OP_F64_DIV)
  2360. {
  2361. DEF_OP_NUMERIC_64(float64, float64, F64, /);
  2362. HANDLE_OP_END();
  2363. }
  2364. HANDLE_OP(WASM_OP_F64_MIN)
  2365. {
  2366. float64 a, b;
  2367. b = POP_F64();
  2368. a = POP_F64();
  2369. PUSH_F64(f64_min(a, b));
  2370. HANDLE_OP_END();
  2371. }
  2372. HANDLE_OP(WASM_OP_F64_MAX)
  2373. {
  2374. float64 a, b;
  2375. b = POP_F64();
  2376. a = POP_F64();
  2377. PUSH_F64(f64_max(a, b));
  2378. HANDLE_OP_END();
  2379. }
  2380. HANDLE_OP(WASM_OP_F64_COPYSIGN)
  2381. {
  2382. float64 a, b;
  2383. b = POP_F64();
  2384. a = POP_F64();
  2385. PUSH_F64(local_copysign(a, b));
  2386. HANDLE_OP_END();
  2387. }
  2388. /* conversions of i32 */
  2389. HANDLE_OP(WASM_OP_I32_WRAP_I64)
  2390. {
  2391. int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
  2392. PUSH_I32(value);
  2393. HANDLE_OP_END();
  2394. }
  2395. HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
  2396. {
  2397. /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
  2398. since float/double values of ieee754 cannot precisely
  2399. represent all int32/uint32/int64/uint64 values, e.g.:
  2400. UINT32_MAX is 4294967295, but (float32)4294967295 is
  2401. 4294967296.0f, but not 4294967295.0f. */
  2402. DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
  2403. HANDLE_OP_END();
  2404. }
  2405. HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
  2406. {
  2407. DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
  2408. HANDLE_OP_END();
  2409. }
  2410. HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
  2411. {
  2412. DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
  2413. HANDLE_OP_END();
  2414. }
  2415. HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
  2416. {
  2417. DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
  2418. HANDLE_OP_END();
  2419. }
  2420. /* conversions of i64 */
  2421. HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
  2422. {
  2423. DEF_OP_CONVERT(int64, I64, int32, I32);
  2424. HANDLE_OP_END();
  2425. }
  2426. HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
  2427. {
  2428. DEF_OP_CONVERT(int64, I64, uint32, I32);
  2429. HANDLE_OP_END();
  2430. }
  2431. HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
  2432. {
  2433. DEF_OP_TRUNC_F32(-9223373136366403584.0f,
  2434. 9223372036854775808.0f, false, true);
  2435. HANDLE_OP_END();
  2436. }
  2437. HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
  2438. {
  2439. DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
  2440. HANDLE_OP_END();
  2441. }
  2442. HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
  2443. {
  2444. DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
  2445. false, true);
  2446. HANDLE_OP_END();
  2447. }
  2448. HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
  2449. {
  2450. DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
  2451. HANDLE_OP_END();
  2452. }
  2453. /* conversions of f32 */
  2454. HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
  2455. {
  2456. DEF_OP_CONVERT(float32, F32, int32, I32);
  2457. HANDLE_OP_END();
  2458. }
  2459. HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
  2460. {
  2461. DEF_OP_CONVERT(float32, F32, uint32, I32);
  2462. HANDLE_OP_END();
  2463. }
  2464. HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
  2465. {
  2466. DEF_OP_CONVERT(float32, F32, int64, I64);
  2467. HANDLE_OP_END();
  2468. }
  2469. HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
  2470. {
  2471. DEF_OP_CONVERT(float32, F32, uint64, I64);
  2472. HANDLE_OP_END();
  2473. }
  2474. HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
  2475. {
  2476. DEF_OP_CONVERT(float32, F32, float64, F64);
  2477. HANDLE_OP_END();
  2478. }
  2479. /* conversions of f64 */
  2480. HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
  2481. {
  2482. DEF_OP_CONVERT(float64, F64, int32, I32);
  2483. HANDLE_OP_END();
  2484. }
  2485. HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
  2486. {
  2487. DEF_OP_CONVERT(float64, F64, uint32, I32);
  2488. HANDLE_OP_END();
  2489. }
  2490. HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
  2491. {
  2492. DEF_OP_CONVERT(float64, F64, int64, I64);
  2493. HANDLE_OP_END();
  2494. }
  2495. HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
  2496. {
  2497. DEF_OP_CONVERT(float64, F64, uint64, I64);
  2498. HANDLE_OP_END();
  2499. }
  2500. HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
  2501. {
  2502. DEF_OP_CONVERT(float64, F64, float32, F32);
  2503. HANDLE_OP_END();
  2504. }
  2505. /* reinterpretations */
  2506. HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
  2507. HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
  2508. {
  2509. DEF_OP_REINTERPRET(uint32, I32);
  2510. HANDLE_OP_END();
  2511. }
  2512. HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
  2513. HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
  2514. {
  2515. DEF_OP_REINTERPRET(int64, I64);
  2516. HANDLE_OP_END();
  2517. }
  2518. HANDLE_OP(EXT_OP_COPY_STACK_TOP)
  2519. {
  2520. addr1 = GET_OFFSET();
  2521. addr2 = GET_OFFSET();
  2522. frame_lp[addr2] = frame_lp[addr1];
  2523. HANDLE_OP_END();
  2524. }
  2525. HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
  2526. {
  2527. addr1 = GET_OFFSET();
  2528. addr2 = GET_OFFSET();
  2529. frame_lp[addr2] = frame_lp[addr1];
  2530. frame_lp[addr2 + 1] = frame_lp[addr1 + 1];
  2531. HANDLE_OP_END();
  2532. }
  2533. HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
  2534. {
  2535. uint32 values_count, total_cell;
  2536. uint8 *cells;
  2537. int16 *src_offsets = NULL;
  2538. uint16 *dst_offsets = NULL;
  2539. /* read values_count */
  2540. values_count = read_uint32(frame_ip);
  2541. /* read total cell num */
  2542. total_cell = read_uint32(frame_ip);
  2543. /* cells */
  2544. cells = (uint8 *)frame_ip;
  2545. frame_ip += values_count * CELL_SIZE;
  2546. /* src offsets */
  2547. src_offsets = (int16 *)frame_ip;
  2548. frame_ip += values_count * sizeof(int16);
  2549. /* dst offsets */
  2550. dst_offsets = (uint16 *)frame_ip;
  2551. frame_ip += values_count * sizeof(uint16);
  2552. if (!copy_stack_values(module, frame_lp, values_count,
  2553. total_cell, cells, src_offsets,
  2554. dst_offsets))
  2555. goto got_exception;
  2556. HANDLE_OP_END();
  2557. }
  2558. HANDLE_OP(WASM_OP_SET_LOCAL)
  2559. HANDLE_OP(WASM_OP_TEE_LOCAL)
  2560. {
  2561. GET_LOCAL_INDEX_TYPE_AND_OFFSET();
  2562. addr1 = GET_OFFSET();
  2563. if (local_type == VALUE_TYPE_I32
  2564. || local_type == VALUE_TYPE_F32) {
  2565. *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
  2566. }
  2567. else if (local_type == VALUE_TYPE_I64
  2568. || local_type == VALUE_TYPE_F64) {
  2569. PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
  2570. GET_I64_FROM_ADDR(frame_lp + addr1));
  2571. }
  2572. else {
  2573. wasm_set_exception(module, "invalid local type");
  2574. goto got_exception;
  2575. }
  2576. HANDLE_OP_END();
  2577. }
  2578. HANDLE_OP(WASM_OP_I32_EXTEND8_S)
  2579. {
  2580. DEF_OP_CONVERT(int32, I32, int8, I32);
  2581. HANDLE_OP_END();
  2582. }
  2583. HANDLE_OP(WASM_OP_I32_EXTEND16_S)
  2584. {
  2585. DEF_OP_CONVERT(int32, I32, int16, I32);
  2586. HANDLE_OP_END();
  2587. }
  2588. HANDLE_OP(WASM_OP_I64_EXTEND8_S)
  2589. {
  2590. DEF_OP_CONVERT(int64, I64, int8, I64);
  2591. HANDLE_OP_END();
  2592. }
  2593. HANDLE_OP(WASM_OP_I64_EXTEND16_S)
  2594. {
  2595. DEF_OP_CONVERT(int64, I64, int16, I64);
  2596. HANDLE_OP_END();
  2597. }
  2598. HANDLE_OP(WASM_OP_I64_EXTEND32_S)
  2599. {
  2600. DEF_OP_CONVERT(int64, I64, int32, I64);
  2601. HANDLE_OP_END();
  2602. }
  2603. HANDLE_OP(WASM_OP_MISC_PREFIX)
  2604. {
  2605. GET_OPCODE();
  2606. switch (opcode) {
  2607. case WASM_OP_I32_TRUNC_SAT_S_F32:
  2608. DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
  2609. true, true);
  2610. break;
  2611. case WASM_OP_I32_TRUNC_SAT_U_F32:
  2612. DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
  2613. break;
  2614. case WASM_OP_I32_TRUNC_SAT_S_F64:
  2615. DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
  2616. true);
  2617. break;
  2618. case WASM_OP_I32_TRUNC_SAT_U_F64:
  2619. DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
  2620. break;
  2621. case WASM_OP_I64_TRUNC_SAT_S_F32:
  2622. DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
  2623. 9223372036854775808.0f, false,
  2624. true);
  2625. break;
  2626. case WASM_OP_I64_TRUNC_SAT_U_F32:
  2627. DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
  2628. false, false);
  2629. break;
  2630. case WASM_OP_I64_TRUNC_SAT_S_F64:
  2631. DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
  2632. 9223372036854775808.0, false,
  2633. true);
  2634. break;
  2635. case WASM_OP_I64_TRUNC_SAT_U_F64:
  2636. DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
  2637. false, false);
  2638. break;
  2639. #if WASM_ENABLE_BULK_MEMORY != 0
  2640. case WASM_OP_MEMORY_INIT:
  2641. {
  2642. uint32 addr, segment;
  2643. uint64 bytes, offset, seg_len;
  2644. uint8 *data;
  2645. segment = read_uint32(frame_ip);
  2646. bytes = (uint64)(uint32)POP_I32();
  2647. offset = (uint64)(uint32)POP_I32();
  2648. addr = POP_I32();
  2649. #if WASM_ENABLE_THREAD_MGR != 0
  2650. linear_mem_size = get_linear_mem_size();
  2651. #endif
  2652. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2653. CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
  2654. #else
  2655. if ((uint64)(uint32)addr + bytes
  2656. > (uint64)linear_mem_size)
  2657. goto out_of_bounds;
  2658. maddr = memory->memory_data + (uint32)addr;
  2659. #endif
  2660. if (bh_bitmap_get_bit(module->e->common.data_dropped,
  2661. segment)) {
  2662. seg_len = 0;
  2663. data = NULL;
  2664. }
  2665. else {
  2666. seg_len =
  2667. (uint64)module->module->data_segments[segment]
  2668. ->data_length;
  2669. data = module->module->data_segments[segment]->data;
  2670. }
  2671. if (offset + bytes > seg_len)
  2672. goto out_of_bounds;
  2673. bh_memcpy_s(maddr, linear_mem_size - addr,
  2674. data + offset, (uint32)bytes);
  2675. break;
  2676. }
  2677. case WASM_OP_DATA_DROP:
  2678. {
  2679. uint32 segment;
  2680. segment = read_uint32(frame_ip);
  2681. bh_bitmap_set_bit(module->e->common.data_dropped,
  2682. segment);
  2683. break;
  2684. }
  2685. case WASM_OP_MEMORY_COPY:
  2686. {
  2687. uint32 dst, src, len;
  2688. uint8 *mdst, *msrc;
  2689. len = POP_I32();
  2690. src = POP_I32();
  2691. dst = POP_I32();
  2692. #if WASM_ENABLE_THREAD_MGR != 0
  2693. linear_mem_size = get_linear_mem_size();
  2694. #endif
  2695. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2696. CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
  2697. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2698. #else
  2699. if ((uint64)(uint32)src + len > (uint64)linear_mem_size)
  2700. goto out_of_bounds;
  2701. msrc = memory->memory_data + (uint32)src;
  2702. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  2703. goto out_of_bounds;
  2704. mdst = memory->memory_data + (uint32)dst;
  2705. #endif
  2706. /* allowing the destination and source to overlap */
  2707. bh_memmove_s(mdst, linear_mem_size - dst, msrc, len);
  2708. break;
  2709. }
  2710. case WASM_OP_MEMORY_FILL:
  2711. {
  2712. uint32 dst, len;
  2713. uint8 fill_val, *mdst;
  2714. len = POP_I32();
  2715. fill_val = POP_I32();
  2716. dst = POP_I32();
  2717. #if WASM_ENABLE_THREAD_MGR != 0
  2718. linear_mem_size = get_linear_mem_size();
  2719. #endif
  2720. #ifndef OS_ENABLE_HW_BOUND_CHECK
  2721. CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
  2722. #else
  2723. if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
  2724. goto out_of_bounds;
  2725. mdst = memory->memory_data + (uint32)dst;
  2726. #endif
  2727. memset(mdst, fill_val, len);
  2728. break;
  2729. }
  2730. #endif /* WASM_ENABLE_BULK_MEMORY */
  2731. #if WASM_ENABLE_REF_TYPES != 0
  2732. case WASM_OP_TABLE_INIT:
  2733. {
  2734. uint32 tbl_idx, elem_idx;
  2735. uint32 n, s, d;
  2736. WASMTableInstance *tbl_inst;
  2737. uint32 *tbl_seg_elems = NULL, tbl_seg_len = 0;
  2738. elem_idx = read_uint32(frame_ip);
  2739. bh_assert(elem_idx < module->module->table_seg_count);
  2740. tbl_idx = read_uint32(frame_ip);
  2741. bh_assert(tbl_idx < module->module->table_count);
  2742. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2743. n = (uint32)POP_I32();
  2744. s = (uint32)POP_I32();
  2745. d = (uint32)POP_I32();
  2746. if (!bh_bitmap_get_bit(module->e->common.elem_dropped,
  2747. elem_idx)) {
  2748. /* table segment isn't dropped */
  2749. tbl_seg_elems =
  2750. module->module->table_segments[elem_idx]
  2751. .func_indexes;
  2752. tbl_seg_len =
  2753. module->module->table_segments[elem_idx]
  2754. .function_count;
  2755. }
  2756. if (offset_len_out_of_bounds(s, n, tbl_seg_len)
  2757. || offset_len_out_of_bounds(d, n,
  2758. tbl_inst->cur_size)) {
  2759. wasm_set_exception(module,
  2760. "out of bounds table access");
  2761. goto got_exception;
  2762. }
  2763. if (!n) {
  2764. break;
  2765. }
  2766. bh_memcpy_s(
  2767. (uint8 *)tbl_inst
  2768. + offsetof(WASMTableInstance, elems)
  2769. + d * sizeof(uint32),
  2770. (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
  2771. tbl_seg_elems + s, (uint32)(n * sizeof(uint32)));
  2772. break;
  2773. }
  2774. case WASM_OP_ELEM_DROP:
  2775. {
  2776. uint32 elem_idx = read_uint32(frame_ip);
  2777. bh_assert(elem_idx < module->module->table_seg_count);
  2778. bh_bitmap_set_bit(module->e->common.elem_dropped,
  2779. elem_idx);
  2780. break;
  2781. }
  2782. case WASM_OP_TABLE_COPY:
  2783. {
  2784. uint32 src_tbl_idx, dst_tbl_idx;
  2785. uint32 n, s, d;
  2786. WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
  2787. dst_tbl_idx = read_uint32(frame_ip);
  2788. bh_assert(dst_tbl_idx < module->table_count);
  2789. dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
  2790. src_tbl_idx = read_uint32(frame_ip);
  2791. bh_assert(src_tbl_idx < module->table_count);
  2792. src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
  2793. n = (uint32)POP_I32();
  2794. s = (uint32)POP_I32();
  2795. d = (uint32)POP_I32();
  2796. if (offset_len_out_of_bounds(d, n,
  2797. dst_tbl_inst->cur_size)
  2798. || offset_len_out_of_bounds(
  2799. s, n, src_tbl_inst->cur_size)) {
  2800. wasm_set_exception(module,
  2801. "out of bounds table access");
  2802. goto got_exception;
  2803. }
  2804. /* if s >= d, copy from front to back */
  2805. /* if s < d, copy from back to front */
  2806. /* merge all together */
  2807. bh_memmove_s((uint8 *)dst_tbl_inst
  2808. + offsetof(WASMTableInstance, elems)
  2809. + d * sizeof(uint32),
  2810. (uint32)((dst_tbl_inst->cur_size - d)
  2811. * sizeof(uint32)),
  2812. (uint8 *)src_tbl_inst
  2813. + offsetof(WASMTableInstance, elems)
  2814. + s * sizeof(uint32),
  2815. (uint32)(n * sizeof(uint32)));
  2816. break;
  2817. }
  2818. case WASM_OP_TABLE_GROW:
  2819. {
  2820. uint32 tbl_idx, n, init_val, orig_tbl_sz;
  2821. WASMTableInstance *tbl_inst;
  2822. tbl_idx = read_uint32(frame_ip);
  2823. bh_assert(tbl_idx < module->table_count);
  2824. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2825. orig_tbl_sz = tbl_inst->cur_size;
  2826. n = POP_I32();
  2827. init_val = POP_I32();
  2828. if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
  2829. PUSH_I32(-1);
  2830. }
  2831. else {
  2832. PUSH_I32(orig_tbl_sz);
  2833. }
  2834. break;
  2835. }
  2836. case WASM_OP_TABLE_SIZE:
  2837. {
  2838. uint32 tbl_idx;
  2839. WASMTableInstance *tbl_inst;
  2840. tbl_idx = read_uint32(frame_ip);
  2841. bh_assert(tbl_idx < module->table_count);
  2842. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2843. PUSH_I32(tbl_inst->cur_size);
  2844. break;
  2845. }
  2846. case WASM_OP_TABLE_FILL:
  2847. {
  2848. uint32 tbl_idx, n, fill_val, i;
  2849. WASMTableInstance *tbl_inst;
  2850. tbl_idx = read_uint32(frame_ip);
  2851. bh_assert(tbl_idx < module->table_count);
  2852. tbl_inst = wasm_get_table_inst(module, tbl_idx);
  2853. n = POP_I32();
  2854. fill_val = POP_I32();
  2855. i = POP_I32();
  2856. if (offset_len_out_of_bounds(i, n,
  2857. tbl_inst->cur_size)) {
  2858. wasm_set_exception(module,
  2859. "out of bounds table access");
  2860. goto got_exception;
  2861. }
  2862. for (; n != 0; i++, n--) {
  2863. tbl_inst->elems[i] = fill_val;
  2864. }
  2865. break;
  2866. }
  2867. #endif /* WASM_ENABLE_REF_TYPES */
  2868. default:
  2869. wasm_set_exception(module, "unsupported opcode");
  2870. goto got_exception;
  2871. }
  2872. HANDLE_OP_END();
  2873. }
  2874. #if WASM_ENABLE_SHARED_MEMORY != 0
  2875. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  2876. {
  2877. uint32 offset = 0, addr;
  2878. GET_OPCODE();
  2879. if (opcode != WASM_OP_ATOMIC_FENCE) {
  2880. offset = read_uint32(frame_ip);
  2881. }
  2882. switch (opcode) {
  2883. case WASM_OP_ATOMIC_NOTIFY:
  2884. {
  2885. uint32 notify_count, ret;
  2886. notify_count = POP_I32();
  2887. addr = POP_I32();
  2888. CHECK_MEMORY_OVERFLOW(4);
  2889. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2890. ret = wasm_runtime_atomic_notify(
  2891. (WASMModuleInstanceCommon *)module, maddr,
  2892. notify_count);
  2893. if (ret == (uint32)-1)
  2894. goto got_exception;
  2895. PUSH_I32(ret);
  2896. break;
  2897. }
  2898. case WASM_OP_ATOMIC_WAIT32:
  2899. {
  2900. uint64 timeout;
  2901. uint32 expect, ret;
  2902. timeout = POP_I64();
  2903. expect = POP_I32();
  2904. addr = POP_I32();
  2905. CHECK_MEMORY_OVERFLOW(4);
  2906. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2907. ret = wasm_runtime_atomic_wait(
  2908. (WASMModuleInstanceCommon *)module, maddr,
  2909. (uint64)expect, timeout, false);
  2910. if (ret == (uint32)-1)
  2911. goto got_exception;
  2912. #if WASM_ENABLE_THREAD_MGR != 0
  2913. CHECK_SUSPEND_FLAGS();
  2914. #endif
  2915. PUSH_I32(ret);
  2916. break;
  2917. }
  2918. case WASM_OP_ATOMIC_WAIT64:
  2919. {
  2920. uint64 timeout, expect;
  2921. uint32 ret;
  2922. timeout = POP_I64();
  2923. expect = POP_I64();
  2924. addr = POP_I32();
  2925. CHECK_MEMORY_OVERFLOW(8);
  2926. CHECK_ATOMIC_MEMORY_ACCESS(8);
  2927. ret = wasm_runtime_atomic_wait(
  2928. (WASMModuleInstanceCommon *)module, maddr, expect,
  2929. timeout, true);
  2930. if (ret == (uint32)-1)
  2931. goto got_exception;
  2932. #if WASM_ENABLE_THREAD_MGR != 0
  2933. CHECK_SUSPEND_FLAGS();
  2934. #endif
  2935. PUSH_I32(ret);
  2936. break;
  2937. }
  2938. case WASM_OP_ATOMIC_FENCE:
  2939. {
  2940. os_atomic_thread_fence(os_memory_order_seq_cst);
  2941. break;
  2942. }
  2943. case WASM_OP_ATOMIC_I32_LOAD:
  2944. case WASM_OP_ATOMIC_I32_LOAD8_U:
  2945. case WASM_OP_ATOMIC_I32_LOAD16_U:
  2946. {
  2947. uint32 readv;
  2948. addr = POP_I32();
  2949. if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
  2950. CHECK_MEMORY_OVERFLOW(1);
  2951. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2952. shared_memory_lock(memory);
  2953. readv = (uint32)(*(uint8 *)maddr);
  2954. shared_memory_unlock(memory);
  2955. }
  2956. else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
  2957. CHECK_MEMORY_OVERFLOW(2);
  2958. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2959. shared_memory_lock(memory);
  2960. readv = (uint32)LOAD_U16(maddr);
  2961. shared_memory_unlock(memory);
  2962. }
  2963. else {
  2964. CHECK_MEMORY_OVERFLOW(4);
  2965. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2966. shared_memory_lock(memory);
  2967. readv = LOAD_I32(maddr);
  2968. shared_memory_unlock(memory);
  2969. }
  2970. PUSH_I32(readv);
  2971. break;
  2972. }
  2973. case WASM_OP_ATOMIC_I64_LOAD:
  2974. case WASM_OP_ATOMIC_I64_LOAD8_U:
  2975. case WASM_OP_ATOMIC_I64_LOAD16_U:
  2976. case WASM_OP_ATOMIC_I64_LOAD32_U:
  2977. {
  2978. uint64 readv;
  2979. addr = POP_I32();
  2980. if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
  2981. CHECK_MEMORY_OVERFLOW(1);
  2982. CHECK_ATOMIC_MEMORY_ACCESS(1);
  2983. shared_memory_lock(memory);
  2984. readv = (uint64)(*(uint8 *)maddr);
  2985. shared_memory_unlock(memory);
  2986. }
  2987. else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
  2988. CHECK_MEMORY_OVERFLOW(2);
  2989. CHECK_ATOMIC_MEMORY_ACCESS(2);
  2990. shared_memory_lock(memory);
  2991. readv = (uint64)LOAD_U16(maddr);
  2992. shared_memory_unlock(memory);
  2993. }
  2994. else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
  2995. CHECK_MEMORY_OVERFLOW(4);
  2996. CHECK_ATOMIC_MEMORY_ACCESS(4);
  2997. shared_memory_lock(memory);
  2998. readv = (uint64)LOAD_U32(maddr);
  2999. shared_memory_unlock(memory);
  3000. }
  3001. else {
  3002. CHECK_MEMORY_OVERFLOW(8);
  3003. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3004. shared_memory_lock(memory);
  3005. readv = LOAD_I64(maddr);
  3006. shared_memory_unlock(memory);
  3007. }
  3008. PUSH_I64(readv);
  3009. break;
  3010. }
  3011. case WASM_OP_ATOMIC_I32_STORE:
  3012. case WASM_OP_ATOMIC_I32_STORE8:
  3013. case WASM_OP_ATOMIC_I32_STORE16:
  3014. {
  3015. uint32 sval;
  3016. sval = (uint32)POP_I32();
  3017. addr = POP_I32();
  3018. if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
  3019. CHECK_MEMORY_OVERFLOW(1);
  3020. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3021. shared_memory_lock(memory);
  3022. *(uint8 *)maddr = (uint8)sval;
  3023. shared_memory_unlock(memory);
  3024. }
  3025. else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
  3026. CHECK_MEMORY_OVERFLOW(2);
  3027. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3028. shared_memory_lock(memory);
  3029. STORE_U16(maddr, (uint16)sval);
  3030. shared_memory_unlock(memory);
  3031. }
  3032. else {
  3033. CHECK_MEMORY_OVERFLOW(4);
  3034. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3035. shared_memory_lock(memory);
  3036. STORE_U32(maddr, sval);
  3037. shared_memory_unlock(memory);
  3038. }
  3039. break;
  3040. }
  3041. case WASM_OP_ATOMIC_I64_STORE:
  3042. case WASM_OP_ATOMIC_I64_STORE8:
  3043. case WASM_OP_ATOMIC_I64_STORE16:
  3044. case WASM_OP_ATOMIC_I64_STORE32:
  3045. {
  3046. uint64 sval;
  3047. sval = (uint64)POP_I64();
  3048. addr = POP_I32();
  3049. if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
  3050. CHECK_MEMORY_OVERFLOW(1);
  3051. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3052. shared_memory_lock(memory);
  3053. *(uint8 *)maddr = (uint8)sval;
  3054. shared_memory_unlock(memory);
  3055. }
  3056. else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
  3057. CHECK_MEMORY_OVERFLOW(2);
  3058. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3059. shared_memory_lock(memory);
  3060. STORE_U16(maddr, (uint16)sval);
  3061. shared_memory_unlock(memory);
  3062. }
  3063. else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
  3064. CHECK_MEMORY_OVERFLOW(4);
  3065. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3066. shared_memory_lock(memory);
  3067. STORE_U32(maddr, (uint32)sval);
  3068. shared_memory_unlock(memory);
  3069. }
  3070. else {
  3071. CHECK_MEMORY_OVERFLOW(8);
  3072. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3073. shared_memory_lock(memory);
  3074. STORE_I64(maddr, sval);
  3075. shared_memory_unlock(memory);
  3076. }
  3077. break;
  3078. }
  3079. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
  3080. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
  3081. case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
  3082. {
  3083. uint32 readv, sval, expect;
  3084. sval = POP_I32();
  3085. expect = POP_I32();
  3086. addr = POP_I32();
  3087. if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
  3088. CHECK_MEMORY_OVERFLOW(1);
  3089. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3090. expect = (uint8)expect;
  3091. shared_memory_lock(memory);
  3092. readv = (uint32)(*(uint8 *)maddr);
  3093. if (readv == expect)
  3094. *(uint8 *)maddr = (uint8)(sval);
  3095. shared_memory_unlock(memory);
  3096. }
  3097. else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
  3098. CHECK_MEMORY_OVERFLOW(2);
  3099. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3100. expect = (uint16)expect;
  3101. shared_memory_lock(memory);
  3102. readv = (uint32)LOAD_U16(maddr);
  3103. if (readv == expect)
  3104. STORE_U16(maddr, (uint16)(sval));
  3105. shared_memory_unlock(memory);
  3106. }
  3107. else {
  3108. CHECK_MEMORY_OVERFLOW(4);
  3109. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3110. shared_memory_lock(memory);
  3111. readv = LOAD_I32(maddr);
  3112. if (readv == expect)
  3113. STORE_U32(maddr, sval);
  3114. shared_memory_unlock(memory);
  3115. }
  3116. PUSH_I32(readv);
  3117. break;
  3118. }
  3119. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
  3120. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
  3121. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
  3122. case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
  3123. {
  3124. uint64 readv, sval, expect;
  3125. sval = (uint64)POP_I64();
  3126. expect = (uint64)POP_I64();
  3127. addr = POP_I32();
  3128. if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
  3129. CHECK_MEMORY_OVERFLOW(1);
  3130. CHECK_ATOMIC_MEMORY_ACCESS(1);
  3131. expect = (uint8)expect;
  3132. shared_memory_lock(memory);
  3133. readv = (uint64)(*(uint8 *)maddr);
  3134. if (readv == expect)
  3135. *(uint8 *)maddr = (uint8)(sval);
  3136. shared_memory_unlock(memory);
  3137. }
  3138. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
  3139. CHECK_MEMORY_OVERFLOW(2);
  3140. CHECK_ATOMIC_MEMORY_ACCESS(2);
  3141. expect = (uint16)expect;
  3142. shared_memory_lock(memory);
  3143. readv = (uint64)LOAD_U16(maddr);
  3144. if (readv == expect)
  3145. STORE_U16(maddr, (uint16)(sval));
  3146. shared_memory_unlock(memory);
  3147. }
  3148. else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
  3149. CHECK_MEMORY_OVERFLOW(4);
  3150. CHECK_ATOMIC_MEMORY_ACCESS(4);
  3151. expect = (uint32)expect;
  3152. shared_memory_lock(memory);
  3153. readv = (uint64)LOAD_U32(maddr);
  3154. if (readv == expect)
  3155. STORE_U32(maddr, (uint32)(sval));
  3156. shared_memory_unlock(memory);
  3157. }
  3158. else {
  3159. CHECK_MEMORY_OVERFLOW(8);
  3160. CHECK_ATOMIC_MEMORY_ACCESS(8);
  3161. shared_memory_lock(memory);
  3162. readv = (uint64)LOAD_I64(maddr);
  3163. if (readv == expect)
  3164. STORE_I64(maddr, sval);
  3165. shared_memory_unlock(memory);
  3166. }
  3167. PUSH_I64(readv);
  3168. break;
  3169. }
  3170. DEF_ATOMIC_RMW_OPCODE(ADD, +);
  3171. DEF_ATOMIC_RMW_OPCODE(SUB, -);
  3172. DEF_ATOMIC_RMW_OPCODE(AND, &);
  3173. DEF_ATOMIC_RMW_OPCODE(OR, |);
  3174. DEF_ATOMIC_RMW_OPCODE(XOR, ^);
  3175. /* xchg, ignore the read value, and store the given
  3176. value: readv * 0 + sval */
  3177. DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
  3178. }
  3179. HANDLE_OP_END();
  3180. }
  3181. #endif
  3182. HANDLE_OP(WASM_OP_IMPDEP)
  3183. {
  3184. frame = prev_frame;
  3185. frame_ip = frame->ip;
  3186. goto call_func_from_entry;
  3187. }
  3188. HANDLE_OP(WASM_OP_CALL)
  3189. {
  3190. #if WASM_ENABLE_THREAD_MGR != 0
  3191. CHECK_SUSPEND_FLAGS();
  3192. #endif
  3193. fidx = read_uint32(frame_ip);
  3194. #if WASM_ENABLE_MULTI_MODULE != 0
  3195. if (fidx >= module->e->function_count) {
  3196. wasm_set_exception(module, "unknown function");
  3197. goto got_exception;
  3198. }
  3199. #endif
  3200. cur_func = module->e->functions + fidx;
  3201. goto call_func_from_interp;
  3202. }
  3203. #if WASM_ENABLE_TAIL_CALL != 0
  3204. HANDLE_OP(WASM_OP_RETURN_CALL)
  3205. {
  3206. #if WASM_ENABLE_THREAD_MGR != 0
  3207. CHECK_SUSPEND_FLAGS();
  3208. #endif
  3209. fidx = read_uint32(frame_ip);
  3210. #if WASM_ENABLE_MULTI_MODULE != 0
  3211. if (fidx >= module->e->function_count) {
  3212. wasm_set_exception(module, "unknown function");
  3213. goto got_exception;
  3214. }
  3215. #endif
  3216. cur_func = module->e->functions + fidx;
  3217. goto call_func_from_return_call;
  3218. }
  3219. #endif /* WASM_ENABLE_TAIL_CALL */
  3220. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3221. default:
  3222. wasm_set_exception(module, "unsupported opcode");
  3223. goto got_exception;
  3224. }
  3225. #endif
  3226. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3227. HANDLE_OP(WASM_OP_UNUSED_0x0a)
  3228. #if WASM_ENABLE_TAIL_CALL == 0
  3229. HANDLE_OP(WASM_OP_RETURN_CALL)
  3230. HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
  3231. #endif
  3232. #if WASM_ENABLE_SHARED_MEMORY == 0
  3233. HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
  3234. #endif
  3235. #if WASM_ENABLE_REF_TYPES == 0
  3236. HANDLE_OP(WASM_OP_TABLE_GET)
  3237. HANDLE_OP(WASM_OP_TABLE_SET)
  3238. HANDLE_OP(WASM_OP_REF_NULL)
  3239. HANDLE_OP(WASM_OP_REF_IS_NULL)
  3240. HANDLE_OP(WASM_OP_REF_FUNC)
  3241. #endif
  3242. #if WASM_ENABLE_EXCE_HANDLING == 0
  3243. /* if exception handling is disabled, these opcodes issue a trap */
  3244. HANDLE_OP(WASM_OP_TRY)
  3245. HANDLE_OP(WASM_OP_CATCH)
  3246. HANDLE_OP(WASM_OP_THROW)
  3247. HANDLE_OP(WASM_OP_RETHROW)
  3248. HANDLE_OP(WASM_OP_DELEGATE)
  3249. HANDLE_OP(WASM_OP_CATCH_ALL)
  3250. HANDLE_OP(EXT_OP_TRY)
  3251. #endif
  3252. /* SELECT_T is converted to SELECT or SELECT_64 */
  3253. HANDLE_OP(WASM_OP_SELECT_T)
  3254. HANDLE_OP(WASM_OP_UNUSED_0x14)
  3255. HANDLE_OP(WASM_OP_UNUSED_0x15)
  3256. HANDLE_OP(WASM_OP_UNUSED_0x16)
  3257. HANDLE_OP(WASM_OP_UNUSED_0x17)
  3258. HANDLE_OP(WASM_OP_UNUSED_0x27)
  3259. /* optimized op code */
  3260. HANDLE_OP(WASM_OP_F32_STORE)
  3261. HANDLE_OP(WASM_OP_F64_STORE)
  3262. HANDLE_OP(WASM_OP_F32_LOAD)
  3263. HANDLE_OP(WASM_OP_F64_LOAD)
  3264. HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
  3265. HANDLE_OP(WASM_OP_GET_LOCAL)
  3266. HANDLE_OP(WASM_OP_DROP)
  3267. HANDLE_OP(WASM_OP_DROP_64)
  3268. HANDLE_OP(WASM_OP_BLOCK)
  3269. HANDLE_OP(WASM_OP_LOOP)
  3270. HANDLE_OP(WASM_OP_END)
  3271. HANDLE_OP(WASM_OP_NOP)
  3272. HANDLE_OP(EXT_OP_BLOCK)
  3273. HANDLE_OP(EXT_OP_LOOP)
  3274. HANDLE_OP(EXT_OP_IF)
  3275. HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
  3276. {
  3277. wasm_set_exception(module, "unsupported opcode");
  3278. goto got_exception;
  3279. }
  3280. #endif
  3281. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3282. continue;
  3283. #else
  3284. FETCH_OPCODE_AND_DISPATCH();
  3285. #endif
  3286. #if WASM_ENABLE_TAIL_CALL != 0
  3287. call_func_from_return_call:
  3288. {
  3289. uint32 *lp_base;
  3290. uint32 *lp;
  3291. int i;
  3292. if (!(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
  3293. * sizeof(uint32)))) {
  3294. wasm_set_exception(module, "allocate memory failed");
  3295. goto got_exception;
  3296. }
  3297. for (i = 0; i < cur_func->param_count; i++) {
  3298. if (cur_func->param_types[i] == VALUE_TYPE_I64
  3299. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  3300. PUT_I64_TO_ADDR(
  3301. lp, GET_OPERAND(uint64, I64,
  3302. 2 * (cur_func->param_count - i - 1)));
  3303. lp += 2;
  3304. }
  3305. else {
  3306. *lp = GET_OPERAND(uint32, I32,
  3307. (2 * (cur_func->param_count - i - 1)));
  3308. lp++;
  3309. }
  3310. }
  3311. frame->lp = frame->operand + cur_func->const_cell_num;
  3312. if (lp - lp_base > 0) {
  3313. word_copy(frame->lp, lp_base, lp - lp_base);
  3314. }
  3315. wasm_runtime_free(lp_base);
  3316. FREE_FRAME(exec_env, frame);
  3317. frame_ip += cur_func->param_count * sizeof(int16);
  3318. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3319. goto call_func_from_entry;
  3320. }
  3321. #endif /* WASM_ENABLE_TAIL_CALL */
  3322. call_func_from_interp:
  3323. {
  3324. /* Only do the copy when it's called from interpreter. */
  3325. WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3326. int i;
  3327. #if WASM_ENABLE_MULTI_MODULE != 0
  3328. if (cur_func->is_import_func) {
  3329. outs_area->lp = outs_area->operand
  3330. + (cur_func->import_func_inst
  3331. ? cur_func->import_func_inst->const_cell_num
  3332. : 0);
  3333. }
  3334. else
  3335. #endif
  3336. {
  3337. outs_area->lp = outs_area->operand + cur_func->const_cell_num;
  3338. }
  3339. if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
  3340. > exec_env->wasm_stack.s.top_boundary) {
  3341. wasm_set_exception(module, "wasm operand stack overflow");
  3342. goto got_exception;
  3343. }
  3344. for (i = 0; i < cur_func->param_count; i++) {
  3345. if (cur_func->param_types[i] == VALUE_TYPE_I64
  3346. || cur_func->param_types[i] == VALUE_TYPE_F64) {
  3347. PUT_I64_TO_ADDR(
  3348. outs_area->lp,
  3349. GET_OPERAND(uint64, I64,
  3350. 2 * (cur_func->param_count - i - 1)));
  3351. outs_area->lp += 2;
  3352. }
  3353. else {
  3354. *outs_area->lp = GET_OPERAND(
  3355. uint32, I32, (2 * (cur_func->param_count - i - 1)));
  3356. outs_area->lp++;
  3357. }
  3358. }
  3359. frame_ip += cur_func->param_count * sizeof(int16);
  3360. if (cur_func->ret_cell_num != 0) {
  3361. /* Get the first return value's offset. Since loader emit
  3362. * all return values' offset so we must skip remain return
  3363. * values' offsets.
  3364. */
  3365. WASMType *func_type;
  3366. if (cur_func->is_import_func)
  3367. func_type = cur_func->u.func_import->func_type;
  3368. else
  3369. func_type = cur_func->u.func->func_type;
  3370. frame->ret_offset = GET_OFFSET();
  3371. frame_ip += 2 * (func_type->result_count - 1);
  3372. }
  3373. SYNC_ALL_TO_FRAME();
  3374. prev_frame = frame;
  3375. }
  3376. call_func_from_entry:
  3377. {
  3378. if (cur_func->is_import_func) {
  3379. #if WASM_ENABLE_MULTI_MODULE != 0
  3380. if (cur_func->import_func_inst) {
  3381. wasm_interp_call_func_import(module, exec_env, cur_func,
  3382. prev_frame);
  3383. }
  3384. else
  3385. #endif
  3386. {
  3387. wasm_interp_call_func_native(module, exec_env, cur_func,
  3388. prev_frame);
  3389. }
  3390. prev_frame = frame->prev_frame;
  3391. cur_func = frame->function;
  3392. UPDATE_ALL_FROM_FRAME();
  3393. /* update memory size, no need to update memory ptr as
  3394. it isn't changed in wasm_enlarge_memory */
  3395. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3396. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3397. || WASM_ENABLE_BULK_MEMORY != 0
  3398. if (memory)
  3399. linear_mem_size = get_linear_mem_size();
  3400. #endif
  3401. if (wasm_copy_exception(module, NULL))
  3402. goto got_exception;
  3403. }
  3404. else {
  3405. WASMFunction *cur_wasm_func = cur_func->u.func;
  3406. all_cell_num = cur_func->param_cell_num + cur_func->local_cell_num
  3407. + cur_func->const_cell_num
  3408. + cur_wasm_func->max_stack_cell_num;
  3409. /* param_cell_num, local_cell_num, const_cell_num and
  3410. max_stack_cell_num are all no larger than UINT16_MAX (checked
  3411. in loader), all_cell_num must be smaller than 1MB */
  3412. bh_assert(all_cell_num < 1 * BH_MB);
  3413. frame_size = wasm_interp_interp_frame_size(all_cell_num);
  3414. if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
  3415. frame = prev_frame;
  3416. goto got_exception;
  3417. }
  3418. /* Initialize the interpreter context. */
  3419. frame->function = cur_func;
  3420. frame_ip = wasm_get_func_code(cur_func);
  3421. frame_ip_end = wasm_get_func_code_end(cur_func);
  3422. frame_lp = frame->lp =
  3423. frame->operand + cur_wasm_func->const_cell_num;
  3424. /* Initialize the consts */
  3425. if (cur_wasm_func->const_cell_num > 0) {
  3426. word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
  3427. cur_wasm_func->const_cell_num);
  3428. }
  3429. /* Initialize the local variables */
  3430. memset(frame_lp + cur_func->param_cell_num, 0,
  3431. (uint32)(cur_func->local_cell_num * 4));
  3432. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
  3433. }
  3434. #if WASM_ENABLE_THREAD_MGR != 0
  3435. CHECK_SUSPEND_FLAGS();
  3436. #endif
  3437. HANDLE_OP_END();
  3438. }
  3439. return_func:
  3440. {
  3441. FREE_FRAME(exec_env, frame);
  3442. wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
  3443. if (!prev_frame->ip)
  3444. /* Called from native. */
  3445. return;
  3446. RECOVER_CONTEXT(prev_frame);
  3447. HANDLE_OP_END();
  3448. }
  3449. (void)frame_ip_end;
  3450. #if WASM_ENABLE_SHARED_MEMORY != 0
  3451. unaligned_atomic:
  3452. wasm_set_exception(module, "unaligned atomic");
  3453. goto got_exception;
  3454. #endif
  3455. #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
  3456. || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
  3457. || WASM_ENABLE_BULK_MEMORY != 0
  3458. out_of_bounds:
  3459. wasm_set_exception(module, "out of bounds memory access");
  3460. #endif
  3461. got_exception:
  3462. SYNC_ALL_TO_FRAME();
  3463. return;
  3464. #if WASM_ENABLE_LABELS_AS_VALUES == 0
  3465. }
  3466. #else
  3467. FETCH_OPCODE_AND_DISPATCH();
  3468. #endif
  3469. }
  3470. #if WASM_ENABLE_LABELS_AS_VALUES != 0
  3471. void **
  3472. wasm_interp_get_handle_table()
  3473. {
  3474. WASMModuleInstance module;
  3475. memset(&module, 0, sizeof(WASMModuleInstance));
  3476. wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
  3477. return global_handle_table;
  3478. }
  3479. #endif
  3480. void
  3481. wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
  3482. WASMFunctionInstance *function, uint32 argc,
  3483. uint32 argv[])
  3484. {
  3485. WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
  3486. WASMInterpFrame *frame, *outs_area;
  3487. /* Allocate sufficient cells for all kinds of return values. */
  3488. unsigned all_cell_num =
  3489. function->ret_cell_num > 2 ? function->ret_cell_num : 2,
  3490. i;
  3491. /* This frame won't be used by JITed code, so only allocate interp
  3492. frame here. */
  3493. unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
  3494. if (argc < function->param_cell_num) {
  3495. char buf[128];
  3496. snprintf(buf, sizeof(buf),
  3497. "invalid argument count %" PRIu32
  3498. ", must be no smaller than %" PRIu32,
  3499. argc, (uint32)function->param_cell_num);
  3500. wasm_set_exception(module_inst, buf);
  3501. return;
  3502. }
  3503. argc = function->param_cell_num;
  3504. RECORD_STACK_USAGE(exec_env, (uint8 *)&prev_frame);
  3505. #if !(defined(OS_ENABLE_HW_BOUND_CHECK) \
  3506. && WASM_DISABLE_STACK_HW_BOUND_CHECK == 0)
  3507. if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
  3508. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3509. "native stack overflow");
  3510. return;
  3511. }
  3512. #endif
  3513. if (!(frame =
  3514. ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
  3515. return;
  3516. outs_area = wasm_exec_env_wasm_stack_top(exec_env);
  3517. frame->function = NULL;
  3518. frame->ip = NULL;
  3519. /* There is no local variable. */
  3520. frame->lp = frame->operand + 0;
  3521. frame->ret_offset = 0;
  3522. if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
  3523. > exec_env->wasm_stack.s.top_boundary) {
  3524. wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
  3525. "wasm operand stack overflow");
  3526. return;
  3527. }
  3528. if (argc > 0)
  3529. word_copy(outs_area->operand + function->const_cell_num, argv, argc);
  3530. wasm_exec_env_set_cur_frame(exec_env, frame);
  3531. #if defined(os_writegsbase)
  3532. {
  3533. WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
  3534. if (memory_inst)
  3535. /* write base addr of linear memory to GS segment register */
  3536. os_writegsbase(memory_inst->memory_data);
  3537. }
  3538. #endif
  3539. if (function->is_import_func) {
  3540. #if WASM_ENABLE_MULTI_MODULE != 0
  3541. if (function->import_module_inst) {
  3542. LOG_DEBUG("it is a function of a sub module");
  3543. wasm_interp_call_func_import(module_inst, exec_env, function,
  3544. frame);
  3545. }
  3546. else
  3547. #endif
  3548. {
  3549. LOG_DEBUG("it is an native function");
  3550. wasm_interp_call_func_native(module_inst, exec_env, function,
  3551. frame);
  3552. }
  3553. }
  3554. else {
  3555. wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
  3556. }
  3557. /* Output the return value to the caller */
  3558. if (!wasm_copy_exception(module_inst, NULL)) {
  3559. for (i = 0; i < function->ret_cell_num; i++)
  3560. argv[i] = *(frame->lp + i);
  3561. }
  3562. else {
  3563. #if WASM_ENABLE_DUMP_CALL_STACK != 0
  3564. if (wasm_interp_create_call_stack(exec_env)) {
  3565. wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
  3566. }
  3567. #endif
  3568. }
  3569. wasm_exec_env_set_cur_frame(exec_env, prev_frame);
  3570. FREE_FRAME(exec_env, frame);
  3571. #if WASM_ENABLE_OPCODE_COUNTER != 0
  3572. wasm_interp_dump_op_count();
  3573. #endif
  3574. }