Main Content

Target Language Compiler Basics

Target Language Compiler Overview

Target Language Compiler (TLC) is an integral part 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.

The TLC includes:

  • Files corresponding to a subset of the provided Simulink® blocks.

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

The TLC files are ASCII files that explicitly control the way that code is generated. By editing a TLC file, you can alter the way that the 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 provides tremendous flexibility for customizing the generated code.

For more information, see Author Blocks Using C MEX S-Functions, which describes how to write wrapped and fully inlined S-functions, with emphasis on the mdlRTW() function.

Note

Do not customize TLC files in the folder matlabroot/rtw/c/tlc, even though the capability exists. 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 to convert the model description file model.rtw (or similar files) into target-specific code or text.

The Target Language Compiler transforms a representation of a Simulink block diagram, called model.rtw, into C or C++ code. The model.rtw file contains a partial representation of the model. The representation describes the execution semantics of the block diagram in a high-level language. For more information, see model.rtw File and Scopes.

After reading the model.rtw file, the Target Language Compiler generates its code based on target files, which specify particular code for each block, and model-wide files, which specify the overall code style. The TLC uses the target files and the model.rtw file 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 generic rt_main file (or grt_main). You must modify grt_main to conform to the target’s specific requirements, such as interrupt service routines. See Template Makefiles and Make Options and Customize Template 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 that TLC generated is highly optimized and fully commented. With TLC, you can generate code from linear, nonlinear, continuous, discrete, or hybrid Simulink models. The models can include Simulink blocks that are automatically converted to code. Exceptions are MATLAB function blocks and S-function blocks that invoke MATLAB files. The Target Language Compiler uses block target files to transform each block in the model.rtw file and a model-wide target file for global customization of the code.

You can incorporate C MEX S-functions, with the generated code into the program executable. You can write a target file for your C MEX S-function to inline the S-function (see Inline C MEX S-Functions), to improve performance by eliminating function calls to the S-function itself and the memory overhead of theSimStruct of the S-function. Inlining an S-function incorporates the S-function block 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 through a function call. See Inline S-Functions. You can also write target files for MATLAB language files or Fortran S-functions.

Overview of the Code Generation Process

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

When generating code from a Simulink model, the first step in the automated process is to generate a model.rtw file. The model.rtw file includes the model-specific information required for generating code from the Simulink model. model.rtw is 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.

Only the final executable file is written directly to the current folder. For other files created during code generation, including the model.rtw file, a build folder is used. This folder is created in the current folder and is named model_target_rtw. target is the abbreviation for the target environment grt that is a generic real-time target.

Files placed in the build folder include:

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

  • Header files (model.h)

  • Header file model_private.h defining parameters and data structures private to the generated code

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

  • Additional files, described in Manage Build Process Files

Related Topics