Skip to content

Commit 802d1f8

Browse files
committed
ZJIT: Implement opt_swap
1 parent aaa956e commit 802d1f8

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

zjit/src/hir.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,16 @@ pub fn iseq_to_hir(iseq: *const rb_iseq_t) -> Result<Function, ParseError> {
26062606
let val = state.stack_pop()?;
26072607
fun.push_insn(block, Insn::SetIvar { self_val: self_param, id, val, state: exit_id });
26082608
}
2609+
YARVINSN_opt_reverse => {
2610+
// Reverse the order of the top N stack items.
2611+
let n = get_arg(pc, 0).as_usize();
2612+
for i in 0..n/2 {
2613+
let bottom = state.stack_topn(i)?;
2614+
let top = state.stack_topn(n - 1 - i)?;
2615+
state.stack_setn(i, top);
2616+
state.stack_setn(n - 1 - i, bottom);
2617+
}
2618+
}
26092619
YARVINSN_newrange => {
26102620
let flag = RangeType::from(get_arg(pc, 0).as_u32());
26112621
let high = state.stack_pop()?;
@@ -4164,6 +4174,47 @@ mod tests {
41644174
"#]]);
41654175
}
41664176

4177+
#[test]
4178+
fn opt_reverse() {
4179+
eval("
4180+
def reverse_odd
4181+
a, b, c = @a, @b, @c
4182+
[a, b, c]
4183+
end
4184+
4185+
def reverse_even
4186+
a, b, c, d = @a, @b, @c, @d
4187+
[a, b, c, d]
4188+
end
4189+
");
4190+
assert_method_hir_with_opcode("reverse_odd", YARVINSN_opt_reverse, expect![[r#"
4191+
fn reverse_odd:
4192+
bb0(v0:BasicObject):
4193+
v1:NilClassExact = Const Value(nil)
4194+
v2:NilClassExact = Const Value(nil)
4195+
v3:NilClassExact = Const Value(nil)
4196+
v6:BasicObject = GetIvar v0, :@a
4197+
v8:BasicObject = GetIvar v0, :@b
4198+
v10:BasicObject = GetIvar v0, :@c
4199+
v12:ArrayExact = NewArray v6, v8, v10
4200+
Return v12
4201+
"#]]);
4202+
assert_method_hir_with_opcode("reverse_even", YARVINSN_opt_reverse, expect![[r#"
4203+
fn reverse_even:
4204+
bb0(v0:BasicObject):
4205+
v1:NilClassExact = Const Value(nil)
4206+
v2:NilClassExact = Const Value(nil)
4207+
v3:NilClassExact = Const Value(nil)
4208+
v4:NilClassExact = Const Value(nil)
4209+
v7:BasicObject = GetIvar v0, :@a
4210+
v9:BasicObject = GetIvar v0, :@b
4211+
v11:BasicObject = GetIvar v0, :@c
4212+
v13:BasicObject = GetIvar v0, :@d
4213+
v15:ArrayExact = NewArray v7, v9, v11, v13
4214+
Return v15
4215+
"#]]);
4216+
}
4217+
41674218
#[test]
41684219
fn test_branchnil() {
41694220
eval("

0 commit comments

Comments
 (0)