Example #1
0
def test_getNamedFunction():
    for i in range(3):
        llvmjit.restart()
        assert not llvmjit.getNamedFunction('square')
        assert not llvmjit.getNamedFunction('square')
        assert llvmjit.parse(llsquare)
        assert llvmjit.getNamedFunction('square')
        assert llvmjit.getNamedFunction('square')
Example #2
0
def test_getNamedFunction():
    for i in range(3):
        llvmjit.restart()
        assert not llvmjit.getNamedFunction('square')
        assert not llvmjit.getNamedFunction('square')
        assert llvmjit.parse(llsquare)
        assert llvmjit.getNamedFunction('square')
        assert llvmjit.getNamedFunction('square')
Example #3
0
def test_execute_multiple():
    llvmjit.restart()
    llvmjit.parse(llsquare)
    llvmjit.parse(llmul2)
    square = llvmjit.getNamedFunction('square')
    mul2   = llvmjit.getNamedFunction('mul2')
    for i in range(5):
        assert llvmjit.execute(square, i) == i * i
        assert llvmjit.execute(mul2  , i) == i * 2
Example #4
0
def test_execute_multiple():
    llvmjit.restart()
    llvmjit.parse(llsquare)
    llvmjit.parse(llmul2)
    square = llvmjit.getNamedFunction('square')
    mul2 = llvmjit.getNamedFunction('mul2')
    for i in range(5):
        assert llvmjit.execute(square, i) == i * i
        assert llvmjit.execute(mul2, i) == i * 2
Example #5
0
def test_call_global_function(): #used by PyPy JIT for adding case(s) to a flexswitch
    llvmjit.restart()
    gp_function = llvmjit.get_pointer_to_global_function()
    llvmjit.parse(llcall_global_function)
    p = llvmjit.getNamedFunction('my_global_function...')
    assert not p
    p = llvmjit.getNamedFunction('my_global_function')
    assert p
    llvmjit.addGlobalMapping(p, gp_function) #prior to execute()!
    call_global_function = llvmjit.getNamedFunction('call_global_function')
    assert llvmjit.execute(call_global_function, 5) == 3 + 5 + 7
Example #6
0
def test_call_global_function(
):  #used by PyPy JIT for adding case(s) to a flexswitch
    llvmjit.restart()
    gp_function = llvmjit.get_pointer_to_global_function()
    llvmjit.parse(llcall_global_function)
    p = llvmjit.getNamedFunction('my_global_function...')
    assert not p
    p = llvmjit.getNamedFunction('my_global_function')
    assert p
    llvmjit.addGlobalMapping(p, gp_function)  #prior to execute()!
    call_global_function = llvmjit.getNamedFunction('call_global_function')
    assert llvmjit.execute(call_global_function, 5) == 3 + 5 + 7
Example #7
0
def test_transform(): #XXX This uses Module transforms, think about Function transforms too.
    llvmjit.restart()
    llvmjit.parse(lldeadcode)
    deadcode = llvmjit.getNamedFunction('deadcode')
    assert llvmjit.execute(deadcode, 10) == 10 * 2
    assert llvmjit.transform(3) #optlevel = [0123]
    assert llvmjit.execute(deadcode, 20) == 20 * 2
Example #8
0
def test_transform(
):  #XXX This uses Module transforms, think about Function transforms too.
    llvmjit.restart()
    llvmjit.parse(lldeadcode)
    deadcode = llvmjit.getNamedFunction('deadcode')
    assert llvmjit.execute(deadcode, 10) == 10 * 2
    assert llvmjit.transform(3)  #optlevel = [0123]
    assert llvmjit.execute(deadcode, 20) == 20 * 2
Example #9
0
def test_recompile():
    py.test.skip("recompile new function implementation test is work in progress")

    def funcA(n):
        return n + n
    
    def funcB(n):
        return n * n
    llvmjit.restart()
    llvmjit.parse(llfuncA)
    _llfuncA = llvmjit.getNamedFunction('func')
    print '_llfuncA', _llfuncA
    for i in range(5):
        assert llvmjit.execute(_llfuncA, i) == funcA(i)
    llvmjit.freeMachineCodeForFunction(_llfuncA)
    llvmjit.parse(llfuncB)
    _llfuncB = llvmjit.getNamedFunction('func')
    print '_llfuncB', _llfuncB
    llvmjit.recompile(_llfuncB) #note: because %func has changed because of the 2nd parse
    for i in range(5):
        assert llvmjit.execute(_llfuncB, i) == funcB(i)
