Skip to content

Commit

Permalink
WIP: Remove "vm_core.h"
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-knk committed Apr 22, 2023
1 parent 3292c87 commit 715ccb1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 108 deletions.
87 changes: 87 additions & 0 deletions external/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,77 @@ enum node_type {
NODE_LAST
};


#define nd_head u1.node
#define nd_alen u2.argc
#define nd_next u3.node

#define nd_cond u1.node
#define nd_body u2.node
#define nd_else u3.node

#define nd_resq u2.node
#define nd_ensr u3.node

#define nd_1st u1.node
#define nd_2nd u2.node

#define nd_stts u1.node

#define nd_entry u3.id
#define nd_vid u1.id

#define nd_var u1.node
#define nd_iter u3.node

#define nd_value u2.node
#define nd_aid u3.id

#define nd_lit u1.value

#define nd_recv u1.node
#define nd_mid u2.id
#define nd_args u3.node
#define nd_ainfo u3.args

#define nd_defn u3.node

#define nd_cpath u1.node
#define nd_super u3.node

#define nd_beg u1.node
#define nd_end u2.node
#define nd_state u3.state

#define nd_nth u2.argc

#define nd_alias u1.id
#define nd_orig u2.id
#define nd_undef u2.node

#define nd_brace u2.argc

#define nd_pconst u1.node
#define nd_pkwargs u2.node
#define nd_pkwrestarg u3.node

#define nd_apinfo u3.apinfo

#define nd_fpinfo u3.fpinfo

// for NODE_SCOPE
#define nd_tbl u1.tbl

// for NODE_ARGS_AUX
#define nd_pid u1.id
#define nd_plen u2.argc
#define nd_cflag u2.id

// for ripper
#define nd_cval u3.value
#define nd_rval u2.value
#define nd_tag u1.id

#ifndef FLEX_ARY_LEN
/* From internal/compilers.h */
/* A macro for defining a flexible array, like: VALUE ary[FLEX_ARY_LEN]; */
Expand Down Expand Up @@ -166,6 +237,22 @@ typedef struct RNode {
int node_id;
} NODE;

/* FL : 0..4: T_TYPES, 5: KEEP_WB, 6: PROMOTED, 7: FINALIZE, 8: UNUSED, 9: UNUSED, 10: EXIVAR, 11: FREEZE */
/* NODE_FL: 0..4: T_TYPES, 5: KEEP_WB, 6: PROMOTED, 7: NODE_FL_NEWLINE,
* 8..14: nd_type,
* 15..: nd_line
*/
#define NODE_FL_NEWLINE (((VALUE)1)<<7)

#define NODE_TYPESHIFT 8
#define NODE_TYPEMASK (((VALUE)0x7f)<<NODE_TYPESHIFT)

#define nd_type(n) ((int) (((n)->flags & NODE_TYPEMASK)>>NODE_TYPESHIFT))
#define nd_set_type(n,t) \
rb_node_set_type(n, t)
#define nd_init_type(n,t) \
(n)->flags=(((n)->flags&~NODE_TYPEMASK)|((((unsigned long)(t))<<NODE_TYPESHIFT)&NODE_TYPEMASK))

