Recent

Author Topic: Delphi compatibility  (Read 3444 times)

Janusz

  • Newbie
  • Posts: 2
Delphi compatibility
« on: April 15, 2024, 02:14:04 pm »
Hi,
I wrote a program in CodeTyphon (based on Free Pascal). Now I need to use Delphi instruction AtomicIncrement.
Is there possiblity to use Free Pascal function named AtomicIncrement.
I know there is something like this :

{ delphi compatibility aliases }
function AtomicIncrement (var Target: Int64) : Int64; overload; external name

maybe is is dll ?

I know that is not fair to ask about it but it impossible for me to change CodeTyphon to Lazarus 😥
Janusz

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11931
  • FPC developer.
Re: Delphi compatibility
« Reply #1 on: April 15, 2024, 02:17:14 pm »
This is not known to us. Codetyphon is very in-transparent, and we don't know if it is possible to upgrade FPC there yourself.

You could ask on their forum and/or if they plan to release a version with updated trunk

Fibonacci

  • Hero Member
  • *****
  • Posts: 594
  • Internal Error Hunter

Janusz

  • Newbie
  • Posts: 2
Re: Delphi compatibility
« Reply #3 on: April 25, 2024, 01:33:59 pm »
Hi,
Thank you for an answer.
I need to simulate function with UInt64 argument, too.

I got and information that  there is unit (?) in FP written for compatibility with Delphi :
Here is example :

$IFDEF FPC}
{ delphi compatibility aliases }
function AtomicIncrement (var Target: Int64) : Int64; overload; external name 'FPC_INTERLOCKEDINCREMENT';
function AtomicIncrement (var Target: Int64; Value: Int64) : Int64; overload; external name 'FPC_INTERLOCKEDINCREMENT'; 

Where I can find this ?
Maybe it is dll ?

Janusz
                         

cdbc

  • Hero Member
  • *****
  • Posts: 1645
    • http://www.cdbc.dk
