代码之家  ›  专栏  ›  技术社区  ›  Johan

我可以将类/记录方法链接到外部模块吗?

  •  1
  • Johan  · 技术社区  · 6 年前

    在Delphi中,我可以像这样链接到外部代码:

       {$L 'C:\Users\Johan\Documents\Embarcadero\Studio\Projects\Life64\Lazarus\lib\x86_64-win64\AVXGenerate.o'}
    
    function GenerateQtoP_AVX_32(main, N,W,NW: pointer): byte;
      external name 'AVXGENERATE_$$_GENERATEQTOP_AVX_32$POINTER$POINTER$POINTER$POINTER$$BYTE';
    function GeneratePtoQ_AVX_32(main, S,E,SE: pointer): byte;
      external name 'AVXGENERATE_$$_GENERATEPTOQ_AVX_32$POINTER$POINTER$POINTER$POINTER$$BYTE';
    procedure ReverseBitsInAllBytes(ReverseMe: pointer);
      external name 'AVXGENERATE_$$_REVERSEBITSINALLBYTES$POINTER';
    

    在本例中,它链接到我在Lazarus中编写的AVX2汇编代码。

    有没有一种方法可以用这种方式链接类或记录的成员方法?

    类似于以下伪代码:

    type
      TMyRec = record
      public
        procedure DoSomething(x,y: integer) = ExternalMethod;
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   David Heffernan    6 年前

    你不可能做你想做的事。我认为您能管理的最接近的方法可能是使用汇编程序实现方法,跳转到外部函数:

    type
      TMyRec = record
      public
        procedure DoSomething(x, y: integer);
      end;
    
    procedure MyRecDoSomething(var Self: TMyRec; x, y: integer); external;
    
    procedure TMyRec.DoSomething(x, y: integer);
    asm
      JMP MyRecDoSomething
    end;
    

    或者您可以使用内联方法:

    type
      TMyRec = record
      public
        procedure DoSomething(x, y: integer); inline;
      end;
    
    procedure MyRecDoSomething(var Self: TMyRec; x, y: integer); external;
    
    procedure TMyRec.DoSomething(x, y: integer);
    begin 
      MyRecDoSomething(Self, x, y);
    end;
    

    打电话时 TMyRec.DoSomething ,第一个版本的调用后面跟着跳转(使用 asm )对于第二个版本(使用 inline )直接对外部函数进行单个调用。