Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed May 29, 2023
1 parent 6ce4810 commit d8e1609
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 38 deletions.
4 changes: 2 additions & 2 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ rb_ast_node_alloc(VALUE klass)
static const char*
node_type_to_str(rb_ast_t *ast, const NODE *node)
{
return (ruby_node_name(nd_type(node), rb_bug) + rb_strlen_lit("NODE_"));
return (ruby_node_name(nd_type(node)) + rb_strlen_lit("NODE_"));
}

static VALUE
Expand Down Expand Up @@ -675,7 +675,7 @@ node_children(rb_ast_t *ast, const NODE *node)
break;
}

rb_bug("node_children: unknown node: %s", ruby_node_name(type, rb_bug));
rb_bug("node_children: unknown node: %s", ruby_node_name(type));
}

static VALUE
Expand Down
6 changes: 3 additions & 3 deletions compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ do { \
if (error_type != (ndtype)) { \
COMPILE_ERROR(ERROR_ARGS_AT(error_node) \
prefix ": " #ndtype " is expected, but %s", \
ruby_node_name(error_type, rb_bug)); \
ruby_node_name(error_type)); \
return errval; \
} \
} while (0)
Expand All @@ -420,7 +420,7 @@ do { \
do { \
const NODE *error_node = (node); \
COMPILE_ERROR(ERROR_ARGS_AT(error_node) prefix ": unknown node (%s)", \
ruby_node_name(nd_type(error_node), rb_bug)); \
ruby_node_name(nd_type(error_node))); \
return errval; \
} while (0)

Expand Down Expand Up @@ -8910,7 +8910,7 @@ compile_op_cdecl(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node
break;
default:
COMPILE_ERROR(ERROR_ARGS "%s: invalid node in NODE_OP_CDECL",
ruby_node_name(nd_type(node->nd_head), rb_bug));
ruby_node_name(nd_type(node->nd_head)));
return COMPILE_NG;
}
mid = node->nd_head->nd_mid;
Expand Down
2 changes: 1 addition & 1 deletion debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ ruby_debug_print_node(int level, int debug_level, const char *header, const NODE
{
if (level < debug_level) {
fprintf(stderr, "DBG> %s: %s (%u)\n", header,
ruby_node_name(nd_type(node), rb_bug), nd_line(node));
ruby_node_name(nd_type(node)), nd_line(node));
}
return (NODE *)node;
}
Expand Down
80 changes: 67 additions & 13 deletions node.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@ rb_node_init(NODE *n, enum node_type type, VALUE a0, VALUE a1, VALUE a2)
n->node_id = -1;
}

const char *
rb_node_name(int node)
{
switch (node) {
#include "node_name.inc"
default:
return 0;
}
}

#ifdef UNIVERSAL_PARSER
const char *
ruby_node_name(int node)
{
return rb_node_name(node);
}
#else
const char *
ruby_node_name(int node)
{
const char *name = rb_node_name(node);

if (!name) rb_bug("unknown node: %d", node);
return name;
}
#endif

static void
init_node_buffer_list(node_buffer_list_t * nb, node_buffer_elem_t *head)
{
Expand All @@ -63,6 +90,7 @@ init_node_buffer_list(node_buffer_list_t * nb, node_buffer_elem_t *head)
nb->head->next = NULL;
}

#ifdef UNIVERSAL_PARSER
static node_buffer_t *
rb_node_buffer_new(rb_parser_config_t *config)
{
Expand All @@ -81,6 +109,25 @@ rb_node_buffer_new(rb_parser_config_t *config)
nb->config = config;
return nb;
}
#else
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 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)
> 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]);
init_node_buffer_list(&nb->markable, (node_buffer_elem_t*)((size_t)nb->unmarkable.head + bucket_size));
nb->local_tables = 0;
nb->mark_hash = Qnil;
nb->tokens = Qnil;
return nb;
}
#endif

static void
node_buffer_list_free(rb_ast_t *ast, node_buffer_list_t * nb)
Expand Down Expand Up @@ -154,17 +201,6 @@ nodetype_markable_p(enum node_type type)
}
}

const char *
ruby_node_name(int node, bug_report_func rb_bug)
{
switch (node) {
#include "node_name.inc"
default:
rb_bug("unknown node: %d", node);
return 0;
}
}

