123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132(**************************************************************************)(* This file is part of the Codex semantics library. *)(* *)(* Copyright (C) 2013-2025 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file LICENSE). *)(* *)(**************************************************************************)moduleIn_bits=Units.In_bitsmoduleMake(Terms:Sig.TERMS)=structmoduleTC=Operator.Function_symbolexceptionEmptyexceptionNot_a_constantletwraps~(size:In_bits.t)x=Z.signed_extractx0(size:>int)letwrapu~(size:In_bits.t)x=Z.extractx0(size:>int)(* For binaries, we return a Z whose last size bits correspond to
the requested bitvector. *)letrecbinaryx=matchxwith|Terms.(Binary{term=T0{tag=TC.Biconst(size,k)}})->k|Terms.(Binary{term=T1{tag;a};size})->beginmatchtagwith|TC.Bextract{size;oldsize;index}->Z.extract(binarya)(index:>int)(size:>int)|TC.Buext_->letTerms.Binary{size=oldsize}=ainwrapu~size:oldsize(binarya)|TC.Bsext_->letTerms.Binary{size=oldsize}=ainwraps~size:oldsize(binarya)|TC.Bofbool_->assertfalse|TC.Bchoose_->binaryaend|Terms.(Binary{term=T2{tag;a;b};size})->begintrymatchtagwith|TC.Biadd_->Z.add(binarya)(binaryb)|TC.Bisub_->Z.sub(binarya)(binaryb)|TC.Bimul_->Z.mul(binarya)(binaryb)|TC.Biudiv_->Z.div(wrapu~size@@binarya)(wrapu~size@@binaryb)|TC.Bisdiv_->Z.div(wraps~size@@binarya)(wraps~size@@binaryb)|TC.Biumod_->Z.rem(wrapu~size@@binarya)(wrapu~size@@binaryb)|TC.Bismod_->Z.rem(wraps~size@@binarya)(wraps~size@@binaryb)|TC.Bshl_->Z.shift_left(binarya)(Z.to_int@@binaryb)|TC.Bashr_->Z.shift_right(wraps~size@@binarya)(Z.to_int@@binaryb)|TC.Blshr_->Z.shift_right(wrapu~size@@binarya)(Z.to_int@@binaryb)|TC.Band_->Z.logand(binarya)(binaryb)|TC.Bor_->Z.logor(binarya)(binaryb)|TC.Bxor_->Z.logxor(binarya)(binaryb)|TC.Bunion_->assertfalse|TC.Bconcat(size1,size2)->Z.logor(Z.shift_left(wrapu~size:size1@@binarya)(size2:>int))(wrapu~size:size2@@binaryb)withDivision_by_zero->raiseEmptyend|Terms.(Binary{term=Empty})->raiseEmpty|_->Codex_log.fatal"Const eval on %a"Terms.prettyxandbitvectorx=binaryxandintegerx=matchxwith|Terms.(Integer{term=T0{tag=TC.Iconstk}})->k|Terms.(Integer{term=T1{tag;a}})->(* Codex_log.warning "A constant integer was not simplified: %a" Terms.pretty x; *)(matchtagwith|TC.Itimesk->Z.mulk(integera)|_->.)|Terms.(Integer{term=T2{tag;a;b}})->(* Codex_log.warning "A constant integer was not simplified: %a" Terms.pretty x; *)(matchtagwith|TC.Imul->Z.mul(integera)(integerb)|TC.Iadd->Z.add(integera)(integerb)|TC.Isub->Z.sub(integera)(integerb)|TC.Idiv->letb=integerbinifZ.equalbZ.zerothenraiseEmptyelseZ.div(integera)b|TC.Imod->Z.rem(integera)(integerb)|TC.Ishl->Z.shift_left(integera)(Z.to_int(integerb))|TC.Ishr->Z.shift_right(integera)(Z.to_int(integerb))|TC.Ior->Z.logor(integera)(integerb)|TC.Iand->Z.logand(integera)(integerb)|TC.Ixor->Z.logxor(integera)(integerb))|Terms.(Integer{term=Empty})->raiseEmpty|_->Codex_log.fatal"get const on %a"Terms.prettyxandbooleanx=matchxwith|Terms.(Bool{term=T0{tag=TC.True}})->true|Terms.(Bool{term=T0{tag=TC.False}})->false|Terms.(Bool{term=Empty})->raiseEmpty|Terms.(Bool{term=T1{tag;a}})->(* Codex_log.warning "A constant boolean was not simplified: %a" Terms.pretty x; *)(matchtagwith|TC.Not->not(booleana)|TC.CaseOf_->assertfalse|_->.)|Terms.(Bool{term=T2{tag;a;b}})->(* Codex_log.warning "A constant boolean was not simplified: %a" Terms.pretty x; *)(matchtagwith|TC.Ile->Z.leq(integera)(integerb)|TC.Ieq->Z.equal(integera)(integerb)|TC.And->(&&)(booleana)(booleanb)|TC.BoolUnion->assertfalse|TC.Or->(||)(booleana)(booleanb)|TC.Beq_->Z.equal(binarya)(binaryb)|TC.Biulesize->Z.leq(wrapu~size@@binarya)(wrapu~size@@binaryb)|TC.Bislesize->Z.leq(wraps~size@@binarya)(wraps~size@@binaryb)|_->.)|_->Codex_log.fatal"get const on %a"Terms.prettyxandenumx=matchxwith|Terms.(Enum{term=Empty})->raiseEmpty|_->Codex_log.fatal"get const on %a"Terms.prettyxend