Validator.cpp 100 KB

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