Download presentation
Presentation is loading. Please wait.
Published byAvery Linford Modified over 9 years ago
1
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
2
Typed Compilation of ML-like Languages let val 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
3
Defunctionalization let type t1 = {a : int, b : int} type t2 = {b : int} type t3 = (t1 + t2) fun apply 1 (h : t3, x : int) : t3 * int int = case h of r : t1 => let val a : int = #a(r) val b : int = #b(r) val c : int = a * b val d : int = x + c in d end r : t2 => let val b : int = #b(r) val c : int = x + b in c end...
4
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) t3 else inj(2,d) t3 in apply 1 (h, e) end
5
Types with Storage Annotations let type t1 = {a : int H, b : int H} r1 type t2 = {a : int H, b : int H} H type t3 = {b : int H} r2 type t4 = {b : int H} H type t5 = (t2 +H t4) r3 fun apply 1 (h : t5, x : int r4) : int r5 = case h of r : t1 => let val a : int r6 = #a(r) val b : int r7 = #b(r) val c : int r8 = a * b val d : int r5 = x + c in d end r : t3 => let val b : int r9 = #b(r) val c : int r5 = x + b in c end...
6
Types with Storage Annotations … val a : int r1 = 3 val b : int r2 = 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) t5 else inj(2,d) t5 in apply 1 (h, e) end
7
type t = {a : int H, b : {c : int H, d : int H} H} r1 val v : t = {a = 3, b = {c = 4, d = 3}} type t = {a : int H, b : {c : int H, d : int H} H} r1 val v : t = {a = 3, b = {c = 4, d = 3}} 3 Heap Register 4 3 r1 r2 r3 r4 Standard Record Representation
8
type t = {a : int r1, b : {c : int H, d : int H} r3} o val v : t = {a = 3, b = {c = 4, d = 3}} type t = {a : int r1, b : {c : int H, d : int H} r3} o val v : t = {a = 3, b = {c = 4, d = 3}} 3 Heap Register 4 3 r1 r2 r3 r4 Record Registration
9
type t = {a : int r1, b : {c : int r4, d : int r3} o} o val v : t = {a = 3, b = {c = 4, d = 3}} type t = {a : int r1, b : {c : int r4, d : int r3} o} o val v : t = {a = 3, b = {c = 4, d = 3}} 3 Heap Register 3 4 r1 r2 r3 r4 Complete Registration
10
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 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 2 Heap Register 1 25 r1 r2 r3 r4 Standard Variant Representation
11
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 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 2 Heap Register 1 25 r1 r2 r3 r4 Variant Registration
12
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 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 2 Heap Register 25 1 r1 r2 r3 r4 Complete Variant Registration
13
Closure Registration let type t1 = {a : int r1, b : int r2} o type t2 = {b : int r2} o type t3 = (t1 +r3 t2) o fun apply 1 (h : t3, x : int r4) : int r5 = case h of r : t1 => let val a : int r1 = #a(r) // no-op val b : int r2 = #b(r) // no-op val c : int r6 = a * b val d : int r5 = x + c in d end r : t2 => let val b : int r2 = #b(r) // no-op val c : int r5 = x + b in c end...
14
Closure Registration … val a : int r1 = 3 val b : int r2 = 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) t3 else inj(2,d) t3 in apply 1 (h, e) end
15
What we have Developed 1.A storage annotation type system 2.A typed SECD-like abstract machine 3.An annotation inference algorithm Our system works on monomorphized whole- programs in defunctionalized A-nf.
16
Type System Syntax: a ::= r i | H | o ; annotations ::= t a t ::= unit | int | * | + a ft ::= * A e ::= … Judgments: e Kill Set
17
Example: Product Types E |- (x1,x2) : (t1 a’1 * t2 a’2) a ! {a} WFat((t1 a’1 * t2 a’2) a) E |- x1 : t1 a1; E |- x2 : t2 a2; a’i in {ai,H} E |- i (x) : ti a’i ! {a’i} - {ai} WFat(ti a’i) E |- x : (t1 a1 * t2 a2) a; ai in {a’i,H}
18
Recursion Interference and the Stack
19
let fun apply 1 (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 = apply 1 (fact,b) in let val d : int = c * n in d end end val e : int = 4 val fact : unit = {} in apply 1 (fact,e) end Defunctionalized Factorial
20
let fun apply 1 (fact : unit o, n : int r1) : int r2 = let val a : int r2 = 1 in ifzero n then a else let val b : int r3 = n – a in letcall{r1} val c : int r2 = apply 1 (fact,b r3 ) in let val d : int r2 = c * n r1 in d end end val e : int r1 = 6 val fact : unit o = {} in apply 1 (fact,e) end Defunctionalized Factorial move r1,r3
21
Abstract Machine Continuations: K ::= stop | Value Environment Type Environment Saved values
22
Abstract Machine Machine States: Z ::= Register File Heap
23
Abstract Machine Machine States: Z ::= Transition Relation: Z Z’
24
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’.
25
Annotation Inference Algorithm Annotate( E, ue) = (e, , q, C) Set expression Typed but unannotated expression Constraint Set Expression with annotation variables Annotated type scheme
26
What our System Does 1.Interprocedural Virtual Register Allocation 2.Record and Variant Registration Function Argument Registration Multiple-value Return 3. Customized Calling Conventions 4.Space-Efficient Tail-Calls 5.Type Safety
27
What we don’t Do 1.Solve the Constraints 2.Physical Register Allocation 3. Heap Management 4. Empirical Analysis of Allocation Heuristics 5. Separate Compilation
28
Related Work 1.J. Agat 1997, 1998. 2.Morrisett et. al., 1998, 1999,… 3.Crary 2003 4.Petersen et. al., 2003
29
Conclusion 1.We have developed a type system that may be useful in performing reliable and efficient register allocation. 2.We intend to integrate it with a physical register allocation system. 3.We intend to study its performance.
30
Implementation Status Done: 1.Translation, annotation and constraint generation (Brendan Connell) 2.Abstract Machine (Dan Allen) Not Done: 1.Constraint solving (Dan Allen) 2.Physical Register Allocation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.