Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /trunk/src/compiler/codegen/clang.sml
ViewVC logotype

Annotation of /trunk/src/compiler/codegen/clang.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 1115 (* clang.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 1115 * All rights reserved.
7 :     *
8 :     * An tree representation of programs in a C-like language (e.g., C, CUDA,
9 :     * or OpenCL). The purpose of this code is to commonality between the various
10 :     * backends, which are all generating C-like code.
11 :     *)
12 :    
13 :     structure CLang =
14 :     struct
15 :    
16 :     type var = string
17 : jhr 1368 type attr = string (* e.g., "static", "kernel", etc ... *)
18 : jhr 1115
19 :     datatype ty
20 :     = T_Num of RawTypes.ty
21 :     | T_Ptr of ty
22 :     | T_Array of ty * int option
23 :     | T_Named of string
24 : jhr 1444 | T_Qual of attr * ty (* qualified type *)
25 : jhr 1115
26 : jhr 1368 datatype typed_var = V of ty * var
27 :    
28 : jhr 1115 val voidTy = T_Named "void"
29 : jhr 2356 val voidPtr = T_Ptr voidTy
30 : jhr 1301 val charTy = T_Named "char"
31 : jhr 1368 val boolTy = T_Named "bool"
32 : jhr 2356 val charPtr = T_Ptr charTy
33 :     val charArrayPtr = T_Ptr charPtr
34 : jhr 1232 val intTy = T_Named "int"
35 : jhr 1766 val int8 = T_Num(RawTypes.RT_Int8)
36 :     val uint8 = T_Num(RawTypes.RT_UInt8)
37 : jhr 1115 val int32 = T_Num(RawTypes.RT_Int32)
38 :     val uint32 = T_Num(RawTypes.RT_UInt32)
39 :     val int64 = T_Num(RawTypes.RT_Int64)
40 :     val float = T_Num(RawTypes.RT_Float)
41 :     val double = T_Num(RawTypes.RT_Double)
42 :    
43 :     datatype decl
44 : jhr 2356 = D_Pragma of string list
45 :     | D_Comment of string list
46 : jhr 1115 (* verbatim text (e.g., preprocessor directives) *)
47 :     | D_Verbatim of string list
48 :     (* global variable declaration *)
49 :     | D_Var of attr list * ty * var * initializer option
50 : jhr 1766 (* function prototype *)
51 :     | D_Proto of attr list * ty * string * param list
52 : jhr 1115 (* function definition *)
53 :     | D_Func of attr list * ty * string * param list * stm
54 : jhr 1766 (* struct type declaration; if the second argument is SOME name, then a
55 :     * typedef is generated.
56 :     *)
57 :     | D_StructDef of string option * (ty * string) list * string option
58 : jhr 1115
59 :     and initializer
60 :     = I_Exp of exp
61 :     | I_Struct of (string * initializer) list
62 :     | I_Array of (int * initializer) list
63 :    
64 :     and param = PARAM of attr list * ty * var
65 :    
66 :     and stm
67 : jhr 1368 = S_Block of stm list (* "{" stms "}" *)
68 : jhr 1115 | S_Comment of string list
69 : jhr 2356 | S_Verbatim of string list
70 : jhr 1368 | S_Decl of attr list * ty * var * initializer option
71 :     (* ty var [ '=' exp ]';' *)
72 :     | S_Exp of exp (* exp ';' *)
73 :     | S_If of exp * stm * stm (* 'if' exp stm 'else' stm *)
74 :     | S_While of exp * stm (* 'while' exp stm *)
75 : jhr 1444 | S_DoWhile of stm * exp (* 'do' stm 'while' exp *)
76 : jhr 1115 | S_For of (ty * var * exp) list * exp * exp list * stm
77 : jhr 1368 (* 'for' '(' inits ';' exp ';' incrs ')' stm *)
78 :     | S_Call of string * exp list (* func '(' args ')' *)
79 :     | S_Return of exp option (* 'return' [ exp ] ';' *)
80 :     | S_Break (* 'break' ';' *)
81 :     | S_Continue (* 'continue' ';' *)
82 : jhr 1115
83 :     and exp
84 : jhr 1368 = E_Grp of exp (* "(" e ")" *)
85 : jhr 1115 | E_AssignOp of exp * assignop * exp (* lvalue op= e *)
86 : jhr 1640 | E_Cond of exp * exp * exp (* e "?" e ":" e *)
87 : jhr 1368 | E_BinOp of exp * binop * exp (* e op e *)
88 :     | E_UnOp of unop * exp (* op e *)
89 :     | E_PostOp of exp * postfix (* e op *)
90 :     | E_Apply of string * exp list (* f "(" ... ")" *)
91 :     | E_Subscript of exp * exp (* e "[" e "]" *)
92 :     | E_Select of exp * string (* e "." f *)
93 :     | E_Indirect of exp * string (* e "->" f *)
94 :     | E_Cast of ty * exp (* "(" ty ")" e *)
95 : jhr 1115 | E_Var of var
96 :     | E_Int of IntegerLit.integer * ty
97 :     | E_Flt of FloatLit.float * ty
98 :     | E_Bool of bool
99 :     | E_Str of string
100 : jhr 1368 | E_Sizeof of ty (* "sizeof(" ty ")" *)
101 : jhr 1115
102 :     (* assignment operators *)
103 :     and assignop
104 :     = $= | += | *= | /= | %= | <<= | >>= | &= | ^= | |=
105 :    
106 :     (* binary operators in increasing order of precedence *)
107 :     and binop
108 :     = #||
109 :     | #&&
110 : jhr 1444 | #|
111 :     | #^
112 :     | #&
113 : jhr 1115 | #== | #!=
114 :     | #< | #<= | #>= | #>
115 :     | #<< | #>>
116 :     | #+ | #-
117 :     | #* | #/ | #%
118 :    
119 :     and unop = %- | %! | %& | %* | %~ | %++ | %--
120 :    
121 :     and postfix = ^++ | ^--
122 :    
123 :     (* smart constructors that add E_Grp wrappers based on operator precedence *)
124 :     local
125 : jhr 1368 val commaP = 0
126 :     val assignP = 1
127 :     val condP = 2
128 :     val lorP = 3
129 :     val landP = 4
130 :     val borP = 5
131 : jhr 1444 val bxorP = 6
132 :     val bandP = 7
133 :     val eqP = 8
134 :     val relP = 9
135 :     val shiftP = 10
136 :     val addP = 11
137 :     val mulP = 12
138 :     val castP = 13
139 :     val unaryP = 14
140 :     val preP = 15
141 :     val compundP = 16 (* compound literal *)
142 :     val postP = 17
143 :     val callP = 18
144 :     val subP = 19
145 :     val atomP = 20
146 : jhr 1115 fun precOfBinop rator = (case rator
147 : jhr 1368 of #|| => lorP
148 :     | #&& => landP
149 : jhr 1444 | #| => borP
150 :     | #^ => bxorP
151 :     | #& => bandP
152 : jhr 1368 | #== => eqP | #!= => eqP
153 :     | #< => relP | #<= => relP | #>= => relP | #> => relP
154 :     | #<< => shiftP | #>> => shiftP
155 :     | #+ => addP | #- => addP
156 :     | #* => mulP | #/ => mulP | #% => mulP
157 :     (* end case *))
158 : jhr 1115 fun prec (E_Grp _) = atomP
159 : jhr 1368 | prec (E_AssignOp _) = assignP
160 : jhr 1640 | prec (E_Cond _) = condP
161 : jhr 1368 | prec (E_BinOp(_, rator, _)) = precOfBinop rator
162 :     | prec (E_UnOp _) = preP
163 :     | prec (E_PostOp _) = postP
164 :     | prec (E_Apply _) = callP
165 :     | prec (E_Subscript _) = postP
166 :     | prec (E_Select _) = postP
167 :     | prec (E_Indirect _) = postP
168 : jhr 1444 | prec (E_Cast _) = castP
169 : jhr 1368 | prec (E_Var _) = atomP
170 :     | prec (E_Int _) = atomP
171 :     | prec (E_Flt _) = atomP
172 :     | prec (E_Bool _) = atomP
173 :     | prec (E_Str _) = atomP
174 :     | prec (E_Sizeof _) = callP
175 : jhr 1115 in
176 :     fun mkGrp e = if (prec e < atomP) then E_Grp e else e
177 :     fun mkAssignOp (e1, rator, e2) = let
178 : jhr 1368 val e1' = if prec e1 < unaryP then E_Grp e1 else e1
179 :     val e2' = if prec e2 < assignP then E_Grp e2 else e2
180 :     in
181 :     E_AssignOp(e1', rator, e2')
182 :     end
183 : jhr 1640 (* note that we over-parenthesize here, but it makes nested conditionals easeier to read *)
184 :     fun mkCond (e1, e2, e3) = E_Cond(
185 :     if prec e1 <= condP then E_Grp e1 else e1,
186 :     if prec e2 <= condP then E_Grp e2 else e2,
187 :     if prec e3 < condP then E_Grp e3 else e3)
188 : jhr 1115 (* Note that all C binary operators are left associative. *)
189 :     fun mkBinOp (e1, #-, e2 as E_UnOp(%-, _)) = let
190 : jhr 1368 val e1' = if prec e1 < addP then E_Grp e1 else e1
191 :     val e2' = E_Grp e2
192 :     in
193 :     E_BinOp(e1', #-, e2')
194 :     end
195 : jhr 1115 | mkBinOp (e1, rator, e2) = let
196 : jhr 1368 val p = precOfBinop rator
197 :     val e1' = if prec e1 < p then E_Grp e1 else e1
198 :     val e2' = if prec e2 <= p then E_Grp e2 else e2
199 :     in
200 :     E_BinOp(e1', rator, e2')
201 :     end
202 : jhr 1115 fun mkUnOp (%-, e as E_UnOp(%-, _)) = E_UnOp(%-, E_Grp e)
203 :     | mkUnOp (%-, e as E_UnOp(%--, _)) = E_UnOp(%-, E_Grp e)
204 :     | mkUnOp (%--, e as E_UnOp(%-, _)) = E_UnOp(%--, E_Grp e)
205 :     | mkUnOp (%--, e as E_UnOp(%--, _)) = E_UnOp(%--, E_Grp e)
206 : jhr 2356 | mkUnOp (%&, E_UnOp(%*, e)) = e
207 :     | mkUnOp (%*, E_UnOp(%&, e)) = e
208 : jhr 1115 | mkUnOp (rator, e) = if prec e < unaryP
209 : jhr 1368 then E_UnOp(rator, E_Grp e)
210 :     else E_UnOp(rator, e)
211 : jhr 1115 fun mkPostOp (e, rator) = if prec e < postP
212 : jhr 1368 then E_PostOp(E_Grp e, rator)
213 :     else E_PostOp(e, rator)
214 : jhr 1115 fun mkApply (f, args) = E_Apply(f, args)
215 :     fun mkSubscript(e1, e2) = if prec e1 < postP
216 : jhr 1368 then E_Subscript(E_Grp e1, e2)
217 :     else E_Subscript(e1, e2)
218 : jhr 1115 fun mkSelect (e, f) = if prec e < postP
219 : jhr 1368 then E_Select(E_Grp e, f)
220 :     else E_Select(e, f)
221 : jhr 1115 fun mkIndirect (e, f) = if prec e < postP
222 : jhr 1368 then E_Indirect(E_Grp e, f)
223 :     else E_Indirect(e, f)
224 : jhr 1444 fun mkCast (ty, e) = if prec e < castP
225 :     then E_Cast(ty, E_Grp e)
226 :     else E_Cast(ty, e)
227 : jhr 1115 val mkVar = E_Var
228 : jhr 1368 fun mkIntTy (n, ty) = if n < 0 then E_UnOp(%-, E_Int(~n, ty)) else E_Int(n, ty)
229 :     fun mkInt n = mkIntTy(n, intTy)
230 : jhr 1115 fun mkFlt (f, ty) = if FloatLit.isNeg f
231 : jhr 1368 then E_UnOp(%-, E_Flt(FloatLit.negate f, ty))
232 :     else E_Flt(f, ty)
233 : jhr 1115 val mkBool = E_Bool
234 :     val mkStr = E_Str
235 :     val mkSizeof = E_Sizeof
236 :     end (* local *)
237 :    
238 :     val skip = S_Block[]
239 :    
240 :     local
241 :     fun paren (e as E_Grp _) = e
242 : jhr 1368 | paren e = E_Grp e
243 : jhr 1115 in
244 :     val mkComment = S_Comment
245 :     fun mkBlock [stm] = stm
246 :     | mkBlock stms = S_Block stms
247 : jhr 2356 fun unBlock (S_Block stms) = stms
248 :     | unBlock stm = [stm]
249 :     fun prependStm (stm, blk) = mkBlock(stm :: unBlock blk)
250 :     fun appendStm (blk, stm) = mkBlock(unBlock blk @ [stm])
251 : jhr 1368 fun mkDecl (ty, x, init) = S_Decl([], ty, x, init)
252 : jhr 1766 fun mkDeclInit (ty, x, init) = S_Decl([], ty, x, SOME(I_Exp init))
253 : jhr 1368 val mkAttrDecl = S_Decl
254 : jhr 1115 val mkExpStm = S_Exp
255 :     fun mkAssign (e1, e2) = S_Exp(mkAssignOp(e1, $=, e2))
256 :     fun mkIfThenElse (e, b1, b2) = S_If(paren e, b1, b2)
257 :     fun mkIfThen (e, b) = mkIfThenElse (e, b, skip)
258 :     val mkFor = S_For
259 :     fun mkWhile (e, b) = S_While(paren e, b)
260 : jhr 1444 fun mkDoWhile (b, e) = S_DoWhile(b, paren e)
261 : jhr 1115 val mkCall = S_Call
262 :     val mkReturn = S_Return
263 :     val mkBreak = S_Break
264 :     val mkContinue = S_Continue
265 :     end (* local *)
266 :    
267 :     (* utility functions *)
268 :    
269 :     fun varToString x = x
270 :    
271 :     fun assignopToString rator = (case rator
272 : jhr 1368 of $= => "="
273 :     | += => "+="
274 :     | *= => "*="
275 :     | /= => "/="
276 :     | %= => "%="
277 :     | <<= => "<<="
278 :     | >>= => ">>="
279 :     | &= => "&="
280 :     | ^= => "^="
281 :     | |= => "|="
282 :     (* end case *))
283 : jhr 1115
284 :     fun binopToString rator = (case rator
285 : jhr 1368 of #|| => "||"
286 :     | #&& => "&&"
287 :     | #== => "=="
288 : jhr 1444 | #| => "|"
289 :     | #^ => "^"
290 :     | #& => "&"
291 : jhr 1368 | #!= => "!="
292 :     | #< => "<"
293 :     | #<= => "<="
294 :     | #>= => ">="
295 :     | #> => ">"
296 :     | #<< => "<<"
297 :     | #>> => ">>"
298 :     | #+ => "+"
299 :     | #- => "-"
300 :     | #* => "*"
301 :     | #/ => "/"
302 :     | #% => "%"
303 :     (* end case *))
304 : jhr 1115
305 :     fun unopToString rator = (case rator
306 : jhr 1368 of %- => "-"
307 :     | %! => "!"
308 :     | %& => "&"
309 :     | %* => "*"
310 :     | %~ => "~"
311 :     | %++ => "++"
312 :     | %-- => "--"
313 :     (* end case *))
314 : jhr 1115
315 :     fun postopToString rator = (case rator
316 : jhr 1368 of ^++ => "++"
317 :     | ^-- => "--"
318 :     (* end case *))
319 : jhr 1115
320 : jhr 1640 (* generate verbatim text from a template string by substituting for placeholders
321 :     * Placeholders have the syntax @<id>@ and are replaced with the string associated
322 :     * with <id> in the list of substitutions. If <id> is empty, then no substitution
323 :     * is applied, instead the "@@" is replaced by "@".
324 :     *)
325 :     local
326 :     structure SS = Substring
327 : jhr 2356 fun verbatim sl subs = let
328 :     fun scan (start, ss, n, frags) = (case SS.getc ss
329 :     of SOME(#"@", rest) => let
330 :     val frags = SS.slice(start, 0, SOME n) :: frags
331 :     val (expansion, rest) = scanPlaceholder rest
332 :     in
333 :     scan (rest, rest, 0, expansion::frags)
334 :     end
335 :     | SOME(_, rest) => scan (start, rest, n+1, frags)
336 :     | NONE => SS.concat(List.rev(start::frags))
337 :     (* end case *))
338 :     and scanPlaceholder start = let
339 :     fun scan (ss, n) = (case SS.getc ss
340 :     of NONE => raise Fail "incomplete placeholder"
341 :     | SOME(#"@", rest) => (SS.string(SS.slice(start, 0, SOME n)), rest)
342 :     | SOME(_, rest) => scan (rest, n+1)
343 :     (* end case *))
344 :     val (placeholder, rest) = scan (start, 0)
345 :     in
346 :     if (placeholder = "")
347 :     then (SS.full "@", rest)
348 :     else (case List.find (fn (s, _) => (s = placeholder)) subs
349 :     of SOME(_, expansion) => (SS.full expansion, rest)
350 :     | NONE => raise Fail(concat["unknown placeholder @", placeholder, "@"])
351 :     (* end case *))
352 :     end
353 :     fun expand s = let
354 :     val ss = SS.full s
355 :     in
356 :     scan (ss, ss, 0, [])
357 :     end
358 :     in
359 :     List.map expand sl
360 :     end
361 : jhr 1640 in
362 : jhr 2356 fun verbatimDcl sl subs = D_Verbatim(verbatim sl subs)
363 :     fun verbatimStm sl subs = S_Verbatim(verbatim sl subs)
364 : jhr 1640 end (* local *)
365 :    
366 : jhr 1301 (* for debugging (not syntactically correct!) *)
367 : jhr 1115 fun expToString e = let
368 : jhr 1368 fun e2s (e, l) = (case e
369 :     of E_Grp e => "(" :: e2s(e, ")"::l)
370 :     | E_AssignOp(e1, rator, e2) => e2s(e1, assignopToString rator :: e2s(e2, l))
371 : jhr 2356 | E_Cond(e1, e2, e3) => "(" :: e2s(e1, "?" :: e2s(e2, ":" :: e2s (e3, ")" :: l)))
372 : jhr 1368 | E_BinOp(e1, rator, e2) => e2s(e1, binopToString rator :: e2s(e2, l))
373 :     | E_UnOp(rator, e) => unopToString rator :: e2s(e, l)
374 :     | E_PostOp(e, rator) => e2s(e, postopToString rator :: l)
375 :     | E_Apply(f, es) => let
376 :     fun args2s ([], l) = l
377 :     | args2s ([e], l) = e2s(e, l)
378 :     | args2s (e::es, l) = e2s(e, ","::args2s(es, l))
379 :     in
380 :     f :: "(" :: args2s(es, ")"::l)
381 :     end
382 :     | E_Subscript(e1, e2) => e2s(e1, "[" :: e2s(e2, "]"::l))
383 :     | E_Select(e, f) => e2s(e, "." :: f :: l)
384 :     | E_Indirect(e, f) => e2s(e, "->" :: f :: l)
385 : jhr 2356 | E_Cast(ty, e) => "(ty)" :: e2s(e, l) (* FIXME: need tyToString *)
386 : jhr 1368 | E_Var x => x::l
387 :     | E_Int(n, _) => IntegerLit.toString n :: l
388 :     | E_Flt(f, _) => FloatLit.toString f :: l
389 :     | E_Bool b => Bool.toString b :: l
390 :     | E_Str s => concat["\"", String.toCString s, "\""] :: l
391 :     | E_Sizeof ty => "sizeof(ty)" :: l
392 :     (* end case *))
393 :     in
394 :     String.concat(e2s(e, []))
395 :     end
396 : jhr 1115
397 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0