



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




High level languagesCode generation
(^) allocate registers to top of stack
(^) member layout
(^) 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
(method table)
(^) storage for class data
(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
(^) 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
(^) 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
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(); }
class == A)
(a) call x
method[0]
(b) assign value to x.field[0]
class == B)
(a) call x
method[1]
(b) assign value to x.field[2]
method[0]
Objects TablesMethod Class
f
f2 f
a
c b a
Prefixed
f
f1 f
a
a b c
Random
CMSC 430
Lecture 12, Page 8
The big pictureCode generator generators
table-driven code
generator
optimizer
asm
tables
machine
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
load r1,a; load r2,b; add r1,r1,r
(^) 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
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
(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