←back to thread

156 points cpldcpu | 6 comments | | HN request time: 0.198s | source | bottom
1. Someone ◴[] No.41895673[source]
FTA: “One major issue when programming these devices in C is that every function call consumes RAM for the return stack and function parameters. This is unavoidable”

It’s not completely unavoidable: don’t use function parameters (globals are your friends on these CPUs). You can’t avoid having a return stack, but you can make as few function calls as possible (ideally zero, but you may have to write functions to fit things into ROM)

> *”To solve this, I flattened the inference code”

I think that’s “make as few function calls as possible”

> and implemented the inner loop in assembly to optimize variable usage.

That _should_ only make a difference for memory usage if your C compiler isn’t perfect (but of course, it never is, certainly on CPUs like this one, which is a poor fit for C)

replies(4): >>41895876 #>>41896253 #>>41897172 #>>41898306 #
2. dpassens ◴[] No.41895876[source]
You could kind of avoid the return stack, if you only ever do tail calls. Obviously, that's pretty unrealistic, but it's possible.
replies(1): >>41896707 #
3. cpldcpu ◴[] No.41896253[source]
>That _should_ only make a difference for memory usage if your C compiler isn’t perfect

Considering that the PMC150 has an accumulator based 8 bit architecture which is almost hostile to C, it is safe to assume that the compiler is not perfect :)

4. ska ◴[] No.41896707[source]
Assuming your compiler implements TCO properly, also.
5. whobre ◴[] No.41897172[source]
I bet something like Forth would work better on such a microcontroller. It is known to produce very high code density and embedding assembly is usually very straightforward.
6. varispeed ◴[] No.41898306[source]
There is probably a way to change calling convention to use something else instead of stack.