Skip to content

Commit

Permalink
Change Node from union to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Sep 22, 2023
1 parent c8c35de commit eaa4e25
Show file tree
Hide file tree
Showing 11 changed files with 2,174 additions and 1,211 deletions.
221 changes: 112 additions & 109 deletions ast.c

Large diffs are not rendered by default.

908 changes: 454 additions & 454 deletions compile.c

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions ext/ripper/ripper_init.c.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ripper_get_id(VALUE v)
if (!RB_TYPE_P(v, T_NODE)) return 0;
nd = (NODE *)v;
if (!nd_type_p(nd, NODE_RIPPER)) return 0;
return nd->nd_vid;
return RNODE_RIPPER(nd)->nd_vid;
}

VALUE
Expand All @@ -73,7 +73,7 @@ ripper_get_value(VALUE v)
if (!RB_TYPE_P(v, T_NODE)) return v;
nd = (NODE *)v;
if (!nd_type_p(nd, NODE_RIPPER)) return Qnil;
return nd->nd_rval;
return RNODE_RIPPER(nd)->nd_rval;
}

static VALUE
Expand Down
56 changes: 28 additions & 28 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

#define NODE_BUF_DEFAULT_LEN 16

typedef void node_itr_t(rb_ast_t *ast, void *ctx, NODE * node);
typedef void node_itr_t(rb_ast_t *ast, void *ctx, NODE_BASIC * node);
static void iterate_node_values(rb_ast_t *ast, node_buffer_list_t *nb, node_itr_t * func, void *ctx);

/* Setup NODE structure.
Expand All @@ -54,18 +54,18 @@ static void iterate_node_values(rb_ast_t *ast, node_buffer_list_t *nb, node_itr_
* objects.
*/
void
rb_node_init(NODE *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
rb_node_init(NODE_BASIC *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
{
n->flags = T_NODE;
nd_init_type(n, type);
n->u1.value = a0;
n->u2.value = a1;
n->u3.value = a2;
n->nd_loc.beg_pos.lineno = 0;
n->nd_loc.beg_pos.column = 0;
n->nd_loc.end_pos.lineno = 0;
n->nd_loc.end_pos.column = 0;
n->node_id = -1;
RNODE(n)->flags = T_NODE;
nd_init_type(RNODE(n), type);
n->u1 = a0;
n->u2 = a1;
n->u3 = a2;
RNODE(n)->nd_loc.beg_pos.lineno = 0;
RNODE(n)->nd_loc.beg_pos.column = 0;
RNODE(n)->nd_loc.end_pos.lineno = 0;
RNODE(n)->nd_loc.end_pos.column = 0;
RNODE(n)->node_id = -1;
}

const char *
Expand Down Expand Up @@ -109,11 +109,11 @@ init_node_buffer_list(node_buffer_list_t * nb, node_buffer_elem_t *head)
static node_buffer_t *
rb_node_buffer_new(rb_parser_config_t *config)
{
const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE);
const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE_BASIC);
const size_t alloc_size = sizeof(node_buffer_t) + (bucket_size * 2);
STATIC_ASSERT(
integer_overflow,
offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE)
offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE_BASIC)
> sizeof(node_buffer_t) + 2 * sizeof(node_buffer_elem_t));
node_buffer_t *nb = config->malloc(alloc_size);
init_node_buffer_list(&nb->unmarkable, (node_buffer_elem_t*)&nb[1]);
Expand All @@ -128,11 +128,11 @@ rb_node_buffer_new(rb_parser_config_t *config)
static node_buffer_t *
rb_node_buffer_new(void)
{
const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE);
const size_t bucket_size = offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE_BASIC);
const size_t alloc_size = sizeof(node_buffer_t) + (bucket_size * 2);
STATIC_ASSERT(
integer_overflow,
offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE)
offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE_BASIC)
> sizeof(node_buffer_t) + 2 * sizeof(node_buffer_elem_t));
node_buffer_t *nb = ruby_xmalloc(alloc_size);
init_node_buffer_list(&nb->unmarkable, (node_buffer_elem_t*)&nb[1]);
Expand Down Expand Up @@ -169,13 +169,13 @@ free_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
{
switch (nd_type(node)) {
case NODE_ARGS:
xfree(node->nd_ainfo);
xfree(RNODE_ARGS(node)->nd_ainfo);
break;
case NODE_ARYPTN:
xfree(node->nd_apinfo);
xfree(RNODE_ARYPTN(node)->nd_apinfo);
break;
case NODE_FNDPTN:
xfree(node->nd_fpinfo);
xfree(RNODE_FNDPTN(node)->nd_fpinfo);
break;
}
}
Expand All @@ -195,13 +195,13 @@ rb_node_buffer_free(rb_ast_t *ast, node_buffer_t *nb)
xfree(nb);
}

