Adding parameter assignment in __stackcall procedure pow2::n = param(pow2::n)
Eliminating unused variable with no statement pow2::$2
Calling convention __stackcall adding prepare/execute/finalize for main::$0 = call pow2(6)
Calling convention __stackcall adding prepare/execute/finalize for pow2::c = call pow2(pow2::$1)
Calling convention STACK_CALL replacing param(pow2::n) with stackidx(char,pow2::OFFSET_STACK_N)
Calling convention STACK_CALL adding stack return stackidx(char,pow2::OFFSET_STACK_RETURN_0) = pow2::return
Calling convention STACK_CALL adding stack pull main::$0 = stackpull(char)
Calling convention STACK_CALL adding stack pull pow2::c = stackpull(char)
Calling convention STACK_CALL adding stack push stackpush(char) = 6
Calling convention STACK_CALL adding stack push stackpush(char) = pow2::$1

CONTROL FLOW GRAPH SSA

void main()
main: scope:[main]  from __start
  stackpush(char) = 6
  callexecute pow2 
  main::$0 = stackpull(char)
  *SCREEN = main::$0
  to:main::@return
main::@return: scope:[main]  from main
  return 
  to:@return

__stackcall char pow2(char n)
pow2: scope:[pow2]  from
  pow2::n#0 = stackidx(char,pow2::OFFSET_STACK_N)
  pow2::$0 = pow2::n#0 == 0
  if(pow2::$0) goto pow2::@1
  to:pow2::@2
pow2::@1: scope:[pow2]  from pow2
  pow2::return#0 = 1
  to:pow2::@return
pow2::@2: scope:[pow2]  from pow2
  pow2::n#1 = phi( pow2/pow2::n#0 )
  pow2::$1 = pow2::n#1 - 1
  stackpush(char) = pow2::$1
  callexecute pow2 
  pow2::c#0 = stackpull(char)
  pow2::$3 = pow2::c#0 + pow2::c#0
  pow2::return#1 = pow2::$3
  to:pow2::@return
pow2::@return: scope:[pow2]  from pow2::@1 pow2::@2
  pow2::return#2 = phi( pow2::@1/pow2::return#0, pow2::@2/pow2::return#1 )
  stackidx(char,pow2::OFFSET_STACK_RETURN_0) = pow2::return#2
  return 
  to:@return

void __start()
__start: scope:[__start]  from
  call main
  to:__start::@1
__start::@1: scope:[__start]  from __start
  to:__start::@return
__start::@return: scope:[__start]  from __start::@1
  return 
  to:@return

SYMBOL TABLE SSA
__constant char * const SCREEN = (char *)$400
__constant unsigned int STACK_BASE = $103
void __start()
void main()
char main::$0
__stackcall char pow2(char n)
bool pow2::$0
number pow2::$1
char pow2::$3
__constant char pow2::OFFSET_STACK_N = 0
__constant char pow2::OFFSET_STACK_RETURN_0 = 0
char pow2::c
char pow2::c#0
char pow2::n
char pow2::n#0
char pow2::n#1
char pow2::return
char pow2::return#0
char pow2::return#1
char pow2::return#2

