2018-05-25 21:03:48 +02:00
|
|
|
' Gambas module file
|
|
|
|
|
|
|
|
Export
|
2018-06-09 22:42:35 +02:00
|
|
|
Class __Jit
|
2018-05-25 21:03:48 +02:00
|
|
|
|
2018-07-03 00:52:19 +02:00
|
|
|
Property Read Time As Float
|
2019-03-11 16:34:50 +01:00
|
|
|
Property Debug As Boolean
|
2018-07-03 00:52:19 +02:00
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
Public _Time As Float
|
2018-06-25 19:17:44 +02:00
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
Private $bDebug As Integer
|
2018-06-25 19:17:44 +02:00
|
|
|
|
2020-01-26 07:09:36 +01:00
|
|
|
Private Sub GetArchiveName(sArch As String) As String
|
|
|
|
|
|
|
|
If Not sArch Then Return "gb"
|
|
|
|
Return Replace(sArch, "/", ".")
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
Public Sub _Compile(sArch As String) As Boolean
|
2018-05-25 21:03:48 +02:00
|
|
|
|
|
|
|
Dim sFile As String
|
|
|
|
Dim sDir As String
|
|
|
|
Dim sName As String
|
|
|
|
Dim sPath As String
|
|
|
|
Dim hFile As File
|
2018-05-26 16:50:00 +02:00
|
|
|
Dim sPathO As String
|
|
|
|
Dim sPathSO As String
|
2018-05-28 03:18:44 +02:00
|
|
|
Dim fTime As Float
|
2019-03-11 16:34:50 +01:00
|
|
|
Dim hComp As CCompilation
|
2018-05-28 03:18:44 +02:00
|
|
|
|
2018-07-03 00:52:19 +02:00
|
|
|
fTime = Timer
|
|
|
|
|
2018-07-05 07:05:53 +02:00
|
|
|
Try $bDebug = CInt(Env["GB_JIT_DEBUG"])
|
2018-06-01 03:50:42 +02:00
|
|
|
|
2020-01-26 07:09:36 +01:00
|
|
|
If $bDebug Then Error "gb.jit: translating "; If(sArch, "'" & sArch & "'", "project"); "..."
|
2018-05-25 21:03:48 +02:00
|
|
|
|
2020-01-26 07:09:36 +01:00
|
|
|
sName = GetArchiveName(sArch)
|
2018-06-09 22:42:35 +02:00
|
|
|
|
2018-05-25 21:03:48 +02:00
|
|
|
sDir = File.Dir(Temp$()) &/ "jit"
|
|
|
|
Try Mkdir sDir
|
|
|
|
|
2018-06-25 19:17:44 +02:00
|
|
|
sPath = sDir &/ "jit.h"
|
|
|
|
|
|
|
|
If Not Exist(sPath) Then
|
|
|
|
|
2018-07-05 07:05:53 +02:00
|
|
|
If $bDebug Then Error "gb.jit: generating header"
|
2018-06-25 19:17:44 +02:00
|
|
|
|
|
|
|
hFile = Open sPath For Output Create
|
|
|
|
|
|
|
|
Print #hFile, "#define NO_CONFIG_H"
|
2018-07-02 19:37:37 +02:00
|
|
|
|
2018-06-25 19:17:44 +02:00
|
|
|
Print #hFile, File.Load("gambas.h");
|
|
|
|
Print #hFile, File.Load("jit.h");
|
|
|
|
Print #hFile, File.Load("gb.jit.h");
|
2018-07-02 19:37:37 +02:00
|
|
|
|
2018-06-25 19:17:44 +02:00
|
|
|
Print #hFile, "GB_INTERFACE * GB_PTR;"
|
|
|
|
Print #hFile, "#define GB (*GB_PTR)"
|
|
|
|
Print #hFile, "JIT_INTERFACE * JIT_PTR;"
|
|
|
|
Print #hFile, "#define JIT (*JIT_PTR)"
|
|
|
|
|
2018-07-02 19:37:37 +02:00
|
|
|
Print #hFile, File.Load("gb_error_common.h");
|
|
|
|
|
2018-06-25 19:17:44 +02:00
|
|
|
Close #hFile
|
|
|
|
|
2018-07-05 07:05:53 +02:00
|
|
|
If $bDebug Then
|
|
|
|
Try Kill "/tmp/jit.h"
|
|
|
|
Copy sDir &/ "jit.h" To "/tmp/jit.h"
|
|
|
|
Endif
|
|
|
|
|
2018-06-25 19:17:44 +02:00
|
|
|
' '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
|
|
|
|
'
|
2018-07-05 07:05:53 +02:00
|
|
|
' If $bDebug Then
|
2018-06-25 19:17:44 +02:00
|
|
|
' 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"
|
|
|
|
|
2018-07-05 07:05:53 +02:00
|
|
|
If $bDebug Then Error "gb.jit: generating "; sPath
|
2018-05-26 16:50:00 +02:00
|
|
|
|
2018-05-25 21:03:48 +02:00
|
|
|
hFile = Open sPath For Output Create
|
|
|
|
|
2018-06-25 19:17:44 +02:00
|
|
|
Print #hFile, "#include \"jit.h\""
|
|
|
|
Print #hFile
|
2018-06-14 16:42:47 +02:00
|
|
|
|
2018-06-09 22:42:35 +02:00
|
|
|
If sArch Then
|
2020-01-26 07:09:36 +01:00
|
|
|
sDir = "." &/ sName
|
2018-06-09 22:42:35 +02:00
|
|
|
Else
|
|
|
|
sDir = "..."
|
|
|
|
Endif
|
|
|
|
|
|
|
|
For Each sFile In Dir(sDir &/ ".gambas")
|
2018-08-28 17:05:11 +02:00
|
|
|
_ClassStat.Stat(sDir, sFile)
|
|
|
|
If Not _ClassStat.HasFast Then Continue
|
|
|
|
sFile = _ClassStat.Name
|
2018-07-05 07:05:53 +02:00
|
|
|
If $bDebug Then Error "gb.jit: translating class "; sFile
|
2018-06-09 22:42:35 +02:00
|
|
|
Print #hFile, __Jit.Translate(sFile, sArch)
|
2018-05-25 21:03:48 +02:00
|
|
|
Next
|
|
|
|
|
|
|
|
Close #hFile
|
|
|
|
|
2018-07-05 07:05:53 +02:00
|
|
|
If $bDebug Then
|
2018-06-09 22:42:35 +02:00
|
|
|
Try Kill "/tmp/" & File.Name(sPath)
|
|
|
|
Copy sPath To "/tmp/" & File.Name(sPath)
|
|
|
|
Endif
|
|
|
|
|
2018-05-26 16:50:00 +02:00
|
|
|
sPathO = File.SetExt(sPath, "o")
|
|
|
|
sPathSO = File.SetExt(sPath, "so")
|
|
|
|
|
2018-07-05 07:05:53 +02:00
|
|
|
If $bDebug Then Error "gb.jit: compiling to "; sPathO
|
2018-06-01 03:50:42 +02:00
|
|
|
|
2018-05-25 21:03:48 +02:00
|
|
|
'gcc -c -fPIC -o foo.o foo.c
|
2018-05-26 16:50:00 +02:00
|
|
|
'Exec [$sCompiler, "-c", "-fPIC", "-o", File.SetExt(sPath, "o"), sPath] To sResult
|
2018-06-25 19:17:44 +02:00
|
|
|
'Shell $sCompiler & " -c -fPIC " & sFlag & " -o " & Shell(sPathO) & " " & Shell(sPath) & " 2>&1" To sResult
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
hComp = New CCompilation(sName, sPathSO, fTime)
|
|
|
|
|
|
|
|
hComp.Run(sPath, sPathSO, "-w -fPIC -shared -lm")
|
2018-05-28 03:18:44 +02:00
|
|
|
|
2018-07-05 07:05:53 +02:00
|
|
|
' If $bDebug Then Error "gb.jit: linking to "; sPathSO
|
2018-06-25 19:17:44 +02:00
|
|
|
'
|
|
|
|
' '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
|
2018-05-25 21:03:48 +02:00
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub _Wait(sArch As String) As String
|
|
|
|
|
|
|
|
Dim hComp As CCompilation
|
|
|
|
Dim sResult As String
|
2018-07-03 00:52:19 +02:00
|
|
|
|
2020-01-26 07:09:36 +01:00
|
|
|
hComp = CCompilation.All[GetArchiveName(sArch)]
|
2019-03-11 16:34:50 +01:00
|
|
|
If Not hComp Then Return
|
2018-05-28 03:18:44 +02:00
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
sResult = hComp.Wait()
|
|
|
|
Return sResult
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub _Abort()
|
|
|
|
|
|
|
|
Dim hComp As CCompilation
|
2018-05-29 03:43:23 +02:00
|
|
|
|
2020-03-10 00:41:05 +01:00
|
|
|
If CCompilation.All.Count = 0 Then Return
|
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
For Each hComp In CCompilation.All
|
|
|
|
hComp.Kill
|
|
|
|
Next
|
|
|
|
|
|
|
|
CCompilation.All.Clear
|
2018-05-25 21:03:48 +02:00
|
|
|
|
|
|
|
End
|
2018-07-03 00:52:19 +02:00
|
|
|
|
|
|
|
Private Function Time_Read() As Float
|
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
Return _Time
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Private Function Debug_Read() As Boolean
|
|
|
|
|
|
|
|
Return $bDebug
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Private Sub Debug_Write(Value As Boolean)
|
|
|
|
|
|
|
|
$bDebug = Value
|
2018-07-03 00:52:19 +02:00
|
|
|
|
|
|
|
End
|