' Gambas module file

Export
Class __Jit

Property Read Time As Float
Property Debug As Boolean

Public _Time As Float

Private $bDebug As Integer

Private Sub GetArchiveName(sArch As String) As String
  
  If Not sArch Then Return "gb"
  Return Replace(sArch, "/", ".")
  
End

Public Sub _Search() As Boolean
  
  Try CCompilation.Init()
  If Error Then Return True
  
End

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 = GetArchiveName(sArch)
  
  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 = "." &/ sName
  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, "-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
  
  hComp = CCompilation.All[GetArchiveName(sArch)]
  If Not hComp Then Return
  
  sResult = hComp.Wait()
  Return sResult

End

Public Sub _Abort()

  Dim hComp As CCompilation
  
  If CCompilation.All.Count = 0 Then Return
  
  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