llvm

引子

LLVM:Swift、Rust、Clang 等语言的强大后盾  

使用 LLVM 框架创建一个工作编译器,第 1 部分   

使用 LLVM 框架创建有效的编译器,第 2 部分  

Easy to try  

教程   

翻译版本    

LLVM Tutorial: Table of Contents  (最新例子采用ocaml语言, 函数式编程,Haskell也式函数, C++版本

tutorial Haskell version 

tutorial C++ version   

tutorial C++   (CN &EN)
C++ pdf   

gitbook version     

readthedocs.io release_39   

html version   

C++ version 5.00   

Prepare

LLVM Language Reference Manual   

sample example 

build 

用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  

[译] 为什么人人都该懂点 LLVM (原文

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:

I saw a function called main! 

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:

llvm文档示例无法编译

===============>>#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开发新语言-教程简介及词法分析器   

基于LLVM的编译原理简明教程(1) - 写编译器越来越容易了  

基于LLVM开发编译器(01):编译器概述   

Ocaml:   

简易教程:

OCaml学习笔记

OCaml语言学习笔记(一)——Introduction to Objective Camel

OCaml Tutorial by Example  

Redis 教程  

tips:

Can't open Llvm in ocaml

无法在ocaml打开llvm  

 

 

 

 

 
posted @ 2020-02-24 18:39  lvmxh  阅读(892)  评论(0编辑  收藏  举报