Adding number conversion cast (unumber) 6 in stackpush(char) = 6
Adding number conversion cast (unumber) 0 in pow2::$0 = pow2::n#0 == 0
Adding number conversion cast (unumber) 1 in pow2::return#0 = 1
Adding number conversion cast (unumber) 1 in pow2::$1 = pow2::n#1 - 1
Adding number conversion cast (unumber) pow2::$1 in pow2::$1 = pow2::n#1 - (unumber)1
Successful SSA optimization PassNAddNumberTypeConversions
Inlining cast stackpush(char) = (unumber)6
Inlining cast pow2::return#0 = (unumber)1
Successful SSA optimization Pass2InlineCast
Simplifying constant pointer cast (char *) 1024
Simplifying constant integer cast 6
Simplifying constant integer cast 0
Simplifying constant integer cast 1
Simplifying constant integer cast 1
Successful SSA optimization PassNCastSimplification
Finalized unsigned number type (char) 6
Finalized unsigned number type (char) 0
Finalized unsigned number type (char) 1
Finalized unsigned number type (char) 1
Successful SSA optimization PassNFinalizeNumberTypeConversions
Inferred type updated to char in pow2::$1 = pow2::n#1 - 1
Alias pow2::n#0 = pow2::n#1 
Alias pow2::return#1 = pow2::$3 
Successful SSA optimization Pass2AliasElimination
Simple Condition pow2::$0 [7] if(pow2::n#0==0) goto pow2::@1
Successful SSA optimization Pass2ConditionalJumpSimplification
Constant pow2::return#0 = 1
Successful SSA optimization Pass2ConstantIdentification
Removing unused procedure __start
Removing unused procedure block __start
Removing unused procedure block __start::@1
Removing unused procedure block __start::@return
Successful SSA optimization PassNEliminateEmptyStart
Inlining constant with var siblings pow2::return#0
Constant inlined pow2::return#0 = 1
Successful SSA optimization Pass2ConstantInlining
Adding NOP phi() at start of pow2::@1
CALL GRAPH
Calls in [pow2] to pow2:4 
Calls in [main] to pow2:13 

Created 1 initial phi equivalence classes
Coalesced [7] pow2::return#3 = pow2::return#1
Coalesced down to 1 phi equivalence classes
Culled Empty Block label pow2::@1
Renumbering block pow2::@2 to pow2::@1

FINAL CONTROL FLOW GRAPH

__stackcall char pow2(char n)
pow2: scope:[pow2]  from
  [0] pow2::n#0 = stackidx(char,pow2::OFFSET_STACK_N)
  [1] if(pow2::n#0==0) goto pow2::@return
  to:pow2::@1
pow2::@1: scope:[pow2]  from pow2
  [2] pow2::$1 = pow2::n#0 - 1
  [3] stackpush(char) = pow2::$1
  [4] callexecute pow2 
  [5] pow2::c#0 = stackpull(char)
  [6] pow2::return#1 = pow2::c#0 + pow2::c#0
  to:pow2::@return
pow2::@return: scope:[pow2]  from pow2 pow2::@1
  [7] pow2::return#2 = phi( pow2/1, pow2::@1/pow2::return#1 )
  [8] stackidx(char,pow2::OFFSET_STACK_RETURN_0) = pow2::return#2
  [9] return 
  to:@return

void main()
main: scope:[main]  from
  [10] stackpush(char) = 6
  [11] callexecute pow2 
  [12] main::$0 = stackpull(char)
  [13] *SCREEN = main::$0
  to:main::@return
main::@return: scope:[main]  from main
  [14] return 
  to:@return


VARIABLE REGISTER WEIGHTS
void main()
char main::$0 // 4.0
__stackcall char pow2(char n)
char pow2::$1 // 22.0
char pow2::c
char pow2::c#0 // 33.0
char pow2::n
char pow2::n#0 // 16.5
char pow2::return
char pow2::return#1 // 22.0
char pow2::return#2 // 22.0

Initial phi equivalence classes
[ pow2::return#2 pow2::return#1 ]
Added variable pow2::n#0 to live range equivalence class [ pow2::n#0 ]
Added variable pow2::$1 to live range equivalence class [ pow2::$1 ]
Added variable pow2::c#0 to live range equivalence class [ pow2::c#0 ]
Added variable main::$0 to live range equivalence class [ main::$0 ]
Complete equivalence classes
[ pow2::return#2 pow2::return#1 ]
[ pow2::n#0 ]
[ pow2::$1 ]
[ pow2::c#0 ]
[ main::$0 ]
Allocated zp[1]:2 [ pow2::return#2 pow2::return#1 ]
Allocated zp[1]:3 [ pow2::c#0 ]
Allocated zp[1]:4 [ pow2::$1 ]
Allocated zp[1]:5 [ pow2::n#0 ]
Allocated zp[1]:6 [ main::$0 ]
REGISTER UPLIFT POTENTIAL REGISTERS
Statement [0] pow2::n#0 = stackidx(char,pow2::OFFSET_STACK_N) [ pow2::n#0 ] ( pow2:11 [ pow2::n#0 ] { }  ) always clobbers reg byte a reg byte x 
Statement [5] pow2::c#0 = stackpull(char) [ pow2::c#0 ] ( pow2:11 [ pow2::c#0 ] { }  ) always clobbers reg byte a 
Statement [6] pow2::return#1 = pow2::c#0 + pow2::c#0 [ pow2::return#1 ] ( pow2:11 [ pow2::return#1 ] { }  ) always clobbers reg byte a 
Statement [8] stackidx(char,pow2::OFFSET_STACK_RETURN_0) = pow2::return#2 [ ] ( pow2:11 [ ] { }  ) always clobbers reg byte x 
Statement [10] stackpush(char) = 6 [ ] (  [ ] { }  ) always clobbers reg byte a 
Statement [12] main::$0 = stackpull(char) [ main::$0 ] (  [ main::$0 ] { }  ) always clobbers reg byte a 
Potential registers zp[1]:2 [ pow2::return#2 pow2::return#1 ] : zp[1]:2 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:5 [ pow2::n#0 ] : zp[1]:5 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:4 [ pow2::$1 ] : zp[1]:4 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:3 [ pow2::c#0 ] : zp[1]:3 , reg byte a , reg byte x , reg byte y , 
Potential registers zp[1]:6 [ main::$0 ] : zp[1]:6 , reg byte a , reg byte x , reg byte y , 

REGISTER UPLIFT SCOPES
Uplift Scope [pow2] 44: zp[1]:2 [ pow2::return#2 pow2::return#1 ] 33: zp[1]:3 [ pow2::c#0 ] 22: zp[1]:4 [ pow2::$1 ] 16.5: zp[1]:5 [ pow2::n#0 ] 
Uplift Scope [main] 4: zp[1]:6 [ main::$0 ] 
Uplift Scope [] 

Uplifting [pow2] best 87 combination reg byte a [ pow2::return#2 pow2::return#1 ] reg byte a [ pow2::c#0 ] reg byte a [ pow2::$1 ] reg byte a [ pow2::n#0 ] 
Limited combination testing to 100 combinations of 256 possible.
Uplifting [main] best 81 combination reg byte a [ main::$0 ] 
Uplifting [] best 81 combination 

ASSEMBLER BEFORE OPTIMIZATION
  // File Comments
// Test a procedure with calling convention stack
// Recursion that works (no local variables)
  // Upstart
  // Commodore 64 PRG executable file
.file [name="procedure-callingconvention-stack-13.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
  // Global Constants & labels
  .const STACK_BASE = $103
  .label SCREEN = $400
.segment Code
  // pow2
// __register(A) char pow2(__register(A) char n)
pow2: {
    .const OFFSET_STACK_N = 0
    .const OFFSET_STACK_RETURN_0 = 0
    // [0] pow2::n#0 = stackidx(char,pow2::OFFSET_STACK_N) -- vbuaa=_stackidxbyte_vbuc1 
    tsx
    lda STACK_BASE+OFFSET_STACK_N,x
    // [1] if(pow2::n#0==0) goto pow2::@return -- vbuaa_eq_0_then_la1 
    cmp #0
    beq __breturn_from_pow2
    jmp __b1
    // pow2::@1
  __b1:
    // [2] pow2::$1 = pow2::n#0 - 1 -- vbuaa=vbuaa_minus_1 
    sec
    sbc #1
    // [3] stackpush(char) = pow2::$1 -- _stackpushbyte_=vbuaa 
    pha
    // [4] callexecute pow2  -- call_vprc1 
    jsr pow2
    // [5] pow2::c#0 = stackpull(char) -- vbuaa=_stackpullbyte_ 
    pla
    // [6] pow2::return#1 = pow2::c#0 + pow2::c#0 -- vbuaa=vbuaa_plus_vbuaa 
    asl
    // [7] phi from pow2::@1 to pow2::@return [phi:pow2::@1->pow2::@return]
  __breturn_from___b1:
    // [7] phi pow2::return#2 = pow2::return#1 [phi:pow2::@1->pow2::@return#0] -- register_copy 
    jmp __breturn
    // [7] phi from pow2 to pow2::@return [phi:pow2->pow2::@return]
  __breturn_from_pow2:
    // [7] phi pow2::return#2 = 1 [phi:pow2->pow2::@return#0] -- vbuaa=vbuc1 
    lda #1
    jmp __breturn
    // pow2::@return
  __breturn:
    // [8] stackidx(char,pow2::OFFSET_STACK_RETURN_0) = pow2::return#2 -- _stackidxbyte_vbuc1=vbuaa 
    tsx
    sta STACK_BASE+OFFSET_STACK_RETURN_0,x
    // [9] return 
    rts
}
  // main
main: {
    // [10] stackpush(char) = 6 -- _stackpushbyte_=vbuc1 
    lda #6
    pha
    // [11] callexecute pow2  -- call_vprc1 
    jsr pow2
    // [12] main::$0 = stackpull(char) -- vbuaa=_stackpullbyte_ 
    pla
    // [13] *SCREEN = main::$0 -- _deref_pbuc1=vbuaa 
    sta SCREEN
    jmp __breturn
    // main::@return
  __breturn:
    // [14] return 
    rts
}
  // File Data

ASSEMBLER OPTIMIZATIONS
Removing instruction jmp __b1
Removing instruction jmp __breturn
Removing instruction jmp __breturn
Succesful ASM optimization Pass5NextJumpElimination
Removing instruction __b1:
Removing instruction __breturn_from___b1:
Removing instruction __breturn:
Succesful ASM optimization Pass5UnusedLabelElimination
Relabelling long label __breturn_from_pow2 to __b1
Succesful ASM optimization Pass5RelabelLongLabels

FINAL SYMBOL TABLE
__constant char * const SCREEN = (char *) 1024
__constant unsigned int STACK_BASE = $103
void main()
char main::$0 // reg byte a 4.0
__stackcall char pow2(char n)
char pow2::$1 // reg byte a 22.0
__constant char pow2::OFFSET_STACK_N = 0
__constant char pow2::OFFSET_STACK_RETURN_0 = 0
char pow2::c
char pow2::c#0 // reg byte a 33.0
char pow2::n
char pow2::n#0 // reg byte a 16.5
char pow2::return
char pow2::return#1 // reg byte a 22.0
char pow2::return#2 // reg byte a 22.0

reg byte a [ pow2::return#2 pow2::return#1 ]
reg byte a [ pow2::n#0 ]
reg byte a [ pow2::$1 ]
reg byte a [ pow2::c#0 ]
reg byte a [ main::$0 ]


FINAL ASSEMBLER
Score: 72

  // File Comments
// Test a procedure with calling convention stack
// Recursion that works (no local variables)
  // Upstart
  // Commodore 64 PRG executable file
.file [name="procedure-callingconvention-stack-13.prg", type="prg", segments="Program"]
.segmentdef Program [segments="Basic, Code, Data"]
.segmentdef Basic [start=$0801]
.segmentdef Code [start=$80d]
.segmentdef Data [startAfter="Code"]
.segment Basic
:BasicUpstart(main)
  // Global Constants & labels
  .const STACK_BASE = $103
  .label SCREEN = $400
.segment Code
  // pow2
// __register(A) char pow2(__register(A) char n)
pow2: {
    .const OFFSET_STACK_N = 0
    .const OFFSET_STACK_RETURN_0 = 0
    // [0] pow2::n#0 = stackidx(char,pow2::OFFSET_STACK_N) -- vbuaa=_stackidxbyte_vbuc1 
    tsx
    lda STACK_BASE+OFFSET_STACK_N,x
    // if (n == 0)
    // [1] if(pow2::n#0==0) goto pow2::@return -- vbuaa_eq_0_then_la1 
    cmp #0
    beq __b1
    // pow2::@1
    // n-1
    // [2] pow2::$1 = pow2::n#0 - 1 -- vbuaa=vbuaa_minus_1 
    sec
    sbc #1
    // char c = pow2(n-1)
    // [3] stackpush(char) = pow2::$1 -- _stackpushbyte_=vbuaa 
    pha
    // [4] callexecute pow2  -- call_vprc1 
    jsr pow2
    // [5] pow2::c#0 = stackpull(char) -- vbuaa=_stackpullbyte_ 
    pla
    // return c+c;
    // [6] pow2::return#1 = pow2::c#0 + pow2::c#0 -- vbuaa=vbuaa_plus_vbuaa 
    asl
    // [7] phi from pow2::@1 to pow2::@return [phi:pow2::@1->pow2::@return]
    // [7] phi pow2::return#2 = pow2::return#1 [phi:pow2::@1->pow2::@return#0] -- register_copy 
    jmp __breturn
    // [7] phi from pow2 to pow2::@return [phi:pow2->pow2::@return]
  __b1:
    // [7] phi pow2::return#2 = 1 [phi:pow2->pow2::@return#0] -- vbuaa=vbuc1 
    lda #1
    // pow2::@return
  __breturn:
    // }
    // [8] stackidx(char,pow2::OFFSET_STACK_RETURN_0) = pow2::return#2 -- _stackidxbyte_vbuc1=vbuaa 
    tsx
    sta STACK_BASE+OFFSET_STACK_RETURN_0,x
    // [9] return 
    rts
}
  // main
main: {
    // pow2(6)
    // [10] stackpush(char) = 6 -- _stackpushbyte_=vbuc1 
    lda #6
    pha
    // [11] callexecute pow2  -- call_vprc1 
    jsr pow2
    // [12] main::$0 = stackpull(char) -- vbuaa=_stackpullbyte_ 
    pla
    // *SCREEN = pow2(6)
    // [13] *SCREEN = main::$0 -- _deref_pbuc1=vbuaa 
    sta SCREEN
    // main::@return
    // }
    // [14] return 
    rts
}
  // File Data

