@@ -23,14 +23,20 @@ Despite its simplistic nature, it is capable of performing basic optimization st
23
23
24
24
` shecc ` is capable of compiling C source files written in the following
25
25
syntax:
26
- * data types: char, int, struct, and pointer
27
- * condition statements: if, while, for, switch, case, break, return, and
26
+ * data types: ` char ` , ` int ` , ` struct ` , ` enum ` , ` typedef ` , and pointer types
27
+ * condition statements: ` if ` , ` else ` , ` while ` , ` for ` , ` do-while ` , ` switch ` , ` case ` , ` default ` , ` break ` , ` continue ` , ` return ` , and
28
28
general expressions
29
- * compound assignments: ` += ` , ` -= ` , ` *= `
30
- * global/local variable initializations for supported data types
31
- - e.g. ` int i = [expr] `
32
- * limited support for preprocessor directives: ` #define ` , ` #ifdef ` , ` #elif ` , ` #endif ` , ` #undef ` , and ` #error `
33
- * non-nested variadic macros with ` __VA_ARGS__ ` identifier
29
+ * operators: all arithmetic, logical, bitwise, and assignment operators including compound assignments
30
+ (` += ` , ` -= ` , ` *= ` , ` /= ` , ` %= ` , ` &= ` , ` |= ` , ` ^= ` , ` <<= ` , ` >>= ` )
31
+ * arrays: global/local arrays with initializers, multi-dimensional arrays
32
+ * functions: function declarations, definitions, and calls with fixed arguments
33
+ * variadic functions: basic support via direct pointer arithmetic (no ` <stdarg.h> ` )
34
+ * typedef: type aliasing including typedef pointers (` typedef int *ptr_t; ` )
35
+ * pointers: full pointer arithmetic, multi-level pointer dereference (` ***ptr ` )
36
+ * global/local variable initializations for all supported data types
37
+ - e.g. ` int i = [expr]; ` , ` int arr[] = {1, 2, 3}; `
38
+ * preprocessor directives: ` #define ` , ` #ifdef ` , ` #ifndef ` , ` #elif ` , ` #else ` , ` #endif ` , ` #undef ` , ` #error ` , and ` #include `
39
+ * function-like macros with parameters and ` __VA_ARGS__ ` support
34
40
35
41
The backend targets armv7hf with Linux ABI, verified on Raspberry Pi 3,
36
42
and also supports RISC-V 32-bit architecture, verified with QEMU.
@@ -89,6 +95,12 @@ Run `make` and you should see this:
89
95
SHECC out/shecc-stage2.elf
90
96
```
91
97
98
+ For development builds with memory safety checks:
99
+ ``` shell
100
+ $ make sanitizer
101
+ $ make check-sanitizer
102
+ ```
103
+
92
104
File ` out/shecc ` is the first stage compiler. Its usage:
93
105
``` shell
94
106
$ shecc [-o output] [+m] [--no-libc] [--dump-ir] < infile.c>
@@ -128,11 +140,23 @@ use `update-snapshot` / `check-snapshot` instead.
128
140
129
141
### Unit Tests
130
142
131
- ` shecc ` comes with unit tests. To run the tests, give ` check ` as an argument :
143
+ ` shecc ` comes with a comprehensive test suite (200+ test cases). To run the tests :
132
144
``` shell
133
- $ make check
145
+ $ make check # Run all tests (stage 0 and stage 2)
146
+ $ make check-stage0 # Test stage 0 compiler only
147
+ $ make check-stage2 # Test stage 2 compiler only
148
+ $ make check-sanitizer # Test with AddressSanitizer and UBSan
134
149
```
135
150
151
+ The test suite covers:
152
+ * Basic data types and operators
153
+ * Control flow statements
154
+ * Arrays and pointers (including multi-level dereference)
155
+ * Structs, enums, and typedefs
156
+ * Variadic functions
157
+ * Preprocessor directives and macros
158
+ * Self-hosting validation
159
+
136
160
Reference output:
137
161
```
138
162
TEST STAGE 0
@@ -213,10 +237,11 @@ int fib(int n) def int @fib(int %n)
213
237
214
238
## Known Issues
215
239
216
- 1. The generated ELF lacks of .bss and .rodata section
217
- 2. The support of varying number of function arguments is incomplete. No `<stdarg.h>` can be used.
218
- Alternatively, check the implementation `printf` in source `lib/c.c` for `var_arg`.
219
- 3. The C front-end is a bit dirty because there is no effective AST.
240
+ 1. The generated ELF lacks .bss and .rodata sections
241
+ 2. Full `<stdarg.h>` support is not available. Variadic functions work via direct pointer arithmetic.
242
+ See the `printf` implementation in `lib/c.c` for the supported approach.
243
+ 3. The C front-end operates directly on token streams without building a full AST.
244
+ 4. Complex pointer arithmetic expressions like `*(p + offset)` have limited support.
220
245
221
246
## License
222
247
0 commit comments