Computed gotos on Reddit
bearophile
bearophileHUGS at lycos.com
Mon Jul 23 14:33:41 PDT 2012
> Do you have a proof? (Show me asm code)
Just to be more clear, what I mean is that given a D program
equivalent to the C code shown in the article:
int interp_cgoto(unsigned char* code, int initval) {
static const void* dispatch_table[] = {
&&do_halt, &&do_inc, &&do_dec, &&do_mul2,
&&do_div2, &&do_add7, &&do_neg};
#define DISPATCH() goto *dispatch_table[code[pc++]]
int pc = 0;
int val = initval;
DISPATCH();
do_halt:
return val;
do_inc:
val++;
DISPATCH();
do_dec:
val--;
DISPATCH();
do_mul2:
val *= 2;
DISPATCH();
do_div2:
val /= 2;
DISPATCH();
do_add7:
val += 7;
DISPATCH();
do_neg:
val = -val;
DISPATCH();
}
int main() {return 0;}
Is a 32 bit D compiler producing asm (with performance) similar
to:
_interp_cgoto:
movl 4(%esp), %edx
movzbl (%edx), %eax
addl $1, %edx
movl _dispatch_table.1363(,%eax,4), %ecx
movl 8(%esp), %eax
jmp *%ecx
.p2align 4,,7
L3:
rep
ret
.p2align 4,,7
L4:
movzbl (%edx), %ecx
addl $1, %eax
movl _dispatch_table.1363(,%ecx,4), %ecx
.p2align 4,,7
L5:
addl $1, %edx
jmp *%ecx
.p2align 4,,7
L6:
movzbl (%edx), %ecx
subl $1, %eax
movl _dispatch_table.1363(,%ecx,4), %ecx
addl $1, %edx
jmp *%ecx
.p2align 4,,7
L7:
movzbl (%edx), %ecx
addl %eax, %eax
movl _dispatch_table.1363(,%ecx,4), %ecx
addl $1, %edx
jmp *%ecx
.p2align 4,,7
L8:
movl %eax, %ecx
shrl $31, %ecx
addl %ecx, %eax
movzbl (%edx), %ecx
sarl %eax
movl _dispatch_table.1363(,%ecx,4), %ecx
addl $1, %edx
jmp *%ecx
.p2align 4,,7
L9:
movzbl (%edx), %ecx
addl $7, %eax
movl _dispatch_table.1363(,%ecx,4), %ecx
addl $1, %edx
jmp *%ecx
.p2align 4,,7
L10:
movzbl (%edx), %ecx
negl %eax
movl _dispatch_table.1363(,%ecx,4), %ecx
addl $1, %edx
jmp *%ecx
.section .rdata,"dr"
.align 4
_dispatch_table.1363:
.long L3
.long L4
.long L6
.long L7
.long L8
.long L9
.long L10
Bye,
bearophile
More information about the Digitalmars-d
mailing list