Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StreamIntrinsics.cs « Stubs « IL « TypeSystem « Common « tools « coreclr « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc28505f84ced3ca5fc60bb72a55b1c96a668e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Internal.TypeSystem;

using Debug = System.Diagnostics.Debug;

namespace Internal.IL.Stubs
{
    /// <summary>
    /// Provides method bodies for System.IO.Stream intrinsics.
    /// </summary>
    public static class StreamIntrinsics
    {
        public static MethodIL EmitIL(MethodDesc method)
        {
            Debug.Assert(((MetadataType)method.OwningType).Name == "Stream");

            bool isRead = method.Name == "HasOverriddenBeginEndRead";
            if (!isRead && method.Name != "HasOverriddenBeginEndWrite")
                return null;

            TypeDesc streamClass = method.OwningType;
            MethodDesc beginMethod = streamClass.GetMethod(isRead ? "BeginRead" : "BeginWrite", null);
            MethodDesc endMethod = streamClass.GetMethod(isRead ? "EndRead" : "EndWrite", null);

            ILEmitter emitter = new ILEmitter();
            ILCodeStream codestream = emitter.NewCodeStream();

            ILCodeLabel lOverriden = emitter.NewCodeLabel();

            ILToken beginMethodToken = emitter.NewToken(beginMethod);
            codestream.EmitLdArg(0);
            codestream.Emit(ILOpcode.ldvirtftn, beginMethodToken);
            codestream.Emit(ILOpcode.ldftn, beginMethodToken);
            codestream.Emit(ILOpcode.bne_un, lOverriden);

            ILToken endMethodToken = emitter.NewToken(endMethod);
            codestream.EmitLdArg(0);
            codestream.Emit(ILOpcode.ldvirtftn, endMethodToken);
            codestream.Emit(ILOpcode.ldftn, endMethodToken);
            codestream.Emit(ILOpcode.bne_un, lOverriden);

            codestream.EmitLdc(0);
            codestream.Emit(ILOpcode.ret);

            codestream.EmitLabel(lOverriden);
            codestream.EmitLdc(1);
            codestream.Emit(ILOpcode.ret);

            return emitter.Link(method);
        }
    }
}