static NODE *
static NODE_BASIC *
ast_newnode_in_bucket(rb_ast_t *ast, node_buffer_list_t *nb)
{
if (nb->idx >= nb->len) {
long n = nb->len * 2;
node_buffer_elem_t *nbe;
nbe = rb_xmalloc_mul_add(n, sizeof(NODE), offsetof(node_buffer_elem_t, buf));
nbe = rb_xmalloc_mul_add(n, sizeof(NODE_BASIC), offsetof(node_buffer_elem_t, buf));
nbe->len = n;
nb->idx = 0;
nb->len = n;
Expand Down Expand Up @@ -230,7 +230,7 @@ nodetype_markable_p(enum node_type type)
}
}

NODE *
NODE_BASIC *
rb_ast_newnode(rb_ast_t *ast, enum node_type type)
{
node_buffer_t *nb = ast->node_buffer;
Expand Down Expand Up @@ -326,7 +326,7 @@ iterate_node_values(rb_ast_t *ast, node_buffer_list_t *nb, node_itr_t * func, vo
}

static void
mark_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
mark_ast_value(rb_ast_t *ast, void *ctx, NODE_BASIC * node)
{
#ifdef UNIVERSAL_PARSER
bug_report_func rb_bug = ast->node_buffer->config->bug;
Expand All @@ -341,15 +341,15 @@ mark_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
case NODE_DXSTR:
case NODE_DREGX:
case NODE_DSYM:
rb_gc_mark_movable(node->nd_lit);
rb_gc_mark_movable(RNODE_LIT(node)->nd_lit);
break;
default:
rb_bug("unreachable node %s", ruby_node_name(nd_type(node)));
}
}

static void
update_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
update_ast_value(rb_ast_t *ast, void *ctx, NODE_BASIC * node)
{
#ifdef UNIVERSAL_PARSER
bug_report_func rb_bug = ast->node_buffer->config->bug;
Expand All @@ -364,7 +364,7 @@ update_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
case NODE_DXSTR:
case NODE_DREGX:
case NODE_DSYM:
node->nd_lit = rb_gc_location(node->nd_lit);
RNODE_LIT(node)->nd_lit = rb_gc_location(RNODE_LIT(node)->nd_lit);
break;
default:
rb_bug("unreachable");
Expand Down Expand Up @@ -419,7 +419,7 @@ buffer_list_size(node_buffer_list_t *nb)
node_buffer_elem_t *nbe = nb->head;
while (nbe != nb->last) {
nbe = nbe->next;
size += offsetof(node_buffer_elem_t, buf) + nb->len * sizeof(NODE);
size += offsetof(node_buffer_elem_t, buf) + nb->len * sizeof(NODE_BASIC);
}
return size;
}
Expand All @@ -431,7 +431,7 @@ rb_ast_memsize(const rb_ast_t *ast)
node_buffer_t *nb = ast->node_buffer;

if (nb) {
size += sizeof(node_buffer_t) + offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE);
size += sizeof(node_buffer_t) + offsetof(node_buffer_elem_t, buf) + NODE_BUF_DEFAULT_LEN * sizeof(NODE_BASIC);
size += buffer_list_size(&nb->unmarkable);
size += buffer_list_size(&nb->markable);
}
Expand Down
30 changes: 15 additions & 15 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef void (*bug_report_func)(const char *fmt, ...);
typedef struct node_buffer_elem_struct {
struct node_buffer_elem_struct *next;
long len;
NODE buf[FLEX_ARY_LEN];
NODE_BASIC buf[FLEX_ARY_LEN];
} node_buffer_elem_t;

