| -- Copyright 2026 The Fuchsia Authors | |
| -- | |
| -- Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| -- <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| -- license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| -- This file may not be copied, modified, or distributed except according to | |
| -- those terms. | |
| -- anneal-trust-tier: candidate:mir_semantics | |
| module | |
| prelude | |
| public import Init.NotationExtra | |
| public import Init.Data.BitVec.BasicAux | |
| -- `Char.toNat` is not transitively re-exported in Lean 4 `module` mode. | |
| public import Init.Data.Char.Basic | |
| public import Init.Data.Int.Basic | |
| public import Init.Data.Nat.Div.Basic | |
| public import Init.Data.Nat.Bitwise.Basic | |
| public import Init.Control.Except | |
| public import Trusted.MirAst | |
| public import Trusted.MirModel | |
| @[expose] public section | |
| namespace Trusted.MirSemantics | |
| open Trusted.MirAst | |
| /-! | |
| # Scalar Arithmetic and Operator Semantics (`Trusted.MirSemantics.Ops`) | |
| Pure bitvector/integer helpers, scalar decoding and truncation, and binary and | |
| unary operator evaluation rules for $M_{\text{anneal}}$. | |
| -/ | |
| /-- Euclidean integer modulo (`x % m` with `m > 0`) over | |
| `Init.Data.Int.Basic`, matching Rust's `rem_euclid` used for shift-amount | |
| masking in `compiler/rustc_const_eval/src/interpret/operator.rs` | |
| (`rust-lang/rust` `8144389c895da997dbdecaa635e41d6a50be6ffd`): | |
| > "let rem = shift_amount.rem_euclid(l_bits.into());" -/ | |
| noncomputable def intEmod : Int → Int → Int | |
| | .ofNat m, n => .ofNat (Nat.mod m (Int.natAbs n)) | |
| | .negSucc m, n => | |
| Int.subNatNat (Int.natAbs n) (Nat.succ (Nat.mod m (Int.natAbs n))) | |
| /-- Truncated integer division towards zero (`intTdiv`) over | |
| `Init.Data.Int.Basic`, matching Rust's integer `/` operator | |
| (`i128::overflowing_div` / `u128::overflowing_div` in | |
| `compiler/rustc_const_eval/src/interpret/operator.rs`, `rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`). -/ | |
| noncomputable def intTdiv : Int → Int → Int | |
| | .ofNat m, .ofNat n => .ofNat (Nat.div m n) | |
| | .ofNat m, .negSucc n => Int.neg (.ofNat (Nat.div m (Nat.succ n))) | |
| | .negSucc m, .ofNat n => Int.neg (.ofNat (Nat.div (Nat.succ m) n)) | |
| | .negSucc m, .negSucc n => .ofNat (Nat.div (Nat.succ m) (Nat.succ n)) | |
| /-- Truncated integer remainder towards zero (`intTmod`) over | |
| `Init.Data.Int.Basic`, matching Rust's integer `%` operator | |
| (`i128::overflowing_rem` / `u128::overflowing_rem` in | |
| `compiler/rustc_const_eval/src/interpret/operator.rs`, `rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`). -/ | |
| noncomputable def intTmod : Int → Int → Int | |
| | .ofNat m, .ofNat n => .ofNat (Nat.mod m n) | |
| | .ofNat m, .negSucc n => .ofNat (Nat.mod m (Nat.succ n)) | |
| | .negSucc m, .ofNat n => Int.neg (.ofNat (Nat.mod (Nat.succ m) n)) | |
| | .negSucc m, .negSucc n => | |
| Int.neg (.ofNat (Nat.mod (Nat.succ m) (Nat.succ n))) | |
| /-- Arithmetic right shift on `Int` over `Init.Data.Nat.Bitwise.Basic`, | |
| matching `l.checked_shr(shift_amount)` in | |
| `compiler/rustc_const_eval/src/interpret/operator.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`). -/ | |
| noncomputable def intShiftRight : Int → Nat → Int | |
| | .ofNat n, s => .ofNat (Nat.shiftRight n s) | |
| | .negSucc n, s => .negSucc (Nat.shiftRight n s) | |
| /-- Left shift on `Int` over `Init.Data.Nat.Bitwise.Basic`, matching | |
| `l.checked_shl(shift_amount)` in | |
| `compiler/rustc_const_eval/src/interpret/operator.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`). -/ | |
| noncomputable def intShiftLeft : Int → Nat → Int | |
| | .ofNat n, s => .ofNat (Nat.shiftLeft n s) | |
| | .negSucc n, s => .negSucc ((Nat.shiftLeft (n + 1) s) - 1) | |
| /-- Bitwise complement (`!x = -x - 1`) on mathematical `Int`, matching | |
| `UnOp::Not` in `compiler/rustc_const_eval/src/interpret/operator.rs` | |
| (`rust-lang/rust` `8144389c895da997dbdecaa635e41d6a50be6ffd`). -/ | |
| noncomputable def intNot : Int → Int | |
| | .ofNat n => .negSucc n | |
| | .negSucc n => .ofNat n | |
| /-- Extract `len` bits starting at bit `start` (`BitVec.extractLsb'`) using | |
| only `Init.Data.BitVec.BasicAux`. -/ | |
| noncomputable def bvExtractLsb' {n : Nat} (start len : Nat) (x : BitVec n) : | |
| BitVec len := | |
| BitVec.ofNat len (Nat.shiftRight x.toNat start) | |
| /-- Extract bits `hi` down to `lo` inclusive (`BitVec.extractLsb`). -/ | |
| noncomputable def bvExtractLsb {n : Nat} (hi lo : Nat) (x : BitVec n) : | |
| BitVec (hi - lo + 1) := | |
| bvExtractLsb' lo (hi - lo + 1) x | |
| /-- Two's-complement signed integer interpretation of `BitVec n` | |
| (`BitVec.toInt`). -/ | |
| noncomputable def bvToInt {n : Nat} (x : BitVec n) : Int := | |
| if 2 * x.toNat < Nat.pow 2 n then | |
| Int.ofNat x.toNat | |
| else | |
| Int.ofNat x.toNat - Int.ofNat (Nat.pow 2 n) | |
| /-- Wrap mathematical `Int` into `BitVec n` (`BitVec.ofInt`). -/ | |
| noncomputable def bvOfInt (n : Nat) (i : Int) : BitVec n := | |
| BitVec.ofNat n (intEmod i (Int.ofNat (Nat.pow 2 n))).toNat | |
| /-- Zero-extend `BitVec w` to width `v` (`BitVec.zeroExtend` for `w ≤ v`). -/ | |
| noncomputable def bvZeroExtend {w : Nat} (v : Nat) (x : BitVec w) : BitVec v := | |
| BitVec.ofNat v x.toNat | |
| /-- Sign-extend `BitVec w` to width `v` (`BitVec.signExtend`). -/ | |
| noncomputable def bvSignExtend {w : Nat} (v : Nat) (x : BitVec w) : BitVec v := | |
| bvOfInt v (bvToInt x) | |
| /-- Truncate a 128-bit bitvector to the low `w.bitWidth` bits, zero-extended | |
| back to `BitVec 128`, matching `rustc_abi::Size::truncate` | |
| (`compiler/rustc_abi/src/lib.rs`, `rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and `Miri.ABI.Size.truncate` | |
| (`vendor/lean-miri/Miri/ABI/Size.lean`): | |
| > "pub fn truncate(self, value: u128) -> u128" -/ | |
| noncomputable def truncateBits (w : IntWidth) (value : BitVec 128) : | |
| BitVec 128 := | |
| let low := bvExtractLsb (w.bitWidth - 1) 0 value | |
| bvZeroExtend 128 low | |
| /-- Sign-extend the low `w.bitWidth` bits of `value` to a mathematical `Int`, | |
| matching `rustc_abi::Size::sign_extend` (`compiler/rustc_abi/src/lib.rs`, | |
| `rust-lang/rust` `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.ABI.Size.signExtend` (`vendor/lean-miri/Miri/ABI/Size.lean`): | |
| > "pub fn sign_extend(self, value: u128) -> i128" -/ | |
| noncomputable def signExtendBits (w : IntWidth) (value : BitVec 128) : Int := | |
| let low := bvExtractLsb (w.bitWidth - 1) 0 value | |
| bvToInt (bvSignExtend 128 low) | |
| /-- Bitwise AND on `BitVec 128` over `Init.Data.Nat.Bitwise.Basic`. -/ | |
| noncomputable def bvAnd (x y : BitVec 128) : BitVec 128 := | |
| BitVec.ofNat 128 (Nat.land x.toNat y.toNat) | |
| /-- Bitwise OR on `BitVec 128` over `Init.Data.Nat.Bitwise.Basic`. -/ | |
| noncomputable def bvOr (x y : BitVec 128) : BitVec 128 := | |
| BitVec.ofNat 128 (Nat.lor x.toNat y.toNat) | |
| /-- Bitwise XOR on `BitVec 128` over `Init.Data.Nat.Bitwise.Basic`. -/ | |
| noncomputable def bvXor (x y : BitVec 128) : BitVec 128 := | |
| BitVec.ofNat 128 (Nat.xor x.toNat y.toNat) | |
| /-- Minimum signed integer representable at width `w` (`-2^(w.bitWidth - 1)`), | |
| matching `rustc_abi::Size::signed_int_min` (`compiler/rustc_abi/src/lib.rs`, | |
| `rust-lang/rust` `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.ABI.Size.signedIntMin` (`vendor/lean-miri/Miri/ABI/Size.lean`): | |
| > "pub fn signed_int_min(&self) -> i128" -/ | |
| noncomputable def signedMinInt (w : IntWidth) : Int := | |
| Neg.neg (HPow.hPow (2 : Int) (w.bitWidth - 1)) | |
| namespace ScalarVal | |
| /-- Check that `v.bits` fits within `v.width.bitWidth` bits | |
| (`truncateBits v.width v.bits == v.bits`), matching `ScalarInt::check_data` | |
| (`compiler/rustc_middle/src/ty/consts/int.rs`, `rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`): | |
| > "debug_assert_eq!(self.size().truncate(self.data), { self.data }, ...);" -/ | |
| noncomputable def wf (v : ScalarVal) : Bool := | |
| truncateBits v.width v.bits == v.bits | |
| end ScalarVal | |
| /-- Decode the mathematical integer value of `sv` after masking to `sv.width` | |
| bits, matching `ScalarInt::to_int` and `ScalarInt::to_uint` | |
| (`compiler/rustc_middle/src/ty/consts/int.rs`, `rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.IntegerOps.decode` | |
| (`vendor/lean-miri/Miri/Interpreter/IntegerOps.lean`). -/ | |
| noncomputable def decodeScalar (sv : ScalarVal) : Int := | |
| let trunc := truncateBits sv.width sv.bits | |
| if sv.signed then | |
| signExtendBits sv.width trunc | |
| else | |
| Int.ofNat trunc.toNat | |
| /-- Truncate mathematical integer `value` to `(w, s)` and report whether | |
| truncation was lossy, matching `ScalarInt::truncate_from_int` and | |
| `ScalarInt::truncate_from_uint` (`compiler/rustc_middle/src/ty/consts/int.rs`, | |
| `rust-lang/rust` `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.IntegerOps.truncate` | |
| (`vendor/lean-miri/Miri/Interpreter/IntegerOps.lean`): | |
| > "pub fn truncate_from_uint(i: impl Into<u128>, size: Size) -> (Self, bool)" | |
| > "pub fn truncate_from_int(i: impl Into<i128>, size: Size) -> (Self, bool)" | |
| In the signed branch, `wrapped := bvToInt (bvOfInt 128 value)` always lies in | |
| $[-2^{127}, 2^{127})$; the explicit range guard is unreachable and retained for | |
| syntactic lockstep with `Miri.Memory.ScalarInt.truncateFromInt`. -/ | |
| noncomputable def truncateScalar (w : IntWidth) (s : Bool) (value : Int) : | |
| Except FaultKind (ScalarVal × Bool) := | |
| if s then | |
| let wrapped := bvToInt (bvOfInt 128 value) | |
| if decide (wrapped < Neg.neg (HPow.hPow (2 : Int) 127)) || | |
| decide (wrapped ≥ HPow.hPow (2 : Int) 127) then | |
| .error .unsupported | |
| else | |
| let raw := bvOfInt 128 wrapped | |
| let trunc := truncateBits w raw | |
| let decoded := signExtendBits w trunc | |
| .ok (⟨w, s, trunc⟩, decoded != wrapped) | |
| else | |
| let raw := bvOfInt 128 value | |
| let trunc := truncateBits w raw | |
| .ok (⟨w, s, trunc⟩, trunc != raw) | |
| /-- Evaluate a bit-shift operator (`.shl`, `.shr`, `.shlUnchecked`, | |
| `.shrUnchecked`) in lockstep with | |
| `compiler/rustc_const_eval/src/interpret/operator.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.IntegerOps.shift` | |
| (`vendor/lean-miri/Miri/Interpreter/IntegerOps.lean`): | |
| > "let rem = shift_amount.rem_euclid(l_bits.into()); | |
| > (rem, rem != shift_amount)" | |
| > "if overflow && let Some(intrinsic) = throw_ub_on_overflow { | |
| > throw_ub!(ShiftOverflow { ... }); | |
| > }" | |
| > (`compiler/rustc_const_eval/src/interpret/operator.rs`, `rust-lang/rust` | |
| > `8144389c895da997dbdecaa635e41d6a50be6ffd`) -/ | |
| noncomputable def evalShiftOp (op : BinOp) (w : IntWidth) (s : Bool) | |
| (l r : Int) : Except FaultKind Val := | |
| let amount := intEmod r (Int.ofNat w.bitWidth) | |
| let overflow := amount != r | |
| let result := match op with | |
| | .shl | .shlUnchecked => intShiftLeft l amount.toNat | |
| | _ => intShiftRight l amount.toNat | |
| match truncateScalar w s result with | |
| | .error k => .error k | |
| | .ok (res, _) => | |
| if overflow && op.isUnchecked then | |
| .error .undefined | |
| else | |
| .ok (.scalar res) | |
| /-- Evaluate an integer arithmetic operator (`.add`, `.sub`, `.mul`, `.div`, | |
| `.rem`, `.addUnchecked`, `.subUnchecked`, `.mulUnchecked`, | |
| `.addWithOverflow`, `.subWithOverflow`, `.mulWithOverflow`) in lockstep with | |
| `compiler/rustc_const_eval/src/interpret/operator.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.IntegerOps.arithmetic` | |
| (`vendor/lean-miri/Miri/Interpreter/IntegerOps.lean`): | |
| > "// We need a special check for overflowing Rem and Div since they are *UB* | |
| > // on overflow, which can happen with \"int_min $OP -1\"." | |
| > (`compiler/rustc_const_eval/src/interpret/operator.rs`, `rust-lang/rust` | |
| > `8144389c895da997dbdecaa635e41d6a50be6ffd`) | |
| An overflowing operator (`op.isOverflowing`) never faults on overflow and | |
| returns the pair `(wrapped, overflowed)` as `.tuple [.scalar wrapped, | |
| .bool overflowed]`, matching `BinOp::AddWithOverflow`'s documented result: | |
| > "Like `Add`, but returns `(T, bool)` of both the wrapped result | |
| > and a bool indicating whether it overflowed." | |
| > (`compiler/rustc_middle/src/mir/syntax.rs`, `rust-lang/rust` | |
| > `8144389c895da997dbdecaa635e41d6a50be6ffd`) -/ | |
| noncomputable def evalArithOp (op : BinOp) (w : IntWidth) (s : Bool) | |
| (l r : Int) : Except FaultKind Val := | |
| let isDivOrRem := match op with | .div | .rem => true | _ => false | |
| if isDivOrRem && r == 0 then | |
| .error .undefined | |
| else if s && isDivOrRem && (l == signedMinInt w && r == Neg.neg 1) then | |
| .error .undefined | |
| else | |
| let res? : Option Int := match op with | |
| | .add | .addUnchecked | .addWithOverflow => some (l + r) | |
| | .sub | .subUnchecked | .subWithOverflow => some (l - r) | |
| | .mul | .mulUnchecked | .mulWithOverflow => some (l * r) | |
| | .div => some (intTdiv l r) | |
| | .rem => some (intTmod l r) | |
| | _ => none | |
| match res? with | |
| | none => .error .unsupported | |
| | some result => | |
| let hostOverflow : Bool := | |
| if s then | |
| decide (result < Neg.neg (HPow.hPow (2 : Int) 127)) || | |
| decide (result ≥ HPow.hPow (2 : Int) 127) | |
| else | |
| decide (result < 0) || decide (result ≥ HPow.hPow (2 : Int) 128) | |
| match truncateScalar w s result with | |
| | .error k => .error k | |
| | .ok (val, lossy) => | |
| if (hostOverflow || lossy) && op.isUnchecked then | |
| .error .undefined | |
| else if op.isOverflowing then | |
| .ok (.tuple [.scalar val, .bool (hostOverflow || lossy)]) | |
| else | |
| .ok (.scalar val) | |
| /-- Three-way comparison of mathematical integers, matching `Ord::cmp` on | |
| integers (`library/core/src/cmp.rs`, `rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) as `BinOp::Cmp` evaluates it | |
| (`compiler/rustc_middle/src/mir/syntax.rs`): | |
| > "[`Rvalue::BinaryOp`]`(BinOp::Cmp, A, B)` returns | |
| > - `Ordering::Less` (`-1_i8`, as a Scalar) if `A < B` | |
| > - `Ordering::Equal` (`0_i8`, as a Scalar) if `A == B` | |
| > - `Ordering::Greater` (`+1_i8`, as a Scalar) if `A > B`" -/ | |
| noncomputable def cmpOrderingOfInt (l r : Int) : OrderingVal := | |
| if decide (l < r) then | |
| .less | |
| else if decide (l = r) then | |
| .equal | |
| else | |
| .greater | |
| /-- Evaluate a binary operator across all 6 integer widths (`i8`..`i128`, | |
| `isize`), signedness modes, shift operators, and booleans in lockstep with | |
| `compiler/rustc_const_eval/src/interpret/operator.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.IntegerOps.{binary, binaryBool}`: | |
| > "pub fn binary_op(&self, bin_op: mir::BinOp, left: &ImmTy<'tcx, | |
| > M::Provenance>, right: &ImmTy<'tcx, M::Provenance>) -> InterpResult<'tcx, | |
| > ImmTy<'tcx, M::Provenance>>" | |
| > (`compiler/rustc_const_eval/src/interpret/operator.rs`, `rust-lang/rust` | |
| > `8144389c895da997dbdecaa635e41d6a50be6ffd`) | |
| - Non-shift integer ops require matching `(width, signed)`. | |
| - Shift ops (`.shl`, `.shr`, `.shlUnchecked`, `.shrUnchecked`) accept any | |
| integer RHS `(r.width, r.signed)`, mask the shift amount modulo `w.bitWidth`, | |
| fault with `.undefined` on `.shlUnchecked`/`.shrUnchecked` when `r < 0` | |
| or `r ≥ w.bitWidth`, and perform arithmetic right shift on signed `l`. | |
| - Signed `.div` and `.rem` fault with `.undefined` on `INT_MIN / -1` and | |
| `INT_MIN % -1` as well as `r == 0`. | |
| - `.addWithOverflow`, `.subWithOverflow`, and `.mulWithOverflow` return | |
| `.tuple [.scalar wrapped, .bool overflowed]` (`evalArithOp`). | |
| - `.cmp` on integers and on `char`s returns `.ordering` (`cmpOrderingOfInt`); | |
| `.cmp` on `bool` returns `.error .unsupported`, matching | |
| `Miri.Interpreter.IntegerOps.binaryBool`. | |
| - On `char` operands, only the six comparisons and `.cmp` are defined | |
| (`Miri.Interpreter.IntegerOps.binaryChar`), comparing Unicode scalar values; | |
| every other operator returns `.error .unsupported`. -/ | |
| noncomputable def evalBinOp (op : BinOp) (lhs rhs : Val) : | |
| Except FaultKind Val := | |
| match lhs, rhs with | |
| | .scalar l, .scalar r => | |
| let lInt := decodeScalar l | |
| let rInt := decodeScalar r | |
| let w := l.width | |
| let s := l.signed | |
| if op.isShift then | |
| evalShiftOp op w s lInt rInt | |
| else if !(IntWidth.beq l.width r.width && (l.signed == r.signed)) then | |
| .error .unsupported | |
| else | |
| match op with | |
| | .eq => .ok (.bool (lInt == rInt)) | |
| | .ne => .ok (.bool (lInt != rInt)) | |
| | .lt => .ok (.bool (decide (lInt < rInt))) | |
| | .le => .ok (.bool (decide (lInt ≤ rInt))) | |
| | .gt => .ok (.bool (decide (lInt > rInt))) | |
| | .ge => .ok (.bool (decide (lInt ≥ rInt))) | |
| | .cmp => .ok (.ordering (cmpOrderingOfInt lInt rInt)) | |
| | .bitAnd => | |
| let lb := truncateBits w l.bits | |
| let rb := truncateBits r.width r.bits | |
| .ok (.scalar ⟨w, s, truncateBits w (bvAnd lb rb)⟩) | |
| | .bitOr => | |
| let lb := truncateBits w l.bits | |
| let rb := truncateBits r.width r.bits | |
| .ok (.scalar ⟨w, s, truncateBits w (bvOr lb rb)⟩) | |
| | .bitXor => | |
| let lb := truncateBits w l.bits | |
| let rb := truncateBits r.width r.bits | |
| .ok (.scalar ⟨w, s, truncateBits w (bvXor lb rb)⟩) | |
| | .add | .sub | .mul | .div | .rem | |
| | .addUnchecked | .subUnchecked | .mulUnchecked | |
| | .addWithOverflow | .subWithOverflow | .mulWithOverflow => | |
| evalArithOp op w s lInt rInt | |
| | _ => .error .unsupported | |
| | .bool l, .bool r => | |
| match op with | |
| | .eq => .ok (.bool (l == r)) | |
| | .ne => .ok (.bool (l != r)) | |
| | .lt => .ok (.bool (!l && r)) | |
| | .le => .ok (.bool (!l || r)) | |
| | .gt => .ok (.bool (l && !r)) | |
| | .ge => .ok (.bool (l || !r)) | |
| | .bitAnd => .ok (.bool (l && r)) | |
| | .bitOr => .ok (.bool (l || r)) | |
| | .bitXor => .ok (.bool (bne l r)) | |
| | _ => .error .unsupported | |
| | .char l, .char r => | |
| let lInt := Int.ofNat l.toNat | |
| let rInt := Int.ofNat r.toNat | |
| match op with | |
| | .eq => .ok (.bool (lInt == rInt)) | |
| | .ne => .ok (.bool (lInt != rInt)) | |
| | .lt => .ok (.bool (decide (lInt < rInt))) | |
| | .le => .ok (.bool (decide (lInt ≤ rInt))) | |
| | .gt => .ok (.bool (decide (lInt > rInt))) | |
| | .ge => .ok (.bool (decide (lInt ≥ rInt))) | |
| | .cmp => .ok (.ordering (cmpOrderingOfInt lInt rInt)) | |
| | _ => .error .unsupported | |
| | _, _ => .error .unsupported | |
| /-- The `u128` intermediate of an integer-like cast source `v`, or `none` if | |
| `v` is not an integer, `bool`, or `char`. Matches `cast_from_int_like` in | |
| `compiler/rustc_const_eval/src/interpret/cast.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.Cast.intLikeValue` | |
| (`vendor/lean-miri/Miri/Interpreter/Cast.lean`): | |
| > "let v = match src_layout.ty.kind() { | |
| > ty::Uint(_) | ty::RawPtr(..) | ty::FnPtr(..) => | |
| > scalar.to_uint(src_layout.size)?, | |
| > ty::Int(_) => scalar.to_int(src_layout.size)? as u128, // we will cast | |
| > back to `i128` below if the sign matters | |
| > ty::Bool => scalar.to_bool()?.into(), | |
| > ty::Char => scalar.to_char()?.into(), | |
| > _ => span_bug!(self.cur_span(), \"invalid int-like cast from {}\", | |
| > src_layout.ty), | |
| > };" | |
| A signed integer is sign-extended to 128 bits (`i128 as u128`), an unsigned | |
| integer is zero-extended, `bool` is `0` or `1`, and `char` is its Unicode | |
| scalar value. -/ | |
| noncomputable def intLikeBits : Val → Option Nat | |
| | .scalar sv => | |
| if sv.signed then | |
| some (bvOfInt 128 (decodeScalar sv)).toNat | |
| else | |
| some (truncateBits sv.width sv.bits).toNat | |
| | .bool b => some (if b then 1 else 0) | |
| | .char c => some c.toNat | |
| | _ => none | |
| /-- Evaluate `CastKind::IntToInt` of the integer-like value `v` to the target | |
| type `castTy`, matching `cast_from_int_like` in | |
| `compiler/rustc_const_eval/src/interpret/cast.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.Cast.castFromIntLike` with | |
| `Miri.Interpreter.IntegerCast.cast`: | |
| > "// int -> int | |
| > ty::Int(_) | ty::Uint(_) => { | |
| > ... | |
| > let v = size.truncate(v); | |
| > Scalar::from_uint(v, size) | |
| > } | |
| > ... | |
| > // u8 -> char | |
| > ty::Char => Scalar::from_u32(u8::try_from(v).unwrap().into())," | |
| - `.int w s` targets truncate the intermediate (`intLikeBits v`) to `w`. | |
| - `.char` targets accept an intermediate of at most `255` (`u8 as char`, the | |
| only integer-to-`char` cast Rust admits). A larger intermediate panics in | |
| `u8::try_from(v).unwrap()`, which `Miri.Interpreter.Cast.integerCastError` | |
| reports as an invalid program, so the result is `.error .unsupported`. | |
| - A source that is not integer-like, and any other target type, return | |
| `.error .unsupported`. -/ | |
| noncomputable def evalIntToIntCast (v : Val) (castTy : MirTy) : | |
| Except FaultKind Val := | |
| match intLikeBits v with | |
| | none => .error .unsupported | |
| | some bits => | |
| match castTy with | |
| | .int w s => .ok (.scalar ⟨w, s, truncateBits w (BitVec.ofNat 128 bits)⟩) | |
| | .char => | |
| if Nat.ble bits 255 then | |
| .ok (.char (Char.ofNat bits)) | |
| else | |
| .error .unsupported | |
| | _ => .error .unsupported | |
| /-- Evaluate a unary operator (`not` and `neg`) in lockstep with | |
| `compiler/rustc_const_eval/src/interpret/operator.rs` (`rust-lang/rust` | |
| `8144389c895da997dbdecaa635e41d6a50be6ffd`) and | |
| `Miri.Interpreter.ScalarOps.unary` / `Miri.Interpreter.IntegerOps.unary`: | |
| > "pub fn unary_op(&self, un_op: mir::UnOp, val: &ImmTy<'tcx, M::Provenance>) | |
| > -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>>" | |
| > (`compiler/rustc_const_eval/src/interpret/operator.rs`, `rust-lang/rust` | |
| > `8144389c895da997dbdecaa635e41d6a50be6ffd`) | |
| - `.not` on `.bool b` returns `.bool (!b)`. | |
| - `.not` on `.scalar sv` returns bitwise complement `intNot (decodeScalar sv)` | |
| truncated to `(sv.width, sv.signed)`. | |
| - `.neg` on signed `.scalar sv` returns wrapping `-decodeScalar sv` | |
| (`-INT_MIN == INT_MIN`); `.neg` on unsigned `.scalar sv` returns | |
| `.error .unsupported`. | |
| - Every unary operator on `.char` returns `.error .unsupported`, matching | |
| `Miri.Interpreter.ScalarOps.unary`'s `invalid character unary operation`. -/ | |
| noncomputable def evalUnOp (op : UnOp) (v : Val) : Except FaultKind Val := | |
| match op, v with | |
| | .not, .bool b => .ok (.bool (!b)) | |
| | .not, .scalar sv => | |
| let integer := decodeScalar sv | |
| match truncateScalar sv.width sv.signed (intNot integer) with | |
| | .ok (res, _) => .ok (.scalar res) | |
| | .error k => .error k | |
| | .neg, .scalar sv => | |
| let integer := decodeScalar sv | |
| if sv.signed then | |
| match truncateScalar sv.width sv.signed (Neg.neg integer) with | |
| | .ok (res, _) => .ok (.scalar res) | |
| | .error k => .error k | |
| else | |
| .error .unsupported | |
| | _, _ => .error .unsupported | |
| end Trusted.MirSemantics | |
| end |