Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Use-define chain
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Execution example for def-use-chain== This example is based on a Java algorithm for finding the [[Greatest common divisor|gcd]]. (It is not important to understand what this function does.) <syntaxhighlight lang="c" line> /** * @param(a, b) The values used to calculate the divisor. * @return The greatest common divisor of a and b. */ int gcd(int a, int b) { int c = a; int d = b; if (c == 0) return d; while (d != 0) { if (c > d) c = c - d; else d = d - c; } return c; } </syntaxhighlight> To find out all def-use-chains for variable d, do the following steps: #Search for the first time the variable is defined (write access). #: In this case it is "{{code|1=d=b}}" (l.7) # Search for the first time the variable is read. #: In this case it is "{{code|1=return d}}" # Write down this information in the following style: [name of the variable you are creating a def-use-chain for, the concrete write access, the concrete read access] #: In this case it is: {{code|1=[d, d=b, return d]}} Repeat these steps in the following style: combine each write access with each read access (but NOT the other way round). The result should be: <syntaxhighlight lang="c" line> [d, d=b, return d] [d, d=b, while(d!=0)] [d, d=b, if(c>d)] [d, d=b, c=c-d] [d, d=b, d=d-c] [d, d=d-c, while(d!=0)] [d, d=d-c, if(c>d)] [d, d=d-c, c=c-d] [d, d=d-c, d=d-c] </syntaxhighlight> You have to take care, if the variable is changed by the time. For example: From line 7 down to line 13 in the source code, {{mono|d}} is not redefined / changed. At line 14, {{mono|d}} could be redefined. This is why you have to recombine this write access on {{mono|d}} with all possible read accesses which could be reached. In this case, only the code beyond line 10 is relevant. Line 7, for example, cannot be reached again. For your understanding, you can imagine 2 different variables {{mono|d}}: <syntaxhighlight lang="c" line> [d1, d1=b, return d1] [d1, d1=b, while(d1!=0)] [d1, d1=b, if(c>d1)] [d1, d1=b, c=c-d1] [d1, d1=b, d1=d1-c] [d2, d2=d2-c, while(d2!=0)] [d2, d2=d2-c, if(c>d2)] [d2, d2=d2-c, c=c-d2] [d2, d2=d2-c, d2=d2-c] </syntaxhighlight> As a result, you could get something like this. The variable {{mono|d1}} would be replaced by {{mono|b}} <syntaxhighlight lang="c" line> /** * @param(a, b) The values used to calculate the divisor. * @return The greatest common divisor of a and b. **/ int gcd(int a, int b) { int c = a; int d; if (c == 0) return b; if (b != 0) { if (c > b) { c = c - b; d = b; } else d = b - c; while (d != 0) { if (c > d) c = c - d; else d = d - c; } } return c; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)