mewmew / uc

A compiler for the µC language.
57 stars 5 forks source link

irgen: Use of uninitialized local variables #65

Closed mewmew closed 8 years ago

mewmew commented 8 years ago

The use of uninitialized local variables is undefined in C. The purpose of this issue is to discuss the advantages and disadvantages of implementation deterministic vs. non-deterministic behaviour for uninitialized variables.

Deterministic behaviour

For sake of discussion, lets assume that the deterministic behaviour would be defined such that uninitialized local variables are always zero-initialized.

Non-deterministic behaviour

Furthermore, for the sake of discussion, lets assume that one (of potentially many) non-deterministic behaviours would be defined such that uninitialized local variables have the value of the previous contents stored at the same memory location of the stack frame as the local variable.

Examples

Example input C source file:

void f() {
    int x;   // Uninitialized variable
    if (x) { // Use of uninitialized variable.
        ;
    }
}

LLVM IR output from Clang:

define void @f() {
0:
    %x = alloca i32            ; uninitialized variable
    %1 = load i32, i32* %x
    %2 = icmp ne i32 %1, 0     ; use of uninitialized variable
    br i1 %2, label %3, label %4
3:
    br label %4
4:
    ret void
}
mewmew commented 8 years ago

Non-deterministic behaviour

Pros

Cons

Deterministic behaviour

Pros

Cons

mewmew commented 8 years ago

Marked as a future ambition. For now, lets do what Clang does. In the future, we may continue the discussion and decide which approach feels closest to heart.