Ruby  1.9.3p448(2013-06-27revision41675)
vm_core.h
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   vm_core.h -
00004 
00005   $Author: usa $
00006   created at: 04/01/01 19:41:38 JST
00007 
00008   Copyright (C) 2004-2007 Koichi Sasada
00009 
00010 **********************************************************************/
00011 
00012 #ifndef RUBY_VM_CORE_H
00013 #define RUBY_VM_CORE_H
00014 
00015 #define RUBY_VM_THREAD_MODEL 2
00016 
00017 #include "ruby/ruby.h"
00018 #include "ruby/st.h"
00019 
00020 #include "node.h"
00021 #include "debug.h"
00022 #include "vm_opts.h"
00023 #include "id.h"
00024 #include "method.h"
00025 #include "ruby_atomic.h"
00026 
00027 #if   defined(_WIN32)
00028 #include "thread_win32.h"
00029 #elif defined(HAVE_PTHREAD_H)
00030 #include "thread_pthread.h"
00031 #else
00032 #error "unsupported thread type"
00033 #endif
00034 
00035 #ifndef ENABLE_VM_OBJSPACE
00036 #ifdef _WIN32
00037 /*
00038  * TODO: object space independent st_table.
00039  * socklist needs st_table in rb_w32_sysinit(), before object space
00040  * initialization.
00041  * It is too early now to change st_hash_type, since it breaks binary
00042  * compatibility.
00043  */
00044 #define ENABLE_VM_OBJSPACE 0
00045 #else
00046 #define ENABLE_VM_OBJSPACE 1
00047 #endif
00048 #endif
00049 
00050 #include <setjmp.h>
00051 #include <signal.h>
00052 
00053 #ifndef NSIG
00054 # define NSIG (_SIGMAX + 1)      /* For QNX */
00055 #endif
00056 
00057 #define RUBY_NSIG NSIG
00058 
00059 #ifdef HAVE_STDARG_PROTOTYPES
00060 #include <stdarg.h>
00061 #define va_init_list(a,b) va_start((a),(b))
00062 #else
00063 #include <varargs.h>
00064 #define va_init_list(a,b) va_start((a))
00065 #endif
00066 
00067 #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
00068 #define USE_SIGALTSTACK
00069 #endif
00070 
00071 /*****************/
00072 /* configuration */
00073 /*****************/
00074 
00075 /* gcc ver. check */
00076 #if defined(__GNUC__) && __GNUC__ >= 2
00077 
00078 #if OPT_TOKEN_THREADED_CODE
00079 #if OPT_DIRECT_THREADED_CODE
00080 #undef OPT_DIRECT_THREADED_CODE
00081 #endif
00082 #endif
00083 
00084 #else /* defined(__GNUC__) && __GNUC__ >= 2 */
00085 
00086 /* disable threaded code options */
00087 #if OPT_DIRECT_THREADED_CODE
00088 #undef OPT_DIRECT_THREADED_CODE
00089 #endif
00090 #if OPT_TOKEN_THREADED_CODE
00091 #undef OPT_TOKEN_THREADED_CODE
00092 #endif
00093 #endif
00094 
00095 /* call threaded code */
00096 #if    OPT_CALL_THREADED_CODE
00097 #if    OPT_DIRECT_THREADED_CODE
00098 #undef OPT_DIRECT_THREADED_CODE
00099 #endif /* OPT_DIRECT_THREADED_CODE */
00100 #if    OPT_STACK_CACHING
00101 #undef OPT_STACK_CACHING
00102 #endif /* OPT_STACK_CACHING */
00103 #endif /* OPT_CALL_THREADED_CODE */
00104 
00105 /* likely */
00106 #if __GNUC__ >= 3
00107 #define LIKELY(x)   (__builtin_expect((x), 1))
00108 #define UNLIKELY(x) (__builtin_expect((x), 0))
00109 #else /* __GNUC__ >= 3 */
00110 #define LIKELY(x)   (x)
00111 #define UNLIKELY(x) (x)
00112 #endif /* __GNUC__ >= 3 */
00113 
00114 #if __GNUC__ >= 3
00115 #define UNINITIALIZED_VAR(x) x = x
00116 #else
00117 #define UNINITIALIZED_VAR(x) x
00118 #endif
00119 
00120 typedef unsigned long rb_num_t;
00121 
00122 /* iseq data type */
00123 
00124 struct iseq_compile_data_ensure_node_stack;
00125 
00126 typedef struct rb_compile_option_struct rb_compile_option_t;
00127 
00128 struct iseq_inline_cache_entry {
00129     VALUE ic_vmstat;
00130     VALUE ic_class;
00131     union {
00132         VALUE value;
00133         rb_method_entry_t *method;
00134         long index;
00135     } ic_value;
00136 };
00137 
00138 #if 1
00139 #define GetCoreDataFromValue(obj, type, ptr) do { \
00140     (ptr) = (type*)DATA_PTR(obj); \
00141 } while (0)
00142 #else
00143 #define GetCoreDataFromValue(obj, type, ptr) Data_Get_Struct((obj), type, (ptr))
00144 #endif
00145 
00146 #define GetISeqPtr(obj, ptr) \
00147   GetCoreDataFromValue((obj), rb_iseq_t, (ptr))
00148 
00149 struct rb_iseq_struct;
00150 
00151 struct rb_iseq_struct {
00152     /***************/
00153     /* static data */
00154     /***************/
00155 
00156     enum iseq_type {
00157         ISEQ_TYPE_TOP,
00158         ISEQ_TYPE_METHOD,
00159         ISEQ_TYPE_BLOCK,
00160         ISEQ_TYPE_CLASS,
00161         ISEQ_TYPE_RESCUE,
00162         ISEQ_TYPE_ENSURE,
00163         ISEQ_TYPE_EVAL,
00164         ISEQ_TYPE_MAIN,
00165         ISEQ_TYPE_DEFINED_GUARD
00166     } type;              /* instruction sequence type */
00167 
00168     VALUE name;          /* String: iseq name */
00169     VALUE filename;      /* file information where this sequence from */
00170     VALUE filepath;      /* real file path or nil */
00171     VALUE *iseq;         /* iseq (insn number and operands) */
00172     VALUE *iseq_encoded; /* encoded iseq */
00173     unsigned long iseq_size;
00174     VALUE mark_ary;     /* Array: includes operands which should be GC marked */
00175     VALUE coverage;     /* coverage array */
00176     unsigned short line_no;
00177 
00178     /* insn info, must be freed */
00179     struct iseq_insn_info_entry *insn_info_table;
00180     size_t insn_info_size;
00181 
00182     ID *local_table;            /* must free */
00183     int local_table_size;
00184 
00185     /* method, class frame: sizeof(vars) + 1, block frame: sizeof(vars) */
00186     int local_size;
00187 
00188     struct iseq_inline_cache_entry *ic_entries;
00189     int ic_size;
00190 
00214     int argc;
00215     int arg_simple;
00216     int arg_rest;
00217     int arg_block;
00218     int arg_opts;
00219     int arg_post_len;
00220     int arg_post_start;
00221     int arg_size;
00222     VALUE *arg_opt_table;
00223 
00224     size_t stack_max; /* for stack overflow check */
00225 
00226     /* catch table */
00227     struct iseq_catch_table_entry *catch_table;
00228     int catch_table_size;
00229 
00230     /* for child iseq */
00231     struct rb_iseq_struct *parent_iseq;
00232     struct rb_iseq_struct *local_iseq;
00233 
00234     /****************/
00235     /* dynamic data */
00236     /****************/
00237 
00238     VALUE self;
00239     VALUE orig;                 /* non-NULL if its data have origin */
00240 
00241     /* block inlining */
00242     /*
00243      * NODE *node;
00244      * void *special_block_builder;
00245      * void *cached_special_block_builder;
00246      * VALUE cached_special_block;
00247      */
00248 
00249     /* klass/module nest information stack (cref) */
00250     NODE *cref_stack;
00251     VALUE klass;
00252 
00253     /* misc */
00254     ID defined_method_id;       /* for define_method */
00255     rb_num_t flip_cnt;
00256 
00257     /* used at compile time */
00258     struct iseq_compile_data *compile_data;
00259 };
00260 
00261 enum ruby_special_exceptions {
00262     ruby_error_reenter,
00263     ruby_error_nomemory,
00264     ruby_error_sysstack,
00265     ruby_error_closed_stream,
00266     ruby_special_error_count
00267 };
00268 
00269 #define GetVMPtr(obj, ptr) \
00270   GetCoreDataFromValue((obj), rb_vm_t, (ptr))
00271 
00272 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
00273 struct rb_objspace;
00274 void rb_objspace_free(struct rb_objspace *);
00275 #endif
00276 
00277 typedef struct rb_vm_struct {
00278     VALUE self;
00279 
00280     rb_global_vm_lock_t gvl;
00281 
00282     struct rb_thread_struct *main_thread;
00283     struct rb_thread_struct *running_thread;
00284 
00285     st_table *living_threads;
00286     VALUE thgroup_default;
00287 
00288     int running;
00289     int inhibit_thread_creation;
00290     int thread_abort_on_exception;
00291     unsigned long trace_flag;
00292     volatile int sleeper;
00293 
00294     /* object management */
00295     VALUE mark_object_ary;
00296 
00297     VALUE special_exceptions[ruby_special_error_count];
00298 
00299     /* load */
00300     VALUE top_self;
00301     VALUE load_path;
00302     VALUE loaded_features;
00303     struct st_table *loading_table;
00304 
00305     /* signal */
00306     struct {
00307         VALUE cmd;
00308         int safe;
00309     } trap_list[RUBY_NSIG];
00310 
00311     /* hook */
00312     rb_event_hook_t *event_hooks;
00313 
00314     int src_encoding_index;
00315 
00316     VALUE verbose, debug, progname;
00317     VALUE coverages;
00318 
00319     struct unlinked_method_entry_list_entry *unlinked_method_entry_list;
00320 
00321 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
00322     struct rb_objspace *objspace;
00323 #endif
00324 
00325     /*
00326      * @shyouhei notes that this is not for storing normal Ruby
00327      * objects so do *NOT* mark this when you GC.
00328      */
00329     struct RArray at_exit;
00330 } rb_vm_t;
00331 
00332 typedef struct {
00333     VALUE *pc;                  /* cfp[0] */
00334     VALUE *sp;                  /* cfp[1] */
00335     VALUE *bp;                  /* cfp[2] */
00336     rb_iseq_t *iseq;            /* cfp[3] */
00337     VALUE flag;                 /* cfp[4] */
00338     VALUE self;                 /* cfp[5] / block[0] */
00339     VALUE *lfp;                 /* cfp[6] / block[1] */
00340     VALUE *dfp;                 /* cfp[7] / block[2] */
00341     rb_iseq_t *block_iseq;      /* cfp[8] / block[3] */
00342     VALUE proc;                 /* cfp[9] / block[4] */
00343     const rb_method_entry_t *me;/* cfp[10] */
00344 } rb_control_frame_t;
00345 
00346 typedef struct rb_block_struct {
00347     VALUE self;                 /* share with method frame if it's only block */
00348     VALUE *lfp;                 /* share with method frame if it's only block */
00349     VALUE *dfp;                 /* share with method frame if it's only block */
00350     rb_iseq_t *iseq;
00351     VALUE proc;
00352 } rb_block_t;
00353 
00354 extern const rb_data_type_t ruby_threadptr_data_type;
00355 
00356 #define GetThreadPtr(obj, ptr) \
00357     TypedData_Get_Struct((obj), rb_thread_t, &ruby_threadptr_data_type, (ptr))
00358 
00359 enum rb_thread_status {
00360     THREAD_TO_KILL,
00361     THREAD_RUNNABLE,
00362     THREAD_STOPPED,
00363     THREAD_STOPPED_FOREVER,
00364     THREAD_KILLED
00365 };
00366 
00367 typedef RUBY_JMP_BUF rb_jmpbuf_t;
00368 
00369 struct rb_vm_tag {
00370     rb_jmpbuf_t buf;
00371     VALUE tag;
00372     VALUE retval;
00373     struct rb_vm_tag *prev;
00374 };
00375 
00376 struct rb_vm_protect_tag {
00377     struct rb_vm_protect_tag *prev;
00378 };
00379 
00380 struct rb_unblock_callback {
00381     rb_unblock_function_t *func;
00382     void *arg;
00383 };
00384 
00385 struct rb_mutex_struct;
00386 
00387 #ifdef SIGSTKSZ
00388 #define ALT_STACK_SIZE (SIGSTKSZ*2)
00389 #else
00390 #define ALT_STACK_SIZE (4*1024)
00391 #endif
00392 
00393 typedef struct rb_thread_struct {
00394     VALUE self;
00395     rb_vm_t *vm;
00396 
00397     /* execution information */
00398     VALUE *stack;               /* must free, must mark */
00399     unsigned long stack_size;
00400     rb_control_frame_t *cfp;
00401     int safe_level;
00402     int raised_flag;
00403     VALUE last_status; /* $? */
00404 
00405     /* passing state */
00406     int state;
00407 
00408     int waiting_fd;
00409 
00410     /* for rb_iterate */
00411     const rb_block_t *passed_block;
00412 
00413     /* for bmethod */
00414     const rb_method_entry_t *passed_me;
00415 
00416     /* for load(true) */
00417     VALUE top_self;
00418     VALUE top_wrapper;
00419 
00420     /* eval env */
00421     rb_block_t *base_block;
00422 
00423     VALUE *local_lfp;
00424     VALUE local_svar;
00425 
00426     /* thread control */
00427     rb_thread_id_t thread_id;
00428     enum rb_thread_status status;
00429     int priority;
00430 
00431     native_thread_data_t native_thread_data;
00432     void *blocking_region_buffer;
00433 
00434     VALUE thgroup;
00435     VALUE value;
00436 
00437     VALUE errinfo;
00438     VALUE thrown_errinfo;
00439 
00440     rb_atomic_t interrupt_flag;
00441     rb_thread_lock_t interrupt_lock;
00442     struct rb_unblock_callback unblock;
00443     VALUE locking_mutex;
00444     struct rb_mutex_struct *keeping_mutexes;
00445 
00446     struct rb_vm_tag *tag;
00447     struct rb_vm_protect_tag *protect_tag;
00448 
00449     int parse_in_eval;
00450     int mild_compile_error;
00451 
00452     /* storage */
00453     st_table *local_storage;
00454 
00455     struct rb_thread_struct *join_list_next;
00456     struct rb_thread_struct *join_list_head;
00457 
00458     VALUE first_proc;
00459     VALUE first_args;
00460     VALUE (*first_func)(ANYARGS);
00461 
00462     /* for GC */
00463     VALUE *machine_stack_start;
00464     VALUE *machine_stack_end;
00465     size_t machine_stack_maxsize;
00466 #ifdef __ia64
00467     VALUE *machine_register_stack_start;
00468     VALUE *machine_register_stack_end;
00469     size_t machine_register_stack_maxsize;
00470 #endif
00471     jmp_buf machine_regs;
00472     int mark_stack_len;
00473 
00474     /* statistics data for profiler */
00475     VALUE stat_insn_usage;
00476 
00477     /* tracer */
00478     rb_event_hook_t *event_hooks;
00479     rb_event_flag_t event_flags;
00480     int tracing;
00481 
00482     /* fiber */
00483     VALUE fiber;
00484     VALUE root_fiber;
00485     rb_jmpbuf_t root_jmpbuf;
00486 
00487     /* misc */
00488     int method_missing_reason;
00489     int abort_on_exception;
00490 #ifdef USE_SIGALTSTACK
00491     void *altstack;
00492 #endif
00493     unsigned long running_time_us;
00494 } rb_thread_t;
00495 
00496 /* iseq.c */
00497 #if defined __GNUC__ && __GNUC__ >= 4
00498 #pragma GCC visibility push(default)
00499 #endif
00500 VALUE rb_iseq_new(NODE*, VALUE, VALUE, VALUE, VALUE, enum iseq_type);
00501 VALUE rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE filepath, VALUE parent);
00502 VALUE rb_iseq_new_main(NODE *node, VALUE filename, VALUE filepath);
00503 VALUE rb_iseq_new_with_bopt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, enum iseq_type, VALUE);
00504 VALUE rb_iseq_new_with_opt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, enum iseq_type, const rb_compile_option_t*);
00505 VALUE rb_iseq_compile(VALUE src, VALUE file, VALUE line);
00506 VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE filepath, VALUE line, VALUE opt);
00507 VALUE rb_iseq_disasm(VALUE self);
00508 int rb_iseq_disasm_insn(VALUE str, VALUE *iseqval, size_t pos, rb_iseq_t *iseq, VALUE child);
00509 const char *ruby_node_name(int node);
00510 int rb_iseq_first_lineno(rb_iseq_t *iseq);
00511 
00512 RUBY_EXTERN VALUE rb_cISeq;
00513 RUBY_EXTERN VALUE rb_cRubyVM;
00514 RUBY_EXTERN VALUE rb_cEnv;
00515 RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
00516 #if defined __GNUC__ && __GNUC__ >= 4
00517 #pragma GCC visibility pop
00518 #endif
00519 
00520 /* each thread has this size stack : 128KB */
00521 #define RUBY_VM_THREAD_STACK_SIZE (128 * 1024)
00522 
00523 #define GetProcPtr(obj, ptr) \
00524   GetCoreDataFromValue((obj), rb_proc_t, (ptr))
00525 
00526 typedef struct {
00527     rb_block_t block;
00528 
00529     VALUE envval;               /* for GC mark */
00530     VALUE blockprocval;
00531     int safe_level;
00532     int is_from_method;
00533     int is_lambda;
00534 } rb_proc_t;
00535 
00536 #define GetEnvPtr(obj, ptr) \
00537   GetCoreDataFromValue((obj), rb_env_t, (ptr))
00538 
00539 typedef struct {
00540     VALUE *env;
00541     int env_size;
00542     int local_size;
00543     VALUE prev_envval;          /* for GC mark */
00544     rb_block_t block;
00545 } rb_env_t;
00546 
00547 #define GetBindingPtr(obj, ptr) \
00548   GetCoreDataFromValue((obj), rb_binding_t, (ptr))
00549 
00550 typedef struct {
00551     VALUE env;
00552     VALUE filename;
00553     unsigned short line_no;
00554 } rb_binding_t;
00555 
00556 /* used by compile time and send insn */
00557 #define VM_CALL_ARGS_SPLAT_BIT     (0x01 << 1)
00558 #define VM_CALL_ARGS_BLOCKARG_BIT  (0x01 << 2)
00559 #define VM_CALL_FCALL_BIT          (0x01 << 3)
00560 #define VM_CALL_VCALL_BIT          (0x01 << 4)
00561 #define VM_CALL_TAILCALL_BIT       (0x01 << 5)
00562 #define VM_CALL_TAILRECURSION_BIT  (0x01 << 6)
00563 #define VM_CALL_SUPER_BIT          (0x01 << 7)
00564 #define VM_CALL_OPT_SEND_BIT       (0x01 << 8)
00565 
00566 enum vm_special_object_type {
00567     VM_SPECIAL_OBJECT_VMCORE = 1,
00568     VM_SPECIAL_OBJECT_CBASE,
00569     VM_SPECIAL_OBJECT_CONST_BASE
00570 };
00571 
00572 #define VM_FRAME_MAGIC_METHOD 0x11
00573 #define VM_FRAME_MAGIC_BLOCK  0x21
00574 #define VM_FRAME_MAGIC_CLASS  0x31
00575 #define VM_FRAME_MAGIC_TOP    0x41
00576 #define VM_FRAME_MAGIC_FINISH 0x51
00577 #define VM_FRAME_MAGIC_CFUNC  0x61
00578 #define VM_FRAME_MAGIC_PROC   0x71
00579 #define VM_FRAME_MAGIC_IFUNC  0x81
00580 #define VM_FRAME_MAGIC_EVAL   0x91
00581 #define VM_FRAME_MAGIC_LAMBDA 0xa1
00582 #define VM_FRAME_MAGIC_MASK_BITS   8
00583 #define VM_FRAME_MAGIC_MASK   (~(~0<<VM_FRAME_MAGIC_MASK_BITS))
00584 
00585 #define VM_FRAME_TYPE(cfp) ((cfp)->flag & VM_FRAME_MAGIC_MASK)
00586 
00587 /* other frame flag */
00588 #define VM_FRAME_FLAG_PASSED 0x0100
00589 
00590 #define RUBYVM_CFUNC_FRAME_P(cfp) \
00591   (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
00592 
00593 /* inline cache */
00594 typedef struct iseq_inline_cache_entry *IC;
00595 
00596 void rb_vm_change_state(void);
00597 
00598 typedef VALUE CDHASH;
00599 
00600 #ifndef FUNC_FASTCALL
00601 #define FUNC_FASTCALL(x) x
00602 #endif
00603 
00604 typedef rb_control_frame_t *
00605   (FUNC_FASTCALL(*rb_insn_func_t))(rb_thread_t *, rb_control_frame_t *);
00606 
00607 #define GC_GUARDED_PTR(p)     ((VALUE)((VALUE)(p) | 0x01))
00608 #define GC_GUARDED_PTR_REF(p) ((void *)(((VALUE)(p)) & ~0x03))
00609 #define GC_GUARDED_PTR_P(p)   (((VALUE)(p)) & 0x01)
00610 
00611 #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
00612 #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
00613 #define RUBY_VM_END_CONTROL_FRAME(th) \
00614   ((rb_control_frame_t *)((th)->stack + (th)->stack_size))
00615 #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
00616   ((void *)(ecfp) > (void *)(cfp))
00617 #define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp) \
00618   (!RUBY_VM_VALID_CONTROL_FRAME_P((cfp), RUBY_VM_END_CONTROL_FRAME(th)))
00619 
00620 #define RUBY_VM_IFUNC_P(ptr)        (BUILTIN_TYPE(ptr) == T_NODE)
00621 #define RUBY_VM_NORMAL_ISEQ_P(ptr) \
00622   ((ptr) && !RUBY_VM_IFUNC_P(ptr))
00623 
00624 #define RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp) ((rb_block_t *)(&(cfp)->self))
00625 #define RUBY_VM_GET_CFP_FROM_BLOCK_PTR(b) \
00626   ((rb_control_frame_t *)((VALUE *)(b) - 5))
00627 
00628 /* VM related object allocate functions */
00629 VALUE rb_thread_alloc(VALUE klass);
00630 VALUE rb_proc_alloc(VALUE klass);
00631 
00632 /* for debug */
00633 extern void rb_vmdebug_stack_dump_raw(rb_thread_t *, rb_control_frame_t *);
00634 #define SDR() rb_vmdebug_stack_dump_raw(GET_THREAD(), GET_THREAD()->cfp)
00635 #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_THREAD(), (cfp))
00636 void rb_vm_bugreport(void);
00637 
00638 /* functions about thread/vm execution */
00639 #if defined __GNUC__ && __GNUC__ >= 4
00640 #pragma GCC visibility push(default)
00641 #endif
00642 VALUE rb_iseq_eval(VALUE iseqval);
00643 VALUE rb_iseq_eval_main(VALUE iseqval);
00644 void rb_enable_interrupt(void);
00645 void rb_disable_interrupt(void);
00646 #if defined __GNUC__ && __GNUC__ >= 4
00647 #pragma GCC visibility pop
00648 #endif
00649 int rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, VALUE *klassp);
00650 
00651 VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc, VALUE self,
00652                         int argc, const VALUE *argv, const rb_block_t *blockptr);
00653 VALUE rb_vm_make_proc(rb_thread_t *th, const rb_block_t *block, VALUE klass);
00654 VALUE rb_vm_make_env_object(rb_thread_t *th, rb_control_frame_t *cfp);
00655 void rb_vm_rewrite_dfp_in_errinfo(rb_thread_t *th);
00656 void rb_vm_inc_const_missing_count(void);
00657 void rb_vm_gvl_destroy(rb_vm_t *vm);
00658 VALUE rb_vm_call(rb_thread_t *th, VALUE recv, VALUE id, int argc,
00659                  const VALUE *argv, const rb_method_entry_t *me);
00660 void rb_unlink_method_entry(rb_method_entry_t *me);
00661 void rb_gc_mark_unlinked_live_method_entries(void *pvm);
00662 
00663 void rb_thread_start_timer_thread(void);
00664 void rb_thread_stop_timer_thread(int);
00665 void rb_thread_reset_timer_thread(void);
00666 void rb_thread_wakeup_timer_thread(void);
00667 
00668 int ruby_thread_has_gvl_p(void);
00669 VALUE rb_make_backtrace(void);
00670 typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
00671 int rb_backtrace_each(rb_backtrace_iter_func *iter, void *arg);
00672 rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(rb_thread_t *th, rb_control_frame_t *cfp);
00673 int rb_vm_get_sourceline(const rb_control_frame_t *);
00674 VALUE rb_name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method);
00675 void rb_vm_stack_to_heap(rb_thread_t *th);
00676 void ruby_thread_init_stack(rb_thread_t *th);
00677 
00678 NOINLINE(void rb_gc_save_machine_context(rb_thread_t *));
00679 void rb_gc_mark_machine_stack(rb_thread_t *th);
00680 
00681 #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
00682 
00683 /* for thread */
00684 
00685 #if RUBY_VM_THREAD_MODEL == 2
00686 RUBY_EXTERN rb_thread_t *ruby_current_thread;
00687 extern rb_vm_t *ruby_current_vm;
00688 
00689 #define GET_VM() ruby_current_vm
00690 #define GET_THREAD() ruby_current_thread
00691 #define rb_thread_set_current_raw(th) (void)(ruby_current_thread = (th))
00692 #define rb_thread_set_current(th) do { \
00693     if ((th)->vm->running_thread != (th)) { \
00694         (th)->vm->running_thread->running_time_us = 0; \
00695     } \
00696     rb_thread_set_current_raw(th); \
00697     (th)->vm->running_thread = (th); \
00698 } while (0)
00699 
00700 #else
00701 #error "unsupported thread model"
00702 #endif
00703 
00704 #define RUBY_VM_SET_TIMER_INTERRUPT(th)         ATOMIC_OR((th)->interrupt_flag, 0x01)
00705 #define RUBY_VM_SET_INTERRUPT(th)               ATOMIC_OR((th)->interrupt_flag, 0x02)
00706 #define RUBY_VM_SET_FINALIZER_INTERRUPT(th)     ATOMIC_OR((th)->interrupt_flag, 0x04)
00707 #define RUBY_VM_INTERRUPTED(th) ((th)->interrupt_flag & 0x02)
00708 
00709 int rb_signal_buff_size(void);
00710 void rb_signal_exec(rb_thread_t *th, int sig);
00711 void rb_threadptr_check_signal(rb_thread_t *mth);
00712 void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
00713 void rb_threadptr_signal_exit(rb_thread_t *th);
00714 void rb_threadptr_execute_interrupts(rb_thread_t *);
00715 void rb_threadptr_interrupt(rb_thread_t *th);
00716 void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
00717 
00718 void rb_thread_lock_unlock(rb_thread_lock_t *);
00719 void rb_thread_lock_destroy(rb_thread_lock_t *);
00720 
00721 #define RUBY_VM_CHECK_INTS_TH(th) do { \
00722     if (UNLIKELY((th)->interrupt_flag)) { \
00723         rb_threadptr_execute_interrupts(th); \
00724     } \
00725 } while (0)
00726 
00727 #define RUBY_VM_CHECK_INTS() \
00728   RUBY_VM_CHECK_INTS_TH(GET_THREAD())
00729 
00730 /* tracer */
00731 void
00732 rb_threadptr_exec_event_hooks(rb_thread_t *th, rb_event_flag_t flag, VALUE self, ID id, VALUE klass, int pop_p);
00733 
00734 #define EXEC_EVENT_HOOK_ORIG(th, flag, self, id, klass, pop_p) do { \
00735     rb_event_flag_t wait_event__ = (th)->event_flags; \
00736     if (UNLIKELY(wait_event__)) { \
00737         if (wait_event__ & ((flag) | RUBY_EVENT_VM)) { \
00738             rb_threadptr_exec_event_hooks((th), (flag), (self), (id), (klass), (pop_p)); \
00739         } \
00740     } \
00741 } while (0)
00742 
00743 #define EXEC_EVENT_HOOK(th, flag, self, id, klass) \
00744   EXEC_EVENT_HOOK_ORIG(th, flag, self, id, klass, 0)
00745 
00746 #define EXEC_EVENT_HOOK_AND_POP_FRAME(th, flag, self, id, klass) \
00747   EXEC_EVENT_HOOK_ORIG(th, flag, self, id, klass, 1)
00748 
00749 #if defined __GNUC__ && __GNUC__ >= 4
00750 #pragma GCC visibility push(default)
00751 #endif
00752 
00753 int rb_thread_check_trap_pending(void);
00754 
00755 extern VALUE rb_get_coverages(void);
00756 extern void rb_set_coverages(VALUE);
00757 extern void rb_reset_coverages(void);
00758 
00759 #if defined __GNUC__ && __GNUC__ >= 4
00760 #pragma GCC visibility pop
00761 #endif
00762 
00763 #endif /* RUBY_VM_CORE_H */
00764