From 1374424a57d1026a9496fe3f20de3f8cddcf2b6f Mon Sep 17 00:00:00 2001 From: namanbiyani Date: Thu, 13 May 2021 22:54:36 +0530 Subject: [PATCH 1/4] added newton raphson --- tests/final/test22_newtonraphson.c | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/final/test22_newtonraphson.c diff --git a/tests/final/test22_newtonraphson.c b/tests/final/test22_newtonraphson.c new file mode 100644 index 0000000..f3003d5 --- /dev/null +++ b/tests/final/test22_newtonraphson.c @@ -0,0 +1,35 @@ +float f(float x) +{ + return x*log(x)/log(10) - 1.2; +} +float df (float x) +{ + return log(x)/log(10) + 0.43429; +} +float abs(float x) +{ + if(x > 0) + return x; + return -x; +} +int main() +{ + int itr, maxmitr; + float h, x0, x1, allerr; + printf("\nEnter x0, allowed error and maximum iterations\n"); + scanf("%f %f %d", &x0, &allerr, &maxmitr); + for (itr=1; itr<=maxmitr; itr++) + { + h=f(x0)/df(x0); + x1=x0-h; + printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1); + if (abs(h) < allerr) + { + printf("After %3d iterations, root = %8.6f\n", itr, x1); + return 0; + } + x0=x1; + } + printf(" The required solution does not converge or iterations are insufficient\n"); + return 1; +} \ No newline at end of file From 24e8e741e26b4a06c89e620642767d44c28e6abf Mon Sep 17 00:00:00 2001 From: nirmal Date: Thu, 13 May 2021 22:59:33 +0530 Subject: [PATCH 2/4] FileIO updated --- src/lib.py | 2 ++ tests/final/test18_fileio.c | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/lib.py b/src/lib.py index 9f9c7ff..743bbcb 100644 --- a/src/lib.py +++ b/src/lib.py @@ -23,4 +23,6 @@ void* malloc(int size); void free(void *ptr); + +int strlen(char *s); ''' \ No newline at end of file diff --git a/tests/final/test18_fileio.c b/tests/final/test18_fileio.c index 3f52737..b67ec76 100644 --- a/tests/final/test18_fileio.c +++ b/tests/final/test18_fileio.c @@ -1,12 +1,14 @@ int main() { - int fd_out, x; - char file_name[10] = "a.txt"; - char msg[20] = "hello world"; - - fd_out = open(file_name, 2); - x = write(fd_out, msg , strlen(msg)+1); - - close(fd_out); + int O_CREAT = 64; + int O_RDWD = 2; + int O_RDONLY = 0; + int O_WRONLY = 1; + int fd, x; + char msg[20] = "hello world"; + fd = open("fileio.txt", O_CREAT | O_RDWD); + x = write(fd, msg , strlen(msg)+1); + printf("length of buffer: %d", strlen(msg)+1); + close(fd); } \ No newline at end of file From f022e7e105223bb6d08a6fd4de1c7616b5329d98 Mon Sep 17 00:00:00 2001 From: namanbiyani Date: Thu, 13 May 2021 23:08:59 +0530 Subject: [PATCH 3/4] update readme --- README.md | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cf0a027..bf41a06 100644 --- a/README.md +++ b/README.md @@ -21,30 +21,42 @@ $ make $ make clean # For removing the build ``` -### For generating dot file (parser) and dump of symbol table in `csv` format -```bash -$ ./bin/parser -o myAST.dot /tests/helloworld.c +###For running the compiler: -# For generates a postscript program containing the drawing of the tree -$ dot -Tps myAST.dot -o myAST.ps -# Alternatively, for generating .png file -$ ./bin/parser -o myAST.dot --png /tests/helloworld.c +```bash +./bin/gcc_lite [-o outfile] ` eg. `./bin/parser tests/helloworld.c` ``` - -### For more informations -``` -$ ./bin/parser -h +### For more informations about usage +```bash +usage: gcc_lite [-h] [--input INPUT] [-d] [-o OUT] [-l] [-D] [-p] [-I] [--sym] [-S] [-R] + +Compiler for C programs + +optional arguments: + -h, --help show this help message and exit + --input INPUT C program file to parse + -d, --debug Generate assembly with extra information (for debugging purposes) + -o OUT, --out OUT File name to store generated executable + -l, --lex Store output of lexer + -D, --dot Generate AST graph as DOT format + -p, --png Generate AST graph as png format + -I, --ir Dump the generated Intermediate representation + --sym Dump the symbol table + -S, --asm Store the generated assembly file + -R, --exec Execute the generated program ``` + ## Features -### Basic +### Basic Features - Native data types: Int, Char, Float - Variables and Expressions - Conditional: if, if-else, switch-case - Loops: for, while, do-while - Break, Continue +- Switch Case - Arrays: Single and multidimensional - Input,output - Functions, recursion @@ -56,9 +68,14 @@ $ ./bin/parser -h - Typedef ### Advanced Features -- Dynamic memory allocation: -- Register allocation optimization -- +- Dynamic memory allocation (malloc, free) +- Register allocation optimization using conditional spilling +- File Handling +- Fork and exec system calls +- Storage Optimization for Char and string +- Nested Struct and Array Initializers +- Multidimensional arrays as function parameters +- Short circuit expression evaluation ## Members - [Dev Chauhan](https://github.com/dev-chauhan) From 50301f6d69eca05d61faf0ccf67ce9e1f76c902e Mon Sep 17 00:00:00 2001 From: nirmal Date: Thu, 13 May 2021 23:25:58 +0530 Subject: [PATCH 4/4] Update args in gcc_lite --- README.md | 13 +++++++------ src/gcc_lite.py | 30 +++++++++++++++--------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index bf41a06..8a3a490 100644 --- a/README.md +++ b/README.md @@ -17,25 +17,27 @@ $ pip install --ignore-installed -r ./requirements.txt ``` ### For building executable ```bash -$ make $ make clean # For removing the build +$ make ``` -###For running the compiler: +### For running the compiler: ```bash -./bin/gcc_lite [-o outfile] ` eg. `./bin/parser tests/helloworld.c` +./bin/gcc_lite [-o outfile] tests/helloworld.c ``` ### For more informations about usage ```bash -usage: gcc_lite [-h] [--input INPUT] [-d] [-o OUT] [-l] [-D] [-p] [-I] [--sym] [-S] [-R] +usage: gcc_lite [-h] [-d] [-o OUT] [-l] [-D] [-p] [-I] [--sym] [-S] [-R] input Compiler for C programs +positional arguments: + input C program file to compile + optional arguments: -h, --help show this help message and exit - --input INPUT C program file to parse -d, --debug Generate assembly with extra information (for debugging purposes) -o OUT, --out OUT File name to store generated executable -l, --lex Store output of lexer @@ -47,7 +49,6 @@ optional arguments: -R, --exec Execute the generated program ``` - ## Features ### Basic Features diff --git a/src/gcc_lite.py b/src/gcc_lite.py index 644fc4d..259432f 100755 --- a/src/gcc_lite.py +++ b/src/gcc_lite.py @@ -15,8 +15,8 @@ def arg_parser(): argparser = ArgumentParser(prog='gcc_lite', description='Compiler for C programs') - argparser.add_argument('--input', type=str, - help='C program file to parse', default='../test/helloworld.c') + argparser.add_argument('input', type=str, + help='C program file to compile') argparser.add_argument('-d', '--debug', action="store_true", help='Generate assembly with extra information (for debugging purposes)') @@ -101,12 +101,12 @@ def arg_parser(): graph.write_png(png_file) # try generating IR and symbol table - # try: - syntax_tree.gen() -# except: - # print(bcolors.BOLD+f'{lexer.filename}:0:0'+bcolors.ENDC,end='') - # print(bcolors.FAIL+' IR Error'+bcolors.ENDC) - # exit(1) + try: + syntax_tree.gen() + except: + print(bcolors.BOLD+f'{lexer.filename}:0:0'+bcolors.ENDC,end='') + print(bcolors.FAIL+' IR Error'+bcolors.ENDC) + exit(1) if args.sym: sym_file = ofile + '.csv' @@ -116,13 +116,13 @@ def arg_parser(): tac.dump_code(ir_file) # try generating assembly from IR - # try: - asm = AssemblyGen(tac.func_code, debug=args.debug) - asm.gen_assembly() - # except: - # print(bcolors.BOLD+f'{lexer.filename}:0:0'+bcolors.ENDC,end='') - # print(bcolors.FAIL+' Code Generation Error'+bcolors.ENDC) - # exit(1) + try: + asm = AssemblyGen(tac.func_code, debug=args.debug) + asm.gen_assembly() + except: + print(bcolors.BOLD+f'{lexer.filename}:0:0'+bcolors.ENDC,end='') + print(bcolors.FAIL+' Code Generation Error'+bcolors.ENDC) + exit(1) # dump the assembly!