Is it possible to use LLVM-assembly directly?
Asked Answered
F

4

18

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?

Featherbrain answered 14/10, 2011 at 20:25 Comment(4)
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: lists.cs.uiuc.edu/pipermail/llvmdev/2011-October/043719.htmlForbade
@delnan Well, you are right. My aim is to first understand llvm and then write my own toy compiler that targets llvm.Featherbrain
@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.Julesjuley
libclc has some ll files in the source, for example github.com/llvm-mirror/libclc/blob/master/r600/lib/…Dhaulagiri
R
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.

Ralphralston answered 14/10, 2011 at 20:42 Comment(6)
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? etcFeatherbrain
@FUZxxl, your sequence should be llvm-as foo.ll; llc --your-platform-options-blah-blah-blah foo.bc; ./a.outJulesjuley
or use the JIT compiler by doing lli foo.bc to run the bitcode directly.Cleres
@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.Perforate
@boyers, llc can emit ELF directly on some platforms (although it is still experimental and unstable)Julesjuley
@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 jobDecker
F
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

Fatherland answered 18/3, 2016 at 20:32 Comment(0)
U
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

Unclasp answered 11/11, 2012 at 4:37 Comment(0)
E
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.

Enthrone answered 15/3, 2021 at 16:41 Comment(1)
Yes, LLVM IR changes between versions. It is quite unfortunate.Featherbrain

© 2022 - 2025 — McMap. All rights reserved.