9front - general discussion about 9front
 help / color / mirror / Atom feed
From: ori@eigenstate.org
To: 9front@9front.org
Subject: Re: [9front] cc: fix c99 integer conversions
Date: Sun, 02 Aug 2020 09:38:20 -0700	[thread overview]
Message-ID: <BD5AC740625328FB9ECC069C03C36456@eigenstate.org> (raw)
In-Reply-To: <CB654CB37FA64A527E3D8195C4DFFC78@eigenstate.org>

> The C99 standard, section 6.4.4.1 paragraph 5 says that integer
> constants should be converted as follows:
> 
> If they fit in an int, they should be an int.
> 
> If they're written in decimal, they should be converted
> to the smallest signed type that can hold the value.
> 
> If they're written as oct or hex, they should be converted
> to the smallest signed *or* unsigned type that can hold the
> value.
> 
> Right now, we don't widen to vlong when appropriate. This
> fixes the issue.
> 
> This bug/quirk was discovered by Amavect, and they wrote
> the test code:
> 

New revision of the patch, which doesn't touch types with
explicit suffixes, and warns when you fall off of the int
sizes, to catch behavior changes in existing code. There's
just one place in our tree where the change matters, so
this patch shuts up that one warning.

I'm also happy to remove the warning, since it doesn't
seem to be an issue in practice.

diff -r 39206a734718 sys/src/cmd/8c/txt.c
--- a/sys/src/cmd/8c/txt.c	Sat Aug 01 10:54:03 2020 -0700
+++ b/sys/src/cmd/8c/txt.c	Sun Aug 02 09:36:22 2020 -0700
@@ -865,7 +865,7 @@
 		gmove(f, &fregnode0);
 		gins(AFADDD, nodfconst(-2147483648.), &fregnode0);
 		gins(AFMOVLP, f, &nod);
-		gins(ASUBL, nodconst(-2147483648), &nod);
+		gins(ASUBL, nodconst(-0x80000000), &nod);
 		gmove(&nod, t);
 		return;
 
diff -r 39206a734718 sys/src/cmd/cc/lex.c
--- a/sys/src/cmd/cc/lex.c	Sat Aug 01 10:54:03 2020 -0700
+++ b/sys/src/cmd/cc/lex.c	Sun Aug 02 09:36:22 2020 -0700
@@ -444,7 +444,7 @@
 yylex(void)
 {
 	vlong vv;
-	long c, c1, t;
+	long c, c1, t, w;
 	char *cp;
 	Rune rune;
 	Sym *s;
@@ -844,7 +844,16 @@
 		yyerror("overflow in constant");
 
 	vv = yylval.vval;
-	if(c1 & Numvlong) {
+	/*
+	 * c99 is silly. Decimap constants stay signed,
+	 * hex and octal go unsigned before widening.
+	 */
+	w = 32;
+	if((c1 & (Numdec|Numuns)) == Numdec)
+		w = 31;
+	if(c1 & Numvlong || (c1 & Numlong) == 0 && (uvlong)vv >= 1ULL<<w){
+		if((c1&(Numdec|Numvlong)) == Numdec && vv < 1ULL<<32)
+			warn(Z, "int constant widened to vlong: %s", symb);
 		if((c1 & Numuns) || convvtox(vv, TVLONG) < 0) {
 			c = LUVLCONST;
 			t = TUVLONG;



  reply	other threads:[~2020-08-02 16:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-24  4:09 ori
2020-08-02 16:38 ` ori [this message]
2020-08-02 18:28   ` [9front] " ori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BD5AC740625328FB9ECC069C03C36456@eigenstate.org \
    --to=ori@eigenstate.org \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).