123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510(* SPDX-License-Identifier: MIT *)(* Copyright (C) 2023-2024 formalsec *)(* Written by the Smtml programmers *)type_cast=|C8:intcast|C32:int32cast|C64:int64casttypet=|Ty_app|Ty_bitvofint|Ty_bool|Ty_fpofint|Ty_int|Ty_list|Ty_none|Ty_real|Ty_str|Ty_unit|Ty_regexp|Ty_roundingModeletdiscr=function|Ty_app->0|Ty_bool->1|Ty_int->2|Ty_list->3|Ty_none->4|Ty_real->5|Ty_str->6|Ty_unit->7|Ty_regexp->8|Ty_roundingMode->9|Ty_bitvn->10+n|Ty_fpn->11+nletcomparet1t2=compare(discrt1)(discrt2)letequalt1t2=comparet1t2=0letppfmt=function|Ty_int->Fmt.stringfmt"int"|Ty_real->Fmt.stringfmt"real"|Ty_bool->Fmt.stringfmt"bool"|Ty_str->Fmt.stringfmt"str"|Ty_bitvn->Fmt.pffmt"i%d"n|Ty_fpn->Fmt.pffmt"f%d"n|Ty_list->Fmt.stringfmt"list"|Ty_app->Fmt.stringfmt"app"|Ty_unit->Fmt.stringfmt"unit"|Ty_none->Fmt.stringfmt"none"|Ty_regexp->Fmt.stringfmt"regexp"|Ty_roundingMode->Fmt.stringfmt"RoudingMode"letstring_of_type(ty:t):string=Fmt.str"%a"pptyletof_string=function|"int"->OkTy_int|"real"->OkTy_real|"bool"->OkTy_bool|"str"->OkTy_str|"list"->OkTy_list|"app"->OkTy_app|"unit"->OkTy_unit|"none"->OkTy_none|"regexp"->OkTy_regexp|"RoundingMode"->OkTy_roundingMode|s->ifString.starts_with~prefix:"i"sthenbeginlets=String.subs1(String.lengths-1)inmatchint_of_string_optswith|None->Fmt.error_msg"can not parse type %s"s|Somenwhenn<0->Fmt.error_msg"size of bitvectors must be a positive integer"|Somen->Ok(Ty_bitvn)endelseifString.starts_with~prefix:"f"sthenbeginlets=String.subs1(String.lengths-1)inmatchint_of_string_optswith|None->Fmt.error_msg"can not parse type %s"s|Somenwhenn<0->Fmt.error_msg"size of fp must be a positive integer"|Somen->Ok(Ty_fpn)endelseFmt.error_msg"can not parse type %s"sletsize(ty:t):int=matchtywith|Ty_bitvn|Ty_fpn->n/8|Ty_int|Ty_bool->4|Ty_real|Ty_str|Ty_list|Ty_app|Ty_unit|Ty_none|Ty_regexp|Ty_roundingMode->assertfalsemoduleUnop=structtypet=|Neg|Not|Clz|Ctz|Popcnt(* Float *)|Abs|Sqrt|Is_normal|Is_subnormal|Is_negative|Is_positive|Is_infinite|Is_nan|Is_zero|Ceil|Floor|Trunc|Nearest|Head|Tail|Reverse|Length(* String *)|Trim(* RegExp *)|Regexp_star|Regexp_loopof(int*int)|Regexp_plus|Regexp_opt|Regexp_completequalo1o2=match(o1,o2)with|Neg,Neg|Not,Not|Clz,Clz|Popcnt,Popcnt|Ctz,Ctz|Abs,Abs|Sqrt,Sqrt|Is_normal,Is_normal|Is_subnormal,Is_subnormal|Is_negative,Is_negative|Is_positive,Is_positive|Is_infinite,Is_infinite|Is_nan,Is_nan|Is_zero,Is_zero|Ceil,Ceil|Floor,Floor|Trunc,Trunc|Nearest,Nearest|Head,Head|Tail,Tail|Reverse,Reverse|Length,Length|Trim,Trim|Regexp_star,Regexp_star|Regexp_loop_,Regexp_loop_|Regexp_plus,Regexp_plus|Regexp_opt,Regexp_opt|Regexp_comp,Regexp_comp->true|((Neg|Not|Clz|Popcnt|Ctz|Abs|Sqrt|Is_normal|Is_subnormal|Is_negative|Is_positive|Is_infinite|Is_nan|Is_zero|Ceil|Floor|Trunc|Nearest|Head|Tail|Reverse|Length|Trim|Regexp_star|Regexp_loop_|Regexp_plus|Regexp_opt|Regexp_comp),_)->falseletppfmt=function|Neg->Fmt.stringfmt"neg"|Not->Fmt.stringfmt"not"|Clz->Fmt.stringfmt"clz"|Ctz->Fmt.stringfmt"ctz"|Popcnt->Fmt.stringfmt"popcnt"|Abs->Fmt.stringfmt"abs"|Sqrt->Fmt.stringfmt"sqrt"|Is_normal->Fmt.stringfmt"isNormal"|Is_subnormal->Fmt.stringfmt"isSubnormal"|Is_negative->Fmt.stringfmt"isNegative"|Is_positive->Fmt.stringfmt"isPositive"|Is_infinite->Fmt.stringfmt"isInfinite"|Is_nan->Fmt.stringfmt"is_nan"|Is_zero->Fmt.stringfmt"isZero"|Ceil->Fmt.stringfmt"ceil"|Floor->Fmt.stringfmt"floor"|Trunc->Fmt.stringfmt"trunc"|Nearest->Fmt.stringfmt"nearest"|Head->Fmt.stringfmt"head"|Tail->Fmt.stringfmt"tail"|Reverse->Fmt.stringfmt"reverse"|Length->Fmt.stringfmt"length"|Trim->Fmt.stringfmt"trim"|Regexp_star->Fmt.stringfmt"*"|Regexp_loop_->Fmt.stringfmt"loop"|Regexp_plus->Fmt.stringfmt"+"|Regexp_opt->Fmt.stringfmt"opt"|Regexp_comp->Fmt.stringfmt"comp"endmoduleBinop=structtypet=|Add|Sub|Mul|Div|DivU|Rem|RemU|Shl|ShrA|ShrL|And|Or|Xor|Implies|Pow|Min|Max|Copysign|Rotl|Rotr|At|List_cons|List_append(* String *)|String_prefix|String_suffix|String_contains|String_last_index|String_in_re(* Regexp *)|Regexp_range|Regexp_interletequalo1o2=match(o1,o2)with|Add,Add|Sub,Sub|Mul,Mul|Div,Div|DivU,DivU|Rem,Rem|RemU,RemU|Shl,Shl|ShrA,ShrA|ShrL,ShrL|And,And|Or,Or|Xor,Xor|Implies,Implies|Pow,Pow|Min,Min|Max,Max|Copysign,Copysign|Rotl,Rotl|Rotr,Rotr|At,At|List_cons,List_cons|List_append,List_append|String_prefix,String_prefix|String_suffix,String_suffix|String_contains,String_contains|String_last_index,String_last_index|String_in_re,String_in_re|Regexp_range,Regexp_range|Regexp_inter,Regexp_inter->true|((Add|Sub|Mul|Div|DivU|Rem|RemU|Shl|ShrA|ShrL|And|Or|Xor|Implies|Pow|Min|Max|Copysign|Rotl|Rotr|At|List_cons|List_append|String_prefix|String_suffix|String_contains|String_last_index|String_in_re|Regexp_range|Regexp_inter),_)->falseletppfmt=function|Add->Fmt.stringfmt"add"|Sub->Fmt.stringfmt"sub"|Mul->Fmt.stringfmt"mul"|Div->Fmt.stringfmt"div_s"|DivU->Fmt.stringfmt"div_u"|Rem->Fmt.stringfmt"rem_s"|RemU->Fmt.stringfmt"rem_u"|Shl->Fmt.stringfmt"shl"|ShrA->Fmt.stringfmt"shr_s"|ShrL->Fmt.stringfmt"shr_u"|And->Fmt.stringfmt"and"|Or->Fmt.stringfmt"or"|Xor->Fmt.stringfmt"xor"|Implies->Fmt.stringfmt"=>"|Pow->Fmt.stringfmt"pow"|Min->Fmt.stringfmt"min"|Max->Fmt.stringfmt"max"|Copysign->Fmt.stringfmt"copysign"|Rotl->Fmt.stringfmt"rotl"|Rotr->Fmt.stringfmt"rotr"|At->Fmt.stringfmt"at"|List_cons->Fmt.stringfmt"cons"|List_append->Fmt.stringfmt"append"|String_prefix->Fmt.stringfmt"prefixof"|String_suffix->Fmt.stringfmt"suffixof"|String_contains->Fmt.stringfmt"contains"|String_last_index->Fmt.stringfmt"last_indexof"|String_in_re->Fmt.stringfmt"in_re"|Regexp_range->Fmt.stringfmt"range"|Regexp_inter->Fmt.stringfmt"inter"endmoduleRelop=structtypet=|Eq|Ne|Lt|LtU|Gt|GtU|Le|LeU|Ge|GeUletequalop1op2=match(op1,op2)with|Eq,Eq|Ne,Ne|Lt,Lt|LtU,LtU|Gt,Gt|GtU,GtU|Le,Le|LeU,LeU|Ge,Ge|GeU,GeU->true|(Eq|Ne|Lt|LtU|Gt|GtU|Le|LeU|Ge|GeU),_->falseletppfmt=function|Eq->Fmt.stringfmt"eq"|Ne->Fmt.stringfmt"ne"|Lt->Fmt.stringfmt"lt_s"|LtU->Fmt.stringfmt"lt_u"|Gt->Fmt.stringfmt"gt_s"|GtU->Fmt.stringfmt"gt_u"|Le->Fmt.stringfmt"le_s"|LeU->Fmt.stringfmt"le_u"|Ge->Fmt.stringfmt"ge_s"|GeU->Fmt.stringfmt"ge_u"endmoduleTriop=structtypet=|Ite|List_set(* String *)|String_extract|String_replace|String_index|String_replace_allletequalop1op2=match(op1,op2)with|Ite,Ite|List_set,List_set|String_extract,String_extract|String_replace,String_replace|String_index,String_index|String_replace_all,String_replace_all->true|((Ite|List_set|String_extract|String_replace|String_index|String_replace_all),_)->falseletppfmt=function|Ite->Fmt.stringfmt"ite"|String_extract->Fmt.stringfmt"substr"|String_replace->Fmt.stringfmt"replace"|String_index->Fmt.stringfmt"indexof"|String_replace_all->Fmt.stringfmt"replace_all"|List_set->Fmt.stringfmt"set"endmoduleCvtop=structtypet=|ToString|OfString|ToBool|OfBool|Reinterpret_int|Reinterpret_float|DemoteF64|PromoteF32|ConvertSI32|ConvertUI32|ConvertSI64|ConvertUI64|TruncSF32|TruncUF32|TruncSF64|TruncUF64|Trunc_sat_f32_s|Trunc_sat_f32_u|Trunc_sat_f64_s|Trunc_sat_f64_u|WrapI64|Sign_extendofint|Zero_extendofint(* String *)|String_to_code|String_from_code|String_to_int|String_from_int|String_to_float|String_to_reletequalop1op2=match(op1,op2)with|ToString,ToString|OfString,OfString|ToBool,ToBool|OfBool,OfBool|Reinterpret_int,Reinterpret_int|Reinterpret_float,Reinterpret_float|DemoteF64,DemoteF64|PromoteF32,PromoteF32|ConvertSI32,ConvertSI32|ConvertUI32,ConvertUI32|ConvertSI64,ConvertSI64|ConvertUI64,ConvertUI64|TruncSF32,TruncSF32|TruncUF32,TruncUF32|TruncSF64,TruncSF64|TruncUF64,TruncUF64|Trunc_sat_f32_s,Trunc_sat_f32_s|Trunc_sat_f32_u,Trunc_sat_f32_u|Trunc_sat_f64_s,Trunc_sat_f64_s|Trunc_sat_f64_u,Trunc_sat_f64_u|WrapI64,WrapI64|String_to_code,String_to_code|String_from_code,String_from_code|String_to_int,String_to_int|String_from_int,String_from_int|String_to_float,String_to_float|String_to_re,String_to_re->true|Sign_extendx1,Sign_extendx2|Zero_extendx1,Zero_extendx2->x1=x2|((ToString|OfString|ToBool|OfBool|Reinterpret_int|Reinterpret_float|DemoteF64|PromoteF32|ConvertSI32|ConvertUI32|ConvertSI64|ConvertUI64|TruncSF32|TruncUF32|TruncSF64|TruncUF64|Trunc_sat_f32_s|Trunc_sat_f32_u|Trunc_sat_f64_s|Trunc_sat_f64_u|WrapI64|Sign_extend_|Zero_extend_|String_to_code|String_from_code|String_to_int|String_from_int|String_to_float|String_to_re),_)->falseletppfmt=function|ToString->Fmt.stringfmt"to_string"|OfString->Fmt.stringfmt"of_string"|ToBool->Fmt.stringfmt"to_bool"|OfBool->Fmt.stringfmt"of_bool"|Reinterpret_int->Fmt.stringfmt"reinterpret_int"|Reinterpret_float->Fmt.stringfmt"reinterpret_float"|DemoteF64->Fmt.stringfmt"demote_f64"|PromoteF32->Fmt.stringfmt"promote_f32"|ConvertSI32->Fmt.stringfmt"convert_i32_s"|ConvertUI32->Fmt.stringfmt"convert_i32_u"|ConvertSI64->Fmt.stringfmt"convert_i64_s"|ConvertUI64->Fmt.stringfmt"convert_i64_u"|TruncSF32->Fmt.stringfmt"trunc_f32_s"|TruncUF32->Fmt.stringfmt"trunc_f32_u"|TruncSF64->Fmt.stringfmt"trunc_f64_s"|TruncUF64->Fmt.stringfmt"trunc_f64_u"|Trunc_sat_f32_s->Fmt.stringfmt"trunc_sat_f32_s"|Trunc_sat_f32_u->Fmt.stringfmt"trunc_sat_f32_u"|Trunc_sat_f64_s->Fmt.stringfmt"trunc_sat_f64_s"|Trunc_sat_f64_u->Fmt.stringfmt"trunc_sat_f64_u"|WrapI64->Fmt.stringfmt"wrap_i64"|Sign_extendsz->Fmt.pffmt"extend_i%d_s"sz|Zero_extendsz->Fmt.pffmt"extend_i%d_u"sz|String_to_code->Fmt.stringfmt"to_code"|String_from_code->Fmt.stringfmt"from_code"|String_to_int->Fmt.stringfmt"to_int"|String_from_int->Fmt.stringfmt"from_int"|String_to_float->Fmt.stringfmt"to_float"|String_to_re->Fmt.stringfmt"to_re"endmoduleNaryop=structtypet=|Logand|Logor|Concat|Regexp_unionletequalop1op2=match(op1,op2)with|Logand,Logand|Logor,Logor|Concat,Concat|Regexp_union,Regexp_union->true|(Logand|Logor|Concat|Regexp_union),_->falseletppfmt=function|Logand->Fmt.stringfmt"and"|Logor->Fmt.stringfmt"or"|Concat->Fmt.stringfmt"++"|Regexp_union->Fmt.stringfmt"union"end