1 /**********************************************************************
6 created at: 04/01/01 19:41:38 JST
8 Copyright (C) 2004-2007 Koichi Sasada
10 **********************************************************************/
12 #ifndef RUBY_VM_CORE_H
13 #define RUBY_VM_CORE_H
15 #define RUBY_VM_THREAD_MODEL 2
19 #include "ruby/ruby.h"
21 #include "ruby/signal.h"
23 #include "ruby/node.h"
30 #include "thread_win32.h"
31 #elif defined(HAVE_PTHREAD_H)
32 #include "thread_pthread.h"
34 #error "unsupported thread type"
43 # define NSIG (_SIGMAX + 1) /* For QNX */
47 #define RUBY_NSIG NSIG
54 #if defined(__GNUC__) && __GNUC__ >= 2
56 #if OPT_TOKEN_THREADED_CODE
57 #if OPT_DIRECT_THREADED_CODE
58 #undef OPT_DIRECT_THREADED_CODE
62 #else /* defined(__GNUC__) && __GNUC__ >= 2 */
64 /* disable threaded code options */
65 #if OPT_DIRECT_THREADED_CODE
66 #undef OPT_DIRECT_THREADED_CODE
68 #if OPT_TOKEN_THREADED_CODE
69 #undef OPT_TOKEN_THREADED_CODE
73 /* call threaded code */
74 #if OPT_CALL_THREADED_CODE
75 #if OPT_DIRECT_THREADED_CODE
76 #undef OPT_DIRECT_THREADED_CODE
77 #endif /* OPT_DIRECT_THREADED_CODE */
79 #undef OPT_STACK_CACHING
80 #endif /* OPT_STACK_CACHING */
81 #endif /* OPT_CALL_THREADED_CODE */
85 #define LIKELY(x) (__builtin_expect((x), 1))
86 #define UNLIKELY(x) (__builtin_expect((x), 0))
87 #else /* __GNUC__ >= 3 */
89 #define UNLIKELY(x) (x)
90 #endif /* __GNUC__ >= 3 */
92 typedef unsigned long rb_num_t
;
94 #define ISEQ_TYPE_TOP INT2FIX(1)
95 #define ISEQ_TYPE_METHOD INT2FIX(2)
96 #define ISEQ_TYPE_BLOCK INT2FIX(3)
97 #define ISEQ_TYPE_CLASS INT2FIX(4)
98 #define ISEQ_TYPE_RESCUE INT2FIX(5)
99 #define ISEQ_TYPE_ENSURE INT2FIX(6)
100 #define ISEQ_TYPE_EVAL INT2FIX(7)
101 #define ISEQ_TYPE_DEFINED_GUARD INT2FIX(8)
103 #define CATCH_TYPE_RESCUE INT2FIX(1)
104 #define CATCH_TYPE_ENSURE INT2FIX(2)
105 #define CATCH_TYPE_RETRY INT2FIX(3)
106 #define CATCH_TYPE_BREAK INT2FIX(4)
107 #define CATCH_TYPE_REDO INT2FIX(5)
108 #define CATCH_TYPE_NEXT INT2FIX(6)
110 struct iseq_insn_info_entry
{
111 unsigned short position
;
112 unsigned short line_no
;
116 struct iseq_catch_table_entry
{
125 #define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE (512)
127 struct iseq_compile_data_storage
{
128 struct iseq_compile_data_storage
*next
;
134 struct iseq_compile_data_ensure_node_stack
;
136 typedef struct rb_compile_option_struct
{
137 int inline_const_cache
;
138 int peephole_optimization
;
139 int tailcall_optimization
;
140 int specialized_instruction
;
141 int operands_unification
;
142 int instructions_unification
;
144 int trace_instruction
;
146 } rb_compile_option_t
;
148 struct iseq_compile_data
{
152 VALUE catch_table_ary
; /* Array */
154 /* GC is not needed */
155 struct iseq_label_data
*start_label
;
156 struct iseq_label_data
*end_label
;
157 struct iseq_label_data
*redo_label
;
159 VALUE loopval_popped
; /* used by NODE_BREAK */
162 struct iseq_compile_data_ensure_node_stack
*ensure_node_stack
;
164 struct iseq_compile_data_storage
*storage_head
;
165 struct iseq_compile_data_storage
*storage_current
;
170 const rb_compile_option_t
*option
;
174 #define GetCoreDataFromValue(obj, type, ptr) do { \
175 ptr = (type*)DATA_PTR(obj); \
178 #define GetCoreDataFromValue(obj, type, ptr) Data_Get_Struct(obj, type, ptr)
181 #define GetISeqPtr(obj, ptr) \
182 GetCoreDataFromValue(obj, rb_iseq_t, ptr)
184 typedef struct rb_iseq_profile_struct
{
187 VALUE time_cumu
; /* cumulative */
190 struct rb_iseq_struct
;
192 struct rb_iseq_struct
{
197 VALUE type
; /* instruction sequence type */
198 VALUE name
; /* String: iseq name */
199 VALUE filename
; /* file information where this sequence from */
200 VALUE
*iseq
; /* iseq (insn number and openrads) */
201 VALUE
*iseq_encoded
; /* encoded iseq */
202 unsigned long iseq_size
;
203 VALUE mark_ary
; /* Array: includes operands which should be GC marked */
204 VALUE coverage
; /* coverage array */
206 /* insn info, must be freed */
207 struct iseq_insn_info_entry
*insn_info_table
;
208 unsigned long insn_info_size
;
210 ID
*local_table
; /* must free */
211 int local_table_size
;
213 /* method, class frame: sizeof(vars) + 1, block frame: sizeof(vars) */
217 * argument information
219 * def m(a1, a2, ..., aM, # mandatory
220 * b1=(...), b2=(...), ..., bN=(...), # optinal
222 * d1, d2, ..., dO, # post
227 * arg_rest = M+N+1 // or -1 if no rest arg
229 * arg_opts_tbl = [ (N entries) ]
230 * arg_post_len = O // 0 if no post arguments
231 * arg_post_start = M+N+2
232 * arg_block = M+N + 1 + O + 1 // -1 if no block arg
233 * arg_simple = 0 if not simple arguments.
234 * = 1 if no opt, rest, post, block.
235 * = 2 if ambiguos block parameter ({|a|}).
236 * arg_size = argument size.
247 VALUE
*arg_opt_table
;
249 int stack_max
; /* for stack overflow check */
252 struct iseq_catch_table_entry
*catch_table
;
253 int catch_table_size
;
256 struct rb_iseq_struct
*parent_iseq
;
257 struct rb_iseq_struct
*local_iseq
;
264 VALUE orig
; /* non-NULL if its data have origin */
269 * void *special_block_builder;
270 * void *cached_special_block_builder;
271 * VALUE cached_special_block;
274 /* klass/module nest information stack (cref) */
279 ID defined_method_id
; /* for define_method */
280 rb_iseq_profile_t profile
;
282 /* used at compile time */
283 struct iseq_compile_data
*compile_data
;
286 enum ruby_special_exceptions
{
290 ruby_special_error_count
293 typedef struct rb_iseq_struct rb_iseq_t
;
295 #define GetVMPtr(obj, ptr) \
296 GetCoreDataFromValue(obj, rb_vm_t, ptr)
302 rb_thread_lock_t global_vm_lock
;
304 struct rb_thread_struct
*main_thread
;
305 struct rb_thread_struct
*running_thread
;
307 st_table
*living_threads
;
308 VALUE thgroup_default
;
311 int thread_abort_on_exception
;
312 unsigned long trace_flag
;
313 volatile int sleeper
;
315 /* object management */
316 VALUE mark_object_ary
;
318 VALUE special_exceptions
[ruby_special_error_count
];
323 VALUE loaded_features
;
324 struct st_table
*loading_table
;
327 rb_atomic_t signal_buff
[RUBY_NSIG
];
328 rb_atomic_t buffered_signal_size
;
331 rb_event_hook_t
*event_hooks
;
333 int src_encoding_index
;
335 VALUE verbose
, debug
, progname
;
338 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
339 struct rb_objspace
*objspace
;
344 VALUE
*pc
; /* cfp[0] */
345 VALUE
*sp
; /* cfp[1] */
346 VALUE
*bp
; /* cfp[2] */
347 rb_iseq_t
*iseq
; /* cfp[3] */
348 VALUE flag
; /* cfp[4] */
349 VALUE self
; /* cfp[5] / block[0] */
350 VALUE
*lfp
; /* cfp[6] / block[1] */
351 VALUE
*dfp
; /* cfp[7] / block[2] */
352 rb_iseq_t
*block_iseq
; /* cfp[8] / block[3] */
353 VALUE proc
; /* cfp[9] / block[4] */
354 ID method_id
; /* cfp[10] saved in special case */
355 VALUE method_class
; /* cfp[11] saved in special case */
356 VALUE prof_time_self
; /* cfp[12] */
357 VALUE prof_time_chld
; /* cfp[13] */
358 } rb_control_frame_t
;
360 typedef struct rb_block_struct
{
361 VALUE self
; /* share with method frame if it's only block */
362 VALUE
*lfp
; /* share with method frame if it's only block */
363 VALUE
*dfp
; /* share with method frame if it's only block */
368 #define GetThreadPtr(obj, ptr) \
369 GetCoreDataFromValue(obj, rb_thread_t, ptr)
371 enum rb_thread_status
{
375 THREAD_STOPPED_FOREVER
,
379 typedef RUBY_JMP_BUF rb_jmpbuf_t
;
385 struct rb_vm_tag
*prev
;
388 struct rb_vm_trap_tag
{
389 struct rb_vm_trap_tag
*prev
;
392 #define RUBY_VM_VALUE_CACHE_SIZE 0x1000
393 #define USE_VALUE_CACHE 0
395 struct rb_unblock_callback
{
396 rb_unblock_function_t
*func
;
400 struct rb_mutex_struct
;
402 struct rb_thread_struct
407 /* execution information */
408 VALUE
*stack
; /* must free, must mark */
409 unsigned long stack_size
;
410 rb_control_frame_t
*cfp
;
413 VALUE last_status
; /* $? */
419 rb_block_t
*passed_block
;
426 rb_block_t
*base_block
;
432 rb_thread_id_t thread_id
;
433 enum rb_thread_status status
;
437 native_thread_data_t native_thread_data
;
443 VALUE thrown_errinfo
;
447 rb_thread_lock_t interrupt_lock
;
448 struct rb_unblock_callback unblock
;
450 struct rb_mutex_struct
*keeping_mutexes
;
451 int transition_for_lock
;
453 struct rb_vm_tag
*tag
;
454 struct rb_vm_trap_tag
*trap_tag
;
457 int mild_compile_error
;
460 st_table
*local_storage
;
462 VALUE value_cache
[RUBY_VM_VALUE_CACHE_SIZE
+ 1];
463 VALUE
*value_cache_ptr
;
466 struct rb_thread_struct
*join_list_next
;
467 struct rb_thread_struct
*join_list_head
;
471 VALUE (*first_func
)(ANYARGS
);
474 VALUE
*machine_stack_start
;
475 VALUE
*machine_stack_end
;
476 size_t machine_stack_maxsize
;
478 VALUE
*machine_register_stack_start
;
479 VALUE
*machine_register_stack_end
;
480 size_t machine_register_stack_maxsize
;
482 jmp_buf machine_regs
;
485 /* statistics data for profiler */
486 VALUE stat_insn_usage
;
489 rb_event_hook_t
*event_hooks
;
490 rb_event_flag_t event_flags
;
496 rb_jmpbuf_t root_jmpbuf
;
499 int method_missing_reason
;
500 int abort_on_exception
;
504 VALUE
rb_iseq_new(NODE
*, VALUE
, VALUE
, VALUE
, VALUE
);
505 VALUE
rb_iseq_new_with_bopt(NODE
*, VALUE
, VALUE
, VALUE
, VALUE
, VALUE
);
506 VALUE
rb_iseq_new_with_opt(NODE
*, VALUE
, VALUE
, VALUE
, VALUE
, const rb_compile_option_t
*);
507 VALUE
rb_iseq_compile(VALUE src
, VALUE file
, VALUE line
);
508 VALUE
ruby_iseq_disasm(VALUE self
);
509 VALUE
ruby_iseq_disasm_insn(VALUE str
, VALUE
*iseqval
, int pos
, rb_iseq_t
*iseq
, VALUE child
);
510 const char *ruby_node_name(int node
);
511 VALUE
rb_iseq_clone(VALUE iseqval
, VALUE newcbase
);
513 RUBY_EXTERN VALUE rb_cISeq
;
514 RUBY_EXTERN VALUE rb_cRubyVM
;
515 RUBY_EXTERN VALUE rb_cEnv
;
516 RUBY_EXTERN VALUE rb_mRubyVMFrozenCore
;
518 /* each thread has this size stack : 128KB */
519 #define RUBY_VM_THREAD_STACK_SIZE (128 * 1024)
521 struct global_entry
{
522 struct global_variable
*var
;
526 #define GetProcPtr(obj, ptr) \
527 GetCoreDataFromValue(obj, rb_proc_t, ptr)
532 VALUE envval
; /* for GC mark */
539 #define GetEnvPtr(obj, ptr) \
540 GetCoreDataFromValue(obj, rb_env_t, ptr)
546 VALUE prev_envval
; /* for GC mark */
550 #define GetBindingPtr(obj, ptr) \
551 GetCoreDataFromValue(obj, rb_binding_t, ptr)
558 /* used by compile time and send insn */
559 #define VM_CALL_ARGS_SPLAT_BIT (0x01 << 1)
560 #define VM_CALL_ARGS_BLOCKARG_BIT (0x01 << 2)
561 #define VM_CALL_FCALL_BIT (0x01 << 3)
562 #define VM_CALL_VCALL_BIT (0x01 << 4)
563 #define VM_CALL_TAILCALL_BIT (0x01 << 5)
564 #define VM_CALL_TAILRECURSION_BIT (0x01 << 6)
565 #define VM_CALL_SUPER_BIT (0x01 << 7)
566 #define VM_CALL_SEND_BIT (0x01 << 8)
568 #define VM_SPECIAL_OBJECT_VMCORE 0x01
569 #define VM_SPECIAL_OBJECT_CBASE 0x02
571 #define VM_FRAME_MAGIC_METHOD 0x11
572 #define VM_FRAME_MAGIC_BLOCK 0x21
573 #define VM_FRAME_MAGIC_CLASS 0x31
574 #define VM_FRAME_MAGIC_TOP 0x41
575 #define VM_FRAME_MAGIC_FINISH 0x51
576 #define VM_FRAME_MAGIC_CFUNC 0x61
577 #define VM_FRAME_MAGIC_PROC 0x71
578 #define VM_FRAME_MAGIC_IFUNC 0x81
579 #define VM_FRAME_MAGIC_EVAL 0x91
580 #define VM_FRAME_MAGIC_LAMBDA 0xa1
581 #define VM_FRAME_MAGIC_MASK_BITS 8
582 #define VM_FRAME_MAGIC_MASK (~(~0<<VM_FRAME_MAGIC_MASK_BITS))
584 #define VM_FRAME_TYPE(cfp) ((cfp)->flag & VM_FRAME_MAGIC_MASK)
586 /* other frame flag */
587 #define VM_FRAME_FLAG_PASSED 0x0100
590 #define RUBYVM_CFUNC_FRAME_P(cfp) \
591 (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
594 /* inline (method|const) cache */
595 #define NEW_INLINE_CACHE_ENTRY() NEW_WHILE(Qundef, 0, 0)
596 #define ic_class u1.value
597 #define ic_method u2.node
598 #define ic_value u2.value
599 #define ic_vmstat u3.cnt
602 void rb_vm_change_state(void);
604 typedef VALUE CDHASH
;
606 #ifndef FUNC_FASTCALL
607 #define FUNC_FASTCALL(x) x
610 typedef rb_control_frame_t
*
611 (FUNC_FASTCALL(*rb_insn_func_t
))(rb_thread_t
*, rb_control_frame_t
*);
613 #define GC_GUARDED_PTR(p) ((VALUE)((VALUE)(p) | 0x01))
614 #define GC_GUARDED_PTR_REF(p) ((void *)(((VALUE)p) & ~0x03))
615 #define GC_GUARDED_PTR_P(p) (((VALUE)p) & 0x01)
617 #define RUBY_VM_METHOD_NODE NODE_METHOD
619 #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) (cfp+1)
620 #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) (cfp-1)
621 #define RUBY_VM_END_CONTROL_FRAME(th) \
622 ((rb_control_frame_t *)((th)->stack + (th)->stack_size))
623 #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
624 ((void *)(ecfp) > (void *)(cfp))
625 #define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp) \
626 (!RUBY_VM_VALID_CONTROL_FRAME_P((cfp), RUBY_VM_END_CONTROL_FRAME(th)))
628 #define RUBY_VM_IFUNC_P(ptr) (BUILTIN_TYPE(ptr) == T_NODE)
629 #define RUBY_VM_NORMAL_ISEQ_P(ptr) \
630 (ptr && !RUBY_VM_IFUNC_P(ptr))
632 #define RUBY_VM_CLASS_SPECIAL_P(ptr) (((VALUE)(ptr)) & 0x02)
634 #define RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp) ((rb_block_t *)(&(cfp)->self))
635 #define RUBY_VM_GET_CFP_FROM_BLOCK_PTR(b) \
636 ((rb_control_frame_t *)((VALUE *)(b) - 5))
639 #define DEFINED_IVAR INT2FIX(1)
640 #define DEFINED_IVAR2 INT2FIX(2)
641 #define DEFINED_GVAR INT2FIX(3)
642 #define DEFINED_CVAR INT2FIX(4)
643 #define DEFINED_CONST INT2FIX(5)
644 #define DEFINED_METHOD INT2FIX(6)
645 #define DEFINED_YIELD INT2FIX(7)
646 #define DEFINED_REF INT2FIX(8)
647 #define DEFINED_ZSUPER INT2FIX(9)
648 #define DEFINED_FUNC INT2FIX(10)
650 /* VM related object allocate functions */
651 /* TODO: should be static functions */
652 VALUE
rb_thread_alloc(VALUE klass
);
653 VALUE
rb_proc_alloc(VALUE klass
);
656 extern void vm_stack_dump_raw(rb_thread_t
*, rb_control_frame_t
*);
657 #define SDR() vm_stack_dump_raw(GET_THREAD(), GET_THREAD()->cfp)
658 #define SDR2(cfp) vm_stack_dump_raw(GET_THREAD(), (cfp))
659 void rb_vm_bugreport(void);
662 /* functions about thread/vm execution */
664 VALUE
rb_iseq_eval(VALUE iseqval
);
665 void rb_enable_interrupt(void);
666 void rb_disable_interrupt(void);
667 int rb_thread_method_id_and_class(rb_thread_t
*th
, ID
*idp
, VALUE
*klassp
);
669 VALUE
vm_invoke_proc(rb_thread_t
*th
, rb_proc_t
*proc
, VALUE self
,
670 int argc
, const VALUE
*argv
, rb_block_t
*blockptr
);
671 VALUE
vm_make_proc(rb_thread_t
*th
, rb_control_frame_t
*cfp
, const rb_block_t
*block
);
672 VALUE
vm_make_env_object(rb_thread_t
*th
, rb_control_frame_t
*cfp
);
674 NOINLINE(void rb_gc_save_machine_context(rb_thread_t
*));
676 #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
680 #if RUBY_VM_THREAD_MODEL == 2
681 RUBY_EXTERN rb_thread_t
*ruby_current_thread
;
682 extern rb_vm_t
*ruby_current_vm
;
684 #define GET_VM() ruby_current_vm
685 #define GET_THREAD() ruby_current_thread
686 #define rb_thread_set_current_raw(th) (ruby_current_thread = th)
687 #define rb_thread_set_current(th) do { \
688 rb_thread_set_current_raw(th); \
689 th->vm->running_thread = th; \
693 #error "unsupported thread model"
696 #define RUBY_VM_SET_INTERRUPT(th) ((th)->interrupt_flag |= 0x02)
697 #define RUBY_VM_SET_TIMER_INTERRUPT(th) ((th)->interrupt_flag |= 0x01)
698 #define RUBY_VM_SET_FINALIZER_INTERRUPT(th) ((th)->interrupt_flag |= 0x04)
699 #define RUBY_VM_INTERRUPTED(th) ((th)->interrupt_flag & 0x02)
701 void rb_thread_execute_interrupts(rb_thread_t
*);
703 #define RUBY_VM_CHECK_INTS_TH(th) do { \
704 if (th->interrupt_flag) { \
705 /* TODO: trap something event */ \
706 rb_thread_execute_interrupts(th); \
710 #define RUBY_VM_CHECK_INTS() \
711 RUBY_VM_CHECK_INTS_TH(GET_THREAD())
715 exec_event_hooks(rb_event_hook_t
*hook
, rb_event_flag_t flag
, VALUE self
, ID id
, VALUE klass
)
717 if (self
== rb_mRubyVMFrozenCore
) return;
719 if (flag
& hook
->flag
) {
720 (*hook
->func
)(flag
, hook
->data
, self
, id
, klass
);
726 #define EXEC_EVENT_HOOK(th, flag, self, id, klass) do { \
727 rb_event_flag_t wait_event__ = th->event_flags; \
728 if (UNLIKELY(wait_event__)) { \
729 if (wait_event__ & (flag | RUBY_EVENT_VM)) { \
730 VALUE self__ = (self), klass__ = (klass); \
732 if (wait_event__ & flag) { \
733 exec_event_hooks(th->event_hooks, flag, self__, id__, klass__); \
735 if (wait_event__ & RUBY_EVENT_VM) { \
736 exec_event_hooks(th->vm->event_hooks, flag, self__, id__, klass__); \
742 #endif /* RUBY_VM_CORE_H */