From d2e8b99b5bc485b864d25c4293002214b0c64ced Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 4 Aug 2021 13:23:34 -0400 Subject: [PATCH] Implement tostring instruction for yjit Co-authored-by: Aaron Patterson --- test/ruby/test_yjit.rb | 4 ++++ yjit_codegen.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/test/ruby/test_yjit.rb b/test/ruby/test_yjit.rb index 145305ad09..e485e5f64f 100644 --- a/test/ruby/test_yjit.rb +++ b/test/ruby/test_yjit.rb @@ -59,6 +59,10 @@ class TestYJIT < Test::Unit::TestCase assert_compiles('$foo = 123; $foo', insns: %i[setglobal], result: 123) end + def test_compile_tostring + assert_no_exits('"i am a string #{true}"') + end + def test_getlocal_with_level assert_compiles(<<~RUBY, insns: %i[getlocal opt_plus], result: [[7]]) def foo(foo, bar) diff --git a/yjit_codegen.c b/yjit_codegen.c index 297d4f4291..ebb75cbdad 100644 --- a/yjit_codegen.c +++ b/yjit_codegen.c @@ -3494,6 +3494,35 @@ gen_setglobal(jitstate_t* jit, ctx_t* ctx) return YJIT_KEEP_COMPILING; } +static codegen_status_t +gen_tostring(jitstate_t* jit, ctx_t* ctx) +{ + // Save the PC and SP because we might make a Ruby call for + // Kernel#set_trace_var + jit_save_pc(jit, REG0); + jit_save_sp(jit, ctx); + + // Save YJIT registers + yjit_save_regs(cb); + + x86opnd_t str = ctx_stack_pop(ctx, 1); + x86opnd_t val = ctx_stack_pop(ctx, 1); + + mov(cb, C_ARG_REGS[0], str); + mov(cb, C_ARG_REGS[1], val); + + call_ptr(cb, REG0, (void *)&rb_obj_as_string_result); + + // Load YJIT registers + yjit_load_regs(cb); + + // Push the return value + x86opnd_t stack_ret = ctx_stack_push(ctx, TYPE_STRING); + mov(cb, stack_ret, RAX); + + return YJIT_KEEP_COMPILING; +} + static codegen_status_t gen_opt_getinlinecache(jitstate_t *jit, ctx_t *ctx) { @@ -3738,6 +3767,7 @@ yjit_init_codegen(void) yjit_reg_op(BIN(leave), gen_leave); yjit_reg_op(BIN(getglobal), gen_getglobal); yjit_reg_op(BIN(setglobal), gen_setglobal); + yjit_reg_op(BIN(tostring), gen_tostring); yjit_method_codegen_table = st_init_numtable();