performance - Implementing registers in a C virtual machine -


i've written virtual machine in c hobby project. virtual machine executes code that's similar intel syntax x86 assembly. problem registers virtual machine uses registers in name. in vm code, registers used x86 registers, machine stores them in system memory. there no performance improvements using registers on system memory in vm code. (i thought locality alone increase performance somewhat, in practice, nothing has changed.)

when interpreting program, virtual machine stores arguments instructions pointers. allows virtual instruction take memory address, constant value, virtual register, or argument.

since hardware registers don't have addresses, can't think of way store vm registers in hardware registers. using register keyword on virtual register type doesn't work, because have pointer virtual register use argument. there way make these virtual registers perform more native counterparts?

i'm comfortable delving assembly if necessary. i'm aware jit compiling vm code allow me utilize hardware registers, i'd able use them interpreted code well.

  1. machine registers don't have indexing support: can't access register runtime-specified "index", whatever mean, without code generation. since you're decoding register index instructions, way make huge switch (i.e. switch (opcode) { case add_r0_r1: r[0] += r[1]; break; ... }). bad idea since increases interpreter loop size much, introduce instruction cache thrashing.

  2. if we're talking x86, additional problem amount of general-purpose registers pretty low; of them used bookkeeping (storing pc, storing vm stack state, decoding instructions, etc.) - it's unlikely you'll have more 1 free register vm.

  3. even if register indexing support available, it's unlikely give lot of performance. commonly in interpreters largest bottleneck instruction decoding; x86 supports fast , compact memory addressing based on register values (i.e. mov eax, dword ptr [ebx * 4 + ecx]), not win much. it's worthwhile though check generated assembly - i.e. make sure 'register pool' address stored in register.

  4. the best way accelerate interpreters jitting; simple jit (i.e. without smart register allocation - emitting same code execute instruction loop , switch statement, except instruction decoding) can boost performance 3x or more (these actual results simple jitter on top of lua-like register-based vm). interpreter best kept reference code (or cold code decrease jit memory cost - jit generation cost non-issue simple jits).


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -