Recent

Author Topic: [SOLVED] Operators overriding and use of PChars in code for AVRs  (Read 2807 times)

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #45 on: January 16, 2025, 10:02:11 pm »
I think this is one of possible ways to override operators for my simple type TFloat32=UInt32:
Sure, in case that works for you then go ahead.

But... I do not see the benefit over:
Code: Pascal  [Select][+][-]
  1. type
  2.   TFloat32 = record
  3.     value: uint32;
  4.   end;
  5.  
  6. operator * (const f1, f2: TFloat32): TFloat32;
  7. begin
  8.   result.value := f1.value * f2.value;
  9. end;
  10.  
  11. var
  12.   f1, f2, f3: TFloat32;
  13. begin
  14.   f3:=f1 * f2; //only test
  15. end.
  16.  
To me that looks cleaner and and less laborious.

Yes. This is another, possibly much easier way to do it. However my example using array gives smaller code (426 vs 472 bytes for ATmega328p).

TRon

  • Hero Member
  • *****
  • Posts: 3938
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #46 on: January 16, 2025, 10:14:49 pm »
However you can only overload operators that are not already defined internally by the compiler. These operators are also defined when you declare a unique type alias (using type TypeName), because after all a unique type alias of e.g. LongInt is still a LongInt.
I respectfully disagree. On the Delphi page it is explicitly mentioned that using the type alias creates a new type that is not the same type:

Quote
... creates a new type called TMyInteger which is not identical to Integer.

However they are assignment compatible in certain cases as described.
Quote
Every type is compatible with itself. Two distinct types are compatible if they satisfy at least one of the following conditions.
Nowhere have I been able to read that in case types are compatible that the same operators are being used.

From your words I would have to conclude that as long as different types are "compatible" the same operators implementations are used for these "compatible" types.

That strikes me as odd (see quote below) but in case it is then so be it. Are you able to confirm or happen to know if that is mentioned somewhere in the Free Pascal documentation ?

FPC docs: introduction to operator overloading:
Quote
Free Pascal supports operator overloading. This means that it is possible to define the action of some operators on self-defined types, and thus allow the use of these types in mathematical expressions.
« Last Edit: January 16, 2025, 10:29:53 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

TRon

  • Hero Member
  • *****
  • Posts: 3938
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #47 on: January 16, 2025, 10:16:55 pm »
Yes. This is another, possibly much easier way to do it. However my example using array gives smaller code (426 vs 472 bytes for ATmega328p).
Ah, yes restricted hardware. Honestly I hadn't considered checking the code-size. That is a fair point.
I do not have to remember anything anymore thanks to total-recall.

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #48 on: January 16, 2025, 10:32:18 pm »
(...)
2. What is your opinion about PChars (=arrays[0..n] of char) in code for AVRs? Is this feature fully supported by the compiler? In the wiki page https://wiki.freepascal.org/AVR_Programming there is no mention about PChars, but I think it was written at  one point that PChars work. If yes - what is better to use in practice: strings[n] or arrays[0..n] of char?

Please note that PChar is not a array[0..n] of Char, but ^Char. So while you can pass a pointer to the first element of an array[0..n] of Char to a PChar it can also point to some memory. The important point is that it needs to end with a #0 as that is used to determine the length.

@PascalDragon. Thank you for your comment. Yes, I consider array [0..n] of char as place in memory "filled" by n chars and PChar as pointer to this place, normally to the first element of the array. Of course PChar could points to the second element or another. The final #0 is only indicator of the end of "visible" string.

Edit: I rather wonder what type is better as a parameter in a procedure/function: string (=shortstring=255 bytes) or PChar (array defined earlier). My doubts are related to hardware limitations.
« Last Edit: January 16, 2025, 10:47:31 pm by ackarwow »

