PascalScript / FPC: parameters problems (need internals gurus I think)

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

PascalScript / FPC: parameters problems (need internals gurus I think)

Alexandre Leclerc
Hi all,

I hope some have knowledge of PascalScript because I'm having quite a
weird and complex problem. I'll demonstrate it here very simply so if
some have ideas...

*Problem*
I'm exporting some classes (originally a TBitmap; now a custom class)
to the ROPS engine and then interacting with the class in the
scripting engine. Everything works, except for some functions that
behave very strange. They skip/duplicate the parameters and invert
parameters.

*Simple example*
All the following code is typed in the e-mail.

TMyClass
public
  procedure DrawRectangle(left, top, right, bottom: Extended);
end;
//
procedure TMyClass.DrawRectangle(left, top, right, bottom: Extended);
begin
  ShowMessage('left='+FloatToStr(left)+' top='+FloatToStr(top)+
    ' right='+FloatToStr(right)+' bottom='+FloatToStr(bottom));
end;
//
var
  MyClass: TMyClass; //I create it later

Now I call a function in the script defined as procedure
OnMyClass(myclass: TMyClass); Then I pass the class in parameters.

*Example of script*
procedure OnMyClass(myclass: TMyClass);
begin
  myClass.DrawRectangle(1,2,3,4);
end;

*Results of all that*
We would expect to have a nice message displaying 'left=1 top=2
right=3 bottom=4', but in fact I'm getting 'left=4 top=3 right=2
bottom=2'. So parameter order is reversed and I'm always loosing the
first (last!?!) parameter and the second one is duplicated to the
first.

The ROPS units are compiled in delphi mode in there units. My
application is pure fpc. I'm under winXP.

I'm still doing many other tests meanwhile.

Best regards.

--
Alexandre Leclerc
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: PascalScript / FPC: parameters problems (need internals gurus I think)

Jonas Maebe-2

On 08 Aug 2006, at 20:39, Alexandre Leclerc wrote:

> *Problem*
> I'm exporting some classes (originally a TBitmap; now a custom class)
> to the ROPS engine and then interacting with the class in the
> scripting engine. Everything works, except for some functions that
> behave very strange. They skip/duplicate the parameters and invert
> parameters.

Try declaring the methods as "stdcall" (although I'm not sure whether  
this works for methods).


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: PascalScript / FPC: parameters problems (need internals gurus I think)

Alexandre Leclerc
2006/8/8, Jonas Maebe <[hidden email]>:

>
> On 08 Aug 2006, at 20:39, Alexandre Leclerc wrote:
>
> > *Problem*
> > I'm exporting some classes (originally a TBitmap; now a custom class)
> > to the ROPS engine and then interacting with the class in the
> > scripting engine. Everything works, except for some functions that
> > behave very strange. They skip/duplicate the parameters and invert
> > parameters.
>
> Try declaring the methods as "stdcall" (although I'm not sure whether
> this works for methods).

This causes External SIGFPE. Seeing the call, I'm not sure this is the
problem. I must say that what I explaned here is also true (and doing
the same thing) with Bitmap.Canvas.Rectangle() method.

--
Alexandre Leclerc
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: PascalScript / FPC: parameters problems (need internals gurus I think)

Michael Van Canneyt
In reply to this post by Jonas Maebe-2


On Tue, 8 Aug 2006, Jonas Maebe wrote:

>
> On 08 Aug 2006, at 20:39, Alexandre Leclerc wrote:
>
> > *Problem*
> > I'm exporting some classes (originally a TBitmap; now a custom class)
> > to the ROPS engine and then interacting with the class in the
> > scripting engine. Everything works, except for some functions that
> > behave very strange. They skip/duplicate the parameters and invert
> > parameters.
>
> Try declaring the methods as "stdcall" (although I'm not sure whether this
> works for methods).

That won't work. Delphi doesn't use stdcall by default, so the ROPS engine
doesn't expect this.

The problem is more likely that all arguments are extended, which probably
means that the stack is used differently. Are they pushed on the FPU stack ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: PascalScript / FPC: parameters problems (need internals gurus I think)

Alexandre Leclerc
2006/8/8, Michael Van Canneyt <[hidden email]>:

