diff options
| -rw-r--r-- | LICENSE | 9 | ||||
| -rw-r--r-- | README.md | 58 | ||||
| -rw-r--r-- | justfile | 19 |
3 files changed, 83 insertions, 3 deletions
@@ -0,0 +1,9 @@ +The MIT License + +Copyright 2025 nsfisis + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2ff956 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# P4Dcc + +P4Dcc is a tiny, but self-hosted C compiler. It takes C source code and compiles it to assembly language. For assembling and linking, I use gcc as-is. + +This project was started to test the hypothesis: "Could a self-hostable C compiler be built in four days, if the feature set is carefully limited?" - "P4D" stands for the ISO 8601 notation meaning "four days." However, I actually completed the project by the morning of the third day. + +The code is written following the instructions at https://www.sigbus.info/compilerbook, and several key design decisions were inspired by the book. + + +## Dependencies + +* gcc +* [just](https://github.com/casey/just), a general-purpose task runner + + +## Build + +``` +$ just build +``` + + +## Test + +``` +$ just test +$ just test-all # test all things, including binary equiality between generations +``` + + +## Design + +To meet the four-day goal, many design decisions were made to reduce complexity (ideas directly taken from https://www.sigbus.info/compilerbook are not listed): + +* Simplify declaration syntax + * No support for `typedef` + * Structs always begin with `struct` keyword + * No support for array types + * No stack-allocated arrays + * All arrays are heap-allocated and accessed via pointers + * No support for function types + * Type information always precede the variable name +* Most syntax sugar is not implemented + * No increment/decrement operators + * No compound assignment operators + * No `while` +* Limited preprocessor + * Supports only simple `#define` that replaces identifiers with integer literals +* No global variables + * Including `stdin`, `stdout`, and `stderr` + * Only function/struct definitions/declarations are allowed at the top level +* No variable shadowing + * All variables are function-scoped + + +## License + +See [LICENSE](./LICENSE). @@ -11,7 +11,7 @@ build N="1": gcc -s -Wl,-z,noexecstack -o p4dcc{{N}} main{{N}}.s fi -build-upto-10: +build-upto-10-gen: just build 1 just build 2 just build 3 @@ -23,7 +23,7 @@ build-upto-10: just build 9 just build 10 -test-self-hosted: +test-self-hosted: build-upto-10-gen diff -u ./p4dcc2 ./p4dcc3 diff -u ./p4dcc3 ./p4dcc4 diff -u ./p4dcc4 ./p4dcc5 @@ -41,9 +41,22 @@ test TESTCASE="all" $BIN="p4dcc": build bash tests/run.sh {{TESTCASE}} fi if [[ $BIN = p4dcc ]]; then - just build2 + just build 2 fi +test-all: + just test-self-hosted + just test all p4dcc + just test all p4dcc2 + just test all p4dcc3 + just test all p4dcc4 + just test all p4dcc5 + just test all p4dcc6 + just test all p4dcc7 + just test all p4dcc8 + just test all p4dcc9 + just test all p4dcc10 + clean: rm p4dcc* rm -rf tests/tmp |
