Skip to content
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

Use a few integer types from the compiler #2222

Merged
merged 34 commits into from
Nov 19, 2020

Conversation

yamt
Copy link
Contributor

@yamt yamt commented Nov 5, 2020

Summary

The main motivaion is to allow to use -Wformat compiler warnings.
Otherwise, it complains on long vs long long differences for intmax_t.

Note: The compiler only knows the printf format and the host OS types.
It doesn't know NuttX types.

Note: On both of 64-bit macOS and Ubuntu x86_64, the host intmax_t
is "long int" while NuttX's intmax_t without this change is
"signed long long". While they are both 64-bit integers, clang -Wformat
complains on the difference.

Impact

all archs, especially 64-bit ones

Testing

tested with local app on sim/macOS

@yamt yamt changed the title sim: Use a few integer types from the compiler Use a few integer types from the compiler Nov 5, 2020
@yamt yamt force-pushed the sim-intmax branch 4 times, most recently from fd9ad67 to 5544ea5 Compare November 9, 2020 11:17
@yamt yamt force-pushed the sim-intmax branch 3 times, most recently from 775cd4f to 801f83e Compare November 10, 2020 08:25
@yamt
Copy link
Contributor Author

yamt commented Nov 16, 2020

finally the ci is green. yay.

@xiaoxiang781216
Copy link
Contributor

@yamt great work! but there are many intermediate patches, can you organize the patch series into two:

  1. Fix type mismatch and nxstyle(or better to has nxstyle fix in a standalone patch)
  2. inttype.h correction and printflike annotation(or better to split the annotation into another patch)

The fix for type, nxstyle and annotation is good and ready to merge, inttype.h need more review and dissuss.

@yamt
Copy link
Contributor Author

yamt commented Nov 16, 2020

i guess compilers used for arch/z80 and arch/z16 don't provide __INTMAX_TYPE__ or __UINTMAX_TYPE__.
@patacongo is it right?

in that case, those types.h need to be updated to provide _intmax_t and _uintmax_t.

@patacongo
Copy link
Contributor

patacongo commented Nov 16, 2020

i guess compilers used for arch/z80 and arch/z16 don't provide __INTMAX_TYPE__ or __UINTMAX_TYPE__.
@patacongo is it right?

Very unlikely. It is c89 which is before stdint.h

in that case, those types.h need to be updated to provide _intmax_t and _uintmax_t.

You will need to add those.

@yamt
Copy link
Contributor Author

yamt commented Nov 17, 2020

i guess compilers used for arch/z80 and arch/z16 don't provide __INTMAX_TYPE__ or __UINTMAX_TYPE__.
@patacongo is it right?

Very unlikely. It is c89 which is before stdint.h

in that case, those types.h need to be updated to provide _intmax_t and _uintmax_t.

You will need to add those.

thank you. i added them.

yamt added 10 commits November 18, 2020 21:23
    root@212cf3f52994:/tools# p32-gcc -mlong32 -dM -E - < /dev/null | grep "__INT.*_TYPE__"|sort
    #define __INT16_TYPE__ short int
    #define __INT32_TYPE__ long int
    #define __INT64_TYPE__ long long int
    #define __INT8_TYPE__ signed char
    #define __INTMAX_TYPE__ long long int
    #define __INTPTR_TYPE__ int
    #define __INT_FAST16_TYPE__ int
    #define __INT_FAST32_TYPE__ int
    #define __INT_FAST64_TYPE__ long long int
    #define __INT_FAST8_TYPE__ int
    #define __INT_LEAST16_TYPE__ short int
    #define __INT_LEAST32_TYPE__ long int
    #define __INT_LEAST64_TYPE__ long long int
    #define __INT_LEAST8_TYPE__ signed char
    root@212cf3f52994:/tools#
64-bit size_t/intptr_t/ptrdiff_t are long, not long long,
for macOS and Linux.

Note: we don't care CONFIG_SIM_M32 on macOS.

macOS:
    spacetanuki% uname -a
    Darwin spacetanuki.lan 18.7.0 Darwin Kernel Version 18.7.0: Mon Aug 31 20:53:32 PDT 2020; root:xnu-4903.278.44~1/RELEASE_X86_64 x86_64
    spacetanuki% cc --version
    Apple clang version 11.0.0 (clang-1100.0.33.17)
    Target: x86_64-apple-darwin18.7.0
    Thread model: posix
    InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
    spacetanuki% cc -dM -E - < /dev/null|grep __SIZE_TYPE__
    #define __SIZE_TYPE__ long unsigned int
    spacetanuki% cc -m32 -dM -E - < /dev/null|grep __SIZE_TYPE__
    #define __SIZE_TYPE__ long unsigned int
    spacetanuki%

