blob: 58026fd42ac647b0e3f0e9e6a8f1a8bd2812b7f9 (
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
|
(ns cljcc.tacky
(:require
[cljcc.lexer :as l]
[cljcc.util :as u]
[cljcc.parser :as p]
[cljcc.analyze.core :as a]))
(defn- variable
([]
(variable "var"))
([identifier]
{:type :variable
:value (u/create-identifier! (str identifier))}))
(defn parsed-var->tacky-var [v]
{:type :variable
:value (:identifier v)})
(defn- label
([] (label "label"))
([ident] (u/create-identifier! ident)))
(defn constant [^Integer v]
{:type :constant
:value v})
(defn- unary-operator
"Converts parser's unary operator to tacky representation."
[op]
(condp = op
:complement :bit-not
:hyphen :negate
:logical-not :logical-not
(throw (ex-info "Tacky Error. Invalid unary operator." {op op}))))
(defn- assignment-operator->binary-operator
"Converts parser assignment operator to binary operator keyword."
[op]
(condp = op
:assignemnt :assignemnt
:assignment-plus :plus
:assignment-multiply :multiply
:assignment-minus :hyphen
:assignment-divide :divide
:assignment-mod :remainder
:assignment-bitwise-and :ampersand
:assignment-bitwise-or :bitwise-or
:assignment-bitwise-xor :bitwise-xor
:assignment-bitwise-left-shift :bitwise-left-shift
:assignment-bitwise-right-shift :bitwise-right-shift))
(defn- binary-operator
"Converts parser's binary operator to tacky representation."
[binop]
(condp = binop
:plus :add
:hyphen :sub
:multiply :mul
:divide :div
:remainder :mod
:equal-to :equal
:not-equal-to :not-equal
:less-than :less-than
:greater-than :greater-than
:less-than-equal-to :less-or-equal
:greater-than-equal-to :greater-or-equal
:ampersand :bit-and
:bitwise-or :bit-or
:bitwise-xor :bit-xor
:bitwise-right-shift :bit-right-shift
:bitwise-left-shift :bit-left-shift
(throw (ex-info "Tacky Error. Invalid binary operator." {binop binop}))))
;;;; Instructions
(defn- unary-instruction [op src dst]
{:type :unary
:unary-operator op
:dst dst
:src src})
(defn- binary-instruction [op src1 src2 dst]
{:type :binary
:binary-operator op
:src1 src1
:src2 src2
:dst dst})
(defn- return-instruction [val]
{:type :return
:val val})
(defn- copy-instruction [src dst]
{:type :copy
:src src
:dst dst})
(defn- jump-instruction [target]
{:type :jump
:identifier target})
(defn- jump-if-zero-instruction [condition target]
{:type :jump-if-zero
:identifier target
:val condition})
(defn- jump-if-not-zero-instruction [condition target]
{:type :jump-if-not-zero
:identifier target
:val condition})
(defn- label-instruction [identifier]
{:type :label
:identifier identifier})
(defn- fun-call-instruction [identifier arguments dst]
{:type :fun-call
:identifier identifier
:arguments arguments
:dst dst})
;;;; Expression handlers
(declare expression-handler)
(defn- constant-expr-handler [e]
{:val (constant (:value e))})
(defn- unary-expr-handler [e]
(let [inner (expression-handler (:value e))
src (:val inner)
op (unary-operator (:unary-operator e))
dst (variable (str "unary_result_" op))
instruction (unary-instruction op 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)
op (binary-operator (:binary-operator e))
dst (variable (str "binary_result_" op))
instruction (binary-instruction op src1 src2 dst)]
{:val dst
:instructions (flatten [(:instructions e1) (:instructions e2) instruction])}))
(defn- logical-and-handler [e]
(let [e1 (expression-handler (:left e))
e2 (expression-handler (:right e))
v1 (:val e1)
v2 (:val e2)
res (variable "and_result")
false-label (label "and_false")
end-label (label "and_end")]
{:val res
:instructions (flatten [(:instructions e1)
(jump-if-zero-instruction v1 false-label)
(:instructions e2)
(jump-if-zero-instruction v2 false-label)
(copy-instruction (constant 1) res)
(jump-instruction end-label)
(label-instruction false-label)
(copy-instruction (constant 0) res)
(label-instruction end-label)])}))
(defn- logical-or-handler [e]
(let [e1 (expression-handler (:left e))
e2 (expression-handler (:right e))
v1 (:val e1)
v2 (:val e2)
res (variable "or_result")
false-label (label "or_false")
end-label (label "or_end")]
{:val res
:instructions (flatten [(:instructions e1)
(jump-if-not-zero-instruction v1 end-label)
(:instructions e2)
(jump-if-not-zero-instruction v2 end-label)
(copy-instruction (constant 0) res)
(jump-instruction false-label)
(label-instruction end-label)
(copy-instruction (constant 1) res)
(label-instruction false-label)])}))
(defn- assignment-exp-handler [e]
(let [asop (:assignment-operator e)
direct-assignment? (= asop :assignment)
var (parsed-var->tacky-var (:left e))] ; guaranteed to be parsed variable
(if direct-assignment?
(let [rhs (expression-handler (:right e))]
{:val var
:instructions (flatten [(:instructions rhs)
(copy-instruction (:val rhs) var)])})
(let [bin-op (assignment-operator->binary-operator (:assignment-operator e))
bin-exp (p/binary-exp-node (:left e) (:right e) bin-op)
rhs (expression-handler bin-exp)]
{:val var
:instructions (flatten [(:instructions rhs)
(copy-instruction (:val rhs) var)])}))))
(defn- conditional-exp-handler [e]
(let [ce (expression-handler (:left e))
cv (:val ce)
then-e (expression-handler (:middle e))
else-e (expression-handler (:right e))
end-label (label "conditional_end")
else-label (label "conditional_else")
res (variable "conditional_result")]
{:val res
:instructions (flatten
[(:instructions ce)
(jump-if-zero-instruction cv else-label)
(:instructions then-e)
(copy-instruction (:val then-e) res)
(jump-instruction end-label)
(label-instruction else-label)
(:instructions else-e)
(copy-instruction (:val else-e) res)
(label-instruction end-label)])}))
(defn- function-call-exp-handler [{identifier :identifier arguments :arguments}]
(let [arg-exps (mapv expression-handler arguments)
dst (variable (str "function_call_result_" identifier))
fn-instruction (fun-call-instruction identifier (mapv #(:val %) arg-exps) dst)]
{:val dst
:instructions (flatten [(mapv #(:instructions %) arg-exps) fn-instruction])}))
(defn- expression-handler [e]
(when-let [exp-type (:exp-type e)]
(condp = exp-type
:constant-exp (constant-expr-handler e)
:unary-exp (unary-expr-handler e)
:binary-exp (let [op (:binary-operator e)]
(condp = op
:logical-and (logical-and-handler e)
:logical-or (logical-or-handler e)
(binary-expr-handler e)))
:variable-exp {:val (parsed-var->tacky-var e)}
:assignment-exp (assignment-exp-handler e)
:conditional-exp (conditional-exp-handler e)
:function-call-exp (function-call-exp-handler e)
(throw (ex-info "Tacky error. Invalid expression." {e e})))))
(defn- exp-instructions [exp]
(expression-handler exp))
(declare statement->tacky-instruction block-item->tacky-instruction)
(defn if-statement-handler [s]
(let [cond-exp (exp-instructions (:condition s))
cond-value (:val cond-exp)
cond-instructions (:instructions cond-exp)
then-instructions (statement->tacky-instruction (:then-statement s))
end-label (label "if_end")
else-label (label "if_else")
else? (:else-statement s)]
(if else?
[cond-instructions
(jump-if-zero-instruction cond-value else-label)
then-instructions
(jump-instruction end-label)
(label-instruction else-label)
(statement->tacky-instruction (:else-statement s))
(label-instruction end-label)]
[cond-instructions
(jump-if-zero-instruction cond-value end-label)
then-instructions
(label-instruction end-label)])))
(defn- compound-statement-handler [s]
(flatten (mapv block-item->tacky-instruction (:block s))))
(defn- break-statement-handler [s]
[(jump-instruction (str "break_" (:label s)))])
(defn- continue-statement-handler [s]
[(jump-instruction (str "continue_" (:label s)))])
(defn- while-statement-handler [s]
(let [continue-label (str "continue_" (:label s))
break-label (str "break_" (:label s))
cond-exp (exp-instructions (:condition s))
cond-value (:val cond-exp)
cond-instructions (:instructions cond-exp)
body-instructions (statement->tacky-instruction (:body s))]
(flatten [(label-instruction continue-label)
cond-instructions
(jump-if-zero-instruction cond-value break-label)
body-instructions
(jump-instruction continue-label)
(label-instruction break-label)])))
(defn- do-while-statement-handler [s]
(let [start-label (label "do_while_start")
continue-label (str "continue_" (:label s))
break-label (str "break_" (:label s))
cond-exp (exp-instructions (:condition s))
cond-value (:val cond-exp)
cond-instructions (:instructions cond-exp)
body-instructions (statement->tacky-instruction (:body s))]
(flatten [(label-instruction start-label)
body-instructions
(label-instruction continue-label)
cond-instructions
(jump-if-not-zero-instruction cond-value start-label)
(label-instruction break-label)])))
(defn- for-statement-handler [s]
(let [init-instructions (if (= :declaration (:type (:init s)))
(block-item->tacky-instruction (:init s))
(:instructions (exp-instructions (:init s))))
start-label (label "for_start")
break-label (str "break_" (:label s))
continue-label (str "continue_" (:label s))
cond? (not (nil? (:condition s)))
body-instructions (statement->tacky-instruction (:body s))
post-instructions (if (nil? (:post s))
[]
(:instructions (exp-instructions (:post s))))
cond-instructions (if cond?
(let [ce (exp-instructions (:condition s))
ce-inst (:instructions ce)
ce-v (:val ce)]
[ce-inst
(jump-if-zero-instruction ce-v break-label)])
[])]
(flatten
[init-instructions
(label-instruction start-label)
cond-instructions
body-instructions
(label-instruction continue-label)
post-instructions
(jump-instruction start-label)
(label-instruction break-label)])))
(defn- statement->tacky-instruction [s]
(condp = (:statement-type s)
:return (let [e (exp-instructions (:value s))
val (:val e)
instructions (:instructions e)]
(conj (vec instructions) (return-instruction val)))
:expression [(:instructions (exp-instructions (:value s)))]
:if (if-statement-handler s)
:compound (compound-statement-handler s)
:break (break-statement-handler s)
:continue (continue-statement-handler s)
:for (for-statement-handler s)
:while (while-statement-handler s)
:do-while (do-while-statement-handler s)
:empty []
(throw (ex-info "Tacky error. Invalid statement." {:statement s}))))
(defn- declaration->tacky-instruction [d]
(when (:initial d)
(let [local? (nil? (:storage-class d))
var (parsed-var->tacky-var d) ; only needs :identifier key in declaration
rhs (exp-instructions (:initial d))]
(if local?
(flatten [(:instructions rhs) (copy-instruction (:val rhs) var)])
[])))) ; ignoring initializers for non local variable declarations
(defn- block-item->tacky-instruction [item]
(condp = (:type item)
:statement (statement->tacky-instruction item)
:declaration (declaration->tacky-instruction item)
(throw (ex-info "Tacky error. Invalid block item." {:item item}))))
(defn- function-definition->tacky-function [function-definition ident->symbol]
(let [add-return (fn [xs] (conj (vec xs) (return-instruction (constant 0))))
instructions (->> function-definition
:body
(remove nil?)
(mapv block-item->tacky-instruction)
flatten
(remove nil?)
add-return)]
(-> function-definition
(dissoc :body)
(assoc :global? (get-in ident->symbol [(:identifier function-definition)
:attrs
:global?]))
(assoc :instructions instructions))))
(defn- tacky-static-variable [identifier global? initial-value]
{:identifier identifier
:global? global?
:initial-value initial-value
:type :declaration
:declaration-type :static-variable})
(defn- tacky-static-variable-instructions [ident->symbols]
(reduce
(fn [acc [k v]]
(if (string? k)
(if (= :static (get-in v [:attrs :type]))
(condp = (get-in v [:attrs :initial-value :type])
:initial (conj acc (tacky-static-variable k (get-in v [:attrs :global?]) (get-in v [:attrs :initial-value :value])))
:tentative (conj acc (tacky-static-variable k (get-in v [:attrs :global?]) 0))
acc)
acc)
acc))
[]
ident->symbols))
(defn- tacky-function-instructions [ast ident->symbol]
(let [fn-defined? (fn [x] (if (= :function (:declaration-type x))
(or (= (:identifier x) "main") (seq (:body x)))
true))]
(->> ast
(filterv #(= :function (:declaration-type %)))
(filterv fn-defined?)
(mapv #(function-definition->tacky-function % ident->symbol)))))
(defn tacky-generate [{ast :block ident->symbol :ident->symbol}]
(let [variable-instructions (tacky-static-variable-instructions ident->symbol)
function-instructions (tacky-function-instructions ast ident->symbol)]
{:program (concat variable-instructions function-instructions)
:ident->symbol ident->symbol}))
(defn tacky-from-src [src]
(-> src
l/lex
p/parse
a/validate
tacky-generate))
(comment
(tacky-from-src
"
extern int foo;
int foo;
int foo;
int main(void) {
for (int i = 0; i < 5; i = i + 1)
foo = foo + 1;
return foo;
}
int foo;
")
())
|