From 2b8d628fa54062f7ffb9a56666c9dfd22142faa1 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 3 Mar 2024 21:41:38 +0000 Subject: [PATCH] Store only the instance name in T_HIER --- src/common.c | 17 +++++++++++++++++ src/common.h | 1 + src/elab.c | 46 +++++++++++---------------------------------- src/jit/jit-exits.c | 6 ++++-- src/rt/model.c | 33 ++++++++++++++++++-------------- src/rt/wave.c | 9 ++++++--- 6 files changed, 58 insertions(+), 54 deletions(-) diff --git a/src/common.c b/src/common.c index ce480e85..4a33402c 100644 --- a/src/common.c +++ b/src/common.c @@ -2526,3 +2526,20 @@ bool same_tree(tree_t a, tree_t b) return false; } } + +void instance_name_to_path(text_buf_t *tb, const char *str) +{ + bool delete = false; + for (const char *p = str; *p; p++) { + if (*p == '@' || (*p == '(' && !isdigit_iso88591(*(p + 1)))) + delete = true; + else if (*p == ')' && delete) + delete = false; + else if (*p == ':') { + delete = false; + tb_append(tb, ':'); + } + else if (!delete) + tb_append(tb, *p); + } +} diff --git a/src/common.h b/src/common.h index f23081f1..c32423e6 100644 --- a/src/common.h +++ b/src/common.h @@ -89,6 +89,7 @@ tree_t change_ref(tree_t name, tree_t new); bool all_character_literals(type_t type); bool is_operator_symbol(ident_t ident); bool same_tree(tree_t a, tree_t b); +void instance_name_to_path(text_buf_t *tb, const char *str); void analyse_file(const char *file, jit_t *jit, unit_registry_t *ur); diff --git a/src/elab.c b/src/elab.c index 1eb79bb6..a9dc432b 100644 --- a/src/elab.c +++ b/src/elab.c @@ -54,8 +54,7 @@ typedef struct _elab_ctx { tree_t out; tree_t root; tree_t inst; - ident_t path_name; // Current 'PATH_NAME - ident_t inst_name; // Current 'INSTANCE_NAME + ident_t inst_name; // Current 'INSTANCE_NAME ident_t dotted; ident_t prefix[2]; lib_t library; @@ -1342,7 +1341,6 @@ static void elab_inherit_context(elab_ctx_t *ctx, const elab_ctx_t *parent) ctx->registry = parent->registry; ctx->root = parent->root; ctx->dotted = ctx->dotted ?: parent->dotted; - ctx->path_name = ctx->path_name ?: parent->path_name; ctx->inst_name = ctx->inst_name ?: parent->inst_name; ctx->library = ctx->library ?: parent->library; ctx->out = ctx->out ?: parent->out; @@ -1386,12 +1384,10 @@ static void elab_verilog_module(tree_t bind, tree_t wrap, const elab_ctx_t *ctx) ident_t label = ident_rfrom(vlog_ident(mod), '.'); const char *label_str = istr(label); - ident_t npath = hpathf(ctx->path_name, ':', "%s", label_str); ident_t ninst = hpathf(ctx->inst_name, ':', "%s", label_str); ident_t ndotted = ident_prefix(ctx->dotted, label, '.'); elab_ctx_t new_ctx = { - .path_name = npath, .inst_name = ninst, .dotted = ndotted, }; @@ -1424,7 +1420,7 @@ static void elab_verilog_module(tree_t bind, tree_t wrap, const elab_ctx_t *ctx) static void elab_architecture(tree_t bind, tree_t arch, const elab_ctx_t *ctx) { tree_t inst = NULL; - ident_t label, ninst = NULL, npath = NULL; + ident_t label, ninst = NULL; switch (tree_kind(bind)) { case T_BINDING: label = ident_rfrom(tree_ident(tree_primary(arch)), '.'); @@ -1433,10 +1429,7 @@ static void elab_architecture(tree_t bind, tree_t arch, const elab_ctx_t *ctx) { label = tree_ident(bind); inst = bind; - - const char *label_str = istr(label); - npath = hpathf(ctx->path_name, ':', "%s", label_str); - ninst = hpathf(ctx->inst_name, ':', "%s@%s(%s)", label_str, + ninst = hpathf(ctx->inst_name, ':', "%s@%s(%s)", istr(label), simple_name(istr(tree_ident2(arch))), simple_name(istr(tree_ident(arch)))); } @@ -1449,7 +1442,6 @@ static void elab_architecture(tree_t bind, tree_t arch, const elab_ctx_t *ctx) ident_t ndotted = ident_prefix(ctx->dotted, label, '.'); elab_ctx_t new_ctx = { - .path_name = npath, .inst_name = ninst, .dotted = ndotted, .inst = inst, @@ -1503,9 +1495,7 @@ static void elab_component(tree_t inst, tree_t comp, const elab_ctx_t *ctx) else if ((bind = elab_default_binding(inst, ctx))) arch = tree_ref(bind); - const char *label = istr(tree_ident(inst)); - ident_t npath = hpathf(ctx->path_name, ':', "%s", label); - ident_t ninst = hpathf(ctx->inst_name, ':', "%s", label); + ident_t ninst = hpathf(ctx->inst_name, ':', "%s", istr(tree_ident(inst))); if (arch != NULL && tree_kind(arch) != T_VERILOG) ninst = hpathf(ninst, '@', "%s(%s)", @@ -1515,7 +1505,6 @@ static void elab_component(tree_t inst, tree_t comp, const elab_ctx_t *ctx) ident_t ndotted = ident_prefix(ctx->dotted, tree_ident(inst), '.'); elab_ctx_t new_ctx = { - .path_name = npath, .inst_name = ninst, .dotted = ndotted, .inst = inst, @@ -1627,8 +1616,8 @@ static void elab_push_scope(tree_t t, elab_ctx_t *ctx) tree_set_subkind(h, tree_kind(t)); tree_set_ref(h, t); - tree_set_ident(h, ctx->path_name); - tree_set_ident2(h, ctx->inst_name); + tree_set_ident(h, ctx->inst_name); + tree_set_ident2(h, ctx->dotted); tree_add_decl(ctx->out, h); } @@ -1730,13 +1719,11 @@ static void elab_for_generate(tree_t t, const elab_ctx_t *ctx) tree_add_genmap(b, map); const char *label = istr(base); - ident_t npath = hpathf(ctx->path_name, ':', "%s(%"PRIi64")", label, i); ident_t ninst = hpathf(ctx->inst_name, ':', "%s(%"PRIi64")", label, i); ident_t ndotted = ident_prefix(ctx->dotted, id, '.'); elab_ctx_t new_ctx = { .out = b, - .path_name = npath, .inst_name = ninst, .dotted = ndotted, .generics = hash_new(16), @@ -1797,14 +1784,12 @@ static void elab_if_generate(tree_t t, const elab_ctx_t *ctx) tree_add_stmt(ctx->out, b); - const char *label = istr(tree_ident(cond)); - ident_t npath = hpathf(ctx->path_name, ':', "%s", label); - ident_t ninst = hpathf(ctx->inst_name, ':', "%s", label); - ident_t ndotted = ident_prefix(ctx->dotted, tree_ident(cond), '.'); + ident_t name = tree_ident(cond); + ident_t ninst = hpathf(ctx->inst_name, ':', "%s", name); + ident_t ndotted = ident_prefix(ctx->dotted, name, '.'); elab_ctx_t new_ctx = { .out = b, - .path_name = npath, .inst_name = ninst, .dotted = ndotted, }; @@ -1840,14 +1825,11 @@ static void elab_case_generate(tree_t t, const elab_ctx_t *ctx) tree_add_stmt(ctx->out, b); - const char *label = istr(id); - ident_t npath = hpathf(ctx->path_name, ':', "%s", label); - ident_t ninst = hpathf(ctx->inst_name, ':', "%s", label); + ident_t ninst = hpathf(ctx->inst_name, ':', "%s", istr(id)); ident_t ndotted = ident_prefix(ctx->dotted, id, '.'); elab_ctx_t new_ctx = { .out = b, - .path_name = npath, .inst_name = ninst, .dotted = ndotted, }; @@ -1983,14 +1965,11 @@ static void elab_block(tree_t t, const elab_ctx_t *ctx) tree_add_stmt(ctx->out, b); - const char *label = istr(id); - ident_t npath = hpathf(ctx->path_name, ':', "%s", label); - ident_t ninst = hpathf(ctx->inst_name, ':', "%s", label); + ident_t ninst = hpathf(ctx->inst_name, ':', "%s", istr(id)); ident_t ndotted = ident_prefix(ctx->dotted, id, '.'); elab_ctx_t new_ctx = { .out = b, - .path_name = npath, .inst_name = ninst, .dotted = ndotted, }; @@ -2079,10 +2058,8 @@ static void elab_top_level(tree_t arch, ident_t ename, const elab_ctx_t *ctx) const char *name = simple_name(istr(tree_ident2(arch))); ident_t ninst = hpathf(ctx->inst_name, ':', ":%s(%s)", name, simple_name(istr(tree_ident(arch)))); - ident_t npath = hpathf(ctx->path_name, ':', ":%s", name); elab_ctx_t new_ctx = { - .path_name = npath, .inst_name = ninst, }; elab_inherit_context(&new_ctx, ctx); @@ -2136,7 +2113,6 @@ tree_t elab(object_t *top, jit_t *jit, unit_registry_t *ur, cover_data_t *cover) elab_ctx_t ctx = { .out = e, .root = e, - .path_name = NULL, .inst_name = NULL, .cover = cover, .library = work, diff --git a/src/jit/jit-exits.c b/src/jit/jit-exits.c index 4f18c953..9705d239 100644 --- a/src/jit/jit-exits.c +++ b/src/jit/jit-exits.c @@ -360,10 +360,12 @@ void x_instance_name(attr_kind_t kind, text_buf_t *tb) tree_t hier = tree_decl(where, 0); assert(tree_kind(hier) == T_HIER); + ident_t inst = tree_ident(hier); + if (kind == ATTR_PATH_NAME) - tb_istr(tb, tree_ident(hier)); + instance_name_to_path(tb, istr(inst)); else - tb_istr(tb, tree_ident2(hier)); + tb_istr(tb, inst); } return; diff --git a/src/rt/model.c b/src/rt/model.c index 828876a7..e4939bc3 100644 --- a/src/rt/model.c +++ b/src/rt/model.c @@ -427,31 +427,38 @@ static void global_event(rt_model_t *m, rt_event_t kind) } } -static rt_scope_t *scope_for_block(rt_model_t *m, tree_t block, ident_t prefix) +static void scope_for_block(rt_model_t *m, tree_t block, rt_scope_t *parent) { rt_scope_t *s = xcalloc(sizeof(rt_scope_t)); s->where = block; - s->name = ident_prefix(prefix, tree_ident(block), '.'); s->kind = SCOPE_INSTANCE; s->privdata = mptr_new(m->mspace, "block privdata"); + if (parent != NULL) { + s->parent = parent; + s->name = ident_prefix(parent->name, tree_ident(block), '.'); + list_add(&parent->children, s); + } + else + s->name = tree_ident(block); + hash_put(m->scopes, block, s); tree_t hier = tree_decl(block, 0); assert(tree_kind(hier) == T_HIER); - ident_t path = tree_ident(hier); + LOCAL_TEXT_BUF tb = tb_new(); + instance_name_to_path(tb, istr(tree_ident(hier))); + + ident_t path = ident_new(tb_get(tb)); + ident_t sym_prefix = tree_ident2(hier); const int nstmts = tree_stmts(block); for (int i = 0; i < nstmts; i++) { tree_t t = tree_stmt(block, i); switch (tree_kind(t)) { case T_BLOCK: - { - rt_scope_t *c = scope_for_block(m, t, s->name); - c->parent = s; - list_add(&s->children, c); - } + scope_for_block(m, t, s); break; case T_VERILOG: @@ -483,7 +490,7 @@ static rt_scope_t *scope_for_block(rt_model_t *m, tree_t block, ident_t prefix) case T_PROCESS: { ident_t name = tree_ident(t); - ident_t sym = ident_prefix(s->name, name, '.'); + ident_t sym = ident_prefix(sym_prefix, name, '.'); rt_proc_t *p = xcalloc(sizeof(rt_proc_t)); p->where = t; @@ -531,8 +538,6 @@ static rt_scope_t *scope_for_block(rt_model_t *m, tree_t block, ident_t prefix) break; } } - - return s; } rt_model_t *model_new(tree_t top, jit_t *jit) @@ -558,11 +563,11 @@ rt_model_t *model_new(tree_t top, jit_t *jit) m->root->kind = SCOPE_ROOT; m->root->where = top; m->root->privdata = MPTR_INVALID; + m->root->name = lib_name(lib_work()); m->threads[thread_id()] = xcalloc(sizeof(model_thread_t)); - rt_scope_t *s = scope_for_block(m, tree_stmt(top, 0), lib_name(lib_work())); - list_add(&m->root->children, s); + scope_for_block(m, tree_stmt(top, 0), m->root); __trace_on = opt_get_int(OPT_RT_TRACE); @@ -1045,7 +1050,7 @@ static void reset_scope(rt_model_t *m, rt_scope_t *s) jit_scalar_t result, context = { .pointer = NULL }; jit_scalar_t p2 = { .integer = 0 }; - if (s->parent != NULL) + if (s->parent->kind != SCOPE_ROOT) context.pointer = *mptr_get(s->parent->privdata); tlab_t tlab = jit_null_tlab(m->jit); diff --git a/src/rt/wave.c b/src/rt/wave.c index aefe11de..001e5840 100644 --- a/src/rt/wave.c +++ b/src/rt/wave.c @@ -623,7 +623,7 @@ static void gtkw_print_scope_comment(gtkw_writer_t *gtkw, rt_scope_t *scope, else fputc('-', gtkw->file); - if (scope->kind == SCOPE_INSTANCE && scope->parent == NULL) { + if (scope->parent->kind == SCOPE_ROOT) { // Do not emit the root scope label } else if (scope->kind == kind) { @@ -779,7 +779,7 @@ static void fst_walk_design(wave_dumper_t *wd, tree_t block) assert(tree_kind(h) == T_HIER); fst_process_hier(wd, h, block); - ident_t hpath = tree_ident(h); + ident_t hinst = tree_ident(h); if (tree_subkind(h) == T_COMPONENT && tree_stmts(block) > 0) { // Skip over implicit block statement created for component @@ -790,9 +790,12 @@ static void fst_walk_design(wave_dumper_t *wd, tree_t block) rt_scope_t *scope = find_scope(wd->model, block); if (scope == NULL) - fatal_trace("missing scope for %s", istr(hpath)); + fatal_trace("missing scope for %s", istr(hinst)); LOCAL_TEXT_BUF tb = tb_new(); + instance_name_to_path(tb, istr(hinst)); + + ident_t hpath = ident_new(tb_get(tb)); const int nports = tree_ports(block); for (int i = 0; i < nports; i++) { -- 2.39.2