vm_insnhelper.h

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

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7