2

Back in the days before caches and branch prediction, it was relatively common if not encouraged to make self-modifying code for certain kinds of optimizations. It was probably most common in games and demos written in assembler in the eras between 8-bit up to early 32-bit such as the Amiga.

I'm not sure if any compilers from those days emitted self-modifying assembler or machine code.

What I'm wondering is whether there are any current/modern compilers that do. Obviously it would be useless or too difficult on powerful processors with caches.

But What about the very many simple processors such as used in embedded systems? Is self modifying code considered a viable optimization strategy by any modern compilers for any simple / 8-bit / embedded processor?


There is a question with a similar title, "Is there any self-improving compiler around? ", but note that it's not about the same subject:

Please note that I am talking about a compiler that improves itself - not a compiler that improves the code it compiles.

Community
  • 1
  • 1
hippietrail
  • 15,848
  • 18
  • 99
  • 158
  • 1
    It was probably a lot more common via the COBOL ALTER verb in the 1960s. – user207421 Oct 25 '12 at 00:31
  • 1
    The old compilers for 8-bit and Amiga/Atari did not generate very efficient code at all so I'm pretty sure they did not use tricks like that. Also for embedded systems code is often in ROM so self-modification is not an option. But it is an interesting technique :) – Ville Krumlinde Oct 25 '12 at 07:41
  • @VilleKrumlinde: Oh I forgot about the ROM factor - thanks for that. – hippietrail Oct 25 '12 at 07:46
  • I would be amazed if any compiler generated self-modifying code, even as an option. Apart from the ROM issue, it's not interrupt/thread safe. – Martin James Oct 25 '12 at 11:44
  • @MartinJames: Me too but I've been amazed a few times before - there's some strange stuff out there (-: – hippietrail Oct 25 '12 at 11:48
  • 1
    @hippietrail - if you do find a compiler that uses such an abominable 'optimization', switch it off or find another compiler:) – Martin James Oct 25 '12 at 11:50

1 Answers1

4

All embedded systems today use flash ROM. I believe Amiga and similar were RAM-based systems. The only way "self-modification" exists in embedded systems, is where you have boot loaders, that re-program certain parts of the flash depending on what program and/or functionality that should be used.

Apart from that, it doesn't make sense for the program to modify itself in runtime. Running code from RAM is generally discouraged, for safety reasons (potential of accidental modifications caused by bugs) and for electric reasons (RAM is volatile and far more sensitive to EMC than flash).

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Well the Amiga had a ROM which had more in it than a PC BIOS but not the entire OS, much of which was loaded into RAM at boot time. I don't recall how the responsibilities were divided. – hippietrail Dec 20 '12 at 00:38