6

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?

TLW
  • 1,373
  • 9
  • 22
  • 1
    to make your question answerable, not too broad and on-topic - please add code sample of RoboWar code before optimization and same code after your manual optimization (http://stackoverflow.com/help/mcve). For instance whole [Java Virtual Machine](http://en.wikipedia.org/wiki/Jvm) is also stack based. Used on desktops, servers, Android etc. – xmojmr Jul 07 '14 at 07:37
  • After thinking about it for a bit, I realized something. RoboWar is just a context - I am interested in stack machine optimization techniques in general, not specifically for RoboWar. I have removed the discussion of RoboWar entirely, lest people get the wrong idea. – TLW Jul 07 '14 at 14:27
  • although I tried to give you some answer, I'm voting -1 as this question is too broad, does not show any code, very little research effort was shown and does not fit the site [on-topic](http://stackoverflow.com/help/dont-ask) format. If you want code-less question you should move it to http://programmers.stackexchange.com/ or even better to http://cs.stackexchange.com/ – xmojmr Jul 07 '14 at 17:02

1 Answers1

6

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.

xmojmr
  • 8,073
  • 5
  • 31
  • 54
  • "my reading of the various links led me to the conclusion that stack-based machines are just a theoretical tool." So is Brainfuck, and yet there are still optimizers for it. – TLW Jul 06 '16 at 22:26
  • The links were helpful, however. – TLW Jul 06 '16 at 22:26
  • 1
    You call stack machines a theoretical tool, and then you referenced the J1 CPU which is an actual hardware stack machine, which doesn't translate the stack-based code into register-based code, but runs it as real stack-based code. – Rocketmagnet Nov 09 '18 at 21:47