static NodeList* noesc; // list of possible non-escaping nodes, for printing
void
-escapes(void)
+escapes(NodeList *all)
{
NodeList *l;
theSink.escloopdepth = -1;
safetag = strlit("noescape");
+ noesc = nil;
- // flow-analyze top level functions
- for(l=xtop; l; l=l->next)
+ // flow-analyze functions
+ for(l=all; l; l=l->next)
if(l->n->op == ODCLFUNC || l->n->op == OCLOSURE)
escfunc(l->n);
escflood(l->n);
// for all top level functions, tag the typenodes corresponding to the param nodes
- for(l=xtop; l; l=l->next)
+ for(l=all; l; l=l->next)
if(l->n->op == ODCLFUNC)
esctag(l->n);
/*
* esc.c
*/
-void escapes(void);
+void escapes(NodeList*);
/*
* export.c
main(int argc, char *argv[])
{
int i, c;
- NodeList *l;
+ NodeList *l, *batch;
char *p;
#ifdef SIGBUS
// Phase 5: escape analysis.
if(!debug['N'])
- escapes();
+ escapes(xtop);
// Phase 6: Compile top level functions.
for(l=xtop; l; l=l->next)
fninit(xtop);
// Phase 6b: Compile all closures.
+ // Can generate more closures, so run in batches.
while(closures) {
- l = closures;
+ batch = closures;
closures = nil;
- for(; l; l=l->next) {
- if (debug['l'])
+ if(debug['l'])
+ for(l=batch; l; l=l->next)
inlcalls(l->n);
+ if(!debug['N'])
+ escapes(batch);
+ for(l=batch; l; l=l->next)
funccompile(l->n, 1);
- }
}
// Phase 7: check external declarations.
// license that can be found in the LICENSE file.
// Test, using compiler diagnostic flags, that the escape analysis is working.
-// Compiles but does not run.
+// Compiles but does not run. Inlining is disabled.
package foo
--- /dev/null
+// errchk -0 $G -m $D/$F.go
+
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test, using compiler diagnostic flags, that the escape analysis is working.
+// Compiles but does not run. Inlining is enabled.
+
+package foo
+
+var p *int
+
+func alloc(x int) *int { // ERROR "can inline alloc" "moved to heap: x"
+ return &x // ERROR "&x escapes to heap"
+}
+
+var f func()
+
+func f1() {
+ p = alloc(2) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
+
+ // Escape analysis used to miss inlined code in closures.
+
+ func() { // ERROR "func literal does not escape"
+ p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
+ }()
+
+ f = func() { // ERROR "func literal escapes to heap"
+ p = alloc(3) // ERROR "inlining call to alloc" "&x escapes to heap" "moved to heap: x"
+ }
+ f()
+}