Skip to main content
MikeDB
Senior II
March 12, 2019
Solved

Self-compilation

  • March 12, 2019
  • 12 replies
  • 2088 views

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.
    Best answer by After Forever

    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
    March 12, 2019

    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
    March 12, 2019

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

    AvaTar
    Senior III
    March 12, 2019

    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
    March 12, 2019

    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
    March 12, 2019

    And how do you debug?

    MikeDB
    MikeDBAuthor
    Senior II
    March 12, 2019

    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
    March 12, 2019

    Where does compiler input come from?

    MikeDB
    MikeDBAuthor
    Senior II
    March 12, 2019

    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
    March 12, 2019

    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
    March 12, 2019

    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
    March 13, 2019

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