Project

General

Profile

iseq_mark.diff

Update iseq mark function v2 - tenderlovemaking (Aaron Patterson), 01/18/2018 06:53 PM

View differences:

compile.c
562 562
#define APPEND_ELEM(anchor, before, elem) APPEND_ELEM(iseq, (anchor), (before), (elem))
563 563
#endif
564 564

  
565
static int
566
iseq_add_mark_object(const rb_iseq_t *iseq, VALUE v)
567
{
568
    if (!SPECIAL_CONST_P(v)) {
569
	rb_iseq_add_mark_object(iseq, v);
570
    }
571
    return COMPILE_OK;
572
}
573

  
574 565
static int
575 566
iseq_add_mark_object_compile_time(const rb_iseq_t *iseq, VALUE v)
576 567
{
......
1235 1226
				    rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1236 1227
				    INT2FIX(line_no), parent, type, ISEQ_COMPILE_DATA(iseq)->option);
1237 1228
    debugs("[new_child_iseq]< ---------------------------------------\n");
1238
    iseq_add_mark_object(iseq, (VALUE)ret_iseq);
1229
    iseq_add_mark_object_compile_time(iseq, (VALUE)ret_iseq);
1239 1230
    return ret_iseq;
1240 1231
}
1241 1232

  
......
1250 1241
				 rb_iseq_path(iseq), rb_iseq_realpath(iseq),
1251 1242
				 INT2FIX(line_no), parent, type, ISEQ_COMPILE_DATA(iseq)->option);
1252 1243
    debugs("[new_child_iseq_ifunc]< ---------------------------------------\n");
1253
    iseq_add_mark_object(iseq, (VALUE)ret_iseq);
1244
    iseq_add_mark_object_compile_time(iseq, (VALUE)ret_iseq);
1254 1245
    return ret_iseq;
1255 1246
}
1256 1247

  
......
1501 1492
	    switch (nd_type(val_node)) {
1502 1493
	      case NODE_LIT:
1503 1494
		dv = val_node->nd_lit;
1504
		iseq_add_mark_object(iseq, dv);
1505 1495
		break;
1506 1496
	      case NODE_NIL:
1507 1497
		dv = Qnil;
......
2044 2034
			    VALUE v = operands[j];
2045 2035
			    generated_iseq[code_index + 1 + j] = v;
2046 2036
			    /* to mark ruby object */
2047
			    iseq_add_mark_object(iseq, v);
2048 2037
			    break;
2049 2038
			}
2050 2039
		      case TS_IC: /* inline cache */
......
2209 2198
	    entry->end = label_get_position((LABEL *)(ptr[2] & ~1));
2210 2199
	    entry->iseq = (rb_iseq_t *)ptr[3];
2211 2200

  
2212
	    /* register iseq as mark object */
2213
	    if (entry->iseq != 0) {
2214
		iseq_add_mark_object(iseq, (VALUE)entry->iseq);
2215
	    }
2216

  
2217 2201
	    /* stack depth */
2218 2202
	    if (ptr[4]) {
2219 2203
		LABEL *lobj = (LABEL *)(ptr[4] & ~1);
......
4790 4774
    }
4791 4775

  
4792 4776
    if (only_special_literals) {
4793
	iseq_add_mark_object(iseq, literals);
4794 4777

  
4795 4778
	ADD_INSN(ret, nd_line(orig_node), dup);
4796 4779
	ADD_INSN2(ret, nd_line(orig_node), opt_case_dispatch, literals, elselabel);
......
7505 7488
    }
7506 7489

  
7507 7490
    loaded_iseq = rb_iseqw_to_iseq(iseqw);
7508
    iseq_add_mark_object(iseq, (VALUE)loaded_iseq);
7509 7491
    return loaded_iseq;
7510 7492
}
7511 7493

  
......
7636 7618
			break;
7637 7619
		      case TS_VALUE:
7638 7620
			argv[j] = op;
7639
			iseq_add_mark_object(iseq, op);
7640 7621
			break;
7641 7622
		      case TS_ISEQ:
7642 7623
			{
......
7683 7664
			    }
7684 7665
			    RB_GC_GUARD(op);
7685 7666
			    argv[j] = map;
7686
			    rb_iseq_add_mark_object(iseq, map);
7687 7667
			}
7688 7668
			break;
