You are viewing a single comment's thread from:

RE: Compiler Explorer: a Rosetta Stone for Compilers

in #programming7 years ago

The Haskell compiler is smart enough to turn tail recursion into a loop, too, although it is less easy to see what's going on:

factIter :: Int -> Int -> Int
factIter 0 x = x
factIter n x = factIter (n-1) (n * x)

The core of the function is these lines of assembly code:

Example_$wfactIter_info:
.Lc2gC:
  testq %r14,%r14
  je .Lc2gI
  movq %r14,%rax
  imulq %rsi,%rax
  decq %r14
  movq %rax,%rsi
  jmp .Lc2gC
.Lc2gI:
  movq %rsi,%rbx
  jmp *(%rbp)

The variable n appears in %r14, and the variable x in %rsi, I think. (Stupid AT&T syntax.)

Coin Marketplace

STEEM 0.13
TRX 0.35
JST 0.034
BTC 115061.49
ETH 4517.11
SBD 0.86