The code-generation orchestrator for the C backend. It takes the semantically analysed AST of the whole program as input and is responsible for assembling the final C code.
Generating the actual code is implemented by cgen, with the orchestrator directing the code generator, managing emission of .inline procedures, and assembling the C code fragments produced into complete files (currently by dispatching to cgen).
Inlining
Inlining of procedures marked as .inline is currently not implemented at the |NimSkull| side, but is left to the optimizer of the used C compiler. For the C inliner to inline functions (when link-time optimizations are not employed, which by default, they aren't), the full C definition of an inline procedure must be present in all C translation units where the procedure is used.
When the orchestrator encounters an alive inline procedure, it fully transforms and lowers its body (which is then cached), generates the code for it with the context of the module the procedure is first seen in, and then records all the direct dependencies it has on other inline procedures. In addition, an inline procedure is registered with each module it is directly used in.
Once the code for all alive procedure has been generated (i.e., the main part of code generation done), the generated code of each inline procedure (along with its recorded transitive .inline dependencies) is emitted into the modules they were previously registered with.
Imports
-
../ast/ast_query, ../ast/ast_types, ../ast/lineinfos, ../ast/ndi, backends, cgen, cgendata, cgir, extccomp, ../front/options, ../mir/mirbridge, ../mir/mirenv, ../mir/mirtrees, ../modules/magicsys, ../modules/modulegraphs, ../sem/modulelowering, ../utils/containers, ../utils/idioms, ../utils/pathutils, ../utils/platform, ../utils/ropes, ../ast/ast, ../front/msgs, ../ast/reports, ../ast/reports_sem
Procs
proc generateCode(graph: ModuleGraph; g: BModuleList; mlist: sink ModuleList) {....raises: [ Exception, KeyError, ERecoverableError, ValueError, IOError, OSError], tags: [ RootEffect, ReadDirEffect, WriteIOEffect, ReadEnvEffect, ReadIOEffect, WriteDirEffect].}
- Implements the main part of the C code-generation orchestrator. Expects an already populated BModuleList. Source Edit
proc generateCode(graph: ModuleGraph; mlist: sink ModuleList) {. ...raises: [KeyError, Exception, ERecoverableError, OSError, IOError], tags: [ ReadDirEffect, RootEffect, ReadEnvEffect, ReadIOEffect, WriteDirEffect].}
- Entry point for C code generation. Only the C code is generated -- nothing is written to disk yet. Source Edit