gambas-source-code/main/lib/jit/gb.jit/.src/CCompilation.class

166 lines
3.3 KiB
Text
Raw Normal View History

' Gambas class file
Static Public All As New Collection
Static Private $sCompiler As String
'Static Private $sCompilerVersion As String
Static Private $aFlags As String[]
Public Name As String
Public PathSO As String
'Private $sCache As String
Private $sResult As String
Private $fTime As Float
Private $hProcess As Process
Static Public Sub Init()
Dim sFlag As String
Dim sCompiler As String
If $sCompiler Then Return
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"]
If Not sFlag Then sFlag = "-O3 -march=native -mtune=native"
$aFlags = Split(sFlag, " ")
' Exec [$sCompiler, "-V"] To $sCompilerVersion
' $sCompilerVersion = Trim($sCompilerVersion)
End
Public Sub _new(sName As String, sPathSO As String, fTime As Float)
Name = sName
PathSO = sPathSO
$fTime = fTime
All[sName] = Me
End
Public Sub Run(sInput As String, Optional sOption As String)
Dim aExec As String[]
'Dim sHash As String
'Dim iPos As Integer
'Dim I As Integer
' $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)
aExec.Add("-o")
'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
' 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
'If Jit.Debug And If $sCache Then Error "gb.jit: writing cache: "; $sCache
$hProcess = Null
' Shell "objdump -S " & Shell(sPathSO) To sResult
' Error sResult
End
Public Sub Wait() As String
If Jit.Debug Then Error "gb.jit: waiting for compilation of '"; Name; "'..."
If $hProcess Then
$hProcess.Wait
If Jit.Debug Then Error "gb.jit: compilation of '"; Name; "' is available"
Endif
If Not Exist(PathSO) Then
Error "gb.jit: error: unable to compile JIT code of '"; Name; "':\n"
Error $sResult
Return
Endif
Return PathSO
End
Public Sub Kill()
If $hProcess Then
If Jit.Debug Then Error "gb.jit: abort compilation of '"; Name; "'"
$hProcess.Kill
Endif
End