Oxygen Basic

Programming => Example Code => General => Topic started by: Charles Pegge on November 13, 2011, 03:00:02 AM

Title: First 64 bit Windows compilation
Post by: Charles Pegge on November 13, 2011, 03:00:02 AM
I'm starting to get a grip on this 64bit stuff. The calling convention is good for low-level coding due to its use of the extra registers instead the stack, but not so good for high level programming where expressions and functions may be passed as arguments, and inevitably the stack must be used to hold the values before they are passed.

The stack frame for the parameters has to be kept aligned to 16 bytes whenever an OS call is made, and floats have to be passed in the SIMD (xmm0..3) registers, which is a considerable pain when many functions require the services of the Floating point processor (FPU) .

More to be done but 2 independent executables are included below, and the source code which works on the latest Oxygen-in-progress (just posted). You will see that the 64 bit binary is significantly larger due to the 64bit opcode prefixes and stack assignment encodings.

Code: OxygenBasic
  1.  
  2.   'Charles Pegge
  3.  '13 Nov 2011
  4.  
  5.   '===========
  6.  'COMPILATION
  7.  '===========
  8.  
  9.   $ filename "t.exe"
  10.   '
  11.  '$ CompileDependent
  12.  '$ Compile32BitIndependent
  13.  '$ Compile64BitIndependent
  14.  '
  15.  '
  16.  #ifdef CompileDependent
  17.     #file filename
  18.     const string title="OXYGEN BASIC Compiled"
  19.   #else
  20.     const string title="OXYGEN BASIC"
  21.   #endif
  22.   '
  23.  '
  24.  #ifdef Compile64BitIndependent
  25.     #include   "../../inc/RTL64.inc"
  26.     const string title="OXYGEN BASIC 64-bit"
  27.   #endif
  28.   '
  29.  '
  30.  #ifdef Compile32BitIndependent
  31.     #include   "../../inc/RTL32.inc"
  32.     const string title="OXYGEN BASIC 32-bit"
  33.   #endif
  34. ....
  35.  

Charles
Title: Re: First 64 bit Windows compilation
Post by: efgee on November 13, 2011, 01:37:15 PM
Good job, works great  8)

If you need a 64bit debugger:
I use the free Visual DuxDebugger (http://visual-duxdebugger.en.malavida.com/)

Take care
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 13, 2011, 02:01:41 PM

Thank you Frank. I've made a note of the link.

It should be plain sailing from now on. The first hurdle was to get the PE headers correct. The next was to display a message box. After that the biggest step was to get the float_to_ascii and hex functions working. With these working, the diagnostics became much easier.

Charles
Title: Re: First 64 bit Windows compilation
Post by: efgee on November 13, 2011, 05:12:30 PM
Tested the ClassLibrary examples  :'(

It seems there is still some work to do...
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 13, 2011, 10:39:43 PM

Yes of course Frank. My task for the next few weeks is to get the more complex programs working.


Charles
Title: Re: First 64 bit Windows compilation
Post by: efgee on November 13, 2011, 11:31:03 PM
Understood.
Happy hunting  ;D
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 22, 2011, 01:22:28 AM
I've now got 64 bit binaries working with the hard stuff: Windows-GUI, OpenGL, GDIplus images, COM (SAPI5 Speech engine IspVoice) and the ClassLibrary demo.

Latest Oxygen-in-Progress  21 Nov 2011

Quote

Programs Successfully Compiling to 64 bit executables

examples/GUI/

  ChildWin.o2bas
  HelloWin.o2bas
  FileFuncs.o2bas
  GraphWin.o2bas
  KeyboardCodesCmpl.o2bas
  OpenglChildWins.o2bas
  OpenglWin1.o2bas
  OpenglWin2.o2bas
  OpenglWin3.o2bas
  OpenglWinTex.o2bas
  PortViewer1.o2bas

examples/COM/

  Sapi4Cmpl.o2bas

examples/OOP/ClassLibrary64/

  ClassLibrary64.o2bas
  ClassLibraryTest64.o2bas
  

Charles
Title: Re: First 64 bit Windows compilation
Post by: kryton9 on November 22, 2011, 11:07:50 AM
Thanks Charles, will grab the latest and test them out.
Title: Re: First 64 bit Windows compilation
Post by: JRS on November 22, 2011, 11:22:53 AM
Charles,

Is your plan to maintain both a 32 and 64 bit version of OxygenBasic or is it 64 bit moving forward?

John
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 22, 2011, 11:48:48 AM

Hi John,

My current plan is to keep the Oxygen compiler itself 32 bit, producing 32 and 64 bit binaries on demand.

Charles
Title: Re: First 64 bit Windows compilation
Post by: JRS on November 22, 2011, 02:04:20 PM
Sounds like a wise and safe direction to take at this juncture in the development cycle.

Title: Re: First 64 bit Windows compilation
Post by: Petr Schreiber on November 23, 2011, 12:17:58 AM
Go 64bit, go :)

I am very happy you are working on this Charles...


Petr
Title: Re: First 64 bit Windows compilation
Post by: JRS on November 23, 2011, 04:59:17 PM
Quote
My current plan is to keep the Oxygen compiler itself 32 bit, producing 32 and 64 bit binaries on demand.

OxygenBasic might become a popular tool to build 64 bit DLLs. What is your stand on .NET support?

Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 23, 2011, 07:57:53 PM

