wasm_interp_classic.c 195 KB

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