|
| 1 | +/* |
| 2 | + * This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader |
| 3 | + * Copyright (C) 2023 RK_01/RaphiMC and contributors |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package net.raphimc.javadowngrader.transformer.j16; |
| 19 | + |
| 20 | +import net.raphimc.javadowngrader.util.ASMUtil; |
| 21 | +import org.objectweb.asm.Label; |
| 22 | +import org.objectweb.asm.MethodVisitor; |
| 23 | +import org.objectweb.asm.Opcodes; |
| 24 | +import org.objectweb.asm.tree.ClassNode; |
| 25 | + |
| 26 | +public class RandomSupportBoundedNextLongCreator { |
| 27 | + public static final String BOUNDEDNEXTLONG_NAME = "javadowngrader-boundedNextLong"; |
| 28 | + public static final String BOUNDEDNEXTLONG_DESC = "(Ljava/util/Random;J)J"; |
| 29 | + |
| 30 | + public static void ensureHasMethod(final ClassNode classNode) { |
| 31 | + if (ASMUtil.hasMethod(classNode, BOUNDEDNEXTLONG_NAME, BOUNDEDNEXTLONG_DESC)) return; |
| 32 | + |
| 33 | + final MethodVisitor boundedNextLong = classNode.visitMethod( |
| 34 | + Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC | Opcodes.ACC_SYNTHETIC, |
| 35 | + BOUNDEDNEXTLONG_NAME, BOUNDEDNEXTLONG_DESC, null, null |
| 36 | + ); |
| 37 | + boundedNextLong.visitCode(); |
| 38 | + |
| 39 | + // final long m = bound - 1; |
| 40 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 1); |
| 41 | + boundedNextLong.visitInsn(Opcodes.LCONST_1); |
| 42 | + boundedNextLong.visitInsn(Opcodes.LSUB); |
| 43 | + boundedNextLong.visitVarInsn(Opcodes.LSTORE, 3); |
| 44 | + |
| 45 | + // long r = rng.nextLong(); |
| 46 | + boundedNextLong.visitVarInsn(Opcodes.ALOAD, 0); |
| 47 | + boundedNextLong.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/Random", "nextLong", "()J", false); |
| 48 | + boundedNextLong.visitVarInsn(Opcodes.LSTORE, 5); |
| 49 | + |
| 50 | + // if ((bound & m) == 0L) { |
| 51 | + final Label elseStart = new Label(); |
| 52 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 1); |
| 53 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 3); |
| 54 | + boundedNextLong.visitInsn(Opcodes.LAND); |
| 55 | + boundedNextLong.visitInsn(Opcodes.LCONST_0); |
| 56 | + boundedNextLong.visitInsn(Opcodes.LCMP); |
| 57 | + boundedNextLong.visitJumpInsn(Opcodes.IFNE, elseStart); |
| 58 | + |
| 59 | + // r &= m; |
| 60 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 5); |
| 61 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 3); |
| 62 | + boundedNextLong.visitInsn(Opcodes.LAND); |
| 63 | + boundedNextLong.visitVarInsn(Opcodes.LSTORE, 5); |
| 64 | + |
| 65 | + // } else { |
| 66 | + final Label elseEnd = new Label(); |
| 67 | + boundedNextLong.visitJumpInsn(Opcodes.GOTO, elseEnd); |
| 68 | + boundedNextLong.visitLabel(elseStart); |
| 69 | + |
| 70 | + // long u = r >>> 1; |
| 71 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 5); |
| 72 | + boundedNextLong.visitInsn(Opcodes.ICONST_1); |
| 73 | + boundedNextLong.visitInsn(Opcodes.LUSHR); |
| 74 | + boundedNextLong.visitVarInsn(Opcodes.LSTORE, 7); |
| 75 | + |
| 76 | + // while (u + m - (r = u % bound) < 0L) { |
| 77 | + final Label whileStart = new Label(); |
| 78 | + final Label whileEnd = new Label(); |
| 79 | + boundedNextLong.visitLabel(whileStart); |
| 80 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 7); |
| 81 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 3); |
| 82 | + boundedNextLong.visitInsn(Opcodes.LADD); |
| 83 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 7); |
| 84 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 1); |
| 85 | + boundedNextLong.visitInsn(Opcodes.LREM); |
| 86 | + boundedNextLong.visitInsn(Opcodes.DUP2); |
| 87 | + boundedNextLong.visitVarInsn(Opcodes.LSTORE, 5); |
| 88 | + boundedNextLong.visitInsn(Opcodes.LSUB); |
| 89 | + boundedNextLong.visitInsn(Opcodes.LCONST_0); |
| 90 | + boundedNextLong.visitInsn(Opcodes.LCMP); |
| 91 | + boundedNextLong.visitJumpInsn(Opcodes.IFGE, whileEnd); |
| 92 | + |
| 93 | + // u = rng.nextLong() >>> 1; |
| 94 | + boundedNextLong.visitVarInsn(Opcodes.ALOAD, 0); |
| 95 | + boundedNextLong.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/util/Random", "nextLong", "()J", false); |
| 96 | + boundedNextLong.visitInsn(Opcodes.ICONST_1); |
| 97 | + boundedNextLong.visitInsn(Opcodes.LUSHR); |
| 98 | + boundedNextLong.visitVarInsn(Opcodes.LSTORE, 7); |
| 99 | + |
| 100 | + // } |
| 101 | + boundedNextLong.visitJumpInsn(Opcodes.GOTO, whileStart); |
| 102 | + boundedNextLong.visitLabel(whileEnd); |
| 103 | + |
| 104 | + // } |
| 105 | + boundedNextLong.visitLabel(elseEnd); |
| 106 | + |
| 107 | + // return r; |
| 108 | + boundedNextLong.visitVarInsn(Opcodes.LLOAD, 5); |
| 109 | + boundedNextLong.visitInsn(Opcodes.LRETURN); |
| 110 | + |
| 111 | + boundedNextLong.visitEnd(); |
| 112 | + } |
| 113 | +} |
0 commit comments