Skip to content

Fpc linux x64 - #324

Open
dkounal wants to merge 8 commits into
remobjects:masterfrom
dkounal:fpc-linux-x64
Open

dkounal wants to merge 8 commits into
remobjects:masterfrom
dkounal:fpc-linux-x64

Conversation

@dkounal

@dkounal dkounal commented Oct 31, 2026

Copy link
Copy Markdown

FreePascal on Linux x86_64: fix the build, the System V calling code and add a method handler

Summary

On FPC x86_64 Linux the library did not compile, GetProcAsMethod returned a method that did nothing, and several script-to-native calls passed wrong values. This branch fixes all three and adds tests. Win64 code paths are unchanged.

Changes

  • Build: Dec/Inc on a Variant lvalue is replaced by assignment. ResultAsRegister moves out of the block skipped by empty_methods_handler.
  • FPC_UNICODE: the switch in PascalScriptFPC.inc now builds and works. string in scripts is UnicodeString, the same as on Delphi.
  • Script to native, System V:
    • Extended goes on the stack, 16 bytes aligned, and returns in ST0.
    • Currency returns in ST0 as a scaled integer, not in RAX.
    • A dynamic array by value is passed as the array, not as the address of the variable.
    • Records up to 16 bytes use registers according to the class of each eightbyte, and return in RAX, RDX, XMM0 and XMM1.
  • Native to script, System V: MyAllMethodsHandler is implemented for x86_64 Unix (x64sysvhandler.inc).
  • const: the compiler now keeps it, because a record above 16 bytes is passed by reference when const and on the stack otherwise. It is #2 in ImportDecl and & in the export declaration.
  • IntToStr: uPSUtils.IntToStr takes Int64. It shadows SysUtils.IntToStr and silently dropped the upper 32 bits.
  • TStrings/TStringList: the constructors are registered on FPC too. Scripts ended in TObject.Create, so TStrings.Create never ran and Text lost its trailing line break.
  • Tests: dunit/fpcconsole.lpr is a console runner without LCL. dunit/CompilerTestSysV.pas covers parameter and result types in both call directions.

Each ABI case was checked against the assembly FPC generates.

Testing

Target Compiler Result
Linux x86_64 FPC 3.2.3, -Mdelphi fpcconsole: 33/33
Linux x86_64 FPC 3.2.3, -Mdelphiunicode -dFPC_UNICODE fpcconsole: 33/33
Win64, Win32 Delphi 13.1 Separate test program for the changed paths, all passed

The DUnit project was not run on Delphi. The test sources now use fpcunit.

cd dunit
fpc -Mdelphiunicode -dUNICODE -dFPC_UNICODE -Fu../Source -Fi../Source fpcconsole.lpr
./fpcconsole --all --format=plain

Compatibility notes

  • TPSVariantIFC has a new field, ConstParam.
  • ImportDecl and the export declaration can hold new values (#2, &). Bytecode compiled by an older version still loads, and such parameters count as by value.
  • The stack frame of the assembler stub has no unwind information. FPC on Linux uses setjmp based exceptions, so this is not a problem today.

Not covered

  • aarch64 and other non-x86_64 Unix targets.
  • array of const and open array parameters in the method handler.
  • Method pointers (btProcPtr) as parameters in the method handler.

Dec/Inc on a Variant lvalue is not accepted by FPC. ResultAsRegister is declared in the interface, but its body sat inside the part skipped by empty_methods_handler, so it moved in front of that part.
With FPC_UNICODE, string is UnicodeString but the Delphi version defines stayed off, so PChar and char based parser code read bytes as wide chars. TPSPluginItem.GetDisplayName follows the FCL ancestor, which stays AnsiString.
Extended goes on the stack, 16 bytes aligned, and comes back in ST0. Currency comes back in ST0 as a scaled integer. A dynamic array by value is the array, not the address of the variable. Records up to 16 bytes travel in registers according to the class of each eightbyte, and come back in RAX, RDX, XMM0 and XMM1. Each case was checked against the code FPC generates.
MyAllMethodsHandler was empty on FPC x86_64, so GetProcAsMethod gave a method that did nothing. New assembler stub and handler for Unix. Win64 and the other targets are not touched.
On System V a record above 16 bytes goes on the stack by value, but by reference when const. The compiler now records const: remobjects#2 in ImportDecl, & in the export declaration. Consumers that only compare with #0 or read the type number treat them as by value, as before.
uPSUtils.IntToStr shadows SysUtils.IntToStr in units that use it after SysUtils, and silently dropped the upper 32 bits.
Scripts ended in TObject.Create, so TStrings.Create never ran and Text lost its trailing line break.
Console runner without LCL, and tests for every parameter and result type in both call directions on x86_64 Linux. Enables the method handler tests there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant