8339214: Remove misleading CodeBuilder.loadConstant(Opcode, ConstantDesc)

Reviewed-by: asotona
This commit is contained in:
Chen Liang 2024-09-03 13:44:48 +00:00
parent 4ca2c208ea
commit ad40a122d6
10 changed files with 48 additions and 199 deletions

View file

@ -604,23 +604,6 @@ public sealed interface CodeBuilder
return this;
}
/**
* Generate an instruction pushing a constant onto the operand stack
* @see Opcode.Kind#CONSTANT
* @param opcode the constant instruction opcode
* @param value the constant value
* @return this builder
* @since 23
*/
default CodeBuilder loadConstant(Opcode opcode, ConstantDesc value) {
BytecodeHelpers.validateValue(opcode, value);
return with(switch (opcode) {
case SIPUSH, BIPUSH -> ConstantInstruction.ofArgument(opcode, ((Number)value).intValue());
case LDC, LDC_W, LDC2_W -> ConstantInstruction.ofLoad(opcode, BytecodeHelpers.constantEntry(constantPool(), value));
default -> ConstantInstruction.ofIntrinsic(opcode);
});
}
/**
* Generate an instruction pushing a constant onto the operand stack
* @param value the constant value
@ -931,12 +914,12 @@ public sealed interface CodeBuilder
}
/**
* Generate an instruction pushing a byte onto the operand stack
* @param b the byte
* Generate an instruction pushing an int in the range of byte onto the operand stack.
* @param b the int in the range of byte
* @return this builder
*/
default CodeBuilder bipush(int b) {
return loadConstant(Opcode.BIPUSH, b);
return with(ConstantInstruction.ofArgument(Opcode.BIPUSH, b));
}
/**
@ -2396,12 +2379,12 @@ public sealed interface CodeBuilder
}
/**
* Generate an instruction pushing a short onto the operand stack
* @param s the short
* Generate an instruction pushing an int in the range of short onto the operand stack.
* @param s the int in the range of short
* @return this builder
*/
default CodeBuilder sipush(int s) {
return loadConstant(Opcode.SIPUSH, s);
return with(ConstantInstruction.ofArgument(Opcode.SIPUSH, s));
}
/**

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,6 +33,7 @@ import java.lang.classfile.Opcode;
import java.lang.classfile.TypeKind;
import java.lang.classfile.constantpool.LoadableConstantEntry;
import jdk.internal.classfile.impl.AbstractInstruction;
import jdk.internal.classfile.impl.BytecodeHelpers;
import jdk.internal.classfile.impl.Util;
import jdk.internal.javac.PreviewFeature;
@ -144,16 +145,21 @@ public sealed interface ConstantInstruction extends Instruction {
/**
* {@return an argument constant instruction}
*
* @param op the opcode for the specific type of intrinsic constant instruction,
* which must be of kind {@link Opcode.Kind#CONSTANT}
* @param op the opcode for the specific type of argument constant instruction,
* which must be {@link Opcode#BIPUSH} or {@link Opcode#SIPUSH}
* @param value the constant value
* @throws IllegalArgumentException if the opcode is not {@link Opcode#BIPUSH}
* or {@link Opcode#SIPUSH}
* or {@link Opcode#SIPUSH}, or if the constant value is out of range
* for the opcode
*/
static ArgumentConstantInstruction ofArgument(Opcode op, int value) {
Util.checkKind(op, Opcode.Kind.CONSTANT);
if (op != Opcode.BIPUSH && op != Opcode.SIPUSH)
if (op == Opcode.BIPUSH) {
BytecodeHelpers.validateBipush(value);
} else if (op == Opcode.SIPUSH) {
BytecodeHelpers.validateSipush(value);
} else {
throw new IllegalArgumentException(String.format("Wrong opcode specified; found %s, expected BIPUSH or SIPUSH", op));
}
return new AbstractInstruction.UnboundArgumentConstantInstruction(op, value);
}