Validator.cpp 99 KB

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