From: <mic...@us...> - 2007-05-17 22:26:47
|
Revision: 122 http://svn.sourceforge.net/pearcolator/?rev=122&view=rev Author: michael_baer Date: 2007-05-17 15:26:48 -0700 (Thu, 17 May 2007) Log Message: ----------- - Added further branch profile notifications for call & return instructions - Removed registration of unresolved IfCmps for code clarity Modified Paths: -------------- src/org/binarytranslator/arch/arm/decoder/ARM_Translator.java src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java src/org/binarytranslator/arch/x86/decoder/X86_InstructionDecoder.java src/org/binarytranslator/generic/branch/BranchLogic.java src/org/binarytranslator/generic/decoder/AbstractCodeTranslator.java Modified: src/org/binarytranslator/arch/arm/decoder/ARM_Translator.java =================================================================== --- src/org/binarytranslator/arch/arm/decoder/ARM_Translator.java 2007-05-17 19:20:02 UTC (rev 121) +++ src/org/binarytranslator/arch/arm/decoder/ARM_Translator.java 2007-05-17 22:26:48 UTC (rev 122) @@ -1660,7 +1660,7 @@ } arm2ir.getCurrentBlock().deleteNormalOut(); - arm2ir.appendGoto(pc + getOffset() + 8, lazy); + arm2ir.appendGoto(pc + getOffset() + 8, lazy, link ? BranchType.CALL : BranchType.DIRECT_BRANCH); } public int getSuccessor(int pc) { Modified: src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java =================================================================== --- src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java 2007-05-17 19:20:02 UTC (rev 121) +++ src/org/binarytranslator/arch/ppc/decoder/PPC_InstructionDecoder.java 2007-05-17 22:26:48 UTC (rev 122) @@ -11703,9 +11703,9 @@ BI, !branch_if_cond_true, likely_to_fallthrough); } - if ((LK == 0) || (ppc2ir.traceContinuesAfterBranchAndLink(pc))) { + if ((LK == 0) || ppc2ir.traceContinuesAfterBranchAndLink(pc)) { // Plant branch block - ppc2ir.appendGoto(target_address, lazy); + ppc2ir.appendGoto(target_address, lazy, LK != 0 ? BranchType.CALL : BranchType.DIRECT_BRANCH); // stop translation on branch always if (BO == 0x14) { @@ -11724,15 +11724,14 @@ ppc2ir.setCurrentBlock(instructionEndBlock); ppc2ir.setNextBlock(ppc2ir.createBlockAfterCurrent()); - ppc2ir.appendGoto(pc + 4, lazy); + ppc2ir.appendGoto(pc + 4, lazy, BranchType.DIRECT_BRANCH); return target_address; } } } else { // This was a branch and link and the trace should stop, so end // gracefully - ppc2ir.setReturnValueResolveLazinessAndBranchToFinish((PPC_Laziness) lazy - .clone(), new OPT_IntConstantOperand(target_address)); + ppc2ir.appendGoto(target_address, lazy, BranchType.CALL); return -1; } } Modified: src/org/binarytranslator/arch/x86/decoder/X86_InstructionDecoder.java =================================================================== --- src/org/binarytranslator/arch/x86/decoder/X86_InstructionDecoder.java 2007-05-17 19:20:02 UTC (rev 121) +++ src/org/binarytranslator/arch/x86/decoder/X86_InstructionDecoder.java 2007-05-17 22:26:48 UTC (rev 122) @@ -11,6 +11,7 @@ import org.binarytranslator.DBT_Options; import org.binarytranslator.arch.x86.os.process.X86_ProcessSpace; import org.binarytranslator.arch.x86.os.process.X86_Registers; +import org.binarytranslator.generic.branch.BranchLogic.BranchType; import org.binarytranslator.generic.decoder.InstructionDecoder; import org.binarytranslator.generic.decoder.Laziness; import org.binarytranslator.generic.fault.BadInstructionException; @@ -3646,9 +3647,9 @@ translationHelper.appendInstruction(Move.create(INT_MOVE, temp, new OPT_IntConstantOperand(pc + length))); stack.writeValue(translationHelper, lazy, temp.copyRO()); + // Branch - translationHelper.setReturnValueResolveLazinessAndBranchToFinish( - (X86_Laziness) lazy.clone(), destOp.copyRO()); + translationHelper.appendDynamicJump(destOp.copyRO(), lazy, BranchType.CALL); return -1; } @@ -3917,7 +3918,8 @@ X86_Group4PrefixDecoder prefix4, X86_Group5PrefixDecoder prefix5) { // The destination for the branch int target_address = pc + length + immediate; - // Find a pre-translated version + + OPT_BasicBlock executeBranch = translationHelper.createBlockAfterCurrent(); OPT_BasicBlock fallThrough = translationHelper.createBlockAfterCurrent(); boolean branchLikely; if (prefix2 != null) { @@ -4027,11 +4029,14 @@ .getConditionalBranchProfileOperand(false)); } translationHelper.appendInstruction(boolcmp); - translationHelper.appendIfCmp(INT_IFCMP, translationHelper - .getTempValidation(0), temp.copyRO(), new OPT_IntConstantOperand(0), OPT_ConditionOperand.NOT_EQUAL(), target_address, lazy, likelyOp); + translationHelper.appendInstruction(IfCmp.create(INT_IFCMP, translationHelper.getTempValidation(0), temp.copyRO(), new OPT_IntConstantOperand(0), OPT_ConditionOperand.NOT_EQUAL(), executeBranch.makeJumpTarget(), likelyOp)); OPT_Instruction gotoInstr = Goto.create(GOTO, fallThrough.makeJumpTarget()); translationHelper.appendInstruction(gotoInstr); + + translationHelper.setCurrentBlock(executeBranch); + translationHelper.appendGoto(target_address, lazy, BranchType.DIRECT_BRANCH); + translationHelper.setCurrentBlock(fallThrough); return pc + length; } @@ -4164,7 +4169,7 @@ if (modrm == null) { target_address = absolute ? immediate : pc + length + immediate; translationHelper.getCurrentBlock().deleteNormalOut(); - translationHelper.appendGoto(target_address, lazy); + translationHelper.appendGoto(target_address, lazy, BranchType.DIRECT_BRANCH); } else { int operandSize; if (prefix3 == null) { Modified: src/org/binarytranslator/generic/branch/BranchLogic.java =================================================================== --- src/org/binarytranslator/generic/branch/BranchLogic.java 2007-05-17 19:20:02 UTC (rev 121) +++ src/org/binarytranslator/generic/branch/BranchLogic.java 2007-05-17 22:26:48 UTC (rev 122) @@ -23,6 +23,8 @@ public enum BranchType { INDIRECT_BRANCH, + DIRECT_BRANCH, + CALL, RETURN } @@ -56,12 +58,12 @@ * * @param pc * the address of the branch instruction + * @param dest + * the destination of the branch instruction * @param ret * the address that will be returned to - * @param dest - * the destination of the branch instruction */ - public void registerCall(int pc, int ret, int dest) { + public void registerCall(int pc, int dest, int ret) { ProcedureInformation procedure = procedures.get(dest); if (procedure != null) { @@ -71,6 +73,18 @@ procedures.put(dest, procedure); } } + + /** + * Register a call (branch and link) instruction + * + * @param pc + * the address of the branch instruction + * @param dest + * the destination of the branch instruction + */ + public void registerCall(int pc, int dest) { + registerCall(pc, dest, -1); + } /** * Register a branch to the link register @@ -120,6 +134,21 @@ if (DBT.VerifyAssertions) DBT._assert(type > 0 && type < BranchType.values().length); + //Some branch types require a special registration (calls and returns) + switch (BranchType.values()[type]) { + case CALL: + registerCall(origin, target); + break; + + case RETURN: + registerReturn(origin, target); + break; + + default: + break; + } + + //Perform the general branch registration, too Set<Integer> dests = branchSitesAndDestinations.get(origin); if (dests != null && dests.contains(target)) { Modified: src/org/binarytranslator/generic/decoder/AbstractCodeTranslator.java =================================================================== --- src/org/binarytranslator/generic/decoder/AbstractCodeTranslator.java 2007-05-17 19:20:02 UTC (rev 121) +++ src/org/binarytranslator/generic/decoder/AbstractCodeTranslator.java 2007-05-17 22:26:48 UTC (rev 122) @@ -176,8 +176,6 @@ /** List of unresolved Goto instructions */ private final ArrayList<UnresolvedJumpInstruction> unresolvedGoto; - /** List of unresolved IfCmp instructions */ - private final ArrayList<UnresolvedJumpInstruction> unresolvedIfCmp; /** List of unresolved dynamic jumps */ private final ArrayList<UnresolvedJumpInstruction> unresolvedDynamicJumps; @@ -211,7 +209,6 @@ // Fix up stores unresolvedGoto = new ArrayList<UnresolvedJumpInstruction>(); - unresolvedIfCmp = new ArrayList<UnresolvedJumpInstruction>(); unresolvedDynamicJumps = new ArrayList<UnresolvedJumpInstruction>(); } @@ -237,16 +234,16 @@ if (DBT_Options.resolveBranchesAtOnce) { do { resolveGoto(); - resolveIfCmp(); - } while ((unresolvedGoto.size() != 0) || (unresolvedIfCmp.size() != 0)); + + } while (unresolvedGoto.size() != 0); } else { resolveGoto(); - resolveIfCmp(); + } if (!DBT_Options.resolveProceduresBeforeBranches) { resolveAllDynamicJumpTargets(); } - } while (((unresolvedGoto.size() == 0) && (unresolvedIfCmp.size() == 0) + } while (((unresolvedGoto.size() == 0) && areDynamicJumpsReadyToResolve()) == false); // Resolve unresolved dynamic jumps @@ -520,67 +517,6 @@ } /** - * Appends an IfCmp instruction to the current block that (when the condition is true) jumps - * to the address <code>targetPc</code>. The parameters for this function are mostly - * the same as for the {@link IfCmp#create(OPT_Operator, OPT_RegisterOperand, OPT_Operand, OPT_Operand, OPT_ConditionOperand, OPT_BranchOperand, OPT_BranchProfileOperand)} function. - * - * @param operator - * The HIR operator. - * @param guard - * A guard result for this call. - * @param lhs - * The left-hand-side comparision operator. - * @param rhs - * The right-hand-side comparision operator. - * @param condition - * The type of comparison that is to be executed. - * @param targetPc - * The address that the trace shall jump to, when the condition evaluates to true. - * @param targetLaziness - * The laziness at the point of jump. - * @param branchProfile - * A branch profile operand that describes how likely it is that the jump is executed. - */ - public void appendIfCmp(OPT_Operator operator, OPT_RegisterOperand guard, OPT_Operand lhs, - OPT_Operand rhs, OPT_ConditionOperand condition, int targetPc, Laziness targetLaziness, OPT_BranchProfileOperand branchProfile) { - - OPT_BasicBlock targetBlock = findMapping(targetPc, targetLaziness); - OPT_Instruction ifcmp = IfCmp.create(operator, guard, lhs, rhs, condition, null, branchProfile); - - if (targetBlock != null) { - IfCmp.setTarget(ifcmp, targetBlock.makeJumpTarget()); - getCurrentBlock().insertOut(targetBlock); - } - else { - UnresolvedJumpInstruction unresolvedJump = new UnresolvedJumpInstruction(ifcmp, (Laziness)targetLaziness.clone(), targetPc); - unresolvedIfCmp.add(unresolvedJump); - } - - appendInstruction(ifcmp); - } - - /** Resolve a single ifCmp instruction */ - private void resolveIfCmp() { - - if (unresolvedIfCmp.size() == 0) - return; - - //Try to find if block has now been translated - UnresolvedJumpInstruction instruction = unresolvedIfCmp.remove(unresolvedIfCmp.size() - 1); - - OPT_Instruction ifCmp = instruction.instruction; - - if (DBT.VerifyAssertions) DBT._assert(IfCmp.conforms(ifCmp)); - - //try to resolve the ifcmp's jump target - OPT_BasicBlock targetBB = resolveJumpTarget(instruction.pc, instruction.lazyStateAtJump); - - //Fix up the instruction - IfCmp.setTarget(ifCmp, targetBB.makeJumpTarget()); - ifCmp.getBasicBlock().insertOut(targetBB); - } - - /** * Create a HIR Goto instruction that jumps to the address <code>targetPc</code>. There's * a caveat on using this that there are no other out edges for this BB. * @@ -588,27 +524,50 @@ * The address where we shall jump to. * @param targetLaziness * The current at the point of jump. + * @param branchType + * The type of branch that best describes this jump. */ - public void appendGoto(int targetPC, Laziness targetLaziness) { + public void appendGoto(int targetPC, Laziness targetLaziness, BranchType branchType) { - OPT_Instruction jump; + //see if we already compiled the target address OPT_BasicBlock target = findMapping(targetPC, targetLaziness); + //if yes, just jump directly to it if (target != null) { if (DBT_Options.debugBranchResolution) System.out.println(String.format("Found precompiled mapping for pc 0x%x: %s", targetPC, target)); - jump = Goto.create(GOTO, target.makeJumpTarget()); + OPT_Instruction jump = Goto.create(GOTO, target.makeJumpTarget()); getCurrentBlock().insertOut(target); + appendInstruction(jump); } else { - jump = Goto.create(GOTO, null); - UnresolvedJumpInstruction unresolvedJump = new UnresolvedJumpInstruction(jump, (Laziness)targetLaziness.clone(), targetPC); - unresolvedGoto.add(unresolvedJump); + //otherwise, we have to decide whether to compile that address into the trace or to compile + //it as a separate trace. We use the branchType hint for that... + + switch (branchType) { + case CALL: + //exit the trace on a call + ps.branchInfo.registerCall(currentPC, targetPC); + setReturnValueResolveLazinessAndBranchToFinish(targetLaziness, new OPT_IntConstantOperand(targetPC)); + break; + + case RETURN: + //exit the trace + ps.branchInfo.registerReturn(currentPC, targetPC); + setReturnValueResolveLazinessAndBranchToFinish(targetLaziness, new OPT_IntConstantOperand(targetPC)); + break; + + default: + //compile the jump directly into the trace. + OPT_Instruction jump = Goto.create(GOTO, null); + UnresolvedJumpInstruction unresolvedJump = new UnresolvedJumpInstruction(jump, (Laziness)targetLaziness.clone(), targetPC); + unresolvedGoto.add(unresolvedJump); + } } - appendInstruction(jump); + } /** Resolve a single goto instruction */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |