Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/9ccPublic

A Small C Compiler

License

NotificationsYou must be signed in to change notification settings

rui314/9cc

Repository files navigation

Note: 9cc is no longer an active project, and the successor ischibicc.

9cc is a successor of my8cc C compiler.In this new project, I'm trying to write code that can be understoodextremely easily while creating a compiler that generates reasonablyefficient assembly.

9cc has more stages than 8cc. Here is an overview of the internals:

  1. Compiles an input string to abstract syntax trees.
  2. Runs a semantic analyzer on the trees to add a type to each tree node.
  3. Converts the trees to intermediate code (IR), which in some degreeresembles x86-64 instructions but has an infinite number of registers.
  4. Maps an infinite number of registers to a finite number of registers.
  5. Generates x86-64 instructions from the IR.

There are a few important design choices that I made to keep the code assimple as I can get:

  • Like 8cc, 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 areasonable design choice for short-lived programs such as compilers.This policy greatly simplifies code and also eliminates use-after-freebugs entirely.

  • 9cc's parser is a hand-written recursive descendent parser, so that thecompiler doesn't have any blackbox such as lex/yacc.

  • I stick with plain old tools such as Make or shell script so that youdon't need to learn about new stuff other than the compiler source codeitself.

  • We use brute force if it makes code simpler. We don't try too hard toimplement sophisticated data structures to make the compiler run faster.If the performance becomes a problem, we can fix it at that moment.

  • Entire contents are loaded into memory at once if it makes code simpler.We don't use character IO to read from an input file; instead, we readan entire file to a char array in a batch. Likewise, we tokenize awhole file in a batch rather than doing it concurrently with the parser.

Overall, 9cc is still in its very early stage. I hope to continueimproving it to the point where 9cc can compile real-world C programs suchas Linux kernel. That is an ambitious goal, but I believe it's achievable,so stay tuned!

About

A Small C Compiler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp