Validator.cpp 100 KB

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