LLVMExecutionEngine.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 06 Jul 2016 22:40:59 +0100
changeset 71 ab03b0a6d037
parent 60 146527d8dd66
child 76 a1cd10a34b21
permissions -rw-r--r--
Implemented LLVMType>>sizeInBits/sizeInBytes for all data types ...i.e., also for structures, vectors and arrays.

"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
"{ Package: 'jv:llvm_s' }"

"{ NameSpace: Smalltalk }"

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

!LLVMExecutionEngine class methodsFor:'documentation'!

copyright
"
    Copyright (C) 2015-now Jan Vrany

    This code is not an open-source (yet). You may use this code
    for your own experiments and projects, given that:

    * all modification to the code will be sent to the
      original author for inclusion in future releases
    * this is not used in any commercial software

    This license is provisional and may (will) change in
    a future.
"
! !

!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) 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: / 14-08-2015 / 13:08:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

    (LLVM CreateMCJITCompilerForModule: eePtr _: module _: optionsAsBytes _: optionsAsBytes size _: errmsgPtr) 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.
        "/ If options specifies a custom MM, must unregister it from
        "/ finalization as the disposal of LLVMExecutionEngine disposes
        "/ the MM itself.
        options MCJMM notNil ifTrue:[ 
            options MCJMM unregisterForFinalization.
        ].
        ^ ee
    ].

    "Created: / 08-02-2016 / 16:49:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-04-2016 / 11:45:00 / 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!