babyc

A toy C compiler

  • 所有者: Wilfred/babyc
  • 平台:
  • 許可證:
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Babyc

Build Status

An educational foray into compiler writing. Written in C, compiling C
to x86 assembly
(handy x86 reference site,
assembly directives reference,
System V ABI reference).

Technically targetting C11
(standard PDF),
but we will implement such a small subset of C that it's academic.

Table of Contents

Current feature set

  • positive integers (no other types yet)
  • integer constants
  • logical negation (!FOO)
  • bitwise negation (~FOO)
  • addition (foo + bar)
  • subtraction (foo - bar binary only)
  • multiplication (foo * bar)
  • less than comparison (foo < bar, foo <= bar)
  • comments (// foo and /* foo */)
  • sequences of statements (foo; bar)
  • return statements
  • if statements (if (foo) { bar }, no else yet)
  • local variables (int only, function scope only, must be
    initialised)
  • variable assignment (int only)
  • while loops (while (foo) { bar })
  • function calls (only int foo() i.e. no arguments, returning int)
  • preprocessor usage (we shell out to gcc)

License

GPL v2 license.

Usage

You will need clang, lex and yacc installed. GNU Bison is known
to work, other yacc implementations may not.

Compiling babyc:

# Compile the compiler.
$ make

Usage:

# Run it, producing an assembly file.
$ build/babyc test_programs/immediate__return_1.c
# Use the GNU toolchain to assemble and link.
$ ./link

Viewing the code after preprocessing:

$ build/babyc --dump-expansion test_programs/if_false__return_2.c

Viewing the AST:

$ build/babyc --dump-ast test_programs/if_false__return_2.c

Running tests:

$ make test

Debugging

If you're debugging a compiled program that segfaults, you may want to
simply read the out.s file.

To use gdb (given we have no signal table, function prologues or other
conveniences), do the following:

$ gdb out
(gdb) run
... it segfaults
(gdb) layout asm
... shows which line the segfault occurred on
(gdb) info registers
... shows the current state of the registers (`layout reg' also
... provides this data)

If you want to debug a program that doesn't segfault, you can set a
breakpoint to the entrypoint:

$ gdb out
(gdb) info files
    ...
    Entry point: 0x80000000
    ...
(gdb) break *0x80000000
(gdb) run

Improving code quality

The make command will generate warnings, fix them. You can also run
with clang-analyzer to catch further issues:

$ scan-build make

For code formatting, run:

$ make format

主要指標

概覽
名稱與所有者Wilfred/babyc
主編程語言C
編程語言Makefile (語言數: 5)
平台
許可證
所有者活动
創建於2014-06-27 22:17:58
推送於2020-12-26 19:48:54
最后一次提交2020-12-26 11:48:53
發布數0
用户参与
星數523
關注者數44
派生數103
提交數237
已啟用問題?
問題數5
打開的問題數2
拉請求數1
打開的拉請求數0
關閉的拉請求數1
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?