cgo: full supports cgo preambles and auto compile c files

This commit is contained in:
Li Jie
2024-11-14 16:13:13 +08:00
parent 89b111edca
commit a64f4219e9
16 changed files with 588 additions and 53 deletions

View File

@@ -121,6 +121,20 @@ func (p *context) cgoCheckPointer(b llssa.Builder, args []ssa.Value) {
// don't need to do anything
}
// func _cgo_runtime_cgocall(fn unsafe.Pointer, arg unsafe.Pointer) int
func (p *context) cgoCgocall(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
pfn := p.compileValue(b, args[0])
pfn.Type = p.prog.Pointer(p.fn.Type)
fn := b.Load(pfn)
p.cgoRet = b.Call(fn, p.cgoArgs...)
return p.cgoRet
}
// func _Cgo_use(v any)
func (p *context) cgoUse(b llssa.Builder, args []ssa.Value) {
// don't need to do anything
}
// -----------------------------------------------------------------------------
// func index(arr *T, idx int) T
@@ -302,13 +316,15 @@ var llgoInstrs = map[string]int{
"atomicUMax": int(llgoAtomicUMax),
"atomicUMin": int(llgoAtomicUMin),
"_Cfunc_CString": llgoCgoCString,
"_Cfunc_CBytes": llgoCgoCBytes,
"_Cfunc_GoString": llgoCgoGoString,
"_Cfunc_GoStringN": llgoCgoGoStringN,
"_Cfunc_GoBytes": llgoCgoGoBytes,
"_Cfunc__CMalloc": llgoCgoCMalloc,
"_cgoCheckPointer": llgoCgoCheckPointer,
"_Cfunc_CString": llgoCgoCString,
"_Cfunc_CBytes": llgoCgoCBytes,
"_Cfunc_GoString": llgoCgoGoString,
"_Cfunc_GoStringN": llgoCgoGoStringN,
"_Cfunc_GoBytes": llgoCgoGoBytes,
"_Cfunc__CMalloc": llgoCgoCMalloc,
"_cgoCheckPointer": llgoCgoCheckPointer,
"_cgo_runtime_cgocall": llgoCgoCgocall,
"_Cgo_use": llgoCgoUse,
}
// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
@@ -450,6 +466,10 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
ret = p.cgoCMalloc(b, args)
case llgoCgoCheckPointer:
p.cgoCheckPointer(b, args)
case llgoCgoCgocall:
p.cgoCgocall(b, args)
case llgoCgoUse:
p.cgoUse(b, args)
case llgoAdvance:
ret = p.advance(b, args)
case llgoIndex: