author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Fri, 17 Jul 2015 17:20:04 +0100 | |
changeset 9 | 540c1c31a33d |
parent 8 | 890eb7591eca |
child 10 | 8998c2d4e53a |
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>" |
8 | 74 |
! ! |
75 |
||
76 |
!LLVMExamples class methodsFor:'documentation'! |
|
77 |
||
78 |
version_HG |
|
79 |
||
80 |
^ '$Changeset: <not expanded> $' |
|
81 |
! ! |
|
82 |