wasm_interp_fast.c 142 KB

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