wasm_interp_fast.c 143 KB

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