Why ! C++

K

Kaz Kylheku

So how it's been done? I'm very curious about that,
because the plain old C doesn't need any runtime
support AFAIK.

Of course C needs run-time support. What run-time support is needed
depends on the compiler. Not every expression, declaration or
statement is necesarily turned into pure inline machine code.

On some RISC-like processors, for instance, some math operations, even
simple integer ones like divisions, may necessitate the use of a
library routine. This explains the various __mul* and __div* type
functions in the GNU compilers's ``libgcc'' library. If you're using
GCC to write a kernel, then you still have to supply that library.

How about structure initialization and assignment?

Code like

some_struct x = { 0 };
/* ... */
y = x;

may actually translate into machine code which calls memset and
memcpy. If you don't supply these, the thing won't link due to
unresolved references.

C++ construction and destruction has its API also. One possibility is
for the comiler to generate a special hidden construction and
destruction hook functions. Each object file with C++ objects in it
has such functions.

The embedded kernel's startup code would have to have a routine which
would iterates over these functions and calls them.

Now how do you get a list of these functions? One possible way is to
make use of a linker feature known as named sections (also known as
segments). If you've done assembly language programming, you might
have run into conventional sections like .text or .data.

Anyway, for each constructor hook function, a variable holding a
pointer to that function (by way of a symbolic reference that will be
resolved at link time) is also emitted, and that variable is placed
into a section. When the objects are linked, the linker will merge all
of the objects that belong to the same section name into one
contiguous segment of memory in the target object. The routine then
simply has to iterate from some symbolic address marking the section
start to another symbol which resolves to the section end. It knows
that the section contains nothing but pointers to the constructor
functions which are all equally wide, and so essentially it is working
with an array of pointers set up by the linker. And that is how you
can complete the run-time support for C++ global construction and
destruction. The compiler does one part, a linker script defines the
sections to do the second part, and some loop does the rest.

Exceptions? Similar concept. There will be some exception handling ABI
expected by the generated code, which the system has to complete. That
ABI will have some function for unwinding the stack frame by frame,
querying each frame for useful information such as the entry point for
clean-up cod for that frame, etc. The ABI has to map to the
concurrency model also. Do you allow exceptions within interrupt
service routines? If so, the unwinder has to know that it's in an
interrupt stack, and how to recognize that it's hit the top of it,
etc. Moreover, the interrupt may have gone off while the interrupted
task context was processing an exception already, so the two can't get
mixed up, etc.
 
R

Rolf Magnus

SasQ said:
Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisał(a):


How did you make it?

I don't know, but I did it too. The smallest controller I used with C++ is
an 8 bit one with 1k of program memory and 64 bytes of data memory. Of
course there are some sacrifices you have to make on such a small system.
Most of the C++ standard library for instance wouldn't make much sense on
such a system, because it heavily depends on dynamic memory and the code
would be too big.
How can C++ work in an embedded system?

Just like C can.
I thaught C++ needs some additional implementation code,
which make base for C++ language facilities before it
call main() function.
So?

I mean the things like reserving static memory, calling constructors for
static objects, exception handling mechanisms etc. And I thaught that
new and delete operators have to be implemented using some system
API/kernel calls to some system memory manager, and cannot work in "blank"
environment without some system/kernel, which may do the memory management
for new/delete implementation code.

What makes you think new/delete must use a kernel to manage memory? It can
just as well have its own memory manager.
So how it's been done? I'm very curious about that, because the plain old
C doesn't need any runtime support AFAIK.

Well, it also has to initialize static variables. And if you have malloc and
free, implementing new and delete is trivial. Exception handling is
currently not supported by the implementation I use, but that's mostly
because nobody bothered to implement it yet. I guess on the small
controller I mentioned above, the overhead would be way too big, but there
are controllers of the same architecture with up to 256k of program memory
and (through an external memory interface) up to 64k of data space, and
this would definitely be enough for exception handling to be reasonable.
 
L

lakshmi

Dnia Mon, 05 Mar 2007 08:15:19 +0100, Zara napisał(a):
Embedded systems written in c++ ....Did you ever felt that,
you should have used C instead of C++ ?....:)
 
I

Ian Collins

lakshmi said:
Embedded systems written in c++ ....Did you ever felt that,
you should have used C instead of C++ ?....:)
No, why should I? C++ is a very good fit in the embedded space.
 
R

Rolf Magnus

lakshmi said:
Embedded systems written in c++ ....Did you ever felt that,
you should have used C instead of C++ ?....:)

I started with C, but I felt I should have used C++ instead of C ;-)
 
Z

Zara

Embedded systems written in c++ ....Did you ever felt that,
you should have used C instead of C++ ?....:)


I started in C, felt that I shoudl have started in C++, trashed all
previous work, restarted in C++. Now my progress goes faster.

Zara
 
M

mlimber

Embedded systems written in c++ ....Did you ever felt that,
you should have used C instead of C++ ?....:)

We also use C++ on our embedded systems running TI DSPs. (BTW, the TI
compiler is quite compliant with the Standard, but it doesn't ship
with the full standard library, particularly the STL. One can easily
add STLport, though.)

Cheers! --M
 
D

Dave Rahardja

Embedded systems written in c++ ....Did you ever felt that,
you should have used C instead of C++ ?....:)

I've been writing safety-critical embedded code in C++ for years. Wouldn't
trade it for C, ever.

-dr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top