Virtual Machines and JITs Prof. Tom Austin San Jos State - - PowerPoint PPT Presentation

virtual machines and jits
SMART_READER_LITE
LIVE PREVIEW

Virtual Machines and JITs Prof. Tom Austin San Jos State - - PowerPoint PPT Presentation

CS 252: Advanced Programming Language Principles Virtual Machines and JITs Prof. Tom Austin San Jos State University A Review of Compilers Lexer/ source tokens Parser code Tokenizer Abstract Compiler Interpreter Syntax Tree (AST)


slide-1
SLIDE 1

CS 252: Advanced Programming Language Principles

  • Prof. Tom Austin

San José State University

Virtual Machines and JITs

slide-2
SLIDE 2

A Review of Compilers

Lexer/ Tokenizer Parser

source code tokens

Abstract Syntax Tree (AST) Compiler

Machine code

Interpreter

Commands

slide-3
SLIDE 3

Virtual Machines (VM)

  • Code is compiled to bytecode

–low-level –platform independent

  • The VM interprets bytecode
slide-4
SLIDE 4

Lab: Scheme VM

In today's lab, you will implement:

  • a compiler for Scheme
  • a stack-based VM
slide-5
SLIDE 5

Input program

(println (+ 2 3 4)) (println (- 13 (* 2 4))) (println (- 10 4 3))

slide-6
SLIDE 6

Supported VM Operations

  • PUSH – adds argument to stack
  • PRINT – pops & prints top of stack
  • ADD

–pops top two elements –adds them together –places result on stack

  • SUB – subtraction
  • MUL – multiplication
slide-7
SLIDE 7

Bytecode Output

PUSH 2 PUSH 3 ADD PUSH 4 ADD PRINT PUSH 13 PUSH 2 PUSH 4

MUL SUB PRINT PUSH 10 PUSH 4 SUB PUSH 3 SUB PRINT

slide-8
SLIDE 8

Lab – Write a Compiler and a VM

  • Starter code is provided.
  • println is functional.
  • Your job: add support for the

mathematical operators.

slide-9
SLIDE 9

EXTRA CREDIT

Add compiler support for

  • if expressions
  • boolean variables
  • let expressions

Add VM support for

  • labels
  • Jump

(JMP/JZ/JNZ)

  • perations
  • STOR/LOAD
  • perations
slide-10
SLIDE 10

Compiler or Interpreter?

  • Compilers

–efficient code

  • Interpreters

–runtime flexibility

  • Can we get the best of both?
slide-11
SLIDE 11

Just-in-time compilers (JITs)

  • interpret code
  • "hot" sections are compiled

at run time

slide-12
SLIDE 12

JIT tradeoffs

+Speed of compiled code +Flexibility of interpreter

  • Overhead of both approaches
  • Complex implementation
slide-13
SLIDE 13

Dynamic recompilation

  • JIT pursues aggressive
  • ptimizations

–make assumptions about code –guard conditions verify assumptions

  • Unexpected cases interpreted
  • Can outperform static compilation
slide-14
SLIDE 14

Types of JITs

  • Method based

–Compiles methods

  • Trace based

–Compiles loops –Gal et al. 2009 http://www.stanford.edu/class/cs34 3/resources/tracemonkey.pdf

slide-15
SLIDE 15

Trace-based JIT design (Gal et al. 2009)

slide-16
SLIDE 16

How can a language designer make use of a JIT?

  • 1. Become an expert in JITs

–study the latest techniques –build large code bases to test –profile your code execution

  • 2. Use someone else's JIT-ed VM
slide-17
SLIDE 17