PascalDragon

  • Hero Member
  • *****
  • Posts: 5855
  • Compiler Developer
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #49 on: January 18, 2025, 05:20:35 pm »
However you can only overload operators that are not already defined internally by the compiler. These operators are also defined when you declare a unique type alias (using type TypeName), because after all a unique type alias of e.g. LongInt is still a LongInt.
I respectfully disagree. On the Delphi page it is explicitly mentioned that using the type alias creates a new type that is not the same type:

Quote
... creates a new type called TMyInteger which is not identical to Integer.

However they are assignment compatible in certain cases as described.
Quote
Every type is compatible with itself. Two distinct types are compatible if they satisfy at least one of the following conditions.
Nowhere have I been able to read that in case types are compatible that the same operators are being used.

From your words I would have to conclude that as long as different types are "compatible" the same operators implementations are used for these "compatible" types.

Correct. After all you can still use + on a unique type alias of the LongInt type. Also this compiles in Delphi:

Code: Pascal  [Select][+][-]
  1. program topovld;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. type
  6.   TLongInt = type LongInt;
  7.  
  8.   TTest = record
  9.     class operator Implicit(const aArg: LongInt): TTest;
  10.     class operator Implicit(const aArg: TTest): LongInt;
  11.   end;
  12.  
  13. class operator TTest.Implicit(const aArg: LongInt): TTest;
  14. begin
  15.   Writeln('LongInt Implicit');
  16.   Result := Default(TTest);
  17. end;
  18.  
  19. class operator TTest.Implicit(const aArg: TTest): LongInt;
  20. begin
  21.   Writeln('TTest Implicit');
  22.   Result := 0;
  23. end;
  24.  
  25. var
  26.   t: TTest;
  27.   l: LongInt;
  28.   tl: TLongInt;
  29. begin
  30.   t := l;
  31.   l := t;
  32.   t := tl;
  33.   tl := t;
  34. end.

As you can see the compiler will use the LongInt assignment operators even for TLongInt (it will however prefer a TLongInt operator if one is declared).

Delphi also doesn't support global operator overloads only overloads where a record type is involved, so some things simply can't be tested.

That strikes me as odd (see quote below) but in case it is then so be it. Are you able to confirm or happen to know if that is mentioned somewhere in the Free Pascal documentation ?

Not that I'm aware of, but no one said that the documentation can't be improved... 🤷‍♀️

Edit: I rather wonder what type is better as a parameter in a procedure/function: string (=shortstring=255 bytes) or PChar (array defined earlier). My doubts are related to hardware limitations.

A ShortString variable, especially if it's passed by-value will result in a 256-Byte push on the stack. For by-reference or result it will be a reference anyway.
« Last Edit: January 18, 2025, 05:22:34 pm by PascalDragon »

TRon

  • Hero Member
  • *****
  • Posts: 3938
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #50 on: January 18, 2025, 05:33:38 pm »
Thank you for the clarification(s) PascalDragon.

That strikes me as odd (see quote below) but in case it is then so be it. Are you able to confirm or happen to know if that is mentioned somewhere in the Free Pascal documentation ?

Not that I'm aware of, but no one said that the documentation can't be improved... 🤷‍♀️
That was one of the reasons to ask  ;D

Perhaps it is me for whom English is not my first language but sometimes the documentation confuses the hell out of me (which is ofc my issue but can't imagine me being the only one with that problem).
I do not have to remember anything anymore thanks to total-recall.

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #51 on: January 18, 2025, 07:55:54 pm »
(...)
A ShortString variable, especially if it's passed by-value will result in a 256-Byte push on the stack. For by-reference or result it will be a reference anyway.

@PascalDragon, thank you for clarification. It really helps :)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5855
  • Compiler Developer
Re: [SOLVED] Operators overriding and use of PChars in code for AVRs
« Reply #52 on: January 26, 2025, 04:03:30 pm »
Perhaps it is me for whom English is not my first language but sometimes the documentation confuses the hell out of me (which is ofc my issue but can't imagine me being the only one with that problem).

None of the developers who maintain the documentation have English as a native language. ;)

 

TinyPortal © 2005-2018