开发者

Is it possible to use LLVM-assembly directly?

开发者 https://www.devze.com 2023-04-13 07:42 出处:网络
I have read somewebpages 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

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 run开发者_如何学Gotime environmen (libc.so and friends), what is needed to run llvm code? Is there any documentation on that?


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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消