Documentation

Introduction to the Target Language Compiler

Target Language Compiler Overview

目标语言编译器(TLC)是不可分割的一部分of the code generator. It enables you to customize generated code. Through customization, you can produce platform-specific code, or you can incorporate your own algorithmic changes for performance, code size, or compatibility with existing methods that you prefer to maintain.

The TLC includes:

  • A set of TLC files corresponding to a subset of the provided Simulink®blocks.

  • TLC files for model-wide information that specify header and parameter information.

The TLC files are ASCII files that explicitly control the way code is generated. By editing a TLC file, you can alter the way code is generated.

The Target Language Compiler provides a complete set of ready-to-use TLC files for generating ANSI®C or C++ code. You can view the TLC files and make minor — or extensive — changes to them. This open environment gives you tremendous flexibility when it comes to customizing the generated code.

For more information, seeC/C++ S-Functions(Simulink) which explains how to write wrapped and fully inlined S-functions, with a special emphasis on themdlRTW()function.

Note

You should not customize TLC files in the foldermatlabroot/rtw/c/tlceven though the capability exists to do so. Such TLC customizations might not be applied during the code generation process and can lead to unpredictable results.

Overview of the TLC Process

This top-level diagram shows how the Target Language Compiler fits in with the code generation process.

The Target Language Compiler (TLC) is designed for one purpose — to convert themodel description filemodel.rtw(or similar files) into target-specific code or text.

The Target Language Compiler transforms a representation of a Simulink block diagram, calledmodel.rtw, into C or C++ code. Themodel.rtwfile contains a partial representation of the model describing the execution semantics of the block diagram in a very high-level language. For more information, seemodel.rtw File and Scopes.

The wordtargetin Target Language Compiler refers not only to the high-level language to be output, but also to the nature of the real-time system on which the code will be executed. TLC-generated code is thus able to respect and exploit the capabilities and limitations of specific processor architectures (the target).

After reading themodel.rtwfile, the Target Language Compiler generates its code based ontarget files, which specify particular code for each block, andmodel-wide files, which specify the overall code style. The TLC works like a text processor, using the target files and themodel.rtwfile to generate ANSI C or C++ code.

To create a target-specific application, the code generator requires a template makefile that specifies a C or C++ compiler and compiler options for the build process. The code generator transforms the template makefile into a target makefile (model.mk) by performing token expansion specific to a given model. The target makefile is a modified version of the genericrt_mainfile (orgrt_main), which you must modify to conform to the target’s specific requirements, such as interrupt service routines. SeeTemplate Makefiles and Make OptionsandCustomize Template Makefilesfor more information abouttemplate makefiles.

The Target Language Compiler has similarities with HTML, Perl, and MATLAB®. It has markup syntax similar to HTML, the power and flexibility of Perl and other scripting languages, and the data handling power of MATLAB (TLC can invoke MATLAB functions). The code generated by TLC is highly optimized and fully commented, and can be generated from linear, nonlinear, continuous, discrete, or hybrid Simulink models. The models can include Simulink blocks that are automatically converted to code, with the exception of MATLAB function blocks and S-function blocks that invoke MATLAB files. The Target Language Compiler usesblock target filesto transform each block in themodel.rtwfile and amodel-wide target filefor global customization of the code.

You can incorporateC MEX S-functions, along with the generated code, into the program executable. You can also write a target file for your C MEX S-function toinlinethe S-function (seeInline C MEX S-Functions), thus potentially improving performance by eliminating function calls to the S-function itself and the memory overhead of theSimStructof the S-function. Inlining an S-function incorporates the S-function block’s code into the generated code for the model. When a TLC target file is not present for the S-function, its C or C++ code file is invoked via a function call. For more information, see “S-function Inlining” inTarget Language Compiler. You can also write target files for MATLAB language files or Fortran S-functions.

Overview of the Code Generation Process

The following figure shows how the Target Language Compiler works with its target files and the code generator output to produce code.

When generating code from aSimulink model, the first step in the automated process is to generate amodel.rtwfile.Themodel.rtwfile includes the model-specific information required for generating code from the Simulink model.model.rtwis passed to the Target Language Compiler, which uses it in combination with a set of included system target files and block target files to generate the code.

我只有最后的可执行文件s written directly to the current folder. For other files created during code generation, including themodel.rtwfile, a build folder is used. This folder is created in the current folder and is namedmodel_target_rtw, wheretargetis the abbreviation for the target environment, e.g.,grtfor the generic real-time target.

Files placed in the build folder include

  • The body for the generated C or C++ source code (model.cormodel.cpp)

  • Header files (model.h)

  • Header filemodel_private.hdefining parameters and data structures private to the generated code

  • A makefile,model.mk, for building the application

  • Additional files, described inManage Build Process Files

Was this topic helpful?