Andy’s Blog and Tools

Syndicate content
Delphi, C++Builder and other thoughts
Updated: 1 hour 53 min ago

Technical the same but different generated code

Tue, 10/31/2017 - 02:12

While debugging the String4D code to hunt down a bug in the CompilerSpeedPack, I saw a lot of CopyRecord/FinalizeRecord calls with a try/finally that the compiler generated.

If you have a record with managed fields (string, interface, …) and use it as a function return type the Delphi compiler will change the function into a procedure with an extra var-parameter. So you would think that the compiler treats the result-parameter like a normal var-parameter, but that isn’t the case. The compiler will generate code that guarantees that the result record isn’t changed if the called function throws an exception. For this is adds a temporary record that is used for the function result and then copies it to the target record.

type TMyRec = record Value: string; end; function InternalGetRec: TMyRec; begin Result.Value := 'Hello'; end; function GetRec: TMyRec; begin Result := InternalGetRec; end; procedure Test; var R: TMyRec; begin R := GetRec; end;

The compiler rewrites the “Test” function to:

procedure Test; var R, TempR: TMyRec; begin try GetRec(TempR); CopyRecord(TempR, R, TypeInfo(TMyRec)); finally FinalizeArray([TempR, R], TypeInfo(TMyRec)); end; end;

The same happens if you assign another function’s record result value to your own record result value. The compiler rewrites the “GetRec” function’s code to:

function GetRec: TMyRec; var TempResult: TMyRec; begin try InternalGetRec(TempResult); CopyRecord(TempResult, Result, TypeInfo(TMyRec)); finally FinalizeRecord(TempRecord, TypeInfo(TMyRec)); end; end;

Because the compiler assumes that you may want to use “Result” in the function after the call, it has to guarantee that it is unchanged if an exception is thrown. But if it is the last statement in the function and not secured by an explicit try/finally/except where “Result” is used again, an optimization could be to omit the temporary record, making the code a lot faster.

Categories: News, Blogs, and Tips

IDE Fix Pack 6.1 released

Sun, 10/29/2017 - 09:36

With the release of IDE Fix Pack 6.1, the Compiler Speed Pack not only makes the compiler compile faster but it can now also change the generated code, something that IDE Fix Pack has never done before. For this, new command line compiler options are introduced. They all start with “-x” (eXtension) followed by the Compiler Speed Pack option (-x-ff -x-fdi -x-fvs -x-fpr) and if you want to use them from the command line compiler you need to use fastdcc32/fastdcc64.
You can specify these options in the “Project/Options…” dialog under “Delphi Compiler/Compiling/Additional options to pass to the compiler”. You may need to rebuild the project to see an effect as only then the compiler will generate new code.

Windows 10 Creators Update 1703 caused issues with all Delphi programs, libraries and packages because it changed how Windows loads imported DLLs in such a way that it causes performance issues and can crash the debugger. Delphi 10.2 Tokyo Update 2 fixed this by not producing multiple dll import sections for one DLL anymore. IDE Fix Pack 6.1 implements that “feature” for all previous Delphi versions (2009-10.1 Berlin) and extends it to not only eliminate duplicate dll imports but also duplicate delay dll imports.
This patch changes the generated binary, the Win32 and Win64 compiler outputs, and it can be disabled by using the new “-x-fdi-“ option.

The next patch that changes the Win32 code generator is the “fast floating point” option that C++Builder users may know from the “bcc32.exe -ff” option. It removes all “fwait” instructions that the compiler usually emits after floating point operations. Removing “fwait” may cause FPU exceptions to be thrown at the wrong source code line.
This option is disabled by default and can be enabled by specifying the new option “-x-ff”.

When calling virtual methods through an interface the Win32 Delphi compiler has to route that call through a helper function that translates the interface reference into an object reference and calls the virtual method. For this helper the compiler uses the XCHG instruction that has an implicit CPU LOCK.
The new “-x-fvs” / “-x-fvs=1” option replaces the XCHG instruction with an alternative code and if the called virtual function doesn’t use the ECX register for the 3rd parameter, it generates a direct jump into the virtual method.
The “-x-fvs=2” option replaces the XCHG and uses ECX if available but if ECX is not available it keeps the CPU’s “return stack cache” valid by replacing the RET with a JMP instructions. For this it uses stack memory below ESP.

For some functions that meet special conditions the Win32 compiler emits stack frame code that fills the stack with zeros to clear variables with managed types. If there are too many of those the compiler uses a loop and the XCHG instruction to restore the ECX register that is used as the loop counter.
The option “-x-fpr” replaces the XCHG with an alternative code.

 

