Target Language Compiler    

A Look at Inlined and Noninlined S-Function Code

This example focuses on the example S-function sfun_bitop.c in directory matlabroot/simulink/src/. The code generation options are set to allow reuse of signal memory for signal lines that were not set as tunable signals.

The code generated for the bitwise operator block reuses a temporary variable that is set up for the output of the sum block to save memory. This results in one very efficient line of code, as seen here.

There is no initialization or setup code required for this inlined block.

If this block were noninlined, the source code for the S-function itself with all its various options would be added to the generated codebase, memory would be allocated in the generated code for the block's SimStruct data, and calls to the S-function's methods would be generated to initialize, run, and terminate the S-function code. To execute the mdlOutputs function of the S-function, code would be generated like this:

The entire mdlOutputs function is called and runs just as it does during simulation. That's not everything, though. There is also registration, initialization, and termination code for the noninlined S-function. The initialization and termination calls are similar to the fragment above. Then, the registration code for an S-function with just one inport and one outport is 72 lines of C code generated as part of file model_reg.h.

This continues until all S-function sizes and methods are declared, allocated, and initialized. The amount of registration code generated is essentially proportional to the number and size of the input ports and output ports.

A noninlined S-function will typically have a significant impact on the size of the generated code, whereas an inlined S-function can give handcoded size and performance to the generated code.


  Code Generation Process Advantages of Inlining S-Functions