Recent

Author Topic: PascalScript and Windows x64 on Lazarus RC1  (Read 10417 times)

21347

  • New member
  • *
  • Posts: 7
PascalScript and Windows x64 on Lazarus RC1
« on: August 27, 2012, 12:13:44 am »
Although there are plenty of topics about PascalScript, they're either zombies, or refering to x86-Code (or both), so I'm starting a new one. Hopefully I found the right Forum..

Has anyone successfully used PascalScript with Lazarus Classes (e.g. TForm, TButton) on Win x64?

I can compile the downloadable latest stable version from their website (the one with the EXE-Installer) as well as the latest development version from https://github.com/remobjects/pascalscript - no Problem there. Running the script works, adding user defined funktions is ok, too.

But as soon as I try to Create forms and objects, like the following script, the Program (not the Script) crashes with a SIGSEGV.

Code: [Select]
program Script;

var
  f:TForm;
  b:TButton;
begin
  f:=TForm.CreateNew(nil); //<- Works
  b:=TButton.Create(f); //<- crash
  b.Parent:=f;
  f.Show;
end.

Crash in the Program is here:
Code: [Select]
CLASSES_TCOMPONENT_$__INSERTCOMPONENT$TCOMPONENT
000000000044F900 4883ec38                 sub    $0x38,%rsp
000000000044F904 48895c2420               mov    %rbx,0x20(%rsp)
000000000044F909 4889742428               mov    %rsi,0x28(%rsp)
000000000044F90E 4889cb                   mov    %rcx,%rbx
000000000044F911 4889d6                   mov    %rdx,%rsi
000000000044F914 4889da                   mov    %rbx,%rdx
000000000044F917 4889f1                   mov    %rsi,%rcx
000000000044F91A 4889f0                   mov    %rsi,%rax
000000000044F91D 488b00                   mov    (%rax),%rax
000000000044F920 ff9068010000             callq  *0x168(%rax)  <-- crash

Am I doing anything wrong? The same Script runs fine when the Application was compiled with x86 Lazarus RC1.
By the way: creating a TObject with e.g. o:=TObject.Create doesn't break at al.

Interestingly, when I start debuging <SomeClass>.Create(aOwner:TComponent)-Type of Constructors, there is some difference in the Register values, depending on whether the Constructor was called from "pure" FPC-Code or the Script (at least so it seems):
o Called from FPC: rcx: 0 (nil), rdx: Class Reference, r8: Value of aOwner
o Called from Script: rcx: Class Reference, rdx: 1 (!?), r8: Value of aOwner (at least so it seems)

Is there any difference in the calling convention on x64-Systems between Delphi and Lazarus? I could only find the "standard" information on Windows x64 calling conventions, without special care of constructors, if there is any...

Did anybody else stumble about that problem bevore?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5370
    • wiki
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #1 on: August 29, 2012, 06:22:49 pm »
I don't have the answers to your questions.

But leaving out the PascalScript part, and investigating the calling convention, the best place to ask is the fpc mail list.

TheBlackSheep

  • Jr. Member
  • **
  • Posts: 93
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #2 on: August 29, 2012, 10:16:41 pm »
I tried unsuccessfully to get a PascalScript related application I wrote using the CodeTyphon (~v2.40) with Ubuntu 64 (works fine on Ubuntu 32bit and Windows and partially on MacOS) - at that time I couldn't get the SVN version of PascalScript to recompile without amending some of the assembler parts slightly - seem to remember there was a register call that needed some extra brackets around it - the SVN suggested that the code had been modified to work in Delphi 64 bit but I didn't have a copy of win64 to test with.

I've been waiting for PilotLogic to put some Titanscript fixes into their CodeTyphon Lazarus build (supposedly more advanced that PascalScript but I couldn't get it to work doing similar requirements to you - although that was such a long time ago now they might have forgotten some fixes are still required).

I must put the source code of the application I wrote up on a Lazarus related share somewhere if someone can suggest where that could be hosted.  There's so little available information for PascalScript (I managed to find some very old sites that had a bit of information - enough to get me going anyway - an example with some published source must be useful to someone...

TheBlackSheep

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #3 on: August 29, 2012, 11:41:43 pm »
I must put the source code of the application I wrote up on a Lazarus related share somewhere if someone can suggest where that could be hosted.
I like bitbucket myself... see sig.

Thanks for uploading ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

21347

  • New member
  • *
  • Posts: 7
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #4 on: September 04, 2012, 01:27:27 pm »
Hey, seem's I'm not allone with that problem, at all.

But leaving out the PascalScript part, and investigating the calling convention, the best place to ask is the fpc mail list.
Yep, your probably right. I'll do this.

