| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "wasm_interp.h"
- #include "bh_log.h"
- #include "wasm_runtime.h"
- #include "wasm_opcode.h"
- #include "wasm_loader.h"
- #include "../common/wasm_exec_env.h"
- #if WASM_ENABLE_SHARED_MEMORY != 0
- #include "../common/wasm_shared_memory.h"
- #endif
- typedef int32 CellType_I32;
- typedef int64 CellType_I64;
- typedef float32 CellType_F32;
- typedef float64 CellType_F64;
- #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
- || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
- #define CHECK_MEMORY_OVERFLOW(bytes) \
- do { \
- uint64 offset1 = (uint64)offset + (uint64)addr; \
- if (offset1 + bytes <= (uint64)linear_mem_size) \
- /* If offset1 is in valid range, maddr must also \
- be in valid range, no need to check it again. */ \
- maddr = memory->memory_data + offset1; \
- else \
- goto out_of_bounds; \
- } while (0)
- #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
- do { \
- uint64 offset1 = (uint32)(start); \
- if (offset1 + bytes <= linear_mem_size) \
- /* App heap space is not valid space for \
- bulk memory operation */ \
- maddr = memory->memory_data + offset1; \
- else \
- goto out_of_bounds; \
- } while (0)
- #else
- #define CHECK_MEMORY_OVERFLOW(bytes) \
- do { \
- uint64 offset1 = (uint64)offset + (uint64)addr; \
- maddr = memory->memory_data + offset1; \
- } while (0)
- #define CHECK_BULK_MEMORY_OVERFLOW(start, bytes, maddr) \
- do { \
- maddr = memory->memory_data + (uint32)(start); \
- } while (0)
- #endif /* !defined(OS_ENABLE_HW_BOUND_CHECK) \
- || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 */
- #define CHECK_ATOMIC_MEMORY_ACCESS(align) \
- do { \
- if (((uintptr_t)maddr & (align - 1)) != 0) \
- goto unaligned_atomic; \
- } while (0)
- static inline uint32
- rotl32(uint32 n, uint32 c)
- {
- const uint32 mask = (31);
- c = c % 32;
- c &= mask;
- return (n << c) | (n >> ((0 - c) & mask));
- }
- static inline uint32
- rotr32(uint32 n, uint32 c)
- {
- const uint32 mask = (31);
- c = c % 32;
- c &= mask;
- return (n >> c) | (n << ((0 - c) & mask));
- }
- static inline uint64
- rotl64(uint64 n, uint64 c)
- {
- const uint64 mask = (63);
- c = c % 64;
- c &= mask;
- return (n << c) | (n >> ((0 - c) & mask));
- }
- static inline uint64
- rotr64(uint64 n, uint64 c)
- {
- const uint64 mask = (63);
- c = c % 64;
- c &= mask;
- return (n >> c) | (n << ((0 - c) & mask));
- }
- static inline double
- wa_fmax(double a, double b)
- {
- double c = fmax(a, b);
- if (c == 0 && a == b)
- return signbit(a) ? b : a;
- return c;
- }
- static inline double
- wa_fmin(double a, double b)
- {
- double c = fmin(a, b);
- if (c == 0 && a == b)
- return signbit(a) ? a : b;
- return c;
- }
- static inline uint32
- clz32(uint32 type)
- {
- uint32 num = 0;
- if (type == 0)
- return 32;
- while (!(type & 0x80000000)) {
- num++;
- type <<= 1;
- }
- return num;
- }
- static inline uint32
- clz64(uint64 type)
- {
- uint32 num = 0;
- if (type == 0)
- return 64;
- while (!(type & 0x8000000000000000LL)) {
- num++;
- type <<= 1;
- }
- return num;
- }
- static inline uint32
- ctz32(uint32 type)
- {
- uint32 num = 0;
- if (type == 0)
- return 32;
- while (!(type & 1)) {
- num++;
- type >>= 1;
- }
- return num;
- }
- static inline uint32
- ctz64(uint64 type)
- {
- uint32 num = 0;
- if (type == 0)
- return 64;
- while (!(type & 1)) {
- num++;
- type >>= 1;
- }
- return num;
- }
- static inline uint32
- popcount32(uint32 u)
- {
- uint32 ret = 0;
- while (u) {
- u = (u & (u - 1));
- ret++;
- }
- return ret;
- }
- static inline uint32
- popcount64(uint64 u)
- {
- uint32 ret = 0;
- while (u) {
- u = (u & (u - 1));
- ret++;
- }
- return ret;
- }
- static float
- local_copysignf(float x, float y)
- {
- union {
- float f;
- uint32_t i;
- } ux = { x }, uy = { y };
- ux.i &= 0x7fffffff;
- ux.i |= uy.i & 0x80000000;
- return ux.f;
- }
- static double
- local_copysign(double x, double y)
- {
- union {
- double f;
- uint64_t i;
- } ux = { x }, uy = { y };
- ux.i &= -1ULL / 2;
- ux.i |= uy.i & 1ULL << 63;
- return ux.f;
- }
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
- #define LOAD_U32_WITH_2U16S(addr) (*(uint32 *)(addr))
- #define LOAD_PTR(addr) (*(void **)(addr))
- #else /* else of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
- static inline uint32
- LOAD_U32_WITH_2U16S(void *addr)
- {
- union {
- uint32 val;
- uint16 u16[2];
- } u;
- bh_assert(((uintptr_t)addr & 1) == 0);
- u.u16[0] = ((uint16 *)addr)[0];
- u.u16[1] = ((uint16 *)addr)[1];
- return u.val;
- }
- #if UINTPTR_MAX == UINT32_MAX
- #define LOAD_PTR(addr) ((void *)LOAD_U32_WITH_2U16S(addr))
- #elif UINTPTR_MAX == UINT64_MAX
- static inline void *
- LOAD_PTR(void *addr)
- {
- uintptr_t addr1 = (uintptr_t)addr;
- union {
- void *val;
- uint32 u32[2];
- uint16 u16[4];
- } u;
- bh_assert(((uintptr_t)addr & 1) == 0);
- if ((addr1 & (uintptr_t)7) == 0)
- return *(void **)addr;
- if ((addr1 & (uintptr_t)3) == 0) {
- u.u32[0] = ((uint32 *)addr)[0];
- u.u32[1] = ((uint32 *)addr)[1];
- }
- else {
- u.u16[0] = ((uint16 *)addr)[0];
- u.u16[1] = ((uint16 *)addr)[1];
- u.u16[2] = ((uint16 *)addr)[2];
- u.u16[3] = ((uint16 *)addr)[3];
- }
- return u.val;
- }
- #endif /* end of UINTPTR_MAX */
- #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
- #define read_uint32(p) \
- (p += sizeof(uint32), LOAD_U32_WITH_2U16S(p - sizeof(uint32)))
- #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() \
- do { \
- uint32 param_count = cur_func->param_count; \
- local_idx = read_uint32(frame_ip); \
- bh_assert(local_idx < param_count + cur_func->local_count); \
- local_offset = cur_func->local_offsets[local_idx]; \
- if (local_idx < param_count) \
- local_type = cur_func->param_types[local_idx]; \
- else \
- local_type = cur_func->local_types[local_idx - param_count]; \
- } while (0)
- #define GET_OFFSET() (frame_ip += 2, *(int16 *)(frame_ip - 2))
- #define SET_OPERAND_I32(off, value) \
- do { \
- *(uint32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
- } while (0)
- #define SET_OPERAND_F32(off, value) \
- do { \
- *(float32 *)(frame_lp + *(int16 *)(frame_ip + off)) = value; \
- } while (0)
- #define SET_OPERAND_I64(off, value) \
- do { \
- uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
- PUT_I64_TO_ADDR(addr_tmp, value); \
- } while (0)
- #define SET_OPERAND_F64(off, value) \
- do { \
- uint32 *addr_tmp = frame_lp + *(int16 *)(frame_ip + off); \
- PUT_F64_TO_ADDR(addr_tmp, value); \
- } while (0)
- #define SET_OPERAND(op_type, off, value) SET_OPERAND_##op_type(off, value)
- #define GET_OPERAND_I32(type, off) \
- *(type *)(frame_lp + *(int16 *)(frame_ip + off))
- #define GET_OPERAND_F32(type, off) \
- *(type *)(frame_lp + *(int16 *)(frame_ip + off))
- #define GET_OPERAND_I64(type, off) \
- (type) GET_I64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
- #define GET_OPERAND_F64(type, off) \
- (type) GET_F64_FROM_ADDR(frame_lp + *(int16 *)(frame_ip + off))
- #define GET_OPERAND(type, op_type, off) GET_OPERAND_##op_type(type, off)
- #define PUSH_I32(value) \
- do { \
- *(int32 *)(frame_lp + GET_OFFSET()) = value; \
- } while (0)
- #define PUSH_F32(value) \
- do { \
- *(float32 *)(frame_lp + GET_OFFSET()) = value; \
- } while (0)
- #define PUSH_I64(value) \
- do { \
- uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
- PUT_I64_TO_ADDR(addr_tmp, value); \
- } while (0)
- #define PUSH_F64(value) \
- do { \
- uint32 *addr_tmp = frame_lp + GET_OFFSET(); \
- PUT_F64_TO_ADDR(addr_tmp, value); \
- } while (0)
- #define POP_I32() (*(int32 *)(frame_lp + GET_OFFSET()))
- #define POP_F32() (*(float32 *)(frame_lp + GET_OFFSET()))
- #define POP_I64() (GET_I64_FROM_ADDR(frame_lp + GET_OFFSET()))
- #define POP_F64() (GET_F64_FROM_ADDR(frame_lp + GET_OFFSET()))
- #define SYNC_ALL_TO_FRAME() \
- do { \
- frame->ip = frame_ip; \
- } while (0)
- #define UPDATE_ALL_FROM_FRAME() \
- do { \
- frame_ip = frame->ip; \
- } while (0)
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- #define UPDATE_FRAME_IP_END() (void)0
- #else
- #define UPDATE_FRAME_IP_END() frame_ip_end = wasm_get_func_code_end(cur_func)
- #endif
- #define RECOVER_CONTEXT(new_frame) \
- do { \
- frame = (new_frame); \
- cur_func = frame->function; \
- prev_frame = frame->prev_frame; \
- frame_ip = frame->ip; \
- UPDATE_FRAME_IP_END(); \
- frame_lp = frame->lp; \
- } while (0)
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
- #define GET_OPCODE() opcode = *frame_ip++;
- #else
- #define GET_OPCODE() \
- opcode = *frame_ip; \
- frame_ip += 2;
- #endif
- #define DEF_OP_EQZ(ctype, src_op_type) \
- do { \
- SET_OPERAND(I32, 2, (GET_OPERAND(ctype, src_op_type, 0) == 0)); \
- frame_ip += 4; \
- } while (0)
- #define DEF_OP_CMP(src_type, src_op_type, cond) \
- do { \
- SET_OPERAND(I32, 4, \
- GET_OPERAND(src_type, src_op_type, 2) \
- cond GET_OPERAND(src_type, src_op_type, 0)); \
- frame_ip += 6; \
- } while (0)
- #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) \
- do { \
- SET_OPERAND( \
- src_op_type, 2, \
- (src_type)operation(GET_OPERAND(src_type, src_op_type, 0))); \
- frame_ip += 4; \
- } while (0)
- #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) \
- do { \
- SET_OPERAND(src_op_type, 4, \
- GET_OPERAND(src_type1, src_op_type, 2) \
- operation GET_OPERAND(src_type2, src_op_type, 0)); \
- frame_ip += 6; \
- } while (0)
- #define DEF_OP_REINTERPRET(src_type, src_op_type) \
- do { \
- SET_OPERAND(src_op_type, 2, GET_OPERAND(src_type, src_op_type, 0)); \
- frame_ip += 4; \
- } while (0)
- #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
- #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) \
- do { \
- SET_OPERAND(src_op_type, 4, \
- GET_OPERAND(src_type1, src_op_type, 2) operation( \
- GET_OPERAND(src_type2, src_op_type, 0) % 32)); \
- frame_ip += 6; \
- } while (0)
- #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) \
- do { \
- SET_OPERAND(src_op_type, 4, \
- GET_OPERAND(src_type1, src_op_type, 2) operation( \
- GET_OPERAND(src_type2, src_op_type, 0) % 64)); \
- frame_ip += 6; \
- } while (0)
- #define DEF_ATOMIC_RMW_OPCODE(OP_NAME, op) \
- case WASM_OP_ATOMIC_RMW_I32_##OP_NAME: \
- case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U: \
- case WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U: \
- { \
- uint32 readv, sval; \
- \
- sval = POP_I32(); \
- addr = POP_I32(); \
- \
- if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##8_U) { \
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
- CHECK_ATOMIC_MEMORY_ACCESS(1); \
- \
- os_mutex_lock(&module->e->mem_lock); \
- readv = (uint32)(*(uint8 *)maddr); \
- *(uint8 *)maddr = (uint8)(readv op sval); \
- os_mutex_unlock(&module->e->mem_lock); \
- } \
- else if (opcode == WASM_OP_ATOMIC_RMW_I32_##OP_NAME##16_U) { \
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
- CHECK_ATOMIC_MEMORY_ACCESS(2); \
- \
- os_mutex_lock(&module->e->mem_lock); \
- readv = (uint32)LOAD_U16(maddr); \
- STORE_U16(maddr, (uint16)(readv op sval)); \
- os_mutex_unlock(&module->e->mem_lock); \
- } \
- else { \
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
- CHECK_ATOMIC_MEMORY_ACCESS(4); \
- \
- os_mutex_lock(&module->e->mem_lock); \
- readv = LOAD_I32(maddr); \
- STORE_U32(maddr, readv op sval); \
- os_mutex_unlock(&module->e->mem_lock); \
- } \
- PUSH_I32(readv); \
- break; \
- } \
- case WASM_OP_ATOMIC_RMW_I64_##OP_NAME: \
- case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U: \
- case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U: \
- case WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U: \
- { \
- uint64 readv, sval; \
- \
- sval = (uint64)POP_I64(); \
- addr = POP_I32(); \
- \
- if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##8_U) { \
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr); \
- CHECK_ATOMIC_MEMORY_ACCESS(1); \
- \
- os_mutex_lock(&module->e->mem_lock); \
- readv = (uint64)(*(uint8 *)maddr); \
- *(uint8 *)maddr = (uint8)(readv op sval); \
- os_mutex_unlock(&module->e->mem_lock); \
- } \
- else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##16_U) { \
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr); \
- CHECK_ATOMIC_MEMORY_ACCESS(2); \
- \
- os_mutex_lock(&module->e->mem_lock); \
- readv = (uint64)LOAD_U16(maddr); \
- STORE_U16(maddr, (uint16)(readv op sval)); \
- os_mutex_unlock(&module->e->mem_lock); \
- } \
- else if (opcode == WASM_OP_ATOMIC_RMW_I64_##OP_NAME##32_U) { \
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr); \
- CHECK_ATOMIC_MEMORY_ACCESS(4); \
- \
- os_mutex_lock(&module->e->mem_lock); \
- readv = (uint64)LOAD_U32(maddr); \
- STORE_U32(maddr, (uint32)(readv op sval)); \
- os_mutex_unlock(&module->e->mem_lock); \
- } \
- else { \
- uint64 op_result; \
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr); \
- CHECK_ATOMIC_MEMORY_ACCESS(8); \
- \
- os_mutex_lock(&module->e->mem_lock); \
- readv = (uint64)LOAD_I64(maddr); \
- op_result = readv op sval; \
- STORE_I64(maddr, op_result); \
- os_mutex_unlock(&module->e->mem_lock); \
- } \
- PUSH_I64(readv); \
- break; \
- }
- #define DEF_OP_MATH(src_type, src_op_type, method) \
- do { \
- SET_OPERAND(src_op_type, 2, \
- (src_type)method(GET_OPERAND(src_type, src_op_type, 0))); \
- frame_ip += 4; \
- } while (0)
- #define TRUNC_FUNCTION(func_name, src_type, dst_type, signed_type) \
- static dst_type func_name(src_type src_value, src_type src_min, \
- src_type src_max, dst_type dst_min, \
- dst_type dst_max, bool is_sign) \
- { \
- dst_type dst_value = 0; \
- if (!isnan(src_value)) { \
- if (src_value <= src_min) \
- dst_value = dst_min; \
- else if (src_value >= src_max) \
- dst_value = dst_max; \
- else { \
- if (is_sign) \
- dst_value = (dst_type)(signed_type)src_value; \
- else \
- dst_value = (dst_type)src_value; \
- } \
- } \
- return dst_value; \
- }
- TRUNC_FUNCTION(trunc_f32_to_i32, float32, uint32, int32)
- TRUNC_FUNCTION(trunc_f32_to_i64, float32, uint64, int64)
- TRUNC_FUNCTION(trunc_f64_to_i32, float64, uint32, int32)
- TRUNC_FUNCTION(trunc_f64_to_i64, float64, uint64, int64)
- static bool
- trunc_f32_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
- float32 src_min, float32 src_max, bool saturating, bool is_i32,
- bool is_sign)
- {
- float32 src_value = GET_OPERAND(float32, F32, 0);
- uint64 dst_value_i64;
- uint32 dst_value_i32;
- if (!saturating) {
- if (isnan(src_value)) {
- wasm_set_exception(module, "invalid conversion to integer");
- return true;
- }
- else if (src_value <= src_min || src_value >= src_max) {
- wasm_set_exception(module, "integer overflow");
- return true;
- }
- }
- if (is_i32) {
- uint32 dst_min = is_sign ? INT32_MIN : 0;
- uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
- dst_value_i32 = trunc_f32_to_i32(src_value, src_min, src_max, dst_min,
- dst_max, is_sign);
- SET_OPERAND(I32, 2, dst_value_i32);
- }
- else {
- uint64 dst_min = is_sign ? INT64_MIN : 0;
- uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
- dst_value_i64 = trunc_f32_to_i64(src_value, src_min, src_max, dst_min,
- dst_max, is_sign);
- SET_OPERAND(I64, 2, dst_value_i64);
- }
- return false;
- }
- static bool
- trunc_f64_to_int(WASMModuleInstance *module, uint8 *frame_ip, uint32 *frame_lp,
- float64 src_min, float64 src_max, bool saturating, bool is_i32,
- bool is_sign)
- {
- float64 src_value = GET_OPERAND(float64, F64, 0);
- uint64 dst_value_i64;
- uint32 dst_value_i32;
- if (!saturating) {
- if (isnan(src_value)) {
- wasm_set_exception(module, "invalid conversion to integer");
- return true;
- }
- else if (src_value <= src_min || src_value >= src_max) {
- wasm_set_exception(module, "integer overflow");
- return true;
- }
- }
- if (is_i32) {
- uint32 dst_min = is_sign ? INT32_MIN : 0;
- uint32 dst_max = is_sign ? INT32_MAX : UINT32_MAX;
- dst_value_i32 = trunc_f64_to_i32(src_value, src_min, src_max, dst_min,
- dst_max, is_sign);
- SET_OPERAND(I32, 2, dst_value_i32);
- }
- else {
- uint64 dst_min = is_sign ? INT64_MIN : 0;
- uint64 dst_max = is_sign ? INT64_MAX : UINT64_MAX;
- dst_value_i64 = trunc_f64_to_i64(src_value, src_min, src_max, dst_min,
- dst_max, is_sign);
- SET_OPERAND(I64, 2, dst_value_i64);
- }
- return false;
- }
- #define DEF_OP_TRUNC_F32(min, max, is_i32, is_sign) \
- do { \
- if (trunc_f32_to_int(module, frame_ip, frame_lp, min, max, false, \
- is_i32, is_sign)) \
- goto got_exception; \
- frame_ip += 4; \
- } while (0)
- #define DEF_OP_TRUNC_F64(min, max, is_i32, is_sign) \
- do { \
- if (trunc_f64_to_int(module, frame_ip, frame_lp, min, max, false, \
- is_i32, is_sign)) \
- goto got_exception; \
- frame_ip += 4; \
- } while (0)
- #define DEF_OP_TRUNC_SAT_F32(min, max, is_i32, is_sign) \
- do { \
- (void)trunc_f32_to_int(module, frame_ip, frame_lp, min, max, true, \
- is_i32, is_sign); \
- frame_ip += 4; \
- } while (0)
- #define DEF_OP_TRUNC_SAT_F64(min, max, is_i32, is_sign) \
- do { \
- (void)trunc_f64_to_int(module, frame_ip, frame_lp, min, max, true, \
- is_i32, is_sign); \
- frame_ip += 4; \
- } while (0)
- #define DEF_OP_CONVERT(dst_type, dst_op_type, src_type, src_op_type) \
- do { \
- dst_type value = (dst_type)(src_type)POP_##src_op_type(); \
- PUSH_##dst_op_type(value); \
- } while (0)
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
- #define CELL_SIZE sizeof(uint8)
- #else
- #define CELL_SIZE (sizeof(uint8) * 2)
- #endif
- static bool
- copy_stack_values(WASMModuleInstance *module, uint32 *frame_lp, uint32 arity,
- uint32 total_cell_num, const uint8 *cells,
- const int16 *src_offsets, const uint16 *dst_offsets)
- {
- /* To avoid the overlap issue between src offsets and dst offset,
- * we use 2 steps to do the copy. First step, copy the src values
- * to a tmp buf. Second step, copy the values from tmp buf to dst.
- */
- uint32 buf[16] = { 0 }, i;
- uint32 *tmp_buf = buf;
- uint8 cell;
- int16 src, buf_index = 0;
- uint16 dst;
- /* Allocate memory if the buf is not large enough */
- if (total_cell_num > sizeof(buf) / sizeof(uint32)) {
- uint64 total_size = sizeof(uint32) * (uint64)total_cell_num;
- if (total_size >= UINT32_MAX
- || !(tmp_buf = wasm_runtime_malloc((uint32)total_size))) {
- wasm_set_exception(module, "allocate memory failed");
- return false;
- }
- }
- /* 1) Copy values from src to tmp buf */
- for (i = 0; i < arity; i++) {
- cell = cells[i * CELL_SIZE];
- src = src_offsets[i];
- if (cell == 1)
- tmp_buf[buf_index] = frame_lp[src];
- else {
- tmp_buf[buf_index] = frame_lp[src];
- tmp_buf[buf_index + 1] = frame_lp[src + 1];
- }
- buf_index += cell;
- }
- /* 2) Copy values from tmp buf to dest */
- buf_index = 0;
- for (i = 0; i < arity; i++) {
- cell = cells[i * CELL_SIZE];
- dst = dst_offsets[i];
- if (cell == 1)
- frame_lp[dst] = tmp_buf[buf_index];
- else {
- frame_lp[dst] = tmp_buf[buf_index];
- frame_lp[dst + 1] = tmp_buf[buf_index + 1];
- }
- buf_index += cell;
- }
- if (tmp_buf != buf) {
- wasm_runtime_free(tmp_buf);
- }
- return true;
- }
- #define RECOVER_BR_INFO() \
- do { \
- uint32 arity; \
- /* read arity */ \
- arity = read_uint32(frame_ip); \
- if (arity) { \
- uint32 total_cell; \
- uint16 *dst_offsets = NULL; \
- uint8 *cells; \
- int16 *src_offsets = NULL; \
- /* read total cell num */ \
- total_cell = read_uint32(frame_ip); \
- /* cells */ \
- cells = (uint8 *)frame_ip; \
- frame_ip += arity * CELL_SIZE; \
- /* src offsets */ \
- src_offsets = (int16 *)frame_ip; \
- frame_ip += arity * sizeof(int16); \
- /* dst offsets */ \
- dst_offsets = (uint16 *)frame_ip; \
- frame_ip += arity * sizeof(uint16); \
- if (arity == 1) { \
- if (cells[0] == 1) \
- frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
- else if (cells[0] == 2) { \
- frame_lp[dst_offsets[0]] = frame_lp[src_offsets[0]]; \
- frame_lp[dst_offsets[0] + 1] = \
- frame_lp[src_offsets[0] + 1]; \
- } \
- } \
- else { \
- if (!copy_stack_values(module, frame_lp, arity, total_cell, \
- cells, src_offsets, dst_offsets)) \
- goto got_exception; \
- } \
- } \
- frame_ip = (uint8 *)LOAD_PTR(frame_ip); \
- } while (0)
- #define SKIP_BR_INFO() \
- do { \
- uint32 arity; \
- /* read and skip arity */ \
- arity = read_uint32(frame_ip); \
- if (arity) { \
- /* skip total cell num */ \
- frame_ip += sizeof(uint32); \
- /* skip cells, src offsets and dst offsets */ \
- frame_ip += (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity; \
- } \
- /* skip target address */ \
- frame_ip += sizeof(uint8 *); \
- } while (0)
- static inline int32
- sign_ext_8_32(int8 val)
- {
- if (val & 0x80)
- return (int32)val | (int32)0xffffff00;
- return val;
- }
- static inline int32
- sign_ext_16_32(int16 val)
- {
- if (val & 0x8000)
- return (int32)val | (int32)0xffff0000;
- return val;
- }
- static inline int64
- sign_ext_8_64(int8 val)
- {
- if (val & 0x80)
- return (int64)val | (int64)0xffffffffffffff00LL;
- return val;
- }
- static inline int64
- sign_ext_16_64(int16 val)
- {
- if (val & 0x8000)
- return (int64)val | (int64)0xffffffffffff0000LL;
- return val;
- }
- static inline int64
- sign_ext_32_64(int32 val)
- {
- if (val & (int32)0x80000000)
- return (int64)val | (int64)0xffffffff00000000LL;
- return val;
- }
- static inline void
- word_copy(uint32 *dest, uint32 *src, unsigned num)
- {
- bh_assert(dest != NULL);
- bh_assert(src != NULL);
- bh_assert(num > 0);
- if (dest != src) {
- /* No overlap buffer */
- bh_assert(!((src < dest) && (dest < src + num)));
- for (; num > 0; num--)
- *dest++ = *src++;
- }
- }
- static inline WASMInterpFrame *
- ALLOC_FRAME(WASMExecEnv *exec_env, uint32 size, WASMInterpFrame *prev_frame)
- {
- WASMInterpFrame *frame = wasm_exec_env_alloc_wasm_frame(exec_env, size);
- if (frame) {
- frame->prev_frame = prev_frame;
- #if WASM_ENABLE_PERF_PROFILING != 0
- frame->time_started = os_time_get_boot_microsecond();
- #endif
- }
- else {
- wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
- "wasm operand stack overflow");
- }
- return frame;
- }
- static inline void
- FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
- {
- #if WASM_ENABLE_PERF_PROFILING != 0
- if (frame->function) {
- frame->function->total_exec_time +=
- os_time_get_boot_microsecond() - frame->time_started;
- frame->function->total_exec_cnt++;
- }
- #endif
- wasm_exec_env_free_wasm_frame(exec_env, frame);
- }
- void
- wasm_interp_restore_wasm_frame(WASMExecEnv *exec_env)
- {
- WASMInterpFrame *cur_frame, *prev_frame;
- cur_frame = wasm_exec_env_get_cur_frame(exec_env);
- while (cur_frame) {
- prev_frame = cur_frame->prev_frame;
- if (cur_frame->ip) {
- /* FREE_FRAME just set the wasm_stack.s.top pointer, we only need to
- * call it once */
- FREE_FRAME(exec_env, cur_frame);
- break;
- }
- cur_frame = prev_frame;
- }
- wasm_exec_env_set_cur_frame(exec_env, cur_frame);
- }
- static void
- wasm_interp_call_func_native(WASMModuleInstance *module_inst,
- WASMExecEnv *exec_env,
- WASMFunctionInstance *cur_func,
- WASMInterpFrame *prev_frame)
- {
- WASMFunctionImport *func_import = cur_func->u.func_import;
- unsigned local_cell_num = 2;
- WASMInterpFrame *frame;
- uint32 argv_ret[2], cur_func_index;
- void *native_func_pointer = NULL;
- bool ret;
- if (!(frame = ALLOC_FRAME(exec_env,
- wasm_interp_interp_frame_size(local_cell_num),
- prev_frame)))
- return;
- frame->function = cur_func;
- frame->ip = NULL;
- frame->lp = frame->operand;
- wasm_exec_env_set_cur_frame(exec_env, frame);
- cur_func_index = (uint32)(cur_func - module_inst->e->functions);
- bh_assert(cur_func_index < module_inst->module->import_function_count);
- native_func_pointer = module_inst->import_func_ptrs[cur_func_index];
- if (!native_func_pointer) {
- char buf[128];
- snprintf(buf, sizeof(buf),
- "failed to call unlinked import function (%s, %s)",
- func_import->module_name, func_import->field_name);
- wasm_set_exception((WASMModuleInstance *)module_inst, buf);
- return;
- }
- if (func_import->call_conv_wasm_c_api) {
- ret = wasm_runtime_invoke_c_api_native(
- (WASMModuleInstanceCommon *)module_inst, native_func_pointer,
- func_import->func_type, cur_func->param_cell_num, frame->lp,
- func_import->wasm_c_api_with_env, func_import->attachment);
- if (ret) {
- argv_ret[0] = frame->lp[0];
- argv_ret[1] = frame->lp[1];
- }
- }
- else if (!func_import->call_conv_raw) {
- ret = wasm_runtime_invoke_native(
- exec_env, native_func_pointer, func_import->func_type,
- func_import->signature, func_import->attachment, frame->lp,
- cur_func->param_cell_num, argv_ret);
- }
- else {
- ret = wasm_runtime_invoke_native_raw(
- exec_env, native_func_pointer, func_import->func_type,
- func_import->signature, func_import->attachment, frame->lp,
- cur_func->param_cell_num, argv_ret);
- }
- if (!ret)
- return;
- if (cur_func->ret_cell_num == 1) {
- prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
- }
- else if (cur_func->ret_cell_num == 2) {
- prev_frame->lp[prev_frame->ret_offset] = argv_ret[0];
- prev_frame->lp[prev_frame->ret_offset + 1] = argv_ret[1];
- }
- FREE_FRAME(exec_env, frame);
- wasm_exec_env_set_cur_frame(exec_env, prev_frame);
- }
- #if WASM_ENABLE_MULTI_MODULE != 0
- static void
- wasm_interp_call_func_bytecode(WASMModuleInstance *module,
- WASMExecEnv *exec_env,
- WASMFunctionInstance *cur_func,
- WASMInterpFrame *prev_frame);
- static void
- wasm_interp_call_func_import(WASMModuleInstance *module_inst,
- WASMExecEnv *exec_env,
- WASMFunctionInstance *cur_func,
- WASMInterpFrame *prev_frame)
- {
- WASMModuleInstance *sub_module_inst = cur_func->import_module_inst;
- WASMFunctionInstance *sub_func_inst = cur_func->import_func_inst;
- WASMFunctionImport *func_import = cur_func->u.func_import;
- uint8 *ip = prev_frame->ip;
- char buf[128];
- WASMExecEnv *sub_module_exec_env = NULL;
- uint32 aux_stack_origin_boundary = 0;
- uint32 aux_stack_origin_bottom = 0;
- if (!sub_func_inst) {
- snprintf(buf, sizeof(buf),
- "failed to call unlinked import function (%s, %s)",
- func_import->module_name, func_import->field_name);
- wasm_set_exception(module_inst, buf);
- return;
- }
- /* Switch exec_env but keep using the same one by replacing necessary
- * variables */
- sub_module_exec_env = wasm_runtime_get_exec_env_singleton(
- (WASMModuleInstanceCommon *)sub_module_inst);
- if (!sub_module_exec_env) {
- wasm_set_exception(module_inst, "create singleton exec_env failed");
- return;
- }
- /* - module_inst */
- exec_env->module_inst = (WASMModuleInstanceCommon *)sub_module_inst;
- /* - aux_stack_boundary */
- aux_stack_origin_boundary = exec_env->aux_stack_boundary.boundary;
- exec_env->aux_stack_boundary.boundary =
- sub_module_exec_env->aux_stack_boundary.boundary;
- /* - aux_stack_bottom */
- aux_stack_origin_bottom = exec_env->aux_stack_bottom.bottom;
- exec_env->aux_stack_bottom.bottom =
- sub_module_exec_env->aux_stack_bottom.bottom;
- /* set ip NULL to make call_func_bytecode return after executing
- this function */
- prev_frame->ip = NULL;
- /* call function of sub-module*/
- wasm_interp_call_func_bytecode(sub_module_inst, exec_env, sub_func_inst,
- prev_frame);
- /* restore ip and other replaced */
- prev_frame->ip = ip;
- exec_env->aux_stack_boundary.boundary = aux_stack_origin_boundary;
- exec_env->aux_stack_bottom.bottom = aux_stack_origin_bottom;
- exec_env->module_inst = (WASMModuleInstanceCommon *)module_inst;
- /* transfer exception if it is thrown */
- if (wasm_get_exception(sub_module_inst)) {
- bh_memcpy_s(module_inst->cur_exception,
- sizeof(module_inst->cur_exception),
- sub_module_inst->cur_exception,
- sizeof(sub_module_inst->cur_exception));
- }
- }
- #endif
- #if WASM_ENABLE_THREAD_MGR != 0
- #define CHECK_SUSPEND_FLAGS() \
- do { \
- if (exec_env->suspend_flags.flags != 0) { \
- if (exec_env->suspend_flags.flags & 0x01) { \
- /* terminate current thread */ \
- return; \
- } \
- /* TODO: support suspend and breakpoint */ \
- } \
- } while (0)
- #endif
- #if WASM_ENABLE_OPCODE_COUNTER != 0
- typedef struct OpcodeInfo {
- char *name;
- uint64 count;
- } OpcodeInfo;
- /* clang-format off */
- #define HANDLE_OPCODE(op) \
- { \
- #op, 0 \
- }
- DEFINE_GOTO_TABLE(OpcodeInfo, opcode_table);
- #undef HANDLE_OPCODE
- /* clang-format on */
- static void
- wasm_interp_dump_op_count()
- {
- uint32 i;
- uint64 total_count = 0;
- for (i = 0; i < WASM_OP_IMPDEP; i++)
- total_count += opcode_table[i].count;
- printf("total opcode count: %ld\n", total_count);
- for (i = 0; i < WASM_OP_IMPDEP; i++)
- if (opcode_table[i].count > 0)
- printf("\t\t%s count:\t\t%ld,\t\t%.2f%%\n", opcode_table[i].name,
- opcode_table[i].count,
- opcode_table[i].count * 100.0f / total_count);
- }
- #endif
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- /* #define HANDLE_OP(opcode) HANDLE_##opcode:printf(#opcode"\n"); */
- #if WASM_ENABLE_OPCODE_COUNTER != 0
- #define HANDLE_OP(opcode) HANDLE_##opcode : opcode_table[opcode].count++;
- #else
- #define HANDLE_OP(opcode) HANDLE_##opcode:
- #endif
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
- #define FETCH_OPCODE_AND_DISPATCH() \
- do { \
- const void *p_label_addr = *(void **)frame_ip; \
- frame_ip += sizeof(void *); \
- goto *p_label_addr; \
- } while (0)
- #else
- #define FETCH_OPCODE_AND_DISPATCH() \
- do { \
- const void *p_label_addr = label_base + *(int16 *)frame_ip; \
- frame_ip += sizeof(int16); \
- goto *p_label_addr; \
- } while (0)
- #endif /* end of WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS */
- #define HANDLE_OP_END() FETCH_OPCODE_AND_DISPATCH()
- #else /* else of WASM_ENABLE_LABELS_AS_VALUES */
- #define HANDLE_OP(opcode) case opcode:
- #define HANDLE_OP_END() continue
- #endif /* end of WASM_ENABLE_LABELS_AS_VALUES */
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- static void **global_handle_table;
- #endif
- static inline uint8 *
- get_global_addr(uint8 *global_data, WASMGlobalInstance *global)
- {
- #if WASM_ENABLE_MULTI_MODULE == 0
- return global_data + global->data_offset;
- #else
- return global->import_global_inst
- ? global->import_module_inst->global_data
- + global->import_global_inst->data_offset
- : global_data + global->data_offset;
- #endif
- }
- static void
- wasm_interp_call_func_bytecode(WASMModuleInstance *module,
- WASMExecEnv *exec_env,
- WASMFunctionInstance *cur_func,
- WASMInterpFrame *prev_frame)
- {
- WASMMemoryInstance *memory = wasm_get_default_memory(module);
- #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
- || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
- || WASM_ENABLE_BULK_MEMORY != 0
- uint32 num_bytes_per_page = memory ? memory->num_bytes_per_page : 0;
- uint32 linear_mem_size =
- memory ? num_bytes_per_page * memory->cur_page_count : 0;
- #endif
- uint8 *global_data = module->global_data;
- WASMGlobalInstance *globals = module->e ? module->e->globals : NULL;
- WASMGlobalInstance *global;
- uint8 opcode_IMPDEP = WASM_OP_IMPDEP;
- WASMInterpFrame *frame = NULL;
- /* Points to this special opcode so as to jump to the
- * call_method_from_entry. */
- register uint8 *frame_ip = &opcode_IMPDEP; /* cache of frame->ip */
- register uint32 *frame_lp = NULL; /* cache of frame->lp */
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
- /* cache of label base addr */
- register uint8 *label_base = &&HANDLE_WASM_OP_UNREACHABLE;
- #endif
- #endif
- uint8 *frame_ip_end = frame_ip + 1;
- uint32 cond, count, fidx, tidx, frame_size = 0;
- uint64 all_cell_num = 0;
- int16 addr1, addr2, addr_ret = 0;
- int32 didx, val;
- uint8 *maddr = NULL;
- uint32 local_idx, local_offset, global_idx;
- uint8 opcode, local_type, *global_addr;
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- #define HANDLE_OPCODE(op) &&HANDLE_##op
- DEFINE_GOTO_TABLE(const void *, handle_table);
- #undef HANDLE_OPCODE
- if (exec_env == NULL) {
- global_handle_table = (void **)handle_table;
- return;
- }
- #endif
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- while (frame_ip < frame_ip_end) {
- opcode = *frame_ip++;
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0
- frame_ip++;
- #endif
- switch (opcode) {
- #else
- goto *handle_table[WASM_OP_IMPDEP];
- #endif
- /* control instructions */
- HANDLE_OP(WASM_OP_UNREACHABLE)
- {
- wasm_set_exception(module, "unreachable");
- goto got_exception;
- }
- HANDLE_OP(WASM_OP_IF)
- {
- cond = (uint32)POP_I32();
- if (cond == 0) {
- uint8 *else_addr = (uint8 *)LOAD_PTR(frame_ip);
- if (else_addr == NULL) {
- frame_ip =
- (uint8 *)LOAD_PTR(frame_ip + sizeof(uint8 *));
- }
- else {
- frame_ip = else_addr;
- }
- }
- else {
- frame_ip += sizeof(uint8 *) * 2;
- }
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_ELSE)
- {
- frame_ip = (uint8 *)LOAD_PTR(frame_ip);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_BR)
- {
- #if WASM_ENABLE_THREAD_MGR != 0
- CHECK_SUSPEND_FLAGS();
- #endif
- recover_br_info:
- RECOVER_BR_INFO();
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_BR_IF)
- {
- #if WASM_ENABLE_THREAD_MGR != 0
- CHECK_SUSPEND_FLAGS();
- #endif
- cond = frame_lp[GET_OFFSET()];
- if (cond)
- goto recover_br_info;
- else
- SKIP_BR_INFO();
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_BR_TABLE)
- {
- uint32 arity, br_item_size;
- #if WASM_ENABLE_THREAD_MGR != 0
- CHECK_SUSPEND_FLAGS();
- #endif
- count = read_uint32(frame_ip);
- didx = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- if (!(didx >= 0 && (uint32)didx < count))
- didx = count;
- /* all br items must have the same arity and item size,
- so we only calculate the first item size */
- arity = LOAD_U32_WITH_2U16S(frame_ip);
- br_item_size = sizeof(uint32); /* arity */
- if (arity) {
- /* total cell num */
- br_item_size += sizeof(uint32);
- /* cells, src offsets and dst offsets */
- br_item_size +=
- (CELL_SIZE + sizeof(int16) + sizeof(uint16)) * arity;
- }
- /* target address */
- br_item_size += sizeof(uint8 *);
- frame_ip += br_item_size * didx;
- goto recover_br_info;
- }
- HANDLE_OP(WASM_OP_RETURN)
- {
- uint32 ret_idx;
- WASMType *func_type;
- uint32 off, ret_offset;
- uint8 *ret_types;
- if (cur_func->is_import_func)
- func_type = cur_func->u.func_import->func_type;
- else
- func_type = cur_func->u.func->func_type;
- /* types of each return value */
- ret_types = func_type->types + func_type->param_count;
- ret_offset = prev_frame->ret_offset;
- for (ret_idx = 0,
- off = sizeof(int16) * (func_type->result_count - 1);
- ret_idx < func_type->result_count;
- ret_idx++, off -= sizeof(int16)) {
- if (ret_types[ret_idx] == VALUE_TYPE_I64
- || ret_types[ret_idx] == VALUE_TYPE_F64) {
- PUT_I64_TO_ADDR(prev_frame->lp + ret_offset,
- GET_OPERAND(uint64, I64, off));
- ret_offset += 2;
- }
- else {
- prev_frame->lp[ret_offset] =
- GET_OPERAND(uint32, I32, off);
- ret_offset++;
- }
- }
- goto return_func;
- }
- HANDLE_OP(WASM_OP_CALL_INDIRECT)
- #if WASM_ENABLE_TAIL_CALL != 0
- HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
- #endif
- {
- WASMType *cur_type, *cur_func_type;
- WASMTableInstance *tbl_inst;
- uint32 tbl_idx;
- #if WASM_ENABLE_TAIL_CALL != 0
- GET_OPCODE();
- #endif
- #if WASM_ENABLE_THREAD_MGR != 0
- CHECK_SUSPEND_FLAGS();
- #endif
- tidx = read_uint32(frame_ip);
- cur_type = module->module->types[tidx];
- tbl_idx = read_uint32(frame_ip);
- bh_assert(tbl_idx < module->table_count);
- tbl_inst = wasm_get_table_inst(module, tbl_idx);
- val = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- if ((uint32)val >= tbl_inst->cur_size) {
- wasm_set_exception(module, "undefined element");
- goto got_exception;
- }
- fidx = tbl_inst->elems[val];
- if (fidx == NULL_REF) {
- wasm_set_exception(module, "uninitialized element");
- goto got_exception;
- }
- /*
- * we might be using a table injected by host or
- * another module. in that case, we don't validate
- * the elem value while loading
- */
- if (fidx >= module->e->function_count) {
- wasm_set_exception(module, "unknown function");
- goto got_exception;
- }
- /* always call module own functions */
- cur_func = module->e->functions + fidx;
- if (cur_func->is_import_func)
- cur_func_type = cur_func->u.func_import->func_type;
- else
- cur_func_type = cur_func->u.func->func_type;
- if (cur_type != cur_func_type) {
- wasm_set_exception(module, "indirect call type mismatch");
- goto got_exception;
- }
- #if WASM_ENABLE_TAIL_CALL != 0
- if (opcode == WASM_OP_RETURN_CALL_INDIRECT)
- goto call_func_from_return_call;
- #endif
- goto call_func_from_interp;
- }
- /* parametric instructions */
- HANDLE_OP(WASM_OP_SELECT)
- {
- cond = frame_lp[GET_OFFSET()];
- addr1 = GET_OFFSET();
- addr2 = GET_OFFSET();
- addr_ret = GET_OFFSET();
- if (!cond) {
- if (addr_ret != addr1)
- frame_lp[addr_ret] = frame_lp[addr1];
- }
- else {
- if (addr_ret != addr2)
- frame_lp[addr_ret] = frame_lp[addr2];
- }
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_SELECT_64)
- {
- cond = frame_lp[GET_OFFSET()];
- addr1 = GET_OFFSET();
- addr2 = GET_OFFSET();
- addr_ret = GET_OFFSET();
- if (!cond) {
- if (addr_ret != addr1)
- PUT_I64_TO_ADDR(frame_lp + addr_ret,
- GET_I64_FROM_ADDR(frame_lp + addr1));
- }
- else {
- if (addr_ret != addr2)
- PUT_I64_TO_ADDR(frame_lp + addr_ret,
- GET_I64_FROM_ADDR(frame_lp + addr2));
- }
- HANDLE_OP_END();
- }
- #if WASM_ENABLE_REF_TYPES != 0
- HANDLE_OP(WASM_OP_TABLE_GET)
- {
- uint32 tbl_idx, elem_idx;
- WASMTableInstance *tbl_inst;
- tbl_idx = read_uint32(frame_ip);
- bh_assert(tbl_idx < module->table_count);
- tbl_inst = wasm_get_table_inst(module, tbl_idx);
- elem_idx = POP_I32();
- if (elem_idx >= tbl_inst->cur_size) {
- wasm_set_exception(module, "out of bounds table access");
- goto got_exception;
- }
- PUSH_I32(tbl_inst->elems[elem_idx]);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_TABLE_SET)
- {
- uint32 tbl_idx, elem_idx, elem_val;
- WASMTableInstance *tbl_inst;
- tbl_idx = read_uint32(frame_ip);
- bh_assert(tbl_idx < module->table_count);
- tbl_inst = wasm_get_table_inst(module, tbl_idx);
- elem_val = POP_I32();
- elem_idx = POP_I32();
- if (elem_idx >= tbl_inst->cur_size) {
- wasm_set_exception(module, "out of bounds table access");
- goto got_exception;
- }
- tbl_inst->elems[elem_idx] = elem_val;
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_REF_NULL)
- {
- PUSH_I32(NULL_REF);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_REF_IS_NULL)
- {
- uint32 ref_val;
- ref_val = POP_I32();
- PUSH_I32(ref_val == NULL_REF ? 1 : 0);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_REF_FUNC)
- {
- uint32 func_idx = read_uint32(frame_ip);
- PUSH_I32(func_idx);
- HANDLE_OP_END();
- }
- #endif /* WASM_ENABLE_REF_TYPES */
- /* variable instructions */
- HANDLE_OP(EXT_OP_SET_LOCAL_FAST)
- HANDLE_OP(EXT_OP_TEE_LOCAL_FAST)
- {
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
- local_offset = *frame_ip++;
- #else
- /* clang-format off */
- local_offset = *frame_ip;
- frame_ip += 2;
- /* clang-format on */
- #endif
- *(uint32 *)(frame_lp + local_offset) =
- GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- HANDLE_OP_END();
- }
- HANDLE_OP(EXT_OP_SET_LOCAL_FAST_I64)
- HANDLE_OP(EXT_OP_TEE_LOCAL_FAST_I64)
- {
- #if WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS != 0
- local_offset = *frame_ip++;
- #else
- /* clang-format off */
- local_offset = *frame_ip;
- frame_ip += 2;
- /* clang-format on */
- #endif
- PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
- GET_OPERAND(uint64, I64, 0));
- frame_ip += 2;
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_GET_GLOBAL)
- {
- global_idx = read_uint32(frame_ip);
- bh_assert(global_idx < module->e->global_count);
- global = globals + global_idx;
- global_addr = get_global_addr(global_data, global);
- addr_ret = GET_OFFSET();
- frame_lp[addr_ret] = *(uint32 *)global_addr;
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_GET_GLOBAL_64)
- {
- global_idx = read_uint32(frame_ip);
- bh_assert(global_idx < module->e->global_count);
- global = globals + global_idx;
- global_addr = get_global_addr(global_data, global);
- addr_ret = GET_OFFSET();
- PUT_I64_TO_ADDR(frame_lp + addr_ret,
- GET_I64_FROM_ADDR((uint32 *)global_addr));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_SET_GLOBAL)
- {
- global_idx = read_uint32(frame_ip);
- bh_assert(global_idx < module->e->global_count);
- global = globals + global_idx;
- global_addr = get_global_addr(global_data, global);
- addr1 = GET_OFFSET();
- *(int32 *)global_addr = frame_lp[addr1];
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_SET_GLOBAL_AUX_STACK)
- {
- uint32 aux_stack_top;
- global_idx = read_uint32(frame_ip);
- bh_assert(global_idx < module->e->global_count);
- global = globals + global_idx;
- global_addr = get_global_addr(global_data, global);
- aux_stack_top = frame_lp[GET_OFFSET()];
- if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) {
- wasm_set_exception(module, "wasm auxiliary stack overflow");
- goto got_exception;
- }
- if (aux_stack_top > exec_env->aux_stack_bottom.bottom) {
- wasm_set_exception(module,
- "wasm auxiliary stack underflow");
- goto got_exception;
- }
- *(int32 *)global_addr = aux_stack_top;
- #if WASM_ENABLE_MEMORY_PROFILING != 0
- if (module->module->aux_stack_top_global_index != (uint32)-1) {
- uint32 aux_stack_used = module->module->aux_stack_bottom
- - *(uint32 *)global_addr;
- if (aux_stack_used > module->e->max_aux_stack_used)
- module->e->max_aux_stack_used = aux_stack_used;
- }
- #endif
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_SET_GLOBAL_64)
- {
- global_idx = read_uint32(frame_ip);
- bh_assert(global_idx < module->e->global_count);
- global = globals + global_idx;
- global_addr = get_global_addr(global_data, global);
- addr1 = GET_OFFSET();
- PUT_I64_TO_ADDR((uint32 *)global_addr,
- GET_I64_FROM_ADDR(frame_lp + addr1));
- HANDLE_OP_END();
- }
- /* memory load instructions */
- HANDLE_OP(WASM_OP_I32_LOAD)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(4);
- frame_lp[addr_ret] = LOAD_I32(maddr);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LOAD)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(8);
- PUT_I64_TO_ADDR(frame_lp + addr_ret, LOAD_I64(maddr));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LOAD8_S)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(1);
- frame_lp[addr_ret] = sign_ext_8_32(*(int8 *)maddr);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LOAD8_U)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(1);
- frame_lp[addr_ret] = (uint32)(*(uint8 *)maddr);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LOAD16_S)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(2);
- frame_lp[addr_ret] = sign_ext_16_32(LOAD_I16(maddr));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LOAD16_U)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(2);
- frame_lp[addr_ret] = (uint32)(LOAD_U16(maddr));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LOAD8_S)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(1);
- PUT_I64_TO_ADDR(frame_lp + addr_ret,
- sign_ext_8_64(*(int8 *)maddr));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LOAD8_U)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(1);
- PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(*(uint8 *)maddr));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LOAD16_S)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(2);
- PUT_I64_TO_ADDR(frame_lp + addr_ret,
- sign_ext_16_64(LOAD_I16(maddr)));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LOAD16_U)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(2);
- PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U16(maddr)));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LOAD32_S)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(4);
- PUT_I64_TO_ADDR(frame_lp + addr_ret,
- sign_ext_32_64(LOAD_I32(maddr)));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LOAD32_U)
- {
- uint32 offset, addr;
- offset = read_uint32(frame_ip);
- addr = GET_OPERAND(uint32, I32, 0);
- frame_ip += 2;
- addr_ret = GET_OFFSET();
- CHECK_MEMORY_OVERFLOW(4);
- PUT_I64_TO_ADDR(frame_lp + addr_ret, (uint64)(LOAD_U32(maddr)));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_STORE)
- {
- uint32 offset, addr;
- uint32 sval;
- offset = read_uint32(frame_ip);
- sval = GET_OPERAND(uint32, I32, 0);
- addr = GET_OPERAND(uint32, I32, 2);
- frame_ip += 4;
- CHECK_MEMORY_OVERFLOW(4);
- STORE_U32(maddr, sval);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_STORE8)
- {
- uint32 offset, addr;
- uint32 sval;
- offset = read_uint32(frame_ip);
- sval = GET_OPERAND(uint32, I32, 0);
- addr = GET_OPERAND(uint32, I32, 2);
- frame_ip += 4;
- CHECK_MEMORY_OVERFLOW(1);
- *(uint8 *)maddr = (uint8)sval;
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_STORE16)
- {
- uint32 offset, addr;
- uint32 sval;
- offset = read_uint32(frame_ip);
- sval = GET_OPERAND(uint32, I32, 0);
- addr = GET_OPERAND(uint32, I32, 2);
- frame_ip += 4;
- CHECK_MEMORY_OVERFLOW(2);
- STORE_U16(maddr, (uint16)sval);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_STORE)
- {
- uint32 offset, addr;
- uint64 sval;
- offset = read_uint32(frame_ip);
- sval = GET_OPERAND(uint64, I64, 0);
- addr = GET_OPERAND(uint32, I32, 2);
- frame_ip += 4;
- CHECK_MEMORY_OVERFLOW(8);
- STORE_I64(maddr, sval);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_STORE8)
- {
- uint32 offset, addr;
- uint64 sval;
- offset = read_uint32(frame_ip);
- sval = GET_OPERAND(uint64, I64, 0);
- addr = GET_OPERAND(uint32, I32, 2);
- frame_ip += 4;
- CHECK_MEMORY_OVERFLOW(1);
- *(uint8 *)maddr = (uint8)sval;
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_STORE16)
- {
- uint32 offset, addr;
- uint64 sval;
- offset = read_uint32(frame_ip);
- sval = GET_OPERAND(uint64, I64, 0);
- addr = GET_OPERAND(uint32, I32, 2);
- frame_ip += 4;
- CHECK_MEMORY_OVERFLOW(2);
- STORE_U16(maddr, (uint16)sval);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_STORE32)
- {
- uint32 offset, addr;
- uint64 sval;
- offset = read_uint32(frame_ip);
- sval = GET_OPERAND(uint64, I64, 0);
- addr = GET_OPERAND(uint32, I32, 2);
- frame_ip += 4;
- CHECK_MEMORY_OVERFLOW(4);
- STORE_U32(maddr, (uint32)sval);
- HANDLE_OP_END();
- }
- /* memory size and memory grow instructions */
- HANDLE_OP(WASM_OP_MEMORY_SIZE)
- {
- uint32 reserved;
- addr_ret = GET_OFFSET();
- frame_lp[addr_ret] = memory->cur_page_count;
- (void)reserved;
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_MEMORY_GROW)
- {
- uint32 reserved, delta,
- prev_page_count = memory->cur_page_count;
- addr1 = GET_OFFSET();
- addr_ret = GET_OFFSET();
- delta = (uint32)frame_lp[addr1];
- if (!wasm_enlarge_memory(module, delta)) {
- /* failed to memory.grow, return -1 */
- frame_lp[addr_ret] = -1;
- }
- else {
- /* success, return previous page count */
- frame_lp[addr_ret] = prev_page_count;
- /* update memory size, no need to update memory ptr as
- it isn't changed in wasm_enlarge_memory */
- #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
- || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
- || WASM_ENABLE_BULK_MEMORY != 0
- linear_mem_size =
- num_bytes_per_page * memory->cur_page_count;
- #endif
- }
- (void)reserved;
- HANDLE_OP_END();
- }
- /* constant instructions */
- HANDLE_OP(WASM_OP_F64_CONST)
- HANDLE_OP(WASM_OP_I64_CONST)
- {
- uint8 *orig_ip = frame_ip;
- frame_ip += sizeof(uint64);
- addr_ret = GET_OFFSET();
- bh_memcpy_s(frame_lp + addr_ret, sizeof(uint64), orig_ip,
- sizeof(uint64));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_CONST)
- HANDLE_OP(WASM_OP_I32_CONST)
- {
- uint8 *orig_ip = frame_ip;
- frame_ip += sizeof(uint32);
- addr_ret = GET_OFFSET();
- bh_memcpy_s(frame_lp + addr_ret, sizeof(uint32), orig_ip,
- sizeof(uint32));
- HANDLE_OP_END();
- }
- /* comparison instructions of i32 */
- HANDLE_OP(WASM_OP_I32_EQZ)
- {
- DEF_OP_EQZ(int32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_EQ)
- {
- DEF_OP_CMP(uint32, I32, ==);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_NE)
- {
- DEF_OP_CMP(uint32, I32, !=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LT_S)
- {
- DEF_OP_CMP(int32, I32, <);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LT_U)
- {
- DEF_OP_CMP(uint32, I32, <);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_GT_S)
- {
- DEF_OP_CMP(int32, I32, >);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_GT_U)
- {
- DEF_OP_CMP(uint32, I32, >);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LE_S)
- {
- DEF_OP_CMP(int32, I32, <=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_LE_U)
- {
- DEF_OP_CMP(uint32, I32, <=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_GE_S)
- {
- DEF_OP_CMP(int32, I32, >=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_GE_U)
- {
- DEF_OP_CMP(uint32, I32, >=);
- HANDLE_OP_END();
- }
- /* comparison instructions of i64 */
- HANDLE_OP(WASM_OP_I64_EQZ)
- {
- DEF_OP_EQZ(int64, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_EQ)
- {
- DEF_OP_CMP(uint64, I64, ==);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_NE)
- {
- DEF_OP_CMP(uint64, I64, !=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LT_S)
- {
- DEF_OP_CMP(int64, I64, <);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LT_U)
- {
- DEF_OP_CMP(uint64, I64, <);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_GT_S)
- {
- DEF_OP_CMP(int64, I64, >);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_GT_U)
- {
- DEF_OP_CMP(uint64, I64, >);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LE_S)
- {
- DEF_OP_CMP(int64, I64, <=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_LE_U)
- {
- DEF_OP_CMP(uint64, I64, <=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_GE_S)
- {
- DEF_OP_CMP(int64, I64, >=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_GE_U)
- {
- DEF_OP_CMP(uint64, I64, >=);
- HANDLE_OP_END();
- }
- /* comparison instructions of f32 */
- HANDLE_OP(WASM_OP_F32_EQ)
- {
- DEF_OP_CMP(float32, F32, ==);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_NE)
- {
- DEF_OP_CMP(float32, F32, !=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_LT)
- {
- DEF_OP_CMP(float32, F32, <);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_GT)
- {
- DEF_OP_CMP(float32, F32, >);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_LE)
- {
- DEF_OP_CMP(float32, F32, <=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_GE)
- {
- DEF_OP_CMP(float32, F32, >=);
- HANDLE_OP_END();
- }
- /* comparison instructions of f64 */
- HANDLE_OP(WASM_OP_F64_EQ)
- {
- DEF_OP_CMP(float64, F64, ==);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_NE)
- {
- DEF_OP_CMP(float64, F64, !=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_LT)
- {
- DEF_OP_CMP(float64, F64, <);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_GT)
- {
- DEF_OP_CMP(float64, F64, >);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_LE)
- {
- DEF_OP_CMP(float64, F64, <=);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_GE)
- {
- DEF_OP_CMP(float64, F64, >=);
- HANDLE_OP_END();
- }
- /* numberic instructions of i32 */
- HANDLE_OP(WASM_OP_I32_CLZ)
- {
- DEF_OP_BIT_COUNT(uint32, I32, clz32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_CTZ)
- {
- DEF_OP_BIT_COUNT(uint32, I32, ctz32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_POPCNT)
- {
- DEF_OP_BIT_COUNT(uint32, I32, popcount32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_ADD)
- {
- DEF_OP_NUMERIC(uint32, uint32, I32, +);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_SUB)
- {
- DEF_OP_NUMERIC(uint32, uint32, I32, -);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_MUL)
- {
- DEF_OP_NUMERIC(uint32, uint32, I32, *);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_DIV_S)
- {
- int32 a, b;
- b = frame_lp[GET_OFFSET()];
- a = frame_lp[GET_OFFSET()];
- addr_ret = GET_OFFSET();
- if (a == (int32)0x80000000 && b == -1) {
- wasm_set_exception(module, "integer overflow");
- goto got_exception;
- }
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- frame_lp[addr_ret] = (a / b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_DIV_U)
- {
- uint32 a, b;
- addr1 = GET_OFFSET();
- addr2 = GET_OFFSET();
- addr_ret = GET_OFFSET();
- b = (uint32)frame_lp[addr1];
- a = (uint32)frame_lp[addr2];
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- frame_lp[addr_ret] = (a / b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_REM_S)
- {
- int32 a, b;
- addr1 = GET_OFFSET();
- addr2 = GET_OFFSET();
- addr_ret = GET_OFFSET();
- b = frame_lp[addr1];
- a = frame_lp[addr2];
- if (a == (int32)0x80000000 && b == -1) {
- frame_lp[addr_ret] = 0;
- HANDLE_OP_END();
- }
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- frame_lp[addr_ret] = (a % b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_REM_U)
- {
- uint32 a, b;
- addr1 = GET_OFFSET();
- addr2 = GET_OFFSET();
- addr_ret = GET_OFFSET();
- b = (uint32)frame_lp[addr1];
- a = (uint32)frame_lp[addr2];
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- frame_lp[addr_ret] = (a % b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_AND)
- {
- DEF_OP_NUMERIC(uint32, uint32, I32, &);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_OR)
- {
- DEF_OP_NUMERIC(uint32, uint32, I32, |);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_XOR)
- {
- DEF_OP_NUMERIC(uint32, uint32, I32, ^);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_SHL)
- {
- DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_SHR_S)
- {
- DEF_OP_NUMERIC2(int32, uint32, I32, >>);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_SHR_U)
- {
- DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_ROTL)
- {
- uint32 a, b;
- b = (uint32)frame_lp[GET_OFFSET()];
- a = (uint32)frame_lp[GET_OFFSET()];
- frame_lp[GET_OFFSET()] = rotl32(a, b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_ROTR)
- {
- uint32 a, b;
- b = (uint32)frame_lp[GET_OFFSET()];
- a = (uint32)frame_lp[GET_OFFSET()];
- frame_lp[GET_OFFSET()] = rotr32(a, b);
- HANDLE_OP_END();
- }
- /* numberic instructions of i64 */
- HANDLE_OP(WASM_OP_I64_CLZ)
- {
- DEF_OP_BIT_COUNT(uint64, I64, clz64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_CTZ)
- {
- DEF_OP_BIT_COUNT(uint64, I64, ctz64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_POPCNT)
- {
- DEF_OP_BIT_COUNT(uint64, I64, popcount64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_ADD)
- {
- DEF_OP_NUMERIC_64(uint64, uint64, I64, +);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_SUB)
- {
- DEF_OP_NUMERIC_64(uint64, uint64, I64, -);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_MUL)
- {
- DEF_OP_NUMERIC_64(uint64, uint64, I64, *);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_DIV_S)
- {
- int64 a, b;
- b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- if (a == (int64)0x8000000000000000LL && b == -1) {
- wasm_set_exception(module, "integer overflow");
- goto got_exception;
- }
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_DIV_U)
- {
- uint64 a, b;
- b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a / b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_REM_S)
- {
- int64 a, b;
- b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- if (a == (int64)0x8000000000000000LL && b == -1) {
- *(int64 *)(frame_lp + GET_OFFSET()) = 0;
- HANDLE_OP_END();
- }
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_REM_U)
- {
- uint64 a, b;
- b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), a % b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_AND)
- {
- DEF_OP_NUMERIC_64(uint64, uint64, I64, &);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_OR)
- {
- DEF_OP_NUMERIC_64(uint64, uint64, I64, |);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_XOR)
- {
- DEF_OP_NUMERIC_64(uint64, uint64, I64, ^);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_SHL)
- {
- DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_SHR_S)
- {
- DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_SHR_U)
- {
- DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_ROTL)
- {
- uint64 a, b;
- b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotl64(a, b));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_ROTR)
- {
- uint64 a, b;
- b = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- a = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(), rotr64(a, b));
- HANDLE_OP_END();
- }
- /* numberic instructions of f32 */
- HANDLE_OP(WASM_OP_F32_ABS)
- {
- DEF_OP_MATH(float32, F32, fabsf);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_NEG)
- {
- uint32 u32 = frame_lp[GET_OFFSET()];
- uint32 sign_bit = u32 & ((uint32)1 << 31);
- addr_ret = GET_OFFSET();
- if (sign_bit)
- frame_lp[addr_ret] = u32 & ~((uint32)1 << 31);
- else
- frame_lp[addr_ret] = u32 | ((uint32)1 << 31);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_CEIL)
- {
- DEF_OP_MATH(float32, F32, ceilf);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_FLOOR)
- {
- DEF_OP_MATH(float32, F32, floorf);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_TRUNC)
- {
- DEF_OP_MATH(float32, F32, truncf);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_NEAREST)
- {
- DEF_OP_MATH(float32, F32, rintf);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_SQRT)
- {
- DEF_OP_MATH(float32, F32, sqrtf);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_ADD)
- {
- DEF_OP_NUMERIC(float32, float32, F32, +);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_SUB)
- {
- DEF_OP_NUMERIC(float32, float32, F32, -);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_MUL)
- {
- DEF_OP_NUMERIC(float32, float32, F32, *);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_DIV)
- {
- DEF_OP_NUMERIC(float32, float32, F32, /);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_MIN)
- {
- float32 a, b;
- b = *(float32 *)(frame_lp + GET_OFFSET());
- a = *(float32 *)(frame_lp + GET_OFFSET());
- if (isnan(a))
- *(float32 *)(frame_lp + GET_OFFSET()) = a;
- else if (isnan(b))
- *(float32 *)(frame_lp + GET_OFFSET()) = b;
- else
- *(float32 *)(frame_lp + GET_OFFSET()) =
- (float32)wa_fmin(a, b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_MAX)
- {
- float32 a, b;
- b = *(float32 *)(frame_lp + GET_OFFSET());
- a = *(float32 *)(frame_lp + GET_OFFSET());
- if (isnan(a))
- *(float32 *)(frame_lp + GET_OFFSET()) = a;
- else if (isnan(b))
- *(float32 *)(frame_lp + GET_OFFSET()) = b;
- else
- *(float32 *)(frame_lp + GET_OFFSET()) =
- (float32)wa_fmax(a, b);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_COPYSIGN)
- {
- float32 a, b;
- b = *(float32 *)(frame_lp + GET_OFFSET());
- a = *(float32 *)(frame_lp + GET_OFFSET());
- *(float32 *)(frame_lp + GET_OFFSET()) = local_copysignf(a, b);
- HANDLE_OP_END();
- }
- /* numberic instructions of f64 */
- HANDLE_OP(WASM_OP_F64_ABS)
- {
- DEF_OP_MATH(float64, F64, fabs);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_NEG)
- {
- uint64 u64 = GET_I64_FROM_ADDR(frame_lp + GET_OFFSET());
- uint64 sign_bit = u64 & (((uint64)1) << 63);
- if (sign_bit)
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
- (u64 & ~(((uint64)1) << 63)));
- else
- PUT_I64_TO_ADDR(frame_lp + GET_OFFSET(),
- (u64 | (((uint64)1) << 63)));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_CEIL)
- {
- DEF_OP_MATH(float64, F64, ceil);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_FLOOR)
- {
- DEF_OP_MATH(float64, F64, floor);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_TRUNC)
- {
- DEF_OP_MATH(float64, F64, trunc);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_NEAREST)
- {
- DEF_OP_MATH(float64, F64, rint);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_SQRT)
- {
- DEF_OP_MATH(float64, F64, sqrt);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_ADD)
- {
- DEF_OP_NUMERIC_64(float64, float64, F64, +);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_SUB)
- {
- DEF_OP_NUMERIC_64(float64, float64, F64, -);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_MUL)
- {
- DEF_OP_NUMERIC_64(float64, float64, F64, *);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_DIV)
- {
- DEF_OP_NUMERIC_64(float64, float64, F64, /);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_MIN)
- {
- float64 a, b;
- b = POP_F64();
- a = POP_F64();
- if (isnan(a))
- PUSH_F64(a);
- else if (isnan(b))
- PUSH_F64(b);
- else
- PUSH_F64(wa_fmin(a, b));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_MAX)
- {
- float64 a, b;
- b = POP_F64();
- a = POP_F64();
- if (isnan(a))
- PUSH_F64(a);
- else if (isnan(b))
- PUSH_F64(b);
- else
- PUSH_F64(wa_fmax(a, b));
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_COPYSIGN)
- {
- float64 a, b;
- b = POP_F64();
- a = POP_F64();
- PUSH_F64(local_copysign(a, b));
- HANDLE_OP_END();
- }
- /* conversions of i32 */
- HANDLE_OP(WASM_OP_I32_WRAP_I64)
- {
- int32 value = (int32)(POP_I64() & 0xFFFFFFFFLL);
- PUSH_I32(value);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_TRUNC_S_F32)
- {
- /* We don't use INT32_MIN/INT32_MAX/UINT32_MIN/UINT32_MAX,
- since float/double values of ieee754 cannot precisely
- represent all int32/uint32/int64/uint64 values, e.g.:
- UINT32_MAX is 4294967295, but (float32)4294967295 is
- 4294967296.0f, but not 4294967295.0f. */
- DEF_OP_TRUNC_F32(-2147483904.0f, 2147483648.0f, true, true);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_TRUNC_U_F32)
- {
- DEF_OP_TRUNC_F32(-1.0f, 4294967296.0f, true, false);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_TRUNC_S_F64)
- {
- DEF_OP_TRUNC_F64(-2147483649.0, 2147483648.0, true, true);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_TRUNC_U_F64)
- {
- DEF_OP_TRUNC_F64(-1.0, 4294967296.0, true, false);
- HANDLE_OP_END();
- }
- /* conversions of i64 */
- HANDLE_OP(WASM_OP_I64_EXTEND_S_I32)
- {
- DEF_OP_CONVERT(int64, I64, int32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_EXTEND_U_I32)
- {
- DEF_OP_CONVERT(int64, I64, uint32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_TRUNC_S_F32)
- {
- DEF_OP_TRUNC_F32(-9223373136366403584.0f,
- 9223372036854775808.0f, false, true);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_TRUNC_U_F32)
- {
- DEF_OP_TRUNC_F32(-1.0f, 18446744073709551616.0f, false, false);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_TRUNC_S_F64)
- {
- DEF_OP_TRUNC_F64(-9223372036854777856.0, 9223372036854775808.0,
- false, true);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_TRUNC_U_F64)
- {
- DEF_OP_TRUNC_F64(-1.0, 18446744073709551616.0, false, false);
- HANDLE_OP_END();
- }
- /* conversions of f32 */
- HANDLE_OP(WASM_OP_F32_CONVERT_S_I32)
- {
- DEF_OP_CONVERT(float32, F32, int32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_CONVERT_U_I32)
- {
- DEF_OP_CONVERT(float32, F32, uint32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_CONVERT_S_I64)
- {
- DEF_OP_CONVERT(float32, F32, int64, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_CONVERT_U_I64)
- {
- DEF_OP_CONVERT(float32, F32, uint64, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F32_DEMOTE_F64)
- {
- DEF_OP_CONVERT(float32, F32, float64, F64);
- HANDLE_OP_END();
- }
- /* conversions of f64 */
- HANDLE_OP(WASM_OP_F64_CONVERT_S_I32)
- {
- DEF_OP_CONVERT(float64, F64, int32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_CONVERT_U_I32)
- {
- DEF_OP_CONVERT(float64, F64, uint32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_CONVERT_S_I64)
- {
- DEF_OP_CONVERT(float64, F64, int64, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_CONVERT_U_I64)
- {
- DEF_OP_CONVERT(float64, F64, uint64, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_F64_PROMOTE_F32)
- {
- DEF_OP_CONVERT(float64, F64, float32, F32);
- HANDLE_OP_END();
- }
- /* reinterpretations */
- HANDLE_OP(WASM_OP_I32_REINTERPRET_F32)
- HANDLE_OP(WASM_OP_F32_REINTERPRET_I32)
- {
- DEF_OP_REINTERPRET(uint32, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_REINTERPRET_F64)
- HANDLE_OP(WASM_OP_F64_REINTERPRET_I64)
- {
- DEF_OP_REINTERPRET(int64, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(EXT_OP_COPY_STACK_TOP)
- {
- addr1 = GET_OFFSET();
- addr2 = GET_OFFSET();
- frame_lp[addr2] = frame_lp[addr1];
- HANDLE_OP_END();
- }
- HANDLE_OP(EXT_OP_COPY_STACK_TOP_I64)
- {
- addr1 = GET_OFFSET();
- addr2 = GET_OFFSET();
- frame_lp[addr2] = frame_lp[addr1];
- frame_lp[addr2 + 1] = frame_lp[addr1 + 1];
- HANDLE_OP_END();
- }
- HANDLE_OP(EXT_OP_COPY_STACK_VALUES)
- {
- uint32 values_count, total_cell;
- uint8 *cells;
- int16 *src_offsets = NULL;
- uint16 *dst_offsets = NULL;
- /* read values_count */
- values_count = read_uint32(frame_ip);
- /* read total cell num */
- total_cell = read_uint32(frame_ip);
- /* cells */
- cells = (uint8 *)frame_ip;
- frame_ip += values_count * CELL_SIZE;
- /* src offsets */
- src_offsets = (int16 *)frame_ip;
- frame_ip += values_count * sizeof(int16);
- /* dst offsets */
- dst_offsets = (uint16 *)frame_ip;
- frame_ip += values_count * sizeof(uint16);
- if (!copy_stack_values(module, frame_lp, values_count,
- total_cell, cells, src_offsets,
- dst_offsets))
- goto got_exception;
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_SET_LOCAL)
- HANDLE_OP(WASM_OP_TEE_LOCAL)
- {
- GET_LOCAL_INDEX_TYPE_AND_OFFSET();
- addr1 = GET_OFFSET();
- if (local_type == VALUE_TYPE_I32
- || local_type == VALUE_TYPE_F32) {
- *(int32 *)(frame_lp + local_offset) = frame_lp[addr1];
- }
- else if (local_type == VALUE_TYPE_I64
- || local_type == VALUE_TYPE_F64) {
- PUT_I64_TO_ADDR((uint32 *)(frame_lp + local_offset),
- GET_I64_FROM_ADDR(frame_lp + addr1));
- }
- else {
- wasm_set_exception(module, "invalid local type");
- goto got_exception;
- }
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_EXTEND8_S)
- {
- DEF_OP_CONVERT(int32, I32, int8, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I32_EXTEND16_S)
- {
- DEF_OP_CONVERT(int32, I32, int16, I32);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_EXTEND8_S)
- {
- DEF_OP_CONVERT(int64, I64, int8, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_EXTEND16_S)
- {
- DEF_OP_CONVERT(int64, I64, int16, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_I64_EXTEND32_S)
- {
- DEF_OP_CONVERT(int64, I64, int32, I64);
- HANDLE_OP_END();
- }
- HANDLE_OP(WASM_OP_MISC_PREFIX)
- {
- GET_OPCODE();
- switch (opcode) {
- case WASM_OP_I32_TRUNC_SAT_S_F32:
- DEF_OP_TRUNC_SAT_F32(-2147483904.0f, 2147483648.0f,
- true, true);
- break;
- case WASM_OP_I32_TRUNC_SAT_U_F32:
- DEF_OP_TRUNC_SAT_F32(-1.0f, 4294967296.0f, true, false);
- break;
- case WASM_OP_I32_TRUNC_SAT_S_F64:
- DEF_OP_TRUNC_SAT_F64(-2147483649.0, 2147483648.0, true,
- true);
- break;
- case WASM_OP_I32_TRUNC_SAT_U_F64:
- DEF_OP_TRUNC_SAT_F64(-1.0, 4294967296.0, true, false);
- break;
- case WASM_OP_I64_TRUNC_SAT_S_F32:
- DEF_OP_TRUNC_SAT_F32(-9223373136366403584.0f,
- 9223372036854775808.0f, false,
- true);
- break;
- case WASM_OP_I64_TRUNC_SAT_U_F32:
- DEF_OP_TRUNC_SAT_F32(-1.0f, 18446744073709551616.0f,
- false, false);
- break;
- case WASM_OP_I64_TRUNC_SAT_S_F64:
- DEF_OP_TRUNC_SAT_F64(-9223372036854777856.0,
- 9223372036854775808.0, false,
- true);
- break;
- case WASM_OP_I64_TRUNC_SAT_U_F64:
- DEF_OP_TRUNC_SAT_F64(-1.0, 18446744073709551616.0,
- false, false);
- break;
- #if WASM_ENABLE_BULK_MEMORY != 0
- case WASM_OP_MEMORY_INIT:
- {
- uint32 addr, segment;
- uint64 bytes, offset, seg_len;
- uint8 *data;
- segment = read_uint32(frame_ip);
- bytes = (uint64)POP_I32();
- offset = (uint64)POP_I32();
- addr = POP_I32();
- #ifndef OS_ENABLE_HW_BOUND_CHECK
- CHECK_BULK_MEMORY_OVERFLOW(addr, bytes, maddr);
- #else
- if ((uint64)(uint32)addr + bytes
- > (uint64)linear_mem_size)
- goto out_of_bounds;
- maddr = memory->memory_data + (uint32)addr;
- #endif
- seg_len = (uint64)module->module->data_segments[segment]
- ->data_length;
- data = module->module->data_segments[segment]->data;
- if (offset + bytes > seg_len)
- goto out_of_bounds;
- bh_memcpy_s(maddr, linear_mem_size - addr,
- data + offset, (uint32)bytes);
- break;
- }
- case WASM_OP_DATA_DROP:
- {
- uint32 segment;
- segment = read_uint32(frame_ip);
- module->module->data_segments[segment]->data_length = 0;
- break;
- }
- case WASM_OP_MEMORY_COPY:
- {
- uint32 dst, src, len;
- uint8 *mdst, *msrc;
- len = POP_I32();
- src = POP_I32();
- dst = POP_I32();
- #ifndef OS_ENABLE_HW_BOUND_CHECK
- CHECK_BULK_MEMORY_OVERFLOW(src, len, msrc);
- CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
- #else
- if ((uint64)(uint32)src + len > (uint64)linear_mem_size)
- goto out_of_bounds;
- msrc = memory->memory_data + (uint32)src;
- if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
- goto out_of_bounds;
- mdst = memory->memory_data + (uint32)dst;
- #endif
- /* allowing the destination and source to overlap */
- bh_memmove_s(mdst, linear_mem_size - dst, msrc, len);
- break;
- }
- case WASM_OP_MEMORY_FILL:
- {
- uint32 dst, len;
- uint8 fill_val, *mdst;
- len = POP_I32();
- fill_val = POP_I32();
- dst = POP_I32();
- #ifndef OS_ENABLE_HW_BOUND_CHECK
- CHECK_BULK_MEMORY_OVERFLOW(dst, len, mdst);
- #else
- if ((uint64)(uint32)dst + len > (uint64)linear_mem_size)
- goto out_of_bounds;
- mdst = memory->memory_data + (uint32)dst;
- #endif
- memset(mdst, fill_val, len);
- break;
- }
- #endif /* WASM_ENABLE_BULK_MEMORY */
- #if WASM_ENABLE_REF_TYPES != 0
- case WASM_OP_TABLE_INIT:
- {
- uint32 tbl_idx, elem_idx;
- uint64 n, s, d;
- WASMTableInstance *tbl_inst;
- elem_idx = read_uint32(frame_ip);
- bh_assert(elem_idx < module->module->table_seg_count);
- tbl_idx = read_uint32(frame_ip);
- bh_assert(tbl_idx < module->module->table_count);
- tbl_inst = wasm_get_table_inst(module, tbl_idx);
- n = (uint32)POP_I32();
- s = (uint32)POP_I32();
- d = (uint32)POP_I32();
- if (!n) {
- break;
- }
- if (n + s > module->module->table_segments[elem_idx]
- .function_count
- || d + n > tbl_inst->cur_size) {
- wasm_set_exception(module,
- "out of bounds table access");
- goto got_exception;
- }
- if (module->module->table_segments[elem_idx]
- .is_dropped) {
- wasm_set_exception(module,
- "out of bounds table access");
- goto got_exception;
- }
- if (!wasm_elem_is_passive(
- module->module->table_segments[elem_idx]
- .mode)) {
- wasm_set_exception(module,
- "out of bounds table access");
- goto got_exception;
- }
- bh_memcpy_s(
- (uint8 *)tbl_inst
- + offsetof(WASMTableInstance, elems)
- + d * sizeof(uint32),
- (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
- module->module->table_segments[elem_idx]
- .func_indexes
- + s,
- (uint32)(n * sizeof(uint32)));
- break;
- }
- case WASM_OP_ELEM_DROP:
- {
- uint32 elem_idx = read_uint32(frame_ip);
- bh_assert(elem_idx < module->module->table_seg_count);
- module->module->table_segments[elem_idx].is_dropped =
- true;
- break;
- }
- case WASM_OP_TABLE_COPY:
- {
- uint32 src_tbl_idx, dst_tbl_idx;
- uint64 n, s, d;
- WASMTableInstance *src_tbl_inst, *dst_tbl_inst;
- dst_tbl_idx = read_uint32(frame_ip);
- bh_assert(dst_tbl_idx < module->table_count);
- dst_tbl_inst = wasm_get_table_inst(module, dst_tbl_idx);
- src_tbl_idx = read_uint32(frame_ip);
- bh_assert(src_tbl_idx < module->table_count);
- src_tbl_inst = wasm_get_table_inst(module, src_tbl_idx);
- n = (uint32)POP_I32();
- s = (uint32)POP_I32();
- d = (uint32)POP_I32();
- if (d + n > dst_tbl_inst->cur_size
- || s + n > src_tbl_inst->cur_size) {
- wasm_set_exception(module,
- "out of bounds table access");
- goto got_exception;
- }
- /* if s >= d, copy from front to back */
- /* if s < d, copy from back to front */
- /* merge all together */
- bh_memmove_s((uint8 *)dst_tbl_inst
- + offsetof(WASMTableInstance, elems)
- + d * sizeof(uint32),
- (uint32)((dst_tbl_inst->cur_size - d)
- * sizeof(uint32)),
- (uint8 *)src_tbl_inst
- + offsetof(WASMTableInstance, elems)
- + s * sizeof(uint32),
- (uint32)(n * sizeof(uint32)));
- break;
- }
- case WASM_OP_TABLE_GROW:
- {
- uint32 tbl_idx, n, init_val, orig_tbl_sz;
- WASMTableInstance *tbl_inst;
- tbl_idx = read_uint32(frame_ip);
- bh_assert(tbl_idx < module->table_count);
- tbl_inst = wasm_get_table_inst(module, tbl_idx);
- orig_tbl_sz = tbl_inst->cur_size;
- n = POP_I32();
- init_val = POP_I32();
- if (!wasm_enlarge_table(module, tbl_idx, n, init_val)) {
- PUSH_I32(-1);
- }
- else {
- PUSH_I32(orig_tbl_sz);
- }
- break;
- }
- case WASM_OP_TABLE_SIZE:
- {
- uint32 tbl_idx;
- WASMTableInstance *tbl_inst;
- tbl_idx = read_uint32(frame_ip);
- bh_assert(tbl_idx < module->table_count);
- tbl_inst = wasm_get_table_inst(module, tbl_idx);
- PUSH_I32(tbl_inst->cur_size);
- break;
- }
- case WASM_OP_TABLE_FILL:
- {
- uint32 tbl_idx, n, fill_val, i;
- WASMTableInstance *tbl_inst;
- tbl_idx = read_uint32(frame_ip);
- bh_assert(tbl_idx < module->table_count);
- tbl_inst = wasm_get_table_inst(module, tbl_idx);
- n = POP_I32();
- fill_val = POP_I32();
- i = POP_I32();
- if (i + n > tbl_inst->cur_size) {
- wasm_set_exception(module,
- "out of bounds table access");
- goto got_exception;
- }
- for (; n != 0; i++, n--) {
- tbl_inst->elems[i] = fill_val;
- }
- break;
- }
- #endif /* WASM_ENABLE_REF_TYPES */
- default:
- wasm_set_exception(module, "unsupported opcode");
- goto got_exception;
- }
- HANDLE_OP_END();
- }
- #if WASM_ENABLE_SHARED_MEMORY != 0
- HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
- {
- uint32 offset = 0, addr;
- GET_OPCODE();
- if (opcode != WASM_OP_ATOMIC_FENCE) {
- offset = read_uint32(frame_ip);
- }
- switch (opcode) {
- case WASM_OP_ATOMIC_NOTIFY:
- {
- uint32 notify_count, ret;
- notify_count = POP_I32();
- addr = POP_I32();
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- ret = wasm_runtime_atomic_notify(
- (WASMModuleInstanceCommon *)module, maddr,
- notify_count);
- bh_assert((int32)ret >= 0);
- PUSH_I32(ret);
- break;
- }
- case WASM_OP_ATOMIC_WAIT32:
- {
- uint64 timeout;
- uint32 expect, ret;
- timeout = POP_I64();
- expect = POP_I32();
- addr = POP_I32();
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- ret = wasm_runtime_atomic_wait(
- (WASMModuleInstanceCommon *)module, maddr,
- (uint64)expect, timeout, false);
- if (ret == (uint32)-1)
- goto got_exception;
- PUSH_I32(ret);
- break;
- }
- case WASM_OP_ATOMIC_WAIT64:
- {
- uint64 timeout, expect;
- uint32 ret;
- timeout = POP_I64();
- expect = POP_I64();
- addr = POP_I32();
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(8);
- ret = wasm_runtime_atomic_wait(
- (WASMModuleInstanceCommon *)module, maddr, expect,
- timeout, true);
- if (ret == (uint32)-1)
- goto got_exception;
- PUSH_I32(ret);
- break;
- }
- case WASM_OP_ATOMIC_I32_LOAD:
- case WASM_OP_ATOMIC_I32_LOAD8_U:
- case WASM_OP_ATOMIC_I32_LOAD16_U:
- {
- uint32 readv;
- addr = POP_I32();
- if (opcode == WASM_OP_ATOMIC_I32_LOAD8_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(1);
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint32)(*(uint8 *)maddr);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_I32_LOAD16_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(2);
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint32)LOAD_U16(maddr);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- os_mutex_lock(&module->e->mem_lock);
- readv = LOAD_I32(maddr);
- os_mutex_unlock(&module->e->mem_lock);
- }
- PUSH_I32(readv);
- break;
- }
- case WASM_OP_ATOMIC_I64_LOAD:
- case WASM_OP_ATOMIC_I64_LOAD8_U:
- case WASM_OP_ATOMIC_I64_LOAD16_U:
- case WASM_OP_ATOMIC_I64_LOAD32_U:
- {
- uint64 readv;
- addr = POP_I32();
- if (opcode == WASM_OP_ATOMIC_I64_LOAD8_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(1);
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint64)(*(uint8 *)maddr);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_I64_LOAD16_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(2);
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint64)LOAD_U16(maddr);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_I64_LOAD32_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint64)LOAD_U32(maddr);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(8);
- os_mutex_lock(&module->e->mem_lock);
- readv = LOAD_I64(maddr);
- os_mutex_unlock(&module->e->mem_lock);
- }
- PUSH_I64(readv);
- break;
- }
- case WASM_OP_ATOMIC_I32_STORE:
- case WASM_OP_ATOMIC_I32_STORE8:
- case WASM_OP_ATOMIC_I32_STORE16:
- {
- uint32 sval;
- sval = (uint32)POP_I32();
- addr = POP_I32();
- if (opcode == WASM_OP_ATOMIC_I32_STORE8) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(1);
- os_mutex_lock(&module->e->mem_lock);
- *(uint8 *)maddr = (uint8)sval;
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_I32_STORE16) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(2);
- os_mutex_lock(&module->e->mem_lock);
- STORE_U16(maddr, (uint16)sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- os_mutex_lock(&module->e->mem_lock);
- STORE_U32(maddr, sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- break;
- }
- case WASM_OP_ATOMIC_I64_STORE:
- case WASM_OP_ATOMIC_I64_STORE8:
- case WASM_OP_ATOMIC_I64_STORE16:
- case WASM_OP_ATOMIC_I64_STORE32:
- {
- uint64 sval;
- sval = (uint64)POP_I64();
- addr = POP_I32();
- if (opcode == WASM_OP_ATOMIC_I64_STORE8) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(1);
- os_mutex_lock(&module->e->mem_lock);
- *(uint8 *)maddr = (uint8)sval;
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_I64_STORE16) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(2);
- os_mutex_lock(&module->e->mem_lock);
- STORE_U16(maddr, (uint16)sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_I64_STORE32) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- os_mutex_lock(&module->e->mem_lock);
- STORE_U32(maddr, (uint32)sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(8);
- os_mutex_lock(&module->e->mem_lock);
- STORE_I64(maddr, sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- break;
- }
- case WASM_OP_ATOMIC_RMW_I32_CMPXCHG:
- case WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U:
- case WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U:
- {
- uint32 readv, sval, expect;
- sval = POP_I32();
- expect = POP_I32();
- addr = POP_I32();
- if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG8_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(1);
- expect = (uint8)expect;
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint32)(*(uint8 *)maddr);
- if (readv == expect)
- *(uint8 *)maddr = (uint8)(sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_RMW_I32_CMPXCHG16_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(2);
- expect = (uint16)expect;
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint32)LOAD_U16(maddr);
- if (readv == expect)
- STORE_U16(maddr, (uint16)(sval));
- os_mutex_unlock(&module->e->mem_lock);
- }
- else {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- os_mutex_lock(&module->e->mem_lock);
- readv = LOAD_I32(maddr);
- if (readv == expect)
- STORE_U32(maddr, sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- PUSH_I32(readv);
- break;
- }
- case WASM_OP_ATOMIC_RMW_I64_CMPXCHG:
- case WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U:
- case WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U:
- case WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U:
- {
- uint64 readv, sval, expect;
- sval = (uint64)POP_I64();
- expect = (uint64)POP_I64();
- addr = POP_I32();
- if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG8_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 1, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(1);
- expect = (uint8)expect;
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint64)(*(uint8 *)maddr);
- if (readv == expect)
- *(uint8 *)maddr = (uint8)(sval);
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG16_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 2, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(2);
- expect = (uint16)expect;
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint64)LOAD_U16(maddr);
- if (readv == expect)
- STORE_U16(maddr, (uint16)(sval));
- os_mutex_unlock(&module->e->mem_lock);
- }
- else if (opcode == WASM_OP_ATOMIC_RMW_I64_CMPXCHG32_U) {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 4, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(4);
- expect = (uint32)expect;
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint64)LOAD_U32(maddr);
- if (readv == expect)
- STORE_U32(maddr, (uint32)(sval));
- os_mutex_unlock(&module->e->mem_lock);
- }
- else {
- CHECK_BULK_MEMORY_OVERFLOW(addr + offset, 8, maddr);
- CHECK_ATOMIC_MEMORY_ACCESS(8);
- os_mutex_lock(&module->e->mem_lock);
- readv = (uint64)LOAD_I64(maddr);
- if (readv == expect) {
- STORE_I64(maddr, sval);
- }
- os_mutex_unlock(&module->e->mem_lock);
- }
- PUSH_I64(readv);
- break;
- }
- DEF_ATOMIC_RMW_OPCODE(ADD, +);
- DEF_ATOMIC_RMW_OPCODE(SUB, -);
- DEF_ATOMIC_RMW_OPCODE(AND, &);
- DEF_ATOMIC_RMW_OPCODE(OR, |);
- DEF_ATOMIC_RMW_OPCODE(XOR, ^);
- /* xchg, ignore the read value, and store the given
- value: readv * 0 + sval */
- DEF_ATOMIC_RMW_OPCODE(XCHG, *0 +);
- }
- HANDLE_OP_END();
- }
- #endif
- HANDLE_OP(WASM_OP_IMPDEP)
- {
- frame = prev_frame;
- frame_ip = frame->ip;
- goto call_func_from_entry;
- }
- HANDLE_OP(WASM_OP_CALL)
- {
- #if WASM_ENABLE_THREAD_MGR != 0
- CHECK_SUSPEND_FLAGS();
- #endif
- fidx = read_uint32(frame_ip);
- #if WASM_ENABLE_MULTI_MODULE != 0
- if (fidx >= module->e->function_count) {
- wasm_set_exception(module, "unknown function");
- goto got_exception;
- }
- #endif
- cur_func = module->e->functions + fidx;
- goto call_func_from_interp;
- }
- #if WASM_ENABLE_TAIL_CALL != 0
- HANDLE_OP(WASM_OP_RETURN_CALL)
- {
- #if WASM_ENABLE_THREAD_MGR != 0
- CHECK_SUSPEND_FLAGS();
- #endif
- fidx = read_uint32(frame_ip);
- #if WASM_ENABLE_MULTI_MODULE != 0
- if (fidx >= module->e->function_count) {
- wasm_set_exception(module, "unknown function");
- goto got_exception;
- }
- #endif
- cur_func = module->e->functions + fidx;
- goto call_func_from_return_call;
- }
- #endif /* WASM_ENABLE_TAIL_CALL */
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- default:
- wasm_set_exception(module, "unsupported opcode");
- goto got_exception;
- }
- #endif
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- HANDLE_OP(WASM_OP_UNUSED_0x06)
- HANDLE_OP(WASM_OP_UNUSED_0x07)
- HANDLE_OP(WASM_OP_UNUSED_0x08)
- HANDLE_OP(WASM_OP_UNUSED_0x09)
- HANDLE_OP(WASM_OP_UNUSED_0x0a)
- #if WASM_ENABLE_TAIL_CALL == 0
- HANDLE_OP(WASM_OP_RETURN_CALL)
- HANDLE_OP(WASM_OP_RETURN_CALL_INDIRECT)
- #endif
- #if WASM_ENABLE_SHARED_MEMORY == 0
- HANDLE_OP(WASM_OP_ATOMIC_PREFIX)
- #endif
- #if WASM_ENABLE_REF_TYPES == 0
- HANDLE_OP(WASM_OP_TABLE_GET)
- HANDLE_OP(WASM_OP_TABLE_SET)
- HANDLE_OP(WASM_OP_REF_NULL)
- HANDLE_OP(WASM_OP_REF_IS_NULL)
- HANDLE_OP(WASM_OP_REF_FUNC)
- #endif
- /* SELECT_T is converted to SELECT or SELECT_64 */
- HANDLE_OP(WASM_OP_SELECT_T)
- HANDLE_OP(WASM_OP_UNUSED_0x14)
- HANDLE_OP(WASM_OP_UNUSED_0x15)
- HANDLE_OP(WASM_OP_UNUSED_0x16)
- HANDLE_OP(WASM_OP_UNUSED_0x17)
- HANDLE_OP(WASM_OP_UNUSED_0x18)
- HANDLE_OP(WASM_OP_UNUSED_0x19)
- HANDLE_OP(WASM_OP_UNUSED_0x27)
- /* optimized op code */
- HANDLE_OP(WASM_OP_F32_STORE)
- HANDLE_OP(WASM_OP_F64_STORE)
- HANDLE_OP(WASM_OP_F32_LOAD)
- HANDLE_OP(WASM_OP_F64_LOAD)
- HANDLE_OP(EXT_OP_GET_LOCAL_FAST)
- HANDLE_OP(WASM_OP_GET_LOCAL)
- HANDLE_OP(WASM_OP_DROP)
- HANDLE_OP(WASM_OP_DROP_64)
- HANDLE_OP(WASM_OP_BLOCK)
- HANDLE_OP(WASM_OP_LOOP)
- HANDLE_OP(WASM_OP_END)
- HANDLE_OP(WASM_OP_NOP)
- HANDLE_OP(EXT_OP_BLOCK)
- HANDLE_OP(EXT_OP_LOOP)
- HANDLE_OP(EXT_OP_IF)
- HANDLE_OP(EXT_OP_BR_TABLE_CACHE)
- {
- wasm_set_exception(module, "unsupported opcode");
- goto got_exception;
- }
- #endif
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- continue;
- #else
- FETCH_OPCODE_AND_DISPATCH();
- #endif
- #if WASM_ENABLE_TAIL_CALL != 0
- call_func_from_return_call:
- {
- uint32 *lp_base;
- uint32 *lp;
- int i;
- if (!(lp_base = lp = wasm_runtime_malloc(cur_func->param_cell_num
- * sizeof(uint32)))) {
- wasm_set_exception(module, "allocate memory failed");
- goto got_exception;
- }
- for (i = 0; i < cur_func->param_count; i++) {
- if (cur_func->param_types[i] == VALUE_TYPE_I64
- || cur_func->param_types[i] == VALUE_TYPE_F64) {
- PUT_I64_TO_ADDR(
- lp, GET_OPERAND(uint64, I64,
- 2 * (cur_func->param_count - i - 1)));
- lp += 2;
- }
- else {
- *lp = GET_OPERAND(uint32, I32,
- (2 * (cur_func->param_count - i - 1)));
- lp++;
- }
- }
- frame->lp = frame->operand + cur_func->const_cell_num;
- if (lp - lp_base > 0) {
- word_copy(frame->lp, lp_base, lp - lp_base);
- }
- wasm_runtime_free(lp_base);
- FREE_FRAME(exec_env, frame);
- frame_ip += cur_func->param_count * sizeof(int16);
- wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
- goto call_func_from_entry;
- }
- #endif /* WASM_ENABLE_TAIL_CALL */
- call_func_from_interp:
- {
- /* Only do the copy when it's called from interpreter. */
- WASMInterpFrame *outs_area = wasm_exec_env_wasm_stack_top(exec_env);
- int i;
- #if WASM_ENABLE_MULTI_MODULE != 0
- if (cur_func->is_import_func) {
- outs_area->lp = outs_area->operand
- + (cur_func->import_func_inst
- ? cur_func->import_func_inst->const_cell_num
- : 0);
- }
- else
- #endif
- {
- outs_area->lp = outs_area->operand + cur_func->const_cell_num;
- }
- if ((uint8 *)(outs_area->lp + cur_func->param_cell_num)
- > exec_env->wasm_stack.s.top_boundary) {
- wasm_set_exception(module, "wasm operand stack overflow");
- goto got_exception;
- }
- for (i = 0; i < cur_func->param_count; i++) {
- if (cur_func->param_types[i] == VALUE_TYPE_I64
- || cur_func->param_types[i] == VALUE_TYPE_F64) {
- PUT_I64_TO_ADDR(
- outs_area->lp,
- GET_OPERAND(uint64, I64,
- 2 * (cur_func->param_count - i - 1)));
- outs_area->lp += 2;
- }
- else {
- *outs_area->lp = GET_OPERAND(
- uint32, I32, (2 * (cur_func->param_count - i - 1)));
- outs_area->lp++;
- }
- }
- frame_ip += cur_func->param_count * sizeof(int16);
- if (cur_func->ret_cell_num != 0) {
- /* Get the first return value's offset. Since loader emit
- * all return values' offset so we must skip remain return
- * values' offsets.
- */
- WASMType *func_type;
- if (cur_func->is_import_func)
- func_type = cur_func->u.func_import->func_type;
- else
- func_type = cur_func->u.func->func_type;
- frame->ret_offset = GET_OFFSET();
- frame_ip += 2 * (func_type->result_count - 1);
- }
- SYNC_ALL_TO_FRAME();
- prev_frame = frame;
- }
- call_func_from_entry:
- {
- if (cur_func->is_import_func) {
- #if WASM_ENABLE_MULTI_MODULE != 0
- if (cur_func->import_func_inst) {
- wasm_interp_call_func_import(module, exec_env, cur_func,
- prev_frame);
- }
- else
- #endif
- {
- wasm_interp_call_func_native(module, exec_env, cur_func,
- prev_frame);
- }
- prev_frame = frame->prev_frame;
- cur_func = frame->function;
- UPDATE_ALL_FROM_FRAME();
- /* update memory size, no need to update memory ptr as
- it isn't changed in wasm_enlarge_memory */
- #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
- || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
- || WASM_ENABLE_BULK_MEMORY != 0
- if (memory)
- linear_mem_size = num_bytes_per_page * memory->cur_page_count;
- #endif
- if (wasm_get_exception(module))
- goto got_exception;
- }
- else {
- WASMFunction *cur_wasm_func = cur_func->u.func;
- all_cell_num = (uint64)cur_func->param_cell_num
- + (uint64)cur_func->local_cell_num
- + (uint64)cur_func->const_cell_num
- + (uint64)cur_wasm_func->max_stack_cell_num;
- if (all_cell_num >= UINT32_MAX) {
- wasm_set_exception(module, "wasm operand stack overflow");
- goto got_exception;
- }
- frame_size = wasm_interp_interp_frame_size((uint32)all_cell_num);
- if (!(frame = ALLOC_FRAME(exec_env, frame_size, prev_frame))) {
- frame = prev_frame;
- goto got_exception;
- }
- /* Initialize the interpreter context. */
- frame->function = cur_func;
- frame_ip = wasm_get_func_code(cur_func);
- frame_ip_end = wasm_get_func_code_end(cur_func);
- frame_lp = frame->lp =
- frame->operand + cur_wasm_func->const_cell_num;
- /* Initialize the consts */
- if (cur_wasm_func->const_cell_num > 0) {
- word_copy(frame->operand, (uint32 *)cur_wasm_func->consts,
- cur_wasm_func->const_cell_num);
- }
- /* Initialize the local variables */
- memset(frame_lp + cur_func->param_cell_num, 0,
- (uint32)(cur_func->local_cell_num * 4));
- wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)frame);
- }
- HANDLE_OP_END();
- }
- return_func:
- {
- FREE_FRAME(exec_env, frame);
- wasm_exec_env_set_cur_frame(exec_env, (WASMRuntimeFrame *)prev_frame);
- if (!prev_frame->ip)
- /* Called from native. */
- return;
- RECOVER_CONTEXT(prev_frame);
- HANDLE_OP_END();
- }
- (void)frame_ip_end;
- #if WASM_ENABLE_SHARED_MEMORY != 0
- unaligned_atomic:
- wasm_set_exception(module, "unaligned atomic");
- goto got_exception;
- #endif
- #if !defined(OS_ENABLE_HW_BOUND_CHECK) \
- || WASM_CPU_SUPPORTS_UNALIGNED_ADDR_ACCESS == 0 \
- || WASM_ENABLE_BULK_MEMORY != 0
- out_of_bounds:
- wasm_set_exception(module, "out of bounds memory access");
- #endif
- got_exception:
- SYNC_ALL_TO_FRAME();
- return;
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- }
- #else
- FETCH_OPCODE_AND_DISPATCH();
- #endif
- }
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- void **
- wasm_interp_get_handle_table()
- {
- WASMModuleInstance module;
- memset(&module, 0, sizeof(WASMModuleInstance));
- wasm_interp_call_func_bytecode(&module, NULL, NULL, NULL);
- return global_handle_table;
- }
- #endif
- void
- wasm_interp_call_wasm(WASMModuleInstance *module_inst, WASMExecEnv *exec_env,
- WASMFunctionInstance *function, uint32 argc,
- uint32 argv[])
- {
- WASMRuntimeFrame *prev_frame = wasm_exec_env_get_cur_frame(exec_env);
- WASMInterpFrame *frame, *outs_area;
- /* Allocate sufficient cells for all kinds of return values. */
- unsigned all_cell_num =
- function->ret_cell_num > 2 ? function->ret_cell_num : 2,
- i;
- /* This frame won't be used by JITed code, so only allocate interp
- frame here. */
- unsigned frame_size = wasm_interp_interp_frame_size(all_cell_num);
- if (argc < function->param_cell_num) {
- char buf[128];
- snprintf(buf, sizeof(buf),
- "invalid argument count %" PRIu32
- ", must be no smaller than %" PRIu32,
- argc, (uint32)function->param_cell_num);
- wasm_set_exception(module_inst, buf);
- return;
- }
- argc = function->param_cell_num;
- #ifndef OS_ENABLE_HW_BOUND_CHECK
- if ((uint8 *)&prev_frame < exec_env->native_stack_boundary) {
- wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
- "native stack overflow");
- return;
- }
- #endif
- if (!(frame =
- ALLOC_FRAME(exec_env, frame_size, (WASMInterpFrame *)prev_frame)))
- return;
- outs_area = wasm_exec_env_wasm_stack_top(exec_env);
- frame->function = NULL;
- frame->ip = NULL;
- /* There is no local variable. */
- frame->lp = frame->operand + 0;
- frame->ret_offset = 0;
- if ((uint8 *)(outs_area->operand + function->const_cell_num + argc)
- > exec_env->wasm_stack.s.top_boundary) {
- wasm_set_exception((WASMModuleInstance *)exec_env->module_inst,
- "wasm operand stack overflow");
- return;
- }
- if (argc > 0)
- word_copy(outs_area->operand + function->const_cell_num, argv, argc);
- wasm_exec_env_set_cur_frame(exec_env, frame);
- if (function->is_import_func) {
- #if WASM_ENABLE_MULTI_MODULE != 0
- if (function->import_module_inst) {
- LOG_DEBUG("it is a function of a sub module");
- wasm_interp_call_func_import(module_inst, exec_env, function,
- frame);
- }
- else
- #endif
- {
- LOG_DEBUG("it is an native function");
- wasm_interp_call_func_native(module_inst, exec_env, function,
- frame);
- }
- }
- else {
- wasm_interp_call_func_bytecode(module_inst, exec_env, function, frame);
- }
- /* Output the return value to the caller */
- if (!wasm_get_exception(module_inst)) {
- for (i = 0; i < function->ret_cell_num; i++)
- argv[i] = *(frame->lp + i);
- }
- else {
- #if WASM_ENABLE_DUMP_CALL_STACK != 0
- if (wasm_interp_create_call_stack(exec_env)) {
- wasm_interp_dump_call_stack(exec_env, true, NULL, 0);
- }
- #endif
- LOG_DEBUG("meet an exception %s", wasm_get_exception(module_inst));
- }
- wasm_exec_env_set_cur_frame(exec_env, prev_frame);
- FREE_FRAME(exec_env, frame);
- #if WASM_ENABLE_OPCODE_COUNTER != 0
- wasm_interp_dump_op_count();
- #endif
- }
|