progs/sqr.ll
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 21 Oct 2020 09:24:32 +0100
changeset 785 faa4489267d5
parent 701 681c36b2af27
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
@.str = private constant [4 x i8] c"%d\0A\00"
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
declare i32 @printf(i8*, ...)
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
; prints out an integer
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
define i32 @printInt(i32 %x) {
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
   %t0 = getelementptr [4 x i8], [4 x i8]* @.str, i32 0, i32 0
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
   call i32 (i8*, ...) @printf(i8* %t0, i32 %x) 
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
   ret i32 %x
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
}
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
; square function
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
define i32 @sqr(i32 %x) {
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
   %tmp = mul i32 %x, %x
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
   ret i32 %tmp
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
}
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
; main 
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
define i32 @main() {
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  %1 = call i32 @sqr(i32 5)
701
681c36b2af27 updated
Christian Urban <urbanc@in.tum.de>
parents: 678
diff changeset
    21
  %2 = call i32 @printInt(i32 %1)
678
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
  ret i32 %1
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
}
ff3b48da282c updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24