GithubHelp home page GithubHelp logo

compiler-llvm-start's Introduction

LLVM-Start

same as in the report.

1. LLVM Installation

wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh all 我一直在这步出错,原因是dkpg interrupted
export PATH=$PATH:/lib/llvm-17/bin
echo "export PATH=\$PATH:/lib/llvm-17/bin/" >> ~/.profile

Check if successfully installed:

opt --version 

if "dkpg interrupted":

sudo rm /var/lib/dpkg/updates/*
sudo apt-get update
sudo apt-get upgrade

2. Write a test.c

nano test.c

and insert:

// test.c
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int x = 5;
    int y = 10;
    int result = add(x, y);

    printf("The sum of %d and %d is: %d\n", x, y, result);

    return 0;
}

"ctrl+0+enter" to save, "ctrl+X" to exit, "enter" to assure the name.

3. Translation

Code Format:

(1)source(.c)to binary executable:

clang test.c -o test_binary

(2)source(.c)到目标文件(.o):

clang -c test.c -o test.o

(3)source(.c)to Assembly Code(.s):

clang -S -mllvm --x86-asm-syntax=intel test.c -o test.s

(4) source(.c)to LLVM Bitcode(.bc):

clang -c -emit-llvm test.c -o test.bc

(5)source(.c)to LLVM IR(.ll):

clang -S -emit-llvm test.c -o test.ll

LLVM IR Format: (1)LLVM IR(.ll)to LLVM Bitcode(.bc):

llvm-as test.ll -o test.bc

(2)LLVM Bitcode(.bc)to LLVM IR(.ll):

llvm-dis test.bc -o test.ll

LLVM IR(.ll)to Assembly Code(.s):

llc test.ll -o test.s

Explain LLVM IR,without compiling:

lli test.ll

4. Generate Control Flow Graph

Install graphviz

sudo apt install graphviz

Visualize .ll(IR)

opt -passes=dot-cfg -disable-output test.ll
ls -la
//dot -Tpng .function_name.dot -o function_name.png//template
dot -Tpng .add.dot -o add.png

compiler-llvm-start's People

Contributors

hwen020 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.