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-- |
8 | 1 |
"{ Package: 'jv:libllvms' }" |
2 |
||
3 |
"{ NameSpace: Smalltalk }" |
|
4 |
||
5 |
Object subclass:#LLVMExamples |
|
6 |
instanceVariableNames:'' |
|
7 |
classVariableNames:'' |
|
8 |
poolDictionaries:'' |
|
9 |
category:'LLVM-Core-Examples' |
|
10 |
! |
|
11 |
||
12 |
||
13 |
!LLVMExamples class methodsFor:'examples'! |
|
14 |
||
15 |
example1_sum |
|
16 |
| module functionType function functionEntry asm jit externalFunction | |
|
17 |
||
18 |
module := LLVMModule newWithName: thisContext selector. |
|
19 |
functionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr. |
|
20 |
function := module addFunctionNamed: 'sum' type: functionType. |
|
21 |
||
22 |
functionEntry := function addBasicBlockNamed: 'entry'. |
|
23 |
asm := LLVMBuilder new. |
|
24 |
asm positionAtEnd: functionEntry. |
|
25 |
asm ret: (asm add: (function parameterAt: 1) and: (function parameterAt: 2)). |
|
26 |
||
27 |
jit := LLVMExecutionEngine newForModule: module. |
|
28 |
externalFunction := jit externalOfFunction: function. |
|
29 |
||
30 |
^ externalFunction callWith: 3 with: 4. |
|
31 |
||
32 |
" |
|
33 |
LLVMExamples example1_sum |
|
34 |
" |
|
35 |
||
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 | 105 |
! ! |
106 |
||
107 |
!LLVMExamples class methodsFor:'documentation'! |
|
108 |
||
109 |
version_HG |
|
110 |
||
111 |
^ '$Changeset: <not expanded> $' |
|
112 |
! ! |
|
113 |