summaryrefslogtreecommitdiff
path: root/tool/ruby_vm/views/_mjit_compile_ivar.erb
blob: 85850b4446aec8440041b8ea10c9078fcf5f9c55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
% # -*- C -*-
% # Copyright (c) 2018 Takashi Kokubun.  All rights reserved.
% #
% # This file is a part of  the programming language Ruby.  Permission is hereby
% # granted, to either  redistribute and/or modify this file,  provided that the
% # conditions mentioned  in the  file COPYING  are met.   Consult the  file for
% # details.
%
% # Optimized case of get_instancevariable instruction.
#if OPT_IC_FOR_IVAR
{
% # compiler: Prepare operands which may be used by `insn.call_attribute`
% insn.opes.each_with_index do |ope, i|
    MAYBE_UNUSED(<%= ope.fetch(:decl) %>) = (<%= ope.fetch(:type) %>)operands[<%= i %>];
% end
% # compiler: Use copied IVC to avoid race condition
    IVC ic_copy = &(status->is_entries + ((union iseq_inline_storage_entry *)ic - body->is_entries))->iv_cache;
%
    if (!status->compile_info->disable_ivar_cache && ic_copy->ic_serial) {
% # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`.
% # <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%>
%
% # JIT: prepare vm_getivar/vm_setivar arguments and variables
        fprintf(f, "{\n");
        fprintf(f, "    VALUE obj = GET_SELF();\n");
        fprintf(f, "    const rb_serial_t ic_serial = (rb_serial_t)%"PRI_SERIALT_PREFIX"u;\n", ic_copy->ic_serial);
        fprintf(f, "    const st_index_t index = %"PRIuSIZE";\n", ic_copy->index);
% # JIT: cache hit path of vm_getivar/vm_setivar, or cancel JIT (recompile it with exivar)
% if insn.name == 'setinstancevariable'
        fprintf(f, "    VALUE val = stack[%d];\n", b->stack_size - 1);
        fprintf(f, "    if (LIKELY(RB_TYPE_P(obj, T_OBJECT) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && index < ROBJECT_NUMIV(obj) && !RB_OBJ_FROZEN(obj))) {\n");
        fprintf(f, "        VALUE *ptr = ROBJECT_IVPTR(obj);\n");
        fprintf(f, "        RB_OBJ_WRITE(obj, &ptr[index], val);\n");
        fprintf(f, "    }\n");
% else
        fprintf(f, "    VALUE val;\n");
        fprintf(f, "    if (LIKELY(RB_TYPE_P(obj, T_OBJECT) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && index < ROBJECT_NUMIV(obj) && (val = ROBJECT_IVPTR(obj)[index]) != Qundef)) {\n");
        fprintf(f, "        stack[%d] = val;\n", b->stack_size);
        fprintf(f, "    }\n");
% end
        fprintf(f, "    else {\n");
        fprintf(f, "        reg_cfp->pc = original_body_iseq + %d;\n", pos);
        fprintf(f, "        reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size);
        fprintf(f, "        goto ivar_cancel;\n");
        fprintf(f, "    }\n");

% # compiler: Move JIT compiler's internal stack pointer
        b->stack_size += <%= insn.call_attribute('sp_inc') %>;
        fprintf(f, "}\n");
        break;
    }
% if insn.name == 'getinstancevariable'
    else if (!status->compile_info->disable_exivar_cache && ic_copy->ic_serial) {
% # JIT: optimize away motion of sp and pc. This path does not call rb_warning() and so it's always leaf and not `handles_sp`.
% # <%= render 'mjit_compile_pc_and_sp', locals: { insn: insn } -%>
%
% # JIT: prepare vm_getivar's arguments and variables
        fprintf(f, "{\n");
        fprintf(f, "    VALUE obj = GET_SELF();\n");
        fprintf(f, "    const rb_serial_t ic_serial = (rb_serial_t)%"PRI_SERIALT_PREFIX"u;\n", ic_copy->ic_serial);
        fprintf(f, "    const st_index_t index = %"PRIuSIZE";\n", ic_copy->index);
% # JIT: cache hit path of vm_getivar, or cancel JIT (recompile it without any ivar optimization)
        fprintf(f, "    struct gen_ivtbl *ivtbl;\n");
        fprintf(f, "    VALUE val;\n");
        fprintf(f, "    if (LIKELY(FL_TEST_RAW(obj, FL_EXIVAR) && ic_serial == RCLASS_SERIAL(RBASIC(obj)->klass) && st_lookup(rb_ivar_generic_ivtbl(), (st_data_t)obj, (st_data_t *)&ivtbl) && index < ivtbl->numiv && (val = ivtbl->ivptr[index]) != Qundef)) {\n");
        fprintf(f, "        stack[%d] = val;\n", b->stack_size);
        fprintf(f, "    }\n");
        fprintf(f, "    else {\n");
        fprintf(f, "        reg_cfp->pc = original_body_iseq + %d;\n", pos);
        fprintf(f, "        reg_cfp->sp = vm_base_ptr(reg_cfp) + %d;\n", b->stack_size);
        fprintf(f, "        goto exivar_cancel;\n");
        fprintf(f, "    }\n");

% # compiler: Move JIT compiler's internal stack pointer
        b->stack_size += <%= insn.call_attribute('sp_inc') %>;
        fprintf(f, "}\n");
        break;
    }
% end
}
#endif // OPT_IC_FOR_IVAR