Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
support bfm = BFM_InLibC
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 16, 2022
1 parent 8292c1e commit 0e28236
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions cl/blockctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ type blockCtx struct {
file *token.File
curfn *funcCtx
curflow flowCtx
bfm BFMode
multiFileCtl
testMain bool
}
Expand Down
11 changes: 11 additions & 0 deletions cl/codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,26 @@ func decl_builtin(ctx *blockCtx) {
if err != nil {
log.Panicln("decl_builtin decode error:", err)
}
bfm := ctx.bfm
pkg := ctx.pkg.Types
scope := pkg.Scope()
for fn, proto := range fns {
t := toType(ctx, &cast.Type{QualType: strings.ReplaceAll(proto, "size_t", "unsigned long")}, 0)
switch bfm {
case BFM_InLibC:
fn = "X" + fn
case BFM_FromLibC:
panic("TODO: import builtin from libc")
}
scope.Insert(types.NewFunc(token.NoPos, pkg, fn, t.(*types.Signature)))
}
for _, o := range builtin_overloads {
fns := make([]types.Object, len(o.overloads))
for i, item := range o.overloads {
switch bfm {
case BFM_InLibC:
item = "X" + item
}
fns[i] = pkg.Scope().Lookup(item)
}
scope.Insert(gox.NewOverloadFunc(token.NoPos, pkg, o.name, fns...))
Expand Down
14 changes: 14 additions & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func (p *nodeInterp) LoadExpr(v goast.Node) (code string, pos token.Position) {

// -----------------------------------------------------------------------------

type BFMode int8

const (
BFM_Default BFMode = iota
BFM_InLibC // define builtin functions in libc
BFM_FromLibC // import builtin functions from libc
)

// -----------------------------------------------------------------------------

type PkgInfo struct {
typdecls map[string]*gox.TypeDecl
extfns map[string]none // external functions which are used
Expand Down Expand Up @@ -166,6 +176,9 @@ type Config struct {
// PublicFrom specifies header files to fetch public symbols.
PublicFrom []string

// BuiltinFuncMode sets compiling mode of builtin functions.
BuiltinFuncMode BFMode

// NeedPkgInfo allows to check dependencies and write them to c2go_autogen.go file.
NeedPkgInfo bool

Expand Down Expand Up @@ -249,6 +262,7 @@ func loadFile(p *gox.Package, conf *Config, file *ast.Node) (pi *PkgInfo, err er
public: conf.Public,
srcfile: conf.SrcFile,
src: conf.Src,
bfm: conf.BuiltinFuncMode,
testMain: conf.TestMain,
}
ctx.initMultiFileCtl(p, conf)
Expand Down
8 changes: 8 additions & 0 deletions proj.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type c2goConf struct {
dir string `json:"-"`
public map[string]string `json:"-"`
needPkgInfo bool `json:"-"`

InLibC bool `json:"libc"`
}

var json = jsoniter.ConfigCompatibleWithStandardLibrary
Expand Down Expand Up @@ -255,6 +257,10 @@ func execProjFile(infile string, conf *c2goConf, flags int) {
Run("", pkgDir, flags, nil)
}
}
var bfm cl.BFMode
if conf.InLibC {
bfm = cl.BFM_InLibC
}
_, err = cl.NewPackage("", conf.Target.Name, doc, &cl.Config{
SrcFile: outfile,
ProcDepPkg: procDepPkg,
Expand All @@ -267,6 +273,8 @@ func execProjFile(infile string, conf *c2goConf, flags int) {
Ignored: conf.Source.Ignore.Names,
Reused: &conf.Reused,
TestMain: (flags & FlagTestMain) != 0,
// BuiltinFuncMode: compiling mode of builtin functions
BuiltinFuncMode: bfm,
})
check(err)
}
Expand Down
3 changes: 2 additions & 1 deletion testdata/libc/c2go.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"dirs": ["../printf", "../qsort"],
"files": ["../strlen/strlen.c", "./src/foo.c", "./src/foo2.c"]
},
"include": ["./src"]
"include": ["./src"],
"libc": true
}
4 changes: 4 additions & 0 deletions testdata/libc/c2go.pub
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
__builtin___memcpy_chk
__builtin_object_size
__builtin_bswap32
__builtin_bswap64
printf
strlen
qsort QSort
Expand Down
8 changes: 4 additions & 4 deletions testdata/libc/libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ func memcpy(dst unsafe.Pointer, src unsafe.Pointer, n c.SizeT) unsafe.Pointer {
return dst
}

func __builtin___memcpy_chk(dst unsafe.Pointer, src unsafe.Pointer, n c.SizeT, elem c.SizeT) unsafe.Pointer {
func X__builtin___memcpy_chk(dst unsafe.Pointer, src unsafe.Pointer, n c.SizeT, elem c.SizeT) unsafe.Pointer {
copy(sliceOf(dst, n), sliceOf(src, n))
return dst
}

func __builtin_object_size(unsafe.Pointer, int32) c.SizeT {
func X__builtin_object_size(unsafe.Pointer, int32) c.SizeT {
return 1
}

func __builtin_bswap32(v uint32) uint32 {
func X__builtin_bswap32(v uint32) uint32 {
log.Panicln("__builtin_bswap32: notimpl")
return v
}

func __builtin_bswap64(v uint64) uint64 {
func X__builtin_bswap64(v uint64) uint64 {
log.Panicln("__builtin_bswap32: notimpl")
return v
}
Expand Down

0 comments on commit 0e28236

Please sign in to comment.