at that time I couldn't get the SVN version of PascalScript to recompile without amending some of the assembler parts slightly - seem to remember there was a register call that needed some extra brackets around it
Yes, I forgott to mention what I changed in x64.inc to get it to compile:

Code: [Select]
Index: x64.inc
===================================================================
--- x64.inc (revision 38441)
+++ x64.inc (working copy)
@@ -14,7 +14,7 @@
   _RCX, _RDX, _R8, _R9: IPointer;
   var _XMM0: Double;
   _XMM1, _XMM2, _XMM3: Double;
-  aStack: Pointer; aItems: IntPtr); assembler; {$IFDEF FPC}nostackframe;{$ENDIF}
+  aStack: Pointer; aItems: PtrUInt); assembler; {$IFDEF FPC}nostackframe;{$ENDIF}
 asm
 (* Registers:
     RCX: Address
@@ -37,7 +37,7 @@
   mov rdx, aStack
   jmp @compareitems
 @work:
-  push [rdx]
+  push qword ptr [rdx]
   dec rcx
   sub rdx,8
 @compareitems:

There's so little available information for PascalScript (I managed to find some very old sites that had a bit of information - enough to get me going anyway - an example with some published source must be useful to someone...
That would be great!

Thanks for all your help!

21347

  • New member
  • *
  • Posts: 7
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #5 on: September 06, 2012, 05:49:05 pm »
With the Help from the List, I came to the solution, that the Problem existed only when calling virtual constructors.

There existed an extra treatment of those for the x86 version, but not for x64. After kind of copying the code over, it works:

Code: [Select]
Index: x64.inc
===================================================================
--- x64.inc (revision 38441)
+++ x64.inc (working copy)
@@ -14,7 +14,7 @@
   _RCX, _RDX, _R8, _R9: IPointer;
   var _XMM0: Double;
   _XMM1, _XMM2, _XMM3: Double;
-  aStack: Pointer; aItems: IntPtr); assembler; {$IFDEF FPC}nostackframe;{$ENDIF}
+  aStack: Pointer; aItems: PtrUInt); assembler; {$IFDEF FPC}nostackframe;{$ENDIF}
 asm
 (* Registers:
     RCX: Address
@@ -37,7 +37,7 @@
   mov rdx, aStack
   jmp @compareitems
 @work:
-  push [rdx]
+  push qword ptr [rdx]
   dec rcx
   sub rdx,8
 @compareitems:
@@ -280,6 +280,9 @@
   CallData: TPSList;
   I: Integer;
   pp: ^Byte;
+{$IFDEF FPC}
+  IsVirtualCons: Boolean;
+{$ENDIF}
 
   function rp(p: PPSVariantIFC): PPSVariantIFC;
   begin
@@ -579,6 +582,18 @@
 {$ENDIF}
     _RAX := 0;
     RegUsage := 0;
+    {$IFDEF FPC}
+    // FIX FOR FPC constructor calls
+    if (Integer(CallingConv) and 128) <> 0 then begin
+      IsVirtualCons := true;
+    end else
+      IsVirtualCons:= false;
+    if IsVirtualCons then begin
+      if not GetPtr(rp(Params[0])) then exit; // this goes first
+      StoreReg(IPointer(_Self));
+      Params.Delete(0);
+    end else
+    {$ENDIF}
     if assigned(_Self) then begin
       StoreReg(IPointer(_Self));
     end;

A Problem still existing is, that one cannot simply assign EventHandlers like OnClick from within the script on x64. I've made a "patch" thats not perfect but working for me, which can be found here: http://pastebin.com/c8A81kju

Dgby714

  • Newbie
  • Posts: 4
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #6 on: December 13, 2012, 09:42:51 am »
With the Help from the List, I came to the solution, that the Problem existed only when calling virtual constructors.

There existed an extra treatment of those for the x86 version, but not for x64. After kind of copying the code over, it works:

Code: [Select]
Index: x64.inc
===================================================================
--- x64.inc (revision 38441)
+++ x64.inc (working copy)
@@ -14,7 +14,7 @@
   _RCX, _RDX, _R8, _R9: IPointer;
   var _XMM0: Double;
   _XMM1, _XMM2, _XMM3: Double;
-  aStack: Pointer; aItems: IntPtr); assembler; {$IFDEF FPC}nostackframe;{$ENDIF}
+  aStack: Pointer; aItems: PtrUInt); assembler; {$IFDEF FPC}nostackframe;{$ENDIF}
 asm
 (* Registers:
     RCX: Address
@@ -37,7 +37,7 @@
   mov rdx, aStack
   jmp @compareitems
 @work:
-  push [rdx]
+  push qword ptr [rdx]
   dec rcx
   sub rdx,8
 @compareitems:
@@ -280,6 +280,9 @@
   CallData: TPSList;
   I: Integer;
   pp: ^Byte;
+{$IFDEF FPC}
+  IsVirtualCons: Boolean;
+{$ENDIF}
 
   function rp(p: PPSVariantIFC): PPSVariantIFC;
   begin
@@ -579,6 +582,18 @@
 {$ENDIF}
     _RAX := 0;
     RegUsage := 0;
+    {$IFDEF FPC}
+    // FIX FOR FPC constructor calls
+    if (Integer(CallingConv) and 128) <> 0 then begin
+      IsVirtualCons := true;
+    end else
+      IsVirtualCons:= false;
+    if IsVirtualCons then begin
+      if not GetPtr(rp(Params[0])) then exit; // this goes first
+      StoreReg(IPointer(_Self));
+      Params.Delete(0);
+    end else
+    {$ENDIF}
     if assigned(_Self) then begin
       StoreReg(IPointer(_Self));
     end;

A Problem still existing is, that one cannot simply assign EventHandlers like OnClick from within the script on x64. I've made a "patch" thats not perfect but working for me, which can be found here: http://pastebin.com/c8A81kju

Have you made any changes to this patch? It doesn't seem to work for me.

I am using fpc 2.6.0 and Laz 1.0.3.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #7 on: December 13, 2012, 10:05:11 am »
If you're looking at using PascalScript on x64, you might want to look into Lazarus trunk. Martin_fr is working on getting pascalscript working in order to get scripting support for the IDE.
See http://wiki.lazarus.freepascal.org/Editor_Macros_PascalScript
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Dgby714

  • Newbie
  • Posts: 4
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #8 on: December 13, 2012, 10:18:35 am »
If you're looking at using PascalScript on x64, you might want to look into Lazarus trunk. Martin_fr is working on getting pascalscript working in order to get scripting support for the IDE.
See http://wiki.lazarus.freepascal.org/Editor_Macros_PascalScript

Actually turns out my problem was compiling using 32bit lazarus >..> rookie mistake just assumed I was using 64bit without checking.

Seems the patch does work for me. =)

