Validator.cpp 100 KB

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