_demo/cabi: abi test demo

This commit is contained in:
visualfc
2025-08-12 09:46:03 +08:00
parent 843dd03553
commit 7b36cca86b
3 changed files with 721 additions and 66 deletions

395
_demo/cabi/main.go Normal file
View File

@@ -0,0 +1,395 @@
package main
import (
_ "unsafe"
)
const (
LLGoFiles = "wrap/wrap.c"
)
type point struct {
x int32
y int32
}
//go:linkname pt C.pt
func pt(pt point) point
type point1 struct {
x int32
y int32
z int32
}
//go:linkname pt1 C.pt1
func pt1(pt point1) point1
type point2 struct {
x int8
y int32
z int32
}
//go:linkname pt2 C.pt2
func pt2(pt point2) point2
type point3 struct {
x int8
y int8
z int8
}
//go:linkname pt3 C.pt3
func pt3(pt point3) point3
type point4 struct {
x int8
y int8
z int8
m int32
}
//go:linkname pt4 C.pt4
func pt4(pt point4) point4
type point5 struct {
x int8
y int8
z int8
m int8
n int8
}
//go:linkname pt5 C.pt5
func pt5(pt point5) point5
type point6 struct {
x int8
y int8
z int8
m int8
n int8
k int32
}
//go:linkname pt6 C.pt6
func pt6(pt point6) point6
type point7 struct {
x int8
y int8
z int8
m int8
n int8
k int32
o int8
}
//go:linkname pt7 C.pt7
func pt7(pt point7) point7
type data1 struct {
x int8
y int64
}
//go:linkname fn1 C.fn1
func fn1(data1) data1
type data2 struct {
x int32
y int64
}
//go:linkname fn2 C.fn2
func fn2(data2) data2
type data3 struct {
x int64
y int8
}
//go:linkname fn3 C.fn3
func fn3(data3) data3
type fdata1 struct {
x float32
}
//go:linkname ff1 C.ff1
func ff1(fdata1) fdata1
type fdata2 struct {
x float32
y float32
}
//go:linkname ff2 C.ff2
func ff2(fdata2) fdata2
type fdata2i struct {
x float32
y int32
}
//go:linkname ff2i C.ff2i
func ff2i(fdata2i) fdata2i
type fdata3 struct {
x float32
y float32
z float32
}
//go:linkname ff3 C.ff3
func ff3(fdata3) fdata3
type fdata4 struct {
x float32
y float32
z float32
m float32
}
//go:linkname ff4 C.ff4
func ff4(fdata4) fdata4
type fdata5 struct {
x float32
y float32
z float32
m float32
n float32
}
//go:linkname ff5 C.ff5
func ff5(fdata5) fdata5
type fdata2id struct {
x int8
y int8
z float64
}
//go:linkname ff2id C.ff2id
func ff2id(fdata2id) fdata2id
type fdata7if struct {
x [7]int8
y float32
}
//go:linkname ff7if C.ff7if
func ff7if(fdata7if) fdata7if
type fdata4if struct {
x float32
y int8
z float32
m float32
}
//go:linkname ff4if C.ff4if
func ff4if(fdata4if) fdata4if
type array struct {
x [8]int32
}
//go:linkname demo64 C.demo64
func demo64(n int64) int64
//go:linkname demo32 C.demo32
func demo32(n int32) int32
type struct32 struct {
v int32
}
//go:linkname demo32s C.demo32s
func demo32s(v struct32) struct32
type point64 struct {
x int64
y int64
}
//go:linkname pt64 C.pt64
func pt64(pt point64) point64
//go:linkname demo C.demo
func demo(a array) array
//go:linkname demo2 C.demo2
func demo2(x int32) array
type ddata1 struct {
x float64
}
//go:linkname dd1 C.dd1
func dd1(d ddata1) ddata1
type ddata2 struct {
x float64
y float64
}
//go:linkname dd2 C.dd2
func dd2(d ddata2) ddata2
type ddata3 struct {
x float64
y float64
z float64
}
//go:linkname dd3 C.dd3
func dd3(d ddata3) ddata3
//llgo:type C
type Callback func(array, point, point1) array
//go:linkname callback C.callback
func callback(fn Callback, ar array)
//llgo:type C
type Callback1 func(array, point, point1) point
//go:linkname callback1 C.callback1
func callback1(fn Callback1, ar array)
//go:linkname mycallback C.mycallback
func mycallback(ar array, pt point, pt1 point1) point
func myfn1(ar array, pt point, pt1 point1) point {
println("=>", ar.x[0], ar.x[1], ar.x[7], pt.x, pt.y, pt1.x, pt1.y, pt1.z)
return point{100, 200}
}
//export export_demo
func export_demo(ar array) array {
println("=> export", ar.x[0], ar.x[1], ar.x[7])
return ar
}
func main() {
cabi_demo()
callback_demo()
}
func callback_demo() {
export_demo(array{x: [8]int32{1, 2, 3, 4, 5, 6, 7, 8}})
callback(func(ar array, pt point, pt1 point1) array {
println("=> callback", ar.x[0], ar.x[1], ar.x[7], pt.x, pt.y, pt1.x, pt1.y, pt1.z)
return array{x: [8]int32{8, 7, 6, 5, 4, 3, 2, 1}}
}, array{x: [8]int32{1, 2, 3, 4, 5, 6, 7, 8}})
callback1(func(ar array, pt point, pt1 point1) point {
println("=> callback1", ar.x[0], ar.x[1], ar.x[7], pt.x, pt.y, pt1.x, pt1.y, pt1.z)
return point{100, 200}
}, array{x: [8]int32{1, 2, 3, 4, 5, 6, 7, 8}})
ret := mycallback(array{x: [8]int32{1, 2, 3, 4, 5, 6, 7, 8}}, point{1, 2}, point1{1, 2, 3})
println("=> mycallback", ret.x, ret.y)
callback1(myfn1, array{x: [8]int32{1, 2, 3, 4, 5, 6, 7, 8}})
callback1(myfn1, array{x: [8]int32{8, 7, 6, 5, 4, 3, 2, 1}})
callback1(mycallback, array{x: [8]int32{10, 20, 30, 40, 50, 60, 70, 80}})
}
func cabi_demo() {
i32 := demo32(1024)
println("=> demo32", i32)
s32 := demo32s(struct32{100})
println("=> demo32s", s32.v)
i64 := demo64(1024)
println("=> demo64", i64)
p64 := pt64(point64{1024, -1024})
println("=> pt64", p64.x, p64.y)
r := demo(array{x: [8]int32{1, 2, 3, 4, 5, 6, 7, 8}})
println("=> demo", r.x[0], r.x[1])
r2 := demo2(100)
println("=> demo2", r2.x[0], r2.x[1], r2.x[7])
p0 := pt(point{1, 2})
println("=> pt0", p0.x, p0.y)
p1 := pt1(point1{1, 2, 3})
println("=> pt1", p1.x, p1.y, p1.z)
p2 := pt2(point2{1, 2, 3})
println("=> pt2", p2.x, p2.y, p2.z)
p3 := pt3(point3{1, 2, 3})
println("=> pt3", p3.x, p3.y, p3.z)
p4 := pt4(point4{1, 2, 3, 4})
println("=> pt4", p4.x, p4.y, p4.z, p4.m)
p5 := pt5(point5{1, 2, 3, 4, 5})
println("=> pt5", p5.x, p5.y, p5.z, p5.m, p5.n)
p6 := pt6(point6{1, 2, 3, 4, 5, 6})
println("=> pt6", p6.x, p6.y, p6.z, p6.m, p6.n, p6.k)
p7 := pt7(point7{1, 2, 3, 4, 5, 6, 7})
println("=> pt7", p7.x, p7.y, p7.z, p7.m, p7.n, p7.k, p7.o)
// skip wrap
fd1 := fn1(data1{1, 2})
println("=> fd1", fd1.x, fd1.y)
fd2 := fn2(data2{1, 2})
println("=> fd2", fd2.x, fd2.y)
fd3 := fn3(data3{1, 2})
println("=> fd3", fd3.x, fd3.y)
// float
f1 := ff1(fdata1{1.1})
println("=> f1", f1.x)
// float
f2 := ff2(fdata2{1.1, 2.1})
println("=> f2", f2.x, f2.y)
// float
f2i := ff2i(fdata2i{1.1, 2})
println("=> f2i", f2i.x, f2i.y)
// float
f3 := ff3(fdata3{1.1, 2.1, 3.1})
println("=> f3", f3.x, f3.y, f3.z)
// float
f4 := ff4(fdata4{1.1, 2.1, 3.1, 4.1})
println("=> f4", f4.x, f4.y, f4.z, f4.m)
// float
f5 := ff5(fdata5{1.1, 2.1, 3.1, 4.1, 5.1})
println("=> f5", f5.x, f5.y, f5.z, f5.m, f5.n)
f2id := ff2id(fdata2id{1, 2, 3.1})
println("=> f2id", f2id.x, f2id.y, f2id.z)
f7if := ff7if(fdata7if{[7]int8{1, 2, 3, 4, 5, 6, 7}, 3.1})
println("=> f7if", f7if.x[0], f7if.x[1], f7if.y)
f4if := ff4if(fdata4if{1.1, 2, 3.1, 4.1})
println("=> f4if", f4if.x, f4if.y, f4if.z, f4if.m)
d1 := dd1(ddata1{1.1})
println("=> dd1", d1.x)
d2 := dd2(ddata2{1.1, 2.1})
println("=> dd2", d2.x, d2.y)
d3 := dd3(ddata3{1.1, 2.1, 3.1})
println("=> dd3", d3.x, d3.y, d3.z)
}

325
_demo/cabi/wrap/wrap.c Normal file
View File

@@ -0,0 +1,325 @@
extern int printf(const char *format, ...);
int demo32(int v) {
return v+100;
}
long long demo64(long long v) {
return v+100;
}
struct struct32 {
int v;
};
struct point64 {
long long x;
long long y;
};
struct point64 pt64(struct point64 pt) {
printf("point64: %lld %lld\n",pt.x,pt.y);
return pt;
}
struct struct32 demo32s(struct struct32 v) {
printf("struct32: %d\n",v.v);
struct struct32 v2 = {v.v+100};
return v2;
}
struct point {
int x;
int y;
};
struct point pt(struct point pt) {
printf("point: %d %d\n",pt.x,pt.y);
return pt;
}
struct point1 {
int x;
int y;
int z;
};
struct point1 pt1(struct point1 pt) {
printf("point1: %d %d %d\n",pt.x,pt.y,pt.z);
return pt;
}
struct point2 {
char x;
int y;
int z;
};
struct point2 pt2(struct point2 pt) {
printf("point2: %d %d %d\n",pt.x,pt.y,pt.z);
return pt;
}
struct point3 {
char x;
char y;
char z;
};
struct point3 pt3(struct point3 pt) {
printf("point3: %d %d %d\n",pt.x,pt.y,pt.z);
return pt;
}
struct point4 {
char x;
char y;
char z;
int m;
};
struct point4 pt4(struct point4 pt) {
printf("point4: %d %d %d %d\n",pt.x,pt.y,pt.z,pt.m);
return pt;
}
struct point5 {
char x;
char y;
char z;
char m;
char n;
};
struct point5 pt5(struct point5 pt) {
printf("point5: %d %d %d %d %d\n",pt.x,pt.y,pt.z,pt.m,pt.n);
return pt;
}
struct point6 {
char x;
char y;
char z;
char m;
char n;
int k;
};
struct point6 pt6(struct point6 pt) {
printf("point6: %d %d %d %d %d %d\n",pt.x,pt.y,pt.z,pt.m,pt.n,pt.k);
return pt;
}
struct point7 {
char x;
char y;
char z;
char m;
char n;
int k;
char o;
};
struct point7 pt7(struct point7 pt) {
printf("point7: %d %d %d %d %d %d %d\n",pt.x,pt.y,pt.z,pt.m,pt.n,pt.k,pt.o);
return pt;
}
struct data1 {
char x;
long long y;
};
struct data1 fn1(struct data1 pt) {
printf("data1: %d %lld\n",pt.x,pt.y);
return pt;
}
struct data2 {
int x;
long long y;
};
struct data2 fn2(struct data2 pt) {
printf("data2: %d %lld\n",pt.x,pt.y);
return pt;
}
struct data3 {
long long x;
char y;
};
struct data3 fn3(struct data3 pt) {
printf("data3: %lld %d\n",pt.x,pt.y);
return pt;
}
struct fdata1 {
float x;
};
struct fdata1 ff1(struct fdata1 pt) {
printf("ff1: %f\n",pt.x);
return pt;
}
struct ddata1 {
double x;
};
struct ddata1 dd1(struct ddata1 pt) {
printf("dd1: %f\n",pt.x);
return pt;
}
struct ddata2 {
double x;
double y;
};
struct ddata2 dd2(struct ddata2 pt) {
printf("dd2: %f %f\n",pt.x,pt.y);
return pt;
}
struct ddata3 {
double x;
double y;
double z;
};
struct ddata3 dd3(struct ddata3 pt) {
printf("dd3: %f %f %f\n",pt.x,pt.y,pt.z);
return pt;
}
struct fdata2i {
float x;
int y;
};
struct fdata2i ff2i(struct fdata2i pt) {
printf("ff2i: %f %d\n",pt.x,pt.y);
return pt;
}
struct fdata2 {
float x;
float y;
};
struct fdata2 ff2(struct fdata2 pt) {
printf("ff2: %f %f\n",pt.x,pt.y);
return pt;
}
struct fdata3 {
float x;
float y;
float z;
};
struct fdata3 ff3(struct fdata3 pt) {
printf("ff3: %f %f %f\n",pt.x,pt.y,pt.z);
return pt;
}
struct fdata4 {
float x;
float y;
float z;
float m;
};
struct fdata4 ff4(struct fdata4 pt) {
printf("ff4: %f %f %f %f\n",pt.x,pt.y,pt.z,pt.m);
return pt;
}
struct fdata5 {
float x;
float y;
float z;
float m;
float n;
};
struct fdata5 ff5(struct fdata5 pt) {
printf("ff5: %f %f %f %f %f\n",pt.x,pt.y,pt.z,pt.m,pt.n);
return pt;
}
struct fdata2id {
char x;
char y;
double z;
};
struct fdata2id ff2id(struct fdata2id pt) {
printf("ff6: %d %d %f\n",pt.x,pt.y,pt.z);
return pt;
}
struct fdata7if {
char x[7];
float z;
};
struct fdata7if ff7if(struct fdata7if pt) {
printf("ff7if: %d %d %f\n",pt.x[0],pt.x[1],pt.z);
return pt;
}
struct fdata4if {
float x;
char y;
float z;
float m;
};
struct fdata4if ff4if(struct fdata4if pt) {
printf("ff4if: %f %d %f %f\n",pt.x,pt.y,pt.z,pt.m);
return pt;
}
struct array {
int x[8];
};
struct array demo(struct array a) {
printf("demo: %d %d %d\n",a.x[0],a.x[1],a.x[2]);
return a;
}
struct array demo2(int a1){
struct array x;
for (int i = 0; i < 8; i++) {
x.x[i] = i+a1;
}
return x;
}
void callback(struct array (*fn)(struct array ar, struct point pt, struct point1 pt1), struct array ar) {
demo(ar);
struct point pt = {1,2};
struct point1 pt1 = {1,2,3};
struct array ret = fn(ar,pt,pt1);
demo(ret);
}
void callback1(struct point (*fn)(struct array ar, struct point pt, struct point1 pt1), struct array ar) {
printf("callback1 array: %d %d %d\n",ar.x[0],ar.x[1],ar.x[7]);
struct point pt = {1,2};
struct point1 pt1 = {1,2,3};
struct point ret = fn(ar,pt,pt1);
printf("callback1 ret: %d,%d\n",ret.x,ret.y);
}
struct point mycallback(struct array ar, struct point pt, struct point1 pt1) {
printf("mycallback array: %d %d %d\n",ar.x[0],ar.x[1],ar.x[7]);
printf("mycallback pt: %d %d\n",pt.x,pt.y);
printf("mycallback pt1: %d %d %d\n",pt1.x,pt1.y,pt1.z);
struct point ret = {pt.x+pt1.x, pt.y+pt1.y};
return ret;
}

