$ clang -emit-llvm -c sum.c -o sum.bc
$ clang -emit-llvm  -S -c sum.c -o sum.ll
$ clang  -S  sum.c -o sum.asm

一,C源文件

sum.c

int sum(int x, int y){return x+y;
}

二,clang 14.0.3 生成的 LLVM IR 和 asm程序

1. clang 14.0.3 编译出来的 LLVM IR结果

cat  sum.ll

; ModuleID = 'sumPara.c'
source_filename = "sumPara.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @sum(i32 noundef %a, i32 noundef %b) #0 {
entry:%a.addr = alloca i32, align 4%b.addr = alloca i32, align 4store i32 %a, i32* %a.addr, align 4store i32 %b, i32* %b.addr, align 4%0 = load i32, i32* %a.addr, align 4%1 = load i32, i32* %b.addr, align 4%add = add nsw i32 %0, %1ret i32 %add
}attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"uwtable", i32 1}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{!"clang version 14.0.3"}

加注释:

// 以;开始的行,是注释行,直到行尾;
// 以@开头的标识符,是全局标识符; 可能是函数,也可能是全局变量;
// 以%开头的标识符,是局部标识符; 即局部变量的名称,也就是寄存器变量或C语言中的内存变量,其前边是类型;
// 函数的函数体在IR中,是基本块; 基本块以entry:开始, 以ret i32 %add 结束, 表示返回一个int32的局部变量add的值;
// i32 表示整型,即 c语言的int型; i是int,32是32bit;
// align 4 表示 4 字节对齐;
// alloca 是分配堆栈内存的指令,生命周期结束时会自动释放;
// store 是将数据写入局部变量、寄存器变量或叫做内存变量;
// load 是加载数据;; ModuleID = 'sumPara.c'        //注释行
source_filename = "sumPara.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"; Function Attrs: noinline nounwind optnone uwtable             //注释行
define dso_local i32 @sum(i32 noundef %a, i32 noundef %b) #0 {  //@sum 是全局函数;  %a是一个局部变量,即寄存器,类型是 i32,int32;
entry:%a.addr = alloca i32, align 4%b.addr = alloca i32, align 4store i32 %a, i32* %a.addr, align 4store i32 %b, i32* %b.addr, align 4%0 = load i32, i32* %a.addr, align 4%1 = load i32, i32* %b.addr, align 4%add = add nsw i32 %0, %1ret i32 %add
}attributes #0 = { noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }!llvm.module.flags = !{!0, !1, !2}
!llvm.ident = !{!3}!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"uwtable", i32 1}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{!"clang version 14.0.3"}

2. clang 14.0.3 编译出来的 asm

        .text.file   "sumPara.c".globl  sum                             # -- Begin function sum.p2align        4, 0x90.type   sum,@function
sum:                                    # @sum.cfi_startproc
# %bb.0:                                # %entrypushq   %rbp.cfi_def_cfa_offset 16.cfi_offset %rbp, -16movq    %rsp, %rbp.cfi_def_cfa_register %rbpmovl    %edi, -4(%rbp)movl    %esi, -8(%rbp)movl    -4(%rbp), %eaxaddl    -8(%rbp), %eaxpopq    %rbp.cfi_def_cfa %rsp, 8retq
.Lfunc_end0:.size   sum, .Lfunc_end0-sum.cfi_endproc# -- End function.ident  "clang version 14.0.3".section        ".note.GNU-stack","",@progbits.addrsig

三,clang 7.0.0 生成的 LLVM IR 和 asm程序

这个第三部分整体可以忽略,只为增加历史感。

1. clang 7.0.0 编译出来的 LLVM IR结果

cat sum.ll

//$ cat sum.ll
; ModuleID = 'sum.c'
source_filename = "sum.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @sum(i32, i32) #0 {%3 = alloca i32, align 4%4 = alloca i32, align 4store i32 %0, i32* %3, align 4store i32 %1, i32* %4, align 4%5 = load i32, i32* %3, align 4%6 = load i32, i32* %4, align 4%7 = add nsw i32 %5, %6ret i32 %7
}attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }!llvm.module.flags = !{!0}
!llvm.ident = !{!1}!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 7.0.0 (tags/RELEASE_700/final)"}

2. clang 7.0.0 编译出来的 asm

//$ cat sum.asm.text.file   "sum.c".globl  sum                     # -- Begin function sum.p2align        4, 0x90.type   sum,@function
sum:                                    # @sum.cfi_startproc
# %bb.0:pushq   %rbp.cfi_def_cfa_offset 16.cfi_offset %rbp, -16movq    %rsp, %rbp.cfi_def_cfa_register %rbpmovl    %edi, -4(%rbp)movl    %esi, -8(%rbp)movl    -4(%rbp), %esiaddl    -8(%rbp), %esimovl    %esi, %eaxpopq    %rbp.cfi_def_cfa %rsp, 8retq
.Lfunc_end0:.size   sum, .Lfunc_end0-sum.cfi_endproc# -- End function.ident  "clang version 7.0.0 (tags/RELEASE_700/final)".section        ".note.GNU-stack","",@progbits.addrsig.addrsig_sym sum

四,参考资料

1,阅读 LLVM IR的文档

先入个门,再详读官方文档

简单了解LLVM IR基本语法_七妹要奈斯的博客-CSDN博客_llvm nswhttps://blog.csdn.net/qq_42570601/article/details/107157224

