Exemplo n.º 1
0
        private void WeavePropertySetter(MethodDefinition setter, MethodDefinition propertyGet)
        {
            setter.Body.InitLocals = true;
            setter.Body.SimplifyMacros();

            Instruction firstInstruction = setter.Body.Instructions.First();
            ILProcessor processor        = setter.Body.GetILProcessor();

            // Add local variables
            int cacheKeyIndex = setter.AddVariable <string>();

            // Generate CacheKeyTemplate
            string cacheKey = CreateCacheKeyString(setter);

            Instruction current = firstInstruction.Prepend(processor.Create(OpCodes.Ldstr, cacheKey), processor);

            // Create set cache key
            current = current.AppendStloc(processor, cacheKeyIndex);

            current = InjectCacheKeyCreatedCode(setter, current, processor, cacheKeyIndex);

            if (LogDebugOutput)
            {
                current = current.AppendDebugWrite(processor, "Clearing cache.", ModuleDefinition);
            }

            if (!setter.IsStatic)
            {
                current = current.AppendLdarg(processor, 0);
            }

            current.Append(processor.Create(OpCodes.Call, setter.Module.Import(propertyGet)), processor)
            .AppendLdloc(processor, cacheKeyIndex)
            .Append(
                processor.Create(OpCodes.Callvirt,
                                 setter.Module.Import(CacheTypeGetRemoveMethod(propertyGet.ReturnType.Resolve(), CacheTypeRemoveMethodName))),
                processor);

            setter.Body.OptimizeMacros();
        }
Exemplo n.º 2
0
        private void WeaveMethod(MethodDefinition methodDefinition, MethodDefinition propertyGet)
        {
            methodDefinition.Body.InitLocals = true;

            methodDefinition.Body.SimplifyMacros();

            Instruction firstInstruction = methodDefinition.Body.Instructions.First();

            ICollection <Instruction> returnInstructions =
                methodDefinition.Body.Instructions.ToList().Where(x => x.OpCode == OpCodes.Ret).ToList();

            if (returnInstructions.Count == 0)
            {
                LogWarning(string.Format("Method {0} does not contain any return statement. Skip weaving of method {0}.",
                                         methodDefinition.Name));
                return;
            }

            // Add local variables
            int cacheKeyIndex    = methodDefinition.AddVariable <string>();
            int resultIndex      = methodDefinition.AddVariable(methodDefinition.ReturnType);
            int objectArrayIndex = methodDefinition.AddVariable <object[]>();

            ILProcessor processor = methodDefinition.Body.GetILProcessor();

            // Generate CacheKeyTemplate
            string cacheKey = CreateCacheKeyString(methodDefinition);

            Instruction current = firstInstruction.Prepend(processor.Create(OpCodes.Ldstr, cacheKey), processor);

            current = SetCacheKeyLocalVariable(current, methodDefinition, processor, cacheKeyIndex, objectArrayIndex);

            current = InjectCacheKeyCreatedCode(methodDefinition, current, processor, cacheKeyIndex);

            TypeDefinition propertyGetReturnTypeDefinition = propertyGet.ReturnType.Resolve();

            if (!methodDefinition.IsStatic)
            {
                current = current.AppendLdarg(processor, 0);
            }

            MethodReference methodReferenceContain =
                methodDefinition.Module.Import(CacheTypeGetContainsMethod(propertyGetReturnTypeDefinition,
                                                                          CacheTypeContainsMethodName));

            current =
                current.Append(processor.Create(OpCodes.Call, methodDefinition.Module.Import(propertyGet)), processor)
                .AppendLdloc(processor, cacheKeyIndex)
                .Append(processor.Create(OpCodes.Callvirt, methodReferenceContain), processor)
                .Append(processor.Create(OpCodes.Brfalse, firstInstruction), processor);

            // False branche (store value in cache of each return instruction)
            foreach (Instruction returnInstruction in returnInstructions)
            {
                returnInstruction.Previous.AppendStloc(processor, resultIndex);

                if (LogDebugOutput)
                {
                    returnInstruction.Previous.AppendDebugWrite(processor, "Storing to cache.", methodDefinition.Module);
                }

                if (!methodDefinition.IsStatic)
                {
                    returnInstruction.Previous.AppendLdarg(processor, 0);
                }

                MethodReference methodReferenceReturn =
                    methodDefinition.Module.Import(CacheTypeGetStoreMethod(propertyGetReturnTypeDefinition, CacheTypeStoreMethodName));

                returnInstruction.Previous.Append(processor.Create(OpCodes.Call, methodDefinition.Module.Import(propertyGet)),
                                                  processor)
                .AppendLdloc(processor, cacheKeyIndex)
                .AppendLdloc(processor, resultIndex)
                .AppendBoxIfNecessary(processor, methodDefinition.ReturnType)
                .Append(processor.Create(OpCodes.Callvirt, methodReferenceReturn), processor)
                .AppendLdloc(processor, resultIndex);
            }

            if (LogDebugOutput)
            {
                current = current.AppendDebugWrite(processor, "Loading from cache.", methodDefinition.Module);
            }

            if (!methodDefinition.IsStatic)
            {
                current = current.AppendLdarg(processor, 0);
            }

            // Start of branche true
            MethodReference methodReferenceRetrieve =
                methodDefinition.Module.Import(CacheTypeGetRetrieveMethod(propertyGetReturnTypeDefinition,
                                                                          CacheTypeRetrieveMethodName)).MakeGeneric(new[] { methodDefinition.ReturnType });

            current.Append(processor.Create(OpCodes.Call, methodDefinition.Module.Import(propertyGet)), processor)
            .AppendLdloc(processor, cacheKeyIndex)
            .Append(processor.Create(OpCodes.Callvirt, methodReferenceRetrieve), processor)
            .AppendStloc(processor, resultIndex)
            .Append(processor.Create(OpCodes.Br, returnInstructions.Last().Previous), processor);

            methodDefinition.Body.OptimizeMacros();
        }