All Projects → martinferianc → C90Compiler-EIE2

martinferianc / C90Compiler-EIE2

Licence: MIT license
C90 to MIPS I Compiler done as a coursework for EE2-15

Programming Languages

C++
36643 projects - #6 most used programming language
assembly
5116 projects
Yacc
648 projects
c
50402 projects - #5 most used programming language
Lex
420 projects
CMake
9771 projects

Projects that are alternatives of or similar to C90Compiler-EIE2

asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (+53.33%)
Mutual labels:  mips, ast
Go Ast Book
📚 《Go语法树入门——开启自制编程语言和编译器之旅》(开源免费图书/Go语言进阶/掌握抽象语法树/Go语言AST/凹语言)
Stars: ✭ 4,034 (+26793.33%)
Mutual labels:  flex, ast
monkeylearn-php
Official PHP client for the MonkeyLearn API. Build and consume machine learning models for language processing from your PHP apps.
Stars: ✭ 47 (+213.33%)
Mutual labels:  language-processing
VueFlex
A flexbox grid system.
Stars: ✭ 13 (-13.33%)
Mutual labels:  flex
TypeInferencer
Algorithm W and Algorithm M in F#
Stars: ✭ 33 (+120%)
Mutual labels:  ast
puma
Meta-programming framework for JavaScript based on LayerD concepts
Stars: ✭ 30 (+100%)
Mutual labels:  ast
coAST
Universal and language-independent abstract syntax tree
Stars: ✭ 30 (+100%)
Mutual labels:  ast
gradle2kts
Gradle Groovy to Gradle Kotlin conversion tool - discontinued spike
Stars: ✭ 34 (+126.67%)
Mutual labels:  ast
dedupimport
Deduplicate named/unnamed imports that have the same import path in Go files
Stars: ✭ 20 (+33.33%)
Mutual labels:  ast
bison-flex-cpp-example
GNU Bison and GNU Flex C++ example
Stars: ✭ 145 (+866.67%)
Mutual labels:  flex
babel-codemod-example
An example of how to use babel as a codemod
Stars: ✭ 24 (+60%)
Mutual labels:  ast
eval-estree-expression
Safely evaluate JavaScript (estree) expressions, sync and async.
Stars: ✭ 22 (+46.67%)
Mutual labels:  ast
decaf-rs
The Decaf compiler, written in Rust
Stars: ✭ 43 (+186.67%)
Mutual labels:  mips
calculator
基于flex&goyacc实现的计算器
Stars: ✭ 23 (+53.33%)
Mutual labels:  flex
redot
Graphviz dot file processor powered by plugins based on @unifiedjs
Stars: ✭ 60 (+300%)
Mutual labels:  ast
Code-VueWapDemo
“Vue教程--Wap端项目搭建从0到1”的源码
Stars: ✭ 19 (+26.67%)
Mutual labels:  flex
fs-make
Make a folder structure from its description
Stars: ✭ 14 (-6.67%)
Mutual labels:  flex
pytamil
பைந்தமிழ் (pytamil) library is intended to be used in analysis of tamil literary work. A wealth of knowledge is hidden in old literature. They are time machines to past. Ever wondered what is the popular color or food in tamil speaking world in 500AD. The answer is hidden in literature. With right computer tools it becomes possible for us to dig…
Stars: ✭ 41 (+173.33%)
Mutual labels:  language-processing
gogo
Go to MIPS compiler, written in Go. Course project for Compiler Design (CS335).
Stars: ✭ 25 (+66.67%)
Mutual labels:  mips
bredon
A modern CSS value compiler in JavaScript
Stars: ✭ 39 (+160%)
Mutual labels:  ast

C90 to MIPS I Compiler

This is a C90 (ANSI C) to MIPS I compiler. The compiler uses Flex for the lexer, Bison for the parser and virtual functions for the assembly code generation.

The repository also contains parser and lexer in separate directories under the /src directory. It also contains a plenty of tests for all three programs.

