wasm_interp_classic.c 135 KB

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