2020-09-23 08:13:49 -05:00
|
|
|
//! Defines [`Pointer`] which is used to improve the quality of the generated clif ir for pointer
|
|
|
|
//! operations.
|
|
|
|
|
2019-12-20 09:02:47 -06:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-03-28 11:42:23 -05:00
|
|
|
use rustc_target::abi::Align;
|
|
|
|
|
2019-12-24 05:40:18 -06:00
|
|
|
use cranelift_codegen::ir::immediates::Offset32;
|
2019-12-20 09:02:47 -06:00
|
|
|
|
2020-09-23 08:13:49 -05:00
|
|
|
/// A pointer pointing either to a certain address, a certain stack slot or nothing.
|
2019-12-20 09:02:47 -06:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) struct Pointer {
|
2019-12-20 12:11:06 -06:00
|
|
|
base: PointerBase,
|
2019-12-20 09:02:47 -06:00
|
|
|
offset: Offset32,
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:11:06 -06:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) enum PointerBase {
|
2019-12-20 12:11:06 -06:00
|
|
|
Addr(Value),
|
|
|
|
Stack(StackSlot),
|
2020-03-28 11:42:23 -05:00
|
|
|
Dangling(Align),
|
2019-12-20 12:11:06 -06:00
|
|
|
}
|
|
|
|
|
2019-12-20 09:02:47 -06:00
|
|
|
impl Pointer {
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn new(addr: Value) -> Self {
|
2021-03-05 04:21:44 -06:00
|
|
|
Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) }
|
2019-12-20 12:11:06 -06:00
|
|
|
}
|
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn stack_slot(stack_slot: StackSlot) -> Self {
|
2021-03-05 04:21:44 -06:00
|
|
|
Pointer { base: PointerBase::Stack(stack_slot), offset: Offset32::new(0) }
|
2019-12-20 09:02:47 -06:00
|
|
|
}
|
|
|
|
|
2020-03-28 11:42:23 -05:00
|
|
|
pub(crate) fn dangling(align: Align) -> Self {
|
2021-03-05 04:21:44 -06:00
|
|
|
Pointer { base: PointerBase::Dangling(align), offset: Offset32::new(0) }
|
2020-03-28 11:42:23 -05:00
|
|
|
}
|
|
|
|
|
2021-03-06 11:45:04 -06:00
|
|
|
pub(crate) fn debug_base_and_offset(self) -> (PointerBase, Offset32) {
|
2019-12-22 08:27:25 -06:00
|
|
|
(self.base, self.offset)
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn get_addr(self, fx: &mut FunctionCx<'_, '_, '_>) -> Value {
|
2019-12-20 12:11:06 -06:00
|
|
|
match self.base {
|
|
|
|
PointerBase::Addr(base_addr) => {
|
|
|
|
let offset: i64 = self.offset.into();
|
2021-03-05 04:21:44 -06:00
|
|
|
if offset == 0 { base_addr } else { fx.bcx.ins().iadd_imm(base_addr, offset) }
|
2019-12-20 12:11:06 -06:00
|
|
|
}
|
2020-08-28 05:10:48 -05:00
|
|
|
PointerBase::Stack(stack_slot) => {
|
2021-03-05 04:21:44 -06:00
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, self.offset)
|
|
|
|
}
|
|
|
|
PointerBase::Dangling(align) => {
|
|
|
|
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap())
|
2020-03-28 11:42:23 -05:00
|
|
|
}
|
2019-12-20 09:02:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn offset(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Offset32) -> Self {
|
2019-12-20 09:02:47 -06:00
|
|
|
self.offset_i64(fx, extra_offset.into())
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn offset_i64(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: i64) -> Self {
|
2019-12-20 09:02:47 -06:00
|
|
|
if let Some(new_offset) = self.offset.try_add_i64(extra_offset) {
|
2021-03-05 04:21:44 -06:00
|
|
|
Pointer { base: self.base, offset: new_offset }
|
2019-12-20 09:02:47 -06:00
|
|
|
} else {
|
|
|
|
let base_offset: i64 = self.offset.into();
|
2020-08-28 05:10:48 -05:00
|
|
|
if let Some(new_offset) = base_offset.checked_add(extra_offset) {
|
2019-12-20 12:11:06 -06:00
|
|
|
let base_addr = match self.base {
|
|
|
|
PointerBase::Addr(addr) => addr,
|
2020-08-28 05:10:48 -05:00
|
|
|
PointerBase::Stack(stack_slot) => {
|
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0)
|
|
|
|
}
|
2021-03-05 04:21:44 -06:00
|
|
|
PointerBase::Dangling(align) => {
|
|
|
|
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap())
|
|
|
|
}
|
2019-12-20 12:11:06 -06:00
|
|
|
};
|
|
|
|
let addr = fx.bcx.ins().iadd_imm(base_addr, new_offset);
|
2021-03-05 04:21:44 -06:00
|
|
|
Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) }
|
2019-12-20 09:02:47 -06:00
|
|
|
} else {
|
2020-08-28 05:10:48 -05:00
|
|
|
panic!(
|
|
|
|
"self.offset ({}) + extra_offset ({}) not representable in i64",
|
|
|
|
base_offset, extra_offset
|
|
|
|
);
|
2019-12-20 09:02:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn offset_value(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Value) -> Self {
|
2019-12-20 12:11:06 -06:00
|
|
|
match self.base {
|
|
|
|
PointerBase::Addr(addr) => Pointer {
|
|
|
|
base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
|
|
|
|
offset: self.offset,
|
|
|
|
},
|
|
|
|
PointerBase::Stack(stack_slot) => {
|
2021-03-05 04:21:44 -06:00
|
|
|
let base_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, self.offset);
|
2019-12-20 12:11:06 -06:00
|
|
|
Pointer {
|
|
|
|
base: PointerBase::Addr(fx.bcx.ins().iadd(base_addr, extra_offset)),
|
|
|
|
offset: Offset32::new(0),
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 11:42:23 -05:00
|
|
|
PointerBase::Dangling(align) => {
|
2021-03-05 04:21:44 -06:00
|
|
|
let addr =
|
|
|
|
fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap());
|
2020-03-28 11:42:23 -05:00
|
|
|
Pointer {
|
|
|
|
base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)),
|
|
|
|
offset: self.offset,
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 09:02:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn load(self, fx: &mut FunctionCx<'_, '_, '_>, ty: Type, flags: MemFlags) -> Value {
|
2019-12-20 12:11:06 -06:00
|
|
|
match self.base {
|
|
|
|
PointerBase::Addr(base_addr) => fx.bcx.ins().load(ty, flags, base_addr, self.offset),
|
2021-02-21 05:34:21 -06:00
|
|
|
PointerBase::Stack(stack_slot) => fx.bcx.ins().stack_load(ty, stack_slot, self.offset),
|
2020-03-28 11:42:23 -05:00
|
|
|
PointerBase::Dangling(_align) => unreachable!(),
|
2019-12-20 12:11:06 -06:00
|
|
|
}
|
2019-12-20 09:02:47 -06:00
|
|
|
}
|
|
|
|
|
2021-02-22 09:00:20 -06:00
|
|
|
pub(crate) fn store(self, fx: &mut FunctionCx<'_, '_, '_>, value: Value, flags: MemFlags) {
|
2019-12-20 12:11:06 -06:00
|
|
|
match self.base {
|
|
|
|
PointerBase::Addr(base_addr) => {
|
|
|
|
fx.bcx.ins().store(flags, value, base_addr, self.offset);
|
|
|
|
}
|
2019-12-23 08:56:20 -06:00
|
|
|
PointerBase::Stack(stack_slot) => {
|
2021-02-21 05:18:43 -06:00
|
|
|
fx.bcx.ins().stack_store(value, stack_slot, self.offset);
|
2019-12-20 12:11:06 -06:00
|
|
|
}
|
2020-03-28 11:42:23 -05:00
|
|
|
PointerBase::Dangling(_align) => unreachable!(),
|
2019-12-20 12:11:06 -06:00
|
|
|
}
|
2019-12-20 09:02:47 -06:00
|
|
|
}
|
|
|
|
}
|