Ruby 4.0.10p0 (2026-11-19 revision a704b7cd95a42cc0cf5f307137bf3aac0285b915)
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
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
76#define ISEQ_TRACE_EVENTS (RUBY_EVENT_LINE | \
77 RUBY_EVENT_CLASS | \
78 RUBY_EVENT_END | \
79 RUBY_EVENT_CALL | \
80 RUBY_EVENT_RETURN| \
81 RUBY_EVENT_C_CALL| \
82 RUBY_EVENT_C_RETURN | \
83 RUBY_EVENT_B_CALL | \
84 RUBY_EVENT_B_RETURN | \
85 RUBY_EVENT_RESCUE | \
86 RUBY_EVENT_COVERAGE_LINE| \
87 RUBY_EVENT_COVERAGE_BRANCH)
88
89#define ISEQ_NOT_LOADED_YET IMEMO_FL_USER1
90#define ISEQ_USE_COMPILE_DATA IMEMO_FL_USER2
91#define ISEQ_TRANSLATED IMEMO_FL_USER3
92
93#define ISEQ_EXECUTABLE_P(iseq) (FL_TEST_RAW(((VALUE)iseq), ISEQ_NOT_LOADED_YET | ISEQ_USE_COMPILE_DATA) == 0)
94
96 /* GC is needed */
97 const VALUE err_info;
98 const VALUE catch_table_ary; /* Array */
99
100 /* Mirror fields from ISEQ_BODY so they are accessible during iseq setup */
101 unsigned int iseq_size;
102 VALUE *iseq_encoded; /* half-encoded iseq (insn addr and operands) */
103 bool is_single_mark_bit; /* identifies whether mark bits are single or a list */
104
105 union {
106 iseq_bits_t * list; /* Find references for GC */
107 iseq_bits_t single;
108 } mark_bits;
109
110 /* GC is not needed */
111 struct iseq_label_data *start_label;
112 struct iseq_label_data *end_label;
113 struct iseq_label_data *redo_label;
114 const rb_iseq_t *current_block;
115 struct iseq_compile_data_ensure_node_stack *ensure_node_stack;
116 struct {
117 struct iseq_compile_data_storage *storage_head;
118 struct iseq_compile_data_storage *storage_current;
119 } node;
120 struct {
121 struct iseq_compile_data_storage *storage_head;
122 struct iseq_compile_data_storage *storage_current;
123 } insn;
124 bool in_rescue;
125 bool in_masgn;
126 int loopval_popped; /* used by NODE_BREAK */
127 int last_line;
128 int label_no;
129 int node_level;
130 int isolated_depth;
131 unsigned int ci_index;
132 unsigned int ic_index;
133 const rb_compile_option_t *option;
134 struct rb_id_table *ivar_cache_table;
135 const struct rb_builtin_function *builtin_function_table;
136 const NODE *root_node;
137 bool catch_except_p; // If a frame of this ISeq may catch exception, set true.
138#if OPT_SUPPORT_JOKE
139 st_table *labels_table;
140#endif
141};
142
143static inline struct iseq_compile_data *
144ISEQ_COMPILE_DATA(const rb_iseq_t *iseq)
145{
146 if (iseq->flags & ISEQ_USE_COMPILE_DATA) {
147 return iseq->aux.compile_data;
148 }
149 else {
150 return NULL;
151 }
152}
153
154static inline void
155ISEQ_COMPILE_DATA_ALLOC(rb_iseq_t *iseq)
156{
157 iseq->aux.compile_data = ZALLOC(struct iseq_compile_data);
158 iseq->flags |= ISEQ_USE_COMPILE_DATA;
159}
160
161static inline void
162ISEQ_COMPILE_DATA_CLEAR(rb_iseq_t *iseq)
163{
164 iseq->flags &= ~ISEQ_USE_COMPILE_DATA;
165 iseq->aux.compile_data = NULL;
166}
167
168static inline rb_iseq_t *
169iseq_imemo_alloc(void)
170{
171 rb_iseq_t *iseq = SHAREABLE_IMEMO_NEW(rb_iseq_t, imemo_iseq, 0);
172
173 // Clear out the whole iseq except for the flags.
174 memset((char *)iseq + sizeof(VALUE), 0, sizeof(rb_iseq_t) - sizeof(VALUE));
175
176 return iseq;
177}
178
179VALUE rb_iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt);
180void rb_ibf_load_iseq_complete(rb_iseq_t *iseq);
181const rb_iseq_t *rb_iseq_ibf_load(VALUE str);
182const rb_iseq_t *rb_iseq_ibf_load_bytes(const char *cstr, size_t);
183VALUE rb_iseq_ibf_load_extra_data(VALUE str);
184void rb_iseq_init_trace(rb_iseq_t *iseq);
185int 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);
186int rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval, rb_ractor_t *r);
187const rb_iseq_t *rb_iseq_load_iseq(VALUE fname);
188const rb_iseq_t *rb_iseq_compile_iseq(VALUE str, VALUE fname);
189int rb_iseq_opt_frozen_string_literal(void);
190rb_hook_list_t *rb_iseq_local_hooks(const rb_iseq_t *iseq, rb_ractor_t *r, bool create);
191
192
193#if VM_INSN_INFO_TABLE_IMPL == 2
194unsigned int *rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body);
195#endif
196
197int rb_vm_insn_addr2opcode(const void *addr);
198
199RUBY_SYMBOL_EXPORT_BEGIN
200
201/* compile.c */
202VALUE rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node);
203VALUE rb_iseq_compile_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_callback_callback_func * ifunc);
204VALUE *rb_iseq_original_iseq(const rb_iseq_t *iseq);
205void rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc,
206 VALUE locals, VALUE args,
207 VALUE exception, VALUE body);
208void rb_iseq_mark_and_move_insn_storage(struct iseq_compile_data_storage *arena);
209
210VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt);
211VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc);
212unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos);
213#ifdef USE_ISEQ_NODE_ID
214int rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos);
215#endif
216void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events);
217void rb_iseq_trace_set_all(rb_event_flag_t turnon_events);
218void rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq);
219
220struct rb_iseq_constant_body *rb_iseq_constant_body_alloc(void);
221VALUE rb_iseqw_new(const rb_iseq_t *iseq);
222const rb_iseq_t *rb_iseqw_to_iseq(VALUE iseqw);
223
224VALUE rb_iseq_absolute_path(const rb_iseq_t *iseq); /* obsolete */
225int rb_iseq_from_eval_p(const rb_iseq_t *iseq);
226VALUE rb_iseq_type(const rb_iseq_t *iseq);
227VALUE rb_iseq_label(const rb_iseq_t *iseq);
228VALUE rb_iseq_base_label(const rb_iseq_t *iseq);
229VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq);
230VALUE rb_iseq_method_name(const rb_iseq_t *iseq);
231void rb_iseq_code_location(const rb_iseq_t *iseq, int *first_lineno, int *first_column, int *last_lineno, int *last_column);
232
233void rb_iseq_remove_coverage_all(void);
234
235/* proc.c */
236const rb_iseq_t *rb_method_iseq(VALUE body);
237const rb_iseq_t *rb_proc_get_iseq(VALUE proc, int *is_proc);
238
240 unsigned int inline_const_cache: 1;
241 unsigned int peephole_optimization: 1;
242 unsigned int tailcall_optimization: 1;
243 unsigned int specialized_instruction: 1;
244 unsigned int operands_unification: 1;
245 unsigned int instructions_unification: 1;
246 signed int frozen_string_literal: 2; /* -1: not specified, 0: false, 1: true */
247 unsigned int debug_frozen_string_literal: 1;
248 unsigned int coverage_enabled: 1;
249 int debug_level;
250};
251
253 int line_no;
254#ifdef USE_ISEQ_NODE_ID
255 int node_id;
256#endif
257 rb_event_flag_t events;
258};
259
260/*
261 * iseq type:
262 * CATCH_TYPE_RESCUE, CATCH_TYPE_ENSURE:
263 * use iseq as continuation.
264 *
265 * CATCH_TYPE_BREAK (iter):
266 * use iseq as key.
267 *
268 * CATCH_TYPE_BREAK (while), CATCH_TYPE_RETRY,
269 * CATCH_TYPE_REDO, CATCH_TYPE_NEXT:
270 * NULL.
271 */
272enum rb_catch_type {
273 CATCH_TYPE_RESCUE = INT2FIX(1),
274 CATCH_TYPE_ENSURE = INT2FIX(2),
275 CATCH_TYPE_RETRY = INT2FIX(3),
276 CATCH_TYPE_BREAK = INT2FIX(4),
277 CATCH_TYPE_REDO = INT2FIX(5),
278 CATCH_TYPE_NEXT = INT2FIX(6)
279};
280
282 enum rb_catch_type type;
283 rb_iseq_t *iseq;
284
285 unsigned int start;
286 unsigned int end;
287 unsigned int cont;
288 unsigned int sp;
289};
290
291RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_BEGIN()
293 unsigned int size;
294 struct iseq_catch_table_entry entries[FLEX_ARY_LEN];
295} RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_END();
296
297static inline int
298iseq_catch_table_bytes(int n)
299{
300 enum {
301 catch_table_entry_size = sizeof(struct iseq_catch_table_entry),
302 catch_table_entries_max = (INT_MAX - offsetof(struct iseq_catch_table, entries)) / catch_table_entry_size
303 };
304 if (n > catch_table_entries_max) rb_fatal("too large iseq_catch_table - %d", n);
305 return (int)(offsetof(struct iseq_catch_table, entries) +
306 n * catch_table_entry_size);
307}
308
309#define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE (512)
310
312 struct iseq_compile_data_storage *next;
313 unsigned int pos;
314 unsigned int size;
315 char buff[FLEX_ARY_LEN];
316};
317
318/* defined? */
319
320enum defined_type {
321 DEFINED_NOT_DEFINED,
322 DEFINED_NIL = 1,
323 DEFINED_IVAR,
324 DEFINED_LVAR,
325 DEFINED_GVAR,
326 DEFINED_CVAR,
327 DEFINED_CONST,
328 DEFINED_METHOD,
329 DEFINED_YIELD,
330 DEFINED_ZSUPER,
331 DEFINED_SELF,
332 DEFINED_TRUE,
333 DEFINED_FALSE,
334 DEFINED_ASGN,
335 DEFINED_EXPR,
336 DEFINED_REF,
337 DEFINED_FUNC,
338 DEFINED_CONST_FROM
339};
340
341VALUE rb_iseq_defined_string(enum defined_type type);
342
343/* vm.c */
344VALUE rb_iseq_local_variables(const rb_iseq_t *iseq);
345
346attr_index_t rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq);
347
348void rb_free_encoded_insn_data(void);
349
350RUBY_SYMBOL_EXPORT_END
351
352#endif /* RUBY_ISEQ_H */
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:108
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:402
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
Definition io.h:2
const int ruby_api_version[3]
API versions, in { major, minor, teeny } order.
Definition version.c:55
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition iseq.h:281
Definition iseq.h:252
Definition st.h:79
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40