changeset 9247:27c75e4016db

clarified code for emitting LIR code to save RBP
author Doug Simon <doug.simon@oracle.com>
date Tue, 23 Apr 2013 14:03:32 +0200
parents ba3dfa9e36d8
children dd596345f28a 542712a4732a
files graal/com.oracle.graal.hotspot.amd64/src/com/oracle/graal/hotspot/amd64/AMD64HotSpotLIRGenerator.java graal/com.oracle.graal.lir/src/com/oracle/graal/lir/StandardOp.java
diffstat 2 files changed, 75 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot.amd64/src/com/oracle/graal/hotspot/amd64/AMD64HotSpotLIRGenerator.java	Tue Apr 23 11:29:55 2013 +0200
+++ b/graal/com.oracle.graal.hotspot.amd64/src/com/oracle/graal/hotspot/amd64/AMD64HotSpotLIRGenerator.java	Tue Apr 23 14:03:32 2013 +0200
@@ -43,11 +43,11 @@
 import com.oracle.graal.hotspot.stubs.*;
 import com.oracle.graal.lir.*;
 import com.oracle.graal.lir.StandardOp.ParametersOp;
+import com.oracle.graal.lir.StandardOp.PlaceholderOp;
 import com.oracle.graal.lir.amd64.*;
 import com.oracle.graal.lir.amd64.AMD64Move.CompareAndSwapOp;
 import com.oracle.graal.lir.amd64.AMD64Move.MoveFromRegOp;
 import com.oracle.graal.nodes.*;
-import com.oracle.graal.nodes.cfg.*;
 import com.oracle.graal.nodes.java.MethodCallTargetNode.InvokeKind;
 
 /**
@@ -71,15 +71,43 @@
     StackSlot deoptimizationRescueSlot;
 
     /**
-     * The position at which the instruction for saving RBP should be inserted.
+     * Utility for emitting the instruction to save RBP.
      */
-    Block saveRbpBlock;
-    int saveRbpIndex;
+    class SaveRbp {
+
+        final PlaceholderOp placeholder;
+
+        /**
+         * The slot reserved for saving RBP.
+         */
+        final StackSlot reservedSlot;
+
+        public SaveRbp(PlaceholderOp placeholder) {
+            this.placeholder = placeholder;
+            this.reservedSlot = frameMap.allocateSpillSlot(Kind.Long);
+            assert reservedSlot.getRawOffset() == -16 : reservedSlot.getRawOffset();
+        }
 
-    /**
-     * The slot reserved for saving RBP.
-     */
-    StackSlot rbpSlot;
+        /**
+         * Replaces this operation with the appropriate move for saving rbp.
+         * 
+         * @param useStack specifies if rbp must be saved to the stack
+         */
+        public AllocatableValue finalize(boolean useStack) {
+            AllocatableValue dst;
+            if (useStack) {
+                dst = reservedSlot;
+            } else {
+                frameMap.freeSpillSlot(reservedSlot);
+                dst = newVariable(Kind.Long);
+            }
+
+            placeholder.replace(lir, new MoveFromRegOp(dst, rbp.asValue(Kind.Long)));
+            return dst;
+        }
+    }
+
+    private SaveRbp saveRbp;
 
     /**
      * List of epilogue operations that need to restore RBP.
@@ -119,11 +147,9 @@
 
         ParametersOp paramsOp = new ParametersOp(params);
         append(paramsOp);
-        saveRbpBlock = currentBlock;
-        saveRbpIndex = lir.lir(saveRbpBlock).size();
-        append(paramsOp); // placeholder
-        rbpSlot = frameMap.allocateSpillSlot(Kind.Long);
-        assert rbpSlot.getRawOffset() == -16 : rbpSlot.getRawOffset();
+
+        saveRbp = new SaveRbp(new PlaceholderOp(currentBlock, lir.lir(currentBlock).size()));
+        append(saveRbp.placeholder);
 
         for (LocalNode local : graph.getNodes(LocalNode.class)) {
             Value param = params[local.index()];
@@ -238,23 +264,14 @@
 
     @Override
     public void beforeRegisterAllocation() {
-        assert rbpSlot != null;
-        RegisterValue rbpParam = rbp.asValue(Kind.Long);
-        AllocatableValue savedRbp;
-        LIRInstruction saveRbp;
-        if (lir.hasDebugInfo()) {
-            savedRbp = rbpSlot;
+        boolean hasDebugInfo = lir.hasDebugInfo();
+        AllocatableValue savedRbp = saveRbp.finalize(hasDebugInfo);
+        if (hasDebugInfo) {
             deoptimizationRescueSlot = frameMap.allocateSpillSlot(Kind.Long);
-        } else {
-            frameMap.freeSpillSlot(rbpSlot);
-            savedRbp = newVariable(Kind.Long);
         }
 
         for (AMD64HotSpotEpilogueOp op : epilogueOps) {
             op.savedRbp = savedRbp;
         }
-
-        saveRbp = new MoveFromRegOp(savedRbp, rbpParam);
-        lir.lir(saveRbpBlock).set(saveRbpIndex, saveRbp);
     }
 }
--- a/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/StandardOp.java	Tue Apr 23 11:29:55 2013 +0200
+++ b/graal/com.oracle.graal.lir/src/com/oracle/graal/lir/StandardOp.java	Tue Apr 23 14:03:32 2013 +0200
@@ -24,7 +24,10 @@
 
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.asm.*;
+import com.oracle.graal.graph.*;
 import com.oracle.graal.lir.asm.*;
+import com.oracle.graal.nodes.cfg.*;
+
 import static com.oracle.graal.lir.LIRInstruction.OperandFlag.*;
 
 /**
@@ -135,4 +138,34 @@
             // No code to emit.
         }
     }
+
+    /**
+     * Placeholder for a LIR instruction that will be subsequently replaced.
+     */
+    public static class PlaceholderOp extends LIRInstruction {
+
+        /**
+         * The block in which this instruction is located.
+         */
+        final Block block;
+
+        /**
+         * The block index of this instruction.
+         */
+        final int index;
+
+        public PlaceholderOp(Block block, int index) {
+            this.block = block;
+            this.index = index;
+        }
+
+        public void replace(LIR lir, LIRInstruction replacement) {
+            lir.lir(block).set(index, replacement);
+        }
+
+        @Override
+        public void emitCode(TargetMethodAssembler tasm) {
+            throw new GraalInternalError(this + " should have been replaced");
+        }
+    }
 }