Example #10
0
def test_execute_across_module():
    def my_across1(n):
        return n * 3

    def my_across1to2(n):
        return my_across2(n + 5)

    def my_across2(n):
        return n * 7

    def my_across2to1(n):
        return my_across1(n + 9)

    llvmjit.restart()
    llvmjit.parse(llacross1)
    llvmjit.parse(llacross2)
    across1to2 = llvmjit.getNamedFunction('across1to2')
    across2to1 = llvmjit.getNamedFunction('across2to1')
    for i in range(5):
        assert llvmjit.execute(across1to2, i) == my_across1to2(i)
        assert llvmjit.execute(across2to1, i) == my_across2to1(i)
Example #11
0
def test_execute_across_module():
    def my_across1(n):
        return n * 3

    def my_across1to2(n):
        return my_across2(n + 5)

    def my_across2(n):
        return n * 7

    def my_across2to1(n):
        return my_across1(n + 9)

    llvmjit.restart()
    llvmjit.parse(llacross1)
    llvmjit.parse(llacross2)
    across1to2 = llvmjit.getNamedFunction('across1to2')
    across2to1 = llvmjit.getNamedFunction('across2to1')
    for i in range(5):
        assert llvmjit.execute(across1to2, i) == my_across1to2(i)
        assert llvmjit.execute(across2to1, i) == my_across2to1(i)
Example #12
0
def test_modify_global_data():
    llvmjit.restart()
    llvmjit.set_global_data(10)
    assert llvmjit.get_global_data() == 10
    gp_data = llvmjit.get_pointer_to_global_data()
    llvmjit.parse(llglobalmul4)
    p = llvmjit.getNamedGlobal('my_global_data...')
    assert not p
    p = llvmjit.getNamedGlobal('my_global_data')
    assert p
    llvmjit.addGlobalMapping(p, gp_data) #note: should be prior to execute()
    globalmul4 = llvmjit.getNamedFunction('globalmul4')
    assert llvmjit.execute(globalmul4, 5) == 10 * 4 + 5
    assert llvmjit.get_global_data() == 10 * 4 + 5
Example #13
0
def test_modify_global_data():
    llvmjit.restart()
    llvmjit.set_global_data(10)
    assert llvmjit.get_global_data() == 10
    gp_data = llvmjit.get_pointer_to_global_data()
    llvmjit.parse(llglobalmul4)
    p = llvmjit.getNamedGlobal('my_global_data...')
    assert not p
    p = llvmjit.getNamedGlobal('my_global_data')
    assert p
    llvmjit.addGlobalMapping(p, gp_data)  #note: should be prior to execute()
    globalmul4 = llvmjit.getNamedFunction('globalmul4')
    assert llvmjit.execute(globalmul4, 5) == 10 * 4 + 5
    assert llvmjit.get_global_data() == 10 * 4 + 5
Example #14
0
def test_recompile():
    py.test.skip(
        "recompile new function implementation test is work in progress")

    def funcA(n):
        return n + n

    def funcB(n):
        return n * n

    llvmjit.restart()
    llvmjit.parse(llfuncA)
    _llfuncA = llvmjit.getNamedFunction('func')
    print '_llfuncA', _llfuncA
    for i in range(5):
        assert llvmjit.execute(_llfuncA, i) == funcA(i)
    llvmjit.freeMachineCodeForFunction(_llfuncA)
    llvmjit.parse(llfuncB)
    _llfuncB = llvmjit.getNamedFunction('func')
    print '_llfuncB', _llfuncB
    llvmjit.recompile(
        _llfuncB)  #note: because %func has changed because of the 2nd parse
    for i in range(5):
        assert llvmjit.execute(_llfuncB, i) == funcB(i)
Example #15
0
    def end(self):
        log('   RLLVMGenOp.end')
        self.blocklist.append(EpilogueBlock())
        asmlines = []
        for block in self.blocklist:
            block.writecode(asmlines)
        if LINENO:
            asmlines = ['%s ;%d' % (asmlines[i], i+1) for i in range(len(asmlines))]
        asm_string = '\n'.join(asmlines)

        self.blocklist = None
        if PRINT_SOURCE:
            print asm_string
        logger.dump(asm_string)
        parse_ok = llvmjit.parse(asm_string)
        if not parse_ok:
            raise ParseException()
        llvmjit.transform(3) #optimize module (should be on functions actually)
        function   = llvmjit.getNamedFunction(self.name)
        entrypoint = llvmjit.getPointerToFunctionAsInt(function)
        # XXX or directly cast the ctypes ptr to int with:
        #   ctypes.cast(ptr, c_void_p).value
        self.funcsig[entrypoint] = self.funcsig[self.gv_entrypoint.value]
        self.gv_entrypoint.value = entrypoint
Example #16
0
def execute(llsource, function_name, param):
    assert llvmjit.parse(llsource)
    function = llvmjit.getNamedFunction(function_name)
    assert function
    return llvmjit.execute(function, param)
Example #17
0
def execute(llsource, function_name, param):
    assert llvmjit.parse(llsource)
    function = llvmjit.getNamedFunction(function_name)
    assert function
    return llvmjit.execute(function, param)