aboutsummaryrefslogtreecommitdiff
path: root/src/cljcc/tacky.clj
blob: 1d0af27d8642e41e45bfe763ed795c24716e6bd1 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
(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 [^String unop]
  (condp = unop
    "~" :complement
    "-" :negate))

(defn- binary-operator [binop]
  (condp = binop
    :add-exp :add
    :sub-exp :sub
    :mul-exp :mul
    :div-exp :div
    :mod-exp :mod
    :bit-and-exp :bit-and
    :bit-or-exp :bit-or
    :bit-xor-exp :bit-xor
    :bit-right-shift-exp :bit-right-shift
    :bit-left-shift-exp :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})

(def binary-exprs
  #{:add-exp
    :sub-exp
    :mul-exp
    :mod-exp
    :div-exp
    :bit-and-exp
    :bit-or-exp
    :bit-xor-exp
    :bit-right-shift-exp
    :bit-left-shift-exp})

(defn- binary-expr? [v]
  (contains? binary-exprs v))

(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 e)
        instruction (unary-instruction unary-operator src dst)]
    {:val dst
     :instructions (flatten [(:instructions inner) instruction])}))

(defn- binary-expr-handler [e]
  (let [e1 (expression-handler (nth e 1))
        e2 (expression-handler (nth e 2))
        src1 (:val e1)
        src2 (:val e2)
        dst (variable)
        binary-operator (binary-operator (first 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)
      (binary-expr? exp-type) (binary-expr-handler 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 "int main(void) {return 1 * 2 & 3 * (4 + 5);}")))

  (pp/pprint
   (p/parse "int main(void) {return 1 * 2 - 3 * (4 + 5);}"))

  (pp/pprint
   (p/parse "int main(void) {return 1 + 2 + -3 + -(4 + 5);}"))

  (pp/pprint
   (tacky-generate
    (p/parse "int main(void) {return 1 + 2 + -3 + -(4 + 5);}")))

  ())