protected override void VisitLoadFieldInstruction(LoadFieldInstruction instruction)
        {
            ControlState.EvaluationStack.PopValue(_program, instruction.ClassTypeSpec.GetCilType(_program), out var instanceVal);

            CilClassInstance classInstance = null;

            if (instanceVal is CilValueValueType instanceValValueType)
            {
                classInstance = instanceValValueType.Value;
            }
            else if (instanceVal is CilValueReference instanceValReference)
            {
                classInstance = ManagedMemory.Load(instanceValReference) as CilClassInstance;
            }
            else
            {
                throw new System.NotImplementedException();
            }

            var value = classInstance.Fields[instruction.FieldId];

            ControlState.EvaluationStack.PushValue(value);

            ControlState.MoveToNextInstruction();
        }
        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.");
        }
        private static Instruction GetLoadField(FieldInfo field)
        {
            lock (s_loadFields)
            {
                Instruction instruction;
                if (!s_loadFields.TryGetValue(field, out instruction))
                {
                    if (field.IsStatic)
                    {
                        instruction = new LoadStaticFieldInstruction(field);
                    }
                    else
                    {
                        instruction = new LoadFieldInstruction(field);
                    }

                    s_loadFields.Add(field, instruction);
                }

                return(instruction);
            }
        }
示例#4
0
 protected abstract void VisitLoadFieldInstruction(LoadFieldInstruction instruction);