|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Arm Limited. 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 | +package compiler.c2.aarch64; |
| 24 | + |
| 25 | +import jdk.test.lib.process.OutputAnalyzer; |
| 26 | +import jdk.test.lib.process.ProcessTools; |
| 27 | +import java.util.regex.Matcher; |
| 28 | +import java.util.regex.Pattern; |
| 29 | +import java.util.*; |
| 30 | + |
| 31 | +/* |
| 32 | + * @test |
| 33 | + * @summary Calls to c2i interface stubs should be generated with near branches |
| 34 | + * for segmented code cache up to 250MB |
| 35 | + * @library /test/lib / |
| 36 | + * |
| 37 | + * @requires vm.flagless |
| 38 | + * @requires os.arch=="aarch64" |
| 39 | + * @requires vm.debug == false |
| 40 | + * @requires vm.compiler2.enabled |
| 41 | + * |
| 42 | + * @run driver compiler.c2.aarch64.TestStaticCallStub |
| 43 | + */ |
| 44 | +public class TestStaticCallStub { |
| 45 | + |
| 46 | + static String[] nearStaticCallOpcodeSeq = {"isb", "mov", "movk", "movk", "b"}; |
| 47 | + static String[] farStaticCallOpcodeSeq = {"isb", "mov", "movk", "movk", "mov", "movk", "movk", "br"}; |
| 48 | + |
| 49 | + static String extractOpcode(String line) { |
| 50 | + line = line.trim(); |
| 51 | + int semicolonIndex = line.indexOf(';'); |
| 52 | + if (semicolonIndex != -1) { |
| 53 | + line = line.substring(0, semicolonIndex).trim(); |
| 54 | + } |
| 55 | + |
| 56 | + String[] words = line.split("\\s+"); |
| 57 | + if (words.length > 1) { |
| 58 | + return words[1]; |
| 59 | + } |
| 60 | + |
| 61 | + return ""; |
| 62 | + } |
| 63 | + |
| 64 | + static List<String> extractOpcodesN(ListIterator<String> itr, int n) { |
| 65 | + List<String> extractedOpcodes = new ArrayList<>(); |
| 66 | + |
| 67 | + while (itr.hasNext() && extractedOpcodes.size() < n) { |
| 68 | + String opcode = extractOpcode(itr.next()); |
| 69 | + if (!opcode.isEmpty()) { |
| 70 | + extractedOpcodes.add(opcode); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return extractedOpcodes; |
| 75 | + } |
| 76 | + |
| 77 | + static void verifyNearStaticCall(ListIterator<String> itr) { |
| 78 | + List<String> extractedOpcodes = extractOpcodesN(itr, nearStaticCallOpcodeSeq.length); |
| 79 | + |
| 80 | + if (!Arrays.asList(nearStaticCallOpcodeSeq).equals(extractedOpcodes)) { |
| 81 | + throw new RuntimeException("for code cache < 250MB the static call stub is expected to be implemented using near branch"); |
| 82 | + } |
| 83 | + |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + static void verifyFarStaticCall(ListIterator<String> itr) { |
| 88 | + List<String> extractedOpcodes = extractOpcodesN(itr, farStaticCallOpcodeSeq.length); |
| 89 | + |
| 90 | + if (!Arrays.asList(farStaticCallOpcodeSeq).equals(extractedOpcodes)) { |
| 91 | + throw new RuntimeException("for code cache > 250MB the static call stub is expected to be implemented using far branch"); |
| 92 | + } |
| 93 | + |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + static void runVM(boolean bigCodeCache) throws Exception { |
| 98 | + String className = TestStaticCallStub.class.getName(); |
| 99 | + String[] procArgs = { |
| 100 | + "-XX:-Inline", |
| 101 | + "-Xcomp", |
| 102 | + "-Xbatch", |
| 103 | + "-XX:+TieredCompilation", |
| 104 | + "-XX:+SegmentedCodeCache", |
| 105 | + "-XX:ReservedCodeCacheSize=" + (bigCodeCache ? "256M" : "200M"), |
| 106 | + "-XX:+UnlockDiagnosticVMOptions", |
| 107 | + "-XX:CompileCommand=option," + className + "::main,bool,PrintAssembly,true", |
| 108 | + className}; |
| 109 | + |
| 110 | + |
| 111 | + ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(procArgs); |
| 112 | + OutputAnalyzer output = new OutputAnalyzer(pb.start()); |
| 113 | + List<String> lines = output.asLines(); |
| 114 | + |
| 115 | + ListIterator<String> itr = lines.listIterator(); |
| 116 | + while (itr.hasNext()) { |
| 117 | + String line = itr.next(); |
| 118 | + if (line.contains("{static_stub}")) { |
| 119 | + itr.previous(); |
| 120 | + if (bigCodeCache) { |
| 121 | + verifyFarStaticCall(itr); |
| 122 | + } else { |
| 123 | + verifyNearStaticCall(itr); |
| 124 | + } |
| 125 | + return; |
| 126 | + } |
| 127 | + } |
| 128 | + throw new RuntimeException("Assembly output: static call stub is not found"); |
| 129 | + } |
| 130 | + |
| 131 | + public static void main(String[] args) throws Exception { |
| 132 | + if (args.length == 0) { |
| 133 | + // Main VM: fork VM with options |
| 134 | + runVM(true); |
| 135 | + runVM(false); |
| 136 | + return; |
| 137 | + } |
| 138 | + if (args.length > 0) { |
| 139 | + // We are in a forked VM. Just exit |
| 140 | + System.out.println("Ok"); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
0 commit comments