davidgiven / ack

The Amsterdam Compiler Kit
http://tack.sf.net
Other
420 stars 59 forks source link

Incorrect i86 code for `switch` statement with long operand #240

Closed tkchia closed 2 years ago

tkchia commented 2 years ago

Currently, if I write an i86 program which uses a switch statement with a long operand, the resulting program may crash:

$ cat test5.c
#include <stdlib.h>

int main(void)
{
    switch (abs(0))
    {
        case 0:
        case 0x800:
        break;
        default:
        abort();
    }
    switch (labs(0))
    {
        case 0:
        case 0x800000:
        return 0;
        default:
        abort();
    }
}
$ ack-cc -mmsdos86 -O6 -S test5.c -o test5.exe
$ dosemu -dumb -K . -E test5.exe
...
Invalid Opcode at 3A93 019C 3096 04F6 04FA 091E 0161 0001 04F6 04FA 5355 5245 5244
...

It seems that the internal compiler support function .csb4 — which handles such switch statements — is either implemented wrongly, or used wrongly from the compiled code.

__II1:
.data2  I1_3
.data4  2
.data4  0
.data2  I1_4
.data4  8388608
.data2  I1_4
.sect .text
...
I1_2:
xor ax,ax
push ax
push ax
call _labs
pop bx
pop bx
mov bx,__II1
jmp .csb4

Thank you!