---
# System prepended metadata

title: Compiler Final Project
tags: [compiler]

---

# Compiler Final Project
###### tags: `compiler`

## Enviroment
### thu VACS (Ubuntu 22.04.2 Long Term Supported)

### 避免 VACS 因待機過久而被收回資源
#### Show Applications → Settings → Power → Power Saving Options → Screen Blank (Change to **Never**)

## RUN
### 首先要有 Root 權限
```
sudo su
```
![](https://hackmd.io/_uploads/HJvnEtfB2.png)


### 創建新的腳本去跑
```
vim compiler.sh
```
![](https://hackmd.io/_uploads/rJPuLtzB3.png)

### vim 裡面放入以下程式

```
apt update

cd /home/s09350150
apt-get install -y flex bison
apt install -y git
git clone https://github.com/gjlmotea/Compiler-sample.git

cd /home/s09350150/Compiler-sample
lex lex.l
ls
yacc yacc.y
ls
yacc -d yacc.y
cc lex.yy.c y.tab.c -o cpl
./cpl < sample1.c
./cpl < sample2.c
./cpl < sample3.c
./cpl < sample4.c
./cpl < sample5.c
./cpl < sample6.c
./cpl < sample7.c



#cd /home/s09350150
#svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
#cd llvm/tools
#svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
#cd clang/tools
#svn co http://llvm.org/svn/llvm-project/clang-tools-extra/trunk extra
#cd llvm/tools
#svn co http://llvm.org/svn/llvm-project/lld/trunk lld
#svn co http://llvm.org/svn/llvm-project/polly/trunk polly
#cd llvm/projects
#svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
#svn co http://llvm.org/svn/llvm-project/openmp/trunk openmp
#svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx
#svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi
#svn co http://llvm.org/svn/llvm-project/test-suite/trunk test-suite
#cd /home/s09350150
#cd llvm
#mkdir build
#cd build
#sudo apt install cmake
#sudo apt install ninja-build
#sudo cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_ASSERTIONS=On ~/llvm

apt-get install -y clang llvm

clang --version
llc --version

cd /home/s09350150/Compiler-sample
clang -S -emit-llvm sample3.c
opt -S -globalopt -loop-simplify -mem2reg sample3.ll

echo "============================="

cat sample3.ll

echo "============================="

llc sample3.ll

echo "============================="

cat sample3.s

echo "============================="

as sample3.s -o sample3.o
ld sample3.o -o sample3
```
![](https://hackmd.io/_uploads/rybfLYMB2.png)

### 開始執行
```
sh compiler.sh
```
### 編譯1~7 結果
![](https://hackmd.io/_uploads/H1bm3YzSn.png)
![](https://hackmd.io/_uploads/HyHQ3FMHn.png)
![](https://hackmd.io/_uploads/SJKm2tGBn.png)
![](https://hackmd.io/_uploads/SJhQnFfr2.png)
### 軟體安裝成功
![](https://hackmd.io/_uploads/BkUKhKGSn.png)

### 組合語言結果
![](https://hackmd.io/_uploads/Bk-ihYGrh.png)
