public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Rafał Pietrak" <embedded@ztk-rp.eu>
To: gcc@gcc.gnu.org
Subject: a feature to the wishlist
Date: Mon, 13 Sep 2021 08:55:04 +0200	[thread overview]
Message-ID: <34090ee9-8554-9c35-ed2a-3ded1369a4dc@ztk-rp.eu> (raw)

Hi everybody,

I've been using GCC for a varety of ARM micro controller project. With
embedded systems, one often has to access specific memory locations,
which contain hardware registers, that control device behavior.
"traditionally" or "commonly" that's codded by programmers using
"#define", thus relaying on preprocessor to put specific compiler
constructs, that do the job.

IMHO, this is wrong exactly because of the involvement of preprocessor.
Taking as an example exerpts from libopencm3, like:
	>> USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID)
one can understand, that such "code" can actually translate to quite
anything.

Getting to the point, here is code exerpt of by own STM32 usart driver:
------------------------------------------------------------------
#define USART_RE	11
#define USART_RENEIE	8
#define USART_TE	10
#define USART_TXEIE	6
#define USART_UE	0

extern struct usart_s {
	uint sr;
	uint dr;			// offset 0x04
	uint brr;			// offset 0x08
	volatile union { uint cr1;	// offset 0x0C
		struct cr_s { uint sbk:1, rwu:1,
		re:1, te:1, idleie:1, rxneie:1,
		tcie:1, txeie:1, peie:1, ps:1,
		pce:1, wake:1, m:1, ue:1;} cr1_s;
	};
} USART1;

static void EXIT(void *tty) {
	volatile struct usart_s *d = &USART1;

#if VARIANT_TRADITIONAL
	d->cr1 &= ~(1 << USART_RE) & ~(1 << USART_RXNEIE)
		& ~(1 << USART_TE) & ~(1 << USART_TXEIE)
		& ~(1 << USART_UE);
#elif VARIANT_WORKING
	struct cr_s a = (struct cr_s) {
			.re = 1,
			.te = 1,
       			.rxneie = 1,
			.txeie = 1,
			.ue = 1 };
	int *b = (int *) &a;
	d->cr1 &= ~(*b);
#elif VARIANT_BETTER
	(union cr_u) d->cr1 &= ~ {
			.re = 1,
			.te = 1,
       			.rxneie = 1,
			.txeie = 1,
			.ue = 1 };
#elif VARIANT_THE_BEST
	d->cr1_s &= ~ { .re = 1,
			.te = 1,
       			.rxneie = 1,
			.txeie = 1,
			.ue = 1 };
#endif
}
----------------------------------------------------------------

In function EXIT, you can see four variants of "the same code". First
fragment is codded just like todays common practice is. This is BAD
coding, because debugger has no way of presenting "the real thing".

Using gcc-8-branch revision 273027 (arm-none-eabi-gcc linux cross
compiler) I was able to code the very same thing by VARIANT_WORKING,
which is far better to read (IMHO). The disadvantage is, that I had to
use variable "b" to fool gcc syntax parser. No direct multiple "type
overwrite" worked for me.

Then again, it would be even better (and easier to read the code),
should gcc syntax parser recognised VARIANT_BETTER or VARIANT_THE_BEST
as "syntactic sugar" to generate code, that currently gets issued by
VARIANT_WORKING (being the same as issued by VARIANT_TRADITIONAL, which
is very OK).

Pls CC: responses to my address, as I'm not a subscriber to the gcc list.

So my question to the list is: are there chances, the issue can be
recognised as important, and planed for some future gcc?

with best regards,

Rafał Pietrak

             reply	other threads:[~2021-09-13  6:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-13  6:55 Rafał Pietrak [this message]
2021-09-13  8:44 ` Jonathan Wakely
2021-09-13  9:51   ` Rafał Pietrak
2021-09-13 11:41     ` David Brown
2021-09-14 18:48       ` Rafał Pietrak
2021-09-15  7:20         ` David Brown

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=34090ee9-8554-9c35-ed2a-3ded1369a4dc@ztk-rp.eu \
    --to=embedded@ztk-rp.eu \
    --cc=gcc@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).