LLVM IR生成的代码转换为本机代码

LLVM IR generated code to native code

本文关键字:转换 本机代码 代码 IR LLVM      更新时间:2023-10-16

我正在学习编译器的工作原理。为了学习,我使用了几本书和教程,在某个时候偶然发现了这个我无法解决的问题。

我遵循的完整教程代码可以在Github存储库中找到

此代码生成IR代码并成功执行。但是,如果我尝试将代码保存为example.ll文件并(使用llc)编译到本机程序集,则此程序集无法编译成本机可执行文件(使用nasm和ld)。我还尝试将IR编译成本地对象文件,然后使用g++(与教程的make文件中编译的解析器相同)进行编译,但也失败了。我想找到一种方法,将我生成的IR代码实际编译成可执行的二进制文件(至少对于elf64)。

生成的IR代码〔example.ll〕:

; ModuleID = 'main'
@.str = private constant [4 x i8] c"%dA0"
declare i32 @printf(i8*, ...)
define internal void @echo(i64 %toPrint) {
entry:
  %0 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str, i32 0, i32 0), i64 %toPrint)
  ret void
}
define internal void @main() {
entry:
  %0 = call i64 @do_math(i64 11)
  call void @echo(i64 %0)
  %1 = call i64 @do_math(i64 12)
  call void @echo(i64 %1)
  call void @printi(i64 10)
  ret void
}
declare void @printi(i64)
define internal i64 @do_math(i64 %a1) {
entry:
  %a = alloca i64
  store i64 %a1, i64* %a
  %x = alloca i64
  %0 = load i64* %a
  %1 = mul i64 %0, 5
  store i64 %1, i64* %x
  %2 = load i64* %x
  %3 = add i64 %2, 3
  ret i64 %3
}

然后通过asm:

$ llc-3.5 -filetype=asm -x86-asm-syntax=intel -o example.asm example.ll
$ nasm example.asm
example.asm:1: error: attempt to define a local label before any non-local labels
example.asm:2: error: attempt to define a local label before any non-local labels
example.asm:2: error: parser: instruction expected
example.asm:3: error: attempt to define a local label before any non-local labels
example.asm:3: error: parser: instruction expected
example.asm:4: error: attempt to define a local label before any non-local labels
example.asm:4: error: parser: instruction expected
example.asm:5: error: parser: instruction expected
BB#0: # %entry:3: error: parser: instruction expected
BB#0: # %entry:12: error: parser: instruction expected
...
...
<many similar errors here>

或通过GCC:

$ llc-3.5 -filetype=obj -o example.o example.ll
$ g++ native.o example.o 
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

附言:非常欢迎对存储库做出贡献,相应地修改代码(使其工作)!

UPD:按要求

asm代码

    .text
    .file   "example.ll"
    .align  16, 0x90
    .type   echo,@function
echo:                                   # @echo
    .cfi_startproc
# BB#0:                                 # %entry
    pushq   %rax
.Ltmp0:
    .cfi_def_cfa_offset 16
    movq    %rdi, %rcx
    movl    $.L.str, %edi
    xorl    %eax, %eax
    movq    %rcx, %rsi
    callq   printf
    popq    %rax
    retq
.Ltmp1:
    .size   echo, .Ltmp1-echo
    .cfi_endproc
    .align  16, 0x90
    .type   main,@function
main:                                   # @main
    .cfi_startproc
# BB#0:                                 # %entry
    pushq   %rax
.Ltmp2:
    .cfi_def_cfa_offset 16
    movl    $11, %edi
    callq   do_math
    movq    %rax, %rdi
    callq   echo
    movl    $12, %edi
    callq   do_math
    movq    %rax, %rdi
    callq   echo
    movl    $10, %edi
    callq   printi
    popq    %rax
    retq
.Ltmp3:
    .size   main, .Ltmp3-main
    .cfi_endproc
    .align  16, 0x90
    .type   do_math,@function
do_math:                                # @do_math
    .cfi_startproc
# BB#0:                                 # %entry
    movq    %rdi, -8(%rsp)
    leaq    (%rdi,%rdi,4), %rax
    movq    %rax, -16(%rsp)
    leaq    3(%rdi,%rdi,4), %rax
    retq
.Ltmp4:
    .size   do_math, .Ltmp4-do_math
    .cfi_endproc
    .type   .L.str,@object          # @.str
    .section    .rodata,"a",@progbits
.L.str:
    .asciz  "%dn"
    .size   .L.str, 4

    .section    ".note.GNU-stack","",@progbits

nm的输出

$ nm example.o 
0000000000000060 t do_math
0000000000000000 t echo
0000000000000020 t main
                 U printf
                 U printi

汇编文件

你不能组装CCD_ 3的原因可能是它在AT&T语法,而nasm需要Intel语法。您似乎已要求llc输出Intel语法,但您的标志错误。根据本手册,它是--x86-asm-syntax(注意双破折号)。

我怀疑您最好使用as(GNU汇编程序)进行汇编,因为Intel语法中有许多相互不兼容的方言;我真的不确定哪一个LLVM会说话。要执行此操作,请使用以下命令:

$ as example.asm -o example.o

目标文件

您无法将对象文件与C库链接的原因是,您已声明main函数具有内部链接(即define internal)。与C中的static关键字一样,它使符号在对象文件之外"不可见",nm输出中的小写"t"就是明证。

main创建LLVM函数对象时,应将其链接类型设置为llvm::GlobalValue::ExternalLinkage

当然,程序集文件中也出现了同样的问题——缺少.global main


不要认为这意味着你应该给所有功能提供外部链接;如果一个函数只在定义它的翻译单元中使用,那么它确实应该有内部链接。你就是不能为main做这件事。