blob: d753473d45ee735c39862295b6f68795eb8d26e5 (
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
|
(ns cljcc.emit
(:require
[cljcc.util :refer [get-os]]
[cljcc.compiler :as c]
[clojure.string :as str]
[cljcc.symbols :as symbols]))
(defn- handle-label [identifier]
(condp = (get-os)
:mac (str "L" identifier)
:linux (str ".L" identifier)
(throw (ex-info "Error in generating label." {}))))
(defn- handle-symbol-name [name]
(if (= :mac (get-os))
(str "_" name)
name))
(defn- handle-current-translation-unit [name]
(if (= :mac (get-os))
(handle-symbol-name name)
(if (contains? @symbols/symbols name)
name
(str name "@PLT"))))
;;;; Operand Emit
(defn- imm-opernad-emit [operand _opts]
(format "$%d" (:value operand)))
(defn- stack-operand-emit [operand _opts]
(format "%d(%%rbp)" (:value operand)))
(defn- data-operand-emit [operand _opts]
(format "%s(%%rip)" (handle-symbol-name (:identifier operand))))
(defn- register-operand [{:keys [register] :as operand} {register-width :register-width :or {register-width :4-byte}}]
(let [register->width->output {:ax {:8-byte "%rax"
:4-byte "%eax"
:1-byte "%al"}
:dx {:8-byte "%rdx"
:4-byte "%edx"
:1-byte "%dl"}
:cx {:8-byte "%rcx"
:4-byte "%ecx"
:1-byte "%cl"}
:di {:8-byte "%rdi"
:4-byte "%edi"
:1-byte "%dil"}
:si {:8-byte "%rsi"
:4-byte "%esi"
:1-byte "%sil"}
:r8 {:8-byte "%r8"
:4-byte "%r8d"
:1-byte "%r8b"}
:r9 {:8-byte "%r9"
:4-byte "%r9d"
:1-byte "%r9b"}
:r10 {:8-byte "%r10"
:4-byte "%r10d"
:1-byte "%r10b"}
:r11 {:8-byte "%r11"
:4-byte "%r11d"
:1-byte "%r11b"}
:cl {:4-byte "%cl"
:1-byte "%cl"}}]
(if-let [out (get-in register->width->output [register register-width])]
out
(throw (AssertionError. (str "Invalid register operand register width " operand " " register-width))))))
(def operand-emitters
"Map of assembly operands to operand emitters."
{:imm #'imm-opernad-emit
:reg #'register-operand
:data #'data-operand-emit
:stack #'stack-operand-emit})
(defn- operand-emit
([operand]
(operand-emit operand {}))
([operand opts]
(if-let [[_ operand-emit-fn] (find operand-emitters (:operand operand))]
(operand-emit-fn operand opts)
(throw (AssertionError. (str "Invalid operand: " operand))))))
;;;; Instruction Emit
(defn- mov-instruction-emit [instruction]
(let [src (operand-emit (:src instruction))
dst (operand-emit (:dst instruction))]
[(format " %s %s, %s" "movl" src dst)]))
(defn- cmp-instruction-emit [instruction]
(let [src (operand-emit (:src instruction))
dst (operand-emit (:dst instruction))]
[(format " %s %s, %s" "cmpl" src dst)]))
(defn- jmp-instruction-emit [instruction]
[(format " jmp %s" (handle-label (:identifier instruction)))])
(defn- jmpcc-instruction-emit [instruction]
(let [cc (name (:cond-code instruction))
label (handle-label (:identifier instruction))]
[(format " j%s %s" cc label)]))
(defn- setcc-instruction-emit [instruction]
(let [cc (name (:cond-code instruction))
operand (operand-emit (:operand instruction) {:register-width :1-byte})]
[(format " set%s %s" cc operand)]))
(defn- label-instruction-emit [instruction]
[(format " %s:" (handle-label (:identifier instruction)))])
(defn- ret-instruction-emit [_instruction]
[" movq %rbp, %rsp"
" popq %rbp"
" ret"])
(defn- unary-instruction-emit [instruction]
(let [operand (operand-emit (:operand instruction))
assembly-operator (condp = (:unary-operator instruction)
:bit-not "notl"
:negate "negl"
(throw (AssertionError. (str "Invalid unary operator: " instruction))))]
[(format " %s %s" assembly-operator operand)]))
(defn- binary-instruction-emit [instruction]
(let [src (operand-emit (:src instruction))
dst (operand-emit (:dst instruction))
binop (:binary-operator instruction)
binop-operator (condp = binop
:add "addl"
:sub "subl"
:mul "imull"
:bit-and "andl"
:bit-xor "xorl"
:bit-or "orl"
:bit-left-shift "sall"
:bit-right-shift "sarl"
(throw (AssertionError. (str "Invalid binary operator: " instruction))))]
[(format " %s %s, %s" binop-operator src dst)]))
(defn- cdq-instruction-emit [_instruction]
[" cdq"])
(defn- idiv-instruction-emit [instruction]
[(format " idivl %s" (operand-emit (:operand instruction)))])
(defn- allocate-stack-instruction-emit [instruction]
[(format " subq $%d, %%rsp" (:value instruction))])
(defn- deallocate-stack-instruction-emit [instruction]
[(format " addq $%d, %%rsp" (:value instruction))])
(defn- push-instruction-emit [instruction]
[(format " pushq %s" (operand-emit (:operand instruction) {:register-width :8-byte}))])
(defn- call-instruction-emit [instruction]
[(format " call %s" (handle-current-translation-unit (:identifier instruction)))])
(def instruction-emitters
"Map of assembly instructions to function emitters."
{:mov #'mov-instruction-emit
:ret #'ret-instruction-emit
:binary #'binary-instruction-emit
:cdq #'cdq-instruction-emit
:idiv #'idiv-instruction-emit
:unary #'unary-instruction-emit
:setcc #'setcc-instruction-emit
:jmp #'jmp-instruction-emit
:jmpcc #'jmpcc-instruction-emit
:label #'label-instruction-emit
:cmp #'cmp-instruction-emit
:allocate-stack #'allocate-stack-instruction-emit
:deallocate-stack #'deallocate-stack-instruction-emit
:push #'push-instruction-emit
:call #'call-instruction-emit})
(defn instruction-emit [instruction]
(if-let [[_ instruction-emit-fn] (find instruction-emitters (:op instruction))]
(instruction-emit-fn instruction)
(throw (AssertionError. (str "Invalid instruction: " instruction)))))
(defn function-definition-emit [{:keys [identifier instructions global?]}]
(let [name (handle-symbol-name identifier)
globl (if global?
(format " .globl %s", name)
"")
name-line (format "%s:" name)
instructions (mapv instruction-emit instructions)]
(->> [globl
" .text"
name-line
" pushq %rbp"
" movq %rsp, %rbp"
instructions
"\n"]
flatten
(filterv not-empty))))
(defn- static-variable-definition-emit [{:keys [identifier global? initial-value]}]
(let [name (handle-symbol-name identifier)
globl (if global?
(format " .globl %s" name)
"")
data-or-bss (if (zero? initial-value)
" .bss"
" .data")
size-val (if (zero? initial-value)
" .zero 4"
(format " .long %d" initial-value))]
(filterv not-empty [globl
data-or-bss
" .balign 4"
(format "%s:" name)
size-val
"\n"])))
(def emitters-top-level
"Map of assembly top level constructs to their emitters."
{:function #'function-definition-emit
:static-variable #'static-variable-definition-emit})
(defn emit-top-level [tacky-ast]
(if-let [[_ emit-fn] (find emitters-top-level (:type tacky-ast))]
(emit-fn tacky-ast)
(throw (AssertionError. (str "Invalid ast: " tacky-ast)))))
(def linux-assembly-end ".section .note.GNU-stack,\"\",@progbits")
(defn emit [top-levels]
(let [handle-os (fn [ast]
(if (= :linux (get-os))
(conj (conj (vec ast) linux-assembly-end))
ast))]
(->> top-levels
(mapv emit-top-level)
concat
flatten
handle-os
(str/join "\n"))))
(comment
(emit
(c/generate-assembly
"int main(void) {
return ~(-(~(-1)));
}"))
())
|