Changelog:

  • Added: Option -x-ff to enable “fast floating point” (like Borland C++’s -ff command line option)
  • Added: Option -x-fvs and -x-fvs=n to enable fast interface virtual stub (n=1: replace XCHG, n=2: keep the CPU’s return stack buffer in order)
  • Added: Option -x-fpr to remove XCHG from the function prolog code.
  • Added: DLL import table section folding and duplicate name/ordinal elimination, also for delay dll imports
  • Changed: Split “Compiler64.X86″ patch into multiple smaller patches and removed the “Compiler64.X86” patch name
  • Changed: EditorFocusFix now skips the SetActiveWindow call if the mainform (undocked) is not the active window

Download:

Name IDE Version File Size Downloads Added IDE Fix Pack 6.1.1 2009 (UP4) IDEFixPack2009Reg61.1.7z 188.6 KB 34 times 2017-10-30 IDE Fix Pack 6.1.1 2010 (UP5) IDEFixPack2010Reg61.1.7z 185.1 KB 42 times 2017-10-30 IDE Fix Pack 6.1.1 XE (UP1) IDEFixPackXEReg61.1.7z 172.72 KB 41 times 2017-10-30 IDE Fix Pack 6.1.1 XE2 (UP4+HF1) IDEFixPackXE2Reg61.1.7z 257.66 KB 33 times 2017-10-30 IDE Fix Pack 6.1.1 XE3 (UP2) IDEFixPackXE3Reg61.1.7z 211.76 KB 21 times 2017-10-30 IDE Fix Pack 6.1.1 XE4 (UP1) IDEFixPackXE4Reg61.1.7z 214.83 KB 16 times 2017-10-30 IDE Fix Pack 6.1.1 XE5 (UP2) IDEFixPackXE5Reg61.1.7z 211.58 KB 19 times 2017-10-30 IDE Fix Pack 6.1.1 XE6 (UP1) IDEFixPackXE6Reg61.1.7z 371.44 KB 14 times 2017-10-30 IDE Fix Pack 6.1.1 XE7 (UP1) IDEFixPackXE7Reg61.1.7z 387.64 KB 44 times 2017-10-30 IDE Fix Pack 6.1.1 XE8 (UP1) IDEFixPackXE8Reg61.1.7z 385.29 KB 25 times 2017-10-30 IDE Fix Pack 6.1.1 10 Seattle (RTM/UP1) IDEFixPackD10Reg61.1.7z 393.88 KB 64 times 2017-10-30 IDE Fix Pack 6.1.1 10.1 Berlin IDEFixPackD101Reg61.1.7z 391.56 KB 87 times 2017-10-30 IDE Fix Pack 6.1.1 10.2 (RTM/UP1) IDEFixPackD102Reg61.1.7z 392.96 KB 177 times 2017-10-30

Download (fastdcc):

Name IDE Version File Size Downloads Added fastdcc 6.1.1 2009 (UP4) fastdcc2009v61.1.7z 82.74 KB 11 times 2017-10-30 fastdcc 6.1.1 2010 (UP5) fastdcc2010v61.1.7z 90.04 KB 13 times 2017-10-30 fastdcc 6.1.1 XE (UP1) fastdccXEv61.1.7z 91.83 KB 13 times 2017-10-30 fastdcc 6.1.1 XE2 (UP4+HF1) fastdccXE2v61.1.7z 129.47 KB 19 times 2017-10-30 fastdcc 6.1.1 XE3 (UP2) fastdccXE3v61.1.7z 137.35 KB 8 times 2017-10-30 fastdcc 6.1.1 XE4 (UP1) fastdccXE4v61.1.7z 140.06 KB 8 times 2017-10-30 fastdcc 6.1.1 XE5 (UP2) fastdccXE5v61.1.7z 139.37 KB 9 times 2017-10-30 fastdcc 6.1.1 XE6 (UP1) fastdccXE6v61.1.7z 186.19 KB 7 times 2017-10-30 fastdcc 6.1.1 XE7 (UP1) fastdccXE7v61.1.7z 200.05 KB 16 times 2017-10-30 fastdcc 6.1.1 XE8 (UP1) fastdccXE8v61.1.7z 199.91 KB 12 times 2017-10-30 fastdcc 6.1.1 10 Seattle (RTM/UP1) fastdccD10v61.1.7z 200.27 KB 27 times 2017-10-30 fastdcc 6.1.1 10.1 Berlin fastdccD101v61.1.7z 199.99 KB 40 times 2017-10-30 fastdcc 6.1.1 10.2 (RTM/UP1) fastdccD102v61.1.7z 200.58 KB 62 times 2017-10-30
Categories: News, Blogs, and Tips

IDE Fix Pack 6.0 released – dcc64 and 10.2 Update 1 support

Thu, 09/28/2017 - 12:51

The new IDE Fix Pack version 6.0 is available. It supports Delphi 10.2 RTM and 10.2 Update 1. And after over a year of being in BETA testing without any bug reports, I also included all the Win64 compiler performance optimizations. Thus the jump to version 6.0 can finally be done as they make the Win64 compiler up to 50% faster.

