Ruby 1.9.3p327(2012-11-10revision37606)
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_SUCC,
00053   BOP_GT,
00054   BOP_GE,
00055   BOP_NOT,
00056   BOP_NEQ,
00057 
00058   BOP_LAST_
00059 };
00060 
00061 extern char ruby_vm_redefined_flag[BOP_LAST_];
00062 extern VALUE ruby_vm_const_missing_count;
00063 
00064 
00065 /**********************************************************/
00066 /* deal with stack                                        */
00067 /**********************************************************/
00068 
00069 #define PUSH(x) (SET_SV(x), INC_SP(1))
00070 #define TOPN(n) (*(GET_SP()-(n)-1))
00071 #define POPN(n) (DEC_SP(n))
00072 #define POP()   (DEC_SP(1))
00073 #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n))
00074 
00075 #define GET_TOS()  (tos)        /* dummy */
00076 
00077 /**********************************************************/
00078 /* deal with registers                                    */
00079 /**********************************************************/
00080 
00081 #define REG_CFP (reg_cfp)
00082 #define REG_PC  (REG_CFP->pc)
00083 #define REG_SP  (REG_CFP->sp)
00084 #define REG_LFP (REG_CFP->lfp)
00085 #define REG_DFP (REG_CFP->dfp)
00086 
00087 #define RESTORE_REGS() do { \
00088   REG_CFP = th->cfp; \
00089 } while (0)
00090 
00091 #define REG_A   reg_a
00092 #define REG_B   reg_b
00093 
00094 #ifdef COLLECT_USAGE_ANALYSIS
00095 #define USAGE_ANALYSIS_REGISTER_HELPER(a, b, v) \
00096   (USAGE_ANALYSIS_REGISTER((a), (b)), (v))
00097 #else
00098 #define USAGE_ANALYSIS_REGISTER_HELPER(a, b, v) (v)
00099 #endif
00100 
00101 /* PC */
00102 #define GET_PC()           (USAGE_ANALYSIS_REGISTER_HELPER(0, 0, REG_PC))
00103 #define SET_PC(x)          (REG_PC = (USAGE_ANALYSIS_REGISTER_HELPER(0, 1, (x))))
00104 #define GET_CURRENT_INSN() (*GET_PC())
00105 #define GET_OPERAND(n)     (GET_PC()[(n)])
00106 #define ADD_PC(n)          (SET_PC(REG_PC + (n)))
00107 
00108 #define GET_PC_COUNT()     (REG_PC - GET_ISEQ()->iseq_encoded)
00109 #define JUMP(dst)          (REG_PC += (dst))
00110 
00111 /* FP */
00112 #define GET_CFP()  (USAGE_ANALYSIS_REGISTER_HELPER(2, 0, REG_CFP))
00113 #define GET_LFP()  (USAGE_ANALYSIS_REGISTER_HELPER(3, 0, REG_LFP))
00114 #define SET_LFP(x) (REG_LFP = (USAGE_ANALYSIS_REGISTER_HELPER(3, 1, (x))))
00115 #define GET_DFP()  (USAGE_ANALYSIS_REGISTER_HELPER(4, 0, REG_DFP))
00116 #define SET_DFP(x) (REG_DFP = (USAGE_ANALYSIS_REGISTER_HELPER(4, 1, (x))))
00117 
00118 /* SP */
00119 #define GET_SP()   (USAGE_ANALYSIS_REGISTER_HELPER(1, 0, REG_SP))
00120 #define SET_SP(x)  (REG_SP  = (USAGE_ANALYSIS_REGISTER_HELPER(1, 1, (x))))
00121 #define INC_SP(x)  (REG_SP += (USAGE_ANALYSIS_REGISTER_HELPER(1, 1, (x))))
00122 #define DEC_SP(x)  (REG_SP -= (USAGE_ANALYSIS_REGISTER_HELPER(1, 1, (x))))
00123 #define SET_SV(x)  (*GET_SP() = (x))
00124   /* set current stack value as x */
00125 
00126 #define GET_SP_COUNT() (REG_SP - th->stack)
00127 
00128 /* instruction sequence C struct */
00129 #define GET_ISEQ() (GET_CFP()->iseq)
00130 
00131 /**********************************************************/
00132 /* deal with variables                                    */
00133 /**********************************************************/
00134 
00135 #define GET_PREV_DFP(dfp)                ((VALUE *)((dfp)[0] & ~0x03))
00136 
00137 #define GET_GLOBAL(entry)       rb_gvar_get((struct rb_global_entry*)(entry))
00138 #define SET_GLOBAL(entry, val)  rb_gvar_set((struct rb_global_entry*)(entry), (val))
00139 
00140 #define GET_CONST_INLINE_CACHE(dst) ((IC) * (GET_PC() + (dst) + 2))
00141 
00142 /**********************************************************/
00143 /* deal with values                                       */
00144 /**********************************************************/
00145 
00146 #define GET_SELF() (USAGE_ANALYSIS_REGISTER_HELPER(5, 0, GET_CFP()->self))
00147 
00148 /**********************************************************/
00149 /* deal with control flow 2: method/iterator              */
00150 /**********************************************************/
00151 
00152 #define COPY_CREF(c1, c2) do {  \
00153   NODE *__tmp_c2 = (c2); \
00154   (c1)->nd_clss = __tmp_c2->nd_clss; \
00155   (c1)->nd_visi = __tmp_c2->nd_visi;\
00156   (c1)->nd_next = __tmp_c2->nd_next; \
00157   if (__tmp_c2->flags & NODE_FL_CREF_PUSHED_BY_EVAL) { \
00158       (c1)->flags |= NODE_FL_CREF_PUSHED_BY_EVAL; \
00159   } \
00160 } while (0)
00161 
00162 #define CALL_METHOD(num, blockptr, flag, id, me, recv) do { \
00163     VALUE v = vm_call_method(th, GET_CFP(), (num), (blockptr), (flag), (id), (me), (recv)); \
00164     if (v == Qundef) { \
00165         RESTORE_REGS(); \
00166         NEXT_INSN(); \
00167     } \
00168     else { \
00169         val = v; \
00170     } \
00171 } while (0)
00172 
00173 #define GET_BLOCK_PTR() \
00174   ((rb_block_t *)(GC_GUARDED_PTR_REF(GET_LFP()[0] & \
00175                                      ((GET_LFP()[0] & 0x02) - 0x02))))
00176 
00177 /**********************************************************/
00178 /* deal with control flow 3: exception                    */
00179 /**********************************************************/
00180 
00181 
00182 /**********************************************************/
00183 /* others                                                 */
00184 /**********************************************************/
00185 
00186 /* optimize insn */
00187 #define FIXNUM_2_P(a, b) ((a) & (b) & 1)
00188 #define BASIC_OP_UNREDEFINED_P(op) (LIKELY(ruby_vm_redefined_flag[(op)] == 0))
00189 #define HEAP_CLASS_OF(obj) RBASIC(obj)->klass
00190 
00191 #ifndef USE_IC_FOR_SPECIALIZED_METHOD
00192 #define USE_IC_FOR_SPECIALIZED_METHOD 1
00193 #endif
00194 
00195 #if USE_IC_FOR_SPECIALIZED_METHOD
00196 
00197 #define CALL_SIMPLE_METHOD(num, id, recv) do { \
00198     VALUE klass = CLASS_OF(recv); \
00199     CALL_METHOD((num), 0, 0, (id), vm_method_search((id), klass, ic), (recv)); \
00200 } while (0)
00201 
00202 #else
00203 
00204 #define CALL_SIMPLE_METHOD(num, id, recv) do { \
00205     VALUE klass = CLASS_OF(recv); \
00206     CALL_METHOD((num), 0, 0, (id), rb_method_entry(klass, (id)), (recv)); \
00207 } while (0)
00208 
00209 #endif
00210 
00211 static VALUE ruby_vm_global_state_version = 1;
00212 
00213 #define GET_VM_STATE_VERSION() (ruby_vm_global_state_version)
00214 #define INC_VM_STATE_VERSION() do { \
00215     ruby_vm_global_state_version = (ruby_vm_global_state_version + 1); \
00216     if (ruby_vm_global_state_version == 0) vm_clear_all_cache(); \
00217 } while (0)
00218 static void vm_clear_all_cache(void);
00219 
00220 #endif /* RUBY_INSNHELPER_H */
00221