gambas-source-code/main/lib/jit/gb.jit/.src/Jit.module
gambas 3dd1fd493e Fix the JIT compiler: '_init' method, Lock instruction, Object datatype, and DO / REPEAT loops at the beginning of a function now work correctly.
[INTERPRETER]
* BUG: JIT compiler does not run static initializers anymore when loading a class. Otherwise bytecode can be modified before being compiled, leading to a JIT compiler crash.

[COMPILER]
* BUG: Fix LOCK subroutine meta-information.

[GB.JIT]
* NEW: Don't display abortion debugging message if there is no compilation to abort.
* BUG: Object variables are correctly managed now.
* BUG: DO...LOOP and REPEAT...UNTIL at the beginning of the function now is correctly compiled in all cases.
2019-04-04 22:58:06 +02:00

183 lines
4 KiB
Text

' Gambas module file
Export
Class __Jit
Property Read Time As Float
Property Debug As Boolean
Public _Time As Float
Private $bDebug As Integer
Public Sub _Compile(sArch As String) As Boolean
Dim sFile As String
Dim sDir As String
Dim sName As String
Dim sPath As String
Dim hFile As File
Dim sPathO As String
Dim sPathSO As String
Dim fTime As Float
Dim hComp As CCompilation
fTime = Timer
Try $bDebug = CInt(Env["GB_JIT_DEBUG"])
If $bDebug Then Error "gb.jit: translating "; If(sArch, sArch, "project")
sName = sArch
If Not sName Then sName = "gb"
sDir = File.Dir(Temp$()) &/ "jit"
Try Mkdir sDir
sPath = sDir &/ "jit.h"
If Not Exist(sPath) Then
If $bDebug Then Error "gb.jit: generating header"
hFile = Open sPath For Output Create
Print #hFile, "#define NO_CONFIG_H"
Print #hFile, File.Load("gambas.h");
Print #hFile, File.Load("jit.h");
Print #hFile, File.Load("gb.jit.h");
Print #hFile, "GB_INTERFACE * GB_PTR;"
Print #hFile, "#define GB (*GB_PTR)"
Print #hFile, "JIT_INTERFACE * JIT_PTR;"
Print #hFile, "#define JIT (*JIT_PTR)"
Print #hFile, File.Load("gb_error_common.h");
Close #hFile
If $bDebug Then
Try Kill "/tmp/jit.h"
Copy sDir &/ "jit.h" To "/tmp/jit.h"
Endif
' 'Shell $sCompiler & " -fPIC " & Shell(sPath) To sResult
' sResult = RunCompiler(sPath,, "-fPIC")
' If Not Exist(sPath & ".gch") Then
' Error "gb.jit: error: unable to generate precompiled header"
' Error sResult
' Return
' Endif
'
' If $bDebug Then
' Try Kill "/tmp/" & File.Name(sPath)
' Copy sPath To "/tmp/" & File.Name(sPath)
' Try Kill "/tmp/" & File.Name(sPath & ".gch")
' Copy sPath & ".gch" To "/tmp/" & File.Name(sPath & ".gch")
' Endif
Endif
sPath = sDir &/ sName & ".c"
If $bDebug Then Error "gb.jit: generating "; sPath
hFile = Open sPath For Output Create
Print #hFile, "#include \"jit.h\""
Print #hFile
If sArch Then
sDir = "." &/ sArch
Else
sDir = "..."
Endif
For Each sFile In Dir(sDir &/ ".gambas")
_ClassStat.Stat(sDir, sFile)
If Not _ClassStat.HasFast Then Continue
sFile = _ClassStat.Name
If $bDebug Then Error "gb.jit: translating class "; sFile
Print #hFile, __Jit.Translate(sFile, sArch)
Next
Close #hFile
If $bDebug Then
Try Kill "/tmp/" & File.Name(sPath)
Copy sPath To "/tmp/" & File.Name(sPath)
Endif
sPathO = File.SetExt(sPath, "o")
sPathSO = File.SetExt(sPath, "so")
If $bDebug Then Error "gb.jit: compiling to "; sPathO
'gcc -c -fPIC -o foo.o foo.c
'Exec [$sCompiler, "-c", "-fPIC", "-o", File.SetExt(sPath, "o"), sPath] To sResult
'Shell $sCompiler & " -c -fPIC " & sFlag & " -o " & Shell(sPathO) & " " & Shell(sPath) & " 2>&1" To sResult
hComp = New CCompilation(sName, sPathSO, fTime)
hComp.Run(sPath, sPathSO, "-w -fPIC -shared -lm")
' If $bDebug Then Error "gb.jit: linking to "; sPathSO
'
' 'gcc -shared -o libfoo.so foo.o
' 'Exec [$sCompiler, "-shared", "-o", File.SetExt(sPath, "so"), File.SetExt(sPath, "o")] To sResult
' 'Shell $sCompiler & " -shared " & sFlag & " -lm -o " & Shell(sPathSO) & " " & Shell(sPathO) & " 2>&1" To sResult
' RunCompiler(sPathO, sPathSO, "-shared -lm")
' If Not Exist(sPathSO) Then
' Error "gb.jit: warning: unable to link JIT code:"
' Error sResult
' Return
' Endif
End
Public Sub _Wait(sArch As String) As String
Dim hComp As CCompilation
Dim sResult As String
If Not sArch Then sArch = "gb"
hComp = CCompilation.All[sArch]
If Not hComp Then Return
sResult = hComp.Wait()
Return sResult
End
Public Sub _Abort()
Dim hComp As CCompilation
For Each hComp In CCompilation.All
hComp.Kill
Next
CCompilation.All.Clear
End
Private Function Time_Read() As Float
Return _Time
End
Private Function Debug_Read() As Boolean
Return $bDebug
End
Private Sub Debug_Write(Value As Boolean)
$bDebug = Value
End