Skip to content

Commit

Permalink
8304283: Modernize the switch statements in jdk.internal.foreign
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore
  • Loading branch information
minborg committed Mar 16, 2023
1 parent 7277bb1 commit dfc7214
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 205 deletions.
69 changes: 4 additions & 65 deletions src/java.base/share/classes/jdk/internal/foreign/abi/Binding.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, 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
Expand Down Expand Up @@ -197,7 +197,7 @@
*
* --------------------
*/
public interface Binding {
public sealed interface Binding {

/**
* A binding context is used as an helper to carry out evaluation of certain bindings; for instance,
Expand Down Expand Up @@ -292,21 +292,6 @@ public void close() {
};
}

enum Tag {
VM_STORE,
VM_LOAD,
BUFFER_STORE,
BUFFER_LOAD,
COPY_BUFFER,
ALLOC_BUFFER,
BOX_ADDRESS,
UNBOX_ADDRESS,
DUP,
CAST
}

Tag tag();

void verify(Deque<Class<?>> stack);

void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFunc,
Expand Down Expand Up @@ -503,7 +488,7 @@ public List<Binding> build() {
}
}

interface Move extends Binding {
sealed interface Move extends Binding {
VMStorage storage();
Class<?> type();
}
Expand All @@ -514,10 +499,6 @@ interface Move extends Binding {
* The [type] must be one of byte, short, char, int, long, float, or double
*/
record VMStore(VMStorage storage, Class<?> type) implements Move {
@Override
public Tag tag() {
return Tag.VM_STORE;
}

@Override
public void verify(Deque<Class<?>> stack) {
Expand All @@ -539,10 +520,6 @@ public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFun
* The [type] must be one of byte, short, char, int, long, float, or double
*/
record VMLoad(VMStorage storage, Class<?> type) implements Move {
@Override
public Tag tag() {
return Tag.VM_LOAD;
}

@Override
public void verify(Deque<Class<?>> stack) {
Expand All @@ -556,7 +533,7 @@ public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFun
}
}

interface Dereference extends Binding {
sealed interface Dereference extends Binding {
long offset();
Class<?> type();
}
Expand All @@ -568,10 +545,6 @@ interface Dereference extends Binding {
* The [type] must be one of byte, short, char, int, long, float, or double
*/
record BufferStore(long offset, Class<?> type, int byteWidth) implements Dereference {
@Override
public Tag tag() {
return Tag.BUFFER_STORE;
}

@Override
public void verify(Deque<Class<?>> stack) {
Expand Down Expand Up @@ -629,10 +602,6 @@ public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFun
* The [type] must be one of byte, short, char, int, long, float, or double
*/
record BufferLoad(long offset, Class<?> type, int byteWidth) implements Dereference {
@Override
public Tag tag() {
return Tag.BUFFER_LOAD;
}

@Override
public void verify(Deque<Class<?>> stack) {
Expand Down Expand Up @@ -693,11 +662,6 @@ private static MemorySegment copyBuffer(MemorySegment operand, long size, long a
.copyFrom(operand.asSlice(0, size));
}

@Override
public Tag tag() {
return Tag.COPY_BUFFER;
}

@Override
public void verify(Deque<Class<?>> stack) {
Class<?> actualType = stack.pop();
Expand All @@ -723,11 +687,6 @@ private static MemorySegment allocateBuffer(long size, long alignment, Context c
return context.allocator().allocate(size, alignment);
}

@Override
public Tag tag() {
return Tag.ALLOC_BUFFER;
}

@Override
public void verify(Deque<Class<?>> stack) {
stack.push(MemorySegment.class);
Expand All @@ -748,11 +707,6 @@ public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFun
record UnboxAddress() implements Binding {
static final UnboxAddress INSTANCE = new UnboxAddress();

@Override
public Tag tag() {
return Tag.UNBOX_ADDRESS;
}

@Override
public void verify(Deque<Class<?>> stack) {
Class<?> actualType = stack.pop();
Expand All @@ -774,11 +728,6 @@ public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFun
*/
record BoxAddress(long size, boolean needsScope) implements Binding {

@Override
public Tag tag() {
return Tag.BOX_ADDRESS;
}

@Override
public void verify(Deque<Class<?>> stack) {
Class<?> actualType = stack.pop();
Expand All @@ -803,11 +752,6 @@ public void interpret(Deque<Object> stack, BindingInterpreter.StoreFunc storeFun
record Dup() implements Binding {
static final Dup INSTANCE = new Dup();

@Override
public Tag tag() {
return Tag.DUP;
}

@Override
public void verify(Deque<Class<?>> stack) {
stack.push(stack.peekLast());
Expand Down Expand Up @@ -861,11 +805,6 @@ public Class<?> toType() {
return toType;
}

@Override
public Tag tag() {
return Tag.CAST;
}

@Override
public void verify(Deque<Class<?>> stack) {
Class<?> actualType = stack.pop();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 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
Expand Down Expand Up @@ -28,6 +28,16 @@
import jdk.internal.foreign.MemorySessionImpl;
import jdk.internal.foreign.NativeMemorySegmentImpl;
import jdk.internal.foreign.Utils;
import jdk.internal.foreign.abi.Binding.Allocate;
import jdk.internal.foreign.abi.Binding.BoxAddress;
import jdk.internal.foreign.abi.Binding.BufferLoad;
import jdk.internal.foreign.abi.Binding.BufferStore;
import jdk.internal.foreign.abi.Binding.Cast;
import jdk.internal.foreign.abi.Binding.Copy;
import jdk.internal.foreign.abi.Binding.Dup;
import jdk.internal.foreign.abi.Binding.UnboxAddress;
import jdk.internal.foreign.abi.Binding.VMLoad;
import jdk.internal.foreign.abi.Binding.VMStore;
import jdk.internal.misc.VM;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassWriter;
Expand Down Expand Up @@ -437,9 +447,9 @@ private void specialize() {

private boolean needsSession() {
return callingSequence.argumentBindings()
.filter(Binding.BoxAddress.class::isInstance)
.map(Binding.BoxAddress.class::cast)
.anyMatch(Binding.BoxAddress::needsScope);
.filter(BoxAddress.class::isInstance)
.map(BoxAddress.class::cast)
.anyMatch(BoxAddress::needsScope);
}

private boolean shouldAcquire(int paramIndex) {
Expand Down Expand Up @@ -467,17 +477,17 @@ private void emitCleanup() {

private void doBindings(List<Binding> bindings) {
for (Binding binding : bindings) {
switch (binding.tag()) {
case VM_STORE -> emitVMStore((Binding.VMStore) binding);
case VM_LOAD -> emitVMLoad((Binding.VMLoad) binding);
case BUFFER_STORE -> emitBufferStore((Binding.BufferStore) binding);
case BUFFER_LOAD -> emitBufferLoad((Binding.BufferLoad) binding);
case COPY_BUFFER -> emitCopyBuffer((Binding.Copy) binding);
case ALLOC_BUFFER -> emitAllocBuffer((Binding.Allocate) binding);
case BOX_ADDRESS -> emitBoxAddress((Binding.BoxAddress) binding);
case UNBOX_ADDRESS -> emitUnboxAddress();
case DUP -> emitDupBinding();
case CAST -> emitCast((Binding.Cast) binding);
switch (binding) {
case VMStore vmStore -> emitVMStore(vmStore);
case VMLoad vmLoad -> emitVMLoad(vmLoad);
case BufferStore bufferStore -> emitBufferStore(bufferStore);
case BufferLoad bufferLoad -> emitBufferLoad(bufferLoad);
case Copy copy -> emitCopyBuffer(copy);
case Allocate allocate -> emitAllocBuffer(allocate);
case BoxAddress boxAddress -> emitBoxAddress(boxAddress);
case UnboxAddress unused -> emitUnboxAddress();
case Dup unused -> emitDupBinding();
case Cast cast -> emitCast(cast);
}
}
}
Expand Down Expand Up @@ -579,7 +589,7 @@ private void emitCloseContext() {
emitInvokeVirtual(Binding.Context.class, "close", CLOSE_DESC);
}

private void emitBoxAddress(Binding.BoxAddress boxAddress) {
private void emitBoxAddress(BoxAddress boxAddress) {
popType(long.class);
emitConst(boxAddress.size());
if (needsSession()) {
Expand All @@ -591,7 +601,7 @@ private void emitBoxAddress(Binding.BoxAddress boxAddress) {
pushType(MemorySegment.class);
}

private void emitAllocBuffer(Binding.Allocate binding) {
private void emitAllocBuffer(Allocate binding) {
if (callingSequence.forDowncall()) {
assert returnAllocatorIdx != -1;
emitLoad(Object.class, returnAllocatorIdx);
Expand All @@ -602,7 +612,7 @@ private void emitAllocBuffer(Binding.Allocate binding) {
pushType(MemorySegment.class);
}

private void emitBufferStore(Binding.BufferStore bufferStore) {
private void emitBufferStore(BufferStore bufferStore) {
Class<?> storeType = bufferStore.type();
long offset = bufferStore.offset();
int byteWidth = bufferStore.byteWidth();
Expand Down Expand Up @@ -638,15 +648,15 @@ private void emitBufferStore(Binding.BufferStore bufferStore) {
Class<?> chunkStoreType;
long mask;
switch (chunkSize) {
case 4 -> {
case Integer.BYTES -> {
chunkStoreType = int.class;
mask = 0xFFFF_FFFFL;
}
case 2 -> {
case Short.BYTES -> {
chunkStoreType = short.class;
mask = 0xFFFFL;
}
case 1 -> {
case Byte.BYTES -> {
chunkStoreType = byte.class;
mask = 0xFFL;
}
Expand Down Expand Up @@ -684,7 +694,7 @@ private void emitBufferStore(Binding.BufferStore bufferStore) {
}

// VM_STORE and VM_LOAD are emulated, which is different for down/upcalls
private void emitVMStore(Binding.VMStore vmStore) {
private void emitVMStore(VMStore vmStore) {
Class<?> storeType = vmStore.type();
popType(storeType);

Expand All @@ -711,7 +721,7 @@ private void emitVMStore(Binding.VMStore vmStore) {
}
}

private void emitVMLoad(Binding.VMLoad vmLoad) {
private void emitVMLoad(VMLoad vmLoad) {
Class<?> loadType = vmLoad.type();

if (callingSequence.forDowncall()) {
Expand Down Expand Up @@ -740,7 +750,7 @@ private void emitDupBinding() {
pushType(dupType);
}

private void emitCast(Binding.Cast cast) {
private void emitCast(Cast cast) {
Class<?> fromType = cast.fromType();
Class<?> toType = cast.toType();

Expand Down Expand Up @@ -773,7 +783,7 @@ private void emitUnboxAddress() {
pushType(long.class);
}

private void emitBufferLoad(Binding.BufferLoad bufferLoad) {
private void emitBufferLoad(BufferLoad bufferLoad) {
Class<?> loadType = bufferLoad.type();
long offset = bufferLoad.offset();
int byteWidth = bufferLoad.byteWidth();
Expand Down Expand Up @@ -802,17 +812,17 @@ private void emitBufferLoad(Binding.BufferLoad bufferLoad) {
Class<?> toULongHolder;
String toULongDescriptor;
switch (chunkSize) {
case 4 -> {
case Integer.BYTES -> {
chunkType = int.class;
toULongHolder = Integer.class;
toULongDescriptor = INTEGER_TO_UNSIGNED_LONG_DESC;
}
case 2 -> {
case Short.BYTES -> {
chunkType = short.class;
toULongHolder = Short.class;
toULongDescriptor = SHORT_TO_UNSIGNED_LONG_DESC;
}
case 1 -> {
case Byte.BYTES -> {
chunkType = byte.class;
toULongHolder = Byte.class;
toULongDescriptor = BYTE_TO_UNSIGNED_LONG_DESC;
Expand Down Expand Up @@ -855,7 +865,7 @@ private void emitBufferLoad(Binding.BufferLoad bufferLoad) {
pushType(loadType);
}

private void emitCopyBuffer(Binding.Copy copy) {
private void emitCopyBuffer(Copy copy) {
long size = copy.size();
long alignment = copy.alignment();

Expand Down
Loading

1 comment on commit dfc7214

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.