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 {
27 base: PointerBase::Addr(addr),
28 offset: Offset32::new(0),
32 pub(crate) fn stack_slot(stack_slot: StackSlot) -> Self {
34 base: PointerBase::Stack(stack_slot),
35 offset: Offset32::new(0),
39 pub(crate) fn const_addr(fx: &mut FunctionCx<'_, '_, '_>, addr: i64) -> Self {
40 let addr = fx.bcx.ins().iconst(fx.pointer_type, addr);
42 base: PointerBase::Addr(addr),
43 offset: Offset32::new(0),
47 pub(crate) fn dangling(align: Align) -> Self {
49 base: PointerBase::Dangling(align),
50 offset: Offset32::new(0),
54 #[cfg(debug_assertions)]
55 pub(crate) fn base_and_offset(self) -> (PointerBase, Offset32) {
56 (self.base, self.offset)
59 pub(crate) fn get_addr(self, fx: &mut FunctionCx<'_, '_, '_>) -> Value {
61 PointerBase::Addr(base_addr) => {
62 let offset: i64 = self.offset.into();
66 fx.bcx.ins().iadd_imm(base_addr, offset)
69 PointerBase::Stack(stack_slot) => {
72 .stack_addr(fx.pointer_type, stack_slot, self.offset)
74 PointerBase::Dangling(align) => fx
77 .iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()),
81 pub(crate) fn offset(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Offset32) -> Self {
82 self.offset_i64(fx, extra_offset.into())
85 pub(crate) fn offset_i64(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: i64) -> Self {
86 if let Some(new_offset) = self.offset.try_add_i64(extra_offset) {
92 let base_offset: i64 = self.offset.into();
93 if let Some(new_offset) = base_offset.checked_add(extra_offset) {
94 let base_addr = match self.base {
95 PointerBase::Addr(addr) => addr,
96 PointerBase::Stack(stack_slot) => {
97 fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0)
99 PointerBase::Dangling(align) => fx
102 .iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()),
104 let addr = fx.bcx.ins().iadd_imm(base_addr, new_offset);
106 base: PointerBase::Addr(addr),
107 offset: Offset32::new(0),
111 "self.offset ({}) + extra_offset ({}) not representable in i64",
112 base_offset, extra_offset
118 pub(crate) fn offset_value(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Value) -> Self {
120 PointerBase::Addr(addr) => Pointer {
121 base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
124 PointerBase::Stack(stack_slot) => {
128 .stack_addr(fx.pointer_type, stack_slot, self.offset);
130 base: PointerBase::Addr(fx.bcx.ins().iadd(base_addr, extra_offset)),
131 offset: Offset32::new(0),
134 PointerBase::Dangling(align) => {
138 .iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap());
140 base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
147 pub(crate) fn load(self, fx: &mut FunctionCx<'_, '_, '_>, ty: Type, flags: MemFlags) -> Value {
149 PointerBase::Addr(base_addr) => fx.bcx.ins().load(ty, flags, base_addr, self.offset),
150 PointerBase::Stack(stack_slot) => fx.bcx.ins().stack_load(ty, stack_slot, self.offset),
151 PointerBase::Dangling(_align) => unreachable!(),
155 pub(crate) fn store(self, fx: &mut FunctionCx<'_, '_, '_>, value: Value, flags: MemFlags) {
157 PointerBase::Addr(base_addr) => {
158 fx.bcx.ins().store(flags, value, base_addr, self.offset);
160 PointerBase::Stack(stack_slot) => {
161 fx.bcx.ins().stack_store(value, stack_slot, self.offset);
163 PointerBase::Dangling(_align) => unreachable!(),