Commits

Sven Hager committed 86b9bb6

factor out common code

  • Participants
  • Parent commits 4cb3a7f
  • Branches ppc-jit-backend

Comments (0)

Files changed (3)

pypy/jit/backend/ppc/arch.py

 SIZE_LOAD_IMM_PATCH_SP  = 6
 
 FORCE_INDEX_OFS         = len(MANAGED_REGS) * WORD
+
+# offset to LR in BACKCHAIN
+if IS_PPC_32:
+    LR_BC_OFFSET = WORD
+else:
+    LR_BC_OFFSET = 2 * WORD

pypy/jit/backend/ppc/codebuilder.py

 from pypy.jit.backend.ppc.locations import RegisterLocation
 from pypy.jit.backend.ppc.ppc_field import ppc_fields
 from pypy.jit.backend.ppc.assembler import Assembler
-from pypy.jit.backend.ppc.arch import (IS_PPC_32, WORD, IS_PPC_64)
+from pypy.jit.backend.ppc.arch import (IS_PPC_32, WORD, IS_PPC_64, 
+                                       LR_BC_OFFSET)
 import pypy.jit.backend.ppc.register as r
 from pypy.jit.backend.llsupport.asmmemmgr import BlockBuilderMixin
 from pypy.rpython.lltypesystem import lltype, rffi
         if IS_PPC_64:
             self.load(r.TOC.value, r.SP.value, 5 * WORD)
 
+    def make_function_prologue(self, frame_size):
+        """ Build a new stackframe of size frame_size 
+            and store the LR in the previous frame.
+        """
+        with scratch_reg(self):
+            self.store_update(r.SP.value, r.SP.value, -frame_size)
+            self.mflr(r.SCRATCH.value)
+            self.store(r.SCRATCH.value, r.SP.value, frame_size + LR_BC_OFFSET) 
+
+    def restore_LR_from_caller_frame(self, frame_size):
+        """ Restore the LR from the calling frame.
+            frame_size is the size of the current frame.
+        """
+        with scratch_reg(self):
+            lr_offset = frame_size + LR_BC_OFFSET
+            self.load(r.SCRATCH.value, r.SP.value, lr_offset)
+            self.mtlr(r.SCRATCH.value)
+
     def load(self, target_reg, base_reg, offset):
         if IS_PPC_32:
             self.lwz(target_reg, base_reg, offset)
         else:
             self.stdx(from_reg, base_reg, offset_reg)
 
+    def store_update(self, target_reg, from_reg, offset):
+        if IS_PPC_32:
+            self.stwu(target_reg, from_reg, offset)
+        else:
+            self.stdu(target_reg, from_reg, offset)
+
     def srli_op(self, target_reg, from_reg, numbits):
         if IS_PPC_32:
             self.srwi(target_reg, from_reg, numbits)

pypy/jit/backend/ppc/ppc_assembler.py

     # The code generated here allocates a new stackframe 
     # and is the first machine code to be executed.
     def _make_frame(self, frame_depth):
-        if IS_PPC_32:
-            # save it in previous frame (Backchain)
-            self.mc.stwu(r.SP.value, r.SP.value, -frame_depth)
-            self.mc.mflr(r.SCRATCH.value)  # move old link register
-            # save old link register in previous frame
-            self.mc.stw(r.SCRATCH.value, r.SP.value, frame_depth + WORD) 
-        else:
-            self.mc.stdu(r.SP.value, r.SP.value, -frame_depth)
-            self.mc.mflr(r.SCRATCH.value)
-            self.mc.std(r.SCRATCH.value, r.SP.value, frame_depth + 2 * WORD)
+        self.mc.make_function_prologue(frame_depth)
+
         # save SPP at the bottom of the stack frame
         self.mc.store(r.SPP.value, r.SP.value, WORD)
 
             ofs = WORD
         else:
             ofs = WORD * 2
-
+        
         with scratch_reg(mc):
             mc.load(r.SCRATCH.value, r.SP.value, frame_size + ofs) 
             mc.mtlr(r.SCRATCH.value)
                 mc.write32(0)
 
         # build frame
-        with scratch_reg(mc):
-            if IS_PPC_32:
-                mc.stwu(r.SP.value, r.SP.value, -frame_size)
-                mc.mflr(r.SCRATCH.value)
-                mc.stw(r.SCRATCH.value, r.SP.value, frame_size + WORD) 
-            else:
-                mc.stdu(r.SP.value, r.SP.value, -frame_size)
-                mc.mflr(r.SCRATCH.value)
-                mc.std(r.SCRATCH.value, r.SP.value, frame_size + 2 * WORD)
+        mc.make_function_prologue(frame_size)
 
         # save parameter registers
         for i, reg in enumerate(r.PARAM_REGS):
             mc.load(reg.value, r.SP.value, (i + BACKCHAIN_SIZE) * WORD)
 
         # restore LR
-        with scratch_reg(mc):
-            lr_offset = frame_size + WORD
-            if IS_PPC_64:
-                lr_offset += WORD
-                
-            mc.load(r.SCRATCH.value, r.SP.value, 
-                        lr_offset)
-            mc.mtlr(r.SCRATCH.value)
+        mc.restore_LR_from_caller_frame(frame_size)
 
         # reset SP
         mc.addi(r.SP.value, r.SP.value, frame_size)
             # |                            |
             # ============================== <- SP
 
-            if IS_PPC_32:
-                self.mc.stwu(r.SP.value, r.SP.value, -frame_size)
-                self.mc.mflr(r.SCRATCH.value)
-                self.mc.stw(r.SCRATCH.value, r.SP.value, frame_size + WORD) 
-            else:
-                self.mc.stdu(r.SP.value, r.SP.value, -frame_size)
-                self.mc.mflr(r.SCRATCH.value)
-                self.mc.std(r.SCRATCH.value, r.SP.value, frame_size + 2 * WORD)
+            self.mc.make_function_prologue(frame_size)
 
             # make check
             self.mc.call(self.stack_check_slowpath)
 
             # restore LR
-            with scratch_reg(self.mc):
-                lr_offset = frame_size + WORD
-                if IS_PPC_64:
-                    lr_offset += WORD
-                    
-                self.mc.load(r.SCRATCH.value, r.SP.value, 
-                            lr_offset)
-                self.mc.mtlr(r.SCRATCH.value)
+            self.mc.restore_LR_from_caller_frame(frame_size)
 
             # remove minimal frame
             self.mc.addi(r.SP.value, r.SP.value, frame_size)