7689 7669
		      case TS_FUNCPTR:
......
9285 9265

  
9286 9266
	    rb_ary_store(load->obj_list, (long)object_index, obj);
9287 9267
	}
9288
	iseq_add_mark_object(load->iseq, obj);
9289 9268
	return obj;
9290 9269
    }
9291 9270
}
......
9470 9449
	    ibf_load_iseq_complete(iseq);
9471 9450
#endif /* !USE_LAZY_LOAD */
9472 9451

  
9473
	    if (load->iseq) {
9474
		iseq_add_mark_object(load->iseq, (VALUE)iseq);
9475
	    }
9476 9452
	    return iseq;
9477 9453
	}
9478 9454
    }
iseq.c
113 113
    RUBY_FREE_LEAVE("iseq");
114 114
}
115 115

  
116
#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
117
static int
118
rb_vm_insn_addr2insn2(const void *addr)
119
{
120
    int insn;
121
    const void * const *table = rb_vm_get_insns_address_table();
122

  
123
    for (insn = 0; insn < VM_INSTRUCTION_SIZE; insn++) {
124
	if (table[insn] == addr) {
125
	    return insn;
126
	}
127
    }
128
    rb_bug("rb_vm_insn_addr2insn: invalid insn address: %p", addr);
129
}
130
#endif
131

  
132
typedef void iseq_value_itr_t(void *ctx, VALUE obj);
133

  
134
static int
135
iseq_extract_values(const VALUE *code, size_t pos, iseq_value_itr_t * func, void *data)
136
{
137
#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
138
    VALUE insn = rb_vm_insn_addr2insn2((void *)code[pos]);
139
#else
140
    VALUE insn = code[pos];
141
#endif
142
    int len = insn_len(insn);
143
    int op_no;
144
    const char *types = insn_op_types(insn);
145

  
146
    for (op_no = 0; types[op_no]; op_no++) {
147
	char type = types[op_no];
148
	switch (type) {
149
	    case TS_CDHASH:
150
	    case TS_ISEQ:
151
	    case TS_VALUE:
152
		{
153
		    VALUE op = code[pos + op_no + 1];
154
		    if (!SPECIAL_CONST_P(op)) {
155
			func(data, op);
156
		    }
157
		    break;
158
		}
159
	    default:
160
		break;
161
	}
162
    }
163

  
164
    return len;
165
}
166

  
167
static void
168
rb_iseq_each_value(const rb_iseq_t *iseq, iseq_value_itr_t * func, void *data)
169
{
170
    unsigned int size;
171
    const VALUE *code;
172
    size_t n;
173

  
174
    size = iseq->body->iseq_size;
175
    code = iseq->body->iseq_encoded;
176

  
177
    for (n = 0; n < size;) {
178
	n += iseq_extract_values(code, n, func, data);
179
    }
180
}
181

  
182
static void
183
each_insn_value(void *ctx, VALUE obj)
184
{
185
    rb_gc_mark(obj);
186
}
187

  
116 188
void
117 189
rb_iseq_mark(const rb_iseq_t *iseq)
118 190
{
......
121 193
    if (iseq->body) {
122 194
	const struct rb_iseq_constant_body *body = iseq->body;
123 195

  
196
	rb_iseq_each_value(iseq, each_insn_value, NULL);
124 197
	RUBY_MARK_UNLESS_NULL(body->mark_ary);
125 198
	rb_gc_mark(body->location.label);
126 199
	rb_gc_mark(body->location.base_label);
127 200
	rb_gc_mark(body->location.pathobj);
128 201
	RUBY_MARK_UNLESS_NULL((VALUE)body->parent_iseq);
202

  
203
	if (body->catch_table) {
204
	    const struct iseq_catch_table *table = body->catch_table;
205
	    unsigned int i;
206
	    for(i = 0; i < table->size; i++) {
207
		const struct iseq_catch_table_entry *entry;
208
		entry = &table->entries[i];
209
		if (entry->iseq) {
210
		    rb_gc_mark((VALUE)entry->iseq);
211
		}
212
	    }
213
	}
129 214
    }
130 215

  
216

  
131 217
    if (FL_TEST(iseq, ISEQ_NOT_LOADED_YET)) {
132 218
	rb_gc_mark(iseq->aux.loader.obj);
133 219
    }