ruby-changes:46155
From: ko1 <ko1@a...>
Date: Fri, 7 Apr 2017 15:41:37 +0900 (JST)
Subject: [ruby-changes:46155] ko1:r58268 (trunk): introduce imemo_type_p(v, imemo_type)
ko1 2017-04-07 15:41:32 +0900 (Fri, 07 Apr 2017) New Revision: 58268 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58268 Log: introduce imemo_type_p(v, imemo_type) * internal.h: introduce imemo_type_p() which checks the given value is T_IMEMO and imemo_type() == given imemo_type. Modified files: trunk/compile.c trunk/internal.h trunk/load.c trunk/vm.c trunk/vm_core.h trunk/vm_dump.c Index: vm_core.h =================================================================== --- vm_core.h (revision 58267) +++ vm_core.h (revision 58268) @@ -45,8 +45,7 @@ https://github.com/ruby/ruby/blob/trunk/vm_core.h#L45 #include "ruby_assert.h" #if VM_CHECK_MODE > 0 -#define VM_ASSERT(expr) ( \ - RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr)) +#define VM_ASSERT(expr) RUBY_ASSERT_MESG_WHEN(VM_CHECK_MODE > 0, expr, #expr) #define VM_UNREACHABLE(func) rb_bug(#func ": unreachable") @@ -1044,7 +1043,7 @@ VM_FRAME_BMETHOD_P(const rb_control_fram https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1043 static inline int rb_obj_is_iseq(VALUE iseq) { - return RB_TYPE_P(iseq, T_IMEMO) && imemo_type(iseq) == imemo_iseq; + return imemo_type_p(iseq, imemo_iseq); } #if VM_CHECK_MODE > 0 @@ -1106,8 +1105,7 @@ VM_ENV_ESCAPED_P(const VALUE *ep) https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1105 static inline int vm_assert_env(VALUE obj) { - VM_ASSERT(RB_TYPE_P(obj, T_IMEMO)); - VM_ASSERT(imemo_type(obj) == imemo_env); + VM_ASSERT(imemo_type_p(obj, imemo_env)); return 1; } #endif @@ -1186,8 +1184,7 @@ VM_BH_ISEQ_BLOCK_P(VALUE block_handler) https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1184 if ((block_handler & 0x03) == 0x01) { #if VM_CHECK_MODE > 0 struct rb_captured_block *captured = VM_TAGGED_PTR_REF(block_handler, 0x03); - VM_ASSERT(RB_TYPE_P(captured->code.val, T_IMEMO)); - VM_ASSERT(imemo_type(captured->code.val) == imemo_iseq); + VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq)); #endif return 1; } @@ -1218,8 +1215,7 @@ VM_BH_IFUNC_P(VALUE block_handler) https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1215 if ((block_handler & 0x03) == 0x03) { #if VM_CHECK_MODE > 0 struct rb_captured_block *captured = (void *)(block_handler & ~0x03); - VM_ASSERT(RB_TYPE_P(captured->code.val, T_IMEMO)); - VM_ASSERT(imemo_type(captured->code.val) == imemo_ifunc); + VM_ASSERT(imemo_type_p(captured->code.val, imemo_ifunc)); #endif return 1; } @@ -1284,12 +1280,10 @@ vm_block_type(const struct rb_block *blo https://github.com/ruby/ruby/blob/trunk/vm_core.h#L1280 #if VM_CHECK_MODE > 0 switch (block->type) { case block_type_iseq: - VM_ASSERT(RB_TYPE_P(block->as.captured.code.val, T_IMEMO)); - VM_ASSERT(imemo_type(block->as.captured.code.val) == imemo_iseq); + VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_iseq)); break; case block_type_ifunc: - VM_ASSERT(RB_TYPE_P(block->as.captured.code.val, T_IMEMO)); - VM_ASSERT(imemo_type(block->as.captured.code.val) == imemo_ifunc); + VM_ASSERT(imemo_type_p(block->as.captured.code.val, imemo_ifunc)); break; case block_type_symbol: VM_ASSERT(SYMBOL_P(block->as.symbol)); Index: internal.h =================================================================== --- internal.h (revision 58267) +++ internal.h (revision 58268) @@ -855,6 +855,21 @@ imemo_type(VALUE imemo) https://github.com/ruby/ruby/blob/trunk/internal.h#L855 return (RBASIC(imemo)->flags >> FL_USHIFT) & imemo_mask; } +static inline int +imemo_type_p(VALUE imemo, enum imemo_type imemo_type) +{ + if (LIKELY(!RB_SPECIAL_CONST_P(imemo))) { + /* fixed at compile time if imemo_type is given. */ + const VALUE mask = (imemo_mask << FL_USHIFT) | RUBY_T_MASK; + const VALUE expected_type = (imemo_type << FL_USHIFT) | T_IMEMO; + /* fixed at runtime. */ + return expected_type == (RBASIC(imemo)->flags & mask); + } + else { + return 0; + } +} + /* FL_USER0 to FL_USER2 is for type */ #define IMEMO_FL_USHIFT (FL_USHIFT + 3) #define IMEMO_FL_USER0 FL_USER3 Index: compile.c =================================================================== --- compile.c (revision 58267) +++ compile.c (revision 58268) @@ -615,7 +615,7 @@ rb_iseq_compile_node(rb_iseq_t *iseq, NO https://github.com/ruby/ruby/blob/trunk/compile.c#L615 } } } - else if (RB_TYPE_P((VALUE)node, T_IMEMO)) { + else if (imemo_type_p((VALUE)node, imemo_ifunc)) { const struct vm_ifunc *ifunc = (struct vm_ifunc *)node; /* user callback */ (*ifunc->func)(iseq, ret, ifunc->data); Index: load.c =================================================================== --- load.c (revision 58267) +++ load.c (revision 58268) @@ -732,7 +732,7 @@ load_lock(const char *ftptr) https://github.com/ruby/ruby/blob/trunk/load.c#L732 st_insert(loading_tbl, (st_data_t)ftptr, data); return (char *)ftptr; } - else if (RB_TYPE_P((VALUE)data, T_IMEMO) && imemo_type((VALUE)data) == imemo_memo) { + else if (imemo_type_p(data, imemo_memo)) { struct MEMO *memo = MEMO_CAST(data); void (*init)(void) = (void (*)(void))memo->u3.func; data = (st_data_t)rb_thread_shield_new(); Index: vm.c =================================================================== --- vm.c (revision 58267) +++ vm.c (revision 58268) @@ -864,7 +864,8 @@ rb_vm_make_proc_lambda(rb_thread_t *th, https://github.com/ruby/ruby/blob/trunk/vm.c#L864 vm_make_env_object(th, cfp); } VM_ASSERT(VM_EP_IN_HEAP_P(th, captured->ep)); - VM_ASSERT(RB_TYPE_P(captured->code.val, T_IMEMO)); + VM_ASSERT(imemo_type_p(captured->code.val, imemo_iseq) || + imemo_type_p(captured->code.val, imemo_ifunc)); procval = rb_proc_create_from_captured(klass, captured, imemo_type(captured->code.val) == imemo_iseq ? block_type_iseq : block_type_ifunc, Index: vm_dump.c =================================================================== --- vm_dump.c (revision 58267) +++ vm_dump.c (revision 58268) @@ -91,7 +91,7 @@ control_frame_dump(rb_thread_t *th, rb_c https://github.com/ruby/ruby/blob/trunk/vm_dump.c#L91 } if (cfp->iseq != 0) { -#define RUBY_VM_IFUNC_P(ptr) (RB_TYPE_P((VALUE)(ptr), T_IMEMO) && imemo_type((VALUE)ptr) == imemo_ifunc) +#define RUBY_VM_IFUNC_P(ptr) imemo_type_p((VALUE)ptr, imemo_ifunc) if (RUBY_VM_IFUNC_P(cfp->iseq)) { iseq_name = "<ifunc>"; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/