jserv / amacc

Small C Compiler generating ELF executable Arm architecture, supporting JIT execution
Other
1.01k stars 160 forks source link

Modulo (operator%) does not give correct result #61

Closed silverscampi closed 4 years ago

silverscampi commented 4 years ago

I noticed in a larger program that I compiled JIT with amacc, which relies on modulo for prng, that the results were wrong. So I made a small test program, and tried a freshly cloned amacc on it - as you can see below the results are completely wrong.

(Here is a hacky fix for now, which requires the use of mod(a, b) instead of a % b - it gives the correct results for the shown small test program)

$ git clone git@github.com:jserv/amacc.git
Cloning into 'amacc'...
remote: Enumerating objects: 71, done.
remote: Counting objects: 100% (71/71), done.
remote: Compressing objects: 100% (51/51), done.
remote: Total 734 (delta 33), reused 52 (delta 19), pack-reused 663
Receiving objects: 100% (734/734), 297.06 KiB | 1.18 MiB/s, done.
Resolving deltas: 100% (411/411), done.
$ cd amacc/
$ make
  CC+LD     amacc
  CC+LD     amacc-native
amacc.c:2107:5: warning: return type of ‘main’ is not ‘int’ [-Wmain]
 int main(int argc, char **argv)
     ^~~~
amacc.c:2107:5: warning: first argument of ‘main’ should be ‘int’ [-Wmain]
$ ./amacc ~/tmp/modtest.c 
39628
1015073
3
2
$ more ~/tmp/modtest.c 
int main() {
    int a = 2131119850;
    int b = 53777;
    printf("%d\n", (a % b));

    int c = 841495917;
    int d = 829;
    printf("%d\n", (c % d));

    int e = 100;
    int f = 26;
    printf("%d\n", e % f);

    int g = 2;
    int h = 1;
    printf("%d\n", g % h);

    return 0;
}
jserv commented 4 years ago

@lecopzer, can you help verify and resolve EABI specific modulo operation?

lecopzer commented 4 years ago

OK, I need some time to figure out this issue.