wasm_interp_classic.c 146 KB

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