More information on the design of the compiler can be seen in the docs.

Specification

This piece of work was done in partial fulfilment of a language processors module (EE2-15). The specification for the coursework can be found here.

Currently implemented features

1 - [x] Local variables

2 - [x] Integer arithmetic

3 - [x] While

4 - [x] IfElse, SwitchCase

5 - [x] For

6 - [x] Function calls

7 - [x] Arrays

8 - [x] Pointers

9 - [ ] Strings

10 - [ ] Structures

11 - [ ] Floating-point

Building and Running

It must be run on an environment with Flex and Bison installed, a vagrant file configured to the correct target environment has been included. To deploy the environment you can run vagrant up, vagrant ssh, cd /vagrant/ and finish with exit.

The compiler can built by doing:

make bin/c_compiler

The compiler accepts valid C code through stdin and it will return MIPS 1 code through stdout, for example, if we create a file test.c and we store it under /test/c_compiler/test:

int main(){
  int j = 10;
  int i = 2;
  return i + j;
}

and then run it via the testing shell script:

./test_compiler_unit.sh 1

the file output.s will contain:

	.section .mdebug.abi32
	.previous
	.nan	legacy
	.module	fp=xx
	.module	nooddspreg
	.abicalls
	.text

	.align	2
	.globl	main
	.set	nomips16
	.set	nomicromips
	.ent	main
	.type	main, @function
main:
	addiu $sp,$sp,-80
	sw $31,76($sp)	# Storing return address
	sw $fp,72($sp)	# Storing current frame pointer
	move $fp,$sp
	move $t9,$0
	move $2,$0
	move $t0,$0	 #Making Binding j
	sw $t0,0($fp)	 #Saving variable: j
	lw $t0,0($fp)	 #Loading variable: j
	li $t1,10	 #Value to register 10
	move $t0,$t1	 #Assigning value 
	sw $t0,0($fp)	 #Saving variable: j
	move $t1,$0	 #Making Binding i
	sw $t1,4($fp)	 #Saving variable: i
	lw $t1,4($fp)	 #Loading variable: i
	li $t2,2	 #Value to register 2
	move $t1,$t2	 #Assigning value 
	sw $t1,4($fp)	 #Saving variable: i
	move $2,$0
	lw $t1,4($fp)	 #Loading variable: i
	lw $t0,0($fp)	 #Loading variable: j
	add $2,$t1,$t0	 #Adding values 
	b $EXIT_FUNC1
	nop
	 #Releasing binding: i
	 #Releasing binding: j
	 #==Clearing scope context==
$EXIT_FUNC1:
	 #==Loading used variables 16-23==
	move $sp,$fp
	lw $fp,72($sp)	# Loading current frame pointer
	lw $31,76($sp)	# Loading return address
	addiu $sp,$sp,80
	j $31
	nop

	.set	macro
	.set	reorder
	.end	main
	.size	main, .-main
	.ident	"GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609"

and it will also produce a pretty-print XML structure of the AST:

<Program>
    <Function id="main" type="int">
        <Scope>
            <BinaryExpression>
                <Left>
                    <Variable id="j" type="int"/>
                </Left>
                <Operator op="="/>
                <Right>
                    <NumberInt value="10"/>
                </Right>
            </BinaryExpression>
            <BinaryExpression>
                <Left>
                    <Variable id="i" type="int"/>
                </Left>
                <Operator op="="/>
                <Right>
                    <NumberInt value="2"/>
                </Right>
            </BinaryExpression>
            <Return>
                <BinaryExpression>
                    <Left>
                        <Variable id="i" type=""/>
                    </Left>
                    <Operator op="+"/>
                    <Right>
                        <Variable id="j" type=""/>
                    </Right>
                </BinaryExpression>
            </Return>
        </Scope>
    </Function>
</Program>

Credits

  • All code was written by Martin Ferianc in 2017.
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].