Docsity

High Level Language, Code Generation - Lecture Slides | CMSC 430, Study notes of Computer Science

Toggle sidebar
bg1
Code generation
High level languages
•Java
–stack code
–allocate registers to top of stack
•object-oriented
–method invocation
–member layout
•functional
–higher order functions
–function calls
Code generator generators
•tree pattern matching
•tree parsing
•peephole
CMSC 430 Lecture 12, Page 1
Compiling Java
Class files
•structure for describing program
•machine-independent stream of bytes
•verified when loaded
Issues
•stack reduces reordering
•virtual methods reduce inlining
•multiple threads limit transformations
•verify bytecodes to ensure safety
Converting into real code
•analyze stack to determine size
•represent stack as temporary variables
•try to avoid excessive copying
•allocate variables to registers
CMSC 430 Lecture 12, Page 2
Compiling stack code
General algorithm
•determine local storage
max locals + max stack + max temps
•form basic blocks
•find stack height for instruction
•translate instructions
Naive approach
•map each local/stack location to a frame location
•translate each instruction
•move locations between memory and registers
Register allocation approach
•map top of stack, first locals to registers
•fixed approach maps registers for entire method
•basic block approach maps registers for basic blocks
CMSC 430 Lecture 12, Page 3
Object-oriented (OO) languages
Objects
•a collection of data
•functions (methods) for operating on data
Classes
•collection of objects with same attributes
•organizes space of objects
•allows shared implementation of objects
Implementation
•class record
–pointers to methods (method table)
–storage for class data
•object record
–pointer to class type (tag)
–storage for local data
•location →offset in object record/method table
CMSC 430 Lecture 12, Page 4
pf3
pf4
pf5

Partial preview of the text

Download High Level Language, Code Generation - Lecture Slides | CMSC 430 and more Study notes Computer Science in PDF only on Docsity!

High level languagesCode generation

  • Java (^) stack code

(^) allocate registers to top of stack

  • object-oriented (^) method invocation

(^) member layout

  • functional (^) higher order functions

(^) function calls

Code generator generators

tree pattern matching

tree parsing

peephole

CMSC 430

Lecture 12, Page 1

Class files Compiling Java

structure for describing program

machine-independent stream of bytes

verified when loaded

Issues

stack reduces reordering

virtual methods reduce inlining

multiple threads limit transformations

verify bytecodes to ensure safety

Converting into real code

analyze stack to determine size

represent stack as temporary variables

try to avoid excessive copying

allocate variables to registers

CMSC 430

Lecture 12, Page 2

General algorithm Compiling stack code

max locals + max stack + max tempsdetermine local storage

form basic blocks

find stack height for instruction

translate instructions

Naive approach

map each local/stack location to a frame location

translate each instruction

move locations between memory and registers

Register allocation approach

map top of stack, first locals to registers

fixed

(^) approach maps registers for entire method

basic block

(^) approach maps registers for basic blocks

CMSC 430

Lecture 12, Page 3

Objects Object-oriented (OO) languages

a collection of data

functions (methods) for operating on data

Classes

collection of objects with same attributes

organizes space of objects

allows shared implementation of objects

Implementation

  • class record (^) pointers to methods

(method table)

(^) storage for class data

  • object record (^) pointer to class type

(tag)

(^) storage for local data

location

(^) offset in object record/method table

CMSC 430

Lecture 12, Page 4

InheritanceClass hierarchy

class may inherit data/methods from another class

ancestor class bestows attributes

(superclass)

descendent class inherits attributes

(subclass)

subclass should work wherever superclass is expected

subclass may override methods from superclass

(dynamic methods)

multiple ancestors

(^) multiple inheritance

Impact

(since object of type subclass is allowed wherever class is allowed)class of object not completely known at compile-time

need to test tags at runtime

could result in non-constant data/method pointer offset

CMSC 430^ Can we eliminate overhead of data/method lookups?

Lecture 12, Page 5

Single inheritance Data layout optimization

ensure constant offset for fields through

(^) prefixing

  • when class B inherits from class A (^) lay out fields of A at beginning of B in same order

(^) place new fields of B afterwards

field accessed as constant offset from object record

