1/*
2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.jtt.optimize;
24
25import java.util.Random;
26
27import org.junit.Test;
28
29import org.graalvm.compiler.jtt.JTTTest;
30
31/*
32 */
33@SuppressWarnings("unused")
34public class Conditional01 extends JTTTest {
35
36    private static class TestClass {
37        private int nextPC;
38        private int pc;
39        private boolean aC;
40        private boolean aH;
41        private boolean aN;
42        private boolean aZ;
43        private boolean aV;
44        private boolean aS;
45        private int cyclesConsumed;
46        private int[] sram = new int[RAM_SIZE];
47
48        public void visit(CPC i) {
49            nextPC = pc + 2;
50            int tmp0 = getRegisterByte(i.r1);
51            int tmp1 = getRegisterByte(i.r2);
52            int tmp2 = bit(aC);
53            int tmp3 = tmp0 - tmp1 - tmp2;
54            boolean tmp4 = ((tmp0 & 128) != 0);
55            boolean tmp5 = ((tmp1 & 128) != 0);
56            boolean tmp6 = ((tmp3 & 128) != 0);
57            boolean tmp7 = ((tmp0 & 8) != 0);
58            boolean tmp8 = ((tmp1 & 8) != 0);
59            boolean tmp9 = ((tmp3 & 8) != 0);
60            aH = !tmp7 && tmp8 || tmp8 && tmp9 || tmp9 && !tmp7;
61            aC = !tmp4 && tmp5 || tmp5 && tmp6 || tmp6 && !tmp4;
62            aN = tmp6;
63            aZ = low(tmp3) == 0 && aZ;
64            aV = tmp4 && !tmp5 && !tmp6 || !tmp4 && tmp5 && tmp6;
65            aS = (aN != aV);
66            cyclesConsumed++;
67        }
68
69        public int getRegisterByte(Register r1) {
70            if ((r1.val % 10) == 0) {
71                return sram[r1.num];
72            }
73            return r1.val;
74        }
75
76        public int low(int tmp3) {
77            return tmp3 & 0x01;
78        }
79
80        public int bit(boolean c2) {
81            return c2 ? 1 : 0;
82        }
83    }
84
85    private static final int RAM_SIZE = 0x100;
86    private static final int init = new Random().nextInt();
87    private static final int init1 = new Register().val;
88    private static final Register init2 = new CPC().r1;
89
90    public static int test(int arg) {
91        TestClass c = new TestClass();
92        Random rnd = new Random();
93        for (int i = 0; i < arg; i++) {
94            CPC i2 = new CPC();
95            i2.r1 = new Register();
96            i2.r1.val = i;
97            i2.r1.num = i + RAM_SIZE - 20;
98            i2.r2 = new Register();
99            i2.r2.val = rnd.nextInt();
100            i2.r2.num = rnd.nextInt(RAM_SIZE);
101            try {
102                c.visit(i2);
103            } catch (RuntimeException re) {
104
105            }
106        }
107        return c.cyclesConsumed;
108    }
109
110    private static class Register {
111
112        int val;
113        int num;
114    }
115
116    private static class CPC {
117
118        public Register r1;
119        public Register r2;
120
121    }
122
123    @Test
124    public void run0() throws Throwable {
125        runTest("test", 0);
126    }
127
128    @Test
129    public void run1() throws Throwable {
130        runTest("test", 10);
131    }
132
133    @Test
134    public void run2() throws Throwable {
135        runTest("test", 20);
136    }
137
138    @Test
139    public void run3() throws Throwable {
140        runTest("test", 40);
141    }
142
143}
144