Ruby  2.0.0p247(2013-06-27revision41674)
vm_insnhelper.h
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   insnhelper.h - helper macros to implement each instructions
00004 
00005   $Author: naruse $
00006   created at: 04/01/01 15:50:34 JST
00007 
00008   Copyright (C) 2004-2007 Koichi Sasada
00009 
00010 **********************************************************************/
00011 
00012 #ifndef RUBY_INSNHELPER_H
00013 #define RUBY_INSNHELPER_H
00014 
00028 #ifndef VMDEBUG
00029 #define VMDEBUG 0
00030 #endif
00031 
00032 #if 0
00033 #undef  VMDEBUG
00034 #define VMDEBUG 3
00035 #endif
00036 
00037 enum {
00038   BOP_PLUS,
00039   BOP_MINUS,
00040   BOP_MULT,
00041   BOP_DIV,
00042   BOP_MOD,
00043   BOP_EQ,
00044   BOP_EQQ,
00045   BOP_LT,
00046   BOP_LE,
00047   BOP_LTLT,
00048   BOP_AREF,
00049   BOP_ASET,
00050   BOP_LENGTH,
00051   BOP_SIZE,
00052   BOP_EMPTY_P,
00053   BOP_SUCC,
00054   BOP_GT,
00055   BOP_GE,
00056   BOP_NOT,
00057   BOP_NEQ,
00058 
00059   BOP_LAST_
00060 };
00061 
00062 extern char ruby_vm_redefined_flag[BOP_LAST_];
00063 extern VALUE ruby_vm_const_missing_count;
00064 
00065 #if VM_COLLECT_USAGE_DETAILS
00066 #define COLLECT_USAGE_INSN(insn)           vm_collect_usage_insn(insn)
00067 #define COLLECT_USAGE_OPERAND(insn, n, op) vm_collect_usage_operand((insn), (n), ((VALUE)(op)))
00068 
00069 #define COLLECT_USAGE_REGISTER(reg, s)     vm_collect_usage_register((reg), (s))
00070 #else
00071 #define COLLECT_USAGE_INSN(insn)                /* none */
00072 #define COLLECT_USAGE_OPERAND(insn, n, op)      /* none */
00073 #define COLLECT_USAGE_REGISTER(reg, s)          /* none */
00074 #endif
00075 
00076 /**********************************************************/
00077 /* deal with stack                                        */
00078 /**********************************************************/
00079 
00080 #define PUSH(x) (SET_SV(x), INC_SP(1))
00081 #define TOPN(n) (*(GET_SP()-(n)-1))
00082 #define POPN(n) (DEC_SP(n))
00083 #define POP()   (DEC_SP(1))
00084 #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n))
00085 
00086 #define GET_TOS()  (tos)        /* dummy */
00087 
00088 /**********************************************************/
00089 /* deal with registers                                    */
00090 /**********************************************************/
00091 
00092 #define REG_CFP (reg_cfp)
00093 #define REG_PC  (REG_CFP->pc)
00094 #define REG_SP  (REG_CFP->sp)
00095 #define REG_EP  (REG_CFP->ep)
00096 
00097 #define RESTORE_REGS() do { \
00098   REG_CFP = th->cfp; \
00099 } while (0)
00100 
00101 #define REG_A   reg_a
00102 #define REG_B   reg_b
00103 
00104 enum vm_regan_regtype {
00105     VM_REGAN_PC = 0,
00106     VM_REGAN_SP = 1,
00107     VM_REGAN_EP = 2,
00108     VM_REGAN_CFP = 3,
00109     VM_REGAN_SELF = 4,
00110     VM_REGAN_ISEQ = 5,
00111 };
00112 enum vm_regan_acttype {
00113     VM_REGAN_ACT_GET = 0,
00114     VM_REGAN_ACT_SET = 1,
00115 };
00116 
00117 #if VM_COLLECT_USAGE_DETAILS
00118 #define COLLECT_USAGE_REGISTER_HELPER(a, b, v) \
00119   (COLLECT_USAGE_REGISTER((VM_REGAN_##a), (VM_REGAN_ACT_##b)), (v))
00120 #else
00121 #define COLLECT_USAGE_REGISTER_HELPER(a, b, v) (v)
00122 #endif
00123 
00124 /* PC */
00125 #define GET_PC()           (COLLECT_USAGE_REGISTER_HELPER(PC, GET, REG_PC))
00126 #define SET_PC(x)          (REG_PC = (COLLECT_USAGE_REGISTER_HELPER(PC, SET, (x))))
00127 #define GET_CURRENT_INSN() (*GET_PC())
00128 #define GET_OPERAND(n)     (GET_PC()[(n)])
00129 #define ADD_PC(n)          (SET_PC(REG_PC + (n)))
00130 
00131 #define GET_PC_COUNT()     (REG_PC - GET_ISEQ()->iseq_encoded)
00132 #define JUMP(dst)          (REG_PC += (dst))
00133 
00134 /* frame pointer, environment pointer */
00135 #define GET_CFP()  (COLLECT_USAGE_REGISTER_HELPER(CFP, GET, REG_CFP))
00136 #define GET_EP()   (COLLECT_USAGE_REGISTER_HELPER(EP, GET, REG_EP))
00137 #define SET_EP(x)  (REG_EP = (COLLECT_USAGE_REGISTER_HELPER(EP, SET, (x))))
00138 #define GET_LEP()  (VM_EP_LEP(GET_EP()))
00139 
00140 /* SP */
00141 #define GET_SP()   (COLLECT_USAGE_REGISTER_HELPER(SP, GET, REG_SP))
00142 #define SET_SP(x)  (REG_SP  = (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
00143 #define INC_SP(x)  (REG_SP += (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
00144 #define DEC_SP(x)  (REG_SP -= (COLLECT_USAGE_REGISTER_HELPER(SP, SET, (x))))
00145 #define SET_SV(x)  (*GET_SP() = (x))
00146   /* set current stack value as x */
00147 
00148 #define GET_SP_COUNT() (REG_SP - th->stack)
00149 
00150 /* instruction sequence C struct */
00151 #define GET_ISEQ() (GET_CFP()->iseq)
00152 
00153 /**********************************************************/
00154 /* deal with variables                                    */
00155 /**********************************************************/
00156 
00157 #define GET_PREV_EP(ep)                ((VALUE *)((ep)[0] & ~0x03))
00158 
00159 #define GET_GLOBAL(entry)       rb_gvar_get((struct rb_global_entry*)(entry))
00160 #define SET_GLOBAL(entry, val)  rb_gvar_set((struct rb_global_entry*)(entry), (val))
00161 
00162 #define GET_CONST_INLINE_CACHE(dst) ((IC) * (GET_PC() + (dst) + 2))
00163 
00164 /**********************************************************/
00165 /* deal with values                                       */
00166 /**********************************************************/
00167 
00168 #define GET_SELF() (COLLECT_USAGE_REGISTER_HELPER(SELF, GET, GET_CFP()->self))
00169 
00170 /**********************************************************/
00171 /* deal with control flow 2: method/iterator              */
00172 /**********************************************************/
00173 
00174 #define COPY_CREF_OMOD(c1, c2) do {  \
00175   (c1)->nd_refinements = (c2)->nd_refinements; \
00176   if (!NIL_P((c2)->nd_refinements)) { \
00177       (c1)->flags |= NODE_FL_CREF_OMOD_SHARED; \
00178       (c2)->flags |= NODE_FL_CREF_OMOD_SHARED; \
00179   } \
00180 } while (0)
00181 
00182 #define COPY_CREF(c1, c2) do {  \
00183   NODE *__tmp_c2 = (c2); \
00184   COPY_CREF_OMOD(c1, __tmp_c2); \
00185   (c1)->nd_clss = __tmp_c2->nd_clss; \
00186   (c1)->nd_visi = __tmp_c2->nd_visi;\
00187   (c1)->nd_next = __tmp_c2->nd_next; \
00188   if (__tmp_c2->flags & NODE_FL_CREF_PUSHED_BY_EVAL) { \
00189       (c1)->flags |= NODE_FL_CREF_PUSHED_BY_EVAL; \
00190   } \
00191 } while (0)
00192 
00193 #define CALL_METHOD(ci) do { \
00194     VALUE v = (*(ci)->call)(th, GET_CFP(), (ci)); \
00195     if (v == Qundef) { \
00196         RESTORE_REGS(); \
00197         NEXT_INSN(); \
00198     } \
00199     else { \
00200         val = v; \
00201     } \
00202 } while (0)
00203 
00204 /* set fastpath when cached method is *NOT* protected
00205  * because inline method cache does not care about receiver.
00206  */
00207 
00208 #ifndef OPT_CALL_FASTPATH
00209 #define OPT_CALL_FASTPATH 1
00210 #endif
00211 
00212 #if OPT_CALL_FASTPATH
00213 #define CI_SET_FASTPATH(ci, func, enabled) do { \
00214     if (LIKELY(enabled)) ((ci)->call = (func)); \
00215 } while (0)
00216 #else
00217 #define CI_SET_FASTPATH(ci, func, enabled) /* do nothing */
00218 #endif
00219 
00220 #define GET_BLOCK_PTR() ((rb_block_t *)(GC_GUARDED_PTR_REF(GET_LEP()[0])))
00221 
00222 /**********************************************************/
00223 /* deal with control flow 3: exception                    */
00224 /**********************************************************/
00225 
00226 
00227 /**********************************************************/
00228 /* others                                                 */
00229 /**********************************************************/
00230 
00231 /* optimize insn */
00232 #define FIXNUM_REDEFINED_OP_FLAG (1 << 0)
00233 #define FLOAT_REDEFINED_OP_FLAG  (1 << 1)
00234 #define STRING_REDEFINED_OP_FLAG (1 << 2)
00235 #define ARRAY_REDEFINED_OP_FLAG  (1 << 3)
00236 #define HASH_REDEFINED_OP_FLAG   (1 << 4)
00237 #define BIGNUM_REDEFINED_OP_FLAG (1 << 5)
00238 #define SYMBOL_REDEFINED_OP_FLAG (1 << 6)
00239 #define TIME_REDEFINED_OP_FLAG   (1 << 7)
00240 
00241 #define BASIC_OP_UNREDEFINED_P(op, klass) (LIKELY((ruby_vm_redefined_flag[(op)]&(klass)) == 0))
00242 
00243 #define FIXNUM_2_P(a, b) ((a) & (b) & 1)
00244 #if USE_FLONUM
00245 #define FLONUM_2_P(a, b) (((((a)^2) | ((b)^2)) & 3) == 0) /* (FLONUM_P(a) && FLONUM_P(b)) */
00246 #else
00247 #define FLONUM_2_P(a, b) 0
00248 #endif
00249 #define HEAP_CLASS_OF(obj) (RBASIC(obj)->klass)
00250 
00251 #ifndef USE_IC_FOR_SPECIALIZED_METHOD
00252 #define USE_IC_FOR_SPECIALIZED_METHOD 1
00253 #endif
00254 
00255 #define CALL_SIMPLE_METHOD(recv) do { \
00256     ci->blockptr = 0; ci->argc = ci->orig_argc; \
00257     vm_search_method(ci, ci->recv = (recv)); \
00258     CALL_METHOD(ci); \
00259 } while (0)
00260 
00261 static VALUE ruby_vm_global_state_version = 1;
00262 
00263 #define GET_VM_STATE_VERSION() (ruby_vm_global_state_version)
00264 #define INC_VM_STATE_VERSION() do { \
00265     ruby_vm_global_state_version = (ruby_vm_global_state_version + 1); \
00266     if (ruby_vm_global_state_version == 0) vm_clear_all_cache(); \
00267 } while (0)
00268 static void vm_clear_all_cache(void);
00269 
00270 static VALUE make_no_method_exception(VALUE exc, const char *format,
00271                                       VALUE obj, int argc, const VALUE *argv);
00272 
00273 
00274 #endif /* RUBY_INSNHELPER_H */
00275