Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Verilog preprocessor, ifdef around include statement #223

Open
1 task done
Risto97 opened this issue Oct 15, 2024 · 3 comments
Open
1 task done

[BUG] Verilog preprocessor, ifdef around include statement #223

Risto97 opened this issue Oct 15, 2024 · 3 comments

Comments

@Risto97
Copy link

Risto97 commented Oct 15, 2024

Describe the bug

In the following code:

`ifdef SOME_MACRO
`include "inc.rdl"
`endif

addrmap test {
  reg {
    field {
    hw = rw; sw =rw;
    }f1 [`RANDOM] = 0;
  } reg1;
};

I want to include inc.rdl only if SOME_MACRO is defined.
However this doesn't work, as the PerlPreprocessor runs before VerilogPreprocess and expands include directives without considering ifdef, if, else, ... directives.

# Run file through Perl preprocessor
ppp = PerlPreprocessor(env, path, search_paths)
preprocessed_text, seg_map = ppp.preprocess()
included_files = ppp.included_files
# ... then through the Verilog preprocessor
vpp = VerilogPreprocessor(env, preprocessed_text, seg_map, defines=defines)
preprocessed_text, seg_map = vpp.preprocess()

Expected behavior
I expect PerlPreprocessor to ignore include directives if they are surrounded by ifdef which should disable it.

Additional context
Add any other context about the problem here.

@Furetur
Copy link

Furetur commented Oct 17, 2024

I have also encountered this issue

@amykyta3
Copy link
Member

This is intentional due to the Perl preprocessor being a step that is executed completely separately and prior to Verilog preprocessor.
This is partly due to poor specification by the SystemRDL language spec. I spoke to one of the committee members to clarify this and they confirmed that there are some gaps in the spec regarding how includes are handled in a two-layer preprocessor. According to one of the committe members, apparently some of the commercial/proprietary tools work around this mis-specification by reordering some operations to yield less surprising results.
In my implementation i had taken a similar approach. I write about the rationale in more detail here if you are interested: https://systemrdl-compiler.readthedocs.io/en/stable/dev_notes/preprocessors.html

Unfortunately stacking multiple preprocessors like RDL does causes some inherent contradictions which result in quite a messy situation.

@Risto97
Copy link
Author

Risto97 commented Oct 21, 2024

I tried a commercial SystemRDL compiler, and it seems like it works for the example I provided initially, but would fail if the include file contains any perl directives.
The commercial compiler forced me to have ifdef around include directives, as it cannot compile multiple SystemRDL files, in order to be compatible with both.

I think the following example demonstrates the issue about perl variables in include files mixed with verilog preprocessor:

test2.rdl

<% $foo = "FIRST_MACRO"; %>

`ifdef <%=$foo%>
`include "h2.rdl"
`endif

`ifdef <%=$bar%>
<% $macro = "THIRD_MACRO"; %>
`endif

addrmap test {
`ifdef <%=$macro%>
  reg {field {hw=rw;}f1 [32] = 0;
  }r1;
`endif
};

inc/h2.rdl

<% $bar = "SECOND_MACRO"; %>

When I call peakrdl with:

peakrdl html -I./inc2  -DSECOND_MACRO -DTHIRD_MACRO -o here test2.rdl

The question is if the h2.rdl should be processed by perl and verilog preprocessor.
The only way I see it is to do incremental preprocessing through the file, switching between two preprocessors.
But this might be slow to implement, and standard does not define it this way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants