An end-to-end compiler for a new custom programming language, Cold
.
Cold
has a C-like syntax based on ANSI C and also supports some object-oriented features.
Regarding the name of our language, since it is C-like but based on an older version, hence C-old
. Also since some object-oriented features are supported, hence Cold
can also be thought of as "C
but with O
bject-oriented L
anguage D
esign features".
- Source language :
Cold
- Implementation language :
C++
- Intermediate Representaion :
3AC
- Target Language :
x86 32-bit
For learning ANSI C syntax, the perfect book obviously is K&R C Book.
The C Lex specification is taken from here.
The C Yacc specification is taken from here.
Syntax reference for x86 assembly has been mainly taken from here. For floating-point operations, x87 instruction set has been used whose reference has taken mainly from here.
All basic features required have been supported (including structs, library functions and static).
Also pointers and arrays work for all data types including char and float as well as for structs and classes.
Advanced features :
- recursive function call
- classes and objects (including '.' and '->' access)
- inheritance
- function call with variable arguments (including va_list, va_start, va_end)
- dynamic memory allocation (and other standard library functions)
- command line input
- public, private and protected keywords
- typedef
- enum, union
- file manipulation
- until loop
- multi-level pointers
- multi-dimensional arrays
- backpatching and short-circuit evaluation
Extra Advanced Features :
- function overloading
- const type qualifier
- multi-level inheritance
- modified operator precedence (more details in
docs/operator_precedence.md
.)
Extra Optimizations :
- Machine-independent optimizations :
- Constant folding
- Dead code elimination
- Constant propagation
- Strength reduction
- Machine-dependent optimizations:
- Efficient register allocation using next use information and register descriptors
- Peephole optimizations (like strength reduction and algebraic simplification)
- Removing redundant stores and assignments
- Efficient spilling of registers (via minimum number of live variable heuristic)
Some syntax changes and implementation related details have been documented in docs/lang_details.md
.
Error handling
: Multiple errors in same program can be handled (shown in testcases) and descriptive error messages are being printed in terminal.
Run for Ubuntu/Linux. Make sure you have g++
installed.
sudo apt install flex bison build-essential nasm
git clone https://github.com/Project-Group-Compiler/Cold_Compiler.git
cd Cold_Compiler
-
Build the compiler
Run the following command in the root directory:
make
-
Run script for executing testcases. The generated assembly code and executables will get saved in
outputs
directory.chmod +x run.sh ./run.sh
Alternatively, generate assembly for a single input file using (specify input file as the first argument):
bin/compiler <input_file> [options]
Extra Options:
-h, --help Show this help message and exit
-l, --lex Print lexical analysis table
-a, --ast Print abstract syntax tree as dot file
-s, --symtab Print symbol tables
-d, --debug Print debug trace
-t, --tac Print three address code
-f, --force Forcefully continue even if errors are present
-O1 Enable machine-independent optimizations
-O2 Enable machine-dependent optimizations
-O3 Enable all optimizations
nasm -f elf32 <file_name>.asm # for assembling
gcc -m32 -no-pie -z noexecstack <file_name>.o -o <some_name> # for linking
Shortcut :
chmod +x coldc.sh
./coldc.sh <input_file> [cold-compiler-options]
- Meet Sindhav
- Mohammed Haaziq
- Roopam Taneja
- Shubham Kumar Verma