Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Virtual Machines

Introduction to Virtual Machines

Sasha (Oleksandr) Syrotenko

December 07, 2018
Tweet

More Decks by Sasha (Oleksandr) Syrotenko

Other Decks in Programming

Transcript

  1. About speaker Masters student of Kyiv National University, cybernetics Current

    employee of @Aeneas, ex-employee of SAP C++, JavaScript, Scala/Java/Kotlin, sometimes Rust Huge compliers, runtimes and formal verification fan Sometimes making WASM bulk operations and GC in ChakraCore • • • • • 2
  2. Agenda Reasons Technical duty Reference creates a knowledge, knowledge creates

    an efficiency Interesting low-level technologies +1 well answer on your interview • • • • 4
  3. Each Virtual Machine is implementation of abstract machine which is

    executed the code was written in specified language. “ 6
  4. Abstract machine specifications JavaScript VMs : ECMA-262 specification Java VMs

    : JVMS (Java Virtual Machine Specification) C# VMs : Common Language Runtime Specification Erlang VM : BEAM (Björn's Erlang Abstract Machine) • • • • 8
  5. Abstract machine implementations JavaScript VMs : V8, ChakraCore, JSCore, SpiderMonkey,

    TeaVM, etc Java VMs : HotSpot, SAP Machine, Zing VM, Excelsior JET, Dalwik, etc C# VMs : CoreCLR, DotNetCore, Mono Erlang VM : BEAM • • • • 9
  6. Virtual Machines types Stack-based VM Register-based VM Stack A lot

    of virtual registers Uses ip, fp, sp registers Uses ip, fp, sp, lp registers Bytecode uses stack as a data source Data stores in virutal registers 11
  7. 12

  8. Memory model Describes threads interaction through memory Clever words like

    Program order of execution Synchronous order of execution Synchronizes-with relation Happens-before relation ECMA-262 : Chapter 27.6 • • • • • • • 16
  9. Source code to bytecode translator Lexer Parser (could be concurrent)

    AST builder AST phase optimizer Bytecode generator / emitter SSA-form builder, if register-based • • • • • • 18
  10. Bytecode verifier & executor Bytecode verifier Proves if generated bytecode

    is valid by specification The easiest implementation detail in VM ;) Bytecode executor Executes bytecode if no optimizations required Collects execution profiling data • • • • • • 19
  11. Just in time (JIT) compiler Dynamically optimizes bytecode The reason

    why JS is so fast Makes simple optimizations like constant propagation/folding, etc Makes strong optimizations like escape analysis and methods inlining Provides speculative optimizations like branch prediction, etc • • • • • 20
  12. JIT compiler trade-offs Decreases latency of execution, but increase memory

    footprint Provides trade-off between optimizations quiality and execution start Provides deoptimizations if required • • • 21
  13. Machine code generator (part of JIT) Executes machine-dependent optimizations Transforms

    optimized bytecode to the Control Flow Graph Generates final assembly instructions from CFG • • • 22
  14. Native interface Provides interface for hardware devices access, like GPU

    Allows direct offheap allocations Sometimes it is a secret door to black magic Classloader Contains in VMs with staticly typed target language Loads and stores metainformation about data structures Big plus : allows effective allocation strategies • • • • • • • • 24
  15. jsvu Code samples $ npm i g $ jsvu $

    v8 $ spidermonkey $ chakra $ jsc 01. 02. 03. 04. 05. 06. 26
  16. Ecpecial properties of JS VM They have separare bytecode interpreter

    and one (or more) JIT They make very desperate optimizations about type predictions Deoptimize bytecode if type predictions turns out to be wrong • • • 27
  17. Conclusion Any VM is an implementation of specified abstract machine

    JIT is the reason why JS gains big speedup a few years ago It is good to know a weak sides of your low-level code executor Knowledge is power! • • • • 32