From 9455cfeb8c9097987819ada835b05d98a04abc07 Mon Sep 17 00:00:00 2001
From: Not Zed <notzed@gmail.com>
Date: Mon, 27 Jan 2020 21:04:45 +1030
Subject: [PATCH] Use jextract for the bindings.

---
 config.make.in                                |  2 +
 .../classes/au/notzed/zcl/CLCommandQueue.java |  6 +--
 .../au/notzed/zcl/CLContextNotify.java        |  2 +-
 .../classes/au/notzed/zcl/CLEventNotify.java  |  2 +-
 .../classes/au/notzed/zcl/CLLib.java          | 10 ++++
 .../classes/au/notzed/zcl/CLNotify.java       |  4 +-
 src/notzed.zcl/gen/gen.make                   | 54 +++++++------------
 7 files changed, 38 insertions(+), 42 deletions(-)
 create mode 100644 src/notzed.zcl/classes/au/notzed/zcl/CLLib.java

diff --git a/config.make.in b/config.make.in
index 64bf34f..f05b38a 100644
--- a/config.make.in
+++ b/config.make.in
@@ -12,6 +12,8 @@ JAVAC ?= $(JAVA_HOME)/bin/javac
 JAR ?= $(JAVA_HOME)/bin/jar
 JMOD ?= $(JAVA_HOME)/bin/jmod
 
+JEXTRACT = /opt/panama-jextract/bin/jextract
+
 # Linux options
 linux-amd64_CPPFLAGS = \
 	-I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux
diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java b/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java
index 063b480..9906e20 100644
--- a/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java
+++ b/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java
@@ -1606,7 +1606,7 @@ public class CLCommandQueue extends CLExtendable {
 			}
 
 			Callback<CLNativeKernel> call = Native.resolve(
-				Call_pv_v.stub((MemoryAddress memargs) -> {
+				cl_h.clEnqueueNativeKernel$x0$make((MemoryAddress memargs) -> {
 						int xmem = 0;
 
 						for (int i=0;i<args.length;i++) {
@@ -1659,7 +1659,7 @@ public class CLCommandQueue extends CLExtendable {
 				clEnqueueMarker(addr(), info.event);
 			}
 			info.post(events);
-		} catch (CLException | RuntimeException | Error t) {
+		} catch (RuntimeException | Error t) {
 			throw t;
 		} catch (Throwable t) {
 			throw new RuntimeException(t);
@@ -1690,7 +1690,7 @@ public class CLCommandQueue extends CLExtendable {
 				clEnqueueMarker(addr(), info.event);
 			}
 			info.post(events);
-		} catch (CLException | RuntimeException | Error t) {
+		} catch (RuntimeException | Error t) {
 			throw t;
 		} catch (Throwable t) {
 			throw new RuntimeException(t);
diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java b/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java
index c005742..226d239 100644
--- a/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java
+++ b/src/notzed.zcl/classes/au/notzed/zcl/CLContextNotify.java
@@ -33,7 +33,7 @@ public interface CLContextNotify {
 	static Callback<CLContextNotify> call(CLContextNotify notify) {
 		if (notify != null) {
 			return Native.resolve(
-				Call_pBpvJpv_v.stub((cwhat, cinfo, cinfolen, dummy) -> {
+				cl_h.clCreateContext$x0$make((cwhat, cinfo, cinfolen, dummy) -> {
 						MemorySegment seg = Memory.ofNative(cinfo, cinfolen);
 						notify.notify(Native.toString(cwhat), seg.asByteBuffer());
 					}),
diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLEventNotify.java b/src/notzed.zcl/classes/au/notzed/zcl/CLEventNotify.java
index 95331d1..d17615e 100644
--- a/src/notzed.zcl/classes/au/notzed/zcl/CLEventNotify.java
+++ b/src/notzed.zcl/classes/au/notzed/zcl/CLEventNotify.java
@@ -37,7 +37,7 @@ public interface CLEventNotify {
 	static Callback<CLEventNotify> call(CLEventNotify notify) {
 		if (notify != null) {
 			return Native.resolve(
-				Call_pLIpv_v.stub((cevent, status, dummy) -> notify.notify(Native.resolve(cevent, CLEvent::new), status)),
+				cl_h.clSetEventCallback$x0$make(((cevent, status, dummy) -> notify.notify(Native.resolve(cevent, CLEvent::new), status))),
 				(p) -> new Callback<>(p, notify));
 		} else {
 			return Callback.NULL;
diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLLib.java b/src/notzed.zcl/classes/au/notzed/zcl/CLLib.java
new file mode 100644
index 0000000..2e176fc
--- /dev/null
+++ b/src/notzed.zcl/classes/au/notzed/zcl/CLLib.java
@@ -0,0 +1,10 @@
+
+/*
+ * This just imports the jextract types to the same library so i don't have to
+ * change the imports.
+ */
+package au.notzed.zcl;
+
+public class CLLib extends cl_h {
+
+}
diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLNotify.java b/src/notzed.zcl/classes/au/notzed/zcl/CLNotify.java
index ceb6f8d..462ba76 100644
--- a/src/notzed.zcl/classes/au/notzed/zcl/CLNotify.java
+++ b/src/notzed.zcl/classes/au/notzed/zcl/CLNotify.java
@@ -33,8 +33,10 @@ public interface CLNotify<T> {
 	@SuppressWarnings("unchecked")
 	static <T extends Native> Callback<CLNotify<T>> call(CLNotify<T> notify, Function<MemoryAddress,T> create) {
 		if (notify != null) {
+			// jextract note: this 2-parameter callback is used in a few places, but as it's untyped it
+			// doesn't matter which one is used.
 			return Native.resolve(
-				Call_pLpv_v.stub((co, dummy) -> notify.notify(Native.resolve(co, create))),
+				cl_h.clBuildProgram$x0$make((co, dummy) -> notify.notify(Native.resolve(co, create))),
 				(p) -> new Callback<>(p, notify));
 		} else {
 			return Callback.NULL;
diff --git a/src/notzed.zcl/gen/gen.make b/src/notzed.zcl/gen/gen.make
index 2be9c5f..a7a4ceb 100644
--- a/src/notzed.zcl/gen/gen.make
+++ b/src/notzed.zcl/gen/gen.make
@@ -1,42 +1,24 @@
 
-export_defines=src/notzed.zcl/gen/export-defines
-generate_api=src/notzed.zcl/gen/generate-api
-opencl_pm=src/notzed.zcl/gen/opencl.pm
-
-# this is in src/notzed.zcl/classes already
-notzed.zcl_defines = 				\
- au/notzed/zcl/CL.java
-
 notzed.zcl_generated = 				\
- au/notzed/zcl/CLLib.java			\
- au/notzed/zcl/Call_pBpvJpv_v.java		\
- au/notzed/zcl/Call_pLIppvpv_v.java		\
- au/notzed/zcl/Call_pLIpv_v.java		\
- au/notzed/zcl/Call_pLpv_v.java			\
- au/notzed/zcl/Call_pv_v.java
+ au/notzed/zcl/cl_h.java			\
+ au/notzed/zcl/RuntimeHelper.java
 
 notzed.zcl_JAVA_GENERATED =			\
  $(notzed.zcl_generated)
 
-$(notzed.zcl_genjavadir)/au/notzed/zcl/CL.java: src/notzed.zcl/include/CL/cl.h $(export_defines)
-	@install -d $(@D)
-	perl $(export_defines) $< > $@ || rm -f $@
-
-$(addprefix $(notzed.zcl_genjavadir)/,$(notzed.zcl_generated)): $(generate_api) $(opencl_pm)
-	perl $(generate_api) \
-		-d $(notzed.zcl_genjavadir) \
-		-t au.notzed.zcl \
-		-r _cl_platform_id=CLPlatform \
-		-r _cl_device_id=CLDevice \
-		-r _cl_context=CLContext \
-		-r _cl_command_queue=CLCommandQueue \
-		-r _cl_program=CLProgram \
-		-r _cl_kernel=CLKernel \
-		-r _cl_event=CLEvent \
-		-r _cl_mem=CLMemory \
-		-r _cl_sampler=CLSampler \
-		-r _cl_image_desc=CLImageDesc \
-		-r _cl_image_format=CLImageFormat \
-		--raw-calls \
-		-c CLLib -lOpenCL --func-file src/notzed.zcl/gen/opencl.txt \
-		./$(opencl_pm)
+# This edits the jextract output a little bit
+#  - make class non-final
+#  - remove the constants
+#  - enqueueSVMFree uses a void *[], which jextract turns into an unknown-length sequence, don't do that.
+$(addprefix $(notzed.zcl_genjavadir)/,$(notzed.zcl_generated)): src/notzed.zcl/include/CL/cl.h src/notzed.zcl/gen/gen.make
+	$(JEXTRACT) --output $(notzed.zcl_genjavadir)	\
+		-t au.notzed.zcl			\
+		-lOpenCL				\
+		--filter cl.h				\
+		-I /usr/lib64/clang/9.0.1/include	\
+		-I src/notzed.zcl/include		\
+		$< || ( rm $< ; exit 1)
+		( echo '1,30 s/^public final class/public class/' ;							\
+		  echo 'g/public static final int CL_/d' ;								\
+		  echo '1,$$ s/MemoryLayout.ofSequence(MemoryLayouts.SysV.C_POINTER)/MemoryLayouts.SysV.C_POINTER/' ;	\
+		  echo 'w' ) | ed $(notzed.zcl_genjavadir)/au/notzed/zcl/cl_h.java
-- 
2.39.5