Ruby  2.1.10p492(2016-04-01revision54464)
vm_exec.c
Go to the documentation of this file.
1 /* -*-c-*- */
2 /**********************************************************************
3 
4  vm_exec.c -
5 
6  $Author: ko1 $
7 
8  Copyright (C) 2004-2007 Koichi Sasada
9 
10 **********************************************************************/
11 
12 #include <math.h>
13 
14 #if VM_COLLECT_USAGE_DETAILS
15 static void vm_analysis_insn(int insn);
16 #endif
17 
18 #if VMDEBUG > 0
19 #define DECL_SC_REG(type, r, reg) register type reg_##r
20 
21 #elif defined(__GNUC__) && defined(__x86_64__)
22 #define DECL_SC_REG(type, r, reg) register type reg_##r __asm__("r" reg)
23 
24 #elif defined(__GNUC__) && defined(__i386__)
25 #define DECL_SC_REG(type, r, reg) register type reg_##r __asm__("e" reg)
26 
27 #else
28 #define DECL_SC_REG(type, r, reg) register type reg_##r
29 #endif
30 /* #define DECL_SC_REG(r, reg) VALUE reg_##r */
31 
32 #if VM_DEBUG_STACKOVERFLOW
33 NORETURN(static void vm_stack_overflow_for_insn(void));
34 static void
35 vm_stack_overflow_for_insn(void)
36 {
37  rb_bug("CHECK_VM_STACK_OVERFLOW_FOR_INSN: should not overflow here. "
38  "Please contact ruby-core/dev with your (a part of) script. "
39  "This check will be removed soon.");
40 }
41 #endif
42 
43 #if !OPT_CALL_THREADED_CODE
44 static VALUE
46 {
47 
48 #if OPT_STACK_CACHING
49 #if 0
50 #elif __GNUC__ && __x86_64__
51  DECL_SC_REG(VALUE, a, "12");
52  DECL_SC_REG(VALUE, b, "13");
53 #else
54  register VALUE reg_a;
55  register VALUE reg_b;
56 #endif
57 #endif
58 
59 #if defined(__GNUC__) && defined(__i386__)
60  DECL_SC_REG(VALUE *, pc, "di");
61  DECL_SC_REG(rb_control_frame_t *, cfp, "si");
62 #define USE_MACHINE_REGS 1
63 
64 #elif defined(__GNUC__) && defined(__x86_64__)
65  DECL_SC_REG(VALUE *, pc, "14");
66  DECL_SC_REG(rb_control_frame_t *, cfp, "15");
67 #define USE_MACHINE_REGS 1
68 
69 #else
70  register rb_control_frame_t *reg_cfp;
71  VALUE *reg_pc;
72 #endif
73 
74 #if USE_MACHINE_REGS
75 
76 #undef RESTORE_REGS
77 #define RESTORE_REGS() \
78 { \
79  REG_CFP = th->cfp; \
80  reg_pc = reg_cfp->pc; \
81 }
82 
83 #undef REG_PC
84 #define REG_PC reg_pc
85 #undef GET_PC
86 #define GET_PC() (reg_pc)
87 #undef SET_PC
88 #define SET_PC(x) (reg_cfp->pc = REG_PC = (x))
89 #endif
90 
91 #if OPT_TOKEN_THREADED_CODE || OPT_DIRECT_THREADED_CODE
92 #include "vmtc.inc"
93  if (UNLIKELY(th == 0)) {
94  return (VALUE)insns_address_table;
95  }
96 #endif
97  reg_cfp = th->cfp;
98  reg_pc = reg_cfp->pc;
99 
100 #if OPT_STACK_CACHING
101  reg_a = initial;
102  reg_b = 0;
103 #endif
104 
105  first:
106  INSN_DISPATCH();
107 /*****************/
108  #include "vm.inc"
109 /*****************/
111 
112  /* unreachable */
113  rb_bug("vm_eval: unreachable");
114  goto first;
115 }
116 
117 const void **
119 {
120  return (const void **)vm_exec_core(0, 0);
121 }
122 
123 #else /* OPT_CALL_THREADED_CODE */
124 
125 #include "vm.inc"
126 #include "vmtc.inc"
127 
128 const void **
130 {
131  return (const void **)insns_address_table;
132 }
133 
134 static VALUE
135 vm_exec_core(rb_thread_t *th, VALUE initial)
136 {
137  register rb_control_frame_t *reg_cfp = th->cfp;
138 
139  while (1) {
140  reg_cfp = ((rb_insn_func_t) (*GET_PC()))(th, reg_cfp);
141 
142  if (UNLIKELY(reg_cfp == 0)) {
143  break;
144  }
145  }
146 
147  if (th->retval != Qundef) {
148  VALUE ret = th->retval;
149  th->retval = Qundef;
150  return ret;
151  }
152  else {
153  VALUE err = th->errinfo;
154  th->errinfo = Qnil;
155  return err;
156  }
157 }
158 #endif
rb_control_frame_t * cfp
Definition: vm_core.h:531
#define END_INSNS_DISPATCH()
Definition: vm_exec.h:149
void rb_bug(const char *fmt,...)
Definition: error.c:327
static VALUE VALUE th
Definition: tcltklib.c:2944
const void ** rb_vm_get_insns_address_table(void)
Definition: vm_exec.c:118
int ret
Definition: tcltklib.c:285
Real * a
Definition: bigdecimal.c:1198
#define NORETURN(x)
Definition: ruby.h:33
#define Qnil
Definition: enum.c:67
#define GET_PC()
#define UNLIKELY(x)
Definition: vm_core.h:109
#define DECL_SC_REG(type, r, reg)
Definition: vm_exec.c:28
int err
Definition: win32.c:114
#define INSN_DISPATCH()
Definition: vm_exec.h:145
Real * b
Definition: bigdecimal.c:1198
static VALUE vm_exec_core(rb_thread_t *th, VALUE initial)
Definition: vm_exec.c:45
#define Qundef
rb_control_frame_t *FUNC_FASTCALL rb_insn_func_t(rb_thread_t *, rb_control_frame_t *)
Definition: vm_core.h:797
unsigned long VALUE
Definition: ripper.y:88