Commit 2339b131 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: cleanup paramstoheap and returnsfromheap

Better documentation. Change parameter types from **Type and int to
just *Type and bool. Make use of short var declarations.

Change-Id: I909846ba0df65cd2bc05ee145b72d60e881588bd
Reviewed-on: https://go-review.googlesource.com/20495Reviewed-by: default avatarDave Cheney <dave@cheney.net>
parent b014b55b
...@@ -2578,23 +2578,21 @@ func vmatch1(l *Node, r *Node) bool { ...@@ -2578,23 +2578,21 @@ func vmatch1(l *Node, r *Node) bool {
return false return false
} }
// walk through argin parameters. // paramstoheap returns code to allocate memory for heap-escaped parameters
// generate and return code to allocate // and to copy non-result prameters' values from the stack.
// copies of escaped parameters to the heap. // If out is true, then code is also produced to zero-initialize their
func paramstoheap(argin **Type, out int) []*Node { // stack memory addresses.
var v *Node func paramstoheap(params *Type, out bool) []*Node {
var as *Node
var nn []*Node var nn []*Node
for t, it := IterFields(*argin); t != nil; t = it.Next() { for t, it := IterFields(params); t != nil; t = it.Next() {
v = t.Nname v := t.Nname
if v != nil && v.Sym != nil && v.Sym.Name[0] == '~' && v.Sym.Name[1] == 'r' { // unnamed result if v != nil && v.Sym != nil && strings.HasPrefix(v.Sym.Name, "~r") { // unnamed result
v = nil v = nil
} }
// For precise stacks, the garbage collector assumes results // For precise stacks, the garbage collector assumes results
// are always live, so zero them always. // are always live, so zero them always.
if out != 0 { if out {
// Defer might stop a panic and show the // Defer might stop a panic and show the
// return values as they exist at the time of panic. // return values as they exist at the time of panic.
// Make sure to zero them on entry to the function. // Make sure to zero them on entry to the function.
...@@ -2614,7 +2612,7 @@ func paramstoheap(argin **Type, out int) []*Node { ...@@ -2614,7 +2612,7 @@ func paramstoheap(argin **Type, out int) []*Node {
} }
nn = append(nn, Nod(OAS, v.Name.Heapaddr, prealloc[v])) nn = append(nn, Nod(OAS, v.Name.Heapaddr, prealloc[v]))
if v.Class&^PHEAP != PPARAMOUT { if v.Class&^PHEAP != PPARAMOUT {
as = Nod(OAS, v, v.Name.Param.Stackparam) as := Nod(OAS, v, v.Name.Param.Stackparam)
v.Name.Param.Stackparam.Typecheck = 1 v.Name.Param.Stackparam.Typecheck = 1
typecheck(&as, Etop) typecheck(&as, Etop)
as = applywritebarrier(as) as = applywritebarrier(as)
...@@ -2625,13 +2623,12 @@ func paramstoheap(argin **Type, out int) []*Node { ...@@ -2625,13 +2623,12 @@ func paramstoheap(argin **Type, out int) []*Node {
return nn return nn
} }
// walk through argout parameters copying back to stack // returnsfromheap returns code to copy values for heap-escaped parameters
func returnsfromheap(argin **Type) []*Node { // back to the stack.
var v *Node func returnsfromheap(params *Type) []*Node {
var nn []*Node var nn []*Node
for t, it := IterFields(*argin); t != nil; t = it.Next() { for t, it := IterFields(params); t != nil; t = it.Next() {
v = t.Nname v := t.Nname
if v == nil || v.Class != PHEAP|PPARAMOUT { if v == nil || v.Class != PHEAP|PPARAMOUT {
continue continue
} }
...@@ -2641,18 +2638,18 @@ func returnsfromheap(argin **Type) []*Node { ...@@ -2641,18 +2638,18 @@ func returnsfromheap(argin **Type) []*Node {
return nn return nn
} }
// take care of migrating any function in/out args // heapmoves generates code to handle migrating heap-escaped parameters
// between the stack and the heap. adds code to // between the stack and the heap. The generated code is added to Curfn's
// curfn's before and after lists. // Enter and Exit lists.
func heapmoves() { func heapmoves() {
lno := lineno lno := lineno
lineno = Curfn.Lineno lineno = Curfn.Lineno
nn := paramstoheap(Curfn.Type.RecvP(), 0) nn := paramstoheap(Curfn.Type.Recv(), false)
nn = append(nn, paramstoheap(Curfn.Type.ParamsP(), 0)...) nn = append(nn, paramstoheap(Curfn.Type.Params(), false)...)
nn = append(nn, paramstoheap(Curfn.Type.ResultsP(), 1)...) nn = append(nn, paramstoheap(Curfn.Type.Results(), true)...)
Curfn.Func.Enter.Append(nn...) Curfn.Func.Enter.Append(nn...)
lineno = Curfn.Func.Endlineno lineno = Curfn.Func.Endlineno
Curfn.Func.Exit.Append(returnsfromheap(Curfn.Type.ResultsP())...) Curfn.Func.Exit.Append(returnsfromheap(Curfn.Type.Results())...)
lineno = lno lineno = lno
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment