Is posible transpiling code for better performance?

I was thinking to edit registers directly for better performance in my microcontroller instead of using Arduino Framework, like this video:

But is really hard coding in this way.
Does PlaformIO has a way to transpile the code before compile? I mean, I have this:

pinMode(2, OUTPUT);

But before compile I want that turn into this:

DDRD = B00100000;
PORTD = B00000000;

Just GitHub - NicksonYap/digitalWriteFast: Arduino library for faster digitalWrite using port manipulation and macro for ease in pin assignments.

Thanks, but is not my solution, is only compatible with Arduino boards, and I’m using just Atmel microtroller, but there is one library compatible with my model.

I really want a transpiler, so I can share the final code before compile.

You said you have this. pinMode(2, OUTPUT);. Install the library in the project and use pinModeFast() instead.

I said TRANSPILING is not the same to install a library, a library was my first thought, it’s simplified my code time, but doesn’t generate a new file .cpp file.

The only way of turning pinMode(2, OUTPUT); into the most efficient assembly code for setting the data direction registers is the code found in that library, which uses some nice compile-time tricks to achieve that. Based on your wanted end result, this is what you’re looking for.

Otherwise, let me know when you find that Arduino API to atmel register writes transpiler.

I suggest researching how “inline compilation” works - it’s the same thing as replacing a call with the code inside the function, and then optimising away all unnecessary code. So the result of what you’re asking is the same as using well-crafted inline functions.

Or perhaps you don’t want the Arduino runtime library code to be included in your project? In that case, have a look at the AVR Libc Home Page. But it’s been a long time since I’ve used that, it might be obsolete by now (and it’s probably not supported in PlatformIO).

Exactly! This and other tricks is exactly what digitalWriteFast() is using. The other part is GCC magic for guaranteed constant propagation. There is no need for a transpiler to convert one source file into another source file to get the most efficient binary output, since that library already does exactly that by instructing GCC to do so. In fact, using a transpiler (which I’m very sure does not exist for Arduino in the use case presented here) or creating one would introduce another level of complexity that one should generally avoid.

I don’t think the same. Arduino is just a framework, a transpile just transforms the functions. For example, there is a ton of transpiler for JavaScript, they convert arrow function () => {} to normal function function () {}, const variables replaces by his value for save memory. It doesn’t add any level of complexity, just optimize the code.

The transpiler is for the IDE, not the library… in this weekend I made a simple transpiler in python for mode and write using regex. If I have pinMode(2,OUTPUT) it converted to PORTD = 0b0000010 in a new file. With more time, I think I could increase the optimization.