9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Charles Forsyth <charles.forsyth@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] A compiler bug
Date: Thu,  2 Aug 2018 00:35:47 +0100	[thread overview]
Message-ID: <CAOw7k5jsW0WUTaqE1N_L5_2KVyvxLt74=Nwrd5JTOuRnNGQSuw@mail.gmail.com> (raw)
In-Reply-To: <20180801072134.DEB31156E400@mail.bitblocks.com>

[-- Attachment #1: Type: text/plain, Size: 1473 bytes --]

"6.6 Constant expressions" doesn't allow a cast from a non-arithmetic type
to an arithmetic one generally, and a cast
in an address constant can only cast from an integer constant to a pointer
type (eg, char *reg = (char*)0x123450);

the one example with 8c escaped with a warning ("initialize pointer to an
integer") because of some 8c x86-specific folding that makes the expression
acceptable.

even so, the format and intention of the example seems practical (with the
correct cast to uintptr) and "An implementation may accept other forms of
constant expressions".
it should be fairly easy to add as an extension with consistent handling
across ?c.



On Wed, 1 Aug 2018 at 08:22, Bakul Shah <bakul@bitblocks.com> wrote:

> Consider:
>
> % cat x.c
> #include <u.h>
> uintptr foo[3];
> uintptr bar=&foo[2];
>
> % 8c -c x.c     # this works.
> % 5c -c x.c     # this fails
> x.c:3 initializer is not a constant: bar
>
> If I change the last line to
>
> uintptr* bar=&foo[2];
>
> Both compilers compile it fine. But if I change the last line
> to
>
> uintptr bar=(uintptr)&foo[2];
>
> both compilers fail. Note that the last two examples are
> type correct.
>
> uintptr is the same size as int* so there should be no runtime
> cost and we already know from the second example that &foo[2]
> is a link time constant.
>
> Similar code to the last two examples compiles on gcc/clang.
>
> This seems like a compiler bug.
>
>

[-- Attachment #2: Type: text/html, Size: 2148 bytes --]

  reply	other threads:[~2018-08-01 23:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-01  7:21 Bakul Shah
2018-08-01 23:35 ` Charles Forsyth [this message]
2018-08-02  0:27   ` Bakul Shah
2018-08-02  9:38     ` Charles Forsyth
2018-08-02 13:28       ` Benjamin Purcell
2018-08-02 14:20 cinap_lenrek
2018-08-05 14:53 ` Charles Forsyth

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='CAOw7k5jsW0WUTaqE1N_L5_2KVyvxLt74=Nwrd5JTOuRnNGQSuw@mail.gmail.com' \
    --to=charles.forsyth@gmail.com \
    --cc=9fans@9fans.net \
    /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).