LLVMExecutionEngine.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 11 Jul 2015 16:56:36 +0100
changeset 5 3ac0c9381634
parent 3 ddfc3a7db3a8
child 7 a41b88b05f9a
permissions -rw-r--r--
Temporary commit

"{ Package: 'jv:libllvms' }"

"{ NameSpace: Smalltalk }"

LLVMDisposableObject subclass:#LLVMExecutionEngine
	instanceVariableNames:''
	classVariableNames:'Initialized'
	poolDictionaries:''
	category:'LLVM-Core'
!


!LLVMExecutionEngine class methodsFor:'initialization'!

initialize
    "Invoked at system start or when the class is dynamically loaded."

    Initialized := false

    "Modified: / 10-07-2015 / 14:43:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

initializeIfNotAlready
    Initialized ifFalse:[
        LLVM LinkInMCJIT.
        LLVM InitializeX86TargetInfo.
        LLVM InitializeX86Target.
        LLVM InitializeX86TargetMC.
        LLVM InitializeX86AsmPrinter.
        Initialized := true.
    ].

    "Created: / 10-07-2015 / 15:35:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 10-07-2015 / 22:34:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMExecutionEngine class methodsFor:'instance creation'!

newForModule: module
    | eePtr errmsgPtr |
    self assert: (module isKindOf: LLVMModule).
    self initializeIfNotAlready.
    eePtr := ExternalBytes new: ExternalBytes sizeofPointer.
    errmsgPtr := ExternalBytes new: ExternalBytes sizeofPointer.

    (LLVM CreateExecutionEngineForModule: eePtr _: module _: errmsgPtr) ~~ 0 ifTrue:[ 
        | errmsg err |

        errmsg := errmsgPtr pointerAt: 1.
        err := Error newException.
        err messageText: 'Cannot create execution engine: ', errmsg copyCStringFromHeap.
        LLVM DisposeMessage: errmsg.
        err signal.
    ] ifFalse:[ 
        | ee |

        ee := eePtr pointerAt: 1.
        ee := LLVMExecutionEngine basicNew setAddress: ee address.
        ee initialize.
        ^ ee
    ].

    "Created: / 10-07-2015 / 15:47:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 11-07-2015 / 06:38:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMExecutionEngine methodsFor:'accessing'!

addressOfFunction: anLLVMFunction
    ^ self addressOfFunctionNamed: anLLVMFunction name

    "Created: / 11-07-2015 / 11:26:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

addressOfFunctionNamed: name
    | addressAsInt |

    self assert: name isSingleByteString.
    addressAsInt := LLVM GetFunctionAddress: self  _: name.
    ^ ExternalAddress new setAddress: addressAsInt.

    "Created: / 10-07-2015 / 21:45:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMExecutionEngine methodsFor:'initialization & release'!

dispose
    ^ LLVM DisposeExecutionEngine: self

    "Modified: / 08-07-2015 / 22:39:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMExecutionEngine class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !


LLVMExecutionEngine initialize!