progs/fun2/sqr.ll
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 30 Aug 2021 14:18:08 +0100
changeset 826 b0352633bf48
parent 820 7fd1f611c21d
child 854 ce4a7bab6bd8
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
declare i32 @printf(i8*, ...)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
@.str_nl = private constant [2 x i8] c"\0A\00"
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
@.str_star = private constant [2 x i8] c"*\00"
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
@.str_space = private constant [2 x i8] c" \00"
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
define void @new_line() #0 {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
  %t0 = getelementptr [2 x i8], [2 x i8]* @.str_nl, i32 0, i32 0
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
  %1 = call i32 (i8*, ...) @printf(i8* %t0)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
  ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
define void @print_star() #0 {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
  %t0 = getelementptr [2 x i8], [2 x i8]* @.str_star, i32 0, i32 0
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  %1 = call i32 (i8*, ...) @printf(i8* %t0)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
define void @print_space() #0 {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
  %t0 = getelementptr [2 x i8], [2 x i8]* @.str_space, i32 0, i32 0
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
  %1 = call i32 (i8*, ...) @printf(i8* %t0)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
  ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
define void @skip() #0 {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
  ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
@.str = private constant [4 x i8] c"%d\0A\00"
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
define void @print_int(i32 %x) {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
   %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
   ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
; END OF BUILD-IN FUNCTIONS (prelude)
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 820
diff changeset
    39
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
@Max = global i32 10
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
define i32 @sqr (i32 %x) {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
   %tmp_20 = mul i32  %x, %x
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
   ret i32 %tmp_20
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
define void @all (i32 %n) {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
   %tmp_22 = load i32, i32* @Max
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
   %tmp_21 = icmp sle i32  %n, %tmp_22
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
   br i1 %tmp_21, label %if_branch_28, label %else_branch_29
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
if_branch_28:
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
   %tmp_23 = call i32 @sqr (i32 %n)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
   call void @print_int (i32 %tmp_23)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
   %tmp_25 = add i32  %n, 1
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
   call void @all (i32 %tmp_25)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
   ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
else_branch_29:
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
   call void @skip ()
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
   ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
define i32 @main() {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
   call void @all (i32 0)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
   ret i32 0
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68