title: What is the fastest virtual machine design for x86?
body: <p>I will implement a virtual machine in x86 and I wonder what kind of design would yield best results. What should I concentrate on to squish out the juice? I will to implement the whole virtual machine in x86 assembly.</p>
  
  <p>I haven't much instructions and I can choose their form. The instructions project directly into smalltalk's syntax in blocks. I give out the instruction design I were thinking of:</p>
  
  <pre><code>^ ...       # return
  ^null     # return nothing
  object    # address to object
  ... selector: ... # message pass (in this case arity:1 selector: #selector:)
  var := ... # set
  var # get
  </code></pre>
  
  <p>The sort of VM I were thinking about:</p>
  
  <pre><code>mov eax, [esi]
  add esi, 2
  mov ecx, eax
  and eax, 0xff
  and ecx, 0xff00 # *256
  shr ecx, 5          # *8
  jmp [ecx*4 + operations]
  align 8:
      operations:
  dd retnull
  dd ret
  # so on...
      retnull:          # jumps here at retnul
  # ... retnull action
      ret:
  # ... ret action
  #etc.
  </code></pre>
  
  <p>Don't start asking why I need yet another virtual machine implementation. Interpretive routines aren't stock stuff you just pick up whenever you need them. Most virtual machines you are proposing elsewhere are weighted towards portability with the cost of performance. My goal is not the portability, my goal is the performance.</p>
  
  <p>The reason this interpreter is needed at all is because smalltalk blocks doesn't end up gotten interpreted the same way:</p>
  
  <pre><code>A := B subclass: [
      def a:x [^ x*x]
      clmet b [...]
      def c [...]
      def d [...]
  ]
  
  [ 2 &lt; x ] whileTrue: [...]
  
  (i isNeat) ifTrue: [...] ifFalse: [...]
  
  List fromBlock: [
      "carrots"
      "apples"
      "oranges" toUpper
  ]
  </code></pre>
  
  <p>I need the real benefit coming from the interpretive routines, that is the choice of context where to read the program in. Of course, good compiler should just most of the time compile the obvious cases like: 'ifTrue:ifFalse' or 'whileTrue:', or the list example. The need for interpreter doesn't just disappear because you always may hit a case where you can't be sure the block gets the treatment you expect.</p>