runtime: clean demo
This commit is contained in:
@@ -1,27 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/debug"
|
|
||||||
)
|
|
||||||
|
|
||||||
type T struct {
|
|
||||||
n int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *T) Demo() {
|
|
||||||
println(t.n)
|
|
||||||
addr := debug.Address()
|
|
||||||
c.Printf(c.Str("addr:0x%x\n"), addr)
|
|
||||||
var info debug.Info
|
|
||||||
r := debug.Addrinfo(addr, &info)
|
|
||||||
if r == 0 {
|
|
||||||
panic("not found info")
|
|
||||||
}
|
|
||||||
c.Printf(c.Str("func file:%s name:%s base:0x%x addr:0x%x\n"), info.Fname, info.Sname, info.Fbase, info.Saddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
t := &T{100}
|
|
||||||
t.Demo()
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/debug"
|
|
||||||
)
|
|
||||||
|
|
||||||
type T struct {
|
|
||||||
n int
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *T) Demo() {
|
|
||||||
println(t.n)
|
|
||||||
debug.StackTrace(0, func(fr *debug.Frame) bool {
|
|
||||||
var info debug.Info
|
|
||||||
debug.Addrinfo(unsafe.Pointer(fr.PC), &info)
|
|
||||||
println("[", fr.PC, "]", fr.Name, "+", fr.Offset, ", SP =", fr.SP)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
t := &T{100}
|
|
||||||
t.Demo()
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
struct array
|
|
||||||
{
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
int z;
|
|
||||||
int k;
|
|
||||||
};
|
|
||||||
|
|
||||||
int demo1(struct array a)
|
|
||||||
{
|
|
||||||
printf("c.demo1: %d %d %d %d\n",a.x,a.y,a.z,a.k);
|
|
||||||
return a.x+a.y+a.z+a.k;
|
|
||||||
}
|
|
||||||
|
|
||||||
int demo2( int (*fn)(struct array)) {
|
|
||||||
printf("c.demo2: %p\n",fn);
|
|
||||||
struct array a;
|
|
||||||
a.x = 1;
|
|
||||||
a.y = 2;
|
|
||||||
a.z = 3;
|
|
||||||
a.k = 4;
|
|
||||||
return (*fn)(a);
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/ffi"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
LLGoPackage = "link"
|
|
||||||
LLGoFiles = "../_wrap/wrap.c"
|
|
||||||
)
|
|
||||||
|
|
||||||
//llgo:type C
|
|
||||||
type Callback func(array) c.Int
|
|
||||||
|
|
||||||
//go:linkname demo1 C.demo1
|
|
||||||
func demo1(array) c.Int
|
|
||||||
|
|
||||||
//go:linkname demo2 C.demo2
|
|
||||||
func demo2(fn Callback) c.Int
|
|
||||||
|
|
||||||
//llgo:type C
|
|
||||||
type array struct {
|
|
||||||
x c.Int
|
|
||||||
y c.Int
|
|
||||||
z c.Int
|
|
||||||
k c.Int
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
typeInt32 = &ffi.Type{4, 4, ffi.Sint32, nil}
|
|
||||||
typePointer = &ffi.Type{unsafe.Sizeof(0), uint16(unsafe.Alignof(0)), ffi.Pointer, nil}
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
cdemo1()
|
|
||||||
cdemo2()
|
|
||||||
}
|
|
||||||
|
|
||||||
func cdemo1() {
|
|
||||||
var cif ffi.Cif
|
|
||||||
tarray := &ffi.Type{0, 0, ffi.Struct, &[]*ffi.Type{typeInt32, typeInt32, typeInt32, typeInt32, nil}[0]}
|
|
||||||
status := ffi.PrepCif(&cif, ffi.DefaultAbi, 1, typeInt32, &[]*ffi.Type{tarray}[0])
|
|
||||||
if status != ffi.OK {
|
|
||||||
panic(status)
|
|
||||||
}
|
|
||||||
ar := array{1, 2, 3, 4}
|
|
||||||
var ret int32
|
|
||||||
ffi.Call(&cif, c.Func(demo1), unsafe.Pointer(&ret), &[]unsafe.Pointer{unsafe.Pointer(&ar)}[0])
|
|
||||||
c.Printf(c.Str("ret: %d\n"), ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func cdemo2() {
|
|
||||||
var cif ffi.Cif
|
|
||||||
status := ffi.PrepCif(&cif, ffi.DefaultAbi, 1, typeInt32, &[]*ffi.Type{typePointer}[0])
|
|
||||||
if status != ffi.OK {
|
|
||||||
panic(status)
|
|
||||||
}
|
|
||||||
var ret int32
|
|
||||||
fn := c.Func(demo1)
|
|
||||||
ffi.Call(&cif, c.Func(demo2), unsafe.Pointer(&ret), &[]unsafe.Pointer{unsafe.Pointer(&fn)}[0])
|
|
||||||
c.Printf(c.Str("ret: %d\n"), ret)
|
|
||||||
}
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/ffi"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
LLGoPackage = "link"
|
|
||||||
LLGoFiles = "../_wrap/wrap.c"
|
|
||||||
)
|
|
||||||
|
|
||||||
//llgo:type C
|
|
||||||
type Callback func(array) c.Int
|
|
||||||
|
|
||||||
//go:linkname demo1 C.demo1
|
|
||||||
func demo1(array) c.Int
|
|
||||||
|
|
||||||
//go:linkname demo2 C.demo2
|
|
||||||
func demo2(fn Callback) c.Int
|
|
||||||
|
|
||||||
//llgo:type C
|
|
||||||
type array struct {
|
|
||||||
x c.Int
|
|
||||||
y c.Int
|
|
||||||
z c.Int
|
|
||||||
k c.Int
|
|
||||||
}
|
|
||||||
|
|
||||||
func demo(a array) c.Int {
|
|
||||||
c.Printf(c.Str("go.demo %d %d %d %d\n"), a.x, a.y, a.z, a.k)
|
|
||||||
return a.x + a.y + a.z + a.k
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
typeInt32 = &ffi.Type{4, 4, ffi.Sint32, nil}
|
|
||||||
typePointer = &ffi.Type{unsafe.Sizeof(0), uint16(unsafe.Alignof(0)), ffi.Pointer, nil}
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
gofn()
|
|
||||||
c.Printf(c.Str("\n"))
|
|
||||||
goclosure()
|
|
||||||
}
|
|
||||||
|
|
||||||
func gofn() {
|
|
||||||
var cif ffi.Cif
|
|
||||||
status := ffi.PrepCif(&cif, ffi.DefaultAbi, 1, typeInt32, &[]*ffi.Type{typePointer}[0])
|
|
||||||
if status != ffi.OK {
|
|
||||||
panic(status)
|
|
||||||
}
|
|
||||||
var fncode unsafe.Pointer
|
|
||||||
closure := ffi.ClosureAlloc(&fncode)
|
|
||||||
defer ffi.ClosureFree(closure)
|
|
||||||
status = ffi.PreClosureLoc(closure, &cif, func(cif *ffi.Cif, ret unsafe.Pointer, args *unsafe.Pointer, userdata unsafe.Pointer) {
|
|
||||||
ar := *(*array)(ffi.Index(args, 0))
|
|
||||||
*(*c.Int)(ret) = demo(ar)
|
|
||||||
}, nil, fncode)
|
|
||||||
if status != ffi.OK {
|
|
||||||
panic(status)
|
|
||||||
}
|
|
||||||
var ret int32
|
|
||||||
ffi.Call(&cif, c.Func(demo2), unsafe.Pointer(&ret), &[]unsafe.Pointer{unsafe.Pointer(&fncode)}[0])
|
|
||||||
c.Printf(c.Str("ret: %d\n"), ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
func goclosure() {
|
|
||||||
var cif ffi.Cif
|
|
||||||
status := ffi.PrepCif(&cif, ffi.DefaultAbi, 1, typeInt32, &[]*ffi.Type{typePointer}[0])
|
|
||||||
if status != ffi.OK {
|
|
||||||
panic(status)
|
|
||||||
}
|
|
||||||
fn := func(ar array) c.Int {
|
|
||||||
c.Printf(c.Str("call closure %d\n"), cif.NArgs)
|
|
||||||
return demo(ar)
|
|
||||||
}
|
|
||||||
var fncode unsafe.Pointer
|
|
||||||
closure := ffi.ClosureAlloc(&fncode)
|
|
||||||
defer ffi.ClosureFree(closure)
|
|
||||||
status = ffi.PreClosureLoc(closure, &cif, func(cif *ffi.Cif, ret unsafe.Pointer, args *unsafe.Pointer, userdata unsafe.Pointer) {
|
|
||||||
ar := *(*array)(ffi.Index(args, 0))
|
|
||||||
fn := *(*func(array) c.Int)(userdata)
|
|
||||||
*(*c.Int)(ret) = fn(ar)
|
|
||||||
}, unsafe.Pointer(&fn), fncode)
|
|
||||||
if status != ffi.OK {
|
|
||||||
panic(status)
|
|
||||||
}
|
|
||||||
var ret int32
|
|
||||||
ffi.Call(&cif, c.Func(demo2), unsafe.Pointer(&ret), &[]unsafe.Pointer{unsafe.Pointer(&fncode)}[0])
|
|
||||||
c.Printf(c.Str("ret: %d\n"), ret)
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/ffi"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
typeInt32 = &ffi.Type{4, 4, ffi.Sint32, nil}
|
|
||||||
typePointer = &ffi.Type{unsafe.Sizeof(0), uint16(unsafe.Alignof(0)), ffi.Pointer, nil}
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var cif ffi.Cif
|
|
||||||
status := ffi.PrepCifVar(&cif, ffi.DefaultAbi, 1, 2, typeInt32, &[]*ffi.Type{typePointer, typeInt32}[0])
|
|
||||||
if status != ffi.OK {
|
|
||||||
panic(status)
|
|
||||||
}
|
|
||||||
var ret int32
|
|
||||||
text := c.Str("hello world: %d\n")
|
|
||||||
var n int32 = 100
|
|
||||||
ffi.Call(&cif, c.Func(c.Printf), unsafe.Pointer(&ret), &[]unsafe.Pointer{unsafe.Pointer(&text), unsafe.Pointer(&n)}[0])
|
|
||||||
c.Printf(c.Str("ret: %d\n"), ret)
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/zlib"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
fmt.Printf("%08x\n", zlib.Crc32ZString(0, "Hello world"))
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/zlib"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
txt := []byte("zlib is a software library used for data compression. It was created by Jean-loup Gailly and Mark Adler and first released in 1995. zlib is designed to be a free, legally unencumbered—that is, not covered by any patents—alternative to the proprietary DEFLATE compression algorithm, which is often used in software applications for data compression.The library provides functions to compress and decompress data using the DEFLATE algorithm, which is a combination of the LZ77 algorithm and Huffman coding. zlib is notable for its versatility; it can be used in a wide range of applications, from web servers and web clients compressing HTTP data, to the compression of data for storage or transmission in various file formats, such as PNG, ZIP, and GZIP.")
|
|
||||||
txtLen := c.Ulong(len(txt))
|
|
||||||
|
|
||||||
for level := 0; level <= 9; level++ {
|
|
||||||
cmpSize := zlib.CompressBound(txtLen)
|
|
||||||
cmpData := make([]byte, int(cmpSize))
|
|
||||||
|
|
||||||
res := zlib.Compress2(unsafe.SliceData(cmpData), &cmpSize, unsafe.SliceData(txt), txtLen, c.Int(level))
|
|
||||||
if res != zlib.OK {
|
|
||||||
c.Printf(c.Str("\nCompression failed at level %d: %d\n"), level, res)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Printf(c.Str("Compression level %d: Text length = %d, Compressed size = %d\n"), level, txtLen, cmpSize)
|
|
||||||
|
|
||||||
ucmpSize := txtLen
|
|
||||||
ucmpData := make([]byte, int(ucmpSize))
|
|
||||||
|
|
||||||
unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize)
|
|
||||||
if unRes != zlib.OK {
|
|
||||||
c.Printf(c.Str("\nDecompression failed at level %d: %d\n"), level, unRes)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
c "github.com/goplus/llgo/runtime/internal/clite"
|
|
||||||
"github.com/goplus/llgo/runtime/internal/clite/zlib"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
txt := []byte("zlib is a software library used for data compression. It was created by Jean-loup Gailly and Mark Adler and first released in 1995. zlib is designed to be a free, legally unencumbered—that is, not covered by any patents—alternative to the proprietary DEFLATE compression algorithm, which is often used in software applications for data compression.The library provides functions to compress and decompress data using the DEFLATE algorithm, which is a combination of the LZ77 algorithm and Huffman coding. zlib is notable for its versatility; it can be used in a wide range of applications, from web servers and web clients compressing HTTP data, to the compression of data for storage or transmission in various file formats, such as PNG, ZIP, and GZIP.")
|
|
||||||
txtLen := c.Ulong(len(txt))
|
|
||||||
|
|
||||||
cmpSize := zlib.CompressBound(txtLen)
|
|
||||||
cmpData := make([]byte, int(cmpSize))
|
|
||||||
|
|
||||||
res := zlib.Compress(unsafe.SliceData(cmpData), &cmpSize, unsafe.SliceData(txt), txtLen)
|
|
||||||
if res != zlib.OK {
|
|
||||||
c.Printf(c.Str("\nCompression failed: %d\n"), res)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Printf(c.Str("Text length = %d, Compressed size = %d\n"), txtLen, cmpSize)
|
|
||||||
|
|
||||||
ucmpSize := txtLen
|
|
||||||
ucmpData := make([]byte, int(ucmpSize))
|
|
||||||
|
|
||||||
unRes := zlib.Uncompress(unsafe.SliceData(ucmpData), &ucmpSize, unsafe.SliceData(cmpData), cmpSize)
|
|
||||||
c.Printf(c.Str("Decompression result = %d, Decompressed size %d\n"), unRes, ucmpSize)
|
|
||||||
|
|
||||||
if unRes != zlib.OK {
|
|
||||||
c.Printf(c.Str("\nDecompression failed: %d\n"), unRes)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Printf(c.Str("Decompressed data: \n"))
|
|
||||||
for i := 0; i < int(ucmpSize); i++ {
|
|
||||||
c.Printf(c.Str("%c"), ucmpData[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Expected output:
|
|
||||||
origin textLen = 73 compressed_size = 36
|
|
||||||
uncompress result = 0 uncompress result size 73
|
|
||||||
after uncompressed data: Hello, zlib compression!Hello, zlib compression!Hello, zlib compression!
|
|
||||||
*/
|
|
||||||
Reference in New Issue
Block a user