Explicit Cast Insertion Transformation: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 1: Line 1:
Code sinking is an optimization technique used in source-to-source compilers to improve execution efficiency by moving computations to less frequently executed parts of the code. This transformation reduces redundant calculations by relocating expressions outside loops or behind conditional statements, thereby minimizing execution overhead. By strategically repositioning code, code sinking enhances performance without altering program behavior.
Explicit cast insertion is a transformation that introduces explicit type casts in situations where implicit type conversions would otherwise occur. By making type conversions explicit, this technique improves code clarity, prevents unintended behavior, and enhances static code analysis. It is particularly useful in strongly typed languages, ensuring that conversions are intentional and reducing ambiguity in expressions involving mixed data types. While preserving program semantics, explicit cast insertion helps avoid compiler warnings and improves maintainability by making type changes more transparent.
==Code Sinking Transformation in emmtrix Studio==
==Explicit Cast Insertion Transformation in emmtrix Studio==
emmtrix Studio can implement code sinking using #pragma directives or via the GUI. Code sinking is a transformation that tries to move code parts to valid positions that are executed less often.
emmtrix Studio can implement explicit cast insertion using #pragma directives or via the GUI. Cast insertion is a transformation that inserts explicit type casts in situations where implicit type conversion will take place. The transformation preserves the code-semantics.
===Typical Usage and Benefits===
===Typical Usage and Benefits===
The transformation is used to optimize the runtime of the application by moving code parts out of loops or behind conditions to reduce the number of times the code is executed.
The transformation is used to generate clearer code and to increase capabilities of the code analysis.
===Example===
===Example===
The following code tests code sinking transformation applied to main function.<syntaxhighlight lang="c">
{| class="wikitable"
#include <stdio.h>
|-
| <syntaxhighlight lang="c">
/* The following code tests cast insertion transformation applied to main function.
* In the given example, in expressions sum(a, b), if (a) and x ? a : b, there is implicit type conversion.
* Explicit type casts are inserted in the relevant places.
*/ 


#pragma EMX_TRANSFORMATION CodeSinking
int sum(int num1, int num2) {
    return num1 + num2;
}
#pragma EMX_TRANSFORMATION ExplicitCastInserter
int main(void) {
int main(void) {
     int n = 10;
     char a = 5;
     for (int i = 0; i < 2; ++i) {
    char x = 's';
         int x = n * n;
    float b = 3.14159;
        int y = n * n * n;
     int c;
        int z;
    sum(a, b);
        if (i == 0) {
    if (a) {
            z = x;
         printf(” % c\ n”, a);
        } else {
    }
            z = y;
    c = x ? a : b;
        }
    return 0;
         printf(” % d\ n”, z);
}
</syntaxhighlight>
|<syntaxhighlight lang="c">
/* The following code is the generated code after the transformation has been applied.
*/
 
int sum(int num1, int num2) {
    return num1 + num2;
}
int main() {
    char a = (char) 5;
    char x = (char)
    's';
    float b = (float) 3.14159;
    int c;
    sum((int) a, (int) b);
    if ((_Bool) a) {
         printf(” % c\ n”, a);
     }
     }
    c = (int)((_Bool) x ? (float) a : b);
    return 0;
}
}
</syntaxhighlight>
</syntaxhighlight>
===Parameters===
Following parameters can be set (each description is followed by keyword in pragma-syntax and default value):
{| class="wikitable"
|+
!Id
!Default Value
!Description
|-
|<code>threshold</code>
|1.45
|'''Threshold''' defines the factor by which the number of executions should be reduced in order for an expressions to be moved;
|-
|<code>conditional</code>
|false
|'''Add conditional variables if necessary''' adds additional if blocks that are required for some code movements
|-
|<code>expected_moved_exprs</code>
| -1
|'''Expected moved expressions''' can be used for testing purposes by inserting the number of expected moved expressions. Creates an error if the numbers differ, does nothing when set to -1;
|}
|}
===Note===
[[Category:Code Transformation]]
*To move functions from math.h, use the [[Idiom Recognizer]] transformation to identify the functions and apply the code sinking transformation afterwards.
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu