LLVM Assembly programozási nyelv

Példaprogramok

Parancssori argumentumok

Az alábbi program összeadja egyjegyű parancssori argumentumait, majd visszatér az összegkkel. Például:

llvm-as -o sumargs.bc sumargs.ll
llvm-ld -native -o sumargs sumargs.bc
./sumargs 1 2 3 4
echo $?     # eredmény kiírása (ez esetben: 10)

sumargs.ll:

define i32 @main(i32 %argc, i8** %argv) { LoopHeader: ; the first argument is the executed ; command's name so don't count it %0 = sub i32 %argc, 1 ; check if we have no real arguments %1 = icmp sgt i32 %0, 0 ; jump to the appropriate label br i1 %1, label %LoopStart, label %LoopEnd LoopStart: ; get last counter value %c = phi i32 [ %0, %LoopHeader ], [ %dc, %LoopStart ] ; get last sum value %s = phi i32 [ 0, %LoopHeader ], [ %ns, %LoopStart ] ; decrement counter %dc = sub i32 %c, 1 ; calculate sum ; get a pointer to the actual argument %p0 = getelementptr i8** %argv, i32 %c %v0 = load i8** %p0 ; get the value of the first character %p1 = getelementptr i8* %v0, i8 0 %v1 = load i8* %p1 ; convert ASCII character code to integer %v2 = sub i8 %v1, 48 ; convert number from i8 to i32 %v3 = sext i8 %v2 to i32 ; add the actual value %ns = add i32 %s, %v3 ; evaluate loop condition %2 = icmp sgt i32 %dc, 0 ; jump to the appropriate label br i1 %2, label %LoopStart, label %LoopEnd LoopEnd: %r = phi i32 [ %ns, %LoopStart ], [ 0, %LoopHeader ] ; return the sum ret i32 %r }

Rekurzió példa (Fibonacci)

Ez a példaprogram a rekurzív eljáráshívást mutatja be a Fibonacci-sorozat megadott elemének kiszámításával. Fordítás és futtatás:

llvm-as -o fibonacci.bc fibonacci.ll
llvm-ld -native -o fibonacci fibonacci.bc
./fibonacci 10
Result: 55

fibonacci.ll:

@format = internal constant [13 x i8] c"Result: %d\0D\0A\00" declare i32 @atoi(i8* noalias nocapture) nounwind declare i32 @printf(i8* noalias nocapture, ...) nounwind define i32 @main(i32 %argc, i8** %argv) { Init: ; the first argument is the executed ; command's name so don't count it %0 = sub i32 %argc, 1 ; check if we have no real arguments %1 = icmp sgt i32 %0, 0 ; jump to the appropriate label br i1 %1, label %ReadArg, label %Calculate ReadArg: ; load the first arguments pointer %p0 = getelementptr i8** %argv, i32 1 %v0 = load i8** %p0 ; convert it to number %vl = call i32 @atoi(i8* %v0) br label %Calculate Calculate: %n = phi i32 [ %vl, %ReadArg ], [ 0, %Init ] %f = getelementptr [13 x i8]* @format, i32 0, i32 0 %r = call i32 @fibonacci(i32 %n) call i32 (i8*, ...)* @printf(i8* %f, i32 %r) ret i32 %r } define i32 @fibonacci(i32 %p) { Check: ; (the first two element is 1 in the Fibonacci series) ; if the p parameter is greater than 2 %v0 = icmp ugt i32 %p, 2 ; jump to label Step, else Init br i1 %v0, label %Step, label %Init Init: ; return constant 1 (see jump in Check) ret i32 1 Step: ; calculate %p-1 into %v1 %v1 = sub i32 %p, 1 ; call fibonacci recursively %v2 = call i32 (i32)* @fibonacci(i32 %v1) ; calculate %p-2 into %v3 %v3 = sub i32 %p, 2 ; call fibonacci recursively %v4 = call i32 (i32)* @fibonacci(i32 %v3) ; create the sum of the result of the last two calls %v5 = add i32 %v2, %v4 ; return the sum ret i32 %v5 }