davidgiven / ack

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

i386: wrong rule for cmi 4 without txx/zxx #207

Open kernigh opened 5 years ago

kernigh commented 5 years ago

In mach/i386/ncg/table, the EM patterns for cmi 4 and cmu 4 have wrong rules. They reverse the comparison, turning a < b into a > b. This is almost never a problem, because most code uses cmi txx or cmi zxx, and those patterns have correct rules.

One way to use the wrong rule is when ack -Ropt-n disables peephole optimization. The next example shows the problem.

#include <stdio.h>

int g = 1;

int main(void) {
    if (g < 2) {
        puts("good: 1 < 2");
        return 0;
    } else {
        puts("bad: !(1 < 2)");
        return 1;
    }
}
$ ack -mlinux386 -o try try.c
$ ./try
good: 1 < 2
$ ack -mlinux386 -Ropt-n -o try try.c
$ ./try
bad: !(1 < 2)

The EM code from ack -c.e is loe g loc 2 cmi 4 zlt *3 bra *4 to branch 3 if g < 2, else branch 4. The opt pattern cmi zlt $1==w: blt $2 would handle this, but -Ropt-n disables all opt patterns and exposes the wrong cmi rule.

I suspect that I can easily write a test case in EM and fix the rules, but I haven't done so. I also don't know if cmi, cmu are broken in other machines.