1 //! Defines [`Pointer`] which is used to improve the quality of the generated clif ir for pointer
6 use rustc_target::abi::Align;
8 use cranelift_codegen::ir::immediates::Offset32;
10 /// A pointer pointing either to a certain address, a certain stack slot or nothing.
11 #[derive(Copy, Clone, Debug)]
12 pub(crate) struct Pointer {
17 #[derive(Copy, Clone, Debug)]
18 pub(crate) enum PointerBase {
25 pub(crate) fn new(addr: Value) -> Self {
26 Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) }
29 pub(crate) fn stack_slot(stack_slot: StackSlot) -> Self {
30 Pointer { base: PointerBase::Stack(stack_slot), offset: Offset32::new(0) }
33 pub(crate) fn const_addr(fx: &mut FunctionCx<'_, '_, '_>, addr: i64) -> Self {
34 let addr = fx.bcx.ins().iconst(fx.pointer_type, addr);
35 Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) }
38 pub(crate) fn dangling(align: Align) -> Self {
39 Pointer { base: PointerBase::Dangling(align), offset: Offset32::new(0) }
42 pub(crate) fn debug_base_and_offset(self) -> (PointerBase, Offset32) {
43 (self.base, self.offset)
46 pub(crate) fn get_addr(self, fx: &mut FunctionCx<'_, '_, '_>) -> Value {
48 PointerBase::Addr(base_addr) => {
49 let offset: i64 = self.offset.into();
50 if offset == 0 { base_addr } else { fx.bcx.ins().iadd_imm(base_addr, offset) }
52 PointerBase::Stack(stack_slot) => {
53 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, self.offset)
55 PointerBase::Dangling(align) => {
56 fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap())
61 pub(crate) fn offset(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Offset32) -> Self {
62 self.offset_i64(fx, extra_offset.into())
65 pub(crate) fn offset_i64(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: i64) -> Self {
66 if let Some(new_offset) = self.offset.try_add_i64(extra_offset) {
67 Pointer { base: self.base, offset: new_offset }
69 let base_offset: i64 = self.offset.into();
70 if let Some(new_offset) = base_offset.checked_add(extra_offset) {
71 let base_addr = match self.base {
72 PointerBase::Addr(addr) => addr,
73 PointerBase::Stack(stack_slot) => {
74 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0)
76 PointerBase::Dangling(align) => {
77 fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap())
80 let addr = fx.bcx.ins().iadd_imm(base_addr, new_offset);
81 Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) }
84 "self.offset ({}) + extra_offset ({}) not representable in i64",
85 base_offset, extra_offset
91 pub(crate) fn offset_value(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Value) -> Self {
93 PointerBase::Addr(addr) => Pointer {
94 base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
97 PointerBase::Stack(stack_slot) => {
98 let base_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, self.offset);
100 base: PointerBase::Addr(fx.bcx.ins().iadd(base_addr, extra_offset)),
101 offset: Offset32::new(0),
104 PointerBase::Dangling(align) => {
106 fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap());
108 base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
115 pub(crate) fn load(self, fx: &mut FunctionCx<'_, '_, '_>, ty: Type, flags: MemFlags) -> Value {
117 PointerBase::Addr(base_addr) => fx.bcx.ins().load(ty, flags, base_addr, self.offset),
118 PointerBase::Stack(stack_slot) => fx.bcx.ins().stack_load(ty, stack_slot, self.offset),
119 PointerBase::Dangling(_align) => unreachable!(),
123 pub(crate) fn store(self, fx: &mut FunctionCx<'_, '_, '_>, value: Value, flags: MemFlags) {
125 PointerBase::Addr(base_addr) => {
126 fx.bcx.ins().store(flags, value, base_addr, self.offset);
128 PointerBase::Stack(stack_slot) => {
129 fx.bcx.ins().stack_store(value, stack_slot, self.offset);
131 PointerBase::Dangling(_align) => unreachable!(),