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