/
stmt.lisp
308 lines (237 loc) · 9.51 KB
/
stmt.lisp
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
;;;; Statement AST types and support code
(in-package #:se.defmacro.c-amplify)
(defmacro %cg-stmt-around (&rest body)
`(let ((*stmt-depth* (1+ *stmt-depth*)))
(generate-code *cg-freshline*)
,@body))
(defun c-type-p (datum)
(typep datum 'c-type))
(defun c-stmt-p (datum)
(typep datum 'c-stmt))
(defun c-stmt-list-p (datum)
(and (listp datum)
(every #'c-stmt-p datum)))
(defmacro %cg-block-around (&rest body)
`(progn
(generate-code *cg-begin-block*)
(let ((*compound-depth* (1+ *compound-depth*)))
,@body)
(generate-code *cg-end-block*)))
(deftype c-stmt-list ()
'(satisfies c-stmt-list-p))
(defclass c-stmt (ast-node) ())
(defclass c-null-stmt (c-stmt) ())
(defmethod ast-children ((self c-null-stmt))
nil)
(defmethod generate-code :around ((self c-stmt))
(let ((*stmt-depth* (1+ *stmt-depth*)))
(generate-code *cg-freshline*)
(call-next-method)))
(defmethod generate-code ((self c-null-stmt))
(when (> *stmt-depth* 0)
(princ ";")))
(defclass c-compound-stmt-base (c-stmt)
((body :type c-stmt-list :initarg :body :accessor c-compound-body))
(:documentation "Superclass for statements that contain other statements."))
(defun %emit-compound-body (compound-stmt)
(%cg-block-around
(generate-code (c-compound-body compound-stmt))))
(defclass c-compound-stmt (c-compound-stmt-base) ())
(defmethod generate-code ((self c-compound-stmt))
(%emit-compound-body self))
(defun inline-compound-bodies (stmt-list outer-env)
(let ((only-declarations-so-far t))
(labels ((compound-declarations (compound) ; => list of c-declarator instances for compound
(loop for stmt in (c-compound-body compound)
while (typep stmt 'c-declare-stmt)
appending (declarators stmt)))
(will-shadow-p (compound) ; => t if any declaration in the compound will shadow outer-env
(some #'(lambda (decl) (%env-lookup (name decl) outer-env))
(compound-declarations compound)))
(mergable-p (compound) ; => t if the sub-compound can be merged
(and (typep compound 'c-compound-stmt)
only-declarations-so-far
(not (will-shadow-p compound))))
;; Try to merge the contents of stmt if it is
(simplify-stmt (stmt)
(simplify stmt)
(cond ((mergable-p stmt) (copy-list (c-compound-body stmt)))
((typep stmt 'c-declare-stmt) (setf outer-env (ast-env stmt)) (list stmt))
(t (setf only-declarations-so-far nil)
(list stmt)))))
(if (some #'(lambda (x) (typep x 'c-compound-stmt)) stmt-list)
(loop for stmt in stmt-list appending (simplify-stmt stmt))
stmt-list))))
(defmethod simplify ((self c-compound-stmt))
(with-slots (body) self
(setf body (inline-compound-bodies body (ast-env self))))
self)
(defclass c-break-stmt (c-stmt) ())
(defmethod generate-code ((self c-break-stmt))
(generate-code "break;"))
(defclass c-continue-stmt (c-stmt) ())
(defmethod generate-code ((self c-continue-stmt))
(generate-code "continue;"))
(defclass c-do-while-stmt (c-stmt)
((test-expr :initarg :test-expr :accessor test-expr :type c-expr)
(stmt :initarg :stmt :accessor stmt :type c-stmt)))
(defmethod generate-code ((self c-do-while-stmt))
(with-slots (test-expr stmt) self
(generate-code* "do" stmt "while (" test-expr ");")))
(defclass c-expr-stmt (c-stmt)
((expr :type c-expr :initarg :expr :accessor expr)))
(defmethod generate-code ((self c-expr-stmt))
(with-slots (expr) self
(%cg-stmt-around
(generate-code* expr ";"))))
(defclass c-for-stmt (c-compound-stmt-base)
((init-expr :type c-expr :initarg :init-expr :accessor init-expr)
(test-expr :type c-expr :initarg :test-expr :accessor test-expr)
(iter-expr :type c-expr :initarg :iter-expr :accessor iter-expr)))
(defmethod generate-code ((self c-for-stmt))
(with-slots (init-expr test-expr iter-expr) self
(%cg-stmt-around
(generate-code* "for (" init-expr "; " test-expr "; " iter-expr ")")
(%emit-compound-body self))))
(defclass c-goto-stmt (c-stmt)
((label :type symbol :initarg :label :accessor label)))
(defmethod generate-code ((self c-goto-stmt))
(with-slots (label) self
(%cg-stmt-around
(generate-code* "goto " label ";"))))
(defmethod ast-children ((self c-goto-stmt))
nil)
(defclass c-label-stmt (c-stmt)
((label :type symbol :initarg :label :accessor label)))
(defmethod generate-code ((self c-label-stmt))
(with-slots (label) self
(%cg-stmt-around
(generate-code* *cg-freshline* label ":"))))
(defmethod ast-children ((self c-label-stmt))
nil)
(defclass c-if-stmt (c-stmt)
((test-expr :type c-expr :initarg :test-expr :accessor test-expr)
(true-stmt :type c-stmt :initarg :true-stmt :accessor true-stmt)
(false-stmt :type (or c-stmt null) :initform nil :initarg :false-stmt :accessor false-stmt)))
(defmethod generate-code ((self c-if-stmt))
(with-slots (test-expr true-stmt false-stmt) self
(%cg-stmt-around
(generate-code* "if (" test-expr ")" true-stmt)
(when false-stmt
(generate-code* "else" false-stmt)))))
(defclass c-return-stmt (c-stmt)
((expr :type (or c-expr null) :initarg :expr :initform nil :accessor expr)))
(defmethod generate-code ((self c-return-stmt))
(with-slots (expr) self
(%cg-stmt-around
(generate-code "return")
(when expr
(generate-code* " " expr))
(generate-code ";"))))
(defclass c-declarator (ast-node)
((name :type symbol :initarg :name :accessor name)
(decl-type :type c-type :initarg :decl-type :accessor decl-type)
(init-expr :type (or c-expr null) :initarg :init-expr :initform nil :accessor init-expr)))
(defmethod generate-code ((self c-declarator))
(with-slots (name decl-type init-expr) self
;; FIXME: need a (emit-declaration ...) to make function types
;; work, because there the type surrounds the identifier
(%cg-stmt-around
(generate-code (c-type->string decl-type name))
(when init-expr
(generate-code* " = " init-expr)))))
(defclass c-declare-stmt (c-stmt)
((declarators :type list :initarg :declarators :accessor declarators)))
(defmethod generate-code ((self c-declare-stmt))
(with-slots (declarators) self
(dolist (d declarators)
(generate-code* d ";"))))
(defclass c-defstruct-node (ast-node)
((struct-type :type c-structured-type :initarg :struct-type :accessor struct-type)))
(defmethod generate-code ((self c-defstruct-node))
(with-slots (struct-type) self
(with-slots (fields kind name) struct-type
(generate-code* *cg-toplevel-spacing* (format nil "~(~a~)" kind) " " name)
(%cg-block-around
(dolist (f fields)
(generate-code* *cg-freshline* (c-type->string (cdr f) (car f)) ";")))
(generate-code ";"))))
(defclass c-defun-node (c-compound-stmt-base)
((gv :type gval :initarg :gval)
(formals :type list :initarg :formals)))
(defmethod generate-code ((self c-defun-node))
(with-slots (gv body formals) self
(assert (eq (gval-kind gv) :defun))
(let* ((symbol (gval-sym gv))
(ftype (gval-type gv))
(return-type (return-type ftype))
(argument-types (argument-types ftype))
(variadic-p (variadic-p ftype)))
(generate-code *cg-toplevel-spacing*)
(generate-code return-type)
(generate-code *cg-defun-return-type-separator*)
(generate-code* symbol "(")
(loop
for formal in formals
for argtype in argument-types
for comma-required-p = nil then t
do (progn
(when comma-required-p
(generate-code ", "))
(generate-code* argtype " " formal)))
(when variadic-p
(when (not (null formals))
(generate-code ", "))
(generate-code "..."))
(generate-code ")")
(%cg-block-around
(generate-code body)))))
(defmethod simplify ((self c-defun-node))
(with-slots (body) self
(setf body (inline-compound-bodies body (ast-env self))))
self)
(defclass c-switch-case (c-compound-stmt-base)
((test-expr :type c-expr :initarg :test-expr :accessor test-expr)))
(defmethod generate-code ((self c-switch-case))
(generate-code* *cg-freshline* "case " (test-expr self) ":")
(%emit-compound-body self))
(defmethod simplify ((self c-switch-case)) self)
(defmethod ast-children ((self c-switch-case))
(cons (test-expr self) (c-compound-body self)))
(defclass c-switch-default (c-compound-stmt-base) ())
(defmethod generate-code ((self c-switch-default))
(generate-code* *cg-freshline* "default:")
(%emit-compound-body self))
(defmethod simplify ((self c-switch-default)) self)
(defmethod ast-children ((self c-switch-default))
(c-compound-body self))
(defclass c-switch-stmt (c-compound-stmt-base)
((test-expr :type c-expr :initarg :test-expr :accessor test-expr)
(cases :initarg :cases :accessor cases)))
(defmethod generate-code ((self c-switch-stmt))
(generate-code* "switch (" (test-expr self) ")")
(%cg-block-around
(generate-code (cases self))))
(defmethod simplify ((self c-switch-stmt)) self)
(defmethod ast-children ((self c-switch-stmt))
(with-slots (test-expr) self
(cons test-expr (c-compound-body self))))
(defmethod ast-children ((ast c-compound-stmt-base))
(with-slots (body) ast
body))
(defmethod ast-children ((ast c-defstruct-node)) nil)
(defmethod ast-children ((ast c-if-stmt))
(with-slots (test-expr true-stmt false-stmt) ast
(if false-stmt
(list test-expr true-stmt false-stmt)
(list test-expr true-stmt))))
(defmethod ast-children ((ast c-expr))
nil)
(defmethod ast-children ((ast c-declare-stmt))
(loop for decl in (declarators ast)
when (init-expr decl)
collect it))
(defmethod ast-children ((ast c-return-stmt))
(list (expr ast)))
(defmethod ast-children ((ast c-expr-stmt))
(list (expr ast)))