Skip to content

Commit

Permalink
Use UNALIGNED_MEMBER_PTR
Browse files Browse the repository at this point in the history
* internal.h (UNALIGNED_MEMBER_ACCESS, UNALIGNED_MEMBER_PTR):
  moved from eval_intern.h.

* compile.c iseq.c, vm.c: use UNALIGNED_MEMBER_PTR for `entries`
  in `struct iseq_catch_table`.

* vm_eval.c, vm_insnhelper.c: use UNALIGNED_MEMBER_PTR for `body`
  in `rb_method_definition_t`.
  • Loading branch information
nobu committed May 31, 2019
1 parent ea42423 commit b1aecef
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 33 deletions.
5 changes: 3 additions & 2 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,8 @@ update_catch_except_flags(struct rb_iseq_constant_body *body)
return;

for (i = 0; i < ct->size; i++) {
const struct iseq_catch_table_entry *entry = &ct->entries[i];
const struct iseq_catch_table_entry *entry =
UNALIGNED_MEMBER_PTR(ct, entries[i]);
if (entry->type != CATCH_TYPE_BREAK
&& entry->type != CATCH_TYPE_NEXT
&& entry->type != CATCH_TYPE_REDO) {
Expand Down Expand Up @@ -2322,7 +2323,7 @@ iseq_set_exception_table(rb_iseq_t *iseq)

for (i = 0; i < table->size; i++) {
ptr = RARRAY_CONST_PTR_TRANSIENT(tptr[i]);
entry = &table->entries[i];
entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
entry->type = (enum catch_type)(ptr[0] & 0xffff);
entry->start = label_get_position((LABEL *)(ptr[1] & ~1));
entry->end = label_get_position((LABEL *)(ptr[2] & ~1));
Expand Down
17 changes: 0 additions & 17 deletions eval_intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,6 @@ LONG WINAPI rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *);
# define VAR_NOCLOBBERED(var) var
#endif

#if defined(USE_UNALIGNED_MEMBER_ACCESS) && USE_UNALIGNED_MEMBER_ACCESS && \
(defined(__clang__) || GCC_VERSION_SINCE(9, 0, 0))
# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
COMPILER_WARNING_PUSH; \
COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
typeof(expr) unaligned_member_access_result = (expr); \
COMPILER_WARNING_POP; \
unaligned_member_access_result; \
})
#else
# define UNALIGNED_MEMBER_ACCESS(expr) expr
#endif
#define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)

#undef RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b) UNALIGNED_MEMBER_ACCESS(rb_obj_write((VALUE)(a), (VALUE *)(slot), (VALUE)(b), __FILE__, __LINE__))

/* clear ec->tag->state, and return the value */
static inline int
rb_ec_tag_state(const rb_execution_context_t *ec)
Expand Down
17 changes: 17 additions & 0 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,23 @@ rb_obj_builtin_type(VALUE obj)
#define COMPILER_WARNING_PRAGMA(str) COMPILER_WARNING_PRAGMA_(str)
#define COMPILER_WARNING_PRAGMA_(str) _Pragma(#str)

#if defined(USE_UNALIGNED_MEMBER_ACCESS) && USE_UNALIGNED_MEMBER_ACCESS && \
(defined(__clang__) || GCC_VERSION_SINCE(9, 0, 0))
# define UNALIGNED_MEMBER_ACCESS(expr) __extension__({ \
COMPILER_WARNING_PUSH; \
COMPILER_WARNING_IGNORED(-Waddress-of-packed-member); \
typeof(expr) unaligned_member_access_result = (expr); \
COMPILER_WARNING_POP; \
unaligned_member_access_result; \
})
#else
# define UNALIGNED_MEMBER_ACCESS(expr) expr
#endif
#define UNALIGNED_MEMBER_PTR(ptr, mem) UNALIGNED_MEMBER_ACCESS(&(ptr)->mem)

#undef RB_OBJ_WRITE
#define RB_OBJ_WRITE(a, slot, b) UNALIGNED_MEMBER_ACCESS(rb_obj_write((VALUE)(a), (VALUE *)(slot), (VALUE)(b), __FILE__, __LINE__))