Re: Delphi compatibility
« Reply #4 on: April 25, 2024, 02:06:29 pm »
Hi
"AtomicIncrement()" is a builtin in FPC.
I can just type this in a form's eventhandler "Result:= AtomicIncrement(someQWord);"
edit: Sorry, my bad, the aliases / overloads are implemented in fpc 3.3.1 and I just checked fpc 3.2.2, where they're not   :'(
edit2: just put this:
Code: Pascal  [Select][+][-]
  1. { delphi compatibility aliases }
  2. function AtomicIncrement (var Target: int64): int64; external name 'FPC_INTERLOCKEDINCREMENT';
  3. function AtomicIncrement (var Target: QWord): QWord; external name 'FPC_INTERLOCKEDINCREMENT';
in the top of your unit, then it works in fpc 3.2.2 too
These aliases are courtesey of @Thaddy, I directly lifted them from GitLab
Regards Benny
« Last Edit: April 25, 2024, 02:36:32 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16145
  • Censorship about opinions does not belong here.
Re: Delphi compatibility
« Reply #5 on: April 25, 2024, 06:52:13 pm »
Although that is possible, these are also already exposed in the system unit as:
InterlockedIncrement, InterlockedIncrement64 and InterlockedDecrement, InterlockedDecrement64.
See:
https://www.freepascal.org/docs-html/rtl/system/interlockedincrement.html
and family.
But indeed, that also needs an alias to be declared. And the way you do it should work too.
« Last Edit: April 25, 2024, 06:54:52 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11931
  • FPC developer.
Re: Delphi compatibility
« Reply #6 on: April 25, 2024, 08:14:49 pm »
But CodeTyphon afaik uses trunk

Thaddy

  • Hero Member
  • *****
  • Posts: 16145
  • Censorship about opinions does not belong here.
Re: Delphi compatibility
« Reply #7 on: April 26, 2024, 08:05:09 am »
Indeed these atomic aliases are added in trunk.
If I smell bad code it usually is bad code and that includes my own code.

cdbc

  • Hero Member
  • *****
  • Posts: 1645
    • http://www.cdbc.dk
Re: Delphi compatibility
« Reply #8 on: April 26, 2024, 08:14:05 am »
Hi
@Thaddy: Yup, in 2021 by you Thaddy  :D
If you read the 2 edits in my post above, youl'll see, that I directly lifted them from your discussion with @PascalDragon in GitLab...  8-)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Thaddy

  • Hero Member
  • *****
  • Posts: 16145
  • Censorship about opinions does not belong here.
Re: Delphi compatibility
« Reply #9 on: April 26, 2024, 10:20:25 am »
Forgot.  :(
It did not make it to 3.2.2, but maybe it is in 3.2.3 fixes.
(After all it is a fix for Delphi compatibility and as patches go, very trivial.)
« Last Edit: April 26, 2024, 10:37:11 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: Delphi compatibility
« Reply #10 on: April 26, 2024, 12:21:42 pm »
is in the new version operator overloading supported in Delphi mode ?

Thaddy

  • Hero Member
  • *****
  • Posts: 16145
  • Censorship about opinions does not belong here.
Re: Delphi compatibility
« Reply #11 on: April 26, 2024, 12:50:08 pm »
Yes:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$macro on}
  2. { delphi compatibility aliases }
  3. {$if fpc_fullversion < 30301}
  4. function AtomicIncrement (var Target: integer): integer;overload; external name 'FPC_INTERLOCKEDINCREMENT';
  5. function AtomicIncrement (var Target: int64): int64;overload; external name 'FPC_INTERLOCKEDINCREMENT';
  6. function AtomicIncrement (var Target: QWord): QWord;overload; external name 'FPC_INTERLOCKEDINCREMENT';
  7. // a lot more
  8. {$ifend}
  9. var
  10.  i:integer = 0;
  11. begin
  12.   AtomicIncrement(i);
  13.   writeln(i);
  14. end.
If I smell bad code it usually is bad code and that includes my own code.

paule32

  • Sr. Member
  • ****
  • Posts: 280
Re: Delphi compatibility
« Reply #12 on: April 26, 2024, 01:53:42 pm »
sorry for misunderstanding.
I mean:
operator + (const LValue: Integer; const RValue: Integer): Integer;

Thaddy

  • Hero Member
  • *****
  • Posts: 16145
  • Censorship about opinions does not belong here.
Re: Delphi compatibility
« Reply #13 on: April 26, 2024, 04:52:17 pm »
yes, freestanding operators like that are supported in {$mode Delphi}, but note that Delphi itself does not have that feature. It is FreePascal specific. You put your operators in a separate unit, compile that in {$mode objfpc}. You can then use that unit in a program or other unit in {$mode delphi}
However, your example will not work and fail with "impossible overload" because there is already a compiler intrinsic with the same signature.
Quick example that will work:
Code: Pascal  [Select][+][-]
  1. unit ops;
  2. {$mode objfpc}{$H+}
  3. // this unit must be compiled in objfpc mode
  4. interface
  5. operator +(const left:string ; const right:integer):string;
  6.  
  7. implementation
  8. operator +(const left:string ;  const right:integer):string;
  9. begin
  10.    writestr(Result, left,right);
  11. end;
  12. end.

You can subsequently use it in delphi mode like so:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. uses ops;
  3. var
  4.   a:string = 'test';
  5.   b:integer = 0;
  6. begin
  7.   writeln(a+b);// prints 'test0'
  8. end.

Maybe there is a {$Modeswitch xxx}to add it to {$mode delphi} but I either forgot or there isn't any.. ;)
« Last Edit: April 26, 2024, 05:02:23 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5752
  • Compiler Developer
Re: Delphi compatibility
« Reply #14 on: May 05, 2024, 01:10:37 pm »
sorry for misunderstanding.
I mean:
operator + (const LValue: Integer; const RValue: Integer): Integer;

It is not allowed to overload operators that have a built-in implementation.

 

TinyPortal © 2005-2018