Skip to content

dtc: Add support for signed operations

Andrei Ziureaev devicetree-compiler 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v2 Latest6 patches 2 repliesIn review

Changes in v2 from the cover letter

- avoid UB when shifting
- add a bytestring type
- add support for all the operators
- use TYPE_SIGNED, instead of TYPE_NEGATIVE
- use macros on the output side
- add more tests

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6dtc: Avoid UB when shiftingdtc-parser.y+2 -21 reply
2/6dtc: Add bytestring typedtc-parser.y dtc.h tests/type-preservation.dt.yaml +2 more+23 -120 replies
3/6dtc: Turn 'uint64_t integer' into a structdtc-lexer.l dtc-parser.y dtc.h+102 -380 replies
4/6dtc: Add support for signed operationsdtc-parser.y dtc.h tests/integer-expressions.c+81 -101 reply
5/6dtc: Preserve negative integers in yaml and dtsdtc.h treesource.c yamltree.c+42 -180 replies
6/6dtc: Add sign preservation and operation teststests/operations-expected.dts tests/operations.dt.yaml tests/operations.dts +3 more+162 -30 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 0/6] dtc: Add support for signed operations

Following the discussion on v1, I decided to add proper support for all signed operations.

The first two patches can be useful on their own.

Patches 3/6 to 6/6 add support for signed operations and sign preservation in high-level output formats.

All operators are supported, which means some of them will behave differently with negative values, namely: /, %, >>, <, <=, >, >=. I haven't found any existing dts files using these operators with negative values.

There is no explicit signed type in the dts format, but the idea is to infer the sign as intuitively as possible: negative values are signed, positive values are unsigned. Zero can be either, so the output side checks for that. I haven't given much thought to overflows, but they happen to be handled well, I think (at least UB in C is prevented).

This change allows us to validate dts files that contain negative values. One could argue that (-1 == 0xffffffff), so there's no point in doing this, but dt-schema is written in Python 3, which uses arbitrary-precision arithmetic and doesn't have the traditional overflowing types. This makes it hard to solve this problem in dt-schema (especially checking if a signed value is within a range, I think).

Thanks,
Andrei.

Changes in v2:
- avoid UB when shifting
- add a bytestring type
- add support for all the operators
- use TYPE_SIGNED, instead of TYPE_NEGATIVE
- use macros on the output side
- add more tests

Andrei Ziureaev (6):
dtc: Avoid UB when shifting
dtc: Add bytestring type
dtc: Turn 'uint64_t integer' into a struct
dtc: Add support for signed operations
dtc: Preserve negative integers in yaml and dts
dtc: Add sign preservation and operation tests

 dtc-lexer.l                     |  10 +-
 dtc-parser.y                    | 190 ++++++++++++++++++++++++++------
 dtc.h                           |  14 +++
 tests/integer-expressions.c     |   8 ++
 tests/operations-expected.dts   |  31 ++++++
 tests/operations.dt.yaml        |  29 +++++
 tests/operations.dts            |  38 +++++++
 tests/run_tests.sh              |  13 ++-
 tests/sign-preservation.dt.yaml |  23 ++++
 tests/sign-preservation.dts     |  31 ++++++
 tests/type-preservation.dt.yaml |   2 +
 tests/type-preservation.dts     |   6 +-
 treesource.c                    |  47 +++++---
 yamltree.c                      |  27 ++---
 14 files changed, 399 insertions(+), 70 deletions(-)
 create mode 100644 tests/operations-expected.dts
 create mode 100644 tests/operations.dt.yaml
 create mode 100644 tests/operations.dts
 create mode 100644 tests/sign-preservation.dt.yaml
 create mode 100644 tests/sign-preservation.dts
Signature · 1 line
2.17.1