1 #![allow(non_camel_case_types)]
3 use rustc_errors::struct_span_err;
4 use rustc_hir::LangItem;
5 use rustc_middle::mir::interpret::ConstValue;
6 use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
7 use rustc_session::Session;
13 #[derive(Copy, Clone)]
14 pub enum IntPredicate {
27 #[derive(Copy, Clone)]
28 pub enum RealPredicate {
47 #[derive(Copy, Clone)]
48 pub enum AtomicRmwBinOp {
62 #[derive(Copy, Clone)]
63 pub enum AtomicOrdering {
69 SequentiallyConsistent,
72 #[derive(Copy, Clone)]
73 pub enum SynchronizationScope {
78 #[derive(Copy, Clone, PartialEq, Debug)]
102 // FIXME(mw): Anything that is produced via DepGraph::with_task() must implement
103 // the HashStable trait. Normally DepGraph::with_task() calls are
104 // hidden behind queries, but CGU creation is a special case in two
105 // ways: (1) it's not a query and (2) CGU are output nodes, so their
106 // Fingerprints are not actually needed. It remains to be clarified
107 // how exactly this case will be handled in the red/green system but
108 // for now we content ourselves with providing a no-op HashStable
109 // implementation for CGUs.
110 mod temp_stable_hash_impls {
111 use crate::ModuleCodegen;
112 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
114 impl<HCX, M> HashStable<HCX> for ModuleCodegen<M> {
115 fn hash_stable(&self, _: &mut HCX, _: &mut StableHasher) {
121 pub fn build_langcall<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
125 ) -> (Bx::FnAbiOfResult, Bx::Value) {
127 let def_id = tcx.require_lang_item(li, span);
128 let instance = ty::Instance::mono(tcx, def_id);
129 (bx.fn_abi_of_instance(instance, ty::List::empty()), bx.get_fn_addr(instance))
132 // To avoid UB from LLVM, these two functions mask RHS with an
133 // appropriate mask unconditionally (i.e., the fallback behavior for
134 // all shifts). For 32- and 64-bit types, this matches the semantics
135 // of Java. (See related discussion on #1877 and #10183.)
137 pub fn build_unchecked_lshift<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
142 let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
143 // #1877, #10183: Ensure that input is always valid
144 let rhs = shift_mask_rhs(bx, rhs);
148 pub fn build_unchecked_rshift<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
154 let rhs = base::cast_shift_expr_rhs(bx, lhs, rhs);
155 // #1877, #10183: Ensure that input is always valid
156 let rhs = shift_mask_rhs(bx, rhs);
157 let is_signed = lhs_t.is_signed();
158 if is_signed { bx.ashr(lhs, rhs) } else { bx.lshr(lhs, rhs) }
161 fn shift_mask_rhs<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
165 let rhs_llty = bx.val_ty(rhs);
166 let shift_val = shift_mask_val(bx, rhs_llty, rhs_llty, false);
167 bx.and(rhs, shift_val)
170 pub fn shift_mask_val<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
176 let kind = bx.type_kind(llty);
178 TypeKind::Integer => {
179 // i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
180 let val = bx.int_width(llty) - 1;
182 bx.const_int(mask_llty, !val as i64)
184 bx.const_uint(mask_llty, val)
187 TypeKind::Vector => {
189 shift_mask_val(bx, bx.element_type(llty), bx.element_type(mask_llty), invert);
190 bx.vector_splat(bx.vector_length(mask_llty), mask)
192 _ => bug!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
196 pub fn span_invalid_monomorphization_error(a: &Session, b: Span, c: &str) {
197 struct_span_err!(a, b, E0511, "{}", c).emit();
200 pub fn asm_const_to_str<'tcx>(
203 const_value: ConstValue<'tcx>,
204 ty_and_layout: TyAndLayout<'tcx>,
206 let ConstValue::Scalar(scalar) = const_value else {
207 span_bug!(sp, "expected Scalar for promoted asm const, but got {:#?}", const_value)
209 let value = scalar.assert_bits(ty_and_layout.size);
210 match ty_and_layout.ty.kind() {
211 ty::Uint(_) => value.to_string(),
212 ty::Int(int_ty) => match int_ty.normalize(tcx.sess.target.pointer_width) {
213 ty::IntTy::I8 => (value as i8).to_string(),
214 ty::IntTy::I16 => (value as i16).to_string(),
215 ty::IntTy::I32 => (value as i32).to_string(),
216 ty::IntTy::I64 => (value as i64).to_string(),
217 ty::IntTy::I128 => (value as i128).to_string(),
218 ty::IntTy::Isize => unreachable!(),
220 _ => span_bug!(sp, "asm const has bad type {}", ty_and_layout.ty),