Coder Social home page Coder Social logo

compiler's Introduction

C compiler using Flex and Bison

##Build make

##Run With a file that needs preprocessing (has #includes, #defines, etc):

gcc -E inputfile.c | ./compile >outputfile.s

Otherwise, from some file:

./compile <inputfile.c >outputfile.s

Or just run ./compile >outputfile.s and type in your code manually.

The outputfile.s file contains x86 assembly (AT&T syntax). To create an executable and run it:

gcc -m32 outputfile.s -o a.out
./a.out

###Options

  • -a: show AST
  • -d: show declarations
  • -q: show quads
  • -t: show target code (x86)
  • -T: same as -t but also show the quads as comments in target code

Note: if none of the above options are specified, -t will be set by default.

For sample input/output files, see the tests directory.


##Features

  • show AST, declarations, quads, and target code (x86)
  • reading and writing local and global variables (including block scopes)
    • ints and int pointers/arrays only
  • computation of expressions (most of them)
  • call functions with arguments
    • including external ones such as printf, assuming linked
  • string literals (necessary to get printf working)
  • pointers and pointer indirection
    • array access
    • pointer arithmetic
  • most control flow statements (see anti-features section below)

##Anti-features What this compiler doesn't support:

  • certain declarations
    • type specifiers that aren't int or pointers to / arrays of ints
    • type qualifiers (const, volatile, restrict)
    • initialized declarations
    • storage classes
    • anything to do with structs or unions
    • enums
    • typedefs?
    • functions that take parameters
    • multi-dimensional arrays
  • certain expressions
    • sizeof
    • casts
    • ternary operator (i.e. a ? b : c)
    • short circuit operators && and ||
      • the operators exist and return 0 or 1; they just don't short circuit
    • division of pointers
    • unary + operator
      • what does this even do
  • certain control flow statements
    • do loops
    • switches
    • goto statements
    • labels
  • handling of multiple input files hasn't been tested

Things that it does now that could be improved:

  • no register allocation
    • currently just uses a separate local variable for each temporary var
  • memory leaks everywhere
    • hasn't been a problem yet
    • just don't use this to compile any large and useful programs
  • short circuit operators don't actually short circuit
  • terribly disorganized
    • do the AST nodes properly!
  • quads are internal (the printed quads aren't actually used as input)
  • sizes are hardcoded to 4, since only ints/pointers are supported

compiler's People

Contributors

walfas avatar

Watchers

 avatar

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.