ruby-changes:68984
From: Aaron <ko1@a...>
Date: Thu, 21 Oct 2021 08:19:34 +0900 (JST)
Subject: [ruby-changes:68984] 9043ad3d74 (master): Implement topn instruction
https://git.ruby-lang.org/ruby.git/commit/?id=9043ad3d74 From 9043ad3d74413823e62b11923652d272e41dc230 Mon Sep 17 00:00:00 2001 From: Aaron Patterson <tenderlove@r...> Date: Thu, 10 Jun 2021 09:40:30 -0700 Subject: Implement topn instruction This commit implements the topn instruction Co-Authored-By: Maxime Chevalier-Boisvert <maxime.chevalierboisvert@s...> Co-Authored-By: Noah Gibbs <noah.gibbs@s...> --- bootstraptest/test_yjit.rb | 18 ++++++++++++++++++ yjit_codegen.c | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/bootstraptest/test_yjit.rb b/bootstraptest/test_yjit.rb index 51c82b55b5..fcf027d728 100644 --- a/bootstraptest/test_yjit.rb +++ b/bootstraptest/test_yjit.rb @@ -1,3 +1,21 @@ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_yjit.rb#L1 +# Test for topn +assert_equal 'array', %q{ + def threequals(a) + case a + when Array + "array" + when Hash + "hash" + else + "unknown" + end + end + + threequals([]) + threequals([]) + threequals([]) +} + # Test for opt_mod assert_equal '2', %q{ def mod(a, b) diff --git a/yjit_codegen.c b/yjit_codegen.c index dc148c1951..487c1cee98 100644 --- a/yjit_codegen.c +++ b/yjit_codegen.c @@ -583,6 +583,23 @@ gen_setn(jitstate_t* jit, ctx_t* ctx) https://github.com/ruby/ruby/blob/trunk/yjit_codegen.c#L583 return YJIT_KEEP_COMPILING; } +// get nth stack value, then push it +static codegen_status_t +gen_topn(jitstate_t* jit, ctx_t* ctx) +{ + int32_t n = (int32_t)jit_get_arg(jit, 0); + + // Get top n type / operand + val_type_t top_n_type = ctx_get_opnd_type(ctx, OPND_STACK(n)); + x86opnd_t top_n_val = ctx_stack_opnd(ctx, n); + + x86opnd_t loc0 = ctx_stack_push(ctx, top_n_type); + mov(cb, REG0, top_n_val); + mov(cb, loc0, REG0); + + return YJIT_KEEP_COMPILING; +} + static codegen_status_t gen_pop(jitstate_t* jit, ctx_t* ctx) { @@ -3191,6 +3208,7 @@ yjit_init_codegen(void) https://github.com/ruby/ruby/blob/trunk/yjit_codegen.c#L3208 yjit_reg_op(BIN(dup), gen_dup); yjit_reg_op(BIN(dupn), gen_dupn); yjit_reg_op(BIN(setn), gen_setn); + yjit_reg_op(BIN(topn), gen_topn); yjit_reg_op(BIN(pop), gen_pop); yjit_reg_op(BIN(adjuststack), gen_adjuststack); yjit_reg_op(BIN(newarray), gen_newarray); -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/