Intermediate forms: A-Normal Form Matt Might University of Utah - - PowerPoint PPT Presentation

intermediate forms a normal form
SMART_READER_LITE
LIVE PREVIEW

Intermediate forms: A-Normal Form Matt Might University of Utah - - PowerPoint PPT Presentation

Intermediate forms: A-Normal Form Matt Might University of Utah www.ucombinator.org matt.might.net Terminology Intermediate language Intermediate representation Intermediate form Intermediate form An intermediate language is a


slide-1
SLIDE 1

Intermediate forms: A-Normal Form

Matt Might University of Utah www.ucombinator.org matt.might.net

slide-2
SLIDE 2

Terminology

  • Intermediate language
  • Intermediate representation
  • Intermediate form
slide-3
SLIDE 3

Intermediate form

An intermediate language is a target language that sits above the output language.

slide-4
SLIDE 4

Purpose

IR Scheme Java C++ x86 ARM MIPS

slide-5
SLIDE 5

Nano-pass compilation

60+ intermediate forms!

slide-6
SLIDE 6

History

  • Register-transfer languages (1950s)
  • Continuation-passing style (1970s)
  • Static single assignment (1980s)
  • A-normal form (1990s)
  • Hybridization? (2010s)
slide-7
SLIDE 7

A pathway

Java, C#, Scheme ANF CPS RTL SSA RTL ASM

slide-8
SLIDE 8

Lesser-known

  • Store-passing style
  • Monadic languages
slide-9
SLIDE 9

Standard IRs

  • CIL
  • LLVM
  • GIMPLE
  • C--
  • JIMPLE/SHIMPLE
slide-10
SLIDE 10

A-Normal Form

All arguments to functions are atomic. Flanagan et al., “The Essence of Compiling with Continuations.”

slide-11
SLIDE 11

A-Normal Form

The inverse CPS transformation: I?J: CPS(CS) +

U[(k w)] =

U[(let (z W) P)] = U[(if’O W P1 Pz)] =

Z.f[(w k WI . . . w.)] = U[(W (AZ.P) WI . . . w.)] = U[(o’ k- WI . . . Wn)] = q(o’

(XZ.P)

WI ... Wn)] = V2:w+

W[c] = !qx] = qMx] . ..%. q = A(CS)

V[w]

(let (z iU[W]) U[P]) (if’(l Q[W] UIP1] U[l%])

(w[w] ‘3qw,] ... V[wn])

(let (z (V[W] Ui[Wl] . . . If[W.])) U[P])

(o W[w,] ... W[wn])

(let (z (O VIW1] . . . f17[Wn])) L/[P])

v c

x Axl . ..xn.u[itq The language A(CS) M ::= V I (let (z V) M) l(if13V MM)

I(VK ... Vn) I (let

(x (V K Vn)) M) I(ovl... vn) \ (let (x (O Vi . . . V~)) M)

v ::=

clxl(kc]. ..x M)M) Figure 6: The inverse CPS transformation and its

  • utput

(return) (bind) (branch) (tad call) (call) (prira-op) (prim-op) (values) Evaluation Contexts: & ::= [1 The A-reductions: l(let(z$)M) l(ifO~MM) I(FV. V8M.. .M) where F= Vor F=O

~[(let (X M) AT)] +

(let (x M) S[N]) where&# [ ], z @ FV(f) t[(i~ v M, M,)]

  • +

(ifO V

SIMI]

S[MZ]) where ~ # [ ]

t[(F VI . . . Vn)] + (let (t (F VI . . . V~)) t[t])

where

F = V or F = O,: # ~’[(let (Z []) M)], $ # [ ],t C FV(~) Figure 7: Evaluation contexts and the set

  • f

A-reductions (AI) (Az) (A) subexpression to be evaluated according to the CEK se- The A-reductions transform programs in a natu- mantics. For example, in an expression (let (% Ml) flfz), ral and intuitive manner. The first two reductions the next reducible expression must

  • ccur

within Ml, merge code segments across declarations and condi- hence the definition

  • f

evaluation contexts includes the t ionals. The last reduction Iifis redexes

  • ut
  • f

eval- clause (let (z ~) M). uation contexts and names intermediate results. Us- ing evaluation contexts and the A-reductions, we can 243

slide-12
SLIDE 12

Discovering ANF

Semantics: Let P G CPS(CS), ew&(P) = c if (P, O, (ar z, (k z), O, stop)) w: ((k z), O[z := c], stop). Data Specifications: SC E Statec = E- E Envc = W* G Valuee = Ek E Contc =

