dcsimg
Results 1 to 17 of 17

Thread: [WIP] LLVM bindings for VB6

  1. #1

    Thread Starter
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    715

    [WIP] LLVM bindings for VB6

    https://github.com/wqweto/VBLLVM/releases

    VBLLVM.dll in vbllvm-x86.[release|debug].zip is compiled with `stdcall` calling convention.

    There is a working basic sample in Module1.bas that show how to use LLVM to compile a function and how to interpret it vs JIT compile + execute.
    Code:
    Interpreter Result: 5
    MCJIT Result: 9
    ; ModuleID = 'my_module'
    source_filename = "my_module"
    target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
    
    define x86_stdcallcc i32 @sum(i32, i32) {
    entry:
      %tmp = add i32 %0, %1
      ret i32 %tmp
    }
    The sample dump function's IR (Intermediate Representation) and writes bytecode to Project1.bc which can be dumped with ..\..\lib\install-release\bin\llvm-dis.exe Project1.bc -o=con

    This is work in progress: will need a typelib to make all LLVM enums/types/functions available in VB6 projects and will need a complete Kaledoscope toy language sample implementatio
    Last edited by wqweto; Jul 6th, 2018 at 12:16 PM.

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    2,035

    Re: [WIP] LLVM bindings for VB6

    It's cool to see some good efforts towards an open source VB Compiler.

  3. #3
    Fanatic Member
    Join Date
    May 2014
    Location
    Preveza Greece
    Posts
    932

    Re: [WIP] LLVM bindings for VB6

    I am waiting to see how this can be used for M2000 interpreter. By the way here http://www.rosettacode.org/wiki/Cate...00_Interpreter you can find (at the end of page) code of M2000, running in an application M2000 Environment, written in VB6.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2002
    Posts
    272

    Re: [WIP] LLVM bindings for VB6

    Can you explain in more simple terms how this can be used with vb6 projects? Are you aiming at creating a LLVM compiler for vb6 apps?

  5. #5
    Hyperactive Member
    Join Date
    Jul 2002
    Posts
    272

    Re: [WIP] LLVM bindings for VB6

    Quote Originally Posted by axisdj View Post
    Can you explain in more simple terms how this can be used with vb6 projects? Are you aiming at creating a LLVM compiler for vb6 apps?
    Anyone have insight on this?

    I see the sample can basically compile itself. Does this mean I can compile parts of my vb6 app using llvm, how do the objects get put back together? can this work for a GUI app?

    Thanks
    WP

  6. #6
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    2,035

    Re: [WIP] LLVM bindings for VB6

    The project is a proof of concept testing out a portion of the LLVM tool-chain that has been partially adapted for use in VB.
    Mainly the JIT which converts the generated IM and Bytecode to machine language in memory, and then calls it.
    He posted the IR (Intermediate Representation) function in the original post.
    This part of the tool-chain is the critical part needed for edit and continue, etc.


    Also as mentioned in the first post - you still need to implement the portion that transforms your the VB code, into IR.
    More info on how transform VB to IR is here http://llvm.org/docs/tutorial/ and here http://llvm.org/docs/tutorial/OCamlLangImpl2.html
    Of course the examples are in C++ and targeting an example language (Kaleidoscope Toy Language).

    edit: I was hesitant to go the LLVM route myself - but I think Vlad is right by pushing the use of LLVM.
    It's a way to position the future of VB for the long haul. Otherwise it's stuck with a handmade parser, written by one person. (which is much simpler, and potentially faster, but less flexible)

    maybe wqweto can chime in to clear up anything I've misinterpreted.
    Last edited by DEXWERX; Jul 9th, 2018 at 02:03 PM.

  7. #7

    Thread Starter
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    715

    Re: [WIP] LLVM bindings for VB6

    Quote Originally Posted by axisdj View Post
    Can you explain in more simple terms how this can be used with vb6 projects? Are you aiming at creating a LLVM compiler for vb6 apps?
    This would be a long shot to go straight to the moon. . .

    LLVM is a library to enable writing compiler backends, the machine code generating part of a compiler/interpreter. It has lots of benefits like fairly easy aiming different OSes (Windows vs Linux), different architectures (x86 vs x64) and different CPUs (Intel vs ARM). And it allows to interpret/JIT compile your (dynamic) language too.

    It's the main booster rocket for clang compiler, which can emulate both gcc and cl.exe and optimize the final output better than the originals. That's the reason most recent languages are developed with LLVM as backend (e.g. Rust, Zig, etc.)

    My first aim is to port Kaleidoscope toy language in VB6, make a cross-compiler which can produce both x86 and x64 binaries and then make an interpreter that can JIT compile sources while executing them straight. I've already done a similar cross-compiler lowering to Lua but the LLVM backend is going to take much more effort as first I have to finish the tooling before starting the toy compiler project.

    My second aim is to make a VBScript compiler, then extend it w/ types, then evolve it w/ features the way VB1 to VB6 has evolved until finally it can bootstrap itself - i.e. it can compile it's own sources and the produced binaries can compile these sources once again successfully.

    cheers,
    </wqw>

  8. #8

    Thread Starter
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    715

    Re: [WIP] LLVM bindings for VB6

    Hi all,

    Second release 0.2 includes an auto-generated typelib (VBLLVM.tlb) that declares all the 800+ functions exported by the DLL, along with all the enums, structs and typedefs used by parameters/return types.

    The source IDL for the typelib is automatically generated by a gen_idl utility that includes a PEG parser compiled w/ VbPeg that parses LLVM's C/C++ header files to internal JSON representation and converts C/C++ data-types to VB6/IDL compatible ones.

    cheers,
    </wqw>

  9. #9
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    2,035

    Re: [WIP] LLVM bindings for VB6

    could you make it so the enums and structs use the same name as the typedef. Otherwise MIDL will auto generate the alias. kind of pollutes the symbol table

    just nitpicking of course.

    Code:
        typedef enum LLVMVisibility {
            LLVMDefaultVisibility,
            LLVMHiddenVisibility,
            LLVMProtectedVisibility,
        } LLVMVisibility;

  10. #10
    Addicted Member
    Join Date
    Jan 2015
    Posts
    135

    Re: [WIP] LLVM bindings for VB6

    Last edited by loquat; Jul 10th, 2018 at 08:19 PM. Reason: update links

  11. #11

    Thread Starter
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    715

    Re: [WIP] LLVM bindings for VB6

    Major milestone reached today for kscope.exe test project: The Kaleidoscope toy language VB6 port is now able to execute mandelbrot fractal sample on MCJIT correctly.

    Code:
    C:\Work\Temp\VBLLVM\test\kscope\test>..\kscope.exe mandel.ks
    kscope VB6 port 0.1 (c) 2018 by wqweto@gmail.com (7.8.2018 12:25:28)
    
    *******************************************************************************
    *******************************************************************************
    ****************************************++++++*********************************
    ************************************+++++...++++++*****************************
    *********************************++++++++.. ...+++++***************************
    *******************************++++++++++..   ..+++++**************************
    ******************************++++++++++.     ..++++++*************************
    ****************************+++++++++....      ..++++++************************
    **************************++++++++.......      .....++++***********************
    *************************++++++++.   .            ... .++**********************
    ***********************++++++++...                     ++**********************
    *********************+++++++++....                    .+++*********************
    ******************+++..+++++....                      ..+++********************
    **************++++++. ..........                        +++********************
    ***********++++++++..        ..                         .++********************
    *********++++++++++...                                 .++++*******************
    ********++++++++++..                                   .++++*******************
    *******++++++.....                                    ..++++*******************
    *******+........                                     ...++++*******************
    *******+... ....                                     ...++++*******************
    *******+++++......                                    ..++++*******************
    *******++++++++++...                                   .++++*******************
    *********++++++++++...                                  ++++*******************
    **********+++++++++..        ..                        ..++********************
    *************++++++.. ..........                        +++********************
    ******************+++...+++.....                      ..+++********************
    *********************+++++++++....                    ..++*********************
    ***********************++++++++...                     +++*********************
    *************************+++++++..   .            ... .++**********************
    **************************++++++++.......      ......+++***********************
    ****************************+++++++++....      ..++++++************************
    *****************************++++++++++..     ..++++++*************************
    *******************************++++++++++..  ...+++++**************************
    *********************************++++++++.. ...+++++***************************
    ***********************************++++++....+++++*****************************
    ***************************************++++++++********************************
    *******************************************************************************
    *******************************************************************************
    *******************************************************************************
    *******************************************************************************
    *******************************************************************************
    Evaluated to 0
    The kscope.exe as compiler (emitting .obj files) is already working too, though lack of any runtime impl prevents successful linking.

    cheers,
    </wqw>

  12. #12

    Thread Starter
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    715

    Re: [WIP] LLVM bindings for VB6

    In release 0.3 of VBLLVM project the Kaleidoscope toy language compiler's VB6 port is feature-complete (in test/kscope folder), implementing x64 (default) and x86 cross-compiler (including static runtime for each architecture) *and* built-in MCJIT interpreter in a single kscope.exe 32-bit VB6 binary.

    Unfortunately the toy compiler produced executables are instantenously flagged by Windows Defender as being too small and too useless not to be malware/trojan/dropper/whatever :-)) Too sad. . .

    cheers,
    </wqw>

  13. #13

    Thread Starter
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    715

    Re: [WIP] LLVM bindings for VB6

    In release 0.4 of VBLLVM project both the debug and release DLLs now include LLVM's LLD linker (staticly) built-in, which allows kscope project (or any other compiler) to produce EXEs with no external dependenices, while it previously needed to shell an external linker (e.g LINK.exe from VB6 folder or LLVM's lld-link.exe) as a final step.

    Bringing LLD linker in VBLLVM.dll finally allows for building a true architecture and *OS* cross-compiler, one that can build Linux ELF binaries on Windows hosts. At present kscope compiler accepts -target option to specify i686-unknown-linux-gnu or x86_64-unknown-linux-gnu triples and it can successfully produce ELF object files from the compilation phase but there are problems with libc linking (failing dependencies) which needs further research on my part.

    cheers,
    </wqw>

  14. #14
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    2,035

    Re: [WIP] LLVM bindings for VB6

    That's just awesome.

  15. #15
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    538

    Re: [WIP] LLVM bindings for VB6

    Wow, fantastic project !!!

    Good luck to you!

  16. #16

    Thread Starter
    Fanatic Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    715

    Re: [WIP] LLVM bindings for VB6

    10x to both of you, really appreciated!

    FYI, just pushed release 0.4.1 that has kscope ELF linker problems and it's linux runtime problems sorted out.

    Now the toy language compiler is able to produce both 32-bit and 64-bit Linux ELF executables under *Windows* (being a x86 .exe) and doing this with *no* external dependencies helping it other than VBLLVM.dll itself. . . mind boggling to me!

    Probably lazarus does it but I personally have never seen a compiler on Windows do this.

    Edit: the ELF executable are dynamicly linked againt libc.so and work as expected under WSL and full-blown VMs w/ CentOS 6/7.

    cheers,
    </wqw>

  17. #17
    Hyperactive Member
    Join Date
    Jul 2002
    Posts
    272

    Re: [WIP] LLVM bindings for VB6

    wqweto congratulations on your progress, we all thank you and wish you the best. This looks very promising for the future of VB6 if I understand it correctly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width