-
Notifications
You must be signed in to change notification settings - Fork 8.1k
[RFC] Preprocessor-Based Arithmetic and Condition Evaluation #83668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
da0d0c0 to
683e72b
Compare
683e72b to
8b93c98
Compare
8b93c98 to
b897c00
Compare
|
We currently have a bit of this implemented, but spread out in It will add many thousands of lines of code, but if we hide these macros behind a header like |
|
I have concerns, though not likely worth -1ing over as we have already been going this direction for some time. What happens if there's a typo or mistake in the macro? What kind of errors are reported by gcc? How do you go about debugging these easily? Basically I'd like to see what the debugging story is for this sort of thing. Our biggest hurdle today for new people are macros which are not very easy to debug. This leads to our yearly developer survey showing DT/Kconfig/macros being a hurdle to using Zephyr. I find myself avoiding these things in Zephyr as I've personally burned hours debugging macros, and I'm constantly using and developing Zephyr as a maintainer! At what point are we going to be concerned about our developer experience in Zephyr with all of these things? |
I do agree many macros in zephyr are near impossible to follow "intuitively", stuff like having to use |
|
Architecture WG:
|
The answer is straightforward but likely many will not like where it goes. Not using the C preprocessor for this stuff. Going a step further... encoding more information in this not-C-preprocessor, enough such that typos and errors don't get punted to the next build step until at long last you get a linker error that all goes back to a typo you had in some metadata file describing you wanting to enable a hardware peripheral and its driver. Basically re-look at all this stuff from orbit rather from the anthill on the ground trying to solve the next micro problem like this that comes along. |
Well, the "what we don't want" is straightforward, no C-preprocessor, the "what to replace it with", that's the answer I'm interested in :) The preprocessor is solving the not having to store every node, child, prop, etc. in C structures (like Linux), which I'm fairly sure is not an option. What else can we do? |
|
I've been intrigued by https://gcc-python-plugin.readthedocs.io/en/latest/index.html to create some custom pre-processor shenanigans, but I think it's |
b897c00 to
ccef878
Compare
ccef878 to
b6390fe
Compare
13a8cd4 to
392c4b8
Compare
Gets the specified number of elements from the beginning of the list. This is the symmetircal operation as `GET_ARGS_LESS_N`. Signed-off-by: TOKITA Hiroshi <[email protected]>
Add `GET_ARGS_FIRST_N` tests. Signed-off-by: TOKITA Hiroshi <[email protected]>
47c8488 to
ac2adf0
Compare
Generates decimal definitions from devicetree_generated.h for use with the EXPR utils that will add. Signed-off-by: TOKITA Hiroshi <[email protected]>
This commit introduces a set of macros (EXPR utilities) designed for efficient compile-time evaluation of arithmetic, bitwise, and logical operations. This allows for flexible numeric processing even at compile time. Combined with the DeviceTree macro, calculations can be performed from the dt value. Co-authored-by: Joel Hirsbrunner <[email protected]> Signed-off-by: TOKITA Hiroshi <[email protected]>
We will re-implement IS_EQ, UTIL_INC, UTIL_DEC, and UTIL_X2, which were implemented as LUTs. Since it is still necessary to include large headers, we will also extract these into util_expr_macro.h and include them explicitly. Signed-off-by: TOKITA Hiroshi <[email protected]>
Add test for `UTIL_INC`, `UTIL_DEC`, `UTIL_X2` `IS_LT`, `IS_GT`, `IS_LE`, and `IS_GE`. Co-authored-by: Joel Hirsbrunner <[email protected]> Signed-off-by: TOKITA Hiroshi <[email protected]>
Add tests for EXPR utilities Co-authored-by: Joel Hirsbrunner <[email protected]> Signed-off-by: TOKITA Hiroshi <[email protected]>
ac2adf0 to
c92e8b3
Compare
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
Problem
Currently, DeviceTree macros face limitations in setting flexible compile-time conditions. Specifically,
COND_CODE_1evaluates whether a given argument is1or not, but deriving this1using 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.
Integration with DeviceTree
This functionality is designed to work alongside DeviceTree macros.
My suggestion is to process devicetree_generated.h and automatically generate definitions that
correspond to decimal numbers that appear; it is possible to include values that can be obtained
through the DeviceTree API in the processing. I think this covers the main use cases.
Considerations
Performance
Multiplication and division are inherently slower operations. For performance reasons, we may need to be avoided in CI environments.
Limitations of Number Representations
Due to the preprocessor's constraints, decimal and hexadecimal literals cannot be handled directly. They must first be converted into the internal bit args representation.
Lookup tables containing up to 4096 entries for decimal and hexadecimal numbers have been defined to facilitate this. However, this approach has limitations in computation resources.
Internally, generating hexadecimal literals from bit args is easy, so outputting results in hexadecimal is unrestricted.
The limitations can be summarized as follows:
Benefits
This feature enables flexible conditional branching and complex calculations at compile time, enhancing the power and versatility of DeviceTree macros.
Changes to some common parts have been grouped into a separate PR.
I will rebase as appropriate.
#84191
#84190