From 06ef9c611e6176f0c2e071bfa758ac6ea6dcbb14 Mon Sep 17 00:00:00 2001 From: SantriptaSharma Date: Wed, 6 Dec 2023 20:14:38 +0530 Subject: [PATCH] input lib (a5) partial --- 15_A5.y | 2 +- A4_tests/testsimple.nc | 1 + A5_Tests/test4.nc | 4 ++-- Makefile | 10 +++++---- createa5.sh | 2 +- inputlib.s | 48 ++++++++++++++++++++++++++++++++++++++++++ lexmain.c | 1 + 7 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 inputlib.s diff --git a/15_A5.y b/15_A5.y index 8cea038..80b803d 100644 --- a/15_A5.y +++ b/15_A5.y @@ -684,7 +684,7 @@ compound_statement: block_item_list: block_item - | block_item_list marker block_item { Backpatch($1, $2); $$ = $3; Backpatch($$, quads_size); Emit(JmpLabel()); } + | block_item_list marker block_item { Backpatch($1, $2); $$ = $3; Backpatch($$, quads_size); } block_item: declaration { $$ = NULL; } diff --git a/A4_tests/testsimple.nc b/A4_tests/testsimple.nc index 483d444..ed0d4a7 100644 --- a/A4_tests/testsimple.nc +++ b/A4_tests/testsimple.nc @@ -5,6 +5,7 @@ char ithmetic2 = charithmetic - 'd'; int arithmetic = ithmetic2 + 5; char d = +'c'; char f = -'f'; +void *thing = &GLOB; void printf(char *c, int argv); diff --git a/A5_Tests/test4.nc b/A5_Tests/test4.nc index 4a9e973..dcdcd00 100644 --- a/A5_Tests/test4.nc +++ b/A5_Tests/test4.nc @@ -1,6 +1,6 @@ -int sum(int n){ +int fun(int n){ if(n == 0) return n; - return sum(n-1) + n; + return fun(n-1) + n; } int main(){ diff --git a/Makefile b/Makefile index 00c1134..f2e4180 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ l=flex yy=bison CFLAGS=-Werror -lfl -g -o=translator +o=compiler lx=lexer team=15 @@ -15,8 +15,9 @@ fname=$(team)_A$(assignment) build: $(o) -test: build - ./$(o) < $(testfile) +# TODO: write this according to spec +# test: build +# ./$(o) < $(testfile) $(o): $(fname)_translator.o $(fname).tab.o $(lx).o compiler.o $(CC) -o $@ $^ $(CFLAGS) @@ -34,7 +35,8 @@ $(lx).c: $(fname).l $(fname).tab.c $(CC) -c $^ $(CFLAGS) clean: - rm -rf $(o)* + rm -rf $(o) + rm -rf $(o).exe rm -rf $(lx)* rm -rf *.o rm -rf $(fname).tab.* diff --git a/createa5.sh b/createa5.sh index 7ed4d2f..0745adf 100644 --- a/createa5.sh +++ b/createa5.sh @@ -1,3 +1,3 @@ cp tex_build/15_A5.pdf . -tar -c 15_A5_translator.c 15_A5_translator.h compiler.c compiler.h 15_A5.l 15_A5.nc 15_A5.y Makefile 15_A5.pdf 15_A5_quads* -f 15_A5.tar +tar -c 15_A5_translator.c 15_A5_translator.h compiler.c compiler.h 15_A5.l 15_A5.nc 15_A5.y Makefile 15_A5.pdf 15_A5_quads* inputlib.asm -f 15_A5.tar rm 15_A5.pdf \ No newline at end of file diff --git a/inputlib.s b/inputlib.s new file mode 100644 index 0000000..f07eb63 --- /dev/null +++ b/inputlib.s @@ -0,0 +1,48 @@ +.data +STRING1: + .string "Hello, world!\n" +.text + .globl _main + .globl printStr + +_main: + # write string1 to stdout + pushl $STRING1 + call printStr + addl $4, %esp + + # exit w code 0 + xorl %edi, %edi + movl $0x3c, %eax + syscall + +printStr: + # prologue, no callee-saved registers + pushl %ebp + movl %esp, %ebp + + # let's get the (32-bit) pointer out of the stack + movl 8(%ebp), %ecx + # edx will calc strlen + xorl %edx, %edx + _lenloop: + cmpb $0, (%ecx, %edx, 1) + je _lenloop_end + incl %edx + jmp _lenloop + _lenloop_end: + # edx now contains strlen + # need to write to stdout, fd = 1 + movl $1, %ebx + # syscall number for write is 4 on 32-bit + movl $4, %eax + + int $4 + # eax contains return value from write, no. of written characters + # our calling convention dictates eax as the return value, so all + # done + + # epilogue + movl %esp, %ebp + popl %ebp + ret diff --git a/lexmain.c b/lexmain.c index fb06be5..a3841ff 100644 --- a/lexmain.c +++ b/lexmain.c @@ -1,6 +1,7 @@ #include extern int yylex(); +int y = 10; int main() { int c;