wasm_interp_fast.c 135 KB

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