Ruby: iseq.h Source File

Ruby 3.3.5p100 (2024-09-03 revision ef084cc8f4958c1b6e4ead99136631bef6d8ddba)

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
50static inline rb_snum_t
51ISEQ_FLIP_CNT_INCREMENT(const rb_iseq_t *iseq)
52{
53 rb_snum_t cnt = ISEQ_BODY(iseq)->variable.flip_count;
54 ISEQ_BODY(iseq)->variable.flip_count += 1;
55 return cnt;
56}
57
58static inline VALUE *
59ISEQ_ORIGINAL_ISEQ(const rb_iseq_t *iseq)
60{
61 return ISEQ_BODY(iseq)->variable.original_iseq;
62}
63
64static inline void
65ISEQ_ORIGINAL_ISEQ_CLEAR(const rb_iseq_t *iseq)
66{
67 void *ptr = ISEQ_BODY(iseq)->variable.original_iseq;
68 ISEQ_BODY(iseq)->variable.original_iseq = NULL;
69 if (ptr) {
70 ruby_xfree(ptr);
71 }
72}
73
74static inline VALUE *
75ISEQ_ORIGINAL_ISEQ_ALLOC(const rb_iseq_t *iseq, long size)
76{
77 return ISEQ_BODY(iseq)->variable.original_iseq =
78 ALLOC_N(VALUE, size);
79}
80
81#define ISEQ_TRACE_EVENTS (RUBY_EVENT_LINE | \
82 RUBY_EVENT_CLASS | \
83 RUBY_EVENT_END | \
84 RUBY_EVENT_CALL | \
85 RUBY_EVENT_RETURN| \
86 RUBY_EVENT_C_CALL| \
87 RUBY_EVENT_C_RETURN | \
88 RUBY_EVENT_B_CALL | \
89 RUBY_EVENT_B_RETURN | \
90 RUBY_EVENT_RESCUE | \
91 RUBY_EVENT_COVERAGE_LINE| \
92 RUBY_EVENT_COVERAGE_BRANCH)
93
94#define ISEQ_NOT_LOADED_YET IMEMO_FL_USER1
95#define ISEQ_USE_COMPILE_DATA IMEMO_FL_USER2
96#define ISEQ_TRANSLATED IMEMO_FL_USER3
97
98#define ISEQ_EXECUTABLE_P(iseq) (FL_TEST_RAW(((VALUE)iseq), ISEQ_NOT_LOADED_YET | ISEQ_USE_COMPILE_DATA) == 0)
99
100 struct iseq_compile_data {
101 /* GC is needed */
102 const VALUE err_info;
103 const VALUE catch_table_ary; /* Array */
104
105 /* GC is not needed */
106 struct iseq_label_data *start_label;
107 struct iseq_label_data *end_label;
108 struct iseq_label_data *redo_label;
109 const rb_iseq_t *current_block;
110 struct iseq_compile_data_ensure_node_stack *ensure_node_stack;
111 struct {
112 struct iseq_compile_data_storage *storage_head;
113 struct iseq_compile_data_storage *storage_current;
114 } node;
115 struct {
116 struct iseq_compile_data_storage *storage_head;
117 struct iseq_compile_data_storage *storage_current;
118 } insn;
119 bool in_rescue;
120 int loopval_popped; /* used by NODE_BREAK */
121 int last_line;
122 int label_no;
123 int node_level;
124 int isolated_depth;
125 unsigned int ci_index;
126 unsigned int ic_index;
127 const rb_compile_option_t *option;
128 struct rb_id_table *ivar_cache_table;
129 const struct rb_builtin_function *builtin_function_table;
130 const NODE *root_node;
131 bool catch_except_p; // If a frame of this ISeq may catch exception, set true.
132#if OPT_SUPPORT_JOKE
133 st_table *labels_table;
134#endif
135};
136
137static inline struct iseq_compile_data *
138ISEQ_COMPILE_DATA(const rb_iseq_t *iseq)
139{
140 if (iseq->flags & ISEQ_USE_COMPILE_DATA) {
141 return iseq->aux.compile_data;
142 }
143 else {
144 return NULL;
145 }
146}
147
148static inline void
149ISEQ_COMPILE_DATA_ALLOC(rb_iseq_t *iseq)
150{
151 iseq->aux.compile_data = ZALLOC(struct iseq_compile_data);
152 iseq->flags |= ISEQ_USE_COMPILE_DATA;
153}
154
155static inline void
156ISEQ_COMPILE_DATA_CLEAR(rb_iseq_t *iseq)
157{
158 iseq->flags &= ~ISEQ_USE_COMPILE_DATA;
159 iseq->aux.compile_data = NULL;
160}
161
162static inline rb_iseq_t *
163iseq_imemo_alloc(void)
164{
165 return (rb_iseq_t *)rb_imemo_new(imemo_iseq, 0, 0, 0, 0);
166}
167
168 VALUE rb_iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt);
169void rb_ibf_load_iseq_complete(rb_iseq_t *iseq);
170const rb_iseq_t *rb_iseq_ibf_load(VALUE str);
171const rb_iseq_t *rb_iseq_ibf_load_bytes(const char *cstr, size_t);
172 VALUE rb_iseq_ibf_load_extra_data(VALUE str);
173void rb_iseq_init_trace(rb_iseq_t *iseq);
174int 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);
175int rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval);
176const rb_iseq_t *rb_iseq_load_iseq(VALUE fname);
177 rb_iseq_t * rb_iseq_new_main_prism(pm_string_t *input, pm_options_t *options, VALUE path);
178
179#if VM_INSN_INFO_TABLE_IMPL == 2
180unsigned int *rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body);
181#endif
182
183int rb_vm_insn_addr2opcode(const void *addr);
184
185RUBY_SYMBOL_EXPORT_BEGIN
186
187/* compile.c */
188 VALUE rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node);
189 VALUE rb_iseq_compile_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_callback_callback_func * ifunc);
190 VALUE *rb_iseq_original_iseq(const rb_iseq_t *iseq);
191void rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc,
192 VALUE locals, VALUE args,
193 VALUE exception, VALUE body);
194void rb_iseq_mark_and_pin_insn_storage(struct iseq_compile_data_storage *arena);
195
196 VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt);
197 VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc);
198unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos);
199#ifdef USE_ISEQ_NODE_ID
200int rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos);
201#endif
202void rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events);
203void rb_iseq_trace_set_all(rb_event_flag_t turnon_events);
204void rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq);
205
206struct rb_iseq_constant_body *rb_iseq_constant_body_alloc(void);
207 VALUE rb_iseqw_new(const rb_iseq_t *iseq);
208const rb_iseq_t *rb_iseqw_to_iseq(VALUE iseqw);
209
210 VALUE rb_iseq_absolute_path(const rb_iseq_t *iseq); /* obsolete */
211int rb_iseq_from_eval_p(const rb_iseq_t *iseq);
212 VALUE rb_iseq_type(const rb_iseq_t *iseq);
213 VALUE rb_iseq_label(const rb_iseq_t *iseq);
214 VALUE rb_iseq_base_label(const rb_iseq_t *iseq);
215 VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq);
216 VALUE rb_iseq_method_name(const rb_iseq_t *iseq);
217void rb_iseq_code_location(const rb_iseq_t *iseq, int *first_lineno, int *first_column, int *last_lineno, int *last_column);
218
219void rb_iseq_remove_coverage_all(void);
220
221/* proc.c */
222const rb_iseq_t *rb_method_iseq(VALUE body);
223const rb_iseq_t *rb_proc_get_iseq(VALUE proc, int *is_proc);
224
225 struct rb_compile_option_struct {
226 unsigned int inline_const_cache: 1;
227 unsigned int peephole_optimization: 1;
228 unsigned int tailcall_optimization: 1;
229 unsigned int specialized_instruction: 1;
230 unsigned int operands_unification: 1;
231 unsigned int instructions_unification: 1;
232 unsigned int frozen_string_literal: 1;
233 unsigned int debug_frozen_string_literal: 1;
234 unsigned int coverage_enabled: 1;
235 int debug_level;
236};
237
238 struct iseq_insn_info_entry {
239 int line_no;
240#ifdef USE_ISEQ_NODE_ID
241 int node_id;
242#endif
243 rb_event_flag_t events;
244};
245
246/*
247 * iseq type:
248 * CATCH_TYPE_RESCUE, CATCH_TYPE_ENSURE:
249 * use iseq as continuation.
250 *
251 * CATCH_TYPE_BREAK (iter):
252 * use iseq as key.
253 *
254 * CATCH_TYPE_BREAK (while), CATCH_TYPE_RETRY,
255 * CATCH_TYPE_REDO, CATCH_TYPE_NEXT:
256 * NULL.
257 */
258enum rb_catch_type {
259 CATCH_TYPE_RESCUE = INT2FIX(1),
260 CATCH_TYPE_ENSURE = INT2FIX(2),
261 CATCH_TYPE_RETRY = INT2FIX(3),
262 CATCH_TYPE_BREAK = INT2FIX(4),
263 CATCH_TYPE_REDO = INT2FIX(5),
264 CATCH_TYPE_NEXT = INT2FIX(6)
265};
266
267 struct iseq_catch_table_entry {
268 enum rb_catch_type type;
269 rb_iseq_t *iseq;
270
271 unsigned int start;
272 unsigned int end;
273 unsigned int cont;
274 unsigned int sp;
275};
276
277RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_BEGIN()
278struct iseq_catch_table {
279 unsigned int size;
280 struct iseq_catch_table_entry entries[FLEX_ARY_LEN];
281} RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_END();
282
283static inline int
284iseq_catch_table_bytes(int n)
285{
286 enum {
287 catch_table_entry_size = sizeof(struct iseq_catch_table_entry),
288 catch_table_entries_max = (INT_MAX - offsetof(struct iseq_catch_table, entries)) / catch_table_entry_size
289 };
290 if (n > catch_table_entries_max) rb_fatal("too large iseq_catch_table - %d", n);
291 return (int)(offsetof(struct iseq_catch_table, entries) +
292 n * catch_table_entry_size);
293}
294
295#define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE (512)
296
297 struct iseq_compile_data_storage {
298 struct iseq_compile_data_storage *next;
299 unsigned int pos;
300 unsigned int size;
301 char buff[FLEX_ARY_LEN];
302};
303
304/* defined? */
305
306enum defined_type {
307 DEFINED_NOT_DEFINED,
308 DEFINED_NIL = 1,
309 DEFINED_IVAR,
310 DEFINED_LVAR,
311 DEFINED_GVAR,
312 DEFINED_CVAR,
313 DEFINED_CONST,
314 DEFINED_METHOD,
315 DEFINED_YIELD,
316 DEFINED_ZSUPER,
317 DEFINED_SELF,
318 DEFINED_TRUE,
319 DEFINED_FALSE,
320 DEFINED_ASGN,
321 DEFINED_EXPR,
322 DEFINED_REF,
323 DEFINED_FUNC,
324 DEFINED_CONST_FROM
325};
326
327 VALUE rb_iseq_defined_string(enum defined_type type);
328
329/* vm.c */
330 VALUE rb_iseq_local_variables(const rb_iseq_t *iseq);
331
332attr_index_t rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq);
333
334void rb_free_encoded_insn_data(void);
335
336RUBY_SYMBOL_EXPORT_END
337
338#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:396
ALLOC_N
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:393
ruby_api_version
const int ruby_api_version[3]
API versions, in { major, minor, teeny } order.
Definition version.c:46
ruby::backward::cxxanyargs::type
VALUE type(ANYARGS)
ANYARGS-ed function type.
Definition cxxanyargs.hpp:56
RNode
Definition rubyparser.h:166
iseq_catch_table_entry
Definition iseq.h:267
iseq_catch_table
Definition iseq.h:278
iseq_compile_data_ensure_node_stack
Definition compile.c:121
iseq_compile_data_storage
Definition iseq.h:297
iseq_compile_data
Definition iseq.h:100
iseq_insn_info_entry
Definition iseq.h:238
iseq_label_data
Definition compile.c:78
pm_options_t
The options that can be passed to the parser.
Definition options.h:30
pm_string_t
A generic string type that can have various ownership semantics.
Definition pm_string.h:30
rb_builtin_function
Definition builtin.h:6
rb_compile_option_struct
Definition iseq.h:225
rb_id_table
Definition id_table.c:41
rb_iseq_constant_body
Definition vm_core.h:376
rb_iseq_new_with_callback_callback_func
Definition vm_core.h:1193
rb_iseq_struct
Definition vm_core.h:534
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