Author: hager <sven.ha...@uni-duesseldorf.de>
Branch: ppc-jit-backend
Changeset: r45715:83298f0b8507
Date: 2011-07-15 15:39 +0200
http://bitbucket.org/pypy/pypy/changeset/83298f0b8507/

Log:    Added some more tests.

diff --git a/pypy/jit/backend/ppc/ppcgen/ppc_assembler.py 
b/pypy/jit/backend/ppc/ppcgen/ppc_assembler.py
--- a/pypy/jit/backend/ppc/ppcgen/ppc_assembler.py
+++ b/pypy/jit/backend/ppc/ppcgen/ppc_assembler.py
@@ -1,7 +1,12 @@
+import os
+import struct
 from pypy.jit.backend.ppc.ppcgen.ppc_form import PPCForm as Form
 from pypy.jit.backend.ppc.ppcgen.ppc_field import ppc_fields
 from pypy.jit.backend.ppc.ppcgen.assembler import Assembler
 from pypy.jit.backend.ppc.ppcgen.symbol_lookup import lookup
+from pypy.jit.backend.llsupport.asmmemmgr import BlockBuilderMixin
+from pypy.jit.backend.llsupport.asmmemmgr import AsmMemoryManager
+from pypy.rpython.lltypesystem import lltype, rffi
 
 A = Form("frD", "frA", "frB", "XO3", "Rc")
 A1 = Form("frD", "frB", "XO3", "Rc")
@@ -458,7 +463,7 @@
     xor = XS(31, XO1=316, Rc=0)
     xorx = XS(31, XO1=316, Rc=1)
 
-class PPCAssembler(BasicPPCAssembler):
+class PPCAssembler(BasicPPCAssembler, BlockBuilderMixin):
     BA = BasicPPCAssembler
 
     # awkward mnemonics:
@@ -774,6 +779,11 @@
 
     mtcr = BA.mtcrf(CRM=0xFF)
 
+    def emit(self, insn):
+        bytes = struct.pack("i", insn)
+        for byte in bytes:
+            self.writechar(byte)
+
 def hi(w):
     return w >> 16
 
@@ -793,13 +803,68 @@
     return v
 
 class MyPPCAssembler(PPCAssembler):
+    def __init__(self):
+        PPCAssembler.__init__(self)
+        self.init_block_builder()
+        self.patch_list = []
+
     def load_word(self, rD, word):
         self.addis(rD, 0, hi(word))
         self.ori(rD, rD, lo(word))
+
     def load_from(self, rD, addr):
         self.addis(rD, 0, ha(addr))
         self.lwz(rD, rD, la(addr))
 
+    def nop(self):
+        self.ori(0, 0, 0)
+
+    #def bl(self, addr):
+    #    offset = 4 * len(self.insts)
+    #    self.nop()
+    #    self.patch_list.append((offset, addr))
+
+    #def assemble(self, dump=os.environ.has_key('PYPY_DEBUG')):
+    #    insns = self.assemble0(dump)
+    #    for i in insns:
+    #        self.emit(i)
+    #    i = self.materialize(AsmMemoryManager(), [])
+    #    t = lltype.FuncType([], lltype.Signed)
+    #    self.patch_jumps(i)
+    #    return rffi.cast(lltype.Ptr(t), i)
+    #   
+    #def patch_jumps(self, rawstart):
+    #    #import pdb; pdb.set_trace()
+
+    #    for offset, addr in self.patch_list:
+    #        #delta = (rawstart + offset) - addr
+    #        delta = addr - (rawstart + offset)
+    #        delta >>= 2
+    #        print "delta =", delta
+    #        #assert (delta >> 24) == -1
+
+    #        updater = BranchUpdater()
+    #        #updater.bl(delta)
+    #        self.load_word(reg, h)
+
+
+    #        updater.bctrl()
+    #        updater.write_to_mem(rawstart + offset)
+
+class BranchUpdater(PPCAssembler):
+    def __init__(self):
+        PPCAssembler.__init__(self)
+        self.init_block_builder()
+
+    def write_to_mem(self, addr):
+        self.assemble()
+        self.copy_to_raw_memory(addr)
+        
+    def assemble(self, dump=os.environ.has_key('PYPY_DEBUG')):
+        insns = self.assemble0(dump)
+        for i in insns:
+            self.emit(i)
+
 def b(n):
     r = []
     for i in range(32):
@@ -843,3 +908,5 @@
 
 if __name__ == '__main__':
     main()
+
+
diff --git a/pypy/jit/backend/ppc/ppcgen/test/test_ppc.py 
b/pypy/jit/backend/ppc/ppcgen/test/test_ppc.py
--- a/pypy/jit/backend/ppc/ppcgen/test/test_ppc.py
+++ b/pypy/jit/backend/ppc/ppcgen/test/test_ppc.py
@@ -7,6 +7,8 @@
 from pypy.jit.backend.ppc.ppcgen import form, pystructs
 from pypy.jit.backend.detect_cpu import autodetect_main_model
 
+from pypy.rpython.lltypesystem import lltype, rffi
+from pypy.rpython.annlowlevel import llhelper
 
 class TestDisassemble(object):
     def test_match(self):
@@ -36,7 +38,55 @@
         f = a.assemble()
         assert f() == 7
 
-        
+    def test_load_word(self):
+        a = MyPPCAssembler()
+        word = 12341234
+        a.load_word(10, word)
+        a.mtctr(10)
+        a.mfctr(11)
+        a.mr(3, 11)
+        a.blr()
+        f = a.assemble()
+        assert f() == word
+
+    def test_call_function(self):
+        functype =  lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed))
+        call_addr = rffi.cast(lltype.Signed, llhelper(functype, func))
+        a = MyPPCAssembler()
+
+        # NOW EXPLICITLY:
+        # 
+        # - Load the address of the function to call into a register x
+        # - Move the content of this register x into CTR
+        # - Set the LR manually (or with bctrl)
+        # - Do jump
+        # - hopefully no segfault =)
+
+        a.li(3, 50)
+        a.load_word(10, call_addr)
+        a.mtctr(10)
+        a.bctr()
+        a.blr()
+
+        f = a.assemble()
+        assert f() == 65
+
+class AsmCode(object):
+    def __init__(self, size):
+        self.code = MachineCodeBlockWrapper()
+
+    def emit(self, insn):
+        bytes = struct.pack("i", insn)
+        for byte in bytes:
+            self.code.writechar(byte)
+
+    def get_function(self):
+        i = self.code.materialize(AsmMemoryManager(), [])
+        t = lltype.FuncType([], lltype.Signed)
+        return rffi.cast(lltype.Ptr(t), i)
+
+def func(arg):
+    return arg + 15
 """
 class TestAssemble(object):
         
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to