All Projects → Fedjmike → Fcc

Fedjmike / Fcc

Licence: gpl-3.0
Fedjmike's C Compiler

Programming Languages

c
50402 projects - #5 most used programming language

Projects that are alternatives of or similar to Fcc

Mini C
Dr Strangehack, or: how to write a self-hosting C compiler in 10 hours
Stars: ✭ 372 (+268.32%)
Mutual labels:  compiler, parser, x86, code-generation
Xdpw
XD Pascal: A small embeddable self-hosting Pascal compiler for Windows. Supports Go-style methods and interfaces
Stars: ✭ 199 (+97.03%)
Mutual labels:  compiler, parser, x86
Compiler
The Hoa\Compiler library.
Stars: ✭ 458 (+353.47%)
Mutual labels:  compiler, parser
Awesome Tensor Compilers
A list of awesome compiler projects and papers for tensor computation and deep learning.
Stars: ✭ 490 (+385.15%)
Mutual labels:  compiler, code-generation
Marked
A markdown parser and compiler. Built for speed.
Stars: ✭ 26,556 (+26193.07%)
Mutual labels:  compiler, parser
Flingos
An educational operating system written in C#. A great stepping stone from high to low level development.
Stars: ✭ 451 (+346.53%)
Mutual labels:  compiler, x86
Tinyrb
A tiny subset of Ruby with a Lua'esc VM
Stars: ✭ 452 (+347.52%)
Mutual labels:  compiler, parser
Tiramisu
A polyhedral compiler for expressing fast and portable data parallel algorithms
Stars: ✭ 685 (+578.22%)
Mutual labels:  compiler, code-generation
Javaparser
Java 1-15 Parser and Abstract Syntax Tree for Java, including preview features to Java 13
Stars: ✭ 3,972 (+3832.67%)
Mutual labels:  parser, code-generation
Smallerc
Simple C compiler
Stars: ✭ 986 (+876.24%)
Mutual labels:  compiler, x86
Radon
A scripting language.
Stars: ✭ 22 (-78.22%)
Mutual labels:  compiler, parser
I8086.js
16bit Intel 8086 / 80186 + X87 emulator written in TypeScript with REPL assembly compiler and tiny C compiler
Stars: ✭ 54 (-46.53%)
Mutual labels:  compiler, x86
Tiny Compiler
A tiny compiler for a language featuring LL(2) with Lexer, Parser, ASM-like codegen and VM. Complex enough to give you a flavour of how the "real" thing works whilst not being a mere toy example
Stars: ✭ 425 (+320.79%)
Mutual labels:  compiler, parser
Swaggen
OpenAPI/Swagger 3.0 Parser and Swift code generator
Stars: ✭ 385 (+281.19%)
Mutual labels:  parser, code-generation
Minigo
minigo🐥is a small Go compiler made from scratch. It can compile itself.
Stars: ✭ 456 (+351.49%)
Mutual labels:  compiler, parser
Webassemblyjs
Toolchain for WebAssembly
Stars: ✭ 566 (+460.4%)
Mutual labels:  compiler, parser
Idris Elixir
A code-generator for Idris that targets Elixir
Stars: ✭ 56 (-44.55%)
Mutual labels:  compiler, code-generation
Craftinginterpreters
Repository for the book "Crafting Interpreters"
Stars: ✭ 4,298 (+4155.45%)
Mutual labels:  compiler, parser
Ratel Core
High performance JavaScript to JavaScript compiler with a Rust core
Stars: ✭ 367 (+263.37%)
Mutual labels:  compiler, parser
Inc
an incremental approach to compiler construction
Stars: ✭ 702 (+595.05%)
Mutual labels:  compiler, x86

Fedjmike's C Compiler

Dependencies: C standard library, a POSIX cc for assembly and linking.

Features

