Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

28
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011

description

Overview Ch.7: AST Ch.8-9: Semantic analysis Ch.10: Intermediate representation Ch.11: Code generation for a virtual machine Ch.12: Runtime support Ch.13: Target code generation

Transcript of Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Page 1: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Chap. 10, Intermediate Representations

J. H. WangDec. 27, 2011

Page 2: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Outline• Overview• Java Virtual Machine• Static Single Assignment Form

Page 3: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Overview• Ch.7: AST• Ch.8-9: Semantic analysis• Ch.10: Intermediate representation• Ch.11: Code generation for a virtual

machine• Ch.12: Runtime support• Ch.13: Target code generation

Page 4: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Overview• Semantic gap between high-level

source languages and target machine language

• Examples– Early C++ compilers

• cpp: preprocessor• cfront: translate C++ into C• C compiler

Page 5: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Page 6: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Another Example• LaTeX

– TeX: designed by Donald Knuth– dvi: device-independent intermediate

representation– Ps: PostScript– pixels

• Portability enhanced

Page 7: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Page 8: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Challenges • Challenges

– An intermediate language (IL) must be precisely defined

– Translators and processors must be crafted for an IL

– Connections must be made between levels so that feedback from intermediate steps can be related to the source program

• Other concerns– Efficiency

Page 9: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

The Middle-End• Front-end: parser• Back-end: code generator• Middle-end: components between front-

and back-ends• Compiler suites that host multiple source

languages and target multiple instruction sets obtain great leverage from a middle-end– Ex: s source languages, t target languages

• s*t vs. s+t

Page 10: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Page 11: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Additional Advantages• An IL allows various system components to

interoperate by facilitating access to information about the program– E.g. variable names and types, and source line

numbers could be useful in the debugger• An IL simplifies development and testing

of system components• The middle-end contains phases that

would otherwise be duplicated among the front- and back-ends

• It allows components and tools to interface with other products

Page 12: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

• It can simply the pioneering and prototyping of news ideas

• The ILs and its interpreter can serve as a reference definition of a language

• Interpreters written for a well-defined IL are helpful in testing and porting compilers

• An IL enables the crafting of a retargetable code generator, which greatly enhances its portability– Pascal: P-code– Java: JVM– Ada: DIANA (Descriptive Intermediate Attributed

Notation for Ada)

Page 13: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Java Virtual Machine• Class files: binary encodings of the data

and instructions in a Java program• Design principles

– Compactness• Instructions in nearly zero-address form

– A runtime stack is used– Operands are implicit

» E.g.: iadd instruction– A loss of runtime performance

• Multiple instructions to accomplish the same effect– To push 0 on TOS

» iconst_0, ldc_w 0

Page 14: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

– Safety• An instruction can reference storage only if it

is of the type allowed by the instruction, and only if the storage is located in an area appropriate for access

• From security’s point of view, purely zero-address form is problematic

– The registers that could be accessed by a load instruction may not be known until runtime

– JVM: not zero-address» E.g. iload 5

• When a class file is loaded, many other checks are performed by the bytecode verifier

Page 15: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Contents of a Class File• Attributes that contain various

information about the compiled class– Types: primitive and reference types– (Fig. 10.4)

• Primitive type: a single character• Reference type t: Lt

– E.g.: String type in java.lang package: Ljava/lang/String;

Page 16: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Page 17: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

– Constant pools• tagged union

– int, float, java.lang.String• Referenced by its ordinal position, not byte-

offset

Page 18: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

JVM Instructions• Arithmetic• Register traffic• Registers and types• Static fields• Instance fields• Branching• Other method calls• Stack operations

Page 19: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Arithmetic• int: 32-bit, 2’s complement

– iadd• fadd(float)• ladd(long)• dadd(double)

Page 20: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Register Traffic• JVM has an unlimited number of

virtual registers• JVM registers typically host a

method’s local variables• JVM registers are untyped

– iload 2• iload_2: abbreviated

– istore 10– aload and astore: for reference types

Page 21: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Registers and Types• Static analysis (or bytecode

verification)– To ensure that values flow in and our of

registers without compromising Java’s type systems

• Type conversion– i2f

Page 22: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Static Fields• getstatic

– E.g.: getstatic java/lang/System/out Ljava/io/PrintStream;

• putstatic

Page 23: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Instance Fields• A class can declare instance field for

which instance-specific storage is allocated

• getfield– getfiled Point/x I

• putfield– putfield Point/x I

Page 24: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Branching• ifeq, ifne, iflt, ifle, ifgt, ifge• if_icmpeq, if_icmpne, if_icmplt,

if_icmple, if_icmpgt, if_icmpge

Page 25: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Static Method Calls• invokestatic

– invokestatic java/lang/Math/pow(DD)D

Page 26: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Instance-Specific Method Calls

• invokevirtual– invokevirtual

java/io/PrintStream/print(Z)V

Page 27: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Static Single Assignment Form

• (omitted)

Page 28: Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.

Thanks for Your Attention!