Skip to content

fix: deduplicate register/regfile classes and support nested regfiles - #33

Open
Jozi-07 wants to merge 1 commit into
HEP-SoC:masterfrom
Jozi-07:fix/duplicate-type-generation
Open

Jozi-07 wants to merge 1 commit into
HEP-SoC:masterfrom
Jozi-07:fix/duplicate-type-generation

Conversation

@Jozi-07

@Jozi-07 Jozi-07 commented Sep 17, 2026

Copy link
Copy Markdown

Summary

This PR fixes C++ redefinition errors in the generated code when a reg or regfile type is instantiated more than once within an addrmap. It also adds code generation for regfile instances and arrays nested inside other regfiles.

Follow-up to #20, which fixed duplicate register classes within a single regfile.

Problem

  1. Duplicate register classes: unique_orig_type=True only deduplicates within a single halchildren() call. A register type used directly in the addrmap and inside a regfile, or inside several different regfile types, is emitted once per loop, and the generated header does not compile.
  2. Duplicate regfile classes: Regfile classes are generated via haldescendants() without deduplication, so each instance of the same regfile type produces another class definition.
  3. Nested regfiles are dropped: SystemRDL allows regfile instances inside a regfile. The template already iterates over the children of a regfile but only emits members for registers, so nested regfiles and regfile arrays are silently missing.

Changes

All changes are in src/peakrdl_halcpp/templates/addrmap.h.j2:

  • Add the dictionaries reg_types and regfile_types and pass them as type_dict to halchildren()/haldescendants(). Each register and regfile class is now emitted exactly once per addrmap (same idea as the existing seen_enum_names).
  • Emit nested regfile instances and regfile arrays as static members of the enclosing regfile class, analogous to the existing declarations in the addrmap class (halcpp::RegfileArrayNode for arrays).

No changes to the Python code or the C++ headers are required, because the existing type_dict parameter and halcpp::RegfileArrayNode are reused. Deduplication is scoped per addrmap namespace, so child addrmaps still generate their own classes.

Testing

The RDL below instantiates register and regfile types on several hierarchy levels and contains a nested regfile array and a child addrmap. The generated headers compile with g++ -std=c++17.

RDL used for the deeper hierarchy test
reg test_reg {
    field {} test_field_1;
    field {} test_field_2;
};

regfile test_regfile_2 {
    test_reg  test_reg_1;
    test_reg  test_reg_2;
};

regfile test_regfile_1 {
    test_reg  test_reg_1;
    test_reg  test_reg_2;
    test_regfile_2 test_regfile[4];
};

addrmap test_timer {
        test_reg test_6;
        test_reg test_7;
        test_regfile_1 test_8;
} ;

addrmap test_addrmap {
    test_reg test_1;
    test_reg test_2;
    test_regfile_1 test_3;
    test_regfile_1 test_4;
    test_regfile_2 test_5;
    test_timer test_6;
};

Copilot AI lite review requested due to automatic review settings September 17, 2026 13:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical issue remains for register types declared exclusively inside nested regfiles.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

This PR deduplicates generated register/regfile classes and adds nested regfile support.

Changes:

  • Shares type dictionaries for class deduplication.
  • Emits nested regfile and regfile-array members.
  • Preserves per-addrmap type scoping.
File summaries
File Summary
src/peakrdl_halcpp/templates/addrmap.h.j2 Implements deduplication and nested regfile generation; nested register types may still be omitted, causing compilation failures.
Review details
  • Files reviewed: 1/1 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

{# ======== 2. Generate the registers from the register files ======== #}
{% for rf in halnode.halchildren(children_type=HalRegfileNode, skip_buses=skip_buses, unique_orig_type=True) %}
{% for r in rf.halchildren(children_type=HalRegNode, skip_buses=skip_buses, unique_orig_type=True) %}
{% for r in rf.halchildren(children_type=HalRegNode, skip_buses=skip_buses, unique_orig_type=True, type_dict=reg_types) %}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants