Customize how and when the code generator replaces C/C++ code that it generates by default for functions and operators by developing a custom code replacement library. You can develop libraries interactively with the Code Replacement Tool or programmatically.
Develop libraries tailored to specific application requirements
Add identifiers to the list of reserved keywords the code generator considers during code replacement
Customize the code generator’s match and replacement process for functions
To get started, Quick Start Code Replacement Library Development - Simulink®.
When the code generator encounters a call site for a function or operator, it:
Creates and partially populates a code replacement entry object with the function or operator name or key and conceptual arguments.
Uses the entry object to query the configured code replacement library for a conceptual representation match. The code generator searches the tables in a code replacement library for a match in the order that the tables appear in the library. When searching for a match, the code generator takes into account:
Conceptual name or key
Arguments, including quantity, type, type qualifiers, and complexity
Algorithm (computation method)
Fixed-point saturation and rounding modes
When a match exists, the code generator returns a code replacement object, fully populated with the conceptual representation, implementation representation, and priority. If the code generator finds multiple matches within a table, the entry priority determines the match. The priority can range from 0 to 100. The highest priority is 0. The code generator uses a higher-priority entry over a similar entry with a lower priority.
Uses the C or C++ replacement function prototype in the code replacement object to generate code.
Code replacement verification — It is possible that code replacement behaves differently than you expect. For example, data types that you observe in code generator input might not match what the code generator uses as intermediate data types during an operation. Verify code replacements by examining generated code. See Verify Code Replacement Library.
Code replacement for matrices — Code replacement libraries do not support the replacement of functions that have variable-size inputs.
Tokens in file paths—You can include tokens in file paths when specifying build information for a code replacement entry by using the programming interface only. The ability to include tokens is not available from the Code Replacement Tool. See Specify build information in Define Code Replacement Library Optimizations.
Addition and subtraction operation replacements—See Addition and Subtraction Operator Code Replacement for relevant limitations.
Not supported for
Arguments associated with a built-in storage class with
DataScope set to
Exported or the
imported built-in storage class
Model reference parameters
Exported functions in Stateflow® charts
Replaced functions that are generated with C function prototype control or C++ class I/O arguments step method and that use root-level I/O variables
Replaced functions that are generated with the AUTOSAR system target file and that use root-level I/O or AUTOSAR inter-runnable access functions
If the following conditions exist, the code generator includes data alignment
directives for root-level I/O variables in the
ert_main.cpp file it produces:
Compiler supports global variable alignment
Generate an example main program (select Configuration Parameters > Generate an example main program)
Generate a reusable function interface for the model (set Configuration Parameters > Code Generation > Interface > Code interface packaging to
Function uses root-level I/O variables that are passed in as individual
arguments (set Configuration Parameters > Code Generation > Interface > Pass root-level I/O asto
Replaced function uses a root-level I/O variable
Replaced function imposes alignment requirements
If you discard the generated example main program, align used root-level I/O variables correctly.
If you choose not to generate an example main program in this case, the code generator does not replace the function.
If a replacement imposes alignment requirements on the shared utility interface arguments, the code generator does not honor data alignment. Under these conditions, replacement does not occur. Replacement is allowed if the registered data alignment type specification supports alignment of local variables, and the replacement involves only local variables.
If user registered alignment specifications do not support structure field
Simulink.Bus objects is not supported
Simulink.Bus is imported.
When aligning a
Simulink.Bus data object, the elements
in the bus object are aligned on the same boundary. The boundary is the lowest
common multiple of the alignment requirements for each individual bus
When you specify alignment for functions that occur in a model reference
hierarchy, and multiple models in the hierarchy operate on the same function data,
the bottommost model dictates alignment for the rest of the hierarchy. If the
alignment requirement for a function in a model higher in the hierarchy cannot be
honored due to the alignment set by a model lower in the hierarchy, the
replacement in the higher model does not occur. In some cases, an error message is
generated. To work around this issue, if the shared data is represented by a bus
or signal object, manually set the alignment property on the shared data by
setting the alignment property of the
It is your responsibility to honor the
setting for storage classes that you create.
coder.replace function — See
coder.replace for relevant limitations.