Implement SystemV struct argument passing
This commit is contained in:
@@ -18,6 +18,10 @@ pub enum ArgAction {
|
||||
/// Assign the argument to the given location.
|
||||
Assign(ArgumentLoc),
|
||||
|
||||
/// Assign the argument to the given location and change the type to the specified type.
|
||||
/// This is used by [`ArgumentPurpose::StructArgument`].
|
||||
AssignAndChangeType(ArgumentLoc, Type),
|
||||
|
||||
/// Convert the argument, then call again.
|
||||
///
|
||||
/// This action can split an integer type into two smaller integer arguments, or it can split a
|
||||
@@ -119,6 +123,13 @@ pub fn legalize_args<AA: ArgAssigner>(args: &[AbiParam], aa: &mut AA) -> Option<
|
||||
args.to_mut()[argno].location = loc;
|
||||
argno += 1;
|
||||
}
|
||||
// Assign argument to a location, change type to `INVALID` and move on to the next one.
|
||||
ArgAction::AssignAndChangeType(loc, ty) => {
|
||||
let arg = &mut args.to_mut()[argno];
|
||||
arg.location = loc;
|
||||
arg.value_type = ty;
|
||||
argno += 1;
|
||||
}
|
||||
// Split this argument into two smaller ones. Then revisit both.
|
||||
ArgAction::Convert(conv) => {
|
||||
debug_assert!(
|
||||
|
||||
@@ -282,6 +282,9 @@ pub enum ArgumentPurpose {
|
||||
/// A normal user program value passed to or from a function.
|
||||
Normal,
|
||||
|
||||
/// A C struct passed as argument.
|
||||
StructArgument(u32),
|
||||
|
||||
/// Struct return pointer.
|
||||
///
|
||||
/// When a function needs to return more data than will fit in registers, the caller passes a
|
||||
@@ -334,21 +337,19 @@ pub enum ArgumentPurpose {
|
||||
StackLimit,
|
||||
}
|
||||
|
||||
/// Text format names of the `ArgumentPurpose` variants.
|
||||
static PURPOSE_NAMES: [&str; 8] = [
|
||||
"normal",
|
||||
"sret",
|
||||
"link",
|
||||
"fp",
|
||||
"csr",
|
||||
"vmctx",
|
||||
"sigid",
|
||||
"stack_limit",
|
||||
];
|
||||
|
||||
impl fmt::Display for ArgumentPurpose {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(PURPOSE_NAMES[*self as usize])
|
||||
f.write_str(match self {
|
||||
Self::Normal => "normal",
|
||||
Self::StructArgument(size) => return write!(f, "sarg({})", size),
|
||||
Self::StructReturn => "sret",
|
||||
Self::Link => "link",
|
||||
Self::FramePointer => "fp",
|
||||
Self::CalleeSaved => "csr",
|
||||
Self::VMContext => "vmctx",
|
||||
Self::SignatureId => "sigid",
|
||||
Self::StackLimit => "stack_limit",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,6 +365,13 @@ impl FromStr for ArgumentPurpose {
|
||||
"vmctx" => Ok(Self::VMContext),
|
||||
"sigid" => Ok(Self::SignatureId),
|
||||
"stack_limit" => Ok(Self::StackLimit),
|
||||
_ if s.starts_with("sarg(") => {
|
||||
if !s.ends_with(")") {
|
||||
return Err(());
|
||||
}
|
||||
let size: u32 = s["sarg(".len()..s.len() - 1].parse().map_err(|_| ())?;
|
||||
Ok(Self::StructArgument(size))
|
||||
}
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
@@ -436,16 +444,17 @@ mod tests {
|
||||
#[test]
|
||||
fn argument_purpose() {
|
||||
let all_purpose = [
|
||||
ArgumentPurpose::Normal,
|
||||
ArgumentPurpose::StructReturn,
|
||||
ArgumentPurpose::Link,
|
||||
ArgumentPurpose::FramePointer,
|
||||
ArgumentPurpose::CalleeSaved,
|
||||
ArgumentPurpose::VMContext,
|
||||
ArgumentPurpose::SignatureId,
|
||||
ArgumentPurpose::StackLimit,
|
||||
(ArgumentPurpose::Normal, "normal"),
|
||||
(ArgumentPurpose::StructReturn, "sret"),
|
||||
(ArgumentPurpose::Link, "link"),
|
||||
(ArgumentPurpose::FramePointer, "fp"),
|
||||
(ArgumentPurpose::CalleeSaved, "csr"),
|
||||
(ArgumentPurpose::VMContext, "vmctx"),
|
||||
(ArgumentPurpose::SignatureId, "sigid"),
|
||||
(ArgumentPurpose::StackLimit, "stack_limit"),
|
||||
(ArgumentPurpose::StructArgument(42), "sarg(42)"),
|
||||
];
|
||||
for (&e, &n) in all_purpose.iter().zip(PURPOSE_NAMES.iter()) {
|
||||
for &(e, n) in &all_purpose {
|
||||
assert_eq!(e.to_string(), n);
|
||||
assert_eq!(Ok(e), n.parse());
|
||||
}
|
||||
|
||||
@@ -287,7 +287,12 @@ impl StackSlots {
|
||||
|
||||
/// Create a stack slot representing an incoming function argument.
|
||||
pub fn make_incoming_arg(&mut self, ty: Type, offset: StackOffset) -> StackSlot {
|
||||
let mut data = StackSlotData::new(StackSlotKind::IncomingArg, ty.bytes());
|
||||
self.make_incoming_struct_arg(ty.bytes(), offset)
|
||||
}
|
||||
|
||||
/// Create a stack slot representing an incoming struct function argument.
|
||||
pub fn make_incoming_struct_arg(&mut self, size: u32, offset: StackOffset) -> StackSlot {
|
||||
let mut data = StackSlotData::new(StackSlotKind::IncomingArg, size);
|
||||
debug_assert!(offset <= StackOffset::max_value() - data.size as StackOffset);
|
||||
data.offset = Some(offset);
|
||||
self.push(data)
|
||||
@@ -301,8 +306,11 @@ impl StackSlots {
|
||||
/// The requested offset is relative to this function's stack pointer immediately before making
|
||||
/// the call.
|
||||
pub fn get_outgoing_arg(&mut self, ty: Type, offset: StackOffset) -> StackSlot {
|
||||
let size = ty.bytes();
|
||||
self.get_outgoing_struct_arg(ty.bytes(), offset)
|
||||
}
|
||||
|
||||
/// FIXME
|
||||
pub fn get_outgoing_struct_arg(&mut self, size: u32, offset: StackOffset) -> StackSlot {
|
||||
// Look for an existing outgoing stack slot with the same offset and size.
|
||||
let inspos = match self.outgoing.binary_search_by_key(&(offset, size), |&ss| {
|
||||
(self[ss].offset.unwrap(), self[ss].size)
|
||||
|
||||
@@ -343,6 +343,7 @@ impl Display for Type {
|
||||
f.write_str(match *self {
|
||||
IFLAGS => "iflags",
|
||||
FFLAGS => "fflags",
|
||||
SARG__ => "sarg__",
|
||||
INVALID => panic!("INVALID encountered"),
|
||||
_ => panic!("Unknown Type(0x{:x})", self.0),
|
||||
})
|
||||
|
||||
@@ -2152,6 +2152,8 @@ pub(crate) fn lower_insn_to_regs<C: LowerCtx<I = Inst>>(
|
||||
panic!("x86-specific opcode in supposedly arch-neutral IR!");
|
||||
}
|
||||
|
||||
Opcode::DummySarg => unreachable!(),
|
||||
|
||||
Opcode::AvgRound => unimplemented!(),
|
||||
Opcode::Iabs => unimplemented!(),
|
||||
Opcode::Snarrow
|
||||
|
||||
@@ -108,6 +108,19 @@ impl Args {
|
||||
|
||||
impl ArgAssigner for Args {
|
||||
fn assign(&mut self, arg: &AbiParam) -> ArgAction {
|
||||
if let ArgumentPurpose::StructArgument(size) = arg.purpose {
|
||||
if self.call_conv != CallConv::SystemV {
|
||||
panic!(
|
||||
"The sarg argument purpose is not yet implemented for non-systemv call conv {:?}",
|
||||
self.call_conv,
|
||||
);
|
||||
}
|
||||
let loc = ArgumentLoc::Stack(self.offset as i32);
|
||||
self.offset += size;
|
||||
debug_assert!(self.offset <= i32::MAX as u32);
|
||||
return ArgAction::AssignAndChangeType(loc, types::SARG__);
|
||||
}
|
||||
|
||||
let ty = arg.value_type;
|
||||
|
||||
if ty.bits() > u16::from(self.pointer_bits) {
|
||||
|
||||
@@ -22,8 +22,9 @@ use crate::cursor::{Cursor, FuncCursor};
|
||||
use crate::flowgraph::ControlFlowGraph;
|
||||
use crate::ir::instructions::CallInfo;
|
||||
use crate::ir::{
|
||||
AbiParam, ArgumentLoc, ArgumentPurpose, Block, DataFlowGraph, Function, Inst, InstBuilder,
|
||||
MemFlags, SigRef, Signature, StackSlotData, StackSlotKind, Type, Value, ValueLoc,
|
||||
AbiParam, ArgumentLoc, ArgumentPurpose, Block, DataFlowGraph, ExtFuncData, ExternalName,
|
||||
Function, Inst, InstBuilder, LibCall, MemFlags, SigRef, Signature, StackSlotData,
|
||||
StackSlotKind, Type, Value, ValueLoc,
|
||||
};
|
||||
use crate::isa::TargetIsa;
|
||||
use crate::legalizer::split::{isplit, vsplit};
|
||||
@@ -113,12 +114,33 @@ fn legalize_entry_params(func: &mut Function, entry: Block) {
|
||||
|
||||
let abi_type = pos.func.signature.params[abi_arg];
|
||||
let arg_type = pos.func.dfg.value_type(arg);
|
||||
match abi_type.purpose {
|
||||
ArgumentPurpose::StructArgument(size) => {
|
||||
let offset = if let ArgumentLoc::Stack(offset) = abi_type.location {
|
||||
offset
|
||||
} else {
|
||||
unreachable!("StructArgument must already have a Stack ArgumentLoc assigned");
|
||||
};
|
||||
let ss = pos.func.stack_slots.make_incoming_struct_arg(size, offset);
|
||||
let struct_arg = pos.ins().stack_addr(arg_type, ss, 0);
|
||||
pos.func.dfg.change_to_alias(arg, struct_arg);
|
||||
let dummy = pos
|
||||
.func
|
||||
.dfg
|
||||
.append_block_param(entry, crate::ir::types::SARG__);
|
||||
pos.func.locations[dummy] = ValueLoc::Stack(ss);
|
||||
abi_arg += 1;
|
||||
continue;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if arg_type == abi_type.value_type {
|
||||
// No value translation is necessary, this argument matches the ABI type.
|
||||
// Just use the original block argument value. This is the most common case.
|
||||
pos.func.dfg.attach_block_param(entry, arg);
|
||||
match abi_type.purpose {
|
||||
ArgumentPurpose::Normal => {}
|
||||
ArgumentPurpose::StructArgument(_) => unreachable!("Handled above"),
|
||||
ArgumentPurpose::FramePointer => {}
|
||||
ArgumentPurpose::CalleeSaved => {}
|
||||
ArgumentPurpose::StructReturn => {
|
||||
@@ -137,7 +159,7 @@ fn legalize_entry_params(func: &mut Function, entry: Block) {
|
||||
debug_assert!(!has_stack_limit, "Multiple stack_limit arguments found");
|
||||
has_stack_limit = true;
|
||||
}
|
||||
_ => panic!("Unexpected special-purpose arg {}", abi_type),
|
||||
ArgumentPurpose::Link => panic!("Unexpected link arg {}", abi_type),
|
||||
}
|
||||
abi_arg += 1;
|
||||
} else {
|
||||
@@ -168,7 +190,7 @@ fn legalize_entry_params(func: &mut Function, entry: Block) {
|
||||
for &arg in &pos.func.signature.params[abi_arg..] {
|
||||
match arg.purpose {
|
||||
// Any normal parameters should have been processed above.
|
||||
ArgumentPurpose::Normal => {
|
||||
ArgumentPurpose::Normal | ArgumentPurpose::StructArgument(_) => {
|
||||
panic!("Leftover arg: {}", arg);
|
||||
}
|
||||
// The callee-save parameters should not appear until after register allocation is
|
||||
@@ -587,9 +609,20 @@ fn convert_to_abi<PutArg>(
|
||||
|
||||
/// Check if a sequence of arguments match a desired sequence of argument types.
|
||||
fn check_arg_types(dfg: &DataFlowGraph, args: &[Value], types: &[AbiParam]) -> bool {
|
||||
let arg_types = args.iter().map(|&v| dfg.value_type(v));
|
||||
let sig_types = types.iter().map(|&at| at.value_type);
|
||||
arg_types.eq(sig_types)
|
||||
let mut args = args.iter();
|
||||
let mut types = types.iter();
|
||||
loop {
|
||||
match (args.next(), types.next()) {
|
||||
(Some(&v), Some(at)) => {
|
||||
if let ArgumentPurpose::StructArgument(_) = at.purpose {
|
||||
} else if dfg.value_type(v) != at.value_type {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
(Some(_), None) | (None, Some(_)) => return false,
|
||||
(None, None) => return true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the arguments of the call `inst` match the signature.
|
||||
@@ -699,7 +732,12 @@ fn legalize_inst_arguments<ArgType>(
|
||||
.unwrap();
|
||||
let mut put_arg = |func: &mut Function, arg| {
|
||||
let abi_type = get_abi_type(func, abi_arg);
|
||||
if func.dfg.value_type(arg) == abi_type.value_type {
|
||||
let struct_argument = if let ArgumentPurpose::StructArgument(_) = abi_type.purpose {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if func.dfg.value_type(arg) == abi_type.value_type || struct_argument {
|
||||
// This is the argument type we need.
|
||||
vlist.as_mut_slice(&mut func.dfg.value_lists)[num_fixed_values + abi_arg] = arg;
|
||||
abi_arg += 1;
|
||||
@@ -762,7 +800,7 @@ pub fn handle_call_abi(
|
||||
|
||||
// Start by checking if the argument types already match the signature.
|
||||
let sig_ref = match check_call_signature(&pos.func.dfg, inst) {
|
||||
Ok(_) => return spill_call_arguments(pos),
|
||||
Ok(_) => return spill_call_arguments(pos, isa),
|
||||
Err(s) => s,
|
||||
};
|
||||
|
||||
@@ -800,7 +838,7 @@ pub fn handle_call_abi(
|
||||
|
||||
// Go back and insert spills for any stack arguments.
|
||||
pos.goto_inst(inst);
|
||||
spill_call_arguments(pos);
|
||||
spill_call_arguments(pos, isa);
|
||||
|
||||
// Yes, we changed stuff.
|
||||
true
|
||||
@@ -990,7 +1028,8 @@ fn spill_entry_params(func: &mut Function, entry: Block) {
|
||||
.iter()
|
||||
.zip(func.dfg.block_params(entry))
|
||||
{
|
||||
if let ArgumentLoc::Stack(offset) = abi.location {
|
||||
if let ArgumentPurpose::StructArgument(_) = abi.purpose {
|
||||
} else if let ArgumentLoc::Stack(offset) = abi.location {
|
||||
let ss = func.stack_slots.make_incoming_arg(abi.value_type, offset);
|
||||
func.locations[arg] = ValueLoc::Stack(ss);
|
||||
}
|
||||
@@ -1005,7 +1044,7 @@ fn spill_entry_params(func: &mut Function, entry: Block) {
|
||||
/// TODO: The outgoing stack slots can be written a bit earlier, as long as there are no branches
|
||||
/// or calls between writing the stack slots and the call instruction. Writing the slots earlier
|
||||
/// could help reduce register pressure before the call.
|
||||
fn spill_call_arguments(pos: &mut FuncCursor) -> bool {
|
||||
fn spill_call_arguments(pos: &mut FuncCursor, isa: &dyn TargetIsa) -> bool {
|
||||
let inst = pos
|
||||
.current_inst()
|
||||
.expect("Cursor must point to a call instruction");
|
||||
@@ -1032,9 +1071,15 @@ fn spill_call_arguments(pos: &mut FuncCursor) -> bool {
|
||||
// Assign `arg` to a new stack slot, unless it's already in the correct
|
||||
// slot. The legalization needs to be idempotent, so we should see a
|
||||
// correct outgoing slot on the second pass.
|
||||
let ss = stack_slots.get_outgoing_arg(abi.value_type, offset);
|
||||
let (ss, size) = match abi.purpose {
|
||||
ArgumentPurpose::StructArgument(size) => (
|
||||
stack_slots.get_outgoing_struct_arg(size, offset),
|
||||
Some(size),
|
||||
),
|
||||
_ => (stack_slots.get_outgoing_arg(abi.value_type, offset), None),
|
||||
};
|
||||
if locations[arg] != ValueLoc::Stack(ss) {
|
||||
Some((idx, arg, ss))
|
||||
Some((idx, arg, ss, size))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -1050,8 +1095,34 @@ fn spill_call_arguments(pos: &mut FuncCursor) -> bool {
|
||||
}
|
||||
|
||||
// Insert the spill instructions and rewrite call arguments.
|
||||
for (idx, arg, ss) in arglist {
|
||||
let stack_val = pos.ins().spill(arg);
|
||||
for (idx, arg, ss, size) in arglist {
|
||||
let stack_val = if let Some(size) = size {
|
||||
// Struct argument
|
||||
let pointer_type = pos.func.dfg.value_type(arg);
|
||||
let src = arg;
|
||||
let dest = pos.ins().stack_addr(pointer_type, ss, 0);
|
||||
let size = pos.ins().iconst(pointer_type, i64::from(size));
|
||||
let signature = {
|
||||
let mut s = Signature::new(isa.default_call_conv());
|
||||
s.params.push(AbiParam::new(pointer_type));
|
||||
s.params.push(AbiParam::new(pointer_type));
|
||||
s.params.push(AbiParam::new(pointer_type));
|
||||
legalize_libcall_signature(&mut s, isa);
|
||||
pos.func.import_signature(s)
|
||||
};
|
||||
|
||||
let libc_memcpy = pos.func.import_function(ExtFuncData {
|
||||
name: ExternalName::LibCall(LibCall::Memcpy),
|
||||
signature,
|
||||
colocated: false,
|
||||
});
|
||||
|
||||
pos.ins().call(libc_memcpy, &[dest, src, size]);
|
||||
pos.ins().dummy_sarg__()
|
||||
} else {
|
||||
// Non struct argument
|
||||
pos.ins().spill(arg)
|
||||
};
|
||||
pos.func.locations[stack_val] = ValueLoc::Stack(ss);
|
||||
pos.func.dfg.inst_variable_args_mut(inst)[idx] = stack_val;
|
||||
}
|
||||
|
||||
@@ -65,9 +65,9 @@ use crate::ir;
|
||||
use crate::ir::entities::AnyEntity;
|
||||
use crate::ir::instructions::{BranchInfo, CallInfo, InstructionFormat, ResolvedConstraint};
|
||||
use crate::ir::{
|
||||
types, ArgumentLoc, Block, Constant, FuncRef, Function, GlobalValue, Inst, InstructionData,
|
||||
JumpTable, Opcode, SigRef, StackSlot, StackSlotKind, Type, Value, ValueDef, ValueList,
|
||||
ValueLoc,
|
||||
types, ArgumentLoc, ArgumentPurpose, Block, Constant, FuncRef, Function, GlobalValue, Inst,
|
||||
InstructionData, JumpTable, Opcode, SigRef, StackSlot, StackSlotKind, Type, Value, ValueDef,
|
||||
ValueList, ValueLoc,
|
||||
};
|
||||
use crate::isa::TargetIsa;
|
||||
use crate::iterators::IteratorExtras;
|
||||
@@ -1473,7 +1473,8 @@ impl<'a> Verifier<'a> {
|
||||
),
|
||||
));
|
||||
}
|
||||
if slot.size != abi.value_type.bytes() {
|
||||
if abi.purpose == ArgumentPurpose::StructArgument(slot.size) {
|
||||
} else if slot.size != abi.value_type.bytes() {
|
||||
return errors.fatal((
|
||||
inst,
|
||||
self.context(inst),
|
||||
@@ -1986,6 +1987,20 @@ impl<'a> Verifier<'a> {
|
||||
))
|
||||
});
|
||||
|
||||
self.func
|
||||
.signature
|
||||
.returns
|
||||
.iter()
|
||||
.enumerate()
|
||||
.for_each(|(i, ret)| {
|
||||
if let ArgumentPurpose::StructArgument(_) = ret.purpose {
|
||||
errors.report((
|
||||
AnyEntity::Function,
|
||||
format!("Return value at position {} can't be an struct argument", i),
|
||||
))
|
||||
}
|
||||
});
|
||||
|
||||
if errors.has_error() {
|
||||
Err(())
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user