Validator.cpp 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashTable.h>
  7. #include <AK/Result.h>
  8. #include <AK/SourceLocation.h>
  9. #include <AK/TemporaryChange.h>
  10. #include <AK/Try.h>
  11. #include <LibWasm/AbstractMachine/Validator.h>
  12. #include <LibWasm/Printer/Printer.h>
  13. namespace Wasm {
  14. ErrorOr<void, ValidationError> Validator::validate(Module& module)
  15. {
  16. ErrorOr<void, ValidationError> result {};
  17. // Note: The spec performs this after populating the context, but there's no real reason to do so,
  18. // as this has no dependency.
  19. HashTable<StringView> seen_export_names;
  20. module.for_each_section_of_type<ExportSection>([&result, &seen_export_names](ExportSection const& section) {
  21. if (result.is_error())
  22. return;
  23. for (auto& export_ : section.entries()) {
  24. if (seen_export_names.try_set(export_.name()).release_value_but_fixme_should_propagate_errors() != AK::HashSetResult::InsertedNewEntry)
  25. result = Errors::duplicate_export_name(export_.name());
  26. return;
  27. }
  28. });
  29. if (result.is_error()) {
  30. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  31. return result;
  32. }
  33. m_context = {};
  34. module.for_each_section_of_type<TypeSection>([this](TypeSection const& section) {
  35. m_context.types = section.types();
  36. });
  37. module.for_each_section_of_type<ImportSection>([&](ImportSection const& section) {
  38. for (auto& import_ : section.imports()) {
  39. import_.description().visit(
  40. [this, &result](TypeIndex const& index) {
  41. if (m_context.types.size() > index.value())
  42. m_context.functions.append(m_context.types[index.value()]);
  43. else
  44. result = Errors::invalid("TypeIndex"sv);
  45. m_context.imported_function_count++;
  46. },
  47. [this](FunctionType const& type) {
  48. m_context.functions.append(type);
  49. m_context.imported_function_count++;
  50. },
  51. [this](TableType const& type) { m_context.tables.append(type); },
  52. [this](MemoryType const& type) { m_context.memories.append(type); },
  53. [this](GlobalType const& type) {
  54. m_globals_without_internal_globals.append(type);
  55. m_context.globals.append(type);
  56. });
  57. }
  58. });
  59. if (result.is_error()) {
  60. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  61. return result;
  62. }
  63. module.for_each_section_of_type<FunctionSection>([this, &result](FunctionSection const& section) {
  64. if (result.is_error())
  65. return;
  66. m_context.functions.ensure_capacity(section.types().size() + m_context.functions.size());
  67. for (auto& index : section.types()) {
  68. if (m_context.types.size() > index.value()) {
  69. m_context.functions.append(m_context.types[index.value()]);
  70. } else {
  71. result = Errors::invalid("TypeIndex"sv);
  72. break;
  73. }
  74. }
  75. });
  76. if (result.is_error()) {
  77. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  78. return result;
  79. }
  80. module.for_each_section_of_type<TableSection>([this](TableSection const& section) {
  81. m_context.tables.ensure_capacity(m_context.tables.size() + section.tables().size());
  82. for (auto& table : section.tables())
  83. m_context.tables.unchecked_append(table.type());
  84. });
  85. module.for_each_section_of_type<MemorySection>([this](MemorySection const& section) {
  86. m_context.memories.ensure_capacity(m_context.memories.size() + section.memories().size());
  87. for (auto& memory : section.memories())
  88. m_context.memories.unchecked_append(memory.type());
  89. });
  90. module.for_each_section_of_type<GlobalSection>([this](GlobalSection const& section) {
  91. m_context.globals.ensure_capacity(m_context.globals.size() + section.entries().size());
  92. for (auto& global : section.entries())
  93. m_context.globals.unchecked_append(global.type());
  94. });
  95. module.for_each_section_of_type<ElementSection>([this](ElementSection const& section) {
  96. m_context.elements.ensure_capacity(section.segments().size());
  97. for (auto& segment : section.segments())
  98. m_context.elements.unchecked_append(segment.type);
  99. });
  100. module.for_each_section_of_type<DataSection>([this](DataSection const& section) {
  101. m_context.datas.resize(section.data().size());
  102. });
  103. // FIXME: C.refs is the set funcidx(module with funcs=ϵ with start=ϵ),
  104. // i.e., the set of function indices occurring in the module, except in its functions or start function.
  105. // This is rather weird, it seems to ultimately be checking that `ref.func` uses a specific set of predetermined functions:
  106. // 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,
  107. // which limits it to only functions referenced from the elements section.
  108. // 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
  109. // _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.
  110. //
  111. // For now, we simply assume that we need to scan the aforementioned section initializers for (ref.func f).
  112. auto scan_expression_for_function_indices = [&](auto& expression) {
  113. for (auto& instruction : expression.instructions()) {
  114. if (instruction.opcode() == Instructions::ref_func)
  115. m_context.references.set(instruction.arguments().template get<FunctionIndex>());
  116. }
  117. };
  118. module.for_each_section_of_type<ElementSection>([&](ElementSection const& section) {
  119. for (auto& segment : section.segments()) {
  120. for (auto& expression : segment.init)
  121. scan_expression_for_function_indices(expression);
  122. }
  123. });
  124. module.for_each_section_of_type<GlobalSection>([&](GlobalSection const& section) {
  125. for (auto& segment : section.entries())
  126. scan_expression_for_function_indices(segment.expression());
  127. });
  128. for (auto& section : module.sections()) {
  129. section.visit([this, &result](auto& section) {
  130. result = validate(section);
  131. });
  132. if (result.is_error()) {
  133. module.set_validation_status(Module::ValidationStatus::Invalid, {});
  134. return result;
  135. }
  136. }
  137. module.set_validation_status(Module::ValidationStatus::Valid, {});
  138. return {};
  139. }
  140. ErrorOr<void, ValidationError> Validator::validate(ImportSection const& section)
  141. {
  142. for (auto& import_ : section.imports())
  143. TRY(import_.description().visit([&](auto& entry) { return validate(entry); }));
  144. return {};
  145. }
  146. ErrorOr<void, ValidationError> Validator::validate(ExportSection const& section)
  147. {
  148. for (auto& export_ : section.entries())
  149. TRY(export_.description().visit([&](auto& entry) { return validate(entry); }));
  150. return {};
  151. }
  152. ErrorOr<void, ValidationError> Validator::validate(StartSection const& section)
  153. {
  154. TRY(validate(section.function().index()));
  155. FunctionType const& type = m_context.functions[section.function().index().value()];
  156. if (!type.parameters().is_empty() || !type.results().is_empty())
  157. return Errors::invalid("start function signature"sv);
  158. return {};
  159. }
  160. ErrorOr<void, ValidationError> Validator::validate(DataSection const& section)
  161. {
  162. for (auto& entry : section.data()) {
  163. TRY(entry.value().visit(
  164. [](DataSection::Data::Passive const&) { return ErrorOr<void, ValidationError> {}; },
  165. [&](DataSection::Data::Active const& active) -> ErrorOr<void, ValidationError> {
  166. TRY(validate(active.index));
  167. auto expression_result = TRY(validate(active.offset, { ValueType(ValueType::I32) }));
  168. if (!expression_result.is_constant)
  169. return Errors::invalid("active data initializer"sv);
  170. if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(ValueType::I32))
  171. return Errors::invalid("active data initializer type"sv, ValueType(ValueType::I32), expression_result.result_types);
  172. return {};
  173. }));
  174. }
  175. return {};
  176. }
  177. ErrorOr<void, ValidationError> Validator::validate(ElementSection const& section)
  178. {
  179. for (auto& segment : section.segments()) {
  180. TRY(segment.mode.visit(
  181. [](ElementSection::Declarative const&) -> ErrorOr<void, ValidationError> { return {}; },
  182. [](ElementSection::Passive const&) -> ErrorOr<void, ValidationError> { return {}; },
  183. [&](ElementSection::Active const& active) -> ErrorOr<void, ValidationError> {
  184. TRY(validate(active.index));
  185. auto expression_result = TRY(validate(active.expression, { ValueType(ValueType::I32) }));
  186. if (!expression_result.is_constant)
  187. return Errors::invalid("active element initializer"sv);
  188. if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(ValueType::I32))
  189. return Errors::invalid("active element initializer type"sv, ValueType(ValueType::I32), expression_result.result_types);
  190. return {};
  191. }));
  192. for (auto& expression : segment.init) {
  193. auto result = TRY(validate(expression, { segment.type }));
  194. if (!result.is_constant)
  195. return Errors::invalid("element initializer"sv);
  196. }
  197. }
  198. return {};
  199. }
  200. ErrorOr<void, ValidationError> Validator::validate(GlobalSection const& section)
  201. {
  202. TemporaryChange omit_internal_globals { m_context.globals, m_globals_without_internal_globals };
  203. for (auto& entry : section.entries()) {
  204. auto& type = entry.type();
  205. TRY(validate(type));
  206. auto expression_result = TRY(validate(entry.expression(), { type.type() }));
  207. if (!expression_result.is_constant)
  208. return Errors::invalid("global variable initializer"sv);
  209. if (expression_result.result_types.size() != 1 || !expression_result.result_types.first().is_of_kind(type.type().kind()))
  210. return Errors::invalid("global variable initializer type"sv, ValueType(ValueType::I32), expression_result.result_types);
  211. }
  212. return {};
  213. }
  214. ErrorOr<void, ValidationError> Validator::validate(MemorySection const& section)
  215. {
  216. for (auto& entry : section.memories())
  217. TRY(validate(entry.type()));
  218. return {};
  219. }
  220. ErrorOr<void, ValidationError> Validator::validate(TableSection const& section)
  221. {
  222. for (auto& entry : section.tables())
  223. TRY(validate(entry.type()));
  224. return {};
  225. }
  226. ErrorOr<void, ValidationError> Validator::validate(CodeSection const& section)
  227. {
  228. size_t index = m_context.imported_function_count;
  229. for (auto& entry : section.functions()) {
  230. auto function_index = index++;
  231. TRY(validate(FunctionIndex { function_index }));
  232. auto& function_type = m_context.functions[function_index];
  233. auto& function = entry.func();
  234. auto function_validator = fork();
  235. function_validator.m_context.locals = {};
  236. function_validator.m_context.locals.extend(function_type.parameters());
  237. for (auto& local : function.locals()) {
  238. for (size_t i = 0; i < local.n(); ++i)
  239. function_validator.m_context.locals.append(local.type());
  240. }
  241. function_validator.m_context.labels = { ResultType { function_type.results() } };
  242. function_validator.m_context.return_ = ResultType { function_type.results() };
  243. TRY(function_validator.validate(function.body(), function_type.results()));
  244. }
  245. return {};
  246. }
  247. ErrorOr<void, ValidationError> Validator::validate(TableType const& type)
  248. {
  249. return validate(type.limits(), 32);
  250. }
  251. ErrorOr<void, ValidationError> Validator::validate(MemoryType const& type)
  252. {
  253. return validate(type.limits(), 16);
  254. }
  255. ErrorOr<FunctionType, ValidationError> Validator::validate(BlockType const& type)
  256. {
  257. if (type.kind() == BlockType::Index) {
  258. TRY(validate(type.type_index()));
  259. return m_context.types[type.type_index().value()];
  260. }
  261. if (type.kind() == BlockType::Type) {
  262. FunctionType function_type { {}, { type.value_type() } };
  263. TRY(validate(function_type));
  264. return function_type;
  265. }
  266. if (type.kind() == BlockType::Empty)
  267. return FunctionType { {}, {} };
  268. return Errors::invalid("BlockType"sv);
  269. }
  270. ErrorOr<void, ValidationError> Validator::validate(Limits const& limits, size_t k)
  271. {
  272. auto bound = (1ull << k) - 1;
  273. auto check_bound = [bound](auto value) {
  274. return static_cast<u64>(value) <= bound;
  275. };
  276. if (!check_bound(limits.min()))
  277. return Errors::out_of_bounds("limit minimum"sv, limits.min(), 0, bound);
  278. if (limits.max().has_value() && (limits.max().value() < limits.min() || !check_bound(*limits.max())))
  279. return Errors::out_of_bounds("limit maximum"sv, limits.max().value(), limits.min(), bound);
  280. return {};
  281. }
  282. template<u64 opcode>
  283. ErrorOr<void, ValidationError> Validator::validate_instruction(Instruction const& instruction, Stack&, bool&)
  284. {
  285. return Errors::invalid(ByteString::formatted("instruction opcode (0x{:x}) (missing validation!)", instruction.opcode().value()));
  286. }
  287. #define VALIDATE_INSTRUCTION(name) \
  288. template<> \
  289. ErrorOr<void, ValidationError> Validator::validate_instruction<Instructions::name.value()>([[maybe_unused]] Instruction const& instruction, [[maybe_unused]] Stack& stack, [[maybe_unused]] bool& is_constant)
  290. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-instr-numericmathsfconstc
  291. VALIDATE_INSTRUCTION(i32_const)
  292. {
  293. is_constant = true;
  294. stack.append(ValueType(ValueType::I32));
  295. return {};
  296. }
  297. VALIDATE_INSTRUCTION(i64_const)
  298. {
  299. is_constant = true;
  300. stack.append(ValueType(ValueType::I64));
  301. return {};
  302. }
  303. VALIDATE_INSTRUCTION(f32_const)
  304. {
  305. is_constant = true;
  306. stack.append(ValueType(ValueType::F32));
  307. return {};
  308. }
  309. VALIDATE_INSTRUCTION(f64_const)
  310. {
  311. is_constant = true;
  312. stack.append(ValueType(ValueType::F64));
  313. return {};
  314. }
  315. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-unopmathitunop
  316. VALIDATE_INSTRUCTION(i32_clz)
  317. {
  318. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  319. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  320. return {};
  321. }
  322. VALIDATE_INSTRUCTION(i32_ctz)
  323. {
  324. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  325. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  326. return {};
  327. }
  328. VALIDATE_INSTRUCTION(i32_popcnt)
  329. {
  330. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  331. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  332. return {};
  333. }
  334. VALIDATE_INSTRUCTION(i64_clz)
  335. {
  336. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  337. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  338. return {};
  339. }
  340. VALIDATE_INSTRUCTION(i64_ctz)
  341. {
  342. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  343. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  344. return {};
  345. }
  346. VALIDATE_INSTRUCTION(i64_popcnt)
  347. {
  348. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  349. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  350. return {};
  351. }
  352. VALIDATE_INSTRUCTION(f32_abs)
  353. {
  354. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  355. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  356. return {};
  357. }
  358. VALIDATE_INSTRUCTION(f32_neg)
  359. {
  360. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  361. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  362. return {};
  363. }
  364. VALIDATE_INSTRUCTION(f32_sqrt)
  365. {
  366. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  367. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  368. return {};
  369. }
  370. VALIDATE_INSTRUCTION(f32_ceil)
  371. {
  372. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  373. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  374. return {};
  375. }
  376. VALIDATE_INSTRUCTION(f32_floor)
  377. {
  378. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  379. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  380. return {};
  381. }
  382. VALIDATE_INSTRUCTION(f32_trunc)
  383. {
  384. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  385. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  386. return {};
  387. }
  388. VALIDATE_INSTRUCTION(f32_nearest)
  389. {
  390. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F32))
  391. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  392. return {};
  393. }
  394. VALIDATE_INSTRUCTION(f64_abs)
  395. {
  396. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  397. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F32) });
  398. return {};
  399. }
  400. VALIDATE_INSTRUCTION(f64_neg)
  401. {
  402. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  403. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  404. return {};
  405. }
  406. VALIDATE_INSTRUCTION(f64_sqrt)
  407. {
  408. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  409. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  410. return {};
  411. }
  412. VALIDATE_INSTRUCTION(f64_ceil)
  413. {
  414. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  415. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  416. return {};
  417. }
  418. VALIDATE_INSTRUCTION(f64_floor)
  419. {
  420. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  421. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  422. return {};
  423. }
  424. VALIDATE_INSTRUCTION(f64_trunc)
  425. {
  426. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  427. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  428. return {};
  429. }
  430. VALIDATE_INSTRUCTION(f64_nearest)
  431. {
  432. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::F64))
  433. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::F64) });
  434. return {};
  435. }
  436. VALIDATE_INSTRUCTION(i32_extend16_s)
  437. {
  438. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  439. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  440. return {};
  441. }
  442. VALIDATE_INSTRUCTION(i32_extend8_s)
  443. {
  444. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I32))
  445. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32) });
  446. return {};
  447. }
  448. VALIDATE_INSTRUCTION(i64_extend32_s)
  449. {
  450. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  451. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  452. return {};
  453. }
  454. VALIDATE_INSTRUCTION(i64_extend16_s)
  455. {
  456. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  457. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  458. return {};
  459. }
  460. VALIDATE_INSTRUCTION(i64_extend8_s)
  461. {
  462. if (stack.is_empty() || !stack.last().is_of_kind(ValueType::I64))
  463. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I64) });
  464. return {};
  465. }
  466. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-binopmathitbinop
  467. VALIDATE_INSTRUCTION(i32_add)
  468. {
  469. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  470. stack.append(ValueType(ValueType::I32));
  471. return {};
  472. }
  473. VALIDATE_INSTRUCTION(i32_sub)
  474. {
  475. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  476. stack.append(ValueType(ValueType::I32));
  477. return {};
  478. }
  479. VALIDATE_INSTRUCTION(i32_mul)
  480. {
  481. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  482. stack.append(ValueType(ValueType::I32));
  483. return {};
  484. }
  485. VALIDATE_INSTRUCTION(i32_divs)
  486. {
  487. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  488. stack.append(ValueType(ValueType::I32));
  489. return {};
  490. }
  491. VALIDATE_INSTRUCTION(i32_divu)
  492. {
  493. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  494. stack.append(ValueType(ValueType::I32));
  495. return {};
  496. }
  497. VALIDATE_INSTRUCTION(i32_rems)
  498. {
  499. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  500. stack.append(ValueType(ValueType::I32));
  501. return {};
  502. }
  503. VALIDATE_INSTRUCTION(i32_remu)
  504. {
  505. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  506. stack.append(ValueType(ValueType::I32));
  507. return {};
  508. }
  509. VALIDATE_INSTRUCTION(i32_and)
  510. {
  511. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  512. stack.append(ValueType(ValueType::I32));
  513. return {};
  514. }
  515. VALIDATE_INSTRUCTION(i32_or)
  516. {
  517. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  518. stack.append(ValueType(ValueType::I32));
  519. return {};
  520. }
  521. VALIDATE_INSTRUCTION(i32_xor)
  522. {
  523. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  524. stack.append(ValueType(ValueType::I32));
  525. return {};
  526. }
  527. VALIDATE_INSTRUCTION(i32_shl)
  528. {
  529. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  530. stack.append(ValueType(ValueType::I32));
  531. return {};
  532. }
  533. VALIDATE_INSTRUCTION(i32_shrs)
  534. {
  535. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  536. stack.append(ValueType(ValueType::I32));
  537. return {};
  538. }
  539. VALIDATE_INSTRUCTION(i32_shru)
  540. {
  541. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  542. stack.append(ValueType(ValueType::I32));
  543. return {};
  544. }
  545. VALIDATE_INSTRUCTION(i32_rotl)
  546. {
  547. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  548. stack.append(ValueType(ValueType::I32));
  549. return {};
  550. }
  551. VALIDATE_INSTRUCTION(i32_rotr)
  552. {
  553. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  554. stack.append(ValueType(ValueType::I32));
  555. return {};
  556. }
  557. VALIDATE_INSTRUCTION(i64_add)
  558. {
  559. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  560. stack.append(ValueType(ValueType::I64));
  561. return {};
  562. }
  563. VALIDATE_INSTRUCTION(i64_sub)
  564. {
  565. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  566. stack.append(ValueType(ValueType::I64));
  567. return {};
  568. }
  569. VALIDATE_INSTRUCTION(i64_mul)
  570. {
  571. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  572. stack.append(ValueType(ValueType::I64));
  573. return {};
  574. }
  575. VALIDATE_INSTRUCTION(i64_divs)
  576. {
  577. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  578. stack.append(ValueType(ValueType::I64));
  579. return {};
  580. }
  581. VALIDATE_INSTRUCTION(i64_divu)
  582. {
  583. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  584. stack.append(ValueType(ValueType::I64));
  585. return {};
  586. }
  587. VALIDATE_INSTRUCTION(i64_rems)
  588. {
  589. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  590. stack.append(ValueType(ValueType::I64));
  591. return {};
  592. }
  593. VALIDATE_INSTRUCTION(i64_remu)
  594. {
  595. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  596. stack.append(ValueType(ValueType::I64));
  597. return {};
  598. }
  599. VALIDATE_INSTRUCTION(i64_and)
  600. {
  601. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  602. stack.append(ValueType(ValueType::I64));
  603. return {};
  604. }
  605. VALIDATE_INSTRUCTION(i64_or)
  606. {
  607. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  608. stack.append(ValueType(ValueType::I64));
  609. return {};
  610. }
  611. VALIDATE_INSTRUCTION(i64_xor)
  612. {
  613. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  614. stack.append(ValueType(ValueType::I64));
  615. return {};
  616. }
  617. VALIDATE_INSTRUCTION(i64_shl)
  618. {
  619. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  620. stack.append(ValueType(ValueType::I64));
  621. return {};
  622. }
  623. VALIDATE_INSTRUCTION(i64_shrs)
  624. {
  625. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  626. stack.append(ValueType(ValueType::I64));
  627. return {};
  628. }
  629. VALIDATE_INSTRUCTION(i64_shru)
  630. {
  631. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  632. stack.append(ValueType(ValueType::I64));
  633. return {};
  634. }
  635. VALIDATE_INSTRUCTION(i64_rotl)
  636. {
  637. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  638. stack.append(ValueType(ValueType::I64));
  639. return {};
  640. }
  641. VALIDATE_INSTRUCTION(i64_rotr)
  642. {
  643. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  644. stack.append(ValueType(ValueType::I64));
  645. return {};
  646. }
  647. VALIDATE_INSTRUCTION(f32_add)
  648. {
  649. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  650. stack.append(ValueType(ValueType::F32));
  651. return {};
  652. }
  653. VALIDATE_INSTRUCTION(f32_sub)
  654. {
  655. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  656. stack.append(ValueType(ValueType::F32));
  657. return {};
  658. }
  659. VALIDATE_INSTRUCTION(f32_mul)
  660. {
  661. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  662. stack.append(ValueType(ValueType::F32));
  663. return {};
  664. }
  665. VALIDATE_INSTRUCTION(f32_div)
  666. {
  667. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  668. stack.append(ValueType(ValueType::F32));
  669. return {};
  670. }
  671. VALIDATE_INSTRUCTION(f32_min)
  672. {
  673. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  674. stack.append(ValueType(ValueType::F32));
  675. return {};
  676. }
  677. VALIDATE_INSTRUCTION(f32_max)
  678. {
  679. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  680. stack.append(ValueType(ValueType::F32));
  681. return {};
  682. }
  683. VALIDATE_INSTRUCTION(f32_copysign)
  684. {
  685. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  686. stack.append(ValueType(ValueType::F32));
  687. return {};
  688. }
  689. VALIDATE_INSTRUCTION(f64_add)
  690. {
  691. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  692. stack.append(ValueType(ValueType::F64));
  693. return {};
  694. }
  695. VALIDATE_INSTRUCTION(f64_sub)
  696. {
  697. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  698. stack.append(ValueType(ValueType::F64));
  699. return {};
  700. }
  701. VALIDATE_INSTRUCTION(f64_mul)
  702. {
  703. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  704. stack.append(ValueType(ValueType::F64));
  705. return {};
  706. }
  707. VALIDATE_INSTRUCTION(f64_div)
  708. {
  709. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  710. stack.append(ValueType(ValueType::F64));
  711. return {};
  712. }
  713. VALIDATE_INSTRUCTION(f64_min)
  714. {
  715. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  716. stack.append(ValueType(ValueType::F64));
  717. return {};
  718. }
  719. VALIDATE_INSTRUCTION(f64_max)
  720. {
  721. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  722. stack.append(ValueType(ValueType::F64));
  723. return {};
  724. }
  725. VALIDATE_INSTRUCTION(f64_copysign)
  726. {
  727. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  728. stack.append(ValueType(ValueType::F64));
  729. return {};
  730. }
  731. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-testopmathittestop
  732. VALIDATE_INSTRUCTION(i32_eqz)
  733. {
  734. TRY((stack.take<ValueType::I32>()));
  735. stack.append(ValueType(ValueType::I32));
  736. return {};
  737. }
  738. VALIDATE_INSTRUCTION(i64_eqz)
  739. {
  740. TRY((stack.take<ValueType::I64>()));
  741. stack.append(ValueType(ValueType::I32));
  742. return {};
  743. }
  744. // https://webassembly.github.io/spec/core/bikeshed/#-tmathsfhrefsyntax-relopmathitrelop
  745. VALIDATE_INSTRUCTION(i32_eq)
  746. {
  747. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  748. stack.append(ValueType(ValueType::I32));
  749. return {};
  750. }
  751. VALIDATE_INSTRUCTION(i32_ne)
  752. {
  753. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  754. stack.append(ValueType(ValueType::I32));
  755. return {};
  756. }
  757. VALIDATE_INSTRUCTION(i32_lts)
  758. {
  759. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  760. stack.append(ValueType(ValueType::I32));
  761. return {};
  762. }
  763. VALIDATE_INSTRUCTION(i32_ltu)
  764. {
  765. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  766. stack.append(ValueType(ValueType::I32));
  767. return {};
  768. }
  769. VALIDATE_INSTRUCTION(i32_gts)
  770. {
  771. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  772. stack.append(ValueType(ValueType::I32));
  773. return {};
  774. }
  775. VALIDATE_INSTRUCTION(i32_gtu)
  776. {
  777. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  778. stack.append(ValueType(ValueType::I32));
  779. return {};
  780. }
  781. VALIDATE_INSTRUCTION(i32_les)
  782. {
  783. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  784. stack.append(ValueType(ValueType::I32));
  785. return {};
  786. }
  787. VALIDATE_INSTRUCTION(i32_leu)
  788. {
  789. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  790. stack.append(ValueType(ValueType::I32));
  791. return {};
  792. }
  793. VALIDATE_INSTRUCTION(i32_ges)
  794. {
  795. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  796. stack.append(ValueType(ValueType::I32));
  797. return {};
  798. }
  799. VALIDATE_INSTRUCTION(i32_geu)
  800. {
  801. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  802. stack.append(ValueType(ValueType::I32));
  803. return {};
  804. }
  805. VALIDATE_INSTRUCTION(i64_eq)
  806. {
  807. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  808. stack.append(ValueType(ValueType::I32));
  809. return {};
  810. }
  811. VALIDATE_INSTRUCTION(i64_ne)
  812. {
  813. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  814. stack.append(ValueType(ValueType::I32));
  815. return {};
  816. }
  817. VALIDATE_INSTRUCTION(i64_lts)
  818. {
  819. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  820. stack.append(ValueType(ValueType::I32));
  821. return {};
  822. }
  823. VALIDATE_INSTRUCTION(i64_ltu)
  824. {
  825. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  826. stack.append(ValueType(ValueType::I32));
  827. return {};
  828. }
  829. VALIDATE_INSTRUCTION(i64_gts)
  830. {
  831. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  832. stack.append(ValueType(ValueType::I32));
  833. return {};
  834. }
  835. VALIDATE_INSTRUCTION(i64_gtu)
  836. {
  837. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  838. stack.append(ValueType(ValueType::I32));
  839. return {};
  840. }
  841. VALIDATE_INSTRUCTION(i64_les)
  842. {
  843. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  844. stack.append(ValueType(ValueType::I32));
  845. return {};
  846. }
  847. VALIDATE_INSTRUCTION(i64_leu)
  848. {
  849. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  850. stack.append(ValueType(ValueType::I32));
  851. return {};
  852. }
  853. VALIDATE_INSTRUCTION(i64_ges)
  854. {
  855. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  856. stack.append(ValueType(ValueType::I32));
  857. return {};
  858. }
  859. VALIDATE_INSTRUCTION(i64_geu)
  860. {
  861. TRY((stack.take<ValueType::I64, ValueType::I64>()));
  862. stack.append(ValueType(ValueType::I32));
  863. return {};
  864. }
  865. VALIDATE_INSTRUCTION(f32_eq)
  866. {
  867. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  868. stack.append(ValueType(ValueType::I32));
  869. return {};
  870. }
  871. VALIDATE_INSTRUCTION(f32_ne)
  872. {
  873. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  874. stack.append(ValueType(ValueType::I32));
  875. return {};
  876. }
  877. VALIDATE_INSTRUCTION(f32_lt)
  878. {
  879. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  880. stack.append(ValueType(ValueType::I32));
  881. return {};
  882. }
  883. VALIDATE_INSTRUCTION(f32_le)
  884. {
  885. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  886. stack.append(ValueType(ValueType::I32));
  887. return {};
  888. }
  889. VALIDATE_INSTRUCTION(f32_gt)
  890. {
  891. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  892. stack.append(ValueType(ValueType::I32));
  893. return {};
  894. }
  895. VALIDATE_INSTRUCTION(f32_ge)
  896. {
  897. TRY((stack.take<ValueType::F32, ValueType::F32>()));
  898. stack.append(ValueType(ValueType::I32));
  899. return {};
  900. }
  901. VALIDATE_INSTRUCTION(f64_eq)
  902. {
  903. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  904. stack.append(ValueType(ValueType::I32));
  905. return {};
  906. }
  907. VALIDATE_INSTRUCTION(f64_ne)
  908. {
  909. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  910. stack.append(ValueType(ValueType::I32));
  911. return {};
  912. }
  913. VALIDATE_INSTRUCTION(f64_lt)
  914. {
  915. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  916. stack.append(ValueType(ValueType::I32));
  917. return {};
  918. }
  919. VALIDATE_INSTRUCTION(f64_le)
  920. {
  921. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  922. stack.append(ValueType(ValueType::I32));
  923. return {};
  924. }
  925. VALIDATE_INSTRUCTION(f64_gt)
  926. {
  927. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  928. stack.append(ValueType(ValueType::I32));
  929. return {};
  930. }
  931. VALIDATE_INSTRUCTION(f64_ge)
  932. {
  933. TRY((stack.take<ValueType::F64, ValueType::F64>()));
  934. stack.append(ValueType(ValueType::I32));
  935. return {};
  936. }
  937. // https://webassembly.github.io/spec/core/bikeshed/#-t_2mathsfhrefsyntax-cvtopmathitcvtopmathsf_t_1mathsf_hrefsyntax-sxmathitsx
  938. VALIDATE_INSTRUCTION(i32_wrap_i64)
  939. {
  940. TRY(stack.take<ValueType::I64>());
  941. stack.append(ValueType(ValueType::I32));
  942. return {};
  943. }
  944. VALIDATE_INSTRUCTION(i64_extend_si32)
  945. {
  946. TRY(stack.take<ValueType::I32>());
  947. stack.append(ValueType(ValueType::I64));
  948. return {};
  949. }
  950. VALIDATE_INSTRUCTION(i64_extend_ui32)
  951. {
  952. TRY(stack.take<ValueType::I32>());
  953. stack.append(ValueType(ValueType::I64));
  954. return {};
  955. }
  956. VALIDATE_INSTRUCTION(i32_trunc_sf32)
  957. {
  958. TRY(stack.take<ValueType::F32>());
  959. stack.append(ValueType(ValueType::I32));
  960. return {};
  961. }
  962. VALIDATE_INSTRUCTION(i32_trunc_uf32)
  963. {
  964. TRY(stack.take<ValueType::F32>());
  965. stack.append(ValueType(ValueType::I32));
  966. return {};
  967. }
  968. VALIDATE_INSTRUCTION(i32_trunc_sf64)
  969. {
  970. TRY(stack.take<ValueType::F64>());
  971. stack.append(ValueType(ValueType::I32));
  972. return {};
  973. }
  974. VALIDATE_INSTRUCTION(i32_trunc_uf64)
  975. {
  976. TRY(stack.take<ValueType::F64>());
  977. stack.append(ValueType(ValueType::I32));
  978. return {};
  979. }
  980. VALIDATE_INSTRUCTION(i64_trunc_sf32)
  981. {
  982. TRY(stack.take<ValueType::F32>());
  983. stack.append(ValueType(ValueType::I64));
  984. return {};
  985. }
  986. VALIDATE_INSTRUCTION(i64_trunc_uf32)
  987. {
  988. TRY(stack.take<ValueType::F32>());
  989. stack.append(ValueType(ValueType::I64));
  990. return {};
  991. }
  992. VALIDATE_INSTRUCTION(i64_trunc_sf64)
  993. {
  994. TRY(stack.take<ValueType::F64>());
  995. stack.append(ValueType(ValueType::I64));
  996. return {};
  997. }
  998. VALIDATE_INSTRUCTION(i64_trunc_uf64)
  999. {
  1000. TRY(stack.take<ValueType::F64>());
  1001. stack.append(ValueType(ValueType::I64));
  1002. return {};
  1003. }
  1004. VALIDATE_INSTRUCTION(i32_trunc_sat_f32_s)
  1005. {
  1006. TRY(stack.take<ValueType::F32>());
  1007. stack.append(ValueType(ValueType::I32));
  1008. return {};
  1009. }
  1010. VALIDATE_INSTRUCTION(i32_trunc_sat_f32_u)
  1011. {
  1012. TRY(stack.take<ValueType::F32>());
  1013. stack.append(ValueType(ValueType::I32));
  1014. return {};
  1015. }
  1016. VALIDATE_INSTRUCTION(i32_trunc_sat_f64_s)
  1017. {
  1018. TRY(stack.take<ValueType::F64>());
  1019. stack.append(ValueType(ValueType::I32));
  1020. return {};
  1021. }
  1022. VALIDATE_INSTRUCTION(i32_trunc_sat_f64_u)
  1023. {
  1024. TRY(stack.take<ValueType::F64>());
  1025. stack.append(ValueType(ValueType::I32));
  1026. return {};
  1027. }
  1028. VALIDATE_INSTRUCTION(i64_trunc_sat_f32_s)
  1029. {
  1030. TRY(stack.take<ValueType::F32>());
  1031. stack.append(ValueType(ValueType::I64));
  1032. return {};
  1033. }
  1034. VALIDATE_INSTRUCTION(i64_trunc_sat_f32_u)
  1035. {
  1036. TRY(stack.take<ValueType::F32>());
  1037. stack.append(ValueType(ValueType::I64));
  1038. return {};
  1039. }
  1040. VALIDATE_INSTRUCTION(i64_trunc_sat_f64_s)
  1041. {
  1042. TRY(stack.take<ValueType::F64>());
  1043. stack.append(ValueType(ValueType::I64));
  1044. return {};
  1045. }
  1046. VALIDATE_INSTRUCTION(i64_trunc_sat_f64_u)
  1047. {
  1048. TRY(stack.take<ValueType::F64>());
  1049. stack.append(ValueType(ValueType::I64));
  1050. return {};
  1051. }
  1052. VALIDATE_INSTRUCTION(f32_convert_si32)
  1053. {
  1054. TRY(stack.take<ValueType::I32>());
  1055. stack.append(ValueType(ValueType::F32));
  1056. return {};
  1057. }
  1058. VALIDATE_INSTRUCTION(f32_convert_ui32)
  1059. {
  1060. TRY(stack.take<ValueType::I32>());
  1061. stack.append(ValueType(ValueType::F32));
  1062. return {};
  1063. }
  1064. VALIDATE_INSTRUCTION(f32_convert_si64)
  1065. {
  1066. TRY(stack.take<ValueType::I64>());
  1067. stack.append(ValueType(ValueType::F32));
  1068. return {};
  1069. }
  1070. VALIDATE_INSTRUCTION(f32_convert_ui64)
  1071. {
  1072. TRY(stack.take<ValueType::I64>());
  1073. stack.append(ValueType(ValueType::F32));
  1074. return {};
  1075. }
  1076. VALIDATE_INSTRUCTION(f64_convert_si32)
  1077. {
  1078. TRY(stack.take<ValueType::I32>());
  1079. stack.append(ValueType(ValueType::F64));
  1080. return {};
  1081. }
  1082. VALIDATE_INSTRUCTION(f64_convert_ui32)
  1083. {
  1084. TRY(stack.take<ValueType::I32>());
  1085. stack.append(ValueType(ValueType::F64));
  1086. return {};
  1087. }
  1088. VALIDATE_INSTRUCTION(f64_convert_si64)
  1089. {
  1090. TRY(stack.take<ValueType::I64>());
  1091. stack.append(ValueType(ValueType::F64));
  1092. return {};
  1093. }
  1094. VALIDATE_INSTRUCTION(f64_convert_ui64)
  1095. {
  1096. TRY(stack.take<ValueType::I64>());
  1097. stack.append(ValueType(ValueType::F64));
  1098. return {};
  1099. }
  1100. VALIDATE_INSTRUCTION(f32_demote_f64)
  1101. {
  1102. TRY(stack.take<ValueType::F64>());
  1103. stack.append(ValueType(ValueType::F32));
  1104. return {};
  1105. }
  1106. VALIDATE_INSTRUCTION(f64_promote_f32)
  1107. {
  1108. TRY(stack.take<ValueType::F32>());
  1109. stack.append(ValueType(ValueType::F64));
  1110. return {};
  1111. }
  1112. VALIDATE_INSTRUCTION(f32_reinterpret_i32)
  1113. {
  1114. TRY(stack.take<ValueType::I32>());
  1115. stack.append(ValueType(ValueType::F32));
  1116. return {};
  1117. }
  1118. VALIDATE_INSTRUCTION(f64_reinterpret_i64)
  1119. {
  1120. TRY(stack.take<ValueType::I64>());
  1121. stack.append(ValueType(ValueType::F64));
  1122. return {};
  1123. }
  1124. VALIDATE_INSTRUCTION(i32_reinterpret_f32)
  1125. {
  1126. TRY(stack.take<ValueType::F32>());
  1127. stack.append(ValueType(ValueType::I32));
  1128. return {};
  1129. }
  1130. VALIDATE_INSTRUCTION(i64_reinterpret_f64)
  1131. {
  1132. TRY(stack.take<ValueType::F64>());
  1133. stack.append(ValueType(ValueType::I64));
  1134. return {};
  1135. }
  1136. // https://webassembly.github.io/spec/core/bikeshed/#reference-instructions%E2%91%A2
  1137. VALIDATE_INSTRUCTION(ref_null)
  1138. {
  1139. is_constant = true;
  1140. stack.append(instruction.arguments().get<ValueType>());
  1141. return {};
  1142. }
  1143. VALIDATE_INSTRUCTION(ref_is_null)
  1144. {
  1145. if (stack.is_empty() || !stack.last().is_reference())
  1146. return Errors::invalid_stack_state(stack, Tuple { "reference" });
  1147. stack.take_last();
  1148. stack.append(ValueType(ValueType::I32));
  1149. return {};
  1150. }
  1151. VALIDATE_INSTRUCTION(ref_func)
  1152. {
  1153. auto index = instruction.arguments().get<FunctionIndex>();
  1154. TRY(validate(index));
  1155. if (!m_context.references.contains(index))
  1156. return Errors::invalid("function reference"sv);
  1157. is_constant = true;
  1158. stack.append(ValueType(ValueType::FunctionReference));
  1159. return {};
  1160. }
  1161. // https://webassembly.github.io/spec/core/bikeshed/#parametric-instructions%E2%91%A2
  1162. VALIDATE_INSTRUCTION(drop)
  1163. {
  1164. if (stack.is_empty())
  1165. return Errors::invalid_stack_state(stack, Tuple { "any" });
  1166. stack.take_last();
  1167. return {};
  1168. }
  1169. VALIDATE_INSTRUCTION(select)
  1170. {
  1171. if (stack.size() < 3)
  1172. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32), "any", "any" });
  1173. auto index_type = stack.take_last();
  1174. auto arg0_type = stack.take_last();
  1175. auto& arg1_type = stack.last();
  1176. if (!index_type.is_of_kind(ValueType::I32))
  1177. return Errors::invalid("select index type"sv, ValueType(ValueType::I32), index_type);
  1178. if (arg0_type != arg1_type)
  1179. return Errors::invalid("select argument types"sv, Vector { arg0_type, arg0_type }, Vector { arg0_type, arg1_type });
  1180. return {};
  1181. }
  1182. VALIDATE_INSTRUCTION(select_typed)
  1183. {
  1184. auto& required_types = instruction.arguments().get<Vector<ValueType>>();
  1185. if (required_types.size() != 1)
  1186. return Errors::invalid("select types"sv, "exactly one type"sv, required_types);
  1187. if (stack.size() < 3)
  1188. return Errors::invalid_stack_state(stack, Tuple { ValueType(ValueType::I32), required_types.first(), required_types.first() });
  1189. auto index_type = stack.take_last();
  1190. auto arg0_type = stack.take_last();
  1191. auto& arg1_type = stack.last();
  1192. if (!index_type.is_of_kind(ValueType::I32))
  1193. return Errors::invalid("select index type"sv, ValueType(ValueType::I32), index_type);
  1194. if (arg0_type != arg1_type || arg0_type != required_types.first())
  1195. return Errors::invalid("select argument types"sv, Vector { required_types.first(), required_types.first() }, Vector { arg0_type, arg1_type });
  1196. return {};
  1197. }
  1198. // https://webassembly.github.io/spec/core/bikeshed/#variable-instructions%E2%91%A2
  1199. VALIDATE_INSTRUCTION(local_get)
  1200. {
  1201. auto index = instruction.arguments().get<LocalIndex>();
  1202. TRY(validate(index));
  1203. stack.append(m_context.locals[index.value()]);
  1204. return {};
  1205. }
  1206. VALIDATE_INSTRUCTION(local_set)
  1207. {
  1208. auto index = instruction.arguments().get<LocalIndex>();
  1209. TRY(validate(index));
  1210. auto& value_type = m_context.locals[index.value()];
  1211. TRY(stack.take(value_type));
  1212. return {};
  1213. }
  1214. VALIDATE_INSTRUCTION(local_tee)
  1215. {
  1216. auto index = instruction.arguments().get<LocalIndex>();
  1217. TRY(validate(index));
  1218. auto& value_type = m_context.locals[index.value()];
  1219. TRY(stack.take(value_type));
  1220. stack.append(value_type);
  1221. return {};
  1222. }
  1223. VALIDATE_INSTRUCTION(global_get)
  1224. {
  1225. auto index = instruction.arguments().get<GlobalIndex>();
  1226. TRY(validate(index));
  1227. auto& global = m_context.globals[index.value()];
  1228. is_constant = !global.is_mutable();
  1229. stack.append(global.type());
  1230. return {};
  1231. }
  1232. VALIDATE_INSTRUCTION(global_set)
  1233. {
  1234. auto index = instruction.arguments().get<GlobalIndex>();
  1235. TRY(validate(index));
  1236. auto& global = m_context.globals[index.value()];
  1237. if (!global.is_mutable())
  1238. return Errors::invalid("global variable for global.set"sv);
  1239. TRY(stack.take(global.type()));
  1240. return {};
  1241. }
  1242. // https://webassembly.github.io/spec/core/bikeshed/#table-instructions%E2%91%A2
  1243. VALIDATE_INSTRUCTION(table_get)
  1244. {
  1245. auto index = instruction.arguments().get<TableIndex>();
  1246. TRY(validate(index));
  1247. auto& table = m_context.tables[index.value()];
  1248. TRY(stack.take<ValueType::I32>());
  1249. stack.append(table.element_type());
  1250. return {};
  1251. }
  1252. VALIDATE_INSTRUCTION(table_set)
  1253. {
  1254. auto index = instruction.arguments().get<TableIndex>();
  1255. TRY(validate(index));
  1256. auto& table = m_context.tables[index.value()];
  1257. TRY(stack.take(table.element_type()));
  1258. TRY(stack.take<ValueType::I32>());
  1259. return {};
  1260. }
  1261. VALIDATE_INSTRUCTION(table_size)
  1262. {
  1263. auto index = instruction.arguments().get<TableIndex>();
  1264. TRY(validate(index));
  1265. stack.append(ValueType(ValueType::I32));
  1266. return {};
  1267. }
  1268. VALIDATE_INSTRUCTION(table_grow)
  1269. {
  1270. auto index = instruction.arguments().get<TableIndex>();
  1271. TRY(validate(index));
  1272. auto& table = m_context.tables[index.value()];
  1273. TRY(stack.take<ValueType::I32>());
  1274. TRY(stack.take(table.element_type()));
  1275. stack.append(ValueType(ValueType::I32));
  1276. return {};
  1277. }
  1278. VALIDATE_INSTRUCTION(table_fill)
  1279. {
  1280. auto index = instruction.arguments().get<TableIndex>();
  1281. TRY(validate(index));
  1282. auto& table = m_context.tables[index.value()];
  1283. TRY(stack.take<ValueType::I32>());
  1284. TRY(stack.take(table.element_type()));
  1285. TRY(stack.take<ValueType::I32>());
  1286. return {};
  1287. }
  1288. VALIDATE_INSTRUCTION(table_copy)
  1289. {
  1290. auto& args = instruction.arguments().get<Instruction::TableTableArgs>();
  1291. TRY(validate(args.lhs));
  1292. TRY(validate(args.rhs));
  1293. auto& lhs_table = m_context.tables[args.lhs.value()];
  1294. auto& rhs_table = m_context.tables[args.rhs.value()];
  1295. if (lhs_table.element_type() != rhs_table.element_type())
  1296. return Errors::non_conforming_types("table.copy"sv, lhs_table.element_type(), rhs_table.element_type());
  1297. if (!lhs_table.element_type().is_reference())
  1298. return Errors::invalid("table.copy element type"sv, "a reference type"sv, lhs_table.element_type());
  1299. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1300. return {};
  1301. }
  1302. VALIDATE_INSTRUCTION(table_init)
  1303. {
  1304. auto& args = instruction.arguments().get<Instruction::TableElementArgs>();
  1305. TRY(validate(args.table_index));
  1306. TRY(validate(args.element_index));
  1307. auto& table = m_context.tables[args.table_index.value()];
  1308. auto& element_type = m_context.elements[args.element_index.value()];
  1309. if (table.element_type() != element_type)
  1310. return Errors::non_conforming_types("table.init"sv, table.element_type(), element_type);
  1311. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1312. return {};
  1313. }
  1314. VALIDATE_INSTRUCTION(elem_drop)
  1315. {
  1316. auto index = instruction.arguments().get<ElementIndex>();
  1317. TRY(validate(index));
  1318. return {};
  1319. }
  1320. // https://webassembly.github.io/spec/core/bikeshed/#memory-instructions%E2%91%A2
  1321. VALIDATE_INSTRUCTION(i32_load)
  1322. {
  1323. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1324. TRY(validate(arg.memory_index));
  1325. if ((1ull << arg.align) > sizeof(i32))
  1326. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i32));
  1327. TRY((stack.take<ValueType::I32>()));
  1328. stack.append(ValueType(ValueType::I32));
  1329. return {};
  1330. }
  1331. VALIDATE_INSTRUCTION(i64_load)
  1332. {
  1333. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1334. TRY(validate(arg.memory_index));
  1335. if ((1ull << arg.align) > sizeof(i64))
  1336. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i64));
  1337. TRY((stack.take<ValueType::I32>()));
  1338. stack.append(ValueType(ValueType::I64));
  1339. return {};
  1340. }
  1341. VALIDATE_INSTRUCTION(f32_load)
  1342. {
  1343. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1344. TRY(validate(arg.memory_index));
  1345. if ((1ull << arg.align) > sizeof(float))
  1346. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(float));
  1347. TRY((stack.take<ValueType::I32>()));
  1348. stack.append(ValueType(ValueType::F32));
  1349. return {};
  1350. }
  1351. VALIDATE_INSTRUCTION(f64_load)
  1352. {
  1353. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1354. TRY(validate(arg.memory_index));
  1355. if ((1ull << arg.align) > sizeof(double))
  1356. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(double));
  1357. TRY((stack.take<ValueType::I32>()));
  1358. stack.append(ValueType(ValueType::F64));
  1359. return {};
  1360. }
  1361. VALIDATE_INSTRUCTION(i32_load16_s)
  1362. {
  1363. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1364. TRY(validate(arg.memory_index));
  1365. if ((1ull << arg.align) > 16 / 8)
  1366. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
  1367. TRY((stack.take<ValueType::I32>()));
  1368. stack.append(ValueType(ValueType::I32));
  1369. return {};
  1370. }
  1371. VALIDATE_INSTRUCTION(i32_load16_u)
  1372. {
  1373. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1374. TRY(validate(arg.memory_index));
  1375. if ((1ull << arg.align) > 16 / 8)
  1376. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
  1377. TRY((stack.take<ValueType::I32>()));
  1378. stack.append(ValueType(ValueType::I32));
  1379. return {};
  1380. }
  1381. VALIDATE_INSTRUCTION(i32_load8_s)
  1382. {
  1383. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1384. TRY(validate(arg.memory_index));
  1385. if ((1ull << arg.align) > 8 / 8)
  1386. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
  1387. TRY((stack.take<ValueType::I32>()));
  1388. stack.append(ValueType(ValueType::I32));
  1389. return {};
  1390. }
  1391. VALIDATE_INSTRUCTION(i32_load8_u)
  1392. {
  1393. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1394. TRY(validate(arg.memory_index));
  1395. if ((1ull << arg.align) > 8 / 8)
  1396. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
  1397. TRY((stack.take<ValueType::I32>()));
  1398. stack.append(ValueType(ValueType::I32));
  1399. return {};
  1400. }
  1401. VALIDATE_INSTRUCTION(i64_load32_s)
  1402. {
  1403. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1404. TRY(validate(arg.memory_index));
  1405. if ((1ull << arg.align) > 32 / 8)
  1406. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 32 / 8);
  1407. TRY((stack.take<ValueType::I32>()));
  1408. stack.append(ValueType(ValueType::I64));
  1409. return {};
  1410. }
  1411. VALIDATE_INSTRUCTION(i64_load32_u)
  1412. {
  1413. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1414. TRY(validate(arg.memory_index));
  1415. if ((1ull << arg.align) > 32 / 8)
  1416. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 32 / 8);
  1417. TRY((stack.take<ValueType::I32>()));
  1418. stack.append(ValueType(ValueType::I64));
  1419. return {};
  1420. }
  1421. VALIDATE_INSTRUCTION(i64_load16_s)
  1422. {
  1423. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1424. TRY(validate(arg.memory_index));
  1425. if ((1ull << arg.align) > 16 / 8)
  1426. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
  1427. TRY((stack.take<ValueType::I32>()));
  1428. stack.append(ValueType(ValueType::I64));
  1429. return {};
  1430. }
  1431. VALIDATE_INSTRUCTION(i64_load16_u)
  1432. {
  1433. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1434. TRY(validate(arg.memory_index));
  1435. if ((1ull << arg.align) > 16 / 8)
  1436. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
  1437. TRY((stack.take<ValueType::I32>()));
  1438. stack.append(ValueType(ValueType::I64));
  1439. return {};
  1440. }
  1441. VALIDATE_INSTRUCTION(i64_load8_s)
  1442. {
  1443. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1444. TRY(validate(arg.memory_index));
  1445. if ((1ull << arg.align) > 8 / 8)
  1446. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
  1447. TRY((stack.take<ValueType::I32>()));
  1448. stack.append(ValueType(ValueType::I64));
  1449. return {};
  1450. }
  1451. VALIDATE_INSTRUCTION(i64_load8_u)
  1452. {
  1453. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1454. TRY(validate(arg.memory_index));
  1455. if ((1ull << arg.align) > 8 / 8)
  1456. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
  1457. TRY((stack.take<ValueType::I32>()));
  1458. stack.append(ValueType(ValueType::I64));
  1459. return {};
  1460. }
  1461. VALIDATE_INSTRUCTION(i32_store)
  1462. {
  1463. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1464. TRY(validate(arg.memory_index));
  1465. if ((1ull << arg.align) > sizeof(i32))
  1466. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i32));
  1467. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1468. return {};
  1469. }
  1470. VALIDATE_INSTRUCTION(i64_store)
  1471. {
  1472. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1473. TRY(validate(arg.memory_index));
  1474. if ((1ull << arg.align) > sizeof(i64))
  1475. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(i64));
  1476. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1477. return {};
  1478. }
  1479. VALIDATE_INSTRUCTION(f32_store)
  1480. {
  1481. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1482. TRY(validate(arg.memory_index));
  1483. if ((1ull << arg.align) > sizeof(float))
  1484. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(float));
  1485. TRY((stack.take<ValueType::F32, ValueType::I32>()));
  1486. return {};
  1487. }
  1488. VALIDATE_INSTRUCTION(f64_store)
  1489. {
  1490. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1491. TRY(validate(arg.memory_index));
  1492. if ((1ull << arg.align) > sizeof(double))
  1493. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(double));
  1494. TRY((stack.take<ValueType::F64, ValueType::I32>()));
  1495. return {};
  1496. }
  1497. VALIDATE_INSTRUCTION(i32_store16)
  1498. {
  1499. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1500. TRY(validate(arg.memory_index));
  1501. if ((1ull << arg.align) > 16 / 8)
  1502. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
  1503. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1504. return {};
  1505. }
  1506. VALIDATE_INSTRUCTION(i32_store8)
  1507. {
  1508. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1509. TRY(validate(arg.memory_index));
  1510. if ((1ull << arg.align) > 8 / 8)
  1511. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
  1512. TRY((stack.take<ValueType::I32, ValueType::I32>()));
  1513. return {};
  1514. }
  1515. VALIDATE_INSTRUCTION(i64_store32)
  1516. {
  1517. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1518. TRY(validate(arg.memory_index));
  1519. if ((1ull << arg.align) > 32 / 8)
  1520. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 32 / 8);
  1521. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1522. return {};
  1523. }
  1524. VALIDATE_INSTRUCTION(i64_store16)
  1525. {
  1526. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1527. TRY(validate(arg.memory_index));
  1528. if ((1ull << arg.align) > 16 / 8)
  1529. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 16 / 8);
  1530. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1531. return {};
  1532. }
  1533. VALIDATE_INSTRUCTION(i64_store8)
  1534. {
  1535. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1536. TRY(validate(arg.memory_index));
  1537. if ((1ull << arg.align) > 8 / 8)
  1538. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, 8 / 8);
  1539. TRY((stack.take<ValueType::I64, ValueType::I32>()));
  1540. return {};
  1541. }
  1542. VALIDATE_INSTRUCTION(memory_size)
  1543. {
  1544. TRY(validate(instruction.arguments().get<Instruction::MemoryIndexArgument>().memory_index));
  1545. stack.append(ValueType(ValueType::I32));
  1546. return {};
  1547. }
  1548. VALIDATE_INSTRUCTION(memory_grow)
  1549. {
  1550. TRY(validate(instruction.arguments().get<Instruction::MemoryIndexArgument>().memory_index));
  1551. TRY((stack.take<ValueType::I32>()));
  1552. stack.append(ValueType(ValueType::I32));
  1553. return {};
  1554. }
  1555. VALIDATE_INSTRUCTION(memory_fill)
  1556. {
  1557. TRY(validate(instruction.arguments().get<Instruction::MemoryIndexArgument>().memory_index));
  1558. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1559. return {};
  1560. }
  1561. VALIDATE_INSTRUCTION(memory_copy)
  1562. {
  1563. auto& args = instruction.arguments().get<Instruction::MemoryCopyArgs>();
  1564. TRY(validate(args.src_index));
  1565. TRY(validate(args.dst_index));
  1566. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1567. return {};
  1568. }
  1569. VALIDATE_INSTRUCTION(memory_init)
  1570. {
  1571. auto& args = instruction.arguments().get<Instruction::MemoryInitArgs>();
  1572. TRY(validate(args.memory_index));
  1573. TRY(validate(args.data_index));
  1574. TRY((stack.take<ValueType::I32, ValueType::I32, ValueType::I32>()));
  1575. return {};
  1576. }
  1577. VALIDATE_INSTRUCTION(data_drop)
  1578. {
  1579. auto index = instruction.arguments().get<DataIndex>();
  1580. TRY(validate(index));
  1581. return {};
  1582. }
  1583. // https://webassembly.github.io/spec/core/bikeshed/#control-instructions%E2%91%A2
  1584. VALIDATE_INSTRUCTION(nop)
  1585. {
  1586. return {};
  1587. }
  1588. VALIDATE_INSTRUCTION(unreachable)
  1589. {
  1590. // https://webassembly.github.io/spec/core/bikeshed/#polymorphism
  1591. stack.append(StackEntry());
  1592. return {};
  1593. }
  1594. // Note: This is responsible for _all_ structured instructions, and is *not* from the spec.
  1595. VALIDATE_INSTRUCTION(structured_end)
  1596. {
  1597. if (m_entered_scopes.is_empty())
  1598. return Errors::invalid("usage of structured end"sv);
  1599. auto last_scope = m_entered_scopes.take_last();
  1600. m_context = m_parent_contexts.take_last();
  1601. auto last_block_type = m_entered_blocks.take_last();
  1602. switch (last_scope) {
  1603. case ChildScopeKind::Block:
  1604. case ChildScopeKind::IfWithoutElse:
  1605. case ChildScopeKind::Else:
  1606. m_block_details.take_last();
  1607. break;
  1608. case ChildScopeKind::IfWithElse:
  1609. return Errors::invalid("usage of if without an else clause that appears to have one anyway"sv);
  1610. }
  1611. auto& results = last_block_type.results();
  1612. for (size_t i = 1; i <= results.size(); ++i)
  1613. TRY(stack.take(results[results.size() - i]));
  1614. for (auto& result : results)
  1615. stack.append(result);
  1616. return {};
  1617. }
  1618. // Note: This is *not* from the spec.
  1619. VALIDATE_INSTRUCTION(structured_else)
  1620. {
  1621. if (m_entered_scopes.is_empty())
  1622. return Errors::invalid("usage of structured else"sv);
  1623. if (m_entered_scopes.last() != ChildScopeKind::IfWithElse)
  1624. return Errors::invalid("usage of structured else"sv);
  1625. auto& block_type = m_entered_blocks.last();
  1626. auto& results = block_type.results();
  1627. for (size_t i = 1; i <= results.size(); ++i)
  1628. TRY(stack.take(results[results.size() - i]));
  1629. auto& details = m_block_details.last().details.get<BlockDetails::IfDetails>();
  1630. m_entered_scopes.last() = ChildScopeKind::Else;
  1631. stack = move(details.initial_stack);
  1632. return {};
  1633. }
  1634. VALIDATE_INSTRUCTION(block)
  1635. {
  1636. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1637. auto block_type = TRY(validate(args.block_type));
  1638. auto& parameters = block_type.parameters();
  1639. for (size_t i = 1; i <= parameters.size(); ++i)
  1640. TRY(stack.take(parameters[parameters.size() - i]));
  1641. for (auto& parameter : parameters)
  1642. stack.append(parameter);
  1643. m_entered_scopes.append(ChildScopeKind::Block);
  1644. m_block_details.empend(stack.actual_size(), Empty {});
  1645. m_parent_contexts.append(m_context);
  1646. m_entered_blocks.append(block_type);
  1647. m_context.labels.prepend(ResultType { block_type.results() });
  1648. return {};
  1649. }
  1650. VALIDATE_INSTRUCTION(loop)
  1651. {
  1652. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1653. auto block_type = TRY(validate(args.block_type));
  1654. auto& parameters = block_type.parameters();
  1655. for (size_t i = 1; i <= parameters.size(); ++i)
  1656. TRY(stack.take(parameters[parameters.size() - i]));
  1657. for (auto& parameter : parameters)
  1658. stack.append(parameter);
  1659. m_entered_scopes.append(ChildScopeKind::Block);
  1660. m_block_details.empend(stack.actual_size(), Empty {});
  1661. m_parent_contexts.append(m_context);
  1662. m_entered_blocks.append(block_type);
  1663. m_context.labels.prepend(ResultType { block_type.parameters() });
  1664. return {};
  1665. }
  1666. VALIDATE_INSTRUCTION(if_)
  1667. {
  1668. auto& args = instruction.arguments().get<Instruction::StructuredInstructionArgs>();
  1669. auto block_type = TRY(validate(args.block_type));
  1670. TRY(stack.take<ValueType::I32>());
  1671. auto stack_snapshot = stack;
  1672. auto& parameters = block_type.parameters();
  1673. for (size_t i = 1; i <= parameters.size(); ++i)
  1674. TRY(stack.take(parameters[parameters.size() - i]));
  1675. for (auto& parameter : parameters)
  1676. stack.append(parameter);
  1677. m_entered_scopes.append(args.else_ip.has_value() ? ChildScopeKind::IfWithElse : ChildScopeKind::IfWithoutElse);
  1678. m_block_details.empend(stack.actual_size(), BlockDetails::IfDetails { move(stack_snapshot) });
  1679. m_parent_contexts.append(m_context);
  1680. m_entered_blocks.append(block_type);
  1681. m_context.labels.prepend(ResultType { block_type.results() });
  1682. return {};
  1683. }
  1684. VALIDATE_INSTRUCTION(br)
  1685. {
  1686. auto label = instruction.arguments().get<LabelIndex>();
  1687. TRY(validate(label));
  1688. auto& type = m_context.labels[label.value()];
  1689. for (size_t i = 1; i <= type.types().size(); ++i)
  1690. TRY(stack.take(type.types()[type.types().size() - i]));
  1691. stack.append(StackEntry());
  1692. return {};
  1693. }
  1694. VALIDATE_INSTRUCTION(br_if)
  1695. {
  1696. auto label = instruction.arguments().get<LabelIndex>();
  1697. TRY(validate(label));
  1698. TRY(stack.take<ValueType::I32>());
  1699. auto& type = m_context.labels[label.value()];
  1700. Vector<StackEntry> entries;
  1701. entries.ensure_capacity(type.types().size());
  1702. for (size_t i = 0; i < type.types().size(); ++i) {
  1703. auto& entry = type.types()[type.types().size() - i - 1];
  1704. TRY(stack.take(entry));
  1705. entries.append(entry);
  1706. }
  1707. for (size_t i = 0; i < entries.size(); ++i)
  1708. stack.append(entries[entries.size() - i - 1]);
  1709. return {};
  1710. }
  1711. VALIDATE_INSTRUCTION(br_table)
  1712. {
  1713. auto& args = instruction.arguments().get<Instruction::TableBranchArgs>();
  1714. TRY(validate(args.default_));
  1715. for (auto& label : args.labels)
  1716. TRY(validate(label));
  1717. TRY(stack.take<ValueType::I32>());
  1718. auto& default_types = m_context.labels[args.default_.value()].types();
  1719. auto arity = default_types.size();
  1720. auto stack_snapshot = stack;
  1721. auto stack_to_check = stack_snapshot;
  1722. for (auto& label : args.labels) {
  1723. auto& label_types = m_context.labels[label.value()].types();
  1724. if (label_types.size() != arity)
  1725. return Errors::invalid("br_table label arity mismatch"sv);
  1726. for (size_t i = 0; i < arity; ++i)
  1727. TRY(stack_to_check.take(label_types[label_types.size() - i - 1]));
  1728. stack_to_check = stack_snapshot;
  1729. }
  1730. for (size_t i = 0; i < arity; ++i) {
  1731. auto expected = default_types[default_types.size() - i - 1];
  1732. TRY((stack.take(expected)));
  1733. }
  1734. stack.append(StackEntry());
  1735. return {};
  1736. }
  1737. VALIDATE_INSTRUCTION(return_)
  1738. {
  1739. if (!m_context.return_.has_value())
  1740. return Errors::invalid("use of return outside function"sv);
  1741. auto& return_types = m_context.return_->types();
  1742. for (size_t i = 0; i < return_types.size(); ++i)
  1743. TRY((stack.take(return_types[return_types.size() - i - 1])));
  1744. stack.append(StackEntry());
  1745. return {};
  1746. }
  1747. VALIDATE_INSTRUCTION(call)
  1748. {
  1749. auto index = instruction.arguments().get<FunctionIndex>();
  1750. TRY(validate(index));
  1751. auto& function_type = m_context.functions[index.value()];
  1752. for (size_t i = 0; i < function_type.parameters().size(); ++i)
  1753. TRY(stack.take(function_type.parameters()[function_type.parameters().size() - i - 1]));
  1754. for (auto& type : function_type.results())
  1755. stack.append(type);
  1756. return {};
  1757. }
  1758. VALIDATE_INSTRUCTION(call_indirect)
  1759. {
  1760. auto& args = instruction.arguments().get<Instruction::IndirectCallArgs>();
  1761. TRY(validate(args.table));
  1762. TRY(validate(args.type));
  1763. auto& table = m_context.tables[args.table.value()];
  1764. if (!table.element_type().is_reference())
  1765. return Errors::invalid("table element type for call.indirect"sv, "a reference type"sv, table.element_type());
  1766. auto& type = m_context.types[args.type.value()];
  1767. TRY(stack.take<ValueType::I32>());
  1768. for (size_t i = 0; i < type.parameters().size(); ++i)
  1769. TRY(stack.take(type.parameters()[type.parameters().size() - i - 1]));
  1770. for (auto& type : type.results())
  1771. stack.append(type);
  1772. return {};
  1773. }
  1774. VALIDATE_INSTRUCTION(v128_load)
  1775. {
  1776. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1777. TRY(validate(arg.memory_index));
  1778. if ((1ull << arg.align) > sizeof(u128))
  1779. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(u128));
  1780. TRY((stack.take_and_put<ValueType::I32>(ValueType::V128)));
  1781. return {};
  1782. }
  1783. VALIDATE_INSTRUCTION(v128_load8x8_s)
  1784. {
  1785. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1786. constexpr auto N = 8;
  1787. constexpr auto M = 8;
  1788. constexpr auto max_alignment = N * M / 8;
  1789. TRY(validate(arg.memory_index));
  1790. if ((1 << arg.align) > max_alignment)
  1791. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1792. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1793. }
  1794. VALIDATE_INSTRUCTION(v128_load8x8_u)
  1795. {
  1796. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1797. constexpr auto N = 8;
  1798. constexpr auto M = 8;
  1799. constexpr auto max_alignment = N * M / 8;
  1800. TRY(validate(arg.memory_index));
  1801. if ((1 << arg.align) > max_alignment)
  1802. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1803. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1804. }
  1805. VALIDATE_INSTRUCTION(v128_load16x4_s)
  1806. {
  1807. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1808. constexpr auto N = 16;
  1809. constexpr auto M = 4;
  1810. constexpr auto max_alignment = N * M / 8;
  1811. TRY(validate(arg.memory_index));
  1812. if ((1 << arg.align) > max_alignment)
  1813. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1814. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1815. }
  1816. VALIDATE_INSTRUCTION(v128_load16x4_u)
  1817. {
  1818. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1819. constexpr auto N = 16;
  1820. constexpr auto M = 4;
  1821. constexpr auto max_alignment = N * M / 8;
  1822. TRY(validate(arg.memory_index));
  1823. if ((1 << arg.align) > max_alignment)
  1824. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1825. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1826. }
  1827. VALIDATE_INSTRUCTION(v128_load32x2_s)
  1828. {
  1829. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1830. constexpr auto N = 32;
  1831. constexpr auto M = 2;
  1832. constexpr auto max_alignment = N * M / 8;
  1833. TRY(validate(arg.memory_index));
  1834. if ((1 << arg.align) > max_alignment)
  1835. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1836. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1837. }
  1838. VALIDATE_INSTRUCTION(v128_load32x2_u)
  1839. {
  1840. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1841. constexpr auto N = 32;
  1842. constexpr auto M = 2;
  1843. constexpr auto max_alignment = N * M / 8;
  1844. TRY(validate(arg.memory_index));
  1845. if ((1 << arg.align) > max_alignment)
  1846. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1847. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1848. }
  1849. VALIDATE_INSTRUCTION(v128_load8_splat)
  1850. {
  1851. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1852. constexpr auto N = 8;
  1853. constexpr auto max_alignment = N / 8;
  1854. TRY(validate(arg.memory_index));
  1855. if ((1 << arg.align) > max_alignment)
  1856. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1857. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1858. }
  1859. VALIDATE_INSTRUCTION(v128_load16_splat)
  1860. {
  1861. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1862. constexpr auto N = 16;
  1863. constexpr auto max_alignment = N / 8;
  1864. TRY(validate(arg.memory_index));
  1865. if ((1 << arg.align) > max_alignment)
  1866. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1867. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1868. }
  1869. VALIDATE_INSTRUCTION(v128_load32_splat)
  1870. {
  1871. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1872. constexpr auto N = 32;
  1873. constexpr auto max_alignment = N / 8;
  1874. TRY(validate(arg.memory_index));
  1875. if ((1 << arg.align) > max_alignment)
  1876. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1877. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1878. }
  1879. VALIDATE_INSTRUCTION(v128_load64_splat)
  1880. {
  1881. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1882. constexpr auto N = 64;
  1883. constexpr auto max_alignment = N / 8;
  1884. TRY(validate(arg.memory_index));
  1885. if ((1 << arg.align) > max_alignment)
  1886. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  1887. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  1888. }
  1889. VALIDATE_INSTRUCTION(v128_store)
  1890. {
  1891. auto& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  1892. TRY(validate(arg.memory_index));
  1893. if ((1ull << arg.align) > sizeof(u128))
  1894. return Errors::out_of_bounds("memory op alignment"sv, 1ull << arg.align, 0, sizeof(u128));
  1895. TRY((stack.take<ValueType::V128, ValueType::I32>()));
  1896. return {};
  1897. }
  1898. VALIDATE_INSTRUCTION(v128_const)
  1899. {
  1900. is_constant = true;
  1901. stack.append(ValueType(ValueType::V128));
  1902. return {};
  1903. }
  1904. VALIDATE_INSTRUCTION(i8x16_shuffle)
  1905. {
  1906. auto const& arg = instruction.arguments().get<Instruction::ShuffleArgument>();
  1907. for (auto lane : arg.lanes) {
  1908. if (lane >= 32)
  1909. return Errors::out_of_bounds("shuffle lane"sv, lane, 0, 32);
  1910. }
  1911. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  1912. }
  1913. VALIDATE_INSTRUCTION(i8x16_swizzle)
  1914. {
  1915. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  1916. }
  1917. enum class Shape {
  1918. I8x16,
  1919. I16x8,
  1920. I32x4,
  1921. I64x2,
  1922. F32x4,
  1923. F64x2,
  1924. };
  1925. template<Shape shape>
  1926. static constexpr Wasm::ValueType::Kind unpacked()
  1927. {
  1928. switch (shape) {
  1929. case Shape::I8x16:
  1930. case Shape::I16x8:
  1931. case Shape::I32x4:
  1932. return Wasm::ValueType::I32;
  1933. case Shape::I64x2:
  1934. return Wasm::ValueType::I64;
  1935. case Shape::F32x4:
  1936. return Wasm::ValueType::F32;
  1937. case Shape::F64x2:
  1938. return Wasm::ValueType::F64;
  1939. }
  1940. }
  1941. template<Shape shape>
  1942. static constexpr size_t dimensions()
  1943. {
  1944. switch (shape) {
  1945. case Shape::I8x16:
  1946. return 16;
  1947. case Shape::I16x8:
  1948. return 8;
  1949. case Shape::I32x4:
  1950. return 4;
  1951. case Shape::I64x2:
  1952. return 2;
  1953. case Shape::F32x4:
  1954. return 4;
  1955. case Shape::F64x2:
  1956. return 2;
  1957. }
  1958. }
  1959. VALIDATE_INSTRUCTION(i8x16_splat)
  1960. {
  1961. constexpr auto unpacked_shape = unpacked<Shape::I8x16>();
  1962. return stack.take_and_put<unpacked_shape>(ValueType::V128);
  1963. }
  1964. VALIDATE_INSTRUCTION(i16x8_splat)
  1965. {
  1966. constexpr auto unpacked_shape = unpacked<Shape::I16x8>();
  1967. return stack.take_and_put<unpacked_shape>(ValueType::V128);
  1968. }
  1969. VALIDATE_INSTRUCTION(i32x4_splat)
  1970. {
  1971. constexpr auto unpacked_shape = unpacked<Shape::I32x4>();
  1972. return stack.take_and_put<unpacked_shape>(ValueType::V128);
  1973. }
  1974. VALIDATE_INSTRUCTION(i64x2_splat)
  1975. {
  1976. constexpr auto unpacked_shape = unpacked<Shape::I64x2>();
  1977. return stack.take_and_put<unpacked_shape>(ValueType::V128);
  1978. }
  1979. VALIDATE_INSTRUCTION(f32x4_splat)
  1980. {
  1981. constexpr auto unpacked_shape = unpacked<Shape::F32x4>();
  1982. return stack.take_and_put<unpacked_shape>(ValueType::V128);
  1983. }
  1984. VALIDATE_INSTRUCTION(f64x2_splat)
  1985. {
  1986. constexpr auto unpacked_shape = unpacked<Shape::F64x2>();
  1987. return stack.take_and_put<unpacked_shape>(ValueType::V128);
  1988. }
  1989. VALIDATE_INSTRUCTION(i8x16_extract_lane_s)
  1990. {
  1991. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  1992. constexpr auto shape = Shape::I8x16;
  1993. constexpr auto max_lane = dimensions<shape>();
  1994. if (arg.lane >= max_lane)
  1995. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  1996. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  1997. }
  1998. VALIDATE_INSTRUCTION(i8x16_extract_lane_u)
  1999. {
  2000. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2001. constexpr auto shape = Shape::I8x16;
  2002. constexpr auto max_lane = dimensions<shape>();
  2003. if (arg.lane >= max_lane)
  2004. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2005. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  2006. }
  2007. VALIDATE_INSTRUCTION(i8x16_replace_lane)
  2008. {
  2009. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2010. constexpr auto shape = Shape::I8x16;
  2011. constexpr auto max_lane = dimensions<shape>();
  2012. if (arg.lane >= max_lane)
  2013. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2014. return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
  2015. }
  2016. VALIDATE_INSTRUCTION(i16x8_extract_lane_s)
  2017. {
  2018. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2019. constexpr auto shape = Shape::I16x8;
  2020. constexpr auto max_lane = dimensions<shape>();
  2021. if (arg.lane >= max_lane)
  2022. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2023. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  2024. }
  2025. VALIDATE_INSTRUCTION(i16x8_extract_lane_u)
  2026. {
  2027. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2028. constexpr auto shape = Shape::I16x8;
  2029. constexpr auto max_lane = dimensions<shape>();
  2030. if (arg.lane >= max_lane)
  2031. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2032. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  2033. }
  2034. VALIDATE_INSTRUCTION(i16x8_replace_lane)
  2035. {
  2036. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2037. constexpr auto shape = Shape::I16x8;
  2038. constexpr auto max_lane = dimensions<shape>();
  2039. if (arg.lane >= max_lane)
  2040. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2041. return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
  2042. }
  2043. VALIDATE_INSTRUCTION(i32x4_extract_lane)
  2044. {
  2045. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2046. constexpr auto shape = Shape::I32x4;
  2047. constexpr auto max_lane = dimensions<shape>();
  2048. if (arg.lane >= max_lane)
  2049. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2050. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  2051. }
  2052. VALIDATE_INSTRUCTION(i32x4_replace_lane)
  2053. {
  2054. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2055. constexpr auto shape = Shape::I32x4;
  2056. constexpr auto max_lane = dimensions<shape>();
  2057. if (arg.lane >= max_lane)
  2058. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2059. return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
  2060. }
  2061. VALIDATE_INSTRUCTION(i64x2_extract_lane)
  2062. {
  2063. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2064. constexpr auto shape = Shape::I64x2;
  2065. constexpr auto max_lane = dimensions<shape>();
  2066. if (arg.lane >= max_lane)
  2067. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2068. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  2069. }
  2070. VALIDATE_INSTRUCTION(i64x2_replace_lane)
  2071. {
  2072. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2073. constexpr auto shape = Shape::I64x2;
  2074. constexpr auto max_lane = dimensions<shape>();
  2075. if (arg.lane >= max_lane)
  2076. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2077. return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
  2078. }
  2079. VALIDATE_INSTRUCTION(f32x4_extract_lane)
  2080. {
  2081. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2082. constexpr auto shape = Shape::F32x4;
  2083. constexpr auto max_lane = dimensions<shape>();
  2084. if (arg.lane >= max_lane)
  2085. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2086. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  2087. }
  2088. VALIDATE_INSTRUCTION(f32x4_replace_lane)
  2089. {
  2090. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2091. constexpr auto shape = Shape::F32x4;
  2092. constexpr auto max_lane = dimensions<shape>();
  2093. if (arg.lane >= max_lane)
  2094. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2095. return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
  2096. }
  2097. VALIDATE_INSTRUCTION(f64x2_extract_lane)
  2098. {
  2099. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2100. constexpr auto shape = Shape::F64x2;
  2101. constexpr auto max_lane = dimensions<shape>();
  2102. if (arg.lane >= max_lane)
  2103. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2104. return stack.take_and_put<ValueType::V128>(unpacked<shape>());
  2105. }
  2106. VALIDATE_INSTRUCTION(f64x2_replace_lane)
  2107. {
  2108. auto const& arg = instruction.arguments().get<Instruction::LaneIndex>();
  2109. constexpr auto shape = Shape::F64x2;
  2110. constexpr auto max_lane = dimensions<shape>();
  2111. if (arg.lane >= max_lane)
  2112. return Errors::out_of_bounds("extract lane"sv, arg.lane, 0, max_lane);
  2113. return stack.take_and_put<unpacked<shape>(), ValueType::V128>(ValueType::V128);
  2114. }
  2115. VALIDATE_INSTRUCTION(i8x16_eq)
  2116. {
  2117. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2118. }
  2119. VALIDATE_INSTRUCTION(i8x16_ne)
  2120. {
  2121. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2122. }
  2123. VALIDATE_INSTRUCTION(i8x16_lt_s)
  2124. {
  2125. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2126. }
  2127. VALIDATE_INSTRUCTION(i8x16_lt_u)
  2128. {
  2129. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2130. }
  2131. VALIDATE_INSTRUCTION(i8x16_gt_s)
  2132. {
  2133. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2134. }
  2135. VALIDATE_INSTRUCTION(i8x16_gt_u)
  2136. {
  2137. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2138. }
  2139. VALIDATE_INSTRUCTION(i8x16_le_s)
  2140. {
  2141. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2142. }
  2143. VALIDATE_INSTRUCTION(i8x16_le_u)
  2144. {
  2145. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2146. }
  2147. VALIDATE_INSTRUCTION(i8x16_ge_s)
  2148. {
  2149. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2150. }
  2151. VALIDATE_INSTRUCTION(i8x16_ge_u)
  2152. {
  2153. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2154. }
  2155. VALIDATE_INSTRUCTION(i16x8_eq)
  2156. {
  2157. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2158. }
  2159. VALIDATE_INSTRUCTION(i16x8_ne)
  2160. {
  2161. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2162. }
  2163. VALIDATE_INSTRUCTION(i16x8_lt_s)
  2164. {
  2165. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2166. }
  2167. VALIDATE_INSTRUCTION(i16x8_lt_u)
  2168. {
  2169. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2170. }
  2171. VALIDATE_INSTRUCTION(i16x8_gt_s)
  2172. {
  2173. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2174. }
  2175. VALIDATE_INSTRUCTION(i16x8_gt_u)
  2176. {
  2177. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2178. }
  2179. VALIDATE_INSTRUCTION(i16x8_le_s)
  2180. {
  2181. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2182. }
  2183. VALIDATE_INSTRUCTION(i16x8_le_u)
  2184. {
  2185. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2186. }
  2187. VALIDATE_INSTRUCTION(i16x8_ge_s)
  2188. {
  2189. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2190. }
  2191. VALIDATE_INSTRUCTION(i16x8_ge_u)
  2192. {
  2193. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2194. }
  2195. VALIDATE_INSTRUCTION(i32x4_eq)
  2196. {
  2197. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2198. }
  2199. VALIDATE_INSTRUCTION(i32x4_ne)
  2200. {
  2201. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2202. }
  2203. VALIDATE_INSTRUCTION(i32x4_lt_s)
  2204. {
  2205. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2206. }
  2207. VALIDATE_INSTRUCTION(i32x4_lt_u)
  2208. {
  2209. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2210. }
  2211. VALIDATE_INSTRUCTION(i32x4_gt_s)
  2212. {
  2213. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2214. }
  2215. VALIDATE_INSTRUCTION(i32x4_gt_u)
  2216. {
  2217. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2218. }
  2219. VALIDATE_INSTRUCTION(i32x4_le_s)
  2220. {
  2221. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2222. }
  2223. VALIDATE_INSTRUCTION(i32x4_le_u)
  2224. {
  2225. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2226. }
  2227. VALIDATE_INSTRUCTION(i32x4_ge_s)
  2228. {
  2229. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2230. }
  2231. VALIDATE_INSTRUCTION(i32x4_ge_u)
  2232. {
  2233. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2234. }
  2235. VALIDATE_INSTRUCTION(f32x4_eq)
  2236. {
  2237. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2238. }
  2239. VALIDATE_INSTRUCTION(f32x4_ne)
  2240. {
  2241. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2242. }
  2243. VALIDATE_INSTRUCTION(f32x4_lt)
  2244. {
  2245. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2246. }
  2247. VALIDATE_INSTRUCTION(f32x4_gt)
  2248. {
  2249. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2250. }
  2251. VALIDATE_INSTRUCTION(f32x4_le)
  2252. {
  2253. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2254. }
  2255. VALIDATE_INSTRUCTION(f32x4_ge)
  2256. {
  2257. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2258. }
  2259. VALIDATE_INSTRUCTION(f64x2_eq)
  2260. {
  2261. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2262. }
  2263. VALIDATE_INSTRUCTION(f64x2_ne)
  2264. {
  2265. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2266. }
  2267. VALIDATE_INSTRUCTION(f64x2_lt)
  2268. {
  2269. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2270. }
  2271. VALIDATE_INSTRUCTION(f64x2_gt)
  2272. {
  2273. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2274. }
  2275. VALIDATE_INSTRUCTION(f64x2_le)
  2276. {
  2277. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2278. }
  2279. VALIDATE_INSTRUCTION(f64x2_ge)
  2280. {
  2281. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2282. }
  2283. VALIDATE_INSTRUCTION(v128_not)
  2284. {
  2285. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2286. }
  2287. VALIDATE_INSTRUCTION(v128_and)
  2288. {
  2289. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2290. }
  2291. VALIDATE_INSTRUCTION(v128_andnot)
  2292. {
  2293. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2294. }
  2295. VALIDATE_INSTRUCTION(v128_or)
  2296. {
  2297. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2298. }
  2299. VALIDATE_INSTRUCTION(v128_xor)
  2300. {
  2301. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2302. }
  2303. VALIDATE_INSTRUCTION(v128_bitselect)
  2304. {
  2305. return stack.take_and_put<ValueType::V128, ValueType::V128, ValueType::V128>(ValueType::V128);
  2306. }
  2307. VALIDATE_INSTRUCTION(v128_any_true)
  2308. {
  2309. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2310. }
  2311. VALIDATE_INSTRUCTION(v128_load8_lane)
  2312. {
  2313. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2314. constexpr auto N = 8;
  2315. constexpr auto max_lane = 128 / N;
  2316. constexpr auto max_alignment = N / 8;
  2317. if (arg.lane > max_lane)
  2318. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2319. TRY(validate(arg.memory.memory_index));
  2320. if ((1 << arg.memory.align) > max_alignment)
  2321. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2322. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2323. }
  2324. VALIDATE_INSTRUCTION(v128_load16_lane)
  2325. {
  2326. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2327. constexpr auto N = 16;
  2328. constexpr auto max_lane = 128 / N;
  2329. constexpr auto max_alignment = N / 8;
  2330. if (arg.lane >= max_lane)
  2331. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2332. TRY(validate(arg.memory.memory_index));
  2333. if ((1 << arg.memory.align) > max_alignment)
  2334. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2335. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2336. }
  2337. VALIDATE_INSTRUCTION(v128_load32_lane)
  2338. {
  2339. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2340. constexpr auto N = 32;
  2341. constexpr auto max_lane = 128 / N;
  2342. constexpr auto max_alignment = N / 8;
  2343. if (arg.lane >= max_lane)
  2344. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2345. TRY(validate(arg.memory.memory_index));
  2346. if ((1 << arg.memory.align) > max_alignment)
  2347. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2348. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2349. }
  2350. VALIDATE_INSTRUCTION(v128_load64_lane)
  2351. {
  2352. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2353. constexpr auto N = 64;
  2354. constexpr auto max_lane = 128 / N;
  2355. constexpr auto max_alignment = N / 8;
  2356. if (arg.lane >= max_lane)
  2357. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2358. TRY(validate(arg.memory.memory_index));
  2359. if (arg.memory.align > max_alignment)
  2360. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2361. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2362. }
  2363. VALIDATE_INSTRUCTION(v128_store8_lane)
  2364. {
  2365. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2366. constexpr auto N = 8;
  2367. constexpr auto max_lane = 128 / N;
  2368. constexpr auto max_alignment = N / 8;
  2369. if (arg.lane >= max_lane)
  2370. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2371. TRY(validate(arg.memory.memory_index));
  2372. if ((1 << arg.memory.align) > max_alignment)
  2373. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2374. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2375. }
  2376. VALIDATE_INSTRUCTION(v128_store16_lane)
  2377. {
  2378. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2379. constexpr auto N = 16;
  2380. constexpr auto max_lane = 128 / N;
  2381. constexpr auto max_alignment = N / 8;
  2382. if (arg.lane >= max_lane)
  2383. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2384. TRY(validate(arg.memory.memory_index));
  2385. if ((1 << arg.memory.align) > max_alignment)
  2386. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2387. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2388. }
  2389. VALIDATE_INSTRUCTION(v128_store32_lane)
  2390. {
  2391. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2392. constexpr auto N = 32;
  2393. constexpr auto max_lane = 128 / N;
  2394. constexpr auto max_alignment = N / 8;
  2395. if (arg.lane >= max_lane)
  2396. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2397. TRY(validate(arg.memory.memory_index));
  2398. if ((1 << arg.memory.align) > max_alignment)
  2399. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2400. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2401. }
  2402. VALIDATE_INSTRUCTION(v128_store64_lane)
  2403. {
  2404. auto const& arg = instruction.arguments().get<Instruction::MemoryAndLaneArgument>();
  2405. constexpr auto N = 64;
  2406. constexpr auto max_lane = 128 / N;
  2407. constexpr auto max_alignment = N / 8;
  2408. if (arg.lane >= max_lane)
  2409. return Errors::out_of_bounds("lane index"sv, arg.lane, 0u, max_lane);
  2410. TRY(validate(arg.memory.memory_index));
  2411. if ((1 << arg.memory.align) > max_alignment)
  2412. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.memory.align, 0u, max_alignment);
  2413. return stack.take_and_put<ValueType::V128, ValueType::I32>(ValueType::V128);
  2414. }
  2415. VALIDATE_INSTRUCTION(v128_load32_zero)
  2416. {
  2417. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  2418. constexpr auto N = 32;
  2419. constexpr auto max_alignment = N / 8;
  2420. TRY(validate(arg.memory_index));
  2421. if ((1 << arg.align) > max_alignment)
  2422. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  2423. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  2424. }
  2425. VALIDATE_INSTRUCTION(v128_load64_zero)
  2426. {
  2427. auto const& arg = instruction.arguments().get<Instruction::MemoryArgument>();
  2428. constexpr auto N = 64;
  2429. constexpr auto max_alignment = N / 8;
  2430. TRY(validate(arg.memory_index));
  2431. if ((1 << arg.align) > max_alignment)
  2432. return Errors::out_of_bounds("memory op alignment"sv, 1 << arg.align, 0u, max_alignment);
  2433. return stack.take_and_put<ValueType::I32>(ValueType::V128);
  2434. }
  2435. VALIDATE_INSTRUCTION(f32x4_demote_f64x2_zero)
  2436. {
  2437. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2438. }
  2439. VALIDATE_INSTRUCTION(f64x2_promote_low_f32x4)
  2440. {
  2441. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2442. }
  2443. VALIDATE_INSTRUCTION(i8x16_abs)
  2444. {
  2445. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2446. }
  2447. VALIDATE_INSTRUCTION(i8x16_neg)
  2448. {
  2449. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2450. }
  2451. VALIDATE_INSTRUCTION(i8x16_popcnt)
  2452. {
  2453. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2454. }
  2455. VALIDATE_INSTRUCTION(i8x16_all_true)
  2456. {
  2457. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2458. }
  2459. VALIDATE_INSTRUCTION(i8x16_bitmask)
  2460. {
  2461. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2462. }
  2463. VALIDATE_INSTRUCTION(i8x16_narrow_i16x8_s)
  2464. {
  2465. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2466. }
  2467. VALIDATE_INSTRUCTION(i8x16_narrow_i16x8_u)
  2468. {
  2469. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2470. }
  2471. VALIDATE_INSTRUCTION(f32x4_ceil)
  2472. {
  2473. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2474. }
  2475. VALIDATE_INSTRUCTION(f32x4_floor)
  2476. {
  2477. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2478. }
  2479. VALIDATE_INSTRUCTION(f32x4_trunc)
  2480. {
  2481. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2482. }
  2483. VALIDATE_INSTRUCTION(f32x4_nearest)
  2484. {
  2485. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2486. }
  2487. VALIDATE_INSTRUCTION(i8x16_shl)
  2488. {
  2489. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2490. }
  2491. VALIDATE_INSTRUCTION(i8x16_shr_s)
  2492. {
  2493. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2494. }
  2495. VALIDATE_INSTRUCTION(i8x16_shr_u)
  2496. {
  2497. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2498. }
  2499. VALIDATE_INSTRUCTION(i8x16_add)
  2500. {
  2501. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2502. }
  2503. VALIDATE_INSTRUCTION(i8x16_add_sat_s)
  2504. {
  2505. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2506. }
  2507. VALIDATE_INSTRUCTION(i8x16_add_sat_u)
  2508. {
  2509. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2510. }
  2511. VALIDATE_INSTRUCTION(i8x16_sub)
  2512. {
  2513. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2514. }
  2515. VALIDATE_INSTRUCTION(i8x16_sub_sat_s)
  2516. {
  2517. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2518. }
  2519. VALIDATE_INSTRUCTION(i8x16_sub_sat_u)
  2520. {
  2521. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2522. }
  2523. VALIDATE_INSTRUCTION(f64x2_ceil)
  2524. {
  2525. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2526. }
  2527. VALIDATE_INSTRUCTION(f64x2_floor)
  2528. {
  2529. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2530. }
  2531. VALIDATE_INSTRUCTION(i8x16_min_s)
  2532. {
  2533. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2534. }
  2535. VALIDATE_INSTRUCTION(i8x16_min_u)
  2536. {
  2537. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2538. }
  2539. VALIDATE_INSTRUCTION(i8x16_max_s)
  2540. {
  2541. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2542. }
  2543. VALIDATE_INSTRUCTION(i8x16_max_u)
  2544. {
  2545. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2546. }
  2547. VALIDATE_INSTRUCTION(f64x2_trunc)
  2548. {
  2549. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2550. }
  2551. VALIDATE_INSTRUCTION(i8x16_avgr_u)
  2552. {
  2553. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2554. }
  2555. VALIDATE_INSTRUCTION(i16x8_extadd_pairwise_i8x16_s)
  2556. {
  2557. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2558. }
  2559. VALIDATE_INSTRUCTION(i16x8_extadd_pairwise_i8x16_u)
  2560. {
  2561. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2562. }
  2563. VALIDATE_INSTRUCTION(i32x4_extadd_pairwise_i16x8_s)
  2564. {
  2565. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2566. }
  2567. VALIDATE_INSTRUCTION(i32x4_extadd_pairwise_i16x8_u)
  2568. {
  2569. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2570. }
  2571. VALIDATE_INSTRUCTION(i16x8_abs)
  2572. {
  2573. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2574. }
  2575. VALIDATE_INSTRUCTION(i16x8_neg)
  2576. {
  2577. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2578. }
  2579. VALIDATE_INSTRUCTION(i16x8_q15mulr_sat_s)
  2580. {
  2581. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2582. }
  2583. VALIDATE_INSTRUCTION(i16x8_all_true)
  2584. {
  2585. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2586. }
  2587. VALIDATE_INSTRUCTION(i16x8_bitmask)
  2588. {
  2589. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2590. }
  2591. VALIDATE_INSTRUCTION(i16x8_narrow_i32x4_s)
  2592. {
  2593. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2594. }
  2595. VALIDATE_INSTRUCTION(i16x8_narrow_i32x4_u)
  2596. {
  2597. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2598. }
  2599. VALIDATE_INSTRUCTION(i16x8_extend_low_i8x16_s)
  2600. {
  2601. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2602. }
  2603. VALIDATE_INSTRUCTION(i16x8_extend_high_i8x16_s)
  2604. {
  2605. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2606. }
  2607. VALIDATE_INSTRUCTION(i16x8_extend_low_i8x16_u)
  2608. {
  2609. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2610. }
  2611. VALIDATE_INSTRUCTION(i16x8_extend_high_i8x16_u)
  2612. {
  2613. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2614. }
  2615. VALIDATE_INSTRUCTION(i16x8_shl)
  2616. {
  2617. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2618. }
  2619. VALIDATE_INSTRUCTION(i16x8_shr_s)
  2620. {
  2621. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2622. }
  2623. VALIDATE_INSTRUCTION(i16x8_shr_u)
  2624. {
  2625. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2626. }
  2627. VALIDATE_INSTRUCTION(i16x8_add)
  2628. {
  2629. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2630. }
  2631. VALIDATE_INSTRUCTION(i16x8_add_sat_s)
  2632. {
  2633. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2634. }
  2635. VALIDATE_INSTRUCTION(i16x8_add_sat_u)
  2636. {
  2637. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2638. }
  2639. VALIDATE_INSTRUCTION(i16x8_sub)
  2640. {
  2641. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2642. }
  2643. VALIDATE_INSTRUCTION(i16x8_sub_sat_s)
  2644. {
  2645. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2646. }
  2647. VALIDATE_INSTRUCTION(i16x8_sub_sat_u)
  2648. {
  2649. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2650. }
  2651. VALIDATE_INSTRUCTION(f64x2_nearest)
  2652. {
  2653. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2654. }
  2655. VALIDATE_INSTRUCTION(i16x8_mul)
  2656. {
  2657. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2658. }
  2659. VALIDATE_INSTRUCTION(i16x8_min_s)
  2660. {
  2661. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2662. }
  2663. VALIDATE_INSTRUCTION(i16x8_min_u)
  2664. {
  2665. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2666. }
  2667. VALIDATE_INSTRUCTION(i16x8_max_s)
  2668. {
  2669. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2670. }
  2671. VALIDATE_INSTRUCTION(i16x8_max_u)
  2672. {
  2673. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2674. }
  2675. VALIDATE_INSTRUCTION(i16x8_avgr_u)
  2676. {
  2677. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2678. }
  2679. VALIDATE_INSTRUCTION(i16x8_extmul_low_i8x16_s)
  2680. {
  2681. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2682. }
  2683. VALIDATE_INSTRUCTION(i16x8_extmul_high_i8x16_s)
  2684. {
  2685. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2686. }
  2687. VALIDATE_INSTRUCTION(i16x8_extmul_low_i8x16_u)
  2688. {
  2689. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2690. }
  2691. VALIDATE_INSTRUCTION(i16x8_extmul_high_i8x16_u)
  2692. {
  2693. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2694. }
  2695. VALIDATE_INSTRUCTION(i32x4_abs)
  2696. {
  2697. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2698. }
  2699. VALIDATE_INSTRUCTION(i32x4_neg)
  2700. {
  2701. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2702. }
  2703. VALIDATE_INSTRUCTION(i32x4_all_true)
  2704. {
  2705. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2706. }
  2707. VALIDATE_INSTRUCTION(i32x4_bitmask)
  2708. {
  2709. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2710. }
  2711. VALIDATE_INSTRUCTION(i32x4_extend_low_i16x8_s)
  2712. {
  2713. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2714. }
  2715. VALIDATE_INSTRUCTION(i32x4_extend_high_i16x8_s)
  2716. {
  2717. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2718. }
  2719. VALIDATE_INSTRUCTION(i32x4_extend_low_i16x8_u)
  2720. {
  2721. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2722. }
  2723. VALIDATE_INSTRUCTION(i32x4_extend_high_i16x8_u)
  2724. {
  2725. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2726. }
  2727. VALIDATE_INSTRUCTION(i32x4_shl)
  2728. {
  2729. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2730. }
  2731. VALIDATE_INSTRUCTION(i32x4_shr_s)
  2732. {
  2733. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2734. }
  2735. VALIDATE_INSTRUCTION(i32x4_shr_u)
  2736. {
  2737. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2738. }
  2739. VALIDATE_INSTRUCTION(i32x4_add)
  2740. {
  2741. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2742. }
  2743. VALIDATE_INSTRUCTION(i32x4_sub)
  2744. {
  2745. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2746. }
  2747. VALIDATE_INSTRUCTION(i32x4_mul)
  2748. {
  2749. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2750. }
  2751. VALIDATE_INSTRUCTION(i32x4_min_s)
  2752. {
  2753. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2754. }
  2755. VALIDATE_INSTRUCTION(i32x4_min_u)
  2756. {
  2757. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2758. }
  2759. VALIDATE_INSTRUCTION(i32x4_max_s)
  2760. {
  2761. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2762. }
  2763. VALIDATE_INSTRUCTION(i32x4_max_u)
  2764. {
  2765. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2766. }
  2767. VALIDATE_INSTRUCTION(i32x4_dot_i16x8_s)
  2768. {
  2769. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2770. }
  2771. VALIDATE_INSTRUCTION(i32x4_extmul_low_i16x8_s)
  2772. {
  2773. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2774. }
  2775. VALIDATE_INSTRUCTION(i32x4_extmul_high_i16x8_s)
  2776. {
  2777. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2778. }
  2779. VALIDATE_INSTRUCTION(i32x4_extmul_low_i16x8_u)
  2780. {
  2781. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2782. }
  2783. VALIDATE_INSTRUCTION(i32x4_extmul_high_i16x8_u)
  2784. {
  2785. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2786. }
  2787. VALIDATE_INSTRUCTION(i64x2_abs)
  2788. {
  2789. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2790. }
  2791. VALIDATE_INSTRUCTION(i64x2_neg)
  2792. {
  2793. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2794. }
  2795. VALIDATE_INSTRUCTION(i64x2_all_true)
  2796. {
  2797. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2798. }
  2799. VALIDATE_INSTRUCTION(i64x2_bitmask)
  2800. {
  2801. return stack.take_and_put<ValueType::V128>(ValueType::I32);
  2802. }
  2803. VALIDATE_INSTRUCTION(i64x2_extend_low_i32x4_s)
  2804. {
  2805. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2806. }
  2807. VALIDATE_INSTRUCTION(i64x2_extend_high_i32x4_s)
  2808. {
  2809. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2810. }
  2811. VALIDATE_INSTRUCTION(i64x2_extend_low_i32x4_u)
  2812. {
  2813. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2814. }
  2815. VALIDATE_INSTRUCTION(i64x2_extend_high_i32x4_u)
  2816. {
  2817. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2818. }
  2819. VALIDATE_INSTRUCTION(i64x2_shl)
  2820. {
  2821. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2822. }
  2823. VALIDATE_INSTRUCTION(i64x2_shr_s)
  2824. {
  2825. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2826. }
  2827. VALIDATE_INSTRUCTION(i64x2_shr_u)
  2828. {
  2829. return stack.take_and_put<ValueType::I32, ValueType::V128>(ValueType::V128);
  2830. }
  2831. VALIDATE_INSTRUCTION(i64x2_add)
  2832. {
  2833. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2834. }
  2835. VALIDATE_INSTRUCTION(i64x2_sub)
  2836. {
  2837. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2838. }
  2839. VALIDATE_INSTRUCTION(i64x2_mul)
  2840. {
  2841. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2842. }
  2843. VALIDATE_INSTRUCTION(i64x2_eq)
  2844. {
  2845. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2846. }
  2847. VALIDATE_INSTRUCTION(i64x2_ne)
  2848. {
  2849. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2850. }
  2851. VALIDATE_INSTRUCTION(i64x2_lt_s)
  2852. {
  2853. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2854. }
  2855. VALIDATE_INSTRUCTION(i64x2_gt_s)
  2856. {
  2857. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2858. }
  2859. VALIDATE_INSTRUCTION(i64x2_le_s)
  2860. {
  2861. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2862. }
  2863. VALIDATE_INSTRUCTION(i64x2_ge_s)
  2864. {
  2865. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2866. }
  2867. VALIDATE_INSTRUCTION(i64x2_extmul_low_i32x4_s)
  2868. {
  2869. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2870. }
  2871. VALIDATE_INSTRUCTION(i64x2_extmul_high_i32x4_s)
  2872. {
  2873. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2874. }
  2875. VALIDATE_INSTRUCTION(i64x2_extmul_low_i32x4_u)
  2876. {
  2877. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2878. }
  2879. VALIDATE_INSTRUCTION(i64x2_extmul_high_i32x4_u)
  2880. {
  2881. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2882. }
  2883. VALIDATE_INSTRUCTION(f32x4_abs)
  2884. {
  2885. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2886. }
  2887. VALIDATE_INSTRUCTION(f32x4_neg)
  2888. {
  2889. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2890. }
  2891. VALIDATE_INSTRUCTION(f32x4_sqrt)
  2892. {
  2893. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2894. }
  2895. VALIDATE_INSTRUCTION(f32x4_add)
  2896. {
  2897. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2898. }
  2899. VALIDATE_INSTRUCTION(f32x4_sub)
  2900. {
  2901. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2902. }
  2903. VALIDATE_INSTRUCTION(f32x4_mul)
  2904. {
  2905. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2906. }
  2907. VALIDATE_INSTRUCTION(f32x4_div)
  2908. {
  2909. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2910. }
  2911. VALIDATE_INSTRUCTION(f32x4_min)
  2912. {
  2913. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2914. }
  2915. VALIDATE_INSTRUCTION(f32x4_max)
  2916. {
  2917. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2918. }
  2919. VALIDATE_INSTRUCTION(f32x4_pmin)
  2920. {
  2921. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2922. }
  2923. VALIDATE_INSTRUCTION(f32x4_pmax)
  2924. {
  2925. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2926. }
  2927. VALIDATE_INSTRUCTION(f64x2_abs)
  2928. {
  2929. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2930. }
  2931. VALIDATE_INSTRUCTION(f64x2_neg)
  2932. {
  2933. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2934. }
  2935. VALIDATE_INSTRUCTION(f64x2_sqrt)
  2936. {
  2937. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2938. }
  2939. VALIDATE_INSTRUCTION(f64x2_add)
  2940. {
  2941. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2942. }
  2943. VALIDATE_INSTRUCTION(f64x2_sub)
  2944. {
  2945. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2946. }
  2947. VALIDATE_INSTRUCTION(f64x2_mul)
  2948. {
  2949. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2950. }
  2951. VALIDATE_INSTRUCTION(f64x2_div)
  2952. {
  2953. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2954. }
  2955. VALIDATE_INSTRUCTION(f64x2_min)
  2956. {
  2957. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2958. }
  2959. VALIDATE_INSTRUCTION(f64x2_max)
  2960. {
  2961. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2962. }
  2963. VALIDATE_INSTRUCTION(f64x2_pmin)
  2964. {
  2965. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2966. }
  2967. VALIDATE_INSTRUCTION(f64x2_pmax)
  2968. {
  2969. return stack.take_and_put<ValueType::V128, ValueType::V128>(ValueType::V128);
  2970. }
  2971. VALIDATE_INSTRUCTION(i32x4_trunc_sat_f32x4_s)
  2972. {
  2973. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2974. }
  2975. VALIDATE_INSTRUCTION(i32x4_trunc_sat_f32x4_u)
  2976. {
  2977. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2978. }
  2979. VALIDATE_INSTRUCTION(f32x4_convert_i32x4_s)
  2980. {
  2981. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2982. }
  2983. VALIDATE_INSTRUCTION(f32x4_convert_i32x4_u)
  2984. {
  2985. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2986. }
  2987. VALIDATE_INSTRUCTION(i32x4_trunc_sat_f64x2_s_zero)
  2988. {
  2989. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2990. }
  2991. VALIDATE_INSTRUCTION(i32x4_trunc_sat_f64x2_u_zero)
  2992. {
  2993. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2994. }
  2995. VALIDATE_INSTRUCTION(f64x2_convert_low_i32x4_s)
  2996. {
  2997. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  2998. }
  2999. VALIDATE_INSTRUCTION(f64x2_convert_low_i32x4_u)
  3000. {
  3001. return stack.take_and_put<ValueType::V128>(ValueType::V128);
  3002. }
  3003. ErrorOr<void, ValidationError> Validator::validate(Instruction const& instruction, Stack& stack, bool& is_constant)
  3004. {
  3005. switch (instruction.opcode().value()) {
  3006. #define M(name, integer_value) \
  3007. case Instructions::name.value(): \
  3008. dbgln_if(WASM_VALIDATOR_DEBUG, "checking {}, stack = {}", #name, stack); \
  3009. return validate_instruction<integer_value>(instruction, stack, is_constant);
  3010. ENUMERATE_WASM_OPCODES(M)
  3011. #undef M
  3012. default:
  3013. is_constant = false;
  3014. return Errors::invalid(ByteString::formatted("instruction opcode (0x{:x})", instruction.opcode().value()));
  3015. }
  3016. }
  3017. ErrorOr<Validator::ExpressionTypeResult, ValidationError> Validator::validate(Expression const& expression, Vector<ValueType> const& result_types)
  3018. {
  3019. Stack stack;
  3020. bool is_constant_expression = true;
  3021. for (auto& instruction : expression.instructions()) {
  3022. bool is_constant = false;
  3023. TRY(validate(instruction, stack, is_constant));
  3024. is_constant_expression &= is_constant;
  3025. }
  3026. auto expected_result_types = result_types;
  3027. while (!expected_result_types.is_empty())
  3028. TRY(stack.take(expected_result_types.take_last()));
  3029. for (auto& type : result_types)
  3030. stack.append(type);
  3031. return ExpressionTypeResult { stack.release_vector(), is_constant_expression };
  3032. }
  3033. bool Validator::Stack::operator==(Stack const& other) const
  3034. {
  3035. if (!m_did_insert_unknown_entry && !other.m_did_insert_unknown_entry)
  3036. return static_cast<Vector<StackEntry> const&>(*this) == static_cast<Vector<StackEntry> const&>(other);
  3037. Optional<size_t> own_last_unknown_entry_index_from_end, other_last_unknown_entry_index_from_end;
  3038. auto other_size = static_cast<Vector<StackEntry> const&>(other).size();
  3039. auto own_size = Vector<StackEntry>::size();
  3040. for (size_t i = 0; i < own_size; ++i) {
  3041. if (other_size <= i)
  3042. break;
  3043. auto own_entry = at(own_size - i - 1);
  3044. auto other_entry = other.at(other_size - i - 1);
  3045. if (!own_entry.is_known) {
  3046. own_last_unknown_entry_index_from_end = i;
  3047. break;
  3048. }
  3049. if (!other_entry.is_known) {
  3050. other_last_unknown_entry_index_from_end = i;
  3051. break;
  3052. }
  3053. }
  3054. if (!own_last_unknown_entry_index_from_end.has_value() && !other_last_unknown_entry_index_from_end.has_value()) {
  3055. if (static_cast<Vector<StackEntry> const&>(other).is_empty() || Vector<StackEntry>::is_empty())
  3056. return true;
  3057. dbgln("Equality check internal error between");
  3058. dbgln("stack:");
  3059. for (auto& entry : *this)
  3060. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  3061. dbgln("and stack:");
  3062. for (auto& entry : other)
  3063. dbgln("- {}", entry.is_known ? Wasm::ValueType::kind_name(entry.concrete_type.kind()) : "<unknown>");
  3064. VERIFY_NOT_REACHED();
  3065. }
  3066. 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));
  3067. for (size_t i = 0; i < index_from_end; ++i) {
  3068. if (at(own_size - i - 1) != other.at(other_size - i - 1))
  3069. return false;
  3070. }
  3071. return true;
  3072. }
  3073. ByteString Validator::Errors::find_instruction_name(SourceLocation const& location)
  3074. {
  3075. auto index = location.function_name().find('<');
  3076. auto end_index = location.function_name().find('>');
  3077. if (!index.has_value() || !end_index.has_value())
  3078. return ByteString::formatted("{}", location);
  3079. auto opcode = location.function_name().substring_view(index.value() + 1, end_index.value() - index.value() - 1).to_number<unsigned>();
  3080. if (!opcode.has_value())
  3081. return ByteString::formatted("{}", location);
  3082. return instruction_name(OpCode { *opcode });
  3083. }
  3084. }