Docsity

Virtual Machines: Emulation, Interpretation, and Binary Translation - Prof. Prasad Kulkarn, Study notes of Deductive Database Systems

Partial preview of the text

Download Virtual Machines: Emulation, Interpretation, and Binary Translation - Prof. Prasad Kulkarn and more Study notes Deductive Database Systems in PDF only on Docsity!

Emulation – Outline

• Emulation

• Interpretation

– basic, threaded, directed threaded

– other issues

• Binary translation

– code discovery, code location

– other issues

• Control Transfer Optimizations

Key VM Technologies

• Emulation – binary in one ISA is executed in

processor supporting a different ISA

• Dynamic Optimization – binary is improved for

higher performance

– may be done as part of emulation

– may optimize same ISA (no emulation needed)

HP PA ISA HP UX HP Apps. Optimization Alpha Windows X86 apps Emulation

Definitions

• Guest

– environment being

supported by

underlying platform

• Host

– underlying platform

that provides guest

environment

Guest

Host

supported by

Definitions (2)

• Source ISA or binary

– original instruction set or binary

– the ISA to be emulated

• Target ISA or binary

– ISA of the host processor

– underlying ISA

• Source/Target refer to ISAs

• Guest/Host refer to platforms

Source

Target

emulated by

Interpretation Vs. Translation

• Interpretation

– simple and easy to implement, portable

– low performance

– threaded interpretation

• Binary translation

– complex implementation

– high initial translation cost, small execution cost

– selective compilation

• We focus on user-level instruction set emulation of

program binaries.

Interpreter State

• An interpreter needs to

maintain the complete

architected state of the

machine implementing

the source ISA

– registers

– memory

• code

• data

• stack

Code Data Stack Program Counter Condition Codes Reg 0 Reg 1 Reg n- . . . Interpreter Code

Decode – Dispatch Interpreter (2)

• Instruction function: Load

LoadWordAndZero(inst){ RT = extract (inst,25,5); RA = extract (inst,20,5); displacement = extract (inst,15,16); if (RA == 0) source = 0; else source = regs[RA]; address = source + displacement; regs[RT] = (data[address]<< 32)>> 32; PC = PC + 4; }

Decode – Dispatch Interpreter (3)

• Instruction function: ALU

ALU(inst){ RT = extract (inst,25,5); RA = extract (inst,20,5); RB = extract (inst, 15,5); source1 = regs[RA]; source2 = regs[RB]; extended_opcode = extract (inst,10,10); switch(extended_opcode) { case Add: Add (inst); case AddCarrying: AddCarrying (inst); case AddExtended: AddExtended (inst);

.. .} PC = PC + 4; }

Indirect Threaded Interpretation

• High number of branches in decode-dispatch

interpretation reduces performance

– overhead of 5 branches per instruction

• Threaded interpretation improves efficiency by

reducing branch overhead

– append dispatch code with each interpretation

routine

– removes 3 branches

– threads^ together function routines

Indirect Threaded Interpretation (2)

LoadWordAndZero:

RT = extract (inst,25,5);

RA = extract (inst,20,5);

displacement = extract (inst,15,16);

if (RA == 0) source = 0;

else source = regs(RA);

address = source + displacement;

regs(RT) = (data(address)<< 32) >> 32;

PC = PC +4;

If (halt || interrupt) goto exit;

inst = code[PC];

opcode = extract (inst,31,6)

extended_opcode = extract (inst,10,10);

routine = dispatch[opcode,extended_opcode];

goto *routine;

Indirect Threaded Interpretation (4)

  • Dispatch occurs indirectly through a table
    • interpretation routines can be modified and relocated independently
  • Advantages
    • binary intermediate code still portable
    • improves efficiency over basic interpretation
  • Disadvantages
    • code replication increases interpreter size

Indirect Threaded Interpretation (5) source code dispatch loop interpreter routines "data" accesses

Decode-dispatch

source code interpreter routines

Threaded

Predecoding (2)

struct instruction {

unsigned long op;

unsigned char dest, src1, src2;

} code [CODE_SIZE];

Load Word and Zero:

RT = code[TPC].dest;

RA = code[TPC].src1;

displacement = code[TPC].src2;

if (RA == 0) source = 0;

else source = regs[RA];

address = source + displacement;

regs[RT] = (data[address]<< 32) >> 32;

SPC = SPC + 4; TPC = TPC + 1;

If (halt || interrupt) goto exit;

opcode = code[TPC].op

routine = dispatch[opcode];

goto *routine;

Direct Threaded Interpretation

• Allow even higher efficiency by

– removing the memory access to the centralized table

– requires predecoding

– dependent on locations of interpreter routines

  • loses^ portability

001048d

(load word and zero)

(add) (store word)