Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: refactor generic visitor code #42981

Closed
mdempsky opened this issue Dec 3, 2020 · 5 comments
Closed

cmd/compile: refactor generic visitor code #42981

mdempsky opened this issue Dec 3, 2020 · 5 comments

Comments

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Dec 3, 2020

There's a bunch of implementations of ad hoc, generic IR tree walking code within the compiler frontend.

Examples:

if n.Left() != nil && hasCall(n.Left()) {
return true
}
if n.Right() != nil && hasCall(n.Right()) {
return true
}
for _, x := range n.Init().Slice() {
if hasCall(x) {
return true
}
}
for _, x := range n.Body().Slice() {
if hasCall(x) {
return true
}
}
for _, x := range n.List().Slice() {
if hasCall(x) {
return true
}
}
for _, x := range n.Rlist().Slice() {
if hasCall(x) {
return true
}
}

if hascallchan(n.Left()) || hascallchan(n.Right()) {
return true
}
for _, n1 := range n.List().Slice() {
if hascallchan(n1) {
return true
}
}
for _, n2 := range n.Rlist().Slice() {

return v.visit(n.Left()) || v.visit(n.Right()) ||
v.visitList(n.List()) || v.visitList(n.Rlist()) ||
v.visitList(n.Init()) || v.visitList(n.Body())

cnt += countNodes(n.Left())
cnt += countNodes(n.Right())
for _, n1 := range n.Init().Slice() {
cnt += countNodes(n1)
}
for _, n1 := range n.Body().Slice() {
cnt += countNodes(n1)
}
for _, n1 := range n.List().Slice() {
cnt += countNodes(n1)
}
for _, n1 := range n.Rlist().Slice() {
cnt += countNodes(n1)
}

if a := v.visit(n.Left()); a != nil {
return a
}
if a := v.visit(n.Right()); a != nil {
return a
}
if a := v.visitList(n.List()); a != nil {
return a
}
if a := v.visitList(n.Rlist()); a != nil {
return a
}
if a := v.visitList(n.Init()); a != nil {
return a
}
if a := v.visitList(n.Body()); a != nil {
return a
}

markbreak(labels, n.Left(), implicit)
markbreak(labels, n.Right(), implicit)
markbreaklist(labels, n.Init(), implicit)
markbreaklist(labels, n.Body(), implicit)
markbreaklist(labels, n.List(), implicit)
markbreaklist(labels, n.Rlist(), implicit)

It should be possible to rewrite a lot of this code to use ir.Inspect or ir.InspectList instead. This will eventually be helpful for getting rid of the generic Left/Right/etc methods.

For the markbreak case (and maybe others), it might be useful to provide ir analogs to ast.Walk and ast.Visitor. (See also how ast.Inspect is implemented, as that API was inspirational for ir.Inspect.)

/cc @cuonglm

@mdempsky
Copy link
Member Author

@mdempsky mdempsky commented Dec 3, 2020

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Dec 4, 2020

Change https://golang.org/cl/275305 mentions this issue: [dev.regabi] cmd/compile: add RefersTo and DeclaredBy helpers

Loading

@rsc
Copy link
Contributor

@rsc rsc commented Dec 4, 2020

I have been working on this the last couple days. Please don't start. CLs coming soon!

Loading

@rsc
Copy link
Contributor

@rsc rsc commented Dec 4, 2020

https://go-review.googlesource.com/c/go/+/275311 is the CL introducing the API. See the rest of the stack for uses.
Best to discuss on the CLs themselves - I get too much GitHub spam to keep up with the issue tracker.

Loading

@mdempsky
Copy link
Member Author

@mdempsky mdempsky commented Dec 4, 2020

Russ's CLs have landed. This is done now.

Loading

@mdempsky mdempsky closed this Dec 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants