Posts Tagged ‘LLVM’

  1. Hello, LLVM Assembler World!

    Posted on 1月 1st, 2012 by cx20

    LLVM

    LLVM Assembly Language は LLVM で使用されている中間言語である。LLVM は Low Level Virtual Machine の略で、言語に独立な仮想マシンである。
    位置づけとしては Java VM のクラスファイルや、.NET Framework に対する MSIL(中間言語)に相当する。

    ソースコード(Mac 版)

    target triple = "x86_64-apple-macosx10.7.2"
     
    @str = internal constant [19 x i8] c"Hello, LLVM World!0"
     
    define i32 @main(i32 %argc, i8** nocapture %argv) nounwind uwtable ssp {
        %puts = tail call i32 @puts(i8* getelementptr inbounds ([19 x i8]* @str, i64 0, i64 0))
        ret i32 0
    }
    declare i32 @puts(i8* nocapture) nounwind

    ソースコード(MinGW 版)

    ; ModuleID = 'hello.c'
    target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-f80:128:128-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S32"
    target triple = "i686-w64-mingw32"
     
    @str = private unnamed_addr constant [19 x i8] c"Hello, LLVM World!0"
     
    define i32 @main(i32 %argc, i8** nocapture %argv) nounwind {
      %puts = tail call i32 @puts(i8* getelementptr inbounds ([19 x i8]* @str, i32 0, i32 0))
      ret i32 0
    }
     
    declare i32 @puts(i8* nocapture) nounwind

    上記コードは以下のC言語のソースを Clang でアセンブリコード出力(clang -S -O4 hello.c)したものに相当する。

    #include <stdio.h>
     
    int main( int argc, char* argv[] )
    {
        printf( "Hello, LLVM World!¥n" );
        return 0;
    }

    コンパイル(LLVM Clang コンパイラ)

    $ clang -o hello hello.ll

    実行結果

    Hello, LLVM World!