summaryrefslogtreecommitdiff
path: root/tests/src/JIT/CodeGenBringUpTests/RecursiveTailCall.cs
blob: a38b34d799cc2f81092ba64d5ebf255a694b7ad0 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

using System;
using System.Runtime.CompilerServices;

public struct Struct1
{
    public bool bfield;

    public Struct1(bool b)
    {
        bfield = b;
    }
}

public struct Struct8
{
    public double dfield;

    public Struct8(double d)
    {
        dfield = d;
    }
}

class GenericClass<T> { }
class GenericException<T> : Exception
{
}

public class Test
{
    // Test a recursive tail call with a 1-byte struct parameter.
    static bool TestStruct1Param(Struct1 str1, int count)
    {
        Console.WriteLine(count);
        Console.WriteLine(count);
        Console.WriteLine(count);
        Console.WriteLine(count);
        if (!CheckStruct1(str1))
        {
            return false;
        }

        if (count <= 0)
        {
            return true;
        }

        return TestStruct1Param(str1, count - 1);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool CheckStruct1(Struct1 str1)
    {
        return str1.bfield;
    }

    // Test a recursive tail call with an 8-byte struct parameter.
    static bool TestStruct8Param(Struct8 str8, int count)
    {
        Console.WriteLine(count);
        Console.WriteLine(count);
        Console.WriteLine(count);
        Console.WriteLine(count);
        if (!CheckStruct8(str8))
        {
            return false;
        }

        if (count <= 0)
        {
            return true;
        }

        return TestStruct8Param(str8, count - 1);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool CheckStruct8(Struct8 str8)
    {
        return (str8.dfield == 1.0);
    }

    // Test a recursive tail call to a method with generic sharing.
    bool TestGenericSharing<T>()
    {
        if (typeof(T) == typeof(string))
        {
            return true;
        }
        else
        {
            return TestGenericSharing<string>();
        }
    }

    // Test a recursive tail call to a method with hidden generic context param.
    // This test will make sure that when a recursive call is converte to a loop
    // there is no mismatch of generic context reported to VM and the one used
    // within the method.
    public static int TestGenericContext<T>(int x)
    {
        try
        {
            if (x == 1) throw new GenericException<T>();
        }
        catch (GenericException<T>)
        {            
            return 1;
        }

        return x * TestGenericContext<GenericClass<T>>(x - 1);
    }

    // Test a recursive tail call to a method that has a 'this' parameter
    // and a parameter passed on the stack.
    bool TestStackParam(int i1, int i2, int i3, int i4)
    {
        if ((i3 != 5) || (i4 != 7))
        {
            return false;
        }
        if (i1 == 0)
        {
            return true;
        }
        return TestStackParam(i1 - 1, i2, i3, i4);
    }

    public static int Main()
    {
        const int Pass = 100;
        const int Fail = -1;

        Struct1 str1 = new Struct1(true);

        if (!TestStruct1Param(str1, 4))
        {
            return Fail;
        }

        Struct8 str8 = new Struct8(1.0);

        if (!TestStruct8Param(str8, 4))
        {
            return Fail;
        }

        Test test = new Test();

        if (!test.TestGenericSharing<object>())
        {
            return Fail;
        }

        if (!test.TestStackParam(1, 0, 5, 7))
        {
            return Fail;
        }

        if (TestGenericContext<GenericClass<int>>(5) != 120)
        {
           return Fail;
        }

        return Pass;
    }
}