Skip to main content
MikeDB
Senior II
Mar 12, 2019 10:21 AM UTC
Solved

Self-compilation

Has anybody implemented a small compiler that runs on an STM32 M3 or M7 to produce code for itself. Must be a compiler, not interpreter, as runtime speed is paramount. Doesn't need to be a complete C++ instruction set, a mini-C or something like that would be fine.

    This topic has been closed for replies.
    After Forever
    After ForeverBest answer
    Senior III
    Mar 12, 2019 11:10 AM UTC

    There's this interesting project - https://github.com/jserv/amacc

    But unfortunately it's for ARMv7-A, not ARMv7-M.

    Anyway, you can try to find an interpreter/vm (Lua, Forth, etc.) with a JIT compilation instead of C compiler. [Click Show More]

    For example, here's a "Stack" VM implementation with JIT for micro:bit (ARMv6-M) - https://github.com/thomasdenney/microjit

    12 replies

    After Forever
    After ForeverBest answer
    Senior III
    Mar 12, 2019 11:10 AM UTC

    There's this interesting project - https://github.com/jserv/amacc

    But unfortunately it's for ARMv7-A, not ARMv7-M.

    Anyway, you can try to find an interpreter/vm (Lua, Forth, etc.) with a JIT compilation instead of C compiler. [Click Show More]

    For example, here's a "Stack" VM implementation with JIT for micro:bit (ARMv6-M) - https://github.com/thomasdenney/microjit

    MikeDB
    MikeDBAuthor
    Senior II
    Mar 12, 2019 11:14 AM UTC

    JIT is too slow sorry. I'll have a look at the first one and see if can be converted

    AvaTar
    Senior III
    Mar 12, 2019 11:32 AM UTC

    Minding the limited RAM and absence of a MMU on a Cortex M MCU, this does not make sense practically. As exercise, perhaps.

    MikeDB
    MikeDBAuthor
    Senior II
    Mar 12, 2019 11:47 AM UTC

    I once had perfectly good C and Pascal compilers on an early PC with 256K RAM and floppy discs so I think a 1M RAM H series STM32 with about 1000 times the processing power should be up to the job.

    S.Ma
    Principal
    Mar 12, 2019 4:51 PM UTC

    And how do you debug?

    MikeDB
    MikeDBAuthor
    Senior II
    Mar 12, 2019 5:54 PM UTC

    Same as in the 80s - printing to the LCD. This is only a few hundred k of code tops, not the multi-megabyte stuff that obviously needs modern facilities on an attached PC.

    Uwe Bonnes
    Chief
    Mar 12, 2019 6:33 PM UTC

    Where does compiler input come from?

    MikeDB
    MikeDBAuthor
    Senior II
    Mar 12, 2019 7:18 PM UTC

    A flash card. Main development would be on a PC of course as its more convenient, but I want users to be able to alter a line of code on site without having to find a PC that has the complete ARM development suite on it. The editor of course would also be more simplistic than Visual Studio or whatever, but I have a solution to that. Problem is a small compiler that produces ARM assembly code.

    S.Ma
    Principal
    Mar 12, 2019 7:28 PM UTC

    What is the intrisic need to generate machine code within the MCU ? Patch FW ?

    Most MCU devices don't necessarily have Displays or console connexions. Take a Rice cooker or an e-bike.

    MikeDB
    MikeDBAuthor
    Senior II
    Mar 12, 2019 7:44 PM UTC

    It's not a rice cooker :) It has six H750s and a dozen F103s with several LCDs as a minimum configuration. I don't want to discuss the application, but being able to make small changes in the field without a PC loaded with a complete STM32 development system is a definite plus. The code uses a very small subset of C++ (more C really) so it's not undoable.

    AvaTar
    Senior III
    Mar 13, 2019 6:27 AM UTC

    > The code uses a very small subset of C++ (more C really) so it's not undoable.

    That's true, but it is not the problem.

    The problem is, MCUs are made for specific use cases. And practically no other user needs such a thing. Except you ...

    I believe there are small compilers/linkers around you can port to the ARM CM4.

    Or try the MP1 (or a competitors Cortex A - Cortex M multicore), which would give you a full-blown toolchain, even with SSH console and/or graphics GUI.