blob: 5f09181df258c1ba6c88a10880c0273f750122ab (
plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
(ns cljcc.tacky
(:require
[clojure.pprint :as pp]
[cljcc.lexer :as l]
[cljcc.parser :as p]))
(def counter "Global integer counter for generating unique identifier names." (atom 0))
(defn create-identifier
"Returns a unique identifier. Used for generating tacky variable names."
([]
(create-identifier "tmp"))
([identifier]
(let [n @counter
_ (swap! counter #(+ % 1))]
(str identifier "." n))))
(defn variable
([]
{:type :variable
:value (create-identifier)})
([^String identifier]
{:type :variable
:value (create-identifier identifier)}))
(defn constant-instruction [^Integer v]
{:type :constant
:value v})
(defn- unary-operator [op]
(condp = op
:complement :complement
:hyphen :negate))
(defn- binary-operator [binop]
(condp = binop
:plus :add
:hyphen :sub
:multiply :mul
:divide :div
:remainder :mod
:bit-and :bit-and
:bit-or :bit-or
:bit-xor :bit-xor
:bit-right-shift :bit-right-shift
:bit-left-shift :bit-left-shift))
(defn- unary-instruction [unary-operator src dst]
{:type :unary
:unary-operator unary-operator
:dst dst
:src src})
(defn- binary-instruction [binary-operator src1 src2 dst]
{:type :binary
:binary-operator binary-operator
:src1 src1
:src2 src2
:dst dst})
(defn return-instruction [val]
{:type :return
:val val})
(declare expression-handler)
(defn- constant-expr-handler [e]
{:val (constant-instruction (:value e))})
(defn- unary-expr-handler [e]
(let [inner (expression-handler (:value e))
dst (variable)
src (:val inner)
unary-operator (unary-operator (:unary-operator e))
instruction (unary-instruction unary-operator src dst)]
{:val dst
:instructions (flatten [(:instructions inner) instruction])}))
(defn- binary-expr-handler [e]
(let [e1 (expression-handler (:left e))
e2 (expression-handler (:right e))
src1 (:val e1)
src2 (:val e2)
dst (variable)
binary-operator (binary-operator (:binary-operator e))
instruction (binary-instruction binary-operator src1 src2 dst)]
{:val dst
:instructions (flatten [(:instructions e1) (:instructions e2) instruction])}))
(defn- expression-handler [e]
(when-let [exp-type (:exp-type e)]
(cond
(= exp-type :constant-exp) (constant-expr-handler e)
(= exp-type :unary-exp) (unary-expr-handler e)
(= exp-type :binary-exp) (binary-expr-handler e)
:else (throw (ex-info "Tacky error. Invalid expression." {e e})))))
(defn- exp-instructions [exp]
(expression-handler exp))
(defn- ret-instructions [exp]
(let [e (exp-instructions exp)
val (:val e)
instructions (:instructions e)]
(conj (vec instructions) (return-instruction val))))
(defn ast-statement->tacky-instructions [statement]
(remove nil? (ret-instructions (:value statement))))
(defn tacky-generate [ast]
(reset! counter 0)
(map (fn [f]
(-> f
(assoc :instructions (flatten (map ast-statement->tacky-instructions (:statements f))))
(dissoc :statements)))
ast))
(comment
(reset! counter 0)
(pp/pprint
(tacky-generate
(p/parse (l/lex "int main(void) {return -(~1);}"))))
(pp/pprint
(tacky-generate
(p/parse (l/lex "int main(void) {return 1 * -2 / ~3 * (4 - 5);}"))))
())
|