Mapping TargetLink generated C code back to Simulink blocks

From emmtrix Wiki
Jump to navigation Jump to search

When using TargetLink to generate C code out of Simulink models, the C code contains comments that indicate from which block the C code was generated from.

In the simplest case it looks like this:

  /* Sum: MyModel/SubSystem/Sum */
   res = var1 + var2;

The statement `res = var1 + var2;` is generated from a block `Sum` inside the `SubSystem` of `MyModel`.

TargetLink tries to optimize the code by combining different statements:

  /* Sum: MyModel/SubSystem/Sum
      ## combined ## Sum: MyModel/SubSystem/Sum2 */
   res = (var1 + var2) - var3;

Here, the code of the blocks `Sum` and `Sum1` are combined into a single statement and assigned to the variable `res`. The statement can therefore be mapped partially to each of the Sum blocks.

This can become more complicated with control structures like if blocks:

  /* Switch: MyModel/SubSystem/Switch
     ## combined ## Product: MyModel/SubSystem/product 
     ## combined ## Product: MyModel/SubSystem/product2  */
  if (enable) {
    res = var1 * var 2;
  } else {
    res = var3 * var4;
  }

Here, both product blocks were combined with the if block.

Some parts of the code will be generated to handle array accesses like in this example:

  for (Aux_S32 = 0; Aux_S32 < 75; Aux_S32++) {
    /* Assignment: MyModel/SubSystem/Assignment - output initialization */
	data[Aux_S32] = 0;
  }

While the for loop doesn't have a comment on it's own, the code belongs to the Assignment block as can be extracted from the comment inside the loop. These kind of loops use an iterator variable with a name containing `aux`, usually have a fixed length and are used to perform operations over indices of an array.

Other loops use iterator variables with names based on existing sub systems:

   for (SBS25_For_Iterator_it = 1; SBS25_For_Iterator_it <= 2; SBS25_For_Iterator_it++) {
     /* Sum: MyModel/SubSystem/for_loop/Sum */
     res = var1 + var2;
   }

`SBS25_For` is the name of a subsystem that is defined like this at the top of the file:

*** SBS25     MyModel/SubSystem/for_loop

It is typically a for loop used in the model that can contain several internal blocks.