Validator.cpp 100 KB

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