LLVMExamples.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 10 Aug 2015 10:33:42 +0100
changeset 23 0744cd9c0acc
parent 22 789a35bd30ac
child 24 7e7ddd55174c
permissions -rw-r--r--
Added an example of iterative factorial function It does not check for integer overflow as that would require using an intrinsic (which are not yet supported)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    15
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    16
"{ NameSpace: Smalltalk }"
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    19
	instanceVariableNames:''
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    23
!
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    52
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    68
    functionType := LLVMType function: { LLVMType intptr . LLVMType intptr } returning: LLVMType intptr.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    69
    function := module addFunctionNamed: 'sum' type: functionType.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    94
    externalFunction := jit externalOfFunction: function.
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    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
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    97
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    98
    "
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    99
    LLVMExamples example1_sum
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   100
    "
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   101
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   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.
9
540c1c31a33d Added basic support for calling functions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 8
diff changeset
   125
    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
   126
     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
   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>"
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
   152
    "Modified: / 08-08-2015 / 04:34:33 / 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.
19
706be0fcef22 Refactored 'Hello World' example to ise GEP.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18
diff changeset
   177
    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
   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>"
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
   193
    "Modified: / 08-08-2015 / 04:34:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
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
    "
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   235
    S simple factorial using recursive algorithm.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   236
    No negative argument or overflow checks
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   237
    "    
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   238
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   239
    | module 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   240
      functionType function asm 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   241
      "Variables" result i 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   242
      "Blocks"    entry loop loopBody exit
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   243
      jit externalFunction |
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   244
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   245
    module := LLVMModule newWithName: testSelector.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   246
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   247
    functionType := LLVMType function: { LLVMType intptr } returning: LLVMType intptr.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   248
    function := module addFunctionNamed: 'factorial' type: functionType.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   249
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   250
    asm := LLVMBuilder new.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   251
    entry := function entry.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   252
    loop  := function addBasicBlockNamed: 'loop'.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   253
    loopBody  := function addBasicBlockNamed: 'loopBody'.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   254
    exit  := function addBasicBlockNamed: 'exit'.
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
    "/ Generate function setup
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   257
    "/ 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   258
    "/   function f(v) {
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   259
    "/     var result;
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   260
    "/     var i;
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   261
    "/     result = 0;
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   262
    "/     i := v;
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   263
    asm block: entry.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   264
    result := asm alloca: LLVMType intptr as: 'result'.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   265
    i := asm alloca: LLVMType intptr as: 'i'.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   266
    asm store: (function parameterAt: 1)   _: i.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   267
    asm store: (function parameterAt: 1) _: result.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   268
    asm br: loop.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   269
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   270
    "/ 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
   271
    "/ 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   272
    "/     while ( i > 1 ) {
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   273
    "/       result = result * i;
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   274
    "/       i = i - 1.
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
    "/ 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   277
    "/ 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
   278
    "/ 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
   279
    "/ 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
   280
    "/ then-branch.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   281
    asm block: loop.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   282
    asm if: (asm icmp: (asm load: i) _: (LLVMConstant sintptr:1) cond: LLVMIntSGT) then: loopBody else: exit.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   283
    asm block: loopBody.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   284
    asm store: (asm mul: (asm load: result) _: (asm load: i)) _: result.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   285
    asm store: (asm sub: (asm load: i) _: (LLVMConstant sintptr:1)) _: i.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   286
    asm br: loop.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   287
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   288
    "/ Generate return from function
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   289
    "/ 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   290
    "/     return result;
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   291
    "/ 
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   292
    asm block: exit.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   293
    asm ret: (asm load: result).
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   294
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   295
    jit := LLVMExecutionEngine newForModule: module.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   296
    externalFunction := jit externalOfFunction: function.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   297
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   298
    self assert: (externalFunction callWith: 5) == 120.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   299
    self assert: (externalFunction callWith: 1) == 1.
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   300
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   301
    "
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   302
    LLVMExamples example3_cond
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   303
    "
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   304
0744cd9c0acc Added an example of iterative factorial function
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 22
diff changeset
   305
    "Created: / 10-08-2015 / 09:46:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
8
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   306
! !
890eb7591eca Added class LLVMExamples
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   307