arm32 codegen
This commit adds arm32 code generation for some IR insts. Floating-point instructions are not supported, because regalloc does not allow to represent overlapping register classes, which are needed by VFP/Neon. There is also no support for big-endianness, I64 and I128 types.
This commit is contained in:
@@ -175,6 +175,32 @@ pub trait ABIMachineSpec {
|
||||
/// The instruction type.
|
||||
type I: VCodeInst;
|
||||
|
||||
/// Returns the number of bits in a word, that is 32/64 for 32/64-bit architecture.
|
||||
fn word_bits() -> u32;
|
||||
|
||||
/// Returns the number of bytes in a word.
|
||||
fn word_bytes() -> u32 {
|
||||
return Self::word_bits() / 8;
|
||||
}
|
||||
|
||||
/// Returns word-size integer type.
|
||||
fn word_type() -> Type {
|
||||
match Self::word_bits() {
|
||||
32 => I32,
|
||||
64 => I64,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns word register class.
|
||||
fn word_reg_class() -> RegClass {
|
||||
match Self::word_bits() {
|
||||
32 => RegClass::I32,
|
||||
64 => RegClass::I64,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Process a list of parameters or return values and allocate them to registers
|
||||
/// and stack slots.
|
||||
///
|
||||
@@ -453,7 +479,8 @@ impl<M: ABIMachineSpec> ABICalleeImpl<M> {
|
||||
for (stackslot, data) in f.stack_slots.iter() {
|
||||
let off = stack_offset;
|
||||
stack_offset += data.size;
|
||||
stack_offset = (stack_offset + 7) & !7;
|
||||
let mask = M::word_bytes() - 1;
|
||||
stack_offset = (stack_offset + mask) & !mask;
|
||||
debug_assert_eq!(stackslot.as_u32() as usize, stackslots.len());
|
||||
stackslots.push(off);
|
||||
}
|
||||
@@ -587,7 +614,12 @@ fn generate_gv<M: ABIMachineSpec>(
|
||||
} => {
|
||||
let base = generate_gv::<M>(f, abi, base, insts);
|
||||
let into_reg = Writable::from_reg(M::get_stacklimit_reg());
|
||||
insts.push(M::gen_load_base_offset(into_reg, base, offset.into(), I64));
|
||||
insts.push(M::gen_load_base_offset(
|
||||
into_reg,
|
||||
base,
|
||||
offset.into(),
|
||||
M::word_type(),
|
||||
));
|
||||
return into_reg.to_reg();
|
||||
}
|
||||
ref other => panic!("global value for stack limit not supported: {}", other),
|
||||
@@ -603,13 +635,13 @@ fn generate_gv<M: ABIMachineSpec>(
|
||||
/// associated vreg when needed to satisfy a safepoint (which requires all
|
||||
/// ref-typed values, even those in real registers in the original vcode, to be
|
||||
/// in spillslots).
|
||||
fn ty_from_ty_hint_or_reg_class(r: Reg, ty: Option<Type>) -> Type {
|
||||
fn ty_from_ty_hint_or_reg_class<M: ABIMachineSpec>(r: Reg, ty: Option<Type>) -> Type {
|
||||
match (ty, r.get_class()) {
|
||||
// If the type is provided
|
||||
(Some(t), _) => t,
|
||||
// If no type is provided, this should be a register spill for a
|
||||
// safepoint, so we only expect I64 (integer) registers.
|
||||
(None, RegClass::I64) => I64,
|
||||
// safepoint, so we only expect I32/I64 (integer) registers.
|
||||
(None, rc) if rc == M::word_reg_class() => M::word_type(),
|
||||
_ => panic!("Unexpected register class!"),
|
||||
}
|
||||
}
|
||||
@@ -679,19 +711,22 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
|
||||
fn gen_copy_reg_to_retval(&self, idx: usize, from_reg: Writable<Reg>) -> Vec<Self::I> {
|
||||
let mut ret = Vec::new();
|
||||
let word_bits = M::word_bits() as u8;
|
||||
match &self.sig.rets[idx] {
|
||||
&ABIArg::Reg(r, ty, ext) => {
|
||||
let from_bits = ty_bits(ty) as u8;
|
||||
let dest_reg = Writable::from_reg(r.to_reg());
|
||||
match (ext, from_bits) {
|
||||
(ArgumentExtension::Uext, n) | (ArgumentExtension::Sext, n) if n < 64 => {
|
||||
(ArgumentExtension::Uext, n) | (ArgumentExtension::Sext, n)
|
||||
if n < word_bits =>
|
||||
{
|
||||
let signed = ext == ArgumentExtension::Sext;
|
||||
ret.push(M::gen_extend(
|
||||
dest_reg,
|
||||
from_reg.to_reg(),
|
||||
signed,
|
||||
from_bits,
|
||||
/* to_bits = */ 64,
|
||||
/* to_bits = */ word_bits,
|
||||
));
|
||||
}
|
||||
_ => ret.push(M::gen_move(dest_reg, from_reg.to_reg(), ty)),
|
||||
@@ -706,18 +741,20 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
.expect("Argument stack offset greater than 2GB; should hit impl limit first");
|
||||
// Trash the from_reg; it should be its last use.
|
||||
match (ext, from_bits) {
|
||||
(ArgumentExtension::Uext, n) | (ArgumentExtension::Sext, n) if n < 64 => {
|
||||
assert_eq!(RegClass::I64, from_reg.to_reg().get_class());
|
||||
(ArgumentExtension::Uext, n) | (ArgumentExtension::Sext, n)
|
||||
if n < word_bits =>
|
||||
{
|
||||
assert_eq!(M::word_reg_class(), from_reg.to_reg().get_class());
|
||||
let signed = ext == ArgumentExtension::Sext;
|
||||
ret.push(M::gen_extend(
|
||||
from_reg,
|
||||
from_reg.to_reg(),
|
||||
signed,
|
||||
from_bits,
|
||||
/* to_bits = */ 64,
|
||||
/* to_bits = */ word_bits,
|
||||
));
|
||||
// Store the extended version.
|
||||
ty = I64;
|
||||
ty = M::word_type();
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
@@ -802,7 +839,7 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
fn load_spillslot(&self, slot: SpillSlot, ty: Type, into_reg: Writable<Reg>) -> Self::I {
|
||||
// Offset from beginning of spillslot area, which is at nominal SP + stackslots_size.
|
||||
let islot = slot.get() as i64;
|
||||
let spill_off = islot * 8; // FIXME: 64-bit machine assumed.
|
||||
let spill_off = islot * M::word_bytes() as i64;
|
||||
let sp_off = self.stackslots_size as i64 + spill_off;
|
||||
trace!("load_spillslot: slot {:?} -> sp_off {}", slot, sp_off);
|
||||
M::gen_load_stack(StackAMode::NominalSPOffset(sp_off, ty), into_reg, ty)
|
||||
@@ -812,7 +849,7 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
fn store_spillslot(&self, slot: SpillSlot, ty: Type, from_reg: Reg) -> Self::I {
|
||||
// Offset from beginning of spillslot area, which is at nominal SP + stackslots_size.
|
||||
let islot = slot.get() as i64;
|
||||
let spill_off = islot * 8; // FIXME: 64-bit machine assumed.
|
||||
let spill_off = islot * M::word_bytes() as i64;
|
||||
let sp_off = self.stackslots_size as i64 + spill_off;
|
||||
trace!("store_spillslot: slot {:?} -> sp_off {}", slot, sp_off);
|
||||
M::gen_store_stack(StackAMode::NominalSPOffset(sp_off, ty), from_reg, ty)
|
||||
@@ -832,12 +869,14 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
state
|
||||
);
|
||||
let map_size = (virtual_sp_offset + nominal_sp_to_fp) as u32;
|
||||
let map_words = (map_size + 7) / 8; // FIXME: 64-bit machine assumed.
|
||||
let bytes = M::word_bytes();
|
||||
let map_words = (map_size + bytes - 1) / bytes;
|
||||
let mut bits = std::iter::repeat(false)
|
||||
.take(map_words as usize)
|
||||
.collect::<Vec<bool>>();
|
||||
|
||||
let first_spillslot_word = ((self.stackslots_size + virtual_sp_offset as u32) / 8) as usize;
|
||||
let first_spillslot_word =
|
||||
((self.stackslots_size + virtual_sp_offset as u32) / bytes) as usize;
|
||||
for &slot in slots {
|
||||
let slot = slot.get() as usize;
|
||||
bits[first_spillslot_word + slot] = true;
|
||||
@@ -853,16 +892,17 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
insts.extend(M::gen_prologue_frame_setup().into_iter());
|
||||
}
|
||||
|
||||
let mut total_stacksize = self.stackslots_size + 8 * self.spillslots.unwrap() as u32;
|
||||
let bytes = M::word_bytes();
|
||||
let mut total_stacksize = self.stackslots_size + bytes * self.spillslots.unwrap() as u32;
|
||||
if self.call_conv.extends_baldrdash() {
|
||||
debug_assert!(
|
||||
!self.flags.enable_probestack(),
|
||||
"baldrdash does not expect cranelift to emit stack probes"
|
||||
);
|
||||
// FIXME: 64-bit machine assumed.
|
||||
total_stacksize += self.flags.baldrdash_prologue_words() as u32 * 8;
|
||||
total_stacksize += self.flags.baldrdash_prologue_words() as u32 * bytes;
|
||||
}
|
||||
let total_stacksize = (total_stacksize + 15) & !15; // 16-align the stack.
|
||||
let mask = 2 * bytes - 1;
|
||||
let total_stacksize = (total_stacksize + mask) & !mask; // 16-align the stack.
|
||||
|
||||
let mut total_sp_adjust = 0;
|
||||
|
||||
@@ -943,7 +983,7 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
}
|
||||
|
||||
fn gen_spill(&self, to_slot: SpillSlot, from_reg: RealReg, ty: Option<Type>) -> Self::I {
|
||||
let ty = ty_from_ty_hint_or_reg_class(from_reg.to_reg(), ty);
|
||||
let ty = ty_from_ty_hint_or_reg_class::<M>(from_reg.to_reg(), ty);
|
||||
self.store_spillslot(to_slot, ty, from_reg.to_reg())
|
||||
}
|
||||
|
||||
@@ -953,7 +993,7 @@ impl<M: ABIMachineSpec> ABICallee for ABICalleeImpl<M> {
|
||||
from_slot: SpillSlot,
|
||||
ty: Option<Type>,
|
||||
) -> Self::I {
|
||||
let ty = ty_from_ty_hint_or_reg_class(to_reg.to_reg().to_reg(), ty);
|
||||
let ty = ty_from_ty_hint_or_reg_class::<M>(to_reg.to_reg().to_reg(), ty);
|
||||
self.load_spillslot(from_slot, ty, to_reg.map(|r| r.to_reg()))
|
||||
}
|
||||
}
|
||||
@@ -1092,11 +1132,13 @@ impl<M: ABIMachineSpec> ABICaller for ABICallerImpl<M> {
|
||||
idx: usize,
|
||||
from_reg: Reg,
|
||||
) {
|
||||
let word_rc = M::word_reg_class();
|
||||
let word_bits = M::word_bits() as usize;
|
||||
match &self.sig.args[idx] {
|
||||
&ABIArg::Reg(reg, ty, ext)
|
||||
if ext != ir::ArgumentExtension::None && ty_bits(ty) < 64 =>
|
||||
if ext != ir::ArgumentExtension::None && ty_bits(ty) < word_bits =>
|
||||
{
|
||||
assert_eq!(RegClass::I64, reg.get_class());
|
||||
assert_eq!(word_rc, reg.get_class());
|
||||
let signed = match ext {
|
||||
ir::ArgumentExtension::Uext => false,
|
||||
ir::ArgumentExtension::Sext => true,
|
||||
@@ -1107,15 +1149,15 @@ impl<M: ABIMachineSpec> ABICaller for ABICallerImpl<M> {
|
||||
from_reg,
|
||||
signed,
|
||||
ty_bits(ty) as u8,
|
||||
64,
|
||||
word_bits as u8,
|
||||
));
|
||||
}
|
||||
&ABIArg::Reg(reg, ty, _) => {
|
||||
ctx.emit(M::gen_move(Writable::from_reg(reg.to_reg()), from_reg, ty));
|
||||
}
|
||||
&ABIArg::Stack(off, mut ty, ext) => {
|
||||
if ext != ir::ArgumentExtension::None && ty_bits(ty) < 64 {
|
||||
assert_eq!(RegClass::I64, from_reg.get_class());
|
||||
if ext != ir::ArgumentExtension::None && ty_bits(ty) < word_bits {
|
||||
assert_eq!(word_rc, from_reg.get_class());
|
||||
let signed = match ext {
|
||||
ir::ArgumentExtension::Uext => false,
|
||||
ir::ArgumentExtension::Sext => true,
|
||||
@@ -1129,10 +1171,10 @@ impl<M: ABIMachineSpec> ABICaller for ABICallerImpl<M> {
|
||||
from_reg,
|
||||
signed,
|
||||
ty_bits(ty) as u8,
|
||||
64,
|
||||
word_bits as u8,
|
||||
));
|
||||
// Store the extended version.
|
||||
ty = I64;
|
||||
ty = M::word_type();
|
||||
}
|
||||
ctx.emit(M::gen_store_stack(
|
||||
StackAMode::SPOffset(off, ty),
|
||||
@@ -1169,8 +1211,10 @@ impl<M: ABIMachineSpec> ABICaller for ABICallerImpl<M> {
|
||||
mem::replace(&mut self.uses, Default::default()),
|
||||
mem::replace(&mut self.defs, Default::default()),
|
||||
);
|
||||
let word_rc = M::word_reg_class();
|
||||
let word_type = M::word_type();
|
||||
if let Some(i) = self.sig.stack_ret_arg {
|
||||
let rd = ctx.alloc_tmp(RegClass::I64, I64);
|
||||
let rd = ctx.alloc_tmp(word_rc, word_type);
|
||||
let ret_area_base = self.sig.stack_arg_space;
|
||||
ctx.emit(M::gen_get_stack_addr(
|
||||
StackAMode::SPOffset(ret_area_base, I8),
|
||||
@@ -1179,7 +1223,7 @@ impl<M: ABIMachineSpec> ABICaller for ABICallerImpl<M> {
|
||||
));
|
||||
self.emit_copy_reg_to_arg(ctx, i, rd.to_reg());
|
||||
}
|
||||
let tmp = ctx.alloc_tmp(RegClass::I64, I64);
|
||||
let tmp = ctx.alloc_tmp(word_rc, word_type);
|
||||
for (is_safepoint, inst) in
|
||||
M::gen_call(&self.dest, uses, defs, self.loc, self.opcode, tmp).into_iter()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user