1 /*
  2  * Copyright (c) 2024, 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  * @test
 26  * @summary Smoke test for code reflection with unary operations.
 27  * @modules jdk.incubator.code
 28  * @build UnaryopTest
 29  * @build CodeReflectionTester
 30  * @run main CodeReflectionTester UnaryopTest
 31  */
 32 
 33 import jdk.incubator.code.CodeReflection;
 34 
 35 public class UnaryopTest {
 36     @CodeReflection
 37     @IR("""
 38             func @"test" (%0 : int)int -> {
 39                 %1 : Var<int> = var %0 @"v" ;
 40                 %2 : int = var.load %1 ;
 41                 %3 : int = neg %2 ;
 42                 return %3 ;
 43             };
 44             """)
 45     static int test(int v) {
 46         return -v;
 47     }
 48 
 49     @CodeReflection
 50     @IR("""
 51             func @"test2" (%0 : int)int -> {
 52                 %1 : Var<int> = var %0 @"v";
 53                 %2 : int = var.load %1;
 54                 return %2;
 55             };
 56             """)
 57     static int test2(int v) {
 58         return +v;
 59     }
 60 
 61     @CodeReflection
 62     @IR("""
 63             func @"test3"  (%0 : int)java.lang.Integer -> {
 64                 %1 : Var<int> = var %0 @"v" ;
 65                 %2 : int = var.load %1 ;
 66                 %3 : java.lang.Integer = invoke %2 @"java.lang.Integer::valueOf(int)java.lang.Integer" ;
 67                 return %3 ;
 68             };
 69             """)
 70     // Tests that numeric promotion occurs
 71     static Integer test3(int v) {
 72         return +v;
 73     }
 74 
 75     @CodeReflection
 76     @IR("""
 77             func @"test4"  (%0 : java.lang.Integer)java.lang.Integer -> {
 78                 %1 : Var<java.lang.Integer> = var %0 @"v" ;
 79                 %2 : java.lang.Integer = var.load %1 ;
 80                 %3 : int = invoke %2 @"java.lang.Integer::intValue()int" ;
 81                 %4 : java.lang.Integer = invoke %3 @"java.lang.Integer::valueOf(int)java.lang.Integer" ;
 82                 return %4 ;
 83             };
 84             """)
 85     // Tests that numeric promotion is retained
 86     static Integer test4(Integer v) {
 87         return +v;
 88     }
 89 
 90     @CodeReflection
 91     @IR("""
 92             func @"test5" (%0 : int)int -> {
 93                 %1 : Var<int> = var %0 @"v" ;
 94                 %2 : int = var.load %1 ;
 95                 %3 : int = compl %2 ;
 96                 return %3 ;
 97             };
 98             """)
 99     static int test5(int v) {
100         return ~v;
101     }
102 }