#if defined(__cplusplus)
#if 0
{ /* satisfy cc-mode */
Expand Down
13 changes: 8 additions & 5 deletions iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ rb_iseq_update_references(rb_iseq_t *iseq)
unsigned int i;
for(i = 0; i < table->size; i++) {
struct iseq_catch_table_entry *entry;
entry = &table->entries[i];
entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
if (entry->iseq) {
entry->iseq = (rb_iseq_t *)rb_gc_location((VALUE)entry->iseq);
}
Expand Down Expand Up @@ -315,7 +315,7 @@ rb_iseq_mark(const rb_iseq_t *iseq)
unsigned int i;
for(i = 0; i < table->size; i++) {
const struct iseq_catch_table_entry *entry;
entry = &table->entries[i];
entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
if (entry->iseq) {
rb_gc_mark_no_pin((VALUE)entry->iseq);
}
Expand Down Expand Up @@ -2138,7 +2138,8 @@ rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
rb_str_cat_cstr(indent, "| ");
indent_str = RSTRING_PTR(indent);
for (i = 0; i < body->catch_table->size; i++) {
const struct iseq_catch_table_entry *entry = &body->catch_table->entries[i];
const struct iseq_catch_table_entry *entry =
UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
rb_str_cat(str, indent_str, indent_len);
rb_str_catf(str,
"| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
Expand Down Expand Up @@ -2272,7 +2273,8 @@ iseq_iterate_children(const rb_iseq_t *iseq, void (*iter_func)(const rb_iseq_t *

if (body->catch_table) {
for (i = 0; i < body->catch_table->size; i++) {
const struct iseq_catch_table_entry *entry = &body->catch_table->entries[i];
const struct iseq_catch_table_entry *entry =
UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
child = entry->iseq;
if (child) {
if (rb_hash_aref(all_children, (VALUE)child) == Qnil) {
Expand Down Expand Up @@ -2782,7 +2784,8 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
/* exception */
if (iseq_body->catch_table) for (i=0; i<iseq_body->catch_table->size; i++) {
VALUE ary = rb_ary_new();
const struct iseq_catch_table_entry *entry = &iseq_body->catch_table->entries[i];
const struct iseq_catch_table_entry *entry =
UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
rb_ary_push(ary, exception_type2symbol(entry->type));
if (entry->iseq) {
rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
Expand Down
2 changes: 1 addition & 1 deletion thread_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ queue_do_pop(VALUE self, struct rb_queue *q, int should_block)

qw.w.th = GET_THREAD();
qw.as.q = q;
list_add_tail(&qw.as.q->waitq, &qw.w.node);
list_add_tail(queue_waitq(qw.as.q), &qw.w.node);
qw.as.q->num_waiting++;

rb_ensure(queue_sleep, self, queue_sleep_done, (VALUE)&qw);
Expand Down
10 changes: 5 additions & 5 deletions vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
else {
ct = cfp->iseq->body->catch_table;
if (ct) for (i = 0; i < ct->size; i++) {
entry = &ct->entries[i];
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
if (entry->start < epc && entry->end >= epc) {
if (entry->type == CATCH_TYPE_ENSURE) {
catch_iseq = entry->iseq;
Expand Down Expand Up @@ -1992,7 +1992,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
if (state == TAG_RAISE) {
ct = cfp->iseq->body->catch_table;
if (ct) for (i = 0; i < ct->size; i++) {
entry = &ct->entries[i];
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
if (entry->start < epc && entry->end >= epc) {

if (entry->type == CATCH_TYPE_RESCUE ||
Expand All @@ -2008,7 +2008,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
else if (state == TAG_RETRY) {
ct = cfp->iseq->body->catch_table;
if (ct) for (i = 0; i < ct->size; i++) {
entry = &ct->entries[i];
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
if (entry->start < epc && entry->end >= epc) {

if (entry->type == CATCH_TYPE_ENSURE) {
Expand All @@ -2035,7 +2035,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
search_restart_point:
ct = cfp->iseq->body->catch_table;
if (ct) for (i = 0; i < ct->size; i++) {
entry = &ct->entries[i];
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);

if (entry->start < epc && entry->end >= epc) {
if (entry->type == CATCH_TYPE_ENSURE) {
Expand Down Expand Up @@ -2073,7 +2073,7 @@ vm_exec_handle_exception(rb_execution_context_t *ec, enum ruby_tag_type state,
else {
ct = cfp->iseq->body->catch_table;
if (ct) for (i = 0; i < ct->size; i++) {
entry = &ct->entries[i];
entry = UNALIGNED_MEMBER_PTR(ct, entries[i]);
if (entry->start < epc && entry->end >= epc) {

if (entry->type == CATCH_TYPE_ENSURE) {
Expand Down
2 changes: 1 addition & 1 deletion vm_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ vm_call0_cfunc_with_frame(rb_execution_context_t* ec, struct rb_calling_info *ca
{
VALUE val;
const rb_callable_method_entry_t *me = cc->me;
const rb_method_cfunc_t *cfunc = &me->def->body.cfunc;
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
int len = cfunc->argc;
VALUE recv = calling->recv;
int argc = calling->argc;
Expand Down
5 changes: 3 additions & 2 deletions vm_insnhelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,8 @@ vm_throw_start(const rb_execution_context_t *ec, rb_control_frame_t *const reg_c

if (!ct) break;
for (i=0; i < ct->size; i++) {
const struct iseq_catch_table_entry * const entry = &ct->entries[i];
const struct iseq_catch_table_entry *const entry =
UNALIGNED_MEMBER_PTR(ct, entries[i]);

if (entry->type == CATCH_TYPE_BREAK &&
entry->iseq == base_iseq &&
Expand Down Expand Up @@ -2183,7 +2184,7 @@ vm_method_cfunc_entry(const rb_callable_method_entry_t *me)
rb_bug("wrong method type: %d", me->def->type);
}
#endif
return &me->def->body.cfunc;
return UNALIGNED_MEMBER_PTR(me->def, body.cfunc);
}

static VALUE
Expand Down

0 comments on commit b1aecef

Please sign in to comment.