* process.c (rb_spawn_internal): new function to specify
[ruby-svn.git] / vm.h
blobe3c1df1971ddd642991e8da4e0c763c98bd3c2b6
1 /**********************************************************************
3 vm.h -
5 $Author$
6 created at: 04/01/01 16:56:59 JST
8 Copyright (C) 2004-2007 Koichi Sasada
10 **********************************************************************/
12 #ifndef RUBY_VM_H
13 #define RUBY_VM_H
15 typedef long OFFSET;
16 typedef unsigned long rb_num_t;
17 typedef unsigned long lindex_t;
18 typedef unsigned long dindex_t;
19 typedef rb_num_t GENTRY;
20 typedef rb_iseq_t *ISEQ;
22 extern VALUE rb_cEnv;
23 extern VALUE ruby_vm_global_state_version;
24 extern VALUE ruby_vm_redefined_flag;
27 /**
28 * VM Debug Level
30 * debug level:
31 * 0: no debug output
32 * 1: show instruction name
33 * 2: show stack frame when control stack frame is changed
34 * 3: show stack status
35 * 4: show register
36 * 5:
37 * 10: gc check
41 #ifndef VMDEBUG
42 #define VMDEBUG 0
43 #endif
45 #if 0
46 #undef VMDEBUG
47 #define VMDEBUG 3
48 #endif
50 #ifdef COLLECT_USAGE_ANALYSIS
51 #define USAGE_ANALYSIS_INSN(insn) vm_analysis_insn(insn)
52 #define USAGE_ANALYSIS_OPERAND(insn, n, op) vm_analysis_operand(insn, n, (VALUE)op)
53 #define USAGE_ANALYSIS_REGISTER(reg, s) vm_analysis_register(reg, s)
54 #else
55 #define USAGE_ANALYSIS_INSN(insn) /* none */
56 #define USAGE_ANALYSIS_OPERAND(insn, n, op) /* none */
57 #define USAGE_ANALYSIS_REGISTER(reg, s) /* none */
58 #endif
60 #ifdef __GCC__
61 /* TODO: machine dependent prefetch instruction */
62 #define PREFETCH(pc)
63 #else
64 #define PREFETCH(pc)
65 #endif
67 #if VMDEBUG > 0
68 #define debugs printf
69 #define DEBUG_ENTER_INSN(insn) \
70 debug_print_pre(th, GET_CFP());
72 #if OPT_STACK_CACHING
73 #define SC_REGS() , reg_a, reg_b
74 #else
75 #define SC_REGS()
76 #endif
78 #define DEBUG_END_INSN() \
79 debug_print_post(th, GET_CFP() SC_REGS());
81 #else
83 #define debugs
84 #define DEBUG_ENTER_INSN(insn)
85 #define DEBUG_END_INSN()
86 #endif
88 #define throwdebug if(0)printf
89 /* #define throwdebug printf */
91 #define SDR2(cfp) vm_stack_dump_raw(GET_THREAD(), (cfp))
94 /************************************************/
95 #if DISPATCH_XXX
96 error !
97 /************************************************/
98 #elif OPT_CALL_THREADED_CODE
100 #define LABEL(x) insn_func_##x
101 #define ELABEL(x)
102 #define LABEL_PTR(x) &LABEL(x)
104 #define INSN_ENTRY(insn) \
105 static rb_control_frame_t * \
106 FUNC_FASTCALL(LABEL(insn))(rb_thread_t *th, rb_control_frame_t *reg_cfp) {
108 #define END_INSN(insn) return reg_cfp;}
110 #define NEXT_INSN() return reg_cfp;
112 /************************************************/
113 #elif OPT_TOKEN_THREADED_CODE || OPT_DIRECT_THREADED_CODE
114 /* threaded code with gcc */
116 #define LABEL(x) INSN_LABEL_##x
117 #define ELABEL(x) INSN_ELABEL_##x
118 #define LABEL_PTR(x) &&LABEL(x)
120 #define INSN_ENTRY_SIG(insn)
123 #define INSN_DISPATCH_SIG(insn)
125 #define INSN_ENTRY(insn) \
126 LABEL(insn): \
127 INSN_ENTRY_SIG(insn); \
129 /* dispather */
130 #if __GNUC__ && (__i386__ || __x86_64__) && __GNUC__ == 3
131 #define DISPATCH_ARCH_DEPEND_WAY(addr) \
132 asm volatile("jmp *%0;\t# -- inseted by vm.h\t[length = 2]" : : "r" (addr))
134 #else
135 #define DISPATCH_ARCH_DEPEND_WAY(addr) \
136 /* do nothing */
138 #endif
141 /**********************************/
142 #if OPT_DIRECT_THREADED_CODE
144 /* for GCC 3.4.x */
145 #define TC_DISPATCH(insn) \
146 INSN_DISPATCH_SIG(insn); \
147 goto *GET_CURRENT_INSN(); \
150 #else
151 /* token threade code */
153 #define TC_DISPATCH(insn) \
154 DISPATCH_ARCH_DEPEND_WAY(insns_address_table[GET_CURRENT_INSN()]); \
155 INSN_DISPATCH_SIG(insn); \
156 goto *insns_address_table[GET_CURRENT_INSN()]; \
157 rb_bug("tc error");
160 #endif /* DISPATCH_DIRECT_THREADED_CODE */
162 #define END_INSN(insn) \
163 DEBUG_END_INSN(); \
164 TC_DISPATCH(insn); \
166 #define INSN_DISPATCH() \
167 TC_DISPATCH(__START__) \
170 #define END_INSNS_DISPATCH() \
171 rb_bug("unknown insn: %ld", GET_CURRENT_INSN()); \
172 } /* end of while loop */ \
174 #define NEXT_INSN() TC_DISPATCH(__NEXT_INSN__)
176 /************************************************/
177 #else /* no threaded code */
178 /* most common method */
180 #define INSN_ENTRY(insn) \
181 case BIN(insn):
183 #define END_INSN(insn) \
184 DEBUG_END_INSN(); \
185 break;
188 #define INSN_DISPATCH() \
189 while(1){ \
190 switch(GET_CURRENT_INSN()){
192 #define END_INSNS_DISPATCH() \
193 default: \
194 SDR(); \
195 rb_bug("unknown insn: %ld", GET_CURRENT_INSN()); \
196 } /* end of switch */ \
197 } /* end of while loop */ \
199 #define NEXT_INSN() goto first
201 #endif
204 /************************************************/
205 /************************************************/
207 #define VM_CFP_CNT(th, cfp) \
208 ((rb_control_frame_t *)(th->stack + th->stack_size) - (rb_control_frame_t *)(cfp))
209 #define VM_SP_CNT(th, sp) ((sp) - (th)->stack)
212 env{
213 env[0] // special (block or prev env)
214 env[1] // env object
215 env[2] // prev env val
219 #define ENV_IN_HEAP_P(th, env) \
220 (!((th)->stack < (env) && (env) < ((th)->stack + (th)->stack_size)))
221 #define ENV_VAL(env) ((env)[1])
223 #define FRAME_MAGIC_METHOD 0x11
224 #define FRAME_MAGIC_BLOCK 0x21
225 #define FRAME_MAGIC_CLASS 0x31
226 #define FRAME_MAGIC_TOP 0x41
227 #define FRAME_MAGIC_FINISH 0x51
228 #define FRAME_MAGIC_CFUNC 0x61
229 #define FRAME_MAGIC_PROC 0x71
230 #define FRAME_MAGIC_IFUNC 0x81
231 #define FRAME_MAGIC_EVAL 0x91
232 #define FRAME_MAGIC_LAMBDA 0xa1
233 #define FRAME_MAGIC_MASK_BITS 8
234 #define FRAME_MAGIC_MASK (~(~0<<FRAME_MAGIC_MASK_BITS))
236 #define VM_FRAME_FLAG(type) ((VALUE)((type) & FRAME_MAGIC_MASK))
238 #define VM_FRAME_TYPE(cfp) \
239 ((cfp)->flag & FRAME_MAGIC_MASK)
241 #define RUBYVM_CFUNC_FRAME_P(cfp) \
242 (VM_FRAME_TYPE(cfp) == FRAME_MAGIC_CFUNC)
244 #if OPT_CALL_THREADED_CODE
245 #define THROW_EXCEPTION(exc) do { \
246 th->errinfo = (VALUE)(exc); \
247 return 0; \
248 } while (0)
249 #else
250 #define THROW_EXCEPTION(exc) return (VALUE)(exc)
251 #endif
253 #define SCREG(r) (reg_##r)
255 /* VM state version */
257 #define GET_VM_STATE_VERSION() (ruby_vm_global_state_version)
258 #define INC_VM_STATE_VERSION() \
259 (ruby_vm_global_state_version = (ruby_vm_global_state_version+1) & 0x8fffffff)
261 #define BOP_PLUS 0x01
262 #define BOP_MINUS 0x02
263 #define BOP_MULT 0x04
264 #define BOP_DIV 0x08
265 #define BOP_MOD 0x10
266 #define BOP_EQ 0x20
267 #define BOP_LT 0x40
268 #define BOP_LE 0x80
269 #define BOP_LTLT 0x100
270 #define BOP_AREF 0x200
271 #define BOP_ASET 0x400
272 #define BOP_LENGTH 0x800
273 #define BOP_SUCC 0x1000
274 #define BOP_GT 0x2000
275 #define BOP_GE 0x4000
276 #define BOP_NOT 0x8000
277 #define BOP_NEQ 0x10000
279 #endif /* RUBY_VM_H */