ruby/eval_intern.h

348 строки
8.3 KiB
C
Исходник Обычный вид История

#ifndef RUBY_EVAL_INTERN_H
#define RUBY_EVAL_INTERN_H
#include "ruby/ruby.h"
#include "vm_core.h"
#define PASS_PASSED_BLOCK_TH(th) do { \
* vm_core.h: remove lfp (local frame pointer) and rename dfp (dynamic frame pointer) to ep (environment pointer). This change make VM `normal' (similar to other interpreters). Before this commit: Each frame has two env pointers lfp and dfp. lfp points local environment which is method/class/toplevel frame. lfp[0] is block pointer. dfp is block local frame. dfp[0] points previous (parent) environment pointer. lfp == dfp when frame is method/class/toplevel. You can get lfp from dfp by traversing previous environment pointers. After this commit: Each frame has only `ep' to point respective enviornoment. If there is parent environment, then ep[0] points parent envioenment (as dfp). If there are no more environment, then ep[0] points block pointer (as lfp). We call such ep as `LEP' (local EP). We add some macros to get LEP and to detect LEP or not. In short, we replace dfp and lfp with ep and LEP. rb_block_t and rb_binding_t member `lfp' and `dfp' are removed and member `ep' is added. rename rb_thread_t's member `local_lfp' and `local_svar' to `root_lep' and `root_svar'. (VM_EP_PREV_EP(ep)): get previous environment pointer. This macro assume that ep is not LEP. (VM_EP_BLOCK_PTR(ep)): get block pointer. This macro assume that ep is LEP. (VM_EP_LEP_P(ep)): detect ep is LEP or not. (VM_ENVVAL_BLOCK_PTR(ptr)): make block pointer. (VM_ENVVAL_BLOCK_PTR_P(v)): detect v is block pointer. (VM_ENVVAL_PREV_EP_PTR(ptr)): make prev environment pointer. (VM_ENVVAL_PREV_EP_PTR_P(v)): detect v is prev env pointer. * vm.c: apply above changes. (VM_EP_LEP(ep)): get LEP. (VM_CF_LEP(cfp)): get LEP of cfp->ep. (VM_CF_PREV_EP(cfp)): utility function VM_EP_PREV_EP(cfp->ep). (VM_CF_BLOCK_PTR(cfp)): utility function VM_EP_BLOCK_PTR(cfp->ep). * vm.c, vm_eval.c, vm_insnhelper.c, vm_insnhelper.h, insns.def: apply above changes. * cont.c: ditto. * eval.c, eval_intern.h: ditto. * proc.c: ditto. * thread.c: ditto. * vm_dump.c: ditto. * vm_exec.h: fix function name (on vm debug mode). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-06-11 07:14:59 +04:00
(th)->passed_block = rb_vm_control_frame_block_ptr(th->cfp); \
(th)->cfp->flag |= VM_FRAME_FLAG_PASSED; \
} while (0)
#define PASS_PASSED_BLOCK() do { \
rb_thread_t * const __th__ = GET_THREAD(); \
PASS_PASSED_BLOCK_TH(__th__); \
} while (0)
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
#include <stdio.h>
#include <setjmp.h>
#ifdef __APPLE__
# ifdef HAVE_CRT_EXTERNS_H
# include <crt_externs.h>
# else
# include "missing/crt_externs.h"
# endif
#endif
#ifndef HAVE_STRING_H
char *strrchr(const char *, const char);
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_NET_SOCKET_H
#include <net/socket.h>
#endif
#define ruby_setjmp(env) RUBY_SETJMP(env)
#define ruby_longjmp(env,val) RUBY_LONGJMP((env),(val))
#ifdef __CYGWIN__
# ifndef _setjmp
int _setjmp(jmp_buf);
# endif
# ifndef _longjmp
NORETURN(void _longjmp(jmp_buf, int));
# endif
#endif
#include <sys/types.h>
#include <signal.h>
#include <errno.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
/*
Solaris sys/select.h switches select to select_large_fdset to support larger
file descriptors if FD_SETSIZE is larger than 1024 on 32bit environment.
But Ruby doesn't change FD_SETSIZE because fd_set is allocated dynamically.
So following definition is required to use select_large_fdset.
*/
#ifdef HAVE_SELECT_LARGE_FDSET
#define select(n, r, w, e, t) select_large_fdset((n), (r), (w), (e), (t))
extern int select_large_fdset(int, fd_set *, fd_set *, fd_set *, struct timeval *);
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/stat.h>
#ifdef _MSC_VER
#define SAVE_ROOT_JMPBUF_BEFORE_STMT \
__try {
#define SAVE_ROOT_JMPBUF_AFTER_STMT \
} \
__except (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW ? \
(rb_thread_raised_set(GET_THREAD(), RAISED_STACKOVERFLOW), \
raise(SIGSEGV), \
EXCEPTION_EXECUTE_HANDLER) : \
EXCEPTION_CONTINUE_SEARCH) { \
/* never reaches here */ \
}
#elif defined(__MINGW32__)
LONG WINAPI rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *);
#define SAVE_ROOT_JMPBUF_BEFORE_STMT \
do { \
PVOID _handler = AddVectoredExceptionHandler(1, rb_w32_stack_overflow_handler);
#define SAVE_ROOT_JMPBUF_AFTER_STMT \
RemoveVectoredExceptionHandler(_handler); \
} while (0);
#else
#define SAVE_ROOT_JMPBUF_BEFORE_STMT
#define SAVE_ROOT_JMPBUF_AFTER_STMT
#endif
#define SAVE_ROOT_JMPBUF(th, stmt) do \
if (ruby_setjmp((th)->root_jmpbuf) == 0) { \
SAVE_ROOT_JMPBUF_BEFORE_STMT \
stmt; \
SAVE_ROOT_JMPBUF_AFTER_STMT \
} \
else { \
rb_fiber_start(); \
} while (0)
#define TH_PUSH_TAG(th) do { \
rb_thread_t * const _th = (th); \
struct rb_vm_tag _tag; \
_tag.tag = 0; \
_tag.prev = _th->tag;
#define TH_POP_TAG() \
_th->tag = _tag.prev; \
} while (0)
#define TH_TMPPOP_TAG() \
_th->tag = _tag.prev
#define TH_REPUSH_TAG() (void)(_th->tag = &_tag)
#define PUSH_TAG() TH_PUSH_TAG(GET_THREAD())
#define POP_TAG() TH_POP_TAG()
#if defined __GNUC__ && __GNUC__ == 4 && (__GNUC_MINOR__ >= 6 && __GNUC_MINOR__ <= 8)
# define VAR_FROM_MEMORY(var) __extension__(*(__typeof__(var) volatile *)&(var))
# define VAR_INITIALIZED(var) ((var) = VAR_FROM_MEMORY(var))
#else
# define VAR_FROM_MEMORY(var) (var)
# define VAR_INITIALIZED(var) ((void)&(var))
#endif
/* clear th->state, and return the value */
static inline int
rb_threadptr_tag_state(rb_thread_t *th)
{
int state = th->state;
th->state = 0;
return state;
}
NORETURN(static inline void rb_threadptr_tag_jump(rb_thread_t *, int));
static inline void
rb_threadptr_tag_jump(rb_thread_t *th, int st)
{
th->state = st;
ruby_longjmp(th->tag->buf, 1);
}
/*
setjmp() in assignment expression rhs is undefined behavior
[ISO/IEC 9899:1999] 7.13.1.1
*/
#define TH_EXEC_TAG() \
(ruby_setjmp(_tag.buf) ? rb_threadptr_tag_state(VAR_FROM_MEMORY(_th)) : (TH_REPUSH_TAG(), 0))
#define EXEC_TAG() \
TH_EXEC_TAG()
#define TH_JUMP_TAG(th, st) rb_threadptr_tag_jump(th, st)
#define JUMP_TAG(st) TH_JUMP_TAG(GET_THREAD(), (st))
#define INTERNAL_EXCEPTION_P(exc) FIXNUM_P(exc)
enum ruby_tag_type {
RUBY_TAG_RETURN = 0x1,
RUBY_TAG_BREAK = 0x2,
RUBY_TAG_NEXT = 0x3,
RUBY_TAG_RETRY = 0x4,
RUBY_TAG_REDO = 0x5,
RUBY_TAG_RAISE = 0x6,
RUBY_TAG_THROW = 0x7,
RUBY_TAG_FATAL = 0x8,
RUBY_TAG_MASK = 0xf
};
#define TAG_RETURN RUBY_TAG_RETURN
#define TAG_BREAK RUBY_TAG_BREAK
#define TAG_NEXT RUBY_TAG_NEXT
#define TAG_RETRY RUBY_TAG_RETRY
#define TAG_REDO RUBY_TAG_REDO
#define TAG_RAISE RUBY_TAG_RAISE
#define TAG_THROW RUBY_TAG_THROW
#define TAG_FATAL RUBY_TAG_FATAL
#define TAG_MASK RUBY_TAG_MASK
#define SCOPE_TEST(f) (CREF_VISI(rb_vm_cref()) & (f))
#define SCOPE_CHECK(f) (CREF_VISI(rb_vm_cref()) == (f))
#define SCOPE_SET(f) (CREF_VISI_SET(rb_vm_cref(), (f)))
/* CREF operators */
#define NODE_FL_CREF_PUSHED_BY_EVAL_ (((VALUE)1)<<15)
#define NODE_FL_CREF_OMOD_SHARED_ (((VALUE)1)<<16)
static inline VALUE
CREF_CLASS(const rb_cref_t *cref)
{
return cref->klass;
}
static inline void
CREF_CLASS_SET(rb_cref_t *cref, VALUE klass)
{
RB_OBJ_WRITE(cref, &cref->klass, klass);
}
static inline rb_cref_t *
CREF_NEXT(const rb_cref_t *cref)
{
return cref->next;
}
static inline void
CREF_NEXT_SET(rb_cref_t *cref, const rb_cref_t *next_cref)
{
RB_OBJ_WRITE(cref, &cref->next, next_cref);
}
static inline long
CREF_VISI(const rb_cref_t *cref)
{
return (long)cref->visi;
}
static inline void
CREF_VISI_SET(rb_cref_t *cref, long v)
{
cref->visi = v;
}
static inline VALUE
CREF_REFINEMENTS(const rb_cref_t *cref)
{
return cref->refinements;
}
static inline void
CREF_REFINEMENTS_SET(rb_cref_t *cref, VALUE refs)
{
RB_OBJ_WRITE(cref, &cref->refinements, refs);
}
static inline int
CREF_PUSHED_BY_EVAL(const rb_cref_t *cref)
{
return cref->flags & NODE_FL_CREF_PUSHED_BY_EVAL_;
}
static inline void
CREF_PUSHED_BY_EVAL_SET(rb_cref_t *cref)
{
cref->flags |= NODE_FL_CREF_PUSHED_BY_EVAL_;
}
static inline int
CREF_OMOD_SHARED(const rb_cref_t *cref)
{
return cref->flags & NODE_FL_CREF_OMOD_SHARED_;
}
static inline void
CREF_OMOD_SHARED_SET(rb_cref_t *cref)
{
cref->flags |= NODE_FL_CREF_OMOD_SHARED_;
}
static inline void
CREF_OMOD_SHARED_UNSET(rb_cref_t *cref)
{
cref->flags &= ~NODE_FL_CREF_OMOD_SHARED_;
}
void rb_thread_cleanup(void);
void rb_thread_wait_other_threads(void);
enum {
RAISED_EXCEPTION = 1,
RAISED_STACKOVERFLOW = 2,
RAISED_NOMEMORY = 4
};
int rb_threadptr_set_raised(rb_thread_t *th);
int rb_threadptr_reset_raised(rb_thread_t *th);
#define rb_thread_raised_set(th, f) ((th)->raised_flag |= (f))
#define rb_thread_raised_reset(th, f) ((th)->raised_flag &= ~(f))
#define rb_thread_raised_p(th, f) (((th)->raised_flag & (f)) != 0)
#define rb_thread_raised_clear(th) ((th)->raised_flag = 0)
VALUE rb_f_eval(int argc, const VALUE *argv, VALUE self);
VALUE rb_make_exception(int argc, const VALUE *argv);
NORETURN(void rb_method_name_error(VALUE, VALUE));
NORETURN(void rb_fiber_start(void));
NORETURN(void rb_print_undef(VALUE, ID, int));
NORETURN(void rb_print_undef_str(VALUE, VALUE));
NORETURN(void rb_print_inaccessible(VALUE, ID, int));
NORETURN(void rb_vm_localjump_error(const char *,VALUE, int));
NORETURN(void rb_vm_jump_tag_but_local_jump(int));
NORETURN(void rb_raise_method_missing(rb_thread_t *th, int argc, const VALUE *argv,
VALUE obj, int call_status));
VALUE rb_vm_make_jump_tag_but_local_jump(int state, VALUE val);
rb_cref_t *rb_vm_cref(void);
VALUE rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg, const rb_block_t *blockptr, VALUE filename);
void rb_vm_set_progname(VALUE filename);
* this commit is a result of refactoring. only renaming functions, moving definitions place, add/remove prototypes, deleting unused variables and removing yarv.h. This commit doesn't change any behavior of ruby/vm. * yarv.h, common.mk: remove yarv.h (contents are moved to yarvcore.h). * error.c, eval_intern.h: include yarvcore.h instead yarv.h * rename some functions: * debug.[ch]: debug_*() -> ruby_debug_*() * iseq.c: iseq_*() -> rb_iseq_*(), ruby_iseq_disasm() * iseq.c: node_name() -> ruby_node_name() * vm.c: yarv_check_redefinition_opt_method() -> rb_vm_check_redefinition_opt_method() * some refactoring with checking -Wall. * array.c: remove rb_ary_ptr() (unused) and remove unused local variables. * object.c: add a prototype of rb_mod_module_exec(). * eval_intern.h (ruby_cref): set it inline. * eval_load.c (rb_load), yarvcore.c: yarv_load() -> rb_load_internal(). * parse.y: add a prototype of rb_parse_in_eval() (in eval.c). * process.c: add a prototype of rb_thread_stop_timer_thread() (in thread.c). * thread.c: remove raw_gets() function (unused) and fix some format mismatch (format mismatchs have remained yet. this is todo). * thread.c (rb_thread_wait_fd_rw): fix typo on label name. * thread_pthread.ci: comment out codes with USE_THREAD_CACHE. * vm.c (rb_svar, rb_backref_get, rb_backref_get, rb_lastline_get, rb_lastline_set) : moved from yarvcore.c. * vm.c (yarv_init_redefined_flag): add a prototype and rename yarv_opt_method_table to vm_opt_method_table. * vm.c (rb_thread_eval): moved from yarvcore.c. * yarvcore.c: remove unused global variables and fix to use nsdr(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11652 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-02-07 04:25:05 +03:00
void rb_thread_terminate_all(void);
VALUE rb_vm_top_self();
VALUE rb_vm_cbase(void);
#ifndef CharNext /* defined as CharNext[AW] on Windows. */
# ifdef HAVE_MBLEN
# define CharNext(p) ((p) + mblen((p), RUBY_MBCHAR_MAXSIZE))
# else
# define CharNext(p) ((p) + 1)
# endif
#endif
#if defined DOSISH || defined __CYGWIN__
static inline void
translit_char(char *p, int from, int to)
{
while (*p) {
if ((unsigned char)*p == from)
*p = to;
p = CharNext(p);
}
}
#endif
#endif /* RUBY_EVAL_INTERN_H */