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
3 changes: 0 additions & 3 deletions fncompiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,6 @@ func (fn *funcCompiler) cleanup() {
passes.InlineGotoEnd(fn.decl)
passes.InlineGotoReturn(fn.decl)
passes.UnnestSimple(fn.decl)
if passes.RemoveReceiver(fn.decl) {
fn.call.(*ast.ParenExpr).X = fn.decl.Name
}
}

type funcBlock struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/passes/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func InlineGotoReturn(fn *ast.FuncDecl) {
// into a plain function.
// Returns true if we need to update call sites.
func RemoveReceiver(fn *ast.FuncDecl) bool {
if ast.IsExported(fn.Name.Name) {
if fn.Recv == nil || ast.IsExported(fn.Name.Name) {
return false
}

Expand Down
79 changes: 40 additions & 39 deletions libc-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,51 +135,21 @@ func main() {
}
}

queue := append([]string(nil), funcs...)
visited := make(map[string]bool)
var keep []*ast.FuncDecl

// Tree-shake the AST
for len(queue) > 0 {
name := queue[0]
queue = queue[1:]

if visited[name] {
continue
}
visited[name] = true

fd, ok := available[name]
if !ok && *wasm == "" {
var keep []*ast.FuncDecl
visited := make(set[string])
for _, name := range funcs {
if available[name] == nil && *wasm == "" {
log.Fatalf("function %s not found in templates", name)
} else if !ok {
continue
}
keep = append(keep, fd)

// Search for other internal dependencies inside this one's scope
ast.Inspect(fd, func(n ast.Node) bool {
if call, ok := n.(*ast.CallExpr); ok {
if id, ok := call.Fun.(*ast.Ident); ok {
if _, exists := available[id.Name]; exists {
queue = append(queue, id.Name)
}
}
}
return true
})
keep = appendDeps(keep, name, available, visited)
}

usedImports := make(set[string])

// Execute the rewriting permutations
for _, fd := range keep {
fd.Recv = &ast.FieldList{
List: []*ast.Field{{
Names: []*ast.Ident{ast.NewIdent("m")},
Type: &ast.StarExpr{X: ast.NewIdent("Module")},
}},
}
fd.Name = mangle.ID(fd.Name.Name, mangle.Internal)

astutil.Apply(fd, nil, func(c *astutil.Cursor) bool {
if call, ok := c.Node().(*ast.CallExpr); ok {
Expand All @@ -194,6 +164,7 @@ func main() {
return true
})

var recv bool
ast.Inspect(fd, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.SliceExpr:
Expand All @@ -206,7 +177,12 @@ func main() {
}
case *ast.CallExpr:
if id, ok := x.Fun.(*ast.Ident); ok && available[id.Name] != nil {
x.Fun = &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: mangle.ID(id.Name, mangle.Internal)}
callee := available[id.Name]
if callee.Recv != nil {
x.Fun = &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: callee.Name}
} else {
x.Fun = callee.Name
}
}
for i, arg := range x.Args {
if id, ok := arg.(*ast.Ident); ok && id.Name == "memory" {
Expand All @@ -222,6 +198,9 @@ func main() {
}
case *ast.SelectorExpr:
if id, ok := x.X.(*ast.Ident); ok {
if id.Name == "m" {
recv = true
}
if path, exists := allImports[id.Name]; exists {
usedImports.add(path)
}
Expand All @@ -230,8 +209,14 @@ func main() {
return true
})

// Finally mangle the function's own name
fd.Name = mangle.ID(fd.Name.Name, mangle.Internal)
if recv {
fd.Recv = &ast.FieldList{
List: []*ast.Field{{
Names: []*ast.Ident{ast.NewIdent("m")},
Type: &ast.StarExpr{X: ast.NewIdent("Module")},
}},
}
}
}

// Write the unified file
Expand Down Expand Up @@ -273,6 +258,22 @@ func main() {
}
}

func appendDeps(keep []*ast.FuncDecl, name string, available map[string]*ast.FuncDecl, visited set[string]) []*ast.FuncDecl {
fd := available[name]
if fd == nil || !visited.add(name) {
return keep
}
ast.Inspect(fd, func(n ast.Node) bool {
if call, ok := n.(*ast.CallExpr); ok {
if id, ok := call.Fun.(*ast.Ident); ok {
keep = appendDeps(keep, id.Name, available, visited)
}
}
return true
})
return append(keep, fd)
}

func makeMemoryExpr(deref bool) ast.Expr {
sel := &ast.SelectorExpr{X: ast.NewIdent("m"), Sel: ast.NewIdent("memory")}
if deref {
Expand Down
35 changes: 24 additions & 11 deletions translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type translator struct {
out ast.File
// Dependencies.
packages set[string]
provided set[string]
provided map[string]bool
helpers set[string]
// Sections.
types []funcType
Expand Down Expand Up @@ -94,7 +94,7 @@ func translate(r io.Reader, w io.Writer) error {

fset := token.NewFileSet()
t.packages = set[string]{}
t.provided = set[string]{}
t.provided = map[string]bool{}
t.helpers = set[string]{}

helperNames, err := t.findHelpers(fset, helpersSrc, helpersAtomicsSrc)
Expand All @@ -108,12 +108,12 @@ func translate(r io.Reader, w io.Writer) error {
}
for _, decl := range f.Decls {
if fn, ok := decl.(*ast.FuncDecl); ok {
// Check if the receiver type is *Module.
if fn.Recv != nil {
if star, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok {
if id, ok := star.X.(*ast.Ident); ok && id.Name == "Module" {
t.provided.add(fn.Name.Name)
}
// Check if fn is a plain function, or if the receiver type is *Module.
if fn.Recv == nil {
t.provided[fn.Name.Name] = false
} else if star, ok := fn.Recv.List[0].Type.(*ast.StarExpr); ok {
if id, ok := star.X.(*ast.Ident); ok && id.Name == "Module" {
t.provided[fn.Name.Name] = true
}
}
// Provided files may call helpers by name.
Expand Down Expand Up @@ -263,6 +263,15 @@ func translate(r io.Reader, w io.Writer) error {
}
}

for modified := !*noopt; modified; {
modified = false
for _, fn := range t.functions {
if passes.RemoveReceiver(fn.decl) {
fn.call.(*ast.ParenExpr).X = fn.decl.Name
modified = true
}
}
}
passes.RemoveParens(&t.out)

// Print Go.
Expand Down Expand Up @@ -469,11 +478,15 @@ func (t *translator) readImportSection() error {
}
typ := t.types[index]

if n := mangle.Name(name, mangle.Internal); t.provided.has(n) {
id := ast.NewIdent(n)
if recv, ok := t.provided[mangle.Name(name, mangle.Internal)]; ok {
id := mangle.ID(name, mangle.Internal)
var call ast.Expr = id
if recv {
call = modSel(id)
}
fn := funcCompiler{
typ: typ,
call: latecall(id),
call: call,
decl: &ast.FuncDecl{Name: id},
provided: true}
t.functions = append(t.functions, fn)
Expand Down
Loading