13#include "internal/coverage.h"
14#include "internal/gc.h"
19#define ISEQ_MAJOR_VERSION ((unsigned int)ruby_api_version[0])
20#define ISEQ_MINOR_VERSION ((unsigned int)ruby_api_version[1])
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)
28#define ISEQ_LVAR_STATE_BITS 2
29#define ISEQ_LVAR_STATES_PER_BYTE (CHAR_BIT / ISEQ_LVAR_STATE_BITS)
30#define ISEQ_LVAR_STATES_BUFLEN(size) roomof(size, ISEQ_LVAR_STATES_PER_BYTE)
31#define ISEQ_LVAR_STATES_EMBED_P(size) (ISEQ_LVAR_STATES_BUFLEN(size) <= sizeof(uint8_t *))
32STATIC_ASSERT(lvar_state_fits_in_iseq_lvar_state_bits, lvar_reassigned < (1 << ISEQ_LVAR_STATE_BITS));
34static inline enum lvar_state
35iseq_lvar_state_get(
const uint8_t *buf,
unsigned int i)
37 const unsigned int shift = (i % ISEQ_LVAR_STATES_PER_BYTE) * ISEQ_LVAR_STATE_BITS;
38 return (
enum lvar_state)((buf[i / ISEQ_LVAR_STATES_PER_BYTE] >> shift) & ((1 << ISEQ_LVAR_STATE_BITS) - 1));
42iseq_lvar_state_set(uint8_t *buf,
unsigned int i,
enum lvar_state state)
44 uint8_t *
const byte = &buf[i / ISEQ_LVAR_STATES_PER_BYTE];
45 const unsigned int shift = (i % ISEQ_LVAR_STATES_PER_BYTE) * ISEQ_LVAR_STATE_BITS;
46 *
byte = (*
byte & ~(((1 << ISEQ_LVAR_STATE_BITS) - 1) << shift)) | ((uint8_t)state << shift);
51#define rb_iseq_t rb_iseq_t
54extern const ID rb_iseq_shared_exc_local_tbl[];
59 return body->local_table_size > 0 && body->local_table != rb_iseq_shared_exc_local_tbl;
62static inline uint8_t *
65 if (ISEQ_LVAR_STATES_EMBED_P(body->local_table_size)) {
66 return (uint8_t *)body->lvar_states.single;
69 return body->lvar_states.list;
77ISEQ_BODY_VARIABLE(
const rb_iseq_t *iseq)
79 return ISEQ_BODY(iseq)->variable;
84ISEQ_BODY_VARIABLE_SCRIPT_LINES(
const rb_iseq_t *iseq)
87 return v ? v->script_lines :
Qnil;
91ISEQ_BODY_VARIABLE_COVERAGE(
const rb_iseq_t *iseq)
94 return v ? v->coverage :
Qfalse;
98ISEQ_BODY_VARIABLE_PC2BRANCHINDEX(
const rb_iseq_t *iseq)
101 return v ? v->pc2branchindex :
Qfalse;
104static inline rb_snum_t
105ISEQ_BODY_VARIABLE_FLIP_CNT(
const rb_iseq_t *iseq)
108 return v ? v->flip_count : 0;
112ISEQ_BODY_VARIABLE_ORIGINAL_ISEQ(
const rb_iseq_t *iseq)
115 return v ? v->original_iseq : NULL;
118static inline const rb_iseq_t *
119ISEQ_BODY_VARIABLE_MANDATORY_ONLY_ISEQ(
const rb_iseq_t *iseq)
122 return v ? v->mandatory_only_iseq : NULL;
126void rb_iseq_coverage_set(rb_iseq_t *iseq,
VALUE cov);
127void rb_iseq_pc2branchindex_set(rb_iseq_t *iseq,
VALUE h);
128rb_snum_t rb_iseq_flip_cnt_increment(
const rb_iseq_t *iseq);
129void rb_iseq_mandatory_only_iseq_set(rb_iseq_t *iseq,
const rb_iseq_t *mandatory_only_iseq);
132#define ISEQ_VARIABLE(iseq) ISEQ_BODY_VARIABLE(iseq)
133#define ISEQ_SCRIPT_LINES(iseq) ISEQ_BODY_VARIABLE_SCRIPT_LINES(iseq)
134#define ISEQ_COVERAGE(iseq) ISEQ_BODY_VARIABLE_COVERAGE(iseq)
135#define ISEQ_COVERAGE_SET(iseq, cov) rb_iseq_coverage_set(iseq, cov)
136#define ISEQ_LINE_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_LINES)
137#define ISEQ_BRANCH_COVERAGE(iseq) RARRAY_AREF(ISEQ_COVERAGE(iseq), COVERAGE_INDEX_BRANCHES)
139#define ISEQ_PC2BRANCHINDEX(iseq) ISEQ_BODY_VARIABLE_PC2BRANCHINDEX(iseq)
140#define ISEQ_PC2BRANCHINDEX_SET(iseq,h) rb_iseq_pc2branchindex_set(iseq, h)
142#define ISEQ_FLIP_CNT(iseq) ISEQ_BODY_VARIABLE_FLIP_CNT(iseq)
143#define ISEQ_FLIP_CNT_INCREMENT(iseq) rb_iseq_flip_cnt_increment(iseq)
144#define ISEQ_ORIGINAL_ISEQ(iseq) ISEQ_BODY_VARIABLE_ORIGINAL_ISEQ(iseq)
145#define ISEQ_MANDATORY_ONLY_ISEQ(iseq) ISEQ_BODY_VARIABLE_MANDATORY_ONLY_ISEQ(iseq)
147#define ISEQ_FROZEN_STRING_LITERAL_ENABLED 1
148#define ISEQ_FROZEN_STRING_LITERAL_DISABLED 0
149#define ISEQ_FROZEN_STRING_LITERAL_UNSET -1
152ISEQ_ORIGINAL_ISEQ_CLEAR(
const rb_iseq_t *iseq)
156 VALUE *ptr = v->original_iseq;
158 v->original_iseq = NULL;
159 SIZED_FREE_N(ptr, ISEQ_BODY(iseq)->iseq_size);
164#define ISEQ_TRACE_EVENTS (RUBY_EVENT_LINE | \
170 RUBY_EVENT_C_RETURN | \
171 RUBY_EVENT_B_CALL | \
172 RUBY_EVENT_B_RETURN | \
173 RUBY_EVENT_RESCUE | \
174 RUBY_EVENT_COVERAGE_LINE| \
175 RUBY_EVENT_COVERAGE_BRANCH)
177#define ISEQ_NOT_LOADED_YET IMEMO_FL_USER1
178#define ISEQ_USE_COMPILE_DATA IMEMO_FL_USER2
180#define ISEQ_TRANSLATED IMEMO_FL_USER3
182#define ISEQ_REFINED_COPY IMEMO_FL_USER4
184#define ISEQ_EXECUTABLE_P(iseq) (FL_TEST_RAW(((VALUE)iseq), ISEQ_NOT_LOADED_YET | ISEQ_USE_COMPILE_DATA) == 0)
188 const VALUE err_info;
189 const VALUE catch_table_ary;
192 unsigned int iseq_size;
194 bool is_single_mark_bit;
205 const rb_iseq_t *current_block;
221 unsigned int ci_index;
222 const rb_compile_option_t *option;
225 const NODE *root_node;
233ISEQ_COMPILE_DATA(
const rb_iseq_t *iseq)
235 if (iseq->flags & ISEQ_USE_COMPILE_DATA) {
236 return iseq->aux.compile_data;
244ISEQ_COMPILE_DATA_ALLOC(rb_iseq_t *iseq)
247 iseq->flags |= ISEQ_USE_COMPILE_DATA;
251ISEQ_COMPILE_DATA_CLEAR(rb_iseq_t *iseq)
253 iseq->flags &= ~ISEQ_USE_COMPILE_DATA;
254 iseq->aux.compile_data = NULL;
257static inline rb_iseq_t *
258iseq_imemo_alloc(
void)
260 rb_iseq_t *iseq = SHAREABLE_IMEMO_NEW(rb_iseq_t, imemo_iseq, 0);
263 memset((
char *)iseq +
sizeof(
VALUE), 0,
sizeof(rb_iseq_t) -
sizeof(
VALUE));
268VALUE rb_iseq_ibf_dump(
const rb_iseq_t *iseq,
VALUE opt);
269void rb_ibf_load_iseq_complete(rb_iseq_t *iseq);
271const rb_iseq_t *rb_iseq_ibf_load_bytes(
const char *cstr,
size_t,
VALUE,
VALUE);
273const rb_iseq_t *rb_iseq_dup_with_independent_caches(
const rb_iseq_t *iseq);
274void rb_iseq_init_trace(rb_iseq_t *iseq);
275int 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);
276int rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq,
VALUE tpval, rb_ractor_t *r);
277const rb_iseq_t *rb_iseq_load_iseq(
VALUE fname);
278const rb_iseq_t *rb_iseq_compile_iseq(
VALUE str,
VALUE fname);
279int rb_iseq_opt_frozen_string_literal(
void);
280rb_hook_list_t *rb_iseq_local_hooks(
const rb_iseq_t *iseq, rb_ractor_t *r,
bool create);
285int rb_vm_insn_addr2opcode(
const void *addr);
287RUBY_SYMBOL_EXPORT_BEGIN
290VALUE rb_iseq_compile_node(rb_iseq_t *iseq,
const NODE *node);
292VALUE *rb_iseq_original_iseq(
const rb_iseq_t *iseq);
293void rb_iseq_build_from_ary(rb_iseq_t *iseq,
VALUE misc,
299VALUE rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc);
300unsigned int rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos);
301#ifdef USE_ISEQ_NODE_ID
302int rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos);
304void rb_iseq_trace_set(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events);
306struct succ_index_table *rb_iseq_insns_info_encode_positions(
unsigned int iseq_size,
const unsigned int *positions,
unsigned int size);
309VALUE rb_iseqw_new(
const rb_iseq_t *iseq);
310const rb_iseq_t *rb_iseqw_to_iseq(
VALUE iseqw);
312VALUE rb_iseq_absolute_path(
const rb_iseq_t *iseq);
313int rb_iseq_from_eval_p(
const rb_iseq_t *iseq);
314VALUE rb_iseq_type(
const rb_iseq_t *iseq);
315VALUE rb_iseq_label(
const rb_iseq_t *iseq);
316VALUE rb_iseq_base_label(
const rb_iseq_t *iseq);
317VALUE rb_iseq_first_lineno(
const rb_iseq_t *iseq);
318VALUE rb_iseq_method_name(
const rb_iseq_t *iseq);
319void rb_iseq_code_location(
const rb_iseq_t *iseq,
int *first_lineno,
int *first_column,
int *last_lineno,
int *last_column);
321void rb_iseq_remove_coverage_all(
void);
324const rb_iseq_t *rb_method_iseq(
VALUE body);
325const rb_iseq_t *rb_proc_get_iseq(
VALUE proc,
int *is_proc);
328 unsigned int inline_const_cache: 1;
329 unsigned int peephole_optimization: 1;
330 unsigned int tailcall_optimization: 1;
331 unsigned int specialized_instruction: 1;
332 unsigned int operands_unification: 1;
333 unsigned int instructions_unification: 1;
334 signed int frozen_string_literal: 2;
335 unsigned int debug_frozen_string_literal: 1;
336 unsigned int coverage_enabled: 1;
358 CATCH_TYPE_RESCUE =
INT2FIX(1),
359 CATCH_TYPE_ENSURE =
INT2FIX(2),
367 enum rb_catch_type type;
376RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_BEGIN()
380} RBIMPL_ATTR_PACKED_STRUCT_UNALIGNED_END();
383iseq_catch_table_bytes(
int n)
387 catch_table_entries_max = (INT_MAX - offsetof(
struct iseq_catch_table, entries)) / catch_table_entry_size
389 if (n > catch_table_entries_max) rb_fatal(
"too large iseq_catch_table - %d", n);
391 n * catch_table_entry_size);
394#define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE (512)
400 char buff[FLEX_ARY_LEN];
426VALUE rb_iseq_defined_string(
enum defined_type
type);
429VALUE rb_iseq_local_variables(
const rb_iseq_t *iseq);
431attr_index_t rb_estimate_iv_count(
VALUE klass,
const rb_iseq_t * initialize_iseq);
433void rb_free_encoded_insn_data(
void);
435RUBY_SYMBOL_EXPORT_END
#define RUBY_EXTERN
Declaration of externally visible global variables.
uint32_t rb_event_flag_t
Represents event(s).
#define INT2FIX
Old name of RB_INT2FIX.
#define ZALLOC
Old name of RB_ZALLOC.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
const int ruby_api_version[3]
API versions, in { major, minor, teeny } order.
VALUE type(ANYARGS)
ANYARGS-ed function type.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.