Automatic Code Generation (Model-based Implementation)

  1. What are Code Generators?
  2. Model-based Code Generation
  3. Autocode Tool-Chain
  4. Code Generator Optimizations
  5. Benefits of Model-based Code Generation
  6. Comparison of Code Generators used for Production Code Generation

1. What are Code Generators?

A code generator (also automatic code generation tool or autocode tool) is essentially a compiler that translates a graphical modeling language, for example Simulink or Stateflow, into a high-level programming language such as C or ADA. However, in contrast to textual programming languages translated by compilers, the semantics of a modeling language such as Simulink or Stateflow is also determined by graphical layout information, for example, graphical position of states.

Examples of autocode tools, which are commonly used in automotive model-based development, are:


A comparison of these tools with respect to modeling options, code efficiency, readability of the code generated, real-time operation system integration etc. can be found here.

2. Model-based Code Generation

In automotive model-based development, the seamless use of executable models is characteristic for function and control system design and the following implementation phase. This means that models are used to represent the development of the system from the preliminary to the detailed design. At the beginning of this model evolution there is usually a so-called physical model, which is derived from the functional specification of the software component to be realised (see Fig. 1).

*
Fig. 1: Principle of automatic code generation (autocode tool-chain)

The physical model contains the control function to be developed and describes the behavior of the control function related to a given (continuous) input signal as well as internal or external events or states. The purpose of the physical model is to depict the algorithms to be developed “in their purest form” without already having to pay too much attention to realisation details. The description of the algorithms thus takes place through the use of floating-point arithmetic. Since this model can already be executed in a simulation environment on the development computer (Host PC), it is also called an executable specification.
For reasons of efficiency and because of the fact that the real input and output in the physical model is abstracted where necessary, the physical model cannot serve directly as a basis for deriving production code for the target processor. Therefore, the physical model has to be revised from a realization point of view (for example function parts are distributed to different tasks) and enhanced with the necessary implementation details. In order to do this, the floating-point arithmetic contained in the physical model is adjusted to the arithmetic of the target processor (for example 16-bit fixed-point). This means in particular, that only fixed-point data types are used (e.g. int16), which are augmented with scaling information in order to keep imprecision in the representation of fixed-point numbers as low as possible (this numerical error caused by imprecision is also called quantisation error).
The result of this adaptation is an implementation model which contains all the information necessary for code generation and makes the creation of efficient C code possible.

3. Autocode Tool-Chain

The principle of automatic code generation from the software model to executable code and the tools involved in the translation process are shown in Fig. 1. Depending on the purpose, the code is generated on the development or Host PC whereas a classical compiler/linker combination is used for the translation of the generated code.
For an embedded processor (experimental hardware or ECU) a so-called cross-compiler is required with a linker and a loader that make it possible to put the machine code onto the embedded device.
The code generator with its editor, simulator, etc., the tools involved in the model-to-code translation process (e.g. compiler, linker, loader) as well as the hardware parts (e.g. processor, experimental hardware, ECU) are also termed automatic code generation tool-chain, or autocode tool-chain in short.

4. Code Generator Optimizations

The limited resources of the embedded systems for which the code is generated require optimization techniques to be applied whenever possible in order to generate efficient code with respect to memory consumption and execution speed, for example.
Traditionally, code optimizations are performed by the (cross-)compiler translating the source code into executable target code. Such optimizations are, for instance, dead-code elimination, constant propagation or loop unrolling, known from classical compiler construction.
When using a code generator, optimization techniques can already be performed on the high-level representation (intermediate-graph representation) of the model. This is an important advantage, since some of these optimizations such as interblock optimizations (combination of different Simulink or Stateflow portions in order to create efficient code patterns) are only possible on model-level where additional information is available which cannot be obtained by code analysis.

5. Benefits of Model-based Code Generation

The concept of generating embedded software code directly from a software model is attractive and there are many benefits which a code generator could bring to the development process. The main advantages of the model-based code generation approach can be summed up as follows:

6. Comparison of Code Generators used for Production Code Generation

The following publications are concerned with the comparison of code generators used for production code generation with respect to: Please let me know if you know any other relevant publication covering one of these topics.
© 1998-2007 Ingo Stürmer, www.ichmaschine.de (last update: 03/2007)