Skip to content

Commit

Permalink
✨add install file && my first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
DoubleMice committed Feb 27, 2019
1 parent dc13965 commit 131d37c
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
llvm/
compiler-rt/
clang/
build/
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

test/*.bc
Empty file added Makefile
Empty file.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# llvm-pass-practice
the road to write llvm pass


## notice

* path should not include Chinese character

## install llvm
`./install.sh`

## practices

* my first pass:show function name
26 changes: 26 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#get LLVM
git clone --depth=1 http://llvm.org/git/llvm.git


#get clang
git clone --depth=1 http://llvm.org/git/clang.git


#get compiler-rt
git clone --depth=1 http://llvm.org/git/compiler-rt.git


#Set up clang, compiler-rt
pushd llvm/tools
ln -s ../../clang .
popd

pushd llvm/projects
ln -s ../../compiler-rt .
popd

mkdir build
pushd build
cmake -G "Unix Makefiles" ../llvm
make -j4
popd
6 changes: 6 additions & 0 deletions my first pass/FuncName/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_llvm_library( LLVMFuncName MODULE
FuncName.cpp

PLUGIN_TOOL
opt
)
19 changes: 19 additions & 0 deletions my first pass/FuncName/FuncName.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

namespace {
struct FuncName:public FunctionPass {
static char ID;
FuncName():FunctionPass(ID) {}
bool runOnFunction(Function &F) {
errs()<< F.getName() << "\n";
return false;
}
};
}

char FuncName::ID = 0;
static RegisterPass<FuncName> X("funcName","display function name");
118 changes: 118 additions & 0 deletions my first pass/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
## setup env
1. add library
add
```
add_llvm_library( LLVMFuncName MODULE
YOUR_PASS.cpp

PLUGIN_TOOL
opt
)
```
into `PASS_DIR_PATH/CMakeLists.txt`

2. add subdirectory
add `add_subdirectory(PASS_DIR_PATH)` into `lib/Transforms/CMakeLists.txt`

## coding

1. the necessary header
```cpp
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
```

2. add namespace
```cpp
using namespace llvm;
```

3. write your own pass in an anonymous namespace
```cpp
namespace {
}
```

## build

```sh
⇒ doublemice@DoubleMice-MBP:~/Documents/graduate/llvm-pass-practice|master⚡ pwd
/Users/doublemice/Documents/graduate/llvm-pass-practice
pushd llvm/lib/Transforms
ln ../../../my\ first\ pass/FuncName .
popd
pushd build
make
popd
```

## load the pass

### the output lib
```sh
doublemice@DoubleMice-MBP:~/Documents/graduate/llvm-pass-practice|master⚡
⇒ ls build/lib/LLVMFuncName.dylib
build/lib/LLVMFuncName.dylib
```

as we see,the output dylib name is exact the name we define in `PASS_DIR_PATH/CMakeLists.txt`(see: setup env -> 1)

### load it with opt
1. prepare a test file in test/FuncName_test.c
```cpp
#include <stdio.h>
void sayHello() {
printf("hello\n");
}
void sayGoodbye() {
printf("goodbye\n");
}
int main() {
sayHello();
sayGoodbye();
return 0;
}
```

2. compile it into a llvm bitcode
```sh
$LLVM_BIN/clang -O3 -emit-llvm test/FuncName_test.c -c -o test/FuncName_test.bc
```
3. run the pass with opt
```sh
doublemice@DoubleMice-MBP:~/Documents/graduate/llvm-pass-practice|master⚡
$LLVM_BIN/opt -load build/lib/LLVMFuncName.dylib -funcName < test/FuncName_test.bc > /dev/null
sayHello
sayGoodbye
main
```

## a skeleton
```cpp
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
namespace {
struct Example:public FunctionPass {
static char ID;
Example():FunctionPass(ID) {}
//tells llvm which other pass we need
void getAnalysisUsage(AnalysisUsage &AU) const {
...
}
virtual bool runOnFunction(Function &F) {
...
//true:this pass will change our program
//false:won't change
return false;
}
};
}
char Example::ID = 0;
static RegisterPass<Example> X("PASS_NAME","PASS_DESCRIPTION")
```
12 changes: 12 additions & 0 deletions test/FuncName_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
void sayHello() {
printf("hello\n");
}
void sayGoodbye() {
printf("goodbye\n");
}
int main() {
sayHello();
sayGoodbye();
return 0;
}

0 comments on commit 131d37c

Please sign in to comment.