123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804(*ZZZ Fix string/ident location*)(*** Regexp definitions ***)letsign=[%sedlex.regexp?Opt('+'|'-')]letdigit=[%sedlex.regexp?'0'..'9']lethexdigit=[%sedlex.regexp?'0'..'9'|'a'..'f'|'A'..'F']letnum=[%sedlex.regexp?digit,Star(Opt'_',digit)]lethexnum=[%sedlex.regexp?hexdigit,Star(Opt'_',hexdigit)]letuN=[%sedlex.regexp?num|"0x",hexnum]letsN=[%sedlex.regexp?sign,uN]letiN=[%sedlex.regexp?uN|sN]letfloat=[%sedlex.regexp?num,Opt('.',Optnum),Opt(('e'|'E'),sign,num)]lethexfloat=[%sedlex.regexp?"0x",hexnum,Opt('.',Opthexnum),Opt(('p'|'P'),sign,num)]letfN=[%sedlex.regexp?sign,(float|hexfloat|"inf"|"nan"|"nan:0x",hexnum)]letidchar=[%sedlex.regexp?('0'..'9'|'A'..'Z'|'a'..'z'|'!'|'#'|'$'|'%'|'&'|'\''|'*'|'+'|'-'|'.'|'/'|':'|'<'|'='|'>'|'?'|'@'|'\\'|'^'|'_'|'`'|'|'|'~')]letid=[%sedlex.regexp?'$',Plusidchar]letlinechar=[%sedlex.regexp?Sub(any,(10|13))]letnewline=[%sedlex.regexp?10|13|13,10]letlinecomment=[%sedlex.regexp?";;",Starlinechar,(newline|eof)]letformat=[%sedlex.regexp?'\n'|9]letstringchar=[%sedlex.regexp?(Sub(any,(0..31|0x7f|'"'|'\\'))|"\\t"|"\\n"|"\\r"|"\\'"|"\\\""|"\\\\"|"\\u{",hexnum,"}")]letstringelem=[%sedlex.regexp?stringchar|"\\",hexdigit,hexdigit]letstring=[%sedlex.regexp?'"',Starstringelem,'"']letreserved=[%sedlex.regexp?Plus(idchar|','|'['|']'|'{'|'}')](*
let space = [%sedlex.regexp? ' ' | format | comment]
*)letkeyword=[%sedlex.regexp?'a'..'z',Staridchar](*** String and comment scanning ***)letstring_buffer=Buffer.create256letreccomment_reclexbuf=match%sedlexlexbufwith|";)"->Buffer.add_stringstring_buffer";)"|"(;"->Buffer.add_stringstring_buffer"(;";comment_reclexbuf;comment_reclexbuf|';'|'('|Plus(Sub(any,(';'|'(')))->Buffer.add_stringstring_buffer(Sedlexing.Utf8.lexemelexbuf);comment_reclexbuf|_->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Malformed comment.\n")))letcommentlexbuf=Buffer.add_stringstring_buffer"(;";comment_reclexbuf;lets=Buffer.contentsstring_bufferinBuffer.clearstring_buffer;sletunicode_escapelexbufs=matchWax_utils.Unicode.scalar_of_hexswith|Someu->u|None->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Malformed Unicode escape.\n")))letrecstringlexbuf=match%sedlexlexbufwith|'"'->lets=Buffer.contentsstring_bufferinBuffer.clearstring_buffer;s|'"',('"'|reserved)->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Unknown token '%s'.\n"(Sedlexing.Utf8.lexemelexbuf))))|Plus(Sub(any,(0..31|0x7f|'"'|'\\')))->Buffer.add_stringstring_buffer(Sedlexing.Utf8.lexemelexbuf);stringlexbuf|"\\t"->Buffer.add_charstring_buffer'\t';stringlexbuf|"\\n"->Buffer.add_charstring_buffer'\n';stringlexbuf|"\\r"->Buffer.add_charstring_buffer'\r';stringlexbuf|"\\'"->Buffer.add_charstring_buffer'\'';stringlexbuf|"\\\""->Buffer.add_charstring_buffer'"';stringlexbuf|"\\\\"->Buffer.add_charstring_buffer'\\';stringlexbuf|'\\',hexdigit,hexdigit->lets=String.sub(Sedlexing.Utf8.lexemelexbuf)12inBuffer.add_charstring_buffer(Char.chr(int_of_string("0x"^s)));stringlexbuf|"\\u{",hexnum,"}"->letn=Sedlexing.Utf8.sub_lexemelexbuf3(Sedlexing.lexeme_lengthlexbuf-4)inBuffer.add_utf_8_ucharstring_buffer(unicode_escapelexbufn);stringlexbuf|_->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Malformed string.\n")))letrecscan_stringlexbuf=match%sedlexlexbufwith|'"'->()|'\\',any->scan_stringlexbuf|any->scan_stringlexbuf|_->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Unclosed string in annotation.\n")))letrecskip_annotationdepthlexbuf=match%sedlexlexbufwith|'('->skip_annotation(depth+1)lexbuf|')'->ifdepth>1thenskip_annotation(depth-1)lexbuf|'"'->scan_stringlexbuf;skip_annotationdepthlexbuf|"(;"->comment_reclexbuf;skip_annotationdepthlexbuf|linecomment->skip_annotationdepthlexbuf|Plus(' '|'\t'|'\n'|'\r')->skip_annotationdepthlexbuf|reserved|';'->skip_annotationdepthlexbuf|_->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Illegal character.\n")))letwith_locctxflexbuf=letloc_start=Sedlexing.lexing_bytes_position_startlexbufinletdesc=flexbufinWax_utils.Trivia.with_posctx{loc_start;loc_end=Sedlexing.lexing_bytes_position_currlexbuf}descopenTokens(*** The tokenizer ***)(* Keyword and instruction mnemonics, matched by lexing a generic keyword
and looking it up here rather than as hundreds of explicit sedlex arms
(which compile to a large DFA). Generated once from those arms; extend it
the same way. See also {!Atomics} for the atomic mnemonics. *)letkeyword_table:(string,token)Hashtbl.t=letentries=[("i32",I32);("i64",I64);("f32",F32);("f64",F64);("v128",V128);("i8",PACKEDTYPEI8);("i16",PACKEDTYPEI16);("any",ANY);("eq",EQ);("i31",I31);("struct",STRUCT);("array",ARRAY);("none",NONE);("func",FUNC);("nofunc",NOFUNC);("exn",EXN);("noexn",NOEXN);("cont",CONT);("nocont",NOCONT);("extern",EXTERN);("noextern",NOEXTERN);("exact",EXACT);("descriptor",LPAREN_DESCRIPTOR);("describes",LPAREN_DESCRIBES);("anyref",ANYREF);("eqref",EQREF);("i31ref",I31REF);("structref",STRUCTREF);("arrayref",ARRAYREF);("nullref",NULLREF);("funcref",FUNCREF);("nullfuncref",NULLFUNCREF);("exnref",EXNREF);("nullexnref",NULLEXNREF);("contref",CONTREF);("nullcontref",NULLCONTREF);("externref",EXTERNREF);("nullexternref",NULLEXTERNREF);("ref",REF);("null",NULL);("param",LPAREN_PARAM);("result",LPAREN_RESULT);("mut",MUT);("field",FIELD);("rec",REC);("type",LPAREN_TYPE);("sub",SUB);("final",FINAL);("import",LPAREN_IMPORT);("export",LPAREN_EXPORT);("local",LPAREN_LOCAL);("global",GLOBAL);("start",START);("elem",ELEM);("declare",DECLARE);("item",ITEM);("memory",MEMORY);("pagesize",PAGESIZE);("shared",SHARED);("table",TABLE);("data",DATA);("offset",OFFSET);("module",MODULE);("block",BLOCK);("loop",LOOP);("if",IF);("then",LPAREN_THEN);("else",ELSE);("end",END);("unreachable",INSTRUnreachable);("nop",INSTRNop);("br",BR);("br_if",BR_IF);("br_table",BR_TABLE);("br_on_null",BR_ON_NULL);("br_on_non_null",BR_ON_NON_NULL);("br_on_cast",BR_ON_CAST);("br_on_cast_desc_eq",BR_ON_CAST_DESC_EQ);("br_on_cast_desc_eq_fail",BR_ON_CAST_DESC_EQ_FAIL);("i8x16",I8X16);("i16x8",I16X8);("i32x4",I32X4);("i64x2",I64X2);("f32x4",F32X4);("f64x2",F64X2);("br_on_cast_fail",BR_ON_CAST_FAIL);("return",INSTRReturn);("call",CALL);("call_ref",CALL_REF);("call_indirect",CALL_INDIRECT);("return_call",RETURN_CALL);("return_call_ref",RETURN_CALL_REF);("return_call_indirect",RETURN_CALL_INDIRECT);("drop",INSTRDrop);("select",SELECT);("local.get",LOCAL_GET);("local.set",LOCAL_SET);("local.tee",LOCAL_TEE);("global.get",GLOBAL_GET);("global.set",GLOBAL_SET);("atomic.fence",INSTRAtomicFence);("i32.load",LOADNumI32);("i64.load",LOADNumI64);("f32.load",LOADNumF32);("f64.load",LOADNumF64);("v128.load",VEC_LOADLoad128);("v128.load8x8_s",VEC_LOADLoad8x8S);("v128.load8x8_u",VEC_LOADLoad8x8U);("v128.load16x4_s",VEC_LOADLoad16x4S);("v128.load16x4_u",VEC_LOADLoad16x4U);("v128.load32x2_s",VEC_LOADLoad32x2S);("v128.load32x2_u",VEC_LOADLoad32x2U);("v128.load8_lane",VEC_LOAD_LANE`I8);("v128.load16_lane",VEC_LOAD_LANE`I16);("v128.load32_lane",VEC_LOAD_LANE`I32);("v128.load64_lane",VEC_LOAD_LANE`I64);("v128.store8_lane",VEC_STORE_LANE`I8);("v128.store16_lane",VEC_STORE_LANE`I16);("v128.store32_lane",VEC_STORE_LANE`I32);("v128.store64_lane",VEC_STORE_LANE`I64);("v128.load8_splat",VEC_LOAD_SPLAT`I8);("v128.load16_splat",VEC_LOAD_SPLAT`I16);("v128.load32_splat",VEC_LOAD_SPLAT`I32);("v128.load64_splat",VEC_LOAD_SPLAT`I64);("v128.load32_zero",VEC_LOADLoad32Zero);("v128.load64_zero",VEC_LOADLoad64Zero);("i32.load8_u",LOADS(`I32,`I8,Unsigned));("i32.load8_s",LOADS(`I32,`I8,Signed));("i64.load8_u",LOADS(`I64,`I8,Unsigned));("i64.load8_s",LOADS(`I64,`I8,Signed));("i32.load16_u",LOADS(`I32,`I16,Unsigned));("i32.load16_s",LOADS(`I32,`I16,Signed));("i64.load16_u",LOADS(`I64,`I16,Unsigned));("i64.load16_s",LOADS(`I64,`I16,Signed));("i64.load32_u",LOADS(`I64,`I32,Unsigned));("i64.load32_s",LOADS(`I64,`I32,Signed));("i32.store",STORENumI32);("i64.store",STORENumI64);("f32.store",STORENumF32);("f64.store",STORENumF64);("v128.store",VEC_STORE);("i8x16.extract_lane_s",VEC_EXTRACT(I8x16,SomeSigned));("i8x16.extract_lane_u",VEC_EXTRACT(I8x16,SomeUnsigned));("i8x16.replace_lane",VEC_REPLACEI8x16);("i16x8.extract_lane_s",VEC_EXTRACT(I16x8,SomeSigned));("i16x8.extract_lane_u",VEC_EXTRACT(I16x8,SomeUnsigned));("i16x8.replace_lane",VEC_REPLACEI16x8);("i32x4.extract_lane",VEC_EXTRACT(I32x4,None));("i32x4.replace_lane",VEC_REPLACEI32x4);("i64x2.extract_lane",VEC_EXTRACT(I64x2,None));("i64x2.replace_lane",VEC_REPLACEI64x2);("f32x4.extract_lane",VEC_EXTRACT(F32x4,None));("f32x4.replace_lane",VEC_REPLACEF32x4);("f64x2.extract_lane",VEC_EXTRACT(F64x2,None));("f64x2.replace_lane",VEC_REPLACEF64x2);("i8x16.shuffle",VEC_SHUFFLE);("i32.store8",STORES(`I32,`I8));("i64.store8",STORES(`I64,`I8));("i32.store16",STORES(`I32,`I16));("i64.store16",STORES(`I64,`I16));("i64.store32",STORES(`I64,`I32));("memory.size",MEMORY_SIZE);("memory.grow",MEMORY_GROW);("memory.fill",MEMORY_FILL);("memory.copy",MEMORY_COPY);("memory.init",MEMORY_INIT);("data.drop",DATA_DROP);("table.get",TABLE_GET);("table.set",TABLE_SET);("table.size",TABLE_SIZE);("table.grow",TABLE_GROW);("table.fill",TABLE_FILL);("table.copy",TABLE_COPY);("table.init",TABLE_INIT);("elem.drop",ELEM_DROP);("ref.null",REF_NULL);("ref.func",REF_FUNC);("ref.is_null",INSTRRefIsNull);("ref.as_non_null",INSTRRefAsNonNull);("ref.eq",INSTRRefEq);("ref.test",REF_TEST);("ref.cast",REF_CAST);("ref.cast_desc_eq",REF_CAST_DESC_EQ);("ref.get_desc",REF_GET_DESC);("struct.new",STRUCT_NEW);("struct.new_default",STRUCT_NEW_DEFAULT);("struct.new_desc",STRUCT_NEW_DESC);("struct.new_default_desc",STRUCT_NEW_DEFAULT_DESC);("struct.get",STRUCT_GETNone);("struct.get_u",STRUCT_GET(SomeUnsigned));("struct.get_s",STRUCT_GET(SomeSigned));("struct.set",STRUCT_SET);("array.new",ARRAY_NEW);("array.new_default",ARRAY_NEW_DEFAULT);("array.new_fixed",ARRAY_NEW_FIXED);("array.new_data",ARRAY_NEW_DATA);("array.new_elem",ARRAY_NEW_ELEM);("array.get",ARRAY_GETNone);("array.get_u",ARRAY_GET(SomeUnsigned));("array.get_s",ARRAY_GET(SomeSigned));("array.set",ARRAY_SET);("array.len",INSTRArrayLen);("array.fill",ARRAY_FILL);("array.copy",ARRAY_COPY);("array.init_data",ARRAY_INIT_DATA);("array.init_elem",ARRAY_INIT_ELEM);("ref.i31",INSTRRefI31);("i31.get_s",INSTR(I31GetSigned));("i31.get_u",INSTR(I31GetUnsigned));("i32.const",I32_CONST);("i64.const",I64_CONST);("f32.const",F32_CONST);("f64.const",F64_CONST);("v128.const",V128_CONST);("i32.clz",INSTR(UnOp(I32Clz)));("i32.ctz",INSTR(UnOp(I32Ctz)));("i32.popcnt",INSTR(UnOp(I32Popcnt)));("i32.eqz",INSTR(UnOp(I32Eqz)));("i32.add",INSTR(BinOp(I32Add)));("i32.sub",INSTR(BinOp(I32Sub)));("i32.mul",INSTR(BinOp(I32Mul)));("i32.div_s",INSTR(BinOp(I32(DivSigned))));("i32.div_u",INSTR(BinOp(I32(DivUnsigned))));("i32.rem_s",INSTR(BinOp(I32(RemSigned))));("i32.rem_u",INSTR(BinOp(I32(RemUnsigned))));("i32.and",INSTR(BinOp(I32And)));("i32.or",INSTR(BinOp(I32Or)));("i32.xor",INSTR(BinOp(I32Xor)));("i32.shl",INSTR(BinOp(I32Shl)));("i32.shr_s",INSTR(BinOp(I32(ShrSigned))));("i32.shr_u",INSTR(BinOp(I32(ShrUnsigned))));("i32.rotl",INSTR(BinOp(I32Rotl)));("i32.rotr",INSTR(BinOp(I32Rotr)));("i32.eq",INSTR(BinOp(I32Eq)));("i32.ne",INSTR(BinOp(I32Ne)));("i32.lt_s",INSTR(BinOp(I32(LtSigned))));("i32.lt_u",INSTR(BinOp(I32(LtUnsigned))));("i32.gt_s",INSTR(BinOp(I32(GtSigned))));("i32.gt_u",INSTR(BinOp(I32(GtUnsigned))));("i32.le_s",INSTR(BinOp(I32(LeSigned))));("i32.le_u",INSTR(BinOp(I32(LeUnsigned))));("i32.ge_s",INSTR(BinOp(I32(GeSigned))));("i32.ge_u",INSTR(BinOp(I32(GeUnsigned))));("i64.clz",INSTR(UnOp(I64Clz)));("i64.ctz",INSTR(UnOp(I64Ctz)));("i64.popcnt",INSTR(UnOp(I64Popcnt)));("i64.eqz",INSTR(UnOp(I64Eqz)));("i64.add",INSTR(BinOp(I64Add)));("i64.sub",INSTR(BinOp(I64Sub)));("i64.mul",INSTR(BinOp(I64Mul)));("i64.div_s",INSTR(BinOp(I64(DivSigned))));("i64.div_u",INSTR(BinOp(I64(DivUnsigned))));("i64.rem_s",INSTR(BinOp(I64(RemSigned))));("i64.rem_u",INSTR(BinOp(I64(RemUnsigned))));("i64.and",INSTR(BinOp(I64And)));("i64.or",INSTR(BinOp(I64Or)));("i64.xor",INSTR(BinOp(I64Xor)));("i64.shl",INSTR(BinOp(I64Shl)));("i64.shr_s",INSTR(BinOp(I64(ShrSigned))));("i64.shr_u",INSTR(BinOp(I64(ShrUnsigned))));("i64.rotl",INSTR(BinOp(I64Rotl)));("i64.rotr",INSTR(BinOp(I64Rotr)));("i64.add128",INSTRAdd128);("i64.sub128",INSTRSub128);("i64.mul_wide_s",INSTR(MulWideSigned));("i64.mul_wide_u",INSTR(MulWideUnsigned));("i64.eq",INSTR(BinOp(I64Eq)));("i64.ne",INSTR(BinOp(I64Ne)));("i64.lt_s",INSTR(BinOp(I64(LtSigned))));("i64.lt_u",INSTR(BinOp(I64(LtUnsigned))));("i64.gt_s",INSTR(BinOp(I64(GtSigned))));("i64.gt_u",INSTR(BinOp(I64(GtUnsigned))));("i64.le_s",INSTR(BinOp(I64(LeSigned))));("i64.le_u",INSTR(BinOp(I64(LeUnsigned))));("i64.ge_s",INSTR(BinOp(I64(GeSigned))));("i64.ge_u",INSTR(BinOp(I64(GeUnsigned))));("f32.abs",INSTR(UnOp(F32Abs)));("f32.neg",INSTR(UnOp(F32Neg)));("f32.ceil",INSTR(UnOp(F32Ceil)));("f32.floor",INSTR(UnOp(F32Floor)));("f32.trunc",INSTR(UnOp(F32Trunc)));("f32.nearest",INSTR(UnOp(F32Nearest)));("f32.sqrt",INSTR(UnOp(F32Sqrt)));("f32.add",INSTR(BinOp(F32Add)));("f32.sub",INSTR(BinOp(F32Sub)));("f32.mul",INSTR(BinOp(F32Mul)));("f32.div",INSTR(BinOp(F32Div)));("f32.min",INSTR(BinOp(F32Min)));("f32.max",INSTR(BinOp(F32Max)));("f32.copysign",INSTR(BinOp(F32CopySign)));("f32.eq",INSTR(BinOp(F32Eq)));("f32.ne",INSTR(BinOp(F32Ne)));("f32.lt",INSTR(BinOp(F32Lt)));("f32.gt",INSTR(BinOp(F32Gt)));("f32.le",INSTR(BinOp(F32Le)));("f32.ge",INSTR(BinOp(F32Ge)));("f64.abs",INSTR(UnOp(F64Abs)));("f64.neg",INSTR(UnOp(F64Neg)));("f64.ceil",INSTR(UnOp(F64Ceil)));("f64.floor",INSTR(UnOp(F64Floor)));("f64.trunc",INSTR(UnOp(F64Trunc)));("f64.nearest",INSTR(UnOp(F64Nearest)));("f64.sqrt",INSTR(UnOp(F64Sqrt)));("f64.add",INSTR(BinOp(F64Add)));("f64.sub",INSTR(BinOp(F64Sub)));("f64.mul",INSTR(BinOp(F64Mul)));("f64.div",INSTR(BinOp(F64Div)));("f64.min",INSTR(BinOp(F64Min)));("f64.max",INSTR(BinOp(F64Max)));("f64.copysign",INSTR(BinOp(F64CopySign)));("f64.eq",INSTR(BinOp(F64Eq)));("f64.ne",INSTR(BinOp(F64Ne)));("f64.lt",INSTR(BinOp(F64Lt)));("f64.gt",INSTR(BinOp(F64Gt)));("f64.le",INSTR(BinOp(F64Le)));("f64.ge",INSTR(BinOp(F64Ge)));("v128.bitselect",INSTRVecBitselect);("f32x4.relaxed_madd",VEC_TERN_OP(VecRelaxedMAdd`F32));("f32x4.relaxed_nmadd",VEC_TERN_OP(VecRelaxedNMAdd`F32));("f64x2.relaxed_madd",VEC_TERN_OP(VecRelaxedMAdd`F64));("f64x2.relaxed_nmadd",VEC_TERN_OP(VecRelaxedNMAdd`F64));("i8x16.relaxed_laneselect",VEC_TERN_OP(VecRelaxedLaneSelectI8x16));("i16x8.relaxed_laneselect",VEC_TERN_OP(VecRelaxedLaneSelectI16x8));("i32x4.relaxed_laneselect",VEC_TERN_OP(VecRelaxedLaneSelectI32x4));("i64x2.relaxed_laneselect",VEC_TERN_OP(VecRelaxedLaneSelectI64x2));("i32x4.relaxed_dot_i8x16_i7x16_add_s",VEC_TERN_OPVecRelaxedDotAdd);("i32.wrap_i64",INSTRI32WrapI64);("i32.trunc_f32_s",INSTR(UnOp(I32(Trunc(`F32,Signed)))));("i32.trunc_f32_u",INSTR(UnOp(I32(Trunc(`F32,Unsigned)))));("i32.trunc_f64_s",INSTR(UnOp(I32(Trunc(`F64,Signed)))));("i32.trunc_f64_u",INSTR(UnOp(I32(Trunc(`F64,Unsigned)))));("i32.trunc_sat_f32_s",INSTR(UnOp(I32(TruncSat(`F32,Signed)))));("i32.trunc_sat_f32_u",INSTR(UnOp(I32(TruncSat(`F32,Unsigned)))));("i32.trunc_sat_f64_s",INSTR(UnOp(I32(TruncSat(`F64,Signed)))));("i32.trunc_sat_f64_u",INSTR(UnOp(I32(TruncSat(`F64,Unsigned)))));("i64.extend_i32_s",INSTR(I64ExtendI32Signed));("i64.extend_i32_u",INSTR(I64ExtendI32Unsigned));("i64.trunc_f32_s",INSTR(UnOp(I64(Trunc(`F32,Signed)))));("i64.trunc_f32_u",INSTR(UnOp(I64(Trunc(`F32,Unsigned)))));("i64.trunc_f64_s",INSTR(UnOp(I64(Trunc(`F64,Signed)))));("i64.trunc_f64_u",INSTR(UnOp(I64(Trunc(`F64,Unsigned)))));("i64.trunc_sat_f32_s",INSTR(UnOp(I64(TruncSat(`F32,Signed)))));("i64.trunc_sat_f32_u",INSTR(UnOp(I64(TruncSat(`F32,Unsigned)))));("i64.trunc_sat_f64_s",INSTR(UnOp(I64(TruncSat(`F64,Signed)))));("i64.trunc_sat_f64_u",INSTR(UnOp(I64(TruncSat(`F64,Unsigned)))));("f32.convert_i32_s",INSTR(UnOp(F32(Convert(`I32,Signed)))));("f32.convert_i32_u",INSTR(UnOp(F32(Convert(`I32,Unsigned)))));("f32.convert_i64_s",INSTR(UnOp(F32(Convert(`I64,Signed)))));("f32.convert_i64_u",INSTR(UnOp(F32(Convert(`I64,Unsigned)))));("f32.demote_f64",INSTRF32DemoteF64);("f64.convert_i32_s",INSTR(UnOp(F64(Convert(`I32,Signed)))));("f64.convert_i32_u",INSTR(UnOp(F64(Convert(`I32,Unsigned)))));("f64.convert_i64_s",INSTR(UnOp(F64(Convert(`I64,Signed)))));("f64.convert_i64_u",INSTR(UnOp(F64(Convert(`I64,Unsigned)))));("f64.promote_f32",INSTRF64PromoteF32);("i32.reinterpret_f32",INSTR(UnOp(I32Reinterpret)));("i64.reinterpret_f64",INSTR(UnOp(I64Reinterpret)));("f32.reinterpret_i32",INSTR(UnOp(F32Reinterpret)));("f64.reinterpret_i64",INSTR(UnOp(F64Reinterpret)));("i32.extend8_s",INSTR(UnOp(I32(ExtendS`_8))));("i32.extend16_s",INSTR(UnOp(I32(ExtendS`_16))));("i64.extend8_s",INSTR(UnOp(I64(ExtendS`_8))));("i64.extend16_s",INSTR(UnOp(I64(ExtendS`_16))));("i64.extend32_s",INSTR(UnOp(I64(ExtendS`_32))));("extern.convert_any",INSTRExternConvertAny);("any.convert_extern",INSTRAnyConvertExtern);("tag",TAG);("try",TRY);("try_table",TRY_TABLE);("do",DO);("catch",CATCH);("catch_ref",LPAREN_CATCH_REF);("catch_all",CATCH_ALL);("catch_all_ref",LPAREN_CATCH_ALL_REF);("throw",THROW);("throw_ref",THROW_REF);("cont.new",CONT_NEW);("cont.bind",CONT_BIND);("suspend",SUSPEND);("resume",RESUME);("resume_throw",RESUME_THROW);("resume_throw_ref",RESUME_THROW_REF);("switch",SWITCH);("on",LPAREN_ON);("definition",DEFINITION);("binary",BINARY);("quote",QUOTE);("instance",INSTANCE);("register",REGISTER);("invoke",INVOKE);("get",GET);("thread",THREAD);("wait",WAIT);("ref.host",REF_HOST);("assert_return",ASSERT_RETURN);("assert_exception",ASSERT_EXCEPTION);("assert_suspension",ASSERT_SUSPENSION);("assert_trap",ASSERT_TRAP);("assert_exhaustion",ASSERT_EXHAUSTION);("assert_malformed",ASSERT_MALFORMED);("assert_malformed_custom",ASSERT_MALFORMED_CUSTOM);("assert_invalid",ASSERT_INVALID);("assert_invalid_custom",ASSERT_INVALID_CUSTOM);("assert_unlinkable",ASSERT_UNLINKABLE);("assert_return_arithmetic_nan",ASSERT_RETURN_NAN);("assert_return_canonical_nan",ASSERT_RETURN_NAN);("nan:canonical",NAN);("nan:arithmetic",NAN);("ref.extern",REF_EXTERN);("ref.struct",REF_STRUCT);("ref.array",REF_ARRAY);("either",EITHER);("script",SCRIPT);("input",INPUT);("output",OUTPUT(* Operators of conditional annotation conditions *));("and",AND);("or",OR);("not",NOT);]inleth=Hashtbl.create(List.lengthentries+128)in(* Atomic mnemonics, from the shared Atomics table (its [of_name] lived here). *)List.iter(funop->Hashtbl.replaceh(Atomics.nameop)(ATOMICop))Atomics.all;(* Plain vector instructions (splat/unop/binop/shift/test/bitmask), from the
shared Simd registry. *)List.iter(fund->Option.iter(funn->Hashtbl.replacehn(INSTRd))(Simd.wat_mnemonicd))Simd.plain_vec_instrs;(* Regular keywords last, so they win on any name collision with an atomic. *)List.iter(fun(k,v)->Hashtbl.replacehkv)entries;hletrectoken_recctxlexbuf=match%sedlexlexbufwith|'('->LPAREN|')'->RPAREN|uN->NAT(Sedlexing.Utf8.lexemelexbuf)|sN->INT(Sedlexing.Utf8.lexemelexbuf)|fN->FLOAT(Sedlexing.Utf8.lexemelexbuf)|'"'->STRING(with_locctxstringlexbuf)|"$\""->lets=with_locctxstringlexbufinifnot(String.is_valid_utf_8s.desc)thenraise(Parsing.Syntax_error((s.info.loc_start,s.info.loc_end),Wax_utils.Message.text"Identifier contains malformed UTF-8 byte sequences"));ifs.desc=""thenraise(Parsing.Syntax_error((s.info.loc_start,s.info.loc_end),Wax_utils.Message.text"An identifier cannot be the empty string"));IDs|newline->Wax_utils.Trivia.report_newlinectx;token_recctxlexbuf(* Skip standalone newlines in Wat *)|linecomment->letcontent=Sedlexing.Utf8.lexemelexbufinWax_utils.Trivia.report_itemctxLine_commentcontent;token_recctxlexbuf|Plus(' '|'\t')->token_recctxlexbuf|"(;"->lets=commentlexbufinWax_utils.Trivia.report_itemctxBlock_comments;token_recctxlexbuf|"(@string"->STRING_ANNOT|"(@char"->CHAR_ANNOT|"(@if"->IF_ANNOT|"(@then"->THEN_ANNOT|"(@else"->ELSE_ANNOT|"(@feature"->FEATURE_ANNOT(* Branch-hinting proposal: [(@metadata.code.branch_hint "\00"|"\01")]. *)|"(@metadata.code.branch_hint"->BRANCH_HINT_ANNOT|"(@",Plusidchar->skip_annotation1lexbuf;Wax_utils.Trivia.report_itemctxAnnotation"";token_recctxlexbuf|"(@\""->lets=stringlexbufinifs="string"thenSTRING_ANNOTelseifs="char"thenCHAR_ANNOTelseifs="if"thenIF_ANNOTelseifs="then"thenTHEN_ANNOTelseifs="else"thenELSE_ANNOTelseifs="feature"thenFEATURE_ANNOTelse(ifnot(String.is_valid_utf_8s)thenraise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text"The annotation id contains malformed UTF-8 byte sequences."));ifs=""thenraise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text"An annotation id cannot be the empty string."));skip_annotation1lexbuf;Wax_utils.Trivia.report_itemctxAnnotation"";token_recctxlexbuf)|id->letloc_start,loc_end=Sedlexing.lexing_bytes_positionslexbufinID(Wax_utils.Trivia.with_posctx{Ast.loc_start;loc_end}(Sedlexing.Utf8.sub_lexemelexbuf1(Sedlexing.lexeme_lengthlexbuf-1)))|eof->EOF|"align=",uN->MEM_ALIGN(Sedlexing.Utf8.sub_lexemelexbuf6(Sedlexing.lexeme_lengthlexbuf-6))|"offset=",uN->MEM_OFFSET(Sedlexing.Utf8.sub_lexemelexbuf7(Sedlexing.lexeme_lengthlexbuf-7))|"="->CMP_EQ|"<>"->CMP_NE|"<"->CMP_LT|">"->CMP_GT|"<="->CMP_LE|">="->CMP_GE|keyword->(lets=Sedlexing.Utf8.lexemelexbufinmatchHashtbl.find_optkeyword_tableswith|Sometok->tok|None->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Unknown keyword '%s'.\n"s))))|reserved,Opt'"'->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Unknown token '%s'.\n"(Sedlexing.Utf8.lexemelexbuf))))|_->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Syntax error.\n")))(*** Entry points ***)lettokenctx=letprev_token=refNonein(* A compound opener — [(param], [(then], [(catch], … — is two lexemes: the
[(] is read first (stashed as [LPAREN]) and the keyword next, so the
lexbuf's reported start would be the keyword's, not the [(]'s. Capture the
[(] position when it is stashed and, when the pair is combined into one
token, report it as that token's start via [start_override] (read by the
supplier in [Parsing]). The combined token's span thus really begins at its
opening parenthesis. *)letparen_start=refLexing.dummy_posinletstart_override=refNoneinletflexbuf=start_override:=None;letrecloop()=lett=token_recctxlexbufinletend_=Sedlexing.lexing_bytes_position_currlexbufinWax_utils.Trivia.report_tokenctxend_.pos_cnum;(* Emit a token combined from the stashed [(] and its keyword, starting at
the [(]. *)letcombinedtok=prev_token:=None;start_override:=Some!paren_start;tokinmatch(!prev_token,t)with|(None,(LPAREN_CATCH_ALL_REF|LPAREN_CATCH_REF|LPAREN_EXPORT|LPAREN_IMPORT|LPAREN_LOCAL|LPAREN_PARAM|LPAREN_RESULT|LPAREN_THEN|LPAREN_TYPE|LPAREN_ON|LPAREN_DESCRIPTOR|LPAREN_DESCRIBES))->raise(Parsing.Syntax_error(Sedlexing.lexing_bytes_positionslexbuf,Wax_utils.Message.text(Printf.sprintf"Unexpected keyword '%s'.\n"(Sedlexing.Utf8.lexemelexbuf))))|None,LPAREN->paren_start:=fst(Sedlexing.lexing_bytes_positionslexbuf);prev_token:=Somet;loop()|None,_->t|SomeLPAREN,CATCH->combinedLPAREN_CATCH|SomeLPAREN,CATCH_ALL->combinedLPAREN_CATCH_ALL|(SomeLPAREN,(LPAREN_CATCH_ALL_REF|LPAREN_CATCH_REF|LPAREN_EXPORT|LPAREN_IMPORT|LPAREN_LOCAL|LPAREN_PARAM|LPAREN_RESULT|LPAREN_THEN|LPAREN_TYPE|LPAREN_ON|LPAREN_DESCRIPTOR|LPAREN_DESCRIBES))->combinedt|Somet',_->prev_token:=Somet;t'inmatch!prev_tokenwith|Sometwhent<>LPAREN->prev_token:=None;t|_->loop()in(f,start_override)letis_valid_identifiers=letbuf=Sedlexing.Utf8.from_stringsinmatch%sedlexbufwithPlusidchar,eof->true|_->false