[Research] Discover how `SCOPY@` disables Turbo C++ stack cleanup optimization

Yup, it's a compiler bug, and it removes a small bit of freedom as far
as decompilation order is concerned. In particular, this means that we
can't do TH01's continue and pause menus before having decompiled the
bomb animation.
Would have been nice to pad out the previous push with those, but
instead, I had to spend way too much time figuring *this* out…

Completes P0161, funded by [Anonymous].
This commit is contained in:
nmlgc 2021-10-05 01:50:58 +02:00
parent 44d1389835
commit 81dd96e4f4
1 changed files with 26 additions and 4 deletions

View File

@ -429,10 +429,6 @@ foo_c(100, 200); // PUSH 200; PUSH 100
### `-O` (Optimize jumps)
Also merges individual `ADD SP, imm8` or `POP CX` stack-clearing instructions
after `__cdecl` function calls into a single one with their combined parameter
size.
Inhibited by:
* identical variable declarations within more than one scope the
@ -459,6 +455,24 @@ Inhibited by:
* inlined calls to empty functions
`-O` also merges individual `ADD SP, imm8` or `POP CX` stack-clearing
instructions after `__cdecl` function calls into a single one with their
combined parameter size.
* **[Bug:]** Any emitted call to `SCOPY@` will disable this feature of `-O` for
all generated code in a translation unit that follows the `SCOPY@` call.
This can explain why a function might seem impossible to decompile with the
wrong translation unit layout. If it
* *doesn't* contain the stack-clearing optimization,
* but *does* definitely contain optimized jumps,
* which couldn't be reproduced with the slight jump optimization provided by
`-O- -y`,
the translation unit is simply missing a `SCOPY@` before the function in
question.
### `-y` (Produce line number info)
Provides its own kind of slight jump optimization if combined with `-O-`. Yes,
@ -650,3 +664,11 @@ contains one of the following:
**Workaround**: Not happening when compiling via TASM (`-B` on the command
line, or `#pragma inline`).
* Any emitted call to `SCOPY@` will disable the stack cleanup optimization
generated by [`-O`](#-o-optimize-jumps) for all generated code in a
translation unit that follows the `SCOPY@` call.
----
[Bug:]: #compiler-bugs