News for hackers. Stuff that matters.
Re:Mbeddr – engineering the future of embedded software by Nursie on Saturday September 13, 2014 @05:33AM (#8311972)
"A cleaned up version of C99 helps avoid low-level bugs. For example, the preprocessor is not supported"

NOPE.

From my cold, dead hands.

Re:Mbeddr – engineering the future of embedded software by IshKebab on Saturday September 13, 2014 @05:45AM (#8311986)
Why? The preprocessor is a huge hack that is mostly obsolete. 99% of my programs only use `#include` and `#pragma once`.
Re:Mbeddr – engineering the future of embedded software by metafex on Saturday September 13, 2014 @06:05AM (#8312007)
Macros used right can really help to clean up messy code and/or reduce repeating code to something clearer (e.g. in crypto-code).
Re:Mbeddr – engineering the future of embedded software by hrjet on Saturday September 13, 2014 @06:33AM (#8312041)
Pre-processing macros can reduce repetition, but they are certainly a hack: They are not type-safe. They are not even syntactically safe. A missing brace in one can cause untoward damage down the chain. And getting it right is especially important in crypto-code!
Re:Mbeddr – engineering the future of embedded software by com2kid on Saturday September 13, 2014 @07:32AM (#8312121)
#ifdef alone

#ifdef HW_REVISION_1

#ifdef HW_REVISION_2

etc

Re:Mbeddr – engineering the future of embedded software by mackwic on Saturday September 13, 2014 @08:23AM (#8312187)
Playing the devil's advocate, here. When possible, this kind of if_arch/else should be compartmentalized in their own object files or dynamic libraries, and then, linked conditionally to produce an unique binary.

You shouldn't clutter your code with ifdef. It makes it hard to think with.

Re:Mbeddr – engineering the future of embedded software by SixSigma on Saturday September 13, 2014 @09:42AM (#8312346)
The whole of Plan9 is written like that.

If you look here http://plan9.bell-labs.com/sources/plan9/sys/src/9/

You can see that port & boot are the portable code. Each architecture can fall back on that less optimised code should it need to.

Then each of the directories: bcm, kw, mtx, omap, pc, pcboot, ppc, rb, teg

have platform specific c files

This greatly eases porting to a new architecture and also means cross compilation is significantly easier.

#ifdefs are rare in the source code and other pragmas are few and far between.

Conditional compilation is the enemy of readable code.

Re:Mbeddr – engineering the future of embedded software by sitkack on Saturday September 13, 2014 @10:49AM (#8312504)
Moving and re-moving conditionals is our life. Is there a name for that structure? Polymorphic portability.
Re:Mbeddr – engineering the future of embedded software by vonmoltke on Saturday September 13, 2014 @11:40AM (#8312660)
The "when possible" in your statement is a caveat large enough to drive a semi through. Professionally I have only done "big" embedded, but I have for example had a rather lengthy signal processing algorithm where sections needed to be implemented one way for most POWER systems and another way for x86-64 systems. The wrong branch cratered performance[1]. There was no way to separate the code the way you describe without implementing it twice.

[1] At the risk of starting a flame war, this situation and those like it have really soured me on the rest of the industry's obsession with "big O" for complexity analysis. I have never found it useful; I always needed to pay attention to the stuff I was supposed to be dropping.

Re:Mbeddr – engineering the future of embedded software by mackwic on Saturday September 13, 2014 @11:52AM (#8312695)
> The "when possible" in your statement is a caveat large enough to drive a semi through

You are absolutely right. It's a real effort of architecture, and sometimes one of the marks of a great projects. EDIT (thinking of a bad experience of mine): Sometimes, this compartmentalization is also unnecessary and force the architecture to be far worse, too loosely coupled and hard to follow.

> There was no way to separate the code the way you describe without implementing it twice.

Your mileage may vary but implementing it twice can be the best solution if there was so many dependencies to the platform. You should then document the algorithm and reference this documentation in each implementation so that they don't diverge too much. That's not always the best solution, thought. There's no silver bullet in this world...

Re:Mbeddr – engineering the future of embedded software by vonmoltke on Saturday September 13, 2014 @12:06PM (#8312732)
> You are absolutely right. It's a real effort of architecture, and sometimes one of the marks of a great projects. EDIT (thinking of a bad experience of mine): Sometimes, this compartmentalization is also unnecessary and force the architecture to be far worse, too loosely coupled and hard to follow.

Well, the architecture of what I was working on was not going to win any awards. :P It was made worse because we were converting code that was originally implemented to run on DSPs.

> Your mileage may vary but implementing it twice can be the best solution if there was so many dependencies to the platform. You should then document the algorithm and reference this documentation in each implementation so that they don't diverge too much. That's not always the best solution, thought. There's no silver bullet in this world...

Standard practice was for systems engineers[1] to create Algorithm Description Documents and Algorithm Implementation Documents that explained in mathematical formulas (ADD) and pseudocode (AID) what the code was doing. At least. that was the theoretical practice. The actual practice was a mess. But yes, there is no silver bullet.

[1] Which was me, even though I got to write lots of code, too.

Re:Mbeddr – engineering the future of embedded software by mackwic on Saturday September 13, 2014 @12:31PM (#8312812)
Never done any DSP. How does it change the paradigms ?

> At least. that was the theoretical practice. The actual practice was a mess. :( This another big problem yet to solve...

Re:Mbeddr – engineering the future of embedded software by vonmoltke on Saturday September 13, 2014 @01:49PM (#8313032)
There were two big architectural differences that created some weird (to developers used to conventional processors) code.

First was that the chips were fixed-point. That mainly changed the arithmetic of the individual algorithm steps, but it led to some really strange number packing schemes[1] that affected memory layout and added a bunch of code just for dealing with them.

Second was the concept of "on-chip" and "off-chip" memory. The chips did not have a conventional memory bus. Instead, the program had to manually make a DMA request to move data on-chip for processing. This was basically a stack memory space local to the DSP core. It was the only memory space the core could access directly, though. Thus, algorithms had to be implemented such that they moved memory on-chip in blocks, processed the data, then moved it back off-chip. Since these movements were expensive, the implementation also tried to chain as many things together on the core in order to minimize the DMA transactions.

Needless to say, these make for extremely inefficient code in some cases when you try to recompile on a real processor or controller that actually has an FPU and memory controller.

[1] If you have never tried working with complex numbers on a fixed-point processor, don't.

Re:Mbeddr – engineering the future of embedded software by linuxlizard on Saturday September 13, 2014 @10:56AM (#8312528)
While I dislike #if/#else/#endif, dynamic libraries rarely exist in embedded systems.

Using many small object files is my solution of choice but it pushes the if/else/endif problem into the Makefile instead.

Re:Mbeddr – engineering the future of embedded software by mackwic on Saturday September 13, 2014 @11:58AM (#8312715)
I personally tend to use yaml files which contains matrices of compatibility between objects, used to generate CMake statements.

I know people who use the file header to autodetect the right architecture but I found it too overkill and weird-bug-prone.

Re:Mbeddr – engineering the future of embedded software by com2kid on Sunday September 14, 2014 @07:09PM (#8316555)
I actually agree, none of my code has #IFDEF, except at the top of the C file that decides if that C file will be built or not. (This simplifies having different C files per platform in the build system, adding new projects in our system is complicated enough as is! Right now it just builds .c, .cpp in every directory)