To the average developer, writing Solidity feels like standard object-oriented programming. You write contracts, declare state variables, and define functions. But once you hit compile and deploy, that familiar syntax vanishes. What actually executes on the Ethereum Virtual Machine (EVM)?
The EVM is not a CPU; it doesn’t understand high-level control structures, variable names, or inheritance trees. It is a sandboxed, deterministic, stack-based virtual machine operating on pure hexadecimal instructions.
Bridging the gap between your clean .sol file and the raw state changes recorded across thousands of distributed nodes is a complex journey of compilation, bytecode generation, and low-level opcode orchestration. Let’s pull back the curtain and trace that journey step-by-step.
1. The Transformation: From Solidity to Bytecode
When you run solc contract.sol --bin, your Solidity code undergoes several stages of compilation:
- Lexing and Parsing: The compiler converts plain text into an Abstract Syntax Tree (AST), verifying syntax and structure.
- Intermediate Representation (Yul): Modern Solidity compilers translate the AST into Yul, an intermediate assembly-like language. Yul enables aggressive, low-level optimization before emitting machine instructions.
- Bytecode Emission: Yul is finally compiled into a hex string—the EVM bytecode.
2. Creation Bytecode vs. Runtime Bytecode
A common misconception is that the bytecode you send in a deployment transaction is identical to the bytecode stored at the contract’s address. In reality, bytecode exists in two forms:
Creation (Init) Bytecode
This is the payload included in the data field of a deployment transaction. It contains:
- Your constructor logic and parameter initializations.
- The code that sets initial storage variables.
- A final routine that copies the runtime code into memory and executes the
RETURNopcode.
Runtime Bytecode
This is the code that actually lives on-chain. When the creation code finishes running, it returns this subset of bytes to the EVM, which saves it under the newly generated contract address. The constructor code is deliberately discarded—it never executes again and never takes up permanent storage space.
3. The Execution Context: How the EVM Thinks
Once deployed, invoking a contract function loads the runtime bytecode into the EVM. The machine relies on four distinct data regions to execute operations:
| Memory Area | Type | Persistence | Cost (Gas) |
|---|---|---|---|
| Stack | LIFO (Last-In, First-Out) | Ephemeral (Per-call) | Very Cheap |
| Memory | Byte-addressable Linear Array | Ephemeral (Per-call) | Scales quadratically with size |
| Calldata | Read-only byte array | Ephemeral (Transaction inputs) | Cheap (Base gas per byte) |
| Storage | Key-Value mapping ($2^{256}$ slots) | Persistent (On-chain state) | Extremely Expensive |
Because the EVM is a stack-based machine (not register-based like x86 or ARM), nearly all calculations happen on a stack limited to 1,024 elements of 256-bit words. When you add two numbers in Solidity (a + b), the EVM pushes both values onto the stack and executes ADD, which pops the top two items, sums them, and pushes the result back.
4. How the EVM Knows Which Function to Call
The EVM doesn’t have a lookup table for human-readable function names like transfer(address,uint256). Instead, it relies on a mechanism called the Function Dispatcher.
When you call a smart contract, your transaction includes transaction data (calldata). The first 4 bytes of this data form the function selector, derived from:
bytes4(keccak256("transfer(address,uint256)")) // 0xa9059cbb
At runtime, the EVM runs through a generated conditional switch statement at the very beginning of the bytecode:
- Loads the first 4 bytes of calldata using
CALLDATALOADand bit-shifting opcodes. - Compares those 4 bytes against hardcoded selectors compiled into the contract using
EQ. - If a match is found, it executes
JUMPI(conditional jump) to jump the program counter to the internal label where that function’s code begins. - If no match is found, it checks for a
fallback()orreceive()function. If neither exists, it reverts execution viaREVERT.
5. Tracing a Simple Line of Code
Consider this simple state update in Solidity:
uint256 public count = 5;
function increment() public {
count += 1;
}
When increment() is executed, the EVM runs a series of low-level opcodes that roughly translate to the following sequence:
PUSH1 0x00: Pushes slot index0onto the stack (wherecountis stored).SLOAD: Reads the value from persistent storage at slot0(returns5) and places it on the stack.PUSH1 0x01: Pushes1onto the stack.ADD: Pops5and1, adds them, and pushes6.PUSH1 0x00: Pushes slot index0again.SSTORE: Writes the value6into storage slot0.STOP: Successfully halts execution.
6. The Gas Meter: Halting the Machine
Because the EVM is Turing complete, it must prevent infinite loops (like an accidental while(true)). It solves the Halting Problem through Gas.
Every single opcode has an intrinsic gas cost determined by the network protocol:
- Trivial stack operations like
PUSH1orPOPcost roughly 3 gas. - Writing to an empty storage slot via
SSTOREcan cost upwards of 20,000 gas because it requires state persistence across all network nodes.
The EVM maintains a gas counter throughout execution. Before executing an opcode, the machine checks whether remaining gas is sufficient. If the counter hits zero, execution immediately stops, an Out of Gas exception is triggered, and all state modifications are reverted, while the miner/validator keeps the paid transaction fee.
Conclusion: Why Understanding the EVM Matters
Treating Solidity as a black box works when you are just starting out. But as you begin writing complex protocols, building secure contracts, and optimizing transaction fees, the EVM abstraction begins to leak.
Understanding how storage packing works, why calldata is cheaper than memory, how function dispatchers introduce execution overhead, and what happens during low-level DELEGATECALL operations separates standard Solidity developers from elite smart contract engineers. By understanding the virtual machine beneath your code, you gain the power to write faster, cheaper, and inherently more secure protocols.