Recent

Author Topic: Can this C struct be expressed in FPC Pascal ?  (Read 19283 times)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #60 on: July 07, 2019, 06:45:50 pm »
My two pence.
I believe this is pretty good selfdocumenting.
Of course it would be nicer, if this could be written inline as property. (I love properties ::) )

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch advancedrecords}
  5.  
  6. type
  7.   mystruct = packed record
  8.   a,b: DWORD;
  9.   function c: PDWORD; inline; // return adress after record
  10. end;
  11.  
  12. function mystruct.c: PDWORD;
  13. begin
  14.   {$T+} // @ now gives typed pointers
  15.   c := PDWORD(@self+1);
  16. end;
  17.  

Code: Pascal  [Select][+][-]
  1. function mystruct.c: PDWORD;
  2. begin
  3.   {$Push}{$T+} // @ now gives typed pointers
  4.   c := PDWORD(@self+1);
  5.   {$Pop}
  6. end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #61 on: July 07, 2019, 06:54:46 pm »
A tiny bit faster:
Code: Pascal  [Select][+][-]
  1.   mystruct = packed record
  2.     a,b: DWORD;
  3.     function c: PDWORD; inline;
  4.   strict private
  5.     cStart: record end;
  6.   end;
  7.  
  8. function mystruct.c: PDWORD;
  9. begin
  10.   Result := PDWORD(@cStart);
  11. end;
Thank you. I expected both to have the same assembly, but it seems there is an issue with constant folding in FPC 3.0.4?

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #62 on: July 07, 2019, 07:23:49 pm »
using that style of coding here's my HACK!,,. still.
Code: Pascal  [Select][+][-]
  1. TmyRecord = Packed Record
  2.  A,B:DWord;
  3.  Function C(Index:Integer):DWord; inline;
  4.  Procedure C(Index:Integer;AValue:DWord); inline;
  5.  End;
  6. var
  7.   Form1: TForm1;
  8.  
  9. implementation
  10.  
  11. {$R *.lfm}
  12.  
  13. { TForm1 }
  14. Procedure TMyRecord.C(Index:Integer; AValue:DWord);
  15. begin
  16.  PWord(SizeOf(Self)+UintPtr(@Self))[Index] := AValue;
  17. end;
  18.  
  19. Function TMyrecord.C(Index:Integer):DWord;
  20. Begin
  21.   Result := PWord(SizeOf(Self)+UIntPtr(@Self))[index];
  22. End;
  23. procedure TForm1.FormCreate(Sender: TObject);
  24. Var
  25.   T:TmyRecord;
  26.   A:Dword;
  27.   M:Pointer;
  28. begin
  29.   M := GetMem(1000);
  30.   TMyRecord(M^).C(1,100);
  31.   A := TmyRecord(M^).C(1);
  32.   Caption := A.ToString;
  33.   Freemem(M);
  34. end;                                  
  35.  
The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #63 on: July 07, 2019, 07:37:10 pm »
So I guess the juror have tallied their votes.

There is a [better? easier?] way to get new features added to FPC. If you can get them added to Delphi.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #64 on: July 07, 2019, 07:41:17 pm »
There is a [better? easier?] way to get new features added to FPC. If you can get them added to Delphi.

Very true.  :D

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #65 on: July 07, 2019, 07:48:33 pm »
Yes, I was thinking of that actually   >:D

But I don't really want to poke the bear!

It seems or I've read somewhere that Delphi did copy some of the fpc/Laz ideas for their product so
what are we waiting for, lets help them out!

all of this ridiculous hacking just to object a simple non entity C:Array[] of DWord at the end of the record..

In the old days and maybe still in asm we used

ORG. $AddressValue

I suppose with some compiler trickery we could get the "ABSolute" key word to generate an entity address
type and value for calling code.

Come to think about it I remember trying to implement a palette entry in the bitmap image and was a
problem doing that since the final array at the end of the bitmapHeader used the same technique and in
cases where there was no entrys you had to screw around with the code putting in extra check points in it.
 you couldn't take the C code for what it was and directly use it..


 
« Last Edit: July 07, 2019, 07:52:41 pm by jamie »
The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #66 on: July 07, 2019, 08:19:34 pm »

First, I want to thank all those who contributed some way of implementing that C construct.  There are some really nice ideas and implementations.

All the solutions so far, share the same problem which is, you cannot take the address of "c" or one of its elements i.e,
Code: Pascal  [Select][+][-]
  1. @c
  2. @c[x]


