From 81dd96e4f429b349b81ae98cee02f9289fb908b1 Mon Sep 17 00:00:00 2001 From: nmlgc Date: Tue, 5 Oct 2021 01:50:58 +0200 Subject: [PATCH] [Research] Discover how `SCOPY@` disables Turbo C++ stack cleanup optimization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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]. --- Research/Borland C++ decompilation.md | 30 +++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Research/Borland C++ decompilation.md b/Research/Borland C++ decompilation.md index dd64ea9a..4483323e 100644 --- a/Research/Borland C++ decompilation.md +++ b/Research/Borland C++ decompilation.md @@ -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