9cc: A Small C Compiler
github.com
9cc: A Small C Compiler
1–10 of 70 posts
Re: 9cc: A Small C Compiler
#2The last time I investigated the linux kernel had so many gcc-isms that it was probably true that if you could compile the linux kernel, you could probably compile any program targeted to gcc.
Re: 9cc: A Small C Compiler
#3It lists a goal as "compiling real-world programs such as the linux kernel" The last time I investigated the linux kernel had so many gcc-isms that it was probably true that if you could compile the linux kernel, you could probably compile any program targeted to gcc.
Re: 9cc: A Small C Compiler
#4For functions that have prototypes, it is okay, but when they do not, you're losing type checking (yes, even on the number of arguments).
The following program compiles with no diagnostics for me with -W -Wall -ansi -pedantic, with GCC 7.3 on Ubuntu 18:
int func()
{
return 0;
}
int main()
{
func(3);
return 0;
}
-std=c11 (as you're using) makes no difference.The same is not true of C++: func() in C++ is a prototype definition. C++ supports (void) for compatibility with C, but even in nonsensical contexts: class::class(void);
Re: 9cc: A Small C Compiler
#5Re: 9cc: A Small C Compiler
#6Even when linking, don't call "cc", but "$(CC)". You're relying on the implicit rule to compile your .c to .o which will use $(CC). $(CC) could be some ARM cross-compiler supplied by a distro. When you link using "cc", you end up using the build-machine's native compiler.
Write the build system like this is an awesome program that major distros will be eager to pick up, and make it easy for the package maintainer. :)
Speaking of CFLAGS, only set that conditionally; don't clobber it:
CFLAGS ?= -O2 # Only if CFLAGS is not specified externally, use this default
for things that your program needs in order to build right, put that in other variables of your own: DIALECT_CFLAGS := -std=c11
CFLAGS += $(DIALECT_CFLAGS) # integrate external CFLAGS with our own.
Same deal with LDFLAGS. Both CFLAGS and LDFLAGS can include important things that cause a bad build if you mess with them.Re: 9cc: A Small C Compiler
#7Small nitpick. Functions in C that take no arguments are written name(void){...} not name(){...}. This latter form is an old-style definition which doesn't introduce a prototype into the scope. For functions that have prototypes, it is okay, but when they do not, you're losing type checking (yes, even on the number of arguments). The following program compiles with no diagnostics for me with -W -Wall -ansi -pedantic,…
Re: 9cc: A Small C Compiler
#8Small nitpick. Functions in C that take no arguments are written name(void){...} not name(){...}. This latter form is an old-style definition which doesn't introduce a prototype into the scope. For functions that have prototypes, it is okay, but when they do not, you're losing type checking (yes, even on the number of arguments). The following program compiles with no diagnostics for me with -W -Wall -ansi -pedantic,…
Clang (tested with v7) however does issue a warning even with no flags set.
Formally, this is not introduced into the scope as type information according to the language spec, but that's no reason not to warn about it informally.
Re: 9cc: A Small C Compiler
#9>I know that people find the policy odd, but this is actually a reasonable design choice for short-lived programs such as compilers.
I'm strongly disagree at this point. Memory management is important even for short-lived programs. It would bring burden to the OS if you invoke this kind of "short-lived, memory-management-free" programs multiple times.
>This policy greatly simplifies code and also eliminates use-after-free bugs entirely
Not facing it is definitely not a good way to solve a problem. It's a bad attitude as a programmer be honestly.
Re: 9cc: A Small C Compiler
#10>no memory management is the memory management policy in 9cc. We allocate memory using malloc() but never call free(). >I know that people find the policy odd, but this is actually a reasonable design choice for short-lived programs such as compilers. I'm strongly disagree at this point. Memory management is important even for short-lived programs. It would bring burden to the OS if you invoke this kind of "short-liv…
All the memory is freed when the process exits. Why does it matter if you run the program multiple times?