I've been playing around with a simple stack-based language, and one of the things that I've found myself doing repeatedly is manually optimizing chunks of code.
I figured "hey, this looks very much like something that a computer can do! Repetitive work with a clear goal and semantics.". But looking around, I can't find much of anything on optimizing stack machine code. Register machines, yes. But not stack-based languages. It seems like the general response to "how do you optimize stack machine code?" is "don't."
So: how does one go about optimizing stack machine code? Are there any general methods beyond simple peephole optimizations? Are there any methods of generating peephole optimizations automatically?
1 Wikipedia - Stack Machine lists some stack machines and stack-based languages (that might somehow address the optimization)
1.1 my reading of the various links led me to the conclusion that stack-based machines are just a theoretical tool. No need to "optimize" them as in order to run a program such machines translate the stack-based code into a register-based code, optimize it and run it on register-based hardware. This process is usually called Just In Time compilation (JIT) or Ahead Of Time compilation (AOT)
2 out of the listed options Wikipedia - FORTH describes >40 years old wide-spread stack-based programming language (there should be some optimization-related materials available)
3 Google search for "forth stack machine optimization" gives some lots of articles, e.g.