Transition Rules: ((k

W), E-, Ek) @+c (P’ CPS(CS)

x

Envc

x

Cont.

(machine states)

Variables m Valuec (environments) c I (CllCZI... Z~, P,)-) (machine values) stop I (ar z, P, E-, Ek) (continuations) E;[z := p(W, E-)], Ef) where Ek = (ar x, P’, El–, E!) ((let (z W) P), E-, E’) F@+= (P, E-[z := ,u(W, E-)], E’) {(if’O W PI .P2), E-, Ek) ~c (Pi, E-, E’) where K(W,.E-) = O

  • r

(Pz, E-, E’) where K(W, E-) # O

((wkw, ... W~),

E-, Ek) w= (P’, E~[z] := W:, . . ..z. := WJ, Ek) where v(W, E–) = (cl k’zl . . .xn, P’, El-) and for 1 5 t < n, W,” = P(W,, E–) ((w (AS.P)

w, . .. Wn),

E-, E~) ~c (P’, E~[zl := WY,. ... zn := W:], (ar z, P, E-, Ek)) where p(W, E-) = (cl k’zl

. ..~n. P’, E1-)

and for 1< i < n, W,* = p(W; ,E–) ((0’ k W, . . Wn), E-, Ek) WC (P’, Efl[z := &( O’, W:,..., W;)], E;) where Ek = (ar x, P’, E;, E!) and for

((o’ (/kz.P) w, . .. W~),

E-, Ek) UC (P, E-[x := &( O’, W:,..., W~)], Ek) and for 1 < i < n, W,* = p(W,, E–) if 8C(0’, W~, . . . . W;)

is defined,

1< i < n, W? = ,U(W,, E-) if ISC(O’, W:, . . . . W;)

is defined,

Figure 5: The realistic CPS abstract machine: the C,-P, EK machine. Again, the machine ignores the continuation parameter in the closures and manipulate the “global” register Ek instead. Undoing CPS The crucial elimination

  • f

the redundant CCP,EK machine corresponds to insight is that the information from the an inverse CPS trans- formation [7, 17]

  • n

the intermediate code. The func- tion Z./ in Figure 6 realizes such an inverse [17]. The in- verse transformation formalizes

  • ur

intuition about the redundancies in the CCP.EK machine. It eliminates the variable k from return instructions as well aa the param- eter k from procedures. The latter change implies that continuations are not passed as arguments in function calls but rather become contexts surrounding the calls. For example, the code segment cps (IV) in Section 3 be- comes: A(iV) = (let (f~ (+ 2 2)) (let (z 1) (let

(t,(f %)) (+ -t, t2))))

Based

  • n

the above argument, it appears compilers perform a sequence

  • f

three steps: that CPS (7s

$3

CPS

b

  • I

I Al @normalization

I I I

t

A(C’S) i un-CPS e CPS(CS) The diagram naturally suggests a direct translation A that combines the effects

  • f the

three phases. The iden- tification

  • f

the translation A requires a theorem re- lating ~-reductions

  • n

CPS terms to reductions

  • n

the source language. This correspondence

  • f reductions

was the subject

  • f our

previous paper [17]. The resulting set

  • f

source reductions, the A-reductions, is in Figure 7.6 Since the A-reductions are strongly normalizing, we can characterize the translation A aa any function that aP- plies the A-reductions to a source term until it reaches a normal form [17: Theorem 6.4]. The definition

  • f

the A-reductions refers to the con- cept

  • f

evaluation contexts. An evaluation context is a term with a “hole” (denoted by [ ]) in the place

  • f one

subterm. The location

  • f

the hole points to the next

6Danvy

[8]

and Weise [21] also recognize that the compaction

  • f CPS

terms can be expressed in the source language, but do not explore this topic systematically.

L-t/&

slide-13
SLIDE 13

A-Normalization

(define

na-rndize-terrn

(lambda (M) (w-make M (lambda (z) z))))

(define normalize (lambda (M k) (match M

(’(lambda ,paranu ,My) (k ‘(lambda ,pararns ,(norrnalize-terrn ZIody)))] ~(let (,z ,MI) ,Mz)

(rwrrmdize Ml (lambda (Nl) ‘(let (,z ,Nl) ,(normalize M, k))))]

~(ifO ,MI

,Mz ,M3) (normalize-name Ml (lambda

(t) (k ‘(ifO ,t,(normalize-term

M2) ,(norrmdize-term Ms)))))] ~(,Fn ,M*) (if (PrirnOp? Fn) (normcdwe-name* M* (lambda (t*) (k ‘(,Fn . ,t”)))) (normalize-name Fn (lambda

(t)

(normalize-nom.’ M* (lambda (t*) (k ‘(jt . ,t”)))))))]

[v (k v)])))

