rui314 / 8cc

A Small C Compiler
MIT License
6.13k stars 740 forks source link

maybe bug in gen.c #48

Open xueyumusic opened 9 years ago

xueyumusic commented 9 years ago

In push_struct function in gen.c, there are three for loops, but it seems that only first for loop is executed, the last two for loops is not executed

int i = 0; for (; i < size; i += 8) { emit("movq %d(#rcx), #r11", i); emit("mov #r11, %d(#rsp)", i); } for (; i < size; i += 4) { emit("movl %d(#rcx), #r11", i); emit("movl #r11d, %d(#rsp)", i); } for (; i < size; i++) { emit("movb %d(#rcx), #r11", i); emit("movb #r11b, %d(#rsp)", i); }

andrewchambers commented 9 years ago

Good spotting, its intending to copy in chunks of 8, then chunks of 4 then finish of the remaining bytes. The logic is wrong, it should be (i + 8) < size, then (i + 4) < size. Maybe it should be rewritten to decrement a value so it is more clear.

*edit: The alignment requirements on structs and the stack may mean this does little harm.

andrewchambers commented 9 years ago

Same issue in emit_copy_struct