[前][次][番号順一覧][スレッド一覧]

ruby-changes:69063

From: John <ko1@a...>
Date: Thu, 21 Oct 2021 08:20:43 +0900 (JST)
Subject: [ruby-changes:69063] c210fade27 (master): Implement newrange

https://git.ruby-lang.org/ruby.git/commit/?id=c210fade27

From c210fade27c2f39f078c9b1aec71a05532fb832b Mon Sep 17 00:00:00 2001
From: John Hawthorn <john@h...>
Date: Sat, 17 Jul 2021 03:26:46 -0700
Subject: Implement newrange

---
 test/ruby/test_yjit.rb |  8 ++++++++
 yjit_codegen.c         | 23 +++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/test/ruby/test_yjit.rb b/test/ruby/test_yjit.rb
index 50b415107d..94989dae9e 100644
--- a/test/ruby/test_yjit.rb
+++ b/test/ruby/test_yjit.rb
@@ -36,6 +36,14 @@ class TestYJIT < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_yjit.rb#L36
     assert_compiles('[1, 2, 3]', insns: %i[duparray], result: [1, 2, 3])
   end
 
+  def test_compile_newrange
+    assert_compiles('s = 1; (s..5)', insns: %i[newrange], result: 1..5)
+    assert_compiles('s = 1; e = 5; (s..e)', insns: %i[newrange], result: 1..5)
+    assert_compiles('s = 1; (s...5)', insns: %i[newrange], result: 1...5)
+    assert_compiles('s = 1; (s..)', insns: %i[newrange], result: 1..)
+    assert_compiles('e = 5; (..e)', insns: %i[newrange], result: ..5)
+  end
+
   def test_compile_opt_nil_p
     assert_compiles('nil.nil?', insns: %i[opt_nil_p], result: true)
     assert_compiles('false.nil?', insns: %i[opt_nil_p], result: false)
diff --git a/yjit_codegen.c b/yjit_codegen.c
index 08ac6f0ec6..0abf2376c6 100644
--- a/yjit_codegen.c
+++ b/yjit_codegen.c
@@ -917,6 +917,28 @@ gen_splatarray(jitstate_t* jit, ctx_t* ctx) https://github.com/ruby/ruby/blob/trunk/yjit_codegen.c#L917
     return YJIT_KEEP_COMPILING;
 }
 
+// new range initialized from top 2 values
+static codegen_status_t
+gen_newrange(jitstate_t* jit, ctx_t* ctx)
+{
+    rb_num_t flag = (rb_num_t)jit_get_arg(jit, 0);
+
+    // rb_range_new() allocates and can raise
+    jit_prepare_routine_call(jit, ctx, REG0);
+
+    // val = rb_range_new(low, high, (int)flag);
+    mov(cb, C_ARG_REGS[0], ctx_stack_opnd(ctx, 1));
+    mov(cb, C_ARG_REGS[1], ctx_stack_opnd(ctx, 0));
+    mov(cb, C_ARG_REGS[2], imm_opnd(flag));
+    call_ptr(cb, REG0, (void *)rb_range_new);
+
+    ctx_stack_pop(ctx, 2);
+    x86opnd_t stack_ret = ctx_stack_push(ctx, TYPE_HEAP);
+    mov(cb, stack_ret, RAX);
+
+    return YJIT_KEEP_COMPILING;
+}
+
 static void
 guard_object_is_heap(codeblock_t *cb, x86opnd_t object_opnd, ctx_t *ctx, uint8_t *side_exit)
 {
@@ -4014,6 +4036,7 @@ yjit_init_codegen(void) https://github.com/ruby/ruby/blob/trunk/yjit_codegen.c#L4036
     yjit_reg_op(BIN(splatarray), gen_splatarray);
     yjit_reg_op(BIN(expandarray), gen_expandarray);
     yjit_reg_op(BIN(newhash), gen_newhash);
+    yjit_reg_op(BIN(newrange), gen_newrange);
     yjit_reg_op(BIN(concatstrings), gen_concatstrings);
     yjit_reg_op(BIN(putnil), gen_putnil);
     yjit_reg_op(BIN(putobject), gen_putobject);
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]