This is a compiler written from scratch in C

Related tags

Compilers C-Compiler
Overview

C Compiler

This is a compiler written from scratch in C, with fully supporting C18 as a goal. It can currently compile itself, and most simple programs. Some features are missing, but most have some level of support (including variable length arrays and _Generic.) Avert your eyes from the source code. It is not very nice.

You can read the tests or (if you dare) the source code to get a sense of what this compiler currently supports.

Build instructions

Just run

make

to build.

Compilation instructions

cc $INPUT $OUTPUT -Iinclude_directory/ -DDEFINITION

The output will be an x86-64 assembly file with GAS syntax that can be assembled with your assembler of choice.

Self compilation

For self compiling, create a directory called musl/ and put the musl header there. Alternatively any other C standard library headers should (ideally) work. And run

./self_compile.sh

The compiler will then compile itself, and the resulting compiler will in turn also compile itself, and so on in an infinite loop. Assembly output from the first generation compiler will be put in asm/, and the output from the following generations is put in asm2/. Currently the outputs of all generations of the compiler are identical (anything else would be a bug.)

Testing

There is a very basic test suite implemented. It runs all *.c files in tests/ and aborts if any of them fails, either during compilation, or runtime. It also self-compiles twice, and checks that the outputs are identical. Use the following command to run the tests:

./run_tests.sh
You might also like...
Microvm is a virtual machine and compiler
Microvm is a virtual machine and compiler

The aim of this project is to create a stack based language and virtual machine for microcontrollers. A mix of approaches is used. Separate memory is used for program and variable space (Harvard architecture). An interpreter, virtual machine and compiler are available. A demostration of the interpreter in action is presented below.

yadcc - Yet Another Distributed C++ Compiler

Yet Another Distributed C++ Compiler. yadcc是一套腾讯广告自研的分布式编译系统,用于支撑腾讯广告的日常开发及流水线。相对于已有的同类解决方案,我们针对实际的工业生产环境做了性能、可靠性、易用性等方面优化。

Aheui JIT compiler for PC and web

아희짓 개요 아희짓은 아희 언어를 위한 JIT (Just in Time) 컴파일러입니다. 어셈블러와 유틸 라이브러리외에 외부 라이브러리에 전혀 의존하지 않고 JIT을 바닥부터 구현합니다. 지원 환경 64비트 windows, mac, linux (x86 아키텍쳐) 웹어셈

C implementation of the Tiny BASIC compiler found in an article by Dr. Austin Henley

Teeny Tiny Basic A C implementation of the Tiny BASIC compiler found in this article and this github repo by Dr. Austin Henley. I did pretty well in A

bcc is an interactive compiler of a language called b.

bcc is an interactive compiler of a language called b.

mrcceppc is a reimplementation project for the Metrowerks mwcceppc compiler.

Compiler | mrcceppc mrcceppc is a reimplementation project for the Metrowerks mwcceppc compiler. Compiling Run generate_{version}.bat for which versio

A C header that allow users to compile brainfuck programs within a C compiler.

brainfuck.h A C header that allow users to compile brainfuck programs within a C compiler. You can insert the header into the top of your brainfuck so

Gilbraltar is a version of the OCaml compiler to be able to build a MirageOS for RaspberryPi 4.

Gilbraltar is a version of the OCaml compiler to be able to build a MirageOS for RaspberryPi 4. It's a work in progress repository to provide a dune's toolchain (as ocaml-freestanding) specialized for Raspberry Pi 4.

 NaiveCC: a compiler frontend for a subset of C
NaiveCC: a compiler frontend for a subset of C

NaiveCC: a compiler frontend for a subset of C This is a toy compiler frontend for a subset of the C programming language based on the LR(1) parsing t

Comments
  • hi,this base64 run erro

    hi,this base64 run erro

    void base64_encode(char *dst, const unsigned char *src, int len) { static const char b64[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };

    for (int i = 0; i < len; i += 3, dst += 4) {
    	unsigned long x = (src[i] & 0xfful) << 16;
    	dst[3] = i + 2 >= len ? '=' : b64[(x |= src[i + 2] & 0xfful) & 0x3f];
    	dst[2] = i + 1 >= len ? '=' : b64[(x |= (src[i + 1] & 0xfful) << 8) >> 6 & 0x3f];
    	dst[1] = b64[x >> 12 & 0x3f];
    	dst[0] = b64[x >> 18];
    }
    *dst = '\0';
    

    }

    int base64_decode(unsigned char *dst, const char *src) { static const char b64[] = { ['A'] = 0, ['B'] = 1, ['C'] = 2, ['D'] = 3, ['E'] = 4, ['F'] = 5, ['G'] = 6, ['H'] = 7, ['I'] = 8, ['J'] = 9, ['K'] = 10, ['L'] = 11, ['M'] = 12, ['N'] = 13, ['O'] = 14, ['P'] = 15, ['Q'] = 16, ['R'] = 17, ['S'] = 18, ['T'] = 19, ['U'] = 20, ['V'] = 21, ['W'] = 22, ['X'] = 23, ['Y'] = 24, ['Z'] = 25, ['a'] = 26, ['b'] = 27, ['c'] = 28, ['d'] = 29, ['e'] = 30, ['f'] = 31, ['g'] = 32, ['h'] = 33, ['i'] = 34, ['j'] = 35, ['k'] = 36, ['l'] = 37, ['m'] = 38, ['n'] = 39, ['o'] = 40, ['p'] = 41, ['q'] = 42, ['r'] = 43, ['s'] = 44, ['t'] = 45, ['u'] = 46, ['v'] = 47, ['w'] = 48, ['x'] = 49, ['y'] = 50, ['z'] = 51, ['0'] = 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ['+'] = 62, ['/'] = 63, ['='] = 0, }; unsigned long x; int i, c, len, pad;

    for (i = 0, x = 0, len = 0, pad = 0; src[i]; ++i) {
    	c = src[i];
    	if (c == '=' && (!src[i + 1] || (src[i + 1] == '=' && !src[i + 2])))
    		++pad;
    	else if (c >= sizeof(b64) || (!b64[c] && c != 'A'))
    		return 0;
    	x = x << 6 | b64[c];
    	if (i % 4 == 3) {
    		dst[len + 2] = x & 0xff, x >>= 8;
    		dst[len + 1] = x & 0xff, x >>= 8;
    		dst[len + 0] = x & 0xff;
    		len += 3;
    	}
    }
    if (i % 4 != 0)
    	return 0;
    return len - pad;
    

    }

    int main () {

    char base64_src[100] = { 0 }; base64_src[0] = '9';
    char base64_dst[100] = { 0 };
    char base64_nop[100] = { 0 };
    base64_encode(base64_dst, base64_src, 1);// run ok
    
    base64_decode(base64_nop, base64_dst);// run erro
    
    return 0;
    

    }

    opened by icyfox168168 688
  • -dhalf-assemble

    -dhalf-assemble

    -dhalf-assemble -fabi=ms -fdebug-stack-size=0 -fcmodel=large

    If this parameter is added, the test examples that get the correct results in the previous compilation cannot get the correct results Has the work behind this parameter been completed?

    opened by icyfox168168 12
Owner
null
Compiler Design Project: Simulation of front-end phase of C Compiler involving switch-case construct.

CSPC41 Compiler Design Project Assignment Compiler Design Project: Simulation of front-end phase of C Compiler involving switch-case construct. Using

Adeep Hande 1 Dec 15, 2021
A compiler written in C++ to convert .hoi code to hoi4 code

What is HC4? HC4 is a compiler that converts .hoic filenames to Hearts of Iron IV's .txt. Usage Use hc4 in the terminal (./hc4 if on Unix) and it will

SaCode 1 Jul 31, 2022
A C compiler written in Zig.

Aro A C compiler with the goal of providing fast compilation and low memory usage with good diagnostics. Currently it can preprocess, parse and semant

Veikka Tuominen 431 Jan 1, 2023
A Small C Compiler

8cc C Compiler Note: 8cc is no longer an active project. The successor is chibicc. 8cc is a compiler for the C programming language. It's intended to

Rui Ueyama 5.8k Jan 8, 2023
A C compiler for LLVM. Supports C++11/14/1z C11

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies

LLVM 17.5k Jan 8, 2023
GNU Prolog is a native Prolog compiler

GNU Prolog is a native Prolog compiler with constraint solving over finite domains (FD)

Daniel Diaz 64 Dec 13, 2022
Take your first step in writing a compiler.

first-step Take your first step in writing a compiler. Building from Source Before building first-step, please make sure you have installed the follow

PKU Compiler Course 28 Aug 20, 2022
NCC is an ANSI/ISO-compliant optimizing C compiler.

The compiler is retargetable by design, but, at present, it only produces binaries for Linux/x86_64. As the compiler ABI differs somewhat from the System V ABI used by Linux, its code cannot be linked against Linux system libraries. It does, however, provide its own (incomplete) standard ANSI/Posix C library.

Charles E. Youse 0 Apr 1, 2022
nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines.

nanoc is a tiny subset of C and a tiny compiler that targets 32-bit x86 machines. Tiny? The only types are: int (32-bit signed integer) char (8-

Ajay Tatachar 19 Nov 28, 2022
Smaller C is a simple and small single-pass C compiler

Smaller C is a simple and small single-pass C compiler, currently supporting most of the C language common between C89/ANSI C and C99 (minus some C89 and plus some C99 features).

Alexey Frunze 1.2k Jan 7, 2023