Writing a custom code generator

The register code generator API in hdl-registers is carefully designed to be very extensible. Writing and using your own custom code generator is very simple.

The key is to inherit the RegisterCodeGenerator class, as seen in the example below.

Example

The example below is a simple “code” generator that will dump the names of each register to a text file.

Example of a custom code generator.
 1# Standard libraries
 2import sys
 3from pathlib import Path
 4
 5# First party libraries
 6from hdl_registers.generator.register_code_generator import RegisterCodeGenerator
 7from hdl_registers.register_list import RegisterList
 8
 9
10class TxtRegisterListGenerator(RegisterCodeGenerator):
11    """
12    Custom code generator that generates a .txt file with all register names.
13    """
14
15    SHORT_DESCRIPTION = "text list"
16    COMMENT_START = "#"
17
18    @property
19    def output_file(self) -> Path:
20        """
21        Result will be placed in this file.
22        """
23        return self.output_folder / f"{self.name}_registers.txt"
24
25    def get_code(self, **kwargs) -> str:
26        """
27        Generate a textual list of all registers and register arrays.
28        """
29        txt = f'{self.header}\nAvailable registers in the "{self.name}" register list:\n\n'
30
31        for register, register_array in self.iterate_registers():
32            if register_array:
33                # This register is part of a register array.
34                txt += f"{register_array.name}[{register_array.length}].{register.name}\n"
35            else:
36                # This register is a plain register.
37                txt += f"{register.name}\n"
38
39        return txt
40
41
42def main(output_folder: Path):
43    """
44    Set up some registers and generate text file with our custom generator.
45    """
46    register_list = RegisterList(name="caesar")
47
48    register_list.append_register(name="config", mode="r_w", description="")
49    register_list.append_register(name="status", mode="r", description="")
50    register_list.append_register(name="command", mode="wpulse", description="")
51
52    register_array = register_list.append_register_array(name="channels", length=4, description="")
53    register_array.append_register(name="read_address", mode="r_w", description="")
54    register_array.append_register(name="write_address", mode="r_w", description="")
55
56    TxtRegisterListGenerator(register_list=register_list, output_folder=output_folder).create()
57
58
59if __name__ == "__main__":
60    main(output_folder=Path(sys.argv[1]))

The COMMENT_START, SHORT_DESCRIPTION and output_file class properties are abstract in the super class, and MUST hence be implemented in the subclass. The same is true, naturally, for the get_code method which is where all the code generation happens.

The generator class inheriting from RegisterCodeGenerator means that it has the public methods RegisterCodeGenerator.create(), RegisterCodeGenerator.create_if_needed() and RegisterCodeGenerator.header() just like the standard hdl-registers generators.

It also, by extension, inherits from the RegisterCodeGeneratorHelpers class. Meaning it has access to some useful functions for constructing the generated code. Used in the example above is RegisterCodeGeneratorHelpers.iterate_registers().

Running the example script above will yield the following result file:

Result text file from custom code generator.
# This file is automatically generated by hdl-registers version 5.1.4-dev.
# Code generator TxtRegisterListGenerator version 0.0.1.
# Generated 2024-04-16 20:51 at commit c1d987980d0749fd.
# Register hash 61b9927ebb802ac7b052809196c72bcffa905fda.

Available registers in the "caesar" register list:

config
status
command
channels[4].read_address
channels[4].write_address

Note that the header uses the COMMENT_START character to start each line. This header is used by RegisterCodeGenerator.create_if_needed() to determine if a re-generate of the file is necessary.

Running the script above yields the following shell printout:

Shell output when running custom code generator.
Creating text list file: generated/sphinx_rst/register_code/extensions/extensions_custom_generator/caesar_registers.txt

Note that the “text list” part of the printout is the SHORT_DESCRIPTION property of our generator class above. The file name is of course also given by output_file of our generator class.

Contributions

If you write a high-quality code generator you are more than welcome to contribute it to the hdl-registers project. Please open an issue or a pull request.

Performance

Since code generation is usually quite slow, there is a mechanism in place for speeding up the process and giving a better user experience. For code generation that is time-critical, such as generating a register package before a user simulation run, it is recommended to call the RegisterCodeGenerator.create_if_needed() method. This method will only re-create the artifact if some register definition has changed or a tool version is bumped. Hence using this method as opposed to RegisterCodeGenerator.create() saves a lot of time, especially in a large project.

Code templating engine

The code generation in a custom code generator can be done either using plain Python, as in the example above, or using an engine such as Jinja. This is completely up to the user. The custom code generator is pure Python and you are free to be as fancy/creative as you want.

Custom arguments

The RegisterCodeGenerator.create(), RegisterCodeGenerator.create_if_needed() and RegisterCodeGenerator.get_code() all have a **kwargs argument available. If you want to send further information/arguments to the code generator for some exotic feature, this makes it possible.