Generating source code

Important

Loki is still under active development and has not yet seen a stable release. Interfaces can change at any time, objects may be renamed, or concepts may be re-thought. Make sure to sync your work to the current release frequently by rebasing feature branches and upstreaming more general applicable work in the form of pull requests.

At the end of a source-to-source translation process the output source code needs to be generated. Loki provides a number of different backends depending on the target language, which, once again, are Visitors.

All backends are subclasses of Stringifier that convert the internal representation to a string representation in the syntax of the target language.

loki.ir.pprint.Stringifier([depth, indent, ...])

Convert a given IR tree to a string representation.

loki.ir.pprint.pprint(ir[, stream])

Pretty-print the given IR using Stringifier.

Typically, this includes also a custom mapper for expression trees as a subclass of LokiStringifyMapper. For convenience, each of these visitors is wrapped in a corresponding utility routine that allows to generate code for any IR object via a simple function call, for example:

routine = Subroutine(...)
...
fcode = fgen(routine)

Currently, Loki has backends to generate Fortran, C, Python, and Maxeler MaxJ (a Java dialect that targets FPGAs).

loki.backend.fgen.fgen(ir[, depth, ...])

Generate standardized Fortran code from one or many IR objects/trees.

loki.backend.cufgen.cufgen(ir[, depth, ...])

Generate CUDA Fortran code from one or many IR objects/trees.

loki.backend.cgen.cgen(ir)

Generate standardized C code from one or many IR objects/trees.

loki.backend.pygen.pygen(ir)

Generate standard Python 3 code (that uses Numpy) from one or many IR objects/trees.

loki.backend.dacegen.dacegen(ir)

Generate standard Python 3 code with Dace-specializations (and Numpy) from one or many IR objects/trees.

loki.backend.maxgen.maxjgen(ir)

Generate Maxeler maxj kernel code from one or many IR objects/trees.

Warning

Backends do not make sure that the internal representation is compatible with the target language. Adapting the IR to the desired output format needs to be done before calling the relevant code generation routine. For language transpilation (e.g., Fortran to C), corresponding transformations must be applied (e.g., FortranCTransformation).