[fpc-pascal] Help with Delphi ASM translation to FPC

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Fri Mar 13 00:15:28 CET 2015


Hi,

Could somebody confirm if the Delphi ASM does the same as the FPC code?
This code is used in a unit testing framework to help report the exact
location of where an error was raised. It seems to work well in Delphi,
but not in FPC.

Under FPC it seems to always return the same address, even though I
might have had multiple test cases fail. I'm trying to eliminate where
the possible issue could be.

==========================================
{$IFNDEF FPC}  // Delphi code

function IsBadPointer(const P: Pointer):boolean; register;
begin
  try
    Result := (P = nil) or
              ((Pointer(P^) <> P) and (Pointer(P^) = P));
  except
    Result := true;
  end
end;

function CallerAddr: Pointer; assembler;
const
  CallerIP = $4;
asm
   mov   eax, ebp
   call  IsBadPointer
   test  eax,eax
   jne   @@Error

   mov   eax, [ebp].CallerIP
   sub   eax, 5   // 5 bytes for call

   push  eax
   call  IsBadPointer
   test  eax,eax
   pop   eax
   je    @@Finish

@@Error:
   xor eax, eax
@@Finish:
end;

{$ELSE}

// FPC cross-platform implementation
function CallerAddr: Pointer;
begin
  Result := get_caller_addr(get_frame);
end;

{$ENDIF}
==========================================


Here is sample code of its usage:

procedure TTestProc.Check(const condition: boolean; const ErrorMsg:
string);
begin
  OnCheckCalled;
  if (not condition) then
    Fail(ErrorMsg, CallerAddr);
end;


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/



More information about the fpc-pascal mailing list