Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions _demo/go/export/export.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"runtime"
"unsafe"

C "github.com/goplus/llgo/_demo/go/export/c"
Expand Down Expand Up @@ -47,6 +48,21 @@ type Node struct {
type MyInt int
type MyString string

// FuncInfoResult exposes the information resolved from a Callers PC so the C
// consumer can verify that funcinfo remains usable after loading a c-shared
// library (and from the equivalent c-archive output).
type FuncInfoResult struct {
CallersCount int32
FramePC uintptr
FrameFunction string
FrameFile string
FrameLine int32
FuncName string
FuncEntry uintptr
FuncFile string
FuncLine int32
}

// Function types for callbacks
//
//llgo:type C
Expand All @@ -71,6 +87,53 @@ func HelloWorld() {
println("Hello, World!")
}

//go:noinline
func captureCFuncInfo() FuncInfoResult {
var pcs [16]uintptr
n := runtime.Callers(0, pcs[:])
result := FuncInfoResult{CallersCount: int32(n)}
frames := runtime.CallersFrames(pcs[:n])
for {
frame, more := frames.Next()
if frame.Function == "main.captureCFuncInfo" {
result.FramePC = frame.PC
result.FrameFunction = frame.Function
result.FrameFile = frame.File
result.FrameLine = int32(frame.Line)

// Callers PCs are return addresses. Apply Go's pc-1 convention
// before asking FuncForPC for the containing function.
if frame.PC > 0 {
if fn := runtime.FuncForPC(frame.PC - 1); fn != nil {
result.FuncName = fn.Name()
result.FuncEntry = fn.Entry()
result.FuncFile, frame.Line = fn.FileLine(frame.PC - 1)
result.FuncLine = int32(frame.Line)
}
}
break
}
if !more {
break
}
}
return result
}

//export GetFuncInfo
func GetFuncInfo() FuncInfoResult {
return captureCFuncInfo()
}

//export RunGoroutine
func RunGoroutine(value int) int {
ch := make(chan int, 1)
go func() {
ch <- value + 1
}()
return <-ch
}

// Functions with small struct parameters and return values

//export CreateSmallStruct
Expand Down Expand Up @@ -382,6 +445,11 @@ func ProcessIntSlice(slice []int) int {
return total
}

//export CreateIntSlice
func CreateIntSlice() []int {
return []int{2, 4, 6, 8}
}

//export ProcessStringMap
func ProcessStringMap(m map[string]int) int {
total := 0
Expand All @@ -391,6 +459,11 @@ func ProcessStringMap(m map[string]int) int {
return total
}

//export CreateStringMap
func CreateStringMap() map[string]int {
return map[string]int{"one": 10, "two": 20, "three": 30}
}

//export ProcessIntChannel
func ProcessIntChannel(ch chan int) int {
select {
Expand All @@ -401,6 +474,13 @@ func ProcessIntChannel(ch chan int) int {
}
}

//export CreateIntChannel
func CreateIntChannel() chan int {
ch := make(chan int, 1)
ch <- 321
return ch
}

// Functions with function callbacks

//export ProcessWithIntCallback
Expand Down Expand Up @@ -451,6 +531,16 @@ func ProcessInterface(i interface{}) int {
}
}

//export CreateIntInterface
func CreateIntInterface() interface{} {
return 23
}

//export CreateStringInterface
func CreateStringInterface() interface{} {
return "llgo"
}

// Functions with various parameter counts

//export NoParams
Expand Down
78 changes: 41 additions & 37 deletions _demo/go/export/libexport.h.want
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ typedef struct {
_Bool Flag;
} C_XType;

typedef struct {
int32_t CallersCount;
uintptr_t FramePC;
GoString FrameFunction;
GoString FrameFile;
int32_t FrameLine;
GoString FuncName;
uintptr_t FuncEntry;
GoString FuncFile;
int32_t FuncLine;
} main_FuncInfoResult;

typedef struct {
intptr_t data[5];
} Array_intptr_t_5;

typedef intptr_t main_MyInt;

typedef GoString main_MyString;
Expand Down Expand Up @@ -111,6 +127,15 @@ CreateComplexData(void);
Array_double_5_4
CreateGrid5x4(void);

GoChan
CreateIntChannel(void);

GoInterface
CreateIntInterface(void);

GoSlice
CreateIntSlice(void);

main_LargeStruct
CreateLargeStruct(int64_t id, GoString name);

Expand All @@ -129,9 +154,18 @@ CreateNode(intptr_t data);
main_SmallStruct
CreateSmallStruct(int8_t id, _Bool flag);

GoInterface
CreateStringInterface(void);

GoMap
CreateStringMap(void);

C_XType
CreateXType(int32_t id, GoString name, double value, _Bool flag);

main_FuncInfoResult
GetFuncInfo(void);

void
HelloWorld(void);

Expand Down Expand Up @@ -166,7 +200,7 @@ double
ProcessFloat64(double x);

double
ProcessGrid5x4(double grid[5][4]);
ProcessGrid5x4(Array_double_5_4 grid);

intptr_t
ProcessInt(intptr_t x);
Expand All @@ -184,7 +218,7 @@ int8_t
ProcessInt8(int8_t x);

intptr_t
ProcessIntArray(intptr_t* arr);
ProcessIntArray(Array_intptr_t_5 arr);

intptr_t
ProcessIntChannel(GoChan ch);
Expand All @@ -202,10 +236,10 @@ main_LargeStruct*
ProcessLargeStructPtr(main_LargeStruct* ls);

int32_t
ProcessMatrix2D(int32_t matrix[3][4]);
ProcessMatrix2D(Array_int32_t_3_4 matrix);

uint32_t
ProcessMatrix3D(uint8_t cube[2][3][4]);
ProcessMatrix3D(Array_uint8_t_2_3_4 cube);

main_MyInt
ProcessMyInt(main_MyInt x);
Expand Down Expand Up @@ -264,6 +298,9 @@ ProcessXType(C_XType x);
C_XType*
ProcessXTypePtr(C_XType* x);

intptr_t
RunGoroutine(intptr_t value);

double
ThreeParams(int32_t a, double b, _Bool c);

Expand All @@ -276,39 +313,6 @@ TwoParams(intptr_t a, GoString b);
void
github_com_goplus_llgo__demo_go_export_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/_demo/go/export.init")

void
github_com_goplus_llgo_runtime_abi_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/abi.init")

void
github_com_goplus_llgo_runtime_internal_clite_bdwgc_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/bdwgc.init")

void
github_com_goplus_llgo_runtime_internal_clite_bitcast_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/bitcast.init")

void
github_com_goplus_llgo_runtime_internal_clite_debug_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/debug.init")

void
github_com_goplus_llgo_runtime_internal_clite_pthread_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/pthread.init")

void
github_com_goplus_llgo_runtime_internal_clite_pthread_sync_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/pthread/sync.init")

void
github_com_goplus_llgo_runtime_internal_clite_signal_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/signal.init")

void
github_com_goplus_llgo_runtime_internal_clite_tls_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/clite/tls.init")

void
github_com_goplus_llgo_runtime_internal_runtime_goarch_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/runtime/goarch.init")

void
github_com_goplus_llgo_runtime_internal_runtime_math_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/runtime/math.init")

void
github_com_goplus_llgo_runtime_internal_runtime_init(void) GO_SYMBOL_RENAME("github.com/goplus/llgo/runtime/internal/runtime.init")



#ifdef __cplusplus
Expand Down
Loading
Loading