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
Three-address code
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!
{{Short description|Intermediate code used by optimizing compilers}} In [[computer science]], '''three-address code'''<ref>{{Cite book|url=https://archive.org/details/compilersprincip00ahoa/page/466|title=Compilers, principles, techniques, and tools|last=V.|first=Aho, Alfred|date=1986|publisher=Addison-Wesley Pub. Co|others=Sethi, Ravi., Ullman, Jeffrey D., 1942-|isbn=0201100886|location=Reading, Mass.|pages=[https://archive.org/details/compilersprincip00ahoa/page/466 466]|oclc=12285707|url-access=registration}}</ref> (often abbreviated to TAC or 3AC) is an [[intermediate language|intermediate code]] used by [[optimizing compiler]]s to aid in the implementation of [[code-improving transformation]]s. Each TAC instruction has at most three operands and is typically a combination of assignment and a binary operator. For example, <code>t1 := t2 + t3</code>. The name derives from the use of three operands in these statements even though instructions with fewer operands may occur. Since three-address code is used as an intermediate language within compilers, the operands will most likely not be concrete memory addresses or [[processor registers]], but rather symbolic addresses that will be translated into actual addresses during [[register allocation]]. It is also not uncommon that operand names are numbered sequentially since three-address code is typically generated by the compiler. A refinement of three-address code is [[A-normal form]] (ANF). == Examples == In three-address code, this would be broken down into several separate instructions. These instructions translate more easily to [[assembly language]]. It is also easier to detect [[common subexpression elimination|common sub-expressions]] for shortening the code. In the following example, one calculation is composed of several smaller ones: {{col-begin}} {{col-2}} <pre style="overflow: auto;"> # Calculate one solution to the [[Quadratic equation]]. x = (-b + sqrt(b^2 - 4*a*c)) / (2*a) </pre> {{col-2}} <pre style="overflow: auto;"> t1 := b * b t2 := 4 * a t3 := t2 * c t4 := t1 - t3 t5 := sqrt(t4) t6 := 0 - b t7 := t5 + t6 t8 := 2 * a t9 := t7 / t8 x := t9 </pre> {{col-end}} Three-address code may have conditional and unconditional jumps and methods of accessing memory. It may also have methods of calling functions, or it may reduce these to jumps. In this way, three-address code may be useful in [[control-flow analysis]]. In the following C-like example, a loop stores the squares of the numbers between 0 and 9: {{col-begin}} {{col-2}} <syntaxhighlight lang="c" style="margin-top: 1em;"> ... for (i = 0; i < 10; ++i) { b[i] = i*i; } ... </syntaxhighlight> {{col-2}} <pre style="overflow: auto;"> t1 := 0 ; initialize i L1: if t1 >= 10 goto L2 ; conditional jump t2 := t1 * t1 ; square of i t3 := t1 * 4 ; word-align address t4 := b + t3 ; address to store i*i *t4 := t2 ; store through pointer t1 := t1 + 1 ; increase i goto L1 ; repeat loop L2: </pre> {{col-end}} ==See also== {{Portal|Computer programming}} * [[Intermediate language]] * [[Reduced instruction set computer]] * [[Static single-assignment form]] (SSA) ==References== <references /> [[Category:Compiler construction]] [[Category:Articles with example C code]]
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)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Col-2
(
edit
)
Template:Col-begin
(
edit
)
Template:Col-end
(
edit
)
Template:Portal
(
edit
)
Template:Short description
(
edit
)