1 /* 2 * Copyright (c) 2011, 2018, 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 */ 23 24 25 package org.graalvm.compiler.replacements.test; 26 27 import org.graalvm.compiler.api.replacements.Snippet; 28 import org.graalvm.compiler.debug.DebugContext; 29 import org.graalvm.compiler.nodes.StructuredGraph; 30 import org.graalvm.compiler.nodes.StructuredGraph.Builder; 31 import org.graalvm.compiler.phases.PhaseSuite; 32 import org.graalvm.compiler.phases.tiers.HighTierContext; 33 import org.graalvm.compiler.word.Word; 34 import jdk.internal.vm.compiler.word.Pointer; 35 import jdk.internal.vm.compiler.word.UnsignedWord; 36 import jdk.internal.vm.compiler.word.WordBase; 37 import jdk.internal.vm.compiler.word.WordFactory; 38 import org.junit.Test; 39 40 /** 41 * Tests for the {@link Word} type. 42 */ 43 public class WordTest extends SnippetsTest { 44 45 @Override parse(Builder builder, PhaseSuite<HighTierContext> graphBuilderSuite)46 protected StructuredGraph parse(Builder builder, PhaseSuite<HighTierContext> graphBuilderSuite) { 47 // create a copy to assign a valid compilation id 48 DebugContext debug = getDebugContext(); 49 StructuredGraph originalGraph = installer.makeGraph(debug, bytecodeProvider, builder.getMethod(), null, null, false, null); 50 return originalGraph.copyWithIdentifier(builder.getCompilationId(), debug); 51 } 52 53 @Test construction()54 public void construction() { 55 long[] words = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1L, 0L, 1L, Long.MAX_VALUE - 1, Long.MAX_VALUE, Integer.MAX_VALUE - 1L, Integer.MAX_VALUE, Integer.MAX_VALUE + 1L, 56 Integer.MIN_VALUE - 1L, Integer.MIN_VALUE, Integer.MIN_VALUE + 1L}; 57 for (long word : words) { 58 test("unsignedLong", word); 59 test("unsignedInt", (int) word); 60 test("signedLong", word); 61 test("signedInt", (int) word); 62 } 63 } 64 65 @Test testArithmetic()66 public void testArithmetic() { 67 long[] words = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1L, 0L, 1L, Long.MAX_VALUE - 1, Long.MAX_VALUE, Integer.MAX_VALUE - 1L, Integer.MAX_VALUE, Integer.MAX_VALUE + 1L, 68 Integer.MIN_VALUE - 1L, Integer.MIN_VALUE, Integer.MIN_VALUE + 1L}; 69 for (long word : words) { 70 test("unsignedNot", word); 71 test("signedNot", word); 72 for (long addend : words) { 73 test("unsignedPlusInt", word, (int) addend); 74 test("unsignedMinusInt", word, (int) addend); 75 test("unsignedPlusInt", word, -((int) addend)); 76 test("unsignedMinusInt", word, -((int) addend)); 77 test("unsignedPlusLong", word, addend); 78 test("unsignedMinusLong", word, addend); 79 test("unsignedPlusLong", word, -addend); 80 test("unsignedMinusLong", word, -addend); 81 test("signedPlusInt", word, (int) addend); 82 test("signedMinusInt", word, (int) addend); 83 test("signedPlusInt", word, -((int) addend)); 84 test("signedMinusInt", word, -((int) addend)); 85 test("signedPlusLong", word, addend); 86 test("signedMinusLong", word, addend); 87 test("signedPlusLong", word, -addend); 88 test("signedMinusLong", word, -addend); 89 90 test("andInt", word, (int) addend); 91 test("orInt", word, (int) addend); 92 test("andInt", word, -((int) addend)); 93 test("orInt", word, -((int) addend)); 94 test("andLong", word, addend); 95 test("orLong", word, addend); 96 test("andLong", word, -addend); 97 test("orLong", word, -addend); 98 } 99 } 100 } 101 102 @Test testCompare()103 public void testCompare() { 104 long[] words = new long[]{Long.MIN_VALUE, Long.MIN_VALUE + 1, -1L, 0L, 1L, Long.MAX_VALUE - 1, Long.MAX_VALUE}; 105 for (long word1 : words) { 106 for (long word2 : words) { 107 for (String method : new String[]{"aboveOrEqual", "above", "belowOrEqual", "below"}) { 108 test(method, word1, word2); 109 test(method, word2, word1); 110 } 111 } 112 } 113 } 114 115 @Test testCast()116 public void testCast() { 117 test("cast", 1234L); 118 } 119 120 @Snippet cast(long input)121 public static long cast(long input) { 122 WordBase base = WordFactory.signed(input); 123 UnsignedWord unsigned = (UnsignedWord) base; 124 Pointer pointer = (Pointer) unsigned; 125 Word word = (Word) pointer; 126 return word.rawValue(); 127 } 128 129 @Snippet unsignedLong(long word)130 public static long unsignedLong(long word) { 131 return WordFactory.unsigned(word).rawValue(); 132 } 133 134 @Snippet unsignedInt(int word)135 public static long unsignedInt(int word) { 136 return WordFactory.unsigned(word).rawValue(); 137 } 138 139 @Snippet signedLong(long word)140 public static long signedLong(long word) { 141 return WordFactory.signed(word).rawValue(); 142 } 143 144 @Snippet signedInt(int word)145 public static long signedInt(int word) { 146 return WordFactory.signed(word).rawValue(); 147 } 148 149 @Snippet unsignedPlusInt(long word, int addend)150 public static long unsignedPlusInt(long word, int addend) { 151 return WordFactory.unsigned(word).add(addend).rawValue(); 152 } 153 154 @Snippet unsignedMinusInt(long word, int addend)155 public static long unsignedMinusInt(long word, int addend) { 156 return WordFactory.unsigned(word).subtract(addend).rawValue(); 157 } 158 159 @Snippet unsignedPlusLong(long word, long addend)160 public static long unsignedPlusLong(long word, long addend) { 161 return WordFactory.unsigned(word).add(WordFactory.unsigned(addend)).rawValue(); 162 } 163 164 @Snippet unsignedMinusLong(long word, long addend)165 public static long unsignedMinusLong(long word, long addend) { 166 return WordFactory.unsigned(word).subtract(WordFactory.unsigned(addend)).rawValue(); 167 } 168 169 @Snippet signedPlusInt(long word, int addend)170 public static long signedPlusInt(long word, int addend) { 171 return WordFactory.signed(word).add(addend).rawValue(); 172 } 173 174 @Snippet signedMinusInt(long word, int addend)175 public static long signedMinusInt(long word, int addend) { 176 return WordFactory.signed(word).subtract(addend).rawValue(); 177 } 178 179 @Snippet signedPlusLong(long word, long addend)180 public static long signedPlusLong(long word, long addend) { 181 return WordFactory.signed(word).add(WordFactory.signed(addend)).rawValue(); 182 } 183 184 @Snippet signedMinusLong(long word, long addend)185 public static long signedMinusLong(long word, long addend) { 186 return WordFactory.signed(word).subtract(WordFactory.signed(addend)).rawValue(); 187 } 188 189 @Snippet signedNot(long word)190 public static long signedNot(long word) { 191 return WordFactory.signed(word).not().rawValue(); 192 } 193 194 @Snippet unsignedNot(long word)195 public static long unsignedNot(long word) { 196 return WordFactory.unsigned(word).not().rawValue(); 197 } 198 199 @Snippet aboveOrEqual(long word1, long word2)200 public static boolean aboveOrEqual(long word1, long word2) { 201 return WordFactory.unsigned(word1).aboveOrEqual(WordFactory.unsigned(word2)); 202 } 203 204 @Snippet above(long word1, long word2)205 public static boolean above(long word1, long word2) { 206 return WordFactory.unsigned(word1).aboveThan(WordFactory.unsigned(word2)); 207 } 208 209 @Snippet belowOrEqual(long word1, long word2)210 public static boolean belowOrEqual(long word1, long word2) { 211 return WordFactory.unsigned(word1).belowOrEqual(WordFactory.unsigned(word2)); 212 } 213 214 @Snippet below(long word1, long word2)215 public static boolean below(long word1, long word2) { 216 return WordFactory.unsigned(word1).belowThan(WordFactory.unsigned(word2)); 217 } 218 219 @Snippet andInt(long word, int addend)220 public static long andInt(long word, int addend) { 221 return WordFactory.unsigned(word).and(addend).rawValue(); 222 } 223 224 @Snippet orInt(long word, int addend)225 public static long orInt(long word, int addend) { 226 return WordFactory.unsigned(word).or(addend).rawValue(); 227 } 228 229 @Snippet andLong(long word, long addend)230 public static long andLong(long word, long addend) { 231 return WordFactory.unsigned(word).and(WordFactory.unsigned(addend)).rawValue(); 232 } 233 234 @Snippet orLong(long word, long addend)235 public static long orLong(long word, long addend) { 236 return WordFactory.unsigned(word).or(WordFactory.unsigned(addend)).rawValue(); 237 } 238 } 239