public override CILClassField BuildNode(ParseTreeNode node) { var result = new CILClassField(); var idParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.id); var fieldName = IdParseTreeNodeHelper.GetValue(idParseTreeNode); result.Name = fieldName; var typeParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.type); var fieldType = TypeParseTreeNodeHelper.GetType(typeParseTreeNode); result.Type = fieldType; var fieldAttrParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.fieldAttr); result.Attributes = FieldAttrParseTreeNodeHelper.GetAllAttributes(fieldAttrParseTreeNode); var initOptParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.initOpt); var fieldInitParseTreeNode = initOptParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.fieldInit); if (fieldInitParseTreeNode != null) { var int64ParseTreeNode = fieldInitParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.int64); var fieldInitValue = Int64ParseTreeNodeHelper.GetValue(int64ParseTreeNode); result.InitValue = fieldInitValue; } return(result); }
public override CILInstructionField BuildNode(ParseTreeNode node) { CILInstructionField result = null; var instrFieldParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.INSTR_FIELD); var ldfldParseTreeNode = instrFieldParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldfld); if (ldfldParseTreeNode != null) { result = new LoadFieldInstruction(); } var ldfldaParseTreeNode = instrFieldParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldflda); if (ldfldaParseTreeNode != null) { result = new LoadFieldAddressInstruction(); } var ldsfldParseTreeNode = instrFieldParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldsfld); if (ldsfldParseTreeNode != null) { result = new LoadStaticFieldInstruction(); } var stfldParseTreeNode = instrFieldParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stfld); if (stfldParseTreeNode != null) { result = new SetFieldInstruction(); } var stsfldParseTreeNode = instrFieldParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stsfld); if (stsfldParseTreeNode != null) { result = new SetStaticFieldInstruction(); } if (result != null) { var typeParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.type); result.FieldType = TypeParseTreeNodeHelper.GetType(typeParseTreeNode); var typeSpecParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.typeSpec); result.FieldOwnerTypeSpecification = TypeSpecParseTreeNodeHelper.GetValue(typeSpecParseTreeNode); var idParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.id); result.FieldName = IdParseTreeNodeHelper.GetValue(idParseTreeNode); return(result); } throw new ArgumentException("Cannot recognize CIL instruction field."); }
public override CILInstructionVar BuildNode(ParseTreeNode node) { var instrVarParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.INSTR_VAR); CILInstructionVar result = null; var ldargsParseTreeNode = instrVarParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldargs); if (ldargsParseTreeNode != null) { result = new LoadArgumentShortInstruction(); } var ldlocasParseTreeNode = instrVarParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldlocas); if (ldlocasParseTreeNode != null) { result = new LoadLocalVariableAddressShortInstruction(); } var ldlocsParseTreeNode = instrVarParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_ldlocs); if (ldlocsParseTreeNode != null) { result = new LoadLocalVariableShortInstruction(); } var stlocsParseTreeNode = instrVarParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_stlocs); if (stlocsParseTreeNode != null) { result = new SetLocalVariableShortInstruction(); } if (result != null) { var idParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.id); result.VariableId = IdParseTreeNodeHelper.GetValue(idParseTreeNode); return(result); } throw new ArgumentException("Cannot recognize CIL instruction var."); }
public override CILInstructionBranchTarget BuildNode(ParseTreeNode node) { var instructionBranchTargetParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.INSTR_BRTARGET); CILInstructionBranchTarget result = null; var beqsParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_beqs); if (beqsParseTreeNode != null) { result = new BranchOnEqualShortInstruction(); } var bgesParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_bges); if (bgesParseTreeNode != null) { result = new BranchOnGreaterOrEqualShortInstruction(); } var blesParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_bles); if (blesParseTreeNode != null) { result = new BranchOnLessOrEqualShortInstruction(); } var bltsParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_blts); if (bltsParseTreeNode != null) { result = new BranchOnLessShortInstruction(); } var bneunsParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_bneuns); if (bneunsParseTreeNode != null) { result = new BranchOnNotEqualOrUnorderedShortInstruction(); } var brParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_br); if (brParseTreeNode != null) { result = new BranchInstruction(); } var brfalsesParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_brfalses); if (brfalsesParseTreeNode != null) { result = new BranchOnFalseShortInstruction(); } var brsParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_brs); if (brsParseTreeNode != null) { result = new BranchShortInstruction(); } var brtruesParseTreeNode = instructionBranchTargetParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_brtrues); if (brtruesParseTreeNode != null) { result = new BranchOnTrueShortInstruction(); } if (result != null) { var idParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.id); result.Label = IdParseTreeNodeHelper.GetValue(idParseTreeNode); return(result); } throw new ArgumentException("Cannot recognize CIL instruction branch target."); }
public override CILMethod BuildNode(ParseTreeNode node) { var instructions = new List <CILInstruction>(); var instructionsLabels = new List <string>(); var isEntryPoint = false; var localsTypes = new List <CILType>(); var locals = new OrderedDictionary(); var localsAddresses = new List <Guid>(); var methodName = string.Empty; CILType returnType = null; CILCallConvention callConvention = null; var methodHeadParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.methodHead); var methodNameParseTreeNode = methodHeadParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.methodName); methodName = MethodNameParseTreeNodeHelper.GetMethodName(methodNameParseTreeNode); var headSigArgs0ParseTreeNode = methodHeadParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.sigArgs0); var argumentTypes = SigArgs0ParseTreeNodeHelper.GetTypes(headSigArgs0ParseTreeNode); var argumentNames = SigArgs0ParseTreeNodeHelper.GetNames(headSigArgs0ParseTreeNode); var callConvParseTreeNode = methodHeadParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.callConv); callConvention = CallConvParseTreeNodeHelper.GetValue(callConvParseTreeNode); var headTypeParseTreeNode = methodHeadParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.type); returnType = TypeParseTreeNodeHelper.GetType(headTypeParseTreeNode); var methodDeclsParseTreeNode = node.GetFirstChildWithGrammarName(GrammarNames.methodDecls); while (methodDeclsParseTreeNode != null) { var methodDeclParseTreeNode = methodDeclsParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.methodDecl); var instrParseTreeNode = methodDeclParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.instr); if (instrParseTreeNode != null) { var instruction = _instructionBuilder.BuildNode(instrParseTreeNode); instructions.Add(instruction); } var idParseTreeNode = methodDeclParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.id); if (idParseTreeNode != null) { var label = IdParseTreeNodeHelper.GetValue(idParseTreeNode); instructionsLabels.Add(label); } var dotEntrypointParseTreeNode = methodDeclParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.keyword_dotEntrypoint); if (dotEntrypointParseTreeNode != null) { isEntryPoint = true; } var localsHeadParseTreeNode = methodDeclParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.localsHead); if (localsHeadParseTreeNode != null) { var sigArgs0ParseTreeNode = methodDeclParseTreeNode?.GetFirstChildWithGrammarName(GrammarNames.sigArgs0); localsTypes = SigArgs0ParseTreeNodeHelper.GetTypes(sigArgs0ParseTreeNode); locals = SigArgs0ParseTreeNodeHelper.GetLocalsDictionary(sigArgs0ParseTreeNode); localsAddresses = localsTypes.Select(x => Guid.NewGuid()).ToList(); } methodDeclsParseTreeNode = methodDeclsParseTreeNode.GetFirstChildWithGrammarName(GrammarNames.methodDecls); } instructions.Reverse(); instructionsLabels.Reverse(); var result = new CILMethod { ArgumentTypes = argumentTypes, Instructions = instructions, InstructionsLabels = instructionsLabels, IsEntryPoint = isEntryPoint, LocalsTypes = localsTypes, LocalsAddresses = localsAddresses, Locals = locals, MethodName = methodName, CallConvention = callConvention, ReturnType = returnType, ArgumentNames = argumentNames }; foreach (var instruction in instructions) { instruction.ParentMethod = result; } return(result); }