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

Proposal: enhance handling of conditional preprocessor directives #241

Open
pmaupin opened this issue Mar 25, 2020 · 2 comments
Open

Proposal: enhance handling of conditional preprocessor directives #241

pmaupin opened this issue Mar 25, 2020 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed preprocessor anything related to preprocessing (conditionals, macros, etc.)

Comments

@pmaupin
Copy link

pmaupin commented Mar 25, 2020

Thanks for making this available! It looks immensely useful.

I am faced with a lot of verilog that verible cannot process due to syntax errors, similar to issue #228.

For example:

module two_modules_in_one(
    input a,
    output b
`ifdef EXTENDED_FUNCTIONALITY
    , output c
`endif
);

endmodule

I think you'll find a lot of this sort of thing in real-world codebases.

I haven't read through the codebase or anything (and my C++ is quite rusty anyway), but taking into account the description that the lexer and parser are decoupled, and after reading through the discussion on #228 and thinking about it a bit, I have a proposal that should be much easier to implement than a full preprocessor.

This would probably have to be implemented as a user-configurable option; for reasons that will become clear, badly-constructed modules could result in an exponential blow-up of processing time and the output.

Whenever an `ifxxx or `else is encountered in a context where the parser cannot process it, rewrite the token stream to push the preprocessor directive up and outside the context where it is invalid, add an `else if necessary, and duplicate the non-conditional tokens. For example, the above module could be represented by something like this:

Lexed and filtered tokens: 
(#"`ifdef" @53-59: "`ifdef")
(#PP_Identifier @60-82: "EXTENDED_FUNCTIONALITY")
(#"module" @0-6: "module")
(#SymbolIdentifier @7-25: "two_modules_in_one")
(#'(' @25-26: "(")
(#"input" @31-36: "input")
(#SymbolIdentifier @37-38: "a")
(#',' @38-39: ",")
(#"output" @44-50: "output")
(#SymbolIdentifier @51-52: "b")
(#',' @87-88: ",")
(#"output" @89-95: "output")
(#SymbolIdentifier @96-97: "c")
(#')' @105-106: ")")
(#';' @106-107: ";")
(#"endmodule" @109-118: "endmodule")
(#"`else" @????: "`else")
(#"module" @0-6: "module")
(#SymbolIdentifier @7-25: "two_modules_in_one")
(#'(' @25-26: "(")
(#"input" @31-36: "input")
(#SymbolIdentifier @37-38: "a")
(#',' @38-39: ",")
(#"output" @44-50: "output")
(#SymbolIdentifier @51-52: "b")
(#')' @105-106: ")")
(#';' @106-107: ";")
(#"endmodule" @109-118: "endmodule")
(#"`endif" @98-104: "`endif")
(#$end @119-119: "")

and, of course, the code in issue #128 would simply be represented as two always blocks on different branches of the `ifdef/`else/`endif

@pmaupin
Copy link
Author

pmaupin commented Mar 25, 2020

Actually, I realize now your parser is smart enough to handle conditionals in the header, just not right before a comma, so that's a separate issue. I think the proposal would still be useful, though, for cases like the always block.

Thanks,
Pat

@fangism fangism added enhancement New feature or request preprocessor anything related to preprocessing (conditionals, macros, etc.) labels Mar 25, 2020
@fangism
Copy link
Collaborator

fangism commented Mar 25, 2020

Yes, I've considered the pseudo-preprocessing approaches you described of presenting various preprocessing filtered views, and forwarding those to subsequent parsing and processing. Some early thoughts here:
https://github.com/google/verible/blob/a9d1b6412820264dc3c279d4636e98c1a761545c/verilog/preprocessor/verilog_preprocess.h#L15

Possible strategies include:

  • take first branch, take else branch
  • assume encountered predicates are defined/undefined
  • take multiple/all branches (basically strip preprocessing directives)

It's currently a low priority and a help-wanted area. But I agree it would multiply the potential outreach of the Verible toolset.,

