Post AZQ3OLsWS5hBeAC3d2 by ekaitz_zarraga@mastodon.social
(DIR) More posts by ekaitz_zarraga@mastodon.social
(DIR) Post #AZQ3OLsWS5hBeAC3d2 by ekaitz_zarraga@mastodon.social
2023-09-04T09:21:39Z
1 likes, 3 repeats
QUIZ!!! What does this code return?!intmain (){ char r = -1; unsigned s = r; unsigned t = (unsigned char)r; if (s != -1) return 2; if (t != 255) return 1; return 0;}
(DIR) Post #AZQ3ON6jsif5SXqvQW by piggo@piggo.space
2023-09-04T09:26:34.868366Z
1 likes, 0 repeats
@ekaitz_zarraga what the fuck
(DIR) Post #AZQ3TifFA7w7lPXefA by piggo@piggo.space
2023-09-04T09:27:42.005054Z
0 likes, 0 repeats
@ekaitz_zarraga i can sort of rationalize the way it works but its far from intuitive
(DIR) Post #AZQ5l2oPhpUz5BDhQW by ekaitz_zarraga@mastodon.social
2023-09-04T09:30:31Z
1 likes, 0 repeats
@piggo I've been fighting against this kind of code for weeks as I'm working in the bootstrapping process, fighting against compilers and assemblers...The answer is superfun.
(DIR) Post #AZQ5lX2zICywr4GlJA by tfe@mastodon.eus
2023-09-04T09:33:08Z
1 likes, 0 repeats
@ekaitz_zarraga Shit...
(DIR) Post #AZQ5lZ3PpaVr4yU8Fk by tfe@mastodon.eus
2023-09-04T09:36:20Z
0 likes, 0 repeats
@ekaitz_zarraga Portzierto...Prabatu duzu GEF?https://github.com/hugsy/gefBai, ezta?
(DIR) Post #AZQ5lxaYc1HdAiAwF6 by ekaitz_zarraga@mastodon.social
2023-09-04T09:36:07Z
2 likes, 0 repeats
@tfe Believe me, that's not going to help. There is no god.
(DIR) Post #AZQ87kbpkY7VCDI5ey by condret@shitposter.club
2023-09-04T10:19:50.617286Z
0 likes, 0 repeats
@ekaitz_zarraga please tell me 2 is correct
(DIR) Post #AZQAogRlIjqueABBtw by newt@stereophonic.space
2023-09-04T10:49:31.271835Z
1 likes, 0 repeats
@ekaitz_zarraga which part exactly is surprising to you? Check out section 6.3.1.3 here https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
(DIR) Post #AZQB4yoiliLGFqgmuW by newt@stereophonic.space
2023-09-04T10:52:30.415440Z
1 likes, 0 repeats
@condret @ekaitz_zarraga nah.. it's 0. This is another case of C programmers being surprised by the language they ought to know :akkogiggle:
(DIR) Post #AZQCEOz6wnh1yLbbrk by ekaitz_zarraga@mastodon.social
2023-09-04T11:02:00Z
0 likes, 0 repeats
@newt @condret it's 0 in x86_64.Wanna try other architectures?
(DIR) Post #AZQCEPbkd8bLuBgT5s by newt@stereophonic.space
2023-09-04T11:05:20.382060Z
0 likes, 0 repeats
@ekaitz_zarraga @condret it's 0 on a platform with signed char. Architecture has little to do with this, it's purely ABI.
(DIR) Post #AZQCY3TicJtH3zIFfs by ekaitz_zarraga@mastodon.social
2023-09-04T11:06:49Z
0 likes, 0 repeats
@newt @condret does RV64 have signed char?
(DIR) Post #AZQCY4D5tcBdKiWUOu by newt@stereophonic.space
2023-09-04T11:08:53.667440Z
0 likes, 0 repeats
@ekaitz_zarraga @condret no idea. This depends on compiler settings, not hardware for most cases. You can specify which one you want when you configure your compiler initially.Here's some code that will give you food for thought.#include <stdio.h>#include <limits.h>int main (){#ifdef __CHAR_UNSIGNED__ puts("Char is unsigned");#else puts("Char is signed");#endif char r = -1; unsigned char u = -1; unsigned s = r; unsigned t = (unsigned char)r; printf("r = %d, u = %u, s = %u, t = %u\n", r, u, s, t); return 0;}
(DIR) Post #AZQD9QlYzpsy0ISifY by ekaitz_zarraga@mastodon.social
2023-09-04T11:14:13Z
0 likes, 0 repeats
@newt @condret That is actually really cool. When running it in RV64 char is unsigned!Really good catch!!
(DIR) Post #AZQD9RTsL5KaDjC6jo by newt@stereophonic.space
2023-09-04T11:15:46.749329Z
0 likes, 0 repeats
@ekaitz_zarraga @condret you probably should explicitly specify whether you want signed char if you put negative values there. Or just, you know, use it only for characters and use int for numeric values.
(DIR) Post #AZQEt8JS2PLi2wfYzQ by taylan@pl.tkammer.de
2023-09-04T11:35:36.832268Z
0 likes, 0 repeats
@ekaitz_zarraga Isn't it undefined since char could be signed or unsigned?Let me guess what happens with GCC where char is signed by default:"unsigned s = r" is equivalent to:unsigned s = -1And therefore to:- unsigned s = UINT_MAX(You first get the value, then do the conversion. I think so anyway, couldn't find confirmation of this. But if it wasn't so, it would mean your two assignments would have the same behaviour, making this quiz boring. Sorry for meta-gaming.)The same equivalence between -1 and UINT_MAX applies when you compare it to -1, so the first if is skipped.In the second case, you first coerce the signed char -1 to unsigned char which makes it 255, so the second if is also skipped.So, it returns 0 with GCC and standard flags.If you use -funsigned-char it should return 2 I guess, since s will end up being 255 not UINT_MAX.I feel really smart right now because I'm not even a C programmer, but actually I spent 30 minutes reading the GCC manual and a couple SO questions (none directly addressing this topic though) and could still be wrong. :politecat: I only used SO to confirm that char can be signed or unsigned, and to confirm that "unsigned" means "unsigned int" (which the GCC manual strangely doesn't mention).https://gnu-c-language-manual.github.io/GNU-C-Language-Manual/
(DIR) Post #AZQFTj1r0OTOLQt34q by ekaitz_zarraga@mastodon.social
2023-09-04T11:37:59Z
1 likes, 0 repeats
@newt @condret yeah... this case is a weird test from a compiler we need to compile, and it's making a heavy use of this kind of things... thanks for the help here! It will be really useful to have this information.
(DIR) Post #AZQFXZUSPalhv46Fhg by newt@stereophonic.space
2023-09-04T11:42:22.093139Z
0 likes, 0 repeats
@ekaitz_zarraga @condret the secret here is reading the same document that compiler developers did read. Also known as the C standard. I linked the PDF somewhere above.
(DIR) Post #AZQFmiVf9yfcEisyRs by ekaitz_zarraga@mastodon.social
2023-09-04T11:43:59Z
0 likes, 0 repeats
@newt @condret Yeah of course. I'm not sure if they follow the standard all the time though.
(DIR) Post #AZQFmj7wrdIM9SnY7k by newt@stereophonic.space
2023-09-04T11:45:12.354512Z
0 likes, 0 repeats
@ekaitz_zarraga @condret both GCC and Clang developers are notorious in following it to the letter. If you find deviations from the spec, it will be treated as a bug.
(DIR) Post #AZQG1F232VL79mxXsW by ekaitz_zarraga@mastodon.social
2023-09-04T11:46:44Z
0 likes, 0 repeats
@newt @condret Sadly, it's not any of those. :)
(DIR) Post #AZQG1JOAqBPmfZLGvA by newt@stereophonic.space
2023-09-04T11:47:47.229357Z
0 likes, 0 repeats
@ekaitz_zarraga @condret but you do use one of these to build your compiler, don't you?
(DIR) Post #AZQGMdkYKmpDtnyoLI by ekaitz_zarraga@mastodon.social
2023-09-04T11:49:29Z
0 likes, 0 repeats
@newt @condret No. We are working in the bootstrapping problem. We are building it with a compiler we did. So we need to decide how to treat these weird cases in our compiler. That's why I'm discussing all this.See GNU Mes for more info.
(DIR) Post #AZQGMf1bas3lqyxwYq by newt@stereophonic.space
2023-09-04T11:51:43.252962Z
0 likes, 0 repeats
@ekaitz_zarraga @condret oh.. then you might have a bigger problem :akkoaah:>The Scheme interpreter is written in ~5,000 LOC of simple C, and the C compiler written in Scheme and these are mutual self-hosting.Sounds like utter insanity for no good reason whatsoever.
(DIR) Post #AZQMd1H3Et8pDTNGV6 by ekaitz_zarraga@mastodon.social
2023-09-04T11:52:52Z
0 likes, 0 repeats
@newt @condret There's a good reason: bootstrapping the world.
(DIR) Post #AZQMd2H5WElUJsOoCG by newt@stereophonic.space
2023-09-04T13:01:54.657352Z
0 likes, 0 repeats
@ekaitz_zarraga @condret then you would need a C++ compiler somewhere. Because GCC is written in C++, not in C.
(DIR) Post #AZQMriNxkSyCyic9Fg by ekaitz_zarraga@mastodon.social
2023-09-04T13:03:54Z
1 likes, 0 repeats
@newt @condret That's since some years ago. In the past it wasn't like that.So we use an old GCC and we build the modern one with it.For the old GCC we use TCC, and for that we use Mes, and for mes we use Stage0, which has some assemblers and magic tricks and for that there's nothing... It's just source code all the way down!
(DIR) Post #AZQMzV499d6pz83dvE by ekaitz_zarraga@mastodon.social
2023-09-04T13:05:16Z
0 likes, 0 repeats
@newt @condret If you are interested, there are some talks you can watch.One of them happens to be mine.https://archive.fosdem.org/2023/schedule/event/guixriscv/
(DIR) Post #AZQMzVjGgk0E2fIU1A by newt@stereophonic.space
2023-09-04T13:06:02.694825Z
0 likes, 0 repeats
@ekaitz_zarraga @condret I figured how it works more or less just from what you describe.Personally, I'm not a Guix fan, though I tried to like it.t. NixOS user
(DIR) Post #AZQNEP7dXEvRtWnTe4 by ekaitz_zarraga@mastodon.social
2023-09-04T13:08:18Z
1 likes, 0 repeats
@newt @condret NixOS also has some bootstrapping thingie based on TCC!The other day someone shared they were going to give a talk about it in NixCon. They didn't go that far, but the idea is the same.We what we are doing here is not only for Guix, we are trying to make it accessible to other distributions too.
(DIR) Post #AZQNSwU8I1HLQDFfIe by newt@stereophonic.space
2023-09-04T13:11:16.122801Z
0 likes, 0 repeats
@ekaitz_zarraga @condret that's a nice thing to do.Sadly, still doesn't cover all available corner cases that I might need, since Haskell isn't bootstrappable without GHC :comfyderp:Now I'm wondering if it would be possible to throw in a minimal Haskell compiler in pure Scheme without taking on a few man-decades of work.
(DIR) Post #AZQO11B3LmWZovZlQ0 by ekaitz_zarraga@mastodon.social
2023-09-04T13:15:16Z
0 likes, 0 repeats
@newt @condret There are efforts in Guix for GHC but I'm not sure what's the status of that (I'm not a haskeller).
(DIR) Post #AZQO11zOKcn4L37xse by newt@stereophonic.space
2023-09-04T13:17:23.457558Z
0 likes, 0 repeats
@ekaitz_zarraga @condret i bet it's futile. GHC is the only existing Haskell compiler today, and in order to compile GHC you need GHC.But maybe writing a small independent compiler for C-- could be a nice start.
(DIR) Post #AZQRz00l79ARRR3OF6 by condret@shitposter.club
2023-09-04T14:02:21.698366Z
0 likes, 0 repeats
@newt @ekaitz_zarraga i thought unsigned alone was always going to be 32 bit
(DIR) Post #AZQi5aS48o2NSnzPLU by hyphen@duwa.ng
2023-09-04T17:02:47.607131Z
0 likes, 0 repeats
@ekaitz_zarraga impl defined but most likely 0.char is of impl defined signedness, in most systems it’s signed and 8-bit, operating on that:- unsigned s = r = -1, is defined behavior, the -1 gets converted to UINT_MAX- unsigned t = (unsigned char)r, is defined behavior, the -1 gets converted to UCHAR_MAX which isn’t further converted when promoted to unsigned int, rather it’s simply zero extended as needed.- in the first comparison, -1 is implicitly converted to UINT_MAX, which means s == -1- in the second comparison, t is UCHAR_MAX, which in our case == 255 potential problems:- comparison between signed and unsigned, although dont think it’s ub, usually is a logic bug though- char is unsigned and int is a larger bit repr, r = UCHAR_MAX, s != -1, return 2- no guarantee that UCHAR_MAX = 255, return 1 fixed code:intmain() { signed char r = -1; unsigned s = r; unsigned t = (unsigned char)r; if (s != UINT_MAX) return 2; if (t != UCHAR_MAX) return 1; return 0;}always returns 0 on all systems.:xlr: Fuck C’s integer promotion rules and implicit conversions.
(DIR) Post #AZQj2bkKyFlaZ3gAK0 by ekaitz_zarraga@mastodon.social
2023-09-04T17:12:39Z
1 likes, 0 repeats
@hyphen I asked because I found a case where it returns 2 but it's most likely 0 as you explain.Thanks for the detailed explanation and the possible alternative!!:)