[gambit-list] compiling fib

Bradley Lucier lucier at math.purdue.edu
Thu Jan 20 21:15:53 EST 2011


Marc:

I don't know why, but I got looking at the compiled code for fib, and it
seems to use much more stack space than needed, 8 words per invocation,
which seems counter-intuitive to me; is there a problem with the
compiler?  I'm using the latest git compiler.

Brad

I started with

(declare (standard-bindings)
	 (extended-bindings)
	 (block)
	 (not safe))

(define (fib n)
  (if (or (fx= n 0)
	  (fx= n 1))
      n
      (fx+ (fib (fx- n 1))
	   (fib (fx- n 2)))))

and got

#undef ___PH_PROC
#define ___PH_PROC ___H_fib
#undef ___PH_LBL0
#define ___PH_LBL0 3
#undef ___PD_ALL
#define ___PD_ALL ___D_FP ___D_R0 ___D_R1
#undef ___PR_ALL
#define ___PR_ALL ___R_FP ___R_R0 ___R_R1
#undef ___PW_ALL
#define ___PW_ALL ___W_FP ___W_R0 ___W_R1
___BEGIN_P_COD
___BEGIN_P_HLBL
___DEF_P_HLBL_INTRO
___DEF_P_HLBL(___L0_fib)
___DEF_P_HLBL(___L1_fib)
___DEF_P_HLBL(___L2_fib)
___DEF_P_HLBL(___L3_fib)
___DEF_P_HLBL(___L4_fib)
___END_P_HLBL
___BEGIN_P_SW
___DEF_SLBL(0,___L0_fib)
   ___IF_NARGS_EQ(1,___NOTHING)
   ___WRONG_NARGS(0,1,0,0)
___DEF_GLBL(___L_fib)
   ___IF(___FIXEQ(___R1,___FIX(0L)))
   ___GOTO(___L7_fib)
   ___END_IF
   ___GOTO(___L6_fib)
___DEF_GLBL(___L5_fib)
   ___SET_STK(1,___R0)
   ___SET_STK(2,___R1)
   ___SET_R1(___FIXSUB(___R1,___FIX(1L)))
   ___SET_R0(___LBL(2))
   ___ADJFP(8)
   ___POLL(1)
___DEF_SLBL(1,___L1_fib)
   ___IF(___FIXEQ(___R1,___FIX(0L)))
   ___GOTO(___L7_fib)
   ___END_IF
___DEF_GLBL(___L6_fib)
   ___IF(___NOT(___FIXEQ(___R1,___FIX(1L))))
   ___GOTO(___L5_fib)
   ___END_IF
___DEF_GLBL(___L7_fib)
   ___JUMPPRM(___NOTHING,___R0)
___DEF_SLBL(2,___L2_fib)
   ___SET_STK(-5,___R1)
   ___SET_R1(___FIXSUB(___STK(-6),___FIX(2L)))
   ___SET_R0(___LBL(3))
   ___IF(___FIXEQ(___R1,___FIX(0L)))
   ___GOTO(___L7_fib)
   ___END_IF
   ___GOTO(___L6_fib)
___DEF_SLBL(3,___L3_fib)
   ___SET_R1(___FIXADD(___STK(-5),___R1))
   ___POLL(4)
___DEF_SLBL(4,___L4_fib)
   ___ADJFP(-8)
   ___JUMPPRM(___NOTHING,___STK(1))
___END_P_SW
___END_P_COD





More information about the Gambit-list mailing list