Function return parameter code generation misery lazarus / 67-Windows ---------------------------------------------------------------------- cryborg2 · 2022-11-05 16:09 How to tell the compiler to generate a code that treat Pointer type function return parameter as a "var" parameter just like it does with string type function return parameters? function Test1{EAX}: string; assembler; asm // from here it is like we have a header like this: procedure Test1(var S{EAX}: string); assembler; end; function Test2{EAX}: Pointer; assembler; asm // from here it is like we have a header like this: procedure Test2(out P{EAX}: Pointer); assembler; end; procedure Test;   var     s: string;     P: Pointer; begin   GetMem(P, MemBytes);   s:=Test1; // In this case we pass the string to Test1 as a "var" parameter! Then Test1 can access the string as it was a real "var" parameter.   {     assembler:     LEA     EAX,DWORD PTR [EBP-12]      (lea -0xc(%ebp),%eax)     CALL      }   P:=Test2; // In this case we not pass the pointer's address, we just get an address defined by Test2.   {     assembler:     CALL        MOV     DWORD PTR [EBP-16],EAX      (mov %eax,-0x10(%ebp))   }   FreeMem(P); end; ---------------------------------------------------------------------- cryborg2 · 2022-11-05 18:45 This is important to me because I want to use my custom string data type with the same fashion as the built-in strings (except for concatenation of course). ---------------------------------------------------------------------- PascalDragon · 2022-11-05 20:36 How types are passed is fixed by the ABI and is not up to a user. So a Pointer will always be passed by value in the result register. ---------------------------------------------------------------------- alpine · 2022-11-05 22:44 Quote from: cryborg2 on November 05, 2022, 07:45:29 pmThis is important to me because I want to use my custom string data type with the same fashion as the built-in strings (except for concatenation of course). IMO It is due to the fact that String have a special treatment in FPC. With the help of advanced records and operator/function overloading you can define whatever custom data type you want. What do you mean with "the same fashion as"? ---------------------------------------------------------------------- cryborg2 · 2022-11-06 02:14 Quote from: y.ivanov on November 05, 2022, 11:44:37 pmQuote from: cryborg2 on November 05, 2022, 07:45:29 pmThis is important to me because I want to use my custom string data type with the same fashion as the built-in strings (except for concatenation of course). IMO It is due to the fact that String have a special treatment in FPC. With the help of advanced records and operator/function overloading you can define whatever custom data type you want. What do you mean with "the same fashion as"? I want to use the ":=" operator to get my virtual string type return value, and to do so, the called function must have acces to my VirtualString object -which have it's own memory management- to transfer data through function return mechanism. ---------------------------------------------------------------------- PascalDragon · 2022-11-06 15:06 Then you need to use advanced records with operator overloads. Anything else is just hacks. ---------------------------------------------------------------------- ASerge · 2022-11-06 17:01 Quote from: cryborg2 on November 06, 2022, 03:14:28 amI want to use the ":=" operator to get my virtual string type return value, and to do so, the called function must have acces to my VirtualString object -which have it's own memory management- to transfer data through function return mechanism. You can, but not Pointer. Example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$LONGSTRINGS ON}{$MODESWITCH ADVANCEDRECORDS}{$APPTYPE CONSOLE}type  TVirtualString = record  strict private    FLength: SizeInt;    FData: PAnsiChar;    procedure CopyFrom(const Src; ALength: SizeInt);    class operator Copy(constref Src: TVirtualString; var Dst: TVirtualString);    class operator Finalize(var Self: TVirtualString);    class operator Initialize(var Self: TVirtualString);  public    class operator := (const S: string): TVirtualString;    function AsString: string;  end; class operator TVirtualString. := (const S: string): TVirtualString;begin  Result.CopyFrom(Pointer(S)^, Length(S));end; function TVirtualString.AsString: string;begin  SetString(Result, FData, FLength);end; class operator TVirtualString.Copy(constref Src: TVirtualString; var Dst: TVirtualString);begin  Dst.CopyFrom(Src.FData^, Src.FLength);end; class operator TVirtualString.Finalize(var Self: TVirtualString);begin  ReAllocMem(Self.FData, 0);  Self.FLength := 0;end; class operator TVirtualString.Initialize(var Self: TVirtualString);begin  Self.FLength := 0;  Self.FData := nil;end; procedure TVirtualString.CopyFrom(const Src; ALength: SizeInt);begin  FLength := ALength;  ReAllocMem(FData, FLength);  if FLength > 0 then    Move(Src, FData^, FLength)end; // End TVirtualString function SomeFunction: TVirtualString;begin  Result := '123';end; var  V: TVirtualString;begin  V := '456';  V := SomeFunction; // As you want  Writeln(V.AsString);  Readln;end.