From 6c9ba97d23254045b182edbb8a4448a8c25c7a76 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 14 Nov 2019 10:38:15 +0100 Subject: [PATCH] miri: helper methods for max values of machine's usize/isize --- src/librustc/mir/interpret/pointer.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/librustc/mir/interpret/pointer.rs b/src/librustc/mir/interpret/pointer.rs index 1bb4d9ea4d6..7c77b2c0711 100644 --- a/src/librustc/mir/interpret/pointer.rs +++ b/src/librustc/mir/interpret/pointer.rs @@ -1,4 +1,5 @@ use std::fmt::{self, Display}; +use std::convert::TryFrom; use crate::mir; use crate::ty::layout::{self, HasDataLayout, Size}; @@ -40,6 +41,18 @@ fn pointer_size(&self) -> Size { self.data_layout().pointer_size } + #[inline] + fn usize_max(&self) -> u64 { + let max_usize_plus_1 = 1u128 << self.pointer_size().bits(); + u64::try_from(max_usize_plus_1-1).unwrap() + } + + #[inline] + fn isize_max(&self) -> i64 { + let max_isize_plus_1 = 1u128 << (self.pointer_size().bits()-1); + i64::try_from(max_isize_plus_1-1).unwrap() + } + /// Helper function: truncate given value-"overflowed flag" pair to pointer size and /// update "overflowed flag" if there was an overflow. /// This should be called by all the other methods before returning!