wasm_interp_fast.c 142 KB

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