Class notes for 5/5 Course evaluations Computing maximum stack depth (per method) Assuming that frames grow in a stack from left to right, you place the stack at the right end of the frame. Then by allowing frames to overlap, function argument may be passed by pushing them onto the stack then calling the function. so how deep does the stack go? insights 1) calls don't bother us, their stack is charge to them. 2) every statement leaves the depth exactly the same as before the statement was called --->depth a = b + c * d; --->depth depth is defined to be = to depth this must be true, if statements left the stack deeper then a program would always be using more memory until we ran out. If statements left the stack shallower, then eventually we would run out of stack (underflow). So we only need to figure out the maximum stack depth for any statement in a function to find what the function needs. we could add the function int stackneeded() in all ast nodes depth intlitnode = 1 namenode -> nonsubscripted = 1 subscripted = 1 + expr.stackneeded(); a[b[c[x]]] \__/ 1 1 2 = 4 expr1 op expr 2 = max(expr1.stackneeded(),(expr2.stackneeded() + 1)) the plus one is for the space used by the result of expr1 a + b + c + + + z = 2 \_2_/ 1 1 a + (b + (c + ( ... (y + z))...)) = 25 name = expr 1) name not subscripted expr.stackneeded(); 2) name is subscripted max((1+subscript.stackneeded),(2+expr.stackneeded())) if(expr) stmt1 else stmt2 = max(expr.stackneeded(), stmt1.stackneeded(), stmt2.stackneeded()) function calls, procedure calls, and read and write subr(a,b,c); callnode | \ ID ARGS / \ argnode moreargs expr = max(expr.stackneeded(), 1 + moreargs.stackneeded()) F(a + b, 1, c + d) uses 4 |_2_| |_2_| |__3___| |_____4_______| a(b(c(1,2,3))) code ldc 1 ldc 2 ldc 3 call c call b call a COMPUTING BOOLEAN VALUES jump code evaluates boolean not by putting code on the stack jump code true label false label if(stmt) {eval condition} if eq else statement {then stmts} goto exit elselab {else stmts} exit or a < b push a push b IF_ICOMPLT truelab goto false convert to stack code ( string truelabel string falselabel ){ TRUE: ldc 1 goto skip; FALSE: LDC 0 SKIP short circuiting A||B A&&B ^ ^ | | if true if false b not evaluated now in jump code first pass non optimized A&&B push a ifeq F1 goto T1 push b ifeq F2 goto T2 push c ifeq F3 goto T3 insights with short-circuit evaluation, if a is true go directly to b, and both F1 and F2 are the same, goto c push a ifeq F1 push b ifeq F2 goto T2 F1/F2 push c ifeq F3 goto T3 next notice that if B then c can be skipped and that T2 and T3 are the same push a ifeq F1 push b ifne T2 F1 push c ifne T3 //false stuff here goto END T2/T3 //true stuff END this drops us from 6 operations all the time to an average to 4.5 operations notes taken by ross dickson