View File

@@ -1,66 +1 @@
; ModuleID = 'github.com/goplus/llgo/cl/_testlibc/complex' ;
source_filename = "github.com/goplus/llgo/cl/_testlibc/complex"
%"github.com/goplus/llgo/runtime/internal/runtime.String" = type { ptr, i64 }
@"github.com/goplus/llgo/cl/_testlibc/complex.init$guard" = global i1 false, align 1
@0 = private unnamed_addr constant [5 x i8] c"addr:", align 1
@1 = private unnamed_addr constant [10 x i8] c"abs(3+4i):", align 1
@2 = private unnamed_addr constant [11 x i8] c"real(3+4i):", align 1
@3 = private unnamed_addr constant [11 x i8] c"imag(3+4i):", align 1
define void @"github.com/goplus/llgo/cl/_testlibc/complex.f"({ float, float } %0, { float, float } %1, ptr %2) {
_llgo_0:
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 5 })
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintPointer"(ptr %2)
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10)
%3 = call float @cabsf({ float, float } %0)
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @1, i64 10 })
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32)
%4 = fpext float %3 to double
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintFloat"(double %4)
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10)
%5 = extractvalue { float, float } %1, 0
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @2, i64 11 })
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32)
%6 = fpext float %5 to double
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintFloat"(double %6)
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10)
%7 = extractvalue { float, float } %1, 1
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @3, i64 11 })
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 32)
%8 = fpext float %7 to double
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintFloat"(double %8)
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10)
ret void
}
define void @"github.com/goplus/llgo/cl/_testlibc/complex.init"() {
_llgo_0:
%0 = load i1, ptr @"github.com/goplus/llgo/cl/_testlibc/complex.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1
_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"github.com/goplus/llgo/cl/_testlibc/complex.init$guard", align 1
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}
define void @"github.com/goplus/llgo/cl/_testlibc/complex.main"() {
_llgo_0:
call void @"github.com/goplus/llgo/cl/_testlibc/complex.f"({ float, float } { float 3.000000e+00, float 4.000000e+00 }, { float, float } { float 3.000000e+00, float 4.000000e+00 }, ptr @"github.com/goplus/llgo/cl/_testlibc/complex.f")
ret void
}
declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintString"(%"github.com/goplus/llgo/runtime/internal/runtime.String")
declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8)
declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintPointer"(ptr)
declare float @cabsf({ float, float })
declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintFloat"(double)