What Is Code Replacement?
Code replacement is a technique to change the code that the code generator produces for functions and operators to meet application code requirements. For example, you can replace generated code to meet requirements such as:
Optimization for a specific run-time environment, including, but not limited to, specific target hardware.
Integration with existing application code.
Compliance with a standard, such as AUTOSAR.
Modification of code behavior, such as enabling or disabling nonfinite or inline support.
Application- or project-specific code requirements, such as:
Elimination of system header files.
Elimination of calls to
Use of BLAS.
Use of a specific BLAS.
To apply this technique, configure the code generator to apply a code replacement library (CRL) during code generation. By default, the code generator does not apply a code replacement library. You can choose from libraries that MathWorks® provides and that you create and register by using the Embedded Coder® product. The list of available libraries depends on:
Installed support packages.
System target file, language, language standard, and device vendor configuration.
Whether you have created and registered libraries, using the Embedded Coder product.
Libraries that include GNU99 extensions are intended for use with the GCC compiler. If you use one of those libraries with another compiler, generated code might not compile.
Code Replacement Libraries
A code replacement library consists of one or more code replacement tables that specify application-specific implementations of functions and operators. For example, a library for a specific embedded processor specifies function and operator replacements that optimize generated code for that processor.
A code replacement table contains one or more code replacement entries, with each entry representing a potential replacement for a function or operator. Each entry maps a conceptual representation of a function or operator to an implementation representation and priority.
|Table Entry Component||Description|
Identifies the table entry and contains match criteria for the code generator. Consists of:
Specifies replacement code. Consists of:
Defines the entry priority relative to other entries in the table. The value can range from 0 to 100, with 0 being the highest priority. If multiple entries have the same priority, the code generator uses the first match with that priority.
When the code generator looks for a match in a code replacement library, it creates and populates a call site object with the function or operator conceptual representation. If a match exists, the code generator uses the matched code replacement entry populated with the implementation representation and uses it to generate code.
The code generator searches the tables in a code replacement library for a match in the order that the tables appear in the library. If the code generator finds multiple matches within a table, the priority determines the match. The code generator uses a higher-priority entry over a similar entry with a lower priority.
Code Replacement Terminology
|Cache hit||A code replacement entry for a function or operator, defined in the specified code replacement library, for which the code generator finds a match.|
|Cache miss||A conceptual representation of a function or operator for which the code generator does not find a match.|
|Call site object||Conceptual representation of a function or operator that the code generator uses when it encounters a call site for a function or operator. The code generator uses the object to query the code replacement library for a conceptual representation match. If a match exists, the code generator returns a code replacement object, fully populated with the conceptual representation, implementation representation, and priority, and uses that object to generate replacement code.|
|Code replacement library||One or more code replacement tables that specify application-specific implementations of functions and operators. When configured to use a code replacement library, the code generator uses criteria defined in the library to search for matches. If a match is found, the code generator replaces code that it generates by default with application-specific code defined in the library.|
|Code replacement table||One or more code replacement table entries. Provides a way to group related or shared entries for use in different libraries.|
|Code replacement entry||Represents a potential replacement for a function or operator. Maps a conceptual representation of a function or operator to an implementation representation and priority.|
|Conceptual argument||Represents an input or output argument for a function or operator
being replaced. Conceptual arguments observe naming conventions (|
Represents match criteria that the code generator uses to qualify functions and operators for replacement. Consists of:
|Implementation argument||Represents an input or output argument for a C or C++ replacement function. Implementation arguments observe C/C++ name and data type specifications.|
Specifies C or C++ replacement function prototype. Consists of:
|Key||Identifies a function or operator that is being replaced. A
function name or key appears in the conceptual representation of a
code replacement entry. The key |
|Priority||Defines the match priority for a code replacement entry relative to other entries, which have the same name and conceptual argument list, within a code replacement library. The priority can range from 0 to 100, with 0 being the highest priority. The default is 100. If a library provides two implementations for a function or operator, the implementation with the higher priority shadows the one with the lower priority.|
Code Replacement 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.
Code replacement for matrices — Code replacement libraries do not support Dynamic and Symbolic sized matrices.