Skip to content

Instantly share code, notes, and snippets.

@jiachen247
Created March 26, 2021 09:55
Show Gist options
  • Save jiachen247/75a7e2b268b18f3178f15722d1eb5fa0 to your computer and use it in GitHub Desktop.
Save jiachen247/75a7e2b268b18f3178f15722d1eb5fa0 to your computer and use it in GitHub Desktop.

// Source Program

const x = 1;
{
    const x = 2;
    if (x * 2 === 4) {
        display("Hello world!");
    } else {
         display("boo");
    }
}
//"Hello world!"

// LLVM IR

; ModuleID = 'module'
source_filename = "module"

%literal = type { double, double }

@format_number = private unnamed_addr constant [5 x i8] c"%lf\0A\00", align 1
@format_true = private unnamed_addr constant [6 x i8] c"true\0A\00", align 1
@format_false = private unnamed_addr constant [7 x i8] c"false\0A\00", align 1
@format_string = private unnamed_addr constant [6 x i8] c"\22%s\22\0A\00", align 1
@format_error = private unnamed_addr constant [13 x i8] c"error: \22%s\22\0A\00", align 1
@str = private unnamed_addr constant [13 x i8] c"Hello world!\00", align 1
@str.1 = private unnamed_addr constant [4 x i8] c"boo\00", align 1

declare i8* @malloc(i64)

declare i32 @printf(i8*, ...)

declare i8* @strcat(i8*, i8*)

define void @display(%literal* %0) {
entry:
  %1 = getelementptr inbounds %literal, %literal* %0, i32 0, i32 0
  %2 = getelementptr inbounds %literal, %literal* %0, i32 0, i32 1
  %3 = load double, double* %1
  %4 = load double, double* %2
  %5 = fcmp oeq double %3, 2.000000e+00
  br i1 %5, label %display_boolean, label %tmp

tmp:                                              ; preds = %entry
  %6 = fcmp oeq double %3, 3.000000e+00
  br i1 %6, label %display_string, label %display_number

display_number:                                   ; preds = %tmp
  %7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @format_number, i32 0, i32 0), double %4)
  br label %end

display_boolean:                                  ; preds = %entry
  %8 = fcmp oeq double %4, 1.000000e+00
  br i1 %8, label %print_true, label %print_false

print_true:                                       ; preds = %display_boolean
  %9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @format_true, i32 0, i32 0))
  br label %end

print_false:                                      ; preds = %display_boolean
  %10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @format_false, i32 0, i32 0))
  br label %end

display_string:                                   ; preds = %tmp
  %11 = bitcast double %4 to i64
  %12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @format_string, i32 0, i32 0), i64 %11)
  br label %end

end:                                              ; preds = %display_string, %print_false, %print_true, %display_number
  ret void
}

