public override CILInstructionMethod BuildNode(ParseTreeNode node) { CILInstructionMethod result = null; var instrMethodParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.INSTR_METHOD); var callParseTreeNode = instrMethodParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_call); if (callParseTreeNode != null) { result = new CallInstruction(); } var callvirtParseTreeNode = instrMethodParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_callvirt); if (callvirtParseTreeNode != null) { result = new CallVirtualInstruction(); } var newobjParseTreeNode = instrMethodParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_newobj); if (newobjParseTreeNode != null) { result = new NewObjectInstruction(); } if (result != null) { var typeParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.type); result.MethodReturnType = TypeParseTreeNodeHelper.GetType(typeParseTreeNode); var typeSpecParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.typeSpec); result.TypeSpecification = TypeSpecParseTreeNodeHelper.GetValue(typeSpecParseTreeNode); var methodNameParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.methodName); result.MethodName = MethodNameParseTreeNodeHelper.GetMethodName(methodNameParseTreeNode); var sigArgs0ParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.sigArgs0); result.MethodArgumentTypes = SigArgs0ParseTreeNodeHelper.GetTypes(sigArgs0ParseTreeNode); var callConvParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.callConv); result.CallConvention = CallConvParseTreeNodeHelper.GetValue(callConvParseTreeNode); return(result); } throw new ArgumentException("Cannot recognize CIL instruction method."); }
public override void VisitNewObjectInstruction(NewObjectInstruction instruction) { var callExternal = _program.IsExternalType(instruction.TypeSpec); var methodArgs = PopMethodArgs(instruction); if (callExternal) { object thisObject = null; if (instruction.CallConv.IsInstance) { thisObject = GetRuntimeEmptyInstance(instruction, methodArgs, true); } var result = CallExternalMethod(instruction, null, methodArgs.ToArray(), thisObject); var resultVal = instruction.TypeSpec.GetCilType(_program).CreateValueFromRuntime(result, ManagedMemory, _program); ControlState.EvaluationStack.PushValue(resultVal); ControlState.MoveToNextInstruction(); } else { var @class = _program.Classes.Single(c => c.Name.ToString() == instruction.TypeSpec.ClassName.ToString()); var method = @class.Methods.Single(m => m.Name == instruction.MethodName && AreArgumentsAssignable(instruction.SigArgs, m.Arguments)); var emptyInstance = new CilClassInstance(@class, _program, ManagedMemory); var thisRef = ManagedMemory.Store(emptyInstance); ControlState.EvaluationStack.PushValue(thisRef); var sigArgsWithThis = CompleteSigArgs(instruction, method); var argsWithThis = CompleteArgs(instruction, methodArgs, thisRef); var newMethodState = new CilMethodState(method, sigArgsWithThis, argsWithThis, _program); ControlState.MoveToNextInstruction(); ControlState.CallStack.Push(newMethodState); } }
public abstract void VisitNewObjectInstruction(NewObjectInstruction instruction);