Multiple inheritance

ensure constant offset for fields

(may leave gaps between slots)assign slots for field via graph coloring

  • descriptor table (^) eliminate gaps through indirection

(^) assign unique descriptor slot via coloring

(^) descriptor stores offsets for field

field accessed as constant offset plus indirection

CMSC 430

Lecture 12, Page 6

Single inheritance Method lookup optimization

arrange method tables entries via prefixing

override methods by overwriting slot

ensure constant offset for methods

  1. invoke method through function pointer2. get function pointer at offset in method table1. fetch pointer to class record from objectmethod are executed through

Multiple inheritance

assign slots via graph coloring

overwrite slots as needed

Additional optimizations

type propagation to prove class type - convert method lookup into function call

inlining - merge code into call site, eliminates call overhead

CMSC 430

Lecture 12, Page 7

class A extends Obj { int a; Inheritance example

f1(); }

class B extends A

{ int b,c;

f2(); }

  1. if (x 1. check x’s pointer to class record^ Code for randomx.a = x.f1();A x;

class == A)

(a) call x

method[0]

(b) assign value to x.field[0]

  1. else if (x

class == B)

(a) call x

method[1]

(b) assign value to x.field[2]

  1. call x^ Code for prefix

method[0]

  1. assign value to x.field[0]

Objects TablesMethod Class

A

B

f

f2 f

a

c b a

Prefixed

A

B

f

f1 f

a

a b c

Random

CMSC 430

Lecture 12, Page 8

The big pictureCode generator generators

table-driven code

generator

optimizer

IR

asm

tables

machine

IR

descriptions

code

generatorgenerator

CMSC 430^ This scheme should look familiar

Lecture 12, Page 13

Tree rewriting schemes Assume that the program is represented as a set of trees. Tree pattern matching

(BURS

  1. associated code (to be emitted)1. mapping of subtree into single nodemachine description is
  • example pattern: (^) r i ← (^) + a b

load r1,a; load r2,b; add r1,r1,r

  • paradigm is (^) find a pattern to match subtree

(^) replace

(^) rhs

(^) pattern with

(^) lhs (^) node

(^) emit the associated code

CMSC 430

Lecture 12, Page 14

Several basic techniques Tree rewriting schemes

simple local choice criteriondepth-first traversalwork from a simple tree walk

adopt Aho & Corasick string matching

( TWIG

translate to/from linear formmatches multiple string patterns

adopt Aho & Johnson

(dynamic programming)

choose low-cost alternative at each pointrun rewriting and cost computation concurrently

pick the best overall matchgenerate all subtree matches concurrentlyuse a real tree pattern matching algorithm

CMSC 430

Lecture 12, Page 15

Use LR parsers Tree parsing schemes

— write grammar to describe target machine— use well understood technologyencode pattern matching into parsing problem

— lots of contextual knowledge available— attributed-style specificationreductions emit code

grammars are

(^) very

(^) ambiguous

— reduce/reduce

(^) pick longer reduction

— shift/reduce

(^) shift

linear time scheme!

CMSC 430

Lecture 12, Page 16

Assume program is represented in low-level intermediate representation (Instruction matching

IR ).

Peephole optimization

  • find logically adjacent instructions that can be combined (^) use a very small context

(3-10 instructions)

(^) combining

(^) i (^1) and (^) i (^2) ⇒ (^) faster

(^) i 3

work at register-transfer language (

rtl (^) ) level

(^) machine description in

(^) rtl

(^) low-level

(^) IR (^) description in

(^) rtl

using pattern matching, synthesize more complex instructions

useful for implementing many machine-dependent optimizations

CMSC 430

Lecture 12, Page 17

Generating “peephole” code generators Instruction matching

provide a one-to-one translation for

(^) IR

(more complex instructions and addressing modes)add patterns to improve code

Training generator

exhaustive searchfeed a set of representative programs to the trainer and let it build a table by

one time expense

(and it is expensive

use a linear time pattern matcher run from the tables produced by the trainer

Typical machines

RT/PC

(^) w/o floating point - 70-100 instructions

MC 68020 - millions of possible instructions

CMSC 430

Lecture 12, Page 18