wasm_interp_classic.c 152 KB

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