libjit - Compile and execute code in memory at runtime

User contributed sources that have become inactive, deprecated, or generally unusable. But ... we don't really want to throw them away either.
Post Reply
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

libjit - Compile and execute code in memory at runtime

Post by voodooattack »

Description:
The libjit library implements Just-In-Time compilation functionality. Unlike other JIT's, this one is designed to be independent of any particular virtual machine bytecode format or language. The hope is that Free Software projects can get a leg-up on proprietry VM vendors by using this library rather than spending large amounts of time writing their own JIT from scratch.

This JIT is also designed to be portable to multiple archictures. If you run libjit on a machine for which a native code generator is not yet available, then libjit will fall back to interpreting the code. This way, you don't need to write your own interpreter for your bytecode format if you don't want to.
Features:
  • The primary interface is in C, for maximal reusability. Class interfaces are available for programmers who prefer C++.
  • Designed for portability to all major 32-bit and 64-bit platforms.
  • Simple three-address API for library users, but opaque enough that other representations can be used inside the library in future without affecting existing users.
  • Up-front or on-demand compilation of any function.
  • In-built support to re-compile functions with greater optimization, automatically redirecting previous callers to the new version.
  • Fallback interpreter for running code on platforms that don't have a native code generator yet. This reduces the need for programmers to write their own interpreters for such platforms.
  • Arithmetic, bitwise, conversion, and comparison operators for 8-bit, 16-bit, 32-bit, or 64-bit integer types; and 32-bit, 64-bit, or longer floating point types. Includes overflow detecting arithmetic for integer types.
  • Large set of mathematical and trigonometric operations (sqrt, sin, cos, min, abs, etc) for inlining floating-point library functions.
  • Simplified type layout and exception handling mechanisms, upon which a variety of different object models can be built.
I've translated the headers and examples, get them here:

Download: http://hostfile.org/libjitfb.7z

This also includes pre-compiled binaries, only win32 though.
The doc/ folder contains the generated html/chm documentation.

I hope somebody finds it useful. :)
oyster
Posts: 274
Joined: Oct 11, 2005 10:46

Post by oyster »

looks interesting.
I think it is far from a FB interpreter. oh, my lovely QB interpreter
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

oyster wrote:looks interesting.
I think it is far from a FB interpreter. oh, my lovely QB interpreter
Well, I hope someone makes a FB JIT VM out of it.. I'm sure the outcome would be very interesting =)
FB.NET maybe, or maybe a FreeBASIC web server, a la ASP.NET? :P
porfirio
Posts: 154
Joined: Mar 17, 2006 11:54
Location: Portugal

Post by porfirio »

Hmm...

Another day i made a FBJiT very easy

It was a simple executable when you run it with a bas file as parameter it will compile it as a dll link it to the exe and call the method FBMain

fbjit.bas

Code: Select all

Dim FBMain As Function(args As String) As Integer
Dim As String file=command(1)
If file="" Then
	?"Please specify a file"
	End 1
EndIf
Dim As String dllFile=file+".dll"
Dim text As String
Open Pipe "fbc -dll "+chr(34)+file+chr(34)+" -x "+chr(34)+dllFile+chr(34) For Input As #1
Line Input #1, text
If text<>"" Then
	?text
	Do While Not EOF(1)
	   Line Input #1, text
	   Print text
	Loop
	?"Compiler error"
	end
EndIf
Dim As Any ptr dll=DyLibLoad(dllFile)
If dll<>0 Then
	FBMain=DyLibSymbol(dll,"FBMain")
	If FBMain<>0 Then
		Dim As Integer res=FBMain("")'Havent implemented args yet
		DyLibFree(dll)
		If res<>0 Then
			?"Program ended with error: ";res
			End res
		EndIf
		End 0
	EndIf
	DyLibFree(dll)
Else
	?"Error loading the dll"
	End 2
EndIf
compile with fbc fbjit.bas

test.bas

Code: Select all

Function FBMain Alias "FBMain"(args As String) As Integer export
	?"Hello World"  
	?"Press any key to continue..."
	sleep
	Return 0
End Function
run with fbjit test.bas

Of course its pretty simple, allot more work should be done for it work better but, it works!
[/quote]
fsw
Posts: 260
Joined: May 27, 2005 6:02

Post by fsw »

@porfirio
It works great here.
Have you extended your little fbjit?
porfirio
Posts: 154
Joined: Mar 17, 2006 11:54
Location: Portugal

Post by porfirio »

fsw wrote:@porfirio
It works great here.
Have you extended your little fbjit?
No, but i should.... lol
Post Reply