Created
November 7, 2014 21:16
-
-
Save mcfedr/63ad08370d856bad3694 to your computer and use it in GitHub Desktop.
Example of C compiler tail recursion
This file contains hidden or 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
int isEven(int i) { | |
if (i == 0) { | |
return 1; | |
} | |
else if (i == 1) { | |
return 0; | |
} | |
else if (i < 0) { | |
return isEven(i + 2); | |
} | |
else { | |
return isEven(i - 2); | |
} | |
} |
This file contains hidden or 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
.section __TEXT,__text,regular,pure_instructions | |
.globl _isEven | |
.align 4, 0x90 | |
_isEven: ## @isEven | |
.cfi_startproc | |
## BB#0: | |
pushq %rbp | |
Ltmp2: | |
.cfi_def_cfa_offset 16 | |
Ltmp3: | |
.cfi_offset %rbp, -16 | |
movq %rsp, %rbp | |
Ltmp4: | |
.cfi_def_cfa_register %rbp | |
## kill: EDI<def> EDI<kill> RDI<def> | |
jmp LBB0_1 | |
.align 4, 0x90 | |
LBB0_3: ## in Loop: Header=BB0_1 Depth=1 | |
leal 2(%rdi), %eax | |
testl %edi, %edi | |
leal -2(%rdi), %ecx | |
cmovsl %eax, %ecx | |
movl %ecx, %edi | |
LBB0_1: ## %tailrecurse | |
## =>This Inner Loop Header: Depth=1 | |
testl %edi, %edi | |
je LBB0_4 | |
## BB#2: ## %tailrecurse | |
## in Loop: Header=BB0_1 Depth=1 | |
xorl %eax, %eax | |
cmpl $1, %edi | |
jne LBB0_3 | |
jmp LBB0_5 | |
LBB0_4: ## %.loopexit.loopexit | |
movl $1, %eax | |
LBB0_5: ## %.loopexit | |
popq %rbp | |
retq | |
.cfi_endproc | |
.subsections_via_symbols |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment