nibblebits / PeachCompiler

A C compiler created for the how to create a C Compiler online course
GNU General Public License v2.0
83 stars 36 forks source link

If we pass wrong arguments to printf (i.e forget a comma between 2 arguments), compiler can not check it out, and main function can not be parsed. #9

Open zzdmfk opened 1 year ago

zzdmfk commented 1 year ago

C code: int printf(const char* str, ...);

struct dog { int a; int b; };

void set_dog(struct dog* d, int x) { d->b = x; }

int main() { struct dog d; set_dog(&d, 70); //printf("hello world %i\r\n", d.b); printf("hello world %i\r\n" d.b); //I forget a comma return 0; }

nibblebits commented 1 year ago

Please provide the compiler output

zzdmfk commented 1 year ago

Compiler outputs: george@george-ubuntu:~/Desktop/gitcompiler/PeachCompiler$ ./main section .data section .text extern printf global set_dog ; set_dog function set_dog: push ebp mov ebp, esp push dword [ebp+12] lea ebx, [ebp+8] push ebx pop ebx mov ebx, [ebx] add ebx, 4 push ebx pop edx pop eax mov dword [edx], eax pop ebp ret section .data section .rodata everything compiled file /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../../lib32/Scrt1.o: in function _start': (.text+0x22): undefined reference tomain' collect2: error: ld returned 1 exit status nasm -f elf32 ./test -o ./test.o && gcc -m32 ./test.o -o ./test

And I debugged it, found the function 'main' can not be parsed correctly in 'parse_function'.

nibblebits commented 1 year ago

Thanks for reporting the problem