// Return instruction to evaluate test to its boolean result, leaving an int32 on the stack. // - beforeState has test operands on stack. // - afterState has the test evaluated AND the resulting int32 consumed. // (These states came from the original brtrue, etc instruction). public CompareInstruction ToCompareInstruction(int offset, MachineState beforeState, MachineState afterState, PointsTo bottom) { var state1 = beforeState.PopPushType(Pops, beforeState.RootEnv.Global.Int32Ref, bottom); var dummy = new BoolRef(); state1.PropogateBackwards(afterState, dummy); beforeState.PropogateBackwards(state1, dummy); var op = default(CompareOp); switch (Op) { case TestOp.True: op = CompareOp.CtruePseudo; break; case TestOp.False: op = CompareOp.CfalsePseudo; break; case TestOp.Equal: op = CompareOp.Ceq; break; case TestOp.NotEqual: op = CompareOp.CnePseudo; break; case TestOp.LessThanOrEqual: op = CompareOp.ClePseudo; break; case TestOp.LessThan: op = CompareOp.Clt; break; case TestOp.GreaterThanOrEqual: op = CompareOp.CgePseudo; break; case TestOp.GreaterThan: op = CompareOp.Cgt; break; default: throw new ArgumentOutOfRangeException(); } return(new CompareInstruction(offset, op, IsUnsigned) { Type = Type, BeforeState = beforeState, AfterState = state1 }); }
// ---------------------------------------------------------------------- // Back propogation // ---------------------------------------------------------------------- // Refine given before machine state account for backwards flow from after machine state. // Only propogates liveness, thus we only need to look for read/writes of arguments and locals, // either directly (via ldarg/starg/ldloc/stloc) or indirectly (via ldind/ldobj/cpobj). public void BackwardInstruction(InstructionContext context, int index, MachineState beforeState, MachineState afterState, BoolRef changed) { var instruction = context.Block.Body[index]; switch (instruction.Flavor) { case InstructionFlavor.Misc: { var misci = (MiscInstruction)instruction; switch (misci.Op) { case MiscOp.LdindRef: beforeState.ReadPointer(afterState, beforeState.PeekPointsTo(0), changed); return; case MiscOp.StindRef: // May have overwritten an arg or local, but don't know exactly which one, so must // be conservative and leave everything alive break; case MiscOp.Nop: case MiscOp.Break: case MiscOp.Dup: case MiscOp.Pop: case MiscOp.Ldnull: case MiscOp.Ckfinite: case MiscOp.Throw: case MiscOp.Rethrow: case MiscOp.LdelemRef: case MiscOp.StelemRef: case MiscOp.Ldlen: case MiscOp.Ret: case MiscOp.RetVal: case MiscOp.Endfilter: case MiscOp.Endfinally: break; default: throw new ArgumentOutOfRangeException(); } break; } case InstructionFlavor.ArgLocal: { var sli = (ArgLocalInstruction)instruction; switch (sli.Op) { case ArgLocalOp.Ld: beforeState.ReadArgLocal(afterState, sli.ArgLocal, sli.Index, changed); return; case ArgLocalOp.Lda: // Assume pointer we are creating is read from, and to be conservative don't // assume it is written to. beforeState.ReadArgLocal(afterState, sli.ArgLocal, sli.Index, changed); break; case ArgLocalOp.St: beforeState.WriteArgLocal(afterState, sli.ArgLocal, sli.Index, changed); return; default: throw new ArgumentOutOfRangeException(); } break; } case InstructionFlavor.Type: { var typei = (TypeInstruction)instruction; switch (typei.Op) { case TypeOp.Ldobj: beforeState.ReadPointer(afterState, beforeState.PeekPointsTo(0), changed); return; case TypeOp.Stobj: // As above, can't be sure which args or locals will be written to break; case TypeOp.Cpobj: // As above, can't be sure which args or locals will be written to // But can handle read safely beforeState.ReadPointer(afterState, beforeState.PeekPointsTo(0), changed); return; case TypeOp.Newarr: case TypeOp.Initobj: case TypeOp.Castclass: case TypeOp.Isinst: case TypeOp.Box: case TypeOp.Unbox: case TypeOp.UnboxAny: case TypeOp.Ldtoken: case TypeOp.Ldelem: case TypeOp.Stelem: break; default: throw new ArgumentOutOfRangeException(); } break; } case InstructionFlavor.Try: { var tryi = (TryInstruction)instruction; var tryContext = new TryBodyInstructionContext(context, index, tryi.Body); BackwardBlock(tryContext, changed); for (var j = 0; j < tryi.Handlers.Count; j++) { var h = tryi.Handlers[j]; var handlerContext = new TryHandlerInstructionContext(context, index, h.Body, j); BackwardBlock(handlerContext, changed); } return; } case InstructionFlavor.Method: { var methi = (MethodInstruction)instruction; switch (methi.Op) { case MethodOp.Call: case MethodOp.Newobj: { // Assume any pointers passed to call are read from var sig = (CST.MethodSignature)methi.Method.ExternalSignature; var skippedArgs = (methi.Op == MethodOp.Newobj ? 1 : 0); var passedArgs = sig.Parameters.Count - skippedArgs; for (var i = passedArgs - 1; i >= 0; i--) { if (sig.Parameters[skippedArgs + i].Style(methEnv) is CST.ManagedPointerTypeStyle) beforeState.ReadPointer (afterState, beforeState.PeekPointsTo(passedArgs - 1 - i), changed); } // Also assume call does not write to any pointers, thus everything remains // alive across call. Ie just fallthough. break; } case MethodOp.Ldftn: case MethodOp.Ldtoken: break; default: throw new ArgumentOutOfRangeException(); } break; } case InstructionFlavor.Unsupported: case InstructionFlavor.Field: case InstructionFlavor.Branch: case InstructionFlavor.Switch: case InstructionFlavor.Compare: case InstructionFlavor.LdElemAddr: case InstructionFlavor.LdInt32: case InstructionFlavor.LdInt64: case InstructionFlavor.LdSingle: case InstructionFlavor.LdDouble: case InstructionFlavor.LdString: case InstructionFlavor.Arith: case InstructionFlavor.Conv: case InstructionFlavor.IfThenElsePseudo: case InstructionFlavor.ShortCircuitingPseudo: case InstructionFlavor.StructuralSwitchPseudo: case InstructionFlavor.LoopPseudo: case InstructionFlavor.WhileDoPseudo: case InstructionFlavor.DoWhilePseudo: case InstructionFlavor.LoopControlPseudo: break; default: throw new ArgumentOutOfRangeException(); } // By default, anything alive in after state must be alive in before state. beforeState.PropogateBackwards(afterState, changed); }