public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Brown <david@westcontrol.com>
To: "Rafał Pietrak" <embedded@ztk-rp.eu>,
	"Jonathan Wakely" <jwakely.gcc@gmail.com>
Cc: waffl3x <waffl3x@protonmail.com>, "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: wishlist: support for shorter pointers
Date: Mon, 3 Jul 2023 16:52:32 +0200	[thread overview]
Message-ID: <a515480b-e357-f94f-4325-2974a4468a1d@westcontrol.com> (raw)
In-Reply-To: <21650b1b-1d2e-7929-3105-b8aaca629272@ztk-rp.eu>

On 28/06/2023 10:35, Rafał Pietrak via Gcc wrote:
> Hi Jonathan,
> 
> W dniu 28.06.2023 o 09:31, Jonathan Wakely pisze:
>>
>>
>>
>> If you use a C++ library type for your pointers the syntax above 
>> doesn't need to change, and the fancy pointer type can be implemented 
>> portable, with customisation for targets where you could use 16 bits 
>> for the pointers.
> 
> As you can expect from the problem I've stated - I don't know C++, so 
> I'll need some more advice there.
> 
> But, before I dive into learning C++ (forgive the naive question).... 
> isn't it so, that C++ comes with a heavy runtime? One that will bloat my 
> tiny project? Or the bloat comes only when one uses particular 
> elaborated class/inheritance scenarios, and this particular case ( for 
> (...; ...; x = x->next) {} ) will not draw any of that into this project?
> 


Let me make a few points (in no particular order) :

1. For some RISC targets, such as PowerPC, it is common to have a 
section of memory called the "small data section".  One of the registers 
is dedicated as an anchor to this section, and data within it is 
addressed as Rx + 16-bit offset.  But this is primarily for data at 
fixed (statically allocated) addresses, since reads and writes using 
this address mode are smaller and faster than full 32-bit addresses. 
Normal pointers are still 32-bit.  It also requires a dedicated register 
- not a big cost when you have 31 GPRs, but much more costly when you 
have only 13.

2. C++ is only costly if you use costly features.  On small embedded 
systems, you want "-fno-exceptions -fno-rtti", and you will get as good 
(or bad!) results for C++ as for C.  Many standard library features 
will, however, result in a great deal of code - it is usually fairly 
obvious which classes and functions are appropriate.

3. In C, you could make a type such as :

	typedef struct {
		uint16_t p;
	} small_pointer_t;

and conversion functions :

	static const uintptr_t ram_base = 0x20000000;

	static inline void * sp_to_voidp(small_pointer_t sp) {
		return (void *)(ram_base + sp);
	}

	static inline small_pointer_t voidp_to_sp(void * p) {
		small_pointer_t sp;
		sp.p = (uintptr_t) p - ram_base;
		return sp;
	}

Then you would use these access functions to turn your "small pointers" 
into normal pointers.  The source code would become significantly harder 
to read and write, and less type-safe, but could be quite efficient.

In C++, you'd use the same kinds of functions.  But they would now be 
methods in a class template, and tied to overloaded operators and/or 
conversion functions.  The result would be type-safe and let you 
continue to use a normal pointer-like syntax, and with equally efficient 
generated code.  You could also equally conveniently have small pointers 
to ram and to peripheral groups.  This mailing list is not really the 
place to work through an implementation of such class templates - but it 
certainly could be done.


4. It is worth taking a step back, and thinking about how you would like 
to use these pointers.  It is likely that you would be better thinking 
in terms of an array, rather than pointers - after all, you don't want 
to be using dynamically allocated memory here if you can avoid it, and 
certainly not generic malloc().  If you can use an array, then your 
index type can be as small as you like - maybe uint8_t is enough.


David




  parent reply	other threads:[~2023-07-03 14:52 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-27 12:26 Rafał Pietrak
2023-06-28  1:54 ` waffl3x
2023-06-28  7:13   ` Rafał Pietrak
2023-06-28  7:31     ` Jonathan Wakely
2023-06-28  8:35       ` Rafał Pietrak
2023-06-28  9:56         ` waffl3x
2023-06-28 10:43           ` Rafał Pietrak
2023-06-28 12:12             ` waffl3x
2023-06-28 12:23               ` Rafał Pietrak
2023-07-03 14:52         ` David Brown [this message]
2023-07-03 16:29           ` Rafał Pietrak
2023-07-04 14:20             ` Rafał Pietrak
2023-07-04 15:13               ` David Brown
2023-07-04 16:15                 ` Rafał Pietrak
2023-06-28  7:34     ` waffl3x
2023-06-28  8:41       ` Rafał Pietrak
2023-06-28 13:00 ` Martin Uecker
2023-06-28 14:51   ` Rafał Pietrak
2023-06-28 15:44     ` Richard Earnshaw (lists)
2023-06-28 16:07       ` Martin Uecker
2023-06-28 16:49         ` Richard Earnshaw (lists)
2023-06-28 17:00           ` Martin Uecker
2023-06-28 16:48       ` Rafał Pietrak
2023-06-29  6:19       ` Rafał Pietrak
2023-07-03 15:07         ` Ian Lance Taylor
2023-07-03 16:42           ` Rafał Pietrak
2023-07-03 16:57             ` Richard Earnshaw (lists)
2023-07-03 17:34               ` Rafał Pietrak
2023-07-04 12:38             ` David Brown
2023-07-04 12:57               ` Oleg Endo
2023-07-04 14:46               ` Rafał Pietrak
2023-07-04 15:55                 ` David Brown
2023-07-04 16:20                   ` Rafał Pietrak
2023-07-04 22:57                 ` Martin Uecker
2023-07-05  5:26                   ` Rafał Pietrak
2023-07-05  7:29                     ` Martin Uecker
2023-07-05  8:05                       ` Rafał Pietrak
2023-07-05  9:11                         ` David Brown
2023-07-05  9:25                           ` Martin Uecker
2023-07-05 11:34                             ` David Brown
2023-07-05 12:01                               ` Martin Uecker
2023-07-05  9:42                           ` Rafał Pietrak
2023-07-05 11:55                             ` David Brown
2023-07-05 12:25                               ` Rafał Pietrak
2023-07-05 12:57                                 ` David Brown
2023-07-05 13:29                                   ` Rafał Pietrak
2023-07-05 14:45                                     ` David Brown
2023-07-05 16:13                                       ` Rafał Pietrak
2023-07-05 17:39                                         ` David Brown
2023-07-06  7:00                                           ` Rafał Pietrak
2023-07-06 12:53                                             ` David Brown
2023-07-05  9:29                         ` Martin Uecker
2023-07-05 10:17                           ` Rafał Pietrak
2023-07-05 10:48                             ` Martin Uecker

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=a515480b-e357-f94f-4325-2974a4468a1d@westcontrol.com \
    --to=david@westcontrol.com \
    --cc=embedded@ztk-rp.eu \
    --cc=gcc@gcc.gnu.org \
    --cc=jwakely.gcc@gmail.com \
    --cc=waffl3x@protonmail.com \
    /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).