Git Product home page Git Product logo

mimicc's Introduction

mimicc: MItyu's MIni C Compiler

A toy, self-hosted (subset of) C Compiler for Linux on x86_64.

Supported functionalities

Type

  • char
  • int
  • Array
  • Pointer (including function pointer)
  • Struct
  • Enum
  • Typedef

Function

  • Global function
  • static attribute
  • extern attribute
  • Function call with over 6 arguments
  • Variadic argument
    • va_start and va_end is available. (va_arg is not supported yet.)

Note that passing/returning struct itself is not supported.

Variable

  • Global variable
  • Local variable
  • static attribute
  • extern attribute
  • Variable initializer (primitive types, structs, arrays)
  • Type checking (incomplete)
    • So sometimes mimicc accepts incorrect C program and rejects correct C program.

Literals

  • Decimal, hexadecimal, octal number
  • Characters
  • String literals
  • Escape sequence
    • \', \", \\, \?, \a, \b, \f, \n, \r, \t, \v, and \0

Control syntax

  • if, else if and else
  • for (with variable declaration at initializer statement support: for (int i = 0; ...))
  • while
  • switch, case and default
  • break
  • continue
  • return

Statements

  • Compound literal for struct (it for array is not yet)
  • Type cast
  • One-line comment (// ...)
  • Multi-line comment (/* ... */)

operators

  • ==, !=, <, >, <=, >=, &&, and ||
  • +=, *=, /=, %=, &=, |=, ^=, <<=, and >>=
  • ++ and -- of both prefix and postfix
  • +, *, /, and %
  • &, |, ^, <<, and >> (Bit operation)
  • ! ("not" operator)
  • Addressing &
  • Dereferencing *
  • Unary + and - (e.g. +3 and -5)
  • sizeof
  • . and ->
  • () (function call)
  • [] (array accessing)
  • Ternary operator (condition ? expr-true : expr-false)
  • , (expr, expr, expr, ...)

Preprocess

  • #include <header> and #include "header"
  • #if, #elif, #end, and #ifdef
    • Nesting them is also OK.
  • Macros
    • Object-like macro (e.g. #define BUFSIZE (64))
    • Function-like macro (e.g. #define SIZEOF(ar) (sizeof(ar) / sizeof(ar[0])))
    • #undef
    • Note that mimicc doesn't have infinite macro expansion guard, so mimicc won't return with recursive macro like this:
#define REC REC
#define MUTUAL_REC1(arg) MUTUAL_REC2(arg)
#define MUTUAL_REC2(arg) MUTUAL_REC1(arg)

Build menus

# Build mimicc compiler with gcc. The binary will available at ./mimicc.
$ make

# Build 2nd gen mimicc compiler.  The binary will available at ./test/self/mimicc.
$ make self

# Build 3rd gen mimicc compiler.  The binary will available at ./test/selfself/mimicc.
$ make selfself

Test menus

# Run tests for 1st gen mimicc compiler.
$ make test

# Run tests for 2nd gen mimicc compiler.
$ make test_self  or  $ make self_test

# Run tests for 3rd gen mimicc compiler.  Also check there's no differencies
# between assembly of 2nd gen mimicc and of 3rd gen mimicc.
$ make test_selfself or  $ make selfself_test

# Run all tests at once
$ make test_all

Compile your C program

$ cd path/to/this/repo
$ ./mimicc -o <out-asm-path> <in-c-program-path>
$ gcc -o <out-binary-path> <out-asm-path>

Acknowledgements

This project is heavily, heavily inspired by this web book. Great thanks:

mimicc's People

Contributors

mityu avatar

Stargazers

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