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 import jdk.incubator.code.bytecode.impl.BranchCompactor; 25 import jdk.internal.classfile.components.ClassPrinter; 26 import org.junit.jupiter.api.Assertions; 27 import org.junit.jupiter.api.Test; 28 29 import java.lang.classfile.Attributes; 30 import java.lang.classfile.ClassFile; 31 import java.lang.classfile.Instruction; 32 import java.lang.constant.ClassDesc; 33 import java.lang.constant.ConstantDescs; 34 import java.util.List; 35 36 import static java.lang.classfile.Opcode.*; 37 38 /* 39 * @test 40 * @modules jdk.incubator.code 41 * @modules java.base/jdk.internal.classfile.components 42 * @modules jdk.incubator.code/jdk.incubator.code.bytecode.impl 43 * @enablePreview 44 * @run junit TestBranchCompactor 45 */ 46 public class TestBranchCompactor { 47 48 @Test 49 public void testBranchCompactor() { 50 var cc = ClassFile.of(ClassFile.StackMapsOption.DROP_STACK_MAPS); 51 var clm = cc.parse(cc.build(ClassDesc.of("c"), clb -> clb.withMethodBody("m", ConstantDescs.MTD_void, 0, 52 cb -> cb.transforming(new BranchCompactor(), cob -> { 53 var l = cob.newLabel(); 54 cob.goto_(l) //compact 55 .lineNumber(1) 56 .labelBinding(l) 57 .nop(); 58 59 l = cob.newLabel(); 60 cob.goto_w(l) //compact 61 .lineNumber(2) 62 .labelBinding(l); 63 64 l = cob.newLabel(); 65 cob.goto_(l) //compact 66 .labelBinding(l); 67 68 cob.iconst_0(); 69 l = cob.newLabel(); 70 cob.ifeq(l) //do not compact 71 .labelBinding(l); 72 73 l = cob.newLabel(); 74 cob.goto_(l) //do not compact 75 .nop() 76 .labelBinding(l) 77 .return_(); 78 })))); 79 var code = clm.methods().get(0).code().get(); 80 ClassPrinter.toYaml(code, ClassPrinter.Verbosity.TRACE_ALL, System.out::print); 81 Assertions.assertEquals( 82 List.of(NOP, ICONST_0, IFEQ, GOTO, NOP, RETURN), code.elementList().stream().mapMulti((e, ec) -> {if (e instanceof Instruction i) ec.accept(i.opcode());}).toList() 83 ); 84 Assertions.assertEquals(2, code.findAttribute(Attributes.lineNumberTable()).get().lineNumbers().size()); 85 } 86 }