[RFC] Preprocessor-Based Arithmetic and Condition Evaluation in Zephyr #83668
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It's still in the early stages of implementation and not in perfect condition, but I think it might interest everyone, so I'll put it out as a draft first.
Problem
Currently, DeviceTree macros face limitations in setting flexible compile-time conditions. Specifically,
COND_CODE_1
evaluates whether a given argument is1
or not, but deriving this1
using only macros can be challenging in certain cases.Proposed Solution (Overview)
Introduce the capability to perform "calculations" using the preprocessor, allowing for flexible condition evaluation. The following example demonstrates the proposed functionality:
Proposed Solution (Details)
To address the problem, implement 32-bit arithmetic calculations with macros. Numbers will be represented as lists of 0 and 1 arguments (referred to as "bit args"). Using these bit args, the following operations will be provided:
This enables complex conditional expressions to be evaluated entirely within the preprocessor.
In other words, it is a proposal to implement a simple computer using only macros.
Considerations
Performance
Multiplication and division are inherently slower operations. They may need to be avoided in CI environments for performance reasons.
Introducing a mechanism to limit the bit width of calculations could improve usability and efficiency.
Limitations of Number Representations
Due to the constraints of the preprocessor, decimal and hexadecimal literals cannot be directly handled. These must first be converted into the internal bit args representation.
To facilitate this, lookup tables containing up to 4096 entries for both decimal and hexadecimal numbers have been defined. However, this approach has limitations.
Internally, it is possible to generate hexadecimal literals from bit args with ease. As such, outputting results in hexadecimal is unrestricted.
The limitations can be summarized as follows:
Integration with DeviceTree
This functionality is designed to work alongside DeviceTree macros.
Most use cases can be supported by modifying devicetree_generated.h to include bit args values.
Exceptions may arise when dealing with literals defined in existing headers, which would require manual handling.
Benefits
This feature enables flexible conditional branching and complex calculations at compile time, enhancing the power and versatility of DeviceTree macros.