@fangism fangism added the help wanted Extra attention is needed label Mar 25, 2020
nikhiljha pushed a commit to nikhiljha/verible that referenced this issue Sep 27, 2022
kbieganski added a commit to antmicro/verible that referenced this issue Apr 28, 2023
Currently, the formatter doesn't handle many scenarios involving preprocessor
`ifdef`s/`endif`s interleaved with `begin`s, module headers, etc. (chipsalliance#228, chipsalliance#241, chipsalliance#267)
This patch attempts to solve this problem by performing multiple passes of the
formatting on preprocessed variants of the source. Each of these variants has a
different set of preprocessor branches enabled. Together, they should cover the
entire source (though that doesn't work in all cases yet). After several
formatting passes for different variants of the AST, a correct and properly
formatted file is produced.

This is still work in progress. Hoping to get some early feedback on this.

Signed-off-by: Krzysztof Bieganski <[email protected]>
kbieganski added a commit to antmicro/verible that referenced this issue Apr 28, 2023
Currently, the formatter doesn't handle many scenarios involving preprocessor
`ifdef`s/`endif`s interleaved with `begin`s, module headers, etc. (chipsalliance#228, chipsalliance#241, chipsalliance#267)
This patch attempts to solve this problem by performing multiple passes of the
formatting on preprocessed variants of the source. Each of these variants has a
different set of preprocessor branches enabled. Together, they should cover the
entire source (though that doesn't work in all cases yet). After several
formatting passes for different variants of the AST, a correct and properly
formatted file is produced.

This is still work in progress, so not everything works, and the code isn't very
clean. I'd love to get some early feedback on this.

Signed-off-by: Krzysztof Bieganski <[email protected]>
kbieganski added a commit to antmicro/verible that referenced this issue May 23, 2023
Currently, the formatter doesn't handle many scenarios involving preprocessor
`ifdef`s/`endif`s interleaved with `begin`s, module headers, etc. (chipsalliance#228, chipsalliance#241, chipsalliance#267)
This patch attempts to solve this problem by performing multiple passes of the
formatting on preprocessed variants of the source. Each of these variants has a
different set of preprocessor branches enabled. Together, they should cover the
entire source (though that doesn't work in all cases yet). After several
formatting passes for different variants of the AST, a correct and properly
formatted file is produced.

This is still work in progress, so not everything works, and the code isn't very
clean. I'd love to get some early feedback on this.

Signed-off-by: Krzysztof Bieganski <[email protected]>
kbieganski added a commit to antmicro/verible that referenced this issue May 23, 2023
Currently, the formatter doesn't handle many scenarios involving preprocessor
`ifdef`s/`endif`s interleaved with `begin`s, module headers, etc. (chipsalliance#228, chipsalliance#241, chipsalliance#267)
This patch attempts to solve this problem by performing multiple passes of the
formatting on preprocessed variants of the source. Each of these variants has a
different set of preprocessor branches enabled. Together, they should cover the
entire source (though that doesn't work in all cases yet). After several
formatting passes for different variants of the AST, a correct and properly
formatted file is produced.

This is still work in progress, so not everything works, and the code isn't very
clean. I'd love to get some early feedback on this.

Signed-off-by: Krzysztof Bieganski <[email protected]>
kbieganski added a commit to antmicro/verible that referenced this issue May 23, 2023
Currently, the formatter doesn't handle many scenarios involving preprocessor
`ifdef`s/`endif`s interleaved with `begin`s, module headers, etc. (chipsalliance#228, chipsalliance#241, chipsalliance#267)
This patch attempts to solve this problem by performing multiple passes of the
formatting on preprocessed variants of the source. Each of these variants has a
different set of preprocessor branches enabled. Together, they should cover the
entire source (though that doesn't work in all cases yet). After several
formatting passes for different variants of the AST, a correct and properly
formatted file is produced.

This is still work in progress, so not everything works, and the code isn't very
clean. I'd love to get some early feedback on this.

Signed-off-by: Krzysztof Bieganski <[email protected]>
kbieganski added a commit to antmicro/verible that referenced this issue May 24, 2023
Currently, the formatter doesn't handle many scenarios involving preprocessor
`ifdef`s/`endif`s interleaved with `begin`s, module headers, etc. (chipsalliance#228, chipsalliance#241, chipsalliance#267)
This patch attempts to solve this problem by performing multiple passes of the
formatting on preprocessed variants of the source. Each of these variants has a
different set of preprocessor branches enabled. Together, they should cover the
entire source (though that doesn't work in all cases yet). After several
formatting passes for different variants of the AST, a correct and properly
formatted file is produced.

This is still work in progress, so not everything works, and the code isn't very
clean. I'd love to get some early feedback on this.

Signed-off-by: Krzysztof Bieganski <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed preprocessor anything related to preprocessing (conditionals, macros, etc.)
Projects
None yet
Development

No branches or pull requests

2 participants