11

Click here to load reader

James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

  • Upload
    buique

  • View
    215

  • Download
    3

Embed Size (px)

Citation preview

Page 1: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

Compilers vs. Interpreters

• Compiler: Translate once, then run the result e.g. translate C++ into 80x86machine language→ stand-alone code, faster execution

• Interpreter: Read-check-execute loop, e.g. CAML or Prolog environment→ good for learning/debugging, (slower, not stand-alone)

SE209 Intermediate Representations - 1

Page 2: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

Intermediate Representation

• Like an abstract assembly language, for an imaginary machine

• Make no assumptions about target architecture (e.g. number of registers,what operations are fastest etc.)

• But ... make it “look like” the source language

SE209 Intermediate Representations - 2

Page 3: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

Three (old) reasons to use an IR

1. Re-targeting a compiler onto a different architecture

2. Speeding up the loading of library files in an interpreter

3. “Half-way” between source and target language - make thecompiler/interpreter’s job a bit easier

SE209 Intermediate Representations - 3

Page 4: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

1. Re-targeting a Compiler

• Have e.g. a C to 80x86 compiler

• Want a C to Sparc compiler→ Should only have to re-write the back end

• Want a C++ to 80x86 compiler→ should only have to re-write front end

Targeting M languages on N machines:- without IR: M ×N compilers- with IR: M +N compilers

SE209 Intermediate Representations - 4

Page 5: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

2. Speeding up an Interpreter

• Will be using library code a lot

• Don’t want to have to load it in, do all the analysis (syntax and semantics)and all the translation

• Solution: pre-“compile” the libraries into IR

• Make your interpreter translate input into IR, then execute

• Also makes your interpreter more portable

SE209 Intermediate Representations - 5

Page 6: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

3. Easing the Job for the Compiler/Interpreter

High-Level Language Assembly Language

Types int, double, boolean, char,String, BankAccount, ....

byte, word...

Variables Local, static, private, pointers,...

registers, memory

Expressions 2+3*4-Math.PI addl %edx,(%eax)Control if, while, do...while, for, .... test-and-jump

Abstraction static/virtualfunctions/methods, pass byvalue/reference

subroutines, arguments on stack

Extras: Inheritance, classes, structs,unions, ...

...?

SE209 Intermediate Representations - 6

Page 7: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

Java’s Reasons for using an IR

• Embedded systems - wanted portable code

• Internet - want mobile code

• E-Commerce - want secure mobile code

• (?) Software Engineering - want safe code

• (?) SUN vs. MS - really need portable code

SE209 Intermediate Representations - 7

Page 8: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

Java’s Architecture

• the Java Application Programming Interface (API)

• the Java programming language

• the Java class file format

• The Java Virtual Machine (JVM)= class loader + execution engine

“Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.”

SE209 Intermediate Representations - 8

Page 9: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

Java’s IR - the .class file format

• Knows about classes, objects, methods

• Doesn’t have if-else/while - uses jumps

• Doesn’t use registers - stack-based machine

• Extra safeguards for security

• More later.....

SE209 Intermediate Representations - 9

Page 10: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

JVM - Loading Classes

• There’s a part of the JVM called the Class Loader

• The Class Loader supplies bytecodes (from .class files) to the bytecodeinterpreter

• You get one (trusted) “primordial” Class Loader

• You can create your own Class Loaders (with various levels of privileges)

• There’s a class: java.lang.ClassLoader

SE209 Intermediate Representations - 10

Page 11: James Power Compilers vs. Interpreters - Maynooth …jpower/Courses/Previous/se209/inter/inter.pdf · James Power Compilers vs. Interpreters Compiler: ... Easing the Job for the Compiler/Interpreter

James Power

JVM - The Execution Engine

Options:

• interpret everything

• compile everything

• just-in-time compiler (JIT) - compile as you run code the first time

• hotspot technology - compile “busy” parts of code

SE209 Intermediate Representations - 11