Ruby 4.1.0dev (2026-09-07 revision b57404b461ba8bf34e802d86b0db78388216e182)
vm_exec.h (b57404b461ba8bf34e802d86b0db78388216e182)
1#ifndef RUBY_VM_EXEC_H
2#define RUBY_VM_EXEC_H
3/**********************************************************************
4
5 vm.h -
6
7 $Author$
8 created at: 04/01/01 16:56:59 JST
9
10 Copyright (C) 2004-2007 Koichi Sasada
11
12**********************************************************************/
13
14typedef long OFFSET;
15typedef unsigned long lindex_t;
16typedef VALUE GENTRY;
17typedef rb_iseq_t *ISEQ;
18
19#if VMDEBUG > 0
20#define debugs printf
21#define DEBUG_ENTER_INSN(insn) \
22 rb_vmdebug_debug_print_pre(ec, GET_CFP(), GET_PC());
23
24#define SC_REGS()
25
26#define DEBUG_END_INSN() \
27 rb_vmdebug_debug_print_post(ec, GET_CFP() SC_REGS());
28
29#else
30
31#define debugs
32#define DEBUG_ENTER_INSN(insn)
33#define DEBUG_END_INSN()
34#endif
35
36#define throwdebug if(0)ruby_debug_printf
37/* #define throwdebug ruby_debug_printf */
38
39/************************************************/
40#if defined(DISPATCH_XXX)
41error !
42/************************************************/
43#elif OPT_CALL_THREADED_CODE
44
45#define LABEL(x) insn_func_##x
46#define ELABEL(x)
47#define LABEL_PTR(x) &LABEL(x)
48
49#define INSN_ENTRY(insn) \
50 static rb_control_frame_t * \
51 FUNC_FASTCALL(LABEL(insn))(rb_execution_context_t *ec, rb_control_frame_t *reg_cfp) {
52
53#define END_INSN(insn) return reg_cfp;}
54
55#define NEXT_INSN() return reg_cfp;
56
57#define START_OF_ORIGINAL_INSN(x) /* ignore */
58#define DISPATCH_ORIGINAL_INSN(x) return LABEL(x)(ec, reg_cfp);
59
60/************************************************/
61#elif OPT_TOKEN_THREADED_CODE || OPT_DIRECT_THREADED_CODE
62/* threaded code with gcc */
63
64#define LABEL(x) INSN_LABEL_##x
65#define ELABEL(x) INSN_ELABEL_##x
66#define LABEL_PTR(x) RB_GNUC_EXTENSION(&&LABEL(x))
67
68#define INSN_ENTRY_SIG(insn) \
69 if (0) { \
70 VALUE path = rb_iseq_path(CFP_ISEQ(reg_cfp)); \
71 ruby_debug_printf("exec: %s@(%"PRIdPTRDIFF", %"PRIdPTRDIFF")@%.*s:%u\n", #insn, \
72 (reg_pc - ISEQ_BODY(CFP_ISEQ(reg_cfp))->iseq_encoded), \
73 (reg_cfp->pc - ISEQ_BODY(CFP_ISEQ(reg_cfp))->iseq_encoded), \
74 RSTRING_LENINT(path), RSTRING_PTR(path), \
75 rb_iseq_line_no(CFP_ISEQ(reg_cfp), reg_pc - ISEQ_BODY(CFP_ISEQ(reg_cfp))->iseq_encoded)); \
76 }
77
78#define INSN_DISPATCH_SIG(insn)
79
80#define INSN_ENTRY(insn) \
81 LABEL(insn): \
82 INSN_ENTRY_SIG(insn); \
83
84/**********************************/
85#if OPT_DIRECT_THREADED_CODE
86
87/* for GCC 3.4.x */
88#define TC_DISPATCH(insn) \
89 INSN_DISPATCH_SIG(insn); \
90 RB_GNUC_EXTENSION_BLOCK(goto *(void const *)GET_CURRENT_INSN()); \
91 ;
92
93#else
94/* token threaded code */
95
96/* dispatcher */
97#define TC_DISPATCH(insn) \
98 INSN_DISPATCH_SIG(insn); \
99 RB_GNUC_EXTENSION_BLOCK(goto *insns_address_table[GET_CURRENT_INSN()]); \
100 rb_bug("tc error");
101
102#endif /* OPT_DIRECT_THREADED_CODE */
103
104#define END_INSN(insn) \
105 DEBUG_END_INSN(); \
106 TC_DISPATCH(insn);
107
108#define INSN_DISPATCH() \
109 TC_DISPATCH(__START__) \
110 {
111
112#define END_INSNS_DISPATCH() \
113 rb_bug("unknown insn: %"PRIdVALUE, GET_CURRENT_INSN()); \
114 } /* end of while loop */ \
115
116#define NEXT_INSN() TC_DISPATCH(__NEXT_INSN__)
117
118/************************************************/
119#else /* no threaded code */
120/* most common method */
121
122#define INSN_ENTRY(insn) \
123case BIN(insn):
124
125#define END_INSN(insn) \
126 DEBUG_END_INSN(); \
127 break;
128
129#define INSN_DISPATCH() \
130 while (1) { \
131 switch (GET_CURRENT_INSN()) {
132
133#define END_INSNS_DISPATCH() \
134default: \
135 SDR(); \
136 rb_bug("unknown insn: %ld", GET_CURRENT_INSN()); \
137 } /* end of switch */ \
138 } /* end of while loop */ \
139
140#define NEXT_INSN() goto first
141
142#endif
143
144#ifndef START_OF_ORIGINAL_INSN
145#define START_OF_ORIGINAL_INSN(x) if (0) goto start_of_##x; start_of_##x:
146#define DISPATCH_ORIGINAL_INSN(x) goto start_of_##x;
147#endif
148
149#define VM_SP_CNT(ec, sp) ((sp) - (ec)->vm_stack)
150
151#if OPT_CALL_THREADED_CODE
152#define THROW_EXCEPTION(exc) do { \
153 ec->errinfo = (VALUE)(exc); \
154 return 0; \
155} while (0)
156#else
157#define THROW_EXCEPTION(exc) return (VALUE)(exc)
158#endif
159
160// Run the interpreter from the JIT
161#define VM_EXEC(ec, val) do { \
162 if (UNDEF_P(val)) { \
163 VM_ENV_FLAGS_SET(ec->cfp->ep, VM_FRAME_FLAG_FINISH); \
164 val = vm_exec(ec); \
165 } \
166} while (0)
167
168// Run the JIT from the interpreter
169#define JIT_EXEC(ec, val) do { \
170 /* don't run tailcalls since that breaks FINISH */ \
171 if (UNDEF_P(val) && GET_CFP() != ec->cfp) { \
172 rb_zjit_func_t zjit_entry; \
173 if (rb_yjit_enabled_p) { \
174 rb_jit_func_t func = yjit_compile(ec); \
175 if (func) { \
176 val = func(ec, ec->cfp); \
177 if (ec->tag->state) THROW_EXCEPTION(val); \
178 } \
179 } \
180 else if ((zjit_entry = (rb_zjit_func_t)rb_zjit_entry)) { \
181 rb_jit_func_t func = zjit_compile(ec); \
182 if (func) { \
183 val = zjit_entry(ec, ec->cfp, func); \
184 } \
185 } \
186 } \
187} while (0)
188
189#define SCREG(r) (reg_##r)
190
191#define VM_DEBUG_STACKOVERFLOW 0
192
193#if VM_DEBUG_STACKOVERFLOW
194#define CHECK_VM_STACK_OVERFLOW_FOR_INSN CHECK_VM_STACK_OVERFLOW
195#else
196#define CHECK_VM_STACK_OVERFLOW_FOR_INSN(cfp, margin)
197#endif
198
199#define INSN_LABEL2(insn, name) INSN_LABEL_ ## insn ## _ ## name
200#define INSN_LABEL(x) INSN_LABEL2(NAME_OF_CURRENT_INSN, x)
201
202#endif /* RUBY_VM_EXEC_H */
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40