author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Fri, 07 Aug 2015 14:10:48 +0100 | |
changeset 21 | 64c5f01be2b3 |
parent 19 | 706be0fcef22 |
child 22 | 789a35bd30ac |
permissions | -rw-r--r-- |
14
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
1 |
" |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
2 |
Copyright (C) 2015-now Jan Vrany |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
3 |
|
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
4 |
This code is not an open-source (yet). You may use this code |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
5 |
for your own experiments and projects, given that: |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
6 |
|
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
7 |
* all modification to the code will be sent to the |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
8 |
original author for inclusion in future releases |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
9 |
* this is not used in any commercial software |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
10 |
|
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
11 |
This license is provisional and may (will) change in |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
12 |
a future. |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
13 |
" |
12
f98e97fd02ef
Package renamed from jv:libllvms to jv:llvm_s
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
10
diff
changeset
|
14 |
"{ Package: 'jv:llvm_s' }" |
8 | 15 |
|
16 |
"{ NameSpace: Smalltalk }" |
|
17 |
||
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
18 |
TestCase subclass:#LLVMExamples |
8 | 19 |
instanceVariableNames:'' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
13
fa967c0e1827
Renamed class categories to begin with LLVM-S
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
12
diff
changeset
|
22 |
category:'LLVM-S-Core-Examples' |
8 | 23 |
! |
24 |
||
14
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
25 |
!LLVMExamples class methodsFor:'documentation'! |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
26 |
|
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
27 |
copyright |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
28 |
" |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
29 |
Copyright (C) 2015-now Jan Vrany |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
30 |
|
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
31 |
This code is not an open-source (yet). You may use this code |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
32 |
for your own experiments and projects, given that: |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
33 |
|
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
34 |
* all modification to the code will be sent to the |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
35 |
original author for inclusion in future releases |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
36 |
* this is not used in any commercial software |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
37 |
|
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
38 |
This license is provisional and may (will) change in |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
39 |
a future. |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
40 |
" |
c7dea3fcc5a7
Added short README, license.txt and updated copyright information
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
13
diff
changeset
|
41 |
! ! |
8 | 42 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
43 |
!LLVMExamples class methodsFor:'accessing'! |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
44 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
45 |
isTestSelector:aSelector |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
46 |
^ (super isTestSelector:aSelector) or:[ aSelector startsWith: 'example' ] |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
47 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
48 |
"Created: / 03-08-2015 / 09:25:22 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
49 |
! ! |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
50 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
51 |
!LLVMExamples methodsFor:'examples'! |
8 | 52 |
|
53 |
example1_sum |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
54 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
55 |
Creates a simple function taking two arguments (as intptr_t) and returning |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
56 |
their sum (as intptr_t). |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
57 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
58 |
|
8 | 59 |
| module functionType function functionEntry asm jit externalFunction | |
60 |
||
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
61 |
"/ 1) create a module to which the function would belong. A module is |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
62 |
"/ a set of functions and globals that are compiled at once by the MCJIT. Once |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
63 |
"/ module is compiled, no more methods or clobals can be added. |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
64 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
65 |
module := LLVMModule newWithName: testSelector. |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
66 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
67 |
"/ 2) Define a function within the module with type (intptr_t, intptr_t) -> intptr_t |
8 | 68 |
functionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr. |
69 |
function := module addFunctionNamed: 'sum' type: functionType. |
|
70 |
||
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
71 |
"/ 3) Generate function code. LLVM IR does not work with |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
72 |
"/ labels / jumps to labels but rather the user is responsible |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
73 |
"/ for creating basic blocks and adding them to the function. |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
74 |
"/ Hence, create a basic block named 'entry' |
8 | 75 |
functionEntry := function addBasicBlockNamed: 'entry'. |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
76 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
77 |
"/ 4) To emit LLVM IR, create an IR builder and position it |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
78 |
"/ to the end of just created basic block. |
8 | 79 |
asm := LLVMBuilder new. |
80 |
asm positionAtEnd: functionEntry. |
|
81 |
asm ret: (asm add: (function parameterAt: 1) and: (function parameterAt: 2)). |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
82 |
"/ Now, the module should look like |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
83 |
self assert: (module dumpString = |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
84 |
'; ModuleID = ''example1_sum'' |
8 | 85 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
86 |
define i64 @sum(i64, i64) { |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
87 |
entry: |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
88 |
%2 = add i64 %0, %1 |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
89 |
ret i64 %2 |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
90 |
} |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
91 |
'). |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
92 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
93 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
94 |
"/ 5) To compile a function (strictly speaking, whole module) at runtime, |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
95 |
"/ create a jit object (called ExecutionEngine in LLVM) |
8 | 96 |
jit := LLVMExecutionEngine newForModule: module. |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
97 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
98 |
"/ 6) Finally, obtain a reference to the function. This cause |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
99 |
"/ the module to be closed and compiled to machine code. |
8 | 100 |
externalFunction := jit externalOfFunction: function. |
101 |
||
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
102 |
self assert: (externalFunction callWith: 3 with: 4) == 7 |
8 | 103 |
|
104 |
" |
|
105 |
LLVMExamples example1_sum |
|
106 |
" |
|
107 |
||
108 |
"Created: / 17-07-2015 / 11:47:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
109 |
"Modified: / 03-08-2015 / 10:29:10 / 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
|
110 |
! |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
111 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
112 |
example2_function_call |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
113 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
114 |
Creates a module with two functions, @sum and @sum_caller. The latter |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
115 |
calls the former. |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
116 |
" |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
117 |
| module calleeFunctionType calleeFunction calleFunctionEntry |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
118 |
callerFunctionType callerFunction callerFunctionEntry |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
119 |
asm jit externalFunction | |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
120 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
121 |
module := LLVMModule newWithName: testSelector. |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
122 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
123 |
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
|
124 |
calleeFunction := module addFunctionNamed: 'sum' type: calleeFunctionType. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
125 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
126 |
calleFunctionEntry := calleeFunction addBasicBlockNamed: 'entry'. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
127 |
asm := LLVMBuilder new. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
128 |
asm positionAtEnd: calleFunctionEntry. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
129 |
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
|
130 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
131 |
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
|
132 |
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
|
133 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
134 |
callerFunctionEntry := callerFunction addBasicBlockNamed: 'entry'. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
135 |
asm := LLVMBuilder new. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
136 |
asm positionAtEnd: callerFunctionEntry. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
137 |
asm ret: (asm call: calleeFunction with: (callerFunction parameterAt: 1) with: (callerFunction parameterAt: 2)). |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
138 |
self assert: (module dumpString = |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
139 |
'; ModuleID = ''example2_function_call'' |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
140 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
141 |
define i64 @sum(i64, i64) { |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
142 |
entry: |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
143 |
%2 = add i64 %0, %1 |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
144 |
ret i64 %2 |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
145 |
} |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
146 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
147 |
define i64 @sum_caller(i64, i64) { |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
148 |
entry: |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
149 |
%2 = call i64 @sum(i64 %0, i64 %1) |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
150 |
ret i64 %2 |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
151 |
} |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
152 |
'). |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
153 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
154 |
jit := LLVMExecutionEngine newForModule: module. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
155 |
externalFunction := jit externalOfFunction: callerFunction. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
156 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
157 |
self assert: (externalFunction callWith: 3 with: 4) == 7. |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
158 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
159 |
" |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
160 |
LLVMExamples example2_function_call |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
161 |
" |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
162 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
163 |
"Created: / 17-07-2015 / 12:45:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
164 |
"Modified: / 03-08-2015 / 10:29:00 / 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
|
165 |
! |
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
166 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
167 |
example3_hello_world |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
168 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
169 |
Creates a function @main() which calls @printf() to print |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
170 |
a famous 'Hello World!!' message on stdout |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
171 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
172 |
|
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
173 |
| module printfFunctionType printfFunction |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
174 |
helloWorldString |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
175 |
mainFunctionType mainFunction mainFunctionEntry |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
176 |
asm jit externalFunction | |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
177 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
178 |
module := LLVMModule newWithName: testSelector. |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
179 |
|
19
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
180 |
printfFunctionType := LLVMType function: { LLVMType char pointer } varargs: true returning: LLVMType int32. |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
181 |
printfFunction := module addFunctionNamed: 'printf' type: printfFunctionType. |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
182 |
|
18
6e80a93f4251
Oops, fixed #example3_hello_world
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16
diff
changeset
|
183 |
helloWorldString := module addGlobalNamed: '.str' value: (LLVMConstant string: 'Hello World!!' , Character lf). |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
184 |
|
18
6e80a93f4251
Oops, fixed #example3_hello_world
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16
diff
changeset
|
185 |
mainFunctionType := LLVMType function: #() returning: LLVMType int32. |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
186 |
mainFunction := module addFunctionNamed: 'main' type: mainFunctionType. |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
187 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
188 |
mainFunctionEntry := mainFunction addBasicBlockNamed: 'entry'. |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
189 |
asm := LLVMBuilder new. |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
190 |
asm positionAtEnd: mainFunctionEntry. |
19
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
191 |
asm call: printfFunction with: (asm gep: helloWorldString at: #(0 0)). |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
192 |
asm ret: (LLVMConstant sint32: 0). |
19
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
193 |
" |
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
194 |
module writeBitcodeToFile: '/tmp/main.bc' |
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
195 |
" |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
196 |
jit := LLVMExecutionEngine newForModule: module. |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
197 |
externalFunction := jit externalOfFunction: mainFunction. |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
198 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
199 |
externalFunction call. |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
200 |
Stdout flush |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
201 |
|
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
202 |
" |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
203 |
LLVMExamples example3_hello_world |
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
204 |
" |
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
205 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
206 |
"Created: / 03-08-2015 / 10:28:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
19
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
207 |
"Modified: / 05-08-2015 / 20:59:38 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
208 |
"Modified (comment): / 05-08-2015 / 22:10:48 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
8 | 209 |
! ! |
210 |