Ruby: iseq.h Source File

Ruby 4.0.5p0 (2026-05-20 revision 64336ffd0ee9e1f4c05891695a3d7b49cb709721)

iseq.h
1#ifndef RUBY_ISEQ_H
2#define RUBY_ISEQ_H 1
3/**********************************************************************
4
5 iseq.h -
6
7 $Author$
8 created at: 04/01/01 23:36:57 JST
9
10 Copyright (C) 2004-2008 Koichi Sasada
11
12**********************************************************************/
13#include "internal/gc.h"
14#include "shape.h"
15#include "vm_core.h"
16#include "prism_compile.h"
17
18 RUBY_EXTERN const int ruby_api_version[];
19#define ISEQ_MAJOR_VERSION ((unsigned int)ruby_api_version[0])
20#define ISEQ_MINOR_VERSION ((unsigned int)ruby_api_version[1])
21
22#define ISEQ_MBITS_SIZE sizeof(iseq_bits_t)
23#define ISEQ_MBITS_BITLENGTH (ISEQ_MBITS_SIZE * CHAR_BIT)
24#define ISEQ_MBITS_SET(buf, i) (buf[(i) / ISEQ_MBITS_BITLENGTH] |= ((iseq_bits_t)1 << ((i) % ISEQ_MBITS_BITLENGTH)))
25#define ISEQ_MBITS_SET_P(buf, i) ((buf[(i) / ISEQ_MBITS_BITLENGTH] >> ((i) % ISEQ_MBITS_BITLENGTH)) & 0x1)
26#define ISEQ_MBITS_BUFLEN(size) roomof(size, ISEQ_MBITS_BITLENGTH)
27
28#ifndef USE_ISEQ_NODE_ID
29#define USE_ISEQ_NODE_ID 1
30#endif
31
32#ifndef rb_iseq_t
33typedef struct rb_iseq_struct rb_iseq_t;
34#define rb_iseq_t rb_iseq_t
35#endif
36typedef void (*rb_iseq_callback)(const rb_iseq_t *, void *);
37
38extern const ID rb_iseq_shared_exc_local_tbl[];
39
40#define ISEQ_COVERAGE(iseq) ISEQ_BODY(iseq)->variable.coverage
41#define ISEQ_COVERAGE_SET(iseq, cov) RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->variable.coverage, cov)
42#define ISEQ_LINE_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_LINES)
43#define ISEQ_BRANCH_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_BRANCHES)
44
45#define ISEQ_PC2BRANCHINDEX(iseq) ISEQ_BODY(iseq)->variable.pc2branchindex
46#define ISEQ_PC2BRANCHINDEX_SET(iseq, h) RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->variable.pc2branchindex, h)
47
48#define ISEQ_FLIP_CNT(iseq) ISEQ_BODY(iseq)->variable.flip_count
49
50#define ISEQ_FROZEN_STRING_LITERAL_ENABLED 1
51#define ISEQ_FROZEN_STRING_LITERAL_DISABLED 0
52#define ISEQ_FROZEN_STRING_LITERAL_UNSET -1
53
54static inline rb_snum_t
55ISEQ_FLIP_CNT_INCREMENT(const rb_iseq_t *iseq)
56{
57 rb_snum_t cnt = ISEQ_BODY(iseq)->variable.flip_count;
58 ISEQ_BODY(iseq)->variable.flip_count += 1;
59 return cnt;
60}
61
62static inline VALUE *
63ISEQ_ORIGINAL_ISEQ(const rb_iseq_t *iseq)
64{
65 return ISEQ_BODY(iseq)->variable.original_iseq;
66}
67
68static inline void
69ISEQ_ORIGINAL_ISEQ_CLEAR(const rb_iseq_t *iseq)
70{
71 void *ptr = ISEQ_BODY(iseq)->variable.original_iseq;
72 ISEQ_BODY(iseq)->variable.original_iseq = NULL;
73 ruby_xfree(ptr);
74}
75
76static inline VALUE *
77ISEQ_ORIGINAL_ISEQ_ALLOC(const rb_iseq_t *iseq, long size)
78{
79 return ISEQ_BODY(iseq)->variable.original_iseq =
80 ALLOC_N(VALUE, size);
81}
82
83#define ISEQ_TRACE_EVENTS (RUBY_EVENT_LINE | \
84 RUBY_EVENT_CLASS | \
85 RUBY_EVENT_END | \
86 RUBY_EVENT_CALL | \
87 RUBY_EVENT_RETURN| \
88 RUBY_EVENT_C_CALL| \
89 RUBY_EVENT_C_RETURN | \
90 RUBY_EVENT_B_CALL | \
91 RUBY_EVENT_B_RETURN | \
92 RUBY_EVENT_RESCUE | \
93 RUBY_EVENT_COVERAGE_LINE| \
94 RUBY_EVENT_COVERAGE_BRANCH)
95
96#define ISEQ_NOT_LOADED_YET IMEMO_FL_USER1
97#define ISEQ_USE_COMPILE_DATA IMEMO_FL_USER2
98#define ISEQ_TRANSLATED IMEMO_FL_USER3
99
100#define ISEQ_EXECUTABLE_P(iseq) (FL_TEST_RAW(((VALUE)iseq), ISEQ_NOT_LOADED_YET | ISEQ_USE_COMPILE_DATA) == 0)
101
102 struct iseq_compile_data {
103 /* GC is needed */
104 const VALUE err_info;
105 const VALUE catch_table_ary; /* Array */
106
107 /* Mirror fields from ISEQ_BODY so they are accessible during iseq setup */
108 unsigned int iseq_size;
109 VALUE *iseq_encoded; /* half-encoded iseq (insn addr and operands) */
110 bool is_single_mark_bit; /* identifies whether mark bits are single or a list */
111
112 union {
113 iseq_bits_t * list; /* Find references for GC */
114 iseq_bits_t single;
115 } mark_bits;
116
117 /* GC is not needed */
118 struct iseq_label_data *start_label;
119 struct iseq_label_data *end_label;
120 struct iseq_label_data *redo_label;
121 const rb_iseq_t *current_block;
122 struct iseq_compile_data_ensure_node_stack *ensure_node_stack;
123 struct {
124 struct iseq_compile_data_storage *storage_head;
125 struct iseq_compile_data_storage *storage_current;
126 } node;
127 struct {
128 struct iseq_compile_data_storage *storage_head;
129 struct iseq_compile_data_storage *storage_current;
130 } insn;
131 bool in_rescue;
132 bool in_masgn;
133 int loopval_popped; /* used by NODE_BREAK */
134 int last_line;
135 int label_no;
136 int node_level;
137 int isolated_depth;
138 unsigned int ci_index;
139 unsigned int ic_index;
140 const rb_compile_option_t *option;
141 struct rb_id_table *ivar_cache_table;
142 const struct rb_builtin_function *builtin_function_table;
143 const NODE *root_node;
144 bool catch_except_p; // If a frame of this ISeq may catch exception, set true.
145#if OPT_SUPPORT_JOKE
146 st_table *labels_table;
147#endif
148};
149
150static inline struct iseq_compile_data *
151ISEQ_COMPILE_DATA(const rb_iseq_t *iseq)
152{
153 if (iseq->flags & ISEQ_USE_COMPILE_DATA) {
154 return iseq->aux.compile_data;
155 }
156 else {
157 return NULL;
158 }
159}
160
161static inline void
162ISEQ_COMPILE_DATA_ALLOC(rb_iseq_t *iseq)
163{
164 iseq->aux.compile_data = ZALLOC(struct iseq_compile_data);
165 iseq->flags |= ISEQ_USE_COMPILE_DATA;
166}
167
168static inline void
169ISEQ_COMPILE_DATA_CLEAR(rb_iseq_t *iseq)
170{
171 iseq->flags &= ~ISEQ_USE_COMPILE_DATA;
172 iseq->aux.compile_data = NULL;
173}
174
175static inline rb_iseq_t *
176iseq_imemo_alloc(void)
177{
178 rb_iseq_t *iseq = SHAREABLE_IMEMO_NEW(rb_iseq_t, imemo_iseq, 0);
179
180 // Clear out the whole iseq except for the flags.
181 memset((char *)iseq + sizeof(VALUE), 0, sizeof(rb_iseq_t) - sizeof(VALUE));
182
183 return iseq;
184}
185
186 VALUE rb_iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt);
187void rb_ibf_load_iseq_complete(rb_iseq_t *iseq);
188const rb_iseq_t *rb_iseq_ibf_load(VALUE str);
189const rb_iseq_t *rb_iseq_ibf_load_bytes(const char *cstr, size_t);
190 VALUE rb_iseq_ibf_load_extra_data(VALUE str);
191void rb_iseq_init_trace(rb_iseq_t *iseq);
192int rb_iseq_add_local_tracepoint_recursively(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line, bool target_bmethod);
193int rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval, rb_ractor_t *r);
194const rb_iseq_t *rb_iseq_load_iseq(VALUE fname);
195const rb_iseq_t *rb_iseq_compile_iseq(VALUE str, VALUE fname);
196int rb_iseq_opt_frozen_string_literal(void);
197rb_hook_list_t *rb_iseq_local_hooks(const rb_iseq_t *iseq, rb_ractor_t *r, bool create);
198
199
200#if VM_INSN_INFO_TABLE_IMPL == 2
201unsigned int *rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body);
202#endif
203
204int rb_vm_insn_addr2opcode(const void *addr);
205
206RUBY_SYMBOL_EXPORT_BEGIN
207
208/* compile.c */
209 VALUE rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node);
210 VALUE rb_iseq_compile_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_callback_callback_func * ifunc);
211 VALUE *rb_iseq_original_iseq(const rb_iseq_t *iseq);
212void rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc,
213 VALUE locals, VALUE args,
214 VALUE exception, VALUE body);
215void rb_iseq_mark_and_move_insn_storage(struct iseq_compile_data_storage *arena);
216
217 VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt);
218 VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc);
219unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos);
220#ifdef USE_ISEQ_NODE_ID
221int rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos);
222#endif
223void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events);
224void rb_iseq_trace_set_all(rb_event_flag_t turnon_events);
225void rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq);
226
227struct rb_iseq_constant_body *rb_iseq_constant_body_alloc(void);
228 VALUE rb_iseqw_new(const rb_iseq_t *iseq);
229const rb_iseq_t *rb_iseqw_to_iseq(VALUE iseqw);
230
231 VALUE rb_iseq_absolute_path(const rb_iseq_t *iseq); /* obsolete */
232int rb_iseq_from_eval_p(const rb_iseq_t *iseq);
233 VALUE rb_iseq_type(const rb_iseq_t *iseq);
234 VALUE rb_iseq_label(const rb_iseq_t *iseq);
235 VALUE rb_iseq_base_label(const rb_iseq_t *iseq);
236 VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq);
237 VALUE rb_iseq_method_name(const rb_iseq_t *iseq);
238void rb_iseq_code_location(const rb_iseq_t *iseq, int *first_lineno, int *first_column, int *last_lineno, int *last_column);
239
240void rb_iseq_remove_coverage_all(void);
241
242/* proc.c */
243const rb_iseq_t *rb_method_iseq(VALUE body);
244const rb_iseq_t *rb_proc_get_iseq(VALUE proc, int *is_proc);
245
246 struct rb_compile_option_struct {
247 unsigned int inline_const_cache: 1;
248 unsigned int peephole_optimization: 1;
249 unsigned int tailcall_optimization: 1;
250 unsigned int specialized_instruction: 1;
251 unsigned int operands_unification: 1;
252 unsigned int instructions_unification: 1;
253 signed int frozen_string_literal: 2; /* -1: not specified, 0: false, 1: true */
254 unsigned int debug_frozen_string_literal: 1;
255 unsigned int coverage_enabled: 1;
256 int debug_level;
257};
258
259 struct iseq_insn_info_entry {
260 int line_no;
261#ifdef USE_ISEQ_NODE_ID
262 int node_id;
263#endif
264 rb_event_flag_t events;
265};
266
267/*
268 * iseq type:
269 * CATCH_TYPE_RESCUE, CATCH_TYPE_ENSURE:
270 * use iseq as continuation.
271 *
272 * CATCH_TYPE_BREAK (iter):
273 * use iseq as key.
274 *
275 * CATCH_TYPE_BREAK (while), CATCH_TYPE_RETRY,
276 * CATCH_TYPE_REDO, CATCH_TYPE_NEXT:
277 * NULL.
278 */
279enum rb_catch_type {
280 CATCH_TYPE_RESCUE = INT2FIX(1),
281 CATCH_TYPE_ENSURE = INT2FIX(2),
282 CATCH_TYPE_RETRY = INT2FIX(3),
283 CATCH_TYPE_BREAK = INT2FIX(4),
284 CATCH_TYPE_REDO = INT2FIX(5),
285 CATCH_TYPE_NEXT = INT2FIX(6)
286};
287
288 struct iseq_catch_table_entry {
289 enum rb_catch_type type;
290 rb_iseq_t *iseq;
291
292 unsigned int start;
293 unsigned int end;
294 unsigned int cont;
295 unsigned int sp;
296};
297
298RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_BEGIN()
299struct iseq_catch_table {
300 unsigned int size;
301 struct iseq_catch_table_entry entries[FLEX_ARY_LEN];
302} RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_END();
303
304static inline int
305iseq_catch_table_bytes(int n)
306{
307 enum {
308 catch_table_entry_size = sizeof(struct iseq_catch_table_entry),
309 catch_table_entries_max = (INT_MAX - offsetof(struct iseq_catch_table, entries)) / catch_table_entry_size
310 };
311 if (n > catch_table_entries_max) rb_fatal("too large iseq_catch_table - %d", n);
312 return (int)(offsetof(struct iseq_catch_table, entries) +
313 n * catch_table_entry_size);
314}
315
316#define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE (512)
317
318 struct iseq_compile_data_storage {
319 struct iseq_compile_data_storage *next;
320 unsigned int pos;
321 unsigned int size;
322 char buff[FLEX_ARY_LEN];
323};
324
325/* defined? */
326
327enum defined_type {
328 DEFINED_NOT_DEFINED,
329 DEFINED_NIL = 1,
330 DEFINED_IVAR,
331 DEFINED_LVAR,
332 DEFINED_GVAR,
333 DEFINED_CVAR,
334 DEFINED_CONST,
335 DEFINED_METHOD,
336 DEFINED_YIELD,
337 DEFINED_ZSUPER,
338 DEFINED_SELF,
339 DEFINED_TRUE,
340 DEFINED_FALSE,
341 DEFINED_ASGN,
342 DEFINED_EXPR,
343 DEFINED_REF,
344 DEFINED_FUNC,
345 DEFINED_CONST_FROM
346};
347
348 VALUE rb_iseq_defined_string(enum defined_type type);
349
350/* vm.c */
351 VALUE rb_iseq_local_variables(const rb_iseq_t *iseq);
352
353attr_index_t rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq);
354
355void rb_free_encoded_insn_data(void);
356
357RUBY_SYMBOL_EXPORT_END
358
359#endif /* RUBY_ISEQ_H */
RUBY_EXTERN
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
rb_event_flag_t
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
INT2FIX
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
ZALLOC
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
ALLOC_N
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:399
ruby_api_version
const int ruby_api_version[3]
API versions, in { major, minor, teeny } order.
Definition version.c:55
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
iseq_catch_table_entry
Definition iseq.h:288
iseq_catch_table
Definition iseq.h:299
iseq_compile_data_ensure_node_stack
Definition compile.c:120
iseq_compile_data_storage
Definition iseq.h:318
iseq_compile_data
Definition iseq.h:102
iseq_insn_info_entry
Definition iseq.h:259
iseq_label_data
Definition compile.c:77
rb_builtin_function
Definition builtin.h:6
rb_compile_option_struct
Definition iseq.h:246
rb_id_table
Definition id_table.c:41
rb_iseq_constant_body
Definition vm_core.h:405
rb_iseq_new_with_callback_callback_func
Definition vm_core.h:1252
rb_iseq_struct
Definition vm_core.h:572
st_table
Definition st.h:79
ID
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
VALUE
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40