| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526 |
- /*
- * Copyright (C) 2019 Intel Corporation. All rights reserved.
- * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- */
- #include "wasm_interp.h"
- #include "bh_memory.h"
- #include "bh_log.h"
- #include "wasm_runtime.h"
- #include "wasm_opcode.h"
- #include "wasm_loader.h"
- #include "../common/wasm_exec_env.h"
- typedef int32 CellType_I32;
- typedef int64 CellType_I64;
- typedef float32 CellType_F32;
- typedef float64 CellType_F64;
- #define BR_TABLE_TMP_BUF_LEN 32
- /* 64-bit Memory accessors. */
- #if WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0
- #define PUT_I64_TO_ADDR(addr, value) do { \
- *(int64*)(addr) = (int64)(value); \
- } while (0)
- #define PUT_F64_TO_ADDR(addr, value) do { \
- *(float64*)(addr) = (float64)(value); \
- } while (0)
- #define GET_I64_FROM_ADDR(addr) (*(int64*)(addr))
- #define GET_F64_FROM_ADDR(addr) (*(float64*)(addr))
- /* For STORE opcodes */
- #define STORE_I64 PUT_I64_TO_ADDR
- #define STORE_U32(addr, value) do { \
- *(uint32*)(addr) = (uint32)(value); \
- } while (0)
- #define STORE_U16(addr, value) do { \
- *(uint16*)(addr) = (uint16)(value); \
- } while (0)
- /* For LOAD opcodes */
- #define LOAD_I64(addr) (*(int64*)(addr))
- #define LOAD_F64(addr) (*(float64*)(addr))
- #define LOAD_I32(addr) (*(int32*)(addr))
- #define LOAD_U32(addr) (*(uint32*)(addr))
- #define LOAD_I16(addr) (*(int16*)(addr))
- #define LOAD_U16(addr) (*(uint16*)(addr))
- #else /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
- #define PUT_I64_TO_ADDR(addr, value) do { \
- union { int64 val; uint32 parts[2]; } u; \
- u.val = (int64)(value); \
- (addr)[0] = u.parts[0]; \
- (addr)[1] = u.parts[1]; \
- } while (0)
- #define PUT_F64_TO_ADDR(addr, value) do { \
- union { float64 val; uint32 parts[2]; } u; \
- u.val = (value); \
- (addr)[0] = u.parts[0]; \
- (addr)[1] = u.parts[1]; \
- } while (0)
- static inline int64
- GET_I64_FROM_ADDR(uint32 *addr)
- {
- union { int64 val; uint32 parts[2]; } u;
- u.parts[0] = addr[0];
- u.parts[1] = addr[1];
- return u.val;
- }
- static inline float64
- GET_F64_FROM_ADDR (uint32 *addr)
- {
- union { float64 val; uint32 parts[2]; } u;
- u.parts[0] = addr[0];
- u.parts[1] = addr[1];
- return u.val;
- }
- /* For STORE opcodes */
- #define STORE_I64(addr, value) do { \
- uintptr_t addr1 = (uintptr_t)(addr); \
- union { int64 val; uint32 u32[2]; \
- uint16 u16[4]; uint8 u8[8]; } u; \
- if ((addr1 & (uintptr_t)7) == 0) \
- *(int64*)(addr) = (int64)(value); \
- else { \
- u.val = (int64)(value); \
- if ((addr1 & (uintptr_t)3) == 0) { \
- ((uint32*)(addr))[0] = u.u32[0]; \
- ((uint32*)(addr))[1] = u.u32[1]; \
- } \
- else if ((addr1 & (uintptr_t)1) == 0) { \
- ((uint16*)(addr))[0] = u.u16[0]; \
- ((uint16*)(addr))[1] = u.u16[1]; \
- ((uint16*)(addr))[2] = u.u16[2]; \
- ((uint16*)(addr))[3] = u.u16[3]; \
- } \
- else { \
- int32 t; \
- for (t = 0; t < 8; t++) \
- ((uint8*)(addr))[t] = u.u8[t]; \
- } \
- } \
- } while (0)
- #define STORE_U32(addr, value) do { \
- uintptr_t addr1 = (uintptr_t)(addr); \
- union { uint32 val; \
- uint16 u16[2]; uint8 u8[4]; } u; \
- if ((addr1 & (uintptr_t)3) == 0) \
- *(uint32*)(addr) = (uint32)(value); \
- else { \
- u.val = (uint32)(value); \
- if ((addr1 & (uintptr_t)1) == 0) { \
- ((uint16*)(addr))[0] = u.u16[0]; \
- ((uint16*)(addr))[1] = u.u16[1]; \
- } \
- else { \
- ((uint8*)(addr))[0] = u.u8[0]; \
- ((uint8*)(addr))[1] = u.u8[1]; \
- ((uint8*)(addr))[2] = u.u8[2]; \
- ((uint8*)(addr))[3] = u.u8[3]; \
- } \
- } \
- } while (0)
- #define STORE_U16(addr, value) do { \
- union { uint16 val; uint8 u8[2]; } u; \
- u.val = (uint16)(value); \
- ((uint8*)(addr))[0] = u.u8[0]; \
- ((uint8*)(addr))[1] = u.u8[1]; \
- } while (0)
- /* For LOAD opcodes */
- static inline int64
- LOAD_I64(void *addr)
- {
- uintptr_t addr1 = (uintptr_t)addr;
- union { int64 val; uint32 u32[2];
- uint16 u16[4]; uint8 u8[8]; } u;
- if ((addr1 & (uintptr_t)7) == 0)
- return *(int64*)addr;
- if ((addr1 & (uintptr_t)3) == 0) {
- u.u32[0] = ((uint32*)addr)[0];
- u.u32[1] = ((uint32*)addr)[1];
- }
- else if ((addr1 & (uintptr_t)1) == 0) {
- u.u16[0] = ((uint16*)addr)[0];
- u.u16[1] = ((uint16*)addr)[1];
- u.u16[2] = ((uint16*)addr)[2];
- u.u16[3] = ((uint16*)addr)[3];
- }
- else {
- int32 t;
- for (t = 0; t < 8; t++)
- u.u8[t] = ((uint8*)addr)[t];
- }
- return u.val;
- }
- static inline float64
- LOAD_F64(void *addr)
- {
- uintptr_t addr1 = (uintptr_t)addr;
- union { float64 val; uint32 u32[2];
- uint16 u16[4]; uint8 u8[8]; } u;
- if ((addr1 & (uintptr_t)7) == 0)
- return *(float64*)addr;
- if ((addr1 & (uintptr_t)3) == 0) {
- u.u32[0] = ((uint32*)addr)[0];
- u.u32[1] = ((uint32*)addr)[1];
- }
- else if ((addr1 & (uintptr_t)1) == 0) {
- u.u16[0] = ((uint16*)addr)[0];
- u.u16[1] = ((uint16*)addr)[1];
- u.u16[2] = ((uint16*)addr)[2];
- u.u16[3] = ((uint16*)addr)[3];
- }
- else {
- int32 t;
- for (t = 0; t < 8; t++)
- u.u8[t] = ((uint8*)addr)[t];
- }
- return u.val;
- }
- static inline int32
- LOAD_I32(void *addr)
- {
- uintptr_t addr1 = (uintptr_t)addr;
- union { int32 val; uint16 u16[2]; uint8 u8[4]; } u;
- if ((addr1 & (uintptr_t)3) == 0)
- return *(int32*)addr;
- if ((addr1 & (uintptr_t)1) == 0) {
- u.u16[0] = ((uint16*)addr)[0];
- u.u16[1] = ((uint16*)addr)[1];
- }
- else {
- u.u8[0] = ((uint8*)addr)[0];
- u.u8[1] = ((uint8*)addr)[1];
- u.u8[2] = ((uint8*)addr)[2];
- u.u8[3] = ((uint8*)addr)[3];
- }
- return u.val;
- }
- static inline int16
- LOAD_I16(void *addr)
- {
- union { int16 val; uint8 u8[2]; } u;
- u.u8[0] = ((uint8*)addr)[0];
- u.u8[1] = ((uint8*)addr)[1];
- return u.val;
- }
- #define LOAD_U32(addr) ((uint32)LOAD_I32(addr))
- #define LOAD_U16(addr) ((uint16)LOAD_I16(addr))
- #endif /* WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0 */
- #define CHECK_MEMORY_OVERFLOW() do { \
- uint32 offset1 = offset + addr; \
- /* if (flags != 2) \
- LOG_VERBOSE("unaligned load/store in wasm interp, flag: %d.\n", flags); */\
- /* The WASM spec doesn't require that the dynamic address operand must be \
- unsigned, so we don't check whether integer overflow or not here. */ \
- /* if (offset1 < offset) \
- goto out_of_bounds; */ \
- if (offset1 < memory_data_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; \
- if (maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] > memory->end_addr) \
- goto out_of_bounds; \
- } \
- else if (offset1 > heap_base_offset \
- && offset1 < heap_base_offset + heap_data_size) { \
- /* If offset1 is in valid range, maddr must also be in valid range, \
- no need to check it again. */ \
- maddr = memory->heap_data + offset1 - memory->heap_base_offset; \
- if (maddr + LOAD_SIZE[opcode - WASM_OP_I32_LOAD] > memory->heap_data_end) \
- goto out_of_bounds; \
- } \
- else \
- goto out_of_bounds; \
- } while (0)
- static inline uint32
- rotl32(uint32 n, uint32 c)
- {
- const uint32 mask = (31);
- c = c % 32;
- c &= mask;
- return (n<<c) | (n>>( (-c)&mask ));
- }
- static inline uint32
- rotr32(uint32 n, uint32 c)
- {
- const uint32 mask = (31);
- c = c % 32;
- c &= mask;
- return (n>>c) | (n<<( (-c)&mask ));
- }
- static inline uint64
- rotl64(uint64 n, uint64 c)
- {
- const uint64 mask = (63);
- c = c % 64;
- c &= mask;
- return (n<<c) | (n>>( (-c)&mask ));
- }
- static inline uint64
- rotr64(uint64 n, uint64 c)
- {
- const uint64 mask = (63);
- c = c % 64;
- c &= mask;
- return (n>>c) | (n<<( (-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 uint64
- read_leb(const uint8 *buf, uint32 *p_offset, uint32 maxbits, bool sign)
- {
- uint64 result = 0;
- uint32 shift = 0;
- uint32 bcnt = 0;
- uint64 byte;
- while (true) {
- byte = buf[*p_offset];
- *p_offset += 1;
- result |= ((byte & 0x7f) << shift);
- shift += 7;
- if ((byte & 0x80) == 0) {
- break;
- }
- bcnt += 1;
- }
- if (sign && (shift < maxbits) && (byte & 0x40)) {
- /* Sign extend */
- result |= - ((uint64)1 << shift);
- }
- return result;
- }
- #define PUSH_I32(value) do { \
- *(int32*)frame_sp++ = (int32)(value); \
- } while (0)
- #define PUSH_F32(value) do { \
- *(float32*)frame_sp++ = (float32)(value); \
- } while (0)
- #define PUSH_I64(value) do { \
- PUT_I64_TO_ADDR(frame_sp, value); \
- frame_sp += 2; \
- } while (0)
- #define PUSH_F64(value) do { \
- PUT_F64_TO_ADDR(frame_sp, value); \
- frame_sp += 2; \
- } while (0)
- #define PUSH_CSP(type, ret_type, start, else_, end) do {\
- bh_assert(frame_csp < frame->csp_boundary); \
- frame_csp->block_type = type; \
- frame_csp->return_type = ret_type; \
- frame_csp->start_addr = start; \
- frame_csp->end_addr = end; \
- frame_csp->frame_sp = frame_sp; \
- frame_csp++; \
- } while (0)
- #define POP_I32() (--frame_sp, *(int32*)frame_sp)
- #define POP_F32() (--frame_sp, *(float32*)frame_sp)
- #define POP_I64() (frame_sp -= 2, GET_I64_FROM_ADDR(frame_sp))
- #define POP_F64() (frame_sp -= 2, GET_F64_FROM_ADDR(frame_sp))
- #define POP_CSP_CHECK_OVERFLOW(n) do { \
- bh_assert(frame_csp - n >= frame->csp_bottom); \
- } while (0)
- #define POP_CSP() do { \
- POP_CSP_CHECK_OVERFLOW(1); \
- --frame_csp; \
- } while (0)
- #define POP_CSP_N(n) do { \
- uint32 *frame_sp_old = frame_sp; \
- POP_CSP_CHECK_OVERFLOW(n + 1); \
- frame_csp -= n; \
- if ((frame_csp - 1)->block_type != BLOCK_TYPE_LOOP) \
- /* block block/if/function, jump to end of block */ \
- frame_ip = (frame_csp - 1)->end_addr; \
- else /* loop block, jump to start of block */ \
- frame_ip = (frame_csp - 1)->start_addr; \
- /* copy return value of block */ \
- frame_sp = (frame_csp - 1)->frame_sp; \
- switch ((frame_csp - 1)->return_type) { \
- case VALUE_TYPE_I32: \
- PUSH_I32(*(frame_sp_old - 1)); \
- break; \
- case VALUE_TYPE_I64: \
- PUSH_I64(GET_I64_FROM_ADDR(frame_sp_old - 2)); \
- break; \
- case VALUE_TYPE_F32: \
- PUSH_F32(*(float32*)(frame_sp_old - 1)); \
- break; \
- case VALUE_TYPE_F64: \
- PUSH_F64(GET_F64_FROM_ADDR(frame_sp_old - 2)); \
- break; \
- } \
- } while (0)
- /* Pop the given number of elements from the given frame's stack. */
- #define POP(N) do { \
- int n = (N); \
- frame_sp -= n; \
- } while (0)
- #define SYNC_ALL_TO_FRAME() do { \
- frame->sp = frame_sp; \
- frame->ip = frame_ip; \
- frame->csp = frame_csp; \
- } while (0)
- #define UPDATE_ALL_FROM_FRAME() do { \
- frame_sp = frame->sp; \
- frame_ip = frame->ip; \
- frame_csp = frame->csp; \
- } while (0)
- #define read_leb_int64(p, p_end, res) do { \
- uint8 _val = *p; \
- if (!(_val & 0x80)) { \
- res = (int64)_val; \
- if (_val & 0x40) \
- /* sign extend */ \
- res |= 0xFFFFFFFFFFFFFF80LL; \
- p++; \
- break; \
- } \
- uint32 _off = 0; \
- res = (int64)read_leb(p, &_off, 64, true); \
- p += _off; \
- } while (0)
- #define read_leb_uint32(p, p_end, res) do { \
- uint8 _val = *p; \
- if (!(_val & 0x80)) { \
- res = _val; \
- p++; \
- break; \
- } \
- uint32 _off = 0; \
- res = (uint32)read_leb(p, &_off, 32, false); \
- p += _off; \
- } while (0)
- #define read_leb_int32(p, p_end, res) do { \
- uint8 _val = *p; \
- if (!(_val & 0x80)) { \
- res = (int32)_val; \
- if (_val & 0x40) \
- /* sign extend */ \
- res |= 0xFFFFFF80; \
- p++; \
- break; \
- } \
- uint32 _off = 0; \
- res = (int32)read_leb(p, &_off, 32, true); \
- p += _off; \
- } while (0)
- #define RECOVER_CONTEXT(new_frame) do { \
- frame = (new_frame); \
- cur_func = frame->function; \
- prev_frame = frame->prev_frame; \
- frame_ip = frame->ip; \
- frame_ip_end = wasm_get_func_code_end(cur_func); \
- frame_lp = frame->lp; \
- frame_sp = frame->sp; \
- frame_csp = frame->csp; \
- } while (0)
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- #define GET_OPCODE() opcode = *(frame_ip - 1);
- #else
- #define GET_OPCODE() (void)0
- #endif
- #define DEF_OP_I_CONST(ctype, src_op_type) do { \
- ctype cval; \
- read_leb_##ctype(frame_ip, frame_ip_end, cval); \
- PUSH_##src_op_type(cval); \
- } while (0)
- #define DEF_OP_EQZ(src_op_type) do { \
- int32 val; \
- val = POP_##src_op_type() == 0; \
- PUSH_I32(val); \
- } while (0)
- #define DEF_OP_CMP(src_type, src_op_type, cond) do { \
- uint32 res; \
- src_type val1, val2; \
- val2 = (src_type)POP_##src_op_type(); \
- val1 = (src_type)POP_##src_op_type(); \
- res = val1 cond val2; \
- PUSH_I32(res); \
- } while (0)
- #define DEF_OP_BIT_COUNT(src_type, src_op_type, operation) do { \
- src_type val1, val2; \
- val1 = (src_type)POP_##src_op_type(); \
- val2 = (src_type)operation(val1); \
- PUSH_##src_op_type(val2); \
- } while (0)
- #define DEF_OP_NUMERIC(src_type1, src_type2, src_op_type, operation) do { \
- frame_sp -= sizeof(src_type2)/sizeof(uint32); \
- *(src_type1*)(frame_sp - sizeof(src_type1)/sizeof(uint32)) operation##= \
- *(src_type2*)(frame_sp); \
- } while (0)
- #if WASM_CPU_SUPPORTS_UNALIGNED_64BIT_ACCESS != 0
- #define DEF_OP_NUMERIC_64 DEF_OP_NUMERIC
- #else
- #define DEF_OP_NUMERIC_64(src_type1, src_type2, src_op_type, operation) do {\
- src_type1 val1; \
- src_type2 val2; \
- frame_sp -= 2; \
- val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
- val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
- val1 operation##= val2; \
- PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
- } while (0)
- #endif
- #define DEF_OP_NUMERIC2(src_type1, src_type2, src_op_type, operation) do { \
- frame_sp -= sizeof(src_type2)/sizeof(uint32); \
- *(src_type1*)(frame_sp - sizeof(src_type1)/sizeof(uint32)) operation##= \
- (*(src_type2*)(frame_sp) % 32); \
- } while (0)
- #define DEF_OP_NUMERIC2_64(src_type1, src_type2, src_op_type, operation) do { \
- src_type1 val1; \
- src_type2 val2; \
- frame_sp -= 2; \
- val1 = (src_type1)GET_##src_op_type##_FROM_ADDR(frame_sp - 2); \
- val2 = (src_type2)GET_##src_op_type##_FROM_ADDR(frame_sp); \
- val1 operation##= (val2 % 64); \
- PUT_##src_op_type##_TO_ADDR(frame_sp - 2, val1); \
- } while (0)
- #define DEF_OP_MATH(src_type, src_op_type, method) do { \
- src_type val; \
- val = POP_##src_op_type(); \
- PUSH_##src_op_type(method(val)); \
- } while (0)
- #define DEF_OP_TRUNC(dst_type, dst_op_type, src_type, src_op_type, \
- min_cond, max_cond) do { \
- src_type value = POP_##src_op_type(); \
- if (isnan(value)) { \
- wasm_set_exception(module, "invalid conversion to integer"); \
- goto got_exception; \
- } \
- else if (value min_cond || value max_cond) { \
- wasm_set_exception(module, "integer overflow"); \
- goto got_exception; \
- } \
- PUSH_##dst_op_type(((dst_type)value)); \
- } 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)
- #define GET_LOCAL_INDEX_TYPE_AND_OFFSET() do { \
- uint32 param_count = cur_func->param_count; \
- read_leb_uint32(frame_ip, frame_ip_end, local_idx); \
- 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)
- 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)0xffffffffffffff00;
- return val;
- }
- static inline int64
- sign_ext_16_64(int16 val)
- {
- if (val & 0x8000)
- return (int64)val | (int64)0xffffffffffff0000;
- return val;
- }
- static inline int64
- sign_ext_32_64(int32 val)
- {
- if (val & (int32)0x80000000)
- return (int64)val | (int64)0xffffffff00000000;
- return val;
- }
- static inline void
- word_copy(uint32 *dest, uint32 *src, unsigned 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;
- else {
- wasm_set_exception((WASMModuleInstance*)exec_env->module_inst,
- "WASM interp failed: stack overflow.");
- }
- return frame;
- }
- static inline void
- FREE_FRAME(WASMExecEnv *exec_env, WASMInterpFrame *frame)
- {
- wasm_exec_env_free_wasm_frame(exec_env, frame);
- }
- static void
- wasm_interp_call_func_native(WASMModuleInstance *module_inst,
- WASMExecEnv *exec_env,
- WASMFunctionInstance *cur_func,
- WASMInterpFrame *prev_frame)
- {
- unsigned local_cell_num = 2;
- WASMInterpFrame *frame;
- uint32 argv_ret[2];
- 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->sp = frame->lp + local_cell_num;
- wasm_exec_env_set_cur_frame(exec_env, frame);
- if (!cur_func->u.func_import->func_ptr_linked) {
- char buf[128];
- snprintf(buf,
- sizeof(buf), "fail to call unlinked import function (%s, %s)",
- cur_func->u.func_import->module_name,
- cur_func->u.func_import->field_name);
- wasm_set_exception((WASMModuleInstance*)module_inst, buf);
- return;
- }
- ret = wasm_runtime_invoke_native(cur_func->u.func_import->func_ptr_linked,
- cur_func->u.func_import->func_type,
- exec_env,
- frame->lp, cur_func->param_cell_num, argv_ret);
- if (!ret)
- return;
- if (cur_func->ret_cell_num == 1) {
- prev_frame->sp[0] = argv_ret[0];
- prev_frame->sp++;
- }
- else if (cur_func->ret_cell_num == 2) {
- prev_frame->sp[0] = argv_ret[0];
- prev_frame->sp[1] = argv_ret[1];
- prev_frame->sp += 2;
- }
- FREE_FRAME(exec_env, frame);
- wasm_exec_env_set_cur_frame(exec_env, prev_frame);
- }
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- #define HANDLE_OP(opcode) HANDLE_##opcode
- #define FETCH_OPCODE_AND_DISPATCH() goto *handle_table[*frame_ip++]
- #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 */
- typedef struct BlockAddrCache {
- uint8 *frame_ip;
- uint8 *else_addr;
- uint8 *end_addr;
- } BlockAddrCache;
- static void
- wasm_interp_call_func_bytecode(WASMModuleInstance *module,
- WASMExecEnv *exec_env,
- WASMFunctionInstance *cur_func,
- WASMInterpFrame *prev_frame)
- {
- WASMMemoryInstance *memory = module->default_memory;
- uint32 memory_data_size = memory
- ? memory->num_bytes_per_page * memory->cur_page_count : 0;
- uint32 heap_base_offset = memory ? (uint32)memory->heap_base_offset : 0;
- uint32 heap_data_size = memory
- ? (uint32)(memory->heap_data_end - memory->heap_data) : 0;
- WASMTableInstance *table = module->default_table;
- WASMGlobalInstance *globals = module->globals;
- uint8 *global_data = memory ? memory->global_data : NULL;
- uint8 opcode_IMPDEP2 = WASM_OP_IMPDEP2;
- WASMInterpFrame *frame = NULL;
- /* Points to this special opcode so as to jump to the
- call_method_from_entry. */
- register uint8 *frame_ip = &opcode_IMPDEP2; /* cache of frame->ip */
- register uint32 *frame_lp = NULL; /* cache of frame->lp */
- register uint32 *frame_sp = NULL; /* cache of frame->sp */
- WASMBranchBlock *frame_csp = NULL;
- WASMGlobalInstance *global;
- uint8 *frame_ip_end = frame_ip + 1, *frame_ip_org;
- uint8 opcode, block_ret_type;
- uint32 *depths = NULL;
- uint32 depth_buf[BR_TABLE_TMP_BUF_LEN];
- uint32 i, depth, cond, count, fidx, tidx, frame_size = 0;
- uint64 all_cell_num = 0;
- int32 didx, val;
- uint8 *else_addr, *end_addr, *maddr = NULL;
- uint32 local_idx, local_offset, global_idx, global_data_offset;
- uint8 local_type, *global_addr;
- BlockAddrCache block_addr_cache[32] = { 0 };
- uint32 cache_index, block_addr_cache_size = 32;
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- #define HANDLE_OPCODE(op) &&HANDLE_##op
- DEFINE_GOTO_TABLE (handle_table);
- #undef HANDLE_OPCODE
- #endif
- /* Size of memory load.
- This starts with the first memory load operator at opcode 0x28 */
- uint32 LOAD_SIZE[] = {
- 4, 8, 4, 8, 1, 1, 2, 2, 1, 1, 2, 2, 4, 4, /* loads */
- 4, 8, 4, 8, 1, 2, 1, 2, 4 }; /* stores */
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- while (frame_ip < frame_ip_end) {
- opcode = *frame_ip++;
- switch (opcode) {
- #else
- FETCH_OPCODE_AND_DISPATCH ();
- #endif
- /* control instructions */
- HANDLE_OP (WASM_OP_UNREACHABLE):
- wasm_set_exception(module, "unreachable");
- goto got_exception;
- HANDLE_OP (WASM_OP_NOP):
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_BLOCK):
- block_ret_type = *frame_ip++;
- cache_index = ((uintptr_t)frame_ip) % block_addr_cache_size;
- if (block_addr_cache[cache_index].frame_ip == frame_ip) {
- end_addr = block_addr_cache[cache_index].end_addr;
- }
- else {
- if (!wasm_loader_find_block_addr(module->module,
- frame_ip, frame_ip_end,
- BLOCK_TYPE_BLOCK,
- &else_addr, &end_addr,
- NULL, 0)) {
- wasm_set_exception(module, "find block address failed");
- goto got_exception;
- }
- block_addr_cache[cache_index].frame_ip = frame_ip;
- block_addr_cache[cache_index].end_addr = end_addr;
- }
- PUSH_CSP(BLOCK_TYPE_BLOCK, block_ret_type, frame_ip, NULL, end_addr);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_LOOP):
- block_ret_type = *frame_ip++;
- cache_index = ((uintptr_t)frame_ip) % block_addr_cache_size;
- if (block_addr_cache[cache_index].frame_ip == frame_ip) {
- end_addr = block_addr_cache[cache_index].end_addr;
- }
- else {
- if (!wasm_loader_find_block_addr(module->module,
- frame_ip, frame_ip_end,
- BLOCK_TYPE_LOOP,
- &else_addr, &end_addr,
- NULL, 0)) {
- wasm_set_exception(module, "find block address failed");
- goto got_exception;
- }
- block_addr_cache[cache_index].frame_ip = frame_ip;
- block_addr_cache[cache_index].end_addr = end_addr;
- }
- PUSH_CSP(BLOCK_TYPE_LOOP, block_ret_type, frame_ip, NULL, end_addr);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_IF):
- block_ret_type = *frame_ip++;
- cache_index = ((uintptr_t)frame_ip) % block_addr_cache_size;
- if (block_addr_cache[cache_index].frame_ip == frame_ip) {
- else_addr = block_addr_cache[cache_index].else_addr;
- end_addr = block_addr_cache[cache_index].end_addr;
- }
- else {
- if (!wasm_loader_find_block_addr(module->module,
- frame_ip, frame_ip_end,
- BLOCK_TYPE_IF,
- &else_addr, &end_addr,
- NULL, 0)) {
- wasm_set_exception(module, "find block address failed");
- goto got_exception;
- }
- block_addr_cache[cache_index].frame_ip = frame_ip;
- block_addr_cache[cache_index].else_addr = else_addr;
- block_addr_cache[cache_index].end_addr = end_addr;
- }
- cond = (uint32)POP_I32();
- PUSH_CSP(BLOCK_TYPE_IF, block_ret_type, frame_ip, else_addr, end_addr);
- /* condition of the if branch is false, else condition is met */
- if (cond == 0) {
- /* if there is no else branch, go to the end addr */
- if (else_addr == NULL) {
- POP_CSP();
- frame_ip = end_addr + 1;
- }
- /* if there is an else branch, go to the else addr */
- else
- frame_ip = else_addr + 1;
- }
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_ELSE):
- /* comes from the if branch in WASM_OP_IF */
- frame_ip = (frame_csp - 1)->end_addr;
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_END):
- if (frame_csp > frame->csp_bottom + 1) {
- POP_CSP();
- }
- else { /* end of function, treat as WASM_OP_RETURN */
- frame_sp -= cur_func->ret_cell_num;
- for (i = 0; i < cur_func->ret_cell_num; i++) {
- *prev_frame->sp++ = frame_sp[i];
- }
- goto return_func;
- }
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_BR):
- read_leb_uint32(frame_ip, frame_ip_end, depth);
- POP_CSP_N(depth);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_BR_IF):
- read_leb_uint32(frame_ip, frame_ip_end, depth);
- cond = (uint32)POP_I32();
- if (cond)
- POP_CSP_N(depth);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_BR_TABLE):
- read_leb_uint32(frame_ip, frame_ip_end, count);
- if (count <= BR_TABLE_TMP_BUF_LEN)
- depths = depth_buf;
- else {
- uint64 total_size = sizeof(uint32) * (uint64)count;
- if (total_size >= UINT32_MAX
- || !(depths = wasm_malloc((uint32)total_size))) {
- wasm_set_exception(module,
- "WASM interp failed: allocate memory failed.");
- goto got_exception;
- }
- }
- for (i = 0; i < count; i++) {
- read_leb_uint32(frame_ip, frame_ip_end, depths[i]);
- }
- read_leb_uint32(frame_ip, frame_ip_end, depth);
- didx = POP_I32();
- if (didx >= 0 && (uint32)didx < count) {
- depth = depths[didx];
- }
- if (depths != depth_buf) {
- wasm_free(depths);
- depths = NULL;
- }
- POP_CSP_N(depth);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_RETURN):
- frame_sp -= cur_func->ret_cell_num;
- for (i = 0; i < cur_func->ret_cell_num; i++) {
- *prev_frame->sp++ = frame_sp[i];
- }
- goto return_func;
- HANDLE_OP (WASM_OP_CALL):
- read_leb_uint32(frame_ip, frame_ip_end, fidx);
- bh_assert(fidx < module->function_count);
- cur_func = module->functions + fidx;
- goto call_func_from_interp;
- HANDLE_OP (WASM_OP_CALL_INDIRECT):
- {
- WASMType *cur_type, *cur_func_type;
- read_leb_uint32(frame_ip, frame_ip_end, tidx);
- if (tidx >= module->module->type_count) {
- wasm_set_exception(module, "type index is overflow");
- goto got_exception;
- }
- cur_type = module->module->types[tidx];
- /* to skip 0x00 here */
- frame_ip++;
- val = POP_I32();
- if (val < 0 || val >= (int32)table->cur_size) {
- wasm_set_exception(module, "undefined element");
- goto got_exception;
- }
- fidx = ((uint32*)table->base_addr)[val];
- if (fidx == (uint32)-1) {
- wasm_set_exception(module, "uninitialized element");
- goto got_exception;
- }
- cur_func = module->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 (!wasm_type_equal(cur_type, cur_func_type)) {
- wasm_set_exception(module, "indirect call type mismatch");
- goto got_exception;
- }
- goto call_func_from_interp;
- }
- /* parametric instructions */
- HANDLE_OP (WASM_OP_DROP):
- {
- wasm_set_exception(module, "WASM interp failed: unsupported opcode.");
- goto got_exception;
- }
- HANDLE_OP (WASM_OP_DROP_32):
- {
- frame_sp--;
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_DROP_64):
- {
- frame_sp -= 2;
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_SELECT):
- {
- wasm_set_exception(module, "WASM interp failed: unsupported opcode.");
- goto got_exception;
- }
- HANDLE_OP (WASM_OP_SELECT_32):
- {
- cond = (uint32)POP_I32();
- frame_sp--;
- if (!cond)
- *(frame_sp - 1) = *frame_sp;
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_SELECT_64):
- {
- cond = (uint32)POP_I32();
- frame_sp -= 2;
- if (!cond) {
- *(frame_sp - 2) = *frame_sp;
- *(frame_sp - 1) = *(frame_sp + 1);
- }
- HANDLE_OP_END ();
- }
- /* variable instructions */
- HANDLE_OP (WASM_OP_GET_LOCAL):
- {
- frame_ip_org = frame_ip - 1;
- GET_LOCAL_INDEX_TYPE_AND_OFFSET();
- switch (local_type) {
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- PUSH_I32(*(int32*)(frame_lp + local_offset));
- break;
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- PUSH_I64(GET_I64_FROM_ADDR(frame_lp + local_offset));
- break;
- default:
- wasm_set_exception(module, "invalid local type");
- goto got_exception;
- }
- if (local_offset < 0x80) {
- *frame_ip_org++ = WASM_OP_GET_LOCAL_FAST;
- if (local_type == VALUE_TYPE_I32
- || local_type == VALUE_TYPE_F32)
- *frame_ip_org++ = (uint8)local_offset;
- else
- *frame_ip_org++ = (uint8)(local_offset | 0x80);
- while (frame_ip_org < frame_ip)
- *frame_ip_org++ = WASM_OP_NOP;
- }
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_GET_LOCAL_FAST):
- {
- local_offset = *frame_ip++;
- if (local_offset & 0x80)
- PUSH_I64(GET_I64_FROM_ADDR(frame_lp + (local_offset & 0x7F)));
- else
- PUSH_I32(*(int32*)(frame_lp + local_offset));
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_SET_LOCAL):
- {
- frame_ip_org = frame_ip - 1;
- GET_LOCAL_INDEX_TYPE_AND_OFFSET();
- switch (local_type) {
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- *(int32*)(frame_lp + local_offset) = POP_I32();
- break;
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- PUT_I64_TO_ADDR((uint32*)(frame_lp + local_offset), POP_I64());
- break;
- default:
- wasm_set_exception(module, "invalid local type");
- goto got_exception;
- }
- if (local_offset < 0x80) {
- *frame_ip_org++ = WASM_OP_SET_LOCAL_FAST;
- if (local_type == VALUE_TYPE_I32
- || local_type == VALUE_TYPE_F32)
- *frame_ip_org++ = (uint8)local_offset;
- else
- *frame_ip_org++ = (uint8)(local_offset | 0x80);
- while (frame_ip_org < frame_ip)
- *frame_ip_org++ = WASM_OP_NOP;
- }
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_SET_LOCAL_FAST):
- {
- local_offset = *frame_ip++;
- if (local_offset & 0x80)
- PUT_I64_TO_ADDR((uint32*)(frame_lp + (local_offset & 0x7F)), POP_I64());
- else
- *(int32*)(frame_lp + local_offset) = POP_I32();
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_TEE_LOCAL):
- {
- frame_ip_org = frame_ip - 1;
- GET_LOCAL_INDEX_TYPE_AND_OFFSET();
- switch (local_type) {
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- *(int32*)(frame_lp + local_offset) = *(int32*)(frame_sp - 1);
- break;
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- PUT_I64_TO_ADDR((uint32*)(frame_lp + local_offset),
- GET_I64_FROM_ADDR(frame_sp - 2));
- break;
- default:
- wasm_set_exception(module, "invalid local type");
- goto got_exception;
- }
- if (local_offset < 0x80) {
- *frame_ip_org++ = WASM_OP_TEE_LOCAL_FAST;
- if (local_type == VALUE_TYPE_I32
- || local_type == VALUE_TYPE_F32)
- *frame_ip_org++ = (uint8)local_offset;
- else
- *frame_ip_org++ = (uint8)(local_offset | 0x80);
- while (frame_ip_org < frame_ip)
- *frame_ip_org++ = WASM_OP_NOP;
- }
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_TEE_LOCAL_FAST):
- {
- local_offset = *frame_ip++;
- if (local_offset & 0x80)
- PUT_I64_TO_ADDR((uint32*)(frame_lp + (local_offset & 0x7F)),
- GET_I64_FROM_ADDR(frame_sp - 2));
- else
- *(int32*)(frame_lp + local_offset) = *(int32*)(frame_sp - 1);
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_GET_GLOBAL):
- {
- frame_ip_org = frame_ip - 1;
- read_leb_uint32(frame_ip, frame_ip_end, global_idx);
- bh_assert(global_idx < module->global_count);
- global = globals + global_idx;
- global_addr = global_data + global->data_offset;
- switch (global->type) {
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- PUSH_I32(*(uint32*)global_addr);
- break;
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- PUSH_I64(GET_I64_FROM_ADDR((uint32*)global_addr));
- break;
- default:
- wasm_set_exception(module, "invalid global type");
- goto got_exception;
- }
- if (global->data_offset < 0x80) {
- *frame_ip_org++ = WASM_OP_GET_GLOBAL_FAST;
- if (global->type == VALUE_TYPE_I32
- || global->type == VALUE_TYPE_F32)
- *frame_ip_org++ = (uint8)global->data_offset;
- else
- *frame_ip_org++ = (uint8)(global->data_offset | 0x80);
- while (frame_ip_org < frame_ip)
- *frame_ip_org++ = WASM_OP_NOP;
- }
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_GET_GLOBAL_FAST):
- {
- global_data_offset = *frame_ip++;
- if (global_data_offset & 0x80)
- PUSH_I64(GET_I64_FROM_ADDR((uint32*)(global_data + (global_data_offset & 0x7F))));
- else
- PUSH_I32(*(uint32*)(global_data + global_data_offset));
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_SET_GLOBAL):
- {
- frame_ip_org = frame_ip - 1;
- read_leb_uint32(frame_ip, frame_ip_end, global_idx);
- bh_assert(global_idx < module->global_count);
- global = globals + global_idx;
- global_addr = global_data + global->data_offset;
- switch (global->type) {
- case VALUE_TYPE_I32:
- case VALUE_TYPE_F32:
- *(int32*)global_addr = POP_I32();
- break;
- case VALUE_TYPE_I64:
- case VALUE_TYPE_F64:
- PUT_I64_TO_ADDR((uint32*)global_addr, POP_I64());
- break;
- default:
- wasm_set_exception(module, "invalid global type");
- goto got_exception;
- }
- if (global->data_offset < 0x80) {
- *frame_ip_org++ = WASM_OP_SET_GLOBAL_FAST;
- if (global->type == VALUE_TYPE_I32
- || global->type == VALUE_TYPE_F32)
- *frame_ip_org++ = (uint8)global->data_offset;
- else
- *frame_ip_org++ = (uint8)(global->data_offset | 0x80);
- while (frame_ip_org < frame_ip)
- *frame_ip_org++ = WASM_OP_NOP;
- }
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_SET_GLOBAL_FAST):
- {
- global_data_offset = *frame_ip++;
- if (global_data_offset & 0x80)
- PUT_I64_TO_ADDR((uint32*)(global_data + (global_data_offset & 0x7F)),
- POP_I64());
- else
- *(uint32*)(global_data + global_data_offset) = POP_I32();
- HANDLE_OP_END ();
- }
- /* memory load instructions */
- HANDLE_OP (WASM_OP_I32_LOAD):
- HANDLE_OP (WASM_OP_I64_LOAD):
- HANDLE_OP (WASM_OP_F32_LOAD):
- HANDLE_OP (WASM_OP_F64_LOAD):
- HANDLE_OP (WASM_OP_I32_LOAD8_S):
- HANDLE_OP (WASM_OP_I32_LOAD8_U):
- HANDLE_OP (WASM_OP_I32_LOAD16_S):
- HANDLE_OP (WASM_OP_I32_LOAD16_U):
- HANDLE_OP (WASM_OP_I64_LOAD8_S):
- HANDLE_OP (WASM_OP_I64_LOAD8_U):
- HANDLE_OP (WASM_OP_I64_LOAD16_S):
- HANDLE_OP (WASM_OP_I64_LOAD16_U):
- HANDLE_OP (WASM_OP_I64_LOAD32_S):
- HANDLE_OP (WASM_OP_I64_LOAD32_U):
- {
- uint32 offset, flags, addr;
- GET_OPCODE();
- read_leb_uint32(frame_ip, frame_ip_end, flags);
- read_leb_uint32(frame_ip, frame_ip_end, offset);
- addr = (uint32)POP_I32();
- CHECK_MEMORY_OVERFLOW();
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- static const void *handle_load_table[] = {
- &&HANDLE_LOAD_WASM_OP_I32_LOAD,
- &&HANDLE_LOAD_WASM_OP_I64_LOAD,
- &&HANDLE_LOAD_WASM_OP_F32_LOAD,
- &&HANDLE_LOAD_WASM_OP_F64_LOAD,
- &&HANDLE_LOAD_WASM_OP_I32_LOAD8_S,
- &&HANDLE_LOAD_WASM_OP_I32_LOAD8_U,
- &&HANDLE_LOAD_WASM_OP_I32_LOAD16_S,
- &&HANDLE_LOAD_WASM_OP_I32_LOAD16_U,
- &&HANDLE_LOAD_WASM_OP_I64_LOAD8_S,
- &&HANDLE_LOAD_WASM_OP_I64_LOAD8_U,
- &&HANDLE_LOAD_WASM_OP_I64_LOAD16_S,
- &&HANDLE_LOAD_WASM_OP_I64_LOAD16_U,
- &&HANDLE_LOAD_WASM_OP_I64_LOAD32_S,
- &&HANDLE_LOAD_WASM_OP_I64_LOAD32_U
- };
- #define HANDLE_OP_LOAD(opcode) HANDLE_LOAD_##opcode
- goto *handle_load_table[opcode - WASM_OP_I32_LOAD];
- #else
- #define HANDLE_OP_LOAD(opcode) case opcode
- switch (opcode)
- #endif
- {
- HANDLE_OP_LOAD(WASM_OP_I32_LOAD):
- PUSH_I32(LOAD_I32(maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I64_LOAD):
- PUSH_I64(LOAD_I64(maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_F32_LOAD):
- PUSH_I32(LOAD_I32(maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_F64_LOAD):
- PUSH_F64(LOAD_F64(maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_S):
- PUSH_I32(sign_ext_8_32(*(int8*)maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I32_LOAD8_U):
- PUSH_I32((uint32)(*(uint8*)maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_S):
- PUSH_I32(sign_ext_16_32(LOAD_I16(maddr)));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I32_LOAD16_U):
- PUSH_I32((uint32)(LOAD_U16(maddr)));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_S):
- PUSH_I64(sign_ext_8_64(*(int8*)maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I64_LOAD8_U):
- PUSH_I64((uint64)(*(uint8*)maddr));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_S):
- PUSH_I64(sign_ext_16_64(LOAD_I16(maddr)));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I64_LOAD16_U):
- PUSH_I64((uint64)(LOAD_U16(maddr)));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_S):
- PUSH_I64(sign_ext_32_64(LOAD_I32(maddr)));
- HANDLE_OP_END();
- HANDLE_OP_LOAD(WASM_OP_I64_LOAD32_U):
- PUSH_I64((uint64)(LOAD_U32(maddr)));
- HANDLE_OP_END();
- }
- (void)flags;
- HANDLE_OP_END ();
- }
- /* memory store instructions */
- HANDLE_OP (WASM_OP_F32_STORE):
- {
- uint32 offset, flags, addr;
- GET_OPCODE();
- read_leb_uint32(frame_ip, frame_ip_end, flags);
- read_leb_uint32(frame_ip, frame_ip_end, offset);
- frame_sp--;
- addr = (uint32)POP_I32();
- CHECK_MEMORY_OVERFLOW();
- STORE_U32(maddr, frame_sp[1]);
- (void)flags;
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_F64_STORE):
- {
- uint32 offset, flags, addr;
- GET_OPCODE();
- read_leb_uint32(frame_ip, frame_ip_end, flags);
- read_leb_uint32(frame_ip, frame_ip_end, offset);
- frame_sp -= 2;
- addr = (uint32)POP_I32();
- CHECK_MEMORY_OVERFLOW();
- STORE_U32(maddr, frame_sp[1]);
- STORE_U32(maddr + 4, frame_sp[2]);
- (void)flags;
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_STORE):
- HANDLE_OP (WASM_OP_I32_STORE8):
- HANDLE_OP (WASM_OP_I32_STORE16):
- {
- uint32 offset, flags, addr;
- uint32 sval;
- GET_OPCODE();
- read_leb_uint32(frame_ip, frame_ip_end, flags);
- read_leb_uint32(frame_ip, frame_ip_end, offset);
- sval = (uint32)POP_I32();
- addr = (uint32)POP_I32();
- CHECK_MEMORY_OVERFLOW();
- switch (opcode) {
- case WASM_OP_I32_STORE:
- STORE_U32(maddr, sval);
- break;
- case WASM_OP_I32_STORE8:
- *(uint8*)maddr = (uint8)sval;
- break;
- case WASM_OP_I32_STORE16:
- STORE_U16(maddr, (uint16)sval);
- break;
- }
- (void)flags;
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_STORE):
- HANDLE_OP (WASM_OP_I64_STORE8):
- HANDLE_OP (WASM_OP_I64_STORE16):
- HANDLE_OP (WASM_OP_I64_STORE32):
- {
- uint32 offset, flags, addr;
- uint64 sval;
- GET_OPCODE();
- read_leb_uint32(frame_ip, frame_ip_end, flags);
- read_leb_uint32(frame_ip, frame_ip_end, offset);
- sval = (uint64)POP_I64();
- addr = (uint32)POP_I32();
- CHECK_MEMORY_OVERFLOW();
- switch (opcode) {
- case WASM_OP_I64_STORE:
- STORE_I64(maddr, sval);
- break;
- case WASM_OP_I64_STORE8:
- *(uint8*)maddr = (uint8)sval;
- break;
- case WASM_OP_I64_STORE16:
- STORE_U16(maddr, (uint16)sval);
- break;
- case WASM_OP_I64_STORE32:
- STORE_U32(maddr, (uint32)sval);
- break;
- }
- (void)flags;
- HANDLE_OP_END ();
- }
- /* memory size and memory grow instructions */
- HANDLE_OP (WASM_OP_MEMORY_SIZE):
- {
- uint32 reserved;
- read_leb_uint32(frame_ip, frame_ip_end, reserved);
- PUSH_I32(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;
- read_leb_uint32(frame_ip, frame_ip_end, reserved);
- delta = (uint32)POP_I32();
- if (!wasm_enlarge_memory(module, delta)) {
- /* fail to memory.grow, return -1 */
- PUSH_I32(-1);
- if (wasm_get_exception(module)) {
- bh_printf("%s\n", wasm_get_exception(module));
- wasm_set_exception(module, NULL);
- }
- }
- else {
- /* success, return previous page count */
- PUSH_I32(prev_page_count);
- /* update the memory instance ptr */
- memory = module->default_memory;
- memory_data_size = memory->num_bytes_per_page * memory->cur_page_count;
- global_data = memory->global_data;
- }
- (void)reserved;
- HANDLE_OP_END ();
- }
- /* constant instructions */
- HANDLE_OP (WASM_OP_I32_CONST):
- DEF_OP_I_CONST(int32, I32);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_I64_CONST):
- DEF_OP_I_CONST(int64, I64);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_F32_CONST):
- {
- uint8 *p_float = (uint8*)frame_sp++;
- for (i = 0; i < sizeof(float32); i++)
- *p_float++ = *frame_ip++;
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_F64_CONST):
- {
- uint8 *p_float = (uint8*)frame_sp++;
- frame_sp++;
- for (i = 0; i < sizeof(float64); i++)
- *p_float++ = *frame_ip++;
- HANDLE_OP_END ();
- }
- /* comparison instructions of i32 */
- HANDLE_OP (WASM_OP_I32_EQZ):
- DEF_OP_EQZ(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(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 = POP_I32();
- a = POP_I32();
- 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;
- }
- PUSH_I32(a / b);
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_DIV_U):
- {
- uint32 a, b;
- b = (uint32)POP_I32();
- a = (uint32)POP_I32();
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUSH_I32(a / b);
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_REM_S):
- {
- int32 a, b;
- b = POP_I32();
- a = POP_I32();
- if (a == (int32)0x80000000 && b == -1) {
- PUSH_I32(0);
- HANDLE_OP_END ();
- }
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUSH_I32(a % b);
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_REM_U):
- {
- uint32 a, b;
- b = (uint32)POP_I32();
- a = (uint32)POP_I32();
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUSH_I32(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):
- {
- #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
- DEF_OP_NUMERIC(uint32, uint32, I32, <<);
- #else
- DEF_OP_NUMERIC2(uint32, uint32, I32, <<);
- #endif
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_SHR_S):
- {
- #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
- DEF_OP_NUMERIC(int32, uint32, I32, >>);
- #else
- DEF_OP_NUMERIC2(int32, uint32, I32, >>);
- #endif
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_SHR_U):
- {
- #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
- DEF_OP_NUMERIC(uint32, uint32, I32, >>);
- #else
- DEF_OP_NUMERIC2(uint32, uint32, I32, >>);
- #endif
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_ROTL):
- {
- uint32 a, b;
- b = (uint32)POP_I32();
- a = (uint32)POP_I32();
- PUSH_I32(rotl32(a, b));
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I32_ROTR):
- {
- uint32 a, b;
- b = (uint32)POP_I32();
- a = (uint32)POP_I32();
- PUSH_I32(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 = POP_I64();
- a = POP_I64();
- 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;
- }
- PUSH_I64(a / b);
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_DIV_U):
- {
- uint64 a, b;
- b = (uint64)POP_I64();
- a = (uint64)POP_I64();
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUSH_I64(a / b);
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_REM_S):
- {
- int64 a, b;
- b = POP_I64();
- a = POP_I64();
- if (a == (int64)0x8000000000000000LL && b == -1) {
- PUSH_I64(0);
- HANDLE_OP_END ();
- }
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUSH_I64(a % b);
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_REM_U):
- {
- uint64 a, b;
- b = (uint64)POP_I64();
- a = (uint64)POP_I64();
- if (b == 0) {
- wasm_set_exception(module, "integer divide by zero");
- goto got_exception;
- }
- PUSH_I64(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):
- {
- #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
- DEF_OP_NUMERIC_64(uint64, uint64, I64, <<);
- #else
- DEF_OP_NUMERIC2_64(uint64, uint64, I64, <<);
- #endif
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_SHR_S):
- {
- #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
- DEF_OP_NUMERIC_64(int64, uint64, I64, >>);
- #else
- DEF_OP_NUMERIC2_64(int64, uint64, I64, >>);
- #endif
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_SHR_U):
- {
- #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_X86_32)
- DEF_OP_NUMERIC_64(uint64, uint64, I64, >>);
- #else
- DEF_OP_NUMERIC2_64(uint64, uint64, I64, >>);
- #endif
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_ROTL):
- {
- uint64 a, b;
- b = (uint64)POP_I64();
- a = (uint64)POP_I64();
- PUSH_I64(rotl64(a, b));
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_I64_ROTR):
- {
- uint64 a, b;
- b = (uint64)POP_I64();
- a = (uint64)POP_I64();
- PUSH_I64(rotr64(a, b));
- HANDLE_OP_END ();
- }
- /* numberic instructions of f32 */
- HANDLE_OP (WASM_OP_F32_ABS):
- DEF_OP_MATH(float32, F32, fabs);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_F32_NEG):
- {
- int32 i32 = (int32)frame_sp[-1];
- int32 sign_bit = i32 & (1 << 31);
- if (sign_bit)
- frame_sp[-1] = i32 & ~(1 << 31);
- else
- frame_sp[-1] = (uint32)(i32 | (1 << 31));
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_F32_CEIL):
- DEF_OP_MATH(float32, F32, ceil);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_F32_FLOOR):
- DEF_OP_MATH(float32, F32, floor);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_F32_TRUNC):
- DEF_OP_MATH(float32, F32, trunc);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_F32_NEAREST):
- DEF_OP_MATH(float32, F32, rint);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_F32_SQRT):
- DEF_OP_MATH(float32, F32, sqrt);
- 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 = POP_F32();
- a = POP_F32();
- if (isnan(a))
- PUSH_F32(a);
- else if (isnan(b))
- PUSH_F32(b);
- else
- PUSH_F32(wa_fmin(a, b));
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_F32_MAX):
- {
- float32 a, b;
- b = POP_F32();
- a = POP_F32();
- if (isnan(a))
- PUSH_F32(a);
- else if (isnan(b))
- PUSH_F32(b);
- else
- PUSH_F32(wa_fmax(a, b));
- HANDLE_OP_END ();
- }
- HANDLE_OP (WASM_OP_F32_COPYSIGN):
- {
- float32 a, b;
- b = POP_F32();
- a = POP_F32();
- PUSH_F32(signbit(b) ? -fabs(a) : fabs(a));
- 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):
- {
- int64 i64 = GET_I64_FROM_ADDR(frame_sp - 2);
- int64 sign_bit = i64 & (((int64)1) << 63);
- if (sign_bit)
- PUT_I64_TO_ADDR(frame_sp - 2, ((uint64)i64 & ~(((uint64)1) << 63)));
- else
- PUT_I64_TO_ADDR(frame_sp - 2, ((uint64)i64 | (((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(signbit(b) ? -fabs(a) : fabs(a));
- 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):
- /* Copy the float32/float64 values from WAVM, need to test more.
- 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(int32, I32, float32, F32, <= -2147483904.0f,
- >= 2147483648.0f);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_I32_TRUNC_U_F32):
- DEF_OP_TRUNC(uint32, I32, float32, F32, <= -1.0f,
- >= 4294967296.0f);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_I32_TRUNC_S_F64):
- DEF_OP_TRUNC(int32, I32, float64, F64, <= -2147483649.0,
- >= 2147483648.0);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_I32_TRUNC_U_F64):
- DEF_OP_TRUNC(uint32, I32, float64, F64, <= -1.0 ,
- >= 4294967296.0);
- 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(int64, I64, float32, F32, <= -9223373136366403584.0f,
- >= 9223372036854775808.0f);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_I64_TRUNC_U_F32):
- DEF_OP_TRUNC(uint64, I64, float32, F32, <= -1.0f,
- >= 18446744073709551616.0f);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_I64_TRUNC_S_F64):
- DEF_OP_TRUNC(int64, I64, float64, F64, <= -9223372036854777856.0,
- >= 9223372036854775808.0);
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_I64_TRUNC_U_F64):
- DEF_OP_TRUNC(uint64, I64, float64, F64, <= -1.0,
- >= 18446744073709551616.0);
- 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_I64_REINTERPRET_F64):
- HANDLE_OP (WASM_OP_F32_REINTERPRET_I32):
- HANDLE_OP (WASM_OP_F64_REINTERPRET_I64):
- HANDLE_OP_END ();
- HANDLE_OP (WASM_OP_IMPDEP2):
- frame = prev_frame;
- frame_ip = frame->ip;
- frame_sp = frame->sp;
- frame_csp = frame->csp;
- goto call_func_from_entry;
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- default:
- wasm_set_exception(module, "WASM interp failed: unsupported opcode.");
- goto got_exception;
- }
- #endif
- #if WASM_ENABLE_LABELS_AS_VALUES != 0
- HANDLE_OP (WASM_OP_IMPDEP1):
- 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):
- HANDLE_OP (WASM_OP_UNUSED_0x12):
- HANDLE_OP (WASM_OP_UNUSED_0x13):
- 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_0x1c):
- HANDLE_OP (WASM_OP_UNUSED_0x1d):
- HANDLE_OP (WASM_OP_UNUSED_0x1e):
- HANDLE_OP (WASM_OP_UNUSED_0x1f):
- HANDLE_OP (WASM_OP_UNUSED_0x25):
- HANDLE_OP (WASM_OP_UNUSED_0x26):
- HANDLE_OP (WASM_OP_UNUSED_0x27):
- {
- wasm_set_exception(module, "WASM interp failed: unsupported opcode.");
- goto got_exception;
- }
- #endif
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- continue;
- #else
- FETCH_OPCODE_AND_DISPATCH ();
- #endif
- 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);
- POP(cur_func->param_cell_num);
- SYNC_ALL_TO_FRAME();
- word_copy(outs_area->lp, frame_sp, cur_func->param_cell_num);
- prev_frame = frame;
- }
- call_func_from_entry:
- {
- if (cur_func->is_import_func) {
- 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();
- memory = module->default_memory;
- if (wasm_get_exception(module))
- goto got_exception;
- }
- else {
- WASMFunction *cur_wasm_func = cur_func->u.func;
- WASMType *func_type;
- uint8 ret_type;
- func_type = cur_wasm_func->func_type;
- all_cell_num = (uint64)cur_func->param_cell_num
- + (uint64)cur_func->local_cell_num
- + (uint64)cur_wasm_func->max_stack_cell_num
- + ((uint64)cur_wasm_func->max_block_num) * sizeof(WASMBranchBlock) / 4;
- if (all_cell_num >= UINT32_MAX) {
- wasm_set_exception(module, "WASM interp failed: 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_sp = frame->sp_bottom = frame_lp + cur_func->param_cell_num
- + cur_func->local_cell_num;
- frame->sp_boundary = frame->sp_bottom + cur_wasm_func->max_stack_cell_num;
- frame_csp = frame->csp_bottom = (WASMBranchBlock*)frame->sp_boundary;
- frame->csp_boundary = frame->csp_bottom + cur_wasm_func->max_block_num;
- /* Initialize the local varialbes */
- memset(frame_lp + cur_func->param_cell_num, 0,
- (uint32)(cur_func->local_cell_num * 4));
- /* Push function block as first block */
- ret_type = func_type->result_count
- ? cur_func->param_types[func_type->param_count]
- : VALUE_TYPE_VOID;
- PUSH_CSP(BLOCK_TYPE_FUNCTION, ret_type,
- frame_ip, NULL, frame_ip_end - 1);
- 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 ();
- }
- out_of_bounds:
- wasm_set_exception(module, "out of bounds memory access");
- got_exception:
- if (depths && depths != depth_buf) {
- wasm_free(depths);
- depths = NULL;
- }
- return;
- #if WASM_ENABLE_LABELS_AS_VALUES == 0
- }
- #else
- FETCH_OPCODE_AND_DISPATCH ();
- #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 = 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 %d, expected %d",
- argc, function->param_cell_num);
- wasm_set_exception(module_inst, buf);
- return;
- }
- /* TODO: check stack overflow. */
- 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->sp = frame->lp + 0;
- if (argc > 0)
- word_copy(outs_area->lp, argv, argc);
- wasm_exec_env_set_cur_frame(exec_env, frame);
- if (function->is_import_func)
- 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->sp + i - function->ret_cell_num);
- }
- wasm_exec_env_set_cur_frame(exec_env, prev_frame);
- FREE_FRAME(exec_env, frame);
- }
|