typedef struct {
Expand Down Expand Up @@ -59,14 +59,14 @@ VALUE rb_ast_tokens(rb_ast_t *ast);
void rb_ast_node_type_change(NODE *n, enum node_type type);
#endif
const char *ruby_node_name(int node);
void rb_node_init(NODE *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2);
void rb_node_init(NODE_BASIC *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2);

void rb_ast_mark(rb_ast_t*);
void rb_ast_update_references(rb_ast_t*);
void rb_ast_free(rb_ast_t*);
void rb_ast_add_mark_object(rb_ast_t*, VALUE);
void rb_ast_set_tokens(rb_ast_t*, VALUE);
NODE *rb_ast_newnode(rb_ast_t*, enum node_type type);
NODE_BASIC *rb_ast_newnode(rb_ast_t*, enum node_type type);
void rb_ast_delete_node(rb_ast_t*, NODE *n);
rb_ast_id_table_t *rb_ast_new_local_table(rb_ast_t*, int);
rb_ast_id_table_t *rb_ast_resize_latest_local_table(rb_ast_t*, int);
Expand Down Expand Up @@ -100,21 +100,21 @@ RUBY_SYMBOL_EXPORT_END
#define NODE_SPECIAL_EXCESSIVE_COMMA ((ID)1)
#define NODE_SPECIAL_NO_REST_KEYWORD ((NODE *)-1)

#define nd_first_column(n) ((int)((n)->nd_loc.beg_pos.column))
#define nd_set_first_column(n, v) ((n)->nd_loc.beg_pos.column = (v))
#define nd_first_lineno(n) ((int)((n)->nd_loc.beg_pos.lineno))
#define nd_set_first_lineno(n, v) ((n)->nd_loc.beg_pos.lineno = (v))
#define nd_first_loc(n) ((n)->nd_loc.beg_pos)
#define nd_first_column(n) ((int)(RNODE(n)->nd_loc.beg_pos.column))
#define nd_set_first_column(n, v) (RNODE(n)->nd_loc.beg_pos.column = (v))
#define nd_first_lineno(n) ((int)(RNODE(n)->nd_loc.beg_pos.lineno))
#define nd_set_first_lineno(n, v) (RNODE(n)->nd_loc.beg_pos.lineno = (v))
#define nd_first_loc(n) (RNODE(n)->nd_loc.beg_pos)
#define nd_set_first_loc(n, v) (nd_first_loc(n) = (v))

#define nd_last_column(n) ((int)((n)->nd_loc.end_pos.column))
#define nd_set_last_column(n, v) ((n)->nd_loc.end_pos.column = (v))
#define nd_last_lineno(n) ((int)((n)->nd_loc.end_pos.lineno))
#define nd_set_last_lineno(n, v) ((n)->nd_loc.end_pos.lineno = (v))
#define nd_last_loc(n) ((n)->nd_loc.end_pos)
#define nd_last_column(n) ((int)(RNODE(n)->nd_loc.end_pos.column))
#define nd_set_last_column(n, v) (RNODE(n)->nd_loc.end_pos.column = (v))
#define nd_last_lineno(n) ((int)(RNODE(n)->nd_loc.end_pos.lineno))
#define nd_set_last_lineno(n, v) (RNODE(n)->nd_loc.end_pos.lineno = (v))
#define nd_last_loc(n) (RNODE(n)->nd_loc.end_pos)
#define nd_set_last_loc(n, v) (nd_last_loc(n) = (v))
#define nd_node_id(n) ((n)->node_id)
#define nd_set_node_id(n,id) ((n)->node_id = (id))
#define nd_node_id(n) (RNODE(n)->node_id)
#define nd_set_node_id(n,id) (RNODE(n)->node_id = (id))

static inline bool
nd_type_p(const NODE *n, enum node_type t)
Expand Down
Loading

0 comments on commit eaa4e25

Please sign in to comment.