class: center, middle # LLVM Intermediate Representation _CMPU 331 - Compilers_ --- # Global Variables Global variables are marked with the `@` character. ``` @variable = global i32 21 ``` Equivalent C code: ``` int variable = 21; ``` --- # Functions Functions are also global symbols. ``` define i32 @main() { ... ret i32 0 } ``` Equivalent C code: ``` int main() { ... return 0; } ``` --- # Local Variables Local variables (inside functions) are marked with the `%` character. ``` %x = add i32 %y, 2 ``` Equivalent C code: ``` x = y + 2 ``` --- # Local Variables Local variables (inside functions) are marked with the `%` character. ``` %variable = alloca i32 store i32 21, i32* %variable ``` Equivalent C code: ``` int variable = 21; ``` --- # Control Flow: If-Then-Else Control flow uses the `br` instruction, to branch to a label (transfers control flow to the point in the program marked by a label). ``` entry: %0 = icmp sgt i32 %a, %b br i1 %0, label %btrue, label %bfalse btrue: ; do something when true br label %end bfalse: ; do something when false br label %end end: ``` Equivalent C code: ``` if (a > b) { /* do something when true */ } else { /* do something when false */ } ``` --- # Control Flow: Loop Control flow uses the `br` instruction, to branch to a label (transfers control flow to the point in the program marked by a label). ``` loop: %1 = icmp sgt i32 %x, 0 br i1 %1, label %body, label %end body: ; do something repeatedly br label %loop end: ``` Equivalent C code: ``` while (x > 0) { /* do something repeatedly */ } ```