I think I might take a look, We have been using PascalScript for a while now, without 64bit or Linux support.

Tho recently one of out community member wrote a alternative to PascalScript called Lape.

We haven't completely moved to Lape becuase of it's few missing features. (Some kinda of class support, Calling script methods from the program [callbacks])

I've made a few edits to PascalScript myself, you can see it at https://github.com/SRL/PascalScript

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5370
    • wiki
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #9 on: December 13, 2012, 03:06:06 pm »
Actually turns out my problem was compiling using 32bit lazarus >..> rookie mistake just assumed I was using 64bit without checking.

Actually, all the fixes I made to Lazarus trunk, have been back-ported, so you can get the latest GIT of PascalScript, if you like.
1) I have not tested calling constructors (and the related bit of the patch is still missing.

2) On the other hand IIRC I fixed stack align. This will only be noticed if code called by PascalScript does call the OS.

3) I am aware (but have not yet found the culprit) that passing openarray on 64 bit is faulty. It will seem to work, but something writes to random not allocated memory, and it will crash at some time later. (e.g. using "format(string, [params])" will lead to crashes on 64 bit)


Dgby714

  • Newbie
  • Posts: 4
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #10 on: December 14, 2012, 02:35:49 am »
Have you tried setting any TNotifyEvent in PascalScript? It works fine on 32bit (before the latest changes in the repo) but doesn't work on 64bit....

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5370
    • wiki
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #11 on: December 14, 2012, 02:58:40 am »
IIRC its not implemented for 64 bit.

I think I saw a patch https://github.com/remobjects/pascalscript/issues (not sure which issue)

Dgby714

  • Newbie
  • Posts: 4
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #12 on: December 14, 2012, 03:34:50 am »
Yes, but, for some reason, with the latest commits to the repo, 32bit doesn't work.

I see no reason to try that patch and get 64bit working if 32bit isn't.

If I revert this commit -> https://github.com/remobjects/pascalscript/commit/b276c09909ddae1f7869970ad0f600b773a99ef7
It gets past the assign but the clicks still don't work.
« Last Edit: December 14, 2012, 03:38:31 am by Dgby714 »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5370
    • wiki
Re: PascalScript and Windows x64 on Lazarus RC1
« Reply #13 on: December 14, 2012, 04:07:44 am »
That commit is not the "latest". There where commits in the last couple of days.

As for events, I haven't tested/used them myself. So I do not know much about them.