In C, most of the standard library is mandatory for hosted
implementations only, not for freestadning implementations.
Still, I see many functions, such as memcpy() and abs() often used in >programs for embedded systems, and see no obstacles to implementing them
even on small systems.
[...] there's nothing in the Standard to say that a freestanding implementation that implements part of the hosted standard library
has to do it correctly.
In C, most of the standard library is mandatory for hosted
implementations only, not for freestadning implementations.
Still, I see many functions, such as memcpy() and abs() often used in programs for embedded systems, and see no obstacles to implementing them
even on small systems.
Should more of the standard library become mandatory for freestanding implementations?
For string.h, I have written a first draft of a proposal:
http://www.colecovision.eu/stuff/proposal-freestanding-string.html
What do you think of it?
Do you see any reason those could not be provided on some C implementation?
Which further functions would you like to become mandatory for
freestanding C implementations?
Freestanding implementations may provide whatever additional
library facilities they choose, but the required set should
be kept where it is, at an absolute minimum.
Philipp Klaus Krause <pkk@spth.de> writes:
For many embedded users, memcpy() might be more useful and necessary
than long long or float. Also, memcpy() is easier to implement than
support for long long or float. Still the latter are currently mandatory
for freestanding implementations, while the former is not.
Forth has a core wordset (basically a library) and a bunch of separate optional wordsets, each of which is standardized. Floating point
arithmetic is an optional wordset so it's fine if your implementation
doesn't have it. But if you do choose to supply floating point
features, the standard says what those features should do, so you don't
have to make it up as you go along.
Maybe C could use a similar approach.
Am 07.05.20 um 15:14 schrieb Tim Rentsch:
Freestanding implementations may provide whatever additional
library facilities they choose, but the required set should
be kept where it is, at an absolute minimum.
Why?
By the same argument you could make support for int or anything else
optional for freestanding implemnentations.
Stuff that cannot be implemented easily on certain hardware should
surely be optional (or even better, the standard should try to avoid introducing features in a way that make them hard or inefficient to
implement on certain types of hardware).
But I haven't yet seen a good reason to keep stuff like memcpy()
that can easily be implemented on any hardware on which C can be
implemented at all, optional.
Philipp Klaus Krause <pkk@spth.de> writes:
Am 07.05.20 um 15:14 schrieb Tim Rentsch:
Freestanding implementations may provide whatever additional
library facilities they choose, but the required set should
be kept where it is, at an absolute minimum.
Why?
I would turn that question around and ask why should the minimum
be raised? Your written proposal doesn't offer even any good
arguments, let alone any compelling or convincing ones.
[…]
The idea that <string.h> should be mandatory everywhere is part of
a broader trend that C should be made more uniform by insisting
that implementations be more homogenous. A justification sometimes
offered for this is so programs will be "more portable" (and indeed
this aspect is mentioned in your proposal). That reasoning is
fundamentally misguided. C isn't like Ada, where its use can be
mandated, and certification is necessary. If the bar is raised,
the "offending implementations" won't change, they will just be
left as non-conforming. That effect will /reduce/ the incentive to
comply with the rest of the Standard, not raise it. Fundamental
truth: programs are not made "more portable" by limiting the set
of platforms (HW+SW) that have conforming implementations.
Applications don't have to support every possible conforming
implementation; they are perfectly free to rule out any that don't
give them what they need (ideally with preprocessor conditionals or
static assertions, but that is a separate topic). But thinking a
program is "more portable" by virtue of having ruled out all
implementations where it wouldn't work is like an ostrich sticking
its head in the sand.
* Supporting int has no runtime footprint costs even on
pretty tiny processors. Functions in <string.h> often do.
[…]
Second, the use of "easily" is too glib. You haven't made any
effort, at least not that I can see, to quantify the cost on
implementations to comply with the suggested additions. (Also,
the proposal is a bit vague about exactly what /is/ being
suggested, and this deficiency needs to be corrected before
the proposal should receive any further serious attention.)
For example, under this scheme, we might do this:
// File "fs_string.h"
#include <string-optional.h> // string.h optional subset
#if ! _Provides_strlen
extern size_t strlen( const char * );
#endif
...
// File "fs_string.c"
#include "fs_string.h"
#if ! _Provides_strlen
size_t
strlen( const char *s ){
size_t r = 0;
while( *s++ ) r++;
return r;
}
#endif
Now the rest of the program can simply #include "fs_string.h",
and use strlen() just as it would if strlen() was supplied
by the implementation.
Philipp Klaus Krause <pkk@spth.de> writes:
Am 07.05.20 um 15:14 schrieb Tim Rentsch:
Freestanding implementations may provide whatever additional
library facilities they choose, but the required set should
be kept where it is, at an absolute minimum.
Why?
I would turn that question around and ask why should the minimum
be raised? Your written proposal doesn't offer even any good
arguments, let alone any compelling or convincing ones.
By the same argument you could make support for int or anything else
optional for freestanding implemnentations.
That's wrong. The case for int is not at all the same argument.
* Every program uses int. Not every program uses any
<string.h> functions.
* Supporting int has no runtime footprint costs even on
pretty tiny processors. Functions in <string.h> often do.
* Support for int on freestanding implementations is already
in the Standard. The default position is always in favor
of the status quo. Removing support for int would need at
the very least a compelling argument to do so; leaving it
in needs no argument at all. This situation is exactly
reversed for the <string.h> proposal.
Stuff that cannot be implemented easily on certain hardware should
surely be optional (or even better, the standard should try to avoid
introducing features in a way that make them hard or inefficient to
implement on certain types of hardware).
Personally I think there are reasons to consider lowering the
requirements on freestanding implementations for integer types
wider than int, and perhaps make floating point optional entirely.
But that is another conversation.
Am 08.05.20 um 21:03 schrieb Tim Rentsch:
Philipp Klaus Krause <pkk@spth.de> writes:...
Am 07.05.20 um 15:14 schrieb Tim Rentsch:
Freestanding implementations may provide whatever additional
library facilities they choose, but the required set should
be kept where it is, at an absolute minimum.
Functionality in the C standard is there, since it was found useful to
many C programmers - otherwise it wouldn't have made it into the standard.
I propose making some functionality that is already in the C standard mandatory for freestanding implementations.
And indeed, I see uses of string.h functions in many programs for tiny devices.
Sometimes, to be portable cross freestanding implementations, users roll their own versions.
I do not see adding functionality, that can be easily added to any C implementation as reducing the incentive to comply with the standard. On
the other hand, that would surely be true for features that are complex
to implement or hard to implement efficiently on some hardware (SDCC
does not support VLAs, and thus is more complete in C17 support than in
C99 support).
Am 08.05.20 um 17:38 schrieb Tim Rentsch:
For example, under this scheme, we might do this:
// File "fs_string.h"
#include <string-optional.h> // string.h optional subset
#if ! _Provides_strlen
extern size_t strlen( const char * );
#endif
...
// File "fs_string.c"
#include "fs_string.h"
#if ! _Provides_strlen
size_t
strlen( const char *s ){
size_t r = 0;
while( *s++ ) r++;
return r;
}
#endif
Now the rest of the program can simply #include "fs_string.h",
and use strlen() just as it would if strlen() was supplied
by the implementation.
But this example also shows that it is relatively easy to implement
strlen(), so there is not much burden on implementations when requiring
them to provide it. And it shows that strlen not being mandatory for freestanding implementations results in lots of duplicated code in
software projects that target these implementations.
What I like about your idea is the notion that an interface is
standardized, and an implementation gets a binary choice,
either to provide it as specified, or just not to provide it.
To make that idea work for case under discussion here (ie, for
string functions), I think that needs to be augmented in two
ways. One, it needs to be more fine grained than just all of
<string.h>.
Am 08.05.20 um 17:38 schrieb Tim Rentsch:
What I like about your idea is the notion that an interface is
standardized, and an implementation gets a binary choice,
either to provide it as specified, or just not to provide it.
To make that idea work for case under discussion here (ie, for
string functions), I think that needs to be augmented in two
ways. One, it needs to be more fine grained than just all of
<string.h>.
The current C2X draft has __STDC_VERSION_XXXX_H__, which allows to
specify the version of the standard a header conforms to. While not as fine-grained as a per-function macro would be, it already is much more fine-grained than the previously existing global macro for standard
version compliance.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 339 |
Nodes: | 16 (0 / 16) |
Uptime: | 09:29:41 |
Calls: | 7,467 |
Calls today: | 3 |
Files: | 12,692 |
Messages: | 5,626,424 |