vm_core.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   vm_core.h -
00004 
00005   $Author: nagachika $
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 "vm_debug.h"
00022 #include "vm_opts.h"
00023 #include "id.h"
00024 #include "method.h"
00025 #include "ruby_atomic.h"
00026 
00027 #include "thread_native.h"
00028 
00029 #ifndef ENABLE_VM_OBJSPACE
00030 #ifdef _WIN32
00031 /*
00032  * TODO: object space independent st_table.
00033  * socklist needs st_table in rb_w32_sysinit(), before object space
00034  * initialization.
00035  * It is too early now to change st_hash_type, since it breaks binary
00036  * compatibility.
00037  */
00038 #define ENABLE_VM_OBJSPACE 0
00039 #else
00040 #define ENABLE_VM_OBJSPACE 1
00041 #endif
00042 #endif
00043 
00044 #include <setjmp.h>
00045 #include <signal.h>
00046 
00047 #ifndef NSIG
00048 # define NSIG (_SIGMAX + 1)      /* For QNX */
00049 #endif
00050 
00051 #define RUBY_NSIG NSIG
00052 
00053 #ifdef HAVE_STDARG_PROTOTYPES
00054 #include <stdarg.h>
00055 #define va_init_list(a,b) va_start((a),(b))
00056 #else
00057 #include <varargs.h>
00058 #define va_init_list(a,b) va_start((a))
00059 #endif
00060 
00061 #if defined(SIGSEGV) && defined(HAVE_SIGALTSTACK) && defined(SA_SIGINFO) && !defined(__NetBSD__)
00062 #define USE_SIGALTSTACK
00063 #endif
00064 
00065 /*****************/
00066 /* configuration */
00067 /*****************/
00068 
00069 /* gcc ver. check */
00070 #if defined(__GNUC__) && __GNUC__ >= 2
00071 
00072 #if OPT_TOKEN_THREADED_CODE
00073 #if OPT_DIRECT_THREADED_CODE
00074 #undef OPT_DIRECT_THREADED_CODE
00075 #endif
00076 #endif
00077 
00078 #else /* defined(__GNUC__) && __GNUC__ >= 2 */
00079 
00080 /* disable threaded code options */
00081 #if OPT_DIRECT_THREADED_CODE
00082 #undef OPT_DIRECT_THREADED_CODE
00083 #endif
00084 #if OPT_TOKEN_THREADED_CODE
00085 #undef OPT_TOKEN_THREADED_CODE
00086 #endif
00087 #endif
00088 
00089 #ifdef __native_client__
00090 #undef OPT_DIRECT_THREADED_CODE
00091 #endif
00092 
00093 /* call threaded code */
00094 #if    OPT_CALL_THREADED_CODE
00095 #if    OPT_DIRECT_THREADED_CODE
00096 #undef OPT_DIRECT_THREADED_CODE
00097 #endif /* OPT_DIRECT_THREADED_CODE */
00098 #if    OPT_STACK_CACHING
00099 #undef OPT_STACK_CACHING
00100 #endif /* OPT_STACK_CACHING */
00101 #endif /* OPT_CALL_THREADED_CODE */
00102 
00103 /* likely */
00104 #if __GNUC__ >= 3
00105 #define LIKELY(x)   (__builtin_expect((x), 1))
00106 #define UNLIKELY(x) (__builtin_expect((x), 0))
00107 #else /* __GNUC__ >= 3 */
00108 #define LIKELY(x)   (x)
00109 #define UNLIKELY(x) (x)
00110 #endif /* __GNUC__ >= 3 */
00111 
00112 #ifndef __has_attribute
00113 # define __has_attribute(x) 0
00114 #endif
00115 
00116 #if __has_attribute(unused)
00117 #define UNINITIALIZED_VAR(x) x __attribute__((unused))
00118 #elif defined(__GNUC__) && __GNUC__ >= 3
00119 #define UNINITIALIZED_VAR(x) x = x
00120 #else
00121 #define UNINITIALIZED_VAR(x) x
00122 #endif
00123 
00124 typedef unsigned long rb_num_t;
00125 
00126 /* iseq data type */
00127 
00128 struct iseq_compile_data_ensure_node_stack;
00129 
00130 typedef struct rb_compile_option_struct rb_compile_option_t;
00131 
00132 
00133 struct iseq_inline_cache_entry {
00134     rb_serial_t ic_serial;
00135     union {
00136         size_t index;
00137         VALUE value;
00138     } ic_value;
00139 };
00140 
00141 union iseq_inline_storage_entry {
00142     struct {
00143         struct rb_thread_struct *running_thread;
00144         VALUE value;
00145         VALUE done;
00146     } once;
00147     struct iseq_inline_cache_entry cache;
00148 };
00149 
00150 /* to avoid warning */
00151 struct rb_thread_struct;
00152 struct rb_control_frame_struct;
00153 
00154 /* rb_call_info_t contains calling information including inline cache */
00155 typedef struct rb_call_info_struct {
00156     /* fixed at compile time */
00157     ID mid;
00158     VALUE flag;
00159     int orig_argc;
00160     rb_iseq_t *blockiseq;
00161 
00162     /* inline cache: keys */
00163     rb_serial_t method_state;
00164     rb_serial_t class_serial;
00165     VALUE klass;
00166 
00167     /* inline cache: values */
00168     const rb_method_entry_t *me;
00169     VALUE defined_class;
00170 
00171     /* temporary values for method calling */
00172     int argc;
00173     struct rb_block_struct *blockptr;
00174     VALUE recv;
00175     union {
00176         int opt_pc; /* used by iseq */
00177         long index; /* used by ivar */
00178         int missing_reason; /* used by method_missing */
00179         int inc_sp; /* used by cfunc */
00180     } aux;
00181 
00182     VALUE (*call)(struct rb_thread_struct *th, struct rb_control_frame_struct *cfp, struct rb_call_info_struct *ci);
00183 } rb_call_info_t;
00184 
00185 #if 1
00186 #define GetCoreDataFromValue(obj, type, ptr) do { \
00187     (ptr) = (type*)DATA_PTR(obj); \
00188 } while (0)
00189 #else
00190 #define GetCoreDataFromValue(obj, type, ptr) Data_Get_Struct((obj), type, (ptr))
00191 #endif
00192 
00193 #define GetISeqPtr(obj, ptr) \
00194   GetCoreDataFromValue((obj), rb_iseq_t, (ptr))
00195 
00196 typedef struct rb_iseq_location_struct {
00197     const VALUE path;
00198     const VALUE absolute_path;
00199     const VALUE base_label;
00200     const VALUE label;
00201     size_t first_lineno;
00202 } rb_iseq_location_t;
00203 
00204 struct rb_iseq_struct;
00205 
00206 struct rb_iseq_struct {
00207     /***************/
00208     /* static data */
00209     /***************/
00210 
00211     enum iseq_type {
00212         ISEQ_TYPE_TOP,
00213         ISEQ_TYPE_METHOD,
00214         ISEQ_TYPE_BLOCK,
00215         ISEQ_TYPE_CLASS,
00216         ISEQ_TYPE_RESCUE,
00217         ISEQ_TYPE_ENSURE,
00218         ISEQ_TYPE_EVAL,
00219         ISEQ_TYPE_MAIN,
00220         ISEQ_TYPE_DEFINED_GUARD
00221     } type;              /* instruction sequence type */
00222 
00223     rb_iseq_location_t location;
00224 
00225     VALUE *iseq;         /* iseq (insn number and operands) */
00226     VALUE *iseq_encoded; /* encoded iseq */
00227     unsigned long iseq_size;
00228     const VALUE mark_ary;     /* Array: includes operands which should be GC marked */
00229     const VALUE coverage;     /* coverage array */
00230 
00231     /* insn info, must be freed */
00232     struct iseq_line_info_entry *line_info_table;
00233     size_t line_info_size;
00234 
00235     ID *local_table;            /* must free */
00236     int local_table_size;
00237 
00238     /* sizeof(vars) + 1 */
00239     int local_size;
00240 
00241     union iseq_inline_storage_entry *is_entries;
00242     int is_size;
00243 
00244     rb_call_info_t *callinfo_entries;
00245     int callinfo_size;
00246 
00274     int argc;
00275     int arg_simple;
00276     int arg_rest;
00277     int arg_block;
00278     int arg_opts;
00279     int arg_post_len;
00280     int arg_post_start;
00281     int arg_size;
00282     VALUE *arg_opt_table;
00283     int arg_keyword;
00284     int arg_keyword_check; /* if this is true, raise an ArgumentError when unknown keyword argument is passed */
00285     int arg_keywords;
00286     int arg_keyword_required;
00287     ID *arg_keyword_table;
00288 
00289     size_t stack_max; /* for stack overflow check */
00290 
00291     /* catch table */
00292     struct iseq_catch_table_entry *catch_table;
00293     int catch_table_size;
00294 
00295     /* for child iseq */
00296     struct rb_iseq_struct *parent_iseq;
00297     struct rb_iseq_struct *local_iseq;
00298 
00299     /****************/
00300     /* dynamic data */
00301     /****************/
00302 
00303     VALUE self;
00304     const VALUE orig;                   /* non-NULL if its data have origin */
00305 
00306     /* block inlining */
00307     /*
00308      * NODE *node;
00309      * void *special_block_builder;
00310      * void *cached_special_block_builder;
00311      * VALUE cached_special_block;
00312      */
00313 
00314     /* klass/module nest information stack (cref) */
00315     NODE * const cref_stack;
00316     const VALUE klass;
00317 
00318     /* misc */
00319     ID defined_method_id;       /* for define_method */
00320     rb_num_t flip_cnt;
00321 
00322     /* used at compile time */
00323     struct iseq_compile_data *compile_data;
00324 };
00325 
00326 enum ruby_special_exceptions {
00327     ruby_error_reenter,
00328     ruby_error_nomemory,
00329     ruby_error_sysstack,
00330     ruby_error_closed_stream,
00331     ruby_special_error_count
00332 };
00333 
00334 #define GetVMPtr(obj, ptr) \
00335   GetCoreDataFromValue((obj), rb_vm_t, (ptr))
00336 
00337 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
00338 struct rb_objspace;
00339 void rb_objspace_free(struct rb_objspace *);
00340 #endif
00341 
00342 typedef struct rb_hook_list_struct {
00343     struct rb_event_hook_struct *hooks;
00344     rb_event_flag_t events;
00345     int need_clean;
00346 } rb_hook_list_t;
00347 
00348 typedef struct rb_vm_struct {
00349     VALUE self;
00350 
00351     rb_global_vm_lock_t gvl;
00352     rb_nativethread_lock_t    thread_destruct_lock;
00353 
00354     struct rb_thread_struct *main_thread;
00355     struct rb_thread_struct *running_thread;
00356 
00357     st_table *living_threads;
00358     VALUE thgroup_default;
00359 
00360     int running;
00361     int thread_abort_on_exception;
00362     int trace_running;
00363     volatile int sleeper;
00364 
00365     /* object management */
00366     VALUE mark_object_ary;
00367 
00368     VALUE special_exceptions[ruby_special_error_count];
00369 
00370     /* load */
00371     VALUE top_self;
00372     VALUE load_path;
00373     VALUE load_path_snapshot;
00374     VALUE load_path_check_cache;
00375     VALUE expanded_load_path;
00376     VALUE loaded_features;
00377     VALUE loaded_features_snapshot;
00378     struct st_table *loaded_features_index;
00379     struct st_table *loading_table;
00380 
00381     /* signal */
00382     struct {
00383         VALUE cmd;
00384         int safe;
00385     } trap_list[RUBY_NSIG];
00386 
00387     /* hook */
00388     rb_hook_list_t event_hooks;
00389 
00390     /* relation table of ensure - rollback for callcc */
00391     struct st_table *ensure_rollback_table;
00392 
00393     /* postponed_job */
00394     struct rb_postponed_job_struct *postponed_job_buffer;
00395     int postponed_job_index;
00396 
00397     int src_encoding_index;
00398 
00399     VALUE verbose, debug, orig_progname, progname;
00400     VALUE coverages;
00401 
00402     struct unlinked_method_entry_list_entry *unlinked_method_entry_list;
00403 
00404     VALUE defined_module_hash;
00405 
00406 #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE
00407     struct rb_objspace *objspace;
00408 #endif
00409 
00410     /*
00411      * @shyouhei notes that this is not for storing normal Ruby
00412      * objects so do *NOT* mark this when you GC.
00413      */
00414     struct RArray at_exit;
00415 
00416     VALUE *defined_strings;
00417 
00418     /* params */
00419     struct { /* size in byte */
00420         size_t thread_vm_stack_size;
00421         size_t thread_machine_stack_size;
00422         size_t fiber_vm_stack_size;
00423         size_t fiber_machine_stack_size;
00424     } default_params;
00425 } rb_vm_t;
00426 
00427 /* default values */
00428 
00429 #define RUBY_VM_SIZE_ALIGN 4096
00430 
00431 #define RUBY_VM_THREAD_VM_STACK_SIZE          ( 128 * 1024 * sizeof(VALUE)) /*  512 KB or 1024 KB */
00432 #define RUBY_VM_THREAD_VM_STACK_SIZE_MIN      (   2 * 1024 * sizeof(VALUE)) /*    8 KB or   16 KB */
00433 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE     ( 128 * 1024 * sizeof(VALUE)) /*  512 KB or 1024 KB */
00434 #define RUBY_VM_THREAD_MACHINE_STACK_SIZE_MIN (  16 * 1024 * sizeof(VALUE)) /*   64 KB or  128 KB */
00435 
00436 #define RUBY_VM_FIBER_VM_STACK_SIZE           (  16 * 1024 * sizeof(VALUE)) /*   64 KB or  128 KB */
00437 #define RUBY_VM_FIBER_VM_STACK_SIZE_MIN       (   2 * 1024 * sizeof(VALUE)) /*    8 KB or   16 KB */
00438 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE      (  64 * 1024 * sizeof(VALUE)) /*  256 KB or  512 KB */
00439 #define RUBY_VM_FIBER_MACHINE_STACK_SIZE_MIN  (  16 * 1024 * sizeof(VALUE)) /*   64 KB or  128 KB */
00440 
00441 #ifndef VM_DEBUG_BP_CHECK
00442 #define VM_DEBUG_BP_CHECK 0
00443 #endif
00444 
00445 typedef struct rb_control_frame_struct {
00446     VALUE *pc;                  /* cfp[0] */
00447     VALUE *sp;                  /* cfp[1] */
00448     rb_iseq_t *iseq;            /* cfp[2] */
00449     VALUE flag;                 /* cfp[3] */
00450     VALUE self;                 /* cfp[4] / block[0] */
00451     VALUE klass;                /* cfp[5] / block[1] */
00452     VALUE *ep;                  /* cfp[6] / block[2] */
00453     rb_iseq_t *block_iseq;      /* cfp[7] / block[3] */
00454     VALUE proc;                 /* cfp[8] / block[4] */
00455     const rb_method_entry_t *me;/* cfp[9] */
00456 
00457 #if VM_DEBUG_BP_CHECK
00458     VALUE *bp_check;            /* cfp[10] */
00459 #endif
00460 } rb_control_frame_t;
00461 
00462 typedef struct rb_block_struct {
00463     VALUE self;                 /* share with method frame if it's only block */
00464     VALUE klass;                /* share with method frame if it's only block */
00465     VALUE *ep;                  /* share with method frame if it's only block */
00466     rb_iseq_t *iseq;
00467     VALUE proc;
00468 } rb_block_t;
00469 
00470 extern const rb_data_type_t ruby_threadptr_data_type;
00471 
00472 #define GetThreadPtr(obj, ptr) \
00473     TypedData_Get_Struct((obj), rb_thread_t, &ruby_threadptr_data_type, (ptr))
00474 
00475 enum rb_thread_status {
00476     THREAD_RUNNABLE,
00477     THREAD_STOPPED,
00478     THREAD_STOPPED_FOREVER,
00479     THREAD_KILLED
00480 };
00481 
00482 typedef RUBY_JMP_BUF rb_jmpbuf_t;
00483 
00484 /*
00485   the members which are written in TH_PUSH_TAG() should be placed at
00486   the beginning and the end, so that entire region is accessible.
00487 */
00488 struct rb_vm_tag {
00489     VALUE tag;
00490     VALUE retval;
00491     rb_jmpbuf_t buf;
00492     struct rb_vm_tag *prev;
00493 };
00494 
00495 struct rb_vm_protect_tag {
00496     struct rb_vm_protect_tag *prev;
00497 };
00498 
00499 struct rb_unblock_callback {
00500     rb_unblock_function_t *func;
00501     void *arg;
00502 };
00503 
00504 struct rb_mutex_struct;
00505 
00506 struct rb_thread_struct;
00507 typedef struct rb_thread_list_struct{
00508     struct rb_thread_list_struct *next;
00509     struct rb_thread_struct *th;
00510 } rb_thread_list_t;
00511 
00512 
00513 typedef struct rb_ensure_entry {
00514     VALUE marker;
00515     VALUE (*e_proc)(ANYARGS);
00516     VALUE data2;
00517 } rb_ensure_entry_t;
00518 
00519 typedef struct rb_ensure_list {
00520     struct rb_ensure_list *next;
00521     struct rb_ensure_entry entry;
00522 } rb_ensure_list_t;
00523 
00524 typedef struct rb_thread_struct {
00525     VALUE self;
00526     rb_vm_t *vm;
00527 
00528     /* execution information */
00529     VALUE *stack;               /* must free, must mark */
00530     size_t stack_size;          /* size in word (byte size / sizeof(VALUE)) */
00531     rb_control_frame_t *cfp;
00532     int safe_level;
00533     int raised_flag;
00534     VALUE last_status; /* $? */
00535 
00536     /* passing state */
00537     int state;
00538 
00539     int waiting_fd;
00540 
00541     /* for rb_iterate */
00542     const rb_block_t *passed_block;
00543 
00544     /* for bmethod */
00545     const rb_method_entry_t *passed_bmethod_me;
00546 
00547     /* for cfunc */
00548     rb_call_info_t *passed_ci;
00549 
00550     /* for load(true) */
00551     VALUE top_self;
00552     VALUE top_wrapper;
00553 
00554     /* eval env */
00555     rb_block_t *base_block;
00556 
00557     VALUE *root_lep;
00558     VALUE root_svar;
00559 
00560     /* thread control */
00561     rb_nativethread_id_t thread_id;
00562     enum rb_thread_status status;
00563     int to_kill;
00564     int priority;
00565 
00566     native_thread_data_t native_thread_data;
00567     void *blocking_region_buffer;
00568 
00569     VALUE thgroup;
00570     VALUE value;
00571 
00572     /* temporary place of errinfo */
00573     VALUE errinfo;
00574 
00575     /* temporary place of retval on OPT_CALL_THREADED_CODE */
00576 #if OPT_CALL_THREADED_CODE
00577     VALUE retval;
00578 #endif
00579 
00580     /* async errinfo queue */
00581     VALUE pending_interrupt_queue;
00582     int pending_interrupt_queue_checked;
00583     VALUE pending_interrupt_mask_stack;
00584 
00585     rb_atomic_t interrupt_flag;
00586     unsigned long interrupt_mask;
00587     rb_nativethread_lock_t interrupt_lock;
00588     rb_nativethread_cond_t interrupt_cond;
00589     struct rb_unblock_callback unblock;
00590     VALUE locking_mutex;
00591     struct rb_mutex_struct *keeping_mutexes;
00592 
00593     struct rb_vm_tag *tag;
00594     struct rb_vm_protect_tag *protect_tag;
00595 
00602     int parse_in_eval;
00603 
00608     int mild_compile_error;
00609 
00610     /* storage */
00611     st_table *local_storage;
00612 
00613     rb_thread_list_t *join_list;
00614 
00615     VALUE first_proc;
00616     VALUE first_args;
00617     VALUE (*first_func)(ANYARGS);
00618 
00619     /* for GC */
00620     struct {
00621         VALUE *stack_start;
00622         VALUE *stack_end;
00623         size_t stack_maxsize;
00624 #ifdef __ia64
00625         VALUE *register_stack_start;
00626         VALUE *register_stack_end;
00627         size_t register_stack_maxsize;
00628 #endif
00629         jmp_buf regs;
00630     } machine;
00631     int mark_stack_len;
00632 
00633     /* statistics data for profiler */
00634     VALUE stat_insn_usage;
00635 
00636     /* tracer */
00637     rb_hook_list_t event_hooks;
00638     struct rb_trace_arg_struct *trace_arg; /* trace information */
00639 
00640     /* fiber */
00641     VALUE fiber;
00642     VALUE root_fiber;
00643     rb_jmpbuf_t root_jmpbuf;
00644 
00645     /* ensure & callcc */
00646     rb_ensure_list_t *ensure_list;
00647 
00648     /* misc */
00649     int method_missing_reason;
00650     int abort_on_exception;
00651 #ifdef USE_SIGALTSTACK
00652     void *altstack;
00653 #endif
00654     unsigned long running_time_us;
00655 } rb_thread_t;
00656 
00657 typedef enum {
00658     VM_DEFINECLASS_TYPE_CLASS           = 0x00,
00659     VM_DEFINECLASS_TYPE_SINGLETON_CLASS = 0x01,
00660     VM_DEFINECLASS_TYPE_MODULE          = 0x02,
00661     /* 0x03..0x06 is reserved */
00662     VM_DEFINECLASS_TYPE_MASK            = 0x07
00663 } rb_vm_defineclass_type_t;
00664 
00665 #define VM_DEFINECLASS_TYPE(x) ((rb_vm_defineclass_type_t)(x) & VM_DEFINECLASS_TYPE_MASK)
00666 #define VM_DEFINECLASS_FLAG_SCOPED         0x08
00667 #define VM_DEFINECLASS_FLAG_HAS_SUPERCLASS 0x10
00668 #define VM_DEFINECLASS_SCOPED_P(x) ((x) & VM_DEFINECLASS_FLAG_SCOPED)
00669 #define VM_DEFINECLASS_HAS_SUPERCLASS_P(x) \
00670     ((x) & VM_DEFINECLASS_FLAG_HAS_SUPERCLASS)
00671 
00672 /* iseq.c */
00673 RUBY_SYMBOL_EXPORT_BEGIN
00674 
00675 /* node -> iseq */
00676 VALUE rb_iseq_new(NODE*, VALUE, VALUE, VALUE, VALUE, enum iseq_type);
00677 VALUE rb_iseq_new_top(NODE *node, VALUE name, VALUE path, VALUE absolute_path, VALUE parent);
00678 VALUE rb_iseq_new_main(NODE *node, VALUE path, VALUE absolute_path);
00679 VALUE rb_iseq_new_with_bopt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, enum iseq_type, VALUE);
00680 VALUE rb_iseq_new_with_opt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE, enum iseq_type, const rb_compile_option_t*);
00681 
00682 /* src -> iseq */
00683 VALUE rb_iseq_compile(VALUE src, VALUE file, VALUE line);
00684 VALUE rb_iseq_compile_on_base(VALUE src, VALUE file, VALUE line, rb_block_t *base_block);
00685 VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE absolute_path, VALUE line, rb_block_t *base_block, VALUE opt);
00686 
00687 VALUE rb_iseq_disasm(VALUE self);
00688 int rb_iseq_disasm_insn(VALUE str, VALUE *iseqval, size_t pos, rb_iseq_t *iseq, VALUE child);
00689 const char *ruby_node_name(int node);
00690 
00691 RUBY_EXTERN VALUE rb_cISeq;
00692 RUBY_EXTERN VALUE rb_cRubyVM;
00693 RUBY_EXTERN VALUE rb_cEnv;
00694 RUBY_EXTERN VALUE rb_mRubyVMFrozenCore;
00695 RUBY_SYMBOL_EXPORT_END
00696 
00697 #define GetProcPtr(obj, ptr) \
00698   GetCoreDataFromValue((obj), rb_proc_t, (ptr))
00699 
00700 typedef struct {
00701     rb_block_t block;
00702 
00703     VALUE envval;               /* for GC mark */
00704     VALUE blockprocval;
00705     int safe_level;
00706     int is_from_method;
00707     int is_lambda;
00708 } rb_proc_t;
00709 
00710 #define GetEnvPtr(obj, ptr) \
00711   GetCoreDataFromValue((obj), rb_env_t, (ptr))
00712 
00713 typedef struct {
00714     VALUE *env;
00715     int env_size;
00716     int local_size;
00717     VALUE prev_envval;          /* for GC mark */
00718     rb_block_t block;
00719 } rb_env_t;
00720 
00721 extern const rb_data_type_t ruby_binding_data_type;
00722 
00723 #define GetBindingPtr(obj, ptr) \
00724   GetCoreDataFromValue((obj), rb_binding_t, (ptr))
00725 
00726 typedef struct {
00727     VALUE env;
00728     VALUE path;
00729     VALUE blockprocval; /* for GC mark */
00730     unsigned short first_lineno;
00731 } rb_binding_t;
00732 
00733 /* used by compile time and send insn */
00734 
00735 enum vm_check_match_type {
00736     VM_CHECKMATCH_TYPE_WHEN = 1,
00737     VM_CHECKMATCH_TYPE_CASE = 2,
00738     VM_CHECKMATCH_TYPE_RESCUE = 3
00739 };
00740 
00741 #define VM_CHECKMATCH_TYPE_MASK   0x03
00742 #define VM_CHECKMATCH_ARRAY       0x04
00743 
00744 #define VM_CALL_ARGS_SPLAT      (0x01 << 1) /* m(*args) */
00745 #define VM_CALL_ARGS_BLOCKARG   (0x01 << 2) /* m(&block) */
00746 #define VM_CALL_FCALL           (0x01 << 3) /* m(...) */
00747 #define VM_CALL_VCALL           (0x01 << 4) /* m */
00748 #define VM_CALL_TAILCALL        (0x01 << 5) /* located at tail position */
00749 #define VM_CALL_SUPER           (0x01 << 6) /* super */
00750 #define VM_CALL_OPT_SEND        (0x01 << 7) /* internal flag */
00751 #define VM_CALL_ARGS_SKIP_SETUP (0x01 << 8) /* (flag & (SPLAT|BLOCKARG)) && blockiseq == 0 */
00752 
00753 enum vm_special_object_type {
00754     VM_SPECIAL_OBJECT_VMCORE = 1,
00755     VM_SPECIAL_OBJECT_CBASE,
00756     VM_SPECIAL_OBJECT_CONST_BASE
00757 };
00758 
00759 #define VM_FRAME_MAGIC_METHOD 0x11
00760 #define VM_FRAME_MAGIC_BLOCK  0x21
00761 #define VM_FRAME_MAGIC_CLASS  0x31
00762 #define VM_FRAME_MAGIC_TOP    0x41
00763 #define VM_FRAME_MAGIC_CFUNC  0x61
00764 #define VM_FRAME_MAGIC_PROC   0x71
00765 #define VM_FRAME_MAGIC_IFUNC  0x81
00766 #define VM_FRAME_MAGIC_EVAL   0x91
00767 #define VM_FRAME_MAGIC_LAMBDA 0xa1
00768 #define VM_FRAME_MAGIC_RESCUE 0xb1
00769 #define VM_FRAME_MAGIC_MASK_BITS 8
00770 #define VM_FRAME_MAGIC_MASK   (~(~0<<VM_FRAME_MAGIC_MASK_BITS))
00771 
00772 #define VM_FRAME_TYPE(cfp) ((cfp)->flag & VM_FRAME_MAGIC_MASK)
00773 
00774 /* other frame flag */
00775 #define VM_FRAME_FLAG_PASSED  0x0100
00776 #define VM_FRAME_FLAG_FINISH  0x0200
00777 #define VM_FRAME_FLAG_BMETHOD 0x0400
00778 #define VM_FRAME_TYPE_FINISH_P(cfp)  (((cfp)->flag & VM_FRAME_FLAG_FINISH) != 0)
00779 #define VM_FRAME_TYPE_BMETHOD_P(cfp) (((cfp)->flag & VM_FRAME_FLAG_BMETHOD) != 0)
00780 
00781 #define RUBYVM_CFUNC_FRAME_P(cfp) \
00782   (VM_FRAME_TYPE(cfp) == VM_FRAME_MAGIC_CFUNC)
00783 
00784 /* inline cache */
00785 typedef struct iseq_inline_cache_entry *IC;
00786 typedef rb_call_info_t *CALL_INFO;
00787 
00788 void rb_vm_change_state(void);
00789 
00790 typedef VALUE CDHASH;
00791 
00792 #ifndef FUNC_FASTCALL
00793 #define FUNC_FASTCALL(x) x
00794 #endif
00795 
00796 typedef rb_control_frame_t *
00797   (FUNC_FASTCALL(*rb_insn_func_t))(rb_thread_t *, rb_control_frame_t *);
00798 
00799 #define GC_GUARDED_PTR(p)     ((VALUE)((VALUE)(p) | 0x01))
00800 #define GC_GUARDED_PTR_REF(p) ((void *)(((VALUE)(p)) & ~0x03))
00801 #define GC_GUARDED_PTR_P(p)   (((VALUE)(p)) & 0x01)
00802 
00803 /*
00804  * block frame:
00805  *  ep[ 0]: prev frame
00806  *  ep[-1]: CREF (for *_eval)
00807  *
00808  * method frame:
00809  *  ep[ 0]: block pointer (ptr | VM_ENVVAL_BLOCK_PTR_FLAG)
00810  */
00811 
00812 #define VM_ENVVAL_BLOCK_PTR_FLAG 0x02
00813 #define VM_ENVVAL_BLOCK_PTR(v)     (GC_GUARDED_PTR(v) | VM_ENVVAL_BLOCK_PTR_FLAG)
00814 #define VM_ENVVAL_BLOCK_PTR_P(v)   ((v) & VM_ENVVAL_BLOCK_PTR_FLAG)
00815 #define VM_ENVVAL_PREV_EP_PTR(v)   ((VALUE)GC_GUARDED_PTR(v))
00816 #define VM_ENVVAL_PREV_EP_PTR_P(v) (!(VM_ENVVAL_BLOCK_PTR_P(v)))
00817 
00818 #define VM_EP_PREV_EP(ep)   ((VALUE *)GC_GUARDED_PTR_REF((ep)[0]))
00819 #define VM_EP_BLOCK_PTR(ep) ((rb_block_t *)GC_GUARDED_PTR_REF((ep)[0]))
00820 #define VM_EP_LEP_P(ep)     VM_ENVVAL_BLOCK_PTR_P((ep)[0])
00821 
00822 VALUE *rb_vm_ep_local_ep(VALUE *ep);
00823 rb_block_t *rb_vm_control_frame_block_ptr(rb_control_frame_t *cfp);
00824 
00825 #define RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp) ((cfp)+1)
00826 #define RUBY_VM_NEXT_CONTROL_FRAME(cfp) ((cfp)-1)
00827 #define RUBY_VM_END_CONTROL_FRAME(th) \
00828   ((rb_control_frame_t *)((th)->stack + (th)->stack_size))
00829 #define RUBY_VM_VALID_CONTROL_FRAME_P(cfp, ecfp) \
00830   ((void *)(ecfp) > (void *)(cfp))
00831 #define RUBY_VM_CONTROL_FRAME_STACK_OVERFLOW_P(th, cfp) \
00832   (!RUBY_VM_VALID_CONTROL_FRAME_P((cfp), RUBY_VM_END_CONTROL_FRAME(th)))
00833 
00834 #define RUBY_VM_IFUNC_P(ptr)        (BUILTIN_TYPE(ptr) == T_NODE)
00835 #define RUBY_VM_NORMAL_ISEQ_P(ptr) \
00836   ((ptr) && !RUBY_VM_IFUNC_P(ptr))
00837 
00838 #define RUBY_VM_GET_BLOCK_PTR_IN_CFP(cfp) ((rb_block_t *)(&(cfp)->self))
00839 #define RUBY_VM_GET_CFP_FROM_BLOCK_PTR(b) \
00840   ((rb_control_frame_t *)((VALUE *)(b) - 4))
00841 /* magic number `4' is depend on rb_control_frame_t layout. */
00842 
00843 /* VM related object allocate functions */
00844 VALUE rb_thread_alloc(VALUE klass);
00845 VALUE rb_proc_alloc(VALUE klass);
00846 VALUE rb_binding_alloc(VALUE klass);
00847 
00848 /* for debug */
00849 extern void rb_vmdebug_stack_dump_raw(rb_thread_t *, rb_control_frame_t *);
00850 extern void rb_vmdebug_debug_print_pre(rb_thread_t *th, rb_control_frame_t *cfp, VALUE *_pc);
00851 extern void rb_vmdebug_debug_print_post(rb_thread_t *th, rb_control_frame_t *cfp);
00852 
00853 #define SDR() rb_vmdebug_stack_dump_raw(GET_THREAD(), GET_THREAD()->cfp)
00854 #define SDR2(cfp) rb_vmdebug_stack_dump_raw(GET_THREAD(), (cfp))
00855 void rb_vm_bugreport(void);
00856 
00857 /* functions about thread/vm execution */
00858 RUBY_SYMBOL_EXPORT_BEGIN
00859 VALUE rb_iseq_eval(VALUE iseqval);
00860 VALUE rb_iseq_eval_main(VALUE iseqval);
00861 RUBY_SYMBOL_EXPORT_END
00862 int rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, VALUE *klassp);
00863 
00864 VALUE rb_vm_invoke_proc(rb_thread_t *th, rb_proc_t *proc,
00865                         int argc, const VALUE *argv, const rb_block_t *blockptr);
00866 VALUE rb_vm_make_proc(rb_thread_t *th, const rb_block_t *block, VALUE klass);
00867 VALUE rb_vm_make_binding(rb_thread_t *th, const rb_control_frame_t *src_cfp);
00868 VALUE rb_vm_make_env_object(rb_thread_t *th, rb_control_frame_t *cfp);
00869 VALUE rb_binding_new_with_cfp(rb_thread_t *th, const rb_control_frame_t *src_cfp);
00870 VALUE *rb_binding_add_dynavars(rb_binding_t *bind, int dyncount, const ID *dynvars);
00871 void rb_vm_inc_const_missing_count(void);
00872 void rb_vm_gvl_destroy(rb_vm_t *vm);
00873 VALUE rb_vm_call(rb_thread_t *th, VALUE recv, VALUE id, int argc,
00874                  const VALUE *argv, const rb_method_entry_t *me,
00875                  VALUE defined_class);
00876 void rb_unlink_method_entry(rb_method_entry_t *me);
00877 void rb_gc_mark_unlinked_live_method_entries(void *pvm);
00878 
00879 void rb_thread_start_timer_thread(void);
00880 void rb_thread_stop_timer_thread(int);
00881 void rb_thread_reset_timer_thread(void);
00882 void rb_thread_wakeup_timer_thread(void);
00883 
00884 int ruby_thread_has_gvl_p(void);
00885 typedef int rb_backtrace_iter_func(void *, VALUE, int, VALUE);
00886 rb_control_frame_t *rb_vm_get_ruby_level_next_cfp(rb_thread_t *th, const rb_control_frame_t *cfp);
00887 rb_control_frame_t *rb_vm_get_binding_creatable_next_cfp(rb_thread_t *th, const rb_control_frame_t *cfp);
00888 int rb_vm_get_sourceline(const rb_control_frame_t *);
00889 VALUE rb_name_err_mesg_new(VALUE obj, VALUE mesg, VALUE recv, VALUE method);
00890 void rb_vm_stack_to_heap(rb_thread_t *th);
00891 void ruby_thread_init_stack(rb_thread_t *th);
00892 int rb_vm_control_frame_id_and_class(const rb_control_frame_t *cfp, ID *idp, VALUE *klassp);
00893 void rb_vm_rewind_cfp(rb_thread_t *th, rb_control_frame_t *cfp);
00894 
00895 void rb_gc_mark_machine_stack(rb_thread_t *th);
00896 
00897 int rb_autoloading_value(VALUE mod, ID id, VALUE* value);
00898 
00899 #define sysstack_error GET_VM()->special_exceptions[ruby_error_sysstack]
00900 
00901 #define RUBY_CONST_ASSERT(expr) (1/!!(expr)) /* expr must be a compile-time constant */
00902 #define VM_STACK_OVERFLOWED_P(cfp, sp, margin) \
00903     (!RUBY_CONST_ASSERT(sizeof(*(sp)) == sizeof(VALUE)) || \
00904      !RUBY_CONST_ASSERT(sizeof(*(cfp)) == sizeof(rb_control_frame_t)) || \
00905      ((rb_control_frame_t *)((sp) + (margin)) + 1) >= (cfp))
00906 #define WHEN_VM_STACK_OVERFLOWED(cfp, sp, margin) \
00907     if (LIKELY(!VM_STACK_OVERFLOWED_P(cfp, sp, margin))) {(void)0;} else /* overflowed */
00908 #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin) \
00909     WHEN_VM_STACK_OVERFLOWED(cfp, sp, margin) vm_stackoverflow()
00910 #define CHECK_VM_STACK_OVERFLOW(cfp, margin) \
00911     WHEN_VM_STACK_OVERFLOWED(cfp, (cfp)->sp, margin) vm_stackoverflow()
00912 
00913 /* for thread */
00914 
00915 #if RUBY_VM_THREAD_MODEL == 2
00916 extern rb_thread_t *ruby_current_thread;
00917 extern rb_vm_t *ruby_current_vm;
00918 extern rb_event_flag_t ruby_vm_event_flags;
00919 
00920 #define GET_VM() ruby_current_vm
00921 
00922 #ifndef OPT_CALL_CFUNC_WITHOUT_FRAME
00923 #define OPT_CALL_CFUNC_WITHOUT_FRAME 0
00924 #endif
00925 
00926 static inline rb_thread_t *
00927 GET_THREAD(void)
00928 {
00929     rb_thread_t *th = ruby_current_thread;
00930 #if OPT_CALL_CFUNC_WITHOUT_FRAME
00931     if (UNLIKELY(th->passed_ci != 0)) {
00932         void vm_call_cfunc_push_frame(rb_thread_t *th);
00933         vm_call_cfunc_push_frame(th);
00934     }
00935 #endif
00936     return th;
00937 }
00938 
00939 #define rb_thread_set_current_raw(th) (void)(ruby_current_thread = (th))
00940 #define rb_thread_set_current(th) do { \
00941     if ((th)->vm->running_thread != (th)) { \
00942         (th)->running_time_us = 0; \
00943     } \
00944     rb_thread_set_current_raw(th); \
00945     (th)->vm->running_thread = (th); \
00946 } while (0)
00947 
00948 #else
00949 #error "unsupported thread model"
00950 #endif
00951 
00952 enum {
00953     TIMER_INTERRUPT_MASK         = 0x01,
00954     PENDING_INTERRUPT_MASK       = 0x02,
00955     POSTPONED_JOB_INTERRUPT_MASK = 0x04,
00956     TRAP_INTERRUPT_MASK          = 0x08
00957 };
00958 
00959 #define RUBY_VM_SET_TIMER_INTERRUPT(th)         ATOMIC_OR((th)->interrupt_flag, TIMER_INTERRUPT_MASK)
00960 #define RUBY_VM_SET_INTERRUPT(th)               ATOMIC_OR((th)->interrupt_flag, PENDING_INTERRUPT_MASK)
00961 #define RUBY_VM_SET_POSTPONED_JOB_INTERRUPT(th) ATOMIC_OR((th)->interrupt_flag, POSTPONED_JOB_INTERRUPT_MASK)
00962 #define RUBY_VM_SET_TRAP_INTERRUPT(th)          ATOMIC_OR((th)->interrupt_flag, TRAP_INTERRUPT_MASK)
00963 #define RUBY_VM_INTERRUPTED(th) ((th)->interrupt_flag & ~(th)->interrupt_mask & (PENDING_INTERRUPT_MASK|TRAP_INTERRUPT_MASK))
00964 #define RUBY_VM_INTERRUPTED_ANY(th) ((th)->interrupt_flag & ~(th)->interrupt_mask)
00965 
00966 int rb_signal_buff_size(void);
00967 void rb_signal_exec(rb_thread_t *th, int sig);
00968 void rb_threadptr_check_signal(rb_thread_t *mth);
00969 void rb_threadptr_signal_raise(rb_thread_t *th, int sig);
00970 void rb_threadptr_signal_exit(rb_thread_t *th);
00971 void rb_threadptr_execute_interrupts(rb_thread_t *, int);
00972 void rb_threadptr_interrupt(rb_thread_t *th);
00973 void rb_threadptr_unlock_all_locking_mutexes(rb_thread_t *th);
00974 void rb_threadptr_pending_interrupt_clear(rb_thread_t *th);
00975 void rb_threadptr_pending_interrupt_enque(rb_thread_t *th, VALUE v);
00976 int rb_threadptr_pending_interrupt_active_p(rb_thread_t *th);
00977 
00978 #define RUBY_VM_CHECK_INTS_BLOCKING(th) do {                            \
00979         if (UNLIKELY(!rb_threadptr_pending_interrupt_empty_p(th))) {    \
00980             th->pending_interrupt_queue_checked = 0;                    \
00981             RUBY_VM_SET_INTERRUPT(th);                                  \
00982             rb_threadptr_execute_interrupts(th, 1);                     \
00983         }                                                               \
00984         else if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(th))) {               \
00985             rb_threadptr_execute_interrupts(th, 1);                     \
00986         }                                                               \
00987     } while (0)
00988 
00989 #define RUBY_VM_CHECK_INTS(th) do { \
00990     if (UNLIKELY(RUBY_VM_INTERRUPTED_ANY(th))) {        \
00991         rb_threadptr_execute_interrupts(th, 0); \
00992     } \
00993 } while (0)
00994 
00995 /* tracer */
00996 struct rb_trace_arg_struct {
00997     rb_event_flag_t event;
00998     rb_thread_t *th;
00999     rb_control_frame_t *cfp;
01000     VALUE self;
01001     ID id;
01002     VALUE klass;
01003     VALUE data;
01004 
01005     int klass_solved;
01006 
01007     /* calc from cfp */
01008     int lineno;
01009     VALUE path;
01010 };
01011 
01012 void rb_threadptr_exec_event_hooks(struct rb_trace_arg_struct *trace_arg);
01013 void rb_threadptr_exec_event_hooks_and_pop_frame(struct rb_trace_arg_struct *trace_arg);
01014 
01015 #define EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, klass_, data_, pop_p_) do { \
01016     if (UNLIKELY(ruby_vm_event_flags & (flag_))) { \
01017         if (((th)->event_hooks.events | (th)->vm->event_hooks.events) & (flag_)) { \
01018             struct rb_trace_arg_struct trace_arg; \
01019             trace_arg.event = (flag_); \
01020             trace_arg.th = (th_); \
01021             trace_arg.cfp = (trace_arg.th)->cfp; \
01022             trace_arg.self = (self_); \
01023             trace_arg.id = (id_); \
01024             trace_arg.klass = (klass_); \
01025             trace_arg.data = (data_); \
01026             trace_arg.path = Qundef; \
01027             trace_arg.klass_solved = 0; \
01028             if (pop_p_) rb_threadptr_exec_event_hooks_and_pop_frame(&trace_arg); \
01029             else rb_threadptr_exec_event_hooks(&trace_arg); \
01030         } \
01031     } \
01032 } while (0)
01033 
01034 #define EXEC_EVENT_HOOK(th_, flag_, self_, id_, klass_, data_) \
01035   EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, klass_, data_, 0)
01036 
01037 #define EXEC_EVENT_HOOK_AND_POP_FRAME(th_, flag_, self_, id_, klass_, data_) \
01038   EXEC_EVENT_HOOK_ORIG(th_, flag_, self_, id_, klass_, data_, 1)
01039 
01040 VALUE rb_threadptr_reset_recursive_data(rb_thread_t *th);
01041 void rb_threadptr_restore_recursive_data(rb_thread_t *th, VALUE old);
01042 
01043 RUBY_SYMBOL_EXPORT_BEGIN
01044 
01045 int rb_thread_check_trap_pending(void);
01046 
01047 extern VALUE rb_get_coverages(void);
01048 extern void rb_set_coverages(VALUE);
01049 extern void rb_reset_coverages(void);
01050 
01051 void rb_postponed_job_flush(rb_vm_t *vm);
01052 
01053 RUBY_SYMBOL_EXPORT_END
01054 
01055 #endif /* RUBY_VM_CORE_H */
01056 

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7