llvm
引子
LLVM:Swift、Rust、Clang 等语言的强大后盾
教程
LLVM Tutorial: Table of Contents (最新例子采用ocaml语言, 函数式编程,Haskell也式函数, C++版本)
tutorial C++ (CN &EN)
C++ pdf
Prepare
LLVM Language Reference Manual
用LLVM开发新语言(非常详细的中文教程, 中文翻译)
My First Language Frontend with LLVM Tutorial (C++ org)
CPP example language source (git)
# only works for Chapter2/Chapter3 # sudo apt install clang # sudo apt install llvm# download different components http://releases.llvm.org/download.html# how to build # https://shaharmike.com/cpp/build-clang/# https://quuxplusone.github.io/blog/2018/04/16/building-llvm-from-source/ # git clone https://github.com/llvm-mirror/llvm.git git clone https://github.com/llvm/llvm-project # cd llvm mkdir build cd build # cmake -G Ninja -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" ../llvm # too big 21G cmake -DLLVM_ENABLE_PROJECTS="clang" ../llvm make # make install # Chapter4 need the latest llvm install? cd ../examples/Kaleidoscope/Chapter2/ clang++ -g -O3 toy.cpp `llvm-config --cxxflags` rlwrap ./a.out # If failed try: # clang++ -g -O3 toy.cpp `llvm-config --cxxflags` -std=c++17 # clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy -std=c++17 cd ../Chapter4 # clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy -std=c++17 clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native -rdynamic”` -O3 -o toy # rlwrap ./toy
Clang:
a C language family frontend for LLVM
LLVM
hands on
cat something.c
#include <stdio.h> int main() { /* printf function displays the content that is * * passed between the double quotes. * */ printf("Hello World"); return 0; }
run the follow commond:
sudo apt install cmake git clone https://github.com/sampsyo/llvm-pass-skeleton.git cd llvm-pass-skeleton/ mkdir build cd build/ cmake .. make
cd .. clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.* something.c
it will return:
git branch containers origin/containers git checkout containers # git checkout -b containers origin/containers cd llvm-pass-skeleton/ mkdir build cd build/ cmake .. make cd .. clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.* something.c
hack add (+) to multiple(*)
git checkout -b mutate origin/mutate cd llvm-pass-skeleton/ mkdir build cd build/ cmake .. make cd .. cc example.c ./a.out 10 clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.so example.c ./a.out 10
Linking With a Runtime Library
git checkout -b rtlib origin/rtlib cd llvm-pass-skeleton/ mkdir build cd build/ cmake .. make cd .. cc -c rtlib.c clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.so -c example.c cc example.o rtlib.o ./a.out 12
Annotations
cat foo.c
run the commond:
git checkout -b noauto origin/noauto cd llvm-pass-skeleton/ mkdir build cd build/ cmake .. make cd .. clang -S -emit-llvm -Xclang -disable-O0-optnone foo.c opt -load build/skeleton/libSkeletonPass.* -skeleton -S foo.ll
Tips:
===============>>#1 票数:1
如您所知,std :: make_unique至少需要c ++ 14,而llvm-config扩展提供的-std = c ++ 11标志会覆盖您提供的标志。
作为一种快速的解决方法,您只需将-std = c ++ 17移动到llvm-config扩展的右侧:
# clang++-9 -g -O3 toy.cpp `llvm-config-9 --cxxflags --ldflags --system-libs --libs core` -std=c++17 -o toy
clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -std=c++17 -o toy
这意味着clang将接收矛盾的参数(-std = c ++ 11和-std = c ++ 17),但是以我的经验,最右边的将是生效的。
参见: https : //godbolt.org/z/xwujes
Externd:
1. bpftrace
is a high-level tracing language for Linux enhanced Berkeley Packet Filter (eBPF) available in recent Linux kernels (4.x). bpftrace uses LLVM as a backend to compile scripts to BPF-bytecode and makes use of BCC for interacting with the Linux BPF system, as well as existing Linux tracing capabilities: kernel dynamic tracing (kprobes), user-level dynamic tracing (uprobes), and tracepoints. The bpftrace language is inspired by awk and C, and predecessor tracers such as DTrace and SystemTap. bpftrace was created by Alastair Robertson.
2. cannot build with latest LLVM #137
Docs:
What is LLVM? Definition and Related FAQs
How to get started with the LLVM C API · Paul Smith
LLVM Coding Standards — LLVM 10 documentation
Getting Started with the LLVM System — LLVM 10
Frequently Asked Questions (FAQ) — LLVM 10 documentation
The Architecture of Open Source Applications (编译器,LLVM与IR)
LLVM教程(一)-- LLVM的简介 (LLVM主要的子项目)
基于LLVM的编译原理简明教程(1) - 写编译器越来越容易了
Ocaml:
简易教程:
OCaml语言学习笔记(一)——Introduction to Objective Camel
tips: