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 protected void translateCore( final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException { final IOperandTreeNode registerOperand1 = instruction.getOperands().get(0).getRootNode().getChildren().get(0); final IOperandTreeNode shifter = instruction.getOperands().get(1).getRootNode(); final String sourceRegister = (registerOperand1.getValue()); final OperandSize bt = OperandSize.BYTE; final OperandSize wd = OperandSize.WORD; final OperandSize dw = OperandSize.DWORD; final OperandSize qw = OperandSize.QWORD; long baseOffset = (instruction.getAddress().toLong() * 0x100) + instructions.size(); final String tmpVar1 = environment.getNextVariableString(); final String aluOut = environment.getNextVariableString(); // compute <shifter_operand> final Pair<String, String> shifterPair = AddressingModeOneGenerator.generate( baseOffset, environment, instruction, instructions, shifter); baseOffset = (instruction.getAddress().toLong() * 0x100) + instructions.size(); final String shifterOperand = shifterPair.first(); instructions.add( ReilHelpers.createAdd(baseOffset++, dw, sourceRegister, dw, shifterOperand, qw, tmpVar1)); instructions.add( ReilHelpers.createAnd( baseOffset++, qw, tmpVar1, dw, String.valueOf(0xFFFFFFFFL), dw, aluOut)); // match the case where we have to set flags this does not handle the S == 1 and Rd == R15 case // !!! final String tmpVar3 = environment.getNextVariableString(); final String tmpVar4 = environment.getNextVariableString(); final String msbVar1 = environment.getNextVariableString(); final String msbVar2 = environment.getNextVariableString(); // N Flag Rd[31] instructions.add( ReilHelpers.createBsh(baseOffset++, qw, tmpVar1, dw, String.valueOf(-31L), bt, tmpVar3)); instructions.add( ReilHelpers.createAnd(baseOffset++, bt, tmpVar3, bt, String.valueOf(1L), bt, "N")); // Z Flag if Rd == 0 then 1 else 0 instructions.add(ReilHelpers.createBisz(baseOffset++, dw, aluOut, bt, "Z")); // C Flag CarryFrom(Rn + shifter_operand) instructions.add( ReilHelpers.createBsh(baseOffset++, qw, tmpVar1, wd, String.valueOf(-32L), bt, tmpVar4)); instructions.add( ReilHelpers.createAnd(baseOffset++, bt, tmpVar4, bt, String.valueOf(1L), bt, "C")); // V Flag OverflowFrom(Rn + shifter_operand) instructions.add( ReilHelpers.createBsh( baseOffset++, dw, shifterOperand, wd, String.valueOf(-31L), bt, msbVar1)); instructions.add( ReilHelpers.createBsh( baseOffset++, dw, sourceRegister, wd, String.valueOf(-31L), bt, msbVar2)); // ( msbA XOR msbR ) AND ( msbB XOR msbR ) == OF instructions.add(ReilHelpers.createXor(baseOffset++, bt, msbVar1, bt, "N", bt, tmpVar4)); instructions.add(ReilHelpers.createXor(baseOffset++, bt, msbVar2, bt, "N", bt, tmpVar3)); instructions.add(ReilHelpers.createAnd(baseOffset++, bt, tmpVar4, bt, tmpVar3, bt, "V")); }
@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")); }