I have never knowingly used NET but perhaps Petr is familiar with this area. Externally produced DLLs fall into the category of "unmanaged code".

Charles
Title: Re: First 64 bit Windows compilation
Post by: Petr Schreiber on November 23, 2011, 11:46:13 PM
Hi,

you can call "normal" and COM DLLs from .NET application, so for this purpose no specific effort from Charles side should be necessary.

Adding support for .NET DLLs would be loss of time at the moment - even express editions of VisualStudio already do them very well.
What interests me are high performance classic DLLs, which is what Charles can do.

Is there any Oxygen example on 64bit "normal" DLL? I could test it for interfacing with C# during weekend.


Petr
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 24, 2011, 02:51:09 AM
Hi Petr,

Simple DLL test with bstrings

Code: OxygenBasic
  1.  
  2.   '
  3.  $dll
  4.   $filename "t64.dll"
  5.   #include "..\..\inc\RTL64.inc"
  6.  
  7.  
  8.   print "t64.dll Loaded"
  9.  
  10.   function HelloA(string s) as string, export
  11.     return "HelloA "+s
  12.   end function
  13.  
  14.   function HelloW(wstring s) as wstring, export
  15.     return "HelloW "+s
  16.   end function
  17.  

Testing from Oxygen:

Code: OxygenBasic
  1.   $filename "TestHello64.exe"
  2.   #include "..\..\inc\RTL64.inc"
  3.  
  4.  
  5.   declare function HelloA lib "t64.dll" (string s) as string
  6.   declare function HelloW lib "t64.dll" (wstring s) as wstring
  7.  
  8.   print HelloA "World!"
  9.   print HelloW "World!"
  10.  
  11.  

I will add a DLLs folder to the /examples section

Charles

PS: compiled code here:
Title: Re: First 64 bit Windows compilation
Post by: Petr Schreiber on November 24, 2011, 11:46:48 AM
Hi Charles,

I got this code in C# to wrap HelloW:
Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("t64.dll", EntryPoint = "HelloW")]
        [return: MarshalAs(UnmanagedType.TBStr)]
        public static extern string HelloW([MarshalAs(UnmanagedType.TBStr)] string sText);

        static void Main(string[] args)
        {
            Console.WriteLine(HelloW("Ciao Charles, this is great!"));
            
            Console.ReadKey();
        }
    }
}

It prints to my console
Quote
HelloW Ciao Charles, this is great!

which is ... correct! Tried even some Czech texts, worked perfectly...

Is there any preview release of 64bit-able oxygen available? I will be happy to continue with more complex tests... ;)


Petr
Title: Re: First 64 bit Windows compilation
Post by: Peter on November 24, 2011, 12:06:35 PM
to be honest, I would rather write with OxygenBasic.
C# is from another world without atmosphere.  :D
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 24, 2011, 01:50:40 PM

Glad it works Petr, that is very reassuring.

I'm chasing a bug affecting negative floating point / integer conversions, which seems to be down at the opcode level. But I will post my latest now.

(Oxygen in-progress version from the website)

Charles
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 24, 2011, 09:30:59 PM
Fixed those bugs.

I'm working through some of Peter's demo programs with a 64 bit-compatible version of window.h. These are in the projects/GDIWindow folder

Charles

PS: Judging by the complexity of the C# protocol, they seem to be saying:
"DLLs are weird primitive entities and are not welcome here!"
Title: Re: First 64 bit Windows compilation
Post by: Petr Schreiber on November 24, 2011, 11:51:47 PM
Quote
PS: Judging by the complexity of the C# protocol, they seem to be saying:
"DLLs are weird primitive entities and are not welcome here!"

Hehe, yes. On the other side, the .NET DLLs do not need any declares at all, you just add the .NET DLL as reference to project and it works out of the box (including code completion integration to the IDE).

Thanks again for your efforts on the 64bit side, I will continue testing this weekend :)


Petr
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 26, 2011, 03:19:08 PM

Embedding header information in the DLL is definitely worth thinking about. It could be put into a string constant and thus be accessible to IDES, and programs at runtime. No special DLL section required.

Charles
Title: Re: First 64 bit Windows compilation
Post by: Petr Schreiber on November 27, 2011, 10:50:43 AM
Hi Charles,

seems good idea to me. But in such a case it would have to be header in specific language, or am I wrong? Or would it be some kind of general XML?

I was testing the Oxygen a bit this weekend. Interesting observation is that once I use:
Code: [Select]
#include "..\inc\rtl32.inc"
for example, then the F5 key from the SciTe does not execute anything, and just builds EXE.

I liked what I saw, only suggestions:
Code: [Select]
#include "rtl32.inc", which didn't worked. I had to setup the path as
Code: [Select]
#include "..\inc\rtl32.inc"Petr
Title: Re: First 64 bit Windows compilation
Post by: Charles Pegge on November 27, 2011, 01:27:56 PM
Hi Petr,

Many thanks for your observations.

The options available for SCITE compile / run are limited. The best strategy is to do all the debugging with a direct F5 run, then add the run-time library for independent compilation.

Using SCITE is a temporary measure, and the plan is to create a customised IDE based on Opengl, that can also be deployed in Linux.

The manual needs a lot more material including linked  in examples. CHM help is not the optimal solution. We need a system that ultimately plugs straight into the IDE.

Error messages could also link directly into the manual for fuller description.

Charles