gambas-source-code/main/lib/jit/gb.jit/.src/_Jit.module
gambas 4e50dec11d Work continues on new JIT system. Translation is now done at runtime.
[INTERPRETER]
* OPT: String routines are now compiled with -O3.
* NEW: Don't display JIT debugging message unless GB_JIT_DEBUG is set to something different from zero.
* NEW: String whose length is greater than 256 now have a growth step of 256 bytes instead of 16.

[GB.JIT]
* NEW: Do many global optimizations as now the class metadata is fully available.
* NEW: Support for optional argument. Still buggy at the moment.
2018-06-09 22:42:35 +02:00

123 lines
2.9 KiB
Text

' Gambas module file
Export
Class __Jit
Private $sCompiler As String
Private Sub Init()
$sCompiler = System.Find("gcc")
End
Public Sub Compile(sArch As String) As String
Dim sFile As String
Dim sDir As String
Dim sName As String
Dim sPath As String
Dim hFile As File
Dim sResult As String
Dim sPathO As String
Dim sPathSO As String
Dim fTime As Float
Dim sFlag As String
Dim bDebug As Boolean
Try bDebug = CInt(Env["GB_JIT_DEBUG"])
If bDebug Then
fTime = Timer
Error "gb.jit: translating "; If(sArch, sArch, "project")
Endif
sName = sArch
If Not sName Then sName = "gb"
sDir = File.Dir(Temp$()) &/ "jit"
Try Mkdir sDir
sPath = sDir &/ sName & ".c"
If bDebug Then Error "gb.jit: generating "; sPath
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)"
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 Not $sCompiler Then Init
' Error "gb.jit: preprocessing:"
'
' Shell $sCompiler & " -E " & Shell(sPath) & " 2>&1" To sResult
' Error sResult
If bDebug Then Error "gb.jit: compiling to "; sPathO
sFlag = Env["GB_JIT_CFLAGS"]
If Not sFlag Then sFlag = "-O3"
'Shell $sCompiler & " -E " & Shell(sPath) & " 2>&1" To sResult
'Error sResult
'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
If Not Exist(sPathO) Then
Error "gb.jit: error: unable to compile JIT code:"
Error sResult
Return
Endif
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
If Not Exist(sPathSO) Then
Error "gb.jit: warning: unable to link JIT code:"
Error sResult
Return
Endif
If bDebug Then Error "gb.jit: done in "; Format(Timer - fTime, "0.000"); "s"
' Shell "objdump -S " & Shell(sPathSO) To sResult
' Error sResult
Return sPathSO
End