(define normalize-name (lambda (M k) (rzormahze M (lambda (N) (if ( Value? N) (k N)

(let

([t (neuwar)])

‘(let

(,t ,N) ,(k t)))))))) (define normaiize-name” (lambda (M* k) (if (null? M*) (k ‘())

(normalize-name (car M*)

(lambda (t) (normalize-name” (cxtr M*) (lambda

(t*)

(k ‘(,t . at’))))))))) Figure 9: A linear-tilme A-normalization algorithm

[10]

[11]

[12] [13] [14]

FELLEISEN, M., AND FRIEDMAN, D. Control

  • p-

erators, the SECD-machine, and the A-calculus. [n

Formal Description

  • f

Programmmg Concepts III (Amsterdam, 1986), M. Wirsing, Ed., Elsevier Sci- ence Publishers B.V. (North-Holland), pp. 19;3– 217.

FESSENDEN, C., CLINGER, W., FRIEDMAN, D. P., AND HAYNES, C. T. Scheme 311 version 4 reference manual. Computer Science Technical R,e- port 137, Indiana University, Bloomhy.$on, Indi- ana, Feb. 1983. FISCHER, M. Lambda calculus schemata. In

Pro- ceedings

  • f

the ACM Conference

  • n

Proving As- sertions About Programs (1972), vol. 7(l), Sigplim Notices, pp. 104-109.

KELSEY, R., AND HUDAK, P. Realistic com- pilation by program transformation. In

Confer- ence Record

  • f

the 16th Annuai ACM Symposium

  • n

Principles

  • f

Programming Languages (Austin, TX, Jan. 1989), pp. 281-292.

KRANZ, D., KELSEY, R., REES, J., HUDAK, E’., PHILBIN, J., AND ADAMS, N.

Orbit: An

  • p-

timizing compiler for Scheme. In Proceedings

  • f

the ACM SIGPLAN 1986 Symposium

  • n

Compi/er Construction (1986), vol. 21(7), Sigplan Notices, pp. 219-233. [15] [16] [17] [18] [19]

[20] [21]

LEROY, X. The Zinc experiment: An economical implementation

  • f

the ML language. Tech. Rep. 117, INRIA, 1990. PLOTKIN, G. Call-by-name, call-by-value, and the A-calculus,

Theoretical Computer Science 1 (1975), 125-159.

SAE+RY, A., AND FELLEISEN, M. Reasoning about programs in continuation-passing style. In

Pro- ceedings

  • f

the 1992 ACM Conference

  • n

Lisp and Functional Programming (1992), pp. 288–298. Technical Report 92-180, Rice University. SHIVERS, O. Control-Flow Analysis

  • f

Higher- Order Languages

  • r

Tamtng Lambda. PhD thesis, Carnegie-Mellon University, 1991. STEELE, G. L. RABBIT: A compiler for Scheme. MIT AI Memo 474, Massachusetts Institute

  • f

Technology, Cambridge, Mass., May 1978. WAND, M. Correctness

  • f

procedure representa- tions in higher-order assembly language. In Pro- ceedings

  • f the

1991 Conference

  • n

the Mathemat- ical Foundations

  • f

Programing Semantics (1992),

  • S. Brookes,

Ed., vol. 598

  • f

Lecture Notes an Com- puter Scaence, Springer Verlag, pp. 294-311. WEISE, D. Advanced compiling techniques. Course Notes at Stanford University, 1990. 247

slide-14
SLIDE 14

Example

(define (celsius F) (* (/ 5 9) (- F 32)))

slide-15
SLIDE 15

Example

(define (celsius F) (let ((t1 (/ 5 9))) (let ((t2 (- F 32))) (* t1 t2))))

slide-16
SLIDE 16

Benefits

  • Order of evaluation is specified
  • Evaluation broken into small steps
slide-17
SLIDE 17

Correspondence

(define (celsius F) (let ((t1 (/ 5 9))) (let ((t2 (- F 32))) (* t1 t2)))) celsius: mov F, a0 div t1, 5, 9 sub t2, F, 32 mul res, t1, t2 ret res

slide-18
SLIDE 18

Example

(define (fact n) (let ((zero? (= n 0))) (if zero? 1 (let ((n-1 (- n 1))) (let ((|n-1|! (fact n-1))) (* n |n-1|!)))))) (define (fact n) (if (zero? n) 1 (* n (fact (- n 1)))))

slide-19
SLIDE 19

Example

(define (fact n) (let ((zero? (= n 0))) (if zero? 1 (let ((n-1 (- n 1))) (let ((|n-1|! (fact n-1))) (* n |n-1|!)))))) fact: mov n, a0 cmp t1, n, 0 bz t1, body ret 1 body: sub t2, n, 1 call fact mov t3, rv mul t4, n, t3 ret t4

slide-20
SLIDE 20

Example (Java)

return foo.bar(x.f,qux()) ; F v0 = x.f ; Q q0 = qux() ; return foo.bar(v0,q0) ;