| Target Language Compiler | ![]() |
TLC Coding Conventions
These guidelines help ensure that the programming style in each target file is consistent, and hence, more easily modifiable.
Begin Identifiers with Uppercase Letters
All identifiers in the Real-Time Workshop file begin with an uppercase letter. For example,
NumModelInputs 1 NumModelOutputs 2 NumNonVirtBlocksInModel 42 DirectFeedthrough yes NumContStates 10
Block records that contain a Name identifier should start the name with an uppercase letter since the Name identifier is often promoted into the parent scope. For example, a block may contain
Block { : : RWork [4, 0] : NumRWorkDefines 4 RWorkDefine { Name "TimeStampA" Width 1 StartIndex 0 } }
Since the Name identifier within the RWorkDefine record is promoted to PrevT in its parent scope, it must start with an uppercase letter. The promotion of the Name identifier into the parent block scope is currently done for the Parameter, RWorkDefine, IWorkDefine, and PWorkDefine block records.
The Target Language Compiler assignment directive (%assign) generates a warning if you assign a value to an "unqualified" Real-Time Workshop identifier. For example,
produces an error because TID identifier is not qualified by Block. However, a "qualified" assignment does not generate a warning:
does not generate a warning because the Target Language Compiler assumes the programmer is intentionally modifying an identifier since the assignment contains a qualifier.
Begin Global Variable Assignments with Uppercase Letters
Global TLC variable assignments should start with uppercase letters. A global variable is any variable declared in a system target file (grt.tlc, mdlwide.tlc, mdlhdr.tlc, mdlbody.tlc, mdlreg.tlc, or mdlparam.tlc), or within a function that uses the :: operator. In some sense, global assignments have the same scope as Real-Time Workshop variables. An example of a global TLC variable defined in mdlwide.tlc is
An example of a global reference in a function is
Begin Local Variable Assignments with Lowercase Letters
Local TLC variable assignments should start with lowercase letters. A local TLC variable is a variable assigned inside a function. For example,
Begin Functions Declared in block.tlc files with Fcn
When you declare a function inside a block.tlc file, it should start with Fcn. For example:
| Note Functions declared inside a system file are global; functions declared inside a block file are local. |
Do Not Hard Code Variables Defined in commonsetup.tlc
Since the Real-Time Workshop tracks use of variables and generates code based on usage, you should use access routines instead of directly using a variable. For example, you should not use the following in your TLC file:
Similarly, instead of using %<tTID>, use %<LibTID()>. For a complete list of functions, see TLC Function Library Reference.
All Real-Time Workshop global variables start with rt and all Real-Time Workshop global functions start with rt_.
Avoid naming global variables in your run-time interface modules that start with rt or rt_ since they may conflict with Real-Time Workshop global variables and functions. These TLC variables are declared in commonsetup.tlc.
This convention creates consistent variables throughout the target files. For example, the Gain block contains the following Outputs function.
constant.tlc.
sysIdx and blkIdx for system index
and block index, respectively.
rollVars when using the %roll construct.
sigIdx and lcv when looping
over RollRegions and xidx and xlcv when looping over the states.
Example: Output function in gain.tlc
Example: InitializeConditions function in linblock.tlc
Conditional Inclusion in Library Files
The Target Language Compiler function library files are conditionally included via guard code so that they may be referenced via %include multiple times without worrying if they have previously been included. It is recommended that you follow this same practice for any TLC library files that you yourself create.
The convention is to use a variable with the same name as the base filename, uppercased and with underscores attached at both ends. So, a file named customlib.tlc should have the variable _CUSTOMLIB_ guarding it.
As an example, the main Target Language Compiler function library, funclib.tlc, contains this TLC code to prevent multiple inclusion:
| Inlining Fortran (F-MEX) S-Functions | Block Target File Methods | ![]() |