Because of that, the closest that allows taking the address of the flexible array seems to be:
Code: Pascal  [Select][+][-]
  1.   FlexRec = record
  2.      a        : DWORD;
  3.      b        : DWORD;
  4.      c        : array[0..0] of record end;   { cannot specify a type          }
  5.   end;
  6.  
The obvious problem with the above is, there is no "real" datatype associated with the array elements.  That in turn will require a typed pointer to access the elements of "c" and the typed pointer can be used to derive the address of any element.

I cannot think of a solution that would allow to specify the array element's type, which is necessary to derive the address of an element, without it changing the size of the record.


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #67 on: July 07, 2019, 08:55:27 pm »
I want to thank all those who contributed some way of implementing that C construct.

Someone please document it on the wiki page. It can be valuable for new users coming from C.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #68 on: July 07, 2019, 08:57:07 pm »
ah, but you can with what I used originally..
Code: Pascal  [Select][+][-]
  1. TMyRecord = record
  2.  A:DWord;
  3.  Case Integer of
  4.   0:(B:DWord);
  5.   1:(C:Array[-1..-1] of DWord;
  6. End;
  7.  

That should resolve to an address.
Untested;
Code: Pascal  [Select][+][-]
  1. {$R-}
  2.  @MyRecord.C[0];
  3.  
The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #69 on: July 07, 2019, 09:19:35 pm »
Someone please document it on the wiki page. It can be valuable for new users coming from C.
No way, we do not want them here.  >:D



That should resolve to an address.
Untested;
Code: Pascal  [Select][+][-]
  1. {$R-}
  2.  @MyRecord.C[0];
  3.  
Every time you touch MyRecord.C[ beautify it with:
Code: Pascal  [Select][+][-]
  1. {$Push}{$R-}
  2.  @MyRecord.C[0];{$Pop}

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #70 on: July 07, 2019, 09:23:48 pm »
ah, but you can with what I used originally..
Code: Pascal  [Select][+][-]
  1. TMyRecord = record
  2.  A:DWord;
  3.  Case Integer of
  4.   0:(B:DWord);
  5.   1:(C:Array[-1..-1] of DWord;
  6. End;
  7.  



That should resolve to an address.
Untested;
Code: Pascal  [Select][+][-]
  1. {$R-}
  2.  @MyRecord.C[0];
  3.  

Jamie, you are correct.  That construct does allow taking the address _but_ it has one very significant downside, the bounds of the array have to be carefully set to overlap part of the fixed field definition and, in some cases, it may not even be possible to do that (when the size of the fixed size is not a multiple of the size of the array elements).

I appreciate your suggestion but, it has too many downsides to be used in practice.

Thank you though.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #71 on: July 07, 2019, 09:27:58 pm »
It does get bonus points for creativity though  :)

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #72 on: July 07, 2019, 10:35:18 pm »
Yes of course, That was the item I was referring to, if the previous type is the same size.

it does not work in all cases..

The other way to keep it pascal I guess would be to use overlapping records;
Code: Pascal  [Select][+][-]
  1. Type
  2. TmyRecordHdr = Record
  3.  A,B:DWord;
  4. End;
  5. TmyRecord = Record
  6.   H:TMyRecordHdr;
  7.   C:Array[0..0] of DWord;
  8. End;
  9.  

So we can use the RecordHdr for the most part but when you are ready to access the trailing data..
Code: Pascal  [Select][+][-]
  1. TMyRecord(MyReadhrInstance).C[?];
  2.  

I need to find some Delphi devs and hint them on..... >:D
The only true wisdom is knowing you know nothing

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #73 on: July 07, 2019, 11:08:07 pm »
I need to find some Delphi devs and hint them on..... >:D
Hitting two stones with one bird.  >:D

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #74 on: July 07, 2019, 11:18:20 pm »
I need to find some Delphi devs and hint them on..... >:D
I am not fond of being pessimistic but, it's rather unlikely to make a difference.  The result will most likely be along the lines of what you observed in this thread of yours https://forum.lazarus.freepascal.org/index.php/topic,43898.msg308122.html#msg308122

Hitting two stones with one bird.  >:D
LOL... that either requires a large bird or the stones to be close together (or both!)

Code: Pascal  [Select][+][-]
  1. var
  2.   bird : packed array[1..2] of stone;
« Last Edit: July 07, 2019, 11:22:26 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018