The compiler implements a language quite similar to C, but there are some major differences. The following list is probably not exhaustive, but attempts to be.

  • Features added:
    • Lambdas
    • Simple module system
    • bool type, true and false are boolean literals
  • Different semantics:
    • Unified variable/typedef/struct/union/enum namespace
    • Logical operators (||, &&, and !) return bool
    • va_* are implemented as builtins, and including stdarg.h has no effect
    • The types usually defined by stdint.h are also builtins
    • Empty prototype parentheses means zero arguments (as in C++)
    • Operator precedence simplified
    • Ternary (?:) can return lvalues (as in C++)
    • Comma (,) can return a constant expression
    • Enums are always complete types and can be forward referenced
    • String literals are const char*
  • The features of C99 and C11 supported:
    • Anonymous structs/unions
    • Compound literals and designated initializers
    • Intermingled code/declarations including for-loop declarations
    • C++ comments
  • Features removed:
    • Preprocessor (treated as line comments)
    • switch
    • Bitfields
    • Implicit casts / coercion between integral types
    • goto and labels
  • No support for (yet?):
    • Floating point types
    • Unsigned integers
    • short and long
    • Wide characters
    • volatile qualifier
    • register storage class

The compiler is self hosting, on branch selfhosting (see Building). As the compiler matures experimental additions to the language considered are:

  • Type polymorphism as in ML/Haskell
  • Closures
  • Option types (option in ML, Maybe in Haskell)
  • Algebraic types (a more general solution to option types)

Extensions

[] (parameters, ...) {body}
[] (parameters, ...) (expression)

The syntax for lambdas is similar to that in C++. Closures are not implemented yet, so the opening square brackets must be empty.

A second form with parentheses instead of curly braces is also allowed. This takes a single expression which is the return value of the function.

In either case the result of the expression is a function pointer whose return type is inferred from any and all return expressions in the body. This differs from C++, where there is a special implementation defined type incompatible with normal functions.

See <fcc>/tests/lambda.c and swap.c.

Output

The compiler generates assembly for x86 (32-bit) CPUs. 64-bit AMD64 is experimental. The compiler does very little optimization before emitting straight to assembly.

To run the 32-bit code on a 64-bit OS, you will probably need to install 32-bit runtime libraries (try sudo apt-get install gcc-multilib).

The ABI is largely compatible GCC and Clang's. This means that most code compiled with one compiler can be successfully linked and run with code compiled with another. However, passing and returning structs as values will often not work, for reasons I'm not totally clear on.

Building

Building the compiler is simple, just call make in the root directory:

cd <fcc>
make CC=clang

This puts an fcc binary in <fcc>/bin/$CONFIG

Makefile parameters (all optional):

  • OS=[linux windows], default: linux
  • ARCH=[32 64], default: 32
  • CONFIG=[debug profiling release], default: release
  • CC=[path to C11 compiler], default: gcc
  • FCC=[path to FCC binary], default: bin/$CONFIG/fcc
  • VALGRIND, default: true (if Valgrind is found)

Makefile targets:

  • all clean print tests print-tests selfhost

The selfhost target fully compiles FCC using an existing copy, bin/$CONFIG/fcc. The result goes in bin/self/fcc. As the language accepted by the compiler differs slightly to C, only the selfhosting branch will compile under both FCC and a regular C compiler. The modifications required to be a polyglot are quite minimal, mostly just concerning the module system and explicit type coercion.

If found on the system the compiler will be run against Valgrind when building tests. To disable this, set VALGRIND="".

Use the FCC parameter to choose which copy of FCC to run the tests against. Simply specifying FCC=bin/self/fcc as a parameter for the tests-all target will trigger a self-host. Remember to do this only on the selfhosting branch.

Running

The command line interface is similar to that of a POSIX C compiler, such as GCC.

Usage: fcc [--help] [--version] [-csS] [-I <dir>] [-o <file>] <files...>

  • -I <dir> Add a directory to be searched for headers
  • -c Compile and assemble only, do not link
  • -S Compile only, do not assemble or link
  • -s Keep temporary assembly output after compilation
  • -o <file> Output into a specific file
  • --help Display command line information
  • --version Display version information

License

Unless otherwise stated, a source file in this package is under the GNU GPL V3 license.

Fedjmike's C Compiler Copyright (C) 2012-2015 Sam Nipps

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].