Linux:
    root@4c2e9e83ac82:/tools# uname -a
    Linux 4c2e9e83ac82 5.4.39-linuxkit apache#1 SMP Fri May 8 23:03:06 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
    root@4c2e9e83ac82:/tools# cc --version
    cc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
    Copyright (C) 2019 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    root@4c2e9e83ac82:/tools# cc -dM -E - < /dev/null|grep __SIZE_TYPE__
    #define __SIZE_TYPE__ long unsigned int
    root@4c2e9e83ac82:/tools# cc -m32 -dM -E - < /dev/null|grep __SIZE_TYPE__
    #define __SIZE_TYPE__ unsigned int
    root@4c2e9e83ac82:/tools#
@yamt
Copy link
Contributor Author

yamt commented Nov 18, 2020

Yes, it is used, but all configs can build fine by removing compiler_stdint.h. So I think it is redundant and should be removed.

  • the majority of configs relies on it providing _uintmax_t and _intmax_t.

If we make uint64_t/int64_t same as compiler want, can typedef uintmax_t/intmax_t to uint64_t/int64_t match compiler expectation?
If the answer is false, I still prefer to typedef _uintmax_t and _intmax_t in arch's inttype.h like other type, because the consistence is also important:

No Short Cuts
Reducing effort at the expense of Quality, Portability, or Consistency.

ok, it makes sense.

  • it serves as an assertion to ensure the compiler types matches arch types.h.

The printf annotation can do the same thing, right?

i don't know how compiler printf format checks works wrt default type promotion.
even if it works, i don't think it's a good idea to rely on that level of details.

But, since all libc funcitons are implemented by NuttX self, how to map these basic types should be determined by NuttX too. We have to match the compiler expectation here, just because we want to utilize the printf/scanf format specifier chceck provided by compiler. So it's reasonable to limit the mismatch in printf/scanf format checker.

libc is just a part of C runtime. the compiler is also a fundamental part of it.
it's better to ensure type expectations matched unless you are seeking problems.

having said that, i dropped the compiler_stdint.h stuff from this PR because it turned out to be more controversial than i expected.

@xiaoxiang781216
Copy link
Contributor

xiaoxiang781216 commented Nov 18, 2020

@yamt after reviewing the final patchset, I found that the common typedef for intmax_t/uintmax_t in include/stdint.h:

#ifdef __INT64_DEFINED
typedef _int64_t            intmax_t;
typedef _uint64_t           uintmax_t;
#else
typedef _int32_t            intmax_t;
typedef _uint32_t           uintmax_t;
#endif

should work fine after we correct _int64_t/_uint64_t/_int32_t/_uint32_t. Why we move intmax_t/uintmax_t to arch/inttype.h?

@yamt
Copy link
Contributor Author

yamt commented Nov 18, 2020

@yamt after reviewing the final patchset, I found that the common typedef for intmax_t/uintmax_t in include/stdint.h:

#ifdef __INT64_DEFINED
typedef _int64_t            intmax_t;
typedef _uint64_t           uintmax_t;
#else
typedef _int32_t            intmax_t;
typedef _uint32_t           uintmax_t;
#endif

should work fine after we correct _int64_t/_uint64_t/_int32_t/_uint32_t. Why we move intmax_t/uintmax_t to arch/inttype.h?

  • you suggested it's better to have them in arch types.h for consistency with other types and i agreed.
  • the common definition doesn't work well for macOS:
spacetanuki% cc -dM -E - < /dev/null|grep -E "__INT(32|64|MAX)_TYPE__"
#define __INT32_TYPE__ int
#define __INT64_TYPE__ long long int
#define __INTMAX_TYPE__ long int
spacetanuki% 

@xiaoxiang781216
Copy link
Contributor

  • the common definition doesn't work well for macOS:
spacetanuki% cc -dM -E - < /dev/null|grep -E "__INT(32|64|MAX)_TYPE__"
#define __INT32_TYPE__ int
#define __INT64_TYPE__ long long int
#define __INTMAX_TYPE__ long int
spacetanuki% 

Ok, thanks for explanation, it's clear now.

@xiaoxiang781216
Copy link
Contributor

These two patch should merge into one:
9bc2c99
45a1d68

@yamt
Copy link
Contributor Author

yamt commented Nov 19, 2020

These two patch should merge into one:
9bc2c99
45a1d68

done

Copy link
Contributor

@xiaoxiang781216 xiaoxiang781216 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants