Skip to content

Commit 019496e

Browse files
committed
Instruction::ReturnConst
1 parent 85fa157 commit 019496e

9 files changed

Lines changed: 149 additions & 114 deletions

‎compiler/codegen/src/compile.rs‎

Lines changed: 73 additions & 57 deletions
Large diffs are not rendered by default.

‎compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap‎

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap‎

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap‎

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎compiler/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap‎

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎compiler/core/src/bytecode.rs‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ pub enum Instruction {
486486
target: Arg<Label>,
487487
},
488488
ReturnValue,
489+
ReturnConst {
490+
idx: Arg<u32>,
491+
},
489492
YieldValue,
490493
YieldFrom,
491494
SetupAnnotation,
@@ -1141,7 +1144,12 @@ impl Instruction {
11411144
pub fn unconditional_branch(&self) -> bool {
11421145
matches!(
11431146
self,
1144-
Jump { .. } | Continue { .. } | Break { .. } | ReturnValue | Raise { .. }
1147+
Jump { .. }
1148+
| Continue { .. }
1149+
| Break { .. }
1150+
| ReturnValue
1151+
| ReturnConst { .. }
1152+
| Raise { .. }
11451153
)
11461154
}
11471155

@@ -1219,6 +1227,7 @@ impl Instruction {
12191227
}
12201228
}
12211229
ReturnValue => -1,
1230+
ReturnConst { .. } => 0,
12221231
YieldValue => 0,
12231232
YieldFrom => -1,
12241233
SetupAnnotation | SetupLoop | SetupFinally { .. } | EnterFinally | EndFinally => 0,
@@ -1322,6 +1331,23 @@ impl Instruction {
13221331
let name = |i: u32| ctx.get_name(i as usize);
13231332
let cell_name = |i: u32| ctx.get_cell_name(i as usize);
13241333

1334+
let fmt_const =
1335+
|op: &str, arg: OpArg, f: &mut fmt::Formatter, idx: &Arg<u32>| -> fmt::Result {
1336+
let value = ctx.get_constant(idx.get(arg) as usize);
1337+
match value.borrow_constant() {
1338+
BorrowedConstant::Code { code } if expand_code_objects => {
1339+
write!(f, "{:pad$}({:?}):", op, code)?;
1340+
code.display_inner(f, true, level + 1)?;
1341+
Ok(())
1342+
}
1343+
c => {
1344+
write!(f, "{:pad$}(", op)?;
1345+
c.fmt_display(f)?;
1346+
write!(f, ")")
1347+
}
1348+
}
1349+
};
1350+
13251351
match self {
13261352
ImportName { idx } => w!(ImportName, name = idx),
13271353
ImportNameless => w!(ImportNameless),
@@ -1346,21 +1372,7 @@ impl Instruction {
13461372
DeleteSubscript => w!(DeleteSubscript),
13471373
StoreAttr { idx } => w!(StoreAttr, name = idx),
13481374
DeleteAttr { idx } => w!(DeleteAttr, name = idx),
1349-
LoadConst { idx } => {
1350-
let value = ctx.get_constant(idx.get(arg) as usize);
1351-
match value.borrow_constant() {
1352-
BorrowedConstant::Code { code } if expand_code_objects => {
1353-
write!(f, "{:pad$}({:?}):", "LoadConst", code)?;
1354-
code.display_inner(f, true, level + 1)?;
1355-
Ok(())
1356-
}
1357-
c => {
1358-
write!(f, "{:pad$}(", "LoadConst")?;
1359-
c.fmt_display(f)?;
1360-
write!(f, ")")
1361-
}
1362-
}
1363-
}
1375+
LoadConst { idx } => fmt_const("LoadConst", arg, f, idx),
13641376
UnaryOperation { op } => w!(UnaryOperation, ?op),
13651377
BinaryOperation { op } => w!(BinaryOperation, ?op),
13661378
BinaryOperationInplace { op } => w!(BinaryOperationInplace, ?op),
@@ -1390,6 +1402,7 @@ impl Instruction {
13901402
CallMethodEx { has_kwargs } => w!(CallMethodEx, has_kwargs),
13911403
ForIter { target } => w!(ForIter, target),
13921404
ReturnValue => w!(ReturnValue),
1405+
ReturnConst { idx } => fmt_const("ReturnConst", arg, f, idx),
13931406
YieldValue => w!(YieldValue),
13941407
YieldFrom => w!(YieldFrom),
13951408
SetupAnnotation => w!(SetupAnnotation),

‎jit/src/instructions.rs‎

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -183,35 +183,48 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
183183
Ok(())
184184
}
185185

186-
fn load_const<C: bytecode::Constant>(
186+
fn prepare_const<C: bytecode::Constant>(
187187
&mut self,
188188
constant: BorrowedConstant<C>,
189-
) -> Result<(), JitCompileError> {
190-
match constant {
189+
) -> Result<JitValue, JitCompileError> {
190+
let value = match constant {
191191
BorrowedConstant::Integer { value } => {
192192
let val = self.builder.ins().iconst(
193193
types::I64,
194194
value.to_i64().ok_or(JitCompileError::NotSupported)?,
195195
);
196-
self.stack.push(JitValue::Int(val));
197-
Ok(())
196+
JitValue::Int(val)
198197
}
199198
BorrowedConstant::Float { value } => {
200199
let val = self.builder.ins().f64const(value);
201-
self.stack.push(JitValue::Float(val));
202-
Ok(())
200+
JitValue::Float(val)
203201
}
204202
BorrowedConstant::Boolean { value } => {
205203
let val = self.builder.ins().iconst(types::I8, value as i64);
206-
self.stack.push(JitValue::Bool(val));
207-
Ok(())
204+
JitValue::Bool(val)
208205
}
209-
BorrowedConstant::None => {
210-
self.stack.push(JitValue::None);
211-
Ok(())
206+
BorrowedConstant::None => JitValue::None,
207+
_ => return Err(JitCompileError::NotSupported),
208+
};
209+
Ok(value)
210+
}
211+
212+
fn return_value(&mut self, val: JitValue) -> Result<(), JitCompileError> {
213+
if let Some(ref ty) = self.sig.ret {
214+
if val.to_jit_type().as_ref() != Some(ty) {
215+
return Err(JitCompileError::NotSupported);
212216
}
213-
_ => Err(JitCompileError::NotSupported),
217+
} else {
218+
let ty = val.to_jit_type().ok_or(JitCompileError::NotSupported)?;
219+
self.sig.ret = Some(ty.clone());
220+
self.builder
221+
.func
222+
.signature
223+
.returns
224+
.push(AbiParam::new(ty.to_cranelift()));
214225
}
226+
self.builder.ins().return_(&[val.into_value().unwrap()]);
227+
Ok(())
215228
}
216229

217230
pub fn add_instruction<C: bytecode::Constant>(
@@ -269,7 +282,9 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
269282
self.store_variable(idx.get(arg), val)
270283
}
271284
Instruction::LoadConst { idx } => {
272-
self.load_const(constants[idx.get(arg) as usize].borrow_constant())
285+
let val = self.prepare_const(constants[idx.get(arg) as usize].borrow_constant())?;
286+
self.stack.push(val);
287+
Ok(())
273288
}
274289
Instruction::BuildTuple { size } => {
275290
let elements = self.pop_multiple(size.get(arg) as usize);
@@ -293,21 +308,11 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> {
293308
}
294309
Instruction::ReturnValue => {
295310
let val = self.stack.pop().ok_or(JitCompileError::BadBytecode)?;
296-
if let Some(ref ty) = self.sig.ret {
297-
if val.to_jit_type().as_ref() != Some(ty) {
298-
return Err(JitCompileError::NotSupported);
299-
}
300-
} else {
301-
let ty = val.to_jit_type().ok_or(JitCompileError::NotSupported)?;
302-
self.sig.ret = Some(ty.clone());
303-
self.builder
304-
.func
305-
.signature
306-
.returns
307-
.push(AbiParam::new(ty.to_cranelift()));
308-
}
309-
self.builder.ins().return_(&[val.into_value().unwrap()]);
310-
Ok(())
311+
self.return_value(val)
312+
}
313+
Instruction::ReturnConst { idx } => {
314+
let val = self.prepare_const(constants[idx.get(arg) as usize].borrow_constant())?;
315+
self.return_value(val)
311316
}
312317
Instruction::CompareOperation { op, .. } => {
313318
let op = op.get(arg);

‎jit/tests/common.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ impl StackMachine {
141141
let i = self.stack.len() - 3;
142142
self.stack[i..].rotate_right(1);
143143
}
144+
Instruction::ReturnConst { idx } => {
145+
let idx = idx.get(arg);
146+
self.stack.push(constants[idx as usize].clone().into());
147+
return ControlFlow::Break(());
148+
}
144149
Instruction::ReturnValue => return ControlFlow::Break(()),
145150
Instruction::ExtendedArg => {}
146151
_ => unimplemented!(

‎vm/src/frame.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,10 @@ impl ExecutingFrame<'_> {
806806
let value = self.pop_value();
807807
self.unwind_blocks(vm, UnwindReason::Returning { value })
808808
}
809+
bytecode::Instruction::ReturnConst { idx } => {
810+
let value = self.code.constants[idx.get(arg) as usize].clone().into();
811+
self.unwind_blocks(vm, UnwindReason::Returning { value })
812+
}
809813
bytecode::Instruction::YieldValue => {
810814
let value = self.pop_value();
811815
let value = if self.code.flags.contains(bytecode::CodeFlags::IS_COROUTINE) {

0 commit comments

Comments
 (0)