define void @main() {
hoist:
  br label %entry

entry:                                            ; preds = %hoist
  %e = call i8* @malloc(i64 8)
  %e1 = call i8* @malloc(i64 16)
  %0 = bitcast i8* %e1 to i8**
  store i8* %e, i8** %0
  %1 = call i8* @malloc(i64 16)
  %2 = bitcast i8* %1 to %literal*
  %3 = getelementptr inbounds %literal, %literal* %2, i32 0, i32 0
  %4 = getelementptr inbounds %literal, %literal* %2, i32 0, i32 1
  store double 1.000000e+00, double* %3
  store double 1.000000e+00, double* %4
  %5 = bitcast i8* %e1 to %literal**
  %6 = getelementptr inbounds %literal*, %literal** %5, i32 1
  store %literal* %2, %literal** %6
  %e2 = call i8* @malloc(i64 16)
  %7 = bitcast i8* %e2 to i8**
  store i8* %e1, i8** %7
  %8 = call i8* @malloc(i64 16)
  %9 = bitcast i8* %8 to %literal*
  %10 = getelementptr inbounds %literal, %literal* %9, i32 0, i32 0
  %11 = getelementptr inbounds %literal, %literal* %9, i32 0, i32 1
  store double 1.000000e+00, double* %10
  store double 2.000000e+00, double* %11
  %12 = bitcast i8* %e2 to %literal**
  %13 = getelementptr inbounds %literal*, %literal** %12, i32 1
  store %literal* %9, %literal** %13
  %14 = bitcast i8* %e2 to %literal**
  %15 = getelementptr inbounds %literal*, %literal** %14, i32 1
  %16 = load %literal*, %literal** %15
  %17 = call i8* @malloc(i64 16)
  %18 = bitcast i8* %17 to %literal*
  %19 = getelementptr inbounds %literal, %literal* %18, i32 0, i32 0
  %20 = getelementptr inbounds %literal, %literal* %18, i32 0, i32 1
  store double 1.000000e+00, double* %19
  store double 2.000000e+00, double* %20
  %21 = getelementptr inbounds %literal, %literal* %16, i32 0, i32 1
  %22 = getelementptr inbounds %literal, %literal* %18, i32 0, i32 1
  %23 = load double, double* %21
  %24 = load double, double* %22
  %25 = fmul double %23, %24
  %26 = call i8* @malloc(i64 16)
  %27 = bitcast i8* %26 to %literal*
  %28 = getelementptr inbounds %literal, %literal* %27, i32 0, i32 0
  %29 = getelementptr inbounds %literal, %literal* %27, i32 0, i32 1
  store double 1.000000e+00, double* %28
  store double %25, double* %29
  %30 = call i8* @malloc(i64 16)
  %31 = bitcast i8* %30 to %literal*
  %32 = getelementptr inbounds %literal, %literal* %31, i32 0, i32 0
  %33 = getelementptr inbounds %literal, %literal* %31, i32 0, i32 1
  store double 1.000000e+00, double* %32
  store double 4.000000e+00, double* %33
  %34 = getelementptr inbounds %literal, %literal* %27, i32 0, i32 1
  %35 = getelementptr inbounds %literal, %literal* %31, i32 0, i32 1
  %36 = load double, double* %34
  %37 = load double, double* %35
  %38 = fcmp oeq double %36, %37
  %39 = uitofp i1 %38 to double
  %40 = call i8* @malloc(i64 16)
  %41 = bitcast i8* %40 to %literal*
  %42 = getelementptr inbounds %literal, %literal* %41, i32 0, i32 0
  %43 = getelementptr inbounds %literal, %literal* %41, i32 0, i32 1
  store double 2.000000e+00, double* %42
  store double %39, double* %43
  %44 = getelementptr inbounds %literal, %literal* %41, i32 0, i32 1
  %45 = load double, double* %44
  %46 = fptosi double %45 to i1
  br i1 %46, label %con, label %alt

con:                                              ; preds = %entry
  %e3 = call i8* @malloc(i64 8)
  %47 = bitcast i8* %e3 to i8**
  store i8* %e2, i8** %47
  %48 = call i8* @malloc(i64 16)
  %49 = bitcast i8* %48 to %literal*
  %50 = getelementptr inbounds %literal, %literal* %49, i32 0, i32 0
  %51 = getelementptr inbounds %literal, %literal* %49, i32 0, i32 1
  store double 3.000000e+00, double* %50
  store double bitcast ([13 x i8]* @str to double), double* %51
  call void @display(%literal* %49)
  br label %end

alt:                                              ; preds = %entry
  %e4 = call i8* @malloc(i64 8)
  %52 = bitcast i8* %e4 to i8**
  store i8* %e2, i8** %52
  %53 = call i8* @malloc(i64 16)
  %54 = bitcast i8* %53 to %literal*
  %55 = getelementptr inbounds %literal, %literal* %54, i32 0, i32 0
  %56 = getelementptr inbounds %literal, %literal* %54, i32 0, i32 1
  store double 3.000000e+00, double* %55
  store double bitcast ([4 x i8]* @str.1 to double), double* %56
  call void @display(%literal* %54)
  br label %end

end:                                              ; preds = %alt, %con
  ret void
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment