[Ethereum] What does bytecode of blank contract do

compilationevmopcodesolidity

When I compile a blank contract:

contract A {}

with

solc --optimize --optimize-runs 300000 --opcodes test.sol

I get:

PUSH1 0x60 PUSH1 0x40 MSTORE PUSH1 0x6 DUP1 PUSH1 0x10 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN PUSH1 0x60 PUSH1 0x40 MSTORE STOP

What is it? As I understand this bytecode not the code of contract, but code which during execution generate contract. But I don't get, what is going on.

Probably

PUSH1 0x60 PUSH1 0x40 MSTORE

is a pattern to allocate 64 bytes(0x40) in memory. If yes, why we need manually allocate it?

There are a few lines in Yellow paper about separation 'code' and 'data'. I can see it, when run solc with --asm-json. But how this two parts actually interact?

I feel like I missed a very important paper or manual where everything is cleared. Where can I get it?

To summarize:

  1. What does bytecode of blank contract do? If it is possible, I need explanation for each opcode.
  2. How interact code and data sections of contract?
  3. Where I can find full info about how evm works?

Best Answer

The "data" section mentioned in the yellow paper is the part that follows the PUSH instruction, i.e. the yellow paper only talks about push data. What you see in the assembly output is a higher-level concept.

The (init code of the) blank contract copies the final contract code from code into memory and then returns that.

Both the final contract code and the init code start with storing the so-called "free memory pointer" at memory position 0x40. This pointer tells you where the next free chunk of memory can be allocated. In the case of this contract, this is totally unnecessary and will probably be optimized away by the next improvement phase of the optimizer.

Related Topic