Re:Writing a C compiler in 500 lines of Python
by ak_111 on Tuesday September 05, 2023 @05:03AM
(#37389455)
Would it be possible to create two versions of the language: microC for embedded C programs where these decisions matter, and standard C for PC/servers which basically all use the same representation for these scalers?
My main point is that a C programmer today is forced to learn dozens of rules just to cater for many niche platforms that they will probably never target, so if you were to separate those two use cases you can get a much more neat C that targets modern 64 bit architectures with all the power of traditional C but a bit less portability.
Re:Writing a C compiler in 500 lines of Python
by foldr on Tuesday September 05, 2023 @05:17AM
(#37389573)
Yes, for sure. I don't disagree with your overall point. Just pointing out that people are still writing C code for 8 and 16 bit platforms today.
Re:Writing a C compiler in 500 lines of Python
by mananaysiempre on Tuesday September 05, 2023 @05:06AM
(#37389483)
In fact, the default integer promotions together with the requirement that unsigned int hold numbers until at least 2^16 is the main reason C code for 8-bitters is somewhat... stylized, to put it mildly.
Re:Writing a C compiler in 500 lines of Python
by foldr on Tuesday September 05, 2023 @05:38AM
(#37389681)
I think that's actually an interesting example of how C sees itself as a high level language (from the perspective of the early 70s), even though we now categorize it as low-level. You'd expect the default integer type in a high level language to be able to store values that are large enough for an interesting range of practical tasks; 16 bits is arguably the bare minimum to meet that requirement.
Re:Writing a C compiler in 500 lines of Python
by mananaysiempre on Tuesday September 05, 2023 @07:16AM
(#37390243)
IIRC a retrospective by one of the designers listed the default promotions as a careless mistake, because the original—offhand—reasoning was that the registers are 16-bit anyway, so they might as well widen everything when it’s free (on the only machine the language was implemented on at that moment).
Re:Writing a C compiler in 500 lines of Python
by circuit10 on Tuesday September 05, 2023 @05:53AM
(#37389781)
If you’re writing code for those why not just use the smaller data types if you don’t need bigger ones? That way it will work efficiently on both and the behaviour will be consistent
Re:Writing a C compiler in 500 lines of Python
by foldr on Tuesday September 05, 2023 @06:58AM
(#37390146)
Fair point. I guess the issue is with library code that uses int. But you aren't typically going to use lots of general-purpose library code if you're targeting a microcontroller.
Re:Writing a C compiler in 500 lines of Python
by circuit10 on Tuesday September 05, 2023 @11:31AM
(#37393064)
If that library code doesn’t use the smaller types then it probably isn’t designed for those platforms, which means it won’t be tested with the smaller int types and they will likely cause lots of bugs. The one advantage I do see is that it might avoid extra sign extension instructions on some architectures when working with less than the native size, but there’s int_fast16_t for that
You'd lose something because those decisions would be impractical for 8-bit and 16-bit targets (which still exist in the world of embedded programming).