dan-fritchman / Hdl21

Hardware Description Library
BSD 3-Clause "New" or "Revised" License
60 stars 13 forks source link

Keyword-named `Param`s #171

Open dan-fritchman opened 11 months ago

dan-fritchman commented 11 months ago

Lots of Hdl21 stuff gets built class-style, with Python identifiers serving as Hdl attribute names. E.g. in our front-page example:

import hdl21 as h

@h.module
class MyModule:
    i = h.Input()
    o = h.Output(width=8)
    s = h.Signal()
    a = AnotherModule()

Behind the scenes the identifiers i, o, s, and a are all affixed to those right-hand-side objects, to serve as their "HDL names", e.g. for netlisting.

That fails when the HDL name is a python keyword (e.g. import, from), or an "Hdl21 keyword" - one of the attribute-names of the thing holding it. For example a Module can't have an instance named ports; that identifier ports refers to its ports-dictionary. If you want to make an Hdl object with one of those names - often because you are importing from a non-Hdl21 source, in which that name is not a keyword - then the trick is to use the Module.add & Module.get API, and set the name as a field. E.g.

import hdl21 as h

m = h.Module(name="MyModule")
m.add(h.Input(name="import"))
m.add(AnotherModule(), name="from")
# ...

There's (at least) one place where we can't do this though: parameter names. Param-classes are designed to only take valid python identifiers as names. This could matter for ExternalModules, which have parameters we netlist directly. Note ExternalModule does not require its parameters to be a param-class (they can, and often are, dict instead for example). But by convention and priority for the built-in modules (e.g. in PDK packages), we prefer to use param-classes.

164 originally noticed this. To date it's getting by via using a casing-differences from the Python keywords (e.g. As vs as), and the fact that many downstream SPICE programs are case-insensitive. It's not clear that will always work, and not something I'd prefer we rely on.

Couple options:

Note: this is probably worth a review of anywhere else this "identifier names only" problem might come up. I think(?) we have everything else covered.