Giter VIP home page Giter VIP logo

ucc162.3's Introduction

UCC

A Lightweight Open-Source C Compiler for Research and Education

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).

How to build and use UCC

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

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

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:ucc$ cd examples/sc

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:

How to build and use SC2 (Generate 32-bit x86 assembly for Chapter 1 in the book)

iron@Katana:sc2$ make
gcc  -o sc lex.c expr.c error.c decl.c stmt.c main.c emit.c func.c
./sc < demo.c	> demo.s
gcc  -m32 demo.s -o demo

iron@Katana:sc2$ make run


iron@Katana:sc2$ cat demo.c

	

ucc162.3's People

Contributors

cswef avatar sheisc avatar wangtingzheng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ucc162.3's Issues

centos 6.9 x64报错

make -C driver
make[1]: Entering directory /root/ucc/driver' gcc -m32 -o ucc -g ucc.c linux.c 在包含自 /usr/include/features.h:385 的文件中, 从 /usr/include/stdio.h:28, 从 ucc.c:1: /usr/include/gnu/stubs.h:7:27: 错误:gnu/stubs-32.h:没有那个文件或目录 在包含自 /usr/include/features.h:385 的文件中, 从 /usr/include/unistd.h:26, 从 linux.c:1: /usr/include/gnu/stubs.h:7:27: 错误:gnu/stubs-32.h:没有那个文件或目录 make[1]: *** [all] 错误 1 make[1]: Leaving directory /root/ucc/driver'
make: *** [all] 错误 2
网上稍微查了一下需要安装 yum install glibc-devel
可是还是没有用,能不能告诉我啥原因啊

对gcc还要linux内核版本有要求吗

在Windows虚拟机下折腾了半天,都是按要求设置的,最后跑hello的时候显示memory exhausted;
在Linux下运行 make test 的时候报了一堆的错
我的gcc版本
gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)

linux 编译问题

我用win10的内置子系统 wsl 编译, make之后会有一些warning
image

然后走到make test这一步报错了,不知为何
image
文件没有改过

第一次执行make -s test报错及其解决办法,mv: cannot stat '/ucl': No such file or directory

第一次执行make -s test报错及其解决办法,mv: cannot stat '/ucl': No such file or directory

在书(C 编译器剖析,邹昌伟)的第 23 页中第一次执行make -s test时,报错内容如下:

[oxygen@****** ucc]$ make -s test
(exprchk.c,314):(output.c,70):warning:Too many arguments
(exprchk.c,314):(output.c,86):warning:Too many arguments
mv: cannot stat '/ucl': No such file or directory
make[1]: *** [Makefile:21: test] Error 1
make: *** [Makefile:25: test] Error 2

问题原因:多级 makefile 及参数传递问题,ucc/Makefile 文件中的变量 $(UCCDIR) 没有传递到 ucc/ucl/Makefile 中,导致报错。
解决办法:将 ucc/Makefile 文件的UCCDIR定义前加 export

正确的 ucc/Makefile 文件如下:

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

修改后重新执行,结果如下

[oxygen@****** ucc]$ make -s test
(exprchk.c,314):(output.c,70):warning:Too many arguments
(exprchk.c,314):(output.c,86):warning:Too many arguments
(exprchk.c,314):(output.c,70):warning:Too many arguments
(exprchk.c,314):(output.c,86):warning:Too many arguments

可能对你有帮助的 makefile 相关代码:

1.打印当前Makefile的路径:

$(warning Makefile path: $(CURDIR)/$(lastword $(MAKEFILE_LIST)))

2.在控制台输出变量 $(UCCDIR)/ucl 的值

$(info $(UCCDIR)/ucl)

希望能帮助到有需要的人!

Advice

2年以前,我有一段时间研究了一下UCC这个编译器,当时是在SourceForge上面下载的源代码,发现项目没有再维护了。
这个编译器作为教学工具,可能具有一定意义。但是对于VS的依赖很重,比如汇编器用的是MASM(这个在VC++ 6.0中都不存在),链接器是用的VS的。在这个上面可能需要相应的修改,可以考虑JWASM的链接器。

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.