Main Content

What Is Code Replacement Customization?

Customize how and when the code generator replaces C/C++ code that it generates by default for functions, operators, and blocks 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.

For information about replacing code from some Simulink® blocks, see Block Replacement for Code Optimization.

Code Replacement Match and Replacement Process

When the code generator encounters a call site for a function or operator, it:

  1. Creates and partially populates a code replacement entry object with the function or operator name or key and conceptual arguments.

  2. 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

    • Priority

  3. 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.

  4. Uses the C or C++ replacement function prototype in the code replacement object to generate code.

Code Replacement Customization Limitations

  • 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.

  • 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.

  • coder.replace function — See coder.replace for relevant limitations.

  • coder.dataAlignment function — See coder.dataAlignment for relevant limitations.

Related Topics