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.
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.