* * *

Author Topic: asm from delphi to fpc  (Read 364 times)

nanobit

  • New member
  • *
  • Posts: 8
asm from delphi to fpc
« on: October 23, 2018, 08:26:32 am »
The following delphi asm code (to change the field TFreeObject.VMIndex: Integer)
does not compile with fpc. How could one port this?
Code: Pascal  [Select]
  1. procedure TFreedObject.GetVirtualMethodIndex; assembler;
  2. asm
  3.   Inc(VMIndex); Inc(VMIndex); Inc(VMIndex); Inc(VMIndex); Inc(VMIndex);
  4.   Inc(VMIndex); Inc(VMIndex); Inc(VMIndex); Inc(VMIndex); Inc(VMIndex);
  5.   jmp TFreedObject.VirtualMethodError  
  6. end;


Thaddy

  • Hero Member
  • *****
  • Posts: 7087
Re: asm from delphi to fpc
« Reply #1 on: October 23, 2018, 10:13:11 am »
Such code relies on very low level implementation detail and may even change between Delphi versions: it is bad code! in my opinion.
Since it relies on implementation detail it is not FPC compatible.

But you can use e.g. TObject.MethodAddress
Depends what you want?

[edit]
FPC Has a TypeOf() function that returns the VMT, so the index is something like (TYourObject.MethodAddress('methodname') - TypeOf(TYourObject)) div sizeOf(Pointer);
You can get the address and the VMT and subsequently the index.

That also means it is silly to use assembler for it.
« Last Edit: October 23, 2018, 10:25:21 am by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 6577
Re: asm from delphi to fpc
« Reply #2 on: October 23, 2018, 10:14:13 am »
Compile a small program that has a pascal version of the method with -Rintel -a and look at the generated code in the .s file.

Thaddy

  • Hero Member
  • *****
  • Posts: 7087
Re: asm from delphi to fpc
« Reply #3 on: October 23, 2018, 10:22:44 am »
Marco, I did an edit. Would that work? [edit] No! but something can be done along those lines.
« Last Edit: October 23, 2018, 11:33:44 am by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

nanobit

  • New member
  • *
  • Posts: 8
Re: asm from delphi to fpc
« Reply #4 on: October 23, 2018, 03:02:33 pm »
Unfortunately, I will give up. I wanted to port fastMM to Lazarus, but it compiles only without the FullDebugMode define. FullDebugMode is required to use detection of memory corruptions, the main reason I wanted to use it.

Thaddy

  • Hero Member
  • *****
  • Posts: 7087
Re: asm from delphi to fpc
« Reply #5 on: October 23, 2018, 03:11:08 pm »
heaptrc.pp shows you how to detect memory corruption in much the same way. So examine that.
Also note the memory manager from Freepascal works internally different - not much but significant - from Delphi and is already fast (much faster than the default Delphi one once was before they changed to fastmm light) and it is cross-platform which fastmm isn't...
« Last Edit: October 23, 2018, 03:29:40 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus