Skip to content

Commit d1af6b5

Browse files
authored
Merge pull request RustPython#4341 from evilpie/jit-tuple
Support variable exchange (aka basic tuple support)
2 parents 54764c8 + 8523392 commit d1af6b5

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

jit/src/instructions.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ enum JitValue {
2626
Float(Value),
2727
Bool(Value),
2828
None,
29+
Tuple(Vec<JitValue>),
2930
}
3031

3132
impl JitValue {
@@ -42,14 +43,14 @@ impl JitValue {
4243
JitValue::Int(_) => Some(JitType::Int),
4344
JitValue::Float(_) => Some(JitType::Float),
4445
JitValue::Bool(_) => Some(JitType::Bool),
45-
JitValue::None => None,
46+
JitValue::None | JitValue::Tuple(_) => None,
4647
}
4748
}
4849

4950
fn into_value(self) -> Option<Value> {
5051
match self {
5152
JitValue::Int(val) | JitValue::Float(val) | JitValue::Bool(val) => Some(val),
52-
JitValue::None => None,
53+
JitValue::None | JitValue::Tuple(_) => None,
5354
}
5455
}
5556
}
@@ -88,6 +89,11 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
8889
compiler
8990
}
9091

92+
fn pop_multiple(&mut self, count: usize) -> Vec<JitValue> {
93+
let stack_len = self.stack.len();
94+
self.stack.drain(stack_len - count..).collect()
95+
}
96+
9197
fn store_variable(
9298
&mut self,
9399
idx: bytecode::NameIdx,
@@ -126,6 +132,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
126132
}
127133
JitValue::Bool(val) => Ok(val),
128134
JitValue::None => Ok(self.builder.ins().iconst(types::I8, 0)),
135+
JitValue::Tuple(_) => Err(JitCompileError::NotSupported),
129136
}
130137
}
131138

@@ -260,6 +267,26 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
260267
Instruction::LoadConst { idx } => {
261268
self.load_const(constants[*idx as usize].borrow_constant())
262269
}
270+
Instruction::BuildTuple { unpack, size } if !unpack => {
271+
let elements = self.pop_multiple(*size as usize);
272+
self.stack.push(JitValue::Tuple(elements));
273+
Ok(())
274+
}
275+
Instruction::UnpackSequence { size } => {
276+
let val = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
277+
278+
let elements = match val {
279+
JitValue::Tuple(elements) => elements,
280+
_ => return Err(JitCompileError::NotSupported),
281+
};
282+
283+
if elements.len() != *size as usize {
284+
return Err(JitCompileError::NotSupported);
285+
}
286+
287+
self.stack.extend(elements.into_iter().rev());
288+
Ok(())
289+
}
263290
Instruction::ReturnValue => {
264291
let val = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
265292
if let Some(ref ty) = self.sig.ret {

jit/tests/misc_tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,15 @@ fn test_while_loop() {
101101
assert_eq!(while_loop(1), Ok(1));
102102
assert_eq!(while_loop(10), Ok(10));
103103
}
104+
105+
#[test]
106+
fn test_unpack_tuple() {
107+
let unpack_tuple = jit_function! { unpack_tuple(a:i64, b:i64) -> i64 => r##"
108+
def unpack_tuple(a: int, b: int):
109+
a, b = b, a
110+
return a
111+
"## };
112+
113+
assert_eq!(unpack_tuple(0, 1), Ok(1));
114+
assert_eq!(unpack_tuple(1, 2), Ok(2));
115+
}

0 commit comments

Comments
 (0)