17

I have read some webpages and articles about llvm and I am quite interested in this project. (Maybe to learn something about compiler writing without the need to struggle with the complicated points of x86).

There are pages that describe how to write llvm assembly and how to assemble it, but I did not find anything on what kind of environment is needed to actually execute these. I know that I could run llvm-gcc on my files to get an object file that is executable in a C-context. But in the case that I don't want to use the C runtime environmen (libc.so and friends), what is needed to run llvm code? Is there any documentation on that?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
fuz
  • 88,405
  • 25
  • 200
  • 352
  • 2
    LLVM IR isn't really useful as a language to write your own code in. Even if it was, it's pretty low-level and rarely used. I don't think there's good support for doing that. Related and interesting, although the points made there don't necessarily apply to you: http://lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/043719.html –  Oct 14 '11 at 20:29
  • @delnan Well, you are right. My aim is to first understand llvm and then write my own toy compiler that targets llvm. – fuz Oct 14 '11 at 20:39
  • @delnan, sometimes it makes sense to write IR manually. And there is quite a comprehensive set of tools for doing it: `llvm-as`, `llvm-dis`, `lli`, `llc`. – SK-logic Oct 14 '11 at 20:48
  • libclc has some `ll` files in the source, for example https://github.com/llvm-mirror/libclc/blob/master/r600/lib/synchronization/barrier_impl.ll – J. M. Becker Feb 13 '14 at 21:36

4 Answers4

11

There appears to be an LLVM assembler.

llvm-as is the LLVM assembler. It reads a file containing human-readable LLVM assembly language, translates it to LLVM bitcode, and writes the result into a file or to standard output.

Kurt Stutsman
  • 3,994
  • 17
  • 23
  • And what to do with the bitcode? Can you just type `llvm-as foo.ll ; ./a.out` and it works like a charm? What is needed to make the code run? What is the entrypoint of the program? etc – fuz Oct 14 '11 at 20:45
  • 5
    @FUZxxl, your sequence should be `llvm-as foo.ll; llc --your-platform-options-blah-blah-blah foo.bc; ./a.out` – SK-logic Oct 14 '11 at 20:49
  • 5
    or use the JIT compiler by doing `lli foo.bc` to run the bitcode directly. – bames53 Feb 19 '12 at 08:23
  • @SK-logic, I don't think `llc` outputs native machine code -- it outputs assembly language. However, you can use `llvm-ld foo.bc` to generate native code. –  Apr 24 '12 at 10:02
  • @boyers, llc can emit ELF directly on some platforms (although it is still experimental and unstable) – SK-logic Apr 26 '12 at 10:18
  • @FUZxxl to generate executable code you need to specify a back-end (e.g. x86 CPUs) in order for a codegen module to be able to do its job – Marco A. Mar 19 '16 at 21:40
7

Quick setup: (For llvm 3.4.0 .ll files on windows)

advanced text editor from https://notepad-plus-plus.org/

llvm binaries from https://github.com/CRogers/LLVM-Windows-Binaries

hello.ll as "UTF-8 without BOM" (This code is in llvm 3.4.0 format):

@msg = internal constant [13 x i8] c"Hello World!\00"
declare i32 @puts(i8*)
define i32 @main() {
    call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @msg, i32 0, i32 0))
    ret i32 0
}

In command prompt:

lli hello.ll

Quick setup: (For llvm 3.8.0 .ll files on windows)

advanced text editor from https://notepad-plus-plus.org/

clang binaries from: http://llvm.org/releases/download.html#3.8.0

hello.ll as "UTF-8 without BOM" (This code is in llvm 3.8.0 format):

@msg = internal constant [13 x i8] c"Hello World!\00"
declare i32 @puts(i8*)
define i32 @main() {
    call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @msg, i32 0, i32 0))
    ret i32 0
}

In command prompt:

clang hello.ll -o hello.exe
hello.exe

Or as a single command:

clang hello.ll -o hello.exe & hello.exe

Errors about char16_t, u16String, etc means clang needs: -fms-compatibility-version=19

Mi G
  • 146
  • 2
  • 3
4

The static compiler, which accepts LLVM Assembly:

http://llvm.org/docs/CommandGuide/llc.html

The LLVM Assembly Language Reference:

http://llvm.org/docs/LangRef.html

mda
  • 1,158
  • 14
  • 18
1

LLVM 11.1, on Archlinux, did not accept the code from the answer above. This is from the current LLVM IR docs:

cat > hello.ll <<EOF
@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00"
declare i32 @puts(i8* nocapture) nounwind
define i32 @main() {   ; i32()*
  %cast210 = getelementptr [13 x i8], [13 x i8]* @.str, i64 0, i64 0
  call i32 @puts(i8* %cast210)
  ret i32 0
}
!0 = !{i32 42, null, !"string"}
!foo = !{!0}
EOF
lli hello.ll

If you start lli alone, it does not show a prompt, but accepts input. This input is only evaluated after Ctrl-D (EOF), though.

I came here, because I wanted to find a REPL. No luck.

Roland Puntaier
  • 3,250
  • 30
  • 35