Email or username:

Password:

Forgot your password?
Top-level
Bee O'Problem

@aeva @mdiluz but where does the call to unreachable come from? Your logic only explains the loop being optimized out unless I'm missing something.

6 comments
RAOF

@beeoproblem @aeva @mdiluz The reasoning I've seen is:
You generate code for main() and unreachable(). You use the UB to dead-code eliminate main(). Now the code for unreachable() is where the code for main() was; ie: it has become the entrypoint.

aeva

@RAOF @beeoproblem @mdiluz if you're missing a main function other functions don't get promoted to main. I tested it out in godbolt earlier, what happens is the ret instruction in main gets optimized out, thus the program counter just advances to the next instruction in memory which is the other function. The entry points for main and the other function still exist.

aeva

@RAOF @beeoproblem @mdiluz gcc and msvc both do what you'd expect. Clang and zig's c++ compiler both optimize away the loop and the return.

Marqin

@beeoproblem @aeva @mdiluz the return call from main() got optimised away and so the CPU doesn’t jump away, and keeps taking further instructions from the assembly, which are the body of unreachable()

Go Up