300 likes | 399 Views
The Semantic Soundness of a Type System for Interprocedural Register Allocation and Constructor Registration. Torben Amtoft Kansas State University. joint work with Robert Muller, Boston College. Semantics Seminar, Northeastern University, May 28, 2003. Typed Compilation of ML-like Languages.
E N D
The Semantic Soundness of a Type System for Interprocedural Register Allocation and Constructor Registration Torben Amtoft Kansas State University joint work with Robert Muller, Boston College Semantics Seminar, Northeastern University, May 28, 2003
Typed Compilation of ML-like Languages letval a : int = 3 val b : int = 4 fun f(x : int) = x + a * b fun g(x : int) = x + b in (if test then f else g) @ 343 end
Defunctionalization let typet1 ={a : int, b : int} typet2 ={b : int} typet3 = (t1 + t2) fun apply1(h : t3, x : int) : t3 * int int = case h ofr : t1 =>let val a : int = #a(r) val b : int = #b(r) valc : int = a * b val d : int = x + c in d end r : t2 =>letval b : int = #b(r) val c : int = x + b in cend . . .
Defunctionalization … val a : int = 3 val b : int = 4 val c : t1 = {a = a, b = b} val d : t2 = {b = b} val e: int = 343 val h : t3 = if test then inj(1,c)t3else inj(2,d)t3 in apply1(h, e) end
Types with Storage Annotations let typet1 ={a : int H, b : int H} r1 type t2 = {a : int H, b : int H} H typet3 ={b : int H} r2 type t4 = {b : int H} H typet5 = (t2 +H t4) r3 fun apply1(h : t5, x : int r4) : int r5= case h ofr : t1 =>let val a : intr6 = #a(r) val b : int r7= #b(r) valc : int r8= a * b val d : intr5 = x + c in d end r : t3 =>letval b : intr9 = #b(r) val c : intr5 = x + b in cend . . .
Types with Storage Annotations … val a : intr1 = 3 val b : intr2 = 4 val c : t1 = {a = a, b = b} val d : t3 = {b = b} val e: int r4 = 343 val h : t5 = if test then inj(1,c)t5else inj(2,d)t5 in apply1(h, e) end
Heap Register 3 r1 r2 4 r3 3 r4 Standard Record Representation type t = {a : int H, b : {c : int H, d : int H} H} r1 val v: t = {a = 3, b = {c = 4, d = 3}}
Record Registration type t = {a : int r1, b : {c : int H, d : int H} r3} o val v: t = {a = 3, b = {c = 4, d = 3}} Heap Register 3 r1 r2 4 r3 3 r4
Complete Registration type t = {a : int r1, b : {c : int r4, d : int r3} o} o val v: t = {a = 3, b = {c = 4, d = 3}} Heap Register 3 r1 r2 3 r3 4 r4
Standard Variant Representation type t1 = (int H +H int H) H type t2 = (int H +H t1) r1 val v: t2 = inj(2,inj(1,25)t1)t2 Heap Register 2 r1 r2 1 r3 25 r4
Variant Registration type t1 = (int H +H int H) r3 type t2 = (int r2 +r1 t1) o val v: t2 = inj(2,inj(1,25)t1)t2 Heap Register 2 r1 r2 1 r3 25 r4
Complete Variant Registration type t1 = (int r2 +r4 int r1) o type t2 = (int r3 +r1 t1) o val v: t2 = inj(2,inj(1,25)t1)t2 Heap Register 2 r1 25 r2 r3 1 r4
Closure Registration let typet1 ={a : int r1, b : int r2} o typet2 ={b : int r2} o typet3 = (t1 +r3 t2) o fun apply1(h : t3, x : int r4) : int r5= case h ofr : t1 =>let val a : intr1 = #a(r) // no-op val b : int r2= #b(r) // no-op valc : int r6= a * b val d : intr5 = x + c in d end r : t2 =>letval b : intr2 = #b(r) // no-op val c : intr5 = x + b in cend . . .
Closure Registration … val a : intr1 = 3 val b : intr2 = 4 val c : t1 = {a = a, b = b} // no-op val d : t3 = {b = b} // no-op val e: int r4 = 343 val h : t3 = if test then inj(1,c)t3else inj(2,d)t3 in apply1(h, e) end
What we have Developed • A storage annotation type system • A typed SECD-like abstract machine • An annotation inference algorithm Our system works on monomorphized whole-programs in defunctionalized A-nf.
Kill Set Type System Syntax: a ::= ri| H | o ; annotations t::=t a t::=unit|int|t * t|t +at ft::=t * tAt e ::= … Judgments: E |- e : t ! A
WFat((t1a’1* t2a’2) a) E |- x1 : t1a1; E |- x2 : t2a2; a’i in {ai,H} E |- (x1,x2) : (t1a’1*t2a’2) a ! {a} Example: Product Types WFat(tia’i) E |- x : (t1a1 * t2 a2) a; ai in {a’i,H} E |- pi(x) : tia’i ! {a’i} - {ai}
Defunctionalized Factorial let funapply1(fact : unit, n : int) : int = let val a : int = 1 in ifzero n then a else let val b: int=n –a val c : int=apply1(fact,b) in let val d : int = c * n in d end end end vale : int = 4 valfact : unit = {} inapply1(fact,e) end
move r1,r3 Defunctionalized Factorial let funapply1(fact : unit o, n : intr1) : int r2= let val a : intr2 = 1 in ifzero n then a else let val b: intr3=n –a in letcall{r1} val c : intr2=apply1(fact,br3) in let val d : intr2 = c * nr1in d end end end end vale : int r1= 6 valfact : unit o= {} inapply1(fact,e) end
Type Environment Value Environment Saved values Abstract Machine Continuations: K ::= stop | <x,a,{viri},e,E,W,K>
Heap Register File Abstract Machine Machine States: Z ::= <e, R, H, E, W, K>
Abstract Machine Machine States: Z ::= <e, R, H, E, W, K> Transition Relation: Z Z’
Properties of the System 1. Well-Typedness Preservation: WT(Z) and Z Z’ implies WT(Z’). 2. Progress: If WT(Z) then either Z is final or there exists Z’ such that Z Z’.
Typed but unannotated expression Annotated type scheme Constraint Set Expression with annotation variables Set expression Annotation Inference Algorithm Annotate(E, ue) = (e, s, q, C)
What our System Does • Interprocedural Virtual Register Allocation • Record and Variant Registration • Function Argument Registration • Multiple-value Return • Customized Calling Conventions • Space-Efficient Tail-Calls • Type Safety
What we don’t Do • Solve the Constraints • Physical Register Allocation • 3. Heap Management • 4. Empirical Analysis of Allocation Heuristics • 5. Separate Compilation
Related Work • J. Agat 1997, 1998. • Morrisett et. al., 1998, 1999,… • Crary 2003 • Petersen et. al., 2003
Conclusion • We have developed a type system that maybe useful in performing reliable and efficient register allocation. • We intend to integrate it with a physical register allocation system. • We intend to study its performance.
Implementation Status • Done: • Translation, annotation and constraint generation (Brendan Connell) • Abstract Machine (Dan Allen) • Not Done: • Constraint solving (Dan Allen) • Physical Register Allocation