Writing llvm bytes of code

I just found out LLVM and don't know much more about it. I tried using llvm in the browser . I see that any C code that I write is converted to LLVM bytecode, which is then converted to native code. The page shows a textual representation of the byte code. For example, for the following C code:

int array[] = { 1, 2, 3}; int foo(int X) { return array[X]; } 

It displays the following bytecode:

 target datalayout = "ep:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" target triple = "x86_64-linux-gnu" @array = global [3 x i32] [i32 1, i32 2, i32 3] ; <[3 x i32]*> [#uses=1] define i32 @foo(i32 %X) nounwind readonly { entry: %0 = sext i32 %X to i64 ; <i64> [#uses=1] %1 = getelementptr inbounds [3 x i32]* @array, i64 0, i64 %0 ; <i32*> [#uses=1] %2 = load i32* %1, align 4 ; <i32> [#uses=1] ret i32 %2 } 

My question is: can I write byte code and pass it to the llvm assembler to convert to native code, skipping the first step of writing C code in general? If so, how do I do this? Does anyone have pointers to me?

+6
bytecode llvm
source share
2 answers

Oh sure. First, you can write LLVM IR manually. All tools, such as llc (which will generate native code for you) and opt (LLVM IR => LLVM IR optimizer), accept the textual representation of LLVM IR as input.

+6
source share

One very important feature (and design goal) of LLVM IR-language is its three-way presentation:

  • The text view you can see here
  • Bytecode representation (or binary form)
  • Memory representation

All 3 are truly interchangeable. Nothing that can be expressed in one cannot be expressed in others.

Therefore, if you agree with the syntax, you can really write IR yourself. This is pretty pointless, although if it is not used as an exercise to accustom yourself to the format, it is better to read (and diagnose) IR or create your own compiler :)

+7
source share

All Articles