NODE *
rb_ast_newnode(rb_ast_t *ast, enum node_type type)
{
Expand All @@ -181,7 +217,7 @@ rb_ast_node_type_change(NODE *n, enum node_type type)
enum node_type old_type = nd_type(n);
if (nodetype_markable_p(old_type) != nodetype_markable_p(type)) {
rb_bug("node type changed: %s -> %s",
ruby_node_name(old_type, rb_bug), ruby_node_name(type, rb_bug));
ruby_node_name(old_type), ruby_node_name(type));
}
}
#endif
Expand Down Expand Up @@ -218,13 +254,23 @@ rb_ast_delete_node(rb_ast_t *ast, NODE *n)
/* should we implement freelist? */
}

#ifdef UNIVERSAL_PARSER
rb_ast_t *
rb_ast_new(rb_parser_config_t *config)
{
node_buffer_t *nb = rb_node_buffer_new(config);
config->counter++;
return config->ast_new((VALUE)nb);
}
#else
rb_ast_t *
rb_ast_new(void)
{
node_buffer_t *nb = rb_node_buffer_new();
rb_ast_t *ast = (rb_ast_t *)rb_imemo_new(imemo_ast, 0, 0, 0, (VALUE)nb);
return ast;
}
#endif

typedef void node_itr_t(rb_ast_t *ast, void *ctx, NODE * node);

Expand Down Expand Up @@ -255,7 +301,9 @@ 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)
{
#ifdef UNIVERSAL_PARSER
bug_report_func rb_bug = ast->node_buffer->config->bug;
#endif

switch (nd_type(node)) {
case NODE_ARGS:
Expand All @@ -279,14 +327,16 @@ mark_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
rb_gc_mark_movable(node->nd_rval);
break;
default:
rb_bug("unreachable node %s", ruby_node_name(nd_type(node), rb_bug));
rb_bug("unreachable node %s", ruby_node_name(nd_type(node)));
}
}

static void
update_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
{
#ifdef UNIVERSAL_PARSER
bug_report_func rb_bug = ast->node_buffer->config->bug;
#endif

switch (nd_type(node)) {
case NODE_ARGS:
Expand Down Expand Up @@ -341,14 +391,18 @@ void
rb_ast_free(rb_ast_t *ast)
{
if (ast->node_buffer) {
#ifdef UNIVERSAL_PARSER
rb_parser_config_t *config = ast->node_buffer->config;
#endif

rb_node_buffer_free(ast, ast->node_buffer);
ast->node_buffer = 0;
#ifdef UNIVERSAL_PARSER
config->counter--;
if (config->counter <= 0) {
rb_ruby_parser_config_free(config);
}
#endif
}
}

Expand Down
13 changes: 11 additions & 2 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "ruby/backward/2/attributes.h"

typedef void (*bug_report_func)(const char *fmt, ...);
typedef struct rb_parser_config_struct rb_parser_config_t;

typedef struct node_buffer_elem_struct {
struct node_buffer_elem_struct *next;
Expand All @@ -30,6 +29,10 @@ typedef struct {
node_buffer_elem_t *last;
} node_buffer_list_t;

#ifdef UNIVERSAL_PARSER
typedef struct rb_parser_config_struct rb_parser_config_t;
#endif

struct node_buffer_struct {
node_buffer_list_t unmarkable;
node_buffer_list_t markable;
Expand All @@ -41,19 +44,25 @@ struct node_buffer_struct {
// - location info
// Array, whose entry is array
VALUE tokens;
#ifdef UNIVERSAL_PARSER
rb_parser_config_t *config;
#endif
};

RUBY_SYMBOL_EXPORT_BEGIN

#ifdef UNIVERSAL_PARSER
rb_ast_t *rb_ast_new(rb_parser_config_t *config);
#else
rb_ast_t *rb_ast_new();
#endif
size_t rb_ast_memsize(const rb_ast_t*);
void rb_ast_dispose(rb_ast_t*);
VALUE rb_ast_tokens(rb_ast_t *ast);
#if RUBY_DEBUG
void rb_ast_node_type_change(NODE *n, enum node_type type);
#endif
const char *ruby_node_name(int node, bug_report_func);
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_ast_mark(rb_ast_t*);
Expand Down
Loading

0 comments on commit d8e1609

Please sign in to comment.