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?