"{ Package: 'jv:llvm_s' }"
"{ NameSpace: Smalltalk }"
LLVMDisposableObject subclass:#LLVMExecutionEngine
instanceVariableNames:'module'
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 setModule: module.
ee initialize.
^ ee
].
"Created: / 10-07-2015 / 15:47:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
"Modified: / 17-07-2015 / 12:31:02 / 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>"
!
externalOfFunction: anLLVMFunction
"Return reference to an LLVMFunction as an instance
of ExternalFunction class. The class may be then
called using #callWith:... method."
^ self externalOfFunctionNamed: anLLVMFunction name
"Created: / 17-07-2015 / 11:36:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
externalOfFunctionNamed: name
"Return reference to a function with givrn name as an instance
of ExternalFunction class. The class may be then
called using #callWith:... method."
| code |
code := self addressOfFunctionNamed: name.
^ ExternalFunction new
code: code;
setModuleHandle: self.
"Created: / 17-07-2015 / 11:38:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !
!LLVMExecutionEngine methodsFor:'initialization & release'!
dispose
"Disposes the object. After that it cannot be used anymore"
"/ Must unvalidate module because by default, MCJIT destructor
"/ calls delete on its underalying module.
module unregisterForFinalization.
Transcript showCR: '>>Module: ', module printString.
module setAddress: 0.
LLVM DisposeExecutionEngine: self.
self setAddress: 0.
"Modified: / 17-07-2015 / 12:34:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!
setModule: anLLVMModule
module := anLLVMModule.
"Created: / 17-07-2015 / 12:08:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !
!LLVMExecutionEngine class methodsFor:'documentation'!
version_HG
^ '$Changeset: <not expanded> $'
! !
LLVMExecutionEngine initialize!