# loki.backend.cgen module

cgen(ir, **kwargs)

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

class CCodegen(style, depth=0, **kwargs)

  Bases: `Stringifier`

  Tree visitor to generate standardized C code from IR.

  standard_imports = ['stdio.h', 'stdbool.h', 'float.h', 'math.h']
  visit_Sourcefile(o, **kwargs)
    Format as

      …modules… …subroutines…

  visit_Module(o, **kwargs)

    Format a `Module` as

        <repr(Module)>
          ...spec...
          ...routines...

  visit_Interface(o, **kwargs)
  visit_Subroutine(o, **kwargs)

    Format as:

    > …imports… <return_type> <name>(<args>) {

    > > …spec without imports and argument declarations… …body…

    > }

  visit_Function(o, **kwargs)

    Format as:

    > …imports… <return_type> <name>(<args>) {

    > > …spec without imports and argument declarations… …body…

    > }

  visit_Node(o, **kwargs)
    Format non-supported nodes as

      // <repr(Node)>

  visit_GenericStmt(o, **kwargs)

    Format intrinsic nodes.

  visit_Comment(o, **kwargs)

    Format comments.

  visit_CommentBlock(o, **kwargs)

    Format comment blocks.

  visit_VariableDeclaration(o, **kwargs)
    Format declaration as

      <type> <name> [= <initial>]

  visit_Import(o, **kwargs)
    Format C imports as

      #include “<name>”

  visit_Loop(o, **kwargs)
    Format loop with explicit range as
      for (<var>=<start>; <var><criteria><end>; <var> += <incr>) {

        …body…

      }

  visit_WhileLoop(o, **kwargs)
    Format loop as
      while (<condition>) {

        …body…

      }

  visit_Conditional(o, **kwargs)
    Format conditional as
      if (<condition>) {

        …body…

      [ } else if (<condition>) { ]

        […body…]

      [ } else { ]

        […body…]

      }

  visit_Assignment(o, **kwargs)
    Format statement as

      <target> = <expr> [<comment>]

  visit_Section(o, **kwargs)

    Format the section’s body.

  visit_CallStatement(o, **kwargs)
    Format call statement as

      <name>(<args>)

  visit_SymbolAttributes(o, **kwargs)
  visit_TypeDef(o, **kwargs)
    Format type definition/struct as
      struct <name> {

        …declarations…

      };

  visit_MultiConditional(o, **kwargs)
    Format as

      switch case (<expr>) { case <value>: {

      > …body…

      } [case <value>:] {

      > […body…]

      } [default:] {

      > […body…]

    E.g., the following

    select case (in)
      case (:2)

        out = 1

      case (4, 5, 7:9)

        out = 2

      case (6)

        out = 3

      case default

        out = 4

    end select

    becomes

    switch (in) {

      case 0: case 1: case 2: {

      > out = 1; break;

      } case 4: case 5: case 7: case 8: case 9: {

      > out = 2; break;

      } case 6: {

      > out = 3; break;

      } default: {

      > out = 4; breal;

      }

    }

class CCodeMapper(intrinsic_type_mapper, *args, **kwargs)

  Bases: `LokiStringifyMapper`

  A `StringifyMapper`-derived visitor for Pymbolic expression trees that converts an expression to a string adhering to standardized C.

  map_logic_literal(expr, enclosing_prec, *args, **kwargs)
  map_float_literal(expr, enclosing_prec, *args, **kwargs)
  map_int_literal(expr, enclosing_prec, *args, **kwargs)
  map_string_literal(expr, enclosing_prec, *args, **kwargs)
  map_cast(expr, enclosing_prec, *args, **kwargs)
  map_variable_symbol(expr, enclosing_prec, *args, **kwargs)
  map_meta_symbol(expr, enclosing_prec, *args, **kwargs)
  map_scalar(expr, enclosing_prec, *args, **kwargs)
  map_array(expr, enclosing_prec, *args, **kwargs)
  map_array_subscript(expr, enclosing_prec, *args, **kwargs)
  map_string_subscript(expr, enclosing_prec, *args, **kwargs)
  map_logical_not(expr, enclosing_prec, *args, **kwargs)
  map_logical_or(expr, enclosing_prec, *args, **kwargs)
  map_logical_and(expr, enclosing_prec, *args, **kwargs)
  map_range_index(expr, enclosing_prec, *args, **kwargs)
  map_power(expr, enclosing_prec, *args, **kwargs)
  map_c_reference(expr, enclosing_prec, *args, **kwargs)
  map_c_dereference(expr, enclosing_prec, *args, **kwargs)
  map_inline_call(expr, enclosing_prec, *args, **kwargs)

class IntrinsicTypeC

  Bases: `object`

  Mapping Fortran type to corresponding C type.

  get_str_from_symbol_attr(_type, *args, **kwargs)