2019-03-11 16:34:50 +01:00
|
|
|
' Gambas class file
|
|
|
|
|
|
|
|
Static Public All As New Collection
|
|
|
|
|
|
|
|
Static Private $sCompiler As String
|
2020-05-18 13:22:34 +02:00
|
|
|
'Static Private $sCompilerVersion As String
|
2019-03-11 16:34:50 +01:00
|
|
|
Static Private $aFlags As String[]
|
|
|
|
|
|
|
|
Public Name As String
|
|
|
|
Public PathSO As String
|
|
|
|
|
2020-05-18 13:22:34 +02:00
|
|
|
'Private $sCache As String
|
2019-03-11 16:34:50 +01:00
|
|
|
Private $sResult As String
|
|
|
|
Private $fTime As Float
|
|
|
|
Private $hProcess As Process
|
|
|
|
|
2020-09-14 21:15:20 +02:00
|
|
|
Static Public Sub Init()
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
Dim sFlag As String
|
|
|
|
Dim sCompiler As String
|
|
|
|
|
2020-09-14 21:15:20 +02:00
|
|
|
If $sCompiler Then Return
|
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
sCompiler = Env["GB_JIT_CC"]
|
|
|
|
If Not sCompiler Then sCompiler = "gcc"
|
|
|
|
|
|
|
|
$sCompiler = System.Find(sCompiler)
|
|
|
|
If Not $sCompiler Then Error.Raise("Compiler not found")
|
|
|
|
|
|
|
|
sFlag = Env["GB_JIT_CFLAGS"]
|
2020-09-11 00:43:08 +02:00
|
|
|
If Not sFlag Then sFlag = "-O3 -march=native -mtune=native"
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
$aFlags = Split(sFlag, " ")
|
2020-05-18 13:22:34 +02:00
|
|
|
|
|
|
|
' Exec [$sCompiler, "-V"] To $sCompilerVersion
|
|
|
|
' $sCompilerVersion = Trim($sCompilerVersion)
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub _new(sName As String, sPathSO As String, fTime As Float)
|
|
|
|
|
|
|
|
Name = sName
|
|
|
|
PathSO = sPathSO
|
|
|
|
$fTime = fTime
|
|
|
|
All[sName] = Me
|
|
|
|
|
|
|
|
End
|
|
|
|
|
2020-05-18 13:22:34 +02:00
|
|
|
Public Sub Run(sInput As String, Optional sOption As String)
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
Dim aExec As String[]
|
2020-05-18 13:22:34 +02:00
|
|
|
'Dim sHash As String
|
|
|
|
'Dim iPos As Integer
|
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
'Dim I As Integer
|
|
|
|
|
2020-05-18 13:22:34 +02:00
|
|
|
' $sCache = File.Dir(File.Dir(Temp$())) &/ "gb.jit"
|
|
|
|
' Try Mkdir $sCache
|
|
|
|
|
|
|
|
' Exec ["md5sum", sInput] To sHash
|
|
|
|
' iPos = InStr(sHash, " ")
|
|
|
|
' If iPos Then
|
|
|
|
' $sCache &/= File.BaseName(PathSO) & "-" & Left(sHash, iPos - 1) & "." & File.Ext(PathSO)
|
|
|
|
' Else
|
|
|
|
' $sCache = ""
|
|
|
|
' Endif
|
|
|
|
|
|
|
|
'If Not $sCache Or If Not Exist($sCache) Then
|
|
|
|
|
|
|
|
aExec = [$sCompiler]
|
|
|
|
If sOption Then aExec.Insert(Split(sOption, " "))
|
|
|
|
aExec.Insert($aFlags)
|
|
|
|
aExec.Add(sInput)
|
2019-03-11 16:34:50 +01:00
|
|
|
aExec.Add("-o")
|
2020-05-18 13:22:34 +02:00
|
|
|
'If Not $sCache Then
|
|
|
|
aExec.Add(PathSO)
|
|
|
|
'Else
|
|
|
|
' aExec.Add($sCache)
|
|
|
|
'Endif
|
|
|
|
|
|
|
|
If Jit.Debug Then Error "gb.jit: run: "; aExec.Join(" ")
|
|
|
|
|
|
|
|
$hProcess = Exec aExec For Read As "Compiler"
|
|
|
|
|
|
|
|
' Else
|
|
|
|
'
|
|
|
|
' If Jit.Debug Then Error "gb.jit: using cache: "; $sCache
|
|
|
|
'
|
|
|
|
' Endif
|
|
|
|
|
|
|
|
' If $sCache Then
|
|
|
|
' Link $sCache To PathSO
|
|
|
|
' Endif
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
' For I = 0 To aExec.Max
|
|
|
|
' aExec[I] = Shell$(aExec[I])
|
|
|
|
' Next
|
|
|
|
|
|
|
|
'$hProcess.Ignore = True
|
|
|
|
'System._Breakpoint
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Compiler_Read()
|
|
|
|
|
|
|
|
Dim sData As String
|
|
|
|
|
|
|
|
sData = Read #$hProcess, -1024
|
|
|
|
$sResult &= sData
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Compiler_Error(({Error}) As String)
|
|
|
|
|
|
|
|
$sResult &= {Error}
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Compiler_Kill()
|
|
|
|
|
|
|
|
If $hProcess.Value = 0 Then
|
|
|
|
$fTime = Timer - $fTime
|
|
|
|
Jit._Time += $fTime
|
|
|
|
If Jit.Debug Then Error "gb.jit: compilation of '"; Name; "' done in "; Format($fTime, "0.000"); " s"
|
|
|
|
Else
|
|
|
|
If Jit.Debug Then Error "gb.jit: compilation of '"; Name; "' returns "; $hProcess.Value
|
|
|
|
Endif
|
|
|
|
|
2020-05-18 13:22:34 +02:00
|
|
|
'If Jit.Debug And If $sCache Then Error "gb.jit: writing cache: "; $sCache
|
|
|
|
|
2019-04-04 22:58:06 +02:00
|
|
|
$hProcess = Null
|
|
|
|
|
2019-03-11 16:34:50 +01:00
|
|
|
' Shell "objdump -S " & Shell(sPathSO) To sResult
|
|
|
|
' Error sResult
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Wait() As String
|
|
|
|
|
2020-01-26 07:09:36 +01:00
|
|
|
If Jit.Debug Then Error "gb.jit: waiting for compilation of '"; Name; "'..."
|
|
|
|
|
2019-04-04 22:58:06 +02:00
|
|
|
If $hProcess Then
|
|
|
|
$hProcess.Wait
|
|
|
|
If Jit.Debug Then Error "gb.jit: compilation of '"; Name; "' is available"
|
|
|
|
Endif
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
If Not Exist(PathSO) Then
|
2019-04-04 22:58:06 +02:00
|
|
|
Error "gb.jit: error: unable to compile JIT code of '"; Name; "':\n"
|
2019-03-11 16:34:50 +01:00
|
|
|
Error $sResult
|
|
|
|
Return
|
|
|
|
Endif
|
|
|
|
|
|
|
|
Return PathSO
|
|
|
|
|
|
|
|
End
|
|
|
|
|
|
|
|
Public Sub Kill()
|
2019-04-04 22:58:06 +02:00
|
|
|
|
|
|
|
If $hProcess Then
|
|
|
|
If Jit.Debug Then Error "gb.jit: abort compilation of '"; Name; "'"
|
|
|
|
$hProcess.Kill
|
|
|
|
Endif
|
2019-03-11 16:34:50 +01:00
|
|
|
|
|
|
|
End
|
2019-04-04 22:58:06 +02:00
|
|
|
|