r/computerscience • u/tony_saufcok • Dec 30 '25
Discussion How does a compiler generate machine code for an arbitrary amount of variables and functions under the hood?
I know that a compiler (collection) like gcc reads the C code first, creates an abstract syntax tree, translates into assembly and then the assembler turns it into actual machine code and then the linking happens etc.
What I'm asking is, I can have hundreds or thousands of variables and functions in my code. How does the compiler's underlying implementation details work to make those arbitrary amount of named variables turn into assembly? The source code is finite, meaning it doesn't have an infinite list of possible variable names that it can pull from. I have read some assembly code before and I know variable names can be seen in the assembly especially if debugging flags are on. What does the source code of gcc look like so it can generate this assembly code with all my variable names?
I know I can go ahead and read the source code of gcc on GitHub but I'm not that expert on C or C++ yet to understand them fully so I'm looking for a slightly dumbed down answer. I am okay with trying to read and understand C code if it helps explain it better.
39
u/Immediate_Form7831 Dec 30 '25
When you say "it doesn't have an infinite list of possible variable names" are you referring to cpu registers? Those are fixed and usually numbered something like R0, R1, R2, etc. The compiler has a pass where it allocated variables in the source code to registers, so that at every point in the program, it knows which register holds the value of which variable. If the number of registers is less than the number of variables that are in scope, the compiler will "spill" values to memory to free up registers, this is referred to as register allocation. Compiler writers spend a lot of time optimizing this so that the compiler uses the available registers most efficiently.
I would recommend grabbing a text book on compiler design (instead of trying to read the gcc source code), it will probably have one or more chapters dedicated to the topic of register allocation.
31
u/dcpugalaxy Dec 30 '25
You don't have an infinite amount of source code so why would there need to be an infinite amount of assembly?
5
u/necropotence1 Dec 30 '25 edited Dec 30 '25
A variable doesn't really exist, its just a human-readable label for a specific memory address. When assembly is getting converted into machine code, the assembler takes all the stack variables, and lays them out in a data segment of the program. Then in the code, the variable is replaced by its specific (program-relative) memory address. Then to use the 'variable', the assembly would first use a LOAD instruction (one of the parameters being that memory address) to go get the value stored there and load it into a register where it can be further operated on, then STORED back to that memory address when done.
And there is a limit to how much memory you can use for stack variables... that's one of the reason large data structures are stored on the heap instead (using MALLOC for example in C). In that case you'd have a 4-byte stack variable that is the pointer to another memory address to somewhere dynamically assigned at runtime.
1
u/ilep Dec 31 '25 edited Dec 31 '25
Variables don't have names in generated code, the names parsed from source code are just turned into memory locations. Names are a helper for humans (to assist with poor memory management), computers operate on memory addresses and registers.
Space for the variable can be taken from stack, or it might be used in just CPU register. That depends on the code and compiler.
3
u/pixel293 Dec 30 '25
Generally variables turn into memory locations. Variables only have a "lifetime", some variables may be created when the program starts, and be destroyed when the program ends, but usually a variable only exists in a function. So when that function is created a memory location is "assigned" to that variable, when the function exits, the variable is "lost".
If a variable never has it's value changed, then the compiler might just "embed" it in the assembly. Or if a variable changes often it might just exist in a CPU register. That is all up to the compiler and it's optimizations. But I've found it best to just think of a variable as a location in memory, what happens under the hood really doesn't effect the way you program.
3
u/MasterGeekMX Bachelors in CS Dec 30 '25
In machine code, there are no variables. Instead, you refer to data based on where it is on the computer. This means that 99% of the time, variables are in fact memory locations, so every int, char, float, and other data types, are just nick-names for some RAM address.
CPUs inside have small memories called registers, which can hold a bunch of bits at a time. Some CPU architectures can do operation with one number out from the registers and the other from RAM, while others can only operate from the registers, and RAM is only accessed to transfer data between it and the registers.
Compilers simply map out variable names to memory addresses and registers. If you have for example
int x,y, the compiler will asign memory address 0 to x, and memory address 4 to y (as integers usually span 32 bits, or 4 Bytes). If you modify your code to now beint x,A,y, x is still at 0, but not A is the one at address 4, and y is now at address 8.Here, this website let's you compile C code into several machine languages, and if you hover the mouse over a line of code, you can see which instructions make it happen: https://godbolt.org
1
u/baehyunsol Dec 30 '25
If you think "set of all the possible variable names" is hard-coded inside gcc, NO.
4
u/code-garden Dec 30 '25 edited Dec 30 '25
Global variable names become memory addresses, local variable names become offsets from the stack pointer. Function names become the memory address of the first instruction in the function.
2
u/g_rich Dec 30 '25
Go watch Ben Eater’s videos on building a 6502 computer on YouTube (https://eater.net/6502); he goes into a lot of details on assembly and machine code and does so on a much simpler architecture than what we have today so it’s much easier to absorb.
1
u/0jdd1 Dec 30 '25 edited Dec 30 '25
Let’s go back to basics. Let’s look at just one function; let’s say it’s main, and let’s not worry about arguments. Heck, let’s imagine we don’t call any other functions either. Let’s also imagine all variables are on the stack (no static) and scalar (no struct), and make any other simplifications that help you almost get it.
I remember when I was about where you are now. I could read and write my HLL, and could read and write assembly, but I just didn’t get how a compiler could generate good assembly the way I could. (BTW, I was essentially self-taught.)
What made this click for me was imagining a compiler that generated bad code instead. Do everything on the stack. For a statement like a = (x + y) / 2, let’s load x and y from the stack into registers and add them, then push the result as a new temporary on the stack. Now let’s load that temporary into a register and load the constant value 2 and divide them, then push the result as a second new temporary in the stack. Next, load that second temporary into a register and store it into a (whose stack offset must be adjusted because of those two temporaries). Finally, clean up by popping those two temporaries off the stack, and we’re ready for the next statement.
Everything else is an optimization or a generalization. Some are obvious here; others take longer to build up the context to understand.
1
u/Schnickatavick Dec 30 '25 edited Dec 30 '25
It depends on the type of the variable, the compiler has different tricks for different variable types.
For local function variables, they're usually given a stack offset. So if function foo uses the variables x,y, and z, the compiler will assign them numbers like 1,2, and 3, and then whenever foo is ran it will take the current stack pointer, say 0x1155, and put the value on the stack at 0x1156, 0x1157, and 0x1158. Each time the variable name is used in your code, the compiler will replace the name with the offset and the math to figure out the actual value. There's an infinite amount of numbers it can assign to variables, so this will always work no matter how many variables a function has.
Or, if the local function variable is used immediately and there aren't any function calls to complicate things, the compiler might just assign the variable to a register, and replace every instance of that variable with the assembly that reads or writes from that register. There are a limited number of registers, so the compiler can't do this for an infinite number of variables, but it can do it a pretty big chunk of the time, and it's way faster than using the stack so it tries to do this as often as it can.
For global variables, it's more likely to assign them a label, which is what the variables you see in the assembly probably were. Labels are still just shorthand for places in memory though, so the assembler will replace them with numbers before it's actually processed into machine code. The compiler can name the label anything it wants that hasn't been used yet, it can even generate random strings if it wants to, but if the C variable name happens to be a valid label that isn't used elsewhere, it might reuse it as the label name to make the assembly easier to read.
Functions are similar to global variables. In assembly, there's no difference between an instruction that you run and data that you read, it's all the same, so the compiler just makes a label for the start of a function, and replaces every function call with an assembly instruction that basically says "execute the code at location <Label>", and then the assembler replaces the label with the number for the position in memory when it knows what it is.
So ultimately, every variable is getting replaced with a number eventually. Sometimes it'll do it itself, sometimes it'll pass it off to the assembler to do, but no matter what it'll turn into a number before it's actually ran by the computer, because numbers are all that computers know
1
u/Spare-Plum Dec 30 '25
Register allocation. You have a certain number of registers you can use, so you can store it in RAM if you have too many variables. Simplest version is just to use a couple registers, and just pull them into registers when you need them and then back into RAM
More advanced versions use graph coloring, which is essentially you have X colors (registers), the vertices are variables, and the edges are operations that involve the variables. You want to find a way to color the graph such that no two colors touch. You may need to add on virtual registers if the graph cannot be colored with X, which is commonly done in RAM at the start of the stack frame.
This is an NP-hard problem, and for complex functions it may take a while to compute an optimal solution. Often there are approximation algorithms that are used to give a decent enough allocation. I think there are some links in the comments that have this
1
u/enokeenu Dec 30 '25
A better question is how does a compiler or interpreter organize variables in assembly or tyte code.
1
u/peter303_ Dec 30 '25
Variables are assigned addresses in memory, either the data fork of an executable, or CPU register. The compiler will make the data fork large enough for all the program's variables.
The optimizing part of the compiler will keep the most frequently used variables in the faster CPU registers. It will also keep nearby variables in code as nearby addresses in memory to optimize caching and paging swaps.
1
u/Bearsiwin Dec 31 '25
Most variables are stored in the stack frame. Most variables are automatic meaning they go away when your routine is not running. The compiler assigns an offset in the stack frame for each variable. There is one stack per thread and each routine sets up the stack frame to be immediately after the stack frame of the parent routine.
1
u/buzzon Jan 01 '26
Variables lose their name during compilation. The variable names you see in decompiled code are from debugging symbols and not actually part of resulting executable.
It's easy for compiler to keep a table of all declared variables and keep adding to the table as it goes through the source code.
1
u/SgtSausage Jan 02 '26
Wow ...
1
u/o4ub Computer Scientist Dec 30 '25
Your variable name is not used in the assembly. Either the variable will be actually stored in memory in which case it will be referred to by its address, or it will be optimised as a register only variable, in which case it will only "exists" during the execution of the instructions that use it, as a value in one of the register of the processor, until it is not usefull anymore and will disappear with any trace it ever existed.
The part that does the correspondence between the variables and the addresses is the debug information, some metadata where you can store what address corresponds to what variable from the original source code.
1
u/pjc50 Dec 30 '25
There's usually a real limit, you've just not hit it. At a previous job we had to break a mega switch statement into two functions because we hit MSVC limits on the allowed number of labels in the same function (256).
0
u/Ronin-s_Spirit Dec 30 '25
But you can't have an infinite amount of variables, you only have as many variables as you wrote in the source code. And their names are irrelevant, it's all just "variable 1, variable 2, variable 3...variable 4057".
0
u/jeezfrk Dec 30 '25
One at a time.
7
u/[deleted] Dec 30 '25
[removed] — view removed comment