-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathcompiler.rkt
111 lines (93 loc) · 3.59 KB
/
compiler.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#lang racket
(require racket/set racket/stream)
(require racket/fixnum)
(require "interp-Lint.rkt")
(require "interp-Lvar.rkt")
(require "interp-Cvar.rkt")
(require "interp.rkt")
(require "type-check-Lvar.rkt")
(require "type-check-Cvar.rkt")
(require "utilities.rkt")
(provide (all-defined-out))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lint examples
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The following compiler pass is just a silly one that doesn't change
;; anything important, but is nevertheless an example of a pass. It
;; flips the arguments of +. -Jeremy
(define (flip-exp e)
(match e
[(Var x) e]
[(Prim 'read '()) (Prim 'read '())]
[(Prim '- (list e1)) (Prim '- (list (flip-exp e1)))]
[(Prim '+ (list e1 e2)) (Prim '+ (list (flip-exp e2) (flip-exp e1)))]))
(define (flip-Lint e)
(match e
[(Program info e) (Program info (flip-exp e))]))
;; Next we have the partial evaluation pass described in the book.
(define (pe-neg r)
(match r
[(Int n) (Int (fx- 0 n))]
[else (Prim '- (list r))]))
(define (pe-add r1 r2)
(match* (r1 r2)
[((Int n1) (Int n2)) (Int (fx+ n1 n2))]
[(_ _) (Prim '+ (list r1 r2))]))
(define (pe-exp e)
(match e
[(Int n) (Int n)]
[(Prim 'read '()) (Prim 'read '())]
[(Prim '- (list e1)) (pe-neg (pe-exp e1))]
[(Prim '+ (list e1 e2)) (pe-add (pe-exp e1) (pe-exp e2))]))
(define (pe-Lint p)
(match p
[(Program info e) (Program info (pe-exp e))]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; HW1 Passes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (uniquify-exp env)
(lambda (e)
(match e
[(Var x)
(error "TODO: code goes here (uniquify-exp Var)")]
[(Int n) (Int n)]
[(Let x e body)
(error "TODO: code goes here (uniquify-exp Let)")]
[(Prim op es)
(Prim op (for/list ([e es]) ((uniquify-exp env) e)))])))
;; uniquify : Lvar -> Lvar
(define (uniquify p)
(match p
[(Program info e) (Program info ((uniquify-exp '()) e))]))
;; remove-complex-opera* : Lvar -> Lvar^mon
(define (remove-complex-opera* p)
(error "TODO: code goes here (remove-complex-opera*)"))
;; explicate-control : Lvar^mon -> Cvar
(define (explicate-control p)
(error "TODO: code goes here (explicate-control)"))
;; select-instructions : Cvar -> x86var
(define (select-instructions p)
(error "TODO: code goes here (select-instructions)"))
;; assign-homes : x86var -> x86var
(define (assign-homes p)
(error "TODO: code goes here (assign-homes)"))
;; patch-instructions : x86var -> x86int
(define (patch-instructions p)
(error "TODO: code goes here (patch-instructions)"))
;; prelude-and-conclusion : x86int -> x86int
(define (prelude-and-conclusion p)
(error "TODO: code goes here (prelude-and-conclusion)"))
;; Define the compiler passes to be used by interp-tests and the grader
;; Note that your compiler file (the file that defines the passes)
;; must be named "compiler.rkt"
(define compiler-passes
`(
;; Uncomment the following passes as you finish them.
;; ("uniquify" ,uniquify ,interp-Lvar ,type-check-Lvar)
;; ("remove complex opera*" ,remove-complex-opera* ,interp-Lvar ,type-check-Lvar)
;; ("explicate control" ,explicate-control ,interp-Cvar ,type-check-Cvar)
;; ("instruction selection" ,select-instructions ,interp-pseudo-x86-0)
;; ("assign homes" ,assign-homes ,interp-x86-0)
;; ("patch instructions" ,patch-instructions ,interp-x86-0)
;; ("prelude-and-conclusion" ,prelude-and-conclusion ,interp-x86-0)
))