Inline State Functions in Generated Code - MATLAB & Simulink (original) (raw)

Main Content

Inlined Generated Code for State Functions

By default, the code generator uses an internal heuristic to determine whether to inline generated code for state functions. The heuristic takes into consideration an inlining threshold. As code grows and shrinks in size, generated code for state functions can be unpredictable.

If your model includes Stateflow® objects and you have rigorous requirements for traceability between generated code and the corresponding state functions, you can override the default behavior. Use the state property Function Inline Option to explicitly force or prevent inlining of state functions.

What Happens When You Force Inlining

If you force inlining for a state, the code generator inlines code for state actions into the parent function. The parent function contains code for executing the state actions, outer transitions, and flow charts. It does not include code for empty state actions.

What Happens When You Prevent Inlining

If you prevent inlining for a state, the code generator produces these static functions for state foo.

Function Description
enter_atomic_ foo Marks foo active and performs entry actions.
enter_internal_ foo Calls default paths.
inner_default_ foo Executes flow charts that originate when an inner transition and default transition reach the same junction inside a state.The code generator produces this function only when the flow chart is complex enough to exceed the inlining threshold.In generated code, Stateflow software calls this function from both theenter_internal_ foo and foo functions.
foo Checks for valid outer transitions and if none, performs during actions.
exit_atomic_ foo Performs exit actions and marks foo inactive.
exit_internal_ foo Performs exit actions of the child substates and then exits_foo_.

Suppose the following chart is in model M.

If you prevent inlining for state A, the code generator produces this code.

static void M_inner_default_A(void); static void M_exit_atomic_A(void); static void M_A(void); static void M_enter_atomic_A(void); static void M_enter_internal_A(void);

How to Set the State Function Inline Option

To set the function inlining property for a state:

  1. Right-click inside the state and, from the context menu, selectProperties.
    The State properties dialog box opens.
  2. For property Function Inline Option, select one of these values.
    Value Behavior
    Inline Forces inlining of state functions into the parent function, as long as the function is not part of a recursion. See What Happens When You Force Inlining.
    Function Prevents inlining of state functions. Generates up to six static functions for the state. See What Happens When You Prevent Inlining.
    Auto Uses internal heuristics to determine whether or not to inline the state functions.
  3. Click Apply.

Best Practices for Controlling State Function Inlining

To Set Function Inline Option Property To
Generate a separate function for each action of a state and a separate function for each action of its substates Function for the state and each substate
Generate a separate function for each action of a state, but include code for the associated action of its substates Function for the state andInline for each substate