Target Language Compiler | ![]() ![]() |
Syntax
A target language file consists of a series of statements of the form
%
keyword
[
argument1
,
argument2
, ...]
where keyword
represents one of the Target Language Compiler's directives, and [
argument1
,
argument2
, ...]
represents expressions that define any required parameters. For example,
%assign sysNumber = sysIdx + 1
uses the %assign
directive to change the value of the sysNumber
parameter.
A target language directive must be the first nonblank character on a line and always begins with the %
character. Beginning a line with %%
lets you include a comment on a line.
This table shows the complete set of Target Language Compiler directives. The remainder of this chapter describes each directive in detail.
Directive |
Description |
|
Single line comment where text is the comment |
/% |
Single (or multi-line) comment where text is the comment |
%matlab |
Calls a MATLAB function that does not return a result. For example, %matlab disp(2.718) |
|
Target language expressions which are evaluated. For example, if we have a TLC variable that was created via: %assign varName = "foo" , then %<varName> would expand to foo . Expressions can also be function calls as in %<FcnName(param1,param2)> . On directive lines, TLC expressions do not need to be placed within the %<> syntax. Doing so will cause a double evaluation. For example, %if %<x> == 3 is processed by creating a hidden variable for the evaluated value of the variable x . The %if statement then evaluates this hidden variable and compares it against 3. The efficient way to do this operation is to do: %if x == 3 . In MATLAB notation, this would equate to doing if eval('x') == 3 as opposed to if x = 3 . The exception to this is during a %assign for format control as in %assign str = "value is: %<var>" Note: Nested evaluation expressions (e.g., %<foo(%<expr>)> ) are not supported.Note: There is no speed penalty for evals inside strings, such as
Evals outside of strings, such as the following example, should be avoided whenever possible.
|
%if |
Conditional inclusion, where the constant-expression expr must evaluate to an integer. It is not necessary to expand variables or expressions using the %< expr > notation. Expanding the expression may be necessary when the expression needs to be re-evaluated. For example, assume k represents the value of a the gain block parameter which may be a number or a string variable. The following code will check if k is the numeric value 0.0 by executing a TLC library function to check for equality. Notice that the %< expr > syntax isn't used since we are operating on a directive line. Like other languages, expression evaluation do short circuit.
|
|
The switch directive is very similar to the C language switch statement. The expression, expr , can be of any type that can be compared for equality using the == operator . If the %break is not included after a %case statement, then it will fall through to the next statement. |
|
|
%setcommandswitch |
Changes the value of a command-line switch as in %setcommandswitch "-v1" |
%assert expr |
Tests a value of a Boolean expression. If the expression evaluates to false TLC will issue an error message, a stack trace and exit, and otherwise the execution will be continued as normal. To enable the evaluation of asserts the command line option "-da " has to be added. |
|
Flow control directives:
if the error is produced by an incorrect configuration that the user needs to correct in the model. If you are adding assert code (i.e., code that should never be reached), use |
|
Creates identifiers (variables). The general form is The :: specifies that the variable being created is a global variable, otherwise, it is a local variable in the current scope (i.e., a local variable in the function).If you need to format the variable, say, within a string based upon other TLC variables, then you should perform a double evaluation as in
or alternately To assign a value to a field of a record you must use a qualified variable expression. See Assigning Values to Fields of Records. |
%createrecord |
Creates records. This command accepts a list of one or more record specifications (e.g., %createrecord NEW_RECORD { foo 1 ; SUB_RECORD {foo 2} } %assign x = NEW_RECORD.SUB_RECORD.fooIf more than one record specification appears, these additional record specifications construct an array of records. %createrecord RECORD_ARRAY { NEW_RECORD { foo 1 } } ... { NEW_RECORD { foo 2 } } ... { NEW_RECORD { foo 3 } } %assign x = RECORD_ARRAY[1].NEW_RECORD.foo If |
%addtorecord |
Adds fields to an existing record. The new fields may be name-value pairs or aliases to already existing records. %addtorecord OLD_RECORD foo 1 If the new field being added is a record, then %createrecord NEW_RECORD { foo 1 } %addtorecord OLD_RECORD NEW_RECORD_ALIAS NEW_RECORD |
%mergerecord |
Adds (or merges) one or more records into another. The first record will contain the results of the merge of the first record plus the contents of all the other records specified by the command. The contents of the second (and subsequent) records are deep copied into the first (i.e., they are not references).%mergerecord OLD_RECORD NEW_RECORDIf there are duplicate fields in the records being merged the original record's fields will not be overwritten. |
%copyrecord |
Makes a deep copy of an existing record. It creates a new record in a similar fashion to %createrecord except the components of the record are deep copied from the existing record. Aliases are replaced by copies.%copyrecord NEW_RECORD OLD_RECORD |
%realformat |
Specifies how to format real variables. To format in exponential notation with 16 digits of precision, use To format without loss of precision and minimal number of characters, use When inlining S-functions, the format is set to concise . You can switch to exponential , but should switch it back to concise when done. |
|
This must appear before the first GENERATE or GENERATE_TYPE function call. This specifies the name of the language as a string, which is being generated as in %language "C" . Generally, this is added to your system target file. |
|
Placed within the .tlc file for a specific record type, when mapped via %generatefile . The syntax is: %implements " Type " " Language ". When inlining an S-function in C, this should be the first non-comment line in the file as in
The next noncomment lines will be %function directives specifying the functionality of the S-function.See the %language and GENERATE function descriptions for further information. |
|
Provides a mapping between a record Type and functions contained in a file. Each record can have functions of the same name, but different contents mapped to it (i.e., polymorphism). Generally, this is used to map a Block record Type to the .tlc file that implements the functionality of the block as in
|
|
Limits the scope of variables to the file in which they are defined. All variables defined after the appearance of %filescope in a file have this property, otherwise they default to global variables.%filescope is useful in conserving memory. Variables whose scope is limited by %filescope go out of scope when execution of the file containing them completes. This frees memory allocated to such variables. By contrast, global variables persist in memory throughout execution of the program. |
|
%include " file .tlc" -- insert specified target file at the current point. Use %addincludepath " directory " to add additional paths to be searched. Be sure to escape backslashes in PC directory names as in "C:\\mytlc" . Note that %include directives behave as if they were in a global context. |
|
Multiple inclusion plus intrinsic loop rolling based upon a specified threshold. This directive can be used by most Simulink blocks which have the concept of an overall block width that is usually the width of the signal passing through the block. An example of the %roll directive is for a gain operation, y=u*k %function Outputs(block, system) Output /* %<Type> Block: %<Name> */ %assign rollVars = ["U", "Y", "P"] %roll sigIdx = RollRegions, lcv = RollThreshold, block,... "Roller", rollVars %assign y = LibBlockOutputSignal(0, "", lcv, sigIdx) %assign u = LibBlockInputSignal(0, "", lcv, sigIdx) %assign k = LibBlockParameter(Gain, "", lcv, sigIdx) %<y> = %<u> * %<k>; %endroll %endfunctionThe %roll directive is similar to %foreach , except it iterates the identifier (sigIdx in this example) over roll regions. Roll regions are computed by looking at the input signals and generating regions where the inputs are contiguous. For blocks, the variable RollRegions is automatically computed and placed in the Block record. An example of a roll regions vector is [0:19, 20:39] , where we have two contiguous ranges of signals passing through the block. The first is 0:19 and the second is 20:39 . Each roll region is either placed in a loop body (e.g., the C Language for statement), or inlined depending upon whether or not the length of the region is less than the roll threshold.Each time through the %roll loop, sigIdx is an integer for the start of the current roll region or an offset relative to the overall block width when the current roll region is less than the roll threshold. The TLC global variable RollThreshold is the general model wide value used to decide when to place a given roll region into a loop. When the decision is made to place a given region into a loop, the loop control variable will be a valid identifier (e.g., "i "), otherwise it will be "" . |
|
The block parameter is the current block that is being rolled. The "Roller" parameter specifies the name for an internal GENERATE_TYPE calls made by %roll . The default %roll handler is "Roller" , which is responsible for setting up the default block loop rolling structures (e.g., a C for loop).The rollVars (roll variables) are passed to "Roller" functions to create the correct roll structures. The defined loop variables relative to a block are
|
|
To roll your own vector based upon the block's roll regions, you need to walk a pointer to your vector. Assuming your vector is pointed to by the first PWork , called name ,
Note: in the above example, |
![]() |
A function that returns a value is defined as %function name ( optional-arguments ) value %endfunction A void function does not produce any output and is not required to return a value. It is defined as %function name ( optional-arguments ) void A function that produces outputs to the current stream and is not required to return a value is defined as %function name ( optional-arguments ) Output For block target files, you can add to your inlined .tlc file the following functions that will get called by the model wide target files during code generation
|
%function (continued)
|
|
|
Multiple inclusion that iterates from 0 to the upperLimit-1 constant integer expression. Each time through the loop, the loopIdentifier, (e.g., x ) is assigned the current iteration value.
Note: The |
%for |
Multiple inclusion directive with syntax%for ident1 = const-exp1, const-exp2, ident2 = const-exp3 %body %break %continue %endbody %endfor The first portion of the If const-exp2 evaluates to |
|
These are used to manage the files that are created. The syntax is%openfile %selectfile %closefile Note that the " filename.ext " is optional. If not specified, streamId and filename will be the same and a variable (string buffer) is created containing the output. The mode argument is optional. If specified, it can be "a" for appending, "r" for reading, or "w" for writing.Note that the special streamId NULL_FILE specifies that no output occur. The special streamId STDOUT specifies output to the terminal.To create a buffer of text, use: %openfile buffer text to be placed in the 'buffer' variable. %closefile buffer Now buffer contains the expanded text specified between the %openfile and %closefile directives. |
|
%generate blk fn is equivalent to GENERATE( blk , fn ) .%generate blk fn type is equivalent to GENERATE( blk , fn , type ) .See GENERATE and GENERATE_TYPE Functions. |
|
%undef var removes the variable var from scope. If var is a field in a record, %undef removes that field from the record. If var is a record array, %undef removes the first element of the array. |
![]() | Exception to Using the Library Functions | Comments | ![]() |