Changelog:

  • Added: Win64 compile speed optimizations
  • Added: Delphi 10.2 Update 1 support
  • Added: Editor Block Completion UTF8 fix (Delphi 2009 only)

Download:

Name IDE Version File Size Downloads Added IDE Fix Pack 6.1.1 2009 (UP4) IDEFixPack2009Reg61.1.7z 188.6 KB 34 times 2017-10-30 IDE Fix Pack 6.1.1 2010 (UP5) IDEFixPack2010Reg61.1.7z 185.1 KB 42 times 2017-10-30 IDE Fix Pack 6.1.1 XE (UP1) IDEFixPackXEReg61.1.7z 172.72 KB 41 times 2017-10-30 IDE Fix Pack 6.1.1 XE2 (UP4+HF1) IDEFixPackXE2Reg61.1.7z 257.66 KB 33 times 2017-10-30 IDE Fix Pack 6.1.1 XE3 (UP2) IDEFixPackXE3Reg61.1.7z 211.76 KB 21 times 2017-10-30 IDE Fix Pack 6.1.1 XE4 (UP1) IDEFixPackXE4Reg61.1.7z 214.83 KB 16 times 2017-10-30 IDE Fix Pack 6.1.1 XE5 (UP2) IDEFixPackXE5Reg61.1.7z 211.58 KB 19 times 2017-10-30 IDE Fix Pack 6.1.1 XE6 (UP1) IDEFixPackXE6Reg61.1.7z 371.44 KB 14 times 2017-10-30 IDE Fix Pack 6.1.1 XE7 (UP1) IDEFixPackXE7Reg61.1.7z 387.64 KB 44 times 2017-10-30 IDE Fix Pack 6.1.1 XE8 (UP1) IDEFixPackXE8Reg61.1.7z 385.29 KB 25 times 2017-10-30 IDE Fix Pack 6.1.1 10 Seattle (RTM/UP1) IDEFixPackD10Reg61.1.7z 393.88 KB 64 times 2017-10-30 IDE Fix Pack 6.1.1 10.1 Berlin IDEFixPackD101Reg61.1.7z 391.56 KB 87 times 2017-10-30 IDE Fix Pack 6.1.1 10.2 (RTM/UP1) IDEFixPackD102Reg61.1.7z 392.96 KB 177 times 2017-10-30

Download (fastdcc):

Name IDE Version File Size Downloads Added fastdcc 6.1.1 2009 (UP4) fastdcc2009v61.1.7z 82.74 KB 11 times 2017-10-30 fastdcc 6.1.1 2010 (UP5) fastdcc2010v61.1.7z 90.04 KB 13 times 2017-10-30 fastdcc 6.1.1 XE (UP1) fastdccXEv61.1.7z 91.83 KB 13 times 2017-10-30 fastdcc 6.1.1 XE2 (UP4+HF1) fastdccXE2v61.1.7z 129.47 KB 19 times 2017-10-30 fastdcc 6.1.1 XE3 (UP2) fastdccXE3v61.1.7z 137.35 KB 8 times 2017-10-30 fastdcc 6.1.1 XE4 (UP1) fastdccXE4v61.1.7z 140.06 KB 8 times 2017-10-30 fastdcc 6.1.1 XE5 (UP2) fastdccXE5v61.1.7z 139.37 KB 9 times 2017-10-30 fastdcc 6.1.1 XE6 (UP1) fastdccXE6v61.1.7z 186.19 KB 7 times 2017-10-30 fastdcc 6.1.1 XE7 (UP1) fastdccXE7v61.1.7z 200.05 KB 16 times 2017-10-30 fastdcc 6.1.1 XE8 (UP1) fastdccXE8v61.1.7z 199.91 KB 12 times 2017-10-30 fastdcc 6.1.1 10 Seattle (RTM/UP1) fastdccD10v61.1.7z 200.27 KB 27 times 2017-10-30 fastdcc 6.1.1 10.1 Berlin fastdccD101v61.1.7z 199.99 KB 40 times 2017-10-30 fastdcc 6.1.1 10.2 (RTM/UP1) fastdccD102v61.1.7z 200.58 KB 62 times 2017-10-30
Categories: News, Blogs, and Tips

Debugger Callstack Resolver released

Tue, 08/15/2017 - 08:03

Debugger Callstack Resolver is a Delphi IDE plugin that I wrote in 2011 to make the IDE’s CPU View more readable. It colors different instructions, resolves absolute and memory address jump and call targets and shows their function name if available. It also uses the *.jdbg to show more information (the dcc32 compiler’s jdbg files are for the debug build of the compiler and haven’t match the deployed version since jdbg files exist)

This plugin supports Delphi 2009 – 10.1 Berlin.

[Download]

Categories: News, Blogs, and Tips