RFR: 8303374: Compiler Implementation for Primitive types in patterns, instanceof, and switch (Preview) [v2]
Aggelos Biboudis
abimpoudis at openjdk.org
Fri Sep 29 14:57:51 UTC 2023
On Thu, 28 Sep 2023 16:46:11 GMT, Aggelos Biboudis <abimpoudis at openjdk.org> wrote:
>> This is the first draft of a patch for Primitive types in patterns, instanceof, and switch (Preview).
>>
>> Draft spec here: https://cr.openjdk.org/~abimpoudis/instanceof/instanceof-20230913/specs/instanceof-jls.html
>
> Aggelos Biboudis has updated the pull request incrementally with one additional commit since the last revision:
>
> Apply suggestions from code review
>
> Co-authored-by: Raffaello Giulietti <raffaello.giulietti at oracle.com>
So, the following shows the changes in `Lower.java`. It removes the methods you propose at the cost of a little bit more indirection which is ok. However, there is a non-trivial amount of re-engineering needed to be done on the `SwitchBootstrap` to support this change. It is not a mere porting of the logic from `Lower` to `SwitchBootstrap`. This is easy (despite the duplication of the code which I am inviting recommendations on how to avoid 🙈 ). What is more, is that the `selectorType` changes within `createRepeatIndexSwitch` and needs to change in various sites as well e.g., `createMethodHandleSwitch`. @lahodaj am I correct?
(this is a footprint vs code uniformity question. If the bytecode instructions are exactly the same and in one occasion (`short_byte` -> `int_byte`) one bytecode instruction shorter, maybe code uniformity should win here?)
Subject: [PATCH] [WIP] Implement type pairs to exactnessMethod name
---
Index: src/java.base/share/classes/java/lang/runtime/ExactnessMethods.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/java.base/share/classes/java/lang/runtime/ExactnessMethods.java b/src/java.base/share/classes/java/lang/runtime/ExactnessMethods.java
--- a/src/java.base/share/classes/java/lang/runtime/ExactnessMethods.java (revision 1ae06ac8f8cc0c0ce70920c2a62a7c13fdfc6f23)
+++ b/src/java.base/share/classes/java/lang/runtime/ExactnessMethods.java (date 1695993935975)
@@ -36,46 +36,6 @@
private ExactnessMethods() { }
- /** Exactness method from byte to char
- *
- * @param n value
- * @return true if the passed value can be converted exactly to the target type
- *
- * */
- public static boolean byte_char(byte n) {return n == (char) n;}
-
- /** Exactness method from short to byte
- *
- * @param n value
- * @return true if the passed value can be converted exactly to the target type
- *
- * */
- public static boolean short_byte(short n) {return n == (short)(byte)(n);}
-
- /** Exactness method from short to char
- *
- * @param n value
- * @return true if the passed value can be converted exactly to the target type
- *
- * */
- public static boolean short_char(short n) {return n == (char)(n);}
-
- /** Exactness method from char to byte
- *
- * @param n value
- * @return true if the passed value can be converted exactly to the target type
- *
- * */
- public static boolean char_byte(char n) {return n == (byte)(n);}
-
- /** Exactness method from char to short
- *
- * @param n value
- * @return true if the passed value can be converted exactly to the target type
- *
- * */
- public static boolean char_short(char n) {return n == (short)(n);}
-
/** Exactness method from int to byte
*
* @param n value
Index: src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java (revision 1ae06ac8f8cc0c0ce70920c2a62a7c13fdfc6f23)
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Lower.java (date 1695992937983)
@@ -103,6 +103,7 @@
private final PkgInfo pkginfoOpt;
private final boolean optimizeOuterThis;
private final boolean useMatchException;
+ private final HashMap<TypePairs, String> typePairToName;
@SuppressWarnings("this-escape")
protected Lower(Context context) {
@@ -134,6 +135,7 @@
Preview preview = Preview.instance(context);
useMatchException = Feature.PATTERN_SWITCH.allowedInSource(source) &&
(preview.isEnabled() || !preview.isPreview(Feature.PATTERN_SWITCH));
+ typePairToName = TypePairs.initialize(syms);
}
/** The currently enclosing class.
@@ -2996,15 +2998,81 @@
}
}
+ static class TypePairs {
+ public Type from, to;
+
+ public static TypePairs of(Symtab syms, Type from, Type to) {
+ if (from == syms.byteType || from == syms.shortType || from == syms.charType) {
+ from = syms.intType;
+ }
+ return new TypePairs(from, to);
+ }
+
+ private TypePairs(Type from, Type to) {
+ this.from = from;
+ this.to = to;
+ }
+
+ public static HashMap<TypePairs, String> initialize(Symtab syms) {
+ HashMap<TypePairs, String> typePairToName = new HashMap<>();
+ typePairToName.put(new TypePairs(syms.byteType, syms.charType), "int_char"); // redirected
+ typePairToName.put(new TypePairs(syms.shortType, syms.byteType), "int_byte"); // redirected
+ typePairToName.put(new TypePairs(syms.shortType, syms.charType), "int_char"); // redirected
+ typePairToName.put(new TypePairs(syms.charType, syms.byteType), "int_byte"); // redirected
+ typePairToName.put(new TypePairs(syms.charType, syms.shortType), "int_short"); // redirected
+ typePairToName.put(new TypePairs(syms.intType, syms.byteType), "int_byte");
+ typePairToName.put(new TypePairs(syms.intType, syms.shortType), "int_short");
+ typePairToName.put(new TypePairs(syms.intType, syms.charType), "int_char");
+ typePairToName.put(new TypePairs(syms.intType, syms.floatType), "int_float");
+ typePairToName.put(new TypePairs(syms.longType, syms.byteType), "long_byte");
+ typePairToName.put(new TypePairs(syms.longType, syms.shortType), "long_short");
+ typePairToName.put(new TypePairs(syms.longType, syms.charType), "long_char");
+ typePairToName.put(new TypePairs(syms.longType, syms.intType), "long_int");
+ typePairToName.put(new TypePairs(syms.longType, syms.floatType), "long_float");
+ typePairToName.put(new TypePairs(syms.longType, syms.doubleType), "long_double");
+ typePairToName.put(new TypePairs(syms.floatType, syms.byteType), "float_byte");
+ typePairToName.put(new TypePairs(syms.floatType, syms.shortType), "float_short");
+ typePairToName.put(new TypePairs(syms.floatType, syms.charType), "float_char");
+ typePairToName.put(new TypePairs(syms.floatType, syms.intType), "float_int");
+ typePairToName.put(new TypePairs(syms.floatType, syms.longType), "float_long");
+ typePairToName.put(new TypePairs(syms.doubleType, syms.byteType), "double_byte");
+ typePairToName.put(new TypePairs(syms.doubleType, syms.shortType), "double_short");
+ typePairToName.put(new TypePairs(syms.doubleType, syms.charType), "double_char");
+ typePairToName.put(new TypePairs(syms.doubleType, syms.intType), "double_int");
+ typePairToName.put(new TypePairs(syms.doubleType, syms.longType), "double_long");
+ typePairToName.put(new TypePairs(syms.doubleType, syms.floatType), "double_float");
+ return typePairToName;
+ }
+
+ @Override
+ public int hashCode() {
+ int code = 0;
+ code += from.tsym.hashCode();
+ code += to.tsym.hashCode();
+ return code;
+ }
+
+ @Override
+ public boolean equals(Object testName) {
+ if ((!(testName instanceof TypePairs testNameAsName))) return false;
+ else {
+ return this.from.tsym.equals(testNameAsName.from.tsym) &&
+ this.to.tsym.equals(testNameAsName.to.tsym);
+ }
+ }
+ }
+
private JCExpression getExactnessCheck(JCInstanceOf tree, JCExpression argument) {
- Name exactnessFunction = names.fromString(types.unboxedTypeOrType(tree.expr.type).tsym.name.toString() + "_"+ tree.pattern.type.toString());
+ TypePairs pair = TypePairs.of(syms, types.unboxedTypeOrType(tree.expr.type), tree.pattern.type);
+
+ Name exactnessFunction = names.fromString(typePairToName.get(pair));
// Resolve the exactness method
Symbol ecsym = rs.resolveQualifiedMethod(null,
attrEnv,
syms.exactnessMethodsType,
exactnessFunction,
- List.of(tree.expr.type),
+ List.of(pair.from),
List.nil());
// Generate the method call ExactnessChecks.<exactness method>(<argument>);
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15638#issuecomment-1741006395
More information about the compiler-dev
mailing list