struct rb_args_info {
NODE *pre_init;
NODE *post_init;
Expand Down
3 changes: 3 additions & 0 deletions external/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ typedef struct rb_parser_config_struct {
VALUE (*tmpbuf_auto_free_pointer)(void);
void *(*tmpbuf_set_ptr)(VALUE v, void *ptr);
rb_imemo_tmpbuf_t *(*tmpbuf_parser_heap)(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
rb_ast_t *(*ast_new)(VALUE nb);

// VALUE rb_suppress_tracing(VALUE (*func)(VALUE), VALUE arg);
VALUE (*compile_callback)(VALUE (*func)(VALUE), VALUE arg);
Expand Down Expand Up @@ -233,6 +234,8 @@ typedef struct rb_parser_config_struct {
void (*gc_register_mark_object)(VALUE object);
void (*gc_guard)(VALUE);
void (*gc_mark)(VALUE);
void (*gc_mark_movable)(VALUE ptr);
VALUE (*gc_location)(VALUE value);

/* Re */
VALUE (*reg_compile)(VALUE str, int options, const char *sourcefile, int sourceline);
Expand Down
85 changes: 0 additions & 85 deletions node.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@ code_loc_gen(const rb_code_location_t *loc1, const rb_code_location_t *loc2)

#define RNODE(obj) ((struct RNode *)(obj))

/* FL : 0..4: T_TYPES, 5: KEEP_WB, 6: PROMOTED, 7: FINALIZE, 8: UNUSED, 9: UNUSED, 10: EXIVAR, 11: FREEZE */
/* NODE_FL: 0..4: T_TYPES, 5: KEEP_WB, 6: PROMOTED, 7: NODE_FL_NEWLINE,
* 8..14: nd_type,
* 15..: nd_line
*/
#define NODE_FL_NEWLINE (((VALUE)1)<<7)

#define NODE_TYPESHIFT 8
#define NODE_TYPEMASK (((VALUE)0x7f)<<NODE_TYPESHIFT)

#define nd_type(n) ((int) (((n)->flags & NODE_TYPEMASK)>>NODE_TYPESHIFT))
#define nd_set_type(n,t) \
rb_node_set_type(n, t)
#define nd_init_type(n,t) \
(n)->flags=(((n)->flags&~NODE_TYPEMASK)|((((unsigned long)(t))<<NODE_TYPESHIFT)&NODE_TYPEMASK))

#define NODE_LSHIFT (NODE_TYPESHIFT+7)
#define NODE_LMASK (((SIGNED_VALUE)1<<(sizeof(VALUE)*CHAR_BIT-NODE_LSHIFT))-1)
Expand All @@ -70,76 +55,6 @@ code_loc_gen(const rb_code_location_t *loc1, const rb_code_location_t *loc2)
#define nd_node_id(n) ((n)->node_id)
#define nd_set_node_id(n,id) ((n)->node_id = (id))

#define nd_head u1.node
#define nd_alen u2.argc
#define nd_next u3.node

#define nd_cond u1.node
#define nd_body u2.node
#define nd_else u3.node

#define nd_resq u2.node
#define nd_ensr u3.node

#define nd_1st u1.node
#define nd_2nd u2.node

#define nd_stts u1.node

#define nd_entry u3.id
#define nd_vid u1.id

#define nd_var u1.node
#define nd_iter u3.node

#define nd_value u2.node
#define nd_aid u3.id

#define nd_lit u1.value

#define nd_recv u1.node
#define nd_mid u2.id
#define nd_args u3.node
#define nd_ainfo u3.args

#define nd_defn u3.node

#define nd_cpath u1.node
#define nd_super u3.node

#define nd_beg u1.node
#define nd_end u2.node
#define nd_state u3.state

#define nd_nth u2.argc

#define nd_alias u1.id
#define nd_orig u2.id
#define nd_undef u2.node

#define nd_brace u2.argc

#define nd_pconst u1.node
#define nd_pkwargs u2.node
#define nd_pkwrestarg u3.node

#define nd_apinfo u3.apinfo

#define nd_fpinfo u3.fpinfo

// for NODE_SCOPE
#define nd_tbl u1.tbl

// for NODE_ARGS_AUX
#define nd_pid u1.id
#define nd_plen u2.argc
#define nd_cflag u2.id

// for ripper
#define nd_cval u3.value
#define nd_rval u2.value
#define nd_tag u1.id

#define NEW_NODE(t,a0,a1,a2,loc) rb_node_newnode((t),(VALUE)(a0),(VALUE)(a1),(VALUE)(a2),loc)
#define NEW_NODE_WITH_LOCALS(t,a1,a2,loc) node_newnode_with_locals(p, (t),(VALUE)(a1),(VALUE)(a2),loc)

Expand Down
54 changes: 31 additions & 23 deletions node2.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "external/node.h"
#include "external/parse.h"
#include "internal/parse.h"
#include "vm_core.h"
#include "node2.h"

#define NODE_BUF_DEFAULT_LEN 16
#define T_NODE 0x1b

/* Setup NODE structure.
* NODE is not an object managed by GC, but it imitates an object
Expand Down Expand Up @@ -53,6 +53,19 @@ struct node_buffer_struct {
rb_parser_config_t *config;
};

#define ruby_xmalloc ast->node_buffer->config->malloc
#define rb_ident_hash_new ast->node_buffer->config->ident_hash_new
#define rb_xmalloc_mul_add ast->node_buffer->config->xmalloc_mul_add
#define rb_gc_mark ast->node_buffer->config->gc_mark
#define rb_gc_location ast->node_buffer->config->gc_location
#define rb_gc_mark_movable ast->node_buffer->config->gc_mark_movable
#define Qnil ast->node_buffer->config->qnil
#define Qtrue ast->node_buffer->config->qtrue
#define NIL_P ast->node_buffer->config->nil_p
#define rb_bug ast->node_buffer->config->bug
#define rb_hash_aset ast->node_buffer->config->hash_aset
#define RB_OBJ_WRITE(old, slot, young) ast->node_buffer->config->obj_write((VALUE)(old), (VALUE *)(slot), (VALUE)(young))

static void
init_node_buffer_list(node_buffer_list_t * nb, node_buffer_elem_t *head)
{
Expand All @@ -76,8 +89,8 @@ rb_node_buffer_new(rb_parser_config_t *config)
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;
nb->mark_hash = config->qnil;
nb->tokens = config->qnil;
nb->config = config;
return nb;
}
Expand Down Expand Up @@ -116,10 +129,6 @@ rb_node_buffer_free(node_buffer_t *nb)
xfree(nb);
}

#define ruby_xmalloc ast->node_buffer->config->malloc
#define rb_ident_hash_new ast->node_buffer->config->ident_hash_new
#define rb_xmalloc_mul_add ast->node_buffer->config->xmalloc_mul_add

static NODE *
ast_newnode_in_bucket(rb_ast_t *ast, node_buffer_list_t *nb)
{
Expand Down Expand Up @@ -159,7 +168,7 @@ nodetype_markable_p(enum node_type type)
}

const char *
ruby_node_name(int node)
ruby_node_name(rb_ast_t *ast, int node)
{
switch (node) {
#include "node_name.inc"
Expand All @@ -179,12 +188,12 @@ rb_ast_newnode(rb_ast_t *ast, enum node_type type)
}

void
rb_ast_node_type_change(NODE *n, enum node_type type)
rb_ast_node_type_change(rb_ast_t *ast, 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), ruby_node_name(type));
ruby_node_name(ast, old_type), ruby_node_name(ast, type));
}
}

Expand Down Expand Up @@ -224,38 +233,37 @@ rb_ast_t *
rb_ast_new(rb_parser_config_t *config)
{
node_buffer_t *nb = rb_node_buffer_new(config);
rb_ast_t *ast = (rb_ast_t *)rb_imemo_new(imemo_ast, 0, 0, 0, (VALUE)nb);
return ast;
return config->ast_new((VALUE)nb);
}

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

static void
iterate_buffer_elements(node_buffer_elem_t *nbe, long len, node_itr_t *func, void *ctx)
iterate_buffer_elements(rb_ast_t *ast, node_buffer_elem_t *nbe, long len, node_itr_t *func, void *ctx)
{
long cursor;
for (cursor = 0; cursor < len; cursor++) {
func(ctx, &nbe->buf[cursor]);
func(ast, ctx, &nbe->buf[cursor]);
}
}

static void
iterate_node_values(node_buffer_list_t *nb, node_itr_t * func, void *ctx)
iterate_node_values(rb_ast_t *ast, node_buffer_list_t *nb, node_itr_t * func, void *ctx)
{
node_buffer_elem_t *nbe = nb->head;

/* iterate over the head first because it's not full */
iterate_buffer_elements(nbe, nb->idx, func, ctx);
iterate_buffer_elements(ast, nbe, nb->idx, func, ctx);

nbe = nbe->next;
while (nbe) {
iterate_buffer_elements(nbe, nbe->len, func, ctx);
iterate_buffer_elements(ast, nbe, nbe->len, func, ctx);
nbe = nbe->next;
}
}

static void
mark_ast_value(void *ctx, NODE * node)
mark_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
{
switch (nd_type(node)) {
case NODE_ARGS:
Expand All @@ -279,12 +287,12 @@ mark_ast_value(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("unreachable node %s", ruby_node_name(ast, nd_type(node)));
}
}

static void
update_ast_value(void *ctx, NODE * node)
update_ast_value(rb_ast_t *ast, void *ctx, NODE * node)
{
switch (nd_type(node)) {
case NODE_ARGS:
Expand Down Expand Up @@ -318,7 +326,7 @@ rb_ast_update_references(rb_ast_t *ast)
if (ast->node_buffer) {
node_buffer_t *nb = ast->node_buffer;

iterate_node_values(&nb->markable, update_ast_value, NULL);
iterate_node_values(ast, &nb->markable, update_ast_value, NULL);
}
}

Expand All @@ -333,7 +341,7 @@ rb_ast_mark(rb_ast_t *ast)
if (ast->node_buffer) {
node_buffer_t *nb = ast->node_buffer;

iterate_node_values(&nb->markable, mark_ast_value, NULL);
iterate_node_values(ast, &nb->markable, mark_ast_value, NULL);
}
if (ast->body.script_lines) rb_gc_mark(ast->body.script_lines);
}
Expand Down
Loading

0 comments on commit 715ccb1

Please sign in to comment.