Skip to content

Commit

Permalink
Update driver and readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
sheisc committed May 16, 2022
1 parent 4376a22 commit c6a8d00
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 17 deletions.
155 changes: 152 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,160 @@ The original author is Wenjun Wang (Chief Architect of MIUI, Xiaomi Technology).

## Overview

(1) The directory UCC contains the source code of the lightweight open-source C compiler (C89 standard).
### 1. The directory UCC contains the source code of the lightweight open-source C compiler (C89 standard).

(2) If you are interested in how to interpret a simplified C language, please refer to the directory MYC, which includes an interpreter.
#### How to build and use UCC

```sh
iron@CSE:github$ pwd

/home/iron/github

iron@CSE:github$ sudo apt-get install gcc-multilib g++-multilib

iron@CSE:github$ git clone https://github.com/sheisc/ucc162.3.git

iron@CSE:github$ cd ucc162.3/ucc

iron@CSE:ucc$ . ./ucc.sh



iron@CSE:ucc$ make

iron@CSE:ucc$ make install

mkdir -p /home/iron/github/ucc162.3/ucc/bin
cp driver/ucc /home/iron/github/ucc162.3/ucc/bin
cp ucl/ucl /home/iron/github/ucc162.3/ucc/bin
cp ucl/assert.o /home/iron/github/ucc162.3/ucc/bin
cp -r ucl/linux/include /home/iron/github/ucc162.3/ucc/bin


iron@CSE:ucc$ make test

make -C ucl test
make[1]: Entering directory '/home/iron/github/ucc162.3/ucc/ucl'
../driver/ucc -o ucl1 alloc.c ast.c decl.c declchk.c dumpast.c emit.c error.c expr.c exprchk.c flow.c fold.c gen.c input.c lex.c output.c reg.c simp.c stmt.c stmtchk.c str.c symbol.c tranexpr.c transtmt.c type.c ucl.c uildasm.c vector.c x86.c x86linux.c
mv /home/iron/github/ucc162.3/ucc/bin/ucl /home/iron/github/ucc162.3/ucc/bin/ucl.bak
cp ucl1 /home/iron/github/ucc162.3/ucc/bin/ucl
../driver/ucc -o ucl2 alloc.c ast.c decl.c declchk.c dumpast.c emit.c error.c expr.c exprchk.c flow.c fold.c gen.c input.c lex.c output.c reg.c simp.c stmt.c stmtchk.c str.c symbol.c tranexpr.c transtmt.c type.c ucl.c uildasm.c vector.c x86.c x86linux.c
mv /home/iron/github/ucc162.3/ucc/bin/ucl.bak /home/iron/github/ucc162.3/ucc/bin/ucl
strip ucl1 ucl2
cmp -l ucl1 ucl2
rm ucl1 ucl2
make[1]: Leaving directory '/home/iron/github/ucc162.3/ucc/ucl'


iron@CSE:ucc$ cd demo

iron@CSE:demo$ make ucc89

ucc -o hello hello.c && ./hello

iron@CSE:demo$ objdump -d ./hello



```

### 2. If you are interested in how to interpret a simplified C language, please refer to the directory MYC, which includes an interpreter.

#### How to build and use MYC
```sh
iron@CSE:github$ pwd

/home/iron/github

iron@CSE:github$ cd ucc162.3/myc/src

iron@CSE:src$ make

iron@CSE:src$ ./myc test.c

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

iron@CSE:src$ cat test.s

0: SetBx 1 0 0
1: Jmp 0 0 32
2: Init 4 1 0
3: Jeq 3 4 8
4: Jmp 0 0 5
5: Init 4 2 0
...


```

### 3. The directory ucc/examples/sc contains the code for Chapter 1 in the book (The Dissection of C Compiler.pdf).

#### How to build and use SC

```sh
iron@CSE:sc$ cd ~/github/

iron@CSE:github$ pwd

/home/iron/github

iron@CSE:github$ cd ucc162.3/ucc

iron@CSE:ucc$ . ./ucc.sh

iron@CSE:ucc$ which ucc

/home/iron/github/ucc162.3/ucc/bin/ucc

iron@CSE:sc$ make

ucc -o sc lex.c expr.c error.c decl.c stmt.c main.c
cat demo.c
{
int (*f(int,int,int))[4];
int (*(*fp2)(int,int,int))(int);
if(c)
a = f;
else{
b = k;
}

while(c){
while(d){
if(e){
d = d - 1;
}
}
c = c - 1;
}
}

./sc < demo.c
f is: function(int,int,int) which returns pointer to array[4] of int
fp2 is: pointer to function(int,int,int) which returns pointer to function(int) which returns int
if(!c) goto Label_0
a = f
goto Label_1
Label_0:
b = k
Label_1:
Label_2:
if(!c) goto Label_6
Label_3:
if(!d) goto Label_5
if(!e) goto Label_4
t0 = d - 1
d = t0
Label_4:
goto Label_3
Label_5:
t1 = c - 1
c = t1
goto Label_2
Label_6:

```

(3) The directory ucc/examples/sc contains the code for Chapter 1 in the book (The Dissection of C Compiler.pdf).



Expand Down
1 change: 1 addition & 0 deletions myc/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ myc: expression.o program_body.o statement.o function_body.o var_declare.o \
$(CXX) -c $(OUTPUT_OPTION) $<
clean:
rm -rf *.o myc

10 changes: 7 additions & 3 deletions ucc/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
UCCDIR = /home/iron/bin
export UCCDIR
#UCCDIR = /home/iron/bin

cur_mkfile := $(abspath $(lastword $(MAKEFILE_LIST)))
UCCDIR = $(patsubst %/, %, $(dir $(cur_mkfile)))/bin


all:
make -C driver
Expand All @@ -8,6 +11,7 @@ all:
clean:
make -C driver clean
make -C ucl clean
rm -rf $(UCCDIR)

install:
mkdir -p $(UCCDIR)
Expand All @@ -18,5 +22,5 @@ install:
uninstall:
cd $(UCCDIR);rm -f ucl assert.o ucc ucl.bak ucl1 ucl2;rm -rf $(UCCDIR)/include
test:
make -C ucl test
make -C ucl test

Binary file modified ucc/demo/hello
Binary file not shown.
2 changes: 2 additions & 0 deletions ucc/demo/hello.i
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# 1 "hello.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "hello.c"

void f(void){
Expand Down
Empty file modified ucc/demo/hello.s
100755 → 100644
Empty file.
Binary file modified ucc/demo/hi
Binary file not shown.
17 changes: 10 additions & 7 deletions ucc/demo/hi.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
.globl f
.type f, @function
f:
pushl %ebp
movl %esp, %ebp
popl %ebp
pushq %rbp
movq %rsp, %rbp
nop
popq %rbp
ret
.size f, .-f
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movq %rsi, -16(%rbp)
movl $0, %eax
popl %ebp
popq %rbp
ret
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.ident "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"
.section .note.GNU-stack,"",@progbits
3 changes: 1 addition & 2 deletions ucc/driver/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
CC = gcc -m32
CFLAGS = -g

CFLAGS = -g -DUCCDIR=\"$(shell pwd)/../bin/\"
all: ucc.c linux.c
$(CC) -o ucc $(CFLAGS) $^

Expand Down
4 changes: 3 additions & 1 deletion ucc/driver/linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
#include "ucc.h"

#define _P_WAIT 0
#define UCCDIR "/home/iron/bin/"

#ifndef UCCDIR
#define UCCDIR "/home/iron/bin/"
#endif

/**
ucc -E -v hello.c -I../ -DREAL=double -o hello.ii
Expand Down
6 changes: 6 additions & 0 deletions ucc/ucc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export UCCDIR=$(cd $(dirname $0);pwd)/bin
export PATH=$UCCDIR:$PATH




4 changes: 3 additions & 1 deletion ucc/ucl/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#cur_mkfile := $(abspath $(lastword $(MAKEFILE_LIST)))
#CUR_DIR = $(patsubst %/, %, $(dir $(cur_mkfile)))/../bin
C_SRC = alloc.c ast.c decl.c declchk.c dumpast.c emit.c \
error.c expr.c exprchk.c flow.c fold.c gen.c \
input.c lex.c output.c reg.c simp.c stmt.c \
stmtchk.c str.c symbol.c tranexpr.c transtmt.c type.c \
ucl.c uildasm.c vector.c x86.c x86linux.c
OBJS = $(C_SRC:.c=.o)
CC = gcc -m32
CFLAGS = -g -D_UCC
CFLAGS = -g -D_UCC
UCC = ../driver/ucc

all: $(OBJS) assert.o
Expand Down

0 comments on commit c6a8d00

Please sign in to comment.