From feda058531c0bdd5b673180accb4407dcc798c79 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 18 Nov 2021 03:40:49 +0900 Subject: Refactor hacky ID tables to struct rb_ast_id_table_t The implementation of a local variable tables was represented as `ID*`, but it was very hacky: the first element is not an ID but the size of the table, and, the last element is (sometimes) a link to the next local table only when the id tables are a linked list. This change converts the hacky implementation to a normal struct. --- node.c | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'node.c') diff --git a/node.c b/node.c index 3dea7fe398..180142d089 100644 --- a/node.c +++ b/node.c @@ -1043,12 +1043,12 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node) ANN("new scope"); ANN("format: [nd_tbl]: local table, [nd_args]: arguments, [nd_body]: body"); F_CUSTOM1(nd_tbl, "local table") { - ID *tbl = node->nd_tbl; + rb_ast_id_table_t *tbl = node->nd_tbl; int i; - int size = tbl ? (int)*tbl++ : 0; + int size = tbl ? tbl->size : 0; if (size == 0) A("(empty)"); for (i = 0; i < size; i++) { - A_ID(tbl[i]); if (i < size - 1) A(","); + A_ID(tbl->ids[i]); if (i < size - 1) A(","); } } F_NODE(nd_args, "arguments"); @@ -1162,7 +1162,7 @@ typedef struct { struct node_buffer_struct { node_buffer_list_t unmarkable; node_buffer_list_t markable; - ID *local_tables; + struct rb_ast_local_table_link *local_tables; VALUE mark_hash; }; @@ -1205,15 +1205,22 @@ node_buffer_list_free(node_buffer_list_t * nb) } } +struct rb_ast_local_table_link { + struct rb_ast_local_table_link *next; + // struct rb_ast_id_table { + int size; + ID ids[FLEX_ARY_LEN]; + // } +}; + static void rb_node_buffer_free(node_buffer_t *nb) { node_buffer_list_free(&nb->unmarkable); node_buffer_list_free(&nb->markable); - ID * local_table = nb->local_tables; + struct rb_ast_local_table_link *local_table = nb->local_tables; while (local_table) { - unsigned int size = (unsigned int)*local_table; - ID * next_table = (ID *)local_table[size + 1]; + struct rb_ast_local_table_link *next_table = local_table->next; xfree(local_table); local_table = next_table; } @@ -1277,12 +1284,28 @@ rb_ast_node_type_change(NODE *n, enum node_type type) } } -void -rb_ast_add_local_table(rb_ast_t *ast, ID *buf) +rb_ast_id_table_t * +rb_ast_new_local_table(rb_ast_t *ast, int size) +{ + size_t alloc_size = sizeof(struct rb_ast_local_table_link) + size * sizeof(ID); + struct rb_ast_local_table_link *link = ruby_xmalloc(alloc_size); + link->next = ast->node_buffer->local_tables; + ast->node_buffer->local_tables = link; + link->size = size; + + return (rb_ast_id_table_t *) &link->size; +} + +rb_ast_id_table_t * +rb_ast_resize_latest_local_table(rb_ast_t *ast, int size) { - unsigned int size = (unsigned int)*buf; - buf[size + 1] = (ID)ast->node_buffer->local_tables; - ast->node_buffer->local_tables = buf; + struct rb_ast_local_table_link *link = ast->node_buffer->local_tables; + size_t alloc_size = sizeof(struct rb_ast_local_table_link) + size * sizeof(ID); + link = ruby_xrealloc(link, alloc_size); + ast->node_buffer->local_tables = link; + link->size = size; + + return (rb_ast_id_table_t *) &link->size; } void -- cgit v1.2.1