Define a return instruction.

It is possible to return multiple values from a function, so ReturnData contains
a VariableArgs instance.

We don't want return instructions to appear as 'return (v1)', so tweak the
printing of VariableArgs so the parantheses are added externally.
This commit is contained in:
Jakob Stoklund Olesen
2016-07-08 16:19:26 -07:00
parent a39e418d32
commit 520a438c42
7 changed files with 61 additions and 21 deletions

View File

@@ -210,6 +210,11 @@ pub enum InstructionData {
ty: Type,
data: Box<CallData>,
},
Return {
opcode: Opcode,
ty: Type,
data: Box<ReturnData>,
},
}
/// A variable list of `Value` operands used for function call arguments and passing arguments to
@@ -248,7 +253,6 @@ impl DerefMut for VariableArgs {
impl Display for VariableArgs {
fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
try!(write!(fmt, "("));
for (i, val) in self.0.iter().enumerate() {
if i == 0 {
try!(write!(fmt, "{}", val));
@@ -256,7 +260,7 @@ impl Display for VariableArgs {
try!(write!(fmt, ", {}", val));
}
}
write!(fmt, ")")
Ok(())
}
}
@@ -279,7 +283,7 @@ impl Display for JumpData {
if self.arguments.is_empty() {
write!(f, "{}", self.destination)
} else {
write!(f, "{}{}", self.destination, self.arguments)
write!(f, "{}({})", self.destination, self.arguments)
}
}
}
@@ -297,7 +301,7 @@ impl Display for BranchData {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
try!(write!(f, "{}, {}", self.arg, self.destination));
if !self.arguments.is_empty() {
try!(write!(f, "{}", self.arguments));
try!(write!(f, "({})", self.arguments));
}
Ok(())
}
@@ -310,15 +314,22 @@ pub struct CallData {
second_result: Value,
// Dynamically sized array containing call argument values.
pub arguments: VariableArgs,
pub args: VariableArgs,
}
impl Display for CallData {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "TBD{}", self.arguments)
write!(f, "TBD({})", self.args)
}
}
/// Payload of a return instruction.
#[derive(Debug)]
pub struct ReturnData {
// Dynamically sized array containing return values.
pub args: VariableArgs,
}
impl InstructionData {
/// Create data for a call instruction.
pub fn call(opc: Opcode, return_type: Type) -> InstructionData {
@@ -327,7 +338,7 @@ impl InstructionData {
ty: return_type,
data: Box::new(CallData {
second_result: NO_VALUE,
arguments: VariableArgs::new(),
args: VariableArgs::new(),
}),
}
}

View File

@@ -203,6 +203,13 @@ pub fn write_instruction(w: &mut Write, func: &Function, inst: Inst) -> Result {
Branch { ref data, .. } => writeln!(w, " {}", data),
BranchTable { arg, table, .. } => writeln!(w, " {}, {}", arg, table),
Call { ref data, .. } => writeln!(w, " {}", data),
Return { ref data, .. } => {
if data.args.is_empty() {
writeln!(w, "")
} else {
writeln!(w, " {}", data.args)
}
}
}
}

View File

@@ -15,7 +15,7 @@ use cretonne::types::{Type, VOID, FunctionName, Signature, ArgumentType, Argumen
use cretonne::immediates::{Imm64, Ieee32, Ieee64};
use cretonne::entities::*;
use cretonne::instructions::{Opcode, InstructionFormat, InstructionData, VariableArgs, JumpData,
BranchData};
BranchData, ReturnData};
use cretonne::repr::{Function, StackSlotData};
pub use lexer::Location;
@@ -201,7 +201,11 @@ impl Context {
}
InstructionData::Call { ref mut data, .. } => {
try!(Self::rewrite_values(&self.values, &mut data.arguments, &loc));
try!(Self::rewrite_values(&self.values, &mut data.args, &loc));
}
InstructionData::Return { ref mut data, .. } => {
try!(Self::rewrite_values(&self.values, &mut data.args, &loc));
}
}
}
@@ -1015,6 +1019,14 @@ impl<'a> Parser<'a> {
args: [lhs, rhs],
}
}
InstructionFormat::Return => {
let args = try!(self.parse_value_list());
InstructionData::Return {
opcode: opcode,
ty: VOID,
data: Box::new(ReturnData { args: args }),
}
}
InstructionFormat::BranchTable |
InstructionFormat::Call => {
unimplemented!();