private void DecodeInstructionTargets() { bool branched = false; for (int i = 0; i < MethodCompiler.Method.Code.Count; i++) { instruction = MethodCompiler.Method.Code[i]; if (branched) { GetBlockByLabel(instruction.Offset); branched = false; } var op = (OpCode)instruction.OpCode; var cil = CILInstruction.Get(op); ++counts[(int)op]; branched = cil.DecodeTargets(this); } }
/// <summary> /// Decodes the instruction stream of the reader and populates the compiler. /// </summary> /// <param name="compiler">The compiler to populate.</param> /// <exception cref="InvalidMetadataException"></exception> private void Decode(BaseMethodCompiler compiler) { // Create context Context context = new Context(InstructionSet); // Prefix instruction bool prefix = false; for (int i = 0; i < MethodCompiler.Method.Code.Count; i++) { MosaInstruction instr = MethodCompiler.Method.Code[i]; var op = (OpCode)instr.OpCode; BaseCILInstruction instruction = CILInstruction.Get(op); if (instruction == null) { throw new InvalidMetadataException(); } // Create and initialize the corresponding instruction context.AppendInstruction(instruction); context.Label = instr.Offset; context.HasPrefix = prefix; this.instruction = instr; instruction.Decode(context, this); Debug.Assert(context.Instruction != null); prefix = (instruction is PrefixInstruction); } }
/// <summary> /// Decodes the instruction stream of the reader and populates the compiler. /// </summary> /// <exception cref="InvalidMetadataException"></exception> private void DecodeInstructions() { block = null; // Prefix instruction bool prefix = false; for (int i = 0; i < MethodCompiler.Method.Code.Count; i++) { instruction = MethodCompiler.Method.Code[i]; block = BasicBlocks.GetByLabel(instruction.Offset) ?? block; var op = (OpCode)instruction.OpCode; var cil = CILInstruction.Get(op); if (cil == null) { throw new InvalidMetadataException(); } // Create and initialize the corresponding instruction var node = new InstructionNode(); node.Label = instruction.Offset; node.HasPrefix = prefix; node.Instruction = cil; block.BeforeLast.Insert(node); cil.Decode(node, this); prefix = (cil is PrefixInstruction); instructionCount++; bool addjmp = false; var flow = node.Instruction.FlowControl; if (flow == FlowControl.Next || flow == FlowControl.Call || flow == FlowControl.ConditionalBranch || flow == FlowControl.Switch) { var nextInstruction = MethodCompiler.Method.Code[i + 1]; if (BasicBlocks.GetByLabel(nextInstruction.Offset) != null) { var target = GetBlockByLabel(nextInstruction.Offset); var jmpNode = new InstructionNode(IRInstruction.Jmp, target); jmpNode.Label = instruction.Offset; block.BeforeLast.Insert(jmpNode); } } } }