123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867 |
- /*
- * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/HashTable.h>
- #include <AK/Result.h>
- #include <AK/SourceLocation.h>
- #include <AK/TemporaryChange.h>
- #include <AK/Try.h>
- #include <LibWasm/AbstractMachine/Validator.h>
- #include <LibWasm/Printer/Printer.h>
- namespace Wasm {
- ErrorOr<void, ValidationError> Validator::validate(Module& module)
- {
- ErrorOr<void, ValidationError> result {};
- // Note: The spec performs this after populating the context, but there's no real reason to do so,
- // as this has no dependency.
- HashTable<StringView> seen_export_names;
- module.for_each_section_of_type<ExportSection>([&result, &seen_export_names](ExportSection const& section) {
- if (result.is_error())
- return;
- for (auto& export_ : section.entries()) {
- if (seen_export_names.try_set(export_.name()).release_value_but_fixme_should_propagate_errors() != AK::HashSetResult::InsertedNewEntry)
- result = Errors::duplicate_export_name(export_.name());
- return;
- }
- });
- if (result.is_error()) {
- module.set_validation_status(Module::ValidationStatus::Invalid, {});
- return result;
- }
- m_context = {};
- module.for_each_section_of_type<TypeSection>([this](TypeSection const& section) {
- m_context.types = section.types();
- });
- module.for_each_section_of_type<ImportSection>([&](ImportSection const& section) {
- for (auto& import_ : section.imports()) {
- import_.description().visit(
- [this, &result](TypeIndex const& index) {
- if (m_context.types.size() > index.value())
- m_context.functions.append(m_context.types[index.value()]);
- else
- result = Errors::invalid("TypeIndex"sv);
- m_context.imported_function_count++;
- },
- [this](FunctionType const& type) {
- m_context.functions.append(type);
- m_context.imported_function_count++;
- },
- [this](TableType const& type) { m_context.tables.append(type); },
- [this](MemoryType const& type) { m_context.memories.append(type); },
- [this](GlobalType const& type) {
- m_globals_without_internal_globals.append(type);
- m_context.globals.append(type);
- });
- }
- });
- if (result.is_error()) {
- module.set_validation_status(Module::ValidationStatus::Invalid, {});
- return result;
- }
- module.for_each_section_of_type<FunctionSection>([this, &result](FunctionSection const& section) {
- if (result.is_error())
- return;
- m_context.functions.ensure_capacity(section.types().size() + m_context.functions.size());
- for (auto& index : section.types()) {
- if (m_context.types.size() > index.value()) {
- m_context.functions.append(m_context.types[index.value()]);
- } else {
- result = Errors::invalid("TypeIndex"sv);
- break;
- }
- }
- });
- if (result.is_error()) {
- module.set_validation_status(Module::ValidationStatus::Invalid, {});
- return result;
- }
- module.for_each_section_of_type<TableSection>([this](TableSection const& section) {
- m_context.tables.ensure_capacity(m_context.tables.size() + section.tables().size());
- for (auto& table : section.tables())
- m_context.tables.unchecked_append(table.type());
- });
- module.for_each_section_of_type<MemorySection>([this](MemorySection const& section) {
- m_context.memories.ensure_capacity(m_context.memories.size() + section.memories().size());
- for (auto& memory : section.memories())
- m_context.memories.unchecked_append(memory.type());
- });
- module.for_each_section_of_type<GlobalSection>([this](GlobalSection const& section) {
- m_context.globals.ensure_capacity(m_context.globals.size() + section.entries().size());
- for (auto& global : section.entries())
- m_context.globals.unchecked_append(global.type());
- });
- module.for_each_section_of_type<ElementSection>([this](ElementSection const& section) {
- m_context.elements.ensure_capacity(section.segments().size());
- for (auto& segment : section.segments())
- m_context.elements.unchecked_append(segment.type);
- });
- module.for_each_section_of_type<DataSection>([this](DataSection const& section) {
- m_context.datas.resize(section.data().size());
- });
- // FIXME: C.refs is the set funcidx(module with funcs=ϵ with start=ϵ),
- // i.e., the set of function indices occurring in the module, except in its functions or start function.
- // This is rather weird, it seems to ultimately be checking that `ref.func` uses a specific set of predetermined functions:
- // The only place where this is accessed is in validate_instruction<ref_func>(), but we *populate* this from the ref.func instructions occurring outside regular functions,
- // which limits it to only functions referenced from the elements section.
- // so the only reason for this (as I see) is to ensure that ref.func only hands out references that occur within the elements and global sections
- // _if_ that is indeed the case, then this should be much more specific about where the "valid" references are, and about the actual purpose of this field.
- //
- // For now, we simply assume that we need to scan the aforementioned section initializers for (ref.func f).
- auto scan_expression_for_function_indices = [&](auto& expression) {
- for (auto& instruction : expression.instructions()) {
- if (instruction.opcode() == Instructions::ref_func)
- m_context.references.set(instruction.arguments().template get<FunctionIndex>());
- }
- };
- module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
- for (auto& segment : section.segments()) {
- for (auto& expression : segment.init)
- scan_expression_for_function_indices(expression);
- }
- });
- module.for_each_section_of_type<GlobalSection>([&](GlobalSection const& section) {
- for (auto& segment : section.entries())
- scan_expression_for_function_indices(segment.expression());
- });
- for (auto& section : module.sections()) {
- section.visit([this, &result](auto& section) {
- result = validate(section);
- });
- if (result.is_error()) {
- module.set_validation_status(Module::ValidationStatus::Invalid, {});
- return result;
- }
- }
- module.set_validation_status(Module::ValidationStatus::Valid, {});
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(ImportSection const& section)
- {
- for (auto& import_ : section.imports())
- TRY(import_.description().visit([&](auto& entry) { return validate(entry); }));
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(ExportSection const& section)
- {
- for (auto& export_ : section.entries())
- TRY(export_.description().visit([&](auto& entry) { return validate(entry); }));
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(StartSection const& section)
- {
- TRY(validate(section.function().index()));
- FunctionType const& type = m_context.functions[section.function().index().value()];
- if (!type.parameters().is_empty() || !type.results().is_empty())
- return Errors::invalid("start function signature"sv);
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(DataSection const& section)
- {
- for (auto& entry : section.data()) {
- TRY(entry.value().visit(
- [](DataSection::Data::Passive const&) { return ErrorOr<void, ValidationError> {}; },
- [&](DataSection::Data::Active const& active) -> ErrorOr<void, ValidationError> {
- TRY(validate(active.index));
- auto expression_result = TRY(validate(active.offset, { ValueType(ValueType::I32) }));
- if (!expression_result.is_constant)
- return Errors::invalid("active data initializer"sv);
- if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(ValueType::I32))
- return Errors::invalid("active data initializer type"sv, ValueType(ValueType::I32), expression_result.result_types);
- return {};
- }));
- }
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(ElementSection const& section)
- {
- for (auto& segment : section.segments()) {
- TRY(segment.mode.visit(
- [](ElementSection::Declarative const&) -> ErrorOr<void, ValidationError> { return {}; },
- [](ElementSection::Passive const&) -> ErrorOr<void, ValidationError> { return {}; },
- [&](ElementSection::Active const& active) -> ErrorOr<void, ValidationError> {
- TRY(validate(active.index));
- auto expression_result = TRY(validate(active.expression, { ValueType(ValueType::I32) }));
- if (!expression_result.is_constant)
- return Errors::invalid("active element initializer"sv);
- if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(ValueType::I32))
- return Errors::invalid("active element initializer type"sv, ValueType(ValueType::I32), expression_result.result_types);
- return {};
- }));
- for (auto& expression : segment.init) {
- auto result = TRY(validate(expression, { segment.type }));
- if (!result.is_constant)
- return Errors::invalid("element initializer"sv);
- }
- }
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(GlobalSection const& section)
- {
- TemporaryChange omit_internal_globals { m_context.globals, m_globals_without_internal_globals };
- for (auto& entry : section.entries()) {
- auto& type = entry.type();
- TRY(validate(type));
- auto expression_result = TRY(validate(entry.expression(), { type.type() }));
- if (!expression_result.is_constant)
- return Errors::invalid("global variable initializer"sv);
- if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(type.type().kind()))
- return Errors::invalid("global variable initializer type"sv, ValueType(ValueType::I32), expression_result.result_types);
- }
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(MemorySection const& section)
- {
- for (auto& entry : section.memories())
- TRY(validate(entry.type()));
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(TableSection const& section)
- {
- for (auto& entry : section.tables())
- TRY(validate(entry.type()));
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(CodeSection const& section)
- {
- size_t index = m_context.imported_function_count;
- for (auto& entry : section.functions()) {
- auto function_index = index++;
- TRY(validate(FunctionIndex { function_index }));
- auto& function_type = m_context.functions[function_index];
- auto& function = entry.func();
- auto function_validator = fork();
- function_validator.m_context.locals = {};
- function_validator.m_context.locals.extend(function_type.parameters());
- for (auto& local : function.locals()) {
- for (size_t i = 0; i < local.n(); ++i)
- function_validator.m_context.locals.append(local.type());
- }
- function_validator.m_context.labels = { ResultType { function_type.results() } };
- function_validator.m_context.return_ = ResultType { function_type.results() };
- TRY(function_validator.validate(function.body(), function_type.results()));
- }
- return {};
- }
- ErrorOr<void, ValidationError> Validator::validate(TableType const& type)
- {
- return validate(type.limits(), 32);
- }
- ErrorOr<void, ValidationError> Validator::validate(MemoryType const& type)
- {
- return validate(type.limits(), 16);
- }
- ErrorOr<FunctionType, ValidationError> Validator::validate(BlockType const& type)
- {
- if (type.kind() == BlockType::Index) {
- TRY(validate(type.type_index()));
- return m_context.types[type.type_index().value()];
- }
- if (type.kind() == BlockType::Type) {
- FunctionType function_type { {}, { type.value_type() } };
- TRY(validate(function_type));
- return function_type;
- }
- if (type.kind() == BlockType::Empty)
- return FunctionType { {}, {} };
- return Errors::invalid("BlockType"sv);
- }
- ErrorOr<void, ValidationError> Validator::validate(Limits const& limits, size_t k)
- {
- auto bound = (1ull << k) - 1;
- auto check_bound = [bound](auto value) {
- return static_cast<u64>(value) <= bound;
- };
- if (!check_bound(limits.min()))
- return Errors::out_of_bounds("limit minimum"sv, limits.min(), 0, bound);
- if (limits.max().has_value() && (limits.max().value() < limits.min() || !check_bound(*limits.max())))
- return Errors::out_of_bounds("limit maximum"sv, limits.max().value(), limits.min(), bound);
- return {};
- }
- template<u64 opcode>
- ErrorOr<void, ValidationError> Validator::validate_instruction(Instruction const& instruction, Stack&, bool&)
- {
- return Errors::invalid(ByteString::formatted("instruction opcode (0x{:x}) (missing validation!)", instruction.opcode().value()));
- }
- #define VALIDATE_INSTRUCTION(name) \
- template<> \
- ErrorOr<void, ValidationError> Validator::validate_instruction<Instructions::name.value()>([[maybe_unused]] Instruction const& instruction, [[maybe_unused]] Stack& stack, [[maybe_unused]] bool& is_constant)
- // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-instr-numericmathsfconstc
- VALIDATE_INSTRUCTION(i32_const)
- {
- is_constant = true;
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_const)
- {
- is_constant = true;
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_const)
- {
- is_constant = true;
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_const)
- {
- is_constant = true;
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-unopmathitunop
- VALIDATE_INSTRUCTION(i32_clz)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
- return {};
- }
- VALIDATE_INSTRUCTION(i32_ctz)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
- return {};
- }
- VALIDATE_INSTRUCTION(i32_popcnt)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
- return {};
- }
- VALIDATE_INSTRUCTION(i64_clz)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
- return {};
- }
- VALIDATE_INSTRUCTION(i64_ctz)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
- return {};
- }
- VALIDATE_INSTRUCTION(i64_popcnt)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
- return {};
- }
- VALIDATE_INSTRUCTION(f32_abs)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f32_neg)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f32_sqrt)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f32_ceil)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f32_floor)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f32_trunc)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f32_nearest)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f64_abs)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
- return {};
- }
- VALIDATE_INSTRUCTION(f64_neg)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
- return {};
- }
- VALIDATE_INSTRUCTION(f64_sqrt)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
- return {};
- }
- VALIDATE_INSTRUCTION(f64_ceil)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
- return {};
- }
- VALIDATE_INSTRUCTION(f64_floor)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
- return {};
- }
- VALIDATE_INSTRUCTION(f64_trunc)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
- return {};
- }
- VALIDATE_INSTRUCTION(f64_nearest)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
- return {};
- }
- VALIDATE_INSTRUCTION(i32_extend16_s)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
- return {};
- }
- VALIDATE_INSTRUCTION(i32_extend8_s)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
- return {};
- }
- VALIDATE_INSTRUCTION(i64_extend32_s)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
- return {};
- }
- VALIDATE_INSTRUCTION(i64_extend16_s)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
- return {};
- }
- VALIDATE_INSTRUCTION(i64_extend8_s)
- {
- if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-binopmathitbinop
- VALIDATE_INSTRUCTION(i32_add)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_sub)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_mul)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_divs)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_divu)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_rems)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_remu)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_and)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_or)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_xor)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_shl)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_shrs)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_shru)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_rotl)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_rotr)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_add)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_sub)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_mul)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_divs)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_divu)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_rems)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_remu)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_and)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_or)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_xor)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_shl)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_shrs)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_shru)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_rotl)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_rotr)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_add)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_sub)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_mul)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_div)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_min)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_max)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_copysign)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_add)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_sub)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_mul)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_div)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_min)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_max)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_copysign)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-testopmathittestop
- VALIDATE_INSTRUCTION(i32_eqz)
- {
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_eqz)
- {
- TRY((stack.take<ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-relopmathitrelop
- VALIDATE_INSTRUCTION(i32_eq)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_ne)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_lts)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_ltu)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_gts)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_gtu)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_les)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_leu)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_ges)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_geu)
- {
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_eq)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_ne)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_lts)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_ltu)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_gts)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_gtu)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_les)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_leu)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_ges)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_geu)
- {
- TRY((stack.take<ValueType::I64, ValueType::I64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_eq)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_ne)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_lt)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_le)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_gt)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_ge)
- {
- TRY((stack.take<ValueType::F32, ValueType::F32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_eq)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_ne)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_lt)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_le)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_gt)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_ge)
- {
- TRY((stack.take<ValueType::F64, ValueType::F64>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#-t_2mathsfhrefsyntax-cvtopmathitcvtopmathsf_t_1mathsf_hrefsyntax-sxmathitsx
- VALIDATE_INSTRUCTION(i32_wrap_i64)
- {
- TRY(stack.take<ValueType::I64>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_extend_si32)
- {
- TRY(stack.take<ValueType::I32>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_extend_ui32)
- {
- TRY(stack.take<ValueType::I32>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_sf32)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_uf32)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_sf64)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_uf64)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_sf32)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_uf32)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_sf64)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_uf64)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_sat_f32_s)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_sat_f32_u)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_sat_f64_s)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_trunc_sat_f64_u)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_sat_f32_s)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_sat_f32_u)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_sat_f64_s)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_trunc_sat_f64_u)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_convert_si32)
- {
- TRY(stack.take<ValueType::I32>());
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_convert_ui32)
- {
- TRY(stack.take<ValueType::I32>());
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_convert_si64)
- {
- TRY(stack.take<ValueType::I64>());
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_convert_ui64)
- {
- TRY(stack.take<ValueType::I64>());
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_convert_si32)
- {
- TRY(stack.take<ValueType::I32>());
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_convert_ui32)
- {
- TRY(stack.take<ValueType::I32>());
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_convert_si64)
- {
- TRY(stack.take<ValueType::I64>());
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_convert_ui64)
- {
- TRY(stack.take<ValueType::I64>());
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_demote_f64)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_promote_f32)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_reinterpret_i32)
- {
- TRY(stack.take<ValueType::I32>());
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_reinterpret_i64)
- {
- TRY(stack.take<ValueType::I64>());
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_reinterpret_f32)
- {
- TRY(stack.take<ValueType::F32>());
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_reinterpret_f64)
- {
- TRY(stack.take<ValueType::F64>());
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#reference-instructions%E2%91%A2
- VALIDATE_INSTRUCTION(ref_null)
- {
- is_constant = true;
- stack.append(instruction.arguments().get<ValueType>());
- return {};
- }
- VALIDATE_INSTRUCTION(ref_is_null)
- {
- if (stack.is_empty() || !stack.last().is_reference())
- return Errors::invalid_stack_state(stack, Tuple { "reference" });
- stack.take_last();
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(ref_func)
- {
- auto index = instruction.arguments().get<FunctionIndex>();
- TRY(validate(index));
- if (!m_context.references.contains(index))
- return Errors::invalid("function reference"sv);
- is_constant = true;
- stack.append(ValueType(ValueType::FunctionReference));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#parametric-instructions%E2%91%A2
- VALIDATE_INSTRUCTION(drop)
- {
- if (stack.is_empty())
- return Errors::invalid_stack_state(stack, Tuple { "any" });
- stack.take_last();
- return {};
- }
- VALIDATE_INSTRUCTION(select)
- {
- if (stack.size() < 3)
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32), "any", "any" });
- auto index_type = stack.take_last();
- auto arg0_type = stack.take_last();
- auto& arg1_type = stack.last();
- if (!index_type.is_of_kind(ValueType::I32))
- return Errors::invalid("select index type"sv, ValueType(ValueType::I32), index_type);
- if (arg0_type != arg1_type)
- return Errors::invalid("select argument types"sv, Vector { arg0_type, arg0_type }, Vector { arg0_type, arg1_type });
- return {};
- }
- VALIDATE_INSTRUCTION(select_typed)
- {
- auto& required_types = instruction.arguments().get<Vector<ValueType>>();
- if (required_types.size() != 1)
- return Errors::invalid("select types"sv, "exactly one type"sv, required_types);
- if (stack.size() < 3)
- return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32), required_types.first(), required_types.first() });
- auto index_type = stack.take_last();
- auto arg0_type = stack.take_last();
- auto& arg1_type = stack.last();
- if (!index_type.is_of_kind(ValueType::I32))
- return Errors::invalid("select index type"sv, ValueType(ValueType::I32), index_type);
- if (arg0_type != arg1_type || arg0_type != required_types.first())
- return Errors::invalid("select argument types"sv, Vector { required_types.first(), required_types.first() }, Vector { arg0_type, arg1_type });
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#variable-instructions%E2%91%A2
- VALIDATE_INSTRUCTION(local_get)
- {
- auto index = instruction.arguments().get<LocalIndex>();
- TRY(validate(index));
- stack.append(m_context.locals[index.value()]);
- return {};
- }
- VALIDATE_INSTRUCTION(local_set)
- {
- auto index = instruction.arguments().get<LocalIndex>();
- TRY(validate(index));
- auto& value_type = m_context.locals[index.value()];
- TRY(stack.take(value_type));
- return {};
- }
- VALIDATE_INSTRUCTION(local_tee)
- {
- auto index = instruction.arguments().get<LocalIndex>();
- TRY(validate(index));
- auto& value_type = m_context.locals[index.value()];
- TRY(stack.take(value_type));
- stack.append(value_type);
- return {};
- }
- VALIDATE_INSTRUCTION(global_get)
- {
- auto index = instruction.arguments().get<GlobalIndex>();
- TRY(validate(index));
- auto& global = m_context.globals[index.value()];
- is_constant = !global.is_mutable();
- stack.append(global.type());
- return {};
- }
- VALIDATE_INSTRUCTION(global_set)
- {
- auto index = instruction.arguments().get<GlobalIndex>();
- TRY(validate(index));
- auto& global = m_context.globals[index.value()];
- if (!global.is_mutable())
- return Errors::invalid("global variable for global.set"sv);
- TRY(stack.take(global.type()));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#table-instructions%E2%91%A2
- VALIDATE_INSTRUCTION(table_get)
- {
- auto index = instruction.arguments().get<TableIndex>();
- TRY(validate(index));
- auto& table = m_context.tables[index.value()];
- TRY(stack.take<ValueType::I32>());
- stack.append(table.element_type());
- return {};
- }
- VALIDATE_INSTRUCTION(table_set)
- {
- auto index = instruction.arguments().get<TableIndex>();
- TRY(validate(index));
- auto& table = m_context.tables[index.value()];
- TRY(stack.take(table.element_type()));
- TRY(stack.take<ValueType::I32>());
- return {};
- }
- VALIDATE_INSTRUCTION(table_size)
- {
- auto index = instruction.arguments().get<TableIndex>();
- TRY(validate(index));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(table_grow)
- {
- auto index = instruction.arguments().get<TableIndex>();
- TRY(validate(index));
- auto& table = m_context.tables[index.value()];
- TRY(stack.take<ValueType::I32>());
- TRY(stack.take(table.element_type()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(table_fill)
- {
- auto index = instruction.arguments().get<TableIndex>();
- TRY(validate(index));
- auto& table = m_context.tables[index.value()];
- TRY(stack.take<ValueType::I32>());
- TRY(stack.take(table.element_type()));
- TRY(stack.take<ValueType::I32>());
- return {};
- }
- VALIDATE_INSTRUCTION(table_copy)
- {
- auto& args = instruction.arguments().get<Instruction::TableTableArgs>();
- TRY(validate(args.lhs));
- TRY(validate(args.rhs));
- auto& lhs_table = m_context.tables[args.lhs.value()];
- auto& rhs_table = m_context.tables[args.rhs.value()];
- if (lhs_table.element_type() != rhs_table.element_type())
- return Errors::non_conforming_types("table.copy"sv, lhs_table.element_type(), rhs_table.element_type());
- if (!lhs_table.element_type().is_reference())
- return Errors::invalid("table.copy element type"sv, "a reference type"sv, lhs_table.element_type());
- TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(table_init)
- {
- auto& args = instruction.arguments().get<Instruction::TableElementArgs>();
- TRY(validate(args.table_index));
- TRY(validate(args.element_index));
- auto& table = m_context.tables[args.table_index.value()];
- auto& element_type = m_context.elements[args.element_index.value()];
- if (table.element_type() != element_type)
- return Errors::non_conforming_types("table.init"sv, table.element_type(), element_type);
- TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(elem_drop)
- {
- auto index = instruction.arguments().get<ElementIndex>();
- TRY(validate(index));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#memory-instructions%E2%91%A2
- VALIDATE_INSTRUCTION(i32_load)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(i32))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i32));
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_load)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(i64))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i64));
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_load)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(float))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(float));
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::F32));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_load)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(double))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(double));
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::F64));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_load16_s)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 16 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_load16_u)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 16 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_load8_s)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 8 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_load8_u)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 8 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_load32_s)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 32 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 32 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_load32_u)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 32 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 32 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_load16_s)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 16 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_load16_u)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 16 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_load8_s)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 8 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_load8_u)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 8 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I64));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_store)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(i32))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i32));
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_store)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(i64))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i64));
- TRY((stack.take<ValueType::I64, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(f32_store)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(float))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(float));
- TRY((stack.take<ValueType::F32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(f64_store)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(double))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(double));
- TRY((stack.take<ValueType::F64, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_store16)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 16 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(i32_store8)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 8 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
- TRY((stack.take<ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_store32)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 32 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 32 / 8);
- TRY((stack.take<ValueType::I64, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_store16)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 16 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
- TRY((stack.take<ValueType::I64, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(i64_store8)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > 8 / 8)
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
- TRY((stack.take<ValueType::I64, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(memory_size)
- {
- TRY(validate(instruction.arguments().get<Instruction::MemoryIndexArgument>().memory_index));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(memory_grow)
- {
- TRY(validate(instruction.arguments().get<Instruction::MemoryIndexArgument>().memory_index));
- TRY((stack.take<ValueType::I32>()));
- stack.append(ValueType(ValueType::I32));
- return {};
- }
- VALIDATE_INSTRUCTION(memory_fill)
- {
- TRY(validate(instruction.arguments().get<Instruction::MemoryIndexArgument>().memory_index));
- TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(memory_copy)
- {
- auto& args = instruction.arguments().get<Instruction::MemoryCopyArgs>();
- TRY(validate(args.src_index));
- TRY(validate(args.dst_index));
- TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(memory_init)
- {
- auto& args = instruction.arguments().get<Instruction::MemoryInitArgs>();
- TRY(validate(args.memory_index));
- TRY(validate(args.data_index));
- TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(data_drop)
- {
- auto index = instruction.arguments().get<DataIndex>();
- TRY(validate(index));
- return {};
- }
- // https://webassembly.github.io/spec/core/bikeshed/#control-instructions%E2%91%A2
- VALIDATE_INSTRUCTION(nop)
- {
- return {};
- }
- VALIDATE_INSTRUCTION(unreachable)
- {
- // https://webassembly.github.io/spec/core/bikeshed/#polymorphism
- stack.append(StackEntry());
- return {};
- }
- // Note: This is responsible for _all_ structured instructions, and is *not* from the spec.
- VALIDATE_INSTRUCTION(structured_end)
- {
- if (m_entered_scopes.is_empty())
- return Errors::invalid("usage of structured end"sv);
- auto last_scope = m_entered_scopes.take_last();
- m_context = m_parent_contexts.take_last();
- auto last_block_type = m_entered_blocks.take_last();
- switch (last_scope) {
- case ChildScopeKind::Block:
- case ChildScopeKind::IfWithoutElse:
- case ChildScopeKind::Else:
- m_block_details.take_last();
- break;
- case ChildScopeKind::IfWithElse:
- return Errors::invalid("usage of if without an else clause that appears to have one anyway"sv);
- }
- auto& results = last_block_type.results();
- for (size_t i = 1; i <= results.size(); ++i)
- TRY(stack.take(results[results.size() - i]));
- for (auto& result : results)
- stack.append(result);
- return {};
- }
- // Note: This is *not* from the spec.
- VALIDATE_INSTRUCTION(structured_else)
- {
- if (m_entered_scopes.is_empty())
- return Errors::invalid("usage of structured else"sv);
- if (m_entered_scopes.last() != ChildScopeKind::IfWithElse)
- return Errors::invalid("usage of structured else"sv);
- auto& block_type = m_entered_blocks.last();
- auto& results = block_type.results();
- for (size_t i = 1; i <= results.size(); ++i)
- TRY(stack.take(results[results.size() - i]));
- auto& details = m_block_details.last().details.get<BlockDetails::IfDetails>();
- m_entered_scopes.last() = ChildScopeKind::Else;
- stack = move(details.initial_stack);
- return {};
- }
- VALIDATE_INSTRUCTION(block)
- {
- auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
- auto block_type = TRY(validate(args.block_type));
- auto& parameters = block_type.parameters();
- for (size_t i = 1; i <= parameters.size(); ++i)
- TRY(stack.take(parameters[parameters.size() - i]));
- for (auto& parameter : parameters)
- stack.append(parameter);
- m_entered_scopes.append(ChildScopeKind::Block);
- m_block_details.empend(stack.actual_size(), Empty {});
- m_parent_contexts.append(m_context);
- m_entered_blocks.append(block_type);
- m_context.labels.prepend(ResultType { block_type.results() });
- return {};
- }
- VALIDATE_INSTRUCTION(loop)
- {
- auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
- auto block_type = TRY(validate(args.block_type));
- auto& parameters = block_type.parameters();
- for (size_t i = 1; i <= parameters.size(); ++i)
- TRY(stack.take(parameters[parameters.size() - i]));
- for (auto& parameter : parameters)
- stack.append(parameter);
- m_entered_scopes.append(ChildScopeKind::Block);
- m_block_details.empend(stack.actual_size(), Empty {});
- m_parent_contexts.append(m_context);
- m_entered_blocks.append(block_type);
- m_context.labels.prepend(ResultType { block_type.parameters() });
- return {};
- }
- VALIDATE_INSTRUCTION(if_)
- {
- auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
- auto block_type = TRY(validate(args.block_type));
- TRY(stack.take<ValueType::I32>());
- auto stack_snapshot = stack;
- auto& parameters = block_type.parameters();
- for (size_t i = 1; i <= parameters.size(); ++i)
- TRY(stack.take(parameters[parameters.size() - i]));
- for (auto& parameter : parameters)
- stack.append(parameter);
- m_entered_scopes.append(args.else_ip.has_value() ? ChildScopeKind::IfWithElse : ChildScopeKind::IfWithoutElse);
- m_block_details.empend(stack.actual_size(), BlockDetails::IfDetails { move(stack_snapshot) });
- m_parent_contexts.append(m_context);
- m_entered_blocks.append(block_type);
- m_context.labels.prepend(ResultType { block_type.results() });
- return {};
- }
- VALIDATE_INSTRUCTION(br)
- {
- auto label = instruction.arguments().get<LabelIndex>();
- TRY(validate(label));
- auto& type = m_context.labels[label.value()];
- for (size_t i = 1; i <= type.types().size(); ++i)
- TRY(stack.take(type.types()[type.types().size() - i]));
- stack.append(StackEntry());
- return {};
- }
- VALIDATE_INSTRUCTION(br_if)
- {
- auto label = instruction.arguments().get<LabelIndex>();
- TRY(validate(label));
- TRY(stack.take<ValueType::I32>());
- auto& type = m_context.labels[label.value()];
- Vector<StackEntry> entries;
- entries.ensure_capacity(type.types().size());
- for (size_t i = 0; i < type.types().size(); ++i) {
- auto& entry = type.types()[type.types().size() - i - 1];
- TRY(stack.take(entry));
- entries.append(entry);
- }
- for (size_t i = 0; i < entries.size(); ++i)
- stack.append(entries[entries.size() - i - 1]);
- return {};
- }
- VALIDATE_INSTRUCTION(br_table)
- {
- auto& args = instruction.arguments().get<Instruction::TableBranchArgs>();
- TRY(validate(args.default_));
- for (auto& label : args.labels)
- TRY(validate(label));
- TRY(stack.take<ValueType::I32>());
- auto& default_types = m_context.labels[args.default_.value()].types();
- auto arity = default_types.size();
- auto stack_snapshot = stack;
- auto stack_to_check = stack_snapshot;
- for (auto& label : args.labels) {
- auto& label_types = m_context.labels[label.value()].types();
- if (label_types.size() != arity)
- return Errors::invalid("br_table label arity mismatch"sv);
- for (size_t i = 0; i < arity; ++i)
- TRY(stack_to_check.take(label_types[label_types.size() - i - 1]));
- stack_to_check = stack_snapshot;
- }
- for (size_t i = 0; i < arity; ++i) {
- auto expected = default_types[default_types.size() - i - 1];
- TRY((stack.take(expected)));
- }
- stack.append(StackEntry());
- return {};
- }
- VALIDATE_INSTRUCTION(return_)
- {
- if (!m_context.return_.has_value())
- return Errors::invalid("use of return outside function"sv);
- auto& return_types = m_context.return_->types();
- for (size_t i = 0; i < return_types.size(); ++i)
- TRY((stack.take(return_types[return_types.size() - i - 1])));
- stack.append(StackEntry());
- return {};
- }
- VALIDATE_INSTRUCTION(call)
- {
- auto index = instruction.arguments().get<FunctionIndex>();
- TRY(validate(index));
- auto& function_type = m_context.functions[index.value()];
- for (size_t i = 0; i < function_type.parameters().size(); ++i)
- TRY(stack.take(function_type.parameters()[function_type.parameters().size() - i - 1]));
- for (auto& type : function_type.results())
- stack.append(type);
- return {};
- }
- VALIDATE_INSTRUCTION(call_indirect)
- {
- auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
- TRY(validate(args.table));
- TRY(validate(args.type));
- auto& table = m_context.tables[args.table.value()];
- if (!table.element_type().is_reference())
- return Errors::invalid("table element type for call.indirect"sv, "a reference type"sv, table.element_type());
- auto& type = m_context.types[args.type.value()];
- TRY(stack.take<ValueType::I32>());
- for (size_t i = 0; i < type.parameters().size(); ++i)
- TRY(stack.take(type.parameters()[type.parameters().size() - i - 1]));
- for (auto& type : type.results())
- stack.append(type);
- return {};
- }
- VALIDATE_INSTRUCTION(v128_load)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(u128))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(u128));
- TRY((stack.take_and_put<ValueType::I32>(ValueType::V128)));
- return {};
- }
- VALIDATE_INSTRUCTION(v128_load8x8_s)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 8;
- constexpr auto M = 8;
- constexpr auto max_alignment = N * M / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load8x8_u)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 8;
- constexpr auto M = 8;
- constexpr auto max_alignment = N * M / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load16x4_s)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 16;
- constexpr auto M = 4;
- constexpr auto max_alignment = N * M / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load16x4_u)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 16;
- constexpr auto M = 4;
- constexpr auto max_alignment = N * M / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load32x2_s)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 32;
- constexpr auto M = 2;
- constexpr auto max_alignment = N * M / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load32x2_u)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 32;
- constexpr auto M = 2;
- constexpr auto max_alignment = N * M / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load8_splat)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 8;
- constexpr auto max_alignment = N / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load16_splat)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 16;
- constexpr auto max_alignment = N / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load32_splat)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 32;
- constexpr auto max_alignment = N / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load64_splat)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 64;
- constexpr auto max_alignment = N / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_store)
- {
- auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- TRY(validate(arg.memory_index));
- if ((1ull << arg.align) > sizeof(u128))
- return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(u128));
- TRY((stack.take<ValueType::V128, ValueType::I32>()));
- return {};
- }
- VALIDATE_INSTRUCTION(v128_const)
- {
- is_constant = true;
- stack.append(ValueType(ValueType::V128));
- return {};
- }
- VALIDATE_INSTRUCTION(i8x16_shuffle)
- {
- auto const& arg = instruction.arguments().get<Instruction::ShuffleArgument>();
- for (auto lane : arg.lanes) {
- if (lane >= 32)
- return Errors::out_of_bounds("shuffle lane"sv, lane, 0, 32);
- }
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_swizzle)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- enum class Shape {
- I8x16,
- I16x8,
- I32x4,
- I64x2,
- F32x4,
- F64x2,
- };
- template<Shape shape>
- static constexpr Wasm::ValueType::Kind unpacked()
- {
- switch (shape) {
- case Shape::I8x16:
- case Shape::I16x8:
- case Shape::I32x4:
- return Wasm::ValueType::I32;
- case Shape::I64x2:
- return Wasm::ValueType::I64;
- case Shape::F32x4:
- return Wasm::ValueType::F32;
- case Shape::F64x2:
- return Wasm::ValueType::F64;
- }
- }
- template<Shape shape>
- static constexpr size_t dimensions()
- {
- switch (shape) {
- case Shape::I8x16:
- return 16;
- case Shape::I16x8:
- return 8;
- case Shape::I32x4:
- return 4;
- case Shape::I64x2:
- return 2;
- case Shape::F32x4:
- return 4;
- case Shape::F64x2:
- return 2;
- }
- }
- VALIDATE_INSTRUCTION(i8x16_splat)
- {
- constexpr auto unpacked_shape = unpacked<Shape::I8x16>();
- return stack.take_and_put<unpacked_shape>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_splat)
- {
- constexpr auto unpacked_shape = unpacked<Shape::I16x8>();
- return stack.take_and_put<unpacked_shape>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_splat)
- {
- constexpr auto unpacked_shape = unpacked<Shape::I32x4>();
- return stack.take_and_put<unpacked_shape>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_splat)
- {
- constexpr auto unpacked_shape = unpacked<Shape::I64x2>();
- return stack.take_and_put<unpacked_shape>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_splat)
- {
- constexpr auto unpacked_shape = unpacked<Shape::F32x4>();
- return stack.take_and_put<unpacked_shape>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_splat)
- {
- constexpr auto unpacked_shape = unpacked<Shape::F64x2>();
- return stack.take_and_put<unpacked_shape>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_extract_lane_s)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I8x16;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(i8x16_extract_lane_u)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I8x16;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(i8x16_replace_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I8x16;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extract_lane_s)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I16x8;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(i16x8_extract_lane_u)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I16x8;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(i16x8_replace_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I16x8;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extract_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I32x4;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(i32x4_replace_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I32x4;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extract_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I64x2;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(i64x2_replace_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::I64x2;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_extract_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::F32x4;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(f32x4_replace_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::F32x4;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_extract_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::F64x2;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<ValueType::V128>(unpacked<shape>());
- }
- VALIDATE_INSTRUCTION(f64x2_replace_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
- constexpr auto shape = Shape::F64x2;
- constexpr auto max_lane = dimensions<shape>();
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
- return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_eq)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_ne)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_lt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_lt_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_gt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_gt_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_le_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_le_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_ge_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_ge_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_eq)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_ne)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_lt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_lt_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_gt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_gt_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_le_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_le_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_ge_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_ge_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_eq)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_ne)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_lt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_lt_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_gt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_gt_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_le_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_le_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_ge_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_ge_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_eq)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_ne)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_lt)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_gt)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_le)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_ge)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_eq)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_ne)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_lt)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_gt)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_le)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_ge)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_not)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_and)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_andnot)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_or)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_xor)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_bitselect)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_any_true)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(v128_load8_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 8;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane > max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if ((1 << arg.memory.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load16_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 16;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if ((1 << arg.memory.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load32_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 32;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if ((1 << arg.memory.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load64_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 64;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if (arg.memory.align > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_store8_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 8;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if ((1 << arg.memory.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_store16_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 16;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if ((1 << arg.memory.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_store32_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 32;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if ((1 << arg.memory.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_store64_lane)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
- constexpr auto N = 64;
- constexpr auto max_lane = 128 / N;
- constexpr auto max_alignment = N / 8;
- if (arg.lane >= max_lane)
- return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
- TRY(validate(arg.memory.memory_index));
- if ((1 << arg.memory.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load32_zero)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 32;
- constexpr auto max_alignment = N / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(v128_load64_zero)
- {
- auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
- constexpr auto N = 64;
- constexpr auto max_alignment = N / 8;
- TRY(validate(arg.memory_index));
- if ((1 << arg.align) > max_alignment)
- return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
- return stack.take_and_put<ValueType::I32>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_demote_f64x2_zero)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_promote_low_f32x4)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_abs)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_neg)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_popcnt)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_all_true)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i8x16_bitmask)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i8x16_narrow_i16x8_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_narrow_i16x8_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_ceil)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_floor)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_trunc)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_nearest)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_shl)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_shr_s)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_shr_u)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_add)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_add_sat_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_add_sat_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_sub)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_sub_sat_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_sub_sat_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_ceil)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_floor)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_min_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_min_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_max_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_max_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_trunc)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i8x16_avgr_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extadd_pairwise_i8x16_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extadd_pairwise_i8x16_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extadd_pairwise_i16x8_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extadd_pairwise_i16x8_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_abs)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_neg)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_q15mulr_sat_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_all_true)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i16x8_bitmask)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i16x8_narrow_i32x4_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_narrow_i32x4_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extend_low_i8x16_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extend_high_i8x16_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extend_low_i8x16_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extend_high_i8x16_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_shl)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_shr_s)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_shr_u)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_add)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_add_sat_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_add_sat_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_sub)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_sub_sat_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_sub_sat_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_nearest)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_mul)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_min_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_min_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_max_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_max_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_avgr_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extmul_low_i8x16_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extmul_high_i8x16_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extmul_low_i8x16_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i16x8_extmul_high_i8x16_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_abs)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_neg)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_all_true)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i32x4_bitmask)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i32x4_extend_low_i16x8_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extend_high_i16x8_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extend_low_i16x8_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extend_high_i16x8_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_shl)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_shr_s)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_shr_u)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_add)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_sub)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_mul)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_min_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_min_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_max_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_max_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_dot_i16x8_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extmul_low_i16x8_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extmul_high_i16x8_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extmul_low_i16x8_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_extmul_high_i16x8_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_abs)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_neg)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_all_true)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i64x2_bitmask)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::I32);
- }
- VALIDATE_INSTRUCTION(i64x2_extend_low_i32x4_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extend_high_i32x4_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extend_low_i32x4_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extend_high_i32x4_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_shl)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_shr_s)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_shr_u)
- {
- return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_add)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_sub)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_mul)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_eq)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_ne)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_lt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_gt_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_le_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_ge_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extmul_low_i32x4_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extmul_high_i32x4_s)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extmul_low_i32x4_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i64x2_extmul_high_i32x4_u)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_abs)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_neg)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_sqrt)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_add)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_sub)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_mul)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_div)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_min)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_max)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_pmin)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_pmax)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_abs)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_neg)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_sqrt)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_add)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_sub)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_mul)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_div)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_min)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_max)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_pmin)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_pmax)
- {
- return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_trunc_sat_f32x4_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_trunc_sat_f32x4_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_convert_i32x4_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f32x4_convert_i32x4_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_trunc_sat_f64x2_s_zero)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(i32x4_trunc_sat_f64x2_u_zero)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_convert_low_i32x4_s)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- VALIDATE_INSTRUCTION(f64x2_convert_low_i32x4_u)
- {
- return stack.take_and_put<ValueType::V128>(ValueType::V128);
- }
- ErrorOr<void, ValidationError> Validator::validate(Instruction const& instruction, Stack& stack, bool& is_constant)
- {
- switch (instruction.opcode().value()) {
- #define M(name, integer_value) \
- case Instructions::name.value(): \
- dbgln_if(WASM_VALIDATOR_DEBUG, "checking {}, stack = {}", #name, stack); \
- return validate_instruction<integer_value>(instruction, stack, is_constant);
- ENUMERATE_WASM_OPCODES(M)
- #undef M
- default:
- is_constant = false;
- return Errors::invalid(ByteString::formatted("instruction opcode (0x{:x})", instruction.opcode().value()));
- }
- }
- ErrorOr<Validator::ExpressionTypeResult, ValidationError> Validator::validate(Expression const& expression, Vector<ValueType> const& result_types)
- {
- Stack stack;
- bool is_constant_expression = true;
- for (auto& instruction : expression.instructions()) {
- bool is_constant = false;
- TRY(validate(instruction, stack, is_constant));
- is_constant_expression &= is_constant;
- }
- auto expected_result_types = result_types;
- while (!expected_result_types.is_empty())
- TRY(stack.take(expected_result_types.take_last()));
- for (auto& type : result_types)
- stack.append(type);
- return ExpressionTypeResult { stack.release_vector(), is_constant_expression };
- }
- bool Validator::Stack::operator==(Stack const& other) const
- {
- if (!m_did_insert_unknown_entry && !other.m_did_insert_unknown_entry)
- return static_cast<Vector<StackEntry> const&>(*this) == static_cast<Vector<StackEntry> const&>(other);
- Optional<size_t> own_last_unknown_entry_index_from_end, other_last_unknown_entry_index_from_end;
- auto other_size = static_cast<Vector<StackEntry> const&>(other).size();
- auto own_size = Vector<StackEntry>::size();
- for (size_t i = 0; i < own_size; ++i) {
- if (other_size <= i)
- break;
- auto own_entry = at(own_size - i - 1);
- auto other_entry = other.at(other_size - i - 1);
- if (!own_entry.is_known) {
- own_last_unknown_entry_index_from_end = i;
- break;
- }
- if (!other_entry.is_known) {
- other_last_unknown_entry_index_from_end = i;
- break;
- }
- }
- if (!own_last_unknown_entry_index_from_end.has_value() && !other_last_unknown_entry_index_from_end.has_value()) {
- if (static_cast<Vector<StackEntry> const&>(other).is_empty() || Vector<StackEntry>::is_empty())
- return true;
- dbgln("Equality check internal error between");
- dbgln("stack:");
- for (auto& entry : *this)
- dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
- dbgln("and stack:");
- for (auto& entry : other)
- dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
- VERIFY_NOT_REACHED();
- }
- auto index_from_end = max(own_last_unknown_entry_index_from_end.value_or(0), other_last_unknown_entry_index_from_end.value_or(0));
- for (size_t i = 0; i < index_from_end; ++i) {
- if (at(own_size - i - 1) != other.at(other_size - i - 1))
- return false;
- }
- return true;
- }
- ByteString Validator::Errors::find_instruction_name(SourceLocation const& location)
- {
- auto index = location.function_name().find('<');
- auto end_index = location.function_name().find('>');
- if (!index.has_value() || !end_index.has_value())
- return ByteString::formatted("{}", location);
- auto opcode = location.function_name().substring_view(index.value() + 1, end_index.value() - index.value() - 1).to_number<unsigned>();
- if (!opcode.has_value())
- return ByteString::formatted("{}", location);
- return instruction_name(OpCode { *opcode });
- }
- }
|