DontBelieveMe / helix

"Compiler Optimisation Techniques" - BSc Computer Science Final Year Project (Sheffield Hallam University)
2 stars 0 forks source link

Incorrect IR codegen for overflowing integer constants #1

Open DontBelieveMe opened 2 years ago

DontBelieveMe commented 2 years ago

Given this C code

int main() {
    unsigned char my_byte = 1000;
    return 0;
}

the following IR is generated

function main(): i32 {
.0:
        stack_alloc [i8 x 1], %0:ptr
        store 1000:i8, %0:ptr
        ret 0:i32
}

Of course you can't store 1000 in an i8, so this should wrap the integer around correctly 232.

(In fact clang actually warns about this, and gives this

C:\helix\.\testsuite\f.c:4:15: warning: implicit conversion from 'int' to 'u8' (aka 'unsigned char') changes value from
      1000 to 232 [-Wconstant-conversion]
        u8 my_byte = 1000;
           ~~~~~~~   ^~~~