From 5c03b515e2ffee0b8ea18f8a6b6f4a48a8142f19 Mon Sep 17 00:00:00 2001 From: tmwatchanan Date: Sun, 1 Nov 2026 14:52:28 +0700 Subject: [PATCH] Make the stack-size estimate linear without recursion max_stacksize() walked every simple path of the caller graph from every function, with no reuse: exponential in shared callees and O(n^3) on a chain. On a 9,010-function script it made 10.7 billion calls (800-1,050 s of CPU) for a compile that takes 8 s without it. The result of the walk above a function depends only on the functions on the path that are in its own strongly connected component (only those can be met above it). When the walk enters another component, none are on the path, so that result is computed once and reused. Components come from Tarjan's algorithm; inside a recursive component the walk is path-by-path as before, so every printed number and every -R recursion line stays the same. The chain of the -R report is rebuilt by replaying the last path of the old walk. The same script now takes 0.2 s in the estimate, with identical output and .amx. This also fixes a heap overflow: a self-recursive function can be on the path twice, so the path could exceed the numfunctions+1 entries of symstack/rsymstack (free(): invalid pointer on random programs). run_tests.py: output_check tests can match stdout (stdout_pattern). --- source/compiler/sc1.c | 210 +++++++++++++--- source/compiler/tests/run_tests.py | 14 +- source/compiler/tests/stack_estimate_dag.meta | 7 + source/compiler/tests/stack_estimate_dag.pwn | 43 ++++ .../tests/stack_estimate_recursion_quiet.meta | 7 + .../tests/stack_estimate_recursion_quiet.pwn | 35 +++ .../stack_estimate_recursion_report.meta | 20 ++ .../tests/stack_estimate_recursion_report.pwn | 64 +++++ .../tests/stack_estimate_self_recursion.meta | 7 + .../tests/stack_estimate_self_recursion.pwn | 225 ++++++++++++++++++ .../tests/stack_estimate_wide_dag.meta | 7 + .../tests/stack_estimate_wide_dag.pwn | 171 +++++++++++++ 12 files changed, 769 insertions(+), 41 deletions(-) create mode 100644 source/compiler/tests/stack_estimate_dag.meta create mode 100644 source/compiler/tests/stack_estimate_dag.pwn create mode 100644 source/compiler/tests/stack_estimate_recursion_quiet.meta create mode 100644 source/compiler/tests/stack_estimate_recursion_quiet.pwn create mode 100644 source/compiler/tests/stack_estimate_recursion_report.meta create mode 100644 source/compiler/tests/stack_estimate_recursion_report.pwn create mode 100644 source/compiler/tests/stack_estimate_self_recursion.meta create mode 100644 source/compiler/tests/stack_estimate_self_recursion.pwn create mode 100644 source/compiler/tests/stack_estimate_wide_dag.meta create mode 100644 source/compiler/tests/stack_estimate_wide_dag.pwn diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 89c89833..1405d83a 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -5061,42 +5061,109 @@ static void reduce_referrers(symbol *root) } #if !defined SC_LIGHT -static long max_stacksize_recurse(symbol **sourcesym,symbol *sym,symbol **rsourcesym,long basesize,int *pubfuncparams,int *recursion) +/* Per-function data for the stack estimate. While max_stacksize() runs, the + * entry of a function is info[sym->compound]: the "compound" field is unused + * for functions, so it holds the index (and is reset to 0 afterwards). + */ +typedef struct s_stkfunc { + symbol *sym; + long size; /* memoized max_stacksize_recurse() with basesize 0 */ + int scc; /* strongly connected component of the referrer graph */ + int index,lowlink; /* for Tarjan's algorithm (index -1 = not visited) */ + int onpath; /* how often the function is on the current path */ + char instack; /* function is on the stack of Tarjan's algorithm */ + char memo; /* "size" and "recursion" are valid */ + char recursion; /* recursion was detected above this function */ +} stkfunc; + +#define STKFUNC(info,s) (&(info)[(s)->compound]) + +/* Tarjan's algorithm: functions that reach each other through their + * referrers (that is, recursion) end up in the same component. + */ +static void stack_scc(stkfunc *info,symbol *sym,symbol **stack,int *stktop,int *counter,int *numscc) +{ + stkfunc *f=STKFUNC(info,sym); + stkfunc *r; + symbol *top; + int i; + + assert(f->sym==sym); + f->index=f->lowlink=(*counter)++; + stack[(*stktop)++]=sym; + f->instack=TRUE; + for (i=0; inumrefers; i++) { + if (sym->refer[i]==NULL) + continue; + r=STKFUNC(info,sym->refer[i]); + assert(r->sym==sym->refer[i]); + if (r->index<0) { + stack_scc(info,sym->refer[i],stack,stktop,counter,numscc); + if (r->lowlinklowlink) + f->lowlink=r->lowlink; + } else if (r->instack && r->indexlowlink) { + f->lowlink=r->index; + } /* if */ + } /* for */ + if (f->lowlink==f->index) { + do { + top=stack[--(*stktop)]; + STKFUNC(info,top)->instack=FALSE; + STKFUNC(info,top)->scc=*numscc; + } while (top!=sym); + (*numscc)++; + } /* if */ +} + +static long max_stacksize_recurse(stkfunc *info,symbol *sym,long basesize,int *pubfuncparams,int *recursion) { long size,maxsize; - int i,stkpos; + int i,rec; + stkfunc *f,*r; + symbol *ref; - assert(sourcesym!=NULL); + assert(info!=NULL); assert(sym!=NULL); assert(sym->ident==iFUNCTN); assert((sym->usage & uNATIVE)==0); assert(recursion!=NULL); + f=STKFUNC(info,sym); maxsize=sym->x.stacksize; for (i=0; inumrefers; i++) { - if (sym->refer[i]!=NULL) { - assert(sym->refer[i]->ident==iFUNCTN); - assert((sym->refer[i]->usage & uNATIVE)==0); /* a native function cannot refer to a user-function */ - *(rsourcesym)=sym; - *(rsourcesym+1)=NULL; - for (stkpos=0; sourcesym[stkpos]!=NULL; stkpos++) { - if (sym->refer[i]==sourcesym[stkpos]) { /* recursion detection */ - *recursion=TRUE; - goto break_recursion; /* recursion was detected, quit loop */ - } /* if */ - } /* for */ - /* add this symbol to the stack */ - sourcesym[stkpos]=sym; - sourcesym[stkpos+1]=NULL; + ref=sym->refer[i]; + if (ref==NULL) + continue; + assert(ref->ident==iFUNCTN); + assert((ref->usage & uNATIVE)==0); /* a native function cannot refer to a user-function */ + r=STKFUNC(info,ref); + assert(r->sym==ref); + if (r->onpath>0) { /* recursion detection */ + *recursion=TRUE; + break; /* recursion was detected, quit loop */ + } /* if */ + f->onpath++; /* add this symbol to the path */ + if (r->scc!=f->scc) { + /* no function on the path can be reached from "ref" (it would be in + * the same component as "sym"), so its result does not depend on the + * path: compute it once */ + if (!r->memo) { + rec=FALSE; + r->size=max_stacksize_recurse(info,ref,0L,pubfuncparams,&rec); + r->recursion=(char)rec; + r->memo=TRUE; + } /* if */ + size=r->size+sym->x.stacksize; + if (r->recursion) + *recursion=TRUE; + } else { /* check size of callee */ - size=max_stacksize_recurse(sourcesym,sym->refer[i],rsourcesym+1,sym->x.stacksize,pubfuncparams,recursion); - if (maxsizex.stacksize,pubfuncparams,recursion); } /* if */ + f->onpath--; /* remove this symbol from the path */ + if (maxsizeusage & uPUBLIC)!=0) { /* Find out how many parameters a public function has, then see if this @@ -5117,26 +5184,69 @@ static long max_stacksize_recurse(symbol **sourcesym,symbol *sym,symbol **rsourc return maxsize+basesize; } +/* Fill "chain" with the path that the walk from "sym" took last when every + * referrer is followed (no memo): the chain the recursion report prints. In + * each function on it, the walk ended at the first referrer that was already + * on the path, or else at the last referrer. A function without referrers + * does not appear. The chain ends with NULL. + */ +static void recursion_chain(stkfunc *info,symbol *sym,symbol **chain) +{ + int depth,i,last,onpath; + + assert(STKFUNC(info,sym)->onpath==1); + for (depth=0; ; depth++) { + last=-1; + onpath=FALSE; + for (i=0; inumrefers && !onpath; i++) { + if (sym->refer[i]!=NULL) { + last=i; + onpath=(STKFUNC(info,sym->refer[i])->onpath>0); + } /* if */ + } /* for */ + if (last<0) + break; + chain[depth]=sym; + chain[depth+1]=NULL; + if (onpath) + break; + STKFUNC(info,sym)->onpath++; + sym=sym->refer[last]; + } /* for */ + /* take the functions that were added off the path again */ + for (i=0; ionpath--; +} + static long max_stacksize(symbol *root,int *recursion) { /* Loop over all non-native functions. For each function, loop * over all of its referrers, accumulating the stack requirements. - * Detect (indirect) recursion with a "mark-and-sweep" algorithm. - * I (mis-)use the "compound" field of the symbol structure for - * the marker, as this field is unused for functions. + * A referrer that is already on the path means (indirect) recursion; + * the walk then skips the remaining referrers of that function. + * + * The functions on the path all reach the current function, so the + * walk from a function can only meet those that are in the same + * strongly connected component (they reach each other: recursion). + * When the walk enters another component, it meets none of them, so + * the result for that function is computed once and reused. Without + * recursion, this visits each function and referrer a few times + * instead of following every path; within a component, every path is + * still followed, as before. * * Note that the stack is shared with the heap. A host application * may "eat" cells from the heap as well, through amx_Allot(). The * stack requirements are thus only an estimate. */ long size,maxsize; - int maxparams,numfunctions; + int maxparams,numfunctions,counter,numscc,stktop; symbol *sym; - symbol **symstack,**rsymstack; + symbol **sccstack,**rsymstack; + stkfunc *info; assert(root!=NULL); assert(recursion!=NULL); - /* count number of functions (for allocating the stack for recursion detection) */ + /* count number of functions (for allocating the per-function data) */ numfunctions=0; for (sym=root->next; sym!=NULL; sym=sym->next) { if (sym->ident==iFUNCTN) { @@ -5145,13 +5255,31 @@ static long max_stacksize(symbol *root,int *recursion) numfunctions++; } /* if */ } /* for */ - /* allocate function symbol stack */ - symstack=(symbol **)malloc((numfunctions+1)*sizeof(symbol*)); - rsymstack=(symbol **)malloc((numfunctions+1)*sizeof(symbol*)); - if (symstack==NULL || rsymstack==NULL) + /* allocate the per-function data and the stacks; a function that calls + * itself can be on the path twice, so the chain for the recursion report + * holds up to 2*numfunctions functions plus the terminating NULL */ + info=(stkfunc *)malloc((numfunctions+1)*sizeof(stkfunc)); + sccstack=(symbol **)malloc((numfunctions+1)*sizeof(symbol*)); + rsymstack=(symbol **)malloc((2*numfunctions+2)*sizeof(symbol*)); + if (info==NULL || sccstack==NULL || rsymstack==NULL) error(103); /* insufficient memory (fatal error) */ - memset(symstack,0,(numfunctions+1)*sizeof(symbol*)); - memset(rsymstack,0,(numfunctions+1)*sizeof(symbol*)); + memset(info,0,(numfunctions+1)*sizeof(stkfunc)); + memset(rsymstack,0,(2*numfunctions+2)*sizeof(symbol*)); + numfunctions=0; + for (sym=root->next; sym!=NULL; sym=sym->next) { + if (sym->ident==iFUNCTN && (sym->usage & uNATIVE)==0) { + sym->compound=numfunctions; + info[numfunctions].sym=sym; + info[numfunctions].index=-1; + numfunctions++; + } /* if */ + } /* for */ + counter=numscc=stktop=0; + for (sym=root->next; sym!=NULL; sym=sym->next) + if (sym->ident==iFUNCTN && (sym->usage & uNATIVE)==0 && STKFUNC(info,sym)->index<0) + stack_scc(info,sym,sccstack,&stktop,&counter,&numscc); + assert(stktop==0); + free((void*)sccstack); maxsize=0; maxparams=0; @@ -5162,11 +5290,11 @@ static long max_stacksize(symbol *root,int *recursion) if (sym->ident!=iFUNCTN || (sym->usage & uNATIVE)!=0) continue; /* accumulate stack size for this symbol */ - symstack[0]=sym; - assert(symstack[1]==NULL); + STKFUNC(info,sym)->onpath=1; /* the path starts at this function */ recursion_detected=FALSE; - size=max_stacksize_recurse(symstack,sym,rsymstack,0L,&maxparams,&recursion_detected); + size=max_stacksize_recurse(info,sym,0L,&maxparams,&recursion_detected); if (recursion_detected && pc_recursion) { + recursion_chain(info,sym,rsymstack); if (rsymstack[1]==NULL) { pc_printf("recursion detected: function %s directly calls itself\n", sym->name); } else { @@ -5180,12 +5308,16 @@ static long max_stacksize(symbol *root,int *recursion) } *recursion=recursion_detected; } + STKFUNC(info,sym)->onpath=0; assert(size>=0); if (maxsizenext; sym!=NULL; sym=sym->next) + if (sym->ident==iFUNCTN && (sym->usage & uNATIVE)==0) + sym->compound=0; + free((void*)info); free((void*)rsymstack); maxsize++; /* +1 because a zero cell is always pushed on top * of the stack to catch stack overwrites */ diff --git a/source/compiler/tests/run_tests.py b/source/compiler/tests/run_tests.py index 2b58ce90..f303c707 100644 --- a/source/compiler/tests/run_tests.py +++ b/source/compiler/tests/run_tests.py @@ -58,10 +58,11 @@ def strip(s): return s.strip(' \t\r\n') class OutputCheckTest: - def __init__(self, name, errors=None, extra_args=None): + def __init__(self, name, errors=None, extra_args=None, stdout_pattern=None): self.name = name self.errors = errors self.extra_args = extra_args + self.stdout_pattern = stdout_pattern def run(self): args = [self.name + '.pwn'] @@ -84,6 +85,14 @@ def run(self): 'Actual errors:\n\n{}' ).format(expected_errors, errors) return False + if self.stdout_pattern is not None: + pattern = strip(normalize_newlines(self.stdout_pattern)) + if re.search(pattern, normalize_newlines(stdout), re.MULTILINE) is None: + self.fail_reason = ( + 'Output didn\'t match\n\nExpected output pattern:\n\n{}\n\n' + 'Actual output:\n\n{}' + ).format(pattern, stdout) + return False return True class PCodeCheckTest: @@ -192,7 +201,8 @@ def run(self): tests.append(OutputCheckTest( name=name, errors=metadata.get('errors'), - extra_args=metadata.get('extra_args'))) + extra_args=metadata.get('extra_args'), + stdout_pattern=metadata.get('stdout_pattern'))) elif test_type == 'pcode_check': tests.append(PCodeCheckTest( name=name, diff --git a/source/compiler/tests/stack_estimate_dag.meta b/source/compiler/tests/stack_estimate_dag.meta new file mode 100644 index 00000000..bba2f3a9 --- /dev/null +++ b/source/compiler/tests/stack_estimate_dag.meta @@ -0,0 +1,7 @@ +{ + 'test_type': 'output_check', + 'extra_args': ['-v2'], + 'stdout_pattern': r""" +estimated max\. usage=75 cells +""" +} diff --git a/source/compiler/tests/stack_estimate_dag.pwn b/source/compiler/tests/stack_estimate_dag.pwn new file mode 100644 index 00000000..4fe7704f --- /dev/null +++ b/source/compiler/tests/stack_estimate_dag.pwn @@ -0,0 +1,43 @@ +// Stack estimate of a call graph without recursion: the longest chain of +// callers wins, and the shared callees (Leaf, Mid2) are reached along +// several paths. + +Leaf() +{ + new a[10]; + a[0] = 1; + return a[0]; +} + +Mid1() +{ + new a[20]; + a[0] = Leaf(); + return a[0]; +} + +Mid2() +{ + new a[5]; + a[0] = Leaf(); + return a[0]; +} + +Top() +{ + new a[30]; + a[0] = Mid1() + Mid2(); + return a[0]; +} + +forward Callback(x, y, z); +public Callback(x, y, z) +{ + return Mid2() + x + y + z; +} + +main() +{ + Top(); + Mid2(); +} diff --git a/source/compiler/tests/stack_estimate_recursion_quiet.meta b/source/compiler/tests/stack_estimate_recursion_quiet.meta new file mode 100644 index 00000000..1d0fa064 --- /dev/null +++ b/source/compiler/tests/stack_estimate_recursion_quiet.meta @@ -0,0 +1,7 @@ +{ + 'test_type': 'output_check', + 'extra_args': ['-v2'], + 'stdout_pattern': r""" +^Stack/heap size: .*; estimated max\. usage=31 cells +""" +} diff --git a/source/compiler/tests/stack_estimate_recursion_quiet.pwn b/source/compiler/tests/stack_estimate_recursion_quiet.pwn new file mode 100644 index 00000000..dc980ae5 --- /dev/null +++ b/source/compiler/tests/stack_estimate_recursion_quiet.pwn @@ -0,0 +1,35 @@ +// Without -R, recursion is not reported and the estimate is still printed +// as a number (the walk stops at the first caller that is already on the +// path). + +Fact(n) +{ + new a[8]; + a[0] = n; + if (n <= 1) + return 1; + return a[0] * Fact(n - 1); +} + +Pong(n) +{ + new a[16]; + a[0] = n; + if (n > 0) + return Ping(a[0] - 1); + return 0; +} + +Ping(n) +{ + new a[4]; + a[0] = n; + if (n > 0) + return Pong(a[0] - 1); + return Fact(n); +} + +main() +{ + Ping(3); +} diff --git a/source/compiler/tests/stack_estimate_recursion_report.meta b/source/compiler/tests/stack_estimate_recursion_report.meta new file mode 100644 index 00000000..8cba3a45 --- /dev/null +++ b/source/compiler/tests/stack_estimate_recursion_report.meta @@ -0,0 +1,20 @@ +{ + 'test_type': 'output_check', + 'extra_args': ['-v2', '-R+'], + 'stdout_pattern': r""" +^recursion detected: function Again directly calls itself +recursion detected: function Base indirectly calls itself: +Base <- Again <- Again <- Base +recursion detected: function Fact directly calls itself +recursion detected: function Helper indirectly calls itself: +Helper <- Walk <- Helper +recursion detected: function Ping directly calls itself +recursion detected: function Pong indirectly calls itself: +Pong <- Ping <- Pong +recursion detected: function Walk directly calls itself +Header size: .* +Code size: .* +Data size: .* +Stack/heap size: .*; estimated max\. usage: unknown, due to recursion +""" +} diff --git a/source/compiler/tests/stack_estimate_recursion_report.pwn b/source/compiler/tests/stack_estimate_recursion_report.pwn new file mode 100644 index 00000000..077b2ba5 --- /dev/null +++ b/source/compiler/tests/stack_estimate_recursion_report.pwn @@ -0,0 +1,64 @@ +// With -R+, every function from which the walk meets recursion is reported, +// with the chain of callers the walk took last. The chain does not always +// contain the recursion itself (Helper is reported, but only Walk calls +// itself); this is how the report has always worked. + +Fact(n) +{ + if (n <= 1) + return 1; + return n * Fact(n - 1); +} + +Pong(n) +{ + if (n > 0) + return Ping(n - 1); + return 0; +} + +Ping(n) +{ + if (n > 0) + return Pong(n - 1); + return 0; +} + +Helper() +{ + return 1; +} + +Walk(n) +{ + if (n > 0) + Walk(n - 1); + return Helper(); +} + +// Outer calls Again before Again calls itself, so the walk from Base visits +// Again twice on one path. +Base() +{ + return 1; +} + +Outer() +{ + return Again(0); +} + +Again(n) +{ + if (n > 0) + return Again(n - 1); + return Base(); +} + +main() +{ + Fact(5); + Ping(3); + Walk(2); + Outer(); +} diff --git a/source/compiler/tests/stack_estimate_self_recursion.meta b/source/compiler/tests/stack_estimate_self_recursion.meta new file mode 100644 index 00000000..d23f21aa --- /dev/null +++ b/source/compiler/tests/stack_estimate_self_recursion.meta @@ -0,0 +1,7 @@ +{ + 'test_type': 'output_check', + 'extra_args': ['-v2'], + 'stdout_pattern': r""" +estimated max\. usage=330 cells +""" +} diff --git a/source/compiler/tests/stack_estimate_self_recursion.pwn b/source/compiler/tests/stack_estimate_self_recursion.pwn new file mode 100644 index 00000000..a74ea2ab --- /dev/null +++ b/source/compiler/tests/stack_estimate_self_recursion.pwn @@ -0,0 +1,225 @@ +// Each function calls itself and is called by the next one, defined first so +// that it comes first in the caller list. The walk then puts each function on +// the path twice; the old compiler sized its path buffers for once and +// overflowed them (heap corruption). + +C23(n) +{ + new a[4]; + a[0] = n; + if (n > 0) + return C23(n - 1) + C22(n - 1); + return a[0]; +} + +C22(n) +{ + new a[3]; + a[0] = n; + if (n > 0) + return C22(n - 1) + C21(n - 1); + return a[0]; +} + +C21(n) +{ + new a[2]; + a[0] = n; + if (n > 0) + return C21(n - 1) + C20(n - 1); + return a[0]; +} + +C20(n) +{ + new a[1]; + a[0] = n; + if (n > 0) + return C20(n - 1) + C19(n - 1); + return a[0]; +} + +C19(n) +{ + new a[5]; + a[0] = n; + if (n > 0) + return C19(n - 1) + C18(n - 1); + return a[0]; +} + +C18(n) +{ + new a[4]; + a[0] = n; + if (n > 0) + return C18(n - 1) + C17(n - 1); + return a[0]; +} + +C17(n) +{ + new a[3]; + a[0] = n; + if (n > 0) + return C17(n - 1) + C16(n - 1); + return a[0]; +} + +C16(n) +{ + new a[2]; + a[0] = n; + if (n > 0) + return C16(n - 1) + C15(n - 1); + return a[0]; +} + +C15(n) +{ + new a[1]; + a[0] = n; + if (n > 0) + return C15(n - 1) + C14(n - 1); + return a[0]; +} + +C14(n) +{ + new a[5]; + a[0] = n; + if (n > 0) + return C14(n - 1) + C13(n - 1); + return a[0]; +} + +C13(n) +{ + new a[4]; + a[0] = n; + if (n > 0) + return C13(n - 1) + C12(n - 1); + return a[0]; +} + +C12(n) +{ + new a[3]; + a[0] = n; + if (n > 0) + return C12(n - 1) + C11(n - 1); + return a[0]; +} + +C11(n) +{ + new a[2]; + a[0] = n; + if (n > 0) + return C11(n - 1) + C10(n - 1); + return a[0]; +} + +C10(n) +{ + new a[1]; + a[0] = n; + if (n > 0) + return C10(n - 1) + C9(n - 1); + return a[0]; +} + +C9(n) +{ + new a[5]; + a[0] = n; + if (n > 0) + return C9(n - 1) + C8(n - 1); + return a[0]; +} + +C8(n) +{ + new a[4]; + a[0] = n; + if (n > 0) + return C8(n - 1) + C7(n - 1); + return a[0]; +} + +C7(n) +{ + new a[3]; + a[0] = n; + if (n > 0) + return C7(n - 1) + C6(n - 1); + return a[0]; +} + +C6(n) +{ + new a[2]; + a[0] = n; + if (n > 0) + return C6(n - 1) + C5(n - 1); + return a[0]; +} + +C5(n) +{ + new a[1]; + a[0] = n; + if (n > 0) + return C5(n - 1) + C4(n - 1); + return a[0]; +} + +C4(n) +{ + new a[5]; + a[0] = n; + if (n > 0) + return C4(n - 1) + C3(n - 1); + return a[0]; +} + +C3(n) +{ + new a[4]; + a[0] = n; + if (n > 0) + return C3(n - 1) + C2(n - 1); + return a[0]; +} + +C2(n) +{ + new a[3]; + a[0] = n; + if (n > 0) + return C2(n - 1) + C1(n - 1); + return a[0]; +} + +C1(n) +{ + new a[2]; + a[0] = n; + if (n > 0) + return C1(n - 1) + C0(n - 1); + return a[0]; +} + +C0(n) +{ + new a[1]; + a[0] = n; + if (n > 0) + return C0(n - 1) + 0; + return a[0]; +} + +main() +{ + C23(3); +} diff --git a/source/compiler/tests/stack_estimate_wide_dag.meta b/source/compiler/tests/stack_estimate_wide_dag.meta new file mode 100644 index 00000000..d5579e59 --- /dev/null +++ b/source/compiler/tests/stack_estimate_wide_dag.meta @@ -0,0 +1,7 @@ +{ + 'test_type': 'output_check', + 'extra_args': ['-v2'], + 'stdout_pattern': r""" +estimated max\. usage=596 cells +""" +} diff --git a/source/compiler/tests/stack_estimate_wide_dag.pwn b/source/compiler/tests/stack_estimate_wide_dag.pwn new file mode 100644 index 00000000..dafa7641 --- /dev/null +++ b/source/compiler/tests/stack_estimate_wide_dag.pwn @@ -0,0 +1,171 @@ +// Generated by bench/stack-estimate/gen_layered.py --layers 40 --width 4 +// Layered call graph: 160 functions, 4**40 call paths. + +L0_0() { new a[1]; a[0] = L1_0() + L1_1() + L1_2() + L1_3(); return a[0]; } +L0_1() { new a[4]; a[0] = L1_0() + L1_1() + L1_2() + L1_3(); return a[0]; } +L0_2() { new a[7]; a[0] = L1_0() + L1_1() + L1_2() + L1_3(); return a[0]; } +L0_3() { new a[10]; a[0] = L1_0() + L1_1() + L1_2() + L1_3(); return a[0]; } +L1_0() { new a[8]; a[0] = L2_0() + L2_1() + L2_2() + L2_3(); return a[0]; } +L1_1() { new a[11]; a[0] = L2_0() + L2_1() + L2_2() + L2_3(); return a[0]; } +L1_2() { new a[1]; a[0] = L2_0() + L2_1() + L2_2() + L2_3(); return a[0]; } +L1_3() { new a[4]; a[0] = L2_0() + L2_1() + L2_2() + L2_3(); return a[0]; } +L2_0() { new a[2]; a[0] = L3_0() + L3_1() + L3_2() + L3_3(); return a[0]; } +L2_1() { new a[5]; a[0] = L3_0() + L3_1() + L3_2() + L3_3(); return a[0]; } +L2_2() { new a[8]; a[0] = L3_0() + L3_1() + L3_2() + L3_3(); return a[0]; } +L2_3() { new a[11]; a[0] = L3_0() + L3_1() + L3_2() + L3_3(); return a[0]; } +L3_0() { new a[9]; a[0] = L4_0() + L4_1() + L4_2() + L4_3(); return a[0]; } +L3_1() { new a[12]; a[0] = L4_0() + L4_1() + L4_2() + L4_3(); return a[0]; } +L3_2() { new a[2]; a[0] = L4_0() + L4_1() + L4_2() + L4_3(); return a[0]; } +L3_3() { new a[5]; a[0] = L4_0() + L4_1() + L4_2() + L4_3(); return a[0]; } +L4_0() { new a[3]; a[0] = L5_0() + L5_1() + L5_2() + L5_3(); return a[0]; } +L4_1() { new a[6]; a[0] = L5_0() + L5_1() + L5_2() + L5_3(); return a[0]; } +L4_2() { new a[9]; a[0] = L5_0() + L5_1() + L5_2() + L5_3(); return a[0]; } +L4_3() { new a[12]; a[0] = L5_0() + L5_1() + L5_2() + L5_3(); return a[0]; } +L5_0() { new a[10]; a[0] = L6_0() + L6_1() + L6_2() + L6_3(); return a[0]; } +L5_1() { new a[13]; a[0] = L6_0() + L6_1() + L6_2() + L6_3(); return a[0]; } +L5_2() { new a[3]; a[0] = L6_0() + L6_1() + L6_2() + L6_3(); return a[0]; } +L5_3() { new a[6]; a[0] = L6_0() + L6_1() + L6_2() + L6_3(); return a[0]; } +L6_0() { new a[4]; a[0] = L7_0() + L7_1() + L7_2() + L7_3(); return a[0]; } +L6_1() { new a[7]; a[0] = L7_0() + L7_1() + L7_2() + L7_3(); return a[0]; } +L6_2() { new a[10]; a[0] = L7_0() + L7_1() + L7_2() + L7_3(); return a[0]; } +L6_3() { new a[13]; a[0] = L7_0() + L7_1() + L7_2() + L7_3(); return a[0]; } +L7_0() { new a[11]; a[0] = L8_0() + L8_1() + L8_2() + L8_3(); return a[0]; } +L7_1() { new a[1]; a[0] = L8_0() + L8_1() + L8_2() + L8_3(); return a[0]; } +L7_2() { new a[4]; a[0] = L8_0() + L8_1() + L8_2() + L8_3(); return a[0]; } +L7_3() { new a[7]; a[0] = L8_0() + L8_1() + L8_2() + L8_3(); return a[0]; } +L8_0() { new a[5]; a[0] = L9_0() + L9_1() + L9_2() + L9_3(); return a[0]; } +L8_1() { new a[8]; a[0] = L9_0() + L9_1() + L9_2() + L9_3(); return a[0]; } +L8_2() { new a[11]; a[0] = L9_0() + L9_1() + L9_2() + L9_3(); return a[0]; } +L8_3() { new a[1]; a[0] = L9_0() + L9_1() + L9_2() + L9_3(); return a[0]; } +L9_0() { new a[12]; a[0] = L10_0() + L10_1() + L10_2() + L10_3(); return a[0]; } +L9_1() { new a[2]; a[0] = L10_0() + L10_1() + L10_2() + L10_3(); return a[0]; } +L9_2() { new a[5]; a[0] = L10_0() + L10_1() + L10_2() + L10_3(); return a[0]; } +L9_3() { new a[8]; a[0] = L10_0() + L10_1() + L10_2() + L10_3(); return a[0]; } +L10_0() { new a[6]; a[0] = L11_0() + L11_1() + L11_2() + L11_3(); return a[0]; } +L10_1() { new a[9]; a[0] = L11_0() + L11_1() + L11_2() + L11_3(); return a[0]; } +L10_2() { new a[12]; a[0] = L11_0() + L11_1() + L11_2() + L11_3(); return a[0]; } +L10_3() { new a[2]; a[0] = L11_0() + L11_1() + L11_2() + L11_3(); return a[0]; } +L11_0() { new a[13]; a[0] = L12_0() + L12_1() + L12_2() + L12_3(); return a[0]; } +L11_1() { new a[3]; a[0] = L12_0() + L12_1() + L12_2() + L12_3(); return a[0]; } +L11_2() { new a[6]; a[0] = L12_0() + L12_1() + L12_2() + L12_3(); return a[0]; } +L11_3() { new a[9]; a[0] = L12_0() + L12_1() + L12_2() + L12_3(); return a[0]; } +L12_0() { new a[7]; a[0] = L13_0() + L13_1() + L13_2() + L13_3(); return a[0]; } +L12_1() { new a[10]; a[0] = L13_0() + L13_1() + L13_2() + L13_3(); return a[0]; } +L12_2() { new a[13]; a[0] = L13_0() + L13_1() + L13_2() + L13_3(); return a[0]; } +L12_3() { new a[3]; a[0] = L13_0() + L13_1() + L13_2() + L13_3(); return a[0]; } +L13_0() { new a[1]; a[0] = L14_0() + L14_1() + L14_2() + L14_3(); return a[0]; } +L13_1() { new a[4]; a[0] = L14_0() + L14_1() + L14_2() + L14_3(); return a[0]; } +L13_2() { new a[7]; a[0] = L14_0() + L14_1() + L14_2() + L14_3(); return a[0]; } +L13_3() { new a[10]; a[0] = L14_0() + L14_1() + L14_2() + L14_3(); return a[0]; } +L14_0() { new a[8]; a[0] = L15_0() + L15_1() + L15_2() + L15_3(); return a[0]; } +L14_1() { new a[11]; a[0] = L15_0() + L15_1() + L15_2() + L15_3(); return a[0]; } +L14_2() { new a[1]; a[0] = L15_0() + L15_1() + L15_2() + L15_3(); return a[0]; } +L14_3() { new a[4]; a[0] = L15_0() + L15_1() + L15_2() + L15_3(); return a[0]; } +L15_0() { new a[2]; a[0] = L16_0() + L16_1() + L16_2() + L16_3(); return a[0]; } +L15_1() { new a[5]; a[0] = L16_0() + L16_1() + L16_2() + L16_3(); return a[0]; } +L15_2() { new a[8]; a[0] = L16_0() + L16_1() + L16_2() + L16_3(); return a[0]; } +L15_3() { new a[11]; a[0] = L16_0() + L16_1() + L16_2() + L16_3(); return a[0]; } +L16_0() { new a[9]; a[0] = L17_0() + L17_1() + L17_2() + L17_3(); return a[0]; } +L16_1() { new a[12]; a[0] = L17_0() + L17_1() + L17_2() + L17_3(); return a[0]; } +L16_2() { new a[2]; a[0] = L17_0() + L17_1() + L17_2() + L17_3(); return a[0]; } +L16_3() { new a[5]; a[0] = L17_0() + L17_1() + L17_2() + L17_3(); return a[0]; } +L17_0() { new a[3]; a[0] = L18_0() + L18_1() + L18_2() + L18_3(); return a[0]; } +L17_1() { new a[6]; a[0] = L18_0() + L18_1() + L18_2() + L18_3(); return a[0]; } +L17_2() { new a[9]; a[0] = L18_0() + L18_1() + L18_2() + L18_3(); return a[0]; } +L17_3() { new a[12]; a[0] = L18_0() + L18_1() + L18_2() + L18_3(); return a[0]; } +L18_0() { new a[10]; a[0] = L19_0() + L19_1() + L19_2() + L19_3(); return a[0]; } +L18_1() { new a[13]; a[0] = L19_0() + L19_1() + L19_2() + L19_3(); return a[0]; } +L18_2() { new a[3]; a[0] = L19_0() + L19_1() + L19_2() + L19_3(); return a[0]; } +L18_3() { new a[6]; a[0] = L19_0() + L19_1() + L19_2() + L19_3(); return a[0]; } +L19_0() { new a[4]; a[0] = L20_0() + L20_1() + L20_2() + L20_3(); return a[0]; } +L19_1() { new a[7]; a[0] = L20_0() + L20_1() + L20_2() + L20_3(); return a[0]; } +L19_2() { new a[10]; a[0] = L20_0() + L20_1() + L20_2() + L20_3(); return a[0]; } +L19_3() { new a[13]; a[0] = L20_0() + L20_1() + L20_2() + L20_3(); return a[0]; } +L20_0() { new a[11]; a[0] = L21_0() + L21_1() + L21_2() + L21_3(); return a[0]; } +L20_1() { new a[1]; a[0] = L21_0() + L21_1() + L21_2() + L21_3(); return a[0]; } +L20_2() { new a[4]; a[0] = L21_0() + L21_1() + L21_2() + L21_3(); return a[0]; } +L20_3() { new a[7]; a[0] = L21_0() + L21_1() + L21_2() + L21_3(); return a[0]; } +L21_0() { new a[5]; a[0] = L22_0() + L22_1() + L22_2() + L22_3(); return a[0]; } +L21_1() { new a[8]; a[0] = L22_0() + L22_1() + L22_2() + L22_3(); return a[0]; } +L21_2() { new a[11]; a[0] = L22_0() + L22_1() + L22_2() + L22_3(); return a[0]; } +L21_3() { new a[1]; a[0] = L22_0() + L22_1() + L22_2() + L22_3(); return a[0]; } +L22_0() { new a[12]; a[0] = L23_0() + L23_1() + L23_2() + L23_3(); return a[0]; } +L22_1() { new a[2]; a[0] = L23_0() + L23_1() + L23_2() + L23_3(); return a[0]; } +L22_2() { new a[5]; a[0] = L23_0() + L23_1() + L23_2() + L23_3(); return a[0]; } +L22_3() { new a[8]; a[0] = L23_0() + L23_1() + L23_2() + L23_3(); return a[0]; } +L23_0() { new a[6]; a[0] = L24_0() + L24_1() + L24_2() + L24_3(); return a[0]; } +L23_1() { new a[9]; a[0] = L24_0() + L24_1() + L24_2() + L24_3(); return a[0]; } +L23_2() { new a[12]; a[0] = L24_0() + L24_1() + L24_2() + L24_3(); return a[0]; } +L23_3() { new a[2]; a[0] = L24_0() + L24_1() + L24_2() + L24_3(); return a[0]; } +L24_0() { new a[13]; a[0] = L25_0() + L25_1() + L25_2() + L25_3(); return a[0]; } +L24_1() { new a[3]; a[0] = L25_0() + L25_1() + L25_2() + L25_3(); return a[0]; } +L24_2() { new a[6]; a[0] = L25_0() + L25_1() + L25_2() + L25_3(); return a[0]; } +L24_3() { new a[9]; a[0] = L25_0() + L25_1() + L25_2() + L25_3(); return a[0]; } +L25_0() { new a[7]; a[0] = L26_0() + L26_1() + L26_2() + L26_3(); return a[0]; } +L25_1() { new a[10]; a[0] = L26_0() + L26_1() + L26_2() + L26_3(); return a[0]; } +L25_2() { new a[13]; a[0] = L26_0() + L26_1() + L26_2() + L26_3(); return a[0]; } +L25_3() { new a[3]; a[0] = L26_0() + L26_1() + L26_2() + L26_3(); return a[0]; } +L26_0() { new a[1]; a[0] = L27_0() + L27_1() + L27_2() + L27_3(); return a[0]; } +L26_1() { new a[4]; a[0] = L27_0() + L27_1() + L27_2() + L27_3(); return a[0]; } +L26_2() { new a[7]; a[0] = L27_0() + L27_1() + L27_2() + L27_3(); return a[0]; } +L26_3() { new a[10]; a[0] = L27_0() + L27_1() + L27_2() + L27_3(); return a[0]; } +L27_0() { new a[8]; a[0] = L28_0() + L28_1() + L28_2() + L28_3(); return a[0]; } +L27_1() { new a[11]; a[0] = L28_0() + L28_1() + L28_2() + L28_3(); return a[0]; } +L27_2() { new a[1]; a[0] = L28_0() + L28_1() + L28_2() + L28_3(); return a[0]; } +L27_3() { new a[4]; a[0] = L28_0() + L28_1() + L28_2() + L28_3(); return a[0]; } +L28_0() { new a[2]; a[0] = L29_0() + L29_1() + L29_2() + L29_3(); return a[0]; } +L28_1() { new a[5]; a[0] = L29_0() + L29_1() + L29_2() + L29_3(); return a[0]; } +L28_2() { new a[8]; a[0] = L29_0() + L29_1() + L29_2() + L29_3(); return a[0]; } +L28_3() { new a[11]; a[0] = L29_0() + L29_1() + L29_2() + L29_3(); return a[0]; } +L29_0() { new a[9]; a[0] = L30_0() + L30_1() + L30_2() + L30_3(); return a[0]; } +L29_1() { new a[12]; a[0] = L30_0() + L30_1() + L30_2() + L30_3(); return a[0]; } +L29_2() { new a[2]; a[0] = L30_0() + L30_1() + L30_2() + L30_3(); return a[0]; } +L29_3() { new a[5]; a[0] = L30_0() + L30_1() + L30_2() + L30_3(); return a[0]; } +L30_0() { new a[3]; a[0] = L31_0() + L31_1() + L31_2() + L31_3(); return a[0]; } +L30_1() { new a[6]; a[0] = L31_0() + L31_1() + L31_2() + L31_3(); return a[0]; } +L30_2() { new a[9]; a[0] = L31_0() + L31_1() + L31_2() + L31_3(); return a[0]; } +L30_3() { new a[12]; a[0] = L31_0() + L31_1() + L31_2() + L31_3(); return a[0]; } +L31_0() { new a[10]; a[0] = L32_0() + L32_1() + L32_2() + L32_3(); return a[0]; } +L31_1() { new a[13]; a[0] = L32_0() + L32_1() + L32_2() + L32_3(); return a[0]; } +L31_2() { new a[3]; a[0] = L32_0() + L32_1() + L32_2() + L32_3(); return a[0]; } +L31_3() { new a[6]; a[0] = L32_0() + L32_1() + L32_2() + L32_3(); return a[0]; } +L32_0() { new a[4]; a[0] = L33_0() + L33_1() + L33_2() + L33_3(); return a[0]; } +L32_1() { new a[7]; a[0] = L33_0() + L33_1() + L33_2() + L33_3(); return a[0]; } +L32_2() { new a[10]; a[0] = L33_0() + L33_1() + L33_2() + L33_3(); return a[0]; } +L32_3() { new a[13]; a[0] = L33_0() + L33_1() + L33_2() + L33_3(); return a[0]; } +L33_0() { new a[11]; a[0] = L34_0() + L34_1() + L34_2() + L34_3(); return a[0]; } +L33_1() { new a[1]; a[0] = L34_0() + L34_1() + L34_2() + L34_3(); return a[0]; } +L33_2() { new a[4]; a[0] = L34_0() + L34_1() + L34_2() + L34_3(); return a[0]; } +L33_3() { new a[7]; a[0] = L34_0() + L34_1() + L34_2() + L34_3(); return a[0]; } +L34_0() { new a[5]; a[0] = L35_0() + L35_1() + L35_2() + L35_3(); return a[0]; } +L34_1() { new a[8]; a[0] = L35_0() + L35_1() + L35_2() + L35_3(); return a[0]; } +L34_2() { new a[11]; a[0] = L35_0() + L35_1() + L35_2() + L35_3(); return a[0]; } +L34_3() { new a[1]; a[0] = L35_0() + L35_1() + L35_2() + L35_3(); return a[0]; } +L35_0() { new a[12]; a[0] = L36_0() + L36_1() + L36_2() + L36_3(); return a[0]; } +L35_1() { new a[2]; a[0] = L36_0() + L36_1() + L36_2() + L36_3(); return a[0]; } +L35_2() { new a[5]; a[0] = L36_0() + L36_1() + L36_2() + L36_3(); return a[0]; } +L35_3() { new a[8]; a[0] = L36_0() + L36_1() + L36_2() + L36_3(); return a[0]; } +L36_0() { new a[6]; a[0] = L37_0() + L37_1() + L37_2() + L37_3(); return a[0]; } +L36_1() { new a[9]; a[0] = L37_0() + L37_1() + L37_2() + L37_3(); return a[0]; } +L36_2() { new a[12]; a[0] = L37_0() + L37_1() + L37_2() + L37_3(); return a[0]; } +L36_3() { new a[2]; a[0] = L37_0() + L37_1() + L37_2() + L37_3(); return a[0]; } +L37_0() { new a[13]; a[0] = L38_0() + L38_1() + L38_2() + L38_3(); return a[0]; } +L37_1() { new a[3]; a[0] = L38_0() + L38_1() + L38_2() + L38_3(); return a[0]; } +L37_2() { new a[6]; a[0] = L38_0() + L38_1() + L38_2() + L38_3(); return a[0]; } +L37_3() { new a[9]; a[0] = L38_0() + L38_1() + L38_2() + L38_3(); return a[0]; } +L38_0() { new a[7]; a[0] = L39_0() + L39_1() + L39_2() + L39_3(); return a[0]; } +L38_1() { new a[10]; a[0] = L39_0() + L39_1() + L39_2() + L39_3(); return a[0]; } +L38_2() { new a[13]; a[0] = L39_0() + L39_1() + L39_2() + L39_3(); return a[0]; } +L38_3() { new a[3]; a[0] = L39_0() + L39_1() + L39_2() + L39_3(); return a[0]; } +L39_0() { new a[1]; a[0] = 1; return a[0]; } +L39_1() { new a[4]; a[0] = 1; return a[0]; } +L39_2() { new a[7]; a[0] = 1; return a[0]; } +L39_3() { new a[10]; a[0] = 1; return a[0]; } + +main() +{ + L0_0(); + L0_1(); + L0_2(); + L0_3(); +}