< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/ProgrammableUpcallHandler.java

Print this page

 72     @Override
 73     public long entryPoint() {
 74         return entryPoint;
 75     }
 76 
 77     public static void invoke(ProgrammableUpcallHandler handler, long address) {
 78         handler.invoke(MemoryAddress.ofLong(address));
 79     }
 80 
 81     private void invoke(MemoryAddress buffer) {
 82         try {
 83             if (DEBUG) {
 84                 System.err.println("Buffer state before:");
 85                 layout.dump(abi.arch, buffer, System.err);
 86             }
 87 
 88             MemoryAddress bufferBase = MemoryAddressImpl.ofLongUnchecked(buffer.toRawLongValue(), layout.size);
 89             MemoryAddress stackArgsBase = MemoryAddressImpl.ofLongUnchecked((long)VH_LONG.get(buffer.rebase(bufferBase.segment()).addOffset(layout.stack_args)));
 90             Object[] args = new Object[type.parameterCount()];
 91             for (int i = 0 ; i < type.parameterCount() ; i++) {
 92                 args[i] = jdk.internal.foreign.abi.BindingInterpreter.box(callingSequence.argumentBindings(i),
 93                         s -> abi.arch.isStackType(s.type())
 94                             ? stackArgsBase.addOffset(s.index() * abi.arch.typeSize(abi.arch.stackType()))
 95                             : bufferBase.addOffset(layout.argOffset(s)));



 96             }
 97 
 98             if (DEBUG) {
 99                 System.err.println("Java arguments:");
100                 System.err.println(Arrays.toString(args).indent(2));
101             }
102 
103             Object o = mh.invoke(args);
104 
105             if (DEBUG) {
106                 System.err.println("Java return:");
107                 System.err.println(Objects.toString(o).indent(2));
108             }
109 
110             if (mh.type().returnType() != void.class) {
111                 jdk.internal.foreign.abi.BindingInterpreter.unbox(o,
112                         callingSequence.returnBindings(), s -> bufferBase.addOffset(layout.retOffset(s)), new ArrayList<>());



113             }
114 
115             if (DEBUG) {
116                 System.err.println("Buffer state after:");
117                 layout.dump(abi.arch, buffer, System.err);
118             }
119         } catch (Throwable t) {
120             throw new IllegalStateException(t);
121         }
122     }
123 
124     public native long allocateUpcallStub(ABIDescriptor abi, BufferLayout layout);
125 
126     private static native void registerNatives();
127     static {
128         registerNatives();
129     }
130 }

 72     @Override
 73     public long entryPoint() {
 74         return entryPoint;
 75     }
 76 
 77     public static void invoke(ProgrammableUpcallHandler handler, long address) {
 78         handler.invoke(MemoryAddress.ofLong(address));
 79     }
 80 
 81     private void invoke(MemoryAddress buffer) {
 82         try {
 83             if (DEBUG) {
 84                 System.err.println("Buffer state before:");
 85                 layout.dump(abi.arch, buffer, System.err);
 86             }
 87 
 88             MemoryAddress bufferBase = MemoryAddressImpl.ofLongUnchecked(buffer.toRawLongValue(), layout.size);
 89             MemoryAddress stackArgsBase = MemoryAddressImpl.ofLongUnchecked((long)VH_LONG.get(buffer.rebase(bufferBase.segment()).addOffset(layout.stack_args)));
 90             Object[] args = new Object[type.parameterCount()];
 91             for (int i = 0 ; i < type.parameterCount() ; i++) {
 92                 args[i] = BindingInterpreter.box(callingSequence.argumentBindings(i),
 93                         (storage, type) -> {
 94                             MemoryAddress ptr = abi.arch.isStackType(storage.type())
 95                                 ? stackArgsBase.addOffset(storage.index() * abi.arch.typeSize(abi.arch.stackType()))
 96                                 : bufferBase.addOffset(layout.argOffset(storage));
 97                             return SharedUtils.read(ptr, type);
 98                         });
 99             }
100 
101             if (DEBUG) {
102                 System.err.println("Java arguments:");
103                 System.err.println(Arrays.toString(args).indent(2));
104             }
105 
106             Object o = mh.invoke(args);
107 
108             if (DEBUG) {
109                 System.err.println("Java return:");
110                 System.err.println(Objects.toString(o).indent(2));
111             }
112 
113             if (mh.type().returnType() != void.class) {
114                 BindingInterpreter.unbox(o, callingSequence.returnBindings(),
115                         (storage, type, value) -> {
116                             MemoryAddress ptr = bufferBase.addOffset(layout.retOffset(storage));
117                             SharedUtils.writeOverSized(ptr, type, value);
118                         }, null);
119             }
120 
121             if (DEBUG) {
122                 System.err.println("Buffer state after:");
123                 layout.dump(abi.arch, buffer, System.err);
124             }
125         } catch (Throwable t) {
126             throw new IllegalStateException(t);
127         }
128     }
129 
130     public native long allocateUpcallStub(ABIDescriptor abi, BufferLayout layout);
131 
132     private static native void registerNatives();
133     static {
134         registerNatives();
135     }
136 }
< prev index next >