public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Udo A. Steinberg" <us15@os.inf.tu-dresden.de>
To: gcc-help@gcc.gnu.org
Subject: Clobbering memory region
Date: Sat, 08 Dec 2007 17:20:00 -0000	[thread overview]
Message-ID: <20071208182019.6b983dce@laptop.hypervisor.org> (raw)

Hello all,

An easy way to specify that an inline assembler statement changes memory is
to add "memory" to the clobber list. However, that is overly pessimistic.
As an alternative one can specify an output parameter for the memory region
like this:

#include <stddef.h>

void *memcpy (void *d, void const *s, size_t n)
{
    unsigned dummy;
    asm volatile ("rep; movsb"
                  : "=D" (dummy), "+S" (s), "+c" (n),
                    "=m" (*(struct { char x[n]; } *) d) 
                  : "0" (d));
    return d;
}

This works well in C (e.g., via gcc -c test.c).

With C++ (g++ -c test.c) the compiler complains:
test.c: In function 'void* memcpy(void*, const void*, size_t)':
test.c:9: error: types may not be defined in casts
test.c:9: error: array bound is not an integer constant

Is there a way to make this kind of sophisticated clobber work in C++ also?

Cheers,

	- Udo

                 reply	other threads:[~2007-12-08 17:20 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20071208182019.6b983dce@laptop.hypervisor.org \
    --to=us15@os.inf.tu-dresden.de \
    --cc=gcc-help@gcc.gnu.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 read-only IMAP folder(s) and NNTP newsgroup(s).