summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/Chapter09Test.java
blob: d4d02aa99aa33d0e096a9b4e3574d5d113c63162 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.*;

public class Chapter09Test {

    @Test
    @Ignore
    public void testJig() {
        CodeGen code = new CodeGen("""
int v0=0;
arg=0;
while(v0) {
        while(1) if(arg*arg*0==0) {}
                while(0) {}
    arg=1;
}
return 0;
                """);
        code.parse().opto();
    }

    @Test
    public void testGVN1() {
        CodeGen code = new CodeGen(
                """
int x = arg + arg;
if(arg < 10) {
    return arg + arg;
}
else {
    x = x + 1;
}
return x;
                """);
        code.parse().opto();
        assertEquals("return Phi(Region,(arg<<1),(Shl+1));", code.print());
        Assert.assertEquals( "2", Eval2.eval(code,  1));
        Assert.assertEquals("23", Eval2.eval(code, 11));
    }

    @Test
    public void testGVN2() {
        CodeGen code = new CodeGen("return arg*arg-arg*arg;");
        code.parse().opto();
        assertEquals("return 0;", code.print());
        Assert.assertEquals("0", Eval2.eval(code,  1, 1));
    }

    @Test
    public void testWorklist1() {
        CodeGen code = new CodeGen("""
int step = 1;
while (arg < 10) {
    arg = arg + step + 1;
}
return arg;
""");
        code.parse().opto();
        assertEquals("return Phi(Loop,arg,(Phi_arg+2));", code.print());
        Assert.assertEquals("11", Eval2.eval(code,  1));
    }

    @Test
    public void testWorklist2() {
        CodeGen code = new CodeGen(
                """
int cond = 0;
int one = 1;
while (arg < 10) {
    if (cond) one = 2;
    arg = arg + one*3 + 1;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return Phi(Loop,arg,(Phi_arg+4));", code.print());
        Assert.assertEquals("13", Eval2.eval(code,  1));
    }

    @Test
    public void testWorklist3() {
        CodeGen code = new CodeGen(
                """
int v1 = 0;
int v2 = 0;
int v3 = 0;
int v4 = 0;
int v5 = 0;
int v6 = 0;
int v7 = 0;
int v8 = 0;
while (arg) {
    if (v1) v2 = 1;
    if (v2) v3 = 1;
    if (v3) v4 = 1;
    if (v4) v5 = 1;
    if (v5) v6 = 1;
    if (v6) v7 = 1;
    if (v7) v8 = 1;
    arg = arg + v8 + 1;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testRegionPeepBug() {
        CodeGen code = new CodeGen(
                """
int v0=0;
int v1=0;
while(v1+arg) {
    arg=0;
    int v2=v0;
    while(arg+1) {}
    v0=1;
    v1=v2;
}
                """);
        code.parse().opto();
        assertEquals("return Top;", code.print());
    }

    @Test
    public void testWhile0() {
        CodeGen code = new CodeGen("while(0) continue; if(0) arg=0; return arg;");
        code.parse().opto();
        assertEquals("return arg;", code.print());
    }

    @Test
    public void testWhile1() {
        CodeGen code = new CodeGen("""
if(0) while(0) {
    int arg=arg;
    while(0) {}
}
return 0;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }


    @Test
    public void testPrecedence() {
        CodeGen code = new CodeGen("return 3-1+2;");
        code.parse().opto();
        assertEquals("return 4;", code.print());
    }

    @Test
    public void testSwap2() {
        CodeGen code = new CodeGen("return 1+(1+1);");
        code.parse().opto();
        assertEquals("return 3;", code.print());
    }


    @Test
    public void testFuzz0() {
        CodeGen code = new CodeGen("""
int one = 1;
int a = 0;
int zero = 0;
while(arg) {
    a = -(one + a + 2);
    arg = arg + 1;
    one = one + zero;
}
return a;
""");
        code.parse().opto();
        assertEquals("return Phi(Loop,0,(-(Phi_a+3)));", code.print());
    }

    @Test
    public void testFuzz1() {
        CodeGen code = new CodeGen("""
while(1) {}
while(arg) break;
while(arg) arg=0;
arg=0;
int v0=0!=0<-0;
return -0+0+0;
""");
        code.parse().opto();
        assertEquals("return Top;", code.print());
    }


    @Test
    public void testFuzz2() {
        CodeGen code = new CodeGen("return 0+-0;");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testFuzz3() {
        CodeGen code = new CodeGen("int v0=0; while(0==69) while(v0) return 0;");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testFuzz4() {
        CodeGen code = new CodeGen("""
while(1) {
    arg=0<=0;
    if(1<0) while(arg==-0) arg=arg-arg;
}
""");
        code.parse().opto();
        assertEquals("return Top;", code.print());
    }

    @Test
    public void testFuzz5() {
        CodeGen code = new CodeGen("""
{
    int v0=0;
    while(1)
            int v1=0--0;
    while(v0)
        break;
    while(-v0) {
        while(0+0+v0) continue;
        break;
    }
    if(-0!=-0+0+v0) while(0+0+0+0)
                break;
}
return 0!=0;
""");
        code.parse().opto();
        assertEquals("return Top;", code.print());
    }

    @Test
    public void testFuzz6() {
        CodeGen code = new CodeGen(
"""
int v0=0;
while(0==1) while(v0)
        v0=1+v0;
""");
        code.parse().opto();
        assertEquals("Stop[ ]", code.print());
    }

    @Test
    public void testFuzz7() {
        CodeGen code = new CodeGen("""
while(1) {}
int v0=0;
while(v0)
    {}
int v1=0;
while(1)
        v1=1;
return v1+v0;
                                   """);
        code.parse().opto();
        assertEquals("return Top;", code.print());
    }

    @Test
    public void testFuzz8() {
        CodeGen code = new CodeGen("while(arg) arg = arg - 1;  return arg;");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testMeet() {
        Type t1 = Type.TOP;
        Type t2 = TypeInteger.TOP;
        Assert.assertEquals(TypeInteger.TOP, t1.meet(t2));
        Assert.assertEquals(TypeInteger.TOP, t2.meet(t1));
        t1 = Type.BOTTOM;
        t2 = TypeInteger.BOT;
        Assert.assertEquals(Type.BOTTOM, t1.meet(t2));
        Assert.assertEquals(Type.BOTTOM, t2.meet(t1));
    }

}