Ruby 3.2.4p170 (2024-04-23 revision af471c0e0127eea0cafa6f308c0425bbfab0acf5)
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 "vm_core.h"
15
17#define ISEQ_MAJOR_VERSION ((unsigned int)ruby_api_version[0])
18#define ISEQ_MINOR_VERSION ((unsigned int)ruby_api_version[1])
19
20#define ISEQ_MBITS_SIZE sizeof(iseq_bits_t)
21#define ISEQ_MBITS_BITLENGTH (ISEQ_MBITS_SIZE * CHAR_BIT)
22#define ISEQ_MBITS_SET(buf, i) (buf[(i) / ISEQ_MBITS_BITLENGTH] |= ((iseq_bits_t)1 << ((i) % ISEQ_MBITS_BITLENGTH)))
23#define ISEQ_MBITS_SET_P(buf, i) ((buf[(i) / ISEQ_MBITS_BITLENGTH] >> ((i) % ISEQ_MBITS_BITLENGTH)) & 0x1)
24#define ISEQ_MBITS_BUFLEN(size) roomof(size, ISEQ_MBITS_BITLENGTH)
25
26#ifndef USE_ISEQ_NODE_ID
27#define USE_ISEQ_NODE_ID 1
28#endif
29
30#ifndef rb_iseq_t
31typedef struct rb_iseq_struct rb_iseq_t;
32#define rb_iseq_t rb_iseq_t
33#endif
34typedef void (*rb_iseq_callback)(const rb_iseq_t *, void *);
35
36extern const ID rb_iseq_shared_exc_local_tbl[];
37
38#define ISEQ_COVERAGE(iseq) ISEQ_BODY(iseq)->variable.coverage
39#define ISEQ_COVERAGE_SET(iseq, cov) RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->variable.coverage, cov)
40#define ISEQ_LINE_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_LINES)
41#define ISEQ_BRANCH_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_BRANCHES)
42
43#define ISEQ_PC2BRANCHINDEX(iseq) ISEQ_BODY(iseq)->variable.pc2branchindex
44#define ISEQ_PC2BRANCHINDEX_SET(iseq, h) RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->variable.pc2branchindex, h)
45
46#define ISEQ_FLIP_CNT(iseq) ISEQ_BODY(iseq)->variable.flip_count
47
48static inline rb_snum_t
49ISEQ_FLIP_CNT_INCREMENT(const rb_iseq_t *iseq)
50{
51 rb_snum_t cnt = ISEQ_BODY(iseq)->variable.flip_count;
52 ISEQ_BODY(iseq)->variable.flip_count += 1;
53 return cnt;
54}
55
56static inline VALUE *
57ISEQ_ORIGINAL_ISEQ(const rb_iseq_t *iseq)
58{
59 return ISEQ_BODY(iseq)->variable.original_iseq;
60}
61
62static inline void
63ISEQ_ORIGINAL_ISEQ_CLEAR(const rb_iseq_t *iseq)
64{
65 void *ptr = ISEQ_BODY(iseq)->variable.original_iseq;
66 ISEQ_BODY(iseq)->variable.original_iseq = NULL;
67 if (ptr) {
68 ruby_xfree(ptr);
69 }
70}
71
72static inline VALUE *
73ISEQ_ORIGINAL_ISEQ_ALLOC(const rb_iseq_t *iseq, long size)
74{
75 return ISEQ_BODY(iseq)->variable.original_iseq =
76 ALLOC_N(VALUE, size);
77}
78
79#define ISEQ_TRACE_EVENTS (RUBY_EVENT_LINE | \
80 RUBY_EVENT_CLASS | \
81 RUBY_EVENT_END | \
82 RUBY_EVENT_CALL | \
83 RUBY_EVENT_RETURN| \
84 RUBY_EVENT_C_CALL| \
85 RUBY_EVENT_C_RETURN| \
86 RUBY_EVENT_B_CALL| \
87 RUBY_EVENT_B_RETURN| \
88 RUBY_EVENT_COVERAGE_LINE| \
89 RUBY_EVENT_COVERAGE_BRANCH)
90
91#define ISEQ_NOT_LOADED_YET IMEMO_FL_USER1
92#define ISEQ_USE_COMPILE_DATA IMEMO_FL_USER2
93#define ISEQ_TRANSLATED IMEMO_FL_USER3
94
95#define ISEQ_EXECUTABLE_P(iseq) (FL_TEST_RAW(((VALUE)iseq), ISEQ_NOT_LOADED_YET | ISEQ_USE_COMPILE_DATA) == 0)
96
98 /* GC is needed */
99 const VALUE err_info;
100 const VALUE catch_table_ary; /* Array */
101
102 /* GC is not needed */
103 struct iseq_label_data *start_label;
104 struct iseq_label_data *end_label;
105 struct iseq_label_data *redo_label;
106 const rb_iseq_t *current_block;
107 struct iseq_compile_data_ensure_node_stack *ensure_node_stack;
108 struct {
109 struct iseq_compile_data_storage *storage_head;
110 struct iseq_compile_data_storage *storage_current;
111 } node;
112 struct {
113 struct iseq_compile_data_storage *storage_head;
114 struct iseq_compile_data_storage *storage_current;
115 } insn;
116 bool in_rescue;
117 int loopval_popped; /* used by NODE_BREAK */
118 int last_line;
119 int label_no;
120 int node_level;
121 int isolated_depth;
122 unsigned int ci_index;
123 unsigned int ic_index;
124 const rb_compile_option_t *option;
125 struct rb_id_table *ivar_cache_table;
126 const struct rb_builtin_function *builtin_function_table;
127 const NODE *root_node;
128#if OPT_SUPPORT_JOKE
129 st_table *labels_table;
130#endif
131};
132
133static inline struct iseq_compile_data *
134ISEQ_COMPILE_DATA(const rb_iseq_t *iseq)
135{
136 if (iseq->flags & ISEQ_USE_COMPILE_DATA) {
137 return iseq->aux.compile_data;
138 }
139 else {
140 return NULL;
141 }
142}
143
144static inline void
145ISEQ_COMPILE_DATA_ALLOC(rb_iseq_t *iseq)
146{
147 iseq->aux.compile_data = ZALLOC(struct iseq_compile_data);
148 iseq->flags |= ISEQ_USE_COMPILE_DATA;
149}
150
151static inline void
152ISEQ_COMPILE_DATA_CLEAR(rb_iseq_t *iseq)
153{
154 iseq->flags &= ~ISEQ_USE_COMPILE_DATA;
155 iseq->aux.compile_data = NULL;
156}
157
158static inline rb_iseq_t *
159iseq_imemo_alloc(void)
160{
161 return (rb_iseq_t *)rb_imemo_new(imemo_iseq, 0, 0, 0, 0);
162}
163
164VALUE rb_iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt);
165void rb_ibf_load_iseq_complete(rb_iseq_t *iseq);
166const rb_iseq_t *rb_iseq_ibf_load(VALUE str);
167const rb_iseq_t *rb_iseq_ibf_load_bytes(const char *cstr, size_t);
168VALUE rb_iseq_ibf_load_extra_data(VALUE str);
169void rb_iseq_init_trace(rb_iseq_t *iseq);
170int 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);
171int rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval);
172const rb_iseq_t *rb_iseq_load_iseq(VALUE fname);
173
174#if VM_INSN_INFO_TABLE_IMPL == 2
175unsigned int *rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body);
176#endif
177
178int rb_vm_insn_addr2opcode(const void *addr);
179
180RUBY_SYMBOL_EXPORT_BEGIN
181
182/* compile.c */
183VALUE rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node);
184VALUE rb_iseq_compile_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_callback_callback_func * ifunc);
185VALUE *rb_iseq_original_iseq(const rb_iseq_t *iseq);
186void rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc,
187 VALUE locals, VALUE args,
188 VALUE exception, VALUE body);
189void rb_iseq_mark_insn_storage(struct iseq_compile_data_storage *arena);
190
191VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt);
192VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc);
193unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos);
194#ifdef USE_ISEQ_NODE_ID
195int rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos);
196#endif
197void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events);
198void rb_iseq_trace_set_all(rb_event_flag_t turnon_events);
199void rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq);
200
201struct rb_iseq_constant_body *rb_iseq_constant_body_alloc(void);
202VALUE rb_iseqw_new(const rb_iseq_t *iseq);
203const rb_iseq_t *rb_iseqw_to_iseq(VALUE iseqw);
204
205VALUE rb_iseq_absolute_path(const rb_iseq_t *iseq); /* obsolete */
206int rb_iseq_from_eval_p(const rb_iseq_t *iseq);
207VALUE rb_iseq_type(const rb_iseq_t *iseq);
208VALUE rb_iseq_label(const rb_iseq_t *iseq);
209VALUE rb_iseq_base_label(const rb_iseq_t *iseq);
210VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq);
211VALUE rb_iseq_method_name(const rb_iseq_t *iseq);
212void rb_iseq_code_location(const rb_iseq_t *iseq, int *first_lineno, int *first_column, int *last_lineno, int *last_column);
213
214void rb_iseq_remove_coverage_all(void);
215
216/* proc.c */
217const rb_iseq_t *rb_method_iseq(VALUE body);
218const rb_iseq_t *rb_proc_get_iseq(VALUE proc, int *is_proc);
219
221 unsigned int inline_const_cache: 1;
222 unsigned int peephole_optimization: 1;
223 unsigned int tailcall_optimization: 1;
224 unsigned int specialized_instruction: 1;
225 unsigned int operands_unification: 1;
226 unsigned int instructions_unification: 1;
227 unsigned int stack_caching: 1;
228 unsigned int frozen_string_literal: 1;
229 unsigned int debug_frozen_string_literal: 1;
230 unsigned int coverage_enabled: 1;
231 int debug_level;
232};
233
235 int line_no;
236#ifdef USE_ISEQ_NODE_ID
237 int node_id;
238#endif
239 rb_event_flag_t events;
240};
241
242/*
243 * iseq type:
244 * CATCH_TYPE_RESCUE, CATCH_TYPE_ENSURE:
245 * use iseq as continuation.
246 *
247 * CATCH_TYPE_BREAK (iter):
248 * use iseq as key.
249 *
250 * CATCH_TYPE_BREAK (while), CATCH_TYPE_RETRY,
251 * CATCH_TYPE_REDO, CATCH_TYPE_NEXT:
252 * NULL.
253 */
254enum rb_catch_type {
255 CATCH_TYPE_RESCUE = INT2FIX(1),
256 CATCH_TYPE_ENSURE = INT2FIX(2),
257 CATCH_TYPE_RETRY = INT2FIX(3),
258 CATCH_TYPE_BREAK = INT2FIX(4),
259 CATCH_TYPE_REDO = INT2FIX(5),
260 CATCH_TYPE_NEXT = INT2FIX(6)
261};
262
264 enum rb_catch_type type;
265 rb_iseq_t *iseq;
266
267 unsigned int start;
268 unsigned int end;
269 unsigned int cont;
270 unsigned int sp;
271};
272
273PACKED_STRUCT_UNALIGNED(struct iseq_catch_table {
274 unsigned int size;
275 struct iseq_catch_table_entry entries[FLEX_ARY_LEN];
276});
277
278static inline int
279iseq_catch_table_bytes(int n)
280{
281 enum {
282 catch_table_entry_size = sizeof(struct iseq_catch_table_entry),
283 catch_table_entries_max = (INT_MAX - offsetof(struct iseq_catch_table, entries)) / catch_table_entry_size
284 };
285 if (n > catch_table_entries_max) rb_fatal("too large iseq_catch_table - %d", n);
286 return (int)(offsetof(struct iseq_catch_table, entries) +
287 n * catch_table_entry_size);
288}
289
290#define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE (512)
291
293 struct iseq_compile_data_storage *next;
294 unsigned int pos;
295 unsigned int size;
296 char buff[FLEX_ARY_LEN];
297};
298
299/* defined? */
300
301enum defined_type {
302 DEFINED_NOT_DEFINED,
303 DEFINED_NIL = 1,
304 DEFINED_IVAR,
305 DEFINED_LVAR,
306 DEFINED_GVAR,
307 DEFINED_CVAR,
308 DEFINED_CONST,
309 DEFINED_METHOD,
310 DEFINED_YIELD,
311 DEFINED_ZSUPER,
312 DEFINED_SELF,
313 DEFINED_TRUE,
314 DEFINED_FALSE,
315 DEFINED_ASGN,
316 DEFINED_EXPR,
317 DEFINED_REF,
318 DEFINED_FUNC,
319 DEFINED_CONST_FROM
320};
321
322VALUE rb_iseq_defined_string(enum defined_type type);
323
324/* vm.c */
325VALUE rb_iseq_local_variables(const rb_iseq_t *iseq);
326
327RUBY_SYMBOL_EXPORT_END
328
329#endif /* RUBY_ISEQ_H */
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:47
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:103
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:396
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:393
void rb_fatal(const char *fmt,...)
Raises the unsung "fatal" exception.
Definition error.c:3201
const int ruby_api_version[3]
API versions, in { major, minor, teeny } order.
Definition version.c:51
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition node.h:156
Definition iseq.h:263
Definition iseq.h:234
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