public boolean next() { if (!m_started) { pushLongestPathFrom(m_root); m_started = true; } else { if (traversalStack.empty()) { throw new RuntimeException("Internal Error: Traversal already finished"); } do { if (traversalStack.empty()) { return false; } final Pair<IZyTreeNode, Integer> parentElement = traversalStack.pop(); final int childrenProcessed = parentElement.second() + 1; if (childrenProcessed > parentElement.first().getChildren().size()) { // The parent node was already processed continue; } else if (childrenProcessed == parentElement.first().getChildren().size()) { // Processing finished => Process the parent node now traversalStack.push( new Pair<IZyTreeNode, Integer>(parentElement.first(), childrenProcessed)); return true; } else { traversalStack.push( new Pair<IZyTreeNode, Integer>(parentElement.first(), childrenProcessed)); pushLongestPathFrom(parentElement.first().getChildren().get(childrenProcessed)); return true; } } while (true); } return !traversalStack.empty(); }
@Override public void translate( final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException { TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "div"); final String sourceRegister1 = instruction.getOperands().get(0).getRootNode().getChildren().get(0).getValue(); final String sourceRegister2 = instruction.getOperands().get(1).getRootNode().getChildren().get(0).getValue(); final OperandSize dw = OperandSize.DWORD; final long baseOffset = ReilHelpers.toReilAddress(instruction.getAddress()).toLong(); long offset = baseOffset; final Pair<String, String> sourceRegister1Abs = Helpers.generateAbs(environment, offset, sourceRegister1, dw, instructions); final String sourceRegister1Absolute = sourceRegister1Abs.second(); offset = baseOffset + instructions.size(); final Pair<String, String> sourceRegister2Abs = Helpers.generateAbs(environment, offset, sourceRegister2, dw, instructions); final String sourceRegister2Absolute = sourceRegister2Abs.second(); offset = baseOffset + instructions.size(); final String divResult = environment.getNextVariableString(); final String modResult = environment.getNextVariableString(); instructions.add( ReilHelpers.createDiv( offset++, dw, sourceRegister1Absolute, dw, sourceRegister2Absolute, dw, divResult)); instructions.add( ReilHelpers.createMod( offset++, dw, sourceRegister1Absolute, dw, sourceRegister2Absolute, dw, modResult)); final String xoredSigns = environment.getNextVariableString(); final String divToggleMask = environment.getNextVariableString(); final String xoredDivResult = environment.getNextVariableString(); // Find out if the two operands had different signs and adjust the result accordingly instructions.add( ReilHelpers.createXor( offset++, dw, sourceRegister1Abs.first(), dw, sourceRegister2Abs.first(), dw, xoredSigns)); instructions.add( ReilHelpers.createSub(offset++, dw, String.valueOf(0L), dw, xoredSigns, dw, divToggleMask)); instructions.add( ReilHelpers.createXor(offset++, dw, divToggleMask, dw, divResult, dw, xoredDivResult)); instructions.add(ReilHelpers.createAdd(offset++, dw, xoredDivResult, dw, xoredSigns, dw, "LO")); final String modToggleMask = environment.getNextVariableString(); final String xoredModResult = environment.getNextVariableString(); instructions.add( ReilHelpers.createSub( offset++, dw, String.valueOf(0L), dw, sourceRegister1Abs.first(), dw, modToggleMask)); instructions.add( ReilHelpers.createXor(offset++, dw, modToggleMask, dw, modResult, dw, xoredModResult)); instructions.add( ReilHelpers.createAdd( offset++, dw, xoredModResult, dw, sourceRegister1Abs.first(), dw, "HI")); }