macros - How does a compiler change variable names? -
i've been doing research topic , haven't found concrete answers. let's have these expression in code:
b = 2 … b = b + 5 … b = j + b …
(these simple examples, know aren't realistic)
b
has many different values throughout these lines. in line 1 it's 2
, later on becomes 7
, , more later on it's 7 + j
. compiler need keep track of these different values b
, 1 way rename them. example, when b
redefined b = b+5
, changed b1 = b+5
. last redefinition b2 = j+b1
.
the motivation behind idea involves optimizing program i'm building. involves replacing variables expressions they're related to. if variable redefined, however, character 'b' can mean multiple things @ once. method i'm using keep track of things i've described above, redefining variable names.
is @ how compiler's work? there name this?
i'm trying find out as can process of compiler redefining variables in case of redefining variables.
if helps, believe done @ pre-processing stage of compiling, , believe it's similar concept macro-expansion.
edit: added little more context question.
your hunch correct, many modern compilers use flow analysis rename variables each variable unique. resulting form called "single static assignment" or ssa short.
input:
b = 2 b = b + 5 b = j + b
output:
b1 = 2 b2 = b1 + 5 b3 = j + b2
there additional parts working branches , loops, such as:
input:
if x < 5 b = y + z else b = 2 b = b + 1
output:
if x < 5: b1 = y + z else b2 = 2 b3 = phi(b1, b2) b4 = b3 + 1
the "phi" function selects whichever of inputs live.
this not done during preprocessing, done after code compiled ir, consisting of basic blocks. not similar macro-expansion.
Comments
Post a Comment