Skip to content

Commit ce99e50

Browse files
Move catch_except_p to compile_data
The `catch_except_p` flag is used for communicating between parent and child iseq's that a throw instruction was emitted. So for example if a child iseq has a throw in it and the parent wants to catch the throw, we use this flag to communicate to the parent iseq that a throw instruction was emitted. This flag is only useful at compile time, it only impacts the compilation process so it seems to be fine to move it from the iseq body to the compile_data struct. Co-authored-by: Aaron Patterson <[email protected]>
1 parent b457109 commit ce99e50

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

compile.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,11 +1355,14 @@ new_child_iseq_with_callback(rb_iseq_t *iseq, const struct rb_iseq_new_with_call
13551355
}
13561356

13571357
static void
1358-
set_catch_except_p(struct rb_iseq_constant_body *body)
1358+
set_catch_except_p(rb_iseq_t *iseq)
13591359
{
1360-
body->catch_except_p = true;
1361-
if (body->parent_iseq != NULL) {
1362-
set_catch_except_p(ISEQ_BODY(body->parent_iseq));
1360+
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
1361+
ISEQ_COMPILE_DATA(iseq)->catch_except_p = true;
1362+
if (ISEQ_BODY(iseq)->parent_iseq != NULL) {
1363+
if (ISEQ_COMPILE_DATA(ISEQ_BODY(iseq)->parent_iseq)) {
1364+
set_catch_except_p((rb_iseq_t *) ISEQ_BODY(iseq)->parent_iseq);
1365+
}
13631366
}
13641367
}
13651368

@@ -1371,7 +1374,7 @@ set_catch_except_p(struct rb_iseq_constant_body *body)
13711374
So this function sets true for limited ISeqs with break/next/redo catch table entries
13721375
whose child ISeq would really raise an exception. */
13731376
static void
1374-
update_catch_except_flags(struct rb_iseq_constant_body *body)
1377+
update_catch_except_flags(rb_iseq_t *iseq, struct rb_iseq_constant_body *body)
13751378
{
13761379
unsigned int pos;
13771380
size_t i;
@@ -1384,7 +1387,7 @@ update_catch_except_flags(struct rb_iseq_constant_body *body)
13841387
while (pos < body->iseq_size) {
13851388
insn = rb_vm_insn_decode(body->iseq_encoded[pos]);
13861389
if (insn == BIN(throw)) {
1387-
set_catch_except_p(body);
1390+
set_catch_except_p(iseq);
13881391
break;
13891392
}
13901393
pos += insn_len(insn);
@@ -1399,7 +1402,8 @@ update_catch_except_flags(struct rb_iseq_constant_body *body)
13991402
if (entry->type != CATCH_TYPE_BREAK
14001403
&& entry->type != CATCH_TYPE_NEXT
14011404
&& entry->type != CATCH_TYPE_REDO) {
1402-
body->catch_except_p = true;
1405+
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
1406+
ISEQ_COMPILE_DATA(iseq)->catch_except_p = true;
14031407
break;
14041408
}
14051409
}
@@ -1496,10 +1500,12 @@ iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
14961500
if (!rb_iseq_translate_threaded_code(iseq)) return COMPILE_NG;
14971501

14981502
debugs("[compile step 6 (update_catch_except_flags)] \n");
1499-
update_catch_except_flags(ISEQ_BODY(iseq));
1503+
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
1504+
update_catch_except_flags(iseq, ISEQ_BODY(iseq));
15001505

15011506
debugs("[compile step 6.1 (remove unused catch tables)] \n");
1502-
if (!ISEQ_BODY(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
1507+
RUBY_ASSERT(ISEQ_COMPILE_DATA(iseq));
1508+
if (!ISEQ_COMPILE_DATA(iseq)->catch_except_p && ISEQ_BODY(iseq)->catch_table) {
15031509
xfree(ISEQ_BODY(iseq)->catch_table);
15041510
ISEQ_BODY(iseq)->catch_table = NULL;
15051511
}
@@ -3802,7 +3808,7 @@ iseq_optimize(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
38023808

38033809
int do_block_optimization = 0;
38043810

3805-
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_BLOCK && !ISEQ_BODY(iseq)->catch_except_p) {
3811+
if (ISEQ_BODY(iseq)->type == ISEQ_TYPE_BLOCK && !ISEQ_COMPILE_DATA(iseq)->catch_except_p) {
38063812
do_block_optimization = 1;
38073813
}
38083814

@@ -12175,7 +12181,6 @@ ibf_dump_iseq_each(struct ibf_dump *dump, const rb_iseq_t *iseq)
1217512181
ibf_dump_write_small_value(dump, body->ic_size);
1217612182
ibf_dump_write_small_value(dump, body->ci_size);
1217712183
ibf_dump_write_small_value(dump, body->stack_max);
12178-
ibf_dump_write_small_value(dump, body->catch_except_p);
1217912184
ibf_dump_write_small_value(dump, body->builtin_attrs);
1218012185

1218112186
#undef IBF_BODY_OFFSET
@@ -12288,7 +12293,6 @@ ibf_load_iseq_each(struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t offset)
1228812293

1228912294
const unsigned int ci_size = (unsigned int)ibf_load_small_value(load, &reading_pos);
1229012295
const unsigned int stack_max = (unsigned int)ibf_load_small_value(load, &reading_pos);
12291-
const char catch_except_p = (char)ibf_load_small_value(load, &reading_pos);
1229212296
const unsigned int builtin_attrs = (unsigned int)ibf_load_small_value(load, &reading_pos);
1229312297

1229412298
// setup fname and dummy frame
@@ -12361,7 +12365,6 @@ ibf_load_iseq_each(struct ibf_load *load, rb_iseq_t *iseq, ibf_offset_t offset)
1236112365
load_body->location.code_location.beg_pos.column = location_code_location_beg_pos_column;
1236212366
load_body->location.code_location.end_pos.lineno = location_code_location_end_pos_lineno;
1236312367
load_body->location.code_location.end_pos.column = location_code_location_end_pos_column;
12364-
load_body->catch_except_p = catch_except_p;
1236512368
load_body->builtin_attrs = builtin_attrs;
1236612369

1236712370
load_body->ivc_size = ivc_size;

iseq.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2306,7 +2306,6 @@ rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
23062306
rb_str_cat2(str, "== disasm: ");
23072307

23082308
rb_str_append(str, iseq_inspect(iseq));
2309-
rb_str_catf(str, " (catch: %s)", body->catch_except_p ? "true" : "false");
23102309
if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
23112310
rb_str_modify_expand(str, header_minlen - l);
23122311
memset(RSTRING_END(str), '=', header_minlen - l);

iseq.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ struct iseq_compile_data {
126126
struct rb_id_table *ivar_cache_table;
127127
const struct rb_builtin_function *builtin_function_table;
128128
const NODE *root_node;
129+
bool catch_except_p; // If a frame of this ISeq may catch exception, set true.
129130
#if OPT_SUPPORT_JOKE
130131
st_table *labels_table;
131132
#endif

rjit_c.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,6 @@ def C.rb_iseq_constant_body
11591159
icvarc_size: [CType::Immediate.parse("unsigned int"), Primitive.cexpr!("OFFSETOF((*((struct rb_iseq_constant_body *)NULL)), icvarc_size)")],
11601160
ci_size: [CType::Immediate.parse("unsigned int"), Primitive.cexpr!("OFFSETOF((*((struct rb_iseq_constant_body *)NULL)), ci_size)")],
11611161
stack_max: [CType::Immediate.parse("unsigned int"), Primitive.cexpr!("OFFSETOF((*((struct rb_iseq_constant_body *)NULL)), stack_max)")],
1162-
catch_except_p: [self._Bool, Primitive.cexpr!("OFFSETOF((*((struct rb_iseq_constant_body *)NULL)), catch_except_p)")],
11631162
builtin_attrs: [CType::Immediate.parse("unsigned int"), Primitive.cexpr!("OFFSETOF((*((struct rb_iseq_constant_body *)NULL)), builtin_attrs)")],
11641163
mark_bits: [CType::Union.new(
11651164
"", Primitive.cexpr!("SIZEOF(((struct rb_iseq_constant_body *)NULL)->mark_bits)"),
@@ -1599,10 +1598,6 @@ def C.rb_snum_t
15991598
CType::Stub.new(:rb_snum_t)
16001599
end
16011600

1602-
def C._Bool
1603-
CType::Bool.new
1604-
end
1605-
16061601
def C.iseq_bits_t
16071602
CType::Stub.new(:iseq_bits_t)
16081603
end
@@ -1631,6 +1626,10 @@ def C.rb_method_refined_t
16311626
CType::Stub.new(:rb_method_refined_t)
16321627
end
16331628

1629+
def C._Bool
1630+
CType::Bool.new
1631+
end
1632+
16341633
def C.ccan_list_node
16351634
CType::Stub.new(:ccan_list_node)
16361635
end

vm_core.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,6 @@ struct rb_iseq_constant_body {
496496
unsigned int ci_size;
497497
unsigned int stack_max; /* for stack overflow check */
498498

499-
bool catch_except_p; // If a frame of this ISeq may catch exception, set true.
500499
unsigned int builtin_attrs; // Union of rb_builtin_attr
501500

502501
union {

0 commit comments

Comments
 (0)