Append link and sret arguments in legalize_signature.

These special-purpose arguments and return values are only relevant for
the function being compiled, so add a `current` flag to
legalize_signature().

- Add the necessary argument values to the entry block to represent
  the special-purpose arguments.
- Propagate the link and sret arguments to return instructions if the
  legalized signature asks for it.
This commit is contained in:
Jakob Stoklund Olesen
2017-04-19 15:44:17 -07:00
parent d9ddf4fc5a
commit e44a5a4391
9 changed files with 161 additions and 35 deletions

View File

@@ -166,7 +166,24 @@ pub trait TargetIsa {
/// - Vector types can be bit-cast and broken down into smaller vectors or scalars.
///
/// The legalizer will adapt argument and return values as necessary at all ABI boundaries.
fn legalize_signature(&self, _sig: &mut Signature) {
///
/// When this function is called to legalize the signature of the function currently begin
/// compiler, `_current` is true. The legalized signature can then also contain special purpose
/// arguments and return values such as:
///
/// - A `link` argument representing the link registers on RISC architectures that don't push
/// the return address on the stack.
/// - A `link` return value which will receive the value that was passed to the `link`
/// argument.
/// - An `sret` argument can be added if one wasn't present already. This is necessary if the
/// signature returns more values than registers are available for returning values.
/// - An `sret` return value can be added if the ABI requires a function to return its `sret`
/// argument in a register.
///
/// Arguments and return values for the caller's frame pointer and other callee-saved registers
/// should not be added by this function. These arguments are not added until after register
/// allocation.
fn legalize_signature(&self, _sig: &mut Signature, _current: bool) {
unimplemented!()
}

View File

@@ -6,7 +6,7 @@
//! This doesn't support the soft-float ABI at the moment.
use abi::{ArgAction, ValueConversion, ArgAssigner, legalize_args};
use ir::{Signature, Type, ArgumentType, ArgumentLoc, ArgumentExtension};
use ir::{Signature, Type, ArgumentType, ArgumentLoc, ArgumentExtension, ArgumentPurpose};
use isa::riscv::registers::{GPR, FPR};
use settings as shared_settings;
@@ -80,7 +80,7 @@ impl ArgAssigner for Args {
}
/// Legalize `sig` for RISC-V.
pub fn legalize_signature(sig: &mut Signature, flags: &shared_settings::Flags) {
pub fn legalize_signature(sig: &mut Signature, flags: &shared_settings::Flags, current: bool) {
let bits = if flags.is_64bit() { 64 } else { 32 };
let mut args = Args::new(bits);
@@ -88,4 +88,17 @@ pub fn legalize_signature(sig: &mut Signature, flags: &shared_settings::Flags) {
let mut rets = Args::new(bits);
legalize_args(&mut sig.return_types, &mut rets);
if current {
let ptr = Type::int(bits).unwrap();
// Add the link register as an argument and return value.
//
// The `jalr` instruction implementing a return can technically accept the return address
// in any register, but a micro-architecture with a return address predictor will only
// recognize it as a return if the address is in `x1`.
let link = ArgumentType::special_reg(ptr, ArgumentPurpose::Link, GPR.unit(1));
sig.argument_types.push(link);
sig.return_types.push(link);
}
}

View File

@@ -78,9 +78,9 @@ impl TargetIsa for Isa {
})
}
fn legalize_signature(&self, sig: &mut Signature) {
fn legalize_signature(&self, sig: &mut Signature, current: bool) {
// We can pass in `self.isa_flags` too, if we need it.
abi::legalize_signature(sig, &self.shared_flags)
abi::legalize_signature(sig, &self.shared_flags, current)
}
fn emit_inst(&self, func: &Function, inst: Inst, sink: &mut CodeSink) {