author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 17 Aug 2015 09:08:47 +0100 | |
changeset 30 | c789c1390911 |
parent 28 | 97013ae2abae |
child 33 | feabf14b6c1d |
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:'' |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
21 |
poolDictionaries:'LLVMIntPredicate' |
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 |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
59 |
| module functionType function asm jit externalFunction | |
8 | 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 |
||
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
71 |
"/ 2) To emit LLVM IR, get an IR builder for the function. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
72 |
"/ The LLVMFunction>>builder returns a builder on |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
73 |
"/ function's entry point (basic block, strictly speaking) |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
74 |
asm := function builder. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
75 |
asm ret: (asm add:(function parameterAt:1) _:(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
|
76 |
"/ 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
|
77 |
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
|
78 |
'; ModuleID = ''example1_sum'' |
8 | 79 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
80 |
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
|
81 |
entry: |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
82 |
%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
|
83 |
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
|
84 |
} |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
85 |
'). |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
86 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
87 |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
88 |
"/ 3) To compile a function (strictly speaking, whole module) at runtime, |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
89 |
"/ create a jit object (called ExecutionEngine in LLVM) |
8 | 90 |
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
|
91 |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
92 |
"/ 4) Finally, obtain a reference to the function. This cause |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
93 |
"/ the module to be closed and compiled to machine code. |
8 | 94 |
externalFunction := jit externalOfFunction: function. |
95 |
||
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
96 |
self assert: (externalFunction callWith: 3 with: 4) == 7 |
8 | 97 |
|
98 |
" |
|
99 |
LLVMExamples example1_sum |
|
100 |
" |
|
101 |
||
102 |
"Created: / 17-07-2015 / 11:47:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
103 |
"Modified: / 08-08-2015 / 04:33:22 / 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
|
104 |
! |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
105 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
106 |
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
|
107 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
" |
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
111 |
| module calleeFunctionType calleeFunction callerFunctionType callerFunction asm jit externalFunction | |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
112 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
113 |
module := LLVMModule newWithName: testSelector. |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
114 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
115 |
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
|
116 |
calleeFunction := module addFunctionNamed: 'sum' type: calleeFunctionType. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
117 |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
118 |
asm := calleeFunction builder. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
119 |
asm ret: (asm add:(calleeFunction parameterAt:1) _:(calleeFunction parameterAt:2)). |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
120 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
121 |
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
|
122 |
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
|
123 |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
124 |
asm := callerFunction builder. |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
125 |
asm ret: (asm call: calleeFunction _: { (callerFunction parameterAt: 1) . (callerFunction parameterAt: 2) }). |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
126 |
self assert: (module dumpString = |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
127 |
'; ModuleID = ''example2_function_call'' |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
128 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
129 |
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
|
130 |
entry: |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
131 |
%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
|
132 |
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
|
133 |
} |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
134 |
|
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
135 |
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
|
136 |
entry: |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
137 |
%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
|
138 |
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
|
139 |
} |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
140 |
'). |
9
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
141 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
142 |
jit := LLVMExecutionEngine newForModule: module. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
143 |
externalFunction := jit externalOfFunction: callerFunction. |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
144 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
145 |
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
|
146 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
147 |
" |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
148 |
LLVMExamples example2_function_call |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
149 |
" |
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
150 |
|
540c1c31a33d
Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
8
diff
changeset
|
151 |
"Created: / 17-07-2015 / 12:45:08 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
152 |
"Modified: / 10-08-2015 / 18:58:10 / 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
|
153 |
! |
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
154 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
155 |
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
|
156 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
157 |
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
|
158 |
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
|
159 |
" |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
160 |
|
10
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
161 |
| 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
|
162 |
helloWorldString |
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
163 |
mainFunctionType mainFunction |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
164 |
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
|
165 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
166 |
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
|
167 |
|
19
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
168 |
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
|
169 |
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
|
170 |
|
18
6e80a93f4251
Oops, fixed #example3_hello_world
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16
diff
changeset
|
171 |
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
|
172 |
|
18
6e80a93f4251
Oops, fixed #example3_hello_world
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
16
diff
changeset
|
173 |
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
|
174 |
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
|
175 |
|
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
176 |
asm := mainFunction builder. |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
177 |
asm call: printfFunction _: { 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
|
178 |
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
|
179 |
" |
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
180 |
module writeBitcodeToFile: '/tmp/main.bc' |
706be0fcef22
Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18
diff
changeset
|
181 |
" |
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 |
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
|
183 |
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
|
184 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
185 |
externalFunction call. |
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
186 |
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
|
187 |
|
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
188 |
" |
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
189 |
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
|
190 |
" |
8998c2d4e53a
Added simple example of calling function defined within the same module.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
9
diff
changeset
|
191 |
|
16
23e82cf19788
Tests refactored to be actually a testcase. Introduced LLVMConstant class
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
14
diff
changeset
|
192 |
"Created: / 03-08-2015 / 10:28:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
193 |
"Modified: / 10-08-2015 / 18:58:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
22
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
194 |
! |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
195 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
196 |
example4_cond |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
197 |
" |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
198 |
Creates a function @even(intpr) which returns |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
199 |
1 if the parameter is even or 0 if not. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
200 |
" |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
201 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
202 |
| module |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
203 |
functionType function asm isOdd ifOdd ifEven jit externalFunction | |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
204 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
205 |
module := LLVMModule newWithName: testSelector. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
206 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
207 |
functionType := LLVMType function: { LLVMType intptr } returning: LLVMType intptr. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
208 |
function := module addFunctionNamed: 'even' type: functionType. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
209 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
210 |
asm := function builder. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
211 |
isOdd := asm icmp: (asm and: (function parameterAt: 1) _: (LLVMConstant uintptr: 1)) _: (LLVMConstant uintptr: 1) cond: LLVMIntEQ. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
212 |
ifOdd := function addBasicBlock. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
213 |
ifOdd builder |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
214 |
ret: (LLVMConstant uintptr: 0). |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
215 |
ifEven := function addBasicBlock. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
216 |
ifEven builder |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
217 |
ret: (LLVMConstant uintptr: 1). |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
218 |
asm if: isOdd then: ifOdd else: ifEven. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
219 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
220 |
jit := LLVMExecutionEngine newForModule: module. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
221 |
externalFunction := jit externalOfFunction: function. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
222 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
223 |
self assert: (externalFunction callWith: 10) == 1. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
224 |
self assert: (externalFunction callWith: 11) == 0. |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
225 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
226 |
" |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
227 |
LLVMExamples example3_cond |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
228 |
" |
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
229 |
|
789a35bd30ac
Added example for compiling conditional. Added convenience API to simply code generation.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19
diff
changeset
|
230 |
"Created: / 08-08-2015 / 04:16:25 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
231 |
! |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
232 |
|
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
233 |
example5_factorial |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
234 |
" |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
235 |
S simple factorial using recursive algorithm. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
236 |
No negative argument or overflow checks" |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
237 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
238 |
| module functionType function asm "Variables" |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
239 |
result i "Blocks" |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
240 |
entry loop loopBody exit jit externalFunction | |
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
241 |
|
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
242 |
module := LLVMModule newWithName:testSelector. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
243 |
functionType := LLVMType function:{ |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
244 |
LLVMType intptr |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
245 |
} |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
246 |
returning:LLVMType intptr. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
247 |
function := module addFunctionNamed:'factorial' type:functionType. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
248 |
asm := LLVMIRBuilder new. |
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
249 |
entry := function entry. |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
250 |
loop := function addBasicBlockNamed:'loop'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
251 |
loopBody := function addBasicBlockNamed:'loopBody'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
252 |
exit := function addBasicBlockNamed:'exit'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
253 |
|
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
254 |
"/ Generate function setup |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
255 |
"/ |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
256 |
"/ function f(v) { |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
257 |
"/ var result; |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
258 |
"/ var i; |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
259 |
"/ result = 0; |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
260 |
"/ i := v; |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
261 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
262 |
asm block:entry. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
263 |
result := asm alloca:LLVMType intptr as:'result'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
264 |
i := asm alloca:LLVMType intptr as:'i'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
265 |
asm store:(function parameterAt:1) _:i. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
266 |
asm store:(function parameterAt:1) _:result. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
267 |
asm br:loop. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
268 |
|
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
269 |
"/ Generate loop that computes the factorial |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
270 |
"/ |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
271 |
"/ while ( i > 1 ) { |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
272 |
"/ result = result * i; |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
273 |
"/ i = i - 1. |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
274 |
"/ } |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
275 |
"/ |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
276 |
"/ Note, that unlike 'traditional' assemblers, there's no |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
277 |
"/ fall-through instruction, so we have to introduce a block |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
278 |
"/ loop's body which will become a target for conditional's |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
279 |
"/ then-branch. |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
280 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
281 |
asm block:loop. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
282 |
asm |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
283 |
if:(asm |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
284 |
icmp:(asm load:i) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
285 |
_:(LLVMConstant sintptr:1) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
286 |
cond:LLVMIntSGT) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
287 |
then:loopBody |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
288 |
else:exit. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
289 |
asm block:loopBody. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
290 |
asm store:(asm mul:(asm load:result) _:(asm load:i)) _:result. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
291 |
asm store:(asm sub:(asm load:i) _:(LLVMConstant sintptr:1)) _:i. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
292 |
asm br:loop. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
293 |
|
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
294 |
"/ Generate return from function |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
295 |
"/ |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
296 |
"/ return result; |
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
297 |
"/ |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
298 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
299 |
asm block:exit. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
300 |
asm ret:(asm load:result). |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
301 |
jit := LLVMExecutionEngine newForModule:module. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
302 |
externalFunction := jit externalOfFunction:function. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
303 |
self assert:(externalFunction callWith:5) == 120. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
304 |
self assert:(externalFunction callWith:1) == 1. |
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
305 |
|
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
306 |
" |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
307 |
LLVMExamples example3_cond" |
23
0744cd9c0acc
Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
22
diff
changeset
|
308 |
"Created: / 10-08-2015 / 09:46:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
309 |
! |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
310 |
|
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
311 |
example5_factorial_with_overflow |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
312 |
" |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
313 |
Simple factorial using recursive algorithm. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
314 |
This one checks for overflow, if overflow happens, |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
315 |
return -1" |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
316 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
317 |
| module functionType function asm "Variables" |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
318 |
result i "Blocks" |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
319 |
entry loop loopBody1 loopBody2 exit overflow smulWithOverflow smulWithOverflowValue jit externalFunction | |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
320 |
|
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
321 |
module := LLVMModule newWithName:testSelector. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
322 |
functionType := LLVMType function:{ |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
323 |
LLVMType intptr |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
324 |
} |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
325 |
returning:LLVMType intptr. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
326 |
function := module addFunctionNamed:'factorial' type:functionType. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
327 |
asm := LLVMIRBuilder new. |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
328 |
entry := function entry. |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
329 |
loop := function addBasicBlockNamed:'loop'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
330 |
loopBody1 := function addBasicBlockNamed:'loopBody1'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
331 |
loopBody2 := function addBasicBlockNamed:'loopBody2'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
332 |
exit := function addBasicBlockNamed:'exit'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
333 |
overflow := function addBasicBlockNamed:'overflow'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
334 |
|
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
335 |
"/ Generate function setup |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
336 |
"/ |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
337 |
"/ function f(v) { |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
338 |
"/ var result; |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
339 |
"/ var i; |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
340 |
"/ result = 0; |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
341 |
"/ i := v; |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
342 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
343 |
asm block:entry. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
344 |
result := asm alloca:LLVMType intptr as:'result'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
345 |
i := asm alloca:LLVMType intptr as:'i'. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
346 |
asm store:(function parameterAt:1) _:i. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
347 |
asm store:(function parameterAt:1) _:result. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
348 |
asm br:loop. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
349 |
|
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
350 |
"/ Generate loop that computes the factorial |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
351 |
"/ |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
352 |
"/ while ( i > 1 ) { |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
353 |
"/ result = result * i if overflow goto overflow; |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
354 |
"/ i = i - 1. |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
355 |
"/ } |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
356 |
"/ |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
357 |
"/ First, get the llvm.smul.with.overflow intrinsic: |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
358 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
359 |
smulWithOverflow := module getIntrinsicNamed:'llvm.smul.with.overflow' |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
360 |
types:{ |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
361 |
LLVMType intptr |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
362 |
}. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
363 |
|
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
364 |
"/ Now code the loop |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
365 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
366 |
asm block:loop. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
367 |
asm |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
368 |
if:(asm |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
369 |
icmp:(asm load:i) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
370 |
_:(LLVMConstant sintptr:1) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
371 |
cond:LLVMIntSGT) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
372 |
then:loopBody1 |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
373 |
else:exit. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
374 |
asm block:loopBody1. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
375 |
smulWithOverflowValue := asm call:smulWithOverflow |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
376 |
_:{ |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
377 |
(asm load:result). |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
378 |
(asm load:i) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
379 |
}. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
380 |
asm |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
381 |
if:(asm extractvalue:smulWithOverflowValue at:1) |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
382 |
then:overflow |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
383 |
else:loopBody2. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
384 |
asm block:loopBody2. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
385 |
asm store:(asm extractvalue:smulWithOverflowValue at:0) _:result. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
386 |
asm store:(asm sub:(asm load:i) _:(LLVMConstant sintptr:1)) _:i. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
387 |
asm br:loop. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
388 |
|
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
389 |
"/ Generate return from function |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
390 |
"/ |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
391 |
"/ return result; |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
392 |
"/ |
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
393 |
|
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
394 |
asm block:exit. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
395 |
asm ret:(asm load:result). |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
396 |
|
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
397 |
"/ Generate overflow handler |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
398 |
"/ overflow: |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
399 |
"/ return -1; |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
400 |
"/ |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
401 |
|
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
402 |
asm block:overflow. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
403 |
asm ret:(LLVMConstant sintptr:-1). |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
404 |
jit := LLVMExecutionEngine newForModule:module. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
405 |
externalFunction := jit externalOfFunction:function. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
406 |
self assert:(externalFunction callWith:5) == 120. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
407 |
self assert:(externalFunction callWith:1) == 1. |
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
408 |
self assert:(externalFunction callWith:120) == -1. |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
409 |
|
28
97013ae2abae
Renamed LLVMBuilder to LLVMIRBuilder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
24
diff
changeset
|
410 |
"sly LLVMExamples example3_cond" |
24
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
411 |
"Created: / 10-08-2015 / 17:12:00 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
7e7ddd55174c
Added support for intrinsics.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
23
diff
changeset
|
412 |
"Modified: / 10-08-2015 / 18:58:51 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
8 | 413 |
! ! |
414 |