tests/LLVMIRBuilderTests.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 06 Jul 2016 22:40:59 +0100
changeset 71 ab03b0a6d037
parent 66 0125c050d0f1
permissions -rw-r--r--
Implemented LLVMType>>sizeInBits/sizeInBytes for all data types ...i.e., also for structures, vectors and arrays.

"{ Package: 'jv:llvm_s/tests' }"

"{ NameSpace: Smalltalk }"

TestCase subclass:#LLVMIRBuilderTests
	instanceVariableNames:'module'
	classVariableNames:''
	poolDictionaries:'LLVMIntPredicate'
	category:'LLVM-S-Core-Tests'
!


!LLVMIRBuilderTests methodsFor:'running'!

setUp
    super setUp.

    module := LLVMModule newWithName: testSelector

    "Modified (format): / 23-04-2016 / 23:08:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

tearDown
    module := nil.
    
    super tearDown.

    "Modified: / 23-04-2016 / 23:08:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMIRBuilderTests methodsFor:'tests - terminators'!

test_if_then_else_01a
    | f fTy asm jit |

    fTy := LLVMType function: { LLVMType intptr }  returning: LLVMType intptr.
    f := module addFunctionNamed: testSelector type: fTy.
    asm := f builder.

    asm if: (asm icmp: (LLVMConstant sintptr: 0) _: (f parameterAt:1) cond: LLVMIntEQ) then:[ 
        asm ret: (LLVMConstant sintptr: 1)
    ] else:[ 
        asm ret: (LLVMConstant sintptr: 0)
    ].

    jit := LLVMExecutionEngine newForModule: module.
    self assert: ((jit externalOfFunction: f) callWith: 3) == 0.
    self assert: ((jit externalOfFunction: f) callWith: 0) == 1.

    "Created: / 25-04-2016 / 23:26:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_if_then_else_01b
    | f fTy asm jit |

    fTy := LLVMType function: { LLVMType intptr }  returning: LLVMType intptr.
    f := module addFunctionNamed: testSelector type: fTy.
    asm := f builder.

    asm if: (asm icmp: (LLVMConstant sintptr: 0) _: (f parameterAt:1) cond: LLVMIntEQ) then:[ 
        asm ret: (LLVMConstant sintptr: 1)
    ].
    asm ret: (LLVMConstant sintptr: 0).

    jit := LLVMExecutionEngine newForModule: module.
    self assert: ((jit externalOfFunction: f) callWith: 3) == 0.
    self assert: ((jit externalOfFunction: f) callWith: 0) == 1.

    "Created: / 25-04-2016 / 23:26:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_if_then_else_02a
    | f fTy asm jit |

    fTy := LLVMType function: { LLVMType intptr }  returning: LLVMType intptr.
    f := module addFunctionNamed: testSelector type: fTy.
    asm := f builder.

    asm if: (asm icmp: (LLVMConstant sintptr: 0) _: (f parameterAt:1) cond: LLVMIntEQ) then:[ 
        asm ret: (LLVMConstant sintptr: 0)
    ] else:[ 
        asm if: (asm icmp: (f parameterAt:1) _: (LLVMConstant sintptr: 0) cond: LLVMIntSGT) then:[ 
            asm ret: (LLVMConstant sintptr: 1)
        ] else:[ 
            asm ret: (LLVMConstant sintptr: -1)
        ].
    ].

    jit := LLVMExecutionEngine newForModule: module.
    self assert: ((jit externalOfFunction: f) callWith: 0) == 0.
    self assert: ((jit externalOfFunction: f) callWith: 100) == 1.
    self assert: ((jit externalOfFunction: f) callWith: -100) == -1.

    "Created: / 25-04-2016 / 23:26:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_if_then_else_02b
    | f fTy asm jit |

    fTy := LLVMType function: { LLVMType intptr }  returning: LLVMType intptr.
    f := module addFunctionNamed: testSelector type: fTy.
    asm := f builder.

    asm if: (asm icmp: (LLVMConstant sintptr: 0) _: (f parameterAt:1) cond: LLVMIntEQ) then:[ 
        asm ret: (LLVMConstant sintptr: 0)
    ] else:[ 
        asm if: (asm icmp: (f parameterAt:1) _: (LLVMConstant sintptr: 0) cond: LLVMIntSGT) then:[ 
            asm ret: (LLVMConstant sintptr: 1)
        ]
    ].
    asm ret: (LLVMConstant sintptr: -1).

    jit := LLVMExecutionEngine newForModule: module.
    self assert: ((jit externalOfFunction: f) callWith: 0) == 0.
    self assert: ((jit externalOfFunction: f) callWith: 100) == 1.
    self assert: ((jit externalOfFunction: f) callWith: -100) == -1.

    "Created: / 25-04-2016 / 23:27:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_if_then_else_02c
    | f fTy asm jit |

    fTy := LLVMType function: { LLVMType intptr }  returning: LLVMType intptr.
    f := module addFunctionNamed: testSelector type: fTy.
    asm := f builder.

    asm if: (asm icmp: (LLVMConstant sintptr: 0) _: (f parameterAt:1) cond: LLVMIntEQ) then:[ 
        asm ret: (LLVMConstant sintptr: 0)
    ] else:[ 
        asm if: (asm icmp: (f parameterAt:1) _: (LLVMConstant sintptr: 0) cond: LLVMIntSGT) then:[ 
            asm ret: (LLVMConstant sintptr: 1)
        ] else:[ 
            asm ret: (LLVMConstant sintptr: -1).
        ].
    ].
    jit := LLVMExecutionEngine newForModule: module.
    self assert: ((jit externalOfFunction: f) callWith: 0) == 0.
    self assert: ((jit externalOfFunction: f) callWith: 100) == 1.
    self assert: ((jit externalOfFunction: f) callWith: -100) == -1.

    "Created: / 18-06-2016 / 11:35:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

test_if_then_else_02d
    | f fTy asm retval jit  |

    fTy := LLVMType function: { LLVMType intptr }  returning: LLVMType intptr.
    f := module addFunctionNamed: testSelector type: fTy.
    asm := f builder.
    retval := asm alloca: LLVMType intptr.

    asm if: (asm icmp: (LLVMConstant sintptr: 0) _: (f parameterAt:1) cond: LLVMIntEQ) then:[ 
        asm store: (LLVMConstant sintptr: 0) at: retval.
    ] else:[ 
        asm if: (asm icmp: (f parameterAt:1) _: (LLVMConstant sintptr: 0) cond: LLVMIntSGT) then:[ 
            asm store: (LLVMConstant sintptr: 1) at: retval.
        ] else:[ 
            asm store: (LLVMConstant sintptr: -1) at: retval.
        ].
    ].
    asm ret: (asm load: retval).

    module verify.

    jit := LLVMExecutionEngine newForModule: module.
    self assert: ((jit externalOfFunction: f) callWith: 0) == 0.
    self assert: ((jit externalOfFunction: f) callWith: 100) == 1.
    self assert: ((jit externalOfFunction: f) callWith: -100) == -1.

    "Created: / 18-06-2016 / 11:38:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 18-06-2016 / 16:51:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!LLVMIRBuilderTests class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
! !