-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathir-anf-printable-ast.rkt
91 lines (77 loc) · 2.91 KB
/
ir-anf-printable-ast.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
#lang typed/racket/base
(require racket/match)
(require
"unique.rkt"
(only-in "types.rkt" type record-type-fields)
(rename-in "ir-anf-ast.rkt" (expression ir:expression))
(rename-in "primop.rkt" (primop primop:primop)))
(provide (rename-out (ir->printable anf->printable)))
(define-type primop
(U
'+ '* '- '/ '< '<= '= '<> '>= '> '& '\|
(List 'field-ref Symbol)
(List 'field-set! Symbol)
'array-ref
'array-set!
'call
'box-ref
'box-set!
'unit
'undef
Integer
String
'nil
(List 'call-known Symbol)
(List 'call-runtime Symbol)
(List 'runtime Symbol)
'create-box
'create-array
(Pair 'create-record (Listof Symbol))))
(define-type print-expression (Rec expression
(U
(List 'let (List Symbol (Pair primop (Listof Symbol))) expression)
(List 'if Symbol expression expression)
(List 'letrec (Listof (List Symbol (List 'function Symbol (Listof Symbol) expression))) expression)
Symbol)))
(define-type function-declaration (List Symbol (List 'function Symbol (Listof Symbol) print-expression)))
(: ir->printable (ir:expression -> print-expression))
(define (ir->printable ir)
(match ir
((conditional c t f ty)
(list 'if (unique->symbol c) (ir->printable t) (ir->printable f)))
((bind-primop var type op args body)
(list 'let (list (unique->symbol var) ((inst cons primop (Listof Symbol)) (primop->printable op) (map unique->symbol args))) (ir->printable body)))
((return name) (unique->symbol name))
((bind-rec funs body)
(list 'letrec (map function->printable funs) (ir->printable body)))
(else (error 'ir->printable "Not handled ~a" ir))))
(: primop->printable (primop:primop -> primop))
(define (primop->printable op)
(match op
((math-primop sym) sym)
((equality-primop eql ty) (if eql '= '<>))
((unit-primop) 'unit)
((undefined-primop ty) 'undef)
((call-closure-primop ty) 'call)
((call-known-function-primop ty name) (list 'call-known (unique->symbol name)))
((call-known-runtime-primop ty name) (list 'call-runtime name))
((integer-constant-primop v) v)
((string-constant-primop v) v)
((nil-primop ty) 'nil)
((runtime-primop ty name) (list 'runtime name))
((box-set!-primop ty) 'box-set!)
((box-ref-primop ty) 'box-ref)
((array-set!-primop ty) 'array-set!)
((array-ref-primop ty) 'array-ref)
((field-set!-primop ty name) (list 'field-set! name))
((field-ref-primop ty name) (list 'field-ref name))
((create-box-primop ty) 'create-box)
((create-array-primop ty) 'create-array)
((create-record-primop ty) (cons 'create-record (map (inst car Symbol type) (record-type-fields ty))))
(else (error 'primop->printable "Not handled ~a" op))))
(: function->printable ((Pair unique function) -> function-declaration))
(define (function->printable pair)
(list (unique->symbol (car pair))
(match (cdr pair)
((function name args return body)
(list 'function (unique->symbol name) (map unique->symbol (map (inst car unique type) args)) (ir->printable body))))))