summaryrefslogtreecommitdiff
path: root/chapter21/src/test/java/com/seaofnodes/simple/Chapter14Test.java
blob: f0fe7dcd5145635161509c1b4b0c3538b0ecf10c (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
package com.seaofnodes.simple;

import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.node.StopNode;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class Chapter14Test {

    @Ignore
    @Test
    public void testJig() {
        CodeGen code = new CodeGen(
"""
return 3.14;
""");
        code.parse().opto();
        assertEquals("return 3.14;", code.print());
        assertEquals("3.14", Eval2.eval(code,  0));
    }

    @Test
    public void testRange() {
        CodeGen code = new CodeGen(
"""
int b;
if( arg ) b=1; else b=0;
int c = 99;
if( b < 0 ) c = -1;
if( b > 2 ) c =  1;
return c;
""");
        code.parse().opto();
        assertEquals("return 99;", code.print());
        assertEquals("99", Eval2.eval(code,  0));
    }

    @Test
    public void testU8() {
        CodeGen code = new CodeGen(
"""
u8 b = 123;
b = b + 456;// Truncate
return b;
""");
        code.parse().opto();
        assertEquals("return 67;", code.print());
        assertEquals("67", Eval2.eval(code,  0));
    }


    @Test
    public void testU8While() {
        CodeGen code = new CodeGen(
"""
u8 b = 123;
while( b ) b = b + 456;// Truncate
return b;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testU1() {
        CodeGen code = new CodeGen(
"""
bool b = 123;
b = b + 456;// Truncate
u1 c = b;   // No more truncate needed
return c;
""");
        code.parse().opto();
        assertEquals("return 1;", code.print());
        assertEquals("1", Eval2.eval(code,  0));
    }

    @Test
    public void testAnd() {
        CodeGen code = new CodeGen(
"""
int b = 123;
b = b+456 & 31;                 // Precedence
return b;
""");
        code.parse().opto();
        assertEquals("return 3;", code.print());
        assertEquals("3", Eval2.eval(code,  0));
    }

    @Test
    public void testRefLoad() {
        CodeGen code = new CodeGen(
"""
struct Foo { u1 b; };
!Foo !f = new Foo;
f.b = 123;
return f.b;
""");
        code.parse().opto();
        assertEquals("return 1;", code.print());
        assertEquals("1", Eval2.eval(code,  0));
    }

    @Test
    public void testSigned() {
        CodeGen code = new CodeGen(
"""
i8 b = 255;                     // Chopped
return b;                       // Sign extend
""");
        code.parse().opto();
        assertEquals("return -1;", code.print());
        assertEquals("-1", Eval2.eval(code,  0));
    }

    @Test
    public void testI8() {
        CodeGen code = new CodeGen(
"""
i8 b = arg;
b = b + 1;// Truncate
return b;
""");
        code.parse().opto();
        assertEquals("return (((((arg<<56)>>56)+1)<<56)>>56);", code.print());
        assertEquals("1", Eval2.eval(code, 0));
        assertEquals("-128", Eval2.eval(code, 127));
    }

    @Test
    public void testMask() {
        CodeGen code = new CodeGen(
"""
u16 mask = (1<<16)-1;           // AND mask
int c = 123456789 & mask;
return c;                       //
""");
        code.parse().opto();
        assertEquals("return 52501;", code.print());
        assertEquals("52501", Eval2.eval(code,  0));
    }

    @Test
    public void testOr() {
        CodeGen code = new CodeGen(
"""
return (arg | 123 ^ 456) >>> 1;
""");
        code.parse().opto();
        assertEquals("return (((arg|123)^456)>>>1);", code.print());
        assertEquals("217", Eval2.eval(code,  0));
    }

    @Test
    public void testMaskFloat() {
        CodeGen code = new CodeGen(
"""
flt f = arg;
arg = f & 1;
return arg;
""");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Cannot '&' flt",e.getMessage()); }
    }

    @Test
    public void testCloneAnd() {
        CodeGen code = new CodeGen("""
int v0=0;
u32 v1 = 1&(1<<arg)&(1<<arg);
while(arg) v1=-v0;
while(v1) break;
return v1;
""");
        code.parse().opto();
        assertEquals("return (Phi(Loop,((1<<arg)&1),0)&Phi(Loop,Shl,4294967295));", code.print());
        assertEquals("1", Eval2.eval(code,  0));
    }

    @Test
    public void testAndHigh() {
        CodeGen code = new CodeGen("""
int v0=0;
if(0&0>>>0) {
    while(0) {
        u8 v1=0;
        v0=0>>>0;
        v1=arg;
        while(v1+0) {}
    }
}
return v0;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
        assertEquals("0", Eval2.eval(code,  0));
    }

    @Test
    public void testTypes() {
        CodeGen code = new CodeGen(
"""
i8  xi8  = 123456789;  if( xi8  !=        21 ) return -8;
i16 xi16 = 123456789;  if( xi16 !=    -13035 ) return -16;
i32 xi32 = 123456789;  if( xi32 != 123456789 ) return -32;
i64 xi64 = 123456789;  if( xi64 != 123456789 ) return -64;
int xint = 123456789;  if( xint != 123456789 ) return -64;

u1  ui1  = 123456789;  if( ui1  !=         1 ) return 1;
u8  ui8  = 123456789;  if( ui8  !=        21 ) return 8;
u16 ui16 = 123456789;  if( ui16 !=     52501 ) return 16;
u32 ui32 = 123456789;  if( ui32 != 123456789 ) return 32;

flt fflt = 3.141592653589793;  if( fflt != 3.141592653589793 ) return 3;
f64 ff64 = 3.141592653589793;  if( ff64 != 3.141592653589793 ) return 3;
f32 ff32 = 3.141592653589793;  if( ff32 != 3.1415927410125732) return 5;

return 0;
""");
        code.parse().opto();
        assertEquals("return 0;", code.print());
        assertEquals("0", Eval2.eval(code,  0));
    }

}