• Please review our updated Terms and Rules here

Python

Thanks, I forgot about that. I was aware that they're used as a cache for imported modules, but this stackoverflow question explains how you can compile the main executable scripts too, and then run them via the interpreter. Unfortunately that has the small drawback that you now have to run python3 foo.pyc instead of (if your file had a #!/usr/bin/env python3 or similar line) just ./foo.py.

.pyc files aren’t always smaller than the original source; it’s not usual at all for them to actually be larger. The point of them isn’t to ‘shrink’ the code, it’s to save the effort of repeatedly converting that nasty human readable junk into byte code that can be directly executed by the virtual machine. Full-fat Python simply is not designed to care much about optimizing for memory savings over performance.

MicroPython/Circuitpython *do* have a size-optimized compiled file format; code processed this way has the .mpy extension:


This format offers a few interesting options, including in some cases the ability to directly embed native machine code modules…

But per my original point, if you’re *compiling* things for your microcontroller, well… MicroPython needs a *minimum* of 256k of flash before you even get to your user code. That’s 256k of flash for “hello world” no matter what you do. With C you can get that done in little more than assembly would require.

(As for performance, well… I’ve done some bit-bang-y work with microcontrollers where I resorted to embedded assembly because a loop in C required just one or two more processor clock ticks than the hand-tuned ASM version… we’re literally talking about the difference between 7 and 9 ticks per iteration. Something tells me it’s not going to be that close with MicroPython.) ;)
 
As someone who wrote a Java VM for the 6502, you will have a LOT of work ahead of you. At least the Java VM is really nothing more than a glorified p-code interpreter and I could do the compilation on a modern machine. Even still, cramming a 32 bit virtual memory environment into a 1 MHz, 8 bit, 64K machine was all folly in the end. I did get it functional but it was not particularly useful. The swapping made it unusable for all but the most trivial of programs.

If it also had to parse and compile source into bytecode would mean an unbearably long process. It would probably still be compiling 15 years later.

But that’s not to say something a little less comprehensive wouldn’t be worth challenging yourself with. Everything I learned from the Java VM I applied to PLASMA and ended up with the environment I always wanted.
 
Back
Top