LLVM Language Reference Manual — LLVM 15.0.0git documentationhttps://llvm.org/docs/LangRef.html

https://llvm.liuxfe.comhttps://llvm.liuxfe.com/

2,阅读LLVM编译出来的 x86 汇编语言辅助文档

其中目标机器的汇编语言格式,是llvm特定的规范组织而成的文件。这样可以由llvm中的工具llvm-mc 来处理机器汇编语言文件,翻译成 机器语言的目标文件。

规范说明文档:

LLVM Extensions — LLVM 15.0.0git documentationhttps://llvm.org/docs/Extensions.html

用 clang 编译成 IR 汇编 和 目标机器汇编语言文件相关推荐

  1. python编译成c代码_python如何调用c编译好可执行程序

    以下总结出几种在Python 中调用 C/C++ 代码的方法 -------------------------------------------------------------------- ...

  2. java程序编译成exe文件_将java程序编译成独立运行的exe文件

    将java程序编译成独立运行的exe文件 众所周知java的程序可以"一次编译,到处运行",这个特性不错,但是实现这个特性的前提是当前的平台 必须有相应的jvm,而且如果当前平台的 ...

  3. python代码编译成pyd_python如何编译py文件生成pyc、pyo、pyd以及如何和C语言结合使用...

    python执行py文件的流程 当我们执行一个py文件的时候,直接python xx.py即可,那么这个流程是怎么样的呢.先说明一下,python执行代码实际上是先打开文件然后执行里面的代码,所以文件 ...

  4. 什么是pyc文件,把python的py文件编译成pyc文件,把pyc文件反编译成py文件。以及python编译的如何设置不生成pyc文件

    文章目录 1 什么是pyc文件 1.1 什么是pyc文件 1.2 pyc文件是怎么生成的,有什么好处 2 把python的py文件编译成pyc文件 2.1 使用python内置库py_compile把 ...

  5. aspx文件编译成DLL文件的原理

    前言 Asp.net不是asp的简单升级,而是微软.Net计划中的一个重要组成部分,它依托.Net的多语言与强大的类库支持,引进了服务端HTML控件与WEB控件,自动处理控件的客户端与服务端的 交互, ...

  6. 【转载】把aspx文件编译成DLL文件-.NET教程,Asp.Net开发

    前言 asp.net不是asp的简单升级,而是微软.net计划中的一个重要组成部分,它依托.net的多语言与强大的类库支持,引进了服务端html控件与web控件,自动处理控件的客户端与服务端的 交互, ...

  7. suricata 编译成动态库使用

    项目中需求使用suricata 检测功能,只需要获取检测得到的 alert 结果, 需要将suricata的检测功能集成到我们的项目中,并提供接口动态加载规则. 源代码版本 6.0.4 源码 将sur ...

  8. ARM汇编指令学习---基于启动文件startup.S分析

    本文主要是基于启动文件startup.s对ARM汇编指令进行学习分析. 以 . 开头一般是伪汇编/操作指令,形如: .section伪操作来定义一个段,形如: .section .testsectio ...

  9. 使用clang将C/C++代码编译成LLVM的中间代码(LLVM ir bitcode),并反汇编LLVM bitcode

    test.c文件内容如下: #include<stdio.h> int main(void){printf("hello world!\n");return 0; } ...

最新文章

  1. 动态人脸识别系统服务器,动态人脸识别监控管理平台的设计与实现
  2. 【赠书】金融领域可解释机器学习模型与实践
  3. 使用Seata彻底解决Spring Cloud中的分布式事务问题!
  4. oracle 启动监听提示 :The listener supports no services
  5. linux7补丁安全,CentOS自动打重要安全补丁
  6. MATLAB中神经网络train函数使用说明
  7. 如何学习ReactJS:初学者完整指南
  8. python word2vec使用_使用Python可视化Word2vec的结果
  9. Windows电脑端有什么好用的便签工具?
  10. 2020车载凯立德懒人包下载_【汽车导航升级】2020抖音最新流行音乐包 无损 可CD刻录福利分享...
  11. 饶了我的耳朵吧,音乐
  12. 精通Matlab数字图像处理与识别nbsp;nbsp;nbsp;nbsp;nbsp;nbsp;
  13. 对女朋友超级抠门的星座男 12星座老公的致命缺点 12星女无法招架的求爱手段...
  14. 注销计算机 用户账户设置,老司机设置win10系统如何注销电脑登录账户的处理手法...
  15. 报错:Parameter ‘XXX‘ implicitly has an ‘any‘ type.解决方法
  16. js实现hover效果
  17. Redis配置文件redis.conf内容完整版
  18. WPF Grid边框_se7en3_新浪博客
  19. 基于网络媒介的交互设计研究
  20. 2021年西式面点师(中级)试题及解析及西式面点师(中级)模拟考试题库

热门文章

  1. 2000字长文:探讨报销系统中的数据分析框架设计
  2. Ubuntu下如何将文件夹中图片索引写进txt文件中
  3. 亚马逊证实明年发布新版Kindle阅读器
  4. 水淼和MDBplus组合使用技巧
  5. 二分法的一种通用写法
  6. 影视剪辑,视频剪辑新手10天学习计划分享
  7. BZOJ1143[CTSC2008]祭祀river 偏序集及Dilworth定理
  8. Fn除以10007的余数是多少
  9. 在终端画画、炒股、玩游戏
  10. 一个HR911130C网络变压器的坑