* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / insnhelper.h
bloba862f2672e523a83909dcce22b073aa0b0a2b886
1 /**********************************************************************
3 insnhelper.h - helper macros to implement each instructions
5 $Author$
6 created at: 04/01/01 15:50:34 JST
8 Copyright (C) 2004-2007 Koichi Sasada
10 **********************************************************************/
12 #ifndef RUBY_INSNHELPER_H
13 #define RUBY_INSNHELPER_H
15 #include "ruby/ruby.h"
16 #include "ruby/node.h"
17 #include "eval_intern.h"
18 #include "vm_core.h"
19 #include "vm.h"
21 /**********************************************************/
22 /* deal with stack */
23 /**********************************************************/
25 #define PUSH(x) (SET_SV(x), INC_SP(1))
26 #define TOPN(n) (*(GET_SP()-(n)-1))
27 #define POPN(n) (DEC_SP(n))
28 #define POP() (DEC_SP(1))
29 #define STACK_ADDR_FROM_TOP(n) (GET_SP()-(n))
31 #define GET_TOS() (tos) /* dummy */
33 /**********************************************************/
34 /* deal with registers */
35 /**********************************************************/
37 #define REG_CFP (reg_cfp)
38 #define REG_PC (REG_CFP->pc)
39 #define REG_SP (REG_CFP->sp)
40 #define REG_LFP (REG_CFP->lfp)
41 #define REG_DFP (REG_CFP->dfp)
43 #define RESTORE_REGS() do { \
44 REG_CFP = th->cfp; \
45 } while (0)
47 #define REG_A reg_a
48 #define REG_B reg_b
50 #ifdef COLLECT_USAGE_ANALYSIS
51 #define USAGE_ANALYSIS_REGISTER_HELPER(a, b, v) \
52 (USAGE_ANALYSIS_REGISTER(a, b), (v))
53 #else
54 #define USAGE_ANALYSIS_REGISTER_HELPER(a, b, v) (v)
55 #endif
57 /* PC */
58 #define GET_PC() (USAGE_ANALYSIS_REGISTER_HELPER(0, 0, REG_PC))
59 #define SET_PC(x) (REG_PC = (USAGE_ANALYSIS_REGISTER_HELPER(0, 1, x)))
60 #define GET_CURRENT_INSN() (*GET_PC())
61 #define GET_OPERAND(n) (GET_PC()[(n)])
62 #define ADD_PC(n) (SET_PC(REG_PC + (n)))
64 #define GET_PC_COUNT() (REG_PC - GET_ISEQ()->iseq_encoded)
65 #define JUMP(dst) (REG_PC += (dst))
67 /* FP */
68 #define GET_CFP() (USAGE_ANALYSIS_REGISTER_HELPER(2, 0, REG_CFP))
69 #define GET_LFP() (USAGE_ANALYSIS_REGISTER_HELPER(3, 0, REG_LFP))
70 #define SET_LFP(x) (REG_LFP = (USAGE_ANALYSIS_REGISTER_HELPER(3, 1, (x))))
71 #define GET_DFP() (USAGE_ANALYSIS_REGISTER_HELPER(4, 0, REG_DFP))
72 #define SET_DFP(x) (REG_DFP = (USAGE_ANALYSIS_REGISTER_HELPER(4, 1, (x))))
74 /* SP */
75 #define GET_SP() (USAGE_ANALYSIS_REGISTER_HELPER(1, 0, REG_SP))
76 #define SET_SP(x) (REG_SP = (USAGE_ANALYSIS_REGISTER_HELPER(1, 1, (x))))
77 #define INC_SP(x) (REG_SP += (USAGE_ANALYSIS_REGISTER_HELPER(1, 1, (x))))
78 #define DEC_SP(x) (REG_SP -= (USAGE_ANALYSIS_REGISTER_HELPER(1, 1, (x))))
79 #define SET_SV(x) (*GET_SP() = (x))
80 /* set current stack value as x */
82 #define GET_SP_COUNT() (REG_SP - th->stack)
84 /* instruction sequence C struct */
85 #define GET_ISEQ() (GET_CFP()->iseq)
87 /**********************************************************/
88 /* deal with variables */
89 /**********************************************************/
91 #define GET_PREV_DFP(dfp) ((VALUE *)((dfp)[0] & ~0x03))
93 #define GET_GLOBAL(entry) rb_gvar_get((struct global_entry*)entry)
94 #define SET_GLOBAL(entry, val) rb_gvar_set((struct global_entry*)entry, val)
96 #define GET_CONST_INLINE_CACHE(dst) ((IC) * (GET_PC() + (dst) + 1))
98 /**********************************************************/
99 /* deal with values */
100 /**********************************************************/
102 #define GET_SELF() (USAGE_ANALYSIS_REGISTER_HELPER(5, 0, GET_CFP()->self))
104 /**********************************************************/
105 /* deal with control flow 2: method/iterator */
106 /**********************************************************/
108 #define COPY_CREF(c1, c2) do { \
109 NODE *__tmp_c2 = (c2); \
110 c1->nd_clss = __tmp_c2->nd_clss; \
111 c1->nd_visi = __tmp_c2->nd_visi; \
112 c1->nd_next = __tmp_c2->nd_next; \
113 } while (0)
115 #define CALL_METHOD(num, blockptr, flag, id, mn, recv, klass) do { \
116 VALUE v = vm_call_method(th, GET_CFP(), num, blockptr, flag, id, mn, recv, klass); \
117 if (v == Qundef) { \
118 RESTORE_REGS(); \
119 NEXT_INSN(); \
121 else { \
122 val = v; \
124 } while (0)
126 #define GET_BLOCK_PTR() \
127 ((rb_block_t *)(GC_GUARDED_PTR_REF(GET_LFP()[0])))
129 /**********************************************************/
130 /* deal with control flow 3: exception */
131 /**********************************************************/
134 /**********************************************************/
135 /* others */
136 /**********************************************************/
138 /* optimize insn */
139 #define FIXNUM_2_P(a, b) ((a) & (b) & 1)
140 #define BASIC_OP_UNREDEFINED_P(op) ((ruby_vm_redefined_flag & (op)) == 0)
141 #define HEAP_CLASS_OF(obj) RBASIC(obj)->klass
143 #define CALL_SIMPLE_METHOD(num, id, recv) do { \
144 VALUE klass = CLASS_OF(recv); \
145 CALL_METHOD(num, 0, 0, id, rb_method_node(klass, id), recv, CLASS_OF(recv)); \
146 } while (0)
148 #endif /* RUBY_INSNHELPER_H */