>
>
> On Tue, 8 Aug 2006, Jonas Maebe wrote:
>
> >
> > On 08 Aug 2006, at 20:39, Alexandre Leclerc wrote:
> >
> > > *Problem*
> > > I'm exporting some classes (originally a TBitmap; now a custom class)
> > > to the ROPS engine and then interacting with the class in the
> > > scripting engine. Everything works, except for some functions that
> > > behave very strange. They skip/duplicate the parameters and invert
> > > parameters.
> >
> > Try declaring the methods as "stdcall" (although I'm not sure whether this
> > works for methods).
>
> That won't work. Delphi doesn't use stdcall by default, so the ROPS engine
> doesn't expect this.
>
> The problem is more likely that all arguments are extended, which probably
> means that the stack is used differently. Are they pushed on the FPU stack ?

I was exactly thinking about the stack. But I don't know how it work.
Ok, now Bitmap.Canvas.Rectangle uses Integer and it has the same
behaviour. So it is probably not only a matter of extended? Now I
would gladly answer that question, but Carlo Kok is in vacation (he is
The guy about ROPS). Well, I like what you bring - I'm no stack expert
- but if you want you can speculate more :) I can try to find answers
in the code.

--
Alexandre Leclerc
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: PascalScript / FPC: parameters problems (need internals gurus I think)

Alexandre Leclerc
In reply to this post by Michael Van Canneyt
2006/8/8, Michael Van Canneyt <[hidden email]>:
> The problem is more likely that all arguments are extended, which probably
> means that the stack is used differently. Are they pushed on the FPU stack ?

Ok, I found this procedure that is used to handle parameters... Maybe
this is the actual asm code who is not good... ???

procedure PutOnFPUStackExtended(ft: extended);
asm
  fstp tbyte ptr [ft]
end;

And here is a part of the monstruous function MyAllMethodsHandler2
that work with the arguments:
//...
  if (Res <> nil) then
  begin
    Params.DeleteLast;
    if (ResultAsRegister(Res.FType)) then
    begin
      if (res^.FType.BaseType = btSingle) or (res^.FType.BaseType = btDouble) or
      (res^.FType.BaseType = btCurrency) or (res^.Ftype.BaseType =
btExtended) then
      begin
        case Res^.FType.BaseType of
          btSingle: PutOnFPUStackExtended(PPSVariantSingle(res).Data);
          btDouble: PutOnFPUStackExtended(PPSVariantDouble(res).Data);
          btExtended: PutOnFPUStackExtended(PPSVariantExtended(res).Data);
          btCurrency: PutOnFPUStackExtended(PPSVariantCurrency(res).Data);
        end;
        DestroyHeapVariant(Res);
        Res := nil;
      end else
      begin
{$IFNDEF PS_NOINT64}
        if res^.FType.BaseType <> btS64 then
{$ENDIF}
          CopyArrayContents(Pointer(Longint(Stack)-8),
@PPSVariantData(res)^.Data, 1, Res^.FType);
      end;
    end;
    DestroyHeapVariant(res);
  end;
//...

--
Alexandre Leclerc
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: PascalScript / FPC: parameters problems (need internals gurus I think)

Alexandre Leclerc
The actual procedure that is called. It does some asm processing and
then calls the MyAllMethodsHandler2 that works with parameters. Maybe
this is there that there is a problem? (This is all in the
uPSRuntime.pas file)

procedure MyAllMethodsHandler;
//  On entry:
//     EAX = Self pointer
//     EDX, ECX = param1 and param2
//     STACK = param3... paramcount
asm
  push 0
  push ecx
  push edx
  mov edx, esp
  add edx, 16 // was 12
  pop ecx
  call MyAllMethodsHandler2
  pop ecx
  mov edx, [esp]
  add esp, eax
  mov [esp], edx
  mov eax, ecx
end;


2006/8/8, Alexandre Leclerc <[hidden email]>:
> 2006/8/8, Michael Van Canneyt <[hidden email]>:
> > The problem is more likely that all arguments are extended, which probably
> > means that the stack is used differently. Are they pushed on the FPU stack ?
>
> Ok, I found this procedure that is used to handle parameters... Maybe
> this is the actual asm code who is not good... ???

--
Alexandre Leclerc
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: PascalScript / FPC: parameters problems (need internals gurus I think)

Alexandre Leclerc
2006/8/8, Alexandre Leclerc <[hidden email]>:
> The actual procedure that is called. It does some asm processing and
> then calls the MyAllMethodsHandler2 that works with parameters. Maybe
> this is there that there is a problem? (This is all in the
> uPSRuntime.pas file)

For the records, issue has been fixed by Carlo Kok in svn tree of ROPS
at revision 11 (as of 2006/08/18). It now works great in FPC!

--
Alexandre Leclerc
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal