Git Product home page Git Product logo

babyc's Introduction

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 targeting 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

babyc's People

Contributors

timgates42 avatar wilfred 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

babyc's Issues

ld -s -o out out.o error

ld -s -o out out.ocreates these warnings:


ld: warning: option -s is obsolete and being ignored
ld: warning: -macosx_version_min not specified, assuming 10.10
ld: warning: object file (out.o) was built for newer OSX version (10.11) than being linked (10.10)
Undefined symbols for architecture x86_64:
  "start", referenced from:
     implicit entry/start for main executable
     (maybe you meant: _start)
ld: symbol(s) not found for inferred architecture x86_64

Warnings aren't inherently bad, but the next step: ./out fails with ./out: No such file or directory

Compatibility with Windows

Hello,
I've managed to get babyc to compile (and work!) on Windows 7 with MinGW and minimal tweaks to babyc. The only header file which isn't available on Windows by default is err.h for for warnx due to the fact that Windows isn't POSIX compliant. If you avoided this header (shouldn't be too hard), you could make babyc Windows compatible! Thoughts?
Jerom

P.S. BabyC is pretty darn cool. :)

Trying to compile the main file

hey man, i must say you have done a great work with this. And i will put in the credits.
However im having an issue.. when i try to compile the main.c file
$gcc main.c -o a.out
i get the following error :
main.c:9:25: fatal error: build/y.tab.h: No such file or directory
compilation terminated

Any help please. Urgent

_error not found, error?

I keep getting this error. I am on OSX 10.10.3 with Xcode and the CLI Tools installed with lex, flex, and yacc, as well as clang, but I keep getting this error:

clang -Wall -Wextra -g -O0 -std=gnu99 -fstack-protector-all -ftrapv -o build/babyc main.c build/lex.yy.o build/y.tab.o build/syntax.o build/environment.o build/assembly.o build/stack.o build/context.o build/list.o
Undefined symbols for architecture x86_64:
  "_error", referenced from:
      _comment in lex.yy.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [build/babyc] Error 1

Any suggestions?

Question

i already have flex installed on the computer, and still got the error that i had sent you.

What were the versions of flex, clang and yacc you utilized?

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.