Book LLVM Essentials ⬆
Directory llvm-essentials\ contains LLVM code examples from the book LLVM Essentials by S. Sarda & M. Pandey (Packt Publishing, December 2015).It also includes several batch files for running the example on a Windows machine. |
This code example consists of the two files src\add.c
and build.bat
.
In this first example we simply run clang.exe
with option -emit-llvm
to generate the assembly file add.ll
1.
> build -verbose run Toolset: Clang/CMake, Project: add Generate IR code to file "build\add.ll"
The generated assembly file add.ll
2 looks as follows :
> type target\add.ll ; ModuleID = 'L:\llvm-essentials\1_2_Getting_familiar_with_LLVM_IR\src\add.c' source_filename = "L:\\llvm-essentials\\1_2_Getting_familiar_with_LLVM_IR\\src\\add.c" target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-pc-windows-msvc19.28.29912" @globvar = dso_local global i32 12, align 4 ; Function Attrs: noinline nounwind optnone uwtable define dso_local i32 @add(i32 %0) #0 { %2 = alloca i32, align 4 store i32 %0, i32* %2, align 4 %3 = load i32, i32* @globvar, align 4 %4 = load i32, i32* %2, align 4 %5 = add nsw i32 %3, %4 ret i32 %5 } attributes #0 = { noinline nounwind optnone [...] } !llvm.module.flags = !{!0, !1} !llvm.ident = !{!2} !0 = !{i32 1, !"wchar_size", i32 2} !1 = !{i32 7, !"PIC Level", i32 2} !2 = !{!"clang version 14.0.4"}
3.2 Getting_address_of_element ▴
This code example consists of two files: src\toy.cpp
and build.bat
.
Function main
in source file src\toy.cpp
calls LLVM API functions to generate assembly code for the tiny function foo
:
> build -verbose run Project: toy, Configuration: Release, Platform: x64 Current directory: "L:\llvm-essentials\3_2_Getting_address_of_element\build" Generate configuration files into directory "build" Generate executable "toy.exe" Execute build\Release\toy.exe ; ModuleID = 'my compiler' source_filename = "my compiler" define i32 @foo(<2 x i32>* %a) { entry: %a1 = getelementptr i32, <2 x i32>* %a, i32 1 ret i32 0 }
This code example consists of two files: src\toy.cpp
and build.bat
.
Function main
in source file src\toy.cpp
calls LLVM API functions to generate assembly code for the tiny function foo
:
> build -verbose run Project: toy, Configuration: Release, Platform: x64 Current directory: L:\llvm-essentials\3_3_Reading_from_memory\build Generate configuration files into directory "build" Generate executable "toy.exe" Execute build\Release\toy.exe ; ModuleID = 'my compiler' source_filename = "my compiler" define i32 @foo(<2 x i32>* %a) { entry: %a1 = getelementptr <2 x i32>*, <2 x i32>* %a, i32 1 %load = load <2 x i32>*, <2 x i32>** %a1, align 8 ret <2 x i32>* %load }
3.4 Writing to memory ▴
This code example consists of two files: src\toy.cpp
and build.bat
.
Function main
in source file src\toy.cpp
calls LLVM API functions to generate assembly code for the tiny function foo
:
> build -verbose run Project: toy, Configuration: Release, Platform: x64 Current directory: "L:\llvm-essentials\3_4_Writing_to_memory\build" Generate configuration files into directory "build" Generate executable "toy.exe" Execute "build\Release\toy.exe" ; ModuleID = 'my compiler' source_filename = "my compiler" define i32 @foo(<2 x i32>* %a) { entry: %a1 = getelementptr i32, <2 x i32>* %a, i32 1 %load = load i32, i32* %a1, align 1 %multmp = mul i32 %load, 16 store i32 %multmp, i32* %a1, align 1 ret i32 %multmp }
[1] LLVM file extensions ↩
-
Extension Description .bc
bitcode format (binary) .ll
assembly language format (text)
[2] main
function not found ↩
-
The assembly code in file
add.ll
can not be executed withlli.exe
since it doesn't define amain
function as the program entry point : -
> %LLVM_HOME%\bin\lli.exe build\add.ll C:\opt\LLVM-15.0.6\bin\lli.exe: error: 'main' function not found in module.