progs/fun2/sqr.ll
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 28 Oct 2022 09:08:13 +0100
changeset 893 54a483a33763
parent 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
854
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    30
@.str_int = private constant [3 x i8] c"%d\00"
820
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) {
854
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    33
   %t0 = getelementptr [3 x i8], [3 x i8]* @.str_int, i32 0, i32 0
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    34
   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    35
   ret void
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    36
}
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    37
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    38
@.str_char = private constant [3 x i8] c"%c\00"
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    39
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    40
define void @print_char(i32 %x) {
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    41
   %t0 = getelementptr [3 x i8], [3 x i8]* @.str_char, i32 0, i32 0
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
   ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
}
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
; END OF BUILD-IN FUNCTIONS (prelude)
826
b0352633bf48 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 820
diff changeset
    47
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
@Max = global i32 10
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
define i32 @sqr (i32 %x) {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
   %tmp_20 = mul i32  %x, %x
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
   ret i32 %tmp_20
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
define void @all (i32 %n) {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
   %tmp_22 = load i32, i32* @Max
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
   %tmp_21 = icmp sle i32  %n, %tmp_22
854
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    58
   br i1 %tmp_21, label %if_branch_29, label %else_branch_30
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
854
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    60
if_branch_29:
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
   %tmp_23 = call i32 @sqr (i32 %n)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
   call void @print_int (i32 %tmp_23)
854
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    63
   call void @new_line ()
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    64
   %tmp_26 = add i32  %n, 1
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    65
   call void @all (i32 %tmp_26)
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
   ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
854
ce4a7bab6bd8 testcases
Christian Urban <christian.urban@kcl.ac.uk>
parents: 826
diff changeset
    68
else_branch_30:
820
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
   call void @skip ()
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
   ret void
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
define i32 @main() {
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
   call void @all (i32 0)
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
   ret i32 0
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
}
7fd1f611c21d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77