Created
August 7, 2016 07:44
-
-
Save llibra/378becdcce22e7dd98dd54e59a60180a to your computer and use it in GitHub Desktop.
TCO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fact1(n: Int) -> Int { | |
if n == 1 { | |
return 1 | |
} else { | |
return n * fact1(n - 1) | |
} | |
} | |
func fact2(n: Int, _ acc: Int) -> Int { | |
if n == 1 { | |
return acc | |
} else { | |
return fact2(n - 1, acc * n) | |
} | |
} | |
print(fact1(5)) | |
print(fact2(5, 1)) | |
/* | |
swiftc -O -S tco.swift | |
__TF3tco5fact1FSiSi: | |
pushq %rbp | |
movq %rsp, %rbp | |
pushq %rbx | |
pushq %rax | |
movq %rdi, %rbx | |
movl $1, %eax | |
cmpq $1, %rbx | |
je LBB1_3 | |
movq %rbx, %rdi | |
decq %rdi | |
jo LBB1_4 | |
callq __TF3tco5fact1FSiSi | |
imulq %rax, %rbx | |
movq %rbx, %rax | |
jo LBB1_4 | |
LBB1_3: | |
addq $8, %rsp | |
popq %rbx | |
popq %rbp | |
retq | |
LBB1_4: | |
ud2 | |
.private_extern __TF3tco5fact2FTSiSi_Si | |
.globl __TF3tco5fact2FTSiSi_Si | |
.align 4, 0x90 | |
__TF3tco5fact2FTSiSi_Si: | |
pushq %rbp | |
movq %rsp, %rbp | |
cmpq $1, %rdi | |
je LBB2_4 | |
.align 4, 0x90 | |
LBB2_1: | |
movq %rdi, %rax | |
decq %rax | |
jo LBB2_5 | |
imulq %rdi, %rsi | |
jo LBB2_5 | |
cmpq $1, %rax | |
movq %rax, %rdi | |
jne LBB2_1 | |
LBB2_4: | |
movq %rsi, %rax | |
popq %rbp | |
retq | |
LBB2_5: | |
ud2 | |
.private_extern __TMLGCs23_ContiguousArrayStorageP__ | |
.section __DATA,__datacoal_nt,coalesced | |
.globl __TMLGCs23_ContiguousArrayStorageP__ | |
.weak_definition __TMLGCs23_ContiguousArrayStorageP__ | |
.align 3 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment