All Projects → noti0na1 → socc

noti0na1 / socc

Licence: MIT license
Simple C Compiler in OCaml

Programming Languages

ocaml
1615 projects
assembly
5116 projects
c
50402 projects - #5 most used programming language
Makefile
30231 projects
Standard ML
205 projects
shell
77523 projects

Projects that are alternatives of or similar to socc

re-typescript
An opinionated attempt at finally solving typescript interop for ReasonML / OCaml.
Stars: ✭ 68 (+65.85%)
Mutual labels:  menhir, lexer
ocean
Programming language that compiles into a x86 ELF executable.
Stars: ✭ 164 (+300%)
Mutual labels:  lexer
ta-rust
A mirror for the textadept module ta-rust hosted in bitbucket
Stars: ✭ 21 (-48.78%)
Mutual labels:  lexer
fayrant-lang
Simple, interpreted, dynamically-typed programming language
Stars: ✭ 30 (-26.83%)
Mutual labels:  lexer
compiler
Implementing a complete Compiler for a simple C-like language using the C-tools Flex and Bison
Stars: ✭ 106 (+158.54%)
Mutual labels:  lexer
FoxOS
The FoxOS main repository
Stars: ✭ 48 (+17.07%)
Mutual labels:  x64
intellij-cue
IntelliJ support for the CUE language.
Stars: ✭ 23 (-43.9%)
Mutual labels:  lexer
malluscript
A simple,gentle,humble scripting language for mallus, based on malayalam memes.
Stars: ✭ 112 (+173.17%)
Mutual labels:  lexer
parle
Parser and lexer for PHP
Stars: ✭ 68 (+65.85%)
Mutual labels:  lexer
bshift
Compiler for a language called bshift
Stars: ✭ 15 (-63.41%)
Mutual labels:  lexer
docker-airdcpp-webclient
AirDC++ Web Client Docker image
Stars: ✭ 24 (-41.46%)
Mutual labels:  x64
re avkmgr
逆向小红伞杀毒软件驱动——avkmgr
Stars: ✭ 63 (+53.66%)
Mutual labels:  x64
JavaScript-compiler
编程语言的本质:语言只是一串字符,我们认为它是什么,它就可以是什么
Stars: ✭ 51 (+24.39%)
Mutual labels:  lexer
FLexer
Simple Lexer and Parser in F#
Stars: ✭ 22 (-46.34%)
Mutual labels:  lexer
Corth
It's like Porth, but in C++. Yep, we're going full circle.
Stars: ✭ 17 (-58.54%)
Mutual labels:  x64
fdtd3d
fdtd3d is an open source 1D, 2D, 3D FDTD electromagnetics solver with MPI, OpenMP and CUDA support for x86, arm, arm64 architectures
Stars: ✭ 77 (+87.8%)
Mutual labels:  x64
Reverse-Engineering
A FREE comprehensive reverse engineering tutorial covering x86, x64, 32-bit ARM & 64-bit ARM architectures.
Stars: ✭ 7,234 (+17543.9%)
Mutual labels:  x64
storm-engine
Game engine behind Sea Dogs, Pirates of the Caribbean and Age of Pirates games.
Stars: ✭ 636 (+1451.22%)
Mutual labels:  x64
MonkeyLang.jl
"Writing an Interpreter in GO" and "Writing a Compiler in GO" in Julia.
Stars: ✭ 30 (-26.83%)
Mutual labels:  lexer
mcc
Mini C Compiler
Stars: ✭ 18 (-56.1%)
Mutual labels:  x64

SOCC - Simple C Compiler in OCaml

SOCC is a simple C Compiler written in OCaml using ocamllex and menhir.

Install

Dependences: core, ppx_jane

opam update
opam install core ppx_jane
git clone [email protected]:noti0na1/socc.git
cd socc
make

The executable file is main.native

Usage

# use gcc to preprocess the source code
gcc -E test.c -o test.e.c
./main.native < test.e.c > test.s
gcc test.s -o test
./test

Example

There are 4 buildin functions for test:

  • void println(int)
  • int readi()
  • void *malloc(size_t)
  • void free(void *)
int main() {
    int *pi = malloc(sizeof(int));
    int max = readi();
    for (int i = 0; i < max; i += 2) {
        println(i);
        *pi += i;
    }
    println(*pi);
    free(pi);
    return 0;
}
.LC0:
	.string "%d\n"
	.globl println
println:
	movq	%rdi, %rsi
	movq	$0, %rax
	movl	$.LC0, %edi
	jmp	printf
.LC1:
	.string "%d"
	.globl readi
readi:
	subq	$24, %rsp
	movl	$.LC1, %edi
	movq	$0, %rax
	leaq	12(%rsp), %rsi
	movl	$0, 12(%rsp)
	call	__isoc99_scanf
	movl	12(%rsp), %eax
	addq	$24, %rsp
	ret
	.globl main
main:
	pushq	%rbp
	movq	%rsp, %rbp
	movq	$4, %rax
	movq	%rax, %rdi
	call	malloc
	pushq	%rax
	call	readi
	pushq	%rax
	movl	$0, %eax
	pushq	%rax
LmainFORA0:
	movq	-24(%rbp), %rax
	pushq	%rax
	movq	-16(%rbp), %rax
	movq	%rax, %rcx
	popq	%rax
	cmpl	%ecx, %eax
	movl	$0, %eax
	setl	%al
	cmpl	$0, %eax
	je	LmainFORC0
	movq	-24(%rbp), %rax
	movq	%rax, %rdi
	call	println
	movq	-8(%rbp), %rax
	pushq	%rax
	movq	-8(%rbp), %rax
	movq	(%rax), %rax
	pushq	%rax
	movq	-24(%rbp), %rax
	movq	%rax, %rcx
	popq	%rax
	addl	%ecx, %eax
	movq	%rax, %rcx
	popq	%rax
	movl	%ecx, (%rax)
	addq	$0, %rsp
LmainFORB0:
	movq	-24(%rbp), %rax
	pushq	%rax
	movl	$2, %eax
	movq	%rax, %rcx
	popq	%rax
	addl	%ecx, %eax
	movq	%rax, -24(%rbp)
	jmp	LmainFORA0
LmainFORC0:
	addq	$8, %rsp
	movq	-8(%rbp), %rax
	movq	(%rax), %rax
	movq	%rax, %rdi
	call	println
	movq	-8(%rbp), %rax
	movq	%rax, %rdi
	call	free
	movl	$0, %eax
	leave
	ret

In: 23

Out:

0
2
4
6
8
10
12
14
16
18
20
22
132

Structure

  • socc/src source code
    • ast.ml abstract syntax tree
    • parser.mly parser
    • lexer.mll lexer
    • context.ml
    • x64.ml X64 assembly
    • templib.ml temporery lib (println, readi)
    • generate.ml code generator
    • util.ml utils
    • main.ml program entry
  • socc/test some test files

Note

Core is used across the project

The output assembly is for x64 Linux platform

TODO

Short Term

  • follow the C11 to edit lexer
  • add array type and array operate
  • implement ++ and --
  • support comments
  • add more type, implement struct, union, enum
  • complete type system
  • optimize stack usage
  • improve parser further more

Long Term

  • add error, warning print ...
  • add semantic check pass
  • add intermediate lang

References

Writing a C Compiler

nlsandler/nqcc

Parsing with OCamllex and Menhir

Lexer and parser generators (ocamllex, ocamlyacc)

x64_cheatsheet

Licence

SOCC is distributed under the terms of MIT License

Copyright (c) 2018 noti0na1

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