Skip to content

Commit

Permalink
Add methods for emitting bitwise AND, OR, XOR
Browse files Browse the repository at this point in the history
  • Loading branch information
Ladicek committed Oct 18, 2022
1 parent 25e1f1f commit d242b19
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/main/java/io/quarkus/gizmo/BytecodeCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/io/quarkus/gizmo/BytecodeCreatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand Down

0 comments on commit d242b19

Please sign in to comment.