1 /*
2  * Copyright (c) 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.core.test;
26 
27 import org.graalvm.compiler.debug.GraalError;
28 import org.graalvm.compiler.test.GraalTest;
29 import org.objectweb.asm.Label;
30 import org.objectweb.asm.MethodVisitor;
31 import org.objectweb.asm.Opcodes;
32 
33 import jdk.vm.ci.meta.JavaKind;
34 
35 public final class SubWordTestUtil implements Opcodes {
36 
SubWordTestUtil()37     private SubWordTestUtil() {
38     }
39 
convertToKind(MethodVisitor snippet, JavaKind kind)40     static void convertToKind(MethodVisitor snippet, JavaKind kind) {
41         switch (kind) {
42             case Boolean:
43                 snippet.visitInsn(ICONST_1);
44                 snippet.visitInsn(IAND);
45                 break;
46             case Byte:
47                 snippet.visitInsn(I2B);
48                 break;
49             case Short:
50                 snippet.visitInsn(I2S);
51                 break;
52             case Char:
53                 snippet.visitInsn(I2C);
54                 break;
55             default:
56                 throw GraalError.shouldNotReachHere();
57         }
58     }
59 
testEqual(MethodVisitor snippet)60     static void testEqual(MethodVisitor snippet) {
61         Label label = new Label();
62         snippet.visitJumpInsn(IF_ICMPNE, label);
63         snippet.visitInsn(ICONST_1);
64         snippet.visitInsn(IRETURN);
65         snippet.visitLabel(label);
66         snippet.visitInsn(ICONST_0);
67         snippet.visitInsn(IRETURN);
68     }
69 
getUnsafe(MethodVisitor snippet)70     static void getUnsafe(MethodVisitor snippet) {
71         snippet.visitFieldInsn(GETSTATIC, GraalTest.class.getName().replace('.', '/'), "UNSAFE", "Lsun/misc/Unsafe;");
72     }
73 
getUnsafePutMethodName(JavaKind kind)74     static String getUnsafePutMethodName(JavaKind kind) {
75         String name = kind.getJavaName();
76         return name.substring(0, 1).toUpperCase() + name.substring(1);
77     }
78 
79 }
80