wasm_interp_classic.c 153 KB

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