LLVMExamples.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 18 Jul 2015 07:22:20 +0100
changeset 10 8998c2d4e53a
parent 9 540c1c31a33d
child 12 f98e97fd02ef
permissions -rw-r--r--
Added simple example of calling function defined within the same module.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     1
"{ Package: 'jv:libllvms' }"
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     2
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     3
"{ NameSpace: Smalltalk }"
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     4
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     5
Object subclass:#LLVMExamples
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     6
	instanceVariableNames:''
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     7
	classVariableNames:''
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     8
	poolDictionaries:''
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     9
	category:'LLVM-Core-Examples'
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    10
!
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    11
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    12
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    13
!LLVMExamples class methodsFor:'examples'!
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    14
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    15
example1_sum
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    16
    | module functionType function functionEntry asm jit externalFunction |
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    17
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    18
    module := LLVMModule newWithName: thisContext selector.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    19
    functionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    20
    function := module addFunctionNamed: 'sum' type: functionType.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    21
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    22
    functionEntry := function addBasicBlockNamed: 'entry'.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    23
    asm := LLVMBuilder new.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    24
    asm positionAtEnd: functionEntry.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    25
    asm ret: (asm add: (function parameterAt: 1) and: (function parameterAt: 2)).
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    26
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    27
    jit := LLVMExecutionEngine newForModule: module.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    28
    externalFunction := jit externalOfFunction: function.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    29
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    30
    ^ externalFunction callWith: 3 with: 4.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    31
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    32
    "
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    33
    LLVMExamples example1_sum
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    34
    "
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    35
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    36
    "Created: / 17-07-2015 / 11:47:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
9
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    37
!
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    38
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    39
example2_function_call
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    40
    | module calleeFunctionType calleeFunction calleFunctionEntry 
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    41
    callerFunctionType callerFunction callerFunctionEntry
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    42
    asm jit externalFunction |
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    43
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    44
    module := LLVMModule newWithName: thisContext selector.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    45
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    46
    calleeFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    47
    calleeFunction := module addFunctionNamed: 'sum' type: calleeFunctionType.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    48
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    49
    calleFunctionEntry := calleeFunction addBasicBlockNamed: 'entry'.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    50
    asm := LLVMBuilder new.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    51
    asm positionAtEnd: calleFunctionEntry.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    52
    asm ret: (asm add: (calleeFunction parameterAt: 1) and: (calleeFunction parameterAt: 2)).
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    53
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    54
    callerFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    55
    callerFunction := module addFunctionNamed: 'sum_caller' type: callerFunctionType.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    56
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    57
    callerFunctionEntry := callerFunction addBasicBlockNamed: 'entry'.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    58
    asm := LLVMBuilder new.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    59
    asm positionAtEnd: callerFunctionEntry.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    60
    asm ret: (asm call: calleeFunction with: (callerFunction parameterAt: 1) with: (callerFunction parameterAt: 2)).
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    61
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    62
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    63
    jit := LLVMExecutionEngine newForModule: module.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    64
    externalFunction := jit externalOfFunction: callerFunction.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    65
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    66
    ^ externalFunction callWith: 3 with: 4.
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    67
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    68
    "
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    69
    LLVMExamples example2_function_call
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    70
    "
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    71
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    72
    "Created: / 17-07-2015 / 12:45:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
    73
    "Modified: / 17-07-2015 / 17:18:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
10
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    74
!
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    75
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    76
example3_function_call
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    77
    | module printfFunctionType printfFunction  
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    78
    callerFunctionType callerFunction callerFunctionEntry
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    79
    asm jit externalFunction |
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    80
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    81
    module := LLVMModule newWithName: thisContext selector.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    82
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    83
    printfFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    84
    printfFunction := module addFunctionNamed: 'sum' type: printfFunctionType.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    85
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    86
    callerFunctionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    87
    callerFunction := module addFunctionNamed: 'sum_caller' type: callerFunctionType.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    88
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    89
    callerFunctionEntry := callerFunction addBasicBlockNamed: 'entry'.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    90
    asm := LLVMBuilder new.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    91
    asm positionAtEnd: callerFunctionEntry.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    92
    asm ret: (asm call: printfFunction with: (callerFunction parameterAt: 1) with: (callerFunction parameterAt: 2)).
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    93
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    94
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    95
    jit := LLVMExecutionEngine newForModule: module.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    96
    externalFunction := jit externalOfFunction: callerFunction.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    97
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    98
    ^ externalFunction callWith: 3 with: 4.
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    99
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   100
    "
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   101
    LLVMExamples example2_function_call
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   102
    "
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   103
8998c2d4e53a Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   104
    "Created: / 17-07-2015 / 17:21:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
8
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   105
! !
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   106
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   107
!LLVMExamples class methodsFor:'documentation'!
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   108
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   109
version_HG
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   110
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   111
    ^ '$Changeset: <not expanded> $'
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   112
! !
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   113