From d242b190c387a4e2d564f7100c9e7176e1753fbe Mon Sep 17 00:00:00 2001 From: Ladislav Thon Date: Tue, 18 Oct 2022 17:14:31 +0200 Subject: [PATCH] Add methods for emitting bitwise AND, OR, XOR --- .../io/quarkus/gizmo/BytecodeCreator.java | 27 +++++++++++++++++++ .../io/quarkus/gizmo/BytecodeCreatorImpl.java | 22 ++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/quarkus/gizmo/BytecodeCreator.java b/src/main/java/io/quarkus/gizmo/BytecodeCreator.java index f493d8e..e1fdf44 100644 --- a/src/main/java/io/quarkus/gizmo/BytecodeCreator.java +++ b/src/main/java/io/quarkus/gizmo/BytecodeCreator.java @@ -996,6 +996,33 @@ default void breakScope() { */ ResultHandle multiply(ResultHandle a1, ResultHandle a2); + /** + * Computes the bitwise AND of the two result handles and returns the result + * + * @param a1 The first number + * @param a2 The second number + * @return The result + */ + ResultHandle bitwiseAnd(ResultHandle a1, ResultHandle a2); + + /** + * Computes the bitwise OR of the two result handles and returns the result + * + * @param a1 The first number + * @param a2 The second number + * @return The result + */ + ResultHandle bitwiseOr(ResultHandle a1, ResultHandle a2); + + /** + * Computes the bitwise XOR of the two result handles and returns the result + * + * @param a1 The first number + * @param a2 The second number + * @return The result + */ + ResultHandle bitwiseXor(ResultHandle a1, ResultHandle a2); + /** * Increments a ResultHandle * diff --git a/src/main/java/io/quarkus/gizmo/BytecodeCreatorImpl.java b/src/main/java/io/quarkus/gizmo/BytecodeCreatorImpl.java index 4d7085b..832ef27 100644 --- a/src/main/java/io/quarkus/gizmo/BytecodeCreatorImpl.java +++ b/src/main/java/io/quarkus/gizmo/BytecodeCreatorImpl.java @@ -27,7 +27,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -1249,15 +1248,30 @@ public ForEachLoop forEach(ResultHandle iterable) { @Override public ResultHandle add(ResultHandle a1, ResultHandle a2) { - return emitArithmetic(Opcodes.IADD, a1, a2); + return emitBinaryArithmetic(Opcodes.IADD, a1, a2); } @Override public ResultHandle multiply(ResultHandle a1, ResultHandle a2) { - return emitArithmetic(Opcodes.IMUL, a1, a2); + return emitBinaryArithmetic(Opcodes.IMUL, a1, a2); } - private ResultHandle emitArithmetic(int intOpcode, ResultHandle a1, ResultHandle a2) { + @Override + public ResultHandle bitwiseAnd(ResultHandle a1, ResultHandle a2) { + return emitBinaryArithmetic(Opcodes.IAND, a1, a2); + } + + @Override + public ResultHandle bitwiseOr(ResultHandle a1, ResultHandle a2) { + return emitBinaryArithmetic(Opcodes.IOR, a1, a2); + } + + @Override + public ResultHandle bitwiseXor(ResultHandle a1, ResultHandle a2) { + return emitBinaryArithmetic(Opcodes.IXOR, a1, a2); + } + + private ResultHandle emitBinaryArithmetic(int intOpcode, ResultHandle a1, ResultHandle a2) { Objects.requireNonNull(a1); Objects.requireNonNull(a2); if (!a1.getType().equals(a2.getType())) {