LLVMExamples.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 01 Aug 2015 06:21:29 +0100
changeset 13 fa967c0e1827
parent 12 f98e97fd02ef
child 14 c7dea3fcc5a7
permissions -rw-r--r--
Renamed class categories to begin with LLVM-S ...to allow eventual port to Squeak/Pharo. Monticello would require that.

"{ Package: 'jv:llvm_s' }"

"{ NameSpace: Smalltalk }"

Object subclass:#LLVMExamples
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'LLVM-S-Core-Examples'
!


!LLVMExamples class methodsFor:'examples'!

example1_sum
    | module functionType function functionEntry asm jit externalFunction |

    module := LLVMModule newWithName: thisContext selector.
    functionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
    function := module addFunctionNamed: 'sum' type: functionType.

    functionEntry := function addBasicBlockNamed: 'entry'.
    asm := LLVMBuilder new.
    asm positionAtEnd: functionEntry.
    asm ret: (asm add: (function parameterAt: 1) and: (function parameterAt: 2)).

    jit := LLVMExecutionEngine newForModule: module.
    externalFunction := jit externalOfFunction: function.

    ^ externalFunction callWith: 3 with: 4.

    "
    LLVMExamples example1_sum
    "

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

example2_function_call
    | module calleeFunctionType calleeFunction calleFunctionEntry 
    callerFunctionType callerFunction callerFunctionEntry
    asm jit externalFunction |

    module := LLVMModule newWithName: thisContext selector.

    calleeFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
    calleeFunction := module addFunctionNamed: 'sum' type: calleeFunctionType.

    calleFunctionEntry := calleeFunction addBasicBlockNamed: 'entry'.
    asm := LLVMBuilder new.
    asm positionAtEnd: calleFunctionEntry.
    asm ret: (asm add: (calleeFunction parameterAt: 1) and: (calleeFunction parameterAt: 2)).

    callerFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
    callerFunction := module addFunctionNamed: 'sum_caller' type: callerFunctionType.

    callerFunctionEntry := callerFunction addBasicBlockNamed: 'entry'.
    asm := LLVMBuilder new.
    asm positionAtEnd: callerFunctionEntry.
    asm ret: (asm call: calleeFunction with: (callerFunction parameterAt: 1) with: (callerFunction parameterAt: 2)).


    jit := LLVMExecutionEngine newForModule: module.
    externalFunction := jit externalOfFunction: callerFunction.

    ^ externalFunction callWith: 3 with: 4.

    "
    LLVMExamples example2_function_call
    "

    "Created: / 17-07-2015 / 12:45:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-07-2015 / 17:18:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example3_function_call
    | module printfFunctionType printfFunction  
    callerFunctionType callerFunction callerFunctionEntry
    asm jit externalFunction |

    module := LLVMModule newWithName: thisContext selector.

    printfFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
    printfFunction := module addFunctionNamed: 'sum' type: printfFunctionType.

    callerFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
    callerFunction := module addFunctionNamed: 'sum_caller' type: callerFunctionType.

    callerFunctionEntry := callerFunction addBasicBlockNamed: 'entry'.
    asm := LLVMBuilder new.
    asm positionAtEnd: callerFunctionEntry.
    asm ret: (asm call: printfFunction with: (callerFunction parameterAt: 1) with: (callerFunction parameterAt: 2)).


    jit := LLVMExecutionEngine newForModule: module.
    externalFunction := jit externalOfFunction: callerFunction.

    ^ externalFunction callWith: 3 with: 4.

    "
    LLVMExamples example2_function_call
    "

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

!LLVMExamples class methodsFor:'documentation'!

version_HG

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