Skip to content

Commit

Permalink
Convert classes into records
Browse files Browse the repository at this point in the history
Reviewed-by: jvernee, sundar
  • Loading branch information
minborg authored and sundararajana committed Sep 22, 2022
1 parent b30ae8f commit 5b63be8
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 202 deletions.
91 changes: 16 additions & 75 deletions src/java.base/share/classes/jdk/internal/foreign/abi/VMStorage.java
Expand Up @@ -24,82 +24,23 @@
*/
package jdk.internal.foreign.abi;

import java.util.Objects;

public class VMStorage {
/**
* Type of storage. e.g. stack, or which register type (GP, FP, vector)
*/
private final byte type;

/**
* The (on stack) size in bytes when type = stack, a register mask otherwise.
* The register mask indicates which segments of a register are used.
*/
private final short segmentMaskOrSize;

/**
* The index is either a register number within a type, or
* a stack offset in bytes if type = stack.
* (a particular platform might add a bias to this in generate code)
*/
private final int indexOrOffset;

private final String debugName;

private VMStorage(byte type, short segmentMaskOrSize, int indexOrOffset, String debugName) {
this.type = type;
this.segmentMaskOrSize = segmentMaskOrSize;
this.indexOrOffset = indexOrOffset;
this.debugName = debugName;
}

public static VMStorage stackStorage(byte type, short size, int byteOffset) {
return new VMStorage(type, size, byteOffset, "Stack@" + byteOffset);
}

public static VMStorage regStorage(byte type, short segmentMask, int index, String debugName) {
return new VMStorage(type, segmentMask, index, debugName);
}

public byte type() {
return type;
}

public short segmentMaskOrSize() {
return segmentMaskOrSize;
}

public int indexOrOffset() {
return indexOrOffset;
}

public String name() {
return debugName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
return (o instanceof VMStorage vmStorage)
&& type == vmStorage.type
&& segmentMaskOrSize == vmStorage.segmentMaskOrSize
&& indexOrOffset == vmStorage.indexOrOffset
&& Objects.equals(debugName, vmStorage.debugName);
}
/**
*
* @param type the type of storage. e.g. stack, or which register type (GP, FP, vector)
* @param segmentMaskOrSize the (on stack) size in bytes when type = stack, a register mask otherwise,
* the register mask indicates which segments of a register are used.
* @param indexOrOffset the index is either a register number within a type, or
* a stack offset in bytes if type = stack.
* (a particular platform might add a bias to this in generate code)
* @param debugName the debug name
*/
public record VMStorage(byte type,
short segmentMaskOrSize,
int indexOrOffset,
String debugName) {

@Override
public int hashCode() {
return Objects.hash(type, segmentMaskOrSize, indexOrOffset, debugName);
public VMStorage(byte type, short segmentMaskOrSize, int indexOrOffset) {
this(type, segmentMaskOrSize, indexOrOffset, "Stack@" + indexOrOffset);
}

@Override
public String toString() {
return "VMStorage{" +
"type=" + type +
", segmentMaskOrSize=" + segmentMaskOrSize +
", indexOrOffset=" + indexOrOffset +
", debugName='" + debugName + '\'' +
'}';
}
}
Expand Up @@ -129,15 +129,15 @@ public static class Regs { // break circular dependency
}

private static VMStorage integerRegister(int index) {
return VMStorage.regStorage(StorageClasses.INTEGER, REG64_MASK, index, "r" + index);
return new VMStorage(StorageClasses.INTEGER, REG64_MASK, index, "r" + index);
}

private static VMStorage vectorRegister(int index) {
return VMStorage.regStorage(StorageClasses.VECTOR, V128_MASK, index, "v" + index);
return new VMStorage(StorageClasses.VECTOR, V128_MASK, index, "v" + index);
}

public static VMStorage stackStorage(short size, int byteOffset) {
return VMStorage.stackStorage(StorageClasses.STACK, size, byteOffset);
return new VMStorage(StorageClasses.STACK, size, byteOffset);
}

public static ABIDescriptor abiFor(VMStorage[] inputIntRegs,
Expand Down
Expand Up @@ -92,15 +92,8 @@ public abstract class CallArranger {
r10 // return buffer addr reg
);

// record
public static class Bindings {
public final CallingSequence callingSequence;
public final boolean isInMemoryReturn;

Bindings(CallingSequence callingSequence, boolean isInMemoryReturn) {
this.callingSequence = callingSequence;
this.isInMemoryReturn = isInMemoryReturn;
}
public record Bindings(CallingSequence callingSequence,
boolean isInMemoryReturn) {
}

public static final CallArranger LINUX = new LinuxAArch64CallArranger();
Expand Down
Expand Up @@ -124,19 +124,19 @@ public static class Regs { // break circular dependency
}

private static VMStorage integerRegister(int index, String debugName) {
return VMStorage.regStorage(StorageClasses.INTEGER, REG64_MASK, index, debugName);
return new VMStorage(StorageClasses.INTEGER, REG64_MASK, index, debugName);
}

private static VMStorage vectorRegister(int index, String debugName) {
return VMStorage.regStorage(StorageClasses.VECTOR, XMM_MASK, index, debugName);
return new VMStorage(StorageClasses.VECTOR, XMM_MASK, index, debugName);
}

public static VMStorage stackStorage(short size, int byteOffset) {
return VMStorage.stackStorage(StorageClasses.STACK, size, byteOffset);
return new VMStorage(StorageClasses.STACK, size, byteOffset);
}

public static VMStorage x87Storage(int index) {
return VMStorage.regStorage(StorageClasses.X87, STP_MASK, index, "X87(" + index + ")");
return new VMStorage(StorageClasses.X87, STP_MASK, index, "X87(" + index + ")");
}

public static ABIDescriptor abiFor(VMStorage[] inputIntRegs, VMStorage[] inputVectorRegs, VMStorage[] outputIntRegs,
Expand Down
Expand Up @@ -77,17 +77,10 @@ public class CallArranger {
r11 // ret buf addr reg
);

// record
public static class Bindings {
public final CallingSequence callingSequence;
public final boolean isInMemoryReturn;
public final int nVectorArgs;

Bindings(CallingSequence callingSequence, boolean isInMemoryReturn, int nVectorArgs) {
this.callingSequence = callingSequence;
this.isInMemoryReturn = isInMemoryReturn;
this.nVectorArgs = nVectorArgs;
}
public record Bindings(
CallingSequence callingSequence,
boolean isInMemoryReturn,
int nVectorArgs) {
}

public static Bindings getBindings(MethodType mt, FunctionDescriptor cDesc, boolean forUpcall) {
Expand Down
Expand Up @@ -73,15 +73,9 @@ public class CallArranger {
r11 // ret buf addr reg
);

// record
public static class Bindings {
public final CallingSequence callingSequence;
public final boolean isInMemoryReturn;

Bindings(CallingSequence callingSequence, boolean isInMemoryReturn) {
this.callingSequence = callingSequence;
this.isInMemoryReturn = isInMemoryReturn;
}
public record Bindings(
CallingSequence callingSequence,
boolean isInMemoryReturn) {
}

public static Bindings getBindings(MethodType mt, FunctionDescriptor cDesc, boolean forUpcall) {
Expand Down
Expand Up @@ -38,7 +38,7 @@ enum TypeClass {
VARARG_FLOAT;

private static TypeClass classifyValueType(ValueLayout type, boolean isVararg) {
// No 128 bit integers in the Windows C ABI. There are __m128(i|d) intrinsic types but they act just
// No 128-bit integers in the Windows C ABI. There are __m128(i|d) intrinsic types but they act just
// like a struct when passing as an argument (passed by pointer).
// https://docs.microsoft.com/en-us/cpp/cpp/m128?view=vs-2019

Expand Down
48 changes: 24 additions & 24 deletions test/jdk/java/foreign/callarranger/TestAarch64CallArranger.java
Expand Up @@ -62,8 +62,8 @@ public void testEmpty() {
FunctionDescriptor fd = FunctionDescriptor.ofVoid();
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS));

Expand All @@ -86,8 +86,8 @@ public void testInteger() {
C_INT, C_INT);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS));

Expand Down Expand Up @@ -116,8 +116,8 @@ public void testTwoIntTwoFloat() {
C_INT, C_INT, C_FLOAT, C_FLOAT);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS));

Expand All @@ -138,8 +138,8 @@ public void testStruct(MemoryLayout struct, Binding[] expectedBindings) {
FunctionDescriptor fd = FunctionDescriptor.ofVoid(struct);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS));

Expand Down Expand Up @@ -198,8 +198,8 @@ public void testMultipleStructs() {
FunctionDescriptor fd = FunctionDescriptor.ofVoid(struct1, struct2, C_INT);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS));

Expand Down Expand Up @@ -229,8 +229,8 @@ public void testReturnStruct1() {
FunctionDescriptor fd = FunctionDescriptor.of(struct);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertTrue(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertTrue(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), MethodType.methodType(void.class, MemorySegment.class, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), FunctionDescriptor.ofVoid(ADDRESS, C_POINTER));

Expand All @@ -253,8 +253,8 @@ public void testReturnStruct2() {
FunctionDescriptor fd = FunctionDescriptor.of(struct);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS, ADDRESS));

Expand Down Expand Up @@ -282,8 +282,8 @@ public void testStructHFA1() {
FunctionDescriptor fd = FunctionDescriptor.of(hfa, C_FLOAT, C_INT, hfa);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS, ADDRESS));

Expand Down Expand Up @@ -320,8 +320,8 @@ public void testStructHFA3() {
FunctionDescriptor fd = FunctionDescriptor.ofVoid(struct, struct, struct);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS));

Expand Down Expand Up @@ -374,8 +374,8 @@ public void testStructStackSpill() {
struct, struct, C_INT, C_INT, C_INT, C_INT, C_INT, C_INT, struct, C_INT);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fd.insertArgumentLayouts(0, ADDRESS));

Expand Down Expand Up @@ -403,8 +403,8 @@ public void testVarArgsInRegs() {
FunctionDescriptor fdExpected = FunctionDescriptor.ofVoid(ADDRESS, C_INT, C_INT, C_FLOAT);
CallArranger.Bindings bindings = CallArranger.LINUX.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fdExpected);

Expand All @@ -426,8 +426,8 @@ public void testVarArgsOnStack() {
FunctionDescriptor fdExpected = FunctionDescriptor.ofVoid(ADDRESS, C_INT, C_INT, C_FLOAT);
CallArranger.Bindings bindings = CallArranger.MACOS.getBindings(mt, fd, false);

assertFalse(bindings.isInMemoryReturn);
CallingSequence callingSequence = bindings.callingSequence;
assertFalse(bindings.isInMemoryReturn());
CallingSequence callingSequence = bindings.callingSequence();
assertEquals(callingSequence.callerMethodType(), mt.insertParameterTypes(0, MemorySegment.class));
assertEquals(callingSequence.functionDesc(), fdExpected);

Expand Down

0 comments on commit 5b63be8

Please sign in to comment.