Sujet : transpiling to low level C
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 15. Dec 2024, 04:05:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vjlh19$8j4k$1@dont-email.me>
User-Agent : Mozilla Thunderbird
I am working on a C backend that generates simple C code.
You can test it here:
http://thradams.com/cake/playground.htmlObjective
- Shift all the complexity of the C compiler to the frontend.
Why? This approach simplifies having one frontend and multiple backends.
- Use C as an intermediate language to feed a backend (any C compiler can act as the backend).
The backend can then focus solely on code generation.
- Code is not portable
Removed C89 Features
- Preprocessor
- sizeof
- typedef
- enum
- Constant expressions (the final result is precomputed during earlier stages)
- const
- I may also remove switch.
- struct are declared only at external scope
Most features from C99, C11, and C23 are not included.
However, features directly related to code generation may be supported, in the future such as:
- restrict
- inline
- atomic
- _NoReturn
Current Status
This project is very new and still contains bugs.
When generating a constant like '(unsigned char) 1', the cast is removed. The rationale is that type-specific representations aren't necessary, as values are promoted automatically.
Similarly, 'nullptr' is replaced by '0' instead of '(void*)0'.
Constructs dependent on type (like 'sizeof', '_Generic', etc.) have already been removed in earlier stages.
Example:
unsigned char c = u8'~';
Generated Code:
unsigned char c = 126;
Instead of:
unsigned char c = ((unsigned char)126);
For larger types like 'unsigned long long', a suffix is still included.
Initialization Strategy
Currently, I use 'memset' to initialize structures to zero. For instance:
struct X x = {};
memset(&x, 0, 8 /*bytes*/);
Boolean Conversion
The behavior of converting integers to 'bool' is still incomplete.
For example:
bool b